blob: 5aebc2b764c012a669dac265d74fc28c3dd25f61 [file] [log] [blame]
Sebastian Redl06a59bb2009-10-23 22:13:42 +00001//===--- DeclTemplate.cpp - Template Declaration AST Node Implementation --===//
Douglas Gregoraaba5e32009-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
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/DeclTemplate.h"
Douglas Gregor55f6b142009-02-09 18:46:07 +000016#include "clang/AST/Expr.h"
Douglas Gregorb95cc972011-01-04 02:33:52 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/ASTContext.h"
John McCall833ca992009-10-29 08:12:44 +000019#include "clang/AST/TypeLoc.h"
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +000020#include "clang/AST/ASTMutationListener.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000021#include "clang/Basic/IdentifierTable.h"
22#include "llvm/ADT/STLExtras.h"
Douglas Gregor910f8002010-11-07 23:05:16 +000023#include <memory>
Douglas Gregoraaba5e32009-02-04 19:02:06 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// TemplateParameterList Implementation
28//===----------------------------------------------------------------------===//
29
Douglas Gregorddc29e12009-02-06 22:42:48 +000030TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
31 SourceLocation LAngleLoc,
Douglas Gregorbf4ea562009-09-15 16:23:51 +000032 NamedDecl **Params, unsigned NumParams,
Douglas Gregorddc29e12009-02-06 22:42:48 +000033 SourceLocation RAngleLoc)
34 : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
35 NumParams(NumParams) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +000036 for (unsigned Idx = 0; Idx < NumParams; ++Idx)
37 begin()[Idx] = Params[Idx];
38}
39
40TemplateParameterList *
Jay Foad4ba2a172011-01-12 09:06:06 +000041TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
Douglas Gregorbf4ea562009-09-15 16:23:51 +000042 SourceLocation LAngleLoc, NamedDecl **Params,
Douglas Gregorddc29e12009-02-06 22:42:48 +000043 unsigned NumParams, SourceLocation RAngleLoc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +000044 unsigned Size = sizeof(TemplateParameterList)
45 + sizeof(NamedDecl *) * NumParams;
Douglas Gregoraaba5e32009-02-04 19:02:06 +000046 unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment;
47 void *Mem = C.Allocate(Size, Align);
Mike Stump1eb44332009-09-09 15:08:12 +000048 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
Douglas Gregorddc29e12009-02-06 22:42:48 +000049 NumParams, RAngleLoc);
Douglas Gregoraaba5e32009-02-04 19:02:06 +000050}
51
Douglas Gregor62cb18d2009-02-11 18:16:40 +000052unsigned TemplateParameterList::getMinRequiredArguments() const {
Douglas Gregor6952f1e2011-01-19 20:10:05 +000053 unsigned NumRequiredArgs = 0;
54 for (iterator P = const_cast<TemplateParameterList *>(this)->begin(),
55 PEnd = const_cast<TemplateParameterList *>(this)->end();
56 P != PEnd; ++P) {
57 if ((*P)->isTemplateParameterPack()) {
58 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
59 if (NTTP->isExpandedParameterPack()) {
60 NumRequiredArgs += NTTP->getNumExpansionTypes();
61 continue;
62 }
63
Douglas Gregor62cb18d2009-02-11 18:16:40 +000064 break;
Douglas Gregor6952f1e2011-01-19 20:10:05 +000065 }
66
67 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
68 if (TTP->hasDefaultArgument())
69 break;
70 } else if (NonTypeTemplateParmDecl *NTTP
71 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
72 if (NTTP->hasDefaultArgument())
73 break;
74 } else if (cast<TemplateTemplateParmDecl>(*P)->hasDefaultArgument())
75 break;
76
77 ++NumRequiredArgs;
Douglas Gregor62cb18d2009-02-11 18:16:40 +000078 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +000079
Douglas Gregor62cb18d2009-02-11 18:16:40 +000080 return NumRequiredArgs;
81}
82
Douglas Gregored9c0f92009-10-29 00:04:11 +000083unsigned TemplateParameterList::getDepth() const {
84 if (size() == 0)
85 return 0;
86
87 const NamedDecl *FirstParm = getParam(0);
88 if (const TemplateTypeParmDecl *TTP
89 = dyn_cast<TemplateTypeParmDecl>(FirstParm))
90 return TTP->getDepth();
91 else if (const NonTypeTemplateParmDecl *NTTP
92 = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
93 return NTTP->getDepth();
94 else
95 return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
96}
97
Douglas Gregor787a40d2011-03-04 18:32:38 +000098static void AdoptTemplateParameterList(TemplateParameterList *Params,
99 DeclContext *Owner) {
100 for (TemplateParameterList::iterator P = Params->begin(),
101 PEnd = Params->end();
102 P != PEnd; ++P) {
103 (*P)->setDeclContext(Owner);
104
105 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*P))
106 AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner);
107 }
108}
109
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000110//===----------------------------------------------------------------------===//
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000111// RedeclarableTemplateDecl Implementation
112//===----------------------------------------------------------------------===//
113
114RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() {
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000115 if (!Common) {
116 // Walk the previous-declaration chain until we either find a declaration
117 // with a common pointer or we run out of previous declarations.
118 llvm::SmallVector<RedeclarableTemplateDecl *, 2> PrevDecls;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000119 for (RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
120 Prev = Prev->getPreviousDecl()) {
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000121 if (Prev->Common) {
122 Common = Prev->Common;
123 break;
124 }
125
126 PrevDecls.push_back(Prev);
127 }
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000128
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000129 // If we never found a common pointer, allocate one now.
Douglas Gregor8a8950b2012-01-14 15:30:55 +0000130 if (!Common) {
131 // FIXME: If any of the declarations is from an AST file, we probably
132 // need an update record to add the common data.
133
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000134 Common = newCommon(getASTContext());
Douglas Gregor8a8950b2012-01-14 15:30:55 +0000135 }
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000136
137 // Update any previous declarations we saw with the common pointer.
138 for (unsigned I = 0, N = PrevDecls.size(); I != N; ++I)
139 PrevDecls[I]->Common = Common;
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000140 }
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000141
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000142 return Common;
Peter Collingbournef88718e2010-07-29 16:12:09 +0000143}
144
Peter Collingbourne40485902010-07-30 17:09:04 +0000145template <class EntryType>
146typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType*
147RedeclarableTemplateDecl::findSpecializationImpl(
Chandler Carruthd964d632012-05-03 23:49:05 +0000148 llvm::FoldingSetVector<EntryType> &Specs,
Peter Collingbourne40485902010-07-30 17:09:04 +0000149 const TemplateArgument *Args, unsigned NumArgs,
150 void *&InsertPos) {
151 typedef SpecEntryTraits<EntryType> SETraits;
152 llvm::FoldingSetNodeID ID;
153 EntryType::Profile(ID,Args,NumArgs, getASTContext());
154 EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000155 return Entry ? SETraits::getMostRecentDecl(Entry) : 0;
Peter Collingbourne40485902010-07-30 17:09:04 +0000156}
157
Douglas Gregorc494f772011-03-05 17:54:25 +0000158/// \brief Generate the injected template arguments for the given template
159/// parameter list, e.g., for the injected-class-name of a class template.
160static void GenerateInjectedTemplateArgs(ASTContext &Context,
161 TemplateParameterList *Params,
162 TemplateArgument *Args) {
163 for (TemplateParameterList::iterator Param = Params->begin(),
164 ParamEnd = Params->end();
165 Param != ParamEnd; ++Param) {
166 TemplateArgument Arg;
167 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
168 QualType ArgType = Context.getTypeDeclType(TTP);
169 if (TTP->isParameterPack())
170 ArgType = Context.getPackExpansionType(ArgType,
171 llvm::Optional<unsigned>());
172
173 Arg = TemplateArgument(ArgType);
174 } else if (NonTypeTemplateParmDecl *NTTP =
175 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
John McCallf4b88a42012-03-10 09:33:50 +0000176 Expr *E = new (Context) DeclRefExpr(NTTP, /*enclosing*/ false,
Douglas Gregorc494f772011-03-05 17:54:25 +0000177 NTTP->getType().getNonLValueExprType(Context),
178 Expr::getValueKindForType(NTTP->getType()),
179 NTTP->getLocation());
180
181 if (NTTP->isParameterPack())
182 E = new (Context) PackExpansionExpr(Context.DependentTy, E,
183 NTTP->getLocation(),
184 llvm::Optional<unsigned>());
185 Arg = TemplateArgument(E);
186 } else {
187 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
188 if (TTP->isParameterPack())
189 Arg = TemplateArgument(TemplateName(TTP), llvm::Optional<unsigned>());
190 else
191 Arg = TemplateArgument(TemplateName(TTP));
192 }
193
194 if ((*Param)->isTemplateParameterPack())
195 Arg = TemplateArgument::CreatePackCopy(Context, &Arg, 1);
196
197 *Args++ = Arg;
198 }
199}
200
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000201//===----------------------------------------------------------------------===//
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000202// FunctionTemplateDecl Implementation
203//===----------------------------------------------------------------------===//
204
Douglas Gregor00545312010-05-23 18:26:36 +0000205void FunctionTemplateDecl::DeallocateCommon(void *Ptr) {
206 static_cast<Common *>(Ptr)->~Common();
207}
208
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000209FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
210 DeclContext *DC,
211 SourceLocation L,
212 DeclarationName Name,
Douglas Gregor127102b2009-06-29 20:59:39 +0000213 TemplateParameterList *Params,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000214 NamedDecl *Decl) {
Douglas Gregor787a40d2011-03-04 18:32:38 +0000215 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000216 return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl);
217}
218
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000219FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
220 unsigned ID) {
221 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionTemplateDecl));
222 return new (Mem) FunctionTemplateDecl(0, SourceLocation(), DeclarationName(),
223 0, 0);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000224}
225
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +0000226RedeclarableTemplateDecl::CommonBase *
227FunctionTemplateDecl::newCommon(ASTContext &C) {
228 Common *CommonPtr = new (C) Common;
229 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000230 return CommonPtr;
231}
232
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000233FunctionDecl *
234FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args,
235 unsigned NumArgs, void *&InsertPos) {
Peter Collingbourne40485902010-07-30 17:09:04 +0000236 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000237}
238
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +0000239void FunctionTemplateDecl::addSpecialization(
240 FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
Douglas Gregor1e1e9722012-03-28 14:34:23 +0000241 if (InsertPos)
242 getSpecializations().InsertNode(Info, InsertPos);
243 else
244 getSpecializations().GetOrInsertNode(Info);
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +0000245 if (ASTMutationListener *L = getASTMutationListener())
246 L->AddedCXXTemplateSpecialization(this, Info->Function);
247}
248
Douglas Gregorc494f772011-03-05 17:54:25 +0000249std::pair<const TemplateArgument *, unsigned>
250FunctionTemplateDecl::getInjectedTemplateArgs() {
251 TemplateParameterList *Params = getTemplateParameters();
252 Common *CommonPtr = getCommonPtr();
253 if (!CommonPtr->InjectedArgs) {
254 CommonPtr->InjectedArgs
255 = new (getASTContext()) TemplateArgument [Params->size()];
256 GenerateInjectedTemplateArgs(getASTContext(), Params,
257 CommonPtr->InjectedArgs);
258 }
259
260 return std::make_pair(CommonPtr->InjectedArgs, Params->size());
261}
262
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000263//===----------------------------------------------------------------------===//
264// ClassTemplateDecl Implementation
265//===----------------------------------------------------------------------===//
266
Douglas Gregor00545312010-05-23 18:26:36 +0000267void ClassTemplateDecl::DeallocateCommon(void *Ptr) {
268 static_cast<Common *>(Ptr)->~Common();
269}
270
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000271ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
272 DeclContext *DC,
273 SourceLocation L,
274 DeclarationName Name,
275 TemplateParameterList *Params,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000276 NamedDecl *Decl,
277 ClassTemplateDecl *PrevDecl) {
Douglas Gregor787a40d2011-03-04 18:32:38 +0000278 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000279 ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl);
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000280 New->setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000281 return New;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000282}
283
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000284ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
285 unsigned ID) {
286 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ClassTemplateDecl));
287 return new (Mem) ClassTemplateDecl(EmptyShell());
Douglas Gregor9a299e02011-03-04 17:52:15 +0000288}
289
Douglas Gregorc8e5cf82010-10-27 22:21:36 +0000290void ClassTemplateDecl::LoadLazySpecializations() {
291 Common *CommonPtr = getCommonPtr();
292 if (CommonPtr->LazySpecializations) {
293 ASTContext &Context = getASTContext();
294 uint32_t *Specs = CommonPtr->LazySpecializations;
295 CommonPtr->LazySpecializations = 0;
296 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
297 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
298 }
299}
300
Chandler Carruthd964d632012-05-03 23:49:05 +0000301llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
Douglas Gregorc8e5cf82010-10-27 22:21:36 +0000302ClassTemplateDecl::getSpecializations() {
303 LoadLazySpecializations();
304 return getCommonPtr()->Specializations;
305}
306
Chandler Carruthd964d632012-05-03 23:49:05 +0000307llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
Douglas Gregorc8e5cf82010-10-27 22:21:36 +0000308ClassTemplateDecl::getPartialSpecializations() {
309 LoadLazySpecializations();
310 return getCommonPtr()->PartialSpecializations;
311}
312
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +0000313RedeclarableTemplateDecl::CommonBase *
314ClassTemplateDecl::newCommon(ASTContext &C) {
315 Common *CommonPtr = new (C) Common;
316 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000317 return CommonPtr;
318}
319
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000320ClassTemplateSpecializationDecl *
321ClassTemplateDecl::findSpecialization(const TemplateArgument *Args,
322 unsigned NumArgs, void *&InsertPos) {
Peter Collingbourne40485902010-07-30 17:09:04 +0000323 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000324}
325
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000326void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
327 void *InsertPos) {
Douglas Gregor1e1e9722012-03-28 14:34:23 +0000328 if (InsertPos)
329 getSpecializations().InsertNode(D, InsertPos);
330 else {
331 ClassTemplateSpecializationDecl *Existing
332 = getSpecializations().GetOrInsertNode(D);
333 (void)Existing;
334 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
335 }
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000336 if (ASTMutationListener *L = getASTMutationListener())
337 L->AddedCXXTemplateSpecialization(this, D);
338}
339
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000340ClassTemplatePartialSpecializationDecl *
341ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
342 unsigned NumArgs,
343 void *&InsertPos) {
Peter Collingbourne40485902010-07-30 17:09:04 +0000344 return findSpecializationImpl(getPartialSpecializations(), Args, NumArgs,
345 InsertPos);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000346}
347
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000348void ClassTemplateDecl::AddPartialSpecialization(
349 ClassTemplatePartialSpecializationDecl *D,
350 void *InsertPos) {
Douglas Gregor1e1e9722012-03-28 14:34:23 +0000351 if (InsertPos)
352 getPartialSpecializations().InsertNode(D, InsertPos);
353 else {
354 ClassTemplatePartialSpecializationDecl *Existing
355 = getPartialSpecializations().GetOrInsertNode(D);
356 (void)Existing;
357 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
358 }
359
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000360 if (ASTMutationListener *L = getASTMutationListener())
361 L->AddedCXXTemplateSpecialization(this, D);
362}
363
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000364void ClassTemplateDecl::getPartialSpecializations(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000365 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
Chandler Carruthd964d632012-05-03 23:49:05 +0000366 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000367 = getPartialSpecializations();
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000368 PS.clear();
369 PS.resize(PartialSpecs.size());
Chandler Carruthd964d632012-05-03 23:49:05 +0000370 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000371 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
372 P != PEnd; ++P) {
373 assert(!PS[P->getSequenceNumber()]);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000374 PS[P->getSequenceNumber()] = P->getMostRecentDecl();
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000375 }
376}
377
Douglas Gregorb88e8882009-07-30 17:40:51 +0000378ClassTemplatePartialSpecializationDecl *
379ClassTemplateDecl::findPartialSpecialization(QualType T) {
380 ASTContext &Context = getASTContext();
Chandler Carruthd964d632012-05-03 23:49:05 +0000381 using llvm::FoldingSetVector;
382 typedef FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorb88e8882009-07-30 17:40:51 +0000383 partial_spec_iterator;
384 for (partial_spec_iterator P = getPartialSpecializations().begin(),
385 PEnd = getPartialSpecializations().end();
386 P != PEnd; ++P) {
John McCall31f17ec2010-04-27 00:57:59 +0000387 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregoref96ee02012-01-14 16:38:05 +0000388 return P->getMostRecentDecl();
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000389 }
390
391 return 0;
392}
393
394ClassTemplatePartialSpecializationDecl *
395ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
396 ClassTemplatePartialSpecializationDecl *D) {
397 Decl *DCanon = D->getCanonicalDecl();
Chandler Carruthd964d632012-05-03 23:49:05 +0000398 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000399 P = getPartialSpecializations().begin(),
400 PEnd = getPartialSpecializations().end();
401 P != PEnd; ++P) {
402 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
Douglas Gregoref96ee02012-01-14 16:38:05 +0000403 return P->getMostRecentDecl();
Douglas Gregorb88e8882009-07-30 17:40:51 +0000404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Douglas Gregorb88e8882009-07-30 17:40:51 +0000406 return 0;
407}
408
John McCall3cb0ebd2010-03-10 03:28:59 +0000409QualType
Douglas Gregor24bae922010-07-08 18:37:38 +0000410ClassTemplateDecl::getInjectedClassNameSpecialization() {
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000411 Common *CommonPtr = getCommonPtr();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000412 if (!CommonPtr->InjectedClassNameType.isNull())
413 return CommonPtr->InjectedClassNameType;
414
Douglas Gregorb7d09d62010-12-23 16:00:30 +0000415 // C++0x [temp.dep.type]p2:
416 // The template argument list of a primary template is a template argument
417 // list in which the nth template argument has the value of the nth template
418 // parameter of the class template. If the nth template parameter is a
419 // template parameter pack (14.5.3), the nth template argument is a pack
420 // expansion (14.5.3) whose pattern is the name of the template parameter
421 // pack.
Douglas Gregor24bae922010-07-08 18:37:38 +0000422 ASTContext &Context = getASTContext();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000423 TemplateParameterList *Params = getTemplateParameters();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000424 SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregorc494f772011-03-05 17:54:25 +0000425 TemplateArgs.resize(Params->size());
426 GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000427 CommonPtr->InjectedClassNameType
Douglas Gregor1275ae02009-07-28 23:00:59 +0000428 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregor7da97d02009-05-10 22:57:19 +0000429 &TemplateArgs[0],
Douglas Gregor1275ae02009-07-28 23:00:59 +0000430 TemplateArgs.size());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000431 return CommonPtr->InjectedClassNameType;
432}
433
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000434//===----------------------------------------------------------------------===//
435// TemplateTypeParm Allocation/Deallocation Method Implementations
436//===----------------------------------------------------------------------===//
437
438TemplateTypeParmDecl *
Jay Foad4ba2a172011-01-12 09:06:06 +0000439TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +0000440 SourceLocation KeyLoc, SourceLocation NameLoc,
441 unsigned D, unsigned P, IdentifierInfo *Id,
442 bool Typename, bool ParameterPack) {
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000443 TemplateTypeParmDecl *TTPDecl =
444 new (C) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
445 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
446 TTPDecl->TypeForDecl = TTPType.getTypePtr();
447 return TTPDecl;
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000448}
449
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000450TemplateTypeParmDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000451TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
452 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTypeParmDecl));
453 return new (Mem) TemplateTypeParmDecl(0, SourceLocation(), SourceLocation(),
454 0, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000455}
456
John McCall833ca992009-10-29 08:12:44 +0000457SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara77d4ee22011-03-04 12:42:03 +0000458 return hasDefaultArgument()
459 ? DefaultArgument->getTypeLoc().getBeginLoc()
460 : SourceLocation();
461}
462
463SourceRange TemplateTypeParmDecl::getSourceRange() const {
464 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnara344577e2011-03-06 15:48:19 +0000465 return SourceRange(getLocStart(),
Abramo Bagnara77d4ee22011-03-04 12:42:03 +0000466 DefaultArgument->getTypeLoc().getEndLoc());
467 else
Abramo Bagnara344577e2011-03-06 15:48:19 +0000468 return TypeDecl::getSourceRange();
John McCall833ca992009-10-29 08:12:44 +0000469}
470
Douglas Gregored9c0f92009-10-29 00:04:11 +0000471unsigned TemplateTypeParmDecl::getDepth() const {
472 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
473}
474
475unsigned TemplateTypeParmDecl::getIndex() const {
476 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
477}
478
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000479bool TemplateTypeParmDecl::isParameterPack() const {
480 return TypeForDecl->getAs<TemplateTypeParmType>()->isParameterPack();
481}
482
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000483//===----------------------------------------------------------------------===//
484// NonTypeTemplateParmDecl Method Implementations
485//===----------------------------------------------------------------------===//
486
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000487NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000488 SourceLocation StartLoc,
489 SourceLocation IdLoc,
490 unsigned D, unsigned P,
491 IdentifierInfo *Id,
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000492 QualType T,
493 TypeSourceInfo *TInfo,
494 const QualType *ExpandedTypes,
495 unsigned NumExpandedTypes,
496 TypeSourceInfo **ExpandedTInfos)
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000497 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000498 TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false),
499 ParameterPack(true), ExpandedParameterPack(true),
500 NumExpandedTypes(NumExpandedTypes)
501{
502 if (ExpandedTypes && ExpandedTInfos) {
503 void **TypesAndInfos = reinterpret_cast<void **>(this + 1);
504 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
505 TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr();
506 TypesAndInfos[2*I + 1] = ExpandedTInfos[I];
507 }
508 }
509}
510
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000511NonTypeTemplateParmDecl *
Jay Foad4ba2a172011-01-12 09:06:06 +0000512NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000513 SourceLocation StartLoc, SourceLocation IdLoc,
514 unsigned D, unsigned P, IdentifierInfo *Id,
515 QualType T, bool ParameterPack,
516 TypeSourceInfo *TInfo) {
517 return new (C) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
518 T, ParameterPack, TInfo);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000519}
520
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000521NonTypeTemplateParmDecl *
522NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000523 SourceLocation StartLoc, SourceLocation IdLoc,
524 unsigned D, unsigned P,
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000525 IdentifierInfo *Id, QualType T,
526 TypeSourceInfo *TInfo,
527 const QualType *ExpandedTypes,
528 unsigned NumExpandedTypes,
529 TypeSourceInfo **ExpandedTInfos) {
530 unsigned Size = sizeof(NonTypeTemplateParmDecl)
531 + NumExpandedTypes * 2 * sizeof(void*);
532 void *Mem = C.Allocate(Size);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000533 return new (Mem) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc,
534 D, P, Id, T, TInfo,
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000535 ExpandedTypes, NumExpandedTypes,
536 ExpandedTInfos);
537}
538
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000539NonTypeTemplateParmDecl *
540NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
541 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NonTypeTemplateParmDecl));
542 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
543 SourceLocation(), 0, 0, 0,
544 QualType(), false, 0);
545}
546
547NonTypeTemplateParmDecl *
548NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
549 unsigned NumExpandedTypes) {
550 unsigned Size = sizeof(NonTypeTemplateParmDecl)
551 + NumExpandedTypes * 2 * sizeof(void*);
552
553 void *Mem = AllocateDeserializedDecl(C, ID, Size);
554 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
555 SourceLocation(), 0, 0, 0,
556 QualType(), 0, 0, NumExpandedTypes,
557 0);
558}
559
John McCall76a40212011-02-09 01:13:10 +0000560SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnaraee4bfd42011-03-04 11:03:48 +0000561 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000562 return SourceRange(getOuterLocStart(),
563 getDefaultArgument()->getSourceRange().getEnd());
564 return DeclaratorDecl::getSourceRange();
John McCall76a40212011-02-09 01:13:10 +0000565}
566
Douglas Gregord684b002009-02-10 19:49:53 +0000567SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +0000568 return hasDefaultArgument()
569 ? getDefaultArgument()->getSourceRange().getBegin()
570 : SourceLocation();
Douglas Gregord684b002009-02-10 19:49:53 +0000571}
572
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000573//===----------------------------------------------------------------------===//
574// TemplateTemplateParmDecl Method Implementations
575//===----------------------------------------------------------------------===//
576
David Blaikie99ba9e32011-12-20 02:48:34 +0000577void TemplateTemplateParmDecl::anchor() { }
578
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000579TemplateTemplateParmDecl *
Jay Foad4ba2a172011-01-12 09:06:06 +0000580TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000581 SourceLocation L, unsigned D, unsigned P,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000582 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000583 TemplateParameterList *Params) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000584 return new (C) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
585 Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000586}
587
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000588TemplateTemplateParmDecl *
589TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
590 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTemplateParmDecl));
591 return new (Mem) TemplateTemplateParmDecl(0, SourceLocation(), 0, 0, false,
592 0, 0);
593}
594
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000595//===----------------------------------------------------------------------===//
Douglas Gregor7e063902009-05-11 23:53:27 +0000596// TemplateArgumentList Implementation
597//===----------------------------------------------------------------------===//
Douglas Gregor910f8002010-11-07 23:05:16 +0000598TemplateArgumentList *
599TemplateArgumentList::CreateCopy(ASTContext &Context,
600 const TemplateArgument *Args,
601 unsigned NumArgs) {
602 std::size_t Size = sizeof(TemplateArgumentList)
603 + NumArgs * sizeof(TemplateArgument);
604 void *Mem = Context.Allocate(Size);
605 TemplateArgument *StoredArgs
606 = reinterpret_cast<TemplateArgument *>(
607 static_cast<TemplateArgumentList *>(Mem) + 1);
608 std::uninitialized_copy(Args, Args + NumArgs, StoredArgs);
609 return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true);
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000610}
611
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +0000612FunctionTemplateSpecializationInfo *
613FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD,
614 FunctionTemplateDecl *Template,
615 TemplateSpecializationKind TSK,
616 const TemplateArgumentList *TemplateArgs,
617 const TemplateArgumentListInfo *TemplateArgsAsWritten,
618 SourceLocation POI) {
619 const ASTTemplateArgumentListInfo *ArgsAsWritten = 0;
620 if (TemplateArgsAsWritten)
621 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
622 *TemplateArgsAsWritten);
623
624 return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK,
625 TemplateArgs,
626 ArgsAsWritten,
627 POI);
628}
629
Douglas Gregor7e063902009-05-11 23:53:27 +0000630//===----------------------------------------------------------------------===//
David Blaikie99ba9e32011-12-20 02:48:34 +0000631// TemplateDecl Implementation
632//===----------------------------------------------------------------------===//
633
634void TemplateDecl::anchor() { }
635
636//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000637// ClassTemplateSpecializationDecl Implementation
638//===----------------------------------------------------------------------===//
639ClassTemplateSpecializationDecl::
Douglas Gregor13c85772010-05-06 00:28:52 +0000640ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000641 DeclContext *DC, SourceLocation StartLoc,
642 SourceLocation IdLoc,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000643 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000644 const TemplateArgument *Args,
645 unsigned NumArgs,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000646 ClassTemplateSpecializationDecl *PrevDecl)
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000647 : CXXRecordDecl(DK, TK, DC, StartLoc, IdLoc,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000648 SpecializedTemplate->getIdentifier(),
649 PrevDecl),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000650 SpecializedTemplate(SpecializedTemplate),
Abramo Bagnarac98971d2010-06-12 07:44:57 +0000651 ExplicitInfo(0),
Douglas Gregor910f8002010-11-07 23:05:16 +0000652 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
Douglas Gregor7e063902009-05-11 23:53:27 +0000653 SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000654}
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000656ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK)
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000657 : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0),
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000658 ExplicitInfo(0),
659 SpecializationKind(TSK_Undeclared) {
660}
661
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000662ClassTemplateSpecializationDecl *
Douglas Gregor13c85772010-05-06 00:28:52 +0000663ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000664 DeclContext *DC,
665 SourceLocation StartLoc,
666 SourceLocation IdLoc,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000667 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000668 const TemplateArgument *Args,
669 unsigned NumArgs,
Douglas Gregorcc636682009-02-17 23:15:12 +0000670 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000671 ClassTemplateSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000672 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000673 ClassTemplateSpecialization,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000674 TK, DC, StartLoc, IdLoc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000675 SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000676 Args, NumArgs,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000677 PrevDecl);
Douglas Gregorcc636682009-02-17 23:15:12 +0000678 Context.getTypeDeclType(Result, PrevDecl);
679 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000680}
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000681
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000682ClassTemplateSpecializationDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000683ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
684 unsigned ID) {
685 void *Mem = AllocateDeserializedDecl(C, ID,
686 sizeof(ClassTemplateSpecializationDecl));
687 return new (Mem) ClassTemplateSpecializationDecl(ClassTemplateSpecialization);
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000688}
689
Douglas Gregorda2142f2011-02-19 18:51:44 +0000690void
691ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
692 const PrintingPolicy &Policy,
693 bool Qualified) const {
694 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
695
696 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
697 S += TemplateSpecializationType::PrintTemplateArgumentList(
698 TemplateArgs.data(),
699 TemplateArgs.size(),
700 Policy);
701}
702
Douglas Gregor37d93e92009-08-02 23:24:31 +0000703ClassTemplateDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000704ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
705 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000706 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
707 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
708 return SpecializedTemplate.get<ClassTemplateDecl*>();
709}
710
Abramo Bagnara4a85a732011-03-04 14:20:30 +0000711SourceRange
712ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnara09d82122011-10-03 20:34:03 +0000713 if (ExplicitInfo) {
714 SourceLocation Begin = getExternLoc();
715 if (Begin.isInvalid())
716 Begin = getTemplateKeywordLoc();
717 SourceLocation End = getRBraceLoc();
718 if (End.isInvalid())
719 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
720 return SourceRange(Begin, End);
721 }
722 else {
723 // No explicit info available.
724 llvm::PointerUnion<ClassTemplateDecl *,
725 ClassTemplatePartialSpecializationDecl *>
726 inst_from = getInstantiatedFrom();
727 if (inst_from.isNull())
728 return getSpecializedTemplate()->getSourceRange();
729 if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>())
730 return ctd->getSourceRange();
731 return inst_from.get<ClassTemplatePartialSpecializationDecl*>()
732 ->getSourceRange();
733 }
Abramo Bagnara4a85a732011-03-04 14:20:30 +0000734}
735
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000736//===----------------------------------------------------------------------===//
737// ClassTemplatePartialSpecializationDecl Implementation
738//===----------------------------------------------------------------------===//
David Blaikie99ba9e32011-12-20 02:48:34 +0000739void ClassTemplatePartialSpecializationDecl::anchor() { }
740
Douglas Gregor9a299e02011-03-04 17:52:15 +0000741ClassTemplatePartialSpecializationDecl::
742ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000743 DeclContext *DC,
744 SourceLocation StartLoc,
745 SourceLocation IdLoc,
Douglas Gregor9a299e02011-03-04 17:52:15 +0000746 TemplateParameterList *Params,
747 ClassTemplateDecl *SpecializedTemplate,
748 const TemplateArgument *Args,
749 unsigned NumArgs,
750 TemplateArgumentLoc *ArgInfos,
751 unsigned NumArgInfos,
752 ClassTemplatePartialSpecializationDecl *PrevDecl,
753 unsigned SequenceNumber)
754 : ClassTemplateSpecializationDecl(Context,
755 ClassTemplatePartialSpecialization,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000756 TK, DC, StartLoc, IdLoc,
757 SpecializedTemplate,
Douglas Gregor9a299e02011-03-04 17:52:15 +0000758 Args, NumArgs, PrevDecl),
759 TemplateParams(Params), ArgsAsWritten(ArgInfos),
760 NumArgsAsWritten(NumArgInfos), SequenceNumber(SequenceNumber),
761 InstantiatedFromMember(0, false)
762{
Douglas Gregor787a40d2011-03-04 18:32:38 +0000763 AdoptTemplateParameterList(Params, this);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000764}
765
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000766ClassTemplatePartialSpecializationDecl *
767ClassTemplatePartialSpecializationDecl::
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000768Create(ASTContext &Context, TagKind TK,DeclContext *DC,
769 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000770 TemplateParameterList *Params,
771 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000772 const TemplateArgument *Args,
773 unsigned NumArgs,
John McCalld5532b62009-11-23 01:53:49 +0000774 const TemplateArgumentListInfo &ArgInfos,
John McCall3cb0ebd2010-03-10 03:28:59 +0000775 QualType CanonInjectedType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000776 ClassTemplatePartialSpecializationDecl *PrevDecl,
777 unsigned SequenceNumber) {
John McCalld5532b62009-11-23 01:53:49 +0000778 unsigned N = ArgInfos.size();
John McCall833ca992009-10-29 08:12:44 +0000779 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
780 for (unsigned I = 0; I != N; ++I)
781 ClonedArgs[I] = ArgInfos[I];
782
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000783 ClassTemplatePartialSpecializationDecl *Result
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000784 = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK, DC,
785 StartLoc, IdLoc,
786 Params,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000787 SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000788 Args, NumArgs,
John McCall833ca992009-10-29 08:12:44 +0000789 ClonedArgs, N,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000790 PrevDecl,
791 SequenceNumber);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000792 Result->setSpecializationKind(TSK_ExplicitSpecialization);
John McCall3cb0ebd2010-03-10 03:28:59 +0000793
794 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000795 return Result;
796}
John McCalldd4a3b02009-09-16 22:47:08 +0000797
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000798ClassTemplatePartialSpecializationDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000799ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
800 unsigned ID) {
801 void *Mem = AllocateDeserializedDecl(C, ID,
802 sizeof(ClassTemplatePartialSpecializationDecl));
803 return new (Mem) ClassTemplatePartialSpecializationDecl();
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000804}
805
John McCalldd4a3b02009-09-16 22:47:08 +0000806//===----------------------------------------------------------------------===//
807// FriendTemplateDecl Implementation
808//===----------------------------------------------------------------------===//
809
David Blaikie99ba9e32011-12-20 02:48:34 +0000810void FriendTemplateDecl::anchor() { }
811
John McCalldd4a3b02009-09-16 22:47:08 +0000812FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
813 DeclContext *DC,
814 SourceLocation L,
815 unsigned NParams,
816 TemplateParameterList **Params,
817 FriendUnion Friend,
818 SourceLocation FLoc) {
819 FriendTemplateDecl *Result
820 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
821 return Result;
822}
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +0000823
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000824FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
825 unsigned ID) {
826 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FriendTemplateDecl));
827 return new (Mem) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +0000828}
Richard Smith3e4c6c42011-05-05 21:57:07 +0000829
830//===----------------------------------------------------------------------===//
831// TypeAliasTemplateDecl Implementation
832//===----------------------------------------------------------------------===//
833
834TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
835 DeclContext *DC,
836 SourceLocation L,
837 DeclarationName Name,
838 TemplateParameterList *Params,
839 NamedDecl *Decl) {
840 AdoptTemplateParameterList(Params, DC);
841 return new (C) TypeAliasTemplateDecl(DC, L, Name, Params, Decl);
842}
843
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000844TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
845 unsigned ID) {
846 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasTemplateDecl));
847 return new (Mem) TypeAliasTemplateDecl(0, SourceLocation(), DeclarationName(),
848 0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000849}
850
851void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) {
852 static_cast<Common *>(Ptr)->~Common();
853}
854RedeclarableTemplateDecl::CommonBase *
855TypeAliasTemplateDecl::newCommon(ASTContext &C) {
856 Common *CommonPtr = new (C) Common;
857 C.AddDeallocation(DeallocateCommon, CommonPtr);
858 return CommonPtr;
859}
860
David Blaikie99ba9e32011-12-20 02:48:34 +0000861//===----------------------------------------------------------------------===//
862// ClassScopeFunctionSpecializationDecl Implementation
863//===----------------------------------------------------------------------===//
864
865void ClassScopeFunctionSpecializationDecl::anchor() { }
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000866
867ClassScopeFunctionSpecializationDecl *
868ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
869 unsigned ID) {
870 void *Mem = AllocateDeserializedDecl(C, ID,
871 sizeof(ClassScopeFunctionSpecializationDecl));
Nico Weber6b020092012-06-25 17:21:05 +0000872 return new (Mem) ClassScopeFunctionSpecializationDecl(0, SourceLocation(), 0,
873 false, TemplateArgumentListInfo());
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000874}