blob: 40c39c845db6394e635cc01314a23755835e9cc7 [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());
Abramo Bagnara23485e02011-03-04 12:42:03 +0000513 else
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000514 return TypeDecl::getSourceRange();
John McCall0ad16662009-10-29 08:12:44 +0000515}
516
Douglas Gregor21610382009-10-29 00:04:11 +0000517unsigned TemplateTypeParmDecl::getDepth() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000518 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getDepth();
Douglas Gregor21610382009-10-29 00:04:11 +0000519}
520
521unsigned TemplateTypeParmDecl::getIndex() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000522 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getIndex();
Douglas Gregor21610382009-10-29 00:04:11 +0000523}
524
Chandler Carruth08836322011-05-01 00:51:33 +0000525bool TemplateTypeParmDecl::isParameterPack() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000526 return getTypeForDecl()->getAs<TemplateTypeParmType>()->isParameterPack();
Chandler Carruth08836322011-05-01 00:51:33 +0000527}
528
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000529//===----------------------------------------------------------------------===//
530// NonTypeTemplateParmDecl Method Implementations
531//===----------------------------------------------------------------------===//
532
David Majnemerdfecf1a2016-07-06 04:19:16 +0000533NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(
534 DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D,
535 unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
536 ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos)
537 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
538 TemplateParmPosition(D, P), ParameterPack(true),
539 ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) {
540 if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) {
James Y Knight7a22b242015-08-06 20:26:32 +0000541 auto TypesAndInfos =
542 getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000543 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
James Y Knight7a22b242015-08-06 20:26:32 +0000544 new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
545 TypesAndInfos[I].second = ExpandedTInfos[I];
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000546 }
547 }
548}
549
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000550NonTypeTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000551NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000552 SourceLocation StartLoc, SourceLocation IdLoc,
553 unsigned D, unsigned P, IdentifierInfo *Id,
554 QualType T, bool ParameterPack,
555 TypeSourceInfo *TInfo) {
Richard Smithf7981722013-11-22 09:01:48 +0000556 return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
557 T, ParameterPack, TInfo);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000558}
559
David Majnemerdfecf1a2016-07-06 04:19:16 +0000560NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
561 const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
562 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
563 QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
564 ArrayRef<TypeSourceInfo *> ExpandedTInfos) {
James Y Knight7a22b242015-08-06 20:26:32 +0000565 return new (C, DC,
566 additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
David Majnemerdfecf1a2016-07-06 04:19:16 +0000567 ExpandedTypes.size()))
James Y Knight7a22b242015-08-06 20:26:32 +0000568 NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
David Majnemerdfecf1a2016-07-06 04:19:16 +0000569 ExpandedTypes, ExpandedTInfos);
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000570}
571
Douglas Gregor72172e92012-01-05 21:55:30 +0000572NonTypeTemplateParmDecl *
573NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000574 return new (C, ID) NonTypeTemplateParmDecl(nullptr, SourceLocation(),
575 SourceLocation(), 0, 0, nullptr,
576 QualType(), false, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000577}
578
579NonTypeTemplateParmDecl *
580NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
581 unsigned NumExpandedTypes) {
David Majnemerdfecf1a2016-07-06 04:19:16 +0000582 auto *NTTP =
583 new (C, ID, additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
584 NumExpandedTypes))
585 NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
586 0, 0, nullptr, QualType(), nullptr, None,
587 None);
588 NTTP->NumExpandedTypes = NumExpandedTypes;
589 return NTTP;
Douglas Gregor72172e92012-01-05 21:55:30 +0000590}
591
John McCallf4cd4f92011-02-09 01:13:10 +0000592SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnarae15d5532011-03-04 11:03:48 +0000593 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraea947882011-03-08 16:41:52 +0000594 return SourceRange(getOuterLocStart(),
595 getDefaultArgument()->getSourceRange().getEnd());
596 return DeclaratorDecl::getSourceRange();
John McCallf4cd4f92011-02-09 01:13:10 +0000597}
598
Douglas Gregordba32632009-02-10 19:49:53 +0000599SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara656e3002010-06-09 09:26:05 +0000600 return hasDefaultArgument()
601 ? getDefaultArgument()->getSourceRange().getBegin()
602 : SourceLocation();
Douglas Gregordba32632009-02-10 19:49:53 +0000603}
604
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000605//===----------------------------------------------------------------------===//
606// TemplateTemplateParmDecl Method Implementations
607//===----------------------------------------------------------------------===//
608
Eugene Zelenko421e8902017-11-29 22:39:22 +0000609void TemplateTemplateParmDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000610
Richard Smith1fde8ec2012-09-07 02:06:42 +0000611TemplateTemplateParmDecl::TemplateTemplateParmDecl(
612 DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
613 IdentifierInfo *Id, TemplateParameterList *Params,
David Majnemerdfecf1a2016-07-06 04:19:16 +0000614 ArrayRef<TemplateParameterList *> Expansions)
615 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
616 TemplateParmPosition(D, P), ParameterPack(true),
617 ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) {
618 if (!Expansions.empty())
619 std::uninitialized_copy(Expansions.begin(), Expansions.end(),
James Y Knight7a22b242015-08-06 20:26:32 +0000620 getTrailingObjects<TemplateParameterList *>());
Richard Smith1fde8ec2012-09-07 02:06:42 +0000621}
622
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000623TemplateTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000624TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000625 SourceLocation L, unsigned D, unsigned P,
Douglas Gregorf5500772011-01-05 15:48:55 +0000626 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000627 TemplateParameterList *Params) {
Richard Smithf7981722013-11-22 09:01:48 +0000628 return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
629 Params);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000630}
631
Douglas Gregor72172e92012-01-05 21:55:30 +0000632TemplateTemplateParmDecl *
Richard Smith1fde8ec2012-09-07 02:06:42 +0000633TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
634 SourceLocation L, unsigned D, unsigned P,
635 IdentifierInfo *Id,
636 TemplateParameterList *Params,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000637 ArrayRef<TemplateParameterList *> Expansions) {
James Y Knight7a22b242015-08-06 20:26:32 +0000638 return new (C, DC,
639 additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
David Majnemerdfecf1a2016-07-06 04:19:16 +0000640 TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions);
Richard Smith1fde8ec2012-09-07 02:06:42 +0000641}
642
643TemplateTemplateParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000644TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000645 return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
646 false, nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000647}
648
Richard Smith1fde8ec2012-09-07 02:06:42 +0000649TemplateTemplateParmDecl *
650TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
651 unsigned NumExpansions) {
David Majnemerdfecf1a2016-07-06 04:19:16 +0000652 auto *TTP =
653 new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
654 TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
655 nullptr, None);
656 TTP->NumExpandedParams = NumExpansions;
657 return TTP;
Richard Smith1fde8ec2012-09-07 02:06:42 +0000658}
659
Richard Smith35c1df52015-06-17 20:16:32 +0000660SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
661 return hasDefaultArgument() ? getDefaultArgument().getLocation()
662 : SourceLocation();
663}
664
Richard Smith1469b912015-06-10 00:29:03 +0000665void TemplateTemplateParmDecl::setDefaultArgument(
666 const ASTContext &C, const TemplateArgumentLoc &DefArg) {
667 if (DefArg.getArgument().isNull())
668 DefaultArgument.set(nullptr);
669 else
670 DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
671}
672
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000673//===----------------------------------------------------------------------===//
Douglas Gregord002c7b2009-05-11 23:53:27 +0000674// TemplateArgumentList Implementation
675//===----------------------------------------------------------------------===//
David Majnemer8b622692016-07-03 21:17:51 +0000676TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
677 : Arguments(getTrailingObjects<TemplateArgument>()),
678 NumArguments(Args.size()) {
679 std::uninitialized_copy(Args.begin(), Args.end(),
James Y Knight7a22b242015-08-06 20:26:32 +0000680 getTrailingObjects<TemplateArgument>());
681}
682
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000683TemplateArgumentList *
684TemplateArgumentList::CreateCopy(ASTContext &Context,
David Majnemer8b622692016-07-03 21:17:51 +0000685 ArrayRef<TemplateArgument> Args) {
686 void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
687 return new (Mem) TemplateArgumentList(Args);
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000688}
689
Richard Smithf19a8b02019-05-02 00:49:14 +0000690FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
691 ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
692 TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs,
693 const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI,
694 MemberSpecializationInfo *MSInfo) {
Craig Topper36250ad2014-05-12 05:36:57 +0000695 const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000696 if (TemplateArgsAsWritten)
697 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
698 *TemplateArgsAsWritten);
699
Richard Smithf19a8b02019-05-02 00:49:14 +0000700 void *Mem =
701 C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 1 : 0));
702 return new (Mem) FunctionTemplateSpecializationInfo(
703 FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000704}
705
Douglas Gregord002c7b2009-05-11 23:53:27 +0000706//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000707// TemplateDecl Implementation
708//===----------------------------------------------------------------------===//
709
Eugene Zelenko421e8902017-11-29 22:39:22 +0000710void TemplateDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000711
712//===----------------------------------------------------------------------===//
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000713// ClassTemplateSpecializationDecl Implementation
714//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +0000715
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000716ClassTemplateSpecializationDecl::
Douglas Gregore9029562010-05-06 00:28:52 +0000717ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000718 DeclContext *DC, SourceLocation StartLoc,
719 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000720 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000721 ArrayRef<TemplateArgument> Args,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000722 ClassTemplateSpecializationDecl *PrevDecl)
Eugene Zelenko421e8902017-11-29 22:39:22 +0000723 : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
724 SpecializedTemplate->getIdentifier(), PrevDecl),
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000725 SpecializedTemplate(SpecializedTemplate),
David Majnemer8b622692016-07-03 21:17:51 +0000726 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
Douglas Gregord002c7b2009-05-11 23:53:27 +0000727 SpecializationKind(TSK_Undeclared) {
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000728}
Mike Stump11289f42009-09-09 15:08:12 +0000729
Richard Smith053f6c62014-05-16 23:01:30 +0000730ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
731 Kind DK)
732 : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
733 SourceLocation(), nullptr, nullptr),
Eugene Zelenko421e8902017-11-29 22:39:22 +0000734 SpecializationKind(TSK_Undeclared) {}
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000735
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000736ClassTemplateSpecializationDecl *
Douglas Gregore9029562010-05-06 00:28:52 +0000737ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000738 DeclContext *DC,
739 SourceLocation StartLoc,
740 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000741 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000742 ArrayRef<TemplateArgument> Args,
Douglas Gregor67a65642009-02-17 23:15:12 +0000743 ClassTemplateSpecializationDecl *PrevDecl) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000744 auto *Result =
Richard Smithf7981722013-11-22 09:01:48 +0000745 new (Context, DC) ClassTemplateSpecializationDecl(
746 Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
David Majnemer8b622692016-07-03 21:17:51 +0000747 SpecializedTemplate, Args, PrevDecl);
Erich Keanef92f31c2018-08-01 20:48:16 +0000748 Result->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000749
Douglas Gregor67a65642009-02-17 23:15:12 +0000750 Context.getTypeDeclType(Result, PrevDecl);
751 return Result;
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000752}
Douglas Gregor2373c592009-05-31 09:31:02 +0000753
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000754ClassTemplateSpecializationDecl *
Richard Smithf7981722013-11-22 09:01:48 +0000755ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000756 unsigned ID) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000757 auto *Result =
Richard Smith053f6c62014-05-16 23:01:30 +0000758 new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
Erich Keanef92f31c2018-08-01 20:48:16 +0000759 Result->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000760 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000761}
762
Benjamin Kramer9170e912013-02-22 15:46:01 +0000763void ClassTemplateSpecializationDecl::getNameForDiagnostic(
764 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
765 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +0000766
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000767 const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this);
Richard Smith792c22d2016-12-24 04:09:05 +0000768 if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
769 PS ? PS->getTemplateArgsAsWritten() : nullptr) {
Serge Pavlov03e672c2017-11-28 16:14:14 +0000770 printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +0000771 } else {
772 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
Serge Pavlov03e672c2017-11-28 16:14:14 +0000773 printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +0000774 }
Douglas Gregorb11aad82011-02-19 18:51:44 +0000775}
776
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000777ClassTemplateDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000778ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000779 if (const auto *PartialSpec =
780 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000781 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
782 return SpecializedTemplate.get<ClassTemplateDecl*>();
783}
784
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000785SourceRange
786ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000787 if (ExplicitInfo) {
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000788 SourceLocation Begin = getTemplateKeywordLoc();
789 if (Begin.isValid()) {
790 // Here we have an explicit (partial) specialization or instantiation.
791 assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
792 getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
793 getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
794 if (getExternLoc().isValid())
795 Begin = getExternLoc();
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +0000796 SourceLocation End = getBraceRange().getEnd();
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000797 if (End.isInvalid())
798 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
799 return SourceRange(Begin, End);
800 }
801 // An implicit instantiation of a class template partial specialization
802 // uses ExplicitInfo to record the TypeAsWritten, but the source
803 // locations should be retrieved from the instantiation pattern.
Eugene Zelenko421e8902017-11-29 22:39:22 +0000804 using CTPSDecl = ClassTemplatePartialSpecializationDecl;
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000805 auto *ctpsd = const_cast<CTPSDecl *>(cast<CTPSDecl>(this));
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000806 CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
Craig Topper36250ad2014-05-12 05:36:57 +0000807 assert(inst_from != nullptr);
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000808 return inst_from->getSourceRange();
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000809 }
810 else {
811 // No explicit info available.
812 llvm::PointerUnion<ClassTemplateDecl *,
813 ClassTemplatePartialSpecializationDecl *>
814 inst_from = getInstantiatedFrom();
815 if (inst_from.isNull())
816 return getSpecializedTemplate()->getSourceRange();
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000817 if (const auto *ctd = inst_from.dyn_cast<ClassTemplateDecl *>())
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000818 return ctd->getSourceRange();
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000819 return inst_from.get<ClassTemplatePartialSpecializationDecl *>()
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000820 ->getSourceRange();
821 }
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000822}
823
Douglas Gregor2373c592009-05-31 09:31:02 +0000824//===----------------------------------------------------------------------===//
Saar Razd7aae332019-07-10 21:25:49 +0000825// ConceptDecl Implementation
826//===----------------------------------------------------------------------===//
827ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC,
828 SourceLocation L, DeclarationName Name,
829 TemplateParameterList *Params,
830 Expr *ConstraintExpr) {
831 AdoptTemplateParameterList(Params, DC);
832 return new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr);
833}
834
835ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C,
836 unsigned ID) {
837 ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(),
838 DeclarationName(),
839 nullptr, nullptr);
840
841 return Result;
842}
843
844//===----------------------------------------------------------------------===//
Douglas Gregor2373c592009-05-31 09:31:02 +0000845// ClassTemplatePartialSpecializationDecl Implementation
846//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +0000847void ClassTemplatePartialSpecializationDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000848
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000849ClassTemplatePartialSpecializationDecl::
850ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000851 DeclContext *DC,
852 SourceLocation StartLoc,
853 SourceLocation IdLoc,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000854 TemplateParameterList *Params,
855 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000856 ArrayRef<TemplateArgument> Args,
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000857 const ASTTemplateArgumentListInfo *ArgInfos,
Richard Smithb2f61b42013-08-22 23:27:37 +0000858 ClassTemplatePartialSpecializationDecl *PrevDecl)
Eugene Zelenko421e8902017-11-29 22:39:22 +0000859 : ClassTemplateSpecializationDecl(Context,
860 ClassTemplatePartialSpecialization,
861 TK, DC, StartLoc, IdLoc,
862 SpecializedTemplate, Args, PrevDecl),
863 TemplateParams(Params), ArgsAsWritten(ArgInfos),
864 InstantiatedFromMember(nullptr, false) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000865 AdoptTemplateParameterList(Params, this);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000866}
867
Douglas Gregor2373c592009-05-31 09:31:02 +0000868ClassTemplatePartialSpecializationDecl *
869ClassTemplatePartialSpecializationDecl::
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000870Create(ASTContext &Context, TagKind TK,DeclContext *DC,
871 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregor2373c592009-05-31 09:31:02 +0000872 TemplateParameterList *Params,
873 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000874 ArrayRef<TemplateArgument> Args,
John McCall6b51f282009-11-23 01:53:49 +0000875 const TemplateArgumentListInfo &ArgInfos,
John McCalle78aac42010-03-10 03:28:59 +0000876 QualType CanonInjectedType,
Richard Smithb2f61b42013-08-22 23:27:37 +0000877 ClassTemplatePartialSpecializationDecl *PrevDecl) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000878 const ASTTemplateArgumentListInfo *ASTArgInfos =
879 ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
John McCall0ad16662009-10-29 08:12:44 +0000880
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000881 auto *Result = new (Context, DC)
Richard Smithf7981722013-11-22 09:01:48 +0000882 ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
883 Params, SpecializedTemplate, Args,
David Majnemer8b622692016-07-03 21:17:51 +0000884 ASTArgInfos, PrevDecl);
Douglas Gregor2373c592009-05-31 09:31:02 +0000885 Result->setSpecializationKind(TSK_ExplicitSpecialization);
Erich Keanef92f31c2018-08-01 20:48:16 +0000886 Result->setMayHaveOutOfDateDef(false);
John McCalle78aac42010-03-10 03:28:59 +0000887
888 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregor2373c592009-05-31 09:31:02 +0000889 return Result;
890}
John McCall11083da2009-09-16 22:47:08 +0000891
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000892ClassTemplatePartialSpecializationDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000893ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
894 unsigned ID) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000895 auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C);
Erich Keanef92f31c2018-08-01 20:48:16 +0000896 Result->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000897 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000898}
899
John McCall11083da2009-09-16 22:47:08 +0000900//===----------------------------------------------------------------------===//
901// FriendTemplateDecl Implementation
902//===----------------------------------------------------------------------===//
903
Eugene Zelenko421e8902017-11-29 22:39:22 +0000904void FriendTemplateDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000905
David Majnemerdfecf1a2016-07-06 04:19:16 +0000906FriendTemplateDecl *
907FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
908 SourceLocation L,
909 MutableArrayRef<TemplateParameterList *> Params,
910 FriendUnion Friend, SourceLocation FLoc) {
911 return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc);
John McCall11083da2009-09-16 22:47:08 +0000912}
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000913
Douglas Gregor72172e92012-01-05 21:55:30 +0000914FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
915 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000916 return new (C, ID) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000917}
Richard Smith3f1b5d02011-05-05 21:57:07 +0000918
919//===----------------------------------------------------------------------===//
920// TypeAliasTemplateDecl Implementation
921//===----------------------------------------------------------------------===//
922
923TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
924 DeclContext *DC,
925 SourceLocation L,
926 DeclarationName Name,
927 TemplateParameterList *Params,
928 NamedDecl *Decl) {
929 AdoptTemplateParameterList(Params, DC);
Richard Smith053f6c62014-05-16 23:01:30 +0000930 return new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000931}
932
Douglas Gregor72172e92012-01-05 21:55:30 +0000933TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
934 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000935 return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000936 DeclarationName(), nullptr, nullptr);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000937}
938
Richard Smith3f1b5d02011-05-05 21:57:07 +0000939RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000940TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000941 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +0000942 C.addDestruction(CommonPtr);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000943 return CommonPtr;
944}
945
David Blaikie68e081d2011-12-20 02:48:34 +0000946//===----------------------------------------------------------------------===//
947// ClassScopeFunctionSpecializationDecl Implementation
948//===----------------------------------------------------------------------===//
949
Eugene Zelenko421e8902017-11-29 22:39:22 +0000950void ClassScopeFunctionSpecializationDecl::anchor() {}
Douglas Gregor72172e92012-01-05 21:55:30 +0000951
952ClassScopeFunctionSpecializationDecl *
953ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
954 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000955 return new (C, ID) ClassScopeFunctionSpecializationDecl(
Richard Smithf19a8b02019-05-02 00:49:14 +0000956 nullptr, SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000957}
Larisse Voufo39a1e502013-08-06 01:03:05 +0000958
959//===----------------------------------------------------------------------===//
960// VarTemplateDecl Implementation
961//===----------------------------------------------------------------------===//
962
Larisse Voufoa11bd8a2013-08-13 02:02:26 +0000963VarTemplateDecl *VarTemplateDecl::getDefinition() {
964 VarTemplateDecl *CurD = this;
965 while (CurD) {
966 if (CurD->isThisDeclarationADefinition())
967 return CurD;
968 CurD = CurD->getPreviousDecl();
969 }
Craig Topper36250ad2014-05-12 05:36:57 +0000970 return nullptr;
Larisse Voufoa11bd8a2013-08-13 02:02:26 +0000971}
972
Larisse Voufo39a1e502013-08-06 01:03:05 +0000973VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
974 SourceLocation L, DeclarationName Name,
975 TemplateParameterList *Params,
Richard Smithbeef3452014-01-16 23:39:20 +0000976 VarDecl *Decl) {
David Blaikie07489f92019-04-19 23:04:05 +0000977 AdoptTemplateParameterList(Params, DC);
Richard Smith053f6c62014-05-16 23:01:30 +0000978 return new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000979}
980
981VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
982 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000983 return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
984 DeclarationName(), nullptr, nullptr);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000985}
986
Larisse Voufo39a1e502013-08-06 01:03:05 +0000987void VarTemplateDecl::LoadLazySpecializations() const {
Vassil Vassilev61f64292017-12-14 23:30:18 +0000988 loadLazySpecializationsImpl();
Larisse Voufo39a1e502013-08-06 01:03:05 +0000989}
990
991llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
992VarTemplateDecl::getSpecializations() const {
993 LoadLazySpecializations();
994 return getCommonPtr()->Specializations;
995}
996
997llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
998VarTemplateDecl::getPartialSpecializations() {
999 LoadLazySpecializations();
1000 return getCommonPtr()->PartialSpecializations;
1001}
1002
1003RedeclarableTemplateDecl::CommonBase *
1004VarTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001005 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +00001006 C.addDestruction(CommonPtr);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001007 return CommonPtr;
1008}
1009
1010VarTemplateSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +00001011VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
1012 void *&InsertPos) {
1013 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001014}
1015
1016void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
1017 void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +00001018 addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001019}
1020
1021VarTemplatePartialSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +00001022VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
1023 void *&InsertPos) {
1024 return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001025}
1026
1027void VarTemplateDecl::AddPartialSpecialization(
1028 VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
1029 if (InsertPos)
1030 getPartialSpecializations().InsertNode(D, InsertPos);
1031 else {
1032 VarTemplatePartialSpecializationDecl *Existing =
1033 getPartialSpecializations().GetOrInsertNode(D);
1034 (void)Existing;
1035 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
1036 }
1037
1038 if (ASTMutationListener *L = getASTMutationListener())
1039 L->AddedCXXTemplateSpecialization(this, D);
1040}
1041
1042void VarTemplateDecl::getPartialSpecializations(
1043 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) {
1044 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
1045 getPartialSpecializations();
1046 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +00001047 PS.reserve(PartialSpecs.size());
David Majnemerdfecf1a2016-07-06 04:19:16 +00001048 for (VarTemplatePartialSpecializationDecl &P : PartialSpecs)
1049 PS.push_back(P.getMostRecentDecl());
Larisse Voufo39a1e502013-08-06 01:03:05 +00001050}
1051
1052VarTemplatePartialSpecializationDecl *
1053VarTemplateDecl::findPartialSpecInstantiatedFromMember(
1054 VarTemplatePartialSpecializationDecl *D) {
1055 Decl *DCanon = D->getCanonicalDecl();
David Majnemerdfecf1a2016-07-06 04:19:16 +00001056 for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
1057 if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
1058 return P.getMostRecentDecl();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001059 }
1060
Craig Topper36250ad2014-05-12 05:36:57 +00001061 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00001062}
1063
1064//===----------------------------------------------------------------------===//
1065// VarTemplateSpecializationDecl Implementation
1066//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +00001067
Larisse Voufo39a1e502013-08-06 01:03:05 +00001068VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001069 Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001070 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
David Majnemer8b622692016-07-03 21:17:51 +00001071 TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args)
Richard Smith053f6c62014-05-16 23:01:30 +00001072 : VarDecl(DK, Context, DC, StartLoc, IdLoc,
1073 SpecializedTemplate->getIdentifier(), T, TInfo, S),
Eugene Zelenko421e8902017-11-29 22:39:22 +00001074 SpecializedTemplate(SpecializedTemplate),
David Majnemer8b622692016-07-03 21:17:51 +00001075 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
Richard Smith435e6472017-12-02 02:48:42 +00001076 SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +00001077
Richard Smith053f6c62014-05-16 23:01:30 +00001078VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
1079 ASTContext &C)
1080 : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001081 QualType(), nullptr, SC_None),
Richard Smith435e6472017-12-02 02:48:42 +00001082 SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +00001083
1084VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
1085 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1086 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
David Majnemer8b622692016-07-03 21:17:51 +00001087 TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) {
Richard Smithf7981722013-11-22 09:01:48 +00001088 return new (Context, DC) VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001089 VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
David Majnemer8b622692016-07-03 21:17:51 +00001090 SpecializedTemplate, T, TInfo, S, Args);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001091}
1092
1093VarTemplateSpecializationDecl *
1094VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001095 return new (C, ID)
1096 VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001097}
1098
1099void VarTemplateSpecializationDecl::getNameForDiagnostic(
1100 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
1101 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
1102
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001103 const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this);
Richard Smith792c22d2016-12-24 04:09:05 +00001104 if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
1105 PS ? PS->getTemplateArgsAsWritten() : nullptr) {
Serge Pavlov03e672c2017-11-28 16:14:14 +00001106 printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +00001107 } else {
1108 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
Serge Pavlov03e672c2017-11-28 16:14:14 +00001109 printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +00001110 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001111}
1112
1113VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001114 if (const auto *PartialSpec =
Larisse Voufo39a1e502013-08-06 01:03:05 +00001115 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1116 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
1117 return SpecializedTemplate.get<VarTemplateDecl *>();
1118}
1119
1120void VarTemplateSpecializationDecl::setTemplateArgsInfo(
1121 const TemplateArgumentListInfo &ArgsInfo) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001122 TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
1123 TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
David Majnemerdfecf1a2016-07-06 04:19:16 +00001124 for (const TemplateArgumentLoc &Loc : ArgsInfo.arguments())
1125 TemplateArgsInfo.addArgument(Loc);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001126}
1127
1128//===----------------------------------------------------------------------===//
1129// VarTemplatePartialSpecializationDecl Implementation
1130//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +00001131
Larisse Voufo39a1e502013-08-06 01:03:05 +00001132void VarTemplatePartialSpecializationDecl::anchor() {}
1133
1134VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
1135 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1136 SourceLocation IdLoc, TemplateParameterList *Params,
1137 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00001138 StorageClass S, ArrayRef<TemplateArgument> Args,
Richard Smithb2f61b42013-08-22 23:27:37 +00001139 const ASTTemplateArgumentListInfo *ArgInfos)
Richard Smith053f6c62014-05-16 23:01:30 +00001140 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001141 DC, StartLoc, IdLoc, SpecializedTemplate, T,
David Majnemer8b622692016-07-03 21:17:51 +00001142 TInfo, S, Args),
Larisse Voufo39a1e502013-08-06 01:03:05 +00001143 TemplateParams(Params), ArgsAsWritten(ArgInfos),
Craig Topper36250ad2014-05-12 05:36:57 +00001144 InstantiatedFromMember(nullptr, false) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001145 // TODO: The template parameters should be in DC by now. Verify.
1146 // AdoptTemplateParameterList(Params, DC);
1147}
1148
1149VarTemplatePartialSpecializationDecl *
1150VarTemplatePartialSpecializationDecl::Create(
1151 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1152 SourceLocation IdLoc, TemplateParameterList *Params,
1153 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00001154 StorageClass S, ArrayRef<TemplateArgument> Args,
Richard Smithb2f61b42013-08-22 23:27:37 +00001155 const TemplateArgumentListInfo &ArgInfos) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00001156 const ASTTemplateArgumentListInfo *ASTArgInfos
1157 = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001158
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001159 auto *Result =
Richard Smithf7981722013-11-22 09:01:48 +00001160 new (Context, DC) VarTemplatePartialSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00001161 Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00001162 S, Args, ASTArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001163 Result->setSpecializationKind(TSK_ExplicitSpecialization);
1164 return Result;
1165}
1166
1167VarTemplatePartialSpecializationDecl *
1168VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
1169 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001170 return new (C, ID) VarTemplatePartialSpecializationDecl(C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001171}
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001172
1173static TemplateParameterList *
1174createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
1175 // typename T
1176 auto *T = TemplateTypeParmDecl::Create(
1177 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
1178 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
1179 T->setImplicit(true);
1180
1181 // T ...Ints
1182 TypeSourceInfo *TI =
1183 C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
1184 auto *N = NonTypeTemplateParmDecl::Create(
1185 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1186 /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
1187 N->setImplicit(true);
1188
1189 // <typename T, T ...Ints>
1190 NamedDecl *P[2] = {T, N};
1191 auto *TPL = TemplateParameterList::Create(
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001192 C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001193
1194 // template <typename T, ...Ints> class IntSeq
1195 auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
1196 C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
1197 /*ParameterPack=*/false, /*Id=*/nullptr, TPL);
1198 TemplateTemplateParm->setImplicit(true);
1199
1200 // typename T
1201 auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
1202 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1203 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
1204 TemplateTypeParm->setImplicit(true);
1205
1206 // T N
1207 TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
1208 QualType(TemplateTypeParm->getTypeForDecl(), 0));
1209 auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
1210 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
1211 /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1212 NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
1213 NonTypeTemplateParm};
1214
1215 // template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
1216 return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001217 Params, SourceLocation(), nullptr);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001218}
1219
Eric Fiselier6ad68552016-07-01 01:24:09 +00001220static TemplateParameterList *
1221createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
1222 // std::size_t Index
1223 TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
1224 auto *Index = NonTypeTemplateParmDecl::Create(
1225 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
1226 /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1227
1228 // typename ...T
1229 auto *Ts = TemplateTypeParmDecl::Create(
1230 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1231 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true);
1232 Ts->setImplicit(true);
1233
1234 // template <std::size_t Index, typename ...T>
1235 NamedDecl *Params[] = {Index, Ts};
1236 return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1237 llvm::makeArrayRef(Params),
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001238 SourceLocation(), nullptr);
Eric Fiselier6ad68552016-07-01 01:24:09 +00001239}
1240
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001241static TemplateParameterList *createBuiltinTemplateParameterList(
1242 const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
1243 switch (BTK) {
1244 case BTK__make_integer_seq:
1245 return createMakeIntegerSeqParameterList(C, DC);
Eric Fiselier6ad68552016-07-01 01:24:09 +00001246 case BTK__type_pack_element:
1247 return createTypePackElementParameterList(C, DC);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001248 }
1249
1250 llvm_unreachable("unhandled BuiltinTemplateKind!");
1251}
1252
1253void BuiltinTemplateDecl::anchor() {}
1254
1255BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
1256 DeclarationName Name,
1257 BuiltinTemplateKind BTK)
1258 : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name,
1259 createBuiltinTemplateParameterList(C, DC, BTK)),
1260 BTK(BTK) {}