blob: a7e89994afe847caeb87b1d0cf2884b2c5f99796 [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;
Richard Smith1a30edb2012-08-16 22:51:34 +000046 unsigned Align = std::max(llvm::alignOf<TemplateParameterList>(),
47 llvm::alignOf<NamedDecl*>());
Douglas Gregoraaba5e32009-02-04 19:02:06 +000048 void *Mem = C.Allocate(Size, Align);
Mike Stump1eb44332009-09-09 15:08:12 +000049 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
Douglas Gregorddc29e12009-02-06 22:42:48 +000050 NumParams, RAngleLoc);
Douglas Gregoraaba5e32009-02-04 19:02:06 +000051}
52
Douglas Gregor62cb18d2009-02-11 18:16:40 +000053unsigned TemplateParameterList::getMinRequiredArguments() const {
Douglas Gregor6952f1e2011-01-19 20:10:05 +000054 unsigned NumRequiredArgs = 0;
55 for (iterator P = const_cast<TemplateParameterList *>(this)->begin(),
56 PEnd = const_cast<TemplateParameterList *>(this)->end();
57 P != PEnd; ++P) {
58 if ((*P)->isTemplateParameterPack()) {
59 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
60 if (NTTP->isExpandedParameterPack()) {
61 NumRequiredArgs += NTTP->getNumExpansionTypes();
62 continue;
63 }
64
Douglas Gregor62cb18d2009-02-11 18:16:40 +000065 break;
Douglas Gregor6952f1e2011-01-19 20:10:05 +000066 }
67
68 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
69 if (TTP->hasDefaultArgument())
70 break;
71 } else if (NonTypeTemplateParmDecl *NTTP
72 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
73 if (NTTP->hasDefaultArgument())
74 break;
75 } else if (cast<TemplateTemplateParmDecl>(*P)->hasDefaultArgument())
76 break;
77
78 ++NumRequiredArgs;
Douglas Gregor62cb18d2009-02-11 18:16:40 +000079 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +000080
Douglas Gregor62cb18d2009-02-11 18:16:40 +000081 return NumRequiredArgs;
82}
83
Douglas Gregored9c0f92009-10-29 00:04:11 +000084unsigned TemplateParameterList::getDepth() const {
85 if (size() == 0)
86 return 0;
87
88 const NamedDecl *FirstParm = getParam(0);
89 if (const TemplateTypeParmDecl *TTP
90 = dyn_cast<TemplateTypeParmDecl>(FirstParm))
91 return TTP->getDepth();
92 else if (const NonTypeTemplateParmDecl *NTTP
93 = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
94 return NTTP->getDepth();
95 else
96 return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
97}
98
Douglas Gregor787a40d2011-03-04 18:32:38 +000099static void AdoptTemplateParameterList(TemplateParameterList *Params,
100 DeclContext *Owner) {
101 for (TemplateParameterList::iterator P = Params->begin(),
102 PEnd = Params->end();
103 P != PEnd; ++P) {
104 (*P)->setDeclContext(Owner);
105
106 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*P))
107 AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner);
108 }
109}
110
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000111//===----------------------------------------------------------------------===//
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000112// RedeclarableTemplateDecl Implementation
113//===----------------------------------------------------------------------===//
114
115RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() {
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000116 if (!Common) {
117 // Walk the previous-declaration chain until we either find a declaration
118 // with a common pointer or we run out of previous declarations.
119 llvm::SmallVector<RedeclarableTemplateDecl *, 2> PrevDecls;
Douglas Gregoref96ee02012-01-14 16:38:05 +0000120 for (RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
121 Prev = Prev->getPreviousDecl()) {
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000122 if (Prev->Common) {
123 Common = Prev->Common;
124 break;
125 }
126
127 PrevDecls.push_back(Prev);
128 }
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000129
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000130 // If we never found a common pointer, allocate one now.
Douglas Gregor8a8950b2012-01-14 15:30:55 +0000131 if (!Common) {
132 // FIXME: If any of the declarations is from an AST file, we probably
133 // need an update record to add the common data.
134
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000135 Common = newCommon(getASTContext());
Douglas Gregor8a8950b2012-01-14 15:30:55 +0000136 }
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000137
138 // Update any previous declarations we saw with the common pointer.
139 for (unsigned I = 0, N = PrevDecls.size(); I != N; ++I)
140 PrevDecls[I]->Common = Common;
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000141 }
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000142
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +0000143 return Common;
Peter Collingbournef88718e2010-07-29 16:12:09 +0000144}
145
Peter Collingbourne40485902010-07-30 17:09:04 +0000146template <class EntryType>
147typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType*
148RedeclarableTemplateDecl::findSpecializationImpl(
Chandler Carruthd964d632012-05-03 23:49:05 +0000149 llvm::FoldingSetVector<EntryType> &Specs,
Peter Collingbourne40485902010-07-30 17:09:04 +0000150 const TemplateArgument *Args, unsigned NumArgs,
151 void *&InsertPos) {
152 typedef SpecEntryTraits<EntryType> SETraits;
153 llvm::FoldingSetNodeID ID;
154 EntryType::Profile(ID,Args,NumArgs, getASTContext());
155 EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000156 return Entry ? SETraits::getMostRecentDecl(Entry) : 0;
Peter Collingbourne40485902010-07-30 17:09:04 +0000157}
158
Douglas Gregorc494f772011-03-05 17:54:25 +0000159/// \brief Generate the injected template arguments for the given template
160/// parameter list, e.g., for the injected-class-name of a class template.
161static void GenerateInjectedTemplateArgs(ASTContext &Context,
162 TemplateParameterList *Params,
163 TemplateArgument *Args) {
164 for (TemplateParameterList::iterator Param = Params->begin(),
165 ParamEnd = Params->end();
166 Param != ParamEnd; ++Param) {
167 TemplateArgument Arg;
168 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
169 QualType ArgType = Context.getTypeDeclType(TTP);
170 if (TTP->isParameterPack())
171 ArgType = Context.getPackExpansionType(ArgType,
172 llvm::Optional<unsigned>());
173
174 Arg = TemplateArgument(ArgType);
175 } else if (NonTypeTemplateParmDecl *NTTP =
176 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
John McCallf4b88a42012-03-10 09:33:50 +0000177 Expr *E = new (Context) DeclRefExpr(NTTP, /*enclosing*/ false,
Douglas Gregorc494f772011-03-05 17:54:25 +0000178 NTTP->getType().getNonLValueExprType(Context),
179 Expr::getValueKindForType(NTTP->getType()),
180 NTTP->getLocation());
181
182 if (NTTP->isParameterPack())
183 E = new (Context) PackExpansionExpr(Context.DependentTy, E,
184 NTTP->getLocation(),
185 llvm::Optional<unsigned>());
186 Arg = TemplateArgument(E);
187 } else {
188 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
189 if (TTP->isParameterPack())
190 Arg = TemplateArgument(TemplateName(TTP), llvm::Optional<unsigned>());
191 else
192 Arg = TemplateArgument(TemplateName(TTP));
193 }
194
195 if ((*Param)->isTemplateParameterPack())
196 Arg = TemplateArgument::CreatePackCopy(Context, &Arg, 1);
197
198 *Args++ = Arg;
199 }
200}
201
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000202//===----------------------------------------------------------------------===//
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000203// FunctionTemplateDecl Implementation
204//===----------------------------------------------------------------------===//
205
Douglas Gregor00545312010-05-23 18:26:36 +0000206void FunctionTemplateDecl::DeallocateCommon(void *Ptr) {
207 static_cast<Common *>(Ptr)->~Common();
208}
209
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000210FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
211 DeclContext *DC,
212 SourceLocation L,
213 DeclarationName Name,
Douglas Gregor127102b2009-06-29 20:59:39 +0000214 TemplateParameterList *Params,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000215 NamedDecl *Decl) {
Douglas Gregor787a40d2011-03-04 18:32:38 +0000216 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000217 return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl);
218}
219
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000220FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
221 unsigned ID) {
222 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionTemplateDecl));
223 return new (Mem) FunctionTemplateDecl(0, SourceLocation(), DeclarationName(),
224 0, 0);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000225}
226
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +0000227RedeclarableTemplateDecl::CommonBase *
228FunctionTemplateDecl::newCommon(ASTContext &C) {
229 Common *CommonPtr = new (C) Common;
230 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000231 return CommonPtr;
232}
233
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000234FunctionDecl *
235FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args,
236 unsigned NumArgs, void *&InsertPos) {
Peter Collingbourne40485902010-07-30 17:09:04 +0000237 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000238}
239
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +0000240void FunctionTemplateDecl::addSpecialization(
241 FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
Douglas Gregor1e1e9722012-03-28 14:34:23 +0000242 if (InsertPos)
243 getSpecializations().InsertNode(Info, InsertPos);
244 else
245 getSpecializations().GetOrInsertNode(Info);
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +0000246 if (ASTMutationListener *L = getASTMutationListener())
247 L->AddedCXXTemplateSpecialization(this, Info->Function);
248}
249
Douglas Gregorc494f772011-03-05 17:54:25 +0000250std::pair<const TemplateArgument *, unsigned>
251FunctionTemplateDecl::getInjectedTemplateArgs() {
252 TemplateParameterList *Params = getTemplateParameters();
253 Common *CommonPtr = getCommonPtr();
254 if (!CommonPtr->InjectedArgs) {
255 CommonPtr->InjectedArgs
256 = new (getASTContext()) TemplateArgument [Params->size()];
257 GenerateInjectedTemplateArgs(getASTContext(), Params,
258 CommonPtr->InjectedArgs);
259 }
260
261 return std::make_pair(CommonPtr->InjectedArgs, Params->size());
262}
263
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000264//===----------------------------------------------------------------------===//
265// ClassTemplateDecl Implementation
266//===----------------------------------------------------------------------===//
267
Douglas Gregor00545312010-05-23 18:26:36 +0000268void ClassTemplateDecl::DeallocateCommon(void *Ptr) {
269 static_cast<Common *>(Ptr)->~Common();
270}
271
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000272ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
273 DeclContext *DC,
274 SourceLocation L,
275 DeclarationName Name,
276 TemplateParameterList *Params,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000277 NamedDecl *Decl,
278 ClassTemplateDecl *PrevDecl) {
Douglas Gregor787a40d2011-03-04 18:32:38 +0000279 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000280 ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl);
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000281 New->setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000282 return New;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000283}
284
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000285ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
286 unsigned ID) {
287 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ClassTemplateDecl));
288 return new (Mem) ClassTemplateDecl(EmptyShell());
Douglas Gregor9a299e02011-03-04 17:52:15 +0000289}
290
Douglas Gregorc8e5cf82010-10-27 22:21:36 +0000291void ClassTemplateDecl::LoadLazySpecializations() {
292 Common *CommonPtr = getCommonPtr();
293 if (CommonPtr->LazySpecializations) {
294 ASTContext &Context = getASTContext();
295 uint32_t *Specs = CommonPtr->LazySpecializations;
296 CommonPtr->LazySpecializations = 0;
297 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
298 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
299 }
300}
301
Chandler Carruthd964d632012-05-03 23:49:05 +0000302llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
Douglas Gregorc8e5cf82010-10-27 22:21:36 +0000303ClassTemplateDecl::getSpecializations() {
304 LoadLazySpecializations();
305 return getCommonPtr()->Specializations;
306}
307
Chandler Carruthd964d632012-05-03 23:49:05 +0000308llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
Douglas Gregorc8e5cf82010-10-27 22:21:36 +0000309ClassTemplateDecl::getPartialSpecializations() {
310 LoadLazySpecializations();
311 return getCommonPtr()->PartialSpecializations;
312}
313
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +0000314RedeclarableTemplateDecl::CommonBase *
315ClassTemplateDecl::newCommon(ASTContext &C) {
316 Common *CommonPtr = new (C) Common;
317 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000318 return CommonPtr;
319}
320
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000321ClassTemplateSpecializationDecl *
322ClassTemplateDecl::findSpecialization(const TemplateArgument *Args,
323 unsigned NumArgs, void *&InsertPos) {
Peter Collingbourne40485902010-07-30 17:09:04 +0000324 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000325}
326
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000327void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
328 void *InsertPos) {
Douglas Gregor1e1e9722012-03-28 14:34:23 +0000329 if (InsertPos)
330 getSpecializations().InsertNode(D, InsertPos);
331 else {
332 ClassTemplateSpecializationDecl *Existing
333 = getSpecializations().GetOrInsertNode(D);
334 (void)Existing;
335 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
336 }
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000337 if (ASTMutationListener *L = getASTMutationListener())
338 L->AddedCXXTemplateSpecialization(this, D);
339}
340
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000341ClassTemplatePartialSpecializationDecl *
342ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
343 unsigned NumArgs,
344 void *&InsertPos) {
Peter Collingbourne40485902010-07-30 17:09:04 +0000345 return findSpecializationImpl(getPartialSpecializations(), Args, NumArgs,
346 InsertPos);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000347}
348
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000349void ClassTemplateDecl::AddPartialSpecialization(
350 ClassTemplatePartialSpecializationDecl *D,
351 void *InsertPos) {
Douglas Gregor1e1e9722012-03-28 14:34:23 +0000352 if (InsertPos)
353 getPartialSpecializations().InsertNode(D, InsertPos);
354 else {
355 ClassTemplatePartialSpecializationDecl *Existing
356 = getPartialSpecializations().GetOrInsertNode(D);
357 (void)Existing;
358 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
359 }
360
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +0000361 if (ASTMutationListener *L = getASTMutationListener())
362 L->AddedCXXTemplateSpecialization(this, D);
363}
364
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000365void ClassTemplateDecl::getPartialSpecializations(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000366 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
Chandler Carruthd964d632012-05-03 23:49:05 +0000367 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000368 = getPartialSpecializations();
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000369 PS.clear();
370 PS.resize(PartialSpecs.size());
Chandler Carruthd964d632012-05-03 23:49:05 +0000371 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000372 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
373 P != PEnd; ++P) {
374 assert(!PS[P->getSequenceNumber()]);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000375 PS[P->getSequenceNumber()] = P->getMostRecentDecl();
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000376 }
377}
378
Douglas Gregorb88e8882009-07-30 17:40:51 +0000379ClassTemplatePartialSpecializationDecl *
380ClassTemplateDecl::findPartialSpecialization(QualType T) {
381 ASTContext &Context = getASTContext();
Chandler Carruthd964d632012-05-03 23:49:05 +0000382 using llvm::FoldingSetVector;
383 typedef FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorb88e8882009-07-30 17:40:51 +0000384 partial_spec_iterator;
385 for (partial_spec_iterator P = getPartialSpecializations().begin(),
386 PEnd = getPartialSpecializations().end();
387 P != PEnd; ++P) {
John McCall31f17ec2010-04-27 00:57:59 +0000388 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregoref96ee02012-01-14 16:38:05 +0000389 return P->getMostRecentDecl();
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000390 }
391
392 return 0;
393}
394
395ClassTemplatePartialSpecializationDecl *
396ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
397 ClassTemplatePartialSpecializationDecl *D) {
398 Decl *DCanon = D->getCanonicalDecl();
Chandler Carruthd964d632012-05-03 23:49:05 +0000399 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000400 P = getPartialSpecializations().begin(),
401 PEnd = getPartialSpecializations().end();
402 P != PEnd; ++P) {
403 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
Douglas Gregoref96ee02012-01-14 16:38:05 +0000404 return P->getMostRecentDecl();
Douglas Gregorb88e8882009-07-30 17:40:51 +0000405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregorb88e8882009-07-30 17:40:51 +0000407 return 0;
408}
409
John McCall3cb0ebd2010-03-10 03:28:59 +0000410QualType
Douglas Gregor24bae922010-07-08 18:37:38 +0000411ClassTemplateDecl::getInjectedClassNameSpecialization() {
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000412 Common *CommonPtr = getCommonPtr();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000413 if (!CommonPtr->InjectedClassNameType.isNull())
414 return CommonPtr->InjectedClassNameType;
415
Douglas Gregorb7d09d62010-12-23 16:00:30 +0000416 // C++0x [temp.dep.type]p2:
417 // The template argument list of a primary template is a template argument
418 // list in which the nth template argument has the value of the nth template
419 // parameter of the class template. If the nth template parameter is a
420 // template parameter pack (14.5.3), the nth template argument is a pack
421 // expansion (14.5.3) whose pattern is the name of the template parameter
422 // pack.
Douglas Gregor24bae922010-07-08 18:37:38 +0000423 ASTContext &Context = getASTContext();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000424 TemplateParameterList *Params = getTemplateParameters();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000425 SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregorc494f772011-03-05 17:54:25 +0000426 TemplateArgs.resize(Params->size());
427 GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000428 CommonPtr->InjectedClassNameType
Douglas Gregor1275ae02009-07-28 23:00:59 +0000429 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregor7da97d02009-05-10 22:57:19 +0000430 &TemplateArgs[0],
Douglas Gregor1275ae02009-07-28 23:00:59 +0000431 TemplateArgs.size());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000432 return CommonPtr->InjectedClassNameType;
433}
434
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000435//===----------------------------------------------------------------------===//
436// TemplateTypeParm Allocation/Deallocation Method Implementations
437//===----------------------------------------------------------------------===//
438
439TemplateTypeParmDecl *
Jay Foad4ba2a172011-01-12 09:06:06 +0000440TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +0000441 SourceLocation KeyLoc, SourceLocation NameLoc,
442 unsigned D, unsigned P, IdentifierInfo *Id,
443 bool Typename, bool ParameterPack) {
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000444 TemplateTypeParmDecl *TTPDecl =
445 new (C) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
446 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
447 TTPDecl->TypeForDecl = TTPType.getTypePtr();
448 return TTPDecl;
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000449}
450
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000451TemplateTypeParmDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000452TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
453 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTypeParmDecl));
454 return new (Mem) TemplateTypeParmDecl(0, SourceLocation(), SourceLocation(),
455 0, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000456}
457
John McCall833ca992009-10-29 08:12:44 +0000458SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara77d4ee22011-03-04 12:42:03 +0000459 return hasDefaultArgument()
460 ? DefaultArgument->getTypeLoc().getBeginLoc()
461 : SourceLocation();
462}
463
464SourceRange TemplateTypeParmDecl::getSourceRange() const {
465 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnara344577e2011-03-06 15:48:19 +0000466 return SourceRange(getLocStart(),
Abramo Bagnara77d4ee22011-03-04 12:42:03 +0000467 DefaultArgument->getTypeLoc().getEndLoc());
468 else
Abramo Bagnara344577e2011-03-06 15:48:19 +0000469 return TypeDecl::getSourceRange();
John McCall833ca992009-10-29 08:12:44 +0000470}
471
Douglas Gregored9c0f92009-10-29 00:04:11 +0000472unsigned TemplateTypeParmDecl::getDepth() const {
473 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
474}
475
476unsigned TemplateTypeParmDecl::getIndex() const {
477 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
478}
479
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000480bool TemplateTypeParmDecl::isParameterPack() const {
481 return TypeForDecl->getAs<TemplateTypeParmType>()->isParameterPack();
482}
483
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000484//===----------------------------------------------------------------------===//
485// NonTypeTemplateParmDecl Method Implementations
486//===----------------------------------------------------------------------===//
487
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000488NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000489 SourceLocation StartLoc,
490 SourceLocation IdLoc,
491 unsigned D, unsigned P,
492 IdentifierInfo *Id,
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000493 QualType T,
494 TypeSourceInfo *TInfo,
495 const QualType *ExpandedTypes,
496 unsigned NumExpandedTypes,
497 TypeSourceInfo **ExpandedTInfos)
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000498 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000499 TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false),
500 ParameterPack(true), ExpandedParameterPack(true),
501 NumExpandedTypes(NumExpandedTypes)
502{
503 if (ExpandedTypes && ExpandedTInfos) {
504 void **TypesAndInfos = reinterpret_cast<void **>(this + 1);
505 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
506 TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr();
507 TypesAndInfos[2*I + 1] = ExpandedTInfos[I];
508 }
509 }
510}
511
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000512NonTypeTemplateParmDecl *
Jay Foad4ba2a172011-01-12 09:06:06 +0000513NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000514 SourceLocation StartLoc, SourceLocation IdLoc,
515 unsigned D, unsigned P, IdentifierInfo *Id,
516 QualType T, bool ParameterPack,
517 TypeSourceInfo *TInfo) {
518 return new (C) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
519 T, ParameterPack, TInfo);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000520}
521
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000522NonTypeTemplateParmDecl *
523NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000524 SourceLocation StartLoc, SourceLocation IdLoc,
525 unsigned D, unsigned P,
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000526 IdentifierInfo *Id, QualType T,
527 TypeSourceInfo *TInfo,
528 const QualType *ExpandedTypes,
529 unsigned NumExpandedTypes,
530 TypeSourceInfo **ExpandedTInfos) {
531 unsigned Size = sizeof(NonTypeTemplateParmDecl)
532 + NumExpandedTypes * 2 * sizeof(void*);
533 void *Mem = C.Allocate(Size);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000534 return new (Mem) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc,
535 D, P, Id, T, TInfo,
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000536 ExpandedTypes, NumExpandedTypes,
537 ExpandedTInfos);
538}
539
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000540NonTypeTemplateParmDecl *
541NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
542 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NonTypeTemplateParmDecl));
543 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
544 SourceLocation(), 0, 0, 0,
545 QualType(), false, 0);
546}
547
548NonTypeTemplateParmDecl *
549NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
550 unsigned NumExpandedTypes) {
551 unsigned Size = sizeof(NonTypeTemplateParmDecl)
552 + NumExpandedTypes * 2 * sizeof(void*);
553
554 void *Mem = AllocateDeserializedDecl(C, ID, Size);
555 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
556 SourceLocation(), 0, 0, 0,
557 QualType(), 0, 0, NumExpandedTypes,
558 0);
559}
560
John McCall76a40212011-02-09 01:13:10 +0000561SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnaraee4bfd42011-03-04 11:03:48 +0000562 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000563 return SourceRange(getOuterLocStart(),
564 getDefaultArgument()->getSourceRange().getEnd());
565 return DeclaratorDecl::getSourceRange();
John McCall76a40212011-02-09 01:13:10 +0000566}
567
Douglas Gregord684b002009-02-10 19:49:53 +0000568SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +0000569 return hasDefaultArgument()
570 ? getDefaultArgument()->getSourceRange().getBegin()
571 : SourceLocation();
Douglas Gregord684b002009-02-10 19:49:53 +0000572}
573
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000574//===----------------------------------------------------------------------===//
575// TemplateTemplateParmDecl Method Implementations
576//===----------------------------------------------------------------------===//
577
David Blaikie99ba9e32011-12-20 02:48:34 +0000578void TemplateTemplateParmDecl::anchor() { }
579
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000580TemplateTemplateParmDecl *
Jay Foad4ba2a172011-01-12 09:06:06 +0000581TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000582 SourceLocation L, unsigned D, unsigned P,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000583 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000584 TemplateParameterList *Params) {
Douglas Gregor61c4d282011-01-05 15:48:55 +0000585 return new (C) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
586 Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000587}
588
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000589TemplateTemplateParmDecl *
590TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
591 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTemplateParmDecl));
592 return new (Mem) TemplateTemplateParmDecl(0, SourceLocation(), 0, 0, false,
593 0, 0);
594}
595
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000596//===----------------------------------------------------------------------===//
Douglas Gregor7e063902009-05-11 23:53:27 +0000597// TemplateArgumentList Implementation
598//===----------------------------------------------------------------------===//
Douglas Gregor910f8002010-11-07 23:05:16 +0000599TemplateArgumentList *
600TemplateArgumentList::CreateCopy(ASTContext &Context,
601 const TemplateArgument *Args,
602 unsigned NumArgs) {
603 std::size_t Size = sizeof(TemplateArgumentList)
604 + NumArgs * sizeof(TemplateArgument);
605 void *Mem = Context.Allocate(Size);
606 TemplateArgument *StoredArgs
607 = reinterpret_cast<TemplateArgument *>(
608 static_cast<TemplateArgumentList *>(Mem) + 1);
609 std::uninitialized_copy(Args, Args + NumArgs, StoredArgs);
610 return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true);
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000611}
612
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +0000613FunctionTemplateSpecializationInfo *
614FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD,
615 FunctionTemplateDecl *Template,
616 TemplateSpecializationKind TSK,
617 const TemplateArgumentList *TemplateArgs,
618 const TemplateArgumentListInfo *TemplateArgsAsWritten,
619 SourceLocation POI) {
620 const ASTTemplateArgumentListInfo *ArgsAsWritten = 0;
621 if (TemplateArgsAsWritten)
622 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
623 *TemplateArgsAsWritten);
624
625 return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK,
626 TemplateArgs,
627 ArgsAsWritten,
628 POI);
629}
630
Douglas Gregor7e063902009-05-11 23:53:27 +0000631//===----------------------------------------------------------------------===//
David Blaikie99ba9e32011-12-20 02:48:34 +0000632// TemplateDecl Implementation
633//===----------------------------------------------------------------------===//
634
635void TemplateDecl::anchor() { }
636
637//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000638// ClassTemplateSpecializationDecl Implementation
639//===----------------------------------------------------------------------===//
640ClassTemplateSpecializationDecl::
Douglas Gregor13c85772010-05-06 00:28:52 +0000641ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000642 DeclContext *DC, SourceLocation StartLoc,
643 SourceLocation IdLoc,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000644 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000645 const TemplateArgument *Args,
646 unsigned NumArgs,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000647 ClassTemplateSpecializationDecl *PrevDecl)
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000648 : CXXRecordDecl(DK, TK, DC, StartLoc, IdLoc,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000649 SpecializedTemplate->getIdentifier(),
650 PrevDecl),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000651 SpecializedTemplate(SpecializedTemplate),
Abramo Bagnarac98971d2010-06-12 07:44:57 +0000652 ExplicitInfo(0),
Douglas Gregor910f8002010-11-07 23:05:16 +0000653 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
Douglas Gregor7e063902009-05-11 23:53:27 +0000654 SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000655}
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000657ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK)
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000658 : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0),
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000659 ExplicitInfo(0),
660 SpecializationKind(TSK_Undeclared) {
661}
662
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000663ClassTemplateSpecializationDecl *
Douglas Gregor13c85772010-05-06 00:28:52 +0000664ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000665 DeclContext *DC,
666 SourceLocation StartLoc,
667 SourceLocation IdLoc,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000668 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000669 const TemplateArgument *Args,
670 unsigned NumArgs,
Douglas Gregorcc636682009-02-17 23:15:12 +0000671 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000672 ClassTemplateSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000673 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000674 ClassTemplateSpecialization,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000675 TK, DC, StartLoc, IdLoc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000676 SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000677 Args, NumArgs,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000678 PrevDecl);
Douglas Gregorcc636682009-02-17 23:15:12 +0000679 Context.getTypeDeclType(Result, PrevDecl);
680 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000681}
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000682
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000683ClassTemplateSpecializationDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000684ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
685 unsigned ID) {
686 void *Mem = AllocateDeserializedDecl(C, ID,
687 sizeof(ClassTemplateSpecializationDecl));
688 return new (Mem) ClassTemplateSpecializationDecl(ClassTemplateSpecialization);
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000689}
690
Douglas Gregorda2142f2011-02-19 18:51:44 +0000691void
692ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
693 const PrintingPolicy &Policy,
694 bool Qualified) const {
695 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
696
697 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
698 S += TemplateSpecializationType::PrintTemplateArgumentList(
699 TemplateArgs.data(),
700 TemplateArgs.size(),
701 Policy);
702}
703
Douglas Gregor37d93e92009-08-02 23:24:31 +0000704ClassTemplateDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000705ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
706 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000707 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
708 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
709 return SpecializedTemplate.get<ClassTemplateDecl*>();
710}
711
Abramo Bagnara4a85a732011-03-04 14:20:30 +0000712SourceRange
713ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnara09d82122011-10-03 20:34:03 +0000714 if (ExplicitInfo) {
715 SourceLocation Begin = getExternLoc();
716 if (Begin.isInvalid())
717 Begin = getTemplateKeywordLoc();
718 SourceLocation End = getRBraceLoc();
719 if (End.isInvalid())
720 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
721 return SourceRange(Begin, End);
722 }
723 else {
724 // No explicit info available.
725 llvm::PointerUnion<ClassTemplateDecl *,
726 ClassTemplatePartialSpecializationDecl *>
727 inst_from = getInstantiatedFrom();
728 if (inst_from.isNull())
729 return getSpecializedTemplate()->getSourceRange();
730 if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>())
731 return ctd->getSourceRange();
732 return inst_from.get<ClassTemplatePartialSpecializationDecl*>()
733 ->getSourceRange();
734 }
Abramo Bagnara4a85a732011-03-04 14:20:30 +0000735}
736
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000737//===----------------------------------------------------------------------===//
738// ClassTemplatePartialSpecializationDecl Implementation
739//===----------------------------------------------------------------------===//
David Blaikie99ba9e32011-12-20 02:48:34 +0000740void ClassTemplatePartialSpecializationDecl::anchor() { }
741
Douglas Gregor9a299e02011-03-04 17:52:15 +0000742ClassTemplatePartialSpecializationDecl::
743ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000744 DeclContext *DC,
745 SourceLocation StartLoc,
746 SourceLocation IdLoc,
Douglas Gregor9a299e02011-03-04 17:52:15 +0000747 TemplateParameterList *Params,
748 ClassTemplateDecl *SpecializedTemplate,
749 const TemplateArgument *Args,
750 unsigned NumArgs,
751 TemplateArgumentLoc *ArgInfos,
752 unsigned NumArgInfos,
753 ClassTemplatePartialSpecializationDecl *PrevDecl,
754 unsigned SequenceNumber)
755 : ClassTemplateSpecializationDecl(Context,
756 ClassTemplatePartialSpecialization,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000757 TK, DC, StartLoc, IdLoc,
758 SpecializedTemplate,
Douglas Gregor9a299e02011-03-04 17:52:15 +0000759 Args, NumArgs, PrevDecl),
760 TemplateParams(Params), ArgsAsWritten(ArgInfos),
761 NumArgsAsWritten(NumArgInfos), SequenceNumber(SequenceNumber),
762 InstantiatedFromMember(0, false)
763{
Douglas Gregor787a40d2011-03-04 18:32:38 +0000764 AdoptTemplateParameterList(Params, this);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000765}
766
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000767ClassTemplatePartialSpecializationDecl *
768ClassTemplatePartialSpecializationDecl::
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000769Create(ASTContext &Context, TagKind TK,DeclContext *DC,
770 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000771 TemplateParameterList *Params,
772 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000773 const TemplateArgument *Args,
774 unsigned NumArgs,
John McCalld5532b62009-11-23 01:53:49 +0000775 const TemplateArgumentListInfo &ArgInfos,
John McCall3cb0ebd2010-03-10 03:28:59 +0000776 QualType CanonInjectedType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000777 ClassTemplatePartialSpecializationDecl *PrevDecl,
778 unsigned SequenceNumber) {
John McCalld5532b62009-11-23 01:53:49 +0000779 unsigned N = ArgInfos.size();
John McCall833ca992009-10-29 08:12:44 +0000780 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
781 for (unsigned I = 0; I != N; ++I)
782 ClonedArgs[I] = ArgInfos[I];
783
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000784 ClassTemplatePartialSpecializationDecl *Result
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000785 = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK, DC,
786 StartLoc, IdLoc,
787 Params,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000788 SpecializedTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +0000789 Args, NumArgs,
John McCall833ca992009-10-29 08:12:44 +0000790 ClonedArgs, N,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000791 PrevDecl,
792 SequenceNumber);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000793 Result->setSpecializationKind(TSK_ExplicitSpecialization);
John McCall3cb0ebd2010-03-10 03:28:59 +0000794
795 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000796 return Result;
797}
John McCalldd4a3b02009-09-16 22:47:08 +0000798
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000799ClassTemplatePartialSpecializationDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000800ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
801 unsigned ID) {
802 void *Mem = AllocateDeserializedDecl(C, ID,
803 sizeof(ClassTemplatePartialSpecializationDecl));
804 return new (Mem) ClassTemplatePartialSpecializationDecl();
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000805}
806
John McCalldd4a3b02009-09-16 22:47:08 +0000807//===----------------------------------------------------------------------===//
808// FriendTemplateDecl Implementation
809//===----------------------------------------------------------------------===//
810
David Blaikie99ba9e32011-12-20 02:48:34 +0000811void FriendTemplateDecl::anchor() { }
812
John McCalldd4a3b02009-09-16 22:47:08 +0000813FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
814 DeclContext *DC,
815 SourceLocation L,
816 unsigned NParams,
817 TemplateParameterList **Params,
818 FriendUnion Friend,
819 SourceLocation FLoc) {
820 FriendTemplateDecl *Result
821 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
822 return Result;
823}
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +0000824
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000825FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
826 unsigned ID) {
827 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FriendTemplateDecl));
828 return new (Mem) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +0000829}
Richard Smith3e4c6c42011-05-05 21:57:07 +0000830
831//===----------------------------------------------------------------------===//
832// TypeAliasTemplateDecl Implementation
833//===----------------------------------------------------------------------===//
834
835TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
836 DeclContext *DC,
837 SourceLocation L,
838 DeclarationName Name,
839 TemplateParameterList *Params,
840 NamedDecl *Decl) {
841 AdoptTemplateParameterList(Params, DC);
842 return new (C) TypeAliasTemplateDecl(DC, L, Name, Params, Decl);
843}
844
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000845TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
846 unsigned ID) {
847 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasTemplateDecl));
848 return new (Mem) TypeAliasTemplateDecl(0, SourceLocation(), DeclarationName(),
849 0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000850}
851
852void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) {
853 static_cast<Common *>(Ptr)->~Common();
854}
855RedeclarableTemplateDecl::CommonBase *
856TypeAliasTemplateDecl::newCommon(ASTContext &C) {
857 Common *CommonPtr = new (C) Common;
858 C.AddDeallocation(DeallocateCommon, CommonPtr);
859 return CommonPtr;
860}
861
David Blaikie99ba9e32011-12-20 02:48:34 +0000862//===----------------------------------------------------------------------===//
863// ClassScopeFunctionSpecializationDecl Implementation
864//===----------------------------------------------------------------------===//
865
866void ClassScopeFunctionSpecializationDecl::anchor() { }
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000867
868ClassScopeFunctionSpecializationDecl *
869ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
870 unsigned ID) {
871 void *Mem = AllocateDeserializedDecl(C, ID,
872 sizeof(ClassScopeFunctionSpecializationDecl));
Nico Weber6b020092012-06-25 17:21:05 +0000873 return new (Mem) ClassScopeFunctionSpecializationDecl(0, SourceLocation(), 0,
874 false, TemplateArgumentListInfo());
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000875}