blob: 75b3975322b225b90fa7e824db6bccd63cd9b960 [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(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000217 NTTP->getLocation());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000218 TemplateArgs.push_back(TemplateArgument(E));
Mike Stump1eb44332009-09-09 15:08:12 +0000219 } else {
Douglas Gregor7da97d02009-05-10 22:57:19 +0000220 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregor788cd062009-11-11 01:00:40 +0000221 TemplateArgs.push_back(TemplateArgument(TemplateName(TTP)));
Douglas Gregor7da97d02009-05-10 22:57:19 +0000222 }
223 }
224
Douglas Gregor7da97d02009-05-10 22:57:19 +0000225 CommonPtr->InjectedClassNameType
Douglas Gregor1275ae02009-07-28 23:00:59 +0000226 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregor7da97d02009-05-10 22:57:19 +0000227 &TemplateArgs[0],
Douglas Gregor1275ae02009-07-28 23:00:59 +0000228 TemplateArgs.size());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000229 return CommonPtr->InjectedClassNameType;
230}
231
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000232//===----------------------------------------------------------------------===//
233// TemplateTypeParm Allocation/Deallocation Method Implementations
234//===----------------------------------------------------------------------===//
235
236TemplateTypeParmDecl *
237TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
238 SourceLocation L, unsigned D, unsigned P,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000239 IdentifierInfo *Id, bool Typename,
240 bool ParameterPack) {
Anders Carlsson76e4ce42009-06-16 00:30:48 +0000241 QualType Type = C.getTemplateTypeParmType(D, P, ParameterPack, Id);
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000242 return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type, ParameterPack);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000243}
244
John McCall833ca992009-10-29 08:12:44 +0000245SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
246 return DefaultArgument->getTypeLoc().getFullSourceRange().getBegin();
247}
248
Douglas Gregored9c0f92009-10-29 00:04:11 +0000249unsigned TemplateTypeParmDecl::getDepth() const {
250 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
251}
252
253unsigned TemplateTypeParmDecl::getIndex() const {
254 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
255}
256
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000257//===----------------------------------------------------------------------===//
258// NonTypeTemplateParmDecl Method Implementations
259//===----------------------------------------------------------------------===//
260
261NonTypeTemplateParmDecl *
262NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
263 SourceLocation L, unsigned D, unsigned P,
264 IdentifierInfo *Id, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000265 TypeSourceInfo *TInfo) {
266 return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, TInfo);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000267}
268
Douglas Gregord684b002009-02-10 19:49:53 +0000269SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
270 return DefaultArgument? DefaultArgument->getSourceRange().getBegin()
Mike Stump1eb44332009-09-09 15:08:12 +0000271 : SourceLocation();
Douglas Gregord684b002009-02-10 19:49:53 +0000272}
273
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000274//===----------------------------------------------------------------------===//
275// TemplateTemplateParmDecl Method Implementations
276//===----------------------------------------------------------------------===//
277
278TemplateTemplateParmDecl *
279TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
280 SourceLocation L, unsigned D, unsigned P,
281 IdentifierInfo *Id,
282 TemplateParameterList *Params) {
283 return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params);
284}
285
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000286//===----------------------------------------------------------------------===//
Anders Carlsson9ba41642009-06-05 05:31:27 +0000287// TemplateArgumentListBuilder Implementation
288//===----------------------------------------------------------------------===//
Anders Carlssonfb250522009-06-23 01:26:57 +0000289
290void TemplateArgumentListBuilder::Append(const TemplateArgument& Arg) {
Anders Carlsson9ba41642009-06-05 05:31:27 +0000291 switch (Arg.getKind()) {
Anders Carlssonfb250522009-06-23 01:26:57 +0000292 default: break;
293 case TemplateArgument::Type:
John McCall467b27b2009-10-22 20:10:53 +0000294 assert(Arg.getAsType().isCanonical() && "Type must be canonical!");
Anders Carlssonfb250522009-06-23 01:26:57 +0000295 break;
Anders Carlsson9ba41642009-06-05 05:31:27 +0000296 }
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Anders Carlssonfb250522009-06-23 01:26:57 +0000298 assert(NumFlatArgs < MaxFlatArgs && "Argument list builder is full!");
Mike Stump1eb44332009-09-09 15:08:12 +0000299 assert(!StructuredArgs &&
Anders Carlssonfb250522009-06-23 01:26:57 +0000300 "Can't append arguments when an argument pack has been added!");
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Anders Carlssonfb250522009-06-23 01:26:57 +0000302 if (!FlatArgs)
303 FlatArgs = new TemplateArgument[MaxFlatArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Anders Carlssonfb250522009-06-23 01:26:57 +0000305 FlatArgs[NumFlatArgs++] = Arg;
Anders Carlsson9ba41642009-06-05 05:31:27 +0000306}
307
Anders Carlssonfb250522009-06-23 01:26:57 +0000308void TemplateArgumentListBuilder::BeginPack() {
309 assert(!AddingToPack && "Already adding to pack!");
310 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Anders Carlssonfb250522009-06-23 01:26:57 +0000312 AddingToPack = true;
313 PackBeginIndex = NumFlatArgs;
Anders Carlsson67e33202009-06-13 00:08:58 +0000314}
315
Anders Carlssonfb250522009-06-23 01:26:57 +0000316void TemplateArgumentListBuilder::EndPack() {
317 assert(AddingToPack && "Not adding to pack!");
318 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Anders Carlssonfb250522009-06-23 01:26:57 +0000320 AddingToPack = false;
Anders Carlsson3b36b662009-06-15 17:56:45 +0000321
Anders Carlssonfb250522009-06-23 01:26:57 +0000322 StructuredArgs = new TemplateArgument[MaxStructuredArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Anders Carlssonfb250522009-06-23 01:26:57 +0000324 // First copy the flat entries over to the list (if any)
325 for (unsigned I = 0; I != PackBeginIndex; ++I) {
326 NumStructuredArgs++;
327 StructuredArgs[I] = FlatArgs[I];
328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Anders Carlssonfb250522009-06-23 01:26:57 +0000330 // Next, set the pack.
331 TemplateArgument *PackArgs = 0;
332 unsigned NumPackArgs = NumFlatArgs - PackBeginIndex;
333 if (NumPackArgs)
334 PackArgs = &FlatArgs[PackBeginIndex];
Mike Stump1eb44332009-09-09 15:08:12 +0000335
336 StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs,
Anders Carlssonfb250522009-06-23 01:26:57 +0000337 /*CopyArgs=*/false);
338}
339
340void TemplateArgumentListBuilder::ReleaseArgs() {
341 FlatArgs = 0;
342 NumFlatArgs = 0;
343 MaxFlatArgs = 0;
344 StructuredArgs = 0;
345 NumStructuredArgs = 0;
346 MaxStructuredArgs = 0;
347}
Anders Carlsson67e33202009-06-13 00:08:58 +0000348
Anders Carlsson9ba41642009-06-05 05:31:27 +0000349//===----------------------------------------------------------------------===//
Douglas Gregor7e063902009-05-11 23:53:27 +0000350// TemplateArgumentList Implementation
351//===----------------------------------------------------------------------===//
352TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
Anders Carlssone9c904b2009-06-05 04:47:51 +0000353 TemplateArgumentListBuilder &Builder,
Anders Carlssonfb250522009-06-23 01:26:57 +0000354 bool TakeArgs)
Mike Stump1eb44332009-09-09 15:08:12 +0000355 : FlatArguments(Builder.getFlatArguments(), TakeArgs),
356 NumFlatArguments(Builder.flatSize()),
Anders Carlssonfb250522009-06-23 01:26:57 +0000357 StructuredArguments(Builder.getStructuredArguments(), TakeArgs),
358 NumStructuredArguments(Builder.structuredSize()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Anders Carlssonfb250522009-06-23 01:26:57 +0000360 if (!TakeArgs)
361 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Anders Carlssonfb250522009-06-23 01:26:57 +0000363 if (Builder.getStructuredArguments() == Builder.getFlatArguments())
364 StructuredArguments.setInt(0);
365 Builder.ReleaseArgs();
Douglas Gregor7e063902009-05-11 23:53:27 +0000366}
367
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000368TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList &Other)
369 : FlatArguments(Other.FlatArguments.getPointer(), 1),
370 NumFlatArguments(Other.flat_size()),
371 StructuredArguments(Other.StructuredArguments.getPointer(), 1),
372 NumStructuredArguments(Other.NumStructuredArguments) { }
373
Douglas Gregor7e063902009-05-11 23:53:27 +0000374TemplateArgumentList::~TemplateArgumentList() {
375 // FIXME: Deallocate template arguments
376}
377
378//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000379// ClassTemplateSpecializationDecl Implementation
380//===----------------------------------------------------------------------===//
381ClassTemplateSpecializationDecl::
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000382ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK,
Douglas Gregor7e063902009-05-11 23:53:27 +0000383 DeclContext *DC, SourceLocation L,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000384 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000385 TemplateArgumentListBuilder &Builder,
386 ClassTemplateSpecializationDecl *PrevDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000387 : CXXRecordDecl(DK,
388 SpecializedTemplate->getTemplatedDecl()->getTagKind(),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000389 DC, L,
390 // FIXME: Should we use DeclarationName for the name of
391 // class template specializations?
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000392 SpecializedTemplate->getIdentifier(),
393 PrevDecl),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000394 SpecializedTemplate(SpecializedTemplate),
Anders Carlssonfb250522009-06-23 01:26:57 +0000395 TemplateArgs(Context, Builder, /*TakeArgs=*/true),
Douglas Gregor7e063902009-05-11 23:53:27 +0000396 SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000397}
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000399ClassTemplateSpecializationDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000400ClassTemplateSpecializationDecl::Create(ASTContext &Context,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000401 DeclContext *DC, SourceLocation L,
402 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000403 TemplateArgumentListBuilder &Builder,
Douglas Gregorcc636682009-02-17 23:15:12 +0000404 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000405 ClassTemplateSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000406 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000407 ClassTemplateSpecialization,
Mike Stump1eb44332009-09-09 15:08:12 +0000408 DC, L,
Douglas Gregor7e063902009-05-11 23:53:27 +0000409 SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000410 Builder,
411 PrevDecl);
Douglas Gregorcc636682009-02-17 23:15:12 +0000412 Context.getTypeDeclType(Result, PrevDecl);
413 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000414}
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000415
Douglas Gregor37d93e92009-08-02 23:24:31 +0000416void ClassTemplateSpecializationDecl::Destroy(ASTContext &C) {
Mike Stump1eb44332009-09-09 15:08:12 +0000417 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000418 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
419 C.Deallocate(PartialSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Douglas Gregor37d93e92009-08-02 23:24:31 +0000421 CXXRecordDecl::Destroy(C);
422}
423
John McCall136a6982009-09-11 06:45:03 +0000424void
425ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
426 const PrintingPolicy &Policy,
427 bool Qualified) const {
428 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
429
430 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
431 S += TemplateSpecializationType::PrintTemplateArgumentList(
432 TemplateArgs.getFlatArgumentList(),
433 TemplateArgs.flat_size(),
434 Policy);
435}
436
Douglas Gregor37d93e92009-08-02 23:24:31 +0000437ClassTemplateDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000438ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
439 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000440 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
441 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
442 return SpecializedTemplate.get<ClassTemplateDecl*>();
443}
444
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000445//===----------------------------------------------------------------------===//
446// ClassTemplatePartialSpecializationDecl Implementation
447//===----------------------------------------------------------------------===//
448ClassTemplatePartialSpecializationDecl *
449ClassTemplatePartialSpecializationDecl::
450Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
451 TemplateParameterList *Params,
452 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000453 TemplateArgumentListBuilder &Builder,
John McCalld5532b62009-11-23 01:53:49 +0000454 const TemplateArgumentListInfo &ArgInfos,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000455 ClassTemplatePartialSpecializationDecl *PrevDecl) {
John McCalld5532b62009-11-23 01:53:49 +0000456 unsigned N = ArgInfos.size();
John McCall833ca992009-10-29 08:12:44 +0000457 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
458 for (unsigned I = 0; I != N; ++I)
459 ClonedArgs[I] = ArgInfos[I];
460
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000461 ClassTemplatePartialSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000462 = new (Context)ClassTemplatePartialSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000463 DC, L, Params,
464 SpecializedTemplate,
John McCall833ca992009-10-29 08:12:44 +0000465 Builder,
466 ClonedArgs, N,
467 PrevDecl);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000468 Result->setSpecializationKind(TSK_ExplicitSpecialization);
469 Context.getTypeDeclType(Result, PrevDecl);
470 return Result;
471}
John McCalldd4a3b02009-09-16 22:47:08 +0000472
473//===----------------------------------------------------------------------===//
474// FriendTemplateDecl Implementation
475//===----------------------------------------------------------------------===//
476
477FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
478 DeclContext *DC,
479 SourceLocation L,
480 unsigned NParams,
481 TemplateParameterList **Params,
482 FriendUnion Friend,
483 SourceLocation FLoc) {
484 FriendTemplateDecl *Result
485 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
486 return Result;
487}