blob: ca19d1ff033ecf6a2b298f2f5403d2b1fed970e5 [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) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000165 ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl);
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000166 New->setPreviousDeclaration(PrevDecl);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000167 return New;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000168}
169
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000170void ClassTemplateDecl::Destroy(ASTContext& C) {
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000171 Decl::Destroy(C);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000172}
173
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000174void ClassTemplateDecl::getPartialSpecializations(
175 llvm::SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
176 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000177 = getPartialSpecializations();
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000178 PS.clear();
179 PS.resize(PartialSpecs.size());
180 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
181 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
182 P != PEnd; ++P) {
183 assert(!PS[P->getSequenceNumber()]);
184 PS[P->getSequenceNumber()] = &*P;
185 }
186}
187
Douglas Gregorb88e8882009-07-30 17:40:51 +0000188ClassTemplatePartialSpecializationDecl *
189ClassTemplateDecl::findPartialSpecialization(QualType T) {
190 ASTContext &Context = getASTContext();
191 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
192 partial_spec_iterator;
193 for (partial_spec_iterator P = getPartialSpecializations().begin(),
194 PEnd = getPartialSpecializations().end();
195 P != PEnd; ++P) {
John McCall31f17ec2010-04-27 00:57:59 +0000196 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregorb88e8882009-07-30 17:40:51 +0000197 return &*P;
198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Douglas Gregorb88e8882009-07-30 17:40:51 +0000200 return 0;
201}
202
John McCall3cb0ebd2010-03-10 03:28:59 +0000203QualType
204ClassTemplateDecl::getInjectedClassNameSpecialization(ASTContext &Context) {
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000205 Common *CommonPtr = getCommonPtr();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000206 if (!CommonPtr->InjectedClassNameType.isNull())
207 return CommonPtr->InjectedClassNameType;
208
209 // FIXME: n2800 14.6.1p1 should say how the template arguments
210 // corresponding to template parameter packs should be pack
211 // expansions. We already say that in 14.6.2.1p2, so it would be
212 // better to fix that redundancy.
213
214 TemplateParameterList *Params = getTemplateParameters();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000215 llvm::SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregor7da97d02009-05-10 22:57:19 +0000216 TemplateArgs.reserve(Params->size());
Mike Stump1eb44332009-09-09 15:08:12 +0000217 for (TemplateParameterList::iterator Param = Params->begin(),
218 ParamEnd = Params->end();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000219 Param != ParamEnd; ++Param) {
220 if (isa<TemplateTypeParmDecl>(*Param)) {
221 QualType ParamType = Context.getTypeDeclType(cast<TypeDecl>(*Param));
John McCall833ca992009-10-29 08:12:44 +0000222 TemplateArgs.push_back(TemplateArgument(ParamType));
Mike Stump1eb44332009-09-09 15:08:12 +0000223 } else if (NonTypeTemplateParmDecl *NTTP =
Douglas Gregor7da97d02009-05-10 22:57:19 +0000224 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Chandler Carruthb7de1812010-01-31 07:24:03 +0000225 Expr *E = new (Context) DeclRefExpr(NTTP,
226 NTTP->getType().getNonReferenceType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000227 NTTP->getLocation());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000228 TemplateArgs.push_back(TemplateArgument(E));
Mike Stump1eb44332009-09-09 15:08:12 +0000229 } else {
Douglas Gregor7da97d02009-05-10 22:57:19 +0000230 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
Douglas Gregor788cd062009-11-11 01:00:40 +0000231 TemplateArgs.push_back(TemplateArgument(TemplateName(TTP)));
Douglas Gregor7da97d02009-05-10 22:57:19 +0000232 }
233 }
234
Douglas Gregor7da97d02009-05-10 22:57:19 +0000235 CommonPtr->InjectedClassNameType
Douglas Gregor1275ae02009-07-28 23:00:59 +0000236 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregor7da97d02009-05-10 22:57:19 +0000237 &TemplateArgs[0],
Douglas Gregor1275ae02009-07-28 23:00:59 +0000238 TemplateArgs.size());
Douglas Gregor7da97d02009-05-10 22:57:19 +0000239 return CommonPtr->InjectedClassNameType;
240}
241
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +0000242ClassTemplateDecl::Common *ClassTemplateDecl::getCommonPtr() {
243 // Find the first declaration of this function template.
244 ClassTemplateDecl *First = this;
245 while (First->getPreviousDeclaration())
246 First = First->getPreviousDeclaration();
247
248 if (First->CommonOrPrev.isNull()) {
249 Common *CommonPtr = new (getASTContext()) Common;
250 getASTContext().AddDeallocation(DeallocateCommon, CommonPtr);
251 First->CommonOrPrev = CommonPtr;
252 }
253 return First->CommonOrPrev.get<Common*>();
254}
255
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000256//===----------------------------------------------------------------------===//
257// TemplateTypeParm Allocation/Deallocation Method Implementations
258//===----------------------------------------------------------------------===//
259
260TemplateTypeParmDecl *
261TemplateTypeParmDecl::Create(ASTContext &C, DeclContext *DC,
262 SourceLocation L, unsigned D, unsigned P,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000263 IdentifierInfo *Id, bool Typename,
264 bool ParameterPack) {
Douglas Gregorefed5c82010-06-16 15:23:05 +0000265 QualType Type = C.getTemplateTypeParmType(D, P, ParameterPack, Id);
266 return new (C) TemplateTypeParmDecl(DC, L, Id, Typename, Type, ParameterPack);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000267}
268
John McCall833ca992009-10-29 08:12:44 +0000269SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000270 return DefaultArgument->getTypeLoc().getSourceRange().getBegin();
John McCall833ca992009-10-29 08:12:44 +0000271}
272
Douglas Gregored9c0f92009-10-29 00:04:11 +0000273unsigned TemplateTypeParmDecl::getDepth() const {
274 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
275}
276
277unsigned TemplateTypeParmDecl::getIndex() const {
278 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
279}
280
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000281//===----------------------------------------------------------------------===//
282// NonTypeTemplateParmDecl Method Implementations
283//===----------------------------------------------------------------------===//
284
285NonTypeTemplateParmDecl *
286NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
287 SourceLocation L, unsigned D, unsigned P,
288 IdentifierInfo *Id, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000289 TypeSourceInfo *TInfo) {
290 return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, TInfo);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000291}
292
Douglas Gregord684b002009-02-10 19:49:53 +0000293SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +0000294 return hasDefaultArgument()
295 ? getDefaultArgument()->getSourceRange().getBegin()
296 : SourceLocation();
Douglas Gregord684b002009-02-10 19:49:53 +0000297}
298
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000299//===----------------------------------------------------------------------===//
300// TemplateTemplateParmDecl Method Implementations
301//===----------------------------------------------------------------------===//
302
303TemplateTemplateParmDecl *
304TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
305 SourceLocation L, unsigned D, unsigned P,
306 IdentifierInfo *Id,
307 TemplateParameterList *Params) {
308 return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params);
309}
310
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000311//===----------------------------------------------------------------------===//
Anders Carlsson9ba41642009-06-05 05:31:27 +0000312// TemplateArgumentListBuilder Implementation
313//===----------------------------------------------------------------------===//
Anders Carlssonfb250522009-06-23 01:26:57 +0000314
Chris Lattner98d279b2010-05-20 00:25:36 +0000315void TemplateArgumentListBuilder::Append(const TemplateArgument &Arg) {
316 assert((Arg.getKind() != TemplateArgument::Type ||
317 Arg.getAsType().isCanonical()) && "Type must be canonical!");
318 assert(FlatArgs.size() < MaxFlatArgs && "Argument list builder is full!");
Mike Stump1eb44332009-09-09 15:08:12 +0000319 assert(!StructuredArgs &&
Anders Carlssonfb250522009-06-23 01:26:57 +0000320 "Can't append arguments when an argument pack has been added!");
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Chris Lattner98d279b2010-05-20 00:25:36 +0000322 FlatArgs.push_back(Arg);
Anders Carlsson9ba41642009-06-05 05:31:27 +0000323}
324
Anders Carlssonfb250522009-06-23 01:26:57 +0000325void TemplateArgumentListBuilder::BeginPack() {
326 assert(!AddingToPack && "Already adding to pack!");
327 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Anders Carlssonfb250522009-06-23 01:26:57 +0000329 AddingToPack = true;
Chris Lattner98d279b2010-05-20 00:25:36 +0000330 PackBeginIndex = FlatArgs.size();
Anders Carlsson67e33202009-06-13 00:08:58 +0000331}
332
Anders Carlssonfb250522009-06-23 01:26:57 +0000333void TemplateArgumentListBuilder::EndPack() {
334 assert(AddingToPack && "Not adding to pack!");
335 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Anders Carlssonfb250522009-06-23 01:26:57 +0000337 AddingToPack = false;
Anders Carlsson3b36b662009-06-15 17:56:45 +0000338
Chris Lattner304d0fa2010-05-20 00:26:28 +0000339 // FIXME: This is a memory leak!
Anders Carlssonfb250522009-06-23 01:26:57 +0000340 StructuredArgs = new TemplateArgument[MaxStructuredArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Anders Carlssonfb250522009-06-23 01:26:57 +0000342 // First copy the flat entries over to the list (if any)
343 for (unsigned I = 0; I != PackBeginIndex; ++I) {
344 NumStructuredArgs++;
345 StructuredArgs[I] = FlatArgs[I];
346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Anders Carlssonfb250522009-06-23 01:26:57 +0000348 // Next, set the pack.
349 TemplateArgument *PackArgs = 0;
350 unsigned NumPackArgs = NumFlatArgs - PackBeginIndex;
Chris Lattner98d279b2010-05-20 00:25:36 +0000351 // FIXME: NumPackArgs shouldn't be negative here???
Anders Carlssonfb250522009-06-23 01:26:57 +0000352 if (NumPackArgs)
Chris Lattner98d279b2010-05-20 00:25:36 +0000353 PackArgs = FlatArgs.data()+PackBeginIndex;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
355 StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs,
Anders Carlssonfb250522009-06-23 01:26:57 +0000356 /*CopyArgs=*/false);
357}
358
Anders Carlsson9ba41642009-06-05 05:31:27 +0000359//===----------------------------------------------------------------------===//
Douglas Gregor7e063902009-05-11 23:53:27 +0000360// TemplateArgumentList Implementation
361//===----------------------------------------------------------------------===//
362TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
Anders Carlssone9c904b2009-06-05 04:47:51 +0000363 TemplateArgumentListBuilder &Builder,
Anders Carlssonfb250522009-06-23 01:26:57 +0000364 bool TakeArgs)
Mike Stump1eb44332009-09-09 15:08:12 +0000365 : FlatArguments(Builder.getFlatArguments(), TakeArgs),
366 NumFlatArguments(Builder.flatSize()),
Anders Carlssonfb250522009-06-23 01:26:57 +0000367 StructuredArguments(Builder.getStructuredArguments(), TakeArgs),
368 NumStructuredArguments(Builder.structuredSize()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Anders Carlssonfb250522009-06-23 01:26:57 +0000370 if (!TakeArgs)
371 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattner56ef5502010-05-20 00:11:47 +0000373 // If this does take ownership of the arguments, then we have to new them
374 // and copy over.
Ted Kremenek3458d432010-05-25 20:43:29 +0000375 TemplateArgument *NewArgs =
376 new (Context) TemplateArgument[Builder.flatSize()];
Chris Lattner56ef5502010-05-20 00:11:47 +0000377 std::copy(Builder.getFlatArguments(),
378 Builder.getFlatArguments()+Builder.flatSize(), NewArgs);
379 FlatArguments.setPointer(NewArgs);
380
381 // Just reuse the structured and flat arguments array if possible.
382 if (Builder.getStructuredArguments() == Builder.getFlatArguments()) {
383 StructuredArguments.setPointer(NewArgs);
Anders Carlssonfb250522009-06-23 01:26:57 +0000384 StructuredArguments.setInt(0);
Chris Lattner56ef5502010-05-20 00:11:47 +0000385 } else {
Ted Kremenek3458d432010-05-25 20:43:29 +0000386 TemplateArgument *NewSArgs =
387 new (Context) TemplateArgument[Builder.flatSize()];
Chris Lattner56ef5502010-05-20 00:11:47 +0000388 std::copy(Builder.getFlatArguments(),
389 Builder.getFlatArguments()+Builder.flatSize(), NewSArgs);
390 StructuredArguments.setPointer(NewSArgs);
391 }
Douglas Gregor7e063902009-05-11 23:53:27 +0000392}
393
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +0000394TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000395 const TemplateArgument *Args,
396 unsigned NumArgs) {
397 init(Context, Args, NumArgs);
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +0000398}
399
Chris Lattner88598912010-05-20 00:19:09 +0000400/// Produces a shallow copy of the given template argument list. This
401/// assumes that the input argument list outlives it. This takes the list as
402/// a pointer to avoid looking like a copy constructor, since this really
403/// really isn't safe to use that way.
404TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList *Other)
405 : FlatArguments(Other->FlatArguments.getPointer(), false),
406 NumFlatArguments(Other->flat_size()),
407 StructuredArguments(Other->StructuredArguments.getPointer(), false),
408 NumStructuredArguments(Other->NumStructuredArguments) { }
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000409
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000410void TemplateArgumentList::init(ASTContext &Context,
411 const TemplateArgument *Args,
412 unsigned NumArgs) {
413assert(NumFlatArguments == 0 && NumStructuredArguments == 0 &&
414 "Already initialized!");
415
416NumFlatArguments = NumStructuredArguments = NumArgs;
417TemplateArgument *NewArgs = new (Context) TemplateArgument[NumArgs];
418std::copy(Args, Args+NumArgs, NewArgs);
419FlatArguments.setPointer(NewArgs);
420FlatArguments.setInt(1); // Owns the pointer.
421
422// Just reuse the flat arguments array.
423StructuredArguments.setPointer(NewArgs);
424StructuredArguments.setInt(0); // Doesn't own the pointer.
425}
426
Ted Kremenek3458d432010-05-25 20:43:29 +0000427void TemplateArgumentList::Destroy(ASTContext &C) {
Chris Lattner56ef5502010-05-20 00:11:47 +0000428 if (FlatArguments.getInt())
Ted Kremenek3458d432010-05-25 20:43:29 +0000429 C.Deallocate((void*)FlatArguments.getPointer());
Chris Lattner56ef5502010-05-20 00:11:47 +0000430 if (StructuredArguments.getInt())
Ted Kremenek3458d432010-05-25 20:43:29 +0000431 C.Deallocate((void*)StructuredArguments.getPointer());
Douglas Gregor7e063902009-05-11 23:53:27 +0000432}
433
Ted Kremenek3458d432010-05-25 20:43:29 +0000434TemplateArgumentList::~TemplateArgumentList() {}
435
Douglas Gregor7e063902009-05-11 23:53:27 +0000436//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000437// ClassTemplateSpecializationDecl Implementation
438//===----------------------------------------------------------------------===//
439ClassTemplateSpecializationDecl::
Douglas Gregor13c85772010-05-06 00:28:52 +0000440ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Douglas Gregor7e063902009-05-11 23:53:27 +0000441 DeclContext *DC, SourceLocation L,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000442 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000443 TemplateArgumentListBuilder &Builder,
444 ClassTemplateSpecializationDecl *PrevDecl)
Douglas Gregor13c85772010-05-06 00:28:52 +0000445 : CXXRecordDecl(DK, TK, DC, L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000446 SpecializedTemplate->getIdentifier(),
447 PrevDecl),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000448 SpecializedTemplate(SpecializedTemplate),
Abramo Bagnarac98971d2010-06-12 07:44:57 +0000449 ExplicitInfo(0),
Anders Carlssonfb250522009-06-23 01:26:57 +0000450 TemplateArgs(Context, Builder, /*TakeArgs=*/true),
Douglas Gregor7e063902009-05-11 23:53:27 +0000451 SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000452}
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000454ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK)
455 : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), 0, 0),
456 ExplicitInfo(0),
457 SpecializationKind(TSK_Undeclared) {
458}
459
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000460ClassTemplateSpecializationDecl *
Douglas Gregor13c85772010-05-06 00:28:52 +0000461ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000462 DeclContext *DC, SourceLocation L,
463 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000464 TemplateArgumentListBuilder &Builder,
Douglas Gregorcc636682009-02-17 23:15:12 +0000465 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000466 ClassTemplateSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000467 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000468 ClassTemplateSpecialization,
Douglas Gregor13c85772010-05-06 00:28:52 +0000469 TK, DC, L,
Douglas Gregor7e063902009-05-11 23:53:27 +0000470 SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000471 Builder,
472 PrevDecl);
Douglas Gregorcc636682009-02-17 23:15:12 +0000473 Context.getTypeDeclType(Result, PrevDecl);
474 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000475}
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000476
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000477ClassTemplateSpecializationDecl *
478ClassTemplateSpecializationDecl::CreateEmpty(ASTContext &Context) {
479 return
480 new (Context)ClassTemplateSpecializationDecl(ClassTemplateSpecialization);
481}
482
Douglas Gregor37d93e92009-08-02 23:24:31 +0000483void ClassTemplateSpecializationDecl::Destroy(ASTContext &C) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +0000484 delete ExplicitInfo;
485
Mike Stump1eb44332009-09-09 15:08:12 +0000486 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000487 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
488 C.Deallocate(PartialSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Douglas Gregor37d93e92009-08-02 23:24:31 +0000490 CXXRecordDecl::Destroy(C);
491}
492
John McCall136a6982009-09-11 06:45:03 +0000493void
494ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
495 const PrintingPolicy &Policy,
496 bool Qualified) const {
497 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
498
499 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
500 S += TemplateSpecializationType::PrintTemplateArgumentList(
501 TemplateArgs.getFlatArgumentList(),
502 TemplateArgs.flat_size(),
503 Policy);
504}
505
Douglas Gregor37d93e92009-08-02 23:24:31 +0000506ClassTemplateDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000507ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
508 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000509 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
510 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
511 return SpecializedTemplate.get<ClassTemplateDecl*>();
512}
513
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000514//===----------------------------------------------------------------------===//
515// ClassTemplatePartialSpecializationDecl Implementation
516//===----------------------------------------------------------------------===//
517ClassTemplatePartialSpecializationDecl *
518ClassTemplatePartialSpecializationDecl::
Douglas Gregor13c85772010-05-06 00:28:52 +0000519Create(ASTContext &Context, TagKind TK,DeclContext *DC, SourceLocation L,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000520 TemplateParameterList *Params,
521 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000522 TemplateArgumentListBuilder &Builder,
John McCalld5532b62009-11-23 01:53:49 +0000523 const TemplateArgumentListInfo &ArgInfos,
John McCall3cb0ebd2010-03-10 03:28:59 +0000524 QualType CanonInjectedType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000525 ClassTemplatePartialSpecializationDecl *PrevDecl,
526 unsigned SequenceNumber) {
John McCalld5532b62009-11-23 01:53:49 +0000527 unsigned N = ArgInfos.size();
John McCall833ca992009-10-29 08:12:44 +0000528 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
529 for (unsigned I = 0; I != N; ++I)
530 ClonedArgs[I] = ArgInfos[I];
531
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000532 ClassTemplatePartialSpecializationDecl *Result
Douglas Gregor13c85772010-05-06 00:28:52 +0000533 = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000534 DC, L, Params,
535 SpecializedTemplate,
John McCall833ca992009-10-29 08:12:44 +0000536 Builder,
537 ClonedArgs, N,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000538 PrevDecl,
539 SequenceNumber);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000540 Result->setSpecializationKind(TSK_ExplicitSpecialization);
John McCall3cb0ebd2010-03-10 03:28:59 +0000541
542 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000543 return Result;
544}
John McCalldd4a3b02009-09-16 22:47:08 +0000545
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000546ClassTemplatePartialSpecializationDecl *
547ClassTemplatePartialSpecializationDecl::CreateEmpty(ASTContext &Context) {
548 return new (Context)ClassTemplatePartialSpecializationDecl();
549}
550
551void ClassTemplatePartialSpecializationDecl::
552initTemplateArgsAsWritten(const TemplateArgumentListInfo &ArgInfos) {
553 assert(ArgsAsWritten == 0 && "ArgsAsWritten already set");
554 unsigned N = ArgInfos.size();
555 TemplateArgumentLoc *ClonedArgs
556 = new (getASTContext()) TemplateArgumentLoc[N];
557 for (unsigned I = 0; I != N; ++I)
558 ClonedArgs[I] = ArgInfos[I];
559
560 ArgsAsWritten = ClonedArgs;
561 NumArgsAsWritten = N;
562}
563
John McCalldd4a3b02009-09-16 22:47:08 +0000564//===----------------------------------------------------------------------===//
565// FriendTemplateDecl Implementation
566//===----------------------------------------------------------------------===//
567
568FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
569 DeclContext *DC,
570 SourceLocation L,
571 unsigned NParams,
572 TemplateParameterList **Params,
573 FriendUnion Friend,
574 SourceLocation FLoc) {
575 FriendTemplateDecl *Result
576 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
577 return Result;
578}