blob: af2927977d22ac7075082ebab3c1f7be6e7027ac [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(
148 llvm::FoldingSet<EntryType> &Specs,
149 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)) {
176 Expr *E = new (Context) DeclRefExpr(NTTP,
177 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) {
241 getSpecializations().InsertNode(Info, InsertPos);
242 if (ASTMutationListener *L = getASTMutationListener())
243 L->AddedCXXTemplateSpecialization(this, Info->Function);
244}
245
Douglas Gregorc494f772011-03-05 17:54:25 +0000246std::pair<const TemplateArgument *, unsigned>
247FunctionTemplateDecl::getInjectedTemplateArgs() {
248 TemplateParameterList *Params = getTemplateParameters();
249 Common *CommonPtr = getCommonPtr();
250 if (!CommonPtr->InjectedArgs) {
251 CommonPtr->InjectedArgs
252 = new (getASTContext()) TemplateArgument [Params->size()];
253 GenerateInjectedTemplateArgs(getASTContext(), Params,
254 CommonPtr->InjectedArgs);
255 }
256
257 return std::make_pair(CommonPtr->InjectedArgs, Params->size());
258}
259
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000260//===----------------------------------------------------------------------===//
261// ClassTemplateDecl Implementation
262//===----------------------------------------------------------------------===//
263
Douglas Gregor00545312010-05-23 18:26:36 +0000264void ClassTemplateDecl::DeallocateCommon(void *Ptr) {
265 static_cast<Common *>(Ptr)->~Common();
266}
267
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000268ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
269 DeclContext *DC,
270 SourceLocation L,
271 DeclarationName Name,
272 TemplateParameterList *Params,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000273 NamedDecl *Decl,
274 ClassTemplateDecl *PrevDecl) {
Douglas Gregor787a40d2011-03-04 18:32:38 +0000275 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000276 ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl);
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000277 New->setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000278 return New;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000279}
280
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000281ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
282 unsigned ID) {
283 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ClassTemplateDecl));
284 return new (Mem) ClassTemplateDecl(EmptyShell());
Douglas Gregor9a299e02011-03-04 17:52:15 +0000285}
286
Douglas Gregorc8e5cf82010-10-27 22:21:36 +0000287void ClassTemplateDecl::LoadLazySpecializations() {
288 Common *CommonPtr = getCommonPtr();
289 if (CommonPtr->LazySpecializations) {
290 ASTContext &Context = getASTContext();
291 uint32_t *Specs = CommonPtr->LazySpecializations;
292 CommonPtr->LazySpecializations = 0;
293 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
294 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
295 }
296}
297
298llvm::FoldingSet<ClassTemplateSpecializationDecl> &
299ClassTemplateDecl::getSpecializations() {
300 LoadLazySpecializations();
301 return getCommonPtr()->Specializations;
302}
303
304llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &
305ClassTemplateDecl::getPartialSpecializations() {
306 LoadLazySpecializations();
307 return getCommonPtr()->PartialSpecializations;
308}
309
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +0000310RedeclarableTemplateDecl::CommonBase *
311ClassTemplateDecl::newCommon(ASTContext &C) {
312 Common *CommonPtr = new (C) Common;
313 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000314 return CommonPtr;
315}
316
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000317ClassTemplateSpecializationDecl *
318ClassTemplateDecl::findSpecialization(const TemplateArgument *Args,
319 unsigned NumArgs, void *&InsertPos) {
Peter Collingbourne40485902010-07-30 17:09:04 +0000320 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000321}
322
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000323void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
324 void *InsertPos) {
325 getSpecializations().InsertNode(D, InsertPos);
326 if (ASTMutationListener *L = getASTMutationListener())
327 L->AddedCXXTemplateSpecialization(this, D);
328}
329
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000330ClassTemplatePartialSpecializationDecl *
331ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
332 unsigned NumArgs,
333 void *&InsertPos) {
Peter Collingbourne40485902010-07-30 17:09:04 +0000334 return findSpecializationImpl(getPartialSpecializations(), Args, NumArgs,
335 InsertPos);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000336}
337
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000338void ClassTemplateDecl::AddPartialSpecialization(
339 ClassTemplatePartialSpecializationDecl *D,
340 void *InsertPos) {
341 getPartialSpecializations().InsertNode(D, InsertPos);
342 if (ASTMutationListener *L = getASTMutationListener())
343 L->AddedCXXTemplateSpecialization(this, D);
344}
345
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000346void ClassTemplateDecl::getPartialSpecializations(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000347 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000348 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000349 = getPartialSpecializations();
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000350 PS.clear();
351 PS.resize(PartialSpecs.size());
352 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
353 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
354 P != PEnd; ++P) {
355 assert(!PS[P->getSequenceNumber()]);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000356 PS[P->getSequenceNumber()] = P->getMostRecentDecl();
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000357 }
358}
359
Douglas Gregorb88e8882009-07-30 17:40:51 +0000360ClassTemplatePartialSpecializationDecl *
361ClassTemplateDecl::findPartialSpecialization(QualType T) {
362 ASTContext &Context = getASTContext();
363 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
364 partial_spec_iterator;
365 for (partial_spec_iterator P = getPartialSpecializations().begin(),
366 PEnd = getPartialSpecializations().end();
367 P != PEnd; ++P) {
John McCall31f17ec2010-04-27 00:57:59 +0000368 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregoref96ee02012-01-14 16:38:05 +0000369 return P->getMostRecentDecl();
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000370 }
371
372 return 0;
373}
374
375ClassTemplatePartialSpecializationDecl *
376ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
377 ClassTemplatePartialSpecializationDecl *D) {
378 Decl *DCanon = D->getCanonicalDecl();
379 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
380 P = getPartialSpecializations().begin(),
381 PEnd = getPartialSpecializations().end();
382 P != PEnd; ++P) {
383 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
Douglas Gregoref96ee02012-01-14 16:38:05 +0000384 return P->getMostRecentDecl();
Douglas Gregorb88e8882009-07-30 17:40:51 +0000385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregorb88e8882009-07-30 17:40:51 +0000387 return 0;
388}
389
John McCall3cb0ebd2010-03-10 03:28:59 +0000390QualType
Douglas Gregor24bae922010-07-08 18:37:38 +0000391ClassTemplateDecl::getInjectedClassNameSpecialization() {
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000392 Common *CommonPtr = getCommonPtr();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000393 if (!CommonPtr->InjectedClassNameType.isNull())
394 return CommonPtr->InjectedClassNameType;
395
Douglas Gregorb7d09d62010-12-23 16:00:30 +0000396 // C++0x [temp.dep.type]p2:
397 // The template argument list of a primary template is a template argument
398 // list in which the nth template argument has the value of the nth template
399 // parameter of the class template. If the nth template parameter is a
400 // template parameter pack (14.5.3), the nth template argument is a pack
401 // expansion (14.5.3) whose pattern is the name of the template parameter
402 // pack.
Douglas Gregor24bae922010-07-08 18:37:38 +0000403 ASTContext &Context = getASTContext();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000404 TemplateParameterList *Params = getTemplateParameters();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000405 SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregorc494f772011-03-05 17:54:25 +0000406 TemplateArgs.resize(Params->size());
407 GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000408 CommonPtr->InjectedClassNameType
Douglas Gregor1275ae02009-07-28 23:00:59 +0000409 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregor7da97d02009-05-10 22:57:19 +0000410 &TemplateArgs[0],
Douglas Gregor1275ae02009-07-28 23:00:59 +0000411 TemplateArgs.size());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000412 return CommonPtr->InjectedClassNameType;
413}
414
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000415//===----------------------------------------------------------------------===//
416// TemplateTypeParm Allocation/Deallocation Method Implementations
417//===----------------------------------------------------------------------===//
418
419TemplateTypeParmDecl *
Jay Foad4ba2a172011-01-12 09:06:06 +0000420TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +0000421 SourceLocation KeyLoc, SourceLocation NameLoc,
422 unsigned D, unsigned P, IdentifierInfo *Id,
423 bool Typename, bool ParameterPack) {
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000424 TemplateTypeParmDecl *TTPDecl =
425 new (C) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
426 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
427 TTPDecl->TypeForDecl = TTPType.getTypePtr();
428 return TTPDecl;
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000429}
430
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000431TemplateTypeParmDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000432TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
433 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTypeParmDecl));
434 return new (Mem) TemplateTypeParmDecl(0, SourceLocation(), SourceLocation(),
435 0, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000436}
437
John McCall833ca992009-10-29 08:12:44 +0000438SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara77d4ee22011-03-04 12:42:03 +0000439 return hasDefaultArgument()
440 ? DefaultArgument->getTypeLoc().getBeginLoc()
441 : SourceLocation();
442}
443
444SourceRange TemplateTypeParmDecl::getSourceRange() const {
445 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnara344577e2011-03-06 15:48:19 +0000446 return SourceRange(getLocStart(),
Abramo Bagnara77d4ee22011-03-04 12:42:03 +0000447 DefaultArgument->getTypeLoc().getEndLoc());
448 else
Abramo Bagnara344577e2011-03-06 15:48:19 +0000449 return TypeDecl::getSourceRange();
John McCall833ca992009-10-29 08:12:44 +0000450}
451
Douglas Gregored9c0f92009-10-29 00:04:11 +0000452unsigned TemplateTypeParmDecl::getDepth() const {
453 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
454}
455
456unsigned TemplateTypeParmDecl::getIndex() const {
457 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
458}
459
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000460bool TemplateTypeParmDecl::isParameterPack() const {
461 return TypeForDecl->getAs<TemplateTypeParmType>()->isParameterPack();
462}
463
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000464//===----------------------------------------------------------------------===//
465// NonTypeTemplateParmDecl Method Implementations
466//===----------------------------------------------------------------------===//
467
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000468NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000469 SourceLocation StartLoc,
470 SourceLocation IdLoc,
471 unsigned D, unsigned P,
472 IdentifierInfo *Id,
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000473 QualType T,
474 TypeSourceInfo *TInfo,
475 const QualType *ExpandedTypes,
476 unsigned NumExpandedTypes,
477 TypeSourceInfo **ExpandedTInfos)
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000478 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000479 TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false),
480 ParameterPack(true), ExpandedParameterPack(true),
481 NumExpandedTypes(NumExpandedTypes)
482{
483 if (ExpandedTypes && ExpandedTInfos) {
484 void **TypesAndInfos = reinterpret_cast<void **>(this + 1);
485 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
486 TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr();
487 TypesAndInfos[2*I + 1] = ExpandedTInfos[I];
488 }
489 }
490}
491
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000492NonTypeTemplateParmDecl *
Jay Foad4ba2a172011-01-12 09:06:06 +0000493NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000494 SourceLocation StartLoc, SourceLocation IdLoc,
495 unsigned D, unsigned P, IdentifierInfo *Id,
496 QualType T, bool ParameterPack,
497 TypeSourceInfo *TInfo) {
498 return new (C) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
499 T, ParameterPack, TInfo);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000500}
501
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000502NonTypeTemplateParmDecl *
503NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000504 SourceLocation StartLoc, SourceLocation IdLoc,
505 unsigned D, unsigned P,
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000506 IdentifierInfo *Id, QualType T,
507 TypeSourceInfo *TInfo,
508 const QualType *ExpandedTypes,
509 unsigned NumExpandedTypes,
510 TypeSourceInfo **ExpandedTInfos) {
511 unsigned Size = sizeof(NonTypeTemplateParmDecl)
512 + NumExpandedTypes * 2 * sizeof(void*);
513 void *Mem = C.Allocate(Size);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000514 return new (Mem) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc,
515 D, P, Id, T, TInfo,
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000516 ExpandedTypes, NumExpandedTypes,
517 ExpandedTInfos);
518}
519
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000520NonTypeTemplateParmDecl *
521NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
522 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NonTypeTemplateParmDecl));
523 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
524 SourceLocation(), 0, 0, 0,
525 QualType(), false, 0);
526}
527
528NonTypeTemplateParmDecl *
529NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
530 unsigned NumExpandedTypes) {
531 unsigned Size = sizeof(NonTypeTemplateParmDecl)
532 + NumExpandedTypes * 2 * sizeof(void*);
533
534 void *Mem = AllocateDeserializedDecl(C, ID, Size);
535 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
536 SourceLocation(), 0, 0, 0,
537 QualType(), 0, 0, NumExpandedTypes,
538 0);
539}
540
John McCall76a40212011-02-09 01:13:10 +0000541SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnaraee4bfd42011-03-04 11:03:48 +0000542 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000543 return SourceRange(getOuterLocStart(),
544 getDefaultArgument()->getSourceRange().getEnd());
545 return DeclaratorDecl::getSourceRange();
John McCall76a40212011-02-09 01:13:10 +0000546}
547
Douglas Gregord684b002009-02-10 19:49:53 +0000548SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +0000549 return hasDefaultArgument()
550 ? getDefaultArgument()->getSourceRange().getBegin()
551 : SourceLocation();
Douglas Gregord684b002009-02-10 19:49:53 +0000552}
553
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000554//===----------------------------------------------------------------------===//
555// TemplateTemplateParmDecl Method Implementations
556//===----------------------------------------------------------------------===//
557
David Blaikie99ba9e32011-12-20 02:48:34 +0000558void TemplateTemplateParmDecl::anchor() { }
559
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000560TemplateTemplateParmDecl *
Jay Foad4ba2a172011-01-12 09:06:06 +0000561TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000562 SourceLocation L, unsigned D, unsigned P,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000563 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000564 TemplateParameterList *Params) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000565 return new (C) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
566 Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000567}
568
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000569TemplateTemplateParmDecl *
570TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
571 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTemplateParmDecl));
572 return new (Mem) TemplateTemplateParmDecl(0, SourceLocation(), 0, 0, false,
573 0, 0);
574}
575
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000576//===----------------------------------------------------------------------===//
Douglas Gregor7e063902009-05-11 23:53:27 +0000577// TemplateArgumentList Implementation
578//===----------------------------------------------------------------------===//
Douglas Gregor910f8002010-11-07 23:05:16 +0000579TemplateArgumentList *
580TemplateArgumentList::CreateCopy(ASTContext &Context,
581 const TemplateArgument *Args,
582 unsigned NumArgs) {
583 std::size_t Size = sizeof(TemplateArgumentList)
584 + NumArgs * sizeof(TemplateArgument);
585 void *Mem = Context.Allocate(Size);
586 TemplateArgument *StoredArgs
587 = reinterpret_cast<TemplateArgument *>(
588 static_cast<TemplateArgumentList *>(Mem) + 1);
589 std::uninitialized_copy(Args, Args + NumArgs, StoredArgs);
590 return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true);
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000591}
592
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +0000593FunctionTemplateSpecializationInfo *
594FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD,
595 FunctionTemplateDecl *Template,
596 TemplateSpecializationKind TSK,
597 const TemplateArgumentList *TemplateArgs,
598 const TemplateArgumentListInfo *TemplateArgsAsWritten,
599 SourceLocation POI) {
600 const ASTTemplateArgumentListInfo *ArgsAsWritten = 0;
601 if (TemplateArgsAsWritten)
602 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
603 *TemplateArgsAsWritten);
604
605 return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK,
606 TemplateArgs,
607 ArgsAsWritten,
608 POI);
609}
610
Douglas Gregor7e063902009-05-11 23:53:27 +0000611//===----------------------------------------------------------------------===//
David Blaikie99ba9e32011-12-20 02:48:34 +0000612// TemplateDecl Implementation
613//===----------------------------------------------------------------------===//
614
615void TemplateDecl::anchor() { }
616
617//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000618// ClassTemplateSpecializationDecl Implementation
619//===----------------------------------------------------------------------===//
620ClassTemplateSpecializationDecl::
Douglas Gregor13c85772010-05-06 00:28:52 +0000621ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000622 DeclContext *DC, SourceLocation StartLoc,
623 SourceLocation IdLoc,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000624 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000625 const TemplateArgument *Args,
626 unsigned NumArgs,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000627 ClassTemplateSpecializationDecl *PrevDecl)
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000628 : CXXRecordDecl(DK, TK, DC, StartLoc, IdLoc,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000629 SpecializedTemplate->getIdentifier(),
630 PrevDecl),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000631 SpecializedTemplate(SpecializedTemplate),
Abramo Bagnarac98971d2010-06-12 07:44:57 +0000632 ExplicitInfo(0),
Douglas Gregor910f8002010-11-07 23:05:16 +0000633 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
Douglas Gregor7e063902009-05-11 23:53:27 +0000634 SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000635}
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000637ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK)
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000638 : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0),
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000639 ExplicitInfo(0),
640 SpecializationKind(TSK_Undeclared) {
641}
642
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000643ClassTemplateSpecializationDecl *
Douglas Gregor13c85772010-05-06 00:28:52 +0000644ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000645 DeclContext *DC,
646 SourceLocation StartLoc,
647 SourceLocation IdLoc,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000648 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000649 const TemplateArgument *Args,
650 unsigned NumArgs,
Douglas Gregorcc636682009-02-17 23:15:12 +0000651 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000652 ClassTemplateSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000653 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000654 ClassTemplateSpecialization,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000655 TK, DC, StartLoc, IdLoc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000656 SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000657 Args, NumArgs,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000658 PrevDecl);
Douglas Gregorcc636682009-02-17 23:15:12 +0000659 Context.getTypeDeclType(Result, PrevDecl);
660 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000661}
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000662
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000663ClassTemplateSpecializationDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000664ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
665 unsigned ID) {
666 void *Mem = AllocateDeserializedDecl(C, ID,
667 sizeof(ClassTemplateSpecializationDecl));
668 return new (Mem) ClassTemplateSpecializationDecl(ClassTemplateSpecialization);
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000669}
670
Douglas Gregorda2142f2011-02-19 18:51:44 +0000671void
672ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
673 const PrintingPolicy &Policy,
674 bool Qualified) const {
675 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
676
677 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
678 S += TemplateSpecializationType::PrintTemplateArgumentList(
679 TemplateArgs.data(),
680 TemplateArgs.size(),
681 Policy);
682}
683
Douglas Gregor37d93e92009-08-02 23:24:31 +0000684ClassTemplateDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000685ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
686 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000687 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
688 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
689 return SpecializedTemplate.get<ClassTemplateDecl*>();
690}
691
Abramo Bagnara4a85a732011-03-04 14:20:30 +0000692SourceRange
693ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnara09d82122011-10-03 20:34:03 +0000694 if (ExplicitInfo) {
695 SourceLocation Begin = getExternLoc();
696 if (Begin.isInvalid())
697 Begin = getTemplateKeywordLoc();
698 SourceLocation End = getRBraceLoc();
699 if (End.isInvalid())
700 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
701 return SourceRange(Begin, End);
702 }
703 else {
704 // No explicit info available.
705 llvm::PointerUnion<ClassTemplateDecl *,
706 ClassTemplatePartialSpecializationDecl *>
707 inst_from = getInstantiatedFrom();
708 if (inst_from.isNull())
709 return getSpecializedTemplate()->getSourceRange();
710 if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>())
711 return ctd->getSourceRange();
712 return inst_from.get<ClassTemplatePartialSpecializationDecl*>()
713 ->getSourceRange();
714 }
Abramo Bagnara4a85a732011-03-04 14:20:30 +0000715}
716
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000717//===----------------------------------------------------------------------===//
718// ClassTemplatePartialSpecializationDecl Implementation
719//===----------------------------------------------------------------------===//
David Blaikie99ba9e32011-12-20 02:48:34 +0000720void ClassTemplatePartialSpecializationDecl::anchor() { }
721
Douglas Gregor9a299e02011-03-04 17:52:15 +0000722ClassTemplatePartialSpecializationDecl::
723ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000724 DeclContext *DC,
725 SourceLocation StartLoc,
726 SourceLocation IdLoc,
Douglas Gregor9a299e02011-03-04 17:52:15 +0000727 TemplateParameterList *Params,
728 ClassTemplateDecl *SpecializedTemplate,
729 const TemplateArgument *Args,
730 unsigned NumArgs,
731 TemplateArgumentLoc *ArgInfos,
732 unsigned NumArgInfos,
733 ClassTemplatePartialSpecializationDecl *PrevDecl,
734 unsigned SequenceNumber)
735 : ClassTemplateSpecializationDecl(Context,
736 ClassTemplatePartialSpecialization,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000737 TK, DC, StartLoc, IdLoc,
738 SpecializedTemplate,
Douglas Gregor9a299e02011-03-04 17:52:15 +0000739 Args, NumArgs, PrevDecl),
740 TemplateParams(Params), ArgsAsWritten(ArgInfos),
741 NumArgsAsWritten(NumArgInfos), SequenceNumber(SequenceNumber),
742 InstantiatedFromMember(0, false)
743{
Douglas Gregor787a40d2011-03-04 18:32:38 +0000744 AdoptTemplateParameterList(Params, this);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000745}
746
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000747ClassTemplatePartialSpecializationDecl *
748ClassTemplatePartialSpecializationDecl::
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000749Create(ASTContext &Context, TagKind TK,DeclContext *DC,
750 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000751 TemplateParameterList *Params,
752 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000753 const TemplateArgument *Args,
754 unsigned NumArgs,
John McCalld5532b62009-11-23 01:53:49 +0000755 const TemplateArgumentListInfo &ArgInfos,
John McCall3cb0ebd2010-03-10 03:28:59 +0000756 QualType CanonInjectedType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000757 ClassTemplatePartialSpecializationDecl *PrevDecl,
758 unsigned SequenceNumber) {
John McCalld5532b62009-11-23 01:53:49 +0000759 unsigned N = ArgInfos.size();
John McCall833ca992009-10-29 08:12:44 +0000760 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
761 for (unsigned I = 0; I != N; ++I)
762 ClonedArgs[I] = ArgInfos[I];
763
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000764 ClassTemplatePartialSpecializationDecl *Result
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000765 = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK, DC,
766 StartLoc, IdLoc,
767 Params,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000768 SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000769 Args, NumArgs,
John McCall833ca992009-10-29 08:12:44 +0000770 ClonedArgs, N,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000771 PrevDecl,
772 SequenceNumber);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000773 Result->setSpecializationKind(TSK_ExplicitSpecialization);
John McCall3cb0ebd2010-03-10 03:28:59 +0000774
775 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000776 return Result;
777}
John McCalldd4a3b02009-09-16 22:47:08 +0000778
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000779ClassTemplatePartialSpecializationDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000780ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
781 unsigned ID) {
782 void *Mem = AllocateDeserializedDecl(C, ID,
783 sizeof(ClassTemplatePartialSpecializationDecl));
784 return new (Mem) ClassTemplatePartialSpecializationDecl();
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000785}
786
John McCalldd4a3b02009-09-16 22:47:08 +0000787//===----------------------------------------------------------------------===//
788// FriendTemplateDecl Implementation
789//===----------------------------------------------------------------------===//
790
David Blaikie99ba9e32011-12-20 02:48:34 +0000791void FriendTemplateDecl::anchor() { }
792
John McCalldd4a3b02009-09-16 22:47:08 +0000793FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
794 DeclContext *DC,
795 SourceLocation L,
796 unsigned NParams,
797 TemplateParameterList **Params,
798 FriendUnion Friend,
799 SourceLocation FLoc) {
800 FriendTemplateDecl *Result
801 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
802 return Result;
803}
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +0000804
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000805FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
806 unsigned ID) {
807 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FriendTemplateDecl));
808 return new (Mem) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +0000809}
Richard Smith3e4c6c42011-05-05 21:57:07 +0000810
811//===----------------------------------------------------------------------===//
812// TypeAliasTemplateDecl Implementation
813//===----------------------------------------------------------------------===//
814
815TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
816 DeclContext *DC,
817 SourceLocation L,
818 DeclarationName Name,
819 TemplateParameterList *Params,
820 NamedDecl *Decl) {
821 AdoptTemplateParameterList(Params, DC);
822 return new (C) TypeAliasTemplateDecl(DC, L, Name, Params, Decl);
823}
824
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000825TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
826 unsigned ID) {
827 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasTemplateDecl));
828 return new (Mem) TypeAliasTemplateDecl(0, SourceLocation(), DeclarationName(),
829 0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000830}
831
832void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) {
833 static_cast<Common *>(Ptr)->~Common();
834}
835RedeclarableTemplateDecl::CommonBase *
836TypeAliasTemplateDecl::newCommon(ASTContext &C) {
837 Common *CommonPtr = new (C) Common;
838 C.AddDeallocation(DeallocateCommon, CommonPtr);
839 return CommonPtr;
840}
841
David Blaikie99ba9e32011-12-20 02:48:34 +0000842//===----------------------------------------------------------------------===//
843// ClassScopeFunctionSpecializationDecl Implementation
844//===----------------------------------------------------------------------===//
845
846void ClassScopeFunctionSpecializationDecl::anchor() { }
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000847
848ClassScopeFunctionSpecializationDecl *
849ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
850 unsigned ID) {
851 void *Mem = AllocateDeserializedDecl(C, ID,
852 sizeof(ClassScopeFunctionSpecializationDecl));
853 return new (Mem) ClassScopeFunctionSpecializationDecl(0, SourceLocation(), 0);
854}