blob: 98a724aeee30d2bad062a101ccfec73b9b902c7a [file] [log] [blame]
Sebastian Redle2530ec2009-10-23 22:13:42 +00001//===--- DeclTemplate.cpp - Template Declaration AST Node Implementation --===//
Douglas Gregorded2d7b2009-02-04 19:02:06 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes for templates.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclCXX.h"
15#include "clang/AST/DeclTemplate.h"
Douglas Gregor8bf42052009-02-09 18:46:07 +000016#include "clang/AST/Expr.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000017#include "clang/AST/ASTContext.h"
John McCall0ad16662009-10-29 08:12:44 +000018#include "clang/AST/TypeLoc.h"
Douglas Gregorded2d7b2009-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 Gregorcd72ba92009-02-06 22:42:48 +000027TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
28 SourceLocation LAngleLoc,
Douglas Gregorbe999392009-09-15 16:23:51 +000029 NamedDecl **Params, unsigned NumParams,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000030 SourceLocation RAngleLoc)
31 : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
32 NumParams(NumParams) {
Douglas Gregorded2d7b2009-02-04 19:02:06 +000033 for (unsigned Idx = 0; Idx < NumParams; ++Idx)
34 begin()[Idx] = Params[Idx];
35}
36
37TemplateParameterList *
Douglas Gregorcd72ba92009-02-06 22:42:48 +000038TemplateParameterList::Create(ASTContext &C, SourceLocation TemplateLoc,
Douglas Gregorbe999392009-09-15 16:23:51 +000039 SourceLocation LAngleLoc, NamedDecl **Params,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000040 unsigned NumParams, SourceLocation RAngleLoc) {
Douglas Gregorbe999392009-09-15 16:23:51 +000041 unsigned Size = sizeof(TemplateParameterList)
42 + sizeof(NamedDecl *) * NumParams;
Douglas Gregorded2d7b2009-02-04 19:02:06 +000043 unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment;
44 void *Mem = C.Allocate(Size, Align);
Mike Stump11289f42009-09-09 15:08:12 +000045 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000046 NumParams, RAngleLoc);
Douglas Gregorded2d7b2009-02-04 19:02:06 +000047}
48
Douglas Gregorf8f86832009-02-11 18:16:40 +000049unsigned TemplateParameterList::getMinRequiredArguments() const {
50 unsigned NumRequiredArgs = size();
Mike Stump11289f42009-09-09 15:08:12 +000051 iterator Param = const_cast<TemplateParameterList *>(this)->end(),
Douglas Gregorf8f86832009-02-11 18:16:40 +000052 ParamBegin = const_cast<TemplateParameterList *>(this)->begin();
53 while (Param != ParamBegin) {
54 --Param;
Mike Stump11289f42009-09-09 15:08:12 +000055
Anders Carlsson15201f12009-06-13 02:08:00 +000056 if (!(*Param)->isTemplateParameterPack() &&
Mike Stump11289f42009-09-09 15:08:12 +000057 !(isa<TemplateTypeParmDecl>(*Param) &&
Douglas Gregorf8f86832009-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 Stump11289f42009-09-09 15:08:12 +000064
Douglas Gregorf8f86832009-02-11 18:16:40 +000065 --NumRequiredArgs;
66 }
67
68 return NumRequiredArgs;
69}
70
Douglas Gregor21610382009-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 Gregorded2d7b2009-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 Gregor8f5d4422009-06-29 20:59:39 +0000101 TemplateParameterList *Params,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000102 NamedDecl *Decl) {
103 return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl);
104}
105
Douglas Gregor8f5d4422009-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 Stump11289f42009-09-09 15:08:12 +0000114
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000115 Decl::Destroy(C);
116}
117
Argyrios Kyrtzidis5614aef2009-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 Gregor8f5d4422009-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 Stump11289f42009-09-09 15:08:12 +0000130
Douglas Gregor8f5d4422009-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 Gregorded2d7b2009-02-04 19:02:06 +0000138//===----------------------------------------------------------------------===//
139// ClassTemplateDecl Implementation
140//===----------------------------------------------------------------------===//
141
Argyrios Kyrtzidis5614aef2009-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 Gregorded2d7b2009-02-04 19:02:06 +0000149ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
150 DeclContext *DC,
151 SourceLocation L,
152 DeclarationName Name,
153 TemplateParameterList *Params,
Douglas Gregor90a1a652009-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 Stump11289f42009-09-09 15:08:12 +0000162 return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl, PrevDecl,
Douglas Gregor90a1a652009-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 Gregorded2d7b2009-02-04 19:02:06 +0000179}
180
Douglas Gregor407e9612010-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 Gregor15301382009-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 McCall2408e322010-04-27 00:57:59 +0000203 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregor15301382009-07-30 17:40:51 +0000204 return &*P;
205 }
Mike Stump11289f42009-09-09 15:08:12 +0000206
Douglas Gregor15301382009-07-30 17:40:51 +0000207 return 0;
208}
209
John McCalle78aac42010-03-10 03:28:59 +0000210QualType
211ClassTemplateDecl::getInjectedClassNameSpecialization(ASTContext &Context) {
Douglas Gregore362cea2009-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 Gregore362cea2009-05-10 22:57:19 +0000221 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregore362cea2009-05-10 22:57:19 +0000222 TemplateArgs.reserve(Params->size());
Mike Stump11289f42009-09-09 15:08:12 +0000223 for (TemplateParameterList::iterator Param = Params->begin(),
224 ParamEnd = Params->end();
Douglas Gregore362cea2009-05-10 22:57:19 +0000225 Param != ParamEnd; ++Param) {
226 if (isa<TemplateTypeParmDecl>(*Param)) {
227 QualType ParamType = Context.getTypeDeclType(cast<TypeDecl>(*Param));
John McCall0ad16662009-10-29 08:12:44 +0000228 TemplateArgs.push_back(TemplateArgument(ParamType));
Mike Stump11289f42009-09-09 15:08:12 +0000229 } else if (NonTypeTemplateParmDecl *NTTP =
Douglas Gregore362cea2009-05-10 22:57:19 +0000230 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Chandler Carruth234c1292010-01-31 07:24:03 +0000231 Expr *E = new (Context) DeclRefExpr(NTTP,
232 NTTP->getType().getNonReferenceType(),
Douglas Gregored6c7442009-11-23 11:41:28 +0000233 NTTP->getLocation());
Douglas Gregore362cea2009-05-10 22:57:19 +0000234 TemplateArgs.push_back(TemplateArgument(E));
Mike Stump11289f42009-09-09 15:08:12 +0000235 } else {
Douglas Gregore362cea2009-05-10 22:57:19 +0000236 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000237 TemplateArgs.push_back(TemplateArgument(TemplateName(TTP)));
Douglas Gregore362cea2009-05-10 22:57:19 +0000238 }
239 }
240
Douglas Gregore362cea2009-05-10 22:57:19 +0000241 CommonPtr->InjectedClassNameType
Douglas Gregora8e02e72009-07-28 23:00:59 +0000242 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregore362cea2009-05-10 22:57:19 +0000243 &TemplateArgs[0],
Douglas Gregora8e02e72009-07-28 23:00:59 +0000244 TemplateArgs.size());
Douglas Gregore362cea2009-05-10 22:57:19 +0000245 return CommonPtr->InjectedClassNameType;
246}
247
Douglas Gregorded2d7b2009-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 Carlssonfb1d7762009-06-12 22:23:22 +0000255 IdentifierInfo *Id, bool Typename,
256 bool ParameterPack) {
Anders Carlsson90036dc2009-06-16 00:30:48 +0000257 QualType Type = C.getTemplateTypeParmType(D, P, ParameterPack, Id);
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000258 return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type, ParameterPack);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000259}
260
John McCall0ad16662009-10-29 08:12:44 +0000261SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000262 return DefaultArgument->getTypeLoc().getSourceRange().getBegin();
John McCall0ad16662009-10-29 08:12:44 +0000263}
264
Douglas Gregor21610382009-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 Gregorded2d7b2009-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 McCallbcd03502009-12-07 02:54:59 +0000281 TypeSourceInfo *TInfo) {
282 return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, TInfo);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000283}
284
Douglas Gregordba32632009-02-10 19:49:53 +0000285SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
286 return DefaultArgument? DefaultArgument->getSourceRange().getBegin()
Mike Stump11289f42009-09-09 15:08:12 +0000287 : SourceLocation();
Douglas Gregordba32632009-02-10 19:49:53 +0000288}
289
Douglas Gregorded2d7b2009-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 Gregor264ec4f2009-02-17 01:05:43 +0000302//===----------------------------------------------------------------------===//
Anders Carlsson184cb412009-06-05 05:31:27 +0000303// TemplateArgumentListBuilder Implementation
304//===----------------------------------------------------------------------===//
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000305
Chris Lattner047f5aa2010-05-20 00:25:36 +0000306void TemplateArgumentListBuilder::Append(const TemplateArgument &Arg) {
307 assert((Arg.getKind() != TemplateArgument::Type ||
308 Arg.getAsType().isCanonical()) && "Type must be canonical!");
309 assert(FlatArgs.size() < MaxFlatArgs && "Argument list builder is full!");
Mike Stump11289f42009-09-09 15:08:12 +0000310 assert(!StructuredArgs &&
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000311 "Can't append arguments when an argument pack has been added!");
Mike Stump11289f42009-09-09 15:08:12 +0000312
Chris Lattner047f5aa2010-05-20 00:25:36 +0000313 FlatArgs.push_back(Arg);
Anders Carlsson184cb412009-06-05 05:31:27 +0000314}
315
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000316void TemplateArgumentListBuilder::BeginPack() {
317 assert(!AddingToPack && "Already adding to pack!");
318 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump11289f42009-09-09 15:08:12 +0000319
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000320 AddingToPack = true;
Chris Lattner047f5aa2010-05-20 00:25:36 +0000321 PackBeginIndex = FlatArgs.size();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000322}
323
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000324void TemplateArgumentListBuilder::EndPack() {
325 assert(AddingToPack && "Not adding to pack!");
326 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump11289f42009-09-09 15:08:12 +0000327
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000328 AddingToPack = false;
Anders Carlsson475501b2009-06-15 17:56:45 +0000329
Chris Lattner8ca2fd2e2010-05-20 00:26:28 +0000330 // FIXME: This is a memory leak!
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000331 StructuredArgs = new TemplateArgument[MaxStructuredArgs];
Mike Stump11289f42009-09-09 15:08:12 +0000332
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000333 // First copy the flat entries over to the list (if any)
334 for (unsigned I = 0; I != PackBeginIndex; ++I) {
335 NumStructuredArgs++;
336 StructuredArgs[I] = FlatArgs[I];
337 }
Mike Stump11289f42009-09-09 15:08:12 +0000338
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000339 // Next, set the pack.
340 TemplateArgument *PackArgs = 0;
341 unsigned NumPackArgs = NumFlatArgs - PackBeginIndex;
Chris Lattner047f5aa2010-05-20 00:25:36 +0000342 // FIXME: NumPackArgs shouldn't be negative here???
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000343 if (NumPackArgs)
Chris Lattner047f5aa2010-05-20 00:25:36 +0000344 PackArgs = FlatArgs.data()+PackBeginIndex;
Mike Stump11289f42009-09-09 15:08:12 +0000345
346 StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs,
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000347 /*CopyArgs=*/false);
348}
349
Anders Carlsson184cb412009-06-05 05:31:27 +0000350//===----------------------------------------------------------------------===//
Douglas Gregord002c7b2009-05-11 23:53:27 +0000351// TemplateArgumentList Implementation
352//===----------------------------------------------------------------------===//
353TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
Anders Carlssonc8e71132009-06-05 04:47:51 +0000354 TemplateArgumentListBuilder &Builder,
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000355 bool TakeArgs)
Mike Stump11289f42009-09-09 15:08:12 +0000356 : FlatArguments(Builder.getFlatArguments(), TakeArgs),
357 NumFlatArguments(Builder.flatSize()),
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000358 StructuredArguments(Builder.getStructuredArguments(), TakeArgs),
359 NumStructuredArguments(Builder.structuredSize()) {
Mike Stump11289f42009-09-09 15:08:12 +0000360
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000361 if (!TakeArgs)
362 return;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Chris Lattnerc9b03bc2010-05-20 00:11:47 +0000364 // If this does take ownership of the arguments, then we have to new them
365 // and copy over.
366 TemplateArgument *NewArgs = new TemplateArgument[Builder.flatSize()];
367 std::copy(Builder.getFlatArguments(),
368 Builder.getFlatArguments()+Builder.flatSize(), NewArgs);
369 FlatArguments.setPointer(NewArgs);
370
371 // Just reuse the structured and flat arguments array if possible.
372 if (Builder.getStructuredArguments() == Builder.getFlatArguments()) {
373 StructuredArguments.setPointer(NewArgs);
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000374 StructuredArguments.setInt(0);
Chris Lattnerc9b03bc2010-05-20 00:11:47 +0000375 } else {
376 TemplateArgument *NewSArgs = new TemplateArgument[Builder.flatSize()];
377 std::copy(Builder.getFlatArguments(),
378 Builder.getFlatArguments()+Builder.flatSize(), NewSArgs);
379 StructuredArguments.setPointer(NewSArgs);
380 }
Douglas Gregord002c7b2009-05-11 23:53:27 +0000381}
382
Chris Lattnerce7a22d2010-05-20 00:19:09 +0000383/// Produces a shallow copy of the given template argument list. This
384/// assumes that the input argument list outlives it. This takes the list as
385/// a pointer to avoid looking like a copy constructor, since this really
386/// really isn't safe to use that way.
387TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList *Other)
388 : FlatArguments(Other->FlatArguments.getPointer(), false),
389 NumFlatArguments(Other->flat_size()),
390 StructuredArguments(Other->StructuredArguments.getPointer(), false),
391 NumStructuredArguments(Other->NumStructuredArguments) { }
Douglas Gregor3a923c2d2009-09-24 23:14:47 +0000392
Douglas Gregord002c7b2009-05-11 23:53:27 +0000393TemplateArgumentList::~TemplateArgumentList() {
Chris Lattnerc9b03bc2010-05-20 00:11:47 +0000394 if (FlatArguments.getInt())
395 delete [] FlatArguments.getPointer();
396 if (StructuredArguments.getInt())
397 delete [] StructuredArguments.getPointer();
Douglas Gregord002c7b2009-05-11 23:53:27 +0000398}
399
400//===----------------------------------------------------------------------===//
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000401// ClassTemplateSpecializationDecl Implementation
402//===----------------------------------------------------------------------===//
403ClassTemplateSpecializationDecl::
Douglas Gregore9029562010-05-06 00:28:52 +0000404ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Douglas Gregord002c7b2009-05-11 23:53:27 +0000405 DeclContext *DC, SourceLocation L,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000406 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000407 TemplateArgumentListBuilder &Builder,
408 ClassTemplateSpecializationDecl *PrevDecl)
Douglas Gregore9029562010-05-06 00:28:52 +0000409 : CXXRecordDecl(DK, TK, DC, L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000410 SpecializedTemplate->getIdentifier(),
411 PrevDecl),
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000412 SpecializedTemplate(SpecializedTemplate),
John McCalle78aac42010-03-10 03:28:59 +0000413 TypeAsWritten(0),
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000414 TemplateArgs(Context, Builder, /*TakeArgs=*/true),
Douglas Gregord002c7b2009-05-11 23:53:27 +0000415 SpecializationKind(TSK_Undeclared) {
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000416}
Mike Stump11289f42009-09-09 15:08:12 +0000417
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000418ClassTemplateSpecializationDecl *
Douglas Gregore9029562010-05-06 00:28:52 +0000419ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000420 DeclContext *DC, SourceLocation L,
421 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson1b28c3e2009-06-05 04:06:48 +0000422 TemplateArgumentListBuilder &Builder,
Douglas Gregor67a65642009-02-17 23:15:12 +0000423 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000424 ClassTemplateSpecializationDecl *Result
Mike Stump11289f42009-09-09 15:08:12 +0000425 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregor2373c592009-05-31 09:31:02 +0000426 ClassTemplateSpecialization,
Douglas Gregore9029562010-05-06 00:28:52 +0000427 TK, DC, L,
Douglas Gregord002c7b2009-05-11 23:53:27 +0000428 SpecializedTemplate,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000429 Builder,
430 PrevDecl);
Douglas Gregor67a65642009-02-17 23:15:12 +0000431 Context.getTypeDeclType(Result, PrevDecl);
432 return Result;
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000433}
Douglas Gregor2373c592009-05-31 09:31:02 +0000434
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000435void ClassTemplateSpecializationDecl::Destroy(ASTContext &C) {
Mike Stump11289f42009-09-09 15:08:12 +0000436 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000437 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
438 C.Deallocate(PartialSpec);
Mike Stump11289f42009-09-09 15:08:12 +0000439
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000440 CXXRecordDecl::Destroy(C);
441}
442
John McCalle1f2ec22009-09-11 06:45:03 +0000443void
444ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
445 const PrintingPolicy &Policy,
446 bool Qualified) const {
447 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
448
449 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
450 S += TemplateSpecializationType::PrintTemplateArgumentList(
451 TemplateArgs.getFlatArgumentList(),
452 TemplateArgs.flat_size(),
453 Policy);
454}
455
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000456ClassTemplateDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000457ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
458 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000459 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
460 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
461 return SpecializedTemplate.get<ClassTemplateDecl*>();
462}
463
Douglas Gregor2373c592009-05-31 09:31:02 +0000464//===----------------------------------------------------------------------===//
465// ClassTemplatePartialSpecializationDecl Implementation
466//===----------------------------------------------------------------------===//
467ClassTemplatePartialSpecializationDecl *
468ClassTemplatePartialSpecializationDecl::
Douglas Gregore9029562010-05-06 00:28:52 +0000469Create(ASTContext &Context, TagKind TK,DeclContext *DC, SourceLocation L,
Douglas Gregor2373c592009-05-31 09:31:02 +0000470 TemplateParameterList *Params,
471 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson1b28c3e2009-06-05 04:06:48 +0000472 TemplateArgumentListBuilder &Builder,
John McCall6b51f282009-11-23 01:53:49 +0000473 const TemplateArgumentListInfo &ArgInfos,
John McCalle78aac42010-03-10 03:28:59 +0000474 QualType CanonInjectedType,
Douglas Gregor407e9612010-04-30 05:56:50 +0000475 ClassTemplatePartialSpecializationDecl *PrevDecl,
476 unsigned SequenceNumber) {
John McCall6b51f282009-11-23 01:53:49 +0000477 unsigned N = ArgInfos.size();
John McCall0ad16662009-10-29 08:12:44 +0000478 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
479 for (unsigned I = 0; I != N; ++I)
480 ClonedArgs[I] = ArgInfos[I];
481
Douglas Gregor2373c592009-05-31 09:31:02 +0000482 ClassTemplatePartialSpecializationDecl *Result
Douglas Gregore9029562010-05-06 00:28:52 +0000483 = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK,
Douglas Gregor2373c592009-05-31 09:31:02 +0000484 DC, L, Params,
485 SpecializedTemplate,
John McCall0ad16662009-10-29 08:12:44 +0000486 Builder,
487 ClonedArgs, N,
Douglas Gregor407e9612010-04-30 05:56:50 +0000488 PrevDecl,
489 SequenceNumber);
Douglas Gregor2373c592009-05-31 09:31:02 +0000490 Result->setSpecializationKind(TSK_ExplicitSpecialization);
John McCalle78aac42010-03-10 03:28:59 +0000491
492 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregor2373c592009-05-31 09:31:02 +0000493 return Result;
494}
John McCall11083da2009-09-16 22:47:08 +0000495
496//===----------------------------------------------------------------------===//
497// FriendTemplateDecl Implementation
498//===----------------------------------------------------------------------===//
499
500FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
501 DeclContext *DC,
502 SourceLocation L,
503 unsigned NParams,
504 TemplateParameterList **Params,
505 FriendUnion Friend,
506 SourceLocation FLoc) {
507 FriendTemplateDecl *Result
508 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
509 return Result;
510}