1 //===- DeclTemplate.cpp - Template Declaration AST Node Implementation ----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the C++ related Decl classes for templates. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/AST/DeclTemplate.h" 14 #include "clang/AST/ASTContext.h" 15 #include "clang/AST/ASTMutationListener.h" 16 #include "clang/AST/DeclCXX.h" 17 #include "clang/AST/DeclarationName.h" 18 #include "clang/AST/Expr.h" 19 #include "clang/AST/ExprCXX.h" 20 #include "clang/AST/ExternalASTSource.h" 21 #include "clang/AST/ODRHash.h" 22 #include "clang/AST/TemplateBase.h" 23 #include "clang/AST/TemplateName.h" 24 #include "clang/AST/Type.h" 25 #include "clang/AST/TypeLoc.h" 26 #include "clang/Basic/Builtins.h" 27 #include "clang/Basic/LLVM.h" 28 #include "clang/Basic/SourceLocation.h" 29 #include "llvm/ADT/ArrayRef.h" 30 #include "llvm/ADT/FoldingSet.h" 31 #include "llvm/ADT/PointerUnion.h" 32 #include "llvm/ADT/STLExtras.h" 33 #include "llvm/ADT/SmallVector.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include <cassert> 36 #include <memory> 37 #include <optional> 38 #include <utility> 39 40 using namespace clang; 41 42 //===----------------------------------------------------------------------===// 43 // TemplateParameterList Implementation 44 //===----------------------------------------------------------------------===// 45 46 template <class TemplateParam> 47 static bool 48 DefaultTemplateArgumentContainsUnexpandedPack(const TemplateParam &P) { 49 return P.hasDefaultArgument() && 50 P.getDefaultArgument().getArgument().containsUnexpandedParameterPack(); 51 } 52 53 TemplateParameterList::TemplateParameterList(const ASTContext &C, 54 SourceLocation TemplateLoc, 55 SourceLocation LAngleLoc, 56 ArrayRef<NamedDecl *> Params, 57 SourceLocation RAngleLoc, 58 Expr *RequiresClause) 59 : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc), 60 NumParams(Params.size()), ContainsUnexpandedParameterPack(false), 61 HasRequiresClause(RequiresClause != nullptr), 62 HasConstrainedParameters(false) { 63 for (unsigned Idx = 0; Idx < NumParams; ++Idx) { 64 NamedDecl *P = Params[Idx]; 65 begin()[Idx] = P; 66 67 bool IsPack = P->isTemplateParameterPack(); 68 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { 69 if (!IsPack && (NTTP->getType()->containsUnexpandedParameterPack() || 70 DefaultTemplateArgumentContainsUnexpandedPack(*NTTP))) 71 ContainsUnexpandedParameterPack = true; 72 if (NTTP->hasPlaceholderTypeConstraint()) 73 HasConstrainedParameters = true; 74 } else if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) { 75 if (!IsPack && 76 (TTP->getTemplateParameters()->containsUnexpandedParameterPack() || 77 DefaultTemplateArgumentContainsUnexpandedPack(*TTP))) { 78 ContainsUnexpandedParameterPack = true; 79 } 80 } else if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) { 81 if (!IsPack && DefaultTemplateArgumentContainsUnexpandedPack(*TTP)) { 82 ContainsUnexpandedParameterPack = true; 83 } else if (const TypeConstraint *TC = TTP->getTypeConstraint(); 84 TC && TC->getImmediatelyDeclaredConstraint() 85 ->containsUnexpandedParameterPack()) { 86 ContainsUnexpandedParameterPack = true; 87 } 88 if (TTP->hasTypeConstraint()) 89 HasConstrainedParameters = true; 90 } else { 91 llvm_unreachable("unexpected template parameter type"); 92 } 93 } 94 95 if (HasRequiresClause) { 96 if (RequiresClause->containsUnexpandedParameterPack()) 97 ContainsUnexpandedParameterPack = true; 98 *getTrailingObjects<Expr *>() = RequiresClause; 99 } 100 } 101 102 bool TemplateParameterList::containsUnexpandedParameterPack() const { 103 if (ContainsUnexpandedParameterPack) 104 return true; 105 if (!HasConstrainedParameters) 106 return false; 107 108 // An implicit constrained parameter might have had a use of an unexpanded 109 // pack added to it after the template parameter list was created. All 110 // implicit parameters are at the end of the parameter list. 111 for (const NamedDecl *Param : llvm::reverse(asArray())) { 112 if (!Param->isImplicit()) 113 break; 114 115 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { 116 const auto *TC = TTP->getTypeConstraint(); 117 if (TC && TC->getImmediatelyDeclaredConstraint() 118 ->containsUnexpandedParameterPack()) 119 return true; 120 } 121 } 122 123 return false; 124 } 125 126 TemplateParameterList * 127 TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc, 128 SourceLocation LAngleLoc, 129 ArrayRef<NamedDecl *> Params, 130 SourceLocation RAngleLoc, Expr *RequiresClause) { 131 void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *, Expr *>( 132 Params.size(), RequiresClause ? 1u : 0u), 133 alignof(TemplateParameterList)); 134 return new (Mem) TemplateParameterList(C, TemplateLoc, LAngleLoc, Params, 135 RAngleLoc, RequiresClause); 136 } 137 138 void TemplateParameterList::Profile(llvm::FoldingSetNodeID &ID, 139 const ASTContext &C) const { 140 const Expr *RC = getRequiresClause(); 141 ID.AddBoolean(RC != nullptr); 142 if (RC) 143 RC->Profile(ID, C, /*Canonical=*/true); 144 ID.AddInteger(size()); 145 for (NamedDecl *D : *this) { 146 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { 147 ID.AddInteger(0); 148 ID.AddBoolean(NTTP->isParameterPack()); 149 NTTP->getType().getCanonicalType().Profile(ID); 150 ID.AddBoolean(NTTP->hasPlaceholderTypeConstraint()); 151 if (const Expr *E = NTTP->getPlaceholderTypeConstraint()) 152 E->Profile(ID, C, /*Canonical=*/true); 153 continue; 154 } 155 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) { 156 ID.AddInteger(1); 157 ID.AddBoolean(TTP->isParameterPack()); 158 ID.AddBoolean(TTP->hasTypeConstraint()); 159 if (const TypeConstraint *TC = TTP->getTypeConstraint()) 160 TC->getImmediatelyDeclaredConstraint()->Profile(ID, C, 161 /*Canonical=*/true); 162 continue; 163 } 164 const auto *TTP = cast<TemplateTemplateParmDecl>(D); 165 ID.AddInteger(2); 166 ID.AddBoolean(TTP->isParameterPack()); 167 TTP->getTemplateParameters()->Profile(ID, C); 168 } 169 } 170 171 unsigned TemplateParameterList::getMinRequiredArguments() const { 172 unsigned NumRequiredArgs = 0; 173 for (const NamedDecl *P : asArray()) { 174 if (P->isTemplateParameterPack()) { 175 if (std::optional<unsigned> Expansions = getExpandedPackSize(P)) { 176 NumRequiredArgs += *Expansions; 177 continue; 178 } 179 break; 180 } 181 182 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) { 183 if (TTP->hasDefaultArgument()) 184 break; 185 } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { 186 if (NTTP->hasDefaultArgument()) 187 break; 188 } else if (cast<TemplateTemplateParmDecl>(P)->hasDefaultArgument()) 189 break; 190 191 ++NumRequiredArgs; 192 } 193 194 return NumRequiredArgs; 195 } 196 197 unsigned TemplateParameterList::getDepth() const { 198 if (size() == 0) 199 return 0; 200 201 const NamedDecl *FirstParm = getParam(0); 202 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(FirstParm)) 203 return TTP->getDepth(); 204 else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(FirstParm)) 205 return NTTP->getDepth(); 206 else 207 return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth(); 208 } 209 210 static bool AdoptTemplateParameterList(TemplateParameterList *Params, 211 DeclContext *Owner) { 212 bool Invalid = false; 213 for (NamedDecl *P : *Params) { 214 P->setDeclContext(Owner); 215 216 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) 217 if (AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner)) 218 Invalid = true; 219 220 if (P->isInvalidDecl()) 221 Invalid = true; 222 } 223 return Invalid; 224 } 225 226 void TemplateParameterList:: 227 getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const { 228 if (HasConstrainedParameters) 229 for (const NamedDecl *Param : *this) { 230 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { 231 if (const auto *TC = TTP->getTypeConstraint()) 232 AC.push_back(TC->getImmediatelyDeclaredConstraint()); 233 } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 234 if (const Expr *E = NTTP->getPlaceholderTypeConstraint()) 235 AC.push_back(E); 236 } 237 } 238 if (HasRequiresClause) 239 AC.push_back(getRequiresClause()); 240 } 241 242 bool TemplateParameterList::hasAssociatedConstraints() const { 243 return HasRequiresClause || HasConstrainedParameters; 244 } 245 246 ArrayRef<TemplateArgument> 247 TemplateParameterList::getInjectedTemplateArgs(const ASTContext &Context) { 248 if (!InjectedArgs) { 249 InjectedArgs = new (Context) TemplateArgument[size()]; 250 llvm::transform(*this, InjectedArgs, [&](NamedDecl *ND) { 251 return Context.getInjectedTemplateArg(ND); 252 }); 253 } 254 return {InjectedArgs, NumParams}; 255 } 256 257 bool TemplateParameterList::shouldIncludeTypeForArgument( 258 const PrintingPolicy &Policy, const TemplateParameterList *TPL, 259 unsigned Idx) { 260 if (!TPL || Idx >= TPL->size() || Policy.AlwaysIncludeTypeForTemplateArgument) 261 return true; 262 const NamedDecl *TemplParam = TPL->getParam(Idx); 263 if (const auto *ParamValueDecl = 264 dyn_cast<NonTypeTemplateParmDecl>(TemplParam)) 265 if (ParamValueDecl->getType()->getContainedDeducedType()) 266 return true; 267 return false; 268 } 269 270 namespace clang { 271 272 void *allocateDefaultArgStorageChain(const ASTContext &C) { 273 return new (C) char[sizeof(void*) * 2]; 274 } 275 276 } // namespace clang 277 278 //===----------------------------------------------------------------------===// 279 // TemplateDecl Implementation 280 //===----------------------------------------------------------------------===// 281 282 TemplateDecl::TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, 283 DeclarationName Name, TemplateParameterList *Params, 284 NamedDecl *Decl) 285 : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl), TemplateParams(Params) {} 286 287 void TemplateDecl::anchor() {} 288 289 void TemplateDecl:: 290 getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const { 291 TemplateParams->getAssociatedConstraints(AC); 292 if (auto *FD = dyn_cast_or_null<FunctionDecl>(getTemplatedDecl())) 293 if (const Expr *TRC = FD->getTrailingRequiresClause()) 294 AC.push_back(TRC); 295 } 296 297 bool TemplateDecl::hasAssociatedConstraints() const { 298 if (TemplateParams->hasAssociatedConstraints()) 299 return true; 300 if (auto *FD = dyn_cast_or_null<FunctionDecl>(getTemplatedDecl())) 301 return FD->getTrailingRequiresClause(); 302 return false; 303 } 304 305 bool TemplateDecl::isTypeAlias() const { 306 switch (getKind()) { 307 case TemplateDecl::TypeAliasTemplate: 308 case TemplateDecl::BuiltinTemplate: 309 return true; 310 default: 311 return false; 312 }; 313 } 314 315 //===----------------------------------------------------------------------===// 316 // RedeclarableTemplateDecl Implementation 317 //===----------------------------------------------------------------------===// 318 319 void RedeclarableTemplateDecl::anchor() {} 320 321 RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const { 322 if (Common) 323 return Common; 324 325 // Walk the previous-declaration chain until we either find a declaration 326 // with a common pointer or we run out of previous declarations. 327 SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls; 328 for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev; 329 Prev = Prev->getPreviousDecl()) { 330 if (Prev->Common) { 331 Common = Prev->Common; 332 break; 333 } 334 335 PrevDecls.push_back(Prev); 336 } 337 338 // If we never found a common pointer, allocate one now. 339 if (!Common) { 340 // FIXME: If any of the declarations is from an AST file, we probably 341 // need an update record to add the common data. 342 343 Common = newCommon(getASTContext()); 344 } 345 346 // Update any previous declarations we saw with the common pointer. 347 for (const RedeclarableTemplateDecl *Prev : PrevDecls) 348 Prev->Common = Common; 349 350 return Common; 351 } 352 353 void RedeclarableTemplateDecl::loadLazySpecializationsImpl( 354 bool OnlyPartial /*=false*/) const { 355 auto *ExternalSource = getASTContext().getExternalSource(); 356 if (!ExternalSource) 357 return; 358 359 ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(), 360 OnlyPartial); 361 return; 362 } 363 364 bool RedeclarableTemplateDecl::loadLazySpecializationsImpl( 365 ArrayRef<TemplateArgument> Args, TemplateParameterList *TPL) const { 366 auto *ExternalSource = getASTContext().getExternalSource(); 367 if (!ExternalSource) 368 return false; 369 370 // If TPL is not null, it implies that we're loading specializations for 371 // partial templates. We need to load all specializations in such cases. 372 if (TPL) 373 return ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(), 374 /*OnlyPartial=*/false); 375 376 return ExternalSource->LoadExternalSpecializations(this->getCanonicalDecl(), 377 Args); 378 } 379 380 template <class EntryType, typename... ProfileArguments> 381 typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType * 382 RedeclarableTemplateDecl::findSpecializationLocally( 383 llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos, 384 ProfileArguments &&...ProfileArgs) { 385 using SETraits = RedeclarableTemplateDecl::SpecEntryTraits<EntryType>; 386 387 llvm::FoldingSetNodeID ID; 388 EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)..., 389 getASTContext()); 390 EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos); 391 return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr; 392 } 393 394 template <class EntryType, typename... ProfileArguments> 395 typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType * 396 RedeclarableTemplateDecl::findSpecializationImpl( 397 llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos, 398 ProfileArguments &&...ProfileArgs) { 399 400 if (auto *Found = findSpecializationLocally( 401 Specs, InsertPos, std::forward<ProfileArguments>(ProfileArgs)...)) 402 return Found; 403 404 if (!loadLazySpecializationsImpl( 405 std::forward<ProfileArguments>(ProfileArgs)...)) 406 return nullptr; 407 408 return findSpecializationLocally( 409 Specs, InsertPos, std::forward<ProfileArguments>(ProfileArgs)...); 410 } 411 412 template<class Derived, class EntryType> 413 void RedeclarableTemplateDecl::addSpecializationImpl( 414 llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry, 415 void *InsertPos) { 416 using SETraits = SpecEntryTraits<EntryType>; 417 418 if (InsertPos) { 419 #ifndef NDEBUG 420 auto Args = SETraits::getTemplateArgs(Entry); 421 // Due to hash collisions, it can happen that we load another template 422 // specialization with the same hash. This is fine, as long as the next 423 // call to findSpecializationImpl does not find a matching Decl for the 424 // template arguments. 425 loadLazySpecializationsImpl(Args); 426 void *CorrectInsertPos; 427 assert(!findSpecializationImpl(Specializations, CorrectInsertPos, Args) && 428 InsertPos == CorrectInsertPos && 429 "given incorrect InsertPos for specialization"); 430 #endif 431 Specializations.InsertNode(Entry, InsertPos); 432 } else { 433 EntryType *Existing = Specializations.GetOrInsertNode(Entry); 434 (void)Existing; 435 assert(SETraits::getDecl(Existing)->isCanonicalDecl() && 436 "non-canonical specialization?"); 437 } 438 439 if (ASTMutationListener *L = getASTMutationListener()) 440 L->AddedCXXTemplateSpecialization(cast<Derived>(this), 441 SETraits::getDecl(Entry)); 442 } 443 444 //===----------------------------------------------------------------------===// 445 // FunctionTemplateDecl Implementation 446 //===----------------------------------------------------------------------===// 447 448 FunctionTemplateDecl * 449 FunctionTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 450 DeclarationName Name, 451 TemplateParameterList *Params, NamedDecl *Decl) { 452 bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl)); 453 auto *TD = new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl); 454 if (Invalid) 455 TD->setInvalidDecl(); 456 return TD; 457 } 458 459 FunctionTemplateDecl * 460 FunctionTemplateDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { 461 return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(), 462 DeclarationName(), nullptr, nullptr); 463 } 464 465 RedeclarableTemplateDecl::CommonBase * 466 FunctionTemplateDecl::newCommon(ASTContext &C) const { 467 auto *CommonPtr = new (C) Common; 468 C.addDestruction(CommonPtr); 469 return CommonPtr; 470 } 471 472 void FunctionTemplateDecl::LoadLazySpecializations() const { 473 loadLazySpecializationsImpl(); 474 } 475 476 llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> & 477 FunctionTemplateDecl::getSpecializations() const { 478 LoadLazySpecializations(); 479 return getCommonPtr()->Specializations; 480 } 481 482 FunctionDecl * 483 FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, 484 void *&InsertPos) { 485 auto *Common = getCommonPtr(); 486 return findSpecializationImpl(Common->Specializations, InsertPos, Args); 487 } 488 489 void FunctionTemplateDecl::addSpecialization( 490 FunctionTemplateSpecializationInfo *Info, void *InsertPos) { 491 auto *Common = getCommonPtr(); 492 addSpecializationImpl<FunctionTemplateDecl>(Common->Specializations, Info, 493 InsertPos); 494 } 495 496 void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) { 497 using Base = RedeclarableTemplateDecl; 498 499 // If we haven't created a common pointer yet, then it can just be created 500 // with the usual method. 501 if (!Base::Common) 502 return; 503 504 Common *ThisCommon = static_cast<Common *>(Base::Common); 505 Common *PrevCommon = nullptr; 506 SmallVector<FunctionTemplateDecl *, 8> PreviousDecls; 507 for (; Prev; Prev = Prev->getPreviousDecl()) { 508 if (Prev->Base::Common) { 509 PrevCommon = static_cast<Common *>(Prev->Base::Common); 510 break; 511 } 512 PreviousDecls.push_back(Prev); 513 } 514 515 // If the previous redecl chain hasn't created a common pointer yet, then just 516 // use this common pointer. 517 if (!PrevCommon) { 518 for (auto *D : PreviousDecls) 519 D->Base::Common = ThisCommon; 520 return; 521 } 522 523 // Ensure we don't leak any important state. 524 assert(ThisCommon->Specializations.size() == 0 && 525 "Can't merge incompatible declarations!"); 526 527 Base::Common = PrevCommon; 528 } 529 530 //===----------------------------------------------------------------------===// 531 // ClassTemplateDecl Implementation 532 //===----------------------------------------------------------------------===// 533 534 ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, DeclContext *DC, 535 SourceLocation L, 536 DeclarationName Name, 537 TemplateParameterList *Params, 538 NamedDecl *Decl) { 539 bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl)); 540 auto *TD = new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl); 541 if (Invalid) 542 TD->setInvalidDecl(); 543 return TD; 544 } 545 546 ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C, 547 GlobalDeclID ID) { 548 return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(), 549 DeclarationName(), nullptr, nullptr); 550 } 551 552 void ClassTemplateDecl::LoadLazySpecializations( 553 bool OnlyPartial /*=false*/) const { 554 loadLazySpecializationsImpl(OnlyPartial); 555 } 556 557 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> & 558 ClassTemplateDecl::getSpecializations() const { 559 LoadLazySpecializations(); 560 return getCommonPtr()->Specializations; 561 } 562 563 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> & 564 ClassTemplateDecl::getPartialSpecializations() const { 565 LoadLazySpecializations(/*PartialOnly = */ true); 566 return getCommonPtr()->PartialSpecializations; 567 } 568 569 RedeclarableTemplateDecl::CommonBase * 570 ClassTemplateDecl::newCommon(ASTContext &C) const { 571 auto *CommonPtr = new (C) Common; 572 C.addDestruction(CommonPtr); 573 return CommonPtr; 574 } 575 576 ClassTemplateSpecializationDecl * 577 ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, 578 void *&InsertPos) { 579 auto *Common = getCommonPtr(); 580 return findSpecializationImpl(Common->Specializations, InsertPos, Args); 581 } 582 583 void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D, 584 void *InsertPos) { 585 auto *Common = getCommonPtr(); 586 addSpecializationImpl<ClassTemplateDecl>(Common->Specializations, D, 587 InsertPos); 588 } 589 590 ClassTemplatePartialSpecializationDecl * 591 ClassTemplateDecl::findPartialSpecialization( 592 ArrayRef<TemplateArgument> Args, 593 TemplateParameterList *TPL, void *&InsertPos) { 594 return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args, 595 TPL); 596 } 597 598 void ClassTemplatePartialSpecializationDecl::Profile( 599 llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs, 600 TemplateParameterList *TPL, const ASTContext &Context) { 601 ID.AddInteger(TemplateArgs.size()); 602 for (const TemplateArgument &TemplateArg : TemplateArgs) 603 TemplateArg.Profile(ID, Context); 604 TPL->Profile(ID, Context); 605 } 606 607 void ClassTemplateDecl::AddPartialSpecialization( 608 ClassTemplatePartialSpecializationDecl *D, 609 void *InsertPos) { 610 if (InsertPos) 611 getPartialSpecializations().InsertNode(D, InsertPos); 612 else { 613 ClassTemplatePartialSpecializationDecl *Existing 614 = getPartialSpecializations().GetOrInsertNode(D); 615 (void)Existing; 616 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?"); 617 } 618 619 if (ASTMutationListener *L = getASTMutationListener()) 620 L->AddedCXXTemplateSpecialization(this, D); 621 } 622 623 void ClassTemplateDecl::getPartialSpecializations( 624 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) const { 625 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs 626 = getPartialSpecializations(); 627 PS.clear(); 628 PS.reserve(PartialSpecs.size()); 629 for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs) 630 PS.push_back(P.getMostRecentDecl()); 631 } 632 633 ClassTemplatePartialSpecializationDecl * 634 ClassTemplateDecl::findPartialSpecialization(QualType T) { 635 ASTContext &Context = getASTContext(); 636 for (ClassTemplatePartialSpecializationDecl &P : 637 getPartialSpecializations()) { 638 if (Context.hasSameType(P.getInjectedSpecializationType(), T)) 639 return P.getMostRecentDecl(); 640 } 641 642 return nullptr; 643 } 644 645 ClassTemplatePartialSpecializationDecl * 646 ClassTemplateDecl::findPartialSpecInstantiatedFromMember( 647 ClassTemplatePartialSpecializationDecl *D) { 648 Decl *DCanon = D->getCanonicalDecl(); 649 for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) { 650 if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon) 651 return P.getMostRecentDecl(); 652 } 653 654 return nullptr; 655 } 656 657 QualType 658 ClassTemplateDecl::getInjectedClassNameSpecialization() { 659 Common *CommonPtr = getCommonPtr(); 660 if (!CommonPtr->InjectedClassNameType.isNull()) 661 return CommonPtr->InjectedClassNameType; 662 663 // C++0x [temp.dep.type]p2: 664 // The template argument list of a primary template is a template argument 665 // list in which the nth template argument has the value of the nth template 666 // parameter of the class template. If the nth template parameter is a 667 // template parameter pack (14.5.3), the nth template argument is a pack 668 // expansion (14.5.3) whose pattern is the name of the template parameter 669 // pack. 670 ASTContext &Context = getASTContext(); 671 TemplateName Name = Context.getQualifiedTemplateName( 672 /*NNS=*/nullptr, /*TemplateKeyword=*/false, TemplateName(this)); 673 CommonPtr->InjectedClassNameType = Context.getTemplateSpecializationType( 674 Name, getTemplateParameters()->getInjectedTemplateArgs(Context)); 675 return CommonPtr->InjectedClassNameType; 676 } 677 678 //===----------------------------------------------------------------------===// 679 // TemplateTypeParm Allocation/Deallocation Method Implementations 680 //===----------------------------------------------------------------------===// 681 682 TemplateTypeParmDecl *TemplateTypeParmDecl::Create( 683 const ASTContext &C, DeclContext *DC, SourceLocation KeyLoc, 684 SourceLocation NameLoc, unsigned D, unsigned P, IdentifierInfo *Id, 685 bool Typename, bool ParameterPack, bool HasTypeConstraint, 686 std::optional<unsigned> NumExpanded) { 687 auto *TTPDecl = 688 new (C, DC, 689 additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 1 : 0)) 690 TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename, 691 HasTypeConstraint, NumExpanded); 692 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl); 693 TTPDecl->setTypeForDecl(TTPType.getTypePtr()); 694 return TTPDecl; 695 } 696 697 TemplateTypeParmDecl * 698 TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, GlobalDeclID ID) { 699 return new (C, ID) 700 TemplateTypeParmDecl(nullptr, SourceLocation(), SourceLocation(), nullptr, 701 false, false, std::nullopt); 702 } 703 704 TemplateTypeParmDecl * 705 TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, GlobalDeclID ID, 706 bool HasTypeConstraint) { 707 return new (C, ID, 708 additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 1 : 0)) 709 TemplateTypeParmDecl(nullptr, SourceLocation(), SourceLocation(), nullptr, 710 false, HasTypeConstraint, std::nullopt); 711 } 712 713 SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const { 714 return hasDefaultArgument() ? getDefaultArgument().getLocation() 715 : SourceLocation(); 716 } 717 718 SourceRange TemplateTypeParmDecl::getSourceRange() const { 719 if (hasDefaultArgument() && !defaultArgumentWasInherited()) 720 return SourceRange(getBeginLoc(), 721 getDefaultArgument().getSourceRange().getEnd()); 722 // TypeDecl::getSourceRange returns a range containing name location, which is 723 // wrong for unnamed template parameters. e.g: 724 // it will return <[[typename>]] instead of <[[typename]]> 725 if (getDeclName().isEmpty()) 726 return SourceRange(getBeginLoc()); 727 return TypeDecl::getSourceRange(); 728 } 729 730 void TemplateTypeParmDecl::setDefaultArgument( 731 const ASTContext &C, const TemplateArgumentLoc &DefArg) { 732 if (DefArg.getArgument().isNull()) 733 DefaultArgument.set(nullptr); 734 else 735 DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg)); 736 } 737 738 unsigned TemplateTypeParmDecl::getDepth() const { 739 return getTypeForDecl()->castAs<TemplateTypeParmType>()->getDepth(); 740 } 741 742 unsigned TemplateTypeParmDecl::getIndex() const { 743 return getTypeForDecl()->castAs<TemplateTypeParmType>()->getIndex(); 744 } 745 746 bool TemplateTypeParmDecl::isParameterPack() const { 747 return getTypeForDecl()->castAs<TemplateTypeParmType>()->isParameterPack(); 748 } 749 750 void TemplateTypeParmDecl::setTypeConstraint( 751 ConceptReference *Loc, Expr *ImmediatelyDeclaredConstraint) { 752 assert(HasTypeConstraint && 753 "HasTypeConstraint=true must be passed at construction in order to " 754 "call setTypeConstraint"); 755 assert(!TypeConstraintInitialized && 756 "TypeConstraint was already initialized!"); 757 new (getTrailingObjects<TypeConstraint>()) 758 TypeConstraint(Loc, ImmediatelyDeclaredConstraint); 759 TypeConstraintInitialized = true; 760 } 761 762 //===----------------------------------------------------------------------===// 763 // NonTypeTemplateParmDecl Method Implementations 764 //===----------------------------------------------------------------------===// 765 766 NonTypeTemplateParmDecl::NonTypeTemplateParmDecl( 767 DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D, 768 unsigned P, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, 769 ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos) 770 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc), 771 TemplateParmPosition(D, P), ParameterPack(true), 772 ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) { 773 if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) { 774 auto TypesAndInfos = 775 getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>(); 776 for (unsigned I = 0; I != NumExpandedTypes; ++I) { 777 new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]); 778 TypesAndInfos[I].second = ExpandedTInfos[I]; 779 } 780 } 781 } 782 783 NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create( 784 const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, 785 SourceLocation IdLoc, unsigned D, unsigned P, const IdentifierInfo *Id, 786 QualType T, bool ParameterPack, TypeSourceInfo *TInfo) { 787 AutoType *AT = 788 C.getLangOpts().CPlusPlus20 ? T->getContainedAutoType() : nullptr; 789 return new (C, DC, 790 additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>, 791 Expr *>(0, 792 AT && AT->isConstrained() ? 1 : 0)) 793 NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, ParameterPack, 794 TInfo); 795 } 796 797 NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create( 798 const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, 799 SourceLocation IdLoc, unsigned D, unsigned P, const IdentifierInfo *Id, 800 QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes, 801 ArrayRef<TypeSourceInfo *> ExpandedTInfos) { 802 AutoType *AT = TInfo->getType()->getContainedAutoType(); 803 return new (C, DC, 804 additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>, 805 Expr *>( 806 ExpandedTypes.size(), AT && AT->isConstrained() ? 1 : 0)) 807 NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo, 808 ExpandedTypes, ExpandedTInfos); 809 } 810 811 NonTypeTemplateParmDecl * 812 NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID, 813 bool HasTypeConstraint) { 814 return new (C, ID, additionalSizeToAlloc<std::pair<QualType, 815 TypeSourceInfo *>, 816 Expr *>(0, 817 HasTypeConstraint ? 1 : 0)) 818 NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(), 819 0, 0, nullptr, QualType(), false, nullptr); 820 } 821 822 NonTypeTemplateParmDecl * 823 NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID, 824 unsigned NumExpandedTypes, 825 bool HasTypeConstraint) { 826 auto *NTTP = 827 new (C, ID, 828 additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>, Expr *>( 829 NumExpandedTypes, HasTypeConstraint ? 1 : 0)) 830 NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(), 831 0, 0, nullptr, QualType(), nullptr, {}, {}); 832 NTTP->NumExpandedTypes = NumExpandedTypes; 833 return NTTP; 834 } 835 836 SourceRange NonTypeTemplateParmDecl::getSourceRange() const { 837 if (hasDefaultArgument() && !defaultArgumentWasInherited()) 838 return SourceRange(getOuterLocStart(), 839 getDefaultArgument().getSourceRange().getEnd()); 840 return DeclaratorDecl::getSourceRange(); 841 } 842 843 SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const { 844 return hasDefaultArgument() ? getDefaultArgument().getSourceRange().getBegin() 845 : SourceLocation(); 846 } 847 848 void NonTypeTemplateParmDecl::setDefaultArgument( 849 const ASTContext &C, const TemplateArgumentLoc &DefArg) { 850 if (DefArg.getArgument().isNull()) 851 DefaultArgument.set(nullptr); 852 else 853 DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg)); 854 } 855 856 //===----------------------------------------------------------------------===// 857 // TemplateTemplateParmDecl Method Implementations 858 //===----------------------------------------------------------------------===// 859 860 void TemplateTemplateParmDecl::anchor() {} 861 862 TemplateTemplateParmDecl::TemplateTemplateParmDecl( 863 DeclContext *DC, SourceLocation L, unsigned D, unsigned P, 864 IdentifierInfo *Id, bool Typename, TemplateParameterList *Params, 865 ArrayRef<TemplateParameterList *> Expansions) 866 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params), 867 TemplateParmPosition(D, P), Typename(Typename), ParameterPack(true), 868 ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) { 869 if (!Expansions.empty()) 870 std::uninitialized_copy(Expansions.begin(), Expansions.end(), 871 getTrailingObjects<TemplateParameterList *>()); 872 } 873 874 TemplateTemplateParmDecl * 875 TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, 876 SourceLocation L, unsigned D, unsigned P, 877 bool ParameterPack, IdentifierInfo *Id, 878 bool Typename, TemplateParameterList *Params) { 879 return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id, 880 Typename, Params); 881 } 882 883 TemplateTemplateParmDecl * 884 TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC, 885 SourceLocation L, unsigned D, unsigned P, 886 IdentifierInfo *Id, bool Typename, 887 TemplateParameterList *Params, 888 ArrayRef<TemplateParameterList *> Expansions) { 889 return new (C, DC, 890 additionalSizeToAlloc<TemplateParameterList *>(Expansions.size())) 891 TemplateTemplateParmDecl(DC, L, D, P, Id, Typename, Params, Expansions); 892 } 893 894 TemplateTemplateParmDecl * 895 TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { 896 return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, 897 false, nullptr, false, nullptr); 898 } 899 900 TemplateTemplateParmDecl * 901 TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID, 902 unsigned NumExpansions) { 903 auto *TTP = 904 new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions)) 905 TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr, 906 false, nullptr, {}); 907 TTP->NumExpandedParams = NumExpansions; 908 return TTP; 909 } 910 911 SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const { 912 return hasDefaultArgument() ? getDefaultArgument().getLocation() 913 : SourceLocation(); 914 } 915 916 void TemplateTemplateParmDecl::setDefaultArgument( 917 const ASTContext &C, const TemplateArgumentLoc &DefArg) { 918 if (DefArg.getArgument().isNull()) 919 DefaultArgument.set(nullptr); 920 else 921 DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg)); 922 } 923 924 //===----------------------------------------------------------------------===// 925 // TemplateArgumentList Implementation 926 //===----------------------------------------------------------------------===// 927 TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args) 928 : NumArguments(Args.size()) { 929 std::uninitialized_copy(Args.begin(), Args.end(), 930 getTrailingObjects<TemplateArgument>()); 931 } 932 933 TemplateArgumentList * 934 TemplateArgumentList::CreateCopy(ASTContext &Context, 935 ArrayRef<TemplateArgument> Args) { 936 void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size())); 937 return new (Mem) TemplateArgumentList(Args); 938 } 939 940 FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create( 941 ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template, 942 TemplateSpecializationKind TSK, TemplateArgumentList *TemplateArgs, 943 const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI, 944 MemberSpecializationInfo *MSInfo) { 945 const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr; 946 if (TemplateArgsAsWritten) 947 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C, 948 *TemplateArgsAsWritten); 949 950 void *Mem = 951 C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 1 : 0)); 952 return new (Mem) FunctionTemplateSpecializationInfo( 953 FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo); 954 } 955 956 //===----------------------------------------------------------------------===// 957 // ClassTemplateSpecializationDecl Implementation 958 //===----------------------------------------------------------------------===// 959 960 ClassTemplateSpecializationDecl:: 961 ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, 962 DeclContext *DC, SourceLocation StartLoc, 963 SourceLocation IdLoc, 964 ClassTemplateDecl *SpecializedTemplate, 965 ArrayRef<TemplateArgument> Args, 966 ClassTemplateSpecializationDecl *PrevDecl) 967 : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc, 968 SpecializedTemplate->getIdentifier(), PrevDecl), 969 SpecializedTemplate(SpecializedTemplate), 970 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)), 971 SpecializationKind(TSK_Undeclared) { 972 } 973 974 ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C, 975 Kind DK) 976 : CXXRecordDecl(DK, TagTypeKind::Struct, C, nullptr, SourceLocation(), 977 SourceLocation(), nullptr, nullptr), 978 SpecializationKind(TSK_Undeclared) {} 979 980 ClassTemplateSpecializationDecl * 981 ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK, 982 DeclContext *DC, 983 SourceLocation StartLoc, 984 SourceLocation IdLoc, 985 ClassTemplateDecl *SpecializedTemplate, 986 ArrayRef<TemplateArgument> Args, 987 ClassTemplateSpecializationDecl *PrevDecl) { 988 auto *Result = 989 new (Context, DC) ClassTemplateSpecializationDecl( 990 Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc, 991 SpecializedTemplate, Args, PrevDecl); 992 Result->setMayHaveOutOfDateDef(false); 993 994 // If the template decl is incomplete, copy the external lexical storage from 995 // the base template. This allows instantiations of incomplete types to 996 // complete using the external AST if the template's declaration came from an 997 // external AST. 998 if (!SpecializedTemplate->getTemplatedDecl()->isCompleteDefinition()) 999 Result->setHasExternalLexicalStorage( 1000 SpecializedTemplate->getTemplatedDecl()->hasExternalLexicalStorage()); 1001 1002 Context.getTypeDeclType(Result, PrevDecl); 1003 return Result; 1004 } 1005 1006 ClassTemplateSpecializationDecl * 1007 ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, 1008 GlobalDeclID ID) { 1009 auto *Result = 1010 new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization); 1011 Result->setMayHaveOutOfDateDef(false); 1012 return Result; 1013 } 1014 1015 void ClassTemplateSpecializationDecl::getNameForDiagnostic( 1016 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { 1017 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); 1018 1019 const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this); 1020 if (const ASTTemplateArgumentListInfo *ArgsAsWritten = 1021 PS ? PS->getTemplateArgsAsWritten() : nullptr) { 1022 printTemplateArgumentList( 1023 OS, ArgsAsWritten->arguments(), Policy, 1024 getSpecializedTemplate()->getTemplateParameters()); 1025 } else { 1026 const TemplateArgumentList &TemplateArgs = getTemplateArgs(); 1027 printTemplateArgumentList( 1028 OS, TemplateArgs.asArray(), Policy, 1029 getSpecializedTemplate()->getTemplateParameters()); 1030 } 1031 } 1032 1033 ClassTemplateDecl * 1034 ClassTemplateSpecializationDecl::getSpecializedTemplate() const { 1035 if (const auto *PartialSpec = 1036 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>()) 1037 return PartialSpec->PartialSpecialization->getSpecializedTemplate(); 1038 return cast<ClassTemplateDecl *>(SpecializedTemplate); 1039 } 1040 1041 SourceRange 1042 ClassTemplateSpecializationDecl::getSourceRange() const { 1043 switch (getSpecializationKind()) { 1044 case TSK_Undeclared: 1045 case TSK_ImplicitInstantiation: { 1046 llvm::PointerUnion<ClassTemplateDecl *, 1047 ClassTemplatePartialSpecializationDecl *> 1048 Pattern = getSpecializedTemplateOrPartial(); 1049 assert(!Pattern.isNull() && 1050 "Class template specialization without pattern?"); 1051 if (const auto *CTPSD = 1052 dyn_cast<ClassTemplatePartialSpecializationDecl *>(Pattern)) 1053 return CTPSD->getSourceRange(); 1054 return cast<ClassTemplateDecl *>(Pattern)->getSourceRange(); 1055 } 1056 case TSK_ExplicitSpecialization: { 1057 SourceRange Range = CXXRecordDecl::getSourceRange(); 1058 if (const ASTTemplateArgumentListInfo *Args = getTemplateArgsAsWritten(); 1059 !isThisDeclarationADefinition() && Args) 1060 Range.setEnd(Args->getRAngleLoc()); 1061 return Range; 1062 } 1063 case TSK_ExplicitInstantiationDeclaration: 1064 case TSK_ExplicitInstantiationDefinition: { 1065 SourceRange Range = CXXRecordDecl::getSourceRange(); 1066 if (SourceLocation ExternKW = getExternKeywordLoc(); ExternKW.isValid()) 1067 Range.setBegin(ExternKW); 1068 else if (SourceLocation TemplateKW = getTemplateKeywordLoc(); 1069 TemplateKW.isValid()) 1070 Range.setBegin(TemplateKW); 1071 if (const ASTTemplateArgumentListInfo *Args = getTemplateArgsAsWritten()) 1072 Range.setEnd(Args->getRAngleLoc()); 1073 return Range; 1074 } 1075 } 1076 llvm_unreachable("unhandled template specialization kind"); 1077 } 1078 1079 void ClassTemplateSpecializationDecl::setExternKeywordLoc(SourceLocation Loc) { 1080 auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo); 1081 if (!Info) { 1082 // Don't allocate if the location is invalid. 1083 if (Loc.isInvalid()) 1084 return; 1085 Info = new (getASTContext()) ExplicitInstantiationInfo; 1086 Info->TemplateArgsAsWritten = getTemplateArgsAsWritten(); 1087 ExplicitInfo = Info; 1088 } 1089 Info->ExternKeywordLoc = Loc; 1090 } 1091 1092 void ClassTemplateSpecializationDecl::setTemplateKeywordLoc( 1093 SourceLocation Loc) { 1094 auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo); 1095 if (!Info) { 1096 // Don't allocate if the location is invalid. 1097 if (Loc.isInvalid()) 1098 return; 1099 Info = new (getASTContext()) ExplicitInstantiationInfo; 1100 Info->TemplateArgsAsWritten = getTemplateArgsAsWritten(); 1101 ExplicitInfo = Info; 1102 } 1103 Info->TemplateKeywordLoc = Loc; 1104 } 1105 1106 //===----------------------------------------------------------------------===// 1107 // ConceptDecl Implementation 1108 //===----------------------------------------------------------------------===// 1109 ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC, 1110 SourceLocation L, DeclarationName Name, 1111 TemplateParameterList *Params, 1112 Expr *ConstraintExpr) { 1113 bool Invalid = AdoptTemplateParameterList(Params, DC); 1114 auto *TD = new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr); 1115 if (Invalid) 1116 TD->setInvalidDecl(); 1117 return TD; 1118 } 1119 1120 ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { 1121 ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(), 1122 DeclarationName(), 1123 nullptr, nullptr); 1124 1125 return Result; 1126 } 1127 1128 //===----------------------------------------------------------------------===// 1129 // ImplicitConceptSpecializationDecl Implementation 1130 //===----------------------------------------------------------------------===// 1131 ImplicitConceptSpecializationDecl::ImplicitConceptSpecializationDecl( 1132 DeclContext *DC, SourceLocation SL, 1133 ArrayRef<TemplateArgument> ConvertedArgs) 1134 : Decl(ImplicitConceptSpecialization, DC, SL), 1135 NumTemplateArgs(ConvertedArgs.size()) { 1136 setTemplateArguments(ConvertedArgs); 1137 } 1138 1139 ImplicitConceptSpecializationDecl::ImplicitConceptSpecializationDecl( 1140 EmptyShell Empty, unsigned NumTemplateArgs) 1141 : Decl(ImplicitConceptSpecialization, Empty), 1142 NumTemplateArgs(NumTemplateArgs) {} 1143 1144 ImplicitConceptSpecializationDecl *ImplicitConceptSpecializationDecl::Create( 1145 const ASTContext &C, DeclContext *DC, SourceLocation SL, 1146 ArrayRef<TemplateArgument> ConvertedArgs) { 1147 return new (C, DC, 1148 additionalSizeToAlloc<TemplateArgument>(ConvertedArgs.size())) 1149 ImplicitConceptSpecializationDecl(DC, SL, ConvertedArgs); 1150 } 1151 1152 ImplicitConceptSpecializationDecl * 1153 ImplicitConceptSpecializationDecl::CreateDeserialized( 1154 const ASTContext &C, GlobalDeclID ID, unsigned NumTemplateArgs) { 1155 return new (C, ID, additionalSizeToAlloc<TemplateArgument>(NumTemplateArgs)) 1156 ImplicitConceptSpecializationDecl(EmptyShell{}, NumTemplateArgs); 1157 } 1158 1159 void ImplicitConceptSpecializationDecl::setTemplateArguments( 1160 ArrayRef<TemplateArgument> Converted) { 1161 assert(Converted.size() == NumTemplateArgs); 1162 std::uninitialized_copy(Converted.begin(), Converted.end(), 1163 getTrailingObjects<TemplateArgument>()); 1164 } 1165 1166 //===----------------------------------------------------------------------===// 1167 // ClassTemplatePartialSpecializationDecl Implementation 1168 //===----------------------------------------------------------------------===// 1169 void ClassTemplatePartialSpecializationDecl::anchor() {} 1170 1171 ClassTemplatePartialSpecializationDecl::ClassTemplatePartialSpecializationDecl( 1172 ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc, 1173 SourceLocation IdLoc, TemplateParameterList *Params, 1174 ClassTemplateDecl *SpecializedTemplate, ArrayRef<TemplateArgument> Args, 1175 ClassTemplatePartialSpecializationDecl *PrevDecl) 1176 : ClassTemplateSpecializationDecl( 1177 Context, ClassTemplatePartialSpecialization, TK, DC, StartLoc, IdLoc, 1178 SpecializedTemplate, Args, PrevDecl), 1179 TemplateParams(Params), InstantiatedFromMember(nullptr, false) { 1180 if (AdoptTemplateParameterList(Params, this)) 1181 setInvalidDecl(); 1182 } 1183 1184 ClassTemplatePartialSpecializationDecl * 1185 ClassTemplatePartialSpecializationDecl::Create( 1186 ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation StartLoc, 1187 SourceLocation IdLoc, TemplateParameterList *Params, 1188 ClassTemplateDecl *SpecializedTemplate, ArrayRef<TemplateArgument> Args, 1189 QualType CanonInjectedType, 1190 ClassTemplatePartialSpecializationDecl *PrevDecl) { 1191 auto *Result = new (Context, DC) ClassTemplatePartialSpecializationDecl( 1192 Context, TK, DC, StartLoc, IdLoc, Params, SpecializedTemplate, Args, 1193 PrevDecl); 1194 Result->setSpecializationKind(TSK_ExplicitSpecialization); 1195 Result->setMayHaveOutOfDateDef(false); 1196 1197 Context.getInjectedClassNameType(Result, CanonInjectedType); 1198 return Result; 1199 } 1200 1201 ClassTemplatePartialSpecializationDecl * 1202 ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C, 1203 GlobalDeclID ID) { 1204 auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C); 1205 Result->setMayHaveOutOfDateDef(false); 1206 return Result; 1207 } 1208 1209 SourceRange ClassTemplatePartialSpecializationDecl::getSourceRange() const { 1210 if (const ClassTemplatePartialSpecializationDecl *MT = 1211 getInstantiatedFromMember(); 1212 MT && !isMemberSpecialization()) 1213 return MT->getSourceRange(); 1214 SourceRange Range = ClassTemplateSpecializationDecl::getSourceRange(); 1215 if (const TemplateParameterList *TPL = getTemplateParameters(); 1216 TPL && !getNumTemplateParameterLists()) 1217 Range.setBegin(TPL->getTemplateLoc()); 1218 return Range; 1219 } 1220 1221 //===----------------------------------------------------------------------===// 1222 // FriendTemplateDecl Implementation 1223 //===----------------------------------------------------------------------===// 1224 1225 void FriendTemplateDecl::anchor() {} 1226 1227 FriendTemplateDecl * 1228 FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC, 1229 SourceLocation L, 1230 MutableArrayRef<TemplateParameterList *> Params, 1231 FriendUnion Friend, SourceLocation FLoc) { 1232 TemplateParameterList **TPL = nullptr; 1233 if (!Params.empty()) { 1234 TPL = new (Context) TemplateParameterList *[Params.size()]; 1235 llvm::copy(Params, TPL); 1236 } 1237 return new (Context, DC) 1238 FriendTemplateDecl(DC, L, TPL, Params.size(), Friend, FLoc); 1239 } 1240 1241 FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C, 1242 GlobalDeclID ID) { 1243 return new (C, ID) FriendTemplateDecl(EmptyShell()); 1244 } 1245 1246 //===----------------------------------------------------------------------===// 1247 // TypeAliasTemplateDecl Implementation 1248 //===----------------------------------------------------------------------===// 1249 1250 TypeAliasTemplateDecl * 1251 TypeAliasTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, 1252 DeclarationName Name, 1253 TemplateParameterList *Params, NamedDecl *Decl) { 1254 bool Invalid = AdoptTemplateParameterList(Params, DC); 1255 auto *TD = new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl); 1256 if (Invalid) 1257 TD->setInvalidDecl(); 1258 return TD; 1259 } 1260 1261 TypeAliasTemplateDecl * 1262 TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { 1263 return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(), 1264 DeclarationName(), nullptr, nullptr); 1265 } 1266 1267 RedeclarableTemplateDecl::CommonBase * 1268 TypeAliasTemplateDecl::newCommon(ASTContext &C) const { 1269 auto *CommonPtr = new (C) Common; 1270 C.addDestruction(CommonPtr); 1271 return CommonPtr; 1272 } 1273 1274 //===----------------------------------------------------------------------===// 1275 // VarTemplateDecl Implementation 1276 //===----------------------------------------------------------------------===// 1277 1278 VarTemplateDecl *VarTemplateDecl::getDefinition() { 1279 VarTemplateDecl *CurD = this; 1280 while (CurD) { 1281 if (CurD->isThisDeclarationADefinition()) 1282 return CurD; 1283 CurD = CurD->getPreviousDecl(); 1284 } 1285 return nullptr; 1286 } 1287 1288 VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC, 1289 SourceLocation L, DeclarationName Name, 1290 TemplateParameterList *Params, 1291 VarDecl *Decl) { 1292 bool Invalid = AdoptTemplateParameterList(Params, DC); 1293 auto *TD = new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl); 1294 if (Invalid) 1295 TD->setInvalidDecl(); 1296 return TD; 1297 } 1298 1299 VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C, 1300 GlobalDeclID ID) { 1301 return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(), 1302 DeclarationName(), nullptr, nullptr); 1303 } 1304 1305 void VarTemplateDecl::LoadLazySpecializations( 1306 bool OnlyPartial /*=false*/) const { 1307 loadLazySpecializationsImpl(OnlyPartial); 1308 } 1309 1310 llvm::FoldingSetVector<VarTemplateSpecializationDecl> & 1311 VarTemplateDecl::getSpecializations() const { 1312 LoadLazySpecializations(); 1313 return getCommonPtr()->Specializations; 1314 } 1315 1316 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & 1317 VarTemplateDecl::getPartialSpecializations() const { 1318 LoadLazySpecializations(/*PartialOnly = */ true); 1319 return getCommonPtr()->PartialSpecializations; 1320 } 1321 1322 RedeclarableTemplateDecl::CommonBase * 1323 VarTemplateDecl::newCommon(ASTContext &C) const { 1324 auto *CommonPtr = new (C) Common; 1325 C.addDestruction(CommonPtr); 1326 return CommonPtr; 1327 } 1328 1329 VarTemplateSpecializationDecl * 1330 VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args, 1331 void *&InsertPos) { 1332 auto *Common = getCommonPtr(); 1333 return findSpecializationImpl(Common->Specializations, InsertPos, Args); 1334 } 1335 1336 void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D, 1337 void *InsertPos) { 1338 auto *Common = getCommonPtr(); 1339 addSpecializationImpl<VarTemplateDecl>(Common->Specializations, D, InsertPos); 1340 } 1341 1342 VarTemplatePartialSpecializationDecl * 1343 VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args, 1344 TemplateParameterList *TPL, void *&InsertPos) { 1345 return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args, 1346 TPL); 1347 } 1348 1349 void VarTemplatePartialSpecializationDecl::Profile( 1350 llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs, 1351 TemplateParameterList *TPL, const ASTContext &Context) { 1352 ID.AddInteger(TemplateArgs.size()); 1353 for (const TemplateArgument &TemplateArg : TemplateArgs) 1354 TemplateArg.Profile(ID, Context); 1355 TPL->Profile(ID, Context); 1356 } 1357 1358 void VarTemplateDecl::AddPartialSpecialization( 1359 VarTemplatePartialSpecializationDecl *D, void *InsertPos) { 1360 if (InsertPos) 1361 getPartialSpecializations().InsertNode(D, InsertPos); 1362 else { 1363 VarTemplatePartialSpecializationDecl *Existing = 1364 getPartialSpecializations().GetOrInsertNode(D); 1365 (void)Existing; 1366 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?"); 1367 } 1368 1369 if (ASTMutationListener *L = getASTMutationListener()) 1370 L->AddedCXXTemplateSpecialization(this, D); 1371 } 1372 1373 void VarTemplateDecl::getPartialSpecializations( 1374 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const { 1375 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs = 1376 getPartialSpecializations(); 1377 PS.clear(); 1378 PS.reserve(PartialSpecs.size()); 1379 for (VarTemplatePartialSpecializationDecl &P : PartialSpecs) 1380 PS.push_back(P.getMostRecentDecl()); 1381 } 1382 1383 VarTemplatePartialSpecializationDecl * 1384 VarTemplateDecl::findPartialSpecInstantiatedFromMember( 1385 VarTemplatePartialSpecializationDecl *D) { 1386 Decl *DCanon = D->getCanonicalDecl(); 1387 for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) { 1388 if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon) 1389 return P.getMostRecentDecl(); 1390 } 1391 1392 return nullptr; 1393 } 1394 1395 //===----------------------------------------------------------------------===// 1396 // VarTemplateSpecializationDecl Implementation 1397 //===----------------------------------------------------------------------===// 1398 1399 VarTemplateSpecializationDecl::VarTemplateSpecializationDecl( 1400 Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, 1401 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T, 1402 TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) 1403 : VarDecl(DK, Context, DC, StartLoc, IdLoc, 1404 SpecializedTemplate->getIdentifier(), T, TInfo, S), 1405 SpecializedTemplate(SpecializedTemplate), 1406 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)), 1407 SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {} 1408 1409 VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK, 1410 ASTContext &C) 1411 : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr, 1412 QualType(), nullptr, SC_None), 1413 SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {} 1414 1415 VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create( 1416 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, 1417 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T, 1418 TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) { 1419 return new (Context, DC) VarTemplateSpecializationDecl( 1420 VarTemplateSpecialization, Context, DC, StartLoc, IdLoc, 1421 SpecializedTemplate, T, TInfo, S, Args); 1422 } 1423 1424 VarTemplateSpecializationDecl * 1425 VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, 1426 GlobalDeclID ID) { 1427 return new (C, ID) 1428 VarTemplateSpecializationDecl(VarTemplateSpecialization, C); 1429 } 1430 1431 void VarTemplateSpecializationDecl::getNameForDiagnostic( 1432 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { 1433 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); 1434 1435 const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this); 1436 if (const ASTTemplateArgumentListInfo *ArgsAsWritten = 1437 PS ? PS->getTemplateArgsAsWritten() : nullptr) { 1438 printTemplateArgumentList( 1439 OS, ArgsAsWritten->arguments(), Policy, 1440 getSpecializedTemplate()->getTemplateParameters()); 1441 } else { 1442 const TemplateArgumentList &TemplateArgs = getTemplateArgs(); 1443 printTemplateArgumentList( 1444 OS, TemplateArgs.asArray(), Policy, 1445 getSpecializedTemplate()->getTemplateParameters()); 1446 } 1447 } 1448 1449 VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const { 1450 if (const auto *PartialSpec = 1451 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) 1452 return PartialSpec->PartialSpecialization->getSpecializedTemplate(); 1453 return cast<VarTemplateDecl *>(SpecializedTemplate); 1454 } 1455 1456 SourceRange VarTemplateSpecializationDecl::getSourceRange() const { 1457 switch (getSpecializationKind()) { 1458 case TSK_Undeclared: 1459 case TSK_ImplicitInstantiation: { 1460 llvm::PointerUnion<VarTemplateDecl *, 1461 VarTemplatePartialSpecializationDecl *> 1462 Pattern = getSpecializedTemplateOrPartial(); 1463 assert(!Pattern.isNull() && 1464 "Variable template specialization without pattern?"); 1465 if (const auto *VTPSD = 1466 dyn_cast<VarTemplatePartialSpecializationDecl *>(Pattern)) 1467 return VTPSD->getSourceRange(); 1468 VarTemplateDecl *VTD = cast<VarTemplateDecl *>(Pattern); 1469 if (hasInit()) { 1470 if (VarTemplateDecl *Definition = VTD->getDefinition()) 1471 return Definition->getSourceRange(); 1472 } 1473 return VTD->getCanonicalDecl()->getSourceRange(); 1474 } 1475 case TSK_ExplicitSpecialization: { 1476 SourceRange Range = VarDecl::getSourceRange(); 1477 if (const ASTTemplateArgumentListInfo *Args = getTemplateArgsAsWritten(); 1478 !hasInit() && Args) 1479 Range.setEnd(Args->getRAngleLoc()); 1480 return Range; 1481 } 1482 case TSK_ExplicitInstantiationDeclaration: 1483 case TSK_ExplicitInstantiationDefinition: { 1484 SourceRange Range = VarDecl::getSourceRange(); 1485 if (SourceLocation ExternKW = getExternKeywordLoc(); ExternKW.isValid()) 1486 Range.setBegin(ExternKW); 1487 else if (SourceLocation TemplateKW = getTemplateKeywordLoc(); 1488 TemplateKW.isValid()) 1489 Range.setBegin(TemplateKW); 1490 if (const ASTTemplateArgumentListInfo *Args = getTemplateArgsAsWritten()) 1491 Range.setEnd(Args->getRAngleLoc()); 1492 return Range; 1493 } 1494 } 1495 llvm_unreachable("unhandled template specialization kind"); 1496 } 1497 1498 void VarTemplateSpecializationDecl::setExternKeywordLoc(SourceLocation Loc) { 1499 auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo); 1500 if (!Info) { 1501 // Don't allocate if the location is invalid. 1502 if (Loc.isInvalid()) 1503 return; 1504 Info = new (getASTContext()) ExplicitInstantiationInfo; 1505 Info->TemplateArgsAsWritten = getTemplateArgsAsWritten(); 1506 ExplicitInfo = Info; 1507 } 1508 Info->ExternKeywordLoc = Loc; 1509 } 1510 1511 void VarTemplateSpecializationDecl::setTemplateKeywordLoc(SourceLocation Loc) { 1512 auto *Info = dyn_cast_if_present<ExplicitInstantiationInfo *>(ExplicitInfo); 1513 if (!Info) { 1514 // Don't allocate if the location is invalid. 1515 if (Loc.isInvalid()) 1516 return; 1517 Info = new (getASTContext()) ExplicitInstantiationInfo; 1518 Info->TemplateArgsAsWritten = getTemplateArgsAsWritten(); 1519 ExplicitInfo = Info; 1520 } 1521 Info->TemplateKeywordLoc = Loc; 1522 } 1523 1524 //===----------------------------------------------------------------------===// 1525 // VarTemplatePartialSpecializationDecl Implementation 1526 //===----------------------------------------------------------------------===// 1527 1528 void VarTemplatePartialSpecializationDecl::anchor() {} 1529 1530 VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl( 1531 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, 1532 SourceLocation IdLoc, TemplateParameterList *Params, 1533 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo, 1534 StorageClass S, ArrayRef<TemplateArgument> Args) 1535 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context, 1536 DC, StartLoc, IdLoc, SpecializedTemplate, T, 1537 TInfo, S, Args), 1538 TemplateParams(Params), InstantiatedFromMember(nullptr, false) { 1539 if (AdoptTemplateParameterList(Params, DC)) 1540 setInvalidDecl(); 1541 } 1542 1543 VarTemplatePartialSpecializationDecl * 1544 VarTemplatePartialSpecializationDecl::Create( 1545 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, 1546 SourceLocation IdLoc, TemplateParameterList *Params, 1547 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo, 1548 StorageClass S, ArrayRef<TemplateArgument> Args) { 1549 auto *Result = new (Context, DC) VarTemplatePartialSpecializationDecl( 1550 Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo, S, 1551 Args); 1552 Result->setSpecializationKind(TSK_ExplicitSpecialization); 1553 return Result; 1554 } 1555 1556 VarTemplatePartialSpecializationDecl * 1557 VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C, 1558 GlobalDeclID ID) { 1559 return new (C, ID) VarTemplatePartialSpecializationDecl(C); 1560 } 1561 1562 SourceRange VarTemplatePartialSpecializationDecl::getSourceRange() const { 1563 if (const VarTemplatePartialSpecializationDecl *MT = 1564 getInstantiatedFromMember(); 1565 MT && !isMemberSpecialization()) 1566 return MT->getSourceRange(); 1567 SourceRange Range = VarTemplateSpecializationDecl::getSourceRange(); 1568 if (const TemplateParameterList *TPL = getTemplateParameters(); 1569 TPL && !getNumTemplateParameterLists()) 1570 Range.setBegin(TPL->getTemplateLoc()); 1571 return Range; 1572 } 1573 1574 static TemplateParameterList * 1575 createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) { 1576 // typename T 1577 auto *T = TemplateTypeParmDecl::Create( 1578 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0, 1579 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false, 1580 /*HasTypeConstraint=*/false); 1581 T->setImplicit(true); 1582 1583 // T ...Ints 1584 TypeSourceInfo *TI = 1585 C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0)); 1586 auto *N = NonTypeTemplateParmDecl::Create( 1587 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1, 1588 /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI); 1589 N->setImplicit(true); 1590 1591 // <typename T, T ...Ints> 1592 NamedDecl *P[2] = {T, N}; 1593 auto *TPL = TemplateParameterList::Create( 1594 C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr); 1595 1596 // template <typename T, ...Ints> class IntSeq 1597 auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create( 1598 C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0, 1599 /*ParameterPack=*/false, /*Id=*/nullptr, /*Typename=*/false, TPL); 1600 TemplateTemplateParm->setImplicit(true); 1601 1602 // typename T 1603 auto *TemplateTypeParm = TemplateTypeParmDecl::Create( 1604 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1, 1605 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false, 1606 /*HasTypeConstraint=*/false); 1607 TemplateTypeParm->setImplicit(true); 1608 1609 // T N 1610 TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo( 1611 QualType(TemplateTypeParm->getTypeForDecl(), 0)); 1612 auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create( 1613 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2, 1614 /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo); 1615 NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm, 1616 NonTypeTemplateParm}; 1617 1618 // template <template <typename T, T ...Ints> class IntSeq, typename T, T N> 1619 return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(), 1620 Params, SourceLocation(), nullptr); 1621 } 1622 1623 static TemplateParameterList * 1624 createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) { 1625 // std::size_t Index 1626 TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType()); 1627 auto *Index = NonTypeTemplateParmDecl::Create( 1628 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0, 1629 /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo); 1630 1631 // typename ...T 1632 auto *Ts = TemplateTypeParmDecl::Create( 1633 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1, 1634 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true, 1635 /*HasTypeConstraint=*/false); 1636 Ts->setImplicit(true); 1637 1638 // template <std::size_t Index, typename ...T> 1639 NamedDecl *Params[] = {Index, Ts}; 1640 return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(), 1641 llvm::ArrayRef(Params), SourceLocation(), 1642 nullptr); 1643 } 1644 1645 static TemplateParameterList *createBuiltinCommonTypeList(const ASTContext &C, 1646 DeclContext *DC) { 1647 // class... Args 1648 auto *Args = 1649 TemplateTypeParmDecl::Create(C, DC, SourceLocation(), SourceLocation(), 1650 /*Depth=*/1, /*Position=*/0, /*Id=*/nullptr, 1651 /*Typename=*/false, /*ParameterPack=*/true); 1652 1653 // <class... Args> 1654 auto *BaseTemplateList = TemplateParameterList::Create( 1655 C, SourceLocation(), SourceLocation(), Args, SourceLocation(), nullptr); 1656 1657 // template <class... Args> class BaseTemplate 1658 auto *BaseTemplate = TemplateTemplateParmDecl::Create( 1659 C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0, 1660 /*ParameterPack=*/false, /*Id=*/nullptr, 1661 /*Typename=*/false, BaseTemplateList); 1662 1663 // class TypeMember 1664 auto *TypeMember = 1665 TemplateTypeParmDecl::Create(C, DC, SourceLocation(), SourceLocation(), 1666 /*Depth=*/1, /*Position=*/0, /*Id=*/nullptr, 1667 /*Typename=*/false, /*ParameterPack=*/false); 1668 1669 // <class TypeMember> 1670 auto *HasTypeMemberList = 1671 TemplateParameterList::Create(C, SourceLocation(), SourceLocation(), 1672 TypeMember, SourceLocation(), nullptr); 1673 1674 // template <class TypeMember> class HasTypeMember 1675 auto *HasTypeMember = TemplateTemplateParmDecl::Create( 1676 C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/1, 1677 /*ParameterPack=*/false, /*Id=*/nullptr, 1678 /*Typename=*/false, HasTypeMemberList); 1679 1680 // class HasNoTypeMember 1681 auto *HasNoTypeMember = TemplateTypeParmDecl::Create( 1682 C, DC, {}, {}, /*Depth=*/0, /*Position=*/2, /*Id=*/nullptr, 1683 /*Typename=*/false, /*ParameterPack=*/false); 1684 1685 // class... Ts 1686 auto *Ts = TemplateTypeParmDecl::Create( 1687 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/3, 1688 /*Id=*/nullptr, /*Typename=*/false, /*ParameterPack=*/true); 1689 1690 // template <template <class... Args> class BaseTemplate, 1691 // template <class TypeMember> class HasTypeMember, class HasNoTypeMember, 1692 // class... Ts> 1693 return TemplateParameterList::Create( 1694 C, SourceLocation(), SourceLocation(), 1695 {BaseTemplate, HasTypeMember, HasNoTypeMember, Ts}, SourceLocation(), 1696 nullptr); 1697 } 1698 1699 static TemplateParameterList *createBuiltinTemplateParameterList( 1700 const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) { 1701 switch (BTK) { 1702 case BTK__make_integer_seq: 1703 return createMakeIntegerSeqParameterList(C, DC); 1704 case BTK__type_pack_element: 1705 return createTypePackElementParameterList(C, DC); 1706 case BTK__builtin_common_type: 1707 return createBuiltinCommonTypeList(C, DC); 1708 } 1709 1710 llvm_unreachable("unhandled BuiltinTemplateKind!"); 1711 } 1712 1713 void BuiltinTemplateDecl::anchor() {} 1714 1715 BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC, 1716 DeclarationName Name, 1717 BuiltinTemplateKind BTK) 1718 : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name, 1719 createBuiltinTemplateParameterList(C, DC, BTK)), 1720 BTK(BTK) {} 1721 1722 TemplateParamObjectDecl *TemplateParamObjectDecl::Create(const ASTContext &C, 1723 QualType T, 1724 const APValue &V) { 1725 DeclContext *DC = C.getTranslationUnitDecl(); 1726 auto *TPOD = new (C, DC) TemplateParamObjectDecl(DC, T, V); 1727 C.addDestruction(&TPOD->Value); 1728 return TPOD; 1729 } 1730 1731 TemplateParamObjectDecl * 1732 TemplateParamObjectDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { 1733 auto *TPOD = new (C, ID) TemplateParamObjectDecl(nullptr, QualType(), APValue()); 1734 C.addDestruction(&TPOD->Value); 1735 return TPOD; 1736 } 1737 1738 void TemplateParamObjectDecl::printName(llvm::raw_ostream &OS, 1739 const PrintingPolicy &Policy) const { 1740 OS << "<template param "; 1741 printAsExpr(OS, Policy); 1742 OS << ">"; 1743 } 1744 1745 void TemplateParamObjectDecl::printAsExpr(llvm::raw_ostream &OS) const { 1746 printAsExpr(OS, getASTContext().getPrintingPolicy()); 1747 } 1748 1749 void TemplateParamObjectDecl::printAsExpr(llvm::raw_ostream &OS, 1750 const PrintingPolicy &Policy) const { 1751 getType().getUnqualifiedType().print(OS, Policy); 1752 printAsInit(OS, Policy); 1753 } 1754 1755 void TemplateParamObjectDecl::printAsInit(llvm::raw_ostream &OS) const { 1756 printAsInit(OS, getASTContext().getPrintingPolicy()); 1757 } 1758 1759 void TemplateParamObjectDecl::printAsInit(llvm::raw_ostream &OS, 1760 const PrintingPolicy &Policy) const { 1761 getValue().printPretty(OS, Policy, getType(), &getASTContext()); 1762 } 1763 1764 TemplateParameterList *clang::getReplacedTemplateParameterList(Decl *D) { 1765 switch (D->getKind()) { 1766 case Decl::Kind::CXXRecord: 1767 return cast<CXXRecordDecl>(D) 1768 ->getDescribedTemplate() 1769 ->getTemplateParameters(); 1770 case Decl::Kind::ClassTemplate: 1771 return cast<ClassTemplateDecl>(D)->getTemplateParameters(); 1772 case Decl::Kind::ClassTemplateSpecialization: { 1773 const auto *CTSD = cast<ClassTemplateSpecializationDecl>(D); 1774 auto P = CTSD->getSpecializedTemplateOrPartial(); 1775 if (const auto *CTPSD = 1776 dyn_cast<ClassTemplatePartialSpecializationDecl *>(P)) 1777 return CTPSD->getTemplateParameters(); 1778 return cast<ClassTemplateDecl *>(P)->getTemplateParameters(); 1779 } 1780 case Decl::Kind::ClassTemplatePartialSpecialization: 1781 return cast<ClassTemplatePartialSpecializationDecl>(D) 1782 ->getTemplateParameters(); 1783 case Decl::Kind::TypeAliasTemplate: 1784 return cast<TypeAliasTemplateDecl>(D)->getTemplateParameters(); 1785 case Decl::Kind::BuiltinTemplate: 1786 return cast<BuiltinTemplateDecl>(D)->getTemplateParameters(); 1787 case Decl::Kind::CXXDeductionGuide: 1788 case Decl::Kind::CXXConversion: 1789 case Decl::Kind::CXXConstructor: 1790 case Decl::Kind::CXXDestructor: 1791 case Decl::Kind::CXXMethod: 1792 case Decl::Kind::Function: 1793 return cast<FunctionDecl>(D) 1794 ->getTemplateSpecializationInfo() 1795 ->getTemplate() 1796 ->getTemplateParameters(); 1797 case Decl::Kind::FunctionTemplate: 1798 return cast<FunctionTemplateDecl>(D)->getTemplateParameters(); 1799 case Decl::Kind::VarTemplate: 1800 return cast<VarTemplateDecl>(D)->getTemplateParameters(); 1801 case Decl::Kind::VarTemplateSpecialization: { 1802 const auto *VTSD = cast<VarTemplateSpecializationDecl>(D); 1803 auto P = VTSD->getSpecializedTemplateOrPartial(); 1804 if (const auto *VTPSD = dyn_cast<VarTemplatePartialSpecializationDecl *>(P)) 1805 return VTPSD->getTemplateParameters(); 1806 return cast<VarTemplateDecl *>(P)->getTemplateParameters(); 1807 } 1808 case Decl::Kind::VarTemplatePartialSpecialization: 1809 return cast<VarTemplatePartialSpecializationDecl>(D) 1810 ->getTemplateParameters(); 1811 case Decl::Kind::TemplateTemplateParm: 1812 return cast<TemplateTemplateParmDecl>(D)->getTemplateParameters(); 1813 case Decl::Kind::Concept: 1814 return cast<ConceptDecl>(D)->getTemplateParameters(); 1815 default: 1816 llvm_unreachable("Unhandled templated declaration kind"); 1817 } 1818 } 1819