blob: 2d2851036c1eb566f0ea514d6304711efb2a8c0a [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 Gregorb88e8882009-07-30 17:40:51 +0000181ClassTemplatePartialSpecializationDecl *
182ClassTemplateDecl::findPartialSpecialization(QualType T) {
183 ASTContext &Context = getASTContext();
184 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
185 partial_spec_iterator;
186 for (partial_spec_iterator P = getPartialSpecializations().begin(),
187 PEnd = getPartialSpecializations().end();
188 P != PEnd; ++P) {
189 if (Context.hasSameType(Context.getTypeDeclType(&*P), T))
190 return &*P;
191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Douglas Gregorb88e8882009-07-30 17:40:51 +0000193 return 0;
194}
195
Douglas Gregor7da97d02009-05-10 22:57:19 +0000196QualType ClassTemplateDecl::getInjectedClassNameType(ASTContext &Context) {
197 if (!CommonPtr->InjectedClassNameType.isNull())
198 return CommonPtr->InjectedClassNameType;
199
200 // FIXME: n2800 14.6.1p1 should say how the template arguments
201 // corresponding to template parameter packs should be pack
202 // expansions. We already say that in 14.6.2.1p2, so it would be
203 // better to fix that redundancy.
204
205 TemplateParameterList *Params = getTemplateParameters();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000206 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregor7da97d02009-05-10 22:57:19 +0000207 TemplateArgs.reserve(Params->size());
Mike Stump1eb44332009-09-09 15:08:12 +0000208 for (TemplateParameterList::iterator Param = Params->begin(),
209 ParamEnd = Params->end();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000210 Param != ParamEnd; ++Param) {
211 if (isa<TemplateTypeParmDecl>(*Param)) {
212 QualType ParamType = Context.getTypeDeclType(cast<TypeDecl>(*Param));
John McCall833ca992009-10-29 08:12:44 +0000213 TemplateArgs.push_back(TemplateArgument(ParamType));
Mike Stump1eb44332009-09-09 15:08:12 +0000214 } else if (NonTypeTemplateParmDecl *NTTP =
Douglas Gregor7da97d02009-05-10 22:57:19 +0000215 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Douglas Gregor7da97d02009-05-10 22:57:19 +0000216 Expr *E = new (Context) DeclRefExpr(NTTP, NTTP->getType(),
217 NTTP->getLocation(),
218 NTTP->getType()->isDependentType(),
219 /*Value-dependent=*/true);
220 TemplateArgs.push_back(TemplateArgument(E));
Mike Stump1eb44332009-09-09 15:08:12 +0000221 } else {
Douglas Gregor7da97d02009-05-10 22:57:19 +0000222 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregor788cd062009-11-11 01:00:40 +0000223 TemplateArgs.push_back(TemplateArgument(TemplateName(TTP)));
Douglas Gregor7da97d02009-05-10 22:57:19 +0000224 }
225 }
226
Douglas Gregor7da97d02009-05-10 22:57:19 +0000227 CommonPtr->InjectedClassNameType
Douglas Gregor1275ae02009-07-28 23:00:59 +0000228 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregor7da97d02009-05-10 22:57:19 +0000229 &TemplateArgs[0],
Douglas Gregor1275ae02009-07-28 23:00:59 +0000230 TemplateArgs.size());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000231 return CommonPtr->InjectedClassNameType;
232}
233
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000234//===----------------------------------------------------------------------===//
235// TemplateTypeParm Allocation/Deallocation Method Implementations
236//===----------------------------------------------------------------------===//
237
238TemplateTypeParmDecl *
239TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
240 SourceLocation L, unsigned D, unsigned P,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000241 IdentifierInfo *Id, bool Typename,
242 bool ParameterPack) {
Anders Carlsson76e4ce42009-06-16 00:30:48 +0000243 QualType Type = C.getTemplateTypeParmType(D, P, ParameterPack, Id);
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000244 return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type, ParameterPack);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000245}
246
John McCall833ca992009-10-29 08:12:44 +0000247SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
248 return DefaultArgument->getTypeLoc().getFullSourceRange().getBegin();
249}
250
Douglas Gregored9c0f92009-10-29 00:04:11 +0000251unsigned TemplateTypeParmDecl::getDepth() const {
252 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
253}
254
255unsigned TemplateTypeParmDecl::getIndex() const {
256 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
257}
258
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000259//===----------------------------------------------------------------------===//
260// NonTypeTemplateParmDecl Method Implementations
261//===----------------------------------------------------------------------===//
262
263NonTypeTemplateParmDecl *
264NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
265 SourceLocation L, unsigned D, unsigned P,
266 IdentifierInfo *Id, QualType T,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000267 DeclaratorInfo *DInfo) {
268 return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, DInfo);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000269}
270
Douglas Gregord684b002009-02-10 19:49:53 +0000271SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
272 return DefaultArgument? DefaultArgument->getSourceRange().getBegin()
Mike Stump1eb44332009-09-09 15:08:12 +0000273 : SourceLocation();
Douglas Gregord684b002009-02-10 19:49:53 +0000274}
275
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000276//===----------------------------------------------------------------------===//
277// TemplateTemplateParmDecl Method Implementations
278//===----------------------------------------------------------------------===//
279
280TemplateTemplateParmDecl *
281TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
282 SourceLocation L, unsigned D, unsigned P,
283 IdentifierInfo *Id,
284 TemplateParameterList *Params) {
285 return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params);
286}
287
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000288//===----------------------------------------------------------------------===//
Anders Carlsson9ba41642009-06-05 05:31:27 +0000289// TemplateArgumentListBuilder Implementation
290//===----------------------------------------------------------------------===//
Anders Carlssonfb250522009-06-23 01:26:57 +0000291
292void TemplateArgumentListBuilder::Append(const TemplateArgument& Arg) {
Anders Carlsson9ba41642009-06-05 05:31:27 +0000293 switch (Arg.getKind()) {
Anders Carlssonfb250522009-06-23 01:26:57 +0000294 default: break;
295 case TemplateArgument::Type:
John McCall467b27b2009-10-22 20:10:53 +0000296 assert(Arg.getAsType().isCanonical() && "Type must be canonical!");
Anders Carlssonfb250522009-06-23 01:26:57 +0000297 break;
Anders Carlsson9ba41642009-06-05 05:31:27 +0000298 }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Anders Carlssonfb250522009-06-23 01:26:57 +0000300 assert(NumFlatArgs < MaxFlatArgs && "Argument list builder is full!");
Mike Stump1eb44332009-09-09 15:08:12 +0000301 assert(!StructuredArgs &&
Anders Carlssonfb250522009-06-23 01:26:57 +0000302 "Can't append arguments when an argument pack has been added!");
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Anders Carlssonfb250522009-06-23 01:26:57 +0000304 if (!FlatArgs)
305 FlatArgs = new TemplateArgument[MaxFlatArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Anders Carlssonfb250522009-06-23 01:26:57 +0000307 FlatArgs[NumFlatArgs++] = Arg;
Anders Carlsson9ba41642009-06-05 05:31:27 +0000308}
309
Anders Carlssonfb250522009-06-23 01:26:57 +0000310void TemplateArgumentListBuilder::BeginPack() {
311 assert(!AddingToPack && "Already adding to pack!");
312 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Anders Carlssonfb250522009-06-23 01:26:57 +0000314 AddingToPack = true;
315 PackBeginIndex = NumFlatArgs;
Anders Carlsson67e33202009-06-13 00:08:58 +0000316}
317
Anders Carlssonfb250522009-06-23 01:26:57 +0000318void TemplateArgumentListBuilder::EndPack() {
319 assert(AddingToPack && "Not adding to pack!");
320 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Anders Carlssonfb250522009-06-23 01:26:57 +0000322 AddingToPack = false;
Anders Carlsson3b36b662009-06-15 17:56:45 +0000323
Anders Carlssonfb250522009-06-23 01:26:57 +0000324 StructuredArgs = new TemplateArgument[MaxStructuredArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Anders Carlssonfb250522009-06-23 01:26:57 +0000326 // First copy the flat entries over to the list (if any)
327 for (unsigned I = 0; I != PackBeginIndex; ++I) {
328 NumStructuredArgs++;
329 StructuredArgs[I] = FlatArgs[I];
330 }
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Anders Carlssonfb250522009-06-23 01:26:57 +0000332 // Next, set the pack.
333 TemplateArgument *PackArgs = 0;
334 unsigned NumPackArgs = NumFlatArgs - PackBeginIndex;
335 if (NumPackArgs)
336 PackArgs = &FlatArgs[PackBeginIndex];
Mike Stump1eb44332009-09-09 15:08:12 +0000337
338 StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs,
Anders Carlssonfb250522009-06-23 01:26:57 +0000339 /*CopyArgs=*/false);
340}
341
342void TemplateArgumentListBuilder::ReleaseArgs() {
343 FlatArgs = 0;
344 NumFlatArgs = 0;
345 MaxFlatArgs = 0;
346 StructuredArgs = 0;
347 NumStructuredArgs = 0;
348 MaxStructuredArgs = 0;
349}
Anders Carlsson67e33202009-06-13 00:08:58 +0000350
Anders Carlsson9ba41642009-06-05 05:31:27 +0000351//===----------------------------------------------------------------------===//
Douglas Gregor7e063902009-05-11 23:53:27 +0000352// TemplateArgumentList Implementation
353//===----------------------------------------------------------------------===//
354TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
Anders Carlssone9c904b2009-06-05 04:47:51 +0000355 TemplateArgumentListBuilder &Builder,
Anders Carlssonfb250522009-06-23 01:26:57 +0000356 bool TakeArgs)
Mike Stump1eb44332009-09-09 15:08:12 +0000357 : FlatArguments(Builder.getFlatArguments(), TakeArgs),
358 NumFlatArguments(Builder.flatSize()),
Anders Carlssonfb250522009-06-23 01:26:57 +0000359 StructuredArguments(Builder.getStructuredArguments(), TakeArgs),
360 NumStructuredArguments(Builder.structuredSize()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Anders Carlssonfb250522009-06-23 01:26:57 +0000362 if (!TakeArgs)
363 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Anders Carlssonfb250522009-06-23 01:26:57 +0000365 if (Builder.getStructuredArguments() == Builder.getFlatArguments())
366 StructuredArguments.setInt(0);
367 Builder.ReleaseArgs();
Douglas Gregor7e063902009-05-11 23:53:27 +0000368}
369
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000370TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList &Other)
371 : FlatArguments(Other.FlatArguments.getPointer(), 1),
372 NumFlatArguments(Other.flat_size()),
373 StructuredArguments(Other.StructuredArguments.getPointer(), 1),
374 NumStructuredArguments(Other.NumStructuredArguments) { }
375
Douglas Gregor7e063902009-05-11 23:53:27 +0000376TemplateArgumentList::~TemplateArgumentList() {
377 // FIXME: Deallocate template arguments
378}
379
380//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000381// ClassTemplateSpecializationDecl Implementation
382//===----------------------------------------------------------------------===//
383ClassTemplateSpecializationDecl::
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000384ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK,
Douglas Gregor7e063902009-05-11 23:53:27 +0000385 DeclContext *DC, SourceLocation L,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000386 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000387 TemplateArgumentListBuilder &Builder,
388 ClassTemplateSpecializationDecl *PrevDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000389 : CXXRecordDecl(DK,
390 SpecializedTemplate->getTemplatedDecl()->getTagKind(),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000391 DC, L,
392 // FIXME: Should we use DeclarationName for the name of
393 // class template specializations?
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000394 SpecializedTemplate->getIdentifier(),
395 PrevDecl),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000396 SpecializedTemplate(SpecializedTemplate),
Anders Carlssonfb250522009-06-23 01:26:57 +0000397 TemplateArgs(Context, Builder, /*TakeArgs=*/true),
Douglas Gregor7e063902009-05-11 23:53:27 +0000398 SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000399}
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000401ClassTemplateSpecializationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000402ClassTemplateSpecializationDecl::Create(ASTContext &Context,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000403 DeclContext *DC, SourceLocation L,
404 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000405 TemplateArgumentListBuilder &Builder,
Douglas Gregorcc636682009-02-17 23:15:12 +0000406 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000407 ClassTemplateSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000408 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000409 ClassTemplateSpecialization,
Mike Stump1eb44332009-09-09 15:08:12 +0000410 DC, L,
Douglas Gregor7e063902009-05-11 23:53:27 +0000411 SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000412 Builder,
413 PrevDecl);
Douglas Gregorcc636682009-02-17 23:15:12 +0000414 Context.getTypeDeclType(Result, PrevDecl);
415 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000416}
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000417
Douglas Gregor37d93e92009-08-02 23:24:31 +0000418void ClassTemplateSpecializationDecl::Destroy(ASTContext &C) {
Mike Stump1eb44332009-09-09 15:08:12 +0000419 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000420 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
421 C.Deallocate(PartialSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Douglas Gregor37d93e92009-08-02 23:24:31 +0000423 CXXRecordDecl::Destroy(C);
424}
425
John McCall136a6982009-09-11 06:45:03 +0000426void
427ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
428 const PrintingPolicy &Policy,
429 bool Qualified) const {
430 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
431
432 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
433 S += TemplateSpecializationType::PrintTemplateArgumentList(
434 TemplateArgs.getFlatArgumentList(),
435 TemplateArgs.flat_size(),
436 Policy);
437}
438
Douglas Gregor37d93e92009-08-02 23:24:31 +0000439ClassTemplateDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000440ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
441 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000442 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
443 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
444 return SpecializedTemplate.get<ClassTemplateDecl*>();
445}
446
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000447//===----------------------------------------------------------------------===//
448// ClassTemplatePartialSpecializationDecl Implementation
449//===----------------------------------------------------------------------===//
450ClassTemplatePartialSpecializationDecl *
451ClassTemplatePartialSpecializationDecl::
452Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
453 TemplateParameterList *Params,
454 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000455 TemplateArgumentListBuilder &Builder,
John McCalld5532b62009-11-23 01:53:49 +0000456 const TemplateArgumentListInfo &ArgInfos,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000457 ClassTemplatePartialSpecializationDecl *PrevDecl) {
John McCalld5532b62009-11-23 01:53:49 +0000458 unsigned N = ArgInfos.size();
John McCall833ca992009-10-29 08:12:44 +0000459 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
460 for (unsigned I = 0; I != N; ++I)
461 ClonedArgs[I] = ArgInfos[I];
462
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000463 ClassTemplatePartialSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000464 = new (Context)ClassTemplatePartialSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000465 DC, L, Params,
466 SpecializedTemplate,
John McCall833ca992009-10-29 08:12:44 +0000467 Builder,
468 ClonedArgs, N,
469 PrevDecl);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000470 Result->setSpecializationKind(TSK_ExplicitSpecialization);
471 Context.getTypeDeclType(Result, PrevDecl);
472 return Result;
473}
John McCalldd4a3b02009-09-16 22:47:08 +0000474
475//===----------------------------------------------------------------------===//
476// FriendTemplateDecl Implementation
477//===----------------------------------------------------------------------===//
478
479FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
480 DeclContext *DC,
481 SourceLocation L,
482 unsigned NParams,
483 TemplateParameterList **Params,
484 FriendUnion Friend,
485 SourceLocation FLoc) {
486 FriendTemplateDecl *Result
487 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
488 return Result;
489}