blob: 26e291c94f6422fadb8f05b9b7391ad9371c89ec [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
Douglas Gregor00545312010-05-23 18:26:36 +000097void FunctionTemplateDecl::DeallocateCommon(void *Ptr) {
98 static_cast<Common *>(Ptr)->~Common();
99}
100
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000101FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
102 DeclContext *DC,
103 SourceLocation L,
104 DeclarationName Name,
Douglas Gregor127102b2009-06-29 20:59:39 +0000105 TemplateParameterList *Params,
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000106 NamedDecl *Decl) {
107 return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl);
108}
109
Douglas Gregor127102b2009-06-29 20:59:39 +0000110void FunctionTemplateDecl::Destroy(ASTContext &C) {
111 if (Common *CommonPtr = CommonOrPrev.dyn_cast<Common*>()) {
112 for (llvm::FoldingSet<FunctionTemplateSpecializationInfo>::iterator
113 Spec = CommonPtr->Specializations.begin(),
114 SpecEnd = CommonPtr->Specializations.end();
115 Spec != SpecEnd; ++Spec)
116 C.Deallocate(&*Spec);
117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Douglas Gregor127102b2009-06-29 20:59:39 +0000119 Decl::Destroy(C);
120}
121
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000122FunctionTemplateDecl *FunctionTemplateDecl::getCanonicalDecl() {
123 FunctionTemplateDecl *FunTmpl = this;
124 while (FunTmpl->getPreviousDeclaration())
125 FunTmpl = FunTmpl->getPreviousDeclaration();
126 return FunTmpl;
127}
128
Douglas Gregor127102b2009-06-29 20:59:39 +0000129FunctionTemplateDecl::Common *FunctionTemplateDecl::getCommonPtr() {
130 // Find the first declaration of this function template.
131 FunctionTemplateDecl *First = this;
132 while (First->getPreviousDeclaration())
133 First = First->getPreviousDeclaration();
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Douglas Gregor127102b2009-06-29 20:59:39 +0000135 if (First->CommonOrPrev.isNull()) {
Douglas Gregor00545312010-05-23 18:26:36 +0000136 Common *CommonPtr = new (getASTContext()) Common;
137 getASTContext().AddDeallocation(DeallocateCommon, CommonPtr);
138 First->CommonOrPrev = CommonPtr;
Douglas Gregor127102b2009-06-29 20:59:39 +0000139 }
140 return First->CommonOrPrev.get<Common*>();
141}
142
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000143//===----------------------------------------------------------------------===//
144// ClassTemplateDecl Implementation
145//===----------------------------------------------------------------------===//
146
Douglas Gregor00545312010-05-23 18:26:36 +0000147void ClassTemplateDecl::DeallocateCommon(void *Ptr) {
148 static_cast<Common *>(Ptr)->~Common();
149}
150
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +0000151ClassTemplateDecl *ClassTemplateDecl::getCanonicalDecl() {
152 ClassTemplateDecl *Template = this;
153 while (Template->getPreviousDeclaration())
154 Template = Template->getPreviousDeclaration();
155 return Template;
156}
157
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000158ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
159 DeclContext *DC,
160 SourceLocation L,
161 DeclarationName Name,
162 TemplateParameterList *Params,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000163 NamedDecl *Decl,
164 ClassTemplateDecl *PrevDecl) {
165 Common *CommonPtr;
166 if (PrevDecl)
167 CommonPtr = PrevDecl->CommonPtr;
Douglas Gregor00545312010-05-23 18:26:36 +0000168 else {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000169 CommonPtr = new (C) Common;
Douglas Gregor00545312010-05-23 18:26:36 +0000170 C.AddDeallocation(DeallocateCommon, CommonPtr);
171 }
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000172
Mike Stump1eb44332009-09-09 15:08:12 +0000173 return new (C) ClassTemplateDecl(DC, L, Name, Params, Decl, PrevDecl,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000174 CommonPtr);
175}
176
177ClassTemplateDecl::~ClassTemplateDecl() {
178 assert(CommonPtr == 0 && "ClassTemplateDecl must be explicitly destroyed");
179}
180
181void ClassTemplateDecl::Destroy(ASTContext& C) {
182 if (!PreviousDeclaration) {
183 CommonPtr->~Common();
184 C.Deallocate((void*)CommonPtr);
185 }
186 CommonPtr = 0;
187
188 this->~ClassTemplateDecl();
189 C.Deallocate((void*)this);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000190}
191
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000192void ClassTemplateDecl::getPartialSpecializations(
193 llvm::SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
194 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs
195 = CommonPtr->PartialSpecializations;
196 PS.clear();
197 PS.resize(PartialSpecs.size());
198 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
199 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
200 P != PEnd; ++P) {
201 assert(!PS[P->getSequenceNumber()]);
202 PS[P->getSequenceNumber()] = &*P;
203 }
204}
205
Douglas Gregorb88e8882009-07-30 17:40:51 +0000206ClassTemplatePartialSpecializationDecl *
207ClassTemplateDecl::findPartialSpecialization(QualType T) {
208 ASTContext &Context = getASTContext();
209 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
210 partial_spec_iterator;
211 for (partial_spec_iterator P = getPartialSpecializations().begin(),
212 PEnd = getPartialSpecializations().end();
213 P != PEnd; ++P) {
John McCall31f17ec2010-04-27 00:57:59 +0000214 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregorb88e8882009-07-30 17:40:51 +0000215 return &*P;
216 }
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Douglas Gregorb88e8882009-07-30 17:40:51 +0000218 return 0;
219}
220
John McCall3cb0ebd2010-03-10 03:28:59 +0000221QualType
222ClassTemplateDecl::getInjectedClassNameSpecialization(ASTContext &Context) {
Douglas Gregor7da97d02009-05-10 22:57:19 +0000223 if (!CommonPtr->InjectedClassNameType.isNull())
224 return CommonPtr->InjectedClassNameType;
225
226 // FIXME: n2800 14.6.1p1 should say how the template arguments
227 // corresponding to template parameter packs should be pack
228 // expansions. We already say that in 14.6.2.1p2, so it would be
229 // better to fix that redundancy.
230
231 TemplateParameterList *Params = getTemplateParameters();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000232 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregor7da97d02009-05-10 22:57:19 +0000233 TemplateArgs.reserve(Params->size());
Mike Stump1eb44332009-09-09 15:08:12 +0000234 for (TemplateParameterList::iterator Param = Params->begin(),
235 ParamEnd = Params->end();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000236 Param != ParamEnd; ++Param) {
237 if (isa<TemplateTypeParmDecl>(*Param)) {
238 QualType ParamType = Context.getTypeDeclType(cast<TypeDecl>(*Param));
John McCall833ca992009-10-29 08:12:44 +0000239 TemplateArgs.push_back(TemplateArgument(ParamType));
Mike Stump1eb44332009-09-09 15:08:12 +0000240 } else if (NonTypeTemplateParmDecl *NTTP =
Douglas Gregor7da97d02009-05-10 22:57:19 +0000241 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Chandler Carruthb7de1812010-01-31 07:24:03 +0000242 Expr *E = new (Context) DeclRefExpr(NTTP,
243 NTTP->getType().getNonReferenceType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000244 NTTP->getLocation());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000245 TemplateArgs.push_back(TemplateArgument(E));
Mike Stump1eb44332009-09-09 15:08:12 +0000246 } else {
Douglas Gregor7da97d02009-05-10 22:57:19 +0000247 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregor788cd062009-11-11 01:00:40 +0000248 TemplateArgs.push_back(TemplateArgument(TemplateName(TTP)));
Douglas Gregor7da97d02009-05-10 22:57:19 +0000249 }
250 }
251
Douglas Gregor7da97d02009-05-10 22:57:19 +0000252 CommonPtr->InjectedClassNameType
Douglas Gregor1275ae02009-07-28 23:00:59 +0000253 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregor7da97d02009-05-10 22:57:19 +0000254 &TemplateArgs[0],
Douglas Gregor1275ae02009-07-28 23:00:59 +0000255 TemplateArgs.size());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000256 return CommonPtr->InjectedClassNameType;
257}
258
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000259//===----------------------------------------------------------------------===//
260// TemplateTypeParm Allocation/Deallocation Method Implementations
261//===----------------------------------------------------------------------===//
262
263TemplateTypeParmDecl *
264TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
265 SourceLocation L, unsigned D, unsigned P,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000266 IdentifierInfo *Id, bool Typename,
267 bool ParameterPack) {
Anders Carlsson76e4ce42009-06-16 00:30:48 +0000268 QualType Type = C.getTemplateTypeParmType(D, P, ParameterPack, Id);
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000269 return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type, ParameterPack);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000270}
271
John McCall833ca992009-10-29 08:12:44 +0000272SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000273 return DefaultArgument->getTypeLoc().getSourceRange().getBegin();
John McCall833ca992009-10-29 08:12:44 +0000274}
275
Douglas Gregored9c0f92009-10-29 00:04:11 +0000276unsigned TemplateTypeParmDecl::getDepth() const {
277 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
278}
279
280unsigned TemplateTypeParmDecl::getIndex() const {
281 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
282}
283
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000284//===----------------------------------------------------------------------===//
285// NonTypeTemplateParmDecl Method Implementations
286//===----------------------------------------------------------------------===//
287
288NonTypeTemplateParmDecl *
289NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
290 SourceLocation L, unsigned D, unsigned P,
291 IdentifierInfo *Id, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000292 TypeSourceInfo *TInfo) {
293 return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, TInfo);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000294}
295
Douglas Gregord684b002009-02-10 19:49:53 +0000296SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
297 return DefaultArgument? DefaultArgument->getSourceRange().getBegin()
Mike Stump1eb44332009-09-09 15:08:12 +0000298 : SourceLocation();
Douglas Gregord684b002009-02-10 19:49:53 +0000299}
300
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000301//===----------------------------------------------------------------------===//
302// TemplateTemplateParmDecl Method Implementations
303//===----------------------------------------------------------------------===//
304
305TemplateTemplateParmDecl *
306TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
307 SourceLocation L, unsigned D, unsigned P,
308 IdentifierInfo *Id,
309 TemplateParameterList *Params) {
310 return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params);
311}
312
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000313//===----------------------------------------------------------------------===//
Anders Carlsson9ba41642009-06-05 05:31:27 +0000314// TemplateArgumentListBuilder Implementation
315//===----------------------------------------------------------------------===//
Anders Carlssonfb250522009-06-23 01:26:57 +0000316
Chris Lattner98d279b2010-05-20 00:25:36 +0000317void TemplateArgumentListBuilder::Append(const TemplateArgument &Arg) {
318 assert((Arg.getKind() != TemplateArgument::Type ||
319 Arg.getAsType().isCanonical()) && "Type must be canonical!");
320 assert(FlatArgs.size() < MaxFlatArgs && "Argument list builder is full!");
Mike Stump1eb44332009-09-09 15:08:12 +0000321 assert(!StructuredArgs &&
Anders Carlssonfb250522009-06-23 01:26:57 +0000322 "Can't append arguments when an argument pack has been added!");
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner98d279b2010-05-20 00:25:36 +0000324 FlatArgs.push_back(Arg);
Anders Carlsson9ba41642009-06-05 05:31:27 +0000325}
326
Anders Carlssonfb250522009-06-23 01:26:57 +0000327void TemplateArgumentListBuilder::BeginPack() {
328 assert(!AddingToPack && "Already adding to pack!");
329 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Anders Carlssonfb250522009-06-23 01:26:57 +0000331 AddingToPack = true;
Chris Lattner98d279b2010-05-20 00:25:36 +0000332 PackBeginIndex = FlatArgs.size();
Anders Carlsson67e33202009-06-13 00:08:58 +0000333}
334
Anders Carlssonfb250522009-06-23 01:26:57 +0000335void TemplateArgumentListBuilder::EndPack() {
336 assert(AddingToPack && "Not adding to pack!");
337 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Anders Carlssonfb250522009-06-23 01:26:57 +0000339 AddingToPack = false;
Anders Carlsson3b36b662009-06-15 17:56:45 +0000340
Chris Lattner304d0fa2010-05-20 00:26:28 +0000341 // FIXME: This is a memory leak!
Anders Carlssonfb250522009-06-23 01:26:57 +0000342 StructuredArgs = new TemplateArgument[MaxStructuredArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Anders Carlssonfb250522009-06-23 01:26:57 +0000344 // First copy the flat entries over to the list (if any)
345 for (unsigned I = 0; I != PackBeginIndex; ++I) {
346 NumStructuredArgs++;
347 StructuredArgs[I] = FlatArgs[I];
348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Anders Carlssonfb250522009-06-23 01:26:57 +0000350 // Next, set the pack.
351 TemplateArgument *PackArgs = 0;
352 unsigned NumPackArgs = NumFlatArgs - PackBeginIndex;
Chris Lattner98d279b2010-05-20 00:25:36 +0000353 // FIXME: NumPackArgs shouldn't be negative here???
Anders Carlssonfb250522009-06-23 01:26:57 +0000354 if (NumPackArgs)
Chris Lattner98d279b2010-05-20 00:25:36 +0000355 PackArgs = FlatArgs.data()+PackBeginIndex;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
357 StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs,
Anders Carlssonfb250522009-06-23 01:26:57 +0000358 /*CopyArgs=*/false);
359}
360
Anders Carlsson9ba41642009-06-05 05:31:27 +0000361//===----------------------------------------------------------------------===//
Douglas Gregor7e063902009-05-11 23:53:27 +0000362// TemplateArgumentList Implementation
363//===----------------------------------------------------------------------===//
364TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
Anders Carlssone9c904b2009-06-05 04:47:51 +0000365 TemplateArgumentListBuilder &Builder,
Anders Carlssonfb250522009-06-23 01:26:57 +0000366 bool TakeArgs)
Mike Stump1eb44332009-09-09 15:08:12 +0000367 : FlatArguments(Builder.getFlatArguments(), TakeArgs),
368 NumFlatArguments(Builder.flatSize()),
Anders Carlssonfb250522009-06-23 01:26:57 +0000369 StructuredArguments(Builder.getStructuredArguments(), TakeArgs),
370 NumStructuredArguments(Builder.structuredSize()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Anders Carlssonfb250522009-06-23 01:26:57 +0000372 if (!TakeArgs)
373 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner56ef5502010-05-20 00:11:47 +0000375 // If this does take ownership of the arguments, then we have to new them
376 // and copy over.
Ted Kremenek3458d432010-05-25 20:43:29 +0000377 TemplateArgument *NewArgs =
378 new (Context) TemplateArgument[Builder.flatSize()];
Chris Lattner56ef5502010-05-20 00:11:47 +0000379 std::copy(Builder.getFlatArguments(),
380 Builder.getFlatArguments()+Builder.flatSize(), NewArgs);
381 FlatArguments.setPointer(NewArgs);
382
383 // Just reuse the structured and flat arguments array if possible.
384 if (Builder.getStructuredArguments() == Builder.getFlatArguments()) {
385 StructuredArguments.setPointer(NewArgs);
Anders Carlssonfb250522009-06-23 01:26:57 +0000386 StructuredArguments.setInt(0);
Chris Lattner56ef5502010-05-20 00:11:47 +0000387 } else {
Ted Kremenek3458d432010-05-25 20:43:29 +0000388 TemplateArgument *NewSArgs =
389 new (Context) TemplateArgument[Builder.flatSize()];
Chris Lattner56ef5502010-05-20 00:11:47 +0000390 std::copy(Builder.getFlatArguments(),
391 Builder.getFlatArguments()+Builder.flatSize(), NewSArgs);
392 StructuredArguments.setPointer(NewSArgs);
393 }
Douglas Gregor7e063902009-05-11 23:53:27 +0000394}
395
Chris Lattner88598912010-05-20 00:19:09 +0000396/// Produces a shallow copy of the given template argument list. This
397/// assumes that the input argument list outlives it. This takes the list as
398/// a pointer to avoid looking like a copy constructor, since this really
399/// really isn't safe to use that way.
400TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList *Other)
401 : FlatArguments(Other->FlatArguments.getPointer(), false),
402 NumFlatArguments(Other->flat_size()),
403 StructuredArguments(Other->StructuredArguments.getPointer(), false),
404 NumStructuredArguments(Other->NumStructuredArguments) { }
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000405
Ted Kremenek3458d432010-05-25 20:43:29 +0000406void TemplateArgumentList::Destroy(ASTContext &C) {
Chris Lattner56ef5502010-05-20 00:11:47 +0000407 if (FlatArguments.getInt())
Ted Kremenek3458d432010-05-25 20:43:29 +0000408 C.Deallocate((void*)FlatArguments.getPointer());
Chris Lattner56ef5502010-05-20 00:11:47 +0000409 if (StructuredArguments.getInt())
Ted Kremenek3458d432010-05-25 20:43:29 +0000410 C.Deallocate((void*)StructuredArguments.getPointer());
Douglas Gregor7e063902009-05-11 23:53:27 +0000411}
412
Ted Kremenek3458d432010-05-25 20:43:29 +0000413TemplateArgumentList::~TemplateArgumentList() {}
414
Douglas Gregor7e063902009-05-11 23:53:27 +0000415//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000416// ClassTemplateSpecializationDecl Implementation
417//===----------------------------------------------------------------------===//
418ClassTemplateSpecializationDecl::
Douglas Gregor13c85772010-05-06 00:28:52 +0000419ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Douglas Gregor7e063902009-05-11 23:53:27 +0000420 DeclContext *DC, SourceLocation L,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000421 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000422 TemplateArgumentListBuilder &Builder,
423 ClassTemplateSpecializationDecl *PrevDecl)
Douglas Gregor13c85772010-05-06 00:28:52 +0000424 : CXXRecordDecl(DK, TK, DC, L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000425 SpecializedTemplate->getIdentifier(),
426 PrevDecl),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000427 SpecializedTemplate(SpecializedTemplate),
John McCall3cb0ebd2010-03-10 03:28:59 +0000428 TypeAsWritten(0),
Anders Carlssonfb250522009-06-23 01:26:57 +0000429 TemplateArgs(Context, Builder, /*TakeArgs=*/true),
Douglas Gregor7e063902009-05-11 23:53:27 +0000430 SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000431}
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000433ClassTemplateSpecializationDecl *
Douglas Gregor13c85772010-05-06 00:28:52 +0000434ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000435 DeclContext *DC, SourceLocation L,
436 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000437 TemplateArgumentListBuilder &Builder,
Douglas Gregorcc636682009-02-17 23:15:12 +0000438 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000439 ClassTemplateSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000440 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000441 ClassTemplateSpecialization,
Douglas Gregor13c85772010-05-06 00:28:52 +0000442 TK, DC, L,
Douglas Gregor7e063902009-05-11 23:53:27 +0000443 SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000444 Builder,
445 PrevDecl);
Douglas Gregorcc636682009-02-17 23:15:12 +0000446 Context.getTypeDeclType(Result, PrevDecl);
447 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000448}
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000449
Douglas Gregor37d93e92009-08-02 23:24:31 +0000450void ClassTemplateSpecializationDecl::Destroy(ASTContext &C) {
Mike Stump1eb44332009-09-09 15:08:12 +0000451 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000452 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
453 C.Deallocate(PartialSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Douglas Gregor37d93e92009-08-02 23:24:31 +0000455 CXXRecordDecl::Destroy(C);
456}
457
John McCall136a6982009-09-11 06:45:03 +0000458void
459ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
460 const PrintingPolicy &Policy,
461 bool Qualified) const {
462 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
463
464 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
465 S += TemplateSpecializationType::PrintTemplateArgumentList(
466 TemplateArgs.getFlatArgumentList(),
467 TemplateArgs.flat_size(),
468 Policy);
469}
470
Douglas Gregor37d93e92009-08-02 23:24:31 +0000471ClassTemplateDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000472ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
473 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000474 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
475 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
476 return SpecializedTemplate.get<ClassTemplateDecl*>();
477}
478
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000479//===----------------------------------------------------------------------===//
480// ClassTemplatePartialSpecializationDecl Implementation
481//===----------------------------------------------------------------------===//
482ClassTemplatePartialSpecializationDecl *
483ClassTemplatePartialSpecializationDecl::
Douglas Gregor13c85772010-05-06 00:28:52 +0000484Create(ASTContext &Context, TagKind TK,DeclContext *DC, SourceLocation L,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000485 TemplateParameterList *Params,
486 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000487 TemplateArgumentListBuilder &Builder,
John McCalld5532b62009-11-23 01:53:49 +0000488 const TemplateArgumentListInfo &ArgInfos,
John McCall3cb0ebd2010-03-10 03:28:59 +0000489 QualType CanonInjectedType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000490 ClassTemplatePartialSpecializationDecl *PrevDecl,
491 unsigned SequenceNumber) {
John McCalld5532b62009-11-23 01:53:49 +0000492 unsigned N = ArgInfos.size();
John McCall833ca992009-10-29 08:12:44 +0000493 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
494 for (unsigned I = 0; I != N; ++I)
495 ClonedArgs[I] = ArgInfos[I];
496
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000497 ClassTemplatePartialSpecializationDecl *Result
Douglas Gregor13c85772010-05-06 00:28:52 +0000498 = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000499 DC, L, Params,
500 SpecializedTemplate,
John McCall833ca992009-10-29 08:12:44 +0000501 Builder,
502 ClonedArgs, N,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000503 PrevDecl,
504 SequenceNumber);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000505 Result->setSpecializationKind(TSK_ExplicitSpecialization);
John McCall3cb0ebd2010-03-10 03:28:59 +0000506
507 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000508 return Result;
509}
John McCalldd4a3b02009-09-16 22:47:08 +0000510
511//===----------------------------------------------------------------------===//
512// FriendTemplateDecl Implementation
513//===----------------------------------------------------------------------===//
514
515FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
516 DeclContext *DC,
517 SourceLocation L,
518 unsigned NParams,
519 TemplateParameterList **Params,
520 FriendUnion Friend,
521 SourceLocation FLoc) {
522 FriendTemplateDecl *Result
523 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
524 return Result;
525}