blob: c03ae22fb5d89d0e2c7eddfaee12018c06d9ddc9 [file] [log] [blame]
Eugene Zelenko421e8902017-11-29 22:39:22 +00001//===- DeclTemplate.cpp - Template Declaration AST Node Implementation ----===//
Douglas Gregorded2d7b2009-02-04 19:02:06 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Douglas Gregorded2d7b2009-02-04 19:02:06 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the C++ related Decl classes for templates.
10//
11//===----------------------------------------------------------------------===//
12
Douglas Gregorded2d7b2009-02-04 19:02:06 +000013#include "clang/AST/DeclTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTMutationListener.h"
16#include "clang/AST/DeclCXX.h"
Eugene Zelenko421e8902017-11-29 22:39:22 +000017#include "clang/AST/DeclarationName.h"
Douglas Gregor8bf42052009-02-09 18:46:07 +000018#include "clang/AST/Expr.h"
Eugene Zelenko309e29d2018-03-29 20:51:59 +000019#include "clang/AST/ExternalASTSource.h"
Eugene Zelenko421e8902017-11-29 22:39:22 +000020#include "clang/AST/TemplateBase.h"
21#include "clang/AST/TemplateName.h"
22#include "clang/AST/Type.h"
John McCall0ad16662009-10-29 08:12:44 +000023#include "clang/AST/TypeLoc.h"
David Majnemerd9b1a4f2015-11-04 03:40:30 +000024#include "clang/Basic/Builtins.h"
Eugene Zelenko421e8902017-11-29 22:39:22 +000025#include "clang/Basic/LLVM.h"
26#include "clang/Basic/SourceLocation.h"
27#include "llvm/ADT/ArrayRef.h"
28#include "llvm/ADT/FoldingSet.h"
29#include "llvm/ADT/None.h"
30#include "llvm/ADT/PointerUnion.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>
Douglas Gregor1ccc8412010-11-07 23:05:16 +000037#include <memory>
Eugene Zelenko421e8902017-11-29 22:39:22 +000038#include <utility>
39
Douglas Gregorded2d7b2009-02-04 19:02:06 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43// TemplateParameterList Implementation
44//===----------------------------------------------------------------------===//
45
Douglas Gregorcd72ba92009-02-06 22:42:48 +000046TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
47 SourceLocation LAngleLoc,
David Majnemer902f8c62015-12-27 07:16:27 +000048 ArrayRef<NamedDecl *> Params,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +000049 SourceLocation RAngleLoc,
50 Expr *RequiresClause)
Eugene Zelenko421e8902017-11-29 22:39:22 +000051 : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
52 NumParams(Params.size()), ContainsUnexpandedParameterPack(false),
53 HasRequiresClause(static_cast<bool>(RequiresClause)) {
Richard Smith1fde8ec2012-09-07 02:06:42 +000054 for (unsigned Idx = 0; Idx < NumParams; ++Idx) {
55 NamedDecl *P = Params[Idx];
56 begin()[Idx] = P;
57
58 if (!P->isTemplateParameterPack()) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +000059 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P))
Richard Smith1fde8ec2012-09-07 02:06:42 +000060 if (NTTP->getType()->containsUnexpandedParameterPack())
61 ContainsUnexpandedParameterPack = true;
62
Eugene Zelenko309e29d2018-03-29 20:51:59 +000063 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
Richard Smith1fde8ec2012-09-07 02:06:42 +000064 if (TTP->getTemplateParameters()->containsUnexpandedParameterPack())
65 ContainsUnexpandedParameterPack = true;
66
67 // FIXME: If a default argument contains an unexpanded parameter pack, the
68 // template parameter list does too.
69 }
70 }
Hubert Tonge4a0c0e2016-07-30 22:33:34 +000071 if (RequiresClause) {
72 *getTrailingObjects<Expr *>() = RequiresClause;
73 }
Douglas Gregorded2d7b2009-02-04 19:02:06 +000074}
75
Hubert Tonge4a0c0e2016-07-30 22:33:34 +000076TemplateParameterList *
77TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
78 SourceLocation LAngleLoc,
79 ArrayRef<NamedDecl *> Params,
80 SourceLocation RAngleLoc, Expr *RequiresClause) {
81 void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *, Expr *>(
82 Params.size(), RequiresClause ? 1u : 0u),
Benjamin Kramerc3f89252016-10-20 14:27:22 +000083 alignof(TemplateParameterList));
Mike Stump11289f42009-09-09 15:08:12 +000084 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +000085 RAngleLoc, RequiresClause);
Douglas Gregorded2d7b2009-02-04 19:02:06 +000086}
87
Douglas Gregorf8f86832009-02-11 18:16:40 +000088unsigned TemplateParameterList::getMinRequiredArguments() const {
Douglas Gregor0231d8d2011-01-19 20:10:05 +000089 unsigned NumRequiredArgs = 0;
David Majnemerdfecf1a2016-07-06 04:19:16 +000090 for (const NamedDecl *P : asArray()) {
91 if (P->isTemplateParameterPack()) {
92 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P))
Douglas Gregor0231d8d2011-01-19 20:10:05 +000093 if (NTTP->isExpandedParameterPack()) {
94 NumRequiredArgs += NTTP->getNumExpansionTypes();
95 continue;
96 }
David Majnemerdfecf1a2016-07-06 04:19:16 +000097
Douglas Gregorf8f86832009-02-11 18:16:40 +000098 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +000099 }
David Majnemerdfecf1a2016-07-06 04:19:16 +0000100
101 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000102 if (TTP->hasDefaultArgument())
103 break;
David Majnemerdfecf1a2016-07-06 04:19:16 +0000104 } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000105 if (NTTP->hasDefaultArgument())
106 break;
David Majnemerdfecf1a2016-07-06 04:19:16 +0000107 } else if (cast<TemplateTemplateParmDecl>(P)->hasDefaultArgument())
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000108 break;
David Majnemerdfecf1a2016-07-06 04:19:16 +0000109
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000110 ++NumRequiredArgs;
Douglas Gregorf8f86832009-02-11 18:16:40 +0000111 }
David Majnemerdfecf1a2016-07-06 04:19:16 +0000112
Douglas Gregorf8f86832009-02-11 18:16:40 +0000113 return NumRequiredArgs;
114}
115
Douglas Gregor21610382009-10-29 00:04:11 +0000116unsigned TemplateParameterList::getDepth() const {
117 if (size() == 0)
118 return 0;
Fangrui Song6907ce22018-07-30 19:24:48 +0000119
Douglas Gregor21610382009-10-29 00:04:11 +0000120 const NamedDecl *FirstParm = getParam(0);
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000121 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(FirstParm))
Douglas Gregor21610382009-10-29 00:04:11 +0000122 return TTP->getDepth();
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000123 else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
Douglas Gregor21610382009-10-29 00:04:11 +0000124 return NTTP->getDepth();
125 else
126 return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
127}
128
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000129static void AdoptTemplateParameterList(TemplateParameterList *Params,
130 DeclContext *Owner) {
David Majnemerdfecf1a2016-07-06 04:19:16 +0000131 for (NamedDecl *P : *Params) {
132 P->setDeclContext(Owner);
133
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000134 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000135 AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner);
136 }
137}
138
Richard Smithe7bd6de2015-06-10 20:30:23 +0000139namespace clang {
Eugene Zelenko421e8902017-11-29 22:39:22 +0000140
Richard Smithe7bd6de2015-06-10 20:30:23 +0000141void *allocateDefaultArgStorageChain(const ASTContext &C) {
142 return new (C) char[sizeof(void*) * 2];
143}
Eugene Zelenko421e8902017-11-29 22:39:22 +0000144
145} // namespace clang
Richard Smithe7bd6de2015-06-10 20:30:23 +0000146
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000147//===----------------------------------------------------------------------===//
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000148// RedeclarableTemplateDecl Implementation
149//===----------------------------------------------------------------------===//
150
Richard Trieub3e902f2018-12-29 02:02:30 +0000151void RedeclarableTemplateDecl::anchor() {}
152
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000153RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000154 if (Common)
155 return Common;
156
157 // Walk the previous-declaration chain until we either find a declaration
158 // with a common pointer or we run out of previous declarations.
159 SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
160 for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
161 Prev = Prev->getPreviousDecl()) {
162 if (Prev->Common) {
163 Common = Prev->Common;
164 break;
Douglas Gregor68444de2012-01-14 15:13:49 +0000165 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000166
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000167 PrevDecls.push_back(Prev);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000168 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000169
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000170 // If we never found a common pointer, allocate one now.
171 if (!Common) {
172 // FIXME: If any of the declarations is from an AST file, we probably
173 // need an update record to add the common data.
174
175 Common = newCommon(getASTContext());
176 }
177
178 // Update any previous declarations we saw with the common pointer.
David Majnemerdfecf1a2016-07-06 04:19:16 +0000179 for (const RedeclarableTemplateDecl *Prev : PrevDecls)
180 Prev->Common = Common;
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000181
Douglas Gregor68444de2012-01-14 15:13:49 +0000182 return Common;
Peter Collingbourne029fd692010-07-29 16:12:09 +0000183}
184
Vassil Vassilev61f64292017-12-14 23:30:18 +0000185void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
186 // Grab the most recent declaration to ensure we've loaded any lazy
187 // redeclarations of this template.
188 CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
189 if (CommonBasePtr->LazySpecializations) {
190 ASTContext &Context = getASTContext();
191 uint32_t *Specs = CommonBasePtr->LazySpecializations;
192 CommonBasePtr->LazySpecializations = nullptr;
193 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
194 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
195 }
196}
197
Richard Smithe977e512015-02-24 01:23:23 +0000198template<class EntryType>
199typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000200RedeclarableTemplateDecl::findSpecializationImpl(
Richard Smithe977e512015-02-24 01:23:23 +0000201 llvm::FoldingSetVector<EntryType> &Specs, ArrayRef<TemplateArgument> Args,
202 void *&InsertPos) {
Eugene Zelenko421e8902017-11-29 22:39:22 +0000203 using SETraits = SpecEntryTraits<EntryType>;
204
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000205 llvm::FoldingSetNodeID ID;
Vassil Vassilev61f64292017-12-14 23:30:18 +0000206 EntryType::Profile(ID, Args, getASTContext());
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000207 EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
Richard Smithe977e512015-02-24 01:23:23 +0000208 return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr;
209}
210
211template<class Derived, class EntryType>
212void RedeclarableTemplateDecl::addSpecializationImpl(
213 llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
214 void *InsertPos) {
Eugene Zelenko421e8902017-11-29 22:39:22 +0000215 using SETraits = SpecEntryTraits<EntryType>;
216
Richard Smithe977e512015-02-24 01:23:23 +0000217 if (InsertPos) {
218#ifndef NDEBUG
219 void *CorrectInsertPos;
220 assert(!findSpecializationImpl(Specializations,
221 SETraits::getTemplateArgs(Entry),
222 CorrectInsertPos) &&
223 InsertPos == CorrectInsertPos &&
224 "given incorrect InsertPos for specialization");
225#endif
226 Specializations.InsertNode(Entry, InsertPos);
227 } else {
228 EntryType *Existing = Specializations.GetOrInsertNode(Entry);
229 (void)Existing;
230 assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
231 "non-canonical specialization?");
232 }
233
234 if (ASTMutationListener *L = getASTMutationListener())
235 L->AddedCXXTemplateSpecialization(cast<Derived>(this),
236 SETraits::getDecl(Entry));
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000237}
238
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000239//===----------------------------------------------------------------------===//
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000240// FunctionTemplateDecl Implementation
241//===----------------------------------------------------------------------===//
242
243FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
244 DeclContext *DC,
245 SourceLocation L,
246 DeclarationName Name,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000247 TemplateParameterList *Params,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000248 NamedDecl *Decl) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000249 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Richard Smith053f6c62014-05-16 23:01:30 +0000250 return new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000251}
252
Douglas Gregor72172e92012-01-05 21:55:30 +0000253FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
254 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000255 return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000256 DeclarationName(), nullptr, nullptr);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000257}
258
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000259RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000260FunctionTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000261 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +0000262 C.addDestruction(CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000263 return CommonPtr;
264}
265
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000266void FunctionTemplateDecl::LoadLazySpecializations() const {
Vassil Vassilev61f64292017-12-14 23:30:18 +0000267 loadLazySpecializationsImpl();
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000268}
269
270llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
271FunctionTemplateDecl::getSpecializations() const {
272 LoadLazySpecializations();
273 return getCommonPtr()->Specializations;
274}
275
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000276FunctionDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000277FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
278 void *&InsertPos) {
279 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000280}
281
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000282void FunctionTemplateDecl::addSpecialization(
283 FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +0000284 addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info,
285 InsertPos);
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000286}
287
Richard Smith841d8b22013-05-17 03:04:50 +0000288ArrayRef<TemplateArgument> FunctionTemplateDecl::getInjectedTemplateArgs() {
Douglas Gregor43669f82011-03-05 17:54:25 +0000289 TemplateParameterList *Params = getTemplateParameters();
290 Common *CommonPtr = getCommonPtr();
291 if (!CommonPtr->InjectedArgs) {
Richard Smith43e14d22016-12-23 02:10:11 +0000292 auto &Context = getASTContext();
293 SmallVector<TemplateArgument, 16> TemplateArgs;
294 Context.getInjectedTemplateArgs(Params, TemplateArgs);
295 CommonPtr->InjectedArgs =
296 new (Context) TemplateArgument[TemplateArgs.size()];
297 std::copy(TemplateArgs.begin(), TemplateArgs.end(),
298 CommonPtr->InjectedArgs);
Douglas Gregor43669f82011-03-05 17:54:25 +0000299 }
Richard Smith841d8b22013-05-17 03:04:50 +0000300
301 return llvm::makeArrayRef(CommonPtr->InjectedArgs, Params->size());
Douglas Gregor43669f82011-03-05 17:54:25 +0000302}
303
Erik Pilkingtond1a184f2018-10-10 17:17:51 +0000304void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
305 using Base = RedeclarableTemplateDecl;
306
307 // If we haven't created a common pointer yet, then it can just be created
308 // with the usual method.
309 if (!Base::Common)
310 return;
311
312 Common *ThisCommon = static_cast<Common *>(Base::Common);
313 Common *PrevCommon = nullptr;
314 SmallVector<FunctionTemplateDecl *, 8> PreviousDecls;
315 for (; Prev; Prev = Prev->getPreviousDecl()) {
316 if (Prev->Base::Common) {
317 PrevCommon = static_cast<Common *>(Prev->Base::Common);
318 break;
319 }
320 PreviousDecls.push_back(Prev);
321 }
322
323 // If the previous redecl chain hasn't created a common pointer yet, then just
324 // use this common pointer.
325 if (!PrevCommon) {
326 for (auto *D : PreviousDecls)
327 D->Base::Common = ThisCommon;
328 return;
329 }
330
331 // Ensure we don't leak any important state.
332 assert(ThisCommon->Specializations.size() == 0 &&
Erik Pilkingtond1a184f2018-10-10 17:17:51 +0000333 "Can't merge incompatible declarations!");
334
335 Base::Common = PrevCommon;
336}
337
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000338//===----------------------------------------------------------------------===//
339// ClassTemplateDecl Implementation
340//===----------------------------------------------------------------------===//
341
342ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
343 DeclContext *DC,
344 SourceLocation L,
345 DeclarationName Name,
346 TemplateParameterList *Params,
Hubert Tong5a8ec4e2017-02-10 02:46:19 +0000347 NamedDecl *Decl,
348 Expr *AssociatedConstraints) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000349 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Hubert Tong5a8ec4e2017-02-10 02:46:19 +0000350
351 if (!AssociatedConstraints) {
352 return new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl);
353 }
354
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000355 auto *const CTDI = new (C) ConstrainedTemplateDeclInfo;
356 auto *const New =
Hubert Tong5a8ec4e2017-02-10 02:46:19 +0000357 new (C, DC) ClassTemplateDecl(CTDI, C, DC, L, Name, Params, Decl);
358 New->setAssociatedConstraints(AssociatedConstraints);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000359 return New;
Douglas Gregor90a1a652009-03-19 17:26:29 +0000360}
361
Richard Smith053f6c62014-05-16 23:01:30 +0000362ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000363 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000364 return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
365 DeclarationName(), nullptr, nullptr);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000366}
367
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000368void ClassTemplateDecl::LoadLazySpecializations() const {
Vassil Vassilev61f64292017-12-14 23:30:18 +0000369 loadLazySpecializationsImpl();
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000370}
371
Chandler Carruthb41171b2012-05-03 23:49:05 +0000372llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000373ClassTemplateDecl::getSpecializations() const {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000374 LoadLazySpecializations();
375 return getCommonPtr()->Specializations;
Fangrui Song6907ce22018-07-30 19:24:48 +0000376}
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000377
Chandler Carruthb41171b2012-05-03 23:49:05 +0000378llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000379ClassTemplateDecl::getPartialSpecializations() {
380 LoadLazySpecializations();
381 return getCommonPtr()->PartialSpecializations;
Fangrui Song6907ce22018-07-30 19:24:48 +0000382}
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000383
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000384RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000385ClassTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000386 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +0000387 C.addDestruction(CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000388 return CommonPtr;
389}
390
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000391ClassTemplateSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000392ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
393 void *&InsertPos) {
394 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000395}
396
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000397void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
398 void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +0000399 addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos);
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000400}
401
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000402ClassTemplatePartialSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000403ClassTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000404 void *&InsertPos) {
Craig Topper7e0daca2014-06-26 04:58:53 +0000405 return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000406}
407
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000408void ClassTemplateDecl::AddPartialSpecialization(
409 ClassTemplatePartialSpecializationDecl *D,
410 void *InsertPos) {
Douglas Gregorce9978f2012-03-28 14:34:23 +0000411 if (InsertPos)
412 getPartialSpecializations().InsertNode(D, InsertPos);
413 else {
414 ClassTemplatePartialSpecializationDecl *Existing
415 = getPartialSpecializations().GetOrInsertNode(D);
416 (void)Existing;
417 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
418 }
419
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000420 if (ASTMutationListener *L = getASTMutationListener())
421 L->AddedCXXTemplateSpecialization(this, D);
422}
423
Douglas Gregor407e9612010-04-30 05:56:50 +0000424void ClassTemplateDecl::getPartialSpecializations(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000425 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
Chandler Carruthb41171b2012-05-03 23:49:05 +0000426 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000427 = getPartialSpecializations();
Douglas Gregor407e9612010-04-30 05:56:50 +0000428 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +0000429 PS.reserve(PartialSpecs.size());
David Majnemerdfecf1a2016-07-06 04:19:16 +0000430 for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs)
431 PS.push_back(P.getMostRecentDecl());
Douglas Gregor407e9612010-04-30 05:56:50 +0000432}
433
Douglas Gregor15301382009-07-30 17:40:51 +0000434ClassTemplatePartialSpecializationDecl *
435ClassTemplateDecl::findPartialSpecialization(QualType T) {
436 ASTContext &Context = getASTContext();
David Majnemerdfecf1a2016-07-06 04:19:16 +0000437 for (ClassTemplatePartialSpecializationDecl &P :
438 getPartialSpecializations()) {
439 if (Context.hasSameType(P.getInjectedSpecializationType(), T))
440 return P.getMostRecentDecl();
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000441 }
442
Craig Topper36250ad2014-05-12 05:36:57 +0000443 return nullptr;
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000444}
445
446ClassTemplatePartialSpecializationDecl *
447ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
448 ClassTemplatePartialSpecializationDecl *D) {
449 Decl *DCanon = D->getCanonicalDecl();
David Majnemerdfecf1a2016-07-06 04:19:16 +0000450 for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
451 if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
452 return P.getMostRecentDecl();
Douglas Gregor15301382009-07-30 17:40:51 +0000453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Craig Topper36250ad2014-05-12 05:36:57 +0000455 return nullptr;
Douglas Gregor15301382009-07-30 17:40:51 +0000456}
457
John McCalle78aac42010-03-10 03:28:59 +0000458QualType
Douglas Gregor9961ce92010-07-08 18:37:38 +0000459ClassTemplateDecl::getInjectedClassNameSpecialization() {
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000460 Common *CommonPtr = getCommonPtr();
Douglas Gregore362cea2009-05-10 22:57:19 +0000461 if (!CommonPtr->InjectedClassNameType.isNull())
462 return CommonPtr->InjectedClassNameType;
463
Douglas Gregor8092e802010-12-23 16:00:30 +0000464 // C++0x [temp.dep.type]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +0000465 // The template argument list of a primary template is a template argument
Douglas Gregor8092e802010-12-23 16:00:30 +0000466 // list in which the nth template argument has the value of the nth template
Fangrui Song6907ce22018-07-30 19:24:48 +0000467 // parameter of the class template. If the nth template parameter is a
468 // template parameter pack (14.5.3), the nth template argument is a pack
469 // expansion (14.5.3) whose pattern is the name of the template parameter
Douglas Gregor8092e802010-12-23 16:00:30 +0000470 // pack.
Douglas Gregor9961ce92010-07-08 18:37:38 +0000471 ASTContext &Context = getASTContext();
Douglas Gregore362cea2009-05-10 22:57:19 +0000472 TemplateParameterList *Params = getTemplateParameters();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000473 SmallVector<TemplateArgument, 16> TemplateArgs;
Richard Smith43e14d22016-12-23 02:10:11 +0000474 Context.getInjectedTemplateArgs(Params, TemplateArgs);
Douglas Gregore362cea2009-05-10 22:57:19 +0000475 CommonPtr->InjectedClassNameType
Douglas Gregora8e02e72009-07-28 23:00:59 +0000476 = Context.getTemplateSpecializationType(TemplateName(this),
David Majnemer6fbeee32016-07-07 04:43:07 +0000477 TemplateArgs);
Douglas Gregore362cea2009-05-10 22:57:19 +0000478 return CommonPtr->InjectedClassNameType;
479}
480
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000481//===----------------------------------------------------------------------===//
482// TemplateTypeParm Allocation/Deallocation Method Implementations
483//===----------------------------------------------------------------------===//
484
485TemplateTypeParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000486TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000487 SourceLocation KeyLoc, SourceLocation NameLoc,
488 unsigned D, unsigned P, IdentifierInfo *Id,
489 bool Typename, bool ParameterPack) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000490 auto *TTPDecl =
491 new (C, DC) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
Chandler Carruth08836322011-05-01 00:51:33 +0000492 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
Richard Smith5b21db82014-04-23 18:20:42 +0000493 TTPDecl->setTypeForDecl(TTPType.getTypePtr());
Chandler Carruth08836322011-05-01 00:51:33 +0000494 return TTPDecl;
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000495}
496
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000497TemplateTypeParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000498TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000499 return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(),
500 SourceLocation(), nullptr, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000501}
502
John McCall0ad16662009-10-29 08:12:44 +0000503SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara23485e02011-03-04 12:42:03 +0000504 return hasDefaultArgument()
Richard Smith1469b912015-06-10 00:29:03 +0000505 ? getDefaultArgumentInfo()->getTypeLoc().getBeginLoc()
506 : SourceLocation();
Abramo Bagnara23485e02011-03-04 12:42:03 +0000507}
508
509SourceRange TemplateTypeParmDecl::getSourceRange() const {
510 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000511 return SourceRange(getBeginLoc(),
Richard Smith1469b912015-06-10 00:29:03 +0000512 getDefaultArgumentInfo()->getTypeLoc().getEndLoc());
Kadir Cetinkayafd019ed2019-10-01 14:08:51 +0000513 // TypeDecl::getSourceRange returns a range containing name location, which is
514 // wrong for unnamed template parameters. e.g:
515 // it will return <[[typename>]] instead of <[[typename]]>
516 else if (getDeclName().isEmpty())
517 return SourceRange(getBeginLoc());
518 return TypeDecl::getSourceRange();
John McCall0ad16662009-10-29 08:12:44 +0000519}
520
Douglas Gregor21610382009-10-29 00:04:11 +0000521unsigned TemplateTypeParmDecl::getDepth() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000522 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getDepth();
Douglas Gregor21610382009-10-29 00:04:11 +0000523}
524
525unsigned TemplateTypeParmDecl::getIndex() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000526 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getIndex();
Douglas Gregor21610382009-10-29 00:04:11 +0000527}
528
Chandler Carruth08836322011-05-01 00:51:33 +0000529bool TemplateTypeParmDecl::isParameterPack() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000530 return getTypeForDecl()->getAs<TemplateTypeParmType>()->isParameterPack();
Chandler Carruth08836322011-05-01 00:51:33 +0000531}
532
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000533//===----------------------------------------------------------------------===//
534// NonTypeTemplateParmDecl Method Implementations
535//===----------------------------------------------------------------------===//
536
David Majnemerdfecf1a2016-07-06 04:19:16 +0000537NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(
538 DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D,
539 unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
540 ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos)
541 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
542 TemplateParmPosition(D, P), ParameterPack(true),
543 ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) {
544 if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) {
James Y Knight7a22b242015-08-06 20:26:32 +0000545 auto TypesAndInfos =
546 getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000547 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
James Y Knight7a22b242015-08-06 20:26:32 +0000548 new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
549 TypesAndInfos[I].second = ExpandedTInfos[I];
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000550 }
551 }
552}
553
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000554NonTypeTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000555NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000556 SourceLocation StartLoc, SourceLocation IdLoc,
557 unsigned D, unsigned P, IdentifierInfo *Id,
558 QualType T, bool ParameterPack,
559 TypeSourceInfo *TInfo) {
Richard Smithf7981722013-11-22 09:01:48 +0000560 return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
561 T, ParameterPack, TInfo);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000562}
563
David Majnemerdfecf1a2016-07-06 04:19:16 +0000564NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
565 const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
566 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
567 QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
568 ArrayRef<TypeSourceInfo *> ExpandedTInfos) {
James Y Knight7a22b242015-08-06 20:26:32 +0000569 return new (C, DC,
570 additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
David Majnemerdfecf1a2016-07-06 04:19:16 +0000571 ExpandedTypes.size()))
James Y Knight7a22b242015-08-06 20:26:32 +0000572 NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
David Majnemerdfecf1a2016-07-06 04:19:16 +0000573 ExpandedTypes, ExpandedTInfos);
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000574}
575
Douglas Gregor72172e92012-01-05 21:55:30 +0000576NonTypeTemplateParmDecl *
577NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000578 return new (C, ID) NonTypeTemplateParmDecl(nullptr, SourceLocation(),
579 SourceLocation(), 0, 0, nullptr,
580 QualType(), false, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000581}
582
583NonTypeTemplateParmDecl *
584NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
585 unsigned NumExpandedTypes) {
David Majnemerdfecf1a2016-07-06 04:19:16 +0000586 auto *NTTP =
587 new (C, ID, additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
588 NumExpandedTypes))
589 NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
590 0, 0, nullptr, QualType(), nullptr, None,
591 None);
592 NTTP->NumExpandedTypes = NumExpandedTypes;
593 return NTTP;
Douglas Gregor72172e92012-01-05 21:55:30 +0000594}
595
John McCallf4cd4f92011-02-09 01:13:10 +0000596SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnarae15d5532011-03-04 11:03:48 +0000597 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraea947882011-03-08 16:41:52 +0000598 return SourceRange(getOuterLocStart(),
599 getDefaultArgument()->getSourceRange().getEnd());
600 return DeclaratorDecl::getSourceRange();
John McCallf4cd4f92011-02-09 01:13:10 +0000601}
602
Douglas Gregordba32632009-02-10 19:49:53 +0000603SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara656e3002010-06-09 09:26:05 +0000604 return hasDefaultArgument()
605 ? getDefaultArgument()->getSourceRange().getBegin()
606 : SourceLocation();
Douglas Gregordba32632009-02-10 19:49:53 +0000607}
608
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000609//===----------------------------------------------------------------------===//
610// TemplateTemplateParmDecl Method Implementations
611//===----------------------------------------------------------------------===//
612
Eugene Zelenko421e8902017-11-29 22:39:22 +0000613void TemplateTemplateParmDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000614
Richard Smith1fde8ec2012-09-07 02:06:42 +0000615TemplateTemplateParmDecl::TemplateTemplateParmDecl(
616 DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
617 IdentifierInfo *Id, TemplateParameterList *Params,
David Majnemerdfecf1a2016-07-06 04:19:16 +0000618 ArrayRef<TemplateParameterList *> Expansions)
619 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
620 TemplateParmPosition(D, P), ParameterPack(true),
621 ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) {
622 if (!Expansions.empty())
623 std::uninitialized_copy(Expansions.begin(), Expansions.end(),
James Y Knight7a22b242015-08-06 20:26:32 +0000624 getTrailingObjects<TemplateParameterList *>());
Richard Smith1fde8ec2012-09-07 02:06:42 +0000625}
626
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000627TemplateTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000628TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000629 SourceLocation L, unsigned D, unsigned P,
Douglas Gregorf5500772011-01-05 15:48:55 +0000630 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000631 TemplateParameterList *Params) {
Richard Smithf7981722013-11-22 09:01:48 +0000632 return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
633 Params);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000634}
635
Douglas Gregor72172e92012-01-05 21:55:30 +0000636TemplateTemplateParmDecl *
Richard Smith1fde8ec2012-09-07 02:06:42 +0000637TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
638 SourceLocation L, unsigned D, unsigned P,
639 IdentifierInfo *Id,
640 TemplateParameterList *Params,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000641 ArrayRef<TemplateParameterList *> Expansions) {
James Y Knight7a22b242015-08-06 20:26:32 +0000642 return new (C, DC,
643 additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
David Majnemerdfecf1a2016-07-06 04:19:16 +0000644 TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions);
Richard Smith1fde8ec2012-09-07 02:06:42 +0000645}
646
647TemplateTemplateParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000648TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000649 return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
650 false, nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000651}
652
Richard Smith1fde8ec2012-09-07 02:06:42 +0000653TemplateTemplateParmDecl *
654TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
655 unsigned NumExpansions) {
David Majnemerdfecf1a2016-07-06 04:19:16 +0000656 auto *TTP =
657 new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
658 TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
659 nullptr, None);
660 TTP->NumExpandedParams = NumExpansions;
661 return TTP;
Richard Smith1fde8ec2012-09-07 02:06:42 +0000662}
663
Richard Smith35c1df52015-06-17 20:16:32 +0000664SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
665 return hasDefaultArgument() ? getDefaultArgument().getLocation()
666 : SourceLocation();
667}
668
Richard Smith1469b912015-06-10 00:29:03 +0000669void TemplateTemplateParmDecl::setDefaultArgument(
670 const ASTContext &C, const TemplateArgumentLoc &DefArg) {
671 if (DefArg.getArgument().isNull())
672 DefaultArgument.set(nullptr);
673 else
674 DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
675}
676
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000677//===----------------------------------------------------------------------===//
Douglas Gregord002c7b2009-05-11 23:53:27 +0000678// TemplateArgumentList Implementation
679//===----------------------------------------------------------------------===//
David Majnemer8b622692016-07-03 21:17:51 +0000680TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
681 : Arguments(getTrailingObjects<TemplateArgument>()),
682 NumArguments(Args.size()) {
683 std::uninitialized_copy(Args.begin(), Args.end(),
James Y Knight7a22b242015-08-06 20:26:32 +0000684 getTrailingObjects<TemplateArgument>());
685}
686
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000687TemplateArgumentList *
688TemplateArgumentList::CreateCopy(ASTContext &Context,
David Majnemer8b622692016-07-03 21:17:51 +0000689 ArrayRef<TemplateArgument> Args) {
690 void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
691 return new (Mem) TemplateArgumentList(Args);
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000692}
693
Richard Smithf19a8b02019-05-02 00:49:14 +0000694FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
695 ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
696 TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs,
697 const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI,
698 MemberSpecializationInfo *MSInfo) {
Craig Topper36250ad2014-05-12 05:36:57 +0000699 const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000700 if (TemplateArgsAsWritten)
701 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
702 *TemplateArgsAsWritten);
703
Richard Smithf19a8b02019-05-02 00:49:14 +0000704 void *Mem =
705 C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 1 : 0));
706 return new (Mem) FunctionTemplateSpecializationInfo(
707 FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000708}
709
Douglas Gregord002c7b2009-05-11 23:53:27 +0000710//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000711// TemplateDecl Implementation
712//===----------------------------------------------------------------------===//
713
Eugene Zelenko421e8902017-11-29 22:39:22 +0000714void TemplateDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000715
716//===----------------------------------------------------------------------===//
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000717// ClassTemplateSpecializationDecl Implementation
718//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +0000719
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000720ClassTemplateSpecializationDecl::
Douglas Gregore9029562010-05-06 00:28:52 +0000721ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000722 DeclContext *DC, SourceLocation StartLoc,
723 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000724 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000725 ArrayRef<TemplateArgument> Args,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000726 ClassTemplateSpecializationDecl *PrevDecl)
Eugene Zelenko421e8902017-11-29 22:39:22 +0000727 : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
728 SpecializedTemplate->getIdentifier(), PrevDecl),
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000729 SpecializedTemplate(SpecializedTemplate),
David Majnemer8b622692016-07-03 21:17:51 +0000730 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
Douglas Gregord002c7b2009-05-11 23:53:27 +0000731 SpecializationKind(TSK_Undeclared) {
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000732}
Mike Stump11289f42009-09-09 15:08:12 +0000733
Richard Smith053f6c62014-05-16 23:01:30 +0000734ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
735 Kind DK)
736 : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
737 SourceLocation(), nullptr, nullptr),
Eugene Zelenko421e8902017-11-29 22:39:22 +0000738 SpecializationKind(TSK_Undeclared) {}
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000739
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000740ClassTemplateSpecializationDecl *
Douglas Gregore9029562010-05-06 00:28:52 +0000741ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000742 DeclContext *DC,
743 SourceLocation StartLoc,
744 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000745 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000746 ArrayRef<TemplateArgument> Args,
Douglas Gregor67a65642009-02-17 23:15:12 +0000747 ClassTemplateSpecializationDecl *PrevDecl) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000748 auto *Result =
Richard Smithf7981722013-11-22 09:01:48 +0000749 new (Context, DC) ClassTemplateSpecializationDecl(
750 Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
David Majnemer8b622692016-07-03 21:17:51 +0000751 SpecializedTemplate, Args, PrevDecl);
Erich Keanef92f31c2018-08-01 20:48:16 +0000752 Result->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000753
Douglas Gregor67a65642009-02-17 23:15:12 +0000754 Context.getTypeDeclType(Result, PrevDecl);
755 return Result;
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000756}
Douglas Gregor2373c592009-05-31 09:31:02 +0000757
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000758ClassTemplateSpecializationDecl *
Richard Smithf7981722013-11-22 09:01:48 +0000759ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000760 unsigned ID) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000761 auto *Result =
Richard Smith053f6c62014-05-16 23:01:30 +0000762 new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
Erich Keanef92f31c2018-08-01 20:48:16 +0000763 Result->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000764 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000765}
766
Benjamin Kramer9170e912013-02-22 15:46:01 +0000767void ClassTemplateSpecializationDecl::getNameForDiagnostic(
768 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
769 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +0000770
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000771 const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this);
Richard Smith792c22d2016-12-24 04:09:05 +0000772 if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
773 PS ? PS->getTemplateArgsAsWritten() : nullptr) {
Serge Pavlov03e672c2017-11-28 16:14:14 +0000774 printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +0000775 } else {
776 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
Serge Pavlov03e672c2017-11-28 16:14:14 +0000777 printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +0000778 }
Douglas Gregorb11aad82011-02-19 18:51:44 +0000779}
780
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000781ClassTemplateDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000782ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000783 if (const auto *PartialSpec =
784 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000785 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
786 return SpecializedTemplate.get<ClassTemplateDecl*>();
787}
788
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000789SourceRange
790ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000791 if (ExplicitInfo) {
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000792 SourceLocation Begin = getTemplateKeywordLoc();
793 if (Begin.isValid()) {
794 // Here we have an explicit (partial) specialization or instantiation.
795 assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
796 getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
797 getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
798 if (getExternLoc().isValid())
799 Begin = getExternLoc();
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +0000800 SourceLocation End = getBraceRange().getEnd();
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000801 if (End.isInvalid())
802 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
803 return SourceRange(Begin, End);
804 }
805 // An implicit instantiation of a class template partial specialization
806 // uses ExplicitInfo to record the TypeAsWritten, but the source
807 // locations should be retrieved from the instantiation pattern.
Eugene Zelenko421e8902017-11-29 22:39:22 +0000808 using CTPSDecl = ClassTemplatePartialSpecializationDecl;
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000809 auto *ctpsd = const_cast<CTPSDecl *>(cast<CTPSDecl>(this));
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000810 CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
Craig Topper36250ad2014-05-12 05:36:57 +0000811 assert(inst_from != nullptr);
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000812 return inst_from->getSourceRange();
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000813 }
814 else {
815 // No explicit info available.
816 llvm::PointerUnion<ClassTemplateDecl *,
817 ClassTemplatePartialSpecializationDecl *>
818 inst_from = getInstantiatedFrom();
819 if (inst_from.isNull())
820 return getSpecializedTemplate()->getSourceRange();
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000821 if (const auto *ctd = inst_from.dyn_cast<ClassTemplateDecl *>())
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000822 return ctd->getSourceRange();
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000823 return inst_from.get<ClassTemplatePartialSpecializationDecl *>()
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000824 ->getSourceRange();
825 }
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000826}
827
Douglas Gregor2373c592009-05-31 09:31:02 +0000828//===----------------------------------------------------------------------===//
Saar Razd7aae332019-07-10 21:25:49 +0000829// ConceptDecl Implementation
830//===----------------------------------------------------------------------===//
831ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC,
832 SourceLocation L, DeclarationName Name,
833 TemplateParameterList *Params,
834 Expr *ConstraintExpr) {
835 AdoptTemplateParameterList(Params, DC);
836 return new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr);
837}
838
839ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C,
840 unsigned ID) {
841 ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(),
842 DeclarationName(),
843 nullptr, nullptr);
844
845 return Result;
846}
847
848//===----------------------------------------------------------------------===//
Douglas Gregor2373c592009-05-31 09:31:02 +0000849// ClassTemplatePartialSpecializationDecl Implementation
850//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +0000851void ClassTemplatePartialSpecializationDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000852
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000853ClassTemplatePartialSpecializationDecl::
854ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000855 DeclContext *DC,
856 SourceLocation StartLoc,
857 SourceLocation IdLoc,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000858 TemplateParameterList *Params,
859 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000860 ArrayRef<TemplateArgument> Args,
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000861 const ASTTemplateArgumentListInfo *ArgInfos,
Richard Smithb2f61b42013-08-22 23:27:37 +0000862 ClassTemplatePartialSpecializationDecl *PrevDecl)
Eugene Zelenko421e8902017-11-29 22:39:22 +0000863 : ClassTemplateSpecializationDecl(Context,
864 ClassTemplatePartialSpecialization,
865 TK, DC, StartLoc, IdLoc,
866 SpecializedTemplate, Args, PrevDecl),
867 TemplateParams(Params), ArgsAsWritten(ArgInfos),
868 InstantiatedFromMember(nullptr, false) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000869 AdoptTemplateParameterList(Params, this);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000870}
871
Douglas Gregor2373c592009-05-31 09:31:02 +0000872ClassTemplatePartialSpecializationDecl *
873ClassTemplatePartialSpecializationDecl::
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000874Create(ASTContext &Context, TagKind TK,DeclContext *DC,
875 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregor2373c592009-05-31 09:31:02 +0000876 TemplateParameterList *Params,
877 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000878 ArrayRef<TemplateArgument> Args,
John McCall6b51f282009-11-23 01:53:49 +0000879 const TemplateArgumentListInfo &ArgInfos,
John McCalle78aac42010-03-10 03:28:59 +0000880 QualType CanonInjectedType,
Richard Smithb2f61b42013-08-22 23:27:37 +0000881 ClassTemplatePartialSpecializationDecl *PrevDecl) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000882 const ASTTemplateArgumentListInfo *ASTArgInfos =
883 ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
John McCall0ad16662009-10-29 08:12:44 +0000884
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000885 auto *Result = new (Context, DC)
Richard Smithf7981722013-11-22 09:01:48 +0000886 ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
887 Params, SpecializedTemplate, Args,
David Majnemer8b622692016-07-03 21:17:51 +0000888 ASTArgInfos, PrevDecl);
Douglas Gregor2373c592009-05-31 09:31:02 +0000889 Result->setSpecializationKind(TSK_ExplicitSpecialization);
Erich Keanef92f31c2018-08-01 20:48:16 +0000890 Result->setMayHaveOutOfDateDef(false);
John McCalle78aac42010-03-10 03:28:59 +0000891
892 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregor2373c592009-05-31 09:31:02 +0000893 return Result;
894}
John McCall11083da2009-09-16 22:47:08 +0000895
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000896ClassTemplatePartialSpecializationDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000897ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
898 unsigned ID) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000899 auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C);
Erich Keanef92f31c2018-08-01 20:48:16 +0000900 Result->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000901 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000902}
903
John McCall11083da2009-09-16 22:47:08 +0000904//===----------------------------------------------------------------------===//
905// FriendTemplateDecl Implementation
906//===----------------------------------------------------------------------===//
907
Eugene Zelenko421e8902017-11-29 22:39:22 +0000908void FriendTemplateDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000909
David Majnemerdfecf1a2016-07-06 04:19:16 +0000910FriendTemplateDecl *
911FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
912 SourceLocation L,
913 MutableArrayRef<TemplateParameterList *> Params,
914 FriendUnion Friend, SourceLocation FLoc) {
915 return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc);
John McCall11083da2009-09-16 22:47:08 +0000916}
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000917
Douglas Gregor72172e92012-01-05 21:55:30 +0000918FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
919 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000920 return new (C, ID) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000921}
Richard Smith3f1b5d02011-05-05 21:57:07 +0000922
923//===----------------------------------------------------------------------===//
924// TypeAliasTemplateDecl Implementation
925//===----------------------------------------------------------------------===//
926
927TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
928 DeclContext *DC,
929 SourceLocation L,
930 DeclarationName Name,
931 TemplateParameterList *Params,
932 NamedDecl *Decl) {
933 AdoptTemplateParameterList(Params, DC);
Richard Smith053f6c62014-05-16 23:01:30 +0000934 return new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000935}
936
Douglas Gregor72172e92012-01-05 21:55:30 +0000937TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
938 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000939 return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000940 DeclarationName(), nullptr, nullptr);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000941}
942
Richard Smith3f1b5d02011-05-05 21:57:07 +0000943RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000944TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000945 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +0000946 C.addDestruction(CommonPtr);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000947 return CommonPtr;
948}
949
David Blaikie68e081d2011-12-20 02:48:34 +0000950//===----------------------------------------------------------------------===//
951// ClassScopeFunctionSpecializationDecl Implementation
952//===----------------------------------------------------------------------===//
953
Eugene Zelenko421e8902017-11-29 22:39:22 +0000954void ClassScopeFunctionSpecializationDecl::anchor() {}
Douglas Gregor72172e92012-01-05 21:55:30 +0000955
956ClassScopeFunctionSpecializationDecl *
957ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
958 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000959 return new (C, ID) ClassScopeFunctionSpecializationDecl(
Richard Smithf19a8b02019-05-02 00:49:14 +0000960 nullptr, SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000961}
Larisse Voufo39a1e502013-08-06 01:03:05 +0000962
963//===----------------------------------------------------------------------===//
964// VarTemplateDecl Implementation
965//===----------------------------------------------------------------------===//
966
Larisse Voufoa11bd8a2013-08-13 02:02:26 +0000967VarTemplateDecl *VarTemplateDecl::getDefinition() {
968 VarTemplateDecl *CurD = this;
969 while (CurD) {
970 if (CurD->isThisDeclarationADefinition())
971 return CurD;
972 CurD = CurD->getPreviousDecl();
973 }
Craig Topper36250ad2014-05-12 05:36:57 +0000974 return nullptr;
Larisse Voufoa11bd8a2013-08-13 02:02:26 +0000975}
976
Larisse Voufo39a1e502013-08-06 01:03:05 +0000977VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
978 SourceLocation L, DeclarationName Name,
979 TemplateParameterList *Params,
Richard Smithbeef3452014-01-16 23:39:20 +0000980 VarDecl *Decl) {
David Blaikie07489f92019-04-19 23:04:05 +0000981 AdoptTemplateParameterList(Params, DC);
Richard Smith053f6c62014-05-16 23:01:30 +0000982 return new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000983}
984
985VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
986 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000987 return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
988 DeclarationName(), nullptr, nullptr);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000989}
990
Larisse Voufo39a1e502013-08-06 01:03:05 +0000991void VarTemplateDecl::LoadLazySpecializations() const {
Vassil Vassilev61f64292017-12-14 23:30:18 +0000992 loadLazySpecializationsImpl();
Larisse Voufo39a1e502013-08-06 01:03:05 +0000993}
994
995llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
996VarTemplateDecl::getSpecializations() const {
997 LoadLazySpecializations();
998 return getCommonPtr()->Specializations;
999}
1000
1001llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
1002VarTemplateDecl::getPartialSpecializations() {
1003 LoadLazySpecializations();
1004 return getCommonPtr()->PartialSpecializations;
1005}
1006
1007RedeclarableTemplateDecl::CommonBase *
1008VarTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001009 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +00001010 C.addDestruction(CommonPtr);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001011 return CommonPtr;
1012}
1013
1014VarTemplateSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +00001015VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
1016 void *&InsertPos) {
1017 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001018}
1019
1020void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
1021 void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +00001022 addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001023}
1024
1025VarTemplatePartialSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +00001026VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
1027 void *&InsertPos) {
1028 return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001029}
1030
1031void VarTemplateDecl::AddPartialSpecialization(
1032 VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
1033 if (InsertPos)
1034 getPartialSpecializations().InsertNode(D, InsertPos);
1035 else {
1036 VarTemplatePartialSpecializationDecl *Existing =
1037 getPartialSpecializations().GetOrInsertNode(D);
1038 (void)Existing;
1039 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
1040 }
1041
1042 if (ASTMutationListener *L = getASTMutationListener())
1043 L->AddedCXXTemplateSpecialization(this, D);
1044}
1045
1046void VarTemplateDecl::getPartialSpecializations(
1047 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) {
1048 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
1049 getPartialSpecializations();
1050 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +00001051 PS.reserve(PartialSpecs.size());
David Majnemerdfecf1a2016-07-06 04:19:16 +00001052 for (VarTemplatePartialSpecializationDecl &P : PartialSpecs)
1053 PS.push_back(P.getMostRecentDecl());
Larisse Voufo39a1e502013-08-06 01:03:05 +00001054}
1055
1056VarTemplatePartialSpecializationDecl *
1057VarTemplateDecl::findPartialSpecInstantiatedFromMember(
1058 VarTemplatePartialSpecializationDecl *D) {
1059 Decl *DCanon = D->getCanonicalDecl();
David Majnemerdfecf1a2016-07-06 04:19:16 +00001060 for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
1061 if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
1062 return P.getMostRecentDecl();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001063 }
1064
Craig Topper36250ad2014-05-12 05:36:57 +00001065 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00001066}
1067
1068//===----------------------------------------------------------------------===//
1069// VarTemplateSpecializationDecl Implementation
1070//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +00001071
Larisse Voufo39a1e502013-08-06 01:03:05 +00001072VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001073 Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001074 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
David Majnemer8b622692016-07-03 21:17:51 +00001075 TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args)
Richard Smith053f6c62014-05-16 23:01:30 +00001076 : VarDecl(DK, Context, DC, StartLoc, IdLoc,
1077 SpecializedTemplate->getIdentifier(), T, TInfo, S),
Eugene Zelenko421e8902017-11-29 22:39:22 +00001078 SpecializedTemplate(SpecializedTemplate),
David Majnemer8b622692016-07-03 21:17:51 +00001079 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
Richard Smith435e6472017-12-02 02:48:42 +00001080 SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +00001081
Richard Smith053f6c62014-05-16 23:01:30 +00001082VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
1083 ASTContext &C)
1084 : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001085 QualType(), nullptr, SC_None),
Richard Smith435e6472017-12-02 02:48:42 +00001086 SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +00001087
1088VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
1089 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1090 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
David Majnemer8b622692016-07-03 21:17:51 +00001091 TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) {
Richard Smithf7981722013-11-22 09:01:48 +00001092 return new (Context, DC) VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001093 VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
David Majnemer8b622692016-07-03 21:17:51 +00001094 SpecializedTemplate, T, TInfo, S, Args);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001095}
1096
1097VarTemplateSpecializationDecl *
1098VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001099 return new (C, ID)
1100 VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001101}
1102
1103void VarTemplateSpecializationDecl::getNameForDiagnostic(
1104 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
1105 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
1106
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001107 const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this);
Richard Smith792c22d2016-12-24 04:09:05 +00001108 if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
1109 PS ? PS->getTemplateArgsAsWritten() : nullptr) {
Serge Pavlov03e672c2017-11-28 16:14:14 +00001110 printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +00001111 } else {
1112 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
Serge Pavlov03e672c2017-11-28 16:14:14 +00001113 printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +00001114 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001115}
1116
1117VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001118 if (const auto *PartialSpec =
Larisse Voufo39a1e502013-08-06 01:03:05 +00001119 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1120 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
1121 return SpecializedTemplate.get<VarTemplateDecl *>();
1122}
1123
1124void VarTemplateSpecializationDecl::setTemplateArgsInfo(
1125 const TemplateArgumentListInfo &ArgsInfo) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001126 TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
1127 TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
David Majnemerdfecf1a2016-07-06 04:19:16 +00001128 for (const TemplateArgumentLoc &Loc : ArgsInfo.arguments())
1129 TemplateArgsInfo.addArgument(Loc);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001130}
1131
1132//===----------------------------------------------------------------------===//
1133// VarTemplatePartialSpecializationDecl Implementation
1134//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +00001135
Larisse Voufo39a1e502013-08-06 01:03:05 +00001136void VarTemplatePartialSpecializationDecl::anchor() {}
1137
1138VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
1139 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1140 SourceLocation IdLoc, TemplateParameterList *Params,
1141 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00001142 StorageClass S, ArrayRef<TemplateArgument> Args,
Richard Smithb2f61b42013-08-22 23:27:37 +00001143 const ASTTemplateArgumentListInfo *ArgInfos)
Richard Smith053f6c62014-05-16 23:01:30 +00001144 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001145 DC, StartLoc, IdLoc, SpecializedTemplate, T,
David Majnemer8b622692016-07-03 21:17:51 +00001146 TInfo, S, Args),
Larisse Voufo39a1e502013-08-06 01:03:05 +00001147 TemplateParams(Params), ArgsAsWritten(ArgInfos),
Craig Topper36250ad2014-05-12 05:36:57 +00001148 InstantiatedFromMember(nullptr, false) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001149 // TODO: The template parameters should be in DC by now. Verify.
1150 // AdoptTemplateParameterList(Params, DC);
1151}
1152
1153VarTemplatePartialSpecializationDecl *
1154VarTemplatePartialSpecializationDecl::Create(
1155 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1156 SourceLocation IdLoc, TemplateParameterList *Params,
1157 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00001158 StorageClass S, ArrayRef<TemplateArgument> Args,
Richard Smithb2f61b42013-08-22 23:27:37 +00001159 const TemplateArgumentListInfo &ArgInfos) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00001160 const ASTTemplateArgumentListInfo *ASTArgInfos
1161 = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001162
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001163 auto *Result =
Richard Smithf7981722013-11-22 09:01:48 +00001164 new (Context, DC) VarTemplatePartialSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00001165 Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00001166 S, Args, ASTArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001167 Result->setSpecializationKind(TSK_ExplicitSpecialization);
1168 return Result;
1169}
1170
1171VarTemplatePartialSpecializationDecl *
1172VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
1173 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001174 return new (C, ID) VarTemplatePartialSpecializationDecl(C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001175}
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001176
1177static TemplateParameterList *
1178createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
1179 // typename T
1180 auto *T = TemplateTypeParmDecl::Create(
1181 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
1182 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
1183 T->setImplicit(true);
1184
1185 // T ...Ints
1186 TypeSourceInfo *TI =
1187 C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
1188 auto *N = NonTypeTemplateParmDecl::Create(
1189 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1190 /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
1191 N->setImplicit(true);
1192
1193 // <typename T, T ...Ints>
1194 NamedDecl *P[2] = {T, N};
1195 auto *TPL = TemplateParameterList::Create(
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001196 C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001197
1198 // template <typename T, ...Ints> class IntSeq
1199 auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
1200 C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
1201 /*ParameterPack=*/false, /*Id=*/nullptr, TPL);
1202 TemplateTemplateParm->setImplicit(true);
1203
1204 // typename T
1205 auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
1206 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1207 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
1208 TemplateTypeParm->setImplicit(true);
1209
1210 // T N
1211 TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
1212 QualType(TemplateTypeParm->getTypeForDecl(), 0));
1213 auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
1214 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
1215 /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1216 NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
1217 NonTypeTemplateParm};
1218
1219 // template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
1220 return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001221 Params, SourceLocation(), nullptr);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001222}
1223
Eric Fiselier6ad68552016-07-01 01:24:09 +00001224static TemplateParameterList *
1225createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
1226 // std::size_t Index
1227 TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
1228 auto *Index = NonTypeTemplateParmDecl::Create(
1229 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
1230 /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1231
1232 // typename ...T
1233 auto *Ts = TemplateTypeParmDecl::Create(
1234 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1235 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true);
1236 Ts->setImplicit(true);
1237
1238 // template <std::size_t Index, typename ...T>
1239 NamedDecl *Params[] = {Index, Ts};
1240 return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1241 llvm::makeArrayRef(Params),
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001242 SourceLocation(), nullptr);
Eric Fiselier6ad68552016-07-01 01:24:09 +00001243}
1244
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001245static TemplateParameterList *createBuiltinTemplateParameterList(
1246 const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
1247 switch (BTK) {
1248 case BTK__make_integer_seq:
1249 return createMakeIntegerSeqParameterList(C, DC);
Eric Fiselier6ad68552016-07-01 01:24:09 +00001250 case BTK__type_pack_element:
1251 return createTypePackElementParameterList(C, DC);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001252 }
1253
1254 llvm_unreachable("unhandled BuiltinTemplateKind!");
1255}
1256
1257void BuiltinTemplateDecl::anchor() {}
1258
1259BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
1260 DeclarationName Name,
1261 BuiltinTemplateKind BTK)
1262 : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name,
1263 createBuiltinTemplateParameterList(C, DC, BTK)),
1264 BTK(BTK) {}