blob: 23734396b769478093f69f03f1a4857bf99cff3f [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;
Saar Raz0330fba2019-10-15 18:44:06 +000073 if (RequiresClause->containsUnexpandedParameterPack())
74 ContainsUnexpandedParameterPack = true;
Hubert Tonge4a0c0e2016-07-30 22:33:34 +000075 }
Douglas Gregorded2d7b2009-02-04 19:02:06 +000076}
77
Hubert Tonge4a0c0e2016-07-30 22:33:34 +000078TemplateParameterList *
79TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
80 SourceLocation LAngleLoc,
81 ArrayRef<NamedDecl *> Params,
82 SourceLocation RAngleLoc, Expr *RequiresClause) {
83 void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *, Expr *>(
84 Params.size(), RequiresClause ? 1u : 0u),
Benjamin Kramerc3f89252016-10-20 14:27:22 +000085 alignof(TemplateParameterList));
Mike Stump11289f42009-09-09 15:08:12 +000086 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +000087 RAngleLoc, RequiresClause);
Douglas Gregorded2d7b2009-02-04 19:02:06 +000088}
89
Douglas Gregorf8f86832009-02-11 18:16:40 +000090unsigned TemplateParameterList::getMinRequiredArguments() const {
Douglas Gregor0231d8d2011-01-19 20:10:05 +000091 unsigned NumRequiredArgs = 0;
David Majnemerdfecf1a2016-07-06 04:19:16 +000092 for (const NamedDecl *P : asArray()) {
93 if (P->isTemplateParameterPack()) {
94 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P))
Douglas Gregor0231d8d2011-01-19 20:10:05 +000095 if (NTTP->isExpandedParameterPack()) {
96 NumRequiredArgs += NTTP->getNumExpansionTypes();
97 continue;
98 }
David Majnemerdfecf1a2016-07-06 04:19:16 +000099
Douglas Gregorf8f86832009-02-11 18:16:40 +0000100 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000101 }
David Majnemerdfecf1a2016-07-06 04:19:16 +0000102
103 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000104 if (TTP->hasDefaultArgument())
105 break;
David Majnemerdfecf1a2016-07-06 04:19:16 +0000106 } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000107 if (NTTP->hasDefaultArgument())
108 break;
David Majnemerdfecf1a2016-07-06 04:19:16 +0000109 } else if (cast<TemplateTemplateParmDecl>(P)->hasDefaultArgument())
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000110 break;
David Majnemerdfecf1a2016-07-06 04:19:16 +0000111
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000112 ++NumRequiredArgs;
Douglas Gregorf8f86832009-02-11 18:16:40 +0000113 }
David Majnemerdfecf1a2016-07-06 04:19:16 +0000114
Douglas Gregorf8f86832009-02-11 18:16:40 +0000115 return NumRequiredArgs;
116}
117
Douglas Gregor21610382009-10-29 00:04:11 +0000118unsigned TemplateParameterList::getDepth() const {
119 if (size() == 0)
120 return 0;
Fangrui Song6907ce22018-07-30 19:24:48 +0000121
Douglas Gregor21610382009-10-29 00:04:11 +0000122 const NamedDecl *FirstParm = getParam(0);
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000123 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(FirstParm))
Douglas Gregor21610382009-10-29 00:04:11 +0000124 return TTP->getDepth();
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000125 else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
Douglas Gregor21610382009-10-29 00:04:11 +0000126 return NTTP->getDepth();
127 else
128 return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
129}
130
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000131static void AdoptTemplateParameterList(TemplateParameterList *Params,
132 DeclContext *Owner) {
David Majnemerdfecf1a2016-07-06 04:19:16 +0000133 for (NamedDecl *P : *Params) {
134 P->setDeclContext(Owner);
135
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000136 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000137 AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner);
138 }
139}
140
Saar Raz0330fba2019-10-15 18:44:06 +0000141void TemplateParameterList::
142getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
143 // TODO: Concepts: Collect immediately-introduced constraints.
144 if (HasRequiresClause)
145 AC.push_back(getRequiresClause());
146}
147
148bool TemplateParameterList::hasAssociatedConstraints() const {
149 // TODO: Concepts: Regard immediately-introduced constraints.
150 return HasRequiresClause;
151}
152
Richard Smithe7bd6de2015-06-10 20:30:23 +0000153namespace clang {
Eugene Zelenko421e8902017-11-29 22:39:22 +0000154
Richard Smithe7bd6de2015-06-10 20:30:23 +0000155void *allocateDefaultArgStorageChain(const ASTContext &C) {
156 return new (C) char[sizeof(void*) * 2];
157}
Eugene Zelenko421e8902017-11-29 22:39:22 +0000158
159} // namespace clang
Richard Smithe7bd6de2015-06-10 20:30:23 +0000160
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000161//===----------------------------------------------------------------------===//
Saar Raz0330fba2019-10-15 18:44:06 +0000162// TemplateDecl Implementation
163//===----------------------------------------------------------------------===//
164
165TemplateDecl::TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
166 DeclarationName Name, TemplateParameterList *Params,
167 NamedDecl *Decl)
168 : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl), TemplateParams(Params) {}
169
170void TemplateDecl::anchor() {}
171
172void TemplateDecl::
173getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
174 // TODO: Concepts: Append function trailing requires clause.
175 TemplateParams->getAssociatedConstraints(AC);
176}
177
178bool TemplateDecl::hasAssociatedConstraints() const {
179 // TODO: Concepts: Regard function trailing requires clause.
180 return TemplateParams->hasAssociatedConstraints();
181}
182
183//===----------------------------------------------------------------------===//
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000184// RedeclarableTemplateDecl Implementation
185//===----------------------------------------------------------------------===//
186
Richard Trieub3e902f2018-12-29 02:02:30 +0000187void RedeclarableTemplateDecl::anchor() {}
188
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000189RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000190 if (Common)
191 return Common;
192
193 // Walk the previous-declaration chain until we either find a declaration
194 // with a common pointer or we run out of previous declarations.
195 SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
196 for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
197 Prev = Prev->getPreviousDecl()) {
198 if (Prev->Common) {
199 Common = Prev->Common;
200 break;
Douglas Gregor68444de2012-01-14 15:13:49 +0000201 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000202
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000203 PrevDecls.push_back(Prev);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000204 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000205
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000206 // If we never found a common pointer, allocate one now.
207 if (!Common) {
208 // FIXME: If any of the declarations is from an AST file, we probably
209 // need an update record to add the common data.
210
211 Common = newCommon(getASTContext());
212 }
213
214 // Update any previous declarations we saw with the common pointer.
David Majnemerdfecf1a2016-07-06 04:19:16 +0000215 for (const RedeclarableTemplateDecl *Prev : PrevDecls)
216 Prev->Common = Common;
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000217
Douglas Gregor68444de2012-01-14 15:13:49 +0000218 return Common;
Peter Collingbourne029fd692010-07-29 16:12:09 +0000219}
220
Vassil Vassilev61f64292017-12-14 23:30:18 +0000221void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
222 // Grab the most recent declaration to ensure we've loaded any lazy
223 // redeclarations of this template.
224 CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
225 if (CommonBasePtr->LazySpecializations) {
226 ASTContext &Context = getASTContext();
227 uint32_t *Specs = CommonBasePtr->LazySpecializations;
228 CommonBasePtr->LazySpecializations = nullptr;
229 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
230 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
231 }
232}
233
Saar Razdf061c32019-12-23 08:37:35 +0200234template<class EntryType, typename... ProfileArguments>
Richard Smithe977e512015-02-24 01:23:23 +0000235typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000236RedeclarableTemplateDecl::findSpecializationImpl(
Saar Razdf061c32019-12-23 08:37:35 +0200237 llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos,
238 ProfileArguments&&... ProfileArgs) {
Eugene Zelenko421e8902017-11-29 22:39:22 +0000239 using SETraits = SpecEntryTraits<EntryType>;
240
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000241 llvm::FoldingSetNodeID ID;
Saar Razdf061c32019-12-23 08:37:35 +0200242 EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
243 getASTContext());
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000244 EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
Richard Smithe977e512015-02-24 01:23:23 +0000245 return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr;
246}
247
248template<class Derived, class EntryType>
249void RedeclarableTemplateDecl::addSpecializationImpl(
250 llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
251 void *InsertPos) {
Eugene Zelenko421e8902017-11-29 22:39:22 +0000252 using SETraits = SpecEntryTraits<EntryType>;
253
Richard Smithe977e512015-02-24 01:23:23 +0000254 if (InsertPos) {
255#ifndef NDEBUG
256 void *CorrectInsertPos;
257 assert(!findSpecializationImpl(Specializations,
Saar Razdf061c32019-12-23 08:37:35 +0200258 CorrectInsertPos,
259 SETraits::getTemplateArgs(Entry)) &&
Richard Smithe977e512015-02-24 01:23:23 +0000260 InsertPos == CorrectInsertPos &&
261 "given incorrect InsertPos for specialization");
262#endif
263 Specializations.InsertNode(Entry, InsertPos);
264 } else {
265 EntryType *Existing = Specializations.GetOrInsertNode(Entry);
266 (void)Existing;
267 assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
268 "non-canonical specialization?");
269 }
270
271 if (ASTMutationListener *L = getASTMutationListener())
272 L->AddedCXXTemplateSpecialization(cast<Derived>(this),
273 SETraits::getDecl(Entry));
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000274}
275
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000276//===----------------------------------------------------------------------===//
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000277// FunctionTemplateDecl Implementation
278//===----------------------------------------------------------------------===//
279
280FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
281 DeclContext *DC,
282 SourceLocation L,
283 DeclarationName Name,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000284 TemplateParameterList *Params,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000285 NamedDecl *Decl) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000286 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Richard Smith053f6c62014-05-16 23:01:30 +0000287 return new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000288}
289
Douglas Gregor72172e92012-01-05 21:55:30 +0000290FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
291 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000292 return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000293 DeclarationName(), nullptr, nullptr);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000294}
295
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000296RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000297FunctionTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000298 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +0000299 C.addDestruction(CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000300 return CommonPtr;
301}
302
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000303void FunctionTemplateDecl::LoadLazySpecializations() const {
Vassil Vassilev61f64292017-12-14 23:30:18 +0000304 loadLazySpecializationsImpl();
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000305}
306
307llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
308FunctionTemplateDecl::getSpecializations() const {
309 LoadLazySpecializations();
310 return getCommonPtr()->Specializations;
311}
312
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000313FunctionDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000314FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
315 void *&InsertPos) {
Saar Razdf061c32019-12-23 08:37:35 +0200316 return findSpecializationImpl(getSpecializations(), InsertPos, Args);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000317}
318
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000319void FunctionTemplateDecl::addSpecialization(
320 FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +0000321 addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info,
322 InsertPos);
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000323}
324
Richard Smith841d8b22013-05-17 03:04:50 +0000325ArrayRef<TemplateArgument> FunctionTemplateDecl::getInjectedTemplateArgs() {
Douglas Gregor43669f82011-03-05 17:54:25 +0000326 TemplateParameterList *Params = getTemplateParameters();
327 Common *CommonPtr = getCommonPtr();
328 if (!CommonPtr->InjectedArgs) {
Richard Smith43e14d22016-12-23 02:10:11 +0000329 auto &Context = getASTContext();
330 SmallVector<TemplateArgument, 16> TemplateArgs;
331 Context.getInjectedTemplateArgs(Params, TemplateArgs);
332 CommonPtr->InjectedArgs =
333 new (Context) TemplateArgument[TemplateArgs.size()];
334 std::copy(TemplateArgs.begin(), TemplateArgs.end(),
335 CommonPtr->InjectedArgs);
Douglas Gregor43669f82011-03-05 17:54:25 +0000336 }
Richard Smith841d8b22013-05-17 03:04:50 +0000337
338 return llvm::makeArrayRef(CommonPtr->InjectedArgs, Params->size());
Douglas Gregor43669f82011-03-05 17:54:25 +0000339}
340
Erik Pilkingtond1a184f2018-10-10 17:17:51 +0000341void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
342 using Base = RedeclarableTemplateDecl;
343
344 // If we haven't created a common pointer yet, then it can just be created
345 // with the usual method.
346 if (!Base::Common)
347 return;
348
349 Common *ThisCommon = static_cast<Common *>(Base::Common);
350 Common *PrevCommon = nullptr;
351 SmallVector<FunctionTemplateDecl *, 8> PreviousDecls;
352 for (; Prev; Prev = Prev->getPreviousDecl()) {
353 if (Prev->Base::Common) {
354 PrevCommon = static_cast<Common *>(Prev->Base::Common);
355 break;
356 }
357 PreviousDecls.push_back(Prev);
358 }
359
360 // If the previous redecl chain hasn't created a common pointer yet, then just
361 // use this common pointer.
362 if (!PrevCommon) {
363 for (auto *D : PreviousDecls)
364 D->Base::Common = ThisCommon;
365 return;
366 }
367
368 // Ensure we don't leak any important state.
369 assert(ThisCommon->Specializations.size() == 0 &&
Erik Pilkingtond1a184f2018-10-10 17:17:51 +0000370 "Can't merge incompatible declarations!");
371
372 Base::Common = PrevCommon;
373}
374
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000375//===----------------------------------------------------------------------===//
376// ClassTemplateDecl Implementation
377//===----------------------------------------------------------------------===//
378
379ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
380 DeclContext *DC,
381 SourceLocation L,
382 DeclarationName Name,
383 TemplateParameterList *Params,
Saar Raz0330fba2019-10-15 18:44:06 +0000384 NamedDecl *Decl) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000385 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Hubert Tong5a8ec4e2017-02-10 02:46:19 +0000386
Saar Raz0330fba2019-10-15 18:44:06 +0000387 return new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl);
Douglas Gregor90a1a652009-03-19 17:26:29 +0000388}
389
Richard Smith053f6c62014-05-16 23:01:30 +0000390ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000391 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000392 return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
393 DeclarationName(), nullptr, nullptr);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000394}
395
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000396void ClassTemplateDecl::LoadLazySpecializations() const {
Vassil Vassilev61f64292017-12-14 23:30:18 +0000397 loadLazySpecializationsImpl();
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000398}
399
Chandler Carruthb41171b2012-05-03 23:49:05 +0000400llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000401ClassTemplateDecl::getSpecializations() const {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000402 LoadLazySpecializations();
403 return getCommonPtr()->Specializations;
Fangrui Song6907ce22018-07-30 19:24:48 +0000404}
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000405
Chandler Carruthb41171b2012-05-03 23:49:05 +0000406llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000407ClassTemplateDecl::getPartialSpecializations() {
408 LoadLazySpecializations();
409 return getCommonPtr()->PartialSpecializations;
Fangrui Song6907ce22018-07-30 19:24:48 +0000410}
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000411
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000412RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000413ClassTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000414 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +0000415 C.addDestruction(CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000416 return CommonPtr;
417}
418
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000419ClassTemplateSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000420ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
421 void *&InsertPos) {
Saar Razdf061c32019-12-23 08:37:35 +0200422 return findSpecializationImpl(getSpecializations(), InsertPos, Args);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000423}
424
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000425void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
426 void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +0000427 addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos);
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000428}
429
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000430ClassTemplatePartialSpecializationDecl *
Saar Razdf061c32019-12-23 08:37:35 +0200431ClassTemplateDecl::findPartialSpecialization(
432 ArrayRef<TemplateArgument> Args,
433 TemplateParameterList *TPL, void *&InsertPos) {
434 return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
435 TPL);
436}
437
438static void ProfileTemplateParameterList(ASTContext &C,
439 llvm::FoldingSetNodeID &ID, const TemplateParameterList *TPL) {
440 const Expr *RC = TPL->getRequiresClause();
441 ID.AddBoolean(RC != nullptr);
442 if (RC)
443 RC->Profile(ID, C, /*Canonical=*/true);
444 ID.AddInteger(TPL->size());
445 for (NamedDecl *D : *TPL) {
446 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
447 ID.AddInteger(0);
448 ID.AddBoolean(NTTP->isParameterPack());
449 NTTP->getType().getCanonicalType().Profile(ID);
450 continue;
451 }
452 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) {
453 ID.AddInteger(1);
454 ID.AddBoolean(TTP->isParameterPack());
455 // TODO: Concepts: profile type-constraints.
456 continue;
457 }
458 const auto *TTP = cast<TemplateTemplateParmDecl>(D);
459 ID.AddInteger(2);
460 ID.AddBoolean(TTP->isParameterPack());
461 ProfileTemplateParameterList(C, ID, TTP->getTemplateParameters());
462 }
463}
464
465void
466ClassTemplatePartialSpecializationDecl::Profile(llvm::FoldingSetNodeID &ID,
467 ArrayRef<TemplateArgument> TemplateArgs, TemplateParameterList *TPL,
468 ASTContext &Context) {
469 ID.AddInteger(TemplateArgs.size());
470 for (const TemplateArgument &TemplateArg : TemplateArgs)
471 TemplateArg.Profile(ID, Context);
472 ProfileTemplateParameterList(Context, ID, TPL);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000473}
474
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000475void ClassTemplateDecl::AddPartialSpecialization(
476 ClassTemplatePartialSpecializationDecl *D,
477 void *InsertPos) {
Douglas Gregorce9978f2012-03-28 14:34:23 +0000478 if (InsertPos)
479 getPartialSpecializations().InsertNode(D, InsertPos);
480 else {
481 ClassTemplatePartialSpecializationDecl *Existing
482 = getPartialSpecializations().GetOrInsertNode(D);
483 (void)Existing;
484 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
485 }
486
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000487 if (ASTMutationListener *L = getASTMutationListener())
488 L->AddedCXXTemplateSpecialization(this, D);
489}
490
Douglas Gregor407e9612010-04-30 05:56:50 +0000491void ClassTemplateDecl::getPartialSpecializations(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000492 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
Chandler Carruthb41171b2012-05-03 23:49:05 +0000493 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000494 = getPartialSpecializations();
Douglas Gregor407e9612010-04-30 05:56:50 +0000495 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +0000496 PS.reserve(PartialSpecs.size());
David Majnemerdfecf1a2016-07-06 04:19:16 +0000497 for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs)
498 PS.push_back(P.getMostRecentDecl());
Douglas Gregor407e9612010-04-30 05:56:50 +0000499}
500
Douglas Gregor15301382009-07-30 17:40:51 +0000501ClassTemplatePartialSpecializationDecl *
502ClassTemplateDecl::findPartialSpecialization(QualType T) {
503 ASTContext &Context = getASTContext();
David Majnemerdfecf1a2016-07-06 04:19:16 +0000504 for (ClassTemplatePartialSpecializationDecl &P :
505 getPartialSpecializations()) {
506 if (Context.hasSameType(P.getInjectedSpecializationType(), T))
507 return P.getMostRecentDecl();
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000508 }
509
Craig Topper36250ad2014-05-12 05:36:57 +0000510 return nullptr;
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000511}
512
513ClassTemplatePartialSpecializationDecl *
514ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
515 ClassTemplatePartialSpecializationDecl *D) {
516 Decl *DCanon = D->getCanonicalDecl();
David Majnemerdfecf1a2016-07-06 04:19:16 +0000517 for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
518 if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
519 return P.getMostRecentDecl();
Douglas Gregor15301382009-07-30 17:40:51 +0000520 }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Craig Topper36250ad2014-05-12 05:36:57 +0000522 return nullptr;
Douglas Gregor15301382009-07-30 17:40:51 +0000523}
524
John McCalle78aac42010-03-10 03:28:59 +0000525QualType
Douglas Gregor9961ce92010-07-08 18:37:38 +0000526ClassTemplateDecl::getInjectedClassNameSpecialization() {
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000527 Common *CommonPtr = getCommonPtr();
Douglas Gregore362cea2009-05-10 22:57:19 +0000528 if (!CommonPtr->InjectedClassNameType.isNull())
529 return CommonPtr->InjectedClassNameType;
530
Douglas Gregor8092e802010-12-23 16:00:30 +0000531 // C++0x [temp.dep.type]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +0000532 // The template argument list of a primary template is a template argument
Douglas Gregor8092e802010-12-23 16:00:30 +0000533 // list in which the nth template argument has the value of the nth template
Fangrui Song6907ce22018-07-30 19:24:48 +0000534 // parameter of the class template. If the nth template parameter is a
535 // template parameter pack (14.5.3), the nth template argument is a pack
536 // expansion (14.5.3) whose pattern is the name of the template parameter
Douglas Gregor8092e802010-12-23 16:00:30 +0000537 // pack.
Douglas Gregor9961ce92010-07-08 18:37:38 +0000538 ASTContext &Context = getASTContext();
Douglas Gregore362cea2009-05-10 22:57:19 +0000539 TemplateParameterList *Params = getTemplateParameters();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000540 SmallVector<TemplateArgument, 16> TemplateArgs;
Richard Smith43e14d22016-12-23 02:10:11 +0000541 Context.getInjectedTemplateArgs(Params, TemplateArgs);
Douglas Gregore362cea2009-05-10 22:57:19 +0000542 CommonPtr->InjectedClassNameType
Douglas Gregora8e02e72009-07-28 23:00:59 +0000543 = Context.getTemplateSpecializationType(TemplateName(this),
David Majnemer6fbeee32016-07-07 04:43:07 +0000544 TemplateArgs);
Douglas Gregore362cea2009-05-10 22:57:19 +0000545 return CommonPtr->InjectedClassNameType;
546}
547
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000548//===----------------------------------------------------------------------===//
549// TemplateTypeParm Allocation/Deallocation Method Implementations
550//===----------------------------------------------------------------------===//
551
552TemplateTypeParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000553TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000554 SourceLocation KeyLoc, SourceLocation NameLoc,
555 unsigned D, unsigned P, IdentifierInfo *Id,
556 bool Typename, bool ParameterPack) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000557 auto *TTPDecl =
558 new (C, DC) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
Chandler Carruth08836322011-05-01 00:51:33 +0000559 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
Richard Smith5b21db82014-04-23 18:20:42 +0000560 TTPDecl->setTypeForDecl(TTPType.getTypePtr());
Chandler Carruth08836322011-05-01 00:51:33 +0000561 return TTPDecl;
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000562}
563
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000564TemplateTypeParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000565TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000566 return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(),
567 SourceLocation(), nullptr, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000568}
569
John McCall0ad16662009-10-29 08:12:44 +0000570SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara23485e02011-03-04 12:42:03 +0000571 return hasDefaultArgument()
Richard Smith1469b912015-06-10 00:29:03 +0000572 ? getDefaultArgumentInfo()->getTypeLoc().getBeginLoc()
573 : SourceLocation();
Abramo Bagnara23485e02011-03-04 12:42:03 +0000574}
575
576SourceRange TemplateTypeParmDecl::getSourceRange() const {
577 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000578 return SourceRange(getBeginLoc(),
Richard Smith1469b912015-06-10 00:29:03 +0000579 getDefaultArgumentInfo()->getTypeLoc().getEndLoc());
Kadir Cetinkayafd019ed2019-10-01 14:08:51 +0000580 // TypeDecl::getSourceRange returns a range containing name location, which is
581 // wrong for unnamed template parameters. e.g:
582 // it will return <[[typename>]] instead of <[[typename]]>
583 else if (getDeclName().isEmpty())
584 return SourceRange(getBeginLoc());
585 return TypeDecl::getSourceRange();
John McCall0ad16662009-10-29 08:12:44 +0000586}
587
Douglas Gregor21610382009-10-29 00:04:11 +0000588unsigned TemplateTypeParmDecl::getDepth() const {
Simon Pilgrim86976c92019-10-03 16:58:01 +0000589 return getTypeForDecl()->castAs<TemplateTypeParmType>()->getDepth();
Douglas Gregor21610382009-10-29 00:04:11 +0000590}
591
592unsigned TemplateTypeParmDecl::getIndex() const {
Simon Pilgrim86976c92019-10-03 16:58:01 +0000593 return getTypeForDecl()->castAs<TemplateTypeParmType>()->getIndex();
Douglas Gregor21610382009-10-29 00:04:11 +0000594}
595
Chandler Carruth08836322011-05-01 00:51:33 +0000596bool TemplateTypeParmDecl::isParameterPack() const {
Simon Pilgrim86976c92019-10-03 16:58:01 +0000597 return getTypeForDecl()->castAs<TemplateTypeParmType>()->isParameterPack();
Chandler Carruth08836322011-05-01 00:51:33 +0000598}
599
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000600//===----------------------------------------------------------------------===//
601// NonTypeTemplateParmDecl Method Implementations
602//===----------------------------------------------------------------------===//
603
David Majnemerdfecf1a2016-07-06 04:19:16 +0000604NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(
605 DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D,
606 unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
607 ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos)
608 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
609 TemplateParmPosition(D, P), ParameterPack(true),
610 ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) {
611 if (!ExpandedTypes.empty() && !ExpandedTInfos.empty()) {
James Y Knight7a22b242015-08-06 20:26:32 +0000612 auto TypesAndInfos =
613 getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000614 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
James Y Knight7a22b242015-08-06 20:26:32 +0000615 new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
616 TypesAndInfos[I].second = ExpandedTInfos[I];
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000617 }
618 }
619}
620
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000621NonTypeTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000622NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000623 SourceLocation StartLoc, SourceLocation IdLoc,
624 unsigned D, unsigned P, IdentifierInfo *Id,
625 QualType T, bool ParameterPack,
626 TypeSourceInfo *TInfo) {
Richard Smithf7981722013-11-22 09:01:48 +0000627 return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
628 T, ParameterPack, TInfo);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000629}
630
David Majnemerdfecf1a2016-07-06 04:19:16 +0000631NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
632 const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
633 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
634 QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
635 ArrayRef<TypeSourceInfo *> ExpandedTInfos) {
James Y Knight7a22b242015-08-06 20:26:32 +0000636 return new (C, DC,
637 additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
David Majnemerdfecf1a2016-07-06 04:19:16 +0000638 ExpandedTypes.size()))
James Y Knight7a22b242015-08-06 20:26:32 +0000639 NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
David Majnemerdfecf1a2016-07-06 04:19:16 +0000640 ExpandedTypes, ExpandedTInfos);
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000641}
642
Douglas Gregor72172e92012-01-05 21:55:30 +0000643NonTypeTemplateParmDecl *
644NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000645 return new (C, ID) NonTypeTemplateParmDecl(nullptr, SourceLocation(),
646 SourceLocation(), 0, 0, nullptr,
647 QualType(), false, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000648}
649
650NonTypeTemplateParmDecl *
651NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
652 unsigned NumExpandedTypes) {
David Majnemerdfecf1a2016-07-06 04:19:16 +0000653 auto *NTTP =
654 new (C, ID, additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
655 NumExpandedTypes))
656 NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
657 0, 0, nullptr, QualType(), nullptr, None,
658 None);
659 NTTP->NumExpandedTypes = NumExpandedTypes;
660 return NTTP;
Douglas Gregor72172e92012-01-05 21:55:30 +0000661}
662
John McCallf4cd4f92011-02-09 01:13:10 +0000663SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnarae15d5532011-03-04 11:03:48 +0000664 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraea947882011-03-08 16:41:52 +0000665 return SourceRange(getOuterLocStart(),
666 getDefaultArgument()->getSourceRange().getEnd());
667 return DeclaratorDecl::getSourceRange();
John McCallf4cd4f92011-02-09 01:13:10 +0000668}
669
Douglas Gregordba32632009-02-10 19:49:53 +0000670SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara656e3002010-06-09 09:26:05 +0000671 return hasDefaultArgument()
672 ? getDefaultArgument()->getSourceRange().getBegin()
673 : SourceLocation();
Douglas Gregordba32632009-02-10 19:49:53 +0000674}
675
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000676//===----------------------------------------------------------------------===//
677// TemplateTemplateParmDecl Method Implementations
678//===----------------------------------------------------------------------===//
679
Eugene Zelenko421e8902017-11-29 22:39:22 +0000680void TemplateTemplateParmDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000681
Richard Smith1fde8ec2012-09-07 02:06:42 +0000682TemplateTemplateParmDecl::TemplateTemplateParmDecl(
683 DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
684 IdentifierInfo *Id, TemplateParameterList *Params,
David Majnemerdfecf1a2016-07-06 04:19:16 +0000685 ArrayRef<TemplateParameterList *> Expansions)
686 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
687 TemplateParmPosition(D, P), ParameterPack(true),
688 ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) {
689 if (!Expansions.empty())
690 std::uninitialized_copy(Expansions.begin(), Expansions.end(),
James Y Knight7a22b242015-08-06 20:26:32 +0000691 getTrailingObjects<TemplateParameterList *>());
Richard Smith1fde8ec2012-09-07 02:06:42 +0000692}
693
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000694TemplateTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000695TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000696 SourceLocation L, unsigned D, unsigned P,
Douglas Gregorf5500772011-01-05 15:48:55 +0000697 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000698 TemplateParameterList *Params) {
Richard Smithf7981722013-11-22 09:01:48 +0000699 return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
700 Params);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000701}
702
Douglas Gregor72172e92012-01-05 21:55:30 +0000703TemplateTemplateParmDecl *
Richard Smith1fde8ec2012-09-07 02:06:42 +0000704TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
705 SourceLocation L, unsigned D, unsigned P,
706 IdentifierInfo *Id,
707 TemplateParameterList *Params,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000708 ArrayRef<TemplateParameterList *> Expansions) {
James Y Knight7a22b242015-08-06 20:26:32 +0000709 return new (C, DC,
710 additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
David Majnemerdfecf1a2016-07-06 04:19:16 +0000711 TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions);
Richard Smith1fde8ec2012-09-07 02:06:42 +0000712}
713
714TemplateTemplateParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000715TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000716 return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
717 false, nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000718}
719
Richard Smith1fde8ec2012-09-07 02:06:42 +0000720TemplateTemplateParmDecl *
721TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
722 unsigned NumExpansions) {
David Majnemerdfecf1a2016-07-06 04:19:16 +0000723 auto *TTP =
724 new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
725 TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
726 nullptr, None);
727 TTP->NumExpandedParams = NumExpansions;
728 return TTP;
Richard Smith1fde8ec2012-09-07 02:06:42 +0000729}
730
Richard Smith35c1df52015-06-17 20:16:32 +0000731SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
732 return hasDefaultArgument() ? getDefaultArgument().getLocation()
733 : SourceLocation();
734}
735
Richard Smith1469b912015-06-10 00:29:03 +0000736void TemplateTemplateParmDecl::setDefaultArgument(
737 const ASTContext &C, const TemplateArgumentLoc &DefArg) {
738 if (DefArg.getArgument().isNull())
739 DefaultArgument.set(nullptr);
740 else
741 DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
742}
743
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000744//===----------------------------------------------------------------------===//
Douglas Gregord002c7b2009-05-11 23:53:27 +0000745// TemplateArgumentList Implementation
746//===----------------------------------------------------------------------===//
David Majnemer8b622692016-07-03 21:17:51 +0000747TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
748 : Arguments(getTrailingObjects<TemplateArgument>()),
749 NumArguments(Args.size()) {
750 std::uninitialized_copy(Args.begin(), Args.end(),
James Y Knight7a22b242015-08-06 20:26:32 +0000751 getTrailingObjects<TemplateArgument>());
752}
753
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000754TemplateArgumentList *
755TemplateArgumentList::CreateCopy(ASTContext &Context,
David Majnemer8b622692016-07-03 21:17:51 +0000756 ArrayRef<TemplateArgument> Args) {
757 void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
758 return new (Mem) TemplateArgumentList(Args);
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000759}
760
Richard Smithf19a8b02019-05-02 00:49:14 +0000761FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
762 ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
763 TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs,
764 const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI,
765 MemberSpecializationInfo *MSInfo) {
Craig Topper36250ad2014-05-12 05:36:57 +0000766 const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000767 if (TemplateArgsAsWritten)
768 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
769 *TemplateArgsAsWritten);
770
Richard Smithf19a8b02019-05-02 00:49:14 +0000771 void *Mem =
772 C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 1 : 0));
773 return new (Mem) FunctionTemplateSpecializationInfo(
774 FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo);
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000775}
776
Douglas Gregord002c7b2009-05-11 23:53:27 +0000777//===----------------------------------------------------------------------===//
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000778// ClassTemplateSpecializationDecl Implementation
779//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +0000780
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000781ClassTemplateSpecializationDecl::
Douglas Gregore9029562010-05-06 00:28:52 +0000782ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000783 DeclContext *DC, SourceLocation StartLoc,
784 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000785 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000786 ArrayRef<TemplateArgument> Args,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000787 ClassTemplateSpecializationDecl *PrevDecl)
Eugene Zelenko421e8902017-11-29 22:39:22 +0000788 : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
789 SpecializedTemplate->getIdentifier(), PrevDecl),
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000790 SpecializedTemplate(SpecializedTemplate),
David Majnemer8b622692016-07-03 21:17:51 +0000791 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
Douglas Gregord002c7b2009-05-11 23:53:27 +0000792 SpecializationKind(TSK_Undeclared) {
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000793}
Mike Stump11289f42009-09-09 15:08:12 +0000794
Richard Smith053f6c62014-05-16 23:01:30 +0000795ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
796 Kind DK)
797 : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
798 SourceLocation(), nullptr, nullptr),
Eugene Zelenko421e8902017-11-29 22:39:22 +0000799 SpecializationKind(TSK_Undeclared) {}
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000800
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000801ClassTemplateSpecializationDecl *
Douglas Gregore9029562010-05-06 00:28:52 +0000802ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000803 DeclContext *DC,
804 SourceLocation StartLoc,
805 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000806 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000807 ArrayRef<TemplateArgument> Args,
Douglas Gregor67a65642009-02-17 23:15:12 +0000808 ClassTemplateSpecializationDecl *PrevDecl) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000809 auto *Result =
Richard Smithf7981722013-11-22 09:01:48 +0000810 new (Context, DC) ClassTemplateSpecializationDecl(
811 Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
David Majnemer8b622692016-07-03 21:17:51 +0000812 SpecializedTemplate, Args, PrevDecl);
Erich Keanef92f31c2018-08-01 20:48:16 +0000813 Result->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000814
Douglas Gregor67a65642009-02-17 23:15:12 +0000815 Context.getTypeDeclType(Result, PrevDecl);
816 return Result;
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000817}
Douglas Gregor2373c592009-05-31 09:31:02 +0000818
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000819ClassTemplateSpecializationDecl *
Richard Smithf7981722013-11-22 09:01:48 +0000820ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000821 unsigned ID) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000822 auto *Result =
Richard Smith053f6c62014-05-16 23:01:30 +0000823 new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
Erich Keanef92f31c2018-08-01 20:48:16 +0000824 Result->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000825 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000826}
827
Benjamin Kramer9170e912013-02-22 15:46:01 +0000828void ClassTemplateSpecializationDecl::getNameForDiagnostic(
829 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
830 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +0000831
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000832 const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this);
Richard Smith792c22d2016-12-24 04:09:05 +0000833 if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
834 PS ? PS->getTemplateArgsAsWritten() : nullptr) {
Serge Pavlov03e672c2017-11-28 16:14:14 +0000835 printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +0000836 } else {
837 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
Serge Pavlov03e672c2017-11-28 16:14:14 +0000838 printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +0000839 }
Douglas Gregorb11aad82011-02-19 18:51:44 +0000840}
841
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000842ClassTemplateDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000843ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000844 if (const auto *PartialSpec =
845 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000846 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
847 return SpecializedTemplate.get<ClassTemplateDecl*>();
848}
849
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000850SourceRange
851ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000852 if (ExplicitInfo) {
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000853 SourceLocation Begin = getTemplateKeywordLoc();
854 if (Begin.isValid()) {
855 // Here we have an explicit (partial) specialization or instantiation.
856 assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
857 getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
858 getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
859 if (getExternLoc().isValid())
860 Begin = getExternLoc();
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +0000861 SourceLocation End = getBraceRange().getEnd();
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000862 if (End.isInvalid())
863 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
864 return SourceRange(Begin, End);
865 }
866 // An implicit instantiation of a class template partial specialization
867 // uses ExplicitInfo to record the TypeAsWritten, but the source
868 // locations should be retrieved from the instantiation pattern.
Eugene Zelenko421e8902017-11-29 22:39:22 +0000869 using CTPSDecl = ClassTemplatePartialSpecializationDecl;
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000870 auto *ctpsd = const_cast<CTPSDecl *>(cast<CTPSDecl>(this));
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000871 CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
Craig Topper36250ad2014-05-12 05:36:57 +0000872 assert(inst_from != nullptr);
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000873 return inst_from->getSourceRange();
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000874 }
875 else {
876 // No explicit info available.
877 llvm::PointerUnion<ClassTemplateDecl *,
878 ClassTemplatePartialSpecializationDecl *>
879 inst_from = getInstantiatedFrom();
880 if (inst_from.isNull())
881 return getSpecializedTemplate()->getSourceRange();
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000882 if (const auto *ctd = inst_from.dyn_cast<ClassTemplateDecl *>())
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000883 return ctd->getSourceRange();
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000884 return inst_from.get<ClassTemplatePartialSpecializationDecl *>()
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000885 ->getSourceRange();
886 }
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000887}
888
Douglas Gregor2373c592009-05-31 09:31:02 +0000889//===----------------------------------------------------------------------===//
Saar Razd7aae332019-07-10 21:25:49 +0000890// ConceptDecl Implementation
891//===----------------------------------------------------------------------===//
892ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC,
893 SourceLocation L, DeclarationName Name,
894 TemplateParameterList *Params,
895 Expr *ConstraintExpr) {
896 AdoptTemplateParameterList(Params, DC);
897 return new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr);
898}
899
900ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C,
901 unsigned ID) {
902 ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(),
903 DeclarationName(),
904 nullptr, nullptr);
905
906 return Result;
907}
908
909//===----------------------------------------------------------------------===//
Douglas Gregor2373c592009-05-31 09:31:02 +0000910// ClassTemplatePartialSpecializationDecl Implementation
911//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +0000912void ClassTemplatePartialSpecializationDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000913
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000914ClassTemplatePartialSpecializationDecl::
915ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000916 DeclContext *DC,
917 SourceLocation StartLoc,
918 SourceLocation IdLoc,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000919 TemplateParameterList *Params,
920 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000921 ArrayRef<TemplateArgument> Args,
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000922 const ASTTemplateArgumentListInfo *ArgInfos,
Richard Smithb2f61b42013-08-22 23:27:37 +0000923 ClassTemplatePartialSpecializationDecl *PrevDecl)
Eugene Zelenko421e8902017-11-29 22:39:22 +0000924 : ClassTemplateSpecializationDecl(Context,
925 ClassTemplatePartialSpecialization,
926 TK, DC, StartLoc, IdLoc,
927 SpecializedTemplate, Args, PrevDecl),
928 TemplateParams(Params), ArgsAsWritten(ArgInfos),
929 InstantiatedFromMember(nullptr, false) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000930 AdoptTemplateParameterList(Params, this);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000931}
932
Douglas Gregor2373c592009-05-31 09:31:02 +0000933ClassTemplatePartialSpecializationDecl *
934ClassTemplatePartialSpecializationDecl::
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000935Create(ASTContext &Context, TagKind TK,DeclContext *DC,
936 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregor2373c592009-05-31 09:31:02 +0000937 TemplateParameterList *Params,
938 ClassTemplateDecl *SpecializedTemplate,
David Majnemer8b622692016-07-03 21:17:51 +0000939 ArrayRef<TemplateArgument> Args,
John McCall6b51f282009-11-23 01:53:49 +0000940 const TemplateArgumentListInfo &ArgInfos,
John McCalle78aac42010-03-10 03:28:59 +0000941 QualType CanonInjectedType,
Richard Smithb2f61b42013-08-22 23:27:37 +0000942 ClassTemplatePartialSpecializationDecl *PrevDecl) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000943 const ASTTemplateArgumentListInfo *ASTArgInfos =
944 ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
John McCall0ad16662009-10-29 08:12:44 +0000945
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000946 auto *Result = new (Context, DC)
Richard Smithf7981722013-11-22 09:01:48 +0000947 ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
948 Params, SpecializedTemplate, Args,
David Majnemer8b622692016-07-03 21:17:51 +0000949 ASTArgInfos, PrevDecl);
Douglas Gregor2373c592009-05-31 09:31:02 +0000950 Result->setSpecializationKind(TSK_ExplicitSpecialization);
Erich Keanef92f31c2018-08-01 20:48:16 +0000951 Result->setMayHaveOutOfDateDef(false);
John McCalle78aac42010-03-10 03:28:59 +0000952
953 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregor2373c592009-05-31 09:31:02 +0000954 return Result;
955}
John McCall11083da2009-09-16 22:47:08 +0000956
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000957ClassTemplatePartialSpecializationDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000958ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
959 unsigned ID) {
Eugene Zelenko309e29d2018-03-29 20:51:59 +0000960 auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C);
Erich Keanef92f31c2018-08-01 20:48:16 +0000961 Result->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000962 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000963}
964
John McCall11083da2009-09-16 22:47:08 +0000965//===----------------------------------------------------------------------===//
966// FriendTemplateDecl Implementation
967//===----------------------------------------------------------------------===//
968
Eugene Zelenko421e8902017-11-29 22:39:22 +0000969void FriendTemplateDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +0000970
David Majnemerdfecf1a2016-07-06 04:19:16 +0000971FriendTemplateDecl *
972FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
973 SourceLocation L,
974 MutableArrayRef<TemplateParameterList *> Params,
975 FriendUnion Friend, SourceLocation FLoc) {
976 return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc);
John McCall11083da2009-09-16 22:47:08 +0000977}
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000978
Douglas Gregor72172e92012-01-05 21:55:30 +0000979FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
980 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000981 return new (C, ID) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000982}
Richard Smith3f1b5d02011-05-05 21:57:07 +0000983
984//===----------------------------------------------------------------------===//
985// TypeAliasTemplateDecl Implementation
986//===----------------------------------------------------------------------===//
987
988TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
989 DeclContext *DC,
990 SourceLocation L,
991 DeclarationName Name,
992 TemplateParameterList *Params,
993 NamedDecl *Decl) {
994 AdoptTemplateParameterList(Params, DC);
Richard Smith053f6c62014-05-16 23:01:30 +0000995 return new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000996}
997
Douglas Gregor72172e92012-01-05 21:55:30 +0000998TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
999 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001000 return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001001 DeclarationName(), nullptr, nullptr);
Richard Smith3f1b5d02011-05-05 21:57:07 +00001002}
1003
Richard Smith3f1b5d02011-05-05 21:57:07 +00001004RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +00001005TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001006 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +00001007 C.addDestruction(CommonPtr);
Richard Smith3f1b5d02011-05-05 21:57:07 +00001008 return CommonPtr;
1009}
1010
David Blaikie68e081d2011-12-20 02:48:34 +00001011//===----------------------------------------------------------------------===//
1012// ClassScopeFunctionSpecializationDecl Implementation
1013//===----------------------------------------------------------------------===//
1014
Eugene Zelenko421e8902017-11-29 22:39:22 +00001015void ClassScopeFunctionSpecializationDecl::anchor() {}
Douglas Gregor72172e92012-01-05 21:55:30 +00001016
1017ClassScopeFunctionSpecializationDecl *
1018ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
1019 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001020 return new (C, ID) ClassScopeFunctionSpecializationDecl(
Richard Smithf19a8b02019-05-02 00:49:14 +00001021 nullptr, SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001022}
Larisse Voufo39a1e502013-08-06 01:03:05 +00001023
1024//===----------------------------------------------------------------------===//
1025// VarTemplateDecl Implementation
1026//===----------------------------------------------------------------------===//
1027
Larisse Voufoa11bd8a2013-08-13 02:02:26 +00001028VarTemplateDecl *VarTemplateDecl::getDefinition() {
1029 VarTemplateDecl *CurD = this;
1030 while (CurD) {
1031 if (CurD->isThisDeclarationADefinition())
1032 return CurD;
1033 CurD = CurD->getPreviousDecl();
1034 }
Craig Topper36250ad2014-05-12 05:36:57 +00001035 return nullptr;
Larisse Voufoa11bd8a2013-08-13 02:02:26 +00001036}
1037
Larisse Voufo39a1e502013-08-06 01:03:05 +00001038VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
1039 SourceLocation L, DeclarationName Name,
1040 TemplateParameterList *Params,
Richard Smithbeef3452014-01-16 23:39:20 +00001041 VarDecl *Decl) {
David Blaikie07489f92019-04-19 23:04:05 +00001042 AdoptTemplateParameterList(Params, DC);
Richard Smith053f6c62014-05-16 23:01:30 +00001043 return new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001044}
1045
1046VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
1047 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001048 return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
1049 DeclarationName(), nullptr, nullptr);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001050}
1051
Larisse Voufo39a1e502013-08-06 01:03:05 +00001052void VarTemplateDecl::LoadLazySpecializations() const {
Vassil Vassilev61f64292017-12-14 23:30:18 +00001053 loadLazySpecializationsImpl();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001054}
1055
1056llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
1057VarTemplateDecl::getSpecializations() const {
1058 LoadLazySpecializations();
1059 return getCommonPtr()->Specializations;
1060}
1061
1062llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
1063VarTemplateDecl::getPartialSpecializations() {
1064 LoadLazySpecializations();
1065 return getCommonPtr()->PartialSpecializations;
1066}
1067
1068RedeclarableTemplateDecl::CommonBase *
1069VarTemplateDecl::newCommon(ASTContext &C) const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001070 auto *CommonPtr = new (C) Common;
George Burgess IVb61bfbd2017-02-14 05:37:36 +00001071 C.addDestruction(CommonPtr);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001072 return CommonPtr;
1073}
1074
1075VarTemplateSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +00001076VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
1077 void *&InsertPos) {
Saar Razdf061c32019-12-23 08:37:35 +02001078 return findSpecializationImpl(getSpecializations(), InsertPos, Args);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001079}
1080
1081void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
1082 void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +00001083 addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001084}
1085
1086VarTemplatePartialSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +00001087VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
Saar Razdf061c32019-12-23 08:37:35 +02001088 TemplateParameterList *TPL, void *&InsertPos) {
1089 return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
1090 TPL);
1091}
1092
1093void
1094VarTemplatePartialSpecializationDecl::Profile(llvm::FoldingSetNodeID &ID,
1095 ArrayRef<TemplateArgument> TemplateArgs, TemplateParameterList *TPL,
1096 ASTContext &Context) {
1097 ID.AddInteger(TemplateArgs.size());
1098 for (const TemplateArgument &TemplateArg : TemplateArgs)
1099 TemplateArg.Profile(ID, Context);
1100 ProfileTemplateParameterList(Context, ID, TPL);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001101}
1102
1103void VarTemplateDecl::AddPartialSpecialization(
1104 VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
1105 if (InsertPos)
1106 getPartialSpecializations().InsertNode(D, InsertPos);
1107 else {
1108 VarTemplatePartialSpecializationDecl *Existing =
1109 getPartialSpecializations().GetOrInsertNode(D);
1110 (void)Existing;
1111 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
1112 }
1113
1114 if (ASTMutationListener *L = getASTMutationListener())
1115 L->AddedCXXTemplateSpecialization(this, D);
1116}
1117
1118void VarTemplateDecl::getPartialSpecializations(
1119 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) {
1120 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
1121 getPartialSpecializations();
1122 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +00001123 PS.reserve(PartialSpecs.size());
David Majnemerdfecf1a2016-07-06 04:19:16 +00001124 for (VarTemplatePartialSpecializationDecl &P : PartialSpecs)
1125 PS.push_back(P.getMostRecentDecl());
Larisse Voufo39a1e502013-08-06 01:03:05 +00001126}
1127
1128VarTemplatePartialSpecializationDecl *
1129VarTemplateDecl::findPartialSpecInstantiatedFromMember(
1130 VarTemplatePartialSpecializationDecl *D) {
1131 Decl *DCanon = D->getCanonicalDecl();
David Majnemerdfecf1a2016-07-06 04:19:16 +00001132 for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
1133 if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
1134 return P.getMostRecentDecl();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001135 }
1136
Craig Topper36250ad2014-05-12 05:36:57 +00001137 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00001138}
1139
1140//===----------------------------------------------------------------------===//
1141// VarTemplateSpecializationDecl Implementation
1142//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +00001143
Larisse Voufo39a1e502013-08-06 01:03:05 +00001144VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001145 Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001146 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
David Majnemer8b622692016-07-03 21:17:51 +00001147 TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args)
Richard Smith053f6c62014-05-16 23:01:30 +00001148 : VarDecl(DK, Context, DC, StartLoc, IdLoc,
1149 SpecializedTemplate->getIdentifier(), T, TInfo, S),
Eugene Zelenko421e8902017-11-29 22:39:22 +00001150 SpecializedTemplate(SpecializedTemplate),
David Majnemer8b622692016-07-03 21:17:51 +00001151 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
Richard Smith435e6472017-12-02 02:48:42 +00001152 SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +00001153
Richard Smith053f6c62014-05-16 23:01:30 +00001154VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
1155 ASTContext &C)
1156 : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001157 QualType(), nullptr, SC_None),
Richard Smith435e6472017-12-02 02:48:42 +00001158 SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +00001159
1160VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
1161 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1162 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
David Majnemer8b622692016-07-03 21:17:51 +00001163 TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) {
Richard Smithf7981722013-11-22 09:01:48 +00001164 return new (Context, DC) VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001165 VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
David Majnemer8b622692016-07-03 21:17:51 +00001166 SpecializedTemplate, T, TInfo, S, Args);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001167}
1168
1169VarTemplateSpecializationDecl *
1170VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001171 return new (C, ID)
1172 VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001173}
1174
1175void VarTemplateSpecializationDecl::getNameForDiagnostic(
1176 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
1177 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
1178
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001179 const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this);
Richard Smith792c22d2016-12-24 04:09:05 +00001180 if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
1181 PS ? PS->getTemplateArgsAsWritten() : nullptr) {
Serge Pavlov03e672c2017-11-28 16:14:14 +00001182 printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +00001183 } else {
1184 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
Serge Pavlov03e672c2017-11-28 16:14:14 +00001185 printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
Richard Smith792c22d2016-12-24 04:09:05 +00001186 }
Larisse Voufo39a1e502013-08-06 01:03:05 +00001187}
1188
1189VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001190 if (const auto *PartialSpec =
Larisse Voufo39a1e502013-08-06 01:03:05 +00001191 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1192 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
1193 return SpecializedTemplate.get<VarTemplateDecl *>();
1194}
1195
1196void VarTemplateSpecializationDecl::setTemplateArgsInfo(
1197 const TemplateArgumentListInfo &ArgsInfo) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001198 TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
1199 TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
David Majnemerdfecf1a2016-07-06 04:19:16 +00001200 for (const TemplateArgumentLoc &Loc : ArgsInfo.arguments())
1201 TemplateArgsInfo.addArgument(Loc);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001202}
1203
1204//===----------------------------------------------------------------------===//
1205// VarTemplatePartialSpecializationDecl Implementation
1206//===----------------------------------------------------------------------===//
Eugene Zelenko421e8902017-11-29 22:39:22 +00001207
Larisse Voufo39a1e502013-08-06 01:03:05 +00001208void VarTemplatePartialSpecializationDecl::anchor() {}
1209
1210VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
1211 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1212 SourceLocation IdLoc, TemplateParameterList *Params,
1213 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00001214 StorageClass S, ArrayRef<TemplateArgument> Args,
Richard Smithb2f61b42013-08-22 23:27:37 +00001215 const ASTTemplateArgumentListInfo *ArgInfos)
Richard Smith053f6c62014-05-16 23:01:30 +00001216 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001217 DC, StartLoc, IdLoc, SpecializedTemplate, T,
David Majnemer8b622692016-07-03 21:17:51 +00001218 TInfo, S, Args),
Larisse Voufo39a1e502013-08-06 01:03:05 +00001219 TemplateParams(Params), ArgsAsWritten(ArgInfos),
Craig Topper36250ad2014-05-12 05:36:57 +00001220 InstantiatedFromMember(nullptr, false) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001221 // TODO: The template parameters should be in DC by now. Verify.
1222 // AdoptTemplateParameterList(Params, DC);
1223}
1224
1225VarTemplatePartialSpecializationDecl *
1226VarTemplatePartialSpecializationDecl::Create(
1227 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1228 SourceLocation IdLoc, TemplateParameterList *Params,
1229 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00001230 StorageClass S, ArrayRef<TemplateArgument> Args,
Richard Smithb2f61b42013-08-22 23:27:37 +00001231 const TemplateArgumentListInfo &ArgInfos) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00001232 const ASTTemplateArgumentListInfo *ASTArgInfos
1233 = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001234
Eugene Zelenko309e29d2018-03-29 20:51:59 +00001235 auto *Result =
Richard Smithf7981722013-11-22 09:01:48 +00001236 new (Context, DC) VarTemplatePartialSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00001237 Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
David Majnemer8b622692016-07-03 21:17:51 +00001238 S, Args, ASTArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001239 Result->setSpecializationKind(TSK_ExplicitSpecialization);
1240 return Result;
1241}
1242
1243VarTemplatePartialSpecializationDecl *
1244VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
1245 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001246 return new (C, ID) VarTemplatePartialSpecializationDecl(C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001247}
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001248
1249static TemplateParameterList *
1250createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
1251 // typename T
1252 auto *T = TemplateTypeParmDecl::Create(
1253 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
1254 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
1255 T->setImplicit(true);
1256
1257 // T ...Ints
1258 TypeSourceInfo *TI =
1259 C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
1260 auto *N = NonTypeTemplateParmDecl::Create(
1261 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1262 /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
1263 N->setImplicit(true);
1264
1265 // <typename T, T ...Ints>
1266 NamedDecl *P[2] = {T, N};
1267 auto *TPL = TemplateParameterList::Create(
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001268 C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001269
1270 // template <typename T, ...Ints> class IntSeq
1271 auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
1272 C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
1273 /*ParameterPack=*/false, /*Id=*/nullptr, TPL);
1274 TemplateTemplateParm->setImplicit(true);
1275
1276 // typename T
1277 auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
1278 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1279 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
1280 TemplateTypeParm->setImplicit(true);
1281
1282 // T N
1283 TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
1284 QualType(TemplateTypeParm->getTypeForDecl(), 0));
1285 auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
1286 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
1287 /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1288 NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
1289 NonTypeTemplateParm};
1290
1291 // template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
1292 return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001293 Params, SourceLocation(), nullptr);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001294}
1295
Eric Fiselier6ad68552016-07-01 01:24:09 +00001296static TemplateParameterList *
1297createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
1298 // std::size_t Index
1299 TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
1300 auto *Index = NonTypeTemplateParmDecl::Create(
1301 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
1302 /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1303
1304 // typename ...T
1305 auto *Ts = TemplateTypeParmDecl::Create(
1306 C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1307 /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true);
1308 Ts->setImplicit(true);
1309
1310 // template <std::size_t Index, typename ...T>
1311 NamedDecl *Params[] = {Index, Ts};
1312 return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1313 llvm::makeArrayRef(Params),
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00001314 SourceLocation(), nullptr);
Eric Fiselier6ad68552016-07-01 01:24:09 +00001315}
1316
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001317static TemplateParameterList *createBuiltinTemplateParameterList(
1318 const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
1319 switch (BTK) {
1320 case BTK__make_integer_seq:
1321 return createMakeIntegerSeqParameterList(C, DC);
Eric Fiselier6ad68552016-07-01 01:24:09 +00001322 case BTK__type_pack_element:
1323 return createTypePackElementParameterList(C, DC);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00001324 }
1325
1326 llvm_unreachable("unhandled BuiltinTemplateKind!");
1327}
1328
1329void BuiltinTemplateDecl::anchor() {}
1330
1331BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
1332 DeclarationName Name,
1333 BuiltinTemplateKind BTK)
1334 : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name,
1335 createBuiltinTemplateParameterList(C, DC, BTK)),
1336 BTK(BTK) {}