blob: 408e8548b1d60106c5806bafdf2ed0ed96d410d6 [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,
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000167 const TemplateArgument *Args, unsigned NumArgs,
168 void *&InsertPos) {
169 typedef SpecEntryTraits<EntryType> SETraits;
170 llvm::FoldingSetNodeID ID;
171 EntryType::Profile(ID,Args,NumArgs, getASTContext());
172 EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000173 return Entry ? SETraits::getMostRecentDecl(Entry) : 0;
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 Smithf7981722013-11-22 09:01:48 +0000232 return new (C, DC) FunctionTemplateDecl(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 Smithf7981722013-11-22 09:01:48 +0000237 return new (C, ID) FunctionTemplateDecl(0, SourceLocation(), DeclarationName(),
238 0, 0);
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;
253 CommonPtr->LazySpecializations = 0;
254 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 *
266FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args,
267 unsigned NumArgs, void *&InsertPos) {
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000268 return findSpecializationImpl(getSpecializations(), Args, NumArgs, 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 Smithf7981722013-11-22 09:01:48 +0000310 ClassTemplateDecl *New =
311 new (C, DC) ClassTemplateDecl(DC, L, Name, 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
Douglas Gregor72172e92012-01-05 21:55:30 +0000316ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
317 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000318 return new (C, ID) ClassTemplateDecl(EmptyShell());
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000319}
320
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000321void ClassTemplateDecl::LoadLazySpecializations() const {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000322 Common *CommonPtr = getCommonPtr();
323 if (CommonPtr->LazySpecializations) {
324 ASTContext &Context = getASTContext();
325 uint32_t *Specs = CommonPtr->LazySpecializations;
326 CommonPtr->LazySpecializations = 0;
327 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
328 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
329 }
330}
331
Chandler Carruthb41171b2012-05-03 23:49:05 +0000332llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000333ClassTemplateDecl::getSpecializations() const {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000334 LoadLazySpecializations();
335 return getCommonPtr()->Specializations;
336}
337
Chandler Carruthb41171b2012-05-03 23:49:05 +0000338llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000339ClassTemplateDecl::getPartialSpecializations() {
340 LoadLazySpecializations();
341 return getCommonPtr()->PartialSpecializations;
342}
343
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000344RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000345ClassTemplateDecl::newCommon(ASTContext &C) const {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000346 Common *CommonPtr = new (C) Common;
347 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000348 return CommonPtr;
349}
350
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000351ClassTemplateSpecializationDecl *
352ClassTemplateDecl::findSpecialization(const TemplateArgument *Args,
353 unsigned NumArgs, void *&InsertPos) {
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000354 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000355}
356
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000357void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
358 void *InsertPos) {
Douglas Gregorce9978f2012-03-28 14:34:23 +0000359 if (InsertPos)
360 getSpecializations().InsertNode(D, InsertPos);
361 else {
362 ClassTemplateSpecializationDecl *Existing
363 = getSpecializations().GetOrInsertNode(D);
364 (void)Existing;
365 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
366 }
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000367 if (ASTMutationListener *L = getASTMutationListener())
368 L->AddedCXXTemplateSpecialization(this, D);
369}
370
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000371ClassTemplatePartialSpecializationDecl *
372ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
373 unsigned NumArgs,
374 void *&InsertPos) {
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000375 return findSpecializationImpl(getPartialSpecializations(), Args, NumArgs,
376 InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000377}
378
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000379void ClassTemplateDecl::AddPartialSpecialization(
380 ClassTemplatePartialSpecializationDecl *D,
381 void *InsertPos) {
Douglas Gregorce9978f2012-03-28 14:34:23 +0000382 if (InsertPos)
383 getPartialSpecializations().InsertNode(D, InsertPos);
384 else {
385 ClassTemplatePartialSpecializationDecl *Existing
386 = getPartialSpecializations().GetOrInsertNode(D);
387 (void)Existing;
388 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
389 }
390
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000391 if (ASTMutationListener *L = getASTMutationListener())
392 L->AddedCXXTemplateSpecialization(this, D);
393}
394
Douglas Gregor407e9612010-04-30 05:56:50 +0000395void ClassTemplateDecl::getPartialSpecializations(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000396 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
Chandler Carruthb41171b2012-05-03 23:49:05 +0000397 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000398 = getPartialSpecializations();
Douglas Gregor407e9612010-04-30 05:56:50 +0000399 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +0000400 PS.reserve(PartialSpecs.size());
Chandler Carruthb41171b2012-05-03 23:49:05 +0000401 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor407e9612010-04-30 05:56:50 +0000402 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
Richard Smithb2f61b42013-08-22 23:27:37 +0000403 P != PEnd; ++P)
404 PS.push_back(P->getMostRecentDecl());
Douglas Gregor407e9612010-04-30 05:56:50 +0000405}
406
Douglas Gregor15301382009-07-30 17:40:51 +0000407ClassTemplatePartialSpecializationDecl *
408ClassTemplateDecl::findPartialSpecialization(QualType T) {
409 ASTContext &Context = getASTContext();
Chandler Carruthb41171b2012-05-03 23:49:05 +0000410 using llvm::FoldingSetVector;
411 typedef FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor15301382009-07-30 17:40:51 +0000412 partial_spec_iterator;
413 for (partial_spec_iterator P = getPartialSpecializations().begin(),
414 PEnd = getPartialSpecializations().end();
415 P != PEnd; ++P) {
John McCall2408e322010-04-27 00:57:59 +0000416 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregorec9fd132012-01-14 16:38:05 +0000417 return P->getMostRecentDecl();
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000418 }
419
420 return 0;
421}
422
423ClassTemplatePartialSpecializationDecl *
424ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
425 ClassTemplatePartialSpecializationDecl *D) {
426 Decl *DCanon = D->getCanonicalDecl();
Chandler Carruthb41171b2012-05-03 23:49:05 +0000427 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000428 P = getPartialSpecializations().begin(),
429 PEnd = getPartialSpecializations().end();
430 P != PEnd; ++P) {
431 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
Douglas Gregorec9fd132012-01-14 16:38:05 +0000432 return P->getMostRecentDecl();
Douglas Gregor15301382009-07-30 17:40:51 +0000433 }
Mike Stump11289f42009-09-09 15:08:12 +0000434
Douglas Gregor15301382009-07-30 17:40:51 +0000435 return 0;
436}
437
John McCalle78aac42010-03-10 03:28:59 +0000438QualType
Douglas Gregor9961ce92010-07-08 18:37:38 +0000439ClassTemplateDecl::getInjectedClassNameSpecialization() {
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000440 Common *CommonPtr = getCommonPtr();
Douglas Gregore362cea2009-05-10 22:57:19 +0000441 if (!CommonPtr->InjectedClassNameType.isNull())
442 return CommonPtr->InjectedClassNameType;
443
Douglas Gregor8092e802010-12-23 16:00:30 +0000444 // C++0x [temp.dep.type]p2:
445 // The template argument list of a primary template is a template argument
446 // list in which the nth template argument has the value of the nth template
447 // parameter of the class template. If the nth template parameter is a
448 // template parameter pack (14.5.3), the nth template argument is a pack
449 // expansion (14.5.3) whose pattern is the name of the template parameter
450 // pack.
Douglas Gregor9961ce92010-07-08 18:37:38 +0000451 ASTContext &Context = getASTContext();
Douglas Gregore362cea2009-05-10 22:57:19 +0000452 TemplateParameterList *Params = getTemplateParameters();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000453 SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregor43669f82011-03-05 17:54:25 +0000454 TemplateArgs.resize(Params->size());
455 GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data());
Douglas Gregore362cea2009-05-10 22:57:19 +0000456 CommonPtr->InjectedClassNameType
Douglas Gregora8e02e72009-07-28 23:00:59 +0000457 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregore362cea2009-05-10 22:57:19 +0000458 &TemplateArgs[0],
Douglas Gregora8e02e72009-07-28 23:00:59 +0000459 TemplateArgs.size());
Douglas Gregore362cea2009-05-10 22:57:19 +0000460 return CommonPtr->InjectedClassNameType;
461}
462
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000463//===----------------------------------------------------------------------===//
464// TemplateTypeParm Allocation/Deallocation Method Implementations
465//===----------------------------------------------------------------------===//
466
467TemplateTypeParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000468TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000469 SourceLocation KeyLoc, SourceLocation NameLoc,
470 unsigned D, unsigned P, IdentifierInfo *Id,
471 bool Typename, bool ParameterPack) {
Chandler Carruth08836322011-05-01 00:51:33 +0000472 TemplateTypeParmDecl *TTPDecl =
Richard Smithf7981722013-11-22 09:01:48 +0000473 new (C, DC) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
Chandler Carruth08836322011-05-01 00:51:33 +0000474 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
Richard Smith5b21db82014-04-23 18:20:42 +0000475 TTPDecl->setTypeForDecl(TTPType.getTypePtr());
Chandler Carruth08836322011-05-01 00:51:33 +0000476 return TTPDecl;
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000477}
478
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000479TemplateTypeParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000480TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000481 return new (C, ID) TemplateTypeParmDecl(0, SourceLocation(), SourceLocation(),
482 0, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000483}
484
John McCall0ad16662009-10-29 08:12:44 +0000485SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara23485e02011-03-04 12:42:03 +0000486 return hasDefaultArgument()
487 ? DefaultArgument->getTypeLoc().getBeginLoc()
488 : SourceLocation();
489}
490
491SourceRange TemplateTypeParmDecl::getSourceRange() const {
492 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000493 return SourceRange(getLocStart(),
Abramo Bagnara23485e02011-03-04 12:42:03 +0000494 DefaultArgument->getTypeLoc().getEndLoc());
495 else
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000496 return TypeDecl::getSourceRange();
John McCall0ad16662009-10-29 08:12:44 +0000497}
498
Douglas Gregor21610382009-10-29 00:04:11 +0000499unsigned TemplateTypeParmDecl::getDepth() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000500 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getDepth();
Douglas Gregor21610382009-10-29 00:04:11 +0000501}
502
503unsigned TemplateTypeParmDecl::getIndex() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000504 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getIndex();
Douglas Gregor21610382009-10-29 00:04:11 +0000505}
506
Chandler Carruth08836322011-05-01 00:51:33 +0000507bool TemplateTypeParmDecl::isParameterPack() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000508 return getTypeForDecl()->getAs<TemplateTypeParmType>()->isParameterPack();
Chandler Carruth08836322011-05-01 00:51:33 +0000509}
510
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000511//===----------------------------------------------------------------------===//
512// NonTypeTemplateParmDecl Method Implementations
513//===----------------------------------------------------------------------===//
514
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000515NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000516 SourceLocation StartLoc,
517 SourceLocation IdLoc,
518 unsigned D, unsigned P,
519 IdentifierInfo *Id,
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000520 QualType T,
521 TypeSourceInfo *TInfo,
522 const QualType *ExpandedTypes,
523 unsigned NumExpandedTypes,
524 TypeSourceInfo **ExpandedTInfos)
Abramo Bagnaradff19302011-03-08 08:55:46 +0000525 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000526 TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false),
527 ParameterPack(true), ExpandedParameterPack(true),
528 NumExpandedTypes(NumExpandedTypes)
529{
530 if (ExpandedTypes && ExpandedTInfos) {
531 void **TypesAndInfos = reinterpret_cast<void **>(this + 1);
532 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
533 TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr();
534 TypesAndInfos[2*I + 1] = ExpandedTInfos[I];
535 }
536 }
537}
538
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000539NonTypeTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000540NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000541 SourceLocation StartLoc, SourceLocation IdLoc,
542 unsigned D, unsigned P, IdentifierInfo *Id,
543 QualType T, bool ParameterPack,
544 TypeSourceInfo *TInfo) {
Richard Smithf7981722013-11-22 09:01:48 +0000545 return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
546 T, ParameterPack, TInfo);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000547}
548
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000549NonTypeTemplateParmDecl *
550NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000551 SourceLocation StartLoc, SourceLocation IdLoc,
552 unsigned D, unsigned P,
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000553 IdentifierInfo *Id, QualType T,
554 TypeSourceInfo *TInfo,
555 const QualType *ExpandedTypes,
556 unsigned NumExpandedTypes,
557 TypeSourceInfo **ExpandedTInfos) {
Richard Smithf7981722013-11-22 09:01:48 +0000558 unsigned Extra = NumExpandedTypes * 2 * sizeof(void*);
559 return new (C, DC, Extra) NonTypeTemplateParmDecl(
560 DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
561 ExpandedTypes, NumExpandedTypes, ExpandedTInfos);
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000562}
563
Douglas Gregor72172e92012-01-05 21:55:30 +0000564NonTypeTemplateParmDecl *
565NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000566 return new (C, ID) NonTypeTemplateParmDecl(0, SourceLocation(),
567 SourceLocation(), 0, 0, 0,
568 QualType(), false, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000569}
570
571NonTypeTemplateParmDecl *
572NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
573 unsigned NumExpandedTypes) {
Richard Smithf7981722013-11-22 09:01:48 +0000574 unsigned Extra = NumExpandedTypes * 2 * sizeof(void*);
575 return new (C, ID, Extra) NonTypeTemplateParmDecl(
576 0, SourceLocation(), SourceLocation(), 0, 0, 0, QualType(), 0,
577 0, NumExpandedTypes, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000578}
579
John McCallf4cd4f92011-02-09 01:13:10 +0000580SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnarae15d5532011-03-04 11:03:48 +0000581 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraea947882011-03-08 16:41:52 +0000582 return SourceRange(getOuterLocStart(),
583 getDefaultArgument()->getSourceRange().getEnd());
584 return DeclaratorDecl::getSourceRange();
John McCallf4cd4f92011-02-09 01:13:10 +0000585}
586
Douglas Gregordba32632009-02-10 19:49:53 +0000587SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara656e3002010-06-09 09:26:05 +0000588 return hasDefaultArgument()
589 ? getDefaultArgument()->getSourceRange().getBegin()
590 : SourceLocation();
Douglas Gregordba32632009-02-10 19:49:53 +0000591}
592
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000593//===----------------------------------------------------------------------===//
594// TemplateTemplateParmDecl Method Implementations
595//===----------------------------------------------------------------------===//
596
David Blaikie68e081d2011-12-20 02:48:34 +0000597void TemplateTemplateParmDecl::anchor() { }
598
Richard Smith1fde8ec2012-09-07 02:06:42 +0000599TemplateTemplateParmDecl::TemplateTemplateParmDecl(
600 DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
601 IdentifierInfo *Id, TemplateParameterList *Params,
602 unsigned NumExpansions, TemplateParameterList * const *Expansions)
603 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
604 TemplateParmPosition(D, P), DefaultArgument(),
605 DefaultArgumentWasInherited(false), ParameterPack(true),
606 ExpandedParameterPack(true), NumExpandedParams(NumExpansions) {
607 if (Expansions)
608 std::memcpy(reinterpret_cast<void*>(this + 1), Expansions,
609 sizeof(TemplateParameterList*) * NumExpandedParams);
610}
611
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000612TemplateTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000613TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000614 SourceLocation L, unsigned D, unsigned P,
Douglas Gregorf5500772011-01-05 15:48:55 +0000615 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000616 TemplateParameterList *Params) {
Richard Smithf7981722013-11-22 09:01:48 +0000617 return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
618 Params);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000619}
620
Douglas Gregor72172e92012-01-05 21:55:30 +0000621TemplateTemplateParmDecl *
Richard Smith1fde8ec2012-09-07 02:06:42 +0000622TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
623 SourceLocation L, unsigned D, unsigned P,
624 IdentifierInfo *Id,
625 TemplateParameterList *Params,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000626 ArrayRef<TemplateParameterList *> Expansions) {
Richard Smithf7981722013-11-22 09:01:48 +0000627 return new (C, DC, sizeof(TemplateParameterList*) * Expansions.size())
628 TemplateTemplateParmDecl(DC, L, D, P, Id, Params,
629 Expansions.size(), Expansions.data());
Richard Smith1fde8ec2012-09-07 02:06:42 +0000630}
631
632TemplateTemplateParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000633TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000634 return new (C, ID) TemplateTemplateParmDecl(0, SourceLocation(), 0, 0, false,
635 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +0000636}
637
Richard Smith1fde8ec2012-09-07 02:06:42 +0000638TemplateTemplateParmDecl *
639TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
640 unsigned NumExpansions) {
Richard Smithf7981722013-11-22 09:01:48 +0000641 return new (C, ID, sizeof(TemplateParameterList*) * NumExpansions)
642 TemplateTemplateParmDecl(0, SourceLocation(), 0, 0, 0, 0,
643 NumExpansions, 0);
Richard Smith1fde8ec2012-09-07 02:06:42 +0000644}
645
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000646//===----------------------------------------------------------------------===//
Douglas Gregord002c7b2009-05-11 23:53:27 +0000647// TemplateArgumentList Implementation
648//===----------------------------------------------------------------------===//
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000649TemplateArgumentList *
650TemplateArgumentList::CreateCopy(ASTContext &Context,
651 const TemplateArgument *Args,
652 unsigned NumArgs) {
653 std::size_t Size = sizeof(TemplateArgumentList)
654 + NumArgs * sizeof(TemplateArgument);
655 void *Mem = Context.Allocate(Size);
656 TemplateArgument *StoredArgs
657 = reinterpret_cast<TemplateArgument *>(
658 static_cast<TemplateArgumentList *>(Mem) + 1);
659 std::uninitialized_copy(Args, Args + NumArgs, StoredArgs);
660 return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true);
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000661}
662
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000663FunctionTemplateSpecializationInfo *
664FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD,
665 FunctionTemplateDecl *Template,
666 TemplateSpecializationKind TSK,
667 const TemplateArgumentList *TemplateArgs,
668 const TemplateArgumentListInfo *TemplateArgsAsWritten,
669 SourceLocation POI) {
670 const ASTTemplateArgumentListInfo *ArgsAsWritten = 0;
671 if (TemplateArgsAsWritten)
672 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
673 *TemplateArgsAsWritten);
674
675 return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK,
676 TemplateArgs,
677 ArgsAsWritten,
678 POI);
679}
680
Douglas Gregord002c7b2009-05-11 23:53:27 +0000681//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000682// TemplateDecl Implementation
683//===----------------------------------------------------------------------===//
684
685void TemplateDecl::anchor() { }
686
687//===----------------------------------------------------------------------===//
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000688// ClassTemplateSpecializationDecl Implementation
689//===----------------------------------------------------------------------===//
690ClassTemplateSpecializationDecl::
Douglas Gregore9029562010-05-06 00:28:52 +0000691ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000692 DeclContext *DC, SourceLocation StartLoc,
693 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000694 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000695 const TemplateArgument *Args,
696 unsigned NumArgs,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000697 ClassTemplateSpecializationDecl *PrevDecl)
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000698 : CXXRecordDecl(DK, TK, DC, StartLoc, IdLoc,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000699 SpecializedTemplate->getIdentifier(),
700 PrevDecl),
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000701 SpecializedTemplate(SpecializedTemplate),
Abramo Bagnara8075c852010-06-12 07:44:57 +0000702 ExplicitInfo(0),
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000703 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
Douglas Gregord002c7b2009-05-11 23:53:27 +0000704 SpecializationKind(TSK_Undeclared) {
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000705}
Mike Stump11289f42009-09-09 15:08:12 +0000706
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000707ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK)
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000708 : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0),
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000709 ExplicitInfo(0),
710 SpecializationKind(TSK_Undeclared) {
711}
712
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000713ClassTemplateSpecializationDecl *
Douglas Gregore9029562010-05-06 00:28:52 +0000714ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000715 DeclContext *DC,
716 SourceLocation StartLoc,
717 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000718 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000719 const TemplateArgument *Args,
720 unsigned NumArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000721 ClassTemplateSpecializationDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +0000722 ClassTemplateSpecializationDecl *Result =
723 new (Context, DC) ClassTemplateSpecializationDecl(
724 Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
725 SpecializedTemplate, Args, NumArgs, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000726 Result->MayHaveOutOfDateDef = false;
727
Douglas Gregor67a65642009-02-17 23:15:12 +0000728 Context.getTypeDeclType(Result, PrevDecl);
729 return Result;
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000730}
Douglas Gregor2373c592009-05-31 09:31:02 +0000731
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000732ClassTemplateSpecializationDecl *
Richard Smithf7981722013-11-22 09:01:48 +0000733ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000734 unsigned ID) {
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000735 ClassTemplateSpecializationDecl *Result =
Richard Smithf7981722013-11-22 09:01:48 +0000736 new (C, ID) ClassTemplateSpecializationDecl(ClassTemplateSpecialization);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000737 Result->MayHaveOutOfDateDef = false;
738 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000739}
740
Benjamin Kramer9170e912013-02-22 15:46:01 +0000741void ClassTemplateSpecializationDecl::getNameForDiagnostic(
742 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
743 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +0000744
745 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
Benjamin Kramer9170e912013-02-22 15:46:01 +0000746 TemplateSpecializationType::PrintTemplateArgumentList(
747 OS, TemplateArgs.data(), TemplateArgs.size(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +0000748}
749
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000750ClassTemplateDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000751ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
752 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000753 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
754 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
755 return SpecializedTemplate.get<ClassTemplateDecl*>();
756}
757
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000758SourceRange
759ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000760 if (ExplicitInfo) {
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000761 SourceLocation Begin = getTemplateKeywordLoc();
762 if (Begin.isValid()) {
763 // Here we have an explicit (partial) specialization or instantiation.
764 assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
765 getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
766 getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
767 if (getExternLoc().isValid())
768 Begin = getExternLoc();
769 SourceLocation End = getRBraceLoc();
770 if (End.isInvalid())
771 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
772 return SourceRange(Begin, End);
773 }
774 // An implicit instantiation of a class template partial specialization
775 // uses ExplicitInfo to record the TypeAsWritten, but the source
776 // locations should be retrieved from the instantiation pattern.
777 typedef ClassTemplatePartialSpecializationDecl CTPSDecl;
778 CTPSDecl *ctpsd = const_cast<CTPSDecl*>(cast<CTPSDecl>(this));
779 CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
780 assert(inst_from != 0);
781 return inst_from->getSourceRange();
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000782 }
783 else {
784 // No explicit info available.
785 llvm::PointerUnion<ClassTemplateDecl *,
786 ClassTemplatePartialSpecializationDecl *>
787 inst_from = getInstantiatedFrom();
788 if (inst_from.isNull())
789 return getSpecializedTemplate()->getSourceRange();
790 if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>())
791 return ctd->getSourceRange();
792 return inst_from.get<ClassTemplatePartialSpecializationDecl*>()
793 ->getSourceRange();
794 }
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000795}
796
Douglas Gregor2373c592009-05-31 09:31:02 +0000797//===----------------------------------------------------------------------===//
798// ClassTemplatePartialSpecializationDecl Implementation
799//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000800void ClassTemplatePartialSpecializationDecl::anchor() { }
801
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000802ClassTemplatePartialSpecializationDecl::
803ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000804 DeclContext *DC,
805 SourceLocation StartLoc,
806 SourceLocation IdLoc,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000807 TemplateParameterList *Params,
808 ClassTemplateDecl *SpecializedTemplate,
809 const TemplateArgument *Args,
810 unsigned NumArgs,
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000811 const ASTTemplateArgumentListInfo *ArgInfos,
Richard Smithb2f61b42013-08-22 23:27:37 +0000812 ClassTemplatePartialSpecializationDecl *PrevDecl)
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000813 : ClassTemplateSpecializationDecl(Context,
814 ClassTemplatePartialSpecialization,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000815 TK, DC, StartLoc, IdLoc,
816 SpecializedTemplate,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000817 Args, NumArgs, PrevDecl),
818 TemplateParams(Params), ArgsAsWritten(ArgInfos),
Richard Smithb2f61b42013-08-22 23:27:37 +0000819 InstantiatedFromMember(0, false)
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000820{
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000821 AdoptTemplateParameterList(Params, this);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000822}
823
Douglas Gregor2373c592009-05-31 09:31:02 +0000824ClassTemplatePartialSpecializationDecl *
825ClassTemplatePartialSpecializationDecl::
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000826Create(ASTContext &Context, TagKind TK,DeclContext *DC,
827 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregor2373c592009-05-31 09:31:02 +0000828 TemplateParameterList *Params,
829 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000830 const TemplateArgument *Args,
831 unsigned NumArgs,
John McCall6b51f282009-11-23 01:53:49 +0000832 const TemplateArgumentListInfo &ArgInfos,
John McCalle78aac42010-03-10 03:28:59 +0000833 QualType CanonInjectedType,
Richard Smithb2f61b42013-08-22 23:27:37 +0000834 ClassTemplatePartialSpecializationDecl *PrevDecl) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000835 const ASTTemplateArgumentListInfo *ASTArgInfos =
836 ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
John McCall0ad16662009-10-29 08:12:44 +0000837
Richard Smithf7981722013-11-22 09:01:48 +0000838 ClassTemplatePartialSpecializationDecl *Result = new (Context, DC)
839 ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
840 Params, SpecializedTemplate, Args,
841 NumArgs, ASTArgInfos, PrevDecl);
Douglas Gregor2373c592009-05-31 09:31:02 +0000842 Result->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000843 Result->MayHaveOutOfDateDef = false;
John McCalle78aac42010-03-10 03:28:59 +0000844
845 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregor2373c592009-05-31 09:31:02 +0000846 return Result;
847}
John McCall11083da2009-09-16 22:47:08 +0000848
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000849ClassTemplatePartialSpecializationDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000850ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
851 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000852 ClassTemplatePartialSpecializationDecl *Result =
853 new (C, ID) ClassTemplatePartialSpecializationDecl();
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000854 Result->MayHaveOutOfDateDef = false;
855 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000856}
857
John McCall11083da2009-09-16 22:47:08 +0000858//===----------------------------------------------------------------------===//
859// FriendTemplateDecl Implementation
860//===----------------------------------------------------------------------===//
861
David Blaikie68e081d2011-12-20 02:48:34 +0000862void FriendTemplateDecl::anchor() { }
863
John McCall11083da2009-09-16 22:47:08 +0000864FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
865 DeclContext *DC,
866 SourceLocation L,
867 unsigned NParams,
868 TemplateParameterList **Params,
869 FriendUnion Friend,
870 SourceLocation FLoc) {
Richard Smithf7981722013-11-22 09:01:48 +0000871 return new (Context, DC) FriendTemplateDecl(DC, L, NParams, Params,
872 Friend, FLoc);
John McCall11083da2009-09-16 22:47:08 +0000873}
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000874
Douglas Gregor72172e92012-01-05 21:55:30 +0000875FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
876 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000877 return new (C, ID) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000878}
Richard Smith3f1b5d02011-05-05 21:57:07 +0000879
880//===----------------------------------------------------------------------===//
881// TypeAliasTemplateDecl Implementation
882//===----------------------------------------------------------------------===//
883
884TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
885 DeclContext *DC,
886 SourceLocation L,
887 DeclarationName Name,
888 TemplateParameterList *Params,
889 NamedDecl *Decl) {
890 AdoptTemplateParameterList(Params, DC);
Richard Smithf7981722013-11-22 09:01:48 +0000891 return new (C, DC) TypeAliasTemplateDecl(DC, L, Name, Params, Decl);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000892}
893
Douglas Gregor72172e92012-01-05 21:55:30 +0000894TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
895 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000896 return new (C, ID) TypeAliasTemplateDecl(0, SourceLocation(), DeclarationName(),
897 0, 0);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000898}
899
900void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) {
901 static_cast<Common *>(Ptr)->~Common();
902}
903RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000904TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
Richard Smith3f1b5d02011-05-05 21:57:07 +0000905 Common *CommonPtr = new (C) Common;
906 C.AddDeallocation(DeallocateCommon, CommonPtr);
907 return CommonPtr;
908}
909
David Blaikie68e081d2011-12-20 02:48:34 +0000910//===----------------------------------------------------------------------===//
911// ClassScopeFunctionSpecializationDecl Implementation
912//===----------------------------------------------------------------------===//
913
914void ClassScopeFunctionSpecializationDecl::anchor() { }
Douglas Gregor72172e92012-01-05 21:55:30 +0000915
916ClassScopeFunctionSpecializationDecl *
917ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
918 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000919 return new (C, ID) ClassScopeFunctionSpecializationDecl(
920 0, SourceLocation(), 0, false, TemplateArgumentListInfo());
Douglas Gregor72172e92012-01-05 21:55:30 +0000921}
Larisse Voufo39a1e502013-08-06 01:03:05 +0000922
923//===----------------------------------------------------------------------===//
924// VarTemplateDecl Implementation
925//===----------------------------------------------------------------------===//
926
927void VarTemplateDecl::DeallocateCommon(void *Ptr) {
928 static_cast<Common *>(Ptr)->~Common();
929}
930
Larisse Voufoa11bd8a2013-08-13 02:02:26 +0000931VarTemplateDecl *VarTemplateDecl::getDefinition() {
932 VarTemplateDecl *CurD = this;
933 while (CurD) {
934 if (CurD->isThisDeclarationADefinition())
935 return CurD;
936 CurD = CurD->getPreviousDecl();
937 }
938 return 0;
939}
940
Larisse Voufo39a1e502013-08-06 01:03:05 +0000941VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
942 SourceLocation L, DeclarationName Name,
943 TemplateParameterList *Params,
Richard Smithbeef3452014-01-16 23:39:20 +0000944 VarDecl *Decl) {
945 return new (C, DC) VarTemplateDecl(DC, L, Name, Params, Decl);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000946}
947
948VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
949 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000950 return new (C, ID) VarTemplateDecl(EmptyShell());
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;
960 CommonPtr->LazySpecializations = 0;
961 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 *
986VarTemplateDecl::findSpecialization(const TemplateArgument *Args,
987 unsigned NumArgs, void *&InsertPos) {
988 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
989}
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 *
1006VarTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
1007 unsigned NumArgs, void *&InsertPos) {
1008 return findSpecializationImpl(getPartialSpecializations(), Args, NumArgs,
1009 InsertPos);
1010}
1011
1012void VarTemplateDecl::AddPartialSpecialization(
1013 VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
1014 if (InsertPos)
1015 getPartialSpecializations().InsertNode(D, InsertPos);
1016 else {
1017 VarTemplatePartialSpecializationDecl *Existing =
1018 getPartialSpecializations().GetOrInsertNode(D);
1019 (void)Existing;
1020 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
1021 }
1022
1023 if (ASTMutationListener *L = getASTMutationListener())
1024 L->AddedCXXTemplateSpecialization(this, D);
1025}
1026
1027void VarTemplateDecl::getPartialSpecializations(
1028 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) {
1029 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
1030 getPartialSpecializations();
1031 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +00001032 PS.reserve(PartialSpecs.size());
Larisse Voufo39a1e502013-08-06 01:03:05 +00001033 for (llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>::iterator
1034 P = PartialSpecs.begin(),
1035 PEnd = PartialSpecs.end();
Richard Smithb2f61b42013-08-22 23:27:37 +00001036 P != PEnd; ++P)
1037 PS.push_back(P->getMostRecentDecl());
Larisse Voufo39a1e502013-08-06 01:03:05 +00001038}
1039
1040VarTemplatePartialSpecializationDecl *
1041VarTemplateDecl::findPartialSpecInstantiatedFromMember(
1042 VarTemplatePartialSpecializationDecl *D) {
1043 Decl *DCanon = D->getCanonicalDecl();
1044 for (llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>::iterator
1045 P = getPartialSpecializations().begin(),
1046 PEnd = getPartialSpecializations().end();
1047 P != PEnd; ++P) {
1048 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
1049 return P->getMostRecentDecl();
1050 }
1051
1052 return 0;
1053}
1054
1055//===----------------------------------------------------------------------===//
1056// VarTemplateSpecializationDecl Implementation
1057//===----------------------------------------------------------------------===//
1058VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
1059 ASTContext &Context, Kind DK, DeclContext *DC, SourceLocation StartLoc,
1060 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
1061 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
1062 unsigned NumArgs)
1063 : VarDecl(DK, DC, StartLoc, IdLoc, SpecializedTemplate->getIdentifier(), T,
1064 TInfo, S),
1065 SpecializedTemplate(SpecializedTemplate), ExplicitInfo(0),
1066 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
1067 SpecializationKind(TSK_Undeclared) {}
1068
1069VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK)
1070 : VarDecl(DK, 0, SourceLocation(), SourceLocation(), 0, QualType(), 0,
1071 SC_None),
1072 ExplicitInfo(0), SpecializationKind(TSK_Undeclared) {}
1073
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(
1080 Context, VarTemplateSpecialization, DC, StartLoc, IdLoc,
1081 SpecializedTemplate, T, TInfo, S, Args, NumArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001082}
1083
1084VarTemplateSpecializationDecl *
1085VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001086 return new (C, ID) VarTemplateSpecializationDecl(VarTemplateSpecialization);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001087}
1088
1089void VarTemplateSpecializationDecl::getNameForDiagnostic(
1090 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
1091 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
1092
1093 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
1094 TemplateSpecializationType::PrintTemplateArgumentList(
1095 OS, TemplateArgs.data(), TemplateArgs.size(), Policy);
1096}
1097
1098VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
1099 if (SpecializedPartialSpecialization *PartialSpec =
1100 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1101 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
1102 return SpecializedTemplate.get<VarTemplateDecl *>();
1103}
1104
1105void VarTemplateSpecializationDecl::setTemplateArgsInfo(
1106 const TemplateArgumentListInfo &ArgsInfo) {
1107 unsigned N = ArgsInfo.size();
1108 TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
1109 TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
1110 for (unsigned I = 0; I != N; ++I)
1111 TemplateArgsInfo.addArgument(ArgsInfo[I]);
1112}
1113
1114//===----------------------------------------------------------------------===//
1115// VarTemplatePartialSpecializationDecl Implementation
1116//===----------------------------------------------------------------------===//
1117void VarTemplatePartialSpecializationDecl::anchor() {}
1118
1119VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
1120 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1121 SourceLocation IdLoc, TemplateParameterList *Params,
1122 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1123 StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
Richard Smithb2f61b42013-08-22 23:27:37 +00001124 const ASTTemplateArgumentListInfo *ArgInfos)
Larisse Voufo39a1e502013-08-06 01:03:05 +00001125 : VarTemplateSpecializationDecl(Context, VarTemplatePartialSpecialization,
1126 DC, StartLoc, IdLoc, SpecializedTemplate, T,
1127 TInfo, S, Args, NumArgs),
1128 TemplateParams(Params), ArgsAsWritten(ArgInfos),
Richard Smithb2f61b42013-08-22 23:27:37 +00001129 InstantiatedFromMember(0, false) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001130 // TODO: The template parameters should be in DC by now. Verify.
1131 // AdoptTemplateParameterList(Params, DC);
1132}
1133
1134VarTemplatePartialSpecializationDecl *
1135VarTemplatePartialSpecializationDecl::Create(
1136 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1137 SourceLocation IdLoc, TemplateParameterList *Params,
1138 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1139 StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
Richard Smithb2f61b42013-08-22 23:27:37 +00001140 const TemplateArgumentListInfo &ArgInfos) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00001141 const ASTTemplateArgumentListInfo *ASTArgInfos
1142 = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001143
1144 VarTemplatePartialSpecializationDecl *Result =
Richard Smithf7981722013-11-22 09:01:48 +00001145 new (Context, DC) VarTemplatePartialSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00001146 Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
Richard Smithb2f61b42013-08-22 23:27:37 +00001147 S, Args, NumArgs, ASTArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001148 Result->setSpecializationKind(TSK_ExplicitSpecialization);
1149 return Result;
1150}
1151
1152VarTemplatePartialSpecializationDecl *
1153VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
1154 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001155 return new (C, ID) VarTemplatePartialSpecializationDecl();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001156}