DeclTemplate.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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/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 <utility>
  38. using namespace clang;
  39. //===----------------------------------------------------------------------===//
  40. // TemplateParameterList Implementation
  41. //===----------------------------------------------------------------------===//
  42. TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
  43. SourceLocation LAngleLoc,
  44. ArrayRef<NamedDecl *> Params,
  45. SourceLocation RAngleLoc,
  46. Expr *RequiresClause)
  47. : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
  48. NumParams(Params.size()), ContainsUnexpandedParameterPack(false),
  49. HasRequiresClause(static_cast<bool>(RequiresClause)) {
  50. for (unsigned Idx = 0; Idx < NumParams; ++Idx) {
  51. NamedDecl *P = Params[Idx];
  52. begin()[Idx] = P;
  53. if (!P->isTemplateParameterPack()) {
  54. if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P))
  55. if (NTTP->getType()->containsUnexpandedParameterPack())
  56. ContainsUnexpandedParameterPack = true;
  57. if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
  58. if (TTP->getTemplateParameters()->containsUnexpandedParameterPack())
  59. ContainsUnexpandedParameterPack = true;
  60. // FIXME: If a default argument contains an unexpanded parameter pack, the
  61. // template parameter list does too.
  62. }
  63. }
  64. if (RequiresClause) {
  65. *getTrailingObjects<Expr *>() = RequiresClause;
  66. if (RequiresClause->containsUnexpandedParameterPack())
  67. ContainsUnexpandedParameterPack = true;
  68. }
  69. }
  70. TemplateParameterList *
  71. TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
  72. SourceLocation LAngleLoc,
  73. ArrayRef<NamedDecl *> Params,
  74. SourceLocation RAngleLoc, Expr *RequiresClause) {
  75. void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *, Expr *>(
  76. Params.size(), RequiresClause ? 1u : 0u),
  77. alignof(TemplateParameterList));
  78. return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
  79. RAngleLoc, RequiresClause);
  80. }
  81. unsigned TemplateParameterList::getMinRequiredArguments() const {
  82. unsigned NumRequiredArgs = 0;
  83. for (const NamedDecl *P : asArray()) {
  84. if (P->isTemplateParameterPack()) {
  85. if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P))
  86. if (NTTP->isExpandedParameterPack()) {
  87. NumRequiredArgs += NTTP->getNumExpansionTypes();
  88. continue;
  89. }
  90. break;
  91. }
  92. if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
  93. if (TTP->hasDefaultArgument())
  94. break;
  95. } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
  96. if (NTTP->hasDefaultArgument())
  97. break;
  98. } else if (cast<TemplateTemplateParmDecl>(P)->hasDefaultArgument())
  99. break;
  100. ++NumRequiredArgs;
  101. }
  102. return NumRequiredArgs;
  103. }
  104. unsigned TemplateParameterList::getDepth() const {
  105. if (size() == 0)
  106. return 0;
  107. const NamedDecl *FirstParm = getParam(0);
  108. if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(FirstParm))
  109. return TTP->getDepth();
  110. else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
  111. return NTTP->getDepth();
  112. else
  113. return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
  114. }
  115. static void AdoptTemplateParameterList(TemplateParameterList *Params,
  116. DeclContext *Owner) {
  117. for (NamedDecl *P : *Params) {
  118. P->setDeclContext(Owner);
  119. if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
  120. AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner);
  121. }
  122. }
  123. void TemplateParameterList::
  124. getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
  125. // TODO: Concepts: Collect immediately-introduced constraints.
  126. if (HasRequiresClause)
  127. AC.push_back(getRequiresClause());
  128. }
  129. bool TemplateParameterList::hasAssociatedConstraints() const {
  130. // TODO: Concepts: Regard immediately-introduced constraints.
  131. return HasRequiresClause;
  132. }
  133. namespace clang {
  134. void *allocateDefaultArgStorageChain(const ASTContext &C) {
  135. return new (C) char[sizeof(void*) * 2];
  136. }
  137. } // namespace clang
  138. //===----------------------------------------------------------------------===//
  139. // TemplateDecl Implementation
  140. //===----------------------------------------------------------------------===//
  141. TemplateDecl::TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
  142. DeclarationName Name, TemplateParameterList *Params,
  143. NamedDecl *Decl)
  144. : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl), TemplateParams(Params) {}
  145. void TemplateDecl::anchor() {}
  146. void TemplateDecl::
  147. getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
  148. // TODO: Concepts: Append function trailing requires clause.
  149. TemplateParams->getAssociatedConstraints(AC);
  150. }
  151. bool TemplateDecl::hasAssociatedConstraints() const {
  152. // TODO: Concepts: Regard function trailing requires clause.
  153. return TemplateParams->hasAssociatedConstraints();
  154. }
  155. //===----------------------------------------------------------------------===//
  156. // RedeclarableTemplateDecl Implementation
  157. //===----------------------------------------------------------------------===//
  158. void RedeclarableTemplateDecl::anchor() {}
  159. RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
  160. if (Common)
  161. return Common;
  162. // Walk the previous-declaration chain until we either find a declaration
  163. // with a common pointer or we run out of previous declarations.
  164. SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
  165. for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
  166. Prev = Prev->getPreviousDecl()) {
  167. if (Prev->Common) {
  168. Common = Prev->Common;
  169. break;
  170. }
  171. PrevDecls.push_back(Prev);
  172. }
  173. // If we never found a common pointer, allocate one now.
  174. if (!Common) {
  175. // FIXME: If any of the declarations is from an AST file, we probably
  176. // need an update record to add the common data.
  177. Common = newCommon(getASTContext());
  178. }
  179. // Update any previous declarations we saw with the common pointer.
  180. for (const RedeclarableTemplateDecl *Prev : PrevDecls)
  181. Prev->Common = Common;
  182. return Common;
  183. }
  184. void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
  185. // Grab the most recent declaration to ensure we've loaded any lazy
  186. // redeclarations of this template.
  187. CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
  188. if (CommonBasePtr->LazySpecializations) {
  189. ASTContext &Context = getASTContext();
  190. uint32_t *Specs = CommonBasePtr->LazySpecializations;
  191. CommonBasePtr->LazySpecializations = nullptr;
  192. for (uint32_t I = 0, N = *Specs++; I != N; ++I)
  193. (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
  194. }
  195. }
  196. template<class EntryType>
  197. typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
  198. RedeclarableTemplateDecl::findSpecializationImpl(
  199. llvm::FoldingSetVector<EntryType> &Specs, ArrayRef<TemplateArgument> Args,
  200. void *&InsertPos) {
  201. using SETraits = SpecEntryTraits<EntryType>;
  202. llvm::FoldingSetNodeID ID;
  203. EntryType::Profile(ID, Args, getASTContext());
  204. EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
  205. return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr;
  206. }
  207. template<class Derived, class EntryType>
  208. void RedeclarableTemplateDecl::addSpecializationImpl(
  209. llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
  210. void *InsertPos) {
  211. using SETraits = SpecEntryTraits<EntryType>;
  212. if (InsertPos) {
  213. #ifndef NDEBUG
  214. void *CorrectInsertPos;
  215. assert(!findSpecializationImpl(Specializations,
  216. SETraits::getTemplateArgs(Entry),
  217. CorrectInsertPos) &&
  218. InsertPos == CorrectInsertPos &&
  219. "given incorrect InsertPos for specialization");
  220. #endif
  221. Specializations.InsertNode(Entry, InsertPos);
  222. } else {
  223. EntryType *Existing = Specializations.GetOrInsertNode(Entry);
  224. (void)Existing;
  225. assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
  226. "non-canonical specialization?");
  227. }
  228. if (ASTMutationListener *L = getASTMutationListener())
  229. L->AddedCXXTemplateSpecialization(cast<Derived>(this),
  230. SETraits::getDecl(Entry));
  231. }
  232. //===----------------------------------------------------------------------===//
  233. // FunctionTemplateDecl Implementation
  234. //===----------------------------------------------------------------------===//
  235. FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
  236. DeclContext *DC,
  237. SourceLocation L,
  238. DeclarationName Name,
  239. TemplateParameterList *Params,
  240. NamedDecl *Decl) {
  241. AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
  242. return new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
  243. }
  244. FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
  245. unsigned ID) {
  246. return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
  247. DeclarationName(), nullptr, nullptr);
  248. }
  249. RedeclarableTemplateDecl::CommonBase *
  250. FunctionTemplateDecl::newCommon(ASTContext &C) const {
  251. auto *CommonPtr = new (C) Common;
  252. C.addDestruction(CommonPtr);
  253. return CommonPtr;
  254. }
  255. void FunctionTemplateDecl::LoadLazySpecializations() const {
  256. loadLazySpecializationsImpl();
  257. }
  258. llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
  259. FunctionTemplateDecl::getSpecializations() const {
  260. LoadLazySpecializations();
  261. return getCommonPtr()->Specializations;
  262. }
  263. FunctionDecl *
  264. FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
  265. void *&InsertPos) {
  266. return findSpecializationImpl(getSpecializations(), Args, InsertPos);
  267. }
  268. void FunctionTemplateDecl::addSpecialization(
  269. FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
  270. addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info,
  271. InsertPos);
  272. }
  273. ArrayRef<TemplateArgument> FunctionTemplateDecl::getInjectedTemplateArgs() {
  274. TemplateParameterList *Params = getTemplateParameters();
  275. Common *CommonPtr = getCommonPtr();
  276. if (!CommonPtr->InjectedArgs) {
  277. auto &Context = getASTContext();
  278. SmallVector<TemplateArgument, 16> TemplateArgs;
  279. Context.getInjectedTemplateArgs(Params, TemplateArgs);
  280. CommonPtr->InjectedArgs =
  281. new (Context) TemplateArgument[TemplateArgs.size()];
  282. std::copy(TemplateArgs.begin(), TemplateArgs.end(),
  283. CommonPtr->InjectedArgs);
  284. }
  285. return llvm::makeArrayRef(CommonPtr->InjectedArgs, Params->size());
  286. }
  287. void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
  288. using Base = RedeclarableTemplateDecl;
  289. // If we haven't created a common pointer yet, then it can just be created
  290. // with the usual method.
  291. if (!Base::Common)
  292. return;
  293. Common *ThisCommon = static_cast<Common *>(Base::Common);
  294. Common *PrevCommon = nullptr;
  295. SmallVector<FunctionTemplateDecl *, 8> PreviousDecls;
  296. for (; Prev; Prev = Prev->getPreviousDecl()) {
  297. if (Prev->Base::Common) {
  298. PrevCommon = static_cast<Common *>(Prev->Base::Common);
  299. break;
  300. }
  301. PreviousDecls.push_back(Prev);
  302. }
  303. // If the previous redecl chain hasn't created a common pointer yet, then just
  304. // use this common pointer.
  305. if (!PrevCommon) {
  306. for (auto *D : PreviousDecls)
  307. D->Base::Common = ThisCommon;
  308. return;
  309. }
  310. // Ensure we don't leak any important state.
  311. assert(ThisCommon->Specializations.size() == 0 &&
  312. "Can't merge incompatible declarations!");
  313. Base::Common = PrevCommon;
  314. }
  315. //===----------------------------------------------------------------------===//
  316. // ClassTemplateDecl Implementation
  317. //===----------------------------------------------------------------------===//
  318. ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
  319. DeclContext *DC,
  320. SourceLocation L,
  321. DeclarationName Name,
  322. TemplateParameterList *Params,
  323. NamedDecl *Decl) {
  324. AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
  325. return new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl);
  326. }
  327. ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
  328. unsigned ID) {
  329. return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
  330. DeclarationName(), nullptr, nullptr);
  331. }
  332. void ClassTemplateDecl::LoadLazySpecializations() const {
  333. loadLazySpecializationsImpl();
  334. }
  335. llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
  336. ClassTemplateDecl::getSpecializations() const {
  337. LoadLazySpecializations();
  338. return getCommonPtr()->Specializations;
  339. }
  340. llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
  341. ClassTemplateDecl::getPartialSpecializations() {
  342. LoadLazySpecializations();
  343. return getCommonPtr()->PartialSpecializations;
  344. }
  345. RedeclarableTemplateDecl::CommonBase *
  346. ClassTemplateDecl::newCommon(ASTContext &C) const {
  347. auto *CommonPtr = new (C) Common;
  348. C.addDestruction(CommonPtr);
  349. return CommonPtr;
  350. }
  351. ClassTemplateSpecializationDecl *
  352. ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
  353. void *&InsertPos) {
  354. return findSpecializationImpl(getSpecializations(), Args, InsertPos);
  355. }
  356. void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
  357. void *InsertPos) {
  358. addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos);
  359. }
  360. ClassTemplatePartialSpecializationDecl *
  361. ClassTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
  362. void *&InsertPos) {
  363. return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
  364. }
  365. void ClassTemplateDecl::AddPartialSpecialization(
  366. ClassTemplatePartialSpecializationDecl *D,
  367. void *InsertPos) {
  368. if (InsertPos)
  369. getPartialSpecializations().InsertNode(D, InsertPos);
  370. else {
  371. ClassTemplatePartialSpecializationDecl *Existing
  372. = getPartialSpecializations().GetOrInsertNode(D);
  373. (void)Existing;
  374. assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
  375. }
  376. if (ASTMutationListener *L = getASTMutationListener())
  377. L->AddedCXXTemplateSpecialization(this, D);
  378. }
  379. void ClassTemplateDecl::getPartialSpecializations(
  380. SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
  381. llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
  382. = getPartialSpecializations();
  383. PS.clear();
  384. PS.reserve(PartialSpecs.size());
  385. for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs)
  386. PS.push_back(P.getMostRecentDecl());
  387. }
  388. ClassTemplatePartialSpecializationDecl *
  389. ClassTemplateDecl::findPartialSpecialization(QualType T) {
  390. ASTContext &Context = getASTContext();
  391. for (ClassTemplatePartialSpecializationDecl &P :
  392. getPartialSpecializations()) {
  393. if (Context.hasSameType(P.getInjectedSpecializationType(), T))
  394. return P.getMostRecentDecl();
  395. }
  396. return nullptr;
  397. }
  398. ClassTemplatePartialSpecializationDecl *
  399. ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
  400. ClassTemplatePartialSpecializationDecl *D) {
  401. Decl *DCanon = D->getCanonicalDecl();
  402. for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
  403. if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
  404. return P.getMostRecentDecl();
  405. }
  406. return nullptr;
  407. }
  408. QualType
  409. ClassTemplateDecl::getInjectedClassNameSpecialization() {
  410. Common *CommonPtr = getCommonPtr();
  411. if (!CommonPtr->InjectedClassNameType.isNull())
  412. return CommonPtr->InjectedClassNameType;
  413. // C++0x [temp.dep.type]p2:
  414. // The template argument list of a primary template is a template argument
  415. // list in which the nth template argument has the value of the nth template
  416. // parameter of the class template. If the nth template parameter is a
  417. // template parameter pack (14.5.3), the nth template argument is a pack
  418. // expansion (14.5.3) whose pattern is the name of the template parameter
  419. // pack.
  420. ASTContext &Context = getASTContext();
  421. TemplateParameterList *Params = getTemplateParameters();
  422. SmallVector<TemplateArgument, 16> TemplateArgs;
  423. Context.getInjectedTemplateArgs(Params, TemplateArgs);
  424. CommonPtr->InjectedClassNameType
  425. = Context.getTemplateSpecializationType(TemplateName(this),
  426. TemplateArgs);
  427. return CommonPtr->InjectedClassNameType;
  428. }
  429. //===----------------------------------------------------------------------===//
  430. // TemplateTypeParm Allocation/Deallocation Method Implementations
  431. //===----------------------------------------------------------------------===//
  432. TemplateTypeParmDecl *
  433. TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
  434. SourceLocation KeyLoc, SourceLocation NameLoc,
  435. unsigned D, unsigned P, IdentifierInfo *Id,
  436. bool Typename, bool ParameterPack) {
  437. auto *TTPDecl =
  438. new (C, DC) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
  439. QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
  440. TTPDecl->setTypeForDecl(TTPType.getTypePtr());
  441. return TTPDecl;
  442. }
  443. TemplateTypeParmDecl *
  444. TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
  445. return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(),
  446. SourceLocation(), nullptr, false);
  447. }
  448. SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
  449. return hasDefaultArgument()
  450. ? getDefaultArgumentInfo()->getTypeLoc().getBeginLoc()
  451. : SourceLocation();
  452. }
  453. SourceRange TemplateTypeParmDecl::getSourceRange() const {
  454. if (hasDefaultArgument() && !defaultArgumentWasInherited())
  455. return SourceRange(getBeginLoc(),
  456. getDefaultArgumentInfo()->getTypeLoc().getEndLoc());
  457. // TypeDecl::getSourceRange returns a range containing name location, which is
  458. // wrong for unnamed template parameters. e.g:
  459. // it will return <[[typename>]] instead of <[[typename]]>
  460. else if (getDeclName().isEmpty())
  461. return SourceRange(getBeginLoc());
  462. return TypeDecl::getSourceRange();
  463. }
  464. unsigned TemplateTypeParmDecl::getDepth() const {
  465. return getTypeForDecl()->castAs<TemplateTypeParmType>()->getDepth();
  466. }
  467. unsigned TemplateTypeParmDecl::getIndex() const {
  468. return getTypeForDecl()->castAs<TemplateTypeParmType>()->getIndex();
  469. }
  470. bool TemplateTypeParmDecl::isParameterPack() const {
  471. return getTypeForDecl()->castAs<TemplateTypeParmType>()->isParameterPack();
  472. }
  473. //===----------------------------------------------------------------------===//
  474. // NonTypeTemplateParmDecl Method Implementations
  475. //===----------------------------------------------------------------------===//
  476. NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(
  477. DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D,
  478. unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
  479. ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos)
  480. : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
  481. TemplateParmPosition(D, P), ParameterPack(true),
  482. ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) {
  483. if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) {
  484. auto TypesAndInfos =
  485. getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
  486. for (unsigned I = 0; I != NumExpandedTypes; ++I) {
  487. new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
  488. TypesAndInfos[I].second = ExpandedTInfos[I];
  489. }
  490. }
  491. }
  492. NonTypeTemplateParmDecl *
  493. NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
  494. SourceLocation StartLoc, SourceLocation IdLoc,
  495. unsigned D, unsigned P, IdentifierInfo *Id,
  496. QualType T, bool ParameterPack,
  497. TypeSourceInfo *TInfo) {
  498. return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
  499. T, ParameterPack, TInfo);
  500. }
  501. NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
  502. const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
  503. SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
  504. QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
  505. ArrayRef<TypeSourceInfo *> ExpandedTInfos) {
  506. return new (C, DC,
  507. additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
  508. ExpandedTypes.size()))
  509. NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
  510. ExpandedTypes, ExpandedTInfos);
  511. }
  512. NonTypeTemplateParmDecl *
  513. NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  514. return new (C, ID) NonTypeTemplateParmDecl(nullptr, SourceLocation(),
  515. SourceLocation(), 0, 0, nullptr,
  516. QualType(), false, nullptr);
  517. }
  518. NonTypeTemplateParmDecl *
  519. NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  520. unsigned NumExpandedTypes) {
  521. auto *NTTP =
  522. new (C, ID, additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
  523. NumExpandedTypes))
  524. NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
  525. 0, 0, nullptr, QualType(), nullptr, None,
  526. None);
  527. NTTP->NumExpandedTypes = NumExpandedTypes;
  528. return NTTP;
  529. }
  530. SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
  531. if (hasDefaultArgument() && !defaultArgumentWasInherited())
  532. return SourceRange(getOuterLocStart(),
  533. getDefaultArgument()->getSourceRange().getEnd());
  534. return DeclaratorDecl::getSourceRange();
  535. }
  536. SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
  537. return hasDefaultArgument()
  538. ? getDefaultArgument()->getSourceRange().getBegin()
  539. : SourceLocation();
  540. }
  541. //===----------------------------------------------------------------------===//
  542. // TemplateTemplateParmDecl Method Implementations
  543. //===----------------------------------------------------------------------===//
  544. void TemplateTemplateParmDecl::anchor() {}
  545. TemplateTemplateParmDecl::TemplateTemplateParmDecl(
  546. DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
  547. IdentifierInfo *Id, TemplateParameterList *Params,
  548. ArrayRef<TemplateParameterList *> Expansions)
  549. : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
  550. TemplateParmPosition(D, P), ParameterPack(true),
  551. ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) {
  552. if (!Expansions.empty())
  553. std::uninitialized_copy(Expansions.begin(), Expansions.end(),
  554. getTrailingObjects<TemplateParameterList *>());
  555. }
  556. TemplateTemplateParmDecl *
  557. TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
  558. SourceLocation L, unsigned D, unsigned P,
  559. bool ParameterPack, IdentifierInfo *Id,
  560. TemplateParameterList *Params) {
  561. return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
  562. Params);
  563. }
  564. TemplateTemplateParmDecl *
  565. TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
  566. SourceLocation L, unsigned D, unsigned P,
  567. IdentifierInfo *Id,
  568. TemplateParameterList *Params,
  569. ArrayRef<TemplateParameterList *> Expansions) {
  570. return new (C, DC,
  571. additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
  572. TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions);
  573. }
  574. TemplateTemplateParmDecl *
  575. TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  576. return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
  577. false, nullptr, nullptr);
  578. }
  579. TemplateTemplateParmDecl *
  580. TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
  581. unsigned NumExpansions) {
  582. auto *TTP =
  583. new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
  584. TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
  585. nullptr, None);
  586. TTP->NumExpandedParams = NumExpansions;
  587. return TTP;
  588. }
  589. SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
  590. return hasDefaultArgument() ? getDefaultArgument().getLocation()
  591. : SourceLocation();
  592. }
  593. void TemplateTemplateParmDecl::setDefaultArgument(
  594. const ASTContext &C, const TemplateArgumentLoc &DefArg) {
  595. if (DefArg.getArgument().isNull())
  596. DefaultArgument.set(nullptr);
  597. else
  598. DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
  599. }
  600. //===----------------------------------------------------------------------===//
  601. // TemplateArgumentList Implementation
  602. //===----------------------------------------------------------------------===//
  603. TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
  604. : Arguments(getTrailingObjects<TemplateArgument>()),
  605. NumArguments(Args.size()) {
  606. std::uninitialized_copy(Args.begin(), Args.end(),
  607. getTrailingObjects<TemplateArgument>());
  608. }
  609. TemplateArgumentList *
  610. TemplateArgumentList::CreateCopy(ASTContext &Context,
  611. ArrayRef<TemplateArgument> Args) {
  612. void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
  613. return new (Mem) TemplateArgumentList(Args);
  614. }
  615. FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
  616. ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
  617. TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs,
  618. const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI,
  619. MemberSpecializationInfo *MSInfo) {
  620. const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
  621. if (TemplateArgsAsWritten)
  622. ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
  623. *TemplateArgsAsWritten);
  624. void *Mem =
  625. C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 1 : 0));
  626. return new (Mem) FunctionTemplateSpecializationInfo(
  627. FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo);
  628. }
  629. //===----------------------------------------------------------------------===//
  630. // ClassTemplateSpecializationDecl Implementation
  631. //===----------------------------------------------------------------------===//
  632. ClassTemplateSpecializationDecl::
  633. ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
  634. DeclContext *DC, SourceLocation StartLoc,
  635. SourceLocation IdLoc,
  636. ClassTemplateDecl *SpecializedTemplate,
  637. ArrayRef<TemplateArgument> Args,
  638. ClassTemplateSpecializationDecl *PrevDecl)
  639. : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
  640. SpecializedTemplate->getIdentifier(), PrevDecl),
  641. SpecializedTemplate(SpecializedTemplate),
  642. TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
  643. SpecializationKind(TSK_Undeclared) {
  644. }
  645. ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
  646. Kind DK)
  647. : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
  648. SourceLocation(), nullptr, nullptr),
  649. SpecializationKind(TSK_Undeclared) {}
  650. ClassTemplateSpecializationDecl *
  651. ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
  652. DeclContext *DC,
  653. SourceLocation StartLoc,
  654. SourceLocation IdLoc,
  655. ClassTemplateDecl *SpecializedTemplate,
  656. ArrayRef<TemplateArgument> Args,
  657. ClassTemplateSpecializationDecl *PrevDecl) {
  658. auto *Result =
  659. new (Context, DC) ClassTemplateSpecializationDecl(
  660. Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
  661. SpecializedTemplate, Args, PrevDecl);
  662. Result->setMayHaveOutOfDateDef(false);
  663. Context.getTypeDeclType(Result, PrevDecl);
  664. return Result;
  665. }
  666. ClassTemplateSpecializationDecl *
  667. ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
  668. unsigned ID) {
  669. auto *Result =
  670. new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
  671. Result->setMayHaveOutOfDateDef(false);
  672. return Result;
  673. }
  674. void ClassTemplateSpecializationDecl::getNameForDiagnostic(
  675. raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
  676. NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
  677. const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this);
  678. if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
  679. PS ? PS->getTemplateArgsAsWritten() : nullptr) {
  680. printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
  681. } else {
  682. const TemplateArgumentList &TemplateArgs = getTemplateArgs();
  683. printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
  684. }
  685. }
  686. ClassTemplateDecl *
  687. ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
  688. if (const auto *PartialSpec =
  689. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
  690. return PartialSpec->PartialSpecialization->getSpecializedTemplate();
  691. return SpecializedTemplate.get<ClassTemplateDecl*>();
  692. }
  693. SourceRange
  694. ClassTemplateSpecializationDecl::getSourceRange() const {
  695. if (ExplicitInfo) {
  696. SourceLocation Begin = getTemplateKeywordLoc();
  697. if (Begin.isValid()) {
  698. // Here we have an explicit (partial) specialization or instantiation.
  699. assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
  700. getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
  701. getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
  702. if (getExternLoc().isValid())
  703. Begin = getExternLoc();
  704. SourceLocation End = getBraceRange().getEnd();
  705. if (End.isInvalid())
  706. End = getTypeAsWritten()->getTypeLoc().getEndLoc();
  707. return SourceRange(Begin, End);
  708. }
  709. // An implicit instantiation of a class template partial specialization
  710. // uses ExplicitInfo to record the TypeAsWritten, but the source
  711. // locations should be retrieved from the instantiation pattern.
  712. using CTPSDecl = ClassTemplatePartialSpecializationDecl;
  713. auto *ctpsd = const_cast<CTPSDecl *>(cast<CTPSDecl>(this));
  714. CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
  715. assert(inst_from != nullptr);
  716. return inst_from->getSourceRange();
  717. }
  718. else {
  719. // No explicit info available.
  720. llvm::PointerUnion<ClassTemplateDecl *,
  721. ClassTemplatePartialSpecializationDecl *>
  722. inst_from = getInstantiatedFrom();
  723. if (inst_from.isNull())
  724. return getSpecializedTemplate()->getSourceRange();
  725. if (const auto *ctd = inst_from.dyn_cast<ClassTemplateDecl *>())
  726. return ctd->getSourceRange();
  727. return inst_from.get<ClassTemplatePartialSpecializationDecl *>()
  728. ->getSourceRange();
  729. }
  730. }
  731. //===----------------------------------------------------------------------===//
  732. // ConceptDecl Implementation
  733. //===----------------------------------------------------------------------===//
  734. ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC,
  735. SourceLocation L, DeclarationName Name,
  736. TemplateParameterList *Params,
  737. Expr *ConstraintExpr) {
  738. AdoptTemplateParameterList(Params, DC);
  739. return new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr);
  740. }
  741. ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C,
  742. unsigned ID) {
  743. ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(),
  744. DeclarationName(),
  745. nullptr, nullptr);
  746. return Result;
  747. }
  748. //===----------------------------------------------------------------------===//
  749. // ClassTemplatePartialSpecializationDecl Implementation
  750. //===----------------------------------------------------------------------===//
  751. void ClassTemplatePartialSpecializationDecl::anchor() {}
  752. ClassTemplatePartialSpecializationDecl::
  753. ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
  754. DeclContext *DC,
  755. SourceLocation StartLoc,
  756. SourceLocation IdLoc,
  757. TemplateParameterList *Params,
  758. ClassTemplateDecl *SpecializedTemplate,
  759. ArrayRef<TemplateArgument> Args,
  760. const ASTTemplateArgumentListInfo *ArgInfos,
  761. ClassTemplatePartialSpecializationDecl *PrevDecl)
  762. : ClassTemplateSpecializationDecl(Context,
  763. ClassTemplatePartialSpecialization,
  764. TK, DC, StartLoc, IdLoc,
  765. SpecializedTemplate, Args, PrevDecl),
  766. TemplateParams(Params), ArgsAsWritten(ArgInfos),
  767. InstantiatedFromMember(nullptr, false) {
  768. AdoptTemplateParameterList(Params, this);
  769. }
  770. ClassTemplatePartialSpecializationDecl *
  771. ClassTemplatePartialSpecializationDecl::
  772. Create(ASTContext &Context, TagKind TK,DeclContext *DC,
  773. SourceLocation StartLoc, SourceLocation IdLoc,
  774. TemplateParameterList *Params,
  775. ClassTemplateDecl *SpecializedTemplate,
  776. ArrayRef<TemplateArgument> Args,
  777. const TemplateArgumentListInfo &ArgInfos,
  778. QualType CanonInjectedType,
  779. ClassTemplatePartialSpecializationDecl *PrevDecl) {
  780. const ASTTemplateArgumentListInfo *ASTArgInfos =
  781. ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
  782. auto *Result = new (Context, DC)
  783. ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
  784. Params, SpecializedTemplate, Args,
  785. ASTArgInfos, PrevDecl);
  786. Result->setSpecializationKind(TSK_ExplicitSpecialization);
  787. Result->setMayHaveOutOfDateDef(false);
  788. Context.getInjectedClassNameType(Result, CanonInjectedType);
  789. return Result;
  790. }
  791. ClassTemplatePartialSpecializationDecl *
  792. ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
  793. unsigned ID) {
  794. auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C);
  795. Result->setMayHaveOutOfDateDef(false);
  796. return Result;
  797. }
  798. //===----------------------------------------------------------------------===//
  799. // FriendTemplateDecl Implementation
  800. //===----------------------------------------------------------------------===//
  801. void FriendTemplateDecl::anchor() {}
  802. FriendTemplateDecl *
  803. FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
  804. SourceLocation L,
  805. MutableArrayRef<TemplateParameterList *> Params,
  806. FriendUnion Friend, SourceLocation FLoc) {
  807. return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc);
  808. }
  809. FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
  810. unsigned ID) {
  811. return new (C, ID) FriendTemplateDecl(EmptyShell());
  812. }
  813. //===----------------------------------------------------------------------===//
  814. // TypeAliasTemplateDecl Implementation
  815. //===----------------------------------------------------------------------===//
  816. TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
  817. DeclContext *DC,
  818. SourceLocation L,
  819. DeclarationName Name,
  820. TemplateParameterList *Params,
  821. NamedDecl *Decl) {
  822. AdoptTemplateParameterList(Params, DC);
  823. return new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
  824. }
  825. TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
  826. unsigned ID) {
  827. return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
  828. DeclarationName(), nullptr, nullptr);
  829. }
  830. RedeclarableTemplateDecl::CommonBase *
  831. TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
  832. auto *CommonPtr = new (C) Common;
  833. C.addDestruction(CommonPtr);
  834. return CommonPtr;
  835. }
  836. //===----------------------------------------------------------------------===//
  837. // ClassScopeFunctionSpecializationDecl Implementation
  838. //===----------------------------------------------------------------------===//
  839. void ClassScopeFunctionSpecializationDecl::anchor() {}
  840. ClassScopeFunctionSpecializationDecl *
  841. ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
  842. unsigned ID) {
  843. return new (C, ID) ClassScopeFunctionSpecializationDecl(
  844. nullptr, SourceLocation(), nullptr, nullptr);
  845. }
  846. //===----------------------------------------------------------------------===//
  847. // VarTemplateDecl Implementation
  848. //===----------------------------------------------------------------------===//
  849. VarTemplateDecl *VarTemplateDecl::getDefinition() {
  850. VarTemplateDecl *CurD = this;
  851. while (CurD) {
  852. if (CurD->isThisDeclarationADefinition())
  853. return CurD;
  854. CurD = CurD->getPreviousDecl();
  855. }
  856. return nullptr;
  857. }
  858. VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
  859. SourceLocation L, DeclarationName Name,
  860. TemplateParameterList *Params,
  861. VarDecl *Decl) {
  862. AdoptTemplateParameterList(Params, DC);
  863. return new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
  864. }
  865. VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
  866. unsigned ID) {
  867. return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
  868. DeclarationName(), nullptr, nullptr);
  869. }
  870. void VarTemplateDecl::LoadLazySpecializations() const {
  871. loadLazySpecializationsImpl();
  872. }
  873. llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
  874. VarTemplateDecl::getSpecializations() const {
  875. LoadLazySpecializations();
  876. return getCommonPtr()->Specializations;
  877. }
  878. llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
  879. VarTemplateDecl::getPartialSpecializations() {
  880. LoadLazySpecializations();
  881. return getCommonPtr()->PartialSpecializations;
  882. }
  883. RedeclarableTemplateDecl::CommonBase *
  884. VarTemplateDecl::newCommon(ASTContext &C) const {
  885. auto *CommonPtr = new (C) Common;
  886. C.addDestruction(CommonPtr);
  887. return CommonPtr;
  888. }
  889. VarTemplateSpecializationDecl *
  890. VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
  891. void *&InsertPos) {
  892. return findSpecializationImpl(getSpecializations(), Args, InsertPos);
  893. }
  894. void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
  895. void *InsertPos) {
  896. addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos);
  897. }
  898. VarTemplatePartialSpecializationDecl *
  899. VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
  900. void *&InsertPos) {
  901. return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
  902. }
  903. void VarTemplateDecl::AddPartialSpecialization(
  904. VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
  905. if (InsertPos)
  906. getPartialSpecializations().InsertNode(D, InsertPos);
  907. else {
  908. VarTemplatePartialSpecializationDecl *Existing =
  909. getPartialSpecializations().GetOrInsertNode(D);
  910. (void)Existing;
  911. assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
  912. }
  913. if (ASTMutationListener *L = getASTMutationListener())
  914. L->AddedCXXTemplateSpecialization(this, D);
  915. }
  916. void VarTemplateDecl::getPartialSpecializations(
  917. SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) {
  918. llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
  919. getPartialSpecializations();
  920. PS.clear();
  921. PS.reserve(PartialSpecs.size());
  922. for (VarTemplatePartialSpecializationDecl &P : PartialSpecs)
  923. PS.push_back(P.getMostRecentDecl());
  924. }
  925. VarTemplatePartialSpecializationDecl *
  926. VarTemplateDecl::findPartialSpecInstantiatedFromMember(
  927. VarTemplatePartialSpecializationDecl *D) {
  928. Decl *DCanon = D->getCanonicalDecl();
  929. for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
  930. if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
  931. return P.getMostRecentDecl();
  932. }
  933. return nullptr;
  934. }
  935. //===----------------------------------------------------------------------===//
  936. // VarTemplateSpecializationDecl Implementation
  937. //===----------------------------------------------------------------------===//
  938. VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
  939. Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  940. SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
  941. TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args)
  942. : VarDecl(DK, Context, DC, StartLoc, IdLoc,
  943. SpecializedTemplate->getIdentifier(), T, TInfo, S),
  944. SpecializedTemplate(SpecializedTemplate),
  945. TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
  946. SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
  947. VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
  948. ASTContext &C)
  949. : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
  950. QualType(), nullptr, SC_None),
  951. SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
  952. VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
  953. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  954. SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
  955. TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) {
  956. return new (Context, DC) VarTemplateSpecializationDecl(
  957. VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
  958. SpecializedTemplate, T, TInfo, S, Args);
  959. }
  960. VarTemplateSpecializationDecl *
  961. VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
  962. return new (C, ID)
  963. VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
  964. }
  965. void VarTemplateSpecializationDecl::getNameForDiagnostic(
  966. raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
  967. NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
  968. const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this);
  969. if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
  970. PS ? PS->getTemplateArgsAsWritten() : nullptr) {
  971. printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
  972. } else {
  973. const TemplateArgumentList &TemplateArgs = getTemplateArgs();
  974. printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
  975. }
  976. }
  977. VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
  978. if (const auto *PartialSpec =
  979. SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
  980. return PartialSpec->PartialSpecialization->getSpecializedTemplate();
  981. return SpecializedTemplate.get<VarTemplateDecl *>();
  982. }
  983. void VarTemplateSpecializationDecl::setTemplateArgsInfo(
  984. const TemplateArgumentListInfo &ArgsInfo) {
  985. TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
  986. TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
  987. for (const TemplateArgumentLoc &Loc : ArgsInfo.arguments())
  988. TemplateArgsInfo.addArgument(Loc);
  989. }
  990. //===----------------------------------------------------------------------===//
  991. // VarTemplatePartialSpecializationDecl Implementation
  992. //===----------------------------------------------------------------------===//
  993. void VarTemplatePartialSpecializationDecl::anchor() {}
  994. VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
  995. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  996. SourceLocation IdLoc, TemplateParameterList *Params,
  997. VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
  998. StorageClass S, ArrayRef<TemplateArgument> Args,
  999. const ASTTemplateArgumentListInfo *ArgInfos)
  1000. : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
  1001. DC, StartLoc, IdLoc, SpecializedTemplate, T,
  1002. TInfo, S, Args),
  1003. TemplateParams(Params), ArgsAsWritten(ArgInfos),
  1004. InstantiatedFromMember(nullptr, false) {
  1005. // TODO: The template parameters should be in DC by now. Verify.
  1006. // AdoptTemplateParameterList(Params, DC);
  1007. }
  1008. VarTemplatePartialSpecializationDecl *
  1009. VarTemplatePartialSpecializationDecl::Create(
  1010. ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
  1011. SourceLocation IdLoc, TemplateParameterList *Params,
  1012. VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
  1013. StorageClass S, ArrayRef<TemplateArgument> Args,
  1014. const TemplateArgumentListInfo &ArgInfos) {
  1015. const ASTTemplateArgumentListInfo *ASTArgInfos
  1016. = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
  1017. auto *Result =
  1018. new (Context, DC) VarTemplatePartialSpecializationDecl(
  1019. Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
  1020. S, Args, ASTArgInfos);
  1021. Result->setSpecializationKind(TSK_ExplicitSpecialization);
  1022. return Result;
  1023. }
  1024. VarTemplatePartialSpecializationDecl *
  1025. VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
  1026. unsigned ID) {
  1027. return new (C, ID) VarTemplatePartialSpecializationDecl(C);
  1028. }
  1029. static TemplateParameterList *
  1030. createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
  1031. // typename T
  1032. auto *T = TemplateTypeParmDecl::Create(
  1033. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
  1034. /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
  1035. T->setImplicit(true);
  1036. // T ...Ints
  1037. TypeSourceInfo *TI =
  1038. C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
  1039. auto *N = NonTypeTemplateParmDecl::Create(
  1040. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
  1041. /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
  1042. N->setImplicit(true);
  1043. // <typename T, T ...Ints>
  1044. NamedDecl *P[2] = {T, N};
  1045. auto *TPL = TemplateParameterList::Create(
  1046. C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
  1047. // template <typename T, ...Ints> class IntSeq
  1048. auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
  1049. C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
  1050. /*ParameterPack=*/false, /*Id=*/nullptr, TPL);
  1051. TemplateTemplateParm->setImplicit(true);
  1052. // typename T
  1053. auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
  1054. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
  1055. /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
  1056. TemplateTypeParm->setImplicit(true);
  1057. // T N
  1058. TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
  1059. QualType(TemplateTypeParm->getTypeForDecl(), 0));
  1060. auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
  1061. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
  1062. /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
  1063. NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
  1064. NonTypeTemplateParm};
  1065. // template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
  1066. return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
  1067. Params, SourceLocation(), nullptr);
  1068. }
  1069. static TemplateParameterList *
  1070. createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
  1071. // std::size_t Index
  1072. TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
  1073. auto *Index = NonTypeTemplateParmDecl::Create(
  1074. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
  1075. /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
  1076. // typename ...T
  1077. auto *Ts = TemplateTypeParmDecl::Create(
  1078. C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
  1079. /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true);
  1080. Ts->setImplicit(true);
  1081. // template <std::size_t Index, typename ...T>
  1082. NamedDecl *Params[] = {Index, Ts};
  1083. return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
  1084. llvm::makeArrayRef(Params),
  1085. SourceLocation(), nullptr);
  1086. }
  1087. static TemplateParameterList *createBuiltinTemplateParameterList(
  1088. const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
  1089. switch (BTK) {
  1090. case BTK__make_integer_seq:
  1091. return createMakeIntegerSeqParameterList(C, DC);
  1092. case BTK__type_pack_element:
  1093. return createTypePackElementParameterList(C, DC);
  1094. }
  1095. llvm_unreachable("unhandled BuiltinTemplateKind!");
  1096. }
  1097. void BuiltinTemplateDecl::anchor() {}
  1098. BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
  1099. DeclarationName Name,
  1100. BuiltinTemplateKind BTK)
  1101. : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name,
  1102. createBuiltinTemplateParameterList(C, DC, BTK)),
  1103. BTK(BTK) {}