blob: f00eb0478a75ecddb849dd8fe1216500ab4a603f [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
Douglas Gregor24bae922010-07-08 18:37:38 +0000204ClassTemplateDecl::getInjectedClassNameSpecialization() {
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.
Douglas Gregor24bae922010-07-08 18:37:38 +0000213 ASTContext &Context = getASTContext();
Douglas Gregor7da97d02009-05-10 22:57:19 +0000214 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
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000269TemplateTypeParmDecl *
270TemplateTypeParmDecl::Create(ASTContext &C, EmptyShell Empty) {
271 return new (C) TemplateTypeParmDecl(0, SourceLocation(), 0, false,
272 QualType(), false);
273}
274
John McCall833ca992009-10-29 08:12:44 +0000275SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000276 return DefaultArgument->getTypeLoc().getSourceRange().getBegin();
John McCall833ca992009-10-29 08:12:44 +0000277}
278
Douglas Gregored9c0f92009-10-29 00:04:11 +0000279unsigned TemplateTypeParmDecl::getDepth() const {
280 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
281}
282
283unsigned TemplateTypeParmDecl::getIndex() const {
284 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
285}
286
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000287//===----------------------------------------------------------------------===//
288// NonTypeTemplateParmDecl Method Implementations
289//===----------------------------------------------------------------------===//
290
291NonTypeTemplateParmDecl *
292NonTypeTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
293 SourceLocation L, unsigned D, unsigned P,
294 IdentifierInfo *Id, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000295 TypeSourceInfo *TInfo) {
296 return new (C) NonTypeTemplateParmDecl(DC, L, D, P, Id, T, TInfo);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000297}
298
Douglas Gregord684b002009-02-10 19:49:53 +0000299SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +0000300 return hasDefaultArgument()
301 ? getDefaultArgument()->getSourceRange().getBegin()
302 : SourceLocation();
Douglas Gregord684b002009-02-10 19:49:53 +0000303}
304
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000305//===----------------------------------------------------------------------===//
306// TemplateTemplateParmDecl Method Implementations
307//===----------------------------------------------------------------------===//
308
309TemplateTemplateParmDecl *
310TemplateTemplateParmDecl::Create(ASTContext &C, DeclContext *DC,
311 SourceLocation L, unsigned D, unsigned P,
312 IdentifierInfo *Id,
313 TemplateParameterList *Params) {
314 return new (C) TemplateTemplateParmDecl(DC, L, D, P, Id, Params);
315}
316
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000317//===----------------------------------------------------------------------===//
Anders Carlsson9ba41642009-06-05 05:31:27 +0000318// TemplateArgumentListBuilder Implementation
319//===----------------------------------------------------------------------===//
Anders Carlssonfb250522009-06-23 01:26:57 +0000320
Chris Lattner98d279b2010-05-20 00:25:36 +0000321void TemplateArgumentListBuilder::Append(const TemplateArgument &Arg) {
322 assert((Arg.getKind() != TemplateArgument::Type ||
323 Arg.getAsType().isCanonical()) && "Type must be canonical!");
324 assert(FlatArgs.size() < MaxFlatArgs && "Argument list builder is full!");
Mike Stump1eb44332009-09-09 15:08:12 +0000325 assert(!StructuredArgs &&
Anders Carlssonfb250522009-06-23 01:26:57 +0000326 "Can't append arguments when an argument pack has been added!");
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner98d279b2010-05-20 00:25:36 +0000328 FlatArgs.push_back(Arg);
Anders Carlsson9ba41642009-06-05 05:31:27 +0000329}
330
Anders Carlssonfb250522009-06-23 01:26:57 +0000331void TemplateArgumentListBuilder::BeginPack() {
332 assert(!AddingToPack && "Already adding to pack!");
333 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Anders Carlssonfb250522009-06-23 01:26:57 +0000335 AddingToPack = true;
Chris Lattner98d279b2010-05-20 00:25:36 +0000336 PackBeginIndex = FlatArgs.size();
Anders Carlsson67e33202009-06-13 00:08:58 +0000337}
338
Anders Carlssonfb250522009-06-23 01:26:57 +0000339void TemplateArgumentListBuilder::EndPack() {
340 assert(AddingToPack && "Not adding to pack!");
341 assert(!StructuredArgs && "Argument list already contains a pack!");
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Anders Carlssonfb250522009-06-23 01:26:57 +0000343 AddingToPack = false;
Anders Carlsson3b36b662009-06-15 17:56:45 +0000344
Chris Lattner304d0fa2010-05-20 00:26:28 +0000345 // FIXME: This is a memory leak!
Anders Carlssonfb250522009-06-23 01:26:57 +0000346 StructuredArgs = new TemplateArgument[MaxStructuredArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Anders Carlssonfb250522009-06-23 01:26:57 +0000348 // First copy the flat entries over to the list (if any)
349 for (unsigned I = 0; I != PackBeginIndex; ++I) {
350 NumStructuredArgs++;
351 StructuredArgs[I] = FlatArgs[I];
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Anders Carlssonfb250522009-06-23 01:26:57 +0000354 // Next, set the pack.
355 TemplateArgument *PackArgs = 0;
356 unsigned NumPackArgs = NumFlatArgs - PackBeginIndex;
Chris Lattner98d279b2010-05-20 00:25:36 +0000357 // FIXME: NumPackArgs shouldn't be negative here???
Anders Carlssonfb250522009-06-23 01:26:57 +0000358 if (NumPackArgs)
Chris Lattner98d279b2010-05-20 00:25:36 +0000359 PackArgs = FlatArgs.data()+PackBeginIndex;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
361 StructuredArgs[NumStructuredArgs++].setArgumentPack(PackArgs, NumPackArgs,
Anders Carlssonfb250522009-06-23 01:26:57 +0000362 /*CopyArgs=*/false);
363}
364
Anders Carlsson9ba41642009-06-05 05:31:27 +0000365//===----------------------------------------------------------------------===//
Douglas Gregor7e063902009-05-11 23:53:27 +0000366// TemplateArgumentList Implementation
367//===----------------------------------------------------------------------===//
368TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
Anders Carlssone9c904b2009-06-05 04:47:51 +0000369 TemplateArgumentListBuilder &Builder,
Anders Carlssonfb250522009-06-23 01:26:57 +0000370 bool TakeArgs)
Mike Stump1eb44332009-09-09 15:08:12 +0000371 : FlatArguments(Builder.getFlatArguments(), TakeArgs),
372 NumFlatArguments(Builder.flatSize()),
Anders Carlssonfb250522009-06-23 01:26:57 +0000373 StructuredArguments(Builder.getStructuredArguments(), TakeArgs),
374 NumStructuredArguments(Builder.structuredSize()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Anders Carlssonfb250522009-06-23 01:26:57 +0000376 if (!TakeArgs)
377 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattner56ef5502010-05-20 00:11:47 +0000379 // If this does take ownership of the arguments, then we have to new them
380 // and copy over.
Ted Kremenek3458d432010-05-25 20:43:29 +0000381 TemplateArgument *NewArgs =
382 new (Context) TemplateArgument[Builder.flatSize()];
Chris Lattner56ef5502010-05-20 00:11:47 +0000383 std::copy(Builder.getFlatArguments(),
384 Builder.getFlatArguments()+Builder.flatSize(), NewArgs);
385 FlatArguments.setPointer(NewArgs);
386
387 // Just reuse the structured and flat arguments array if possible.
388 if (Builder.getStructuredArguments() == Builder.getFlatArguments()) {
389 StructuredArguments.setPointer(NewArgs);
Anders Carlssonfb250522009-06-23 01:26:57 +0000390 StructuredArguments.setInt(0);
Chris Lattner56ef5502010-05-20 00:11:47 +0000391 } else {
Ted Kremenek3458d432010-05-25 20:43:29 +0000392 TemplateArgument *NewSArgs =
393 new (Context) TemplateArgument[Builder.flatSize()];
Chris Lattner56ef5502010-05-20 00:11:47 +0000394 std::copy(Builder.getFlatArguments(),
395 Builder.getFlatArguments()+Builder.flatSize(), NewSArgs);
396 StructuredArguments.setPointer(NewSArgs);
397 }
Douglas Gregor7e063902009-05-11 23:53:27 +0000398}
399
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +0000400TemplateArgumentList::TemplateArgumentList(ASTContext &Context,
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000401 const TemplateArgument *Args,
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +0000402 unsigned NumArgs)
403 : NumFlatArguments(0), NumStructuredArguments(0) {
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000404 init(Context, Args, NumArgs);
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +0000405}
406
Chris Lattner88598912010-05-20 00:19:09 +0000407/// Produces a shallow copy of the given template argument list. This
408/// assumes that the input argument list outlives it. This takes the list as
409/// a pointer to avoid looking like a copy constructor, since this really
410/// really isn't safe to use that way.
411TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList *Other)
412 : FlatArguments(Other->FlatArguments.getPointer(), false),
413 NumFlatArguments(Other->flat_size()),
414 StructuredArguments(Other->StructuredArguments.getPointer(), false),
415 NumStructuredArguments(Other->NumStructuredArguments) { }
Douglas Gregorb9aa6b22009-09-24 23:14:47 +0000416
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000417void TemplateArgumentList::init(ASTContext &Context,
418 const TemplateArgument *Args,
419 unsigned NumArgs) {
420assert(NumFlatArguments == 0 && NumStructuredArguments == 0 &&
421 "Already initialized!");
422
423NumFlatArguments = NumStructuredArguments = NumArgs;
424TemplateArgument *NewArgs = new (Context) TemplateArgument[NumArgs];
425std::copy(Args, Args+NumArgs, NewArgs);
426FlatArguments.setPointer(NewArgs);
427FlatArguments.setInt(1); // Owns the pointer.
428
429// Just reuse the flat arguments array.
430StructuredArguments.setPointer(NewArgs);
431StructuredArguments.setInt(0); // Doesn't own the pointer.
432}
433
Ted Kremenek3458d432010-05-25 20:43:29 +0000434void TemplateArgumentList::Destroy(ASTContext &C) {
Chris Lattner56ef5502010-05-20 00:11:47 +0000435 if (FlatArguments.getInt())
Ted Kremenek3458d432010-05-25 20:43:29 +0000436 C.Deallocate((void*)FlatArguments.getPointer());
Chris Lattner56ef5502010-05-20 00:11:47 +0000437 if (StructuredArguments.getInt())
Ted Kremenek3458d432010-05-25 20:43:29 +0000438 C.Deallocate((void*)StructuredArguments.getPointer());
Douglas Gregor7e063902009-05-11 23:53:27 +0000439}
440
Ted Kremenek3458d432010-05-25 20:43:29 +0000441TemplateArgumentList::~TemplateArgumentList() {}
442
Douglas Gregor7e063902009-05-11 23:53:27 +0000443//===----------------------------------------------------------------------===//
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000444// ClassTemplateSpecializationDecl Implementation
445//===----------------------------------------------------------------------===//
446ClassTemplateSpecializationDecl::
Douglas Gregor13c85772010-05-06 00:28:52 +0000447ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Douglas Gregor7e063902009-05-11 23:53:27 +0000448 DeclContext *DC, SourceLocation L,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000449 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000450 TemplateArgumentListBuilder &Builder,
451 ClassTemplateSpecializationDecl *PrevDecl)
Douglas Gregor13c85772010-05-06 00:28:52 +0000452 : CXXRecordDecl(DK, TK, DC, L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000453 SpecializedTemplate->getIdentifier(),
454 PrevDecl),
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000455 SpecializedTemplate(SpecializedTemplate),
Abramo Bagnarac98971d2010-06-12 07:44:57 +0000456 ExplicitInfo(0),
Anders Carlssonfb250522009-06-23 01:26:57 +0000457 TemplateArgs(Context, Builder, /*TakeArgs=*/true),
Douglas Gregor7e063902009-05-11 23:53:27 +0000458 SpecializationKind(TSK_Undeclared) {
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000459}
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000461ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK)
462 : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), 0, 0),
463 ExplicitInfo(0),
464 SpecializationKind(TSK_Undeclared) {
465}
466
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000467ClassTemplateSpecializationDecl *
Douglas Gregor13c85772010-05-06 00:28:52 +0000468ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000469 DeclContext *DC, SourceLocation L,
470 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000471 TemplateArgumentListBuilder &Builder,
Douglas Gregorcc636682009-02-17 23:15:12 +0000472 ClassTemplateSpecializationDecl *PrevDecl) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000473 ClassTemplateSpecializationDecl *Result
Mike Stump1eb44332009-09-09 15:08:12 +0000474 = new (Context)ClassTemplateSpecializationDecl(Context,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000475 ClassTemplateSpecialization,
Douglas Gregor13c85772010-05-06 00:28:52 +0000476 TK, DC, L,
Douglas Gregor7e063902009-05-11 23:53:27 +0000477 SpecializedTemplate,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000478 Builder,
479 PrevDecl);
Douglas Gregorcc636682009-02-17 23:15:12 +0000480 Context.getTypeDeclType(Result, PrevDecl);
481 return Result;
Douglas Gregor3e00bad2009-02-17 01:05:43 +0000482}
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000483
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000484ClassTemplateSpecializationDecl *
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000485ClassTemplateSpecializationDecl::Create(ASTContext &Context, EmptyShell Empty) {
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000486 return
487 new (Context)ClassTemplateSpecializationDecl(ClassTemplateSpecialization);
488}
489
Douglas Gregor37d93e92009-08-02 23:24:31 +0000490void ClassTemplateSpecializationDecl::Destroy(ASTContext &C) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +0000491 delete ExplicitInfo;
492
Mike Stump1eb44332009-09-09 15:08:12 +0000493 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000494 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
495 C.Deallocate(PartialSpec);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Douglas Gregor37d93e92009-08-02 23:24:31 +0000497 CXXRecordDecl::Destroy(C);
498}
499
John McCall136a6982009-09-11 06:45:03 +0000500void
501ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
502 const PrintingPolicy &Policy,
503 bool Qualified) const {
504 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
505
506 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
507 S += TemplateSpecializationType::PrintTemplateArgumentList(
508 TemplateArgs.getFlatArgumentList(),
509 TemplateArgs.flat_size(),
510 Policy);
511}
512
Douglas Gregor37d93e92009-08-02 23:24:31 +0000513ClassTemplateDecl *
Mike Stump1eb44332009-09-09 15:08:12 +0000514ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
515 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor37d93e92009-08-02 23:24:31 +0000516 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
517 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
518 return SpecializedTemplate.get<ClassTemplateDecl*>();
519}
520
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000521//===----------------------------------------------------------------------===//
522// ClassTemplatePartialSpecializationDecl Implementation
523//===----------------------------------------------------------------------===//
524ClassTemplatePartialSpecializationDecl *
525ClassTemplatePartialSpecializationDecl::
Douglas Gregor13c85772010-05-06 00:28:52 +0000526Create(ASTContext &Context, TagKind TK,DeclContext *DC, SourceLocation L,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000527 TemplateParameterList *Params,
528 ClassTemplateDecl *SpecializedTemplate,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +0000529 TemplateArgumentListBuilder &Builder,
John McCalld5532b62009-11-23 01:53:49 +0000530 const TemplateArgumentListInfo &ArgInfos,
John McCall3cb0ebd2010-03-10 03:28:59 +0000531 QualType CanonInjectedType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000532 ClassTemplatePartialSpecializationDecl *PrevDecl,
533 unsigned SequenceNumber) {
John McCalld5532b62009-11-23 01:53:49 +0000534 unsigned N = ArgInfos.size();
John McCall833ca992009-10-29 08:12:44 +0000535 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
536 for (unsigned I = 0; I != N; ++I)
537 ClonedArgs[I] = ArgInfos[I];
538
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000539 ClassTemplatePartialSpecializationDecl *Result
Douglas Gregor13c85772010-05-06 00:28:52 +0000540 = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK,
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000541 DC, L, Params,
542 SpecializedTemplate,
John McCall833ca992009-10-29 08:12:44 +0000543 Builder,
544 ClonedArgs, N,
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000545 PrevDecl,
546 SequenceNumber);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000547 Result->setSpecializationKind(TSK_ExplicitSpecialization);
John McCall3cb0ebd2010-03-10 03:28:59 +0000548
549 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000550 return Result;
551}
John McCalldd4a3b02009-09-16 22:47:08 +0000552
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000553ClassTemplatePartialSpecializationDecl *
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000554ClassTemplatePartialSpecializationDecl::Create(ASTContext &Context,
555 EmptyShell Empty) {
Argyrios Kyrtzidis94d228d2010-06-23 13:48:23 +0000556 return new (Context)ClassTemplatePartialSpecializationDecl();
557}
558
559void ClassTemplatePartialSpecializationDecl::
560initTemplateArgsAsWritten(const TemplateArgumentListInfo &ArgInfos) {
561 assert(ArgsAsWritten == 0 && "ArgsAsWritten already set");
562 unsigned N = ArgInfos.size();
563 TemplateArgumentLoc *ClonedArgs
564 = new (getASTContext()) TemplateArgumentLoc[N];
565 for (unsigned I = 0; I != N; ++I)
566 ClonedArgs[I] = ArgInfos[I];
567
568 ArgsAsWritten = ClonedArgs;
569 NumArgsAsWritten = N;
570}
571
John McCalldd4a3b02009-09-16 22:47:08 +0000572//===----------------------------------------------------------------------===//
573// FriendTemplateDecl Implementation
574//===----------------------------------------------------------------------===//
575
576FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
577 DeclContext *DC,
578 SourceLocation L,
579 unsigned NParams,
580 TemplateParameterList **Params,
581 FriendUnion Friend,
582 SourceLocation FLoc) {
583 FriendTemplateDecl *Result
584 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
585 return Result;
586}