blob: c498dea17703b59d2b2d5d027505ca9579246316 [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 Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/ASTContext.h"
John McCall833ca992009-10-29 08:12:44 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/Basic/IdentifierTable.h"
20#include "llvm/ADT/STLExtras.h"
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// TemplateParameterList Implementation
25//===----------------------------------------------------------------------===//
26
Douglas Gregorddc29e12009-02-06 22:42:48 +000027TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
28 SourceLocation LAngleLoc,
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029 NamedDecl **Params, unsigned NumParams,
Douglas Gregorddc29e12009-02-06 22:42:48 +000030 SourceLocation RAngleLoc)
31 : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
32 NumParams(NumParams) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +000033 for (unsigned Idx = 0; Idx < NumParams; ++Idx)
34 begin()[Idx] = Params[Idx];
35}
36
37TemplateParameterList *
Douglas Gregorddc29e12009-02-06 22:42:48 +000038TemplateParameterList::Create(ASTContext &C, SourceLocation TemplateLoc,
Douglas Gregorbf4ea562009-09-15 16:23:51 +000039 SourceLocation LAngleLoc, NamedDecl **Params,
Douglas Gregorddc29e12009-02-06 22:42:48 +000040 unsigned NumParams, SourceLocation RAngleLoc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +000041 unsigned Size = sizeof(TemplateParameterList)
42 + sizeof(NamedDecl *) * NumParams;
Douglas Gregoraaba5e32009-02-04 19:02:06 +000043 unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment;
44 void *Mem = C.Allocate(Size, Align);
Mike Stump1eb44332009-09-09 15:08:12 +000045 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
Douglas Gregorddc29e12009-02-06 22:42:48 +000046 NumParams, RAngleLoc);
Douglas Gregoraaba5e32009-02-04 19:02:06 +000047}
48
Douglas Gregor62cb18d2009-02-11 18:16:40 +000049unsigned TemplateParameterList::getMinRequiredArguments() const {
50 unsigned NumRequiredArgs = size();
Mike Stump1eb44332009-09-09 15:08:12 +000051 iterator Param = const_cast<TemplateParameterList *>(this)->end(),
Douglas Gregor62cb18d2009-02-11 18:16:40 +000052 ParamBegin = const_cast<TemplateParameterList *>(this)->begin();
53 while (Param != ParamBegin) {
54 --Param;
Mike Stump1eb44332009-09-09 15:08:12 +000055
Anders Carlsson0ceffb52009-06-13 02:08:00 +000056 if (!(*Param)->isTemplateParameterPack() &&
Mike Stump1eb44332009-09-09 15:08:12 +000057 !(isa<TemplateTypeParmDecl>(*Param) &&
Douglas Gregor62cb18d2009-02-11 18:16:40 +000058 cast<TemplateTypeParmDecl>(*Param)->hasDefaultArgument()) &&
59 !(isa<NonTypeTemplateParmDecl>(*Param) &&
60 cast<NonTypeTemplateParmDecl>(*Param)->hasDefaultArgument()) &&
61 !(isa<TemplateTemplateParmDecl>(*Param) &&
62 cast<TemplateTemplateParmDecl>(*Param)->hasDefaultArgument()))
63 break;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Douglas Gregor62cb18d2009-02-11 18:16:40 +000065 --NumRequiredArgs;
66 }
67
68 return NumRequiredArgs;
69}
70
Douglas Gregored9c0f92009-10-29 00:04:11 +000071unsigned TemplateParameterList::getDepth() const {
72 if (size() == 0)
73 return 0;
74
75 const NamedDecl *FirstParm = getParam(0);
76 if (const TemplateTypeParmDecl *TTP
77 = dyn_cast<TemplateTypeParmDecl>(FirstParm))
78 return TTP->getDepth();
79 else if (const NonTypeTemplateParmDecl *NTTP
80 = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
81 return NTTP->getDepth();
82 else
83 return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
84}
85
Douglas Gregoraaba5e32009-02-04 19:02:06 +000086//===----------------------------------------------------------------------===//
87// TemplateDecl Implementation
88//===----------------------------------------------------------------------===//
89
90TemplateDecl::~TemplateDecl() {
91}
92
93//===----------------------------------------------------------------------===//
94// FunctionTemplateDecl Implementation
95//===----------------------------------------------------------------------===//
96
97FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
98 DeclContext *DC,
99 SourceLocation L,
100 DeclarationName Name,
Douglas Gregor127102b2009-06-29 20:59:39 +0000101 TemplateParameterList *Params,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000102 NamedDecl *Decl) {
103 return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl);
104}
105
Douglas Gregor127102b2009-06-29 20:59:39 +0000106void FunctionTemplateDecl::Destroy(ASTContext &C) {
107 if (Common *CommonPtr = CommonOrPrev.dyn_cast<Common*>()) {
108 for (llvm::FoldingSet<FunctionTemplateSpecializationInfo>::iterator
109 Spec = CommonPtr->Specializations.begin(),
110 SpecEnd = CommonPtr->Specializations.end();
111 Spec != SpecEnd; ++Spec)
112 C.Deallocate(&*Spec);
113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Douglas Gregor127102b2009-06-29 20:59:39 +0000115 Decl::Destroy(C);
116}
117
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000118FunctionTemplateDecl *FunctionTemplateDecl::getCanonicalDecl() {
119 FunctionTemplateDecl *FunTmpl = this;
120 while (FunTmpl->getPreviousDeclaration())
121 FunTmpl = FunTmpl->getPreviousDeclaration();
122 return FunTmpl;
123}
124
Douglas Gregor127102b2009-06-29 20:59:39 +0000125FunctionTemplateDecl::Common *FunctionTemplateDecl::getCommonPtr() {
126 // Find the first declaration of this function template.
127 FunctionTemplateDecl *First = this;
128 while (First->getPreviousDeclaration())
129 First = First->getPreviousDeclaration();
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregor127102b2009-06-29 20:59:39 +0000131 if (First->CommonOrPrev.isNull()) {
132 // FIXME: Allocate with the ASTContext
133 First->CommonOrPrev = new Common;
134 }
135 return First->CommonOrPrev.get<Common*>();
136}
137
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000138//===----------------------------------------------------------------------===//
139// ClassTemplateDecl Implementation
140//===----------------------------------------------------------------------===//
141
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000142ClassTemplateDecl *ClassTemplateDecl::getCanonicalDecl() {
143 ClassTemplateDecl *Template = this;
144 while (Template->getPreviousDeclaration())
145 Template = Template->getPreviousDeclaration();
146 return Template;
147}
148
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000149ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
150 DeclContext *DC,
151 SourceLocation L,
152 DeclarationName Name,
153 TemplateParameterList *Params,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000154 NamedDecl *Decl,
155 ClassTemplateDecl *PrevDecl) {
156 Common *CommonPtr;
157 if (PrevDecl)
158 CommonPtr = PrevDecl->CommonPtr;
159 else
160 CommonPtr = new (C) Common;
161
Mike Stump1eb44332009-09-09 15:08:12 +0000162 return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl, PrevDecl,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000163 CommonPtr);
164}
165
166ClassTemplateDecl::~ClassTemplateDecl() {
167 assert(CommonPtr == 0 && "ClassTemplateDecl must be explicitly destroyed");
168}
169
170void ClassTemplateDecl::Destroy(ASTContext& C) {
171 if (!PreviousDeclaration) {
172 CommonPtr->~Common();
173 C.Deallocate((void*)CommonPtr);
174 }
175 CommonPtr = 0;
176
177 this->~ClassTemplateDecl();
178 C.Deallocate((void*)this);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000179}
180
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000181void ClassTemplateDecl::getPartialSpecializations(
182 llvm::SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
183 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs
184 = CommonPtr->PartialSpecializations;
185 PS.clear();
186 PS.resize(PartialSpecs.size());
187 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
188 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
189 P != PEnd; ++P) {
190 assert(!PS[P->getSequenceNumber()]);
191 PS[P->getSequenceNumber()] = &*P;
192 }
193}
194
Douglas Gregorb88e8882009-07-30 17:40:51 +0000195ClassTemplatePartialSpecializationDecl *
196ClassTemplateDecl::findPartialSpecialization(QualType T) {
197 ASTContext &Context = getASTContext();
198 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
199 partial_spec_iterator;
200 for (partial_spec_iterator P = getPartialSpecializations().begin(),
201 PEnd = getPartialSpecializations().end();
202 P != PEnd; ++P) {
John McCall31f17ec2010-04-27 00:57:59 +0000203 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregorb88e8882009-07-30 17:40:51 +0000204 return &*P;
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Douglas Gregorb88e8882009-07-30 17:40:51 +0000207 return 0;
208}
209
John McCall3cb0ebd2010-03-10 03:28:59 +0000210QualType
211ClassTemplateDecl::getInjectedClassNameSpecialization(ASTContext &Context) {
Douglas Gregor7da97d02009-05-10 22:57:19 +0000212 if (!CommonPtr->InjectedClassNameType.isNull())
213 return CommonPtr->InjectedClassNameType;
214
215 // FIXME: n2800 14.6.1p1 should say how the template arguments
216 // corresponding to template parameter packs should be pack
217 // expansions. We already say that in 14.6.2.1p2, so it would be
218 // better to fix that redundancy.
219
220 TemplateParameterList *Params = getTemplateParameters();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000221 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregor7da97d02009-05-10 22:57:19 +0000222 TemplateArgs.reserve(Params->size());
Mike Stump1eb44332009-09-09 15:08:12 +0000223 for (TemplateParameterList::iterator Param = Params->begin(),
224 ParamEnd = Params->end();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000225 Param != ParamEnd; ++Param) {
226 if (isa<TemplateTypeParmDecl>(*Param)) {
227 QualType ParamType = Context.getTypeDeclType(cast<TypeDecl>(*Param));
John McCall833ca992009-10-29 08:12:44 +0000228 TemplateArgs.push_back(TemplateArgument(ParamType));
Mike Stump1eb44332009-09-09 15:08:12 +0000229 } else if (NonTypeTemplateParmDecl *NTTP =
Douglas Gregor7da97d02009-05-10 22:57:19 +0000230 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Chandler Carruthb7de1812010-01-31 07:24:03 +0000231 Expr *E = new (Context) DeclRefExpr(NTTP,
232 NTTP->getType().getNonReferenceType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000233 NTTP->getLocation());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000234 TemplateArgs.push_back(TemplateArgument(E));
Mike Stump1eb44332009-09-09 15:08:12 +0000235 } else {
Douglas Gregor7da97d02009-05-10 22:57:19 +0000236 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregor788cd062009-11-11 01:00:40 +0000237 TemplateArgs.push_back(TemplateArgument(TemplateName(TTP)));
Douglas Gregor7da97d02009-05-10 22:57:19 +0000238 }
239 }
240
Douglas Gregor7da97d02009-05-10 22:57:19 +0000241 CommonPtr->InjectedClassNameType
Douglas Gregor1275ae02009-07-28 23:00:59 +0000242 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregor7da97d02009-05-10 22:57:19 +0000243 &TemplateArgs[0],
Douglas Gregor1275ae02009-07-28 23:00:59 +0000244 TemplateArgs.size());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000245 return CommonPtr->InjectedClassNameType;
246}
247
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000248//===----------------------------------------------------------------------===//
249// TemplateTypeParm Allocation/Deallocation Method Implementations
250//===----------------------------------------------------------------------===//
251
252TemplateTypeParmDecl *
253TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
254 SourceLocation L, unsigned D, unsigned P,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000255 IdentifierInfo *Id, bool Typename,
256 bool ParameterPack) {
Anders Carlsson76e4ce42009-06-16 00:30:48 +0000257 QualType Type = C.getTemplateTypeParmType(D, P, ParameterPack, Id);
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000258 return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type, ParameterPack);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000259}
260
John McCall833ca992009-10-29 08:12:44 +0000261SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
262 return DefaultArgument->getTypeLoc().getFullSourceRange().getBegin();
263}
264
Douglas Gregored9c0f92009-10-29 00:04:11 +0000265unsigned TemplateTypeParmDecl::getDepth() const {
266 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
267}
268
269unsigned TemplateTypeParmDecl::getIndex() const {
270 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
271}
272
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000273//===----------------------------------------------------------------------===//
274// NonTypeTemplateParmDecl Method Implementations
275//===----------------------------------------------------------------------===//
276
277NonTypeTemplateParmDecl *
278NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
279 SourceLocation L, unsigned D, unsigned P,
280 IdentifierInfo *Id, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000281 TypeSourceInfo *TInfo) {
282 return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, TInfo);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000283}
284
Douglas Gregord684b002009-02-10 19:49:53 +0000285SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
286 return DefaultArgument? DefaultArgument->getSourceRange().getBegin()
Mike Stump1eb44332009-09-09 15:08:12 +0000287 : SourceLocation();
Douglas Gregord684b002009-02-10 19:49:53 +0000288}
289
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000290//===----------------------------------------------------------------------===//
291// TemplateTemplateParmDecl Method Implementations
292//===----------------------------------------------------------------------===//
293
294TemplateTemplateParmDecl *
295TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
296 SourceLocation L, unsigned D, unsigned P,
297 IdentifierInfo *Id,
298 TemplateParameterList *Params) {
299 return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params);
300}
301
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000302//===----------------------------------------------------------------------===//
Anders Carlsson9ba41642009-06-05 05:31:27 +0000303// TemplateArgumentListBuilder Implementation
304//===----------------------------------------------------------------------===//
Anders Carlssonfb250522009-06-23 01:26:57 +0000305
306void TemplateArgumentListBuilder::Append(const TemplateArgument& Arg) {
Anders Carlsson9ba41642009-06-05 05:31:27 +0000307 switch (Arg.getKind()) {
Anders Carlssonfb250522009-06-23 01:26:57 +0000308 default: break;
309 case TemplateArgument::Type:
John McCall467b27b2009-10-22 20:10:53 +0000310 assert(Arg.getAsType().isCanonical() && "Type must be canonical!");
Anders Carlssonfb250522009-06-23 01:26:57 +0000311 break;
Anders Carlsson9ba41642009-06-05 05:31:27 +0000312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Anders Carlssonfb250522009-06-23 01:26:57 +0000314 assert(NumFlatArgs < MaxFlatArgs && "Argument list builder is full!");
Mike Stump1eb44332009-09-09 15:08:12 +0000315 assert(!StructuredArgs &&
Anders Carlssonfb250522009-06-23 01:26:57 +0000316 "Can't append arguments when an argument pack has been added!");
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Anders Carlssonfb250522009-06-23 01:26:57 +0000318 if (!FlatArgs)
319 FlatArgs = new TemplateArgument[MaxFlatArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Anders Carlssonfb250522009-06-23 01:26:57 +0000321 FlatArgs[NumFlatArgs++] = Arg;
Anders Carlsson9ba41642009-06-05 05:31:27 +0000322}
323
Anders Carlssonfb250522009-06-23 01:26:57 +0000324void TemplateArgumentListBuilder::BeginPack() {
325 assert(!AddingToPack && "Already adding to pack!");
326 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Anders Carlssonfb250522009-06-23 01:26:57 +0000328 AddingToPack = true;
329 PackBeginIndex = NumFlatArgs;
Anders Carlsson67e33202009-06-13 00:08:58 +0000330}
331
Anders Carlssonfb250522009-06-23 01:26:57 +0000332void TemplateArgumentListBuilder::EndPack() {
333 assert(AddingToPack && "Not adding to pack!");
334 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Anders Carlssonfb250522009-06-23 01:26:57 +0000336 AddingToPack = false;
Anders Carlsson3b36b662009-06-15 17:56:45 +0000337
Anders Carlssonfb250522009-06-23 01:26:57 +0000338 StructuredArgs = new TemplateArgument[MaxStructuredArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Anders Carlssonfb250522009-06-23 01:26:57 +0000340 // First copy the flat entries over to the list (if any)
341 for (unsigned I = 0; I != PackBeginIndex; ++I) {
342 NumStructuredArgs++;
343 StructuredArgs[I] = FlatArgs[I];
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Anders Carlssonfb250522009-06-23 01:26:57 +0000346 // Next, set the pack.
347 TemplateArgument *PackArgs = 0;
348 unsigned NumPackArgs = NumFlatArgs - PackBeginIndex;
349 if (NumPackArgs)
350 PackArgs = &FlatArgs[PackBeginIndex];
Mike Stump1eb44332009-09-09 15:08:12 +0000351
352 StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs,
Anders Carlssonfb250522009-06-23 01:26:57 +0000353 /*CopyArgs=*/false);
354}
355
356void TemplateArgumentListBuilder::ReleaseArgs() {
357 FlatArgs = 0;
358 NumFlatArgs = 0;
359 MaxFlatArgs = 0;
360 StructuredArgs = 0;
361 NumStructuredArgs = 0;
362 MaxStructuredArgs = 0;
363}
Anders Carlsson67e33202009-06-13 00:08:58 +0000364
Anders Carlsson9ba41642009-06-05 05:31:27 +0000365//===----------------------------------------------------------------------===//
Douglas Gregor7e063902009-05-11 23:53:27 +0000366// TemplateArgumentList Implementation
367//===----------------------------------------------------------------------===//
368TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
Anders Carlssone9c904b2009-06-05 04:47:51 +0000369 TemplateArgumentListBuilder &Builder,
Anders Carlssonfb250522009-06-23 01:26:57 +0000370 bool TakeArgs)
Mike Stump1eb44332009-09-09 15:08:12 +0000371 : FlatArguments(Builder.getFlatArguments(), TakeArgs),
372 NumFlatArguments(Builder.flatSize()),
Anders Carlssonfb250522009-06-23 01:26:57 +0000373 StructuredArguments(Builder.getStructuredArguments(), TakeArgs),
374 NumStructuredArguments(Builder.structuredSize()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Anders Carlssonfb250522009-06-23 01:26:57 +0000376 if (!TakeArgs)
377 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Anders Carlssonfb250522009-06-23 01:26:57 +0000379 if (Builder.getStructuredArguments() == Builder.getFlatArguments())
380 StructuredArguments.setInt(0);
381 Builder.ReleaseArgs();
Douglas Gregor7e063902009-05-11 23:53:27 +0000382}
383
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000384TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList &Other)
385 : FlatArguments(Other.FlatArguments.getPointer(), 1),
386 NumFlatArguments(Other.flat_size()),
387 StructuredArguments(Other.StructuredArguments.getPointer(), 1),
388 NumStructuredArguments(Other.NumStructuredArguments) { }
389
Douglas Gregor7e063902009-05-11 23:53:27 +0000390TemplateArgumentList::~TemplateArgumentList() {
391 // FIXME: Deallocate template arguments
392}
393
394//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000395// ClassTemplateSpecializationDecl Implementation
396//===----------------------------------------------------------------------===//
397ClassTemplateSpecializationDecl::
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000398ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK,
Douglas Gregor7e063902009-05-11 23:53:27 +0000399 DeclContext *DC, SourceLocation L,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000400 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000401 TemplateArgumentListBuilder &Builder,
402 ClassTemplateSpecializationDecl *PrevDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000403 : CXXRecordDecl(DK,
404 SpecializedTemplate->getTemplatedDecl()->getTagKind(),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000405 DC, L,
406 // FIXME: Should we use DeclarationName for the name of
407 // class template specializations?
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000408 SpecializedTemplate->getIdentifier(),
409 PrevDecl),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000410 SpecializedTemplate(SpecializedTemplate),
John McCall3cb0ebd2010-03-10 03:28:59 +0000411 TypeAsWritten(0),
Anders Carlssonfb250522009-06-23 01:26:57 +0000412 TemplateArgs(Context, Builder, /*TakeArgs=*/true),
Douglas Gregor7e063902009-05-11 23:53:27 +0000413 SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000414}
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000416ClassTemplateSpecializationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000417ClassTemplateSpecializationDecl::Create(ASTContext &Context,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000418 DeclContext *DC, SourceLocation L,
419 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000420 TemplateArgumentListBuilder &Builder,
Douglas Gregorcc636682009-02-17 23:15:12 +0000421 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000422 ClassTemplateSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000423 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000424 ClassTemplateSpecialization,
Mike Stump1eb44332009-09-09 15:08:12 +0000425 DC, L,
Douglas Gregor7e063902009-05-11 23:53:27 +0000426 SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000427 Builder,
428 PrevDecl);
Douglas Gregorcc636682009-02-17 23:15:12 +0000429 Context.getTypeDeclType(Result, PrevDecl);
430 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000431}
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000432
Douglas Gregor37d93e92009-08-02 23:24:31 +0000433void ClassTemplateSpecializationDecl::Destroy(ASTContext &C) {
Mike Stump1eb44332009-09-09 15:08:12 +0000434 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000435 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
436 C.Deallocate(PartialSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Douglas Gregor37d93e92009-08-02 23:24:31 +0000438 CXXRecordDecl::Destroy(C);
439}
440
John McCall136a6982009-09-11 06:45:03 +0000441void
442ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
443 const PrintingPolicy &Policy,
444 bool Qualified) const {
445 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
446
447 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
448 S += TemplateSpecializationType::PrintTemplateArgumentList(
449 TemplateArgs.getFlatArgumentList(),
450 TemplateArgs.flat_size(),
451 Policy);
452}
453
Douglas Gregor37d93e92009-08-02 23:24:31 +0000454ClassTemplateDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000455ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
456 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000457 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
458 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
459 return SpecializedTemplate.get<ClassTemplateDecl*>();
460}
461
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000462//===----------------------------------------------------------------------===//
463// ClassTemplatePartialSpecializationDecl Implementation
464//===----------------------------------------------------------------------===//
465ClassTemplatePartialSpecializationDecl *
466ClassTemplatePartialSpecializationDecl::
467Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
468 TemplateParameterList *Params,
469 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000470 TemplateArgumentListBuilder &Builder,
John McCalld5532b62009-11-23 01:53:49 +0000471 const TemplateArgumentListInfo &ArgInfos,
John McCall3cb0ebd2010-03-10 03:28:59 +0000472 QualType CanonInjectedType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000473 ClassTemplatePartialSpecializationDecl *PrevDecl,
474 unsigned SequenceNumber) {
John McCalld5532b62009-11-23 01:53:49 +0000475 unsigned N = ArgInfos.size();
John McCall833ca992009-10-29 08:12:44 +0000476 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
477 for (unsigned I = 0; I != N; ++I)
478 ClonedArgs[I] = ArgInfos[I];
479
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000480 ClassTemplatePartialSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000481 = new (Context)ClassTemplatePartialSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000482 DC, L, Params,
483 SpecializedTemplate,
John McCall833ca992009-10-29 08:12:44 +0000484 Builder,
485 ClonedArgs, N,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000486 PrevDecl,
487 SequenceNumber);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000488 Result->setSpecializationKind(TSK_ExplicitSpecialization);
John McCall3cb0ebd2010-03-10 03:28:59 +0000489
490 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000491 return Result;
492}
John McCalldd4a3b02009-09-16 22:47:08 +0000493
494//===----------------------------------------------------------------------===//
495// FriendTemplateDecl Implementation
496//===----------------------------------------------------------------------===//
497
498FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
499 DeclContext *DC,
500 SourceLocation L,
501 unsigned NParams,
502 TemplateParameterList **Params,
503 FriendUnion Friend,
504 SourceLocation FLoc) {
505 FriendTemplateDecl *Result
506 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
507 return Result;
508}