blob: 0d1d2a4613dc848198d67677f59269da4abc5504 [file] [log] [blame]
Sebastian Redle2530ec2009-10-23 22:13:42 +00001//===--- DeclTemplate.cpp - Template Declaration AST Node Implementation --===//
Douglas Gregorded2d7b2009-02-04 19:02:06 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes for templates.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregorded2d7b2009-02-04 19:02:06 +000014#include "clang/AST/DeclTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/DeclCXX.h"
Douglas Gregor8bf42052009-02-09 18:46:07 +000018#include "clang/AST/Expr.h"
Douglas Gregor85759112011-01-04 02:33:52 +000019#include "clang/AST/ExprCXX.h"
John McCall0ad16662009-10-29 08:12:44 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000021#include "clang/Basic/IdentifierTable.h"
22#include "llvm/ADT/STLExtras.h"
Douglas Gregor1ccc8412010-11-07 23:05:16 +000023#include <memory>
Douglas Gregorded2d7b2009-02-04 19:02:06 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// TemplateParameterList Implementation
28//===----------------------------------------------------------------------===//
29
Douglas Gregorcd72ba92009-02-06 22:42:48 +000030TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
31 SourceLocation LAngleLoc,
Douglas Gregorbe999392009-09-15 16:23:51 +000032 NamedDecl **Params, unsigned NumParams,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000033 SourceLocation RAngleLoc)
34 : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
Richard Smith1fde8ec2012-09-07 02:06:42 +000035 NumParams(NumParams), ContainsUnexpandedParameterPack(false) {
36 assert(this->NumParams == NumParams && "Too many template parameters");
37 for (unsigned Idx = 0; Idx < NumParams; ++Idx) {
38 NamedDecl *P = Params[Idx];
39 begin()[Idx] = P;
40
41 if (!P->isTemplateParameterPack()) {
42 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P))
43 if (NTTP->getType()->containsUnexpandedParameterPack())
44 ContainsUnexpandedParameterPack = true;
45
46 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
47 if (TTP->getTemplateParameters()->containsUnexpandedParameterPack())
48 ContainsUnexpandedParameterPack = true;
49
50 // FIXME: If a default argument contains an unexpanded parameter pack, the
51 // template parameter list does too.
52 }
53 }
Douglas Gregorded2d7b2009-02-04 19:02:06 +000054}
55
56TemplateParameterList *
Jay Foad39c79802011-01-12 09:06:06 +000057TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
Douglas Gregorbe999392009-09-15 16:23:51 +000058 SourceLocation LAngleLoc, NamedDecl **Params,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000059 unsigned NumParams, SourceLocation RAngleLoc) {
Douglas Gregorbe999392009-09-15 16:23:51 +000060 unsigned Size = sizeof(TemplateParameterList)
61 + sizeof(NamedDecl *) * NumParams;
Richard Smith426f7852012-08-16 22:51:34 +000062 unsigned Align = std::max(llvm::alignOf<TemplateParameterList>(),
63 llvm::alignOf<NamedDecl*>());
Douglas Gregorded2d7b2009-02-04 19:02:06 +000064 void *Mem = C.Allocate(Size, Align);
Mike Stump11289f42009-09-09 15:08:12 +000065 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000066 NumParams, RAngleLoc);
Douglas Gregorded2d7b2009-02-04 19:02:06 +000067}
68
Douglas Gregorf8f86832009-02-11 18:16:40 +000069unsigned TemplateParameterList::getMinRequiredArguments() const {
Douglas Gregor0231d8d2011-01-19 20:10:05 +000070 unsigned NumRequiredArgs = 0;
71 for (iterator P = const_cast<TemplateParameterList *>(this)->begin(),
72 PEnd = const_cast<TemplateParameterList *>(this)->end();
73 P != PEnd; ++P) {
74 if ((*P)->isTemplateParameterPack()) {
75 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
76 if (NTTP->isExpandedParameterPack()) {
77 NumRequiredArgs += NTTP->getNumExpansionTypes();
78 continue;
79 }
80
Douglas Gregorf8f86832009-02-11 18:16:40 +000081 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +000082 }
83
84 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
85 if (TTP->hasDefaultArgument())
86 break;
87 } else if (NonTypeTemplateParmDecl *NTTP
88 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
89 if (NTTP->hasDefaultArgument())
90 break;
91 } else if (cast<TemplateTemplateParmDecl>(*P)->hasDefaultArgument())
92 break;
93
94 ++NumRequiredArgs;
Douglas Gregorf8f86832009-02-11 18:16:40 +000095 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +000096
Douglas Gregorf8f86832009-02-11 18:16:40 +000097 return NumRequiredArgs;
98}
99
Douglas Gregor21610382009-10-29 00:04:11 +0000100unsigned TemplateParameterList::getDepth() const {
101 if (size() == 0)
102 return 0;
103
104 const NamedDecl *FirstParm = getParam(0);
105 if (const TemplateTypeParmDecl *TTP
106 = dyn_cast<TemplateTypeParmDecl>(FirstParm))
107 return TTP->getDepth();
108 else if (const NonTypeTemplateParmDecl *NTTP
109 = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
110 return NTTP->getDepth();
111 else
112 return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
113}
114
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000115static void AdoptTemplateParameterList(TemplateParameterList *Params,
116 DeclContext *Owner) {
117 for (TemplateParameterList::iterator P = Params->begin(),
118 PEnd = Params->end();
119 P != PEnd; ++P) {
120 (*P)->setDeclContext(Owner);
121
122 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*P))
123 AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner);
124 }
125}
126
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000127//===----------------------------------------------------------------------===//
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000128// RedeclarableTemplateDecl Implementation
129//===----------------------------------------------------------------------===//
130
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000131RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000132 if (Common)
133 return Common;
134
135 // Walk the previous-declaration chain until we either find a declaration
136 // with a common pointer or we run out of previous declarations.
137 SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
138 for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
139 Prev = Prev->getPreviousDecl()) {
140 if (Prev->Common) {
141 Common = Prev->Common;
142 break;
Douglas Gregor68444de2012-01-14 15:13:49 +0000143 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000144
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000145 PrevDecls.push_back(Prev);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000146 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000147
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000148 // If we never found a common pointer, allocate one now.
149 if (!Common) {
150 // FIXME: If any of the declarations is from an AST file, we probably
151 // need an update record to add the common data.
152
153 Common = newCommon(getASTContext());
154 }
155
156 // Update any previous declarations we saw with the common pointer.
157 for (unsigned I = 0, N = PrevDecls.size(); I != N; ++I)
158 PrevDecls[I]->Common = Common;
159
Douglas Gregor68444de2012-01-14 15:13:49 +0000160 return Common;
Peter Collingbourne029fd692010-07-29 16:12:09 +0000161}
162
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000163template <class EntryType>
164typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType*
165RedeclarableTemplateDecl::findSpecializationImpl(
Chandler Carruthb41171b2012-05-03 23:49:05 +0000166 llvm::FoldingSetVector<EntryType> &Specs,
Craig Topper7e0daca2014-06-26 04:58:53 +0000167 ArrayRef<TemplateArgument> Args,
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000168 void *&InsertPos) {
169 typedef SpecEntryTraits<EntryType> SETraits;
170 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000171 EntryType::Profile(ID,Args, getASTContext());
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000172 EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
Craig Topper36250ad2014-05-12 05:36:57 +0000173 return Entry ? SETraits::getMostRecentDecl(Entry) : nullptr;
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000174}
175
Douglas Gregor43669f82011-03-05 17:54:25 +0000176/// \brief Generate the injected template arguments for the given template
177/// parameter list, e.g., for the injected-class-name of a class template.
178static void GenerateInjectedTemplateArgs(ASTContext &Context,
179 TemplateParameterList *Params,
180 TemplateArgument *Args) {
181 for (TemplateParameterList::iterator Param = Params->begin(),
182 ParamEnd = Params->end();
183 Param != ParamEnd; ++Param) {
184 TemplateArgument Arg;
185 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
186 QualType ArgType = Context.getTypeDeclType(TTP);
187 if (TTP->isParameterPack())
David Blaikie7a30dc52013-02-21 01:47:18 +0000188 ArgType = Context.getPackExpansionType(ArgType, None);
David Blaikie05785d12013-02-20 22:23:23 +0000189
Douglas Gregor43669f82011-03-05 17:54:25 +0000190 Arg = TemplateArgument(ArgType);
191 } else if (NonTypeTemplateParmDecl *NTTP =
192 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
John McCall113bee02012-03-10 09:33:50 +0000193 Expr *E = new (Context) DeclRefExpr(NTTP, /*enclosing*/ false,
Douglas Gregor43669f82011-03-05 17:54:25 +0000194 NTTP->getType().getNonLValueExprType(Context),
195 Expr::getValueKindForType(NTTP->getType()),
196 NTTP->getLocation());
197
198 if (NTTP->isParameterPack())
David Blaikie7a30dc52013-02-21 01:47:18 +0000199 E = new (Context) PackExpansionExpr(Context.DependentTy, E,
200 NTTP->getLocation(), None);
Douglas Gregor43669f82011-03-05 17:54:25 +0000201 Arg = TemplateArgument(E);
202 } else {
203 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
204 if (TTP->isParameterPack())
David Blaikie05785d12013-02-20 22:23:23 +0000205 Arg = TemplateArgument(TemplateName(TTP), Optional<unsigned>());
Douglas Gregor43669f82011-03-05 17:54:25 +0000206 else
207 Arg = TemplateArgument(TemplateName(TTP));
208 }
209
210 if ((*Param)->isTemplateParameterPack())
211 Arg = TemplateArgument::CreatePackCopy(Context, &Arg, 1);
212
213 *Args++ = Arg;
214 }
215}
216
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000217//===----------------------------------------------------------------------===//
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000218// FunctionTemplateDecl Implementation
219//===----------------------------------------------------------------------===//
220
Douglas Gregor1a809332010-05-23 18:26:36 +0000221void FunctionTemplateDecl::DeallocateCommon(void *Ptr) {
222 static_cast<Common *>(Ptr)->~Common();
223}
224
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000225FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
226 DeclContext *DC,
227 SourceLocation L,
228 DeclarationName Name,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000229 TemplateParameterList *Params,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000230 NamedDecl *Decl) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000231 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Richard Smith053f6c62014-05-16 23:01:30 +0000232 return new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000233}
234
Douglas Gregor72172e92012-01-05 21:55:30 +0000235FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
236 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000237 return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000238 DeclarationName(), nullptr, nullptr);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000239}
240
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000241RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000242FunctionTemplateDecl::newCommon(ASTContext &C) const {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000243 Common *CommonPtr = new (C) Common;
244 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000245 return CommonPtr;
246}
247
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000248void FunctionTemplateDecl::LoadLazySpecializations() const {
249 Common *CommonPtr = getCommonPtr();
250 if (CommonPtr->LazySpecializations) {
251 ASTContext &Context = getASTContext();
252 uint32_t *Specs = CommonPtr->LazySpecializations;
Craig Topper36250ad2014-05-12 05:36:57 +0000253 CommonPtr->LazySpecializations = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000254 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
255 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
256 }
257}
258
259llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
260FunctionTemplateDecl::getSpecializations() const {
261 LoadLazySpecializations();
262 return getCommonPtr()->Specializations;
263}
264
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000265FunctionDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000266FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
267 void *&InsertPos) {
268 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000269}
270
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000271void FunctionTemplateDecl::addSpecialization(
272 FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
Douglas Gregorce9978f2012-03-28 14:34:23 +0000273 if (InsertPos)
274 getSpecializations().InsertNode(Info, InsertPos);
275 else
276 getSpecializations().GetOrInsertNode(Info);
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000277 if (ASTMutationListener *L = getASTMutationListener())
278 L->AddedCXXTemplateSpecialization(this, Info->Function);
279}
280
Richard Smith841d8b22013-05-17 03:04:50 +0000281ArrayRef<TemplateArgument> FunctionTemplateDecl::getInjectedTemplateArgs() {
Douglas Gregor43669f82011-03-05 17:54:25 +0000282 TemplateParameterList *Params = getTemplateParameters();
283 Common *CommonPtr = getCommonPtr();
284 if (!CommonPtr->InjectedArgs) {
285 CommonPtr->InjectedArgs
Richard Smith841d8b22013-05-17 03:04:50 +0000286 = new (getASTContext()) TemplateArgument[Params->size()];
287 GenerateInjectedTemplateArgs(getASTContext(), Params,
Douglas Gregor43669f82011-03-05 17:54:25 +0000288 CommonPtr->InjectedArgs);
289 }
Richard Smith841d8b22013-05-17 03:04:50 +0000290
291 return llvm::makeArrayRef(CommonPtr->InjectedArgs, Params->size());
Douglas Gregor43669f82011-03-05 17:54:25 +0000292}
293
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000294//===----------------------------------------------------------------------===//
295// ClassTemplateDecl Implementation
296//===----------------------------------------------------------------------===//
297
Douglas Gregor1a809332010-05-23 18:26:36 +0000298void ClassTemplateDecl::DeallocateCommon(void *Ptr) {
299 static_cast<Common *>(Ptr)->~Common();
300}
301
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000302ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
303 DeclContext *DC,
304 SourceLocation L,
305 DeclarationName Name,
306 TemplateParameterList *Params,
Douglas Gregor90a1a652009-03-19 17:26:29 +0000307 NamedDecl *Decl,
308 ClassTemplateDecl *PrevDecl) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000309 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Richard Smith053f6c62014-05-16 23:01:30 +0000310 ClassTemplateDecl *New = new (C, DC) ClassTemplateDecl(C, DC, L, Name,
311 Params, Decl);
Rafael Espindola8db352d2013-10-17 15:37:26 +0000312 New->setPreviousDecl(PrevDecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000313 return New;
Douglas Gregor90a1a652009-03-19 17:26:29 +0000314}
315
Richard Smith053f6c62014-05-16 23:01:30 +0000316ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000317 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000318 return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
319 DeclarationName(), nullptr, nullptr);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000320}
321
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000322void ClassTemplateDecl::LoadLazySpecializations() const {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000323 Common *CommonPtr = getCommonPtr();
324 if (CommonPtr->LazySpecializations) {
325 ASTContext &Context = getASTContext();
326 uint32_t *Specs = CommonPtr->LazySpecializations;
Craig Topper36250ad2014-05-12 05:36:57 +0000327 CommonPtr->LazySpecializations = nullptr;
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000328 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
329 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
330 }
331}
332
Chandler Carruthb41171b2012-05-03 23:49:05 +0000333llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000334ClassTemplateDecl::getSpecializations() const {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000335 LoadLazySpecializations();
336 return getCommonPtr()->Specializations;
337}
338
Chandler Carruthb41171b2012-05-03 23:49:05 +0000339llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000340ClassTemplateDecl::getPartialSpecializations() {
341 LoadLazySpecializations();
342 return getCommonPtr()->PartialSpecializations;
343}
344
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000345RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000346ClassTemplateDecl::newCommon(ASTContext &C) const {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000347 Common *CommonPtr = new (C) Common;
348 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000349 return CommonPtr;
350}
351
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000352ClassTemplateSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000353ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
354 void *&InsertPos) {
355 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000356}
357
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000358void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
359 void *InsertPos) {
Douglas Gregorce9978f2012-03-28 14:34:23 +0000360 if (InsertPos)
361 getSpecializations().InsertNode(D, InsertPos);
362 else {
363 ClassTemplateSpecializationDecl *Existing
364 = getSpecializations().GetOrInsertNode(D);
365 (void)Existing;
366 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
367 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000368 if (ASTMutationListener *L = getASTMutationListener())
369 L->AddedCXXTemplateSpecialization(this, D);
370}
371
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000372ClassTemplatePartialSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000373ClassTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000374 void *&InsertPos) {
Craig Topper7e0daca2014-06-26 04:58:53 +0000375 return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000376}
377
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000378void ClassTemplateDecl::AddPartialSpecialization(
379 ClassTemplatePartialSpecializationDecl *D,
380 void *InsertPos) {
Douglas Gregorce9978f2012-03-28 14:34:23 +0000381 if (InsertPos)
382 getPartialSpecializations().InsertNode(D, InsertPos);
383 else {
384 ClassTemplatePartialSpecializationDecl *Existing
385 = getPartialSpecializations().GetOrInsertNode(D);
386 (void)Existing;
387 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
388 }
389
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000390 if (ASTMutationListener *L = getASTMutationListener())
391 L->AddedCXXTemplateSpecialization(this, D);
392}
393
Douglas Gregor407e9612010-04-30 05:56:50 +0000394void ClassTemplateDecl::getPartialSpecializations(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000395 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
Chandler Carruthb41171b2012-05-03 23:49:05 +0000396 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000397 = getPartialSpecializations();
Douglas Gregor407e9612010-04-30 05:56:50 +0000398 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +0000399 PS.reserve(PartialSpecs.size());
Chandler Carruthb41171b2012-05-03 23:49:05 +0000400 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor407e9612010-04-30 05:56:50 +0000401 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
Richard Smithb2f61b42013-08-22 23:27:37 +0000402 P != PEnd; ++P)
403 PS.push_back(P->getMostRecentDecl());
Douglas Gregor407e9612010-04-30 05:56:50 +0000404}
405
Douglas Gregor15301382009-07-30 17:40:51 +0000406ClassTemplatePartialSpecializationDecl *
407ClassTemplateDecl::findPartialSpecialization(QualType T) {
408 ASTContext &Context = getASTContext();
Chandler Carruthb41171b2012-05-03 23:49:05 +0000409 using llvm::FoldingSetVector;
410 typedef FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor15301382009-07-30 17:40:51 +0000411 partial_spec_iterator;
412 for (partial_spec_iterator P = getPartialSpecializations().begin(),
413 PEnd = getPartialSpecializations().end();
414 P != PEnd; ++P) {
John McCall2408e322010-04-27 00:57:59 +0000415 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregorec9fd132012-01-14 16:38:05 +0000416 return P->getMostRecentDecl();
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000417 }
418
Craig Topper36250ad2014-05-12 05:36:57 +0000419 return nullptr;
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000420}
421
422ClassTemplatePartialSpecializationDecl *
423ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
424 ClassTemplatePartialSpecializationDecl *D) {
425 Decl *DCanon = D->getCanonicalDecl();
Chandler Carruthb41171b2012-05-03 23:49:05 +0000426 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000427 P = getPartialSpecializations().begin(),
428 PEnd = getPartialSpecializations().end();
429 P != PEnd; ++P) {
430 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
Douglas Gregorec9fd132012-01-14 16:38:05 +0000431 return P->getMostRecentDecl();
Douglas Gregor15301382009-07-30 17:40:51 +0000432 }
Mike Stump11289f42009-09-09 15:08:12 +0000433
Craig Topper36250ad2014-05-12 05:36:57 +0000434 return nullptr;
Douglas Gregor15301382009-07-30 17:40:51 +0000435}
436
John McCalle78aac42010-03-10 03:28:59 +0000437QualType
Douglas Gregor9961ce92010-07-08 18:37:38 +0000438ClassTemplateDecl::getInjectedClassNameSpecialization() {
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000439 Common *CommonPtr = getCommonPtr();
Douglas Gregore362cea2009-05-10 22:57:19 +0000440 if (!CommonPtr->InjectedClassNameType.isNull())
441 return CommonPtr->InjectedClassNameType;
442
Douglas Gregor8092e802010-12-23 16:00:30 +0000443 // C++0x [temp.dep.type]p2:
444 // The template argument list of a primary template is a template argument
445 // list in which the nth template argument has the value of the nth template
446 // parameter of the class template. If the nth template parameter is a
447 // template parameter pack (14.5.3), the nth template argument is a pack
448 // expansion (14.5.3) whose pattern is the name of the template parameter
449 // pack.
Douglas Gregor9961ce92010-07-08 18:37:38 +0000450 ASTContext &Context = getASTContext();
Douglas Gregore362cea2009-05-10 22:57:19 +0000451 TemplateParameterList *Params = getTemplateParameters();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000452 SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregor43669f82011-03-05 17:54:25 +0000453 TemplateArgs.resize(Params->size());
454 GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data());
Douglas Gregore362cea2009-05-10 22:57:19 +0000455 CommonPtr->InjectedClassNameType
Douglas Gregora8e02e72009-07-28 23:00:59 +0000456 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregore362cea2009-05-10 22:57:19 +0000457 &TemplateArgs[0],
Douglas Gregora8e02e72009-07-28 23:00:59 +0000458 TemplateArgs.size());
Douglas Gregore362cea2009-05-10 22:57:19 +0000459 return CommonPtr->InjectedClassNameType;
460}
461
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000462//===----------------------------------------------------------------------===//
463// TemplateTypeParm Allocation/Deallocation Method Implementations
464//===----------------------------------------------------------------------===//
465
466TemplateTypeParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000467TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000468 SourceLocation KeyLoc, SourceLocation NameLoc,
469 unsigned D, unsigned P, IdentifierInfo *Id,
470 bool Typename, bool ParameterPack) {
Chandler Carruth08836322011-05-01 00:51:33 +0000471 TemplateTypeParmDecl *TTPDecl =
Richard Smithf7981722013-11-22 09:01:48 +0000472 new (C, DC) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
Chandler Carruth08836322011-05-01 00:51:33 +0000473 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
Richard Smith5b21db82014-04-23 18:20:42 +0000474 TTPDecl->setTypeForDecl(TTPType.getTypePtr());
Chandler Carruth08836322011-05-01 00:51:33 +0000475 return TTPDecl;
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000476}
477
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000478TemplateTypeParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000479TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000480 return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(),
481 SourceLocation(), nullptr, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000482}
483
John McCall0ad16662009-10-29 08:12:44 +0000484SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara23485e02011-03-04 12:42:03 +0000485 return hasDefaultArgument()
486 ? DefaultArgument->getTypeLoc().getBeginLoc()
487 : SourceLocation();
488}
489
490SourceRange TemplateTypeParmDecl::getSourceRange() const {
491 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000492 return SourceRange(getLocStart(),
Abramo Bagnara23485e02011-03-04 12:42:03 +0000493 DefaultArgument->getTypeLoc().getEndLoc());
494 else
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000495 return TypeDecl::getSourceRange();
John McCall0ad16662009-10-29 08:12:44 +0000496}
497
Douglas Gregor21610382009-10-29 00:04:11 +0000498unsigned TemplateTypeParmDecl::getDepth() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000499 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getDepth();
Douglas Gregor21610382009-10-29 00:04:11 +0000500}
501
502unsigned TemplateTypeParmDecl::getIndex() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000503 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getIndex();
Douglas Gregor21610382009-10-29 00:04:11 +0000504}
505
Chandler Carruth08836322011-05-01 00:51:33 +0000506bool TemplateTypeParmDecl::isParameterPack() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000507 return getTypeForDecl()->getAs<TemplateTypeParmType>()->isParameterPack();
Chandler Carruth08836322011-05-01 00:51:33 +0000508}
509
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000510//===----------------------------------------------------------------------===//
511// NonTypeTemplateParmDecl Method Implementations
512//===----------------------------------------------------------------------===//
513
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000514NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000515 SourceLocation StartLoc,
516 SourceLocation IdLoc,
517 unsigned D, unsigned P,
518 IdentifierInfo *Id,
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000519 QualType T,
520 TypeSourceInfo *TInfo,
521 const QualType *ExpandedTypes,
522 unsigned NumExpandedTypes,
523 TypeSourceInfo **ExpandedTInfos)
Abramo Bagnaradff19302011-03-08 08:55:46 +0000524 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000525 TemplateParmPosition(D, P), DefaultArgumentAndInherited(nullptr, false),
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000526 ParameterPack(true), ExpandedParameterPack(true),
527 NumExpandedTypes(NumExpandedTypes)
528{
529 if (ExpandedTypes && ExpandedTInfos) {
530 void **TypesAndInfos = reinterpret_cast<void **>(this + 1);
531 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
532 TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr();
533 TypesAndInfos[2*I + 1] = ExpandedTInfos[I];
534 }
535 }
536}
537
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000538NonTypeTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000539NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000540 SourceLocation StartLoc, SourceLocation IdLoc,
541 unsigned D, unsigned P, IdentifierInfo *Id,
542 QualType T, bool ParameterPack,
543 TypeSourceInfo *TInfo) {
Richard Smithf7981722013-11-22 09:01:48 +0000544 return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
545 T, ParameterPack, TInfo);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000546}
547
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000548NonTypeTemplateParmDecl *
549NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000550 SourceLocation StartLoc, SourceLocation IdLoc,
551 unsigned D, unsigned P,
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000552 IdentifierInfo *Id, QualType T,
553 TypeSourceInfo *TInfo,
554 const QualType *ExpandedTypes,
555 unsigned NumExpandedTypes,
556 TypeSourceInfo **ExpandedTInfos) {
Richard Smithf7981722013-11-22 09:01:48 +0000557 unsigned Extra = NumExpandedTypes * 2 * sizeof(void*);
558 return new (C, DC, Extra) NonTypeTemplateParmDecl(
559 DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
560 ExpandedTypes, NumExpandedTypes, ExpandedTInfos);
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000561}
562
Douglas Gregor72172e92012-01-05 21:55:30 +0000563NonTypeTemplateParmDecl *
564NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000565 return new (C, ID) NonTypeTemplateParmDecl(nullptr, SourceLocation(),
566 SourceLocation(), 0, 0, nullptr,
567 QualType(), false, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000568}
569
570NonTypeTemplateParmDecl *
571NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
572 unsigned NumExpandedTypes) {
Richard Smithf7981722013-11-22 09:01:48 +0000573 unsigned Extra = NumExpandedTypes * 2 * sizeof(void*);
574 return new (C, ID, Extra) NonTypeTemplateParmDecl(
Craig Topper36250ad2014-05-12 05:36:57 +0000575 nullptr, SourceLocation(), SourceLocation(), 0, 0, nullptr, QualType(),
576 nullptr, nullptr, NumExpandedTypes, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000577}
578
John McCallf4cd4f92011-02-09 01:13:10 +0000579SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnarae15d5532011-03-04 11:03:48 +0000580 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraea947882011-03-08 16:41:52 +0000581 return SourceRange(getOuterLocStart(),
582 getDefaultArgument()->getSourceRange().getEnd());
583 return DeclaratorDecl::getSourceRange();
John McCallf4cd4f92011-02-09 01:13:10 +0000584}
585
Douglas Gregordba32632009-02-10 19:49:53 +0000586SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara656e3002010-06-09 09:26:05 +0000587 return hasDefaultArgument()
588 ? getDefaultArgument()->getSourceRange().getBegin()
589 : SourceLocation();
Douglas Gregordba32632009-02-10 19:49:53 +0000590}
591
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000592//===----------------------------------------------------------------------===//
593// TemplateTemplateParmDecl Method Implementations
594//===----------------------------------------------------------------------===//
595
David Blaikie68e081d2011-12-20 02:48:34 +0000596void TemplateTemplateParmDecl::anchor() { }
597
Richard Smith1fde8ec2012-09-07 02:06:42 +0000598TemplateTemplateParmDecl::TemplateTemplateParmDecl(
599 DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
600 IdentifierInfo *Id, TemplateParameterList *Params,
601 unsigned NumExpansions, TemplateParameterList * const *Expansions)
602 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
603 TemplateParmPosition(D, P), DefaultArgument(),
604 DefaultArgumentWasInherited(false), ParameterPack(true),
605 ExpandedParameterPack(true), NumExpandedParams(NumExpansions) {
606 if (Expansions)
607 std::memcpy(reinterpret_cast<void*>(this + 1), Expansions,
608 sizeof(TemplateParameterList*) * NumExpandedParams);
609}
610
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000611TemplateTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000612TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000613 SourceLocation L, unsigned D, unsigned P,
Douglas Gregorf5500772011-01-05 15:48:55 +0000614 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000615 TemplateParameterList *Params) {
Richard Smithf7981722013-11-22 09:01:48 +0000616 return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
617 Params);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000618}
619
Douglas Gregor72172e92012-01-05 21:55:30 +0000620TemplateTemplateParmDecl *
Richard Smith1fde8ec2012-09-07 02:06:42 +0000621TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
622 SourceLocation L, unsigned D, unsigned P,
623 IdentifierInfo *Id,
624 TemplateParameterList *Params,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000625 ArrayRef<TemplateParameterList *> Expansions) {
Richard Smithf7981722013-11-22 09:01:48 +0000626 return new (C, DC, sizeof(TemplateParameterList*) * Expansions.size())
627 TemplateTemplateParmDecl(DC, L, D, P, Id, Params,
628 Expansions.size(), Expansions.data());
Richard Smith1fde8ec2012-09-07 02:06:42 +0000629}
630
631TemplateTemplateParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000632TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000633 return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
634 false, nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000635}
636
Richard Smith1fde8ec2012-09-07 02:06:42 +0000637TemplateTemplateParmDecl *
638TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
639 unsigned NumExpansions) {
Richard Smithf7981722013-11-22 09:01:48 +0000640 return new (C, ID, sizeof(TemplateParameterList*) * NumExpansions)
Craig Topper36250ad2014-05-12 05:36:57 +0000641 TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
642 nullptr, NumExpansions, nullptr);
Richard Smith1fde8ec2012-09-07 02:06:42 +0000643}
644
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000645//===----------------------------------------------------------------------===//
Douglas Gregord002c7b2009-05-11 23:53:27 +0000646// TemplateArgumentList Implementation
647//===----------------------------------------------------------------------===//
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000648TemplateArgumentList *
649TemplateArgumentList::CreateCopy(ASTContext &Context,
650 const TemplateArgument *Args,
651 unsigned NumArgs) {
652 std::size_t Size = sizeof(TemplateArgumentList)
653 + NumArgs * sizeof(TemplateArgument);
654 void *Mem = Context.Allocate(Size);
655 TemplateArgument *StoredArgs
656 = reinterpret_cast<TemplateArgument *>(
657 static_cast<TemplateArgumentList *>(Mem) + 1);
658 std::uninitialized_copy(Args, Args + NumArgs, StoredArgs);
659 return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true);
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000660}
661
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000662FunctionTemplateSpecializationInfo *
663FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD,
664 FunctionTemplateDecl *Template,
665 TemplateSpecializationKind TSK,
666 const TemplateArgumentList *TemplateArgs,
667 const TemplateArgumentListInfo *TemplateArgsAsWritten,
668 SourceLocation POI) {
Craig Topper36250ad2014-05-12 05:36:57 +0000669 const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000670 if (TemplateArgsAsWritten)
671 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
672 *TemplateArgsAsWritten);
673
674 return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK,
675 TemplateArgs,
676 ArgsAsWritten,
677 POI);
678}
679
Douglas Gregord002c7b2009-05-11 23:53:27 +0000680//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000681// TemplateDecl Implementation
682//===----------------------------------------------------------------------===//
683
684void TemplateDecl::anchor() { }
685
686//===----------------------------------------------------------------------===//
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000687// ClassTemplateSpecializationDecl Implementation
688//===----------------------------------------------------------------------===//
689ClassTemplateSpecializationDecl::
Douglas Gregore9029562010-05-06 00:28:52 +0000690ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000691 DeclContext *DC, SourceLocation StartLoc,
692 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000693 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000694 const TemplateArgument *Args,
695 unsigned NumArgs,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000696 ClassTemplateSpecializationDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +0000697 : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000698 SpecializedTemplate->getIdentifier(),
699 PrevDecl),
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000700 SpecializedTemplate(SpecializedTemplate),
Craig Topper36250ad2014-05-12 05:36:57 +0000701 ExplicitInfo(nullptr),
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000702 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
Douglas Gregord002c7b2009-05-11 23:53:27 +0000703 SpecializationKind(TSK_Undeclared) {
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000704}
Mike Stump11289f42009-09-09 15:08:12 +0000705
Richard Smith053f6c62014-05-16 23:01:30 +0000706ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
707 Kind DK)
708 : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
709 SourceLocation(), nullptr, nullptr),
710 ExplicitInfo(nullptr), SpecializationKind(TSK_Undeclared) {}
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000711
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000712ClassTemplateSpecializationDecl *
Douglas Gregore9029562010-05-06 00:28:52 +0000713ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000714 DeclContext *DC,
715 SourceLocation StartLoc,
716 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000717 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000718 const TemplateArgument *Args,
719 unsigned NumArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000720 ClassTemplateSpecializationDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +0000721 ClassTemplateSpecializationDecl *Result =
722 new (Context, DC) ClassTemplateSpecializationDecl(
723 Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
724 SpecializedTemplate, Args, NumArgs, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000725 Result->MayHaveOutOfDateDef = false;
726
Douglas Gregor67a65642009-02-17 23:15:12 +0000727 Context.getTypeDeclType(Result, PrevDecl);
728 return Result;
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000729}
Douglas Gregor2373c592009-05-31 09:31:02 +0000730
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000731ClassTemplateSpecializationDecl *
Richard Smithf7981722013-11-22 09:01:48 +0000732ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000733 unsigned ID) {
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000734 ClassTemplateSpecializationDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +0000735 new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000736 Result->MayHaveOutOfDateDef = false;
737 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000738}
739
Benjamin Kramer9170e912013-02-22 15:46:01 +0000740void ClassTemplateSpecializationDecl::getNameForDiagnostic(
741 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
742 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +0000743
744 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
Benjamin Kramer9170e912013-02-22 15:46:01 +0000745 TemplateSpecializationType::PrintTemplateArgumentList(
746 OS, TemplateArgs.data(), TemplateArgs.size(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +0000747}
748
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000749ClassTemplateDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000750ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
751 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000752 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
753 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
754 return SpecializedTemplate.get<ClassTemplateDecl*>();
755}
756
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000757SourceRange
758ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000759 if (ExplicitInfo) {
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000760 SourceLocation Begin = getTemplateKeywordLoc();
761 if (Begin.isValid()) {
762 // Here we have an explicit (partial) specialization or instantiation.
763 assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
764 getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
765 getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
766 if (getExternLoc().isValid())
767 Begin = getExternLoc();
768 SourceLocation End = getRBraceLoc();
769 if (End.isInvalid())
770 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
771 return SourceRange(Begin, End);
772 }
773 // An implicit instantiation of a class template partial specialization
774 // uses ExplicitInfo to record the TypeAsWritten, but the source
775 // locations should be retrieved from the instantiation pattern.
776 typedef ClassTemplatePartialSpecializationDecl CTPSDecl;
777 CTPSDecl *ctpsd = const_cast<CTPSDecl*>(cast<CTPSDecl>(this));
778 CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
Craig Topper36250ad2014-05-12 05:36:57 +0000779 assert(inst_from != nullptr);
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000780 return inst_from->getSourceRange();
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000781 }
782 else {
783 // No explicit info available.
784 llvm::PointerUnion<ClassTemplateDecl *,
785 ClassTemplatePartialSpecializationDecl *>
786 inst_from = getInstantiatedFrom();
787 if (inst_from.isNull())
788 return getSpecializedTemplate()->getSourceRange();
789 if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>())
790 return ctd->getSourceRange();
791 return inst_from.get<ClassTemplatePartialSpecializationDecl*>()
792 ->getSourceRange();
793 }
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000794}
795
Douglas Gregor2373c592009-05-31 09:31:02 +0000796//===----------------------------------------------------------------------===//
797// ClassTemplatePartialSpecializationDecl Implementation
798//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000799void ClassTemplatePartialSpecializationDecl::anchor() { }
800
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000801ClassTemplatePartialSpecializationDecl::
802ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000803 DeclContext *DC,
804 SourceLocation StartLoc,
805 SourceLocation IdLoc,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000806 TemplateParameterList *Params,
807 ClassTemplateDecl *SpecializedTemplate,
808 const TemplateArgument *Args,
809 unsigned NumArgs,
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000810 const ASTTemplateArgumentListInfo *ArgInfos,
Richard Smithb2f61b42013-08-22 23:27:37 +0000811 ClassTemplatePartialSpecializationDecl *PrevDecl)
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000812 : ClassTemplateSpecializationDecl(Context,
813 ClassTemplatePartialSpecialization,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000814 TK, DC, StartLoc, IdLoc,
815 SpecializedTemplate,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000816 Args, NumArgs, PrevDecl),
817 TemplateParams(Params), ArgsAsWritten(ArgInfos),
Craig Topper36250ad2014-05-12 05:36:57 +0000818 InstantiatedFromMember(nullptr, false)
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000819{
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000820 AdoptTemplateParameterList(Params, this);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000821}
822
Douglas Gregor2373c592009-05-31 09:31:02 +0000823ClassTemplatePartialSpecializationDecl *
824ClassTemplatePartialSpecializationDecl::
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000825Create(ASTContext &Context, TagKind TK,DeclContext *DC,
826 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregor2373c592009-05-31 09:31:02 +0000827 TemplateParameterList *Params,
828 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000829 const TemplateArgument *Args,
830 unsigned NumArgs,
John McCall6b51f282009-11-23 01:53:49 +0000831 const TemplateArgumentListInfo &ArgInfos,
John McCalle78aac42010-03-10 03:28:59 +0000832 QualType CanonInjectedType,
Richard Smithb2f61b42013-08-22 23:27:37 +0000833 ClassTemplatePartialSpecializationDecl *PrevDecl) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000834 const ASTTemplateArgumentListInfo *ASTArgInfos =
835 ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
John McCall0ad16662009-10-29 08:12:44 +0000836
Richard Smithf7981722013-11-22 09:01:48 +0000837 ClassTemplatePartialSpecializationDecl *Result = new (Context, DC)
838 ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
839 Params, SpecializedTemplate, Args,
840 NumArgs, ASTArgInfos, PrevDecl);
Douglas Gregor2373c592009-05-31 09:31:02 +0000841 Result->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000842 Result->MayHaveOutOfDateDef = false;
John McCalle78aac42010-03-10 03:28:59 +0000843
844 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregor2373c592009-05-31 09:31:02 +0000845 return Result;
846}
John McCall11083da2009-09-16 22:47:08 +0000847
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000848ClassTemplatePartialSpecializationDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000849ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
850 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000851 ClassTemplatePartialSpecializationDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +0000852 new (C, ID) ClassTemplatePartialSpecializationDecl(C);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000853 Result->MayHaveOutOfDateDef = false;
854 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000855}
856
John McCall11083da2009-09-16 22:47:08 +0000857//===----------------------------------------------------------------------===//
858// FriendTemplateDecl Implementation
859//===----------------------------------------------------------------------===//
860
David Blaikie68e081d2011-12-20 02:48:34 +0000861void FriendTemplateDecl::anchor() { }
862
John McCall11083da2009-09-16 22:47:08 +0000863FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
864 DeclContext *DC,
865 SourceLocation L,
866 unsigned NParams,
867 TemplateParameterList **Params,
868 FriendUnion Friend,
869 SourceLocation FLoc) {
Richard Smithf7981722013-11-22 09:01:48 +0000870 return new (Context, DC) FriendTemplateDecl(DC, L, NParams, Params,
871 Friend, FLoc);
John McCall11083da2009-09-16 22:47:08 +0000872}
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000873
Douglas Gregor72172e92012-01-05 21:55:30 +0000874FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
875 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000876 return new (C, ID) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000877}
Richard Smith3f1b5d02011-05-05 21:57:07 +0000878
879//===----------------------------------------------------------------------===//
880// TypeAliasTemplateDecl Implementation
881//===----------------------------------------------------------------------===//
882
883TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
884 DeclContext *DC,
885 SourceLocation L,
886 DeclarationName Name,
887 TemplateParameterList *Params,
888 NamedDecl *Decl) {
889 AdoptTemplateParameterList(Params, DC);
Richard Smith053f6c62014-05-16 23:01:30 +0000890 return new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000891}
892
Douglas Gregor72172e92012-01-05 21:55:30 +0000893TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
894 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000895 return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000896 DeclarationName(), nullptr, nullptr);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000897}
898
899void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) {
900 static_cast<Common *>(Ptr)->~Common();
901}
902RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000903TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
Richard Smith3f1b5d02011-05-05 21:57:07 +0000904 Common *CommonPtr = new (C) Common;
905 C.AddDeallocation(DeallocateCommon, CommonPtr);
906 return CommonPtr;
907}
908
David Blaikie68e081d2011-12-20 02:48:34 +0000909//===----------------------------------------------------------------------===//
910// ClassScopeFunctionSpecializationDecl Implementation
911//===----------------------------------------------------------------------===//
912
913void ClassScopeFunctionSpecializationDecl::anchor() { }
Douglas Gregor72172e92012-01-05 21:55:30 +0000914
915ClassScopeFunctionSpecializationDecl *
916ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
917 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000918 return new (C, ID) ClassScopeFunctionSpecializationDecl(
Craig Topper36250ad2014-05-12 05:36:57 +0000919 nullptr, SourceLocation(), nullptr, false, TemplateArgumentListInfo());
Douglas Gregor72172e92012-01-05 21:55:30 +0000920}
Larisse Voufo39a1e502013-08-06 01:03:05 +0000921
922//===----------------------------------------------------------------------===//
923// VarTemplateDecl Implementation
924//===----------------------------------------------------------------------===//
925
926void VarTemplateDecl::DeallocateCommon(void *Ptr) {
927 static_cast<Common *>(Ptr)->~Common();
928}
929
Larisse Voufoa11bd8a2013-08-13 02:02:26 +0000930VarTemplateDecl *VarTemplateDecl::getDefinition() {
931 VarTemplateDecl *CurD = this;
932 while (CurD) {
933 if (CurD->isThisDeclarationADefinition())
934 return CurD;
935 CurD = CurD->getPreviousDecl();
936 }
Craig Topper36250ad2014-05-12 05:36:57 +0000937 return nullptr;
Larisse Voufoa11bd8a2013-08-13 02:02:26 +0000938}
939
Larisse Voufo39a1e502013-08-06 01:03:05 +0000940VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
941 SourceLocation L, DeclarationName Name,
942 TemplateParameterList *Params,
Richard Smithbeef3452014-01-16 23:39:20 +0000943 VarDecl *Decl) {
Richard Smith053f6c62014-05-16 23:01:30 +0000944 return new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000945}
946
947VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
948 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000949 return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
950 DeclarationName(), nullptr, nullptr);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000951}
952
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000953// TODO: Unify across class, function and variable templates?
Larisse Voufo30616382013-08-23 22:21:36 +0000954// May require moving this and Common to RedeclarableTemplateDecl.
Larisse Voufo39a1e502013-08-06 01:03:05 +0000955void VarTemplateDecl::LoadLazySpecializations() const {
956 Common *CommonPtr = getCommonPtr();
957 if (CommonPtr->LazySpecializations) {
958 ASTContext &Context = getASTContext();
959 uint32_t *Specs = CommonPtr->LazySpecializations;
Craig Topper36250ad2014-05-12 05:36:57 +0000960 CommonPtr->LazySpecializations = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000961 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
962 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
963 }
964}
965
966llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
967VarTemplateDecl::getSpecializations() const {
968 LoadLazySpecializations();
969 return getCommonPtr()->Specializations;
970}
971
972llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
973VarTemplateDecl::getPartialSpecializations() {
974 LoadLazySpecializations();
975 return getCommonPtr()->PartialSpecializations;
976}
977
978RedeclarableTemplateDecl::CommonBase *
979VarTemplateDecl::newCommon(ASTContext &C) const {
980 Common *CommonPtr = new (C) Common;
981 C.AddDeallocation(DeallocateCommon, CommonPtr);
982 return CommonPtr;
983}
984
985VarTemplateSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000986VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
987 void *&InsertPos) {
988 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000989}
990
991void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
992 void *InsertPos) {
993 if (InsertPos)
994 getSpecializations().InsertNode(D, InsertPos);
995 else {
996 VarTemplateSpecializationDecl *Existing =
997 getSpecializations().GetOrInsertNode(D);
998 (void)Existing;
999 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
1000 }
1001 if (ASTMutationListener *L = getASTMutationListener())
1002 L->AddedCXXTemplateSpecialization(this, D);
1003}
1004
1005VarTemplatePartialSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +00001006VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
1007 void *&InsertPos) {
1008 return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001009}
1010
1011void VarTemplateDecl::AddPartialSpecialization(
1012 VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
1013 if (InsertPos)
1014 getPartialSpecializations().InsertNode(D, InsertPos);
1015 else {
1016 VarTemplatePartialSpecializationDecl *Existing =
1017 getPartialSpecializations().GetOrInsertNode(D);
1018 (void)Existing;
1019 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
1020 }
1021
1022 if (ASTMutationListener *L = getASTMutationListener())
1023 L->AddedCXXTemplateSpecialization(this, D);
1024}
1025
1026void VarTemplateDecl::getPartialSpecializations(
1027 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) {
1028 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
1029 getPartialSpecializations();
1030 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +00001031 PS.reserve(PartialSpecs.size());
Larisse Voufo39a1e502013-08-06 01:03:05 +00001032 for (llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>::iterator
1033 P = PartialSpecs.begin(),
1034 PEnd = PartialSpecs.end();
Richard Smithb2f61b42013-08-22 23:27:37 +00001035 P != PEnd; ++P)
1036 PS.push_back(P->getMostRecentDecl());
Larisse Voufo39a1e502013-08-06 01:03:05 +00001037}
1038
1039VarTemplatePartialSpecializationDecl *
1040VarTemplateDecl::findPartialSpecInstantiatedFromMember(
1041 VarTemplatePartialSpecializationDecl *D) {
1042 Decl *DCanon = D->getCanonicalDecl();
1043 for (llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>::iterator
1044 P = getPartialSpecializations().begin(),
1045 PEnd = getPartialSpecializations().end();
1046 P != PEnd; ++P) {
1047 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
1048 return P->getMostRecentDecl();
1049 }
1050
Craig Topper36250ad2014-05-12 05:36:57 +00001051 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00001052}
1053
1054//===----------------------------------------------------------------------===//
1055// VarTemplateSpecializationDecl Implementation
1056//===----------------------------------------------------------------------===//
1057VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001058 Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001059 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
1060 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
1061 unsigned NumArgs)
Richard Smith053f6c62014-05-16 23:01:30 +00001062 : VarDecl(DK, Context, DC, StartLoc, IdLoc,
1063 SpecializedTemplate->getIdentifier(), T, TInfo, S),
Craig Topper36250ad2014-05-12 05:36:57 +00001064 SpecializedTemplate(SpecializedTemplate), ExplicitInfo(nullptr),
Larisse Voufo39a1e502013-08-06 01:03:05 +00001065 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
1066 SpecializationKind(TSK_Undeclared) {}
1067
Richard Smith053f6c62014-05-16 23:01:30 +00001068VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
1069 ASTContext &C)
1070 : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001071 QualType(), nullptr, SC_None),
1072 ExplicitInfo(nullptr), SpecializationKind(TSK_Undeclared) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +00001073
1074VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
1075 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1076 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
1077 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
1078 unsigned NumArgs) {
Richard Smithf7981722013-11-22 09:01:48 +00001079 return new (Context, DC) VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001080 VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
Richard Smithf7981722013-11-22 09:01:48 +00001081 SpecializedTemplate, T, TInfo, S, Args, NumArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001082}
1083
1084VarTemplateSpecializationDecl *
1085VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001086 return new (C, ID)
1087 VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001088}
1089
1090void VarTemplateSpecializationDecl::getNameForDiagnostic(
1091 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
1092 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
1093
1094 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
1095 TemplateSpecializationType::PrintTemplateArgumentList(
1096 OS, TemplateArgs.data(), TemplateArgs.size(), Policy);
1097}
1098
1099VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
1100 if (SpecializedPartialSpecialization *PartialSpec =
1101 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1102 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
1103 return SpecializedTemplate.get<VarTemplateDecl *>();
1104}
1105
1106void VarTemplateSpecializationDecl::setTemplateArgsInfo(
1107 const TemplateArgumentListInfo &ArgsInfo) {
1108 unsigned N = ArgsInfo.size();
1109 TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
1110 TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
1111 for (unsigned I = 0; I != N; ++I)
1112 TemplateArgsInfo.addArgument(ArgsInfo[I]);
1113}
1114
1115//===----------------------------------------------------------------------===//
1116// VarTemplatePartialSpecializationDecl Implementation
1117//===----------------------------------------------------------------------===//
1118void VarTemplatePartialSpecializationDecl::anchor() {}
1119
1120VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
1121 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1122 SourceLocation IdLoc, TemplateParameterList *Params,
1123 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1124 StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
Richard Smithb2f61b42013-08-22 23:27:37 +00001125 const ASTTemplateArgumentListInfo *ArgInfos)
Richard Smith053f6c62014-05-16 23:01:30 +00001126 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001127 DC, StartLoc, IdLoc, SpecializedTemplate, T,
1128 TInfo, S, Args, NumArgs),
1129 TemplateParams(Params), ArgsAsWritten(ArgInfos),
Craig Topper36250ad2014-05-12 05:36:57 +00001130 InstantiatedFromMember(nullptr, false) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001131 // TODO: The template parameters should be in DC by now. Verify.
1132 // AdoptTemplateParameterList(Params, DC);
1133}
1134
1135VarTemplatePartialSpecializationDecl *
1136VarTemplatePartialSpecializationDecl::Create(
1137 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1138 SourceLocation IdLoc, TemplateParameterList *Params,
1139 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1140 StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
Richard Smithb2f61b42013-08-22 23:27:37 +00001141 const TemplateArgumentListInfo &ArgInfos) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00001142 const ASTTemplateArgumentListInfo *ASTArgInfos
1143 = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001144
1145 VarTemplatePartialSpecializationDecl *Result =
Richard Smithf7981722013-11-22 09:01:48 +00001146 new (Context, DC) VarTemplatePartialSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00001147 Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
Richard Smithb2f61b42013-08-22 23:27:37 +00001148 S, Args, NumArgs, ASTArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001149 Result->setSpecializationKind(TSK_ExplicitSpecialization);
1150 return Result;
1151}
1152
1153VarTemplatePartialSpecializationDecl *
1154VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
1155 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001156 return new (C, ID) VarTemplatePartialSpecializationDecl(C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001157}