blob: 4590195d6b31d2dde1b98202f98737b2f6148e2c [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
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/DeclTemplate.h"
Douglas Gregor8bf42052009-02-09 18:46:07 +000016#include "clang/AST/Expr.h"
Douglas Gregor85759112011-01-04 02:33:52 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000018#include "clang/AST/ASTContext.h"
John McCall0ad16662009-10-29 08:12:44 +000019#include "clang/AST/TypeLoc.h"
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +000020#include "clang/AST/ASTMutationListener.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),
35 NumParams(NumParams) {
Douglas Gregorded2d7b2009-02-04 19:02:06 +000036 for (unsigned Idx = 0; Idx < NumParams; ++Idx)
37 begin()[Idx] = Params[Idx];
38}
39
40TemplateParameterList *
Jay Foad39c79802011-01-12 09:06:06 +000041TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
Douglas Gregorbe999392009-09-15 16:23:51 +000042 SourceLocation LAngleLoc, NamedDecl **Params,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000043 unsigned NumParams, SourceLocation RAngleLoc) {
Douglas Gregorbe999392009-09-15 16:23:51 +000044 unsigned Size = sizeof(TemplateParameterList)
45 + sizeof(NamedDecl *) * NumParams;
Douglas Gregorded2d7b2009-02-04 19:02:06 +000046 unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment;
47 void *Mem = C.Allocate(Size, Align);
Mike Stump11289f42009-09-09 15:08:12 +000048 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000049 NumParams, RAngleLoc);
Douglas Gregorded2d7b2009-02-04 19:02:06 +000050}
51
Douglas Gregorf8f86832009-02-11 18:16:40 +000052unsigned TemplateParameterList::getMinRequiredArguments() const {
Douglas Gregor0231d8d2011-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 Gregorf8f86832009-02-11 18:16:40 +000064 break;
Douglas Gregor0231d8d2011-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 Gregorf8f86832009-02-11 18:16:40 +000078 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +000079
Douglas Gregorf8f86832009-02-11 18:16:40 +000080 return NumRequiredArgs;
81}
82
Douglas Gregor21610382009-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 Gregor3c41bf72011-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 Gregorded2d7b2009-02-04 19:02:06 +0000110//===----------------------------------------------------------------------===//
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000111// RedeclarableTemplateDecl Implementation
112//===----------------------------------------------------------------------===//
113
114RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() {
Douglas Gregor68444de2012-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 Gregorec9fd132012-01-14 16:38:05 +0000119 for (RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
120 Prev = Prev->getPreviousDecl()) {
Douglas Gregor68444de2012-01-14 15:13:49 +0000121 if (Prev->Common) {
122 Common = Prev->Common;
123 break;
124 }
125
126 PrevDecls.push_back(Prev);
127 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000128
Douglas Gregor68444de2012-01-14 15:13:49 +0000129 // If we never found a common pointer, allocate one now.
Douglas Gregor463c8e72012-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 Gregor68444de2012-01-14 15:13:49 +0000134 Common = newCommon(getASTContext());
Douglas Gregor463c8e72012-01-14 15:30:55 +0000135 }
Douglas Gregor68444de2012-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 Collingbourne91b25b72010-07-29 16:11:51 +0000140 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000141
Douglas Gregor68444de2012-01-14 15:13:49 +0000142 return Common;
Peter Collingbourne029fd692010-07-29 16:12:09 +0000143}
144
Peter Collingbourneb498ed62010-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 Gregorec9fd132012-01-14 16:38:05 +0000155 return Entry ? SETraits::getMostRecentDecl(Entry) : 0;
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000156}
157
Douglas Gregor43669f82011-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 McCall113bee02012-03-10 09:33:50 +0000176 Expr *E = new (Context) DeclRefExpr(NTTP, /*enclosing*/ false,
Douglas Gregor43669f82011-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 Collingbourne91b25b72010-07-29 16:11:51 +0000201//===----------------------------------------------------------------------===//
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000202// FunctionTemplateDecl Implementation
203//===----------------------------------------------------------------------===//
204
Douglas Gregor1a809332010-05-23 18:26:36 +0000205void FunctionTemplateDecl::DeallocateCommon(void *Ptr) {
206 static_cast<Common *>(Ptr)->~Common();
207}
208
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000209FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
210 DeclContext *DC,
211 SourceLocation L,
212 DeclarationName Name,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000213 TemplateParameterList *Params,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000214 NamedDecl *Decl) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000215 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000216 return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl);
217}
218
Douglas Gregor72172e92012-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 Gregorfd7c2252011-03-04 17:52:15 +0000224}
225
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000226RedeclarableTemplateDecl::CommonBase *
227FunctionTemplateDecl::newCommon(ASTContext &C) {
228 Common *CommonPtr = new (C) Common;
229 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000230 return CommonPtr;
231}
232
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000233FunctionDecl *
234FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args,
235 unsigned NumArgs, void *&InsertPos) {
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000236 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000237}
238
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000239void FunctionTemplateDecl::addSpecialization(
240 FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
Douglas Gregorce9978f2012-03-28 14:34:23 +0000241 if (InsertPos)
242 getSpecializations().InsertNode(Info, InsertPos);
243 else
244 getSpecializations().GetOrInsertNode(Info);
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000245 if (ASTMutationListener *L = getASTMutationListener())
246 L->AddedCXXTemplateSpecialization(this, Info->Function);
247}
248
Douglas Gregor43669f82011-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 Gregorded2d7b2009-02-04 19:02:06 +0000263//===----------------------------------------------------------------------===//
264// ClassTemplateDecl Implementation
265//===----------------------------------------------------------------------===//
266
Douglas Gregor1a809332010-05-23 18:26:36 +0000267void ClassTemplateDecl::DeallocateCommon(void *Ptr) {
268 static_cast<Common *>(Ptr)->~Common();
269}
270
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000271ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
272 DeclContext *DC,
273 SourceLocation L,
274 DeclarationName Name,
275 TemplateParameterList *Params,
Douglas Gregor90a1a652009-03-19 17:26:29 +0000276 NamedDecl *Decl,
277 ClassTemplateDecl *PrevDecl) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000278 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000279 ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl);
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000280 New->setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000281 return New;
Douglas Gregor90a1a652009-03-19 17:26:29 +0000282}
283
Douglas Gregor72172e92012-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 Gregorfd7c2252011-03-04 17:52:15 +0000288}
289
Douglas Gregor7e8c4e02010-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
301llvm::FoldingSet<ClassTemplateSpecializationDecl> &
302ClassTemplateDecl::getSpecializations() {
303 LoadLazySpecializations();
304 return getCommonPtr()->Specializations;
305}
306
307llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &
308ClassTemplateDecl::getPartialSpecializations() {
309 LoadLazySpecializations();
310 return getCommonPtr()->PartialSpecializations;
311}
312
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000313RedeclarableTemplateDecl::CommonBase *
314ClassTemplateDecl::newCommon(ASTContext &C) {
315 Common *CommonPtr = new (C) Common;
316 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000317 return CommonPtr;
318}
319
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000320ClassTemplateSpecializationDecl *
321ClassTemplateDecl::findSpecialization(const TemplateArgument *Args,
322 unsigned NumArgs, void *&InsertPos) {
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000323 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000324}
325
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000326void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
327 void *InsertPos) {
Douglas Gregorce9978f2012-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 Kyrtzidis402dbbb2010-10-28 07:38:42 +0000336 if (ASTMutationListener *L = getASTMutationListener())
337 L->AddedCXXTemplateSpecialization(this, D);
338}
339
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000340ClassTemplatePartialSpecializationDecl *
341ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
342 unsigned NumArgs,
343 void *&InsertPos) {
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000344 return findSpecializationImpl(getPartialSpecializations(), Args, NumArgs,
345 InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000346}
347
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000348void ClassTemplateDecl::AddPartialSpecialization(
349 ClassTemplatePartialSpecializationDecl *D,
350 void *InsertPos) {
Douglas Gregorce9978f2012-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 Kyrtzidis402dbbb2010-10-28 07:38:42 +0000360 if (ASTMutationListener *L = getASTMutationListener())
361 L->AddedCXXTemplateSpecialization(this, D);
362}
363
Douglas Gregor407e9612010-04-30 05:56:50 +0000364void ClassTemplateDecl::getPartialSpecializations(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000365 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
Douglas Gregor407e9612010-04-30 05:56:50 +0000366 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000367 = getPartialSpecializations();
Douglas Gregor407e9612010-04-30 05:56:50 +0000368 PS.clear();
369 PS.resize(PartialSpecs.size());
370 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
371 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
372 P != PEnd; ++P) {
373 assert(!PS[P->getSequenceNumber()]);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000374 PS[P->getSequenceNumber()] = P->getMostRecentDecl();
Douglas Gregor407e9612010-04-30 05:56:50 +0000375 }
376}
377
Douglas Gregor15301382009-07-30 17:40:51 +0000378ClassTemplatePartialSpecializationDecl *
379ClassTemplateDecl::findPartialSpecialization(QualType T) {
380 ASTContext &Context = getASTContext();
381 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
382 partial_spec_iterator;
383 for (partial_spec_iterator P = getPartialSpecializations().begin(),
384 PEnd = getPartialSpecializations().end();
385 P != PEnd; ++P) {
John McCall2408e322010-04-27 00:57:59 +0000386 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregorec9fd132012-01-14 16:38:05 +0000387 return P->getMostRecentDecl();
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000388 }
389
390 return 0;
391}
392
393ClassTemplatePartialSpecializationDecl *
394ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
395 ClassTemplatePartialSpecializationDecl *D) {
396 Decl *DCanon = D->getCanonicalDecl();
397 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
398 P = getPartialSpecializations().begin(),
399 PEnd = getPartialSpecializations().end();
400 P != PEnd; ++P) {
401 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
Douglas Gregorec9fd132012-01-14 16:38:05 +0000402 return P->getMostRecentDecl();
Douglas Gregor15301382009-07-30 17:40:51 +0000403 }
Mike Stump11289f42009-09-09 15:08:12 +0000404
Douglas Gregor15301382009-07-30 17:40:51 +0000405 return 0;
406}
407
John McCalle78aac42010-03-10 03:28:59 +0000408QualType
Douglas Gregor9961ce92010-07-08 18:37:38 +0000409ClassTemplateDecl::getInjectedClassNameSpecialization() {
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000410 Common *CommonPtr = getCommonPtr();
Douglas Gregore362cea2009-05-10 22:57:19 +0000411 if (!CommonPtr->InjectedClassNameType.isNull())
412 return CommonPtr->InjectedClassNameType;
413
Douglas Gregor8092e802010-12-23 16:00:30 +0000414 // C++0x [temp.dep.type]p2:
415 // The template argument list of a primary template is a template argument
416 // list in which the nth template argument has the value of the nth template
417 // parameter of the class template. If the nth template parameter is a
418 // template parameter pack (14.5.3), the nth template argument is a pack
419 // expansion (14.5.3) whose pattern is the name of the template parameter
420 // pack.
Douglas Gregor9961ce92010-07-08 18:37:38 +0000421 ASTContext &Context = getASTContext();
Douglas Gregore362cea2009-05-10 22:57:19 +0000422 TemplateParameterList *Params = getTemplateParameters();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000423 SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregor43669f82011-03-05 17:54:25 +0000424 TemplateArgs.resize(Params->size());
425 GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data());
Douglas Gregore362cea2009-05-10 22:57:19 +0000426 CommonPtr->InjectedClassNameType
Douglas Gregora8e02e72009-07-28 23:00:59 +0000427 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregore362cea2009-05-10 22:57:19 +0000428 &TemplateArgs[0],
Douglas Gregora8e02e72009-07-28 23:00:59 +0000429 TemplateArgs.size());
Douglas Gregore362cea2009-05-10 22:57:19 +0000430 return CommonPtr->InjectedClassNameType;
431}
432
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000433//===----------------------------------------------------------------------===//
434// TemplateTypeParm Allocation/Deallocation Method Implementations
435//===----------------------------------------------------------------------===//
436
437TemplateTypeParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000438TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000439 SourceLocation KeyLoc, SourceLocation NameLoc,
440 unsigned D, unsigned P, IdentifierInfo *Id,
441 bool Typename, bool ParameterPack) {
Chandler Carruth08836322011-05-01 00:51:33 +0000442 TemplateTypeParmDecl *TTPDecl =
443 new (C) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
444 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
445 TTPDecl->TypeForDecl = TTPType.getTypePtr();
446 return TTPDecl;
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000447}
448
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000449TemplateTypeParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000450TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
451 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTypeParmDecl));
452 return new (Mem) TemplateTypeParmDecl(0, SourceLocation(), SourceLocation(),
453 0, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000454}
455
John McCall0ad16662009-10-29 08:12:44 +0000456SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara23485e02011-03-04 12:42:03 +0000457 return hasDefaultArgument()
458 ? DefaultArgument->getTypeLoc().getBeginLoc()
459 : SourceLocation();
460}
461
462SourceRange TemplateTypeParmDecl::getSourceRange() const {
463 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000464 return SourceRange(getLocStart(),
Abramo Bagnara23485e02011-03-04 12:42:03 +0000465 DefaultArgument->getTypeLoc().getEndLoc());
466 else
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000467 return TypeDecl::getSourceRange();
John McCall0ad16662009-10-29 08:12:44 +0000468}
469
Douglas Gregor21610382009-10-29 00:04:11 +0000470unsigned TemplateTypeParmDecl::getDepth() const {
471 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
472}
473
474unsigned TemplateTypeParmDecl::getIndex() const {
475 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
476}
477
Chandler Carruth08836322011-05-01 00:51:33 +0000478bool TemplateTypeParmDecl::isParameterPack() const {
479 return TypeForDecl->getAs<TemplateTypeParmType>()->isParameterPack();
480}
481
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000482//===----------------------------------------------------------------------===//
483// NonTypeTemplateParmDecl Method Implementations
484//===----------------------------------------------------------------------===//
485
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000486NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000487 SourceLocation StartLoc,
488 SourceLocation IdLoc,
489 unsigned D, unsigned P,
490 IdentifierInfo *Id,
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000491 QualType T,
492 TypeSourceInfo *TInfo,
493 const QualType *ExpandedTypes,
494 unsigned NumExpandedTypes,
495 TypeSourceInfo **ExpandedTInfos)
Abramo Bagnaradff19302011-03-08 08:55:46 +0000496 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000497 TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false),
498 ParameterPack(true), ExpandedParameterPack(true),
499 NumExpandedTypes(NumExpandedTypes)
500{
501 if (ExpandedTypes && ExpandedTInfos) {
502 void **TypesAndInfos = reinterpret_cast<void **>(this + 1);
503 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
504 TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr();
505 TypesAndInfos[2*I + 1] = ExpandedTInfos[I];
506 }
507 }
508}
509
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000510NonTypeTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000511NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000512 SourceLocation StartLoc, SourceLocation IdLoc,
513 unsigned D, unsigned P, IdentifierInfo *Id,
514 QualType T, bool ParameterPack,
515 TypeSourceInfo *TInfo) {
516 return new (C) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
517 T, ParameterPack, TInfo);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000518}
519
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000520NonTypeTemplateParmDecl *
521NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000522 SourceLocation StartLoc, SourceLocation IdLoc,
523 unsigned D, unsigned P,
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000524 IdentifierInfo *Id, QualType T,
525 TypeSourceInfo *TInfo,
526 const QualType *ExpandedTypes,
527 unsigned NumExpandedTypes,
528 TypeSourceInfo **ExpandedTInfos) {
529 unsigned Size = sizeof(NonTypeTemplateParmDecl)
530 + NumExpandedTypes * 2 * sizeof(void*);
531 void *Mem = C.Allocate(Size);
Abramo Bagnaradff19302011-03-08 08:55:46 +0000532 return new (Mem) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc,
533 D, P, Id, T, TInfo,
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000534 ExpandedTypes, NumExpandedTypes,
535 ExpandedTInfos);
536}
537
Douglas Gregor72172e92012-01-05 21:55:30 +0000538NonTypeTemplateParmDecl *
539NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
540 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NonTypeTemplateParmDecl));
541 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
542 SourceLocation(), 0, 0, 0,
543 QualType(), false, 0);
544}
545
546NonTypeTemplateParmDecl *
547NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
548 unsigned NumExpandedTypes) {
549 unsigned Size = sizeof(NonTypeTemplateParmDecl)
550 + NumExpandedTypes * 2 * sizeof(void*);
551
552 void *Mem = AllocateDeserializedDecl(C, ID, Size);
553 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
554 SourceLocation(), 0, 0, 0,
555 QualType(), 0, 0, NumExpandedTypes,
556 0);
557}
558
John McCallf4cd4f92011-02-09 01:13:10 +0000559SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnarae15d5532011-03-04 11:03:48 +0000560 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraea947882011-03-08 16:41:52 +0000561 return SourceRange(getOuterLocStart(),
562 getDefaultArgument()->getSourceRange().getEnd());
563 return DeclaratorDecl::getSourceRange();
John McCallf4cd4f92011-02-09 01:13:10 +0000564}
565
Douglas Gregordba32632009-02-10 19:49:53 +0000566SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara656e3002010-06-09 09:26:05 +0000567 return hasDefaultArgument()
568 ? getDefaultArgument()->getSourceRange().getBegin()
569 : SourceLocation();
Douglas Gregordba32632009-02-10 19:49:53 +0000570}
571
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000572//===----------------------------------------------------------------------===//
573// TemplateTemplateParmDecl Method Implementations
574//===----------------------------------------------------------------------===//
575
David Blaikie68e081d2011-12-20 02:48:34 +0000576void TemplateTemplateParmDecl::anchor() { }
577
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000578TemplateTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000579TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000580 SourceLocation L, unsigned D, unsigned P,
Douglas Gregorf5500772011-01-05 15:48:55 +0000581 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000582 TemplateParameterList *Params) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000583 return new (C) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
584 Params);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000585}
586
Douglas Gregor72172e92012-01-05 21:55:30 +0000587TemplateTemplateParmDecl *
588TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
589 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTemplateParmDecl));
590 return new (Mem) TemplateTemplateParmDecl(0, SourceLocation(), 0, 0, false,
591 0, 0);
592}
593
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000594//===----------------------------------------------------------------------===//
Douglas Gregord002c7b2009-05-11 23:53:27 +0000595// TemplateArgumentList Implementation
596//===----------------------------------------------------------------------===//
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000597TemplateArgumentList *
598TemplateArgumentList::CreateCopy(ASTContext &Context,
599 const TemplateArgument *Args,
600 unsigned NumArgs) {
601 std::size_t Size = sizeof(TemplateArgumentList)
602 + NumArgs * sizeof(TemplateArgument);
603 void *Mem = Context.Allocate(Size);
604 TemplateArgument *StoredArgs
605 = reinterpret_cast<TemplateArgument *>(
606 static_cast<TemplateArgumentList *>(Mem) + 1);
607 std::uninitialized_copy(Args, Args + NumArgs, StoredArgs);
608 return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true);
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000609}
610
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000611FunctionTemplateSpecializationInfo *
612FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD,
613 FunctionTemplateDecl *Template,
614 TemplateSpecializationKind TSK,
615 const TemplateArgumentList *TemplateArgs,
616 const TemplateArgumentListInfo *TemplateArgsAsWritten,
617 SourceLocation POI) {
618 const ASTTemplateArgumentListInfo *ArgsAsWritten = 0;
619 if (TemplateArgsAsWritten)
620 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
621 *TemplateArgsAsWritten);
622
623 return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK,
624 TemplateArgs,
625 ArgsAsWritten,
626 POI);
627}
628
Douglas Gregord002c7b2009-05-11 23:53:27 +0000629//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000630// TemplateDecl Implementation
631//===----------------------------------------------------------------------===//
632
633void TemplateDecl::anchor() { }
634
635//===----------------------------------------------------------------------===//
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000636// ClassTemplateSpecializationDecl Implementation
637//===----------------------------------------------------------------------===//
638ClassTemplateSpecializationDecl::
Douglas Gregore9029562010-05-06 00:28:52 +0000639ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000640 DeclContext *DC, SourceLocation StartLoc,
641 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000642 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000643 const TemplateArgument *Args,
644 unsigned NumArgs,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000645 ClassTemplateSpecializationDecl *PrevDecl)
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000646 : CXXRecordDecl(DK, TK, DC, StartLoc, IdLoc,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000647 SpecializedTemplate->getIdentifier(),
648 PrevDecl),
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000649 SpecializedTemplate(SpecializedTemplate),
Abramo Bagnara8075c852010-06-12 07:44:57 +0000650 ExplicitInfo(0),
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000651 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
Douglas Gregord002c7b2009-05-11 23:53:27 +0000652 SpecializationKind(TSK_Undeclared) {
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000653}
Mike Stump11289f42009-09-09 15:08:12 +0000654
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000655ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK)
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000656 : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0),
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000657 ExplicitInfo(0),
658 SpecializationKind(TSK_Undeclared) {
659}
660
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000661ClassTemplateSpecializationDecl *
Douglas Gregore9029562010-05-06 00:28:52 +0000662ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000663 DeclContext *DC,
664 SourceLocation StartLoc,
665 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000666 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000667 const TemplateArgument *Args,
668 unsigned NumArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000669 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000670 ClassTemplateSpecializationDecl *Result
Mike Stump11289f42009-09-09 15:08:12 +0000671 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregor2373c592009-05-31 09:31:02 +0000672 ClassTemplateSpecialization,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000673 TK, DC, StartLoc, IdLoc,
Douglas Gregord002c7b2009-05-11 23:53:27 +0000674 SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000675 Args, NumArgs,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000676 PrevDecl);
Douglas Gregor67a65642009-02-17 23:15:12 +0000677 Context.getTypeDeclType(Result, PrevDecl);
678 return Result;
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000679}
Douglas Gregor2373c592009-05-31 09:31:02 +0000680
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000681ClassTemplateSpecializationDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000682ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
683 unsigned ID) {
684 void *Mem = AllocateDeserializedDecl(C, ID,
685 sizeof(ClassTemplateSpecializationDecl));
686 return new (Mem) ClassTemplateSpecializationDecl(ClassTemplateSpecialization);
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000687}
688
Douglas Gregorb11aad82011-02-19 18:51:44 +0000689void
690ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
691 const PrintingPolicy &Policy,
692 bool Qualified) const {
693 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
694
695 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
696 S += TemplateSpecializationType::PrintTemplateArgumentList(
697 TemplateArgs.data(),
698 TemplateArgs.size(),
699 Policy);
700}
701
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000702ClassTemplateDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000703ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
704 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000705 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
706 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
707 return SpecializedTemplate.get<ClassTemplateDecl*>();
708}
709
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000710SourceRange
711ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000712 if (ExplicitInfo) {
713 SourceLocation Begin = getExternLoc();
714 if (Begin.isInvalid())
715 Begin = getTemplateKeywordLoc();
716 SourceLocation End = getRBraceLoc();
717 if (End.isInvalid())
718 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
719 return SourceRange(Begin, End);
720 }
721 else {
722 // No explicit info available.
723 llvm::PointerUnion<ClassTemplateDecl *,
724 ClassTemplatePartialSpecializationDecl *>
725 inst_from = getInstantiatedFrom();
726 if (inst_from.isNull())
727 return getSpecializedTemplate()->getSourceRange();
728 if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>())
729 return ctd->getSourceRange();
730 return inst_from.get<ClassTemplatePartialSpecializationDecl*>()
731 ->getSourceRange();
732 }
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000733}
734
Douglas Gregor2373c592009-05-31 09:31:02 +0000735//===----------------------------------------------------------------------===//
736// ClassTemplatePartialSpecializationDecl Implementation
737//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000738void ClassTemplatePartialSpecializationDecl::anchor() { }
739
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000740ClassTemplatePartialSpecializationDecl::
741ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000742 DeclContext *DC,
743 SourceLocation StartLoc,
744 SourceLocation IdLoc,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000745 TemplateParameterList *Params,
746 ClassTemplateDecl *SpecializedTemplate,
747 const TemplateArgument *Args,
748 unsigned NumArgs,
749 TemplateArgumentLoc *ArgInfos,
750 unsigned NumArgInfos,
751 ClassTemplatePartialSpecializationDecl *PrevDecl,
752 unsigned SequenceNumber)
753 : ClassTemplateSpecializationDecl(Context,
754 ClassTemplatePartialSpecialization,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000755 TK, DC, StartLoc, IdLoc,
756 SpecializedTemplate,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000757 Args, NumArgs, PrevDecl),
758 TemplateParams(Params), ArgsAsWritten(ArgInfos),
759 NumArgsAsWritten(NumArgInfos), SequenceNumber(SequenceNumber),
760 InstantiatedFromMember(0, false)
761{
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000762 AdoptTemplateParameterList(Params, this);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000763}
764
Douglas Gregor2373c592009-05-31 09:31:02 +0000765ClassTemplatePartialSpecializationDecl *
766ClassTemplatePartialSpecializationDecl::
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000767Create(ASTContext &Context, TagKind TK,DeclContext *DC,
768 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregor2373c592009-05-31 09:31:02 +0000769 TemplateParameterList *Params,
770 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000771 const TemplateArgument *Args,
772 unsigned NumArgs,
John McCall6b51f282009-11-23 01:53:49 +0000773 const TemplateArgumentListInfo &ArgInfos,
John McCalle78aac42010-03-10 03:28:59 +0000774 QualType CanonInjectedType,
Douglas Gregor407e9612010-04-30 05:56:50 +0000775 ClassTemplatePartialSpecializationDecl *PrevDecl,
776 unsigned SequenceNumber) {
John McCall6b51f282009-11-23 01:53:49 +0000777 unsigned N = ArgInfos.size();
John McCall0ad16662009-10-29 08:12:44 +0000778 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
779 for (unsigned I = 0; I != N; ++I)
780 ClonedArgs[I] = ArgInfos[I];
781
Douglas Gregor2373c592009-05-31 09:31:02 +0000782 ClassTemplatePartialSpecializationDecl *Result
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000783 = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK, DC,
784 StartLoc, IdLoc,
785 Params,
Douglas Gregor2373c592009-05-31 09:31:02 +0000786 SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000787 Args, NumArgs,
John McCall0ad16662009-10-29 08:12:44 +0000788 ClonedArgs, N,
Douglas Gregor407e9612010-04-30 05:56:50 +0000789 PrevDecl,
790 SequenceNumber);
Douglas Gregor2373c592009-05-31 09:31:02 +0000791 Result->setSpecializationKind(TSK_ExplicitSpecialization);
John McCalle78aac42010-03-10 03:28:59 +0000792
793 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregor2373c592009-05-31 09:31:02 +0000794 return Result;
795}
John McCall11083da2009-09-16 22:47:08 +0000796
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000797ClassTemplatePartialSpecializationDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000798ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
799 unsigned ID) {
800 void *Mem = AllocateDeserializedDecl(C, ID,
801 sizeof(ClassTemplatePartialSpecializationDecl));
802 return new (Mem) ClassTemplatePartialSpecializationDecl();
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000803}
804
John McCall11083da2009-09-16 22:47:08 +0000805//===----------------------------------------------------------------------===//
806// FriendTemplateDecl Implementation
807//===----------------------------------------------------------------------===//
808
David Blaikie68e081d2011-12-20 02:48:34 +0000809void FriendTemplateDecl::anchor() { }
810
John McCall11083da2009-09-16 22:47:08 +0000811FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
812 DeclContext *DC,
813 SourceLocation L,
814 unsigned NParams,
815 TemplateParameterList **Params,
816 FriendUnion Friend,
817 SourceLocation FLoc) {
818 FriendTemplateDecl *Result
819 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
820 return Result;
821}
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000822
Douglas Gregor72172e92012-01-05 21:55:30 +0000823FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
824 unsigned ID) {
825 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FriendTemplateDecl));
826 return new (Mem) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000827}
Richard Smith3f1b5d02011-05-05 21:57:07 +0000828
829//===----------------------------------------------------------------------===//
830// TypeAliasTemplateDecl Implementation
831//===----------------------------------------------------------------------===//
832
833TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
834 DeclContext *DC,
835 SourceLocation L,
836 DeclarationName Name,
837 TemplateParameterList *Params,
838 NamedDecl *Decl) {
839 AdoptTemplateParameterList(Params, DC);
840 return new (C) TypeAliasTemplateDecl(DC, L, Name, Params, Decl);
841}
842
Douglas Gregor72172e92012-01-05 21:55:30 +0000843TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
844 unsigned ID) {
845 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasTemplateDecl));
846 return new (Mem) TypeAliasTemplateDecl(0, SourceLocation(), DeclarationName(),
847 0, 0);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000848}
849
850void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) {
851 static_cast<Common *>(Ptr)->~Common();
852}
853RedeclarableTemplateDecl::CommonBase *
854TypeAliasTemplateDecl::newCommon(ASTContext &C) {
855 Common *CommonPtr = new (C) Common;
856 C.AddDeallocation(DeallocateCommon, CommonPtr);
857 return CommonPtr;
858}
859
David Blaikie68e081d2011-12-20 02:48:34 +0000860//===----------------------------------------------------------------------===//
861// ClassScopeFunctionSpecializationDecl Implementation
862//===----------------------------------------------------------------------===//
863
864void ClassScopeFunctionSpecializationDecl::anchor() { }
Douglas Gregor72172e92012-01-05 21:55:30 +0000865
866ClassScopeFunctionSpecializationDecl *
867ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
868 unsigned ID) {
869 void *Mem = AllocateDeserializedDecl(C, ID,
870 sizeof(ClassScopeFunctionSpecializationDecl));
871 return new (Mem) ClassScopeFunctionSpecializationDecl(0, SourceLocation(), 0);
872}