DeclTemplate.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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. #include "clang/AST/DeclTemplate.h"
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/ASTMutationListener.h"
  15. #include "clang/AST/DeclCXX.h"
  16. #include "clang/AST/DeclarationName.h"
  17. #include "clang/AST/Expr.h"
  18. #include "clang/AST/ExternalASTSource.h"
  19. #include "clang/AST/TemplateBase.h"
  20. #include "clang/AST/TemplateName.h"
  21. #include "clang/AST/Type.h"
  22. #include "clang/AST/TypeLoc.h"
  23. #include "clang/Basic/Builtins.h"
  24. #include "clang/Basic/LLVM.h"
  25. #include "clang/Basic/SourceLocation.h"
  26. #include "llvm/ADT/ArrayRef.h"
  27. #include "llvm/ADT/FoldingSet.h"
  28. #include "llvm/ADT/PointerUnion.h"
  29. #include "llvm/ADT/STLExtras.h"
  30. #include "llvm/ADT/SmallVector.h"
  31. #include "llvm/Support/Casting.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include <algorithm>
  34. #include <cassert>
  35. #include <cstdint>
  36. #include <memory>
  37. #include <optional>
  38. #include <utility>
  39. using namespace clang;
  40. //===----------------------------------------------------------------------===//
  41. // TemplateParameterList Implementation
  42. //===----------------------------------------------------------------------===//
  43. TemplateParameterList::TemplateParameterList(const ASTContext& C,
  44. SourceLocation TemplateLoc,
  45. SourceLocation LAngleLoc,
  46. ArrayRef<NamedDecl *> Params,
  47. SourceLocation RAngleLoc,
  48. Expr *RequiresClause)
  49. : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
  50. NumParams(Params.size()), ContainsUnexpandedParameterPack(false),
  51. HasRequiresClause(RequiresClause != nullptr),
  52. HasConstrainedParameters(false) {
  53. for (unsigned Idx = 0; Idx < NumParams; ++Idx) {
  54. NamedDecl *P = Params[Idx];
  55. begin()[Idx] = P;
  56. bool IsPack = P->isTemplateParameterPack();
  57. if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
  58. if (!IsPack && NTTP->getType()->containsUnexpandedParameterPack())
  59. ContainsUnexpandedParameterPack = true;
  60. if (NTTP->hasPlaceholderTypeConstraint())
  61. HasConstrainedParameters = true;
  62. } else if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) {
  63. if (!IsPack &&
  64. TTP->getTemplateParameters()->containsUnexpandedParameterPack())
  65. ContainsUnexpandedParameterPack = true;
  66. } else if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
  67. if (const TypeConstraint *TC = TTP->getTypeConstraint()) {
  68. if (TC->getImmediatelyDeclaredConstraint()
  69. ->containsUnexpandedParameterPack())
  70. ContainsUnexpandedParameterPack = true;
  71. }
  72. if (TTP->hasTypeConstraint())
  73. HasConstrainedParameters = true;
  74. } else {
  75. llvm_unreachable("unexpected template parameter type");
  76. }
  77. // FIXME: If a default argument contains an unexpanded parameter pack, the
  78. // template parameter list does too.
  79. }
  80. if (HasRequiresClause) {
  81. if (RequiresClause->containsUnexpandedParameterPack())
  82. ContainsUnexpandedParameterPack = true;
  83. *getTrailingObjects<Expr *>() = RequiresClause;
  84. }
  85. }
  86. bool TemplateParameterList::containsUnexpandedParameterPack() const {
  87. if (ContainsUnexpandedParameterPack)
  88. return true;
  89. if (!HasConstrainedParameters)
  90. return false;
  91. // An implicit constrained parameter might have had a use of an unexpanded
  92. // pack added to it after the template parameter list was created. All
  93. // implicit parameters are at the end of the parameter list.
  94. for (const NamedDecl *Param : llvm::reverse(asArray())) {
  95. if (!Param->isImplicit())
  96. break;
  97. if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
  98. const auto *TC = TTP->getTypeConstraint();
  99. if (TC && TC->getImmediatelyDeclaredConstraint()
  100. ->containsUnexpandedParameterPack())
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. TemplateParameterList *
  107. TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
  108. SourceLocation LAngleLoc,
  109. ArrayRef<NamedDecl *> Params,
  110. SourceLocation RAngleLoc, Expr *RequiresClause) {
  111. void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *, Expr *>(
  112. Params.size(), RequiresClause ? 1u : 0u),
  113. alignof(TemplateParameterList));
  114. return new (Mem) TemplateParameterList(C, TemplateLoc, LAngleLoc, Params,
  115. RAngleLoc, RequiresClause);
  116. }
  117. unsigned TemplateParameterList::getMinRequiredArguments() const {
  118. unsigned NumRequiredArgs = 0;
  119. for (const NamedDecl *P : asArray()) {
  120. if (P->isTemplateParameterPack()) {
  121. if (std::optional<unsigned> Expansions = getExpandedPackSize(P)) {
  122. NumRequiredArgs += *Expansions;
  123. continue;
  124. }
  125. break;
  126. }
  127. if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
  128. if (TTP->hasDefaultArgument())
  129. break;
  130. } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
  131. if (NTTP->hasDefaultArgument())
  132. break;
  133. } else if (cast<TemplateTemplateParmDecl>(P)->hasDefaultArgument())
  134. break;
  135. ++NumRequiredArgs;
  136. }
  137. return NumRequiredArgs;
  138. }
  139. unsigned TemplateParameterList::getDepth() const {
  140. if (size() == 0)
  141. return 0;
  142. const NamedDecl *FirstParm = getParam(0);
  143. if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(FirstParm))
  144. return TTP->getDepth();
  145. else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
  146. return NTTP->getDepth();
  147. else
  148. return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
  149. }
  150. static bool AdoptTemplateParameterList(TemplateParameterList *Params,
  151. DeclContext *Owner) {
  152. bool Invalid = false;
  153. for (NamedDecl *P : *Params) {
  154. P->setDeclContext(Owner);
  155. if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
  156. if (AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner))
  157. Invalid = true;
  158. if (P->isInvalidDecl())
  159. Invalid = true;
  160. }
  161. return Invalid;
  162. }
  163. void TemplateParameterList::
  164. getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
  165. if (HasConstrainedParameters)
  166. for (const NamedDecl *Param : *this) {
  167. if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
  168. if (const auto *TC = TTP->getTypeConstraint())
  169. AC.push_back(TC->getImmediatelyDeclaredConstraint());
  170. } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
  171. if (const Expr *E = NTTP->getPlaceholderTypeConstraint())
  172. AC.push_back(E);
  173. }
  174. }
  175. if (HasRequiresClause)
  176. AC.push_back(getRequiresClause());
  177. }
  178. bool TemplateParameterList::hasAssociatedConstraints() const {
  179. return HasRequiresClause || HasConstrainedParameters;
  180. }
  181. bool TemplateParameterList::shouldIncludeTypeForArgument(
  182. const PrintingPolicy &Policy, const TemplateParameterList *TPL,
  183. unsigned Idx) {
  184. if (!TPL || Idx >= TPL->size() || Policy.AlwaysIncludeTypeForTemplateArgument)
  185. return true;
  186. const NamedDecl *TemplParam = TPL->getParam(Idx);
  187. if (const auto *ParamValueDecl =
  188. dyn_cast<NonTypeTemplateParmDecl>(TemplParam))
  189. if (ParamValueDecl->getType()->getContainedDeducedType())
  190. return true;
  191. return false;
  192. }
  193. namespace clang {
  194. void *allocateDefaultArgStorageChain(const ASTContext &C) {
  195. return new (C) char[sizeof(void*) * 2];
  196. }
  197. } // namespace clang
  198. //===----------------------------------------------------------------------===//
  199. // TemplateDecl Implementation
  200. //===----------------------------------------------------------------------===//
  201. TemplateDecl::TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
  202. DeclarationName Name, TemplateParameterList *Params,
  203. NamedDecl *Decl)
  204. : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl), TemplateParams(Params) {}
  205. void TemplateDecl::anchor() {}
  206. void TemplateDecl::
  207. getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
  208. TemplateParams->getAssociatedConstraints(AC);
  209. if (auto *FD = dyn_cast_or_null<FunctionDecl>(getTemplatedDecl()))
  210. if (const Expr *TRC = FD->getTrailingRequiresClause())
  211. AC.push_back(TRC);
  212. }
  213. bool TemplateDecl::hasAssociatedConstraints() const {
  214. if (TemplateParams->hasAssociatedConstraints())
  215. return true;
  216. if (auto *FD = dyn_cast_or_null<FunctionDecl>(getTemplatedDecl()))
  217. return FD->getTrailingRequiresClause();
  218. return false;
  219. }
  220. bool TemplateDecl::isTypeAlias() const {
  221. switch (getKind()) {
  222. case TemplateDecl::TypeAliasTemplate:
  223. case TemplateDecl::BuiltinTemplate:
  224. return true;
  225. default:
  226. return false;
  227. };
  228. }
  229. //===----------------------------------------------------------------------===//
  230. // RedeclarableTemplateDecl Implementation
  231. //===----------------------------------------------------------------------===//
  232. void RedeclarableTemplateDecl::anchor() {}
  233. RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
  234. if (Common)
  235. return Common;
  236. // Walk the previous-declaration chain until we either find a declaration
  237. // with a common pointer or we run out of previous declarations.
  238. SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
  239. for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
  240. Prev = Prev->getPreviousDecl()) {
  241. if (Prev->Common) {
  242. Common = Prev->Common;
  243. break;
  244. }
  245. PrevDecls.push_back(Prev);
  246. }
  247. // If we never found a common pointer, allocate one now.
  248. if (!Common) {
  249. // FIXME: If any of the declarations is from an AST file, we probably
  250. // need an update record to add the common data.
  251. Common = newCommon(getASTContext());
  252. }
  253. // Update any previous declarations we saw with the common pointer.
  254. for (const RedeclarableTemplateDecl *Prev : PrevDecls)
  255. Prev->Common = Common;
  256. return Common;
  257. }
  258. void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
  259. // Grab the most recent declaration to ensure we've loaded any lazy
  260. // redeclarations of this template.
  261. CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
  262. if (CommonBasePtr->LazySpecializations) {
  263. ASTContext &Context = getASTContext();
  264. uint32_t *Specs = CommonBasePtr->LazySpecializations;
  265. CommonBasePtr->LazySpecializations = nullptr;
  266. for (uint32_t I = 0, N = *Specs++; I != N; ++I)
  267. (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
  268. }
  269. }
  270. template<class EntryType, typename... ProfileArguments>
  271. typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
  272. RedeclarableTemplateDecl::findSpecializationImpl(
  273. llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos,
  274. ProfileArguments&&... ProfileArgs) {
  275. using SETraits = SpecEntryTraits<EntryType>;
  276. llvm::FoldingSetNodeID ID;
  277. EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
  278. getASTContext());
  279. EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
  280. return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr;
  281. }
  282. template<class Derived, class EntryType>
  283. void RedeclarableTemplateDecl::addSpecializationImpl(
  284. llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
  285. void *InsertPos) {
  286. using SETraits = SpecEntryTraits<EntryType>;
  287. if (InsertPos) {
  288. #ifndef NDEBUG
  289. void *CorrectInsertPos;
  290. assert(!findSpecializationImpl(Specializations,
  291. CorrectInsertPos,
  292. SETraits::getTemplateArgs(Entry)) &&
  293. InsertPos == CorrectInsertPos &&
  294. "given incorrect InsertPos for specialization");
  295. #endif
  296. Specializations.InsertNode(Entry, InsertPos);
  297. } else {
  298. EntryType *Existing = Specializations.GetOrInsertNode(Entry);
  299. (void)Existing;
  300. assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
  301. "non-canonical specialization?");
  302. }
  303. if (ASTMutationListener *L = getASTMutationListener())
  304. L->AddedCXXTemplateSpecialization(cast<Derived>(this),
  305. SETraits::getDecl(Entry));
  306. }
  307. ArrayRef<TemplateArgument> RedeclarableTemplateDecl::getInjectedTemplateArgs() {
  308. TemplateParameterList *Params = getTemplateParameters();
  309. auto *CommonPtr = getCommonPtr();
  310. if (!CommonPtr->InjectedArgs) {
  311. auto &Context = getASTContext();
  312. SmallVector<TemplateArgument, 16> TemplateArgs;
  313. Context.getInjectedTemplateArgs(Params, TemplateArgs);
  314. CommonPtr->InjectedArgs =
  315. new (Context) TemplateArgument[TemplateArgs.size()];
  316. std::copy(TemplateArgs.begin(), TemplateArgs.end(),
  317. CommonPtr->InjectedArgs);
  318. }
  319. return llvm::ArrayRef(CommonPtr->InjectedArgs, Params->size());
  320. }
  321. //===----------------------------------------------------------------------===//
  322. // FunctionTemplateDecl Implementation
  323. //===----------------------------------------------------------------------===//
  324. FunctionTemplateDecl *
  325. FunctionTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
  326. DeclarationName Name,
  327. TemplateParameterList *Params, NamedDecl *Decl) {
  328. bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
  329. auto *TD = new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
  330. if (Invalid)
  331. TD->setInvalidDecl();
  332. return TD;
  333. }
  334. FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
  335. unsigned ID) {
  336. return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
  337. DeclarationName(), nullptr, nullptr);
  338. }
  339. RedeclarableTemplateDecl::CommonBase *
  340. FunctionTemplateDecl::newCommon(ASTContext &C) const {
  341. auto *CommonPtr = new (C) Common;
  342. C.addDestruction(CommonPtr);
  343. return CommonPtr;
  344. }
  345. void FunctionTemplateDecl::LoadLazySpecializations() const {
  346. loadLazySpecializationsImpl();
  347. }
  348. llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
  349. FunctionTemplateDecl::getSpecializations() const {
  350. LoadLazySpecializations();
  351. return getCommonPtr()->Specializations;
  352. }
  353. FunctionDecl *
  354. FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
  355. void *&InsertPos) {
  356. return findSpecializationImpl(getSpecializations(), InsertPos, Args);
  357. }
  358. void FunctionTemplateDecl::addSpecialization(
  359. FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
  360. addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info,
  361. InsertPos);
  362. }
  363. void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
  364. using Base = RedeclarableTemplateDecl;
  365. // If we haven't created a common pointer yet, then it can just be created
  366. // with the usual method.
  367. if (!Base::Common)
  368. return;
  369. Common *ThisCommon = static_cast<Common *>(Base::Common);
  370. Common *PrevCommon = nullptr;
  371. SmallVector<FunctionTemplateDecl *, 8> PreviousDecls;
  372. for (; Prev; Prev = Prev->getPreviousDecl()) {
  373. if (Prev->Base::Common) {
  374. PrevCommon = static_cast<Common *>(Prev->Base::Common);
  375. break;
  376. }
  377. PreviousDecls.push_back(Prev);
  378. }
  379. // If the previous redecl chain hasn't created a common pointer yet, then just
  380. // use this common pointer.
  381. if (!PrevCommon) {
  382. for (auto *D : PreviousDecls)
  383. D->Base::Common = ThisCommon;
  384. return;
  385. }
  386. // Ensure we don't leak any important state.
  387. assert(ThisCommon->Specializations.size() == 0 &&
  388. "Can't merge incompatible declarations!");
  389. Base::Common = PrevCommon;
  390. }
  391. //===----------------------------------------------------------------------===//
  392. // ClassTemplateDecl Implementation
  393. //===----------------------------------------------------------------------===//
  394. ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, DeclContext *DC,
  395. SourceLocation L,
  396. DeclarationName Name,
  397. TemplateParameterList *Params,
  398. NamedDecl *Decl) {
  399. bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
  400. auto *TD = new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl);
  401. if (Invalid)
  402. TD->setInvalidDecl();
  403. return TD;
  404. }
  405. ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
  406. unsigned ID) {
  407. return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
  408. DeclarationName(), nullptr, nullptr);
  409. }
  410. void ClassTemplateDecl::LoadLazySpecializations() const {
  411. loadLazySpecializationsImpl();
  412. }
  413. llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
  414. ClassTemplateDecl::getSpecializations() const {
  415. LoadLazySpecializations();
  416. return getCommonPtr()->Specializations;
  417. }
  418. llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
  419. ClassTemplateDecl::getPartialSpecializations() const {
  420. LoadLazySpecializations();
  421. return getCommonPtr()->PartialSpecializations;
  422. }
  423. RedeclarableTemplateDecl::CommonBase *
  424. ClassTemplateDecl::newCommon(ASTContext &C) const {
  425. auto *CommonPtr = new (C) Common;
  426. C.addDestruction(CommonPtr);
  427. return CommonPtr;
  428. }
  429. ClassTemplateSpecializationDecl *
  430. ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
  431. void *&InsertPos) {
  432. return findSpecializationImpl(getSpecializations(), InsertPos, Args);
  433. }
  434. void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
  435. void *InsertPos) {
  436. addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos);
  437. }
  438. ClassTemplatePartialSpecializationDecl *
  439. ClassTemplateDecl::findPartialSpecialization(
  440. ArrayRef<TemplateArgument> Args,
  441. TemplateParameterList *TPL, void *&InsertPos) {
  442. return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
  443. TPL);
  444. }
  445. static void ProfileTemplateParameterList(ASTContext &C,
  446. llvm::FoldingSetNodeID &ID, const TemplateParameterList *TPL) {
  447. const Expr *RC = TPL->getRequiresClause();
  448. ID.AddBoolean(RC != nullptr);
  449. if (RC)
  450. RC->Profile(ID, C, /*Canonical=*/true);
  451. ID.AddInteger(TPL->size());
  452. for (NamedDecl *D : *TPL) {
  453. if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
  454. ID.AddInteger(0);
  455. ID.AddBoolean(NTTP->isParameterPack());
  456. NTTP->getType().getCanonicalType().Profile(ID);
  457. ID.AddBoolean(NTTP->hasPlaceholderTypeConstraint());
  458. if (const Expr *E = NTTP->getPlaceholderTypeConstraint())
  459. E->Profile(ID, C, /*Canonical=*/true);
  460. continue;
  461. }
  462. if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) {
  463. ID.AddInteger(1);
  464. ID.AddBoolean(TTP->isParameterPack());
  465. ID.AddBoolean(TTP->hasTypeConstraint());
  466. if (const TypeConstraint *TC = TTP->getTypeConstraint())
  467. TC->getImmediatelyDeclaredConstraint()->Profile(ID, C,
  468. /*Canonical=*/true);
  469. continue;
  470. }
  471. const auto *TTP = cast<TemplateTemplateParmDecl>(D);
  472. ID.AddInteger(2);
  473. ID.AddBoolean(TTP->isParameterPack());
  474. ProfileTemplateParameterList(C, ID, TTP->getTemplateParameters());
  475. }
  476. }
  477. void
  478. ClassTemplatePartialSpecializationDecl::Profile(llvm::FoldingSetNodeID &ID,
  479. ArrayRef<TemplateArgument> TemplateArgs, TemplateParameterList *TPL,
  480. ASTContext &Context) {
  481. ID.AddInteger(TemplateArgs.size());
  482. for (const TemplateArgument &TemplateArg : TemplateArgs)
  483. TemplateArg.Profile(ID, Context);
  484. ProfileTemplateParameterList(Context, ID, TPL);
  485. }
  486. void ClassTemplateDecl::AddPartialSpecialization(
  487. ClassTemplatePartialSpecializationDecl *D,
  488. void *InsertPos) {
  489. if (InsertPos)
  490. getPartialSpecializations().InsertNode(D, InsertPos);
  491. else {
  492. ClassTemplatePartialSpecializationDecl *Existing
  493. = getPartialSpecializations().GetOrInsertNode(D);
  494. (void)Existing;
  495. assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
  496. }
  497. if (ASTMutationListener *L = getASTMutationListener())
  498. L->AddedCXXTemplateSpecialization(this, D);
  499. }
  500. void ClassTemplateDecl::getPartialSpecializations(
  501. SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) const {
  502. llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
  503. = getPartialSpecializations();
  504. PS.clear();
  505. PS.reserve(PartialSpecs.size());
  506. for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs)
  507. PS.push_back(P.getMostRecentDecl());
  508. }
  509. ClassTemplatePartialSpecializationDecl *
  510. ClassTemplateDecl::findPartialSpecialization(QualType T) {
  511. ASTContext &Context = getASTContext();
  512. for (ClassTemplatePartialSpecializationDecl &P :
  513. getPartialSpecializations()) {
  514. if (Context.hasSameType(P.getInjectedSpecializationType(), T))
  515. return P.getMostRecentDecl();
  516. }
  517. return nullptr;
  518. }
  519. ClassTemplatePartialSpecializationDecl *
  520. ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
  521. ClassTemplatePartialSpecializationDecl *D) {
  522. Decl *DCanon = D->getCanonicalDecl();
  523. for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
  524. if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
  525. return P.getMostRecentDecl();
  526. }
  527. return nullptr;
  528. }
  529. QualType
  530. ClassTemplateDecl::getInjectedClassNameSpecialization() {
  531. Common *CommonPtr = getCommonPtr();
  532. if (!CommonPtr->InjectedClassNameType.isNull())
  533. return CommonPtr->InjectedClassNameType;
  534. // C++0x [temp.dep.type]p2:
  535. // The template argument list of a primary template is a template argument
  536. // list in which the nth template argument has the value of the nth template
  537. // parameter of the class template. If the nth template parameter is a
  538. // template parameter pack (14.5.3), the nth template argument is a pack
  539. // expansion (14.5.3) whose pattern is the name of the template parameter
  540. // pack.
  541. ASTContext &Context = getASTContext();
  542. TemplateParameterList *Params = getTemplateParameters();
  543. SmallVector<TemplateArgument, 16> TemplateArgs;
  544. Context.getInjectedTemplateArgs(Params, TemplateArgs);
  545. CommonPtr->InjectedClassNameType
  546. = Context.getTemplateSpecializationType(TemplateName(this),
  547. TemplateArgs);
  548. return CommonPtr->InjectedClassNameType;
  549. }
  550. //===----------------------------------------------------------------------===//
  551. // TemplateTypeParm Allocation/Deallocation Method Implementations
  552. //===----------------------------------------------------------------------===//
  553. TemplateTypeParmDecl *TemplateTypeParmDecl::Create(
  554. const ASTContext &C, DeclContext *DC, SourceLocation KeyLoc,
  555. SourceLocation NameLoc, unsigned D, unsigned P, IdentifierInfo *Id,
  556. bool Typename, bool ParameterPack, bool HasTypeConstraint,
  557. std::optional<unsigned> NumExpanded) {
  558. auto *TTPDecl =
  559. new (C, DC,
  560. additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 1 : 0))
  561. TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename,
  562. HasTypeConstraint, NumExpanded);
  563. QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
  564. TTPDecl->setTypeForDecl(TTPType.getTypePtr());
  565. return TTPDecl;
  566. }
  567. TemplateTypeParmDecl *
  568. TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
  569. return new (C, ID)
  570. TemplateTypeParmDecl(nullptr, SourceLocation(), SourceLocation(), nullptr,
  571. false, false, std::nullopt);
  572. }
  573. TemplateTypeParmDecl *
  574. TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID,
  575. bool HasTypeConstraint) {
  576. return new (C, ID,
  577. additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 1 : 0))
  578. TemplateTypeParmDecl(nullptr, SourceLocation(), SourceLocation(), nullptr,
  579. false, HasTypeConstraint, std::nullopt);
  580. }
  581. SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
  582. return hasDefaultArgument()
  583. ? getDefaultArgumentInfo()->getTypeLoc().getBeginLoc()
  584. : SourceLocation();
  585. }
  586. SourceRange TemplateTypeParmDecl::getSourceRange() const {
  587. if (hasDefaultArgument() && !defaultArgumentWasInherited())
  588. return SourceRange(getBeginLoc(),
  589. getDefaultArgumentInfo()->getTypeLoc().getEndLoc());
  590. // TypeDecl::getSourceRange returns a range containing name location, which is
  591. // wrong for unnamed template parameters. e.g:
  592. // it will return <[[typename>]] instead of <[[typename]]>
  593. else if (getDeclName().isEmpty())
  594. return SourceRange(getBeginLoc());
  595. return TypeDecl::getSourceRange();
  596. }
  597. unsigned TemplateTypeParmDecl::getDepth() const {
  598. return getTypeForDecl()->castAs<TemplateTypeParmType>()->getDepth();
  599. }
  600. unsigned TemplateTypeParmDecl::getIndex() const {
  601. return getTypeForDecl()->castAs<TemplateTypeParmType>()->getIndex();
  602. }
  603. bool TemplateTypeParmDecl::isParameterPack() const {
  604. return getTypeForDecl()->castAs<TemplateTypeParmType>()->isParameterPack();
  605. }
  606. void TemplateTypeParmDecl::setTypeConstraint(NestedNameSpecifierLoc NNS,
  607. DeclarationNameInfo NameInfo, NamedDecl *FoundDecl, ConceptDecl *CD,
  608. const ASTTemplateArgumentListInfo *ArgsAsWritten,
  609. Expr *ImmediatelyDeclaredConstraint) {
  610. assert(HasTypeConstraint &&
  611. "HasTypeConstraint=true must be passed at construction in order to "
  612. "call setTypeConstraint");
  613. assert(!TypeConstraintInitialized &&
  614. "TypeConstraint was already initialized!");
  615. new (getTrailingObjects<TypeConstraint>()) TypeConstraint(NNS, NameInfo,
  616. FoundDecl, CD, ArgsAsWritten, ImmediatelyDeclaredConstraint);
  617. TypeConstraintInitialized = true;
  618. }
  619. //===----------------------------------------------------------------------===//
  620. // NonTypeTemplateParmDecl Method Implementations
  621. //===----------------------------------------------------------------------===//
  622. NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(
  623. DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D,
  624. unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
  625. ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos)
  626. : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
  627. TemplateParmPosition(D, P), ParameterPack(true),
  628. ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) {
  629. if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) {
  630. auto TypesAndInfos =
  631. getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
  632. for (unsigned I = 0; I != NumExpandedTypes; ++I) {
  633. new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
  634. TypesAndInfos[I].second = ExpandedTInfos[I];
  635. }
  636. }
  637. }
  638. NonTypeTemplateParmDecl *
  639. NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
  640. SourceLocation StartLoc, SourceLocation IdLoc,
  641. unsigned D, unsigned P, IdentifierInfo *Id,
  642. QualType T, bool ParameterPack,
  643. TypeSourceInfo *TInfo) {
  644. AutoType *AT =
  645. C.getLangOpts().CPlusPlus20 ? T->getContainedAutoType() : nullptr;
  646. return new (C, DC,
  647. additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
  648. Expr *>(0,
  649. AT && AT->isConstrained() ? 1 : 0))
  650. NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, ParameterPack,
  651. TInfo);
  652. }
  653. NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
  654. const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
  655. SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
  656. QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
  657. ArrayRef<TypeSourceInfo *> ExpandedTInfos) {
  658. AutoType *AT = TInfo->getType()->getContainedAutoType();
  659. return new (C, DC,
  660. additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
  661. Expr *>(
  662. ExpandedTypes.size(), AT && AT->isConstrained() ? 1 : 0))
  663. NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
  664. ExpandedTypes, ExpandedTInfos);
  665. }
  666. NonTypeTemplateParmDecl *
  667. NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  668. bool HasTypeConstraint) {
  669. return new (C, ID, additionalSizeToAlloc<std::pair<QualType,
  670. TypeSourceInfo *>,
  671. Expr *>(0,
  672. HasTypeConstraint ? 1 : 0))
  673. NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
  674. 0, 0, nullptr, QualType(), false, nullptr);
  675. }
  676. NonTypeTemplateParmDecl *
  677. NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  678. unsigned NumExpandedTypes,
  679. bool HasTypeConstraint) {
  680. auto *NTTP =
  681. new (C, ID,
  682. additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>, Expr *>(
  683. NumExpandedTypes, HasTypeConstraint ? 1 : 0))
  684. NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
  685. 0, 0, nullptr, QualType(), nullptr,
  686. std::nullopt, std::nullopt);
  687. NTTP->NumExpandedTypes = NumExpandedTypes;
  688. return NTTP;
  689. }
  690. SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
  691. if (hasDefaultArgument() && !defaultArgumentWasInherited())
  692. return SourceRange(getOuterLocStart(),
  693. getDefaultArgument()->getSourceRange().getEnd());
  694. return DeclaratorDecl::getSourceRange();
  695. }
  696. SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
  697. return hasDefaultArgument()
  698. ? getDefaultArgument()->getSourceRange().getBegin()
  699. : SourceLocation();
  700. }
  701. //===----------------------------------------------------------------------===//
  702. // TemplateTemplateParmDecl Method Implementations
  703. //===----------------------------------------------------------------------===//
  704. void TemplateTemplateParmDecl::anchor() {}
  705. TemplateTemplateParmDecl::TemplateTemplateParmDecl(
  706. DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
  707. IdentifierInfo *Id, TemplateParameterList *Params,
  708. ArrayRef<TemplateParameterList *> Expansions)
  709. : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
  710. TemplateParmPosition(D, P), ParameterPack(true),
  711. ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) {
  712. if (!Expansions.empty())
  713. std::uninitialized_copy(Expansions.begin(), Expansions.end(),
  714. getTrailingObjects<TemplateParameterList *>());
  715. }
  716. TemplateTemplateParmDecl *
  717. TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
  718. SourceLocation L, unsigned D, unsigned P,
  719. bool ParameterPack, IdentifierInfo *Id,
  720. TemplateParameterList *Params) {
  721. return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
  722. Params);
  723. }
  724. TemplateTemplateParmDecl *
  725. TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
  726. SourceLocation L, unsigned D, unsigned P,
  727. IdentifierInfo *Id,
  728. TemplateParameterList *Params,
  729. ArrayRef<TemplateParameterList *> Expansions) {
  730. return new (C, DC,
  731. additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
  732. TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions);
  733. }
  734. TemplateTemplateParmDecl *
  735. TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  736. return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
  737. false, nullptr, nullptr);
  738. }
  739. TemplateTemplateParmDecl *
  740. TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  741. unsigned NumExpansions) {
  742. auto *TTP =
  743. new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
  744. TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
  745. nullptr, std::nullopt);
  746. TTP->NumExpandedParams = NumExpansions;
  747. return TTP;
  748. }
  749. SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
  750. return hasDefaultArgument() ? getDefaultArgument().getLocation()
  751. : SourceLocation();
  752. }
  753. void TemplateTemplateParmDecl::setDefaultArgument(
  754. const ASTContext &C, const TemplateArgumentLoc &DefArg) {
  755. if (DefArg.getArgument().isNull())
  756. DefaultArgument.set(nullptr);
  757. else
  758. DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
  759. }
  760. //===----------------------------------------------------------------------===//
  761. // TemplateArgumentList Implementation
  762. //===----------------------------------------------------------------------===//
  763. TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
  764. : Arguments(getTrailingObjects<TemplateArgument>()),
  765. NumArguments(Args.size()) {
  766. std::uninitialized_copy(Args.begin(), Args.end(),
  767. getTrailingObjects<TemplateArgument>());
  768. }
  769. TemplateArgumentList *
  770. TemplateArgumentList::CreateCopy(ASTContext &Context,
  771. ArrayRef<TemplateArgument> Args) {
  772. void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
  773. return new (Mem) TemplateArgumentList(Args);
  774. }
  775. FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
  776. ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
  777. TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs,
  778. const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI,
  779. MemberSpecializationInfo *MSInfo) {
  780. const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
  781. if (TemplateArgsAsWritten)
  782. ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
  783. *TemplateArgsAsWritten);
  784. void *Mem =
  785. C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 1 : 0));
  786. return new (Mem) FunctionTemplateSpecializationInfo(
  787. FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo);
  788. }
  789. //===----------------------------------------------------------------------===//
  790. // ClassTemplateSpecializationDecl Implementation
  791. //===----------------------------------------------------------------------===//
  792. ClassTemplateSpecializationDecl::
  793. ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
  794. DeclContext *DC, SourceLocation StartLoc,
  795. SourceLocation IdLoc,
  796. ClassTemplateDecl *SpecializedTemplate,
  797. ArrayRef<TemplateArgument> Args,
  798. ClassTemplateSpecializationDecl *PrevDecl)
  799. : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
  800. SpecializedTemplate->getIdentifier(), PrevDecl),
  801. SpecializedTemplate(SpecializedTemplate),
  802. TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
  803. SpecializationKind(TSK_Undeclared) {
  804. }
  805. ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
  806. Kind DK)
  807. : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
  808. SourceLocation(), nullptr, nullptr),
  809. SpecializationKind(TSK_Undeclared) {}
  810. ClassTemplateSpecializationDecl *
  811. ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
  812. DeclContext *DC,
  813. SourceLocation StartLoc,
  814. SourceLocation IdLoc,
  815. ClassTemplateDecl *SpecializedTemplate,
  816. ArrayRef<TemplateArgument> Args,
  817. ClassTemplateSpecializationDecl *PrevDecl) {
  818. auto *Result =
  819. new (Context, DC) ClassTemplateSpecializationDecl(
  820. Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
  821. SpecializedTemplate, Args, PrevDecl);
  822. Result->setMayHaveOutOfDateDef(false);
  823. // If the template decl is incomplete, copy the external lexical storage from
  824. // the base template. This allows instantiations of incomplete types to
  825. // complete using the external AST if the template's declaration came from an
  826. // external AST.
  827. if (!SpecializedTemplate->getTemplatedDecl()->isCompleteDefinition())
  828. Result->setHasExternalLexicalStorage(
  829. SpecializedTemplate->getTemplatedDecl()->hasExternalLexicalStorage());
  830. Context.getTypeDeclType(Result, PrevDecl);
  831. return Result;
  832. }
  833. ClassTemplateSpecializationDecl *
  834. ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
  835. unsigned ID) {
  836. auto *Result =
  837. new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
  838. Result->setMayHaveOutOfDateDef(false);
  839. return Result;
  840. }
  841. void ClassTemplateSpecializationDecl::getNameForDiagnostic(
  842. raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
  843. NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
  844. const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this);
  845. if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
  846. PS ? PS->getTemplateArgsAsWritten() : nullptr) {
  847. printTemplateArgumentList(
  848. OS, ArgsAsWritten->arguments(), Policy,
  849. getSpecializedTemplate()->getTemplateParameters());
  850. } else {
  851. const TemplateArgumentList &TemplateArgs = getTemplateArgs();
  852. printTemplateArgumentList(
  853. OS, TemplateArgs.asArray(), Policy,
  854. getSpecializedTemplate()->getTemplateParameters());
  855. }
  856. }
  857. ClassTemplateDecl *
  858. ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
  859. if (const auto *PartialSpec =
  860. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
  861. return PartialSpec->PartialSpecialization->getSpecializedTemplate();
  862. return SpecializedTemplate.get<ClassTemplateDecl*>();
  863. }
  864. SourceRange
  865. ClassTemplateSpecializationDecl::getSourceRange() const {
  866. if (ExplicitInfo) {
  867. SourceLocation Begin = getTemplateKeywordLoc();
  868. if (Begin.isValid()) {
  869. // Here we have an explicit (partial) specialization or instantiation.
  870. assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
  871. getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
  872. getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
  873. if (getExternLoc().isValid())
  874. Begin = getExternLoc();
  875. SourceLocation End = getBraceRange().getEnd();
  876. if (End.isInvalid())
  877. End = getTypeAsWritten()->getTypeLoc().getEndLoc();
  878. return SourceRange(Begin, End);
  879. }
  880. // An implicit instantiation of a class template partial specialization
  881. // uses ExplicitInfo to record the TypeAsWritten, but the source
  882. // locations should be retrieved from the instantiation pattern.
  883. using CTPSDecl = ClassTemplatePartialSpecializationDecl;
  884. auto *ctpsd = const_cast<CTPSDecl *>(cast<CTPSDecl>(this));
  885. CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
  886. assert(inst_from != nullptr);
  887. return inst_from->getSourceRange();
  888. }
  889. else {
  890. // No explicit info available.
  891. llvm::PointerUnion<ClassTemplateDecl *,
  892. ClassTemplatePartialSpecializationDecl *>
  893. inst_from = getInstantiatedFrom();
  894. if (inst_from.isNull())
  895. return getSpecializedTemplate()->getSourceRange();
  896. if (const auto *ctd = inst_from.dyn_cast<ClassTemplateDecl *>())
  897. return ctd->getSourceRange();
  898. return inst_from.get<ClassTemplatePartialSpecializationDecl *>()
  899. ->getSourceRange();
  900. }
  901. }
  902. //===----------------------------------------------------------------------===//
  903. // ConceptDecl Implementation
  904. //===----------------------------------------------------------------------===//
  905. ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC,
  906. SourceLocation L, DeclarationName Name,
  907. TemplateParameterList *Params,
  908. Expr *ConstraintExpr) {
  909. bool Invalid = AdoptTemplateParameterList(Params, DC);
  910. auto *TD = new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr);
  911. if (Invalid)
  912. TD->setInvalidDecl();
  913. return TD;
  914. }
  915. ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C,
  916. unsigned ID) {
  917. ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(),
  918. DeclarationName(),
  919. nullptr, nullptr);
  920. return Result;
  921. }
  922. //===----------------------------------------------------------------------===//
  923. // ImplicitConceptSpecializationDecl Implementation
  924. //===----------------------------------------------------------------------===//
  925. ImplicitConceptSpecializationDecl::ImplicitConceptSpecializationDecl(
  926. DeclContext *DC, SourceLocation SL,
  927. ArrayRef<TemplateArgument> ConvertedArgs)
  928. : Decl(ImplicitConceptSpecialization, DC, SL),
  929. NumTemplateArgs(ConvertedArgs.size()) {
  930. setTemplateArguments(ConvertedArgs);
  931. }
  932. ImplicitConceptSpecializationDecl::ImplicitConceptSpecializationDecl(
  933. EmptyShell Empty, unsigned NumTemplateArgs)
  934. : Decl(ImplicitConceptSpecialization, Empty),
  935. NumTemplateArgs(NumTemplateArgs) {}
  936. ImplicitConceptSpecializationDecl *ImplicitConceptSpecializationDecl::Create(
  937. const ASTContext &C, DeclContext *DC, SourceLocation SL,
  938. ArrayRef<TemplateArgument> ConvertedArgs) {
  939. return new (C, DC,
  940. additionalSizeToAlloc<TemplateArgument>(ConvertedArgs.size()))
  941. ImplicitConceptSpecializationDecl(DC, SL, ConvertedArgs);
  942. }
  943. ImplicitConceptSpecializationDecl *
  944. ImplicitConceptSpecializationDecl::CreateDeserialized(
  945. const ASTContext &C, unsigned ID, unsigned NumTemplateArgs) {
  946. return new (C, ID, additionalSizeToAlloc<TemplateArgument>(NumTemplateArgs))
  947. ImplicitConceptSpecializationDecl(EmptyShell{}, NumTemplateArgs);
  948. }
  949. void ImplicitConceptSpecializationDecl::setTemplateArguments(
  950. ArrayRef<TemplateArgument> Converted) {
  951. assert(Converted.size() == NumTemplateArgs);
  952. std::uninitialized_copy(Converted.begin(), Converted.end(),
  953. getTrailingObjects<TemplateArgument>());
  954. }
  955. //===----------------------------------------------------------------------===//
  956. // ClassTemplatePartialSpecializationDecl Implementation
  957. //===----------------------------------------------------------------------===//
  958. void ClassTemplatePartialSpecializationDecl::anchor() {}
  959. ClassTemplatePartialSpecializationDecl::
  960. ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
  961. DeclContext *DC,
  962. SourceLocation StartLoc,
  963. SourceLocation IdLoc,
  964. TemplateParameterList *Params,
  965. ClassTemplateDecl *SpecializedTemplate,
  966. ArrayRef<TemplateArgument> Args,
  967. const ASTTemplateArgumentListInfo *ArgInfos,
  968. ClassTemplatePartialSpecializationDecl *PrevDecl)
  969. : ClassTemplateSpecializationDecl(Context,
  970. ClassTemplatePartialSpecialization,
  971. TK, DC, StartLoc, IdLoc,
  972. SpecializedTemplate, Args, PrevDecl),
  973. TemplateParams(Params), ArgsAsWritten(ArgInfos),
  974. InstantiatedFromMember(nullptr, false) {
  975. if (AdoptTemplateParameterList(Params, this))
  976. setInvalidDecl();
  977. }
  978. ClassTemplatePartialSpecializationDecl *
  979. ClassTemplatePartialSpecializationDecl::
  980. Create(ASTContext &Context, TagKind TK,DeclContext *DC,
  981. SourceLocation StartLoc, SourceLocation IdLoc,
  982. TemplateParameterList *Params,
  983. ClassTemplateDecl *SpecializedTemplate,
  984. ArrayRef<TemplateArgument> Args,
  985. const TemplateArgumentListInfo &ArgInfos,
  986. QualType CanonInjectedType,
  987. ClassTemplatePartialSpecializationDecl *PrevDecl) {
  988. const ASTTemplateArgumentListInfo *ASTArgInfos =
  989. ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
  990. auto *Result = new (Context, DC)
  991. ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
  992. Params, SpecializedTemplate, Args,
  993. ASTArgInfos, PrevDecl);
  994. Result->setSpecializationKind(TSK_ExplicitSpecialization);
  995. Result->setMayHaveOutOfDateDef(false);
  996. Context.getInjectedClassNameType(Result, CanonInjectedType);
  997. return Result;
  998. }
  999. ClassTemplatePartialSpecializationDecl *
  1000. ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
  1001. unsigned ID) {
  1002. auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C);
  1003. Result->setMayHaveOutOfDateDef(false);
  1004. return Result;
  1005. }
  1006. //===----------------------------------------------------------------------===//
  1007. // FriendTemplateDecl Implementation
  1008. //===----------------------------------------------------------------------===//
  1009. void FriendTemplateDecl::anchor() {}
  1010. FriendTemplateDecl *
  1011. FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
  1012. SourceLocation L,
  1013. MutableArrayRef<TemplateParameterList *> Params,
  1014. FriendUnion Friend, SourceLocation FLoc) {
  1015. TemplateParameterList **TPL = nullptr;
  1016. if (!Params.empty()) {
  1017. TPL = new (Context) TemplateParameterList *[Params.size()];
  1018. llvm::copy(Params, TPL);
  1019. }
  1020. return new (Context, DC)
  1021. FriendTemplateDecl(DC, L, TPL, Params.size(), Friend, FLoc);
  1022. }
  1023. FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
  1024. unsigned ID) {
  1025. return new (C, ID) FriendTemplateDecl(EmptyShell());
  1026. }
  1027. //===----------------------------------------------------------------------===//
  1028. // TypeAliasTemplateDecl Implementation
  1029. //===----------------------------------------------------------------------===//
  1030. TypeAliasTemplateDecl *
  1031. TypeAliasTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
  1032. DeclarationName Name,
  1033. TemplateParameterList *Params, NamedDecl *Decl) {
  1034. bool Invalid = AdoptTemplateParameterList(Params, DC);
  1035. auto *TD = new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
  1036. if (Invalid)
  1037. TD->setInvalidDecl();
  1038. return TD;
  1039. }
  1040. TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
  1041. unsigned ID) {
  1042. return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
  1043. DeclarationName(), nullptr, nullptr);
  1044. }
  1045. RedeclarableTemplateDecl::CommonBase *
  1046. TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
  1047. auto *CommonPtr = new (C) Common;
  1048. C.addDestruction(CommonPtr);
  1049. return CommonPtr;
  1050. }
  1051. //===----------------------------------------------------------------------===//
  1052. // ClassScopeFunctionSpecializationDecl Implementation
  1053. //===----------------------------------------------------------------------===//
  1054. void ClassScopeFunctionSpecializationDecl::anchor() {}
  1055. ClassScopeFunctionSpecializationDecl *
  1056. ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
  1057. unsigned ID) {
  1058. return new (C, ID) ClassScopeFunctionSpecializationDecl(
  1059. nullptr, SourceLocation(), nullptr, nullptr);
  1060. }
  1061. //===----------------------------------------------------------------------===//
  1062. // VarTemplateDecl Implementation
  1063. //===----------------------------------------------------------------------===//
  1064. VarTemplateDecl *VarTemplateDecl::getDefinition() {
  1065. VarTemplateDecl *CurD = this;
  1066. while (CurD) {
  1067. if (CurD->isThisDeclarationADefinition())
  1068. return CurD;
  1069. CurD = CurD->getPreviousDecl();
  1070. }
  1071. return nullptr;
  1072. }
  1073. VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
  1074. SourceLocation L, DeclarationName Name,
  1075. TemplateParameterList *Params,
  1076. VarDecl *Decl) {
  1077. bool Invalid = AdoptTemplateParameterList(Params, DC);
  1078. auto *TD = new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
  1079. if (Invalid)
  1080. TD->setInvalidDecl();
  1081. return TD;
  1082. }
  1083. VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
  1084. unsigned ID) {
  1085. return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
  1086. DeclarationName(), nullptr, nullptr);
  1087. }
  1088. void VarTemplateDecl::LoadLazySpecializations() const {
  1089. loadLazySpecializationsImpl();
  1090. }
  1091. llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
  1092. VarTemplateDecl::getSpecializations() const {
  1093. LoadLazySpecializations();
  1094. return getCommonPtr()->Specializations;
  1095. }
  1096. llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
  1097. VarTemplateDecl::getPartialSpecializations() const {
  1098. LoadLazySpecializations();
  1099. return getCommonPtr()->PartialSpecializations;
  1100. }
  1101. RedeclarableTemplateDecl::CommonBase *
  1102. VarTemplateDecl::newCommon(ASTContext &C) const {
  1103. auto *CommonPtr = new (C) Common;
  1104. C.addDestruction(CommonPtr);
  1105. return CommonPtr;
  1106. }
  1107. VarTemplateSpecializationDecl *
  1108. VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
  1109. void *&InsertPos) {
  1110. return findSpecializationImpl(getSpecializations(), InsertPos, Args);
  1111. }
  1112. void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
  1113. void *InsertPos) {
  1114. addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos);
  1115. }
  1116. VarTemplatePartialSpecializationDecl *
  1117. VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
  1118. TemplateParameterList *TPL, void *&InsertPos) {
  1119. return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
  1120. TPL);
  1121. }
  1122. void
  1123. VarTemplatePartialSpecializationDecl::Profile(llvm::FoldingSetNodeID &ID,
  1124. ArrayRef<TemplateArgument> TemplateArgs, TemplateParameterList *TPL,
  1125. ASTContext &Context) {
  1126. ID.AddInteger(TemplateArgs.size());
  1127. for (const TemplateArgument &TemplateArg : TemplateArgs)
  1128. TemplateArg.Profile(ID, Context);
  1129. ProfileTemplateParameterList(Context, ID, TPL);
  1130. }
  1131. void VarTemplateDecl::AddPartialSpecialization(
  1132. VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
  1133. if (InsertPos)
  1134. getPartialSpecializations().InsertNode(D, InsertPos);
  1135. else {
  1136. VarTemplatePartialSpecializationDecl *Existing =
  1137. getPartialSpecializations().GetOrInsertNode(D);
  1138. (void)Existing;
  1139. assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
  1140. }
  1141. if (ASTMutationListener *L = getASTMutationListener())
  1142. L->AddedCXXTemplateSpecialization(this, D);
  1143. }
  1144. void VarTemplateDecl::getPartialSpecializations(
  1145. SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const {
  1146. llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
  1147. getPartialSpecializations();
  1148. PS.clear();
  1149. PS.reserve(PartialSpecs.size());
  1150. for (VarTemplatePartialSpecializationDecl &P : PartialSpecs)
  1151. PS.push_back(P.getMostRecentDecl());
  1152. }
  1153. VarTemplatePartialSpecializationDecl *
  1154. VarTemplateDecl::findPartialSpecInstantiatedFromMember(
  1155. VarTemplatePartialSpecializationDecl *D) {
  1156. Decl *DCanon = D->getCanonicalDecl();
  1157. for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
  1158. if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
  1159. return P.getMostRecentDecl();
  1160. }
  1161. return nullptr;
  1162. }
  1163. //===----------------------------------------------------------------------===//
  1164. // VarTemplateSpecializationDecl Implementation
  1165. //===----------------------------------------------------------------------===//
  1166. VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
  1167. Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  1168. SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
  1169. TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args)
  1170. : VarDecl(DK, Context, DC, StartLoc, IdLoc,
  1171. SpecializedTemplate->getIdentifier(), T, TInfo, S),
  1172. SpecializedTemplate(SpecializedTemplate),
  1173. TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
  1174. SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
  1175. VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
  1176. ASTContext &C)
  1177. : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
  1178. QualType(), nullptr, SC_None),
  1179. SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
  1180. VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
  1181. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  1182. SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
  1183. TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) {
  1184. return new (Context, DC) VarTemplateSpecializationDecl(
  1185. VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
  1186. SpecializedTemplate, T, TInfo, S, Args);
  1187. }
  1188. VarTemplateSpecializationDecl *
  1189. VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1190. return new (C, ID)
  1191. VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
  1192. }
  1193. void VarTemplateSpecializationDecl::getNameForDiagnostic(
  1194. raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
  1195. NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
  1196. const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this);
  1197. if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
  1198. PS ? PS->getTemplateArgsAsWritten() : nullptr) {
  1199. printTemplateArgumentList(
  1200. OS, ArgsAsWritten->arguments(), Policy,
  1201. getSpecializedTemplate()->getTemplateParameters());
  1202. } else {
  1203. const TemplateArgumentList &TemplateArgs = getTemplateArgs();
  1204. printTemplateArgumentList(
  1205. OS, TemplateArgs.asArray(), Policy,
  1206. getSpecializedTemplate()->getTemplateParameters());
  1207. }
  1208. }
  1209. VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
  1210. if (const auto *PartialSpec =
  1211. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
  1212. return PartialSpec->PartialSpecialization->getSpecializedTemplate();
  1213. return SpecializedTemplate.get<VarTemplateDecl *>();
  1214. }
  1215. void VarTemplateSpecializationDecl::setTemplateArgsInfo(
  1216. const TemplateArgumentListInfo &ArgsInfo) {
  1217. TemplateArgsInfo =
  1218. ASTTemplateArgumentListInfo::Create(getASTContext(), ArgsInfo);
  1219. }
  1220. void VarTemplateSpecializationDecl::setTemplateArgsInfo(
  1221. const ASTTemplateArgumentListInfo *ArgsInfo) {
  1222. TemplateArgsInfo =
  1223. ASTTemplateArgumentListInfo::Create(getASTContext(), ArgsInfo);
  1224. }
  1225. //===----------------------------------------------------------------------===//
  1226. // VarTemplatePartialSpecializationDecl Implementation
  1227. //===----------------------------------------------------------------------===//
  1228. void VarTemplatePartialSpecializationDecl::anchor() {}
  1229. VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
  1230. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  1231. SourceLocation IdLoc, TemplateParameterList *Params,
  1232. VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
  1233. StorageClass S, ArrayRef<TemplateArgument> Args,
  1234. const ASTTemplateArgumentListInfo *ArgInfos)
  1235. : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
  1236. DC, StartLoc, IdLoc, SpecializedTemplate, T,
  1237. TInfo, S, Args),
  1238. TemplateParams(Params), ArgsAsWritten(ArgInfos),
  1239. InstantiatedFromMember(nullptr, false) {
  1240. if (AdoptTemplateParameterList(Params, DC))
  1241. setInvalidDecl();
  1242. }
  1243. VarTemplatePartialSpecializationDecl *
  1244. VarTemplatePartialSpecializationDecl::Create(
  1245. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  1246. SourceLocation IdLoc, TemplateParameterList *Params,
  1247. VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
  1248. StorageClass S, ArrayRef<TemplateArgument> Args,
  1249. const TemplateArgumentListInfo &ArgInfos) {
  1250. const ASTTemplateArgumentListInfo *ASTArgInfos
  1251. = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
  1252. auto *Result =
  1253. new (Context, DC) VarTemplatePartialSpecializationDecl(
  1254. Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
  1255. S, Args, ASTArgInfos);
  1256. Result->setSpecializationKind(TSK_ExplicitSpecialization);
  1257. return Result;
  1258. }
  1259. VarTemplatePartialSpecializationDecl *
  1260. VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
  1261. unsigned ID) {
  1262. return new (C, ID) VarTemplatePartialSpecializationDecl(C);
  1263. }
  1264. static TemplateParameterList *
  1265. createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
  1266. // typename T
  1267. auto *T = TemplateTypeParmDecl::Create(
  1268. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
  1269. /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
  1270. /*HasTypeConstraint=*/false);
  1271. T->setImplicit(true);
  1272. // T ...Ints
  1273. TypeSourceInfo *TI =
  1274. C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
  1275. auto *N = NonTypeTemplateParmDecl::Create(
  1276. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
  1277. /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
  1278. N->setImplicit(true);
  1279. // <typename T, T ...Ints>
  1280. NamedDecl *P[2] = {T, N};
  1281. auto *TPL = TemplateParameterList::Create(
  1282. C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
  1283. // template <typename T, ...Ints> class IntSeq
  1284. auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
  1285. C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
  1286. /*ParameterPack=*/false, /*Id=*/nullptr, TPL);
  1287. TemplateTemplateParm->setImplicit(true);
  1288. // typename T
  1289. auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
  1290. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
  1291. /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
  1292. /*HasTypeConstraint=*/false);
  1293. TemplateTypeParm->setImplicit(true);
  1294. // T N
  1295. TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
  1296. QualType(TemplateTypeParm->getTypeForDecl(), 0));
  1297. auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
  1298. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
  1299. /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
  1300. NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
  1301. NonTypeTemplateParm};
  1302. // template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
  1303. return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
  1304. Params, SourceLocation(), nullptr);
  1305. }
  1306. static TemplateParameterList *
  1307. createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
  1308. // std::size_t Index
  1309. TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
  1310. auto *Index = NonTypeTemplateParmDecl::Create(
  1311. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
  1312. /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
  1313. // typename ...T
  1314. auto *Ts = TemplateTypeParmDecl::Create(
  1315. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
  1316. /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true,
  1317. /*HasTypeConstraint=*/false);
  1318. Ts->setImplicit(true);
  1319. // template <std::size_t Index, typename ...T>
  1320. NamedDecl *Params[] = {Index, Ts};
  1321. return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
  1322. llvm::ArrayRef(Params), SourceLocation(),
  1323. nullptr);
  1324. }
  1325. static TemplateParameterList *createBuiltinTemplateParameterList(
  1326. const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
  1327. switch (BTK) {
  1328. case BTK__make_integer_seq:
  1329. return createMakeIntegerSeqParameterList(C, DC);
  1330. case BTK__type_pack_element:
  1331. return createTypePackElementParameterList(C, DC);
  1332. }
  1333. llvm_unreachable("unhandled BuiltinTemplateKind!");
  1334. }
  1335. void BuiltinTemplateDecl::anchor() {}
  1336. BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
  1337. DeclarationName Name,
  1338. BuiltinTemplateKind BTK)
  1339. : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name,
  1340. createBuiltinTemplateParameterList(C, DC, BTK)),
  1341. BTK(BTK) {}
  1342. void TypeConstraint::print(llvm::raw_ostream &OS, PrintingPolicy Policy) const {
  1343. if (NestedNameSpec)
  1344. NestedNameSpec.getNestedNameSpecifier()->print(OS, Policy);
  1345. ConceptName.printName(OS, Policy);
  1346. if (hasExplicitTemplateArgs()) {
  1347. OS << "<";
  1348. // FIXME: Find corresponding parameter for argument
  1349. for (auto &ArgLoc : ArgsAsWritten->arguments())
  1350. ArgLoc.getArgument().print(Policy, OS, /*IncludeType*/ false);
  1351. OS << ">";
  1352. }
  1353. }
  1354. TemplateParamObjectDecl *TemplateParamObjectDecl::Create(const ASTContext &C,
  1355. QualType T,
  1356. const APValue &V) {
  1357. DeclContext *DC = C.getTranslationUnitDecl();
  1358. auto *TPOD = new (C, DC) TemplateParamObjectDecl(DC, T, V);
  1359. C.addDestruction(&TPOD->Value);
  1360. return TPOD;
  1361. }
  1362. TemplateParamObjectDecl *
  1363. TemplateParamObjectDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1364. auto *TPOD = new (C, ID) TemplateParamObjectDecl(nullptr, QualType(), APValue());
  1365. C.addDestruction(&TPOD->Value);
  1366. return TPOD;
  1367. }
  1368. void TemplateParamObjectDecl::printName(llvm::raw_ostream &OS,
  1369. const PrintingPolicy &Policy) const {
  1370. OS << "<template param ";
  1371. printAsExpr(OS, Policy);
  1372. OS << ">";
  1373. }
  1374. void TemplateParamObjectDecl::printAsExpr(llvm::raw_ostream &OS) const {
  1375. printAsExpr(OS, getASTContext().getPrintingPolicy());
  1376. }
  1377. void TemplateParamObjectDecl::printAsExpr(llvm::raw_ostream &OS,
  1378. const PrintingPolicy &Policy) const {
  1379. getType().getUnqualifiedType().print(OS, Policy);
  1380. printAsInit(OS, Policy);
  1381. }
  1382. void TemplateParamObjectDecl::printAsInit(llvm::raw_ostream &OS) const {
  1383. printAsInit(OS, getASTContext().getPrintingPolicy());
  1384. }
  1385. void TemplateParamObjectDecl::printAsInit(llvm::raw_ostream &OS,
  1386. const PrintingPolicy &Policy) const {
  1387. getValue().printPretty(OS, Policy, getType(), &getASTContext());
  1388. }
  1389. TemplateParameterList *clang::getReplacedTemplateParameterList(Decl *D) {
  1390. switch (D->getKind()) {
  1391. case Decl::Kind::ClassTemplate:
  1392. return cast<ClassTemplateDecl>(D)->getTemplateParameters();
  1393. case Decl::Kind::ClassTemplateSpecialization: {
  1394. const auto *CTSD = cast<ClassTemplateSpecializationDecl>(D);
  1395. auto P = CTSD->getSpecializedTemplateOrPartial();
  1396. if (const auto *CTPSD =
  1397. P.dyn_cast<ClassTemplatePartialSpecializationDecl *>())
  1398. return CTPSD->getTemplateParameters();
  1399. return cast<ClassTemplateDecl *>(P)->getTemplateParameters();
  1400. }
  1401. case Decl::Kind::ClassTemplatePartialSpecialization:
  1402. return cast<ClassTemplatePartialSpecializationDecl>(D)
  1403. ->getTemplateParameters();
  1404. case Decl::Kind::TypeAliasTemplate:
  1405. return cast<TypeAliasTemplateDecl>(D)->getTemplateParameters();
  1406. case Decl::Kind::BuiltinTemplate:
  1407. return cast<BuiltinTemplateDecl>(D)->getTemplateParameters();
  1408. case Decl::Kind::CXXDeductionGuide:
  1409. case Decl::Kind::CXXConversion:
  1410. case Decl::Kind::CXXConstructor:
  1411. case Decl::Kind::CXXDestructor:
  1412. case Decl::Kind::CXXMethod:
  1413. case Decl::Kind::Function:
  1414. return cast<FunctionDecl>(D)
  1415. ->getTemplateSpecializationInfo()
  1416. ->getTemplate()
  1417. ->getTemplateParameters();
  1418. case Decl::Kind::FunctionTemplate:
  1419. return cast<FunctionTemplateDecl>(D)->getTemplateParameters();
  1420. case Decl::Kind::VarTemplate:
  1421. return cast<VarTemplateDecl>(D)->getTemplateParameters();
  1422. case Decl::Kind::VarTemplateSpecialization: {
  1423. const auto *VTSD = cast<VarTemplateSpecializationDecl>(D);
  1424. auto P = VTSD->getSpecializedTemplateOrPartial();
  1425. if (const auto *VTPSD =
  1426. P.dyn_cast<VarTemplatePartialSpecializationDecl *>())
  1427. return VTPSD->getTemplateParameters();
  1428. return cast<VarTemplateDecl *>(P)->getTemplateParameters();
  1429. }
  1430. case Decl::Kind::VarTemplatePartialSpecialization:
  1431. return cast<VarTemplatePartialSpecializationDecl>(D)
  1432. ->getTemplateParameters();
  1433. case Decl::Kind::TemplateTemplateParm:
  1434. return cast<TemplateTemplateParmDecl>(D)->getTemplateParameters();
  1435. case Decl::Kind::Concept:
  1436. return cast<ConceptDecl>(D)->getTemplateParameters();
  1437. default:
  1438. llvm_unreachable("Unhandled templated declaration kind");
  1439. }
  1440. }