DeclTemplate.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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/None.h"
  29. #include "llvm/ADT/PointerUnion.h"
  30. #include "llvm/ADT/STLExtras.h"
  31. #include "llvm/ADT/SmallVector.h"
  32. #include "llvm/Support/Casting.h"
  33. #include "llvm/Support/ErrorHandling.h"
  34. #include <algorithm>
  35. #include <cassert>
  36. #include <cstdint>
  37. #include <memory>
  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 (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. //===----------------------------------------------------------------------===//
  221. // RedeclarableTemplateDecl Implementation
  222. //===----------------------------------------------------------------------===//
  223. void RedeclarableTemplateDecl::anchor() {}
  224. RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
  225. if (Common)
  226. return Common;
  227. // Walk the previous-declaration chain until we either find a declaration
  228. // with a common pointer or we run out of previous declarations.
  229. SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
  230. for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
  231. Prev = Prev->getPreviousDecl()) {
  232. if (Prev->Common) {
  233. Common = Prev->Common;
  234. break;
  235. }
  236. PrevDecls.push_back(Prev);
  237. }
  238. // If we never found a common pointer, allocate one now.
  239. if (!Common) {
  240. // FIXME: If any of the declarations is from an AST file, we probably
  241. // need an update record to add the common data.
  242. Common = newCommon(getASTContext());
  243. }
  244. // Update any previous declarations we saw with the common pointer.
  245. for (const RedeclarableTemplateDecl *Prev : PrevDecls)
  246. Prev->Common = Common;
  247. return Common;
  248. }
  249. void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
  250. // Grab the most recent declaration to ensure we've loaded any lazy
  251. // redeclarations of this template.
  252. CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
  253. if (CommonBasePtr->LazySpecializations) {
  254. ASTContext &Context = getASTContext();
  255. uint32_t *Specs = CommonBasePtr->LazySpecializations;
  256. CommonBasePtr->LazySpecializations = nullptr;
  257. for (uint32_t I = 0, N = *Specs++; I != N; ++I)
  258. (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
  259. }
  260. }
  261. template<class EntryType, typename... ProfileArguments>
  262. typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
  263. RedeclarableTemplateDecl::findSpecializationImpl(
  264. llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos,
  265. ProfileArguments&&... ProfileArgs) {
  266. using SETraits = SpecEntryTraits<EntryType>;
  267. llvm::FoldingSetNodeID ID;
  268. EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
  269. getASTContext());
  270. EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
  271. return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr;
  272. }
  273. template<class Derived, class EntryType>
  274. void RedeclarableTemplateDecl::addSpecializationImpl(
  275. llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
  276. void *InsertPos) {
  277. using SETraits = SpecEntryTraits<EntryType>;
  278. if (InsertPos) {
  279. #ifndef NDEBUG
  280. void *CorrectInsertPos;
  281. assert(!findSpecializationImpl(Specializations,
  282. CorrectInsertPos,
  283. SETraits::getTemplateArgs(Entry)) &&
  284. InsertPos == CorrectInsertPos &&
  285. "given incorrect InsertPos for specialization");
  286. #endif
  287. Specializations.InsertNode(Entry, InsertPos);
  288. } else {
  289. EntryType *Existing = Specializations.GetOrInsertNode(Entry);
  290. (void)Existing;
  291. assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
  292. "non-canonical specialization?");
  293. }
  294. if (ASTMutationListener *L = getASTMutationListener())
  295. L->AddedCXXTemplateSpecialization(cast<Derived>(this),
  296. SETraits::getDecl(Entry));
  297. }
  298. //===----------------------------------------------------------------------===//
  299. // FunctionTemplateDecl Implementation
  300. //===----------------------------------------------------------------------===//
  301. FunctionTemplateDecl *
  302. FunctionTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
  303. DeclarationName Name,
  304. TemplateParameterList *Params, NamedDecl *Decl) {
  305. bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
  306. auto *TD = new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
  307. if (Invalid)
  308. TD->setInvalidDecl();
  309. return TD;
  310. }
  311. FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
  312. unsigned ID) {
  313. return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
  314. DeclarationName(), nullptr, nullptr);
  315. }
  316. RedeclarableTemplateDecl::CommonBase *
  317. FunctionTemplateDecl::newCommon(ASTContext &C) const {
  318. auto *CommonPtr = new (C) Common;
  319. C.addDestruction(CommonPtr);
  320. return CommonPtr;
  321. }
  322. void FunctionTemplateDecl::LoadLazySpecializations() const {
  323. loadLazySpecializationsImpl();
  324. }
  325. llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
  326. FunctionTemplateDecl::getSpecializations() const {
  327. LoadLazySpecializations();
  328. return getCommonPtr()->Specializations;
  329. }
  330. FunctionDecl *
  331. FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
  332. void *&InsertPos) {
  333. return findSpecializationImpl(getSpecializations(), InsertPos, Args);
  334. }
  335. void FunctionTemplateDecl::addSpecialization(
  336. FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
  337. addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info,
  338. InsertPos);
  339. }
  340. ArrayRef<TemplateArgument> FunctionTemplateDecl::getInjectedTemplateArgs() {
  341. TemplateParameterList *Params = getTemplateParameters();
  342. Common *CommonPtr = getCommonPtr();
  343. if (!CommonPtr->InjectedArgs) {
  344. auto &Context = getASTContext();
  345. SmallVector<TemplateArgument, 16> TemplateArgs;
  346. Context.getInjectedTemplateArgs(Params, TemplateArgs);
  347. CommonPtr->InjectedArgs =
  348. new (Context) TemplateArgument[TemplateArgs.size()];
  349. std::copy(TemplateArgs.begin(), TemplateArgs.end(),
  350. CommonPtr->InjectedArgs);
  351. }
  352. return llvm::makeArrayRef(CommonPtr->InjectedArgs, Params->size());
  353. }
  354. void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
  355. using Base = RedeclarableTemplateDecl;
  356. // If we haven't created a common pointer yet, then it can just be created
  357. // with the usual method.
  358. if (!Base::Common)
  359. return;
  360. Common *ThisCommon = static_cast<Common *>(Base::Common);
  361. Common *PrevCommon = nullptr;
  362. SmallVector<FunctionTemplateDecl *, 8> PreviousDecls;
  363. for (; Prev; Prev = Prev->getPreviousDecl()) {
  364. if (Prev->Base::Common) {
  365. PrevCommon = static_cast<Common *>(Prev->Base::Common);
  366. break;
  367. }
  368. PreviousDecls.push_back(Prev);
  369. }
  370. // If the previous redecl chain hasn't created a common pointer yet, then just
  371. // use this common pointer.
  372. if (!PrevCommon) {
  373. for (auto *D : PreviousDecls)
  374. D->Base::Common = ThisCommon;
  375. return;
  376. }
  377. // Ensure we don't leak any important state.
  378. assert(ThisCommon->Specializations.size() == 0 &&
  379. "Can't merge incompatible declarations!");
  380. Base::Common = PrevCommon;
  381. }
  382. //===----------------------------------------------------------------------===//
  383. // ClassTemplateDecl Implementation
  384. //===----------------------------------------------------------------------===//
  385. ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, DeclContext *DC,
  386. SourceLocation L,
  387. DeclarationName Name,
  388. TemplateParameterList *Params,
  389. NamedDecl *Decl) {
  390. bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
  391. auto *TD = new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl);
  392. if (Invalid)
  393. TD->setInvalidDecl();
  394. return TD;
  395. }
  396. ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
  397. unsigned ID) {
  398. return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
  399. DeclarationName(), nullptr, nullptr);
  400. }
  401. void ClassTemplateDecl::LoadLazySpecializations() const {
  402. loadLazySpecializationsImpl();
  403. }
  404. llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
  405. ClassTemplateDecl::getSpecializations() const {
  406. LoadLazySpecializations();
  407. return getCommonPtr()->Specializations;
  408. }
  409. llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
  410. ClassTemplateDecl::getPartialSpecializations() const {
  411. LoadLazySpecializations();
  412. return getCommonPtr()->PartialSpecializations;
  413. }
  414. RedeclarableTemplateDecl::CommonBase *
  415. ClassTemplateDecl::newCommon(ASTContext &C) const {
  416. auto *CommonPtr = new (C) Common;
  417. C.addDestruction(CommonPtr);
  418. return CommonPtr;
  419. }
  420. ClassTemplateSpecializationDecl *
  421. ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
  422. void *&InsertPos) {
  423. return findSpecializationImpl(getSpecializations(), InsertPos, Args);
  424. }
  425. void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
  426. void *InsertPos) {
  427. addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos);
  428. }
  429. ClassTemplatePartialSpecializationDecl *
  430. ClassTemplateDecl::findPartialSpecialization(
  431. ArrayRef<TemplateArgument> Args,
  432. TemplateParameterList *TPL, void *&InsertPos) {
  433. return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
  434. TPL);
  435. }
  436. static void ProfileTemplateParameterList(ASTContext &C,
  437. llvm::FoldingSetNodeID &ID, const TemplateParameterList *TPL) {
  438. const Expr *RC = TPL->getRequiresClause();
  439. ID.AddBoolean(RC != nullptr);
  440. if (RC)
  441. RC->Profile(ID, C, /*Canonical=*/true);
  442. ID.AddInteger(TPL->size());
  443. for (NamedDecl *D : *TPL) {
  444. if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
  445. ID.AddInteger(0);
  446. ID.AddBoolean(NTTP->isParameterPack());
  447. NTTP->getType().getCanonicalType().Profile(ID);
  448. continue;
  449. }
  450. if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) {
  451. ID.AddInteger(1);
  452. ID.AddBoolean(TTP->isParameterPack());
  453. ID.AddBoolean(TTP->hasTypeConstraint());
  454. if (const TypeConstraint *TC = TTP->getTypeConstraint())
  455. TC->getImmediatelyDeclaredConstraint()->Profile(ID, C,
  456. /*Canonical=*/true);
  457. continue;
  458. }
  459. const auto *TTP = cast<TemplateTemplateParmDecl>(D);
  460. ID.AddInteger(2);
  461. ID.AddBoolean(TTP->isParameterPack());
  462. ProfileTemplateParameterList(C, ID, TTP->getTemplateParameters());
  463. }
  464. }
  465. void
  466. ClassTemplatePartialSpecializationDecl::Profile(llvm::FoldingSetNodeID &ID,
  467. ArrayRef<TemplateArgument> TemplateArgs, TemplateParameterList *TPL,
  468. ASTContext &Context) {
  469. ID.AddInteger(TemplateArgs.size());
  470. for (const TemplateArgument &TemplateArg : TemplateArgs)
  471. TemplateArg.Profile(ID, Context);
  472. ProfileTemplateParameterList(Context, ID, TPL);
  473. }
  474. void ClassTemplateDecl::AddPartialSpecialization(
  475. ClassTemplatePartialSpecializationDecl *D,
  476. void *InsertPos) {
  477. if (InsertPos)
  478. getPartialSpecializations().InsertNode(D, InsertPos);
  479. else {
  480. ClassTemplatePartialSpecializationDecl *Existing
  481. = getPartialSpecializations().GetOrInsertNode(D);
  482. (void)Existing;
  483. assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
  484. }
  485. if (ASTMutationListener *L = getASTMutationListener())
  486. L->AddedCXXTemplateSpecialization(this, D);
  487. }
  488. void ClassTemplateDecl::getPartialSpecializations(
  489. SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) const {
  490. llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
  491. = getPartialSpecializations();
  492. PS.clear();
  493. PS.reserve(PartialSpecs.size());
  494. for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs)
  495. PS.push_back(P.getMostRecentDecl());
  496. }
  497. ClassTemplatePartialSpecializationDecl *
  498. ClassTemplateDecl::findPartialSpecialization(QualType T) {
  499. ASTContext &Context = getASTContext();
  500. for (ClassTemplatePartialSpecializationDecl &P :
  501. getPartialSpecializations()) {
  502. if (Context.hasSameType(P.getInjectedSpecializationType(), T))
  503. return P.getMostRecentDecl();
  504. }
  505. return nullptr;
  506. }
  507. ClassTemplatePartialSpecializationDecl *
  508. ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
  509. ClassTemplatePartialSpecializationDecl *D) {
  510. Decl *DCanon = D->getCanonicalDecl();
  511. for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
  512. if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
  513. return P.getMostRecentDecl();
  514. }
  515. return nullptr;
  516. }
  517. QualType
  518. ClassTemplateDecl::getInjectedClassNameSpecialization() {
  519. Common *CommonPtr = getCommonPtr();
  520. if (!CommonPtr->InjectedClassNameType.isNull())
  521. return CommonPtr->InjectedClassNameType;
  522. // C++0x [temp.dep.type]p2:
  523. // The template argument list of a primary template is a template argument
  524. // list in which the nth template argument has the value of the nth template
  525. // parameter of the class template. If the nth template parameter is a
  526. // template parameter pack (14.5.3), the nth template argument is a pack
  527. // expansion (14.5.3) whose pattern is the name of the template parameter
  528. // pack.
  529. ASTContext &Context = getASTContext();
  530. TemplateParameterList *Params = getTemplateParameters();
  531. SmallVector<TemplateArgument, 16> TemplateArgs;
  532. Context.getInjectedTemplateArgs(Params, TemplateArgs);
  533. CommonPtr->InjectedClassNameType
  534. = Context.getTemplateSpecializationType(TemplateName(this),
  535. TemplateArgs);
  536. return CommonPtr->InjectedClassNameType;
  537. }
  538. //===----------------------------------------------------------------------===//
  539. // TemplateTypeParm Allocation/Deallocation Method Implementations
  540. //===----------------------------------------------------------------------===//
  541. TemplateTypeParmDecl *
  542. TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
  543. SourceLocation KeyLoc, SourceLocation NameLoc,
  544. unsigned D, unsigned P, IdentifierInfo *Id,
  545. bool Typename, bool ParameterPack,
  546. bool HasTypeConstraint,
  547. Optional<unsigned> NumExpanded) {
  548. auto *TTPDecl =
  549. new (C, DC,
  550. additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 1 : 0))
  551. TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename,
  552. HasTypeConstraint, NumExpanded);
  553. QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
  554. TTPDecl->setTypeForDecl(TTPType.getTypePtr());
  555. return TTPDecl;
  556. }
  557. TemplateTypeParmDecl *
  558. TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
  559. return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(),
  560. SourceLocation(), nullptr, false,
  561. false, None);
  562. }
  563. TemplateTypeParmDecl *
  564. TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID,
  565. bool HasTypeConstraint) {
  566. return new (C, ID,
  567. additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 1 : 0))
  568. TemplateTypeParmDecl(nullptr, SourceLocation(), SourceLocation(),
  569. nullptr, false, HasTypeConstraint, None);
  570. }
  571. SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
  572. return hasDefaultArgument()
  573. ? getDefaultArgumentInfo()->getTypeLoc().getBeginLoc()
  574. : SourceLocation();
  575. }
  576. SourceRange TemplateTypeParmDecl::getSourceRange() const {
  577. if (hasDefaultArgument() && !defaultArgumentWasInherited())
  578. return SourceRange(getBeginLoc(),
  579. getDefaultArgumentInfo()->getTypeLoc().getEndLoc());
  580. // TypeDecl::getSourceRange returns a range containing name location, which is
  581. // wrong for unnamed template parameters. e.g:
  582. // it will return <[[typename>]] instead of <[[typename]]>
  583. else if (getDeclName().isEmpty())
  584. return SourceRange(getBeginLoc());
  585. return TypeDecl::getSourceRange();
  586. }
  587. unsigned TemplateTypeParmDecl::getDepth() const {
  588. return getTypeForDecl()->castAs<TemplateTypeParmType>()->getDepth();
  589. }
  590. unsigned TemplateTypeParmDecl::getIndex() const {
  591. return getTypeForDecl()->castAs<TemplateTypeParmType>()->getIndex();
  592. }
  593. bool TemplateTypeParmDecl::isParameterPack() const {
  594. return getTypeForDecl()->castAs<TemplateTypeParmType>()->isParameterPack();
  595. }
  596. void TemplateTypeParmDecl::setTypeConstraint(NestedNameSpecifierLoc NNS,
  597. DeclarationNameInfo NameInfo, NamedDecl *FoundDecl, ConceptDecl *CD,
  598. const ASTTemplateArgumentListInfo *ArgsAsWritten,
  599. Expr *ImmediatelyDeclaredConstraint) {
  600. assert(HasTypeConstraint &&
  601. "HasTypeConstraint=true must be passed at construction in order to "
  602. "call setTypeConstraint");
  603. assert(!TypeConstraintInitialized &&
  604. "TypeConstraint was already initialized!");
  605. new (getTrailingObjects<TypeConstraint>()) TypeConstraint(NNS, NameInfo,
  606. FoundDecl, CD, ArgsAsWritten, ImmediatelyDeclaredConstraint);
  607. TypeConstraintInitialized = true;
  608. }
  609. //===----------------------------------------------------------------------===//
  610. // NonTypeTemplateParmDecl Method Implementations
  611. //===----------------------------------------------------------------------===//
  612. NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(
  613. DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D,
  614. unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
  615. ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos)
  616. : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
  617. TemplateParmPosition(D, P), ParameterPack(true),
  618. ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) {
  619. if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) {
  620. auto TypesAndInfos =
  621. getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
  622. for (unsigned I = 0; I != NumExpandedTypes; ++I) {
  623. new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
  624. TypesAndInfos[I].second = ExpandedTInfos[I];
  625. }
  626. }
  627. }
  628. NonTypeTemplateParmDecl *
  629. NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
  630. SourceLocation StartLoc, SourceLocation IdLoc,
  631. unsigned D, unsigned P, IdentifierInfo *Id,
  632. QualType T, bool ParameterPack,
  633. TypeSourceInfo *TInfo) {
  634. AutoType *AT =
  635. C.getLangOpts().CPlusPlus20 ? T->getContainedAutoType() : nullptr;
  636. return new (C, DC,
  637. additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
  638. Expr *>(0,
  639. AT && AT->isConstrained() ? 1 : 0))
  640. NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, ParameterPack,
  641. TInfo);
  642. }
  643. NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
  644. const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
  645. SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
  646. QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
  647. ArrayRef<TypeSourceInfo *> ExpandedTInfos) {
  648. AutoType *AT = TInfo->getType()->getContainedAutoType();
  649. return new (C, DC,
  650. additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
  651. Expr *>(
  652. ExpandedTypes.size(), AT && AT->isConstrained() ? 1 : 0))
  653. NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
  654. ExpandedTypes, ExpandedTInfos);
  655. }
  656. NonTypeTemplateParmDecl *
  657. NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  658. bool HasTypeConstraint) {
  659. return new (C, ID, additionalSizeToAlloc<std::pair<QualType,
  660. TypeSourceInfo *>,
  661. Expr *>(0,
  662. HasTypeConstraint ? 1 : 0))
  663. NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
  664. 0, 0, nullptr, QualType(), false, nullptr);
  665. }
  666. NonTypeTemplateParmDecl *
  667. NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  668. unsigned NumExpandedTypes,
  669. bool HasTypeConstraint) {
  670. auto *NTTP =
  671. new (C, ID, additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
  672. Expr *>(
  673. NumExpandedTypes, HasTypeConstraint ? 1 : 0))
  674. NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
  675. 0, 0, nullptr, QualType(), nullptr, None,
  676. None);
  677. NTTP->NumExpandedTypes = NumExpandedTypes;
  678. return NTTP;
  679. }
  680. SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
  681. if (hasDefaultArgument() && !defaultArgumentWasInherited())
  682. return SourceRange(getOuterLocStart(),
  683. getDefaultArgument()->getSourceRange().getEnd());
  684. return DeclaratorDecl::getSourceRange();
  685. }
  686. SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
  687. return hasDefaultArgument()
  688. ? getDefaultArgument()->getSourceRange().getBegin()
  689. : SourceLocation();
  690. }
  691. //===----------------------------------------------------------------------===//
  692. // TemplateTemplateParmDecl Method Implementations
  693. //===----------------------------------------------------------------------===//
  694. void TemplateTemplateParmDecl::anchor() {}
  695. TemplateTemplateParmDecl::TemplateTemplateParmDecl(
  696. DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
  697. IdentifierInfo *Id, TemplateParameterList *Params,
  698. ArrayRef<TemplateParameterList *> Expansions)
  699. : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
  700. TemplateParmPosition(D, P), ParameterPack(true),
  701. ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) {
  702. if (!Expansions.empty())
  703. std::uninitialized_copy(Expansions.begin(), Expansions.end(),
  704. getTrailingObjects<TemplateParameterList *>());
  705. }
  706. TemplateTemplateParmDecl *
  707. TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
  708. SourceLocation L, unsigned D, unsigned P,
  709. bool ParameterPack, IdentifierInfo *Id,
  710. TemplateParameterList *Params) {
  711. return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
  712. Params);
  713. }
  714. TemplateTemplateParmDecl *
  715. TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
  716. SourceLocation L, unsigned D, unsigned P,
  717. IdentifierInfo *Id,
  718. TemplateParameterList *Params,
  719. ArrayRef<TemplateParameterList *> Expansions) {
  720. return new (C, DC,
  721. additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
  722. TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions);
  723. }
  724. TemplateTemplateParmDecl *
  725. TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  726. return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
  727. false, nullptr, nullptr);
  728. }
  729. TemplateTemplateParmDecl *
  730. TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  731. unsigned NumExpansions) {
  732. auto *TTP =
  733. new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
  734. TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
  735. nullptr, None);
  736. TTP->NumExpandedParams = NumExpansions;
  737. return TTP;
  738. }
  739. SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
  740. return hasDefaultArgument() ? getDefaultArgument().getLocation()
  741. : SourceLocation();
  742. }
  743. void TemplateTemplateParmDecl::setDefaultArgument(
  744. const ASTContext &C, const TemplateArgumentLoc &DefArg) {
  745. if (DefArg.getArgument().isNull())
  746. DefaultArgument.set(nullptr);
  747. else
  748. DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
  749. }
  750. //===----------------------------------------------------------------------===//
  751. // TemplateArgumentList Implementation
  752. //===----------------------------------------------------------------------===//
  753. TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
  754. : Arguments(getTrailingObjects<TemplateArgument>()),
  755. NumArguments(Args.size()) {
  756. std::uninitialized_copy(Args.begin(), Args.end(),
  757. getTrailingObjects<TemplateArgument>());
  758. }
  759. TemplateArgumentList *
  760. TemplateArgumentList::CreateCopy(ASTContext &Context,
  761. ArrayRef<TemplateArgument> Args) {
  762. void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
  763. return new (Mem) TemplateArgumentList(Args);
  764. }
  765. FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
  766. ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
  767. TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs,
  768. const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI,
  769. MemberSpecializationInfo *MSInfo) {
  770. const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
  771. if (TemplateArgsAsWritten)
  772. ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
  773. *TemplateArgsAsWritten);
  774. void *Mem =
  775. C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 1 : 0));
  776. return new (Mem) FunctionTemplateSpecializationInfo(
  777. FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo);
  778. }
  779. //===----------------------------------------------------------------------===//
  780. // ClassTemplateSpecializationDecl Implementation
  781. //===----------------------------------------------------------------------===//
  782. ClassTemplateSpecializationDecl::
  783. ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
  784. DeclContext *DC, SourceLocation StartLoc,
  785. SourceLocation IdLoc,
  786. ClassTemplateDecl *SpecializedTemplate,
  787. ArrayRef<TemplateArgument> Args,
  788. ClassTemplateSpecializationDecl *PrevDecl)
  789. : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
  790. SpecializedTemplate->getIdentifier(), PrevDecl),
  791. SpecializedTemplate(SpecializedTemplate),
  792. TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
  793. SpecializationKind(TSK_Undeclared) {
  794. }
  795. ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
  796. Kind DK)
  797. : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
  798. SourceLocation(), nullptr, nullptr),
  799. SpecializationKind(TSK_Undeclared) {}
  800. ClassTemplateSpecializationDecl *
  801. ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
  802. DeclContext *DC,
  803. SourceLocation StartLoc,
  804. SourceLocation IdLoc,
  805. ClassTemplateDecl *SpecializedTemplate,
  806. ArrayRef<TemplateArgument> Args,
  807. ClassTemplateSpecializationDecl *PrevDecl) {
  808. auto *Result =
  809. new (Context, DC) ClassTemplateSpecializationDecl(
  810. Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
  811. SpecializedTemplate, Args, PrevDecl);
  812. Result->setMayHaveOutOfDateDef(false);
  813. Context.getTypeDeclType(Result, PrevDecl);
  814. return Result;
  815. }
  816. ClassTemplateSpecializationDecl *
  817. ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
  818. unsigned ID) {
  819. auto *Result =
  820. new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
  821. Result->setMayHaveOutOfDateDef(false);
  822. return Result;
  823. }
  824. void ClassTemplateSpecializationDecl::getNameForDiagnostic(
  825. raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
  826. NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
  827. const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this);
  828. if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
  829. PS ? PS->getTemplateArgsAsWritten() : nullptr) {
  830. printTemplateArgumentList(
  831. OS, ArgsAsWritten->arguments(), Policy,
  832. getSpecializedTemplate()->getTemplateParameters());
  833. } else {
  834. const TemplateArgumentList &TemplateArgs = getTemplateArgs();
  835. printTemplateArgumentList(
  836. OS, TemplateArgs.asArray(), Policy,
  837. getSpecializedTemplate()->getTemplateParameters());
  838. }
  839. }
  840. ClassTemplateDecl *
  841. ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
  842. if (const auto *PartialSpec =
  843. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
  844. return PartialSpec->PartialSpecialization->getSpecializedTemplate();
  845. return SpecializedTemplate.get<ClassTemplateDecl*>();
  846. }
  847. SourceRange
  848. ClassTemplateSpecializationDecl::getSourceRange() const {
  849. if (ExplicitInfo) {
  850. SourceLocation Begin = getTemplateKeywordLoc();
  851. if (Begin.isValid()) {
  852. // Here we have an explicit (partial) specialization or instantiation.
  853. assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
  854. getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
  855. getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
  856. if (getExternLoc().isValid())
  857. Begin = getExternLoc();
  858. SourceLocation End = getBraceRange().getEnd();
  859. if (End.isInvalid())
  860. End = getTypeAsWritten()->getTypeLoc().getEndLoc();
  861. return SourceRange(Begin, End);
  862. }
  863. // An implicit instantiation of a class template partial specialization
  864. // uses ExplicitInfo to record the TypeAsWritten, but the source
  865. // locations should be retrieved from the instantiation pattern.
  866. using CTPSDecl = ClassTemplatePartialSpecializationDecl;
  867. auto *ctpsd = const_cast<CTPSDecl *>(cast<CTPSDecl>(this));
  868. CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
  869. assert(inst_from != nullptr);
  870. return inst_from->getSourceRange();
  871. }
  872. else {
  873. // No explicit info available.
  874. llvm::PointerUnion<ClassTemplateDecl *,
  875. ClassTemplatePartialSpecializationDecl *>
  876. inst_from = getInstantiatedFrom();
  877. if (inst_from.isNull())
  878. return getSpecializedTemplate()->getSourceRange();
  879. if (const auto *ctd = inst_from.dyn_cast<ClassTemplateDecl *>())
  880. return ctd->getSourceRange();
  881. return inst_from.get<ClassTemplatePartialSpecializationDecl *>()
  882. ->getSourceRange();
  883. }
  884. }
  885. //===----------------------------------------------------------------------===//
  886. // ConceptDecl Implementation
  887. //===----------------------------------------------------------------------===//
  888. ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC,
  889. SourceLocation L, DeclarationName Name,
  890. TemplateParameterList *Params,
  891. Expr *ConstraintExpr) {
  892. bool Invalid = AdoptTemplateParameterList(Params, DC);
  893. auto *TD = new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr);
  894. if (Invalid)
  895. TD->setInvalidDecl();
  896. return TD;
  897. }
  898. ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C,
  899. unsigned ID) {
  900. ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(),
  901. DeclarationName(),
  902. nullptr, nullptr);
  903. return Result;
  904. }
  905. //===----------------------------------------------------------------------===//
  906. // ClassTemplatePartialSpecializationDecl Implementation
  907. //===----------------------------------------------------------------------===//
  908. void ClassTemplatePartialSpecializationDecl::anchor() {}
  909. ClassTemplatePartialSpecializationDecl::
  910. ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
  911. DeclContext *DC,
  912. SourceLocation StartLoc,
  913. SourceLocation IdLoc,
  914. TemplateParameterList *Params,
  915. ClassTemplateDecl *SpecializedTemplate,
  916. ArrayRef<TemplateArgument> Args,
  917. const ASTTemplateArgumentListInfo *ArgInfos,
  918. ClassTemplatePartialSpecializationDecl *PrevDecl)
  919. : ClassTemplateSpecializationDecl(Context,
  920. ClassTemplatePartialSpecialization,
  921. TK, DC, StartLoc, IdLoc,
  922. SpecializedTemplate, Args, PrevDecl),
  923. TemplateParams(Params), ArgsAsWritten(ArgInfos),
  924. InstantiatedFromMember(nullptr, false) {
  925. if (AdoptTemplateParameterList(Params, this))
  926. setInvalidDecl();
  927. }
  928. ClassTemplatePartialSpecializationDecl *
  929. ClassTemplatePartialSpecializationDecl::
  930. Create(ASTContext &Context, TagKind TK,DeclContext *DC,
  931. SourceLocation StartLoc, SourceLocation IdLoc,
  932. TemplateParameterList *Params,
  933. ClassTemplateDecl *SpecializedTemplate,
  934. ArrayRef<TemplateArgument> Args,
  935. const TemplateArgumentListInfo &ArgInfos,
  936. QualType CanonInjectedType,
  937. ClassTemplatePartialSpecializationDecl *PrevDecl) {
  938. const ASTTemplateArgumentListInfo *ASTArgInfos =
  939. ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
  940. auto *Result = new (Context, DC)
  941. ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
  942. Params, SpecializedTemplate, Args,
  943. ASTArgInfos, PrevDecl);
  944. Result->setSpecializationKind(TSK_ExplicitSpecialization);
  945. Result->setMayHaveOutOfDateDef(false);
  946. Context.getInjectedClassNameType(Result, CanonInjectedType);
  947. return Result;
  948. }
  949. ClassTemplatePartialSpecializationDecl *
  950. ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
  951. unsigned ID) {
  952. auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C);
  953. Result->setMayHaveOutOfDateDef(false);
  954. return Result;
  955. }
  956. //===----------------------------------------------------------------------===//
  957. // FriendTemplateDecl Implementation
  958. //===----------------------------------------------------------------------===//
  959. void FriendTemplateDecl::anchor() {}
  960. FriendTemplateDecl *
  961. FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
  962. SourceLocation L,
  963. MutableArrayRef<TemplateParameterList *> Params,
  964. FriendUnion Friend, SourceLocation FLoc) {
  965. TemplateParameterList **TPL = nullptr;
  966. if (!Params.empty()) {
  967. TPL = new (Context) TemplateParameterList *[Params.size()];
  968. llvm::copy(Params, TPL);
  969. }
  970. return new (Context, DC)
  971. FriendTemplateDecl(DC, L, TPL, Params.size(), Friend, FLoc);
  972. }
  973. FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
  974. unsigned ID) {
  975. return new (C, ID) FriendTemplateDecl(EmptyShell());
  976. }
  977. //===----------------------------------------------------------------------===//
  978. // TypeAliasTemplateDecl Implementation
  979. //===----------------------------------------------------------------------===//
  980. TypeAliasTemplateDecl *
  981. TypeAliasTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
  982. DeclarationName Name,
  983. TemplateParameterList *Params, NamedDecl *Decl) {
  984. bool Invalid = AdoptTemplateParameterList(Params, DC);
  985. auto *TD = new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
  986. if (Invalid)
  987. TD->setInvalidDecl();
  988. return TD;
  989. }
  990. TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
  991. unsigned ID) {
  992. return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
  993. DeclarationName(), nullptr, nullptr);
  994. }
  995. RedeclarableTemplateDecl::CommonBase *
  996. TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
  997. auto *CommonPtr = new (C) Common;
  998. C.addDestruction(CommonPtr);
  999. return CommonPtr;
  1000. }
  1001. //===----------------------------------------------------------------------===//
  1002. // ClassScopeFunctionSpecializationDecl Implementation
  1003. //===----------------------------------------------------------------------===//
  1004. void ClassScopeFunctionSpecializationDecl::anchor() {}
  1005. ClassScopeFunctionSpecializationDecl *
  1006. ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
  1007. unsigned ID) {
  1008. return new (C, ID) ClassScopeFunctionSpecializationDecl(
  1009. nullptr, SourceLocation(), nullptr, nullptr);
  1010. }
  1011. //===----------------------------------------------------------------------===//
  1012. // VarTemplateDecl Implementation
  1013. //===----------------------------------------------------------------------===//
  1014. VarTemplateDecl *VarTemplateDecl::getDefinition() {
  1015. VarTemplateDecl *CurD = this;
  1016. while (CurD) {
  1017. if (CurD->isThisDeclarationADefinition())
  1018. return CurD;
  1019. CurD = CurD->getPreviousDecl();
  1020. }
  1021. return nullptr;
  1022. }
  1023. VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
  1024. SourceLocation L, DeclarationName Name,
  1025. TemplateParameterList *Params,
  1026. VarDecl *Decl) {
  1027. bool Invalid = AdoptTemplateParameterList(Params, DC);
  1028. auto *TD = new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
  1029. if (Invalid)
  1030. TD->setInvalidDecl();
  1031. return TD;
  1032. }
  1033. VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
  1034. unsigned ID) {
  1035. return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
  1036. DeclarationName(), nullptr, nullptr);
  1037. }
  1038. void VarTemplateDecl::LoadLazySpecializations() const {
  1039. loadLazySpecializationsImpl();
  1040. }
  1041. llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
  1042. VarTemplateDecl::getSpecializations() const {
  1043. LoadLazySpecializations();
  1044. return getCommonPtr()->Specializations;
  1045. }
  1046. llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
  1047. VarTemplateDecl::getPartialSpecializations() const {
  1048. LoadLazySpecializations();
  1049. return getCommonPtr()->PartialSpecializations;
  1050. }
  1051. RedeclarableTemplateDecl::CommonBase *
  1052. VarTemplateDecl::newCommon(ASTContext &C) const {
  1053. auto *CommonPtr = new (C) Common;
  1054. C.addDestruction(CommonPtr);
  1055. return CommonPtr;
  1056. }
  1057. VarTemplateSpecializationDecl *
  1058. VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
  1059. void *&InsertPos) {
  1060. return findSpecializationImpl(getSpecializations(), InsertPos, Args);
  1061. }
  1062. void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
  1063. void *InsertPos) {
  1064. addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos);
  1065. }
  1066. VarTemplatePartialSpecializationDecl *
  1067. VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
  1068. TemplateParameterList *TPL, void *&InsertPos) {
  1069. return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
  1070. TPL);
  1071. }
  1072. void
  1073. VarTemplatePartialSpecializationDecl::Profile(llvm::FoldingSetNodeID &ID,
  1074. ArrayRef<TemplateArgument> TemplateArgs, TemplateParameterList *TPL,
  1075. ASTContext &Context) {
  1076. ID.AddInteger(TemplateArgs.size());
  1077. for (const TemplateArgument &TemplateArg : TemplateArgs)
  1078. TemplateArg.Profile(ID, Context);
  1079. ProfileTemplateParameterList(Context, ID, TPL);
  1080. }
  1081. void VarTemplateDecl::AddPartialSpecialization(
  1082. VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
  1083. if (InsertPos)
  1084. getPartialSpecializations().InsertNode(D, InsertPos);
  1085. else {
  1086. VarTemplatePartialSpecializationDecl *Existing =
  1087. getPartialSpecializations().GetOrInsertNode(D);
  1088. (void)Existing;
  1089. assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
  1090. }
  1091. if (ASTMutationListener *L = getASTMutationListener())
  1092. L->AddedCXXTemplateSpecialization(this, D);
  1093. }
  1094. void VarTemplateDecl::getPartialSpecializations(
  1095. SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const {
  1096. llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
  1097. getPartialSpecializations();
  1098. PS.clear();
  1099. PS.reserve(PartialSpecs.size());
  1100. for (VarTemplatePartialSpecializationDecl &P : PartialSpecs)
  1101. PS.push_back(P.getMostRecentDecl());
  1102. }
  1103. VarTemplatePartialSpecializationDecl *
  1104. VarTemplateDecl::findPartialSpecInstantiatedFromMember(
  1105. VarTemplatePartialSpecializationDecl *D) {
  1106. Decl *DCanon = D->getCanonicalDecl();
  1107. for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
  1108. if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
  1109. return P.getMostRecentDecl();
  1110. }
  1111. return nullptr;
  1112. }
  1113. //===----------------------------------------------------------------------===//
  1114. // VarTemplateSpecializationDecl Implementation
  1115. //===----------------------------------------------------------------------===//
  1116. VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
  1117. Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  1118. SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
  1119. TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args)
  1120. : VarDecl(DK, Context, DC, StartLoc, IdLoc,
  1121. SpecializedTemplate->getIdentifier(), T, TInfo, S),
  1122. SpecializedTemplate(SpecializedTemplate),
  1123. TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
  1124. SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
  1125. VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
  1126. ASTContext &C)
  1127. : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
  1128. QualType(), nullptr, SC_None),
  1129. SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
  1130. VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
  1131. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  1132. SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
  1133. TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) {
  1134. return new (Context, DC) VarTemplateSpecializationDecl(
  1135. VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
  1136. SpecializedTemplate, T, TInfo, S, Args);
  1137. }
  1138. VarTemplateSpecializationDecl *
  1139. VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1140. return new (C, ID)
  1141. VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
  1142. }
  1143. void VarTemplateSpecializationDecl::getNameForDiagnostic(
  1144. raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
  1145. NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
  1146. const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this);
  1147. if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
  1148. PS ? PS->getTemplateArgsAsWritten() : nullptr) {
  1149. printTemplateArgumentList(
  1150. OS, ArgsAsWritten->arguments(), Policy,
  1151. getSpecializedTemplate()->getTemplateParameters());
  1152. } else {
  1153. const TemplateArgumentList &TemplateArgs = getTemplateArgs();
  1154. printTemplateArgumentList(
  1155. OS, TemplateArgs.asArray(), Policy,
  1156. getSpecializedTemplate()->getTemplateParameters());
  1157. }
  1158. }
  1159. VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
  1160. if (const auto *PartialSpec =
  1161. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
  1162. return PartialSpec->PartialSpecialization->getSpecializedTemplate();
  1163. return SpecializedTemplate.get<VarTemplateDecl *>();
  1164. }
  1165. void VarTemplateSpecializationDecl::setTemplateArgsInfo(
  1166. const TemplateArgumentListInfo &ArgsInfo) {
  1167. TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
  1168. TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
  1169. for (const TemplateArgumentLoc &Loc : ArgsInfo.arguments())
  1170. TemplateArgsInfo.addArgument(Loc);
  1171. }
  1172. //===----------------------------------------------------------------------===//
  1173. // VarTemplatePartialSpecializationDecl Implementation
  1174. //===----------------------------------------------------------------------===//
  1175. void VarTemplatePartialSpecializationDecl::anchor() {}
  1176. VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
  1177. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  1178. SourceLocation IdLoc, TemplateParameterList *Params,
  1179. VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
  1180. StorageClass S, ArrayRef<TemplateArgument> Args,
  1181. const ASTTemplateArgumentListInfo *ArgInfos)
  1182. : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
  1183. DC, StartLoc, IdLoc, SpecializedTemplate, T,
  1184. TInfo, S, Args),
  1185. TemplateParams(Params), ArgsAsWritten(ArgInfos),
  1186. InstantiatedFromMember(nullptr, false) {
  1187. if (AdoptTemplateParameterList(Params, DC))
  1188. setInvalidDecl();
  1189. }
  1190. VarTemplatePartialSpecializationDecl *
  1191. VarTemplatePartialSpecializationDecl::Create(
  1192. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  1193. SourceLocation IdLoc, TemplateParameterList *Params,
  1194. VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
  1195. StorageClass S, ArrayRef<TemplateArgument> Args,
  1196. const TemplateArgumentListInfo &ArgInfos) {
  1197. const ASTTemplateArgumentListInfo *ASTArgInfos
  1198. = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
  1199. auto *Result =
  1200. new (Context, DC) VarTemplatePartialSpecializationDecl(
  1201. Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
  1202. S, Args, ASTArgInfos);
  1203. Result->setSpecializationKind(TSK_ExplicitSpecialization);
  1204. return Result;
  1205. }
  1206. VarTemplatePartialSpecializationDecl *
  1207. VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
  1208. unsigned ID) {
  1209. return new (C, ID) VarTemplatePartialSpecializationDecl(C);
  1210. }
  1211. static TemplateParameterList *
  1212. createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
  1213. // typename T
  1214. auto *T = TemplateTypeParmDecl::Create(
  1215. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
  1216. /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
  1217. /*HasTypeConstraint=*/false);
  1218. T->setImplicit(true);
  1219. // T ...Ints
  1220. TypeSourceInfo *TI =
  1221. C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
  1222. auto *N = NonTypeTemplateParmDecl::Create(
  1223. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
  1224. /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
  1225. N->setImplicit(true);
  1226. // <typename T, T ...Ints>
  1227. NamedDecl *P[2] = {T, N};
  1228. auto *TPL = TemplateParameterList::Create(
  1229. C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
  1230. // template <typename T, ...Ints> class IntSeq
  1231. auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
  1232. C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
  1233. /*ParameterPack=*/false, /*Id=*/nullptr, TPL);
  1234. TemplateTemplateParm->setImplicit(true);
  1235. // typename T
  1236. auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
  1237. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
  1238. /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
  1239. /*HasTypeConstraint=*/false);
  1240. TemplateTypeParm->setImplicit(true);
  1241. // T N
  1242. TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
  1243. QualType(TemplateTypeParm->getTypeForDecl(), 0));
  1244. auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
  1245. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
  1246. /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
  1247. NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
  1248. NonTypeTemplateParm};
  1249. // template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
  1250. return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
  1251. Params, SourceLocation(), nullptr);
  1252. }
  1253. static TemplateParameterList *
  1254. createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
  1255. // std::size_t Index
  1256. TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
  1257. auto *Index = NonTypeTemplateParmDecl::Create(
  1258. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
  1259. /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
  1260. // typename ...T
  1261. auto *Ts = TemplateTypeParmDecl::Create(
  1262. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
  1263. /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true,
  1264. /*HasTypeConstraint=*/false);
  1265. Ts->setImplicit(true);
  1266. // template <std::size_t Index, typename ...T>
  1267. NamedDecl *Params[] = {Index, Ts};
  1268. return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
  1269. llvm::makeArrayRef(Params),
  1270. SourceLocation(), nullptr);
  1271. }
  1272. static TemplateParameterList *createBuiltinTemplateParameterList(
  1273. const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
  1274. switch (BTK) {
  1275. case BTK__make_integer_seq:
  1276. return createMakeIntegerSeqParameterList(C, DC);
  1277. case BTK__type_pack_element:
  1278. return createTypePackElementParameterList(C, DC);
  1279. }
  1280. llvm_unreachable("unhandled BuiltinTemplateKind!");
  1281. }
  1282. void BuiltinTemplateDecl::anchor() {}
  1283. BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
  1284. DeclarationName Name,
  1285. BuiltinTemplateKind BTK)
  1286. : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name,
  1287. createBuiltinTemplateParameterList(C, DC, BTK)),
  1288. BTK(BTK) {}
  1289. void TypeConstraint::print(llvm::raw_ostream &OS, PrintingPolicy Policy) const {
  1290. if (NestedNameSpec)
  1291. NestedNameSpec.getNestedNameSpecifier()->print(OS, Policy);
  1292. ConceptName.printName(OS, Policy);
  1293. if (hasExplicitTemplateArgs()) {
  1294. OS << "<";
  1295. // FIXME: Find corresponding parameter for argument
  1296. for (auto &ArgLoc : ArgsAsWritten->arguments())
  1297. ArgLoc.getArgument().print(Policy, OS, /*IncludeType*/ false);
  1298. OS << ">";
  1299. }
  1300. }
  1301. TemplateParamObjectDecl *TemplateParamObjectDecl::Create(const ASTContext &C,
  1302. QualType T,
  1303. const APValue &V) {
  1304. DeclContext *DC = C.getTranslationUnitDecl();
  1305. auto *TPOD = new (C, DC) TemplateParamObjectDecl(DC, T, V);
  1306. C.addDestruction(&TPOD->Value);
  1307. return TPOD;
  1308. }
  1309. TemplateParamObjectDecl *
  1310. TemplateParamObjectDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  1311. auto *TPOD = new (C, ID) TemplateParamObjectDecl(nullptr, QualType(), APValue());
  1312. C.addDestruction(&TPOD->Value);
  1313. return TPOD;
  1314. }
  1315. void TemplateParamObjectDecl::printName(llvm::raw_ostream &OS) const {
  1316. OS << "<template param ";
  1317. printAsExpr(OS);
  1318. OS << ">";
  1319. }
  1320. void TemplateParamObjectDecl::printAsExpr(llvm::raw_ostream &OS) const {
  1321. const ASTContext &Ctx = getASTContext();
  1322. getType().getUnqualifiedType().print(OS, Ctx.getPrintingPolicy());
  1323. printAsInit(OS);
  1324. }
  1325. void TemplateParamObjectDecl::printAsInit(llvm::raw_ostream &OS) const {
  1326. const ASTContext &Ctx = getASTContext();
  1327. getValue().printPretty(OS, Ctx, getType());
  1328. }