1 //===--- DeclTemplate.cpp - Template Declaration AST Node Implementation --===//
2 //
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"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/TypeLoc.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/Basic/IdentifierTable.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include <memory>
24 using namespace clang;
25
26 //===----------------------------------------------------------------------===//
27 // TemplateParameterList Implementation
28 //===----------------------------------------------------------------------===//
29
TemplateParameterList(SourceLocation TemplateLoc,SourceLocation LAngleLoc,NamedDecl ** Params,unsigned NumParams,SourceLocation RAngleLoc)30 TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
31 SourceLocation LAngleLoc,
32 NamedDecl **Params, unsigned NumParams,
33 SourceLocation RAngleLoc)
34 : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
35 NumParams(NumParams) {
36 for (unsigned Idx = 0; Idx < NumParams; ++Idx)
37 begin()[Idx] = Params[Idx];
38 }
39
40 TemplateParameterList *
Create(const ASTContext & C,SourceLocation TemplateLoc,SourceLocation LAngleLoc,NamedDecl ** Params,unsigned NumParams,SourceLocation RAngleLoc)41 TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
42 SourceLocation LAngleLoc, NamedDecl **Params,
43 unsigned NumParams, SourceLocation RAngleLoc) {
44 unsigned Size = sizeof(TemplateParameterList)
45 + sizeof(NamedDecl *) * NumParams;
46 unsigned Align = llvm::AlignOf<TemplateParameterList>::Alignment;
47 void *Mem = C.Allocate(Size, Align);
48 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
49 NumParams, RAngleLoc);
50 }
51
getMinRequiredArguments() const52 unsigned TemplateParameterList::getMinRequiredArguments() const {
53 unsigned NumRequiredArgs = 0;
54 for (iterator P = const_cast<TemplateParameterList *>(this)->begin(),
55 PEnd = const_cast<TemplateParameterList *>(this)->end();
56 P != PEnd; ++P) {
57 if ((*P)->isTemplateParameterPack()) {
58 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
59 if (NTTP->isExpandedParameterPack()) {
60 NumRequiredArgs += NTTP->getNumExpansionTypes();
61 continue;
62 }
63
64 break;
65 }
66
67 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
68 if (TTP->hasDefaultArgument())
69 break;
70 } else if (NonTypeTemplateParmDecl *NTTP
71 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
72 if (NTTP->hasDefaultArgument())
73 break;
74 } else if (cast<TemplateTemplateParmDecl>(*P)->hasDefaultArgument())
75 break;
76
77 ++NumRequiredArgs;
78 }
79
80 return NumRequiredArgs;
81 }
82
getDepth() const83 unsigned TemplateParameterList::getDepth() const {
84 if (size() == 0)
85 return 0;
86
87 const NamedDecl *FirstParm = getParam(0);
88 if (const TemplateTypeParmDecl *TTP
89 = dyn_cast<TemplateTypeParmDecl>(FirstParm))
90 return TTP->getDepth();
91 else if (const NonTypeTemplateParmDecl *NTTP
92 = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
93 return NTTP->getDepth();
94 else
95 return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
96 }
97
AdoptTemplateParameterList(TemplateParameterList * Params,DeclContext * Owner)98 static void AdoptTemplateParameterList(TemplateParameterList *Params,
99 DeclContext *Owner) {
100 for (TemplateParameterList::iterator P = Params->begin(),
101 PEnd = Params->end();
102 P != PEnd; ++P) {
103 (*P)->setDeclContext(Owner);
104
105 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*P))
106 AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner);
107 }
108 }
109
110 //===----------------------------------------------------------------------===//
111 // RedeclarableTemplateDecl Implementation
112 //===----------------------------------------------------------------------===//
113
getCommonPtr()114 RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() {
115 if (!Common) {
116 // Walk the previous-declaration chain until we either find a declaration
117 // with a common pointer or we run out of previous declarations.
118 llvm::SmallVector<RedeclarableTemplateDecl *, 2> PrevDecls;
119 for (RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
120 Prev = Prev->getPreviousDecl()) {
121 if (Prev->Common) {
122 Common = Prev->Common;
123 break;
124 }
125
126 PrevDecls.push_back(Prev);
127 }
128
129 // If we never found a common pointer, allocate one now.
130 if (!Common) {
131 // FIXME: If any of the declarations is from an AST file, we probably
132 // need an update record to add the common data.
133
134 Common = newCommon(getASTContext());
135 }
136
137 // Update any previous declarations we saw with the common pointer.
138 for (unsigned I = 0, N = PrevDecls.size(); I != N; ++I)
139 PrevDecls[I]->Common = Common;
140 }
141
142 return Common;
143 }
144
145 template <class EntryType>
146 typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType*
findSpecializationImpl(llvm::FoldingSet<EntryType> & Specs,const TemplateArgument * Args,unsigned NumArgs,void * & InsertPos)147 RedeclarableTemplateDecl::findSpecializationImpl(
148 llvm::FoldingSet<EntryType> &Specs,
149 const TemplateArgument *Args, unsigned NumArgs,
150 void *&InsertPos) {
151 typedef SpecEntryTraits<EntryType> SETraits;
152 llvm::FoldingSetNodeID ID;
153 EntryType::Profile(ID,Args,NumArgs, getASTContext());
154 EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
155 return Entry ? SETraits::getMostRecentDecl(Entry) : 0;
156 }
157
158 /// \brief Generate the injected template arguments for the given template
159 /// parameter list, e.g., for the injected-class-name of a class template.
GenerateInjectedTemplateArgs(ASTContext & Context,TemplateParameterList * Params,TemplateArgument * Args)160 static void GenerateInjectedTemplateArgs(ASTContext &Context,
161 TemplateParameterList *Params,
162 TemplateArgument *Args) {
163 for (TemplateParameterList::iterator Param = Params->begin(),
164 ParamEnd = Params->end();
165 Param != ParamEnd; ++Param) {
166 TemplateArgument Arg;
167 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
168 QualType ArgType = Context.getTypeDeclType(TTP);
169 if (TTP->isParameterPack())
170 ArgType = Context.getPackExpansionType(ArgType,
171 llvm::Optional<unsigned>());
172
173 Arg = TemplateArgument(ArgType);
174 } else if (NonTypeTemplateParmDecl *NTTP =
175 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
176 Expr *E = new (Context) DeclRefExpr(NTTP, /*enclosing*/ false,
177 NTTP->getType().getNonLValueExprType(Context),
178 Expr::getValueKindForType(NTTP->getType()),
179 NTTP->getLocation());
180
181 if (NTTP->isParameterPack())
182 E = new (Context) PackExpansionExpr(Context.DependentTy, E,
183 NTTP->getLocation(),
184 llvm::Optional<unsigned>());
185 Arg = TemplateArgument(E);
186 } else {
187 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
188 if (TTP->isParameterPack())
189 Arg = TemplateArgument(TemplateName(TTP), llvm::Optional<unsigned>());
190 else
191 Arg = TemplateArgument(TemplateName(TTP));
192 }
193
194 if ((*Param)->isTemplateParameterPack())
195 Arg = TemplateArgument::CreatePackCopy(Context, &Arg, 1);
196
197 *Args++ = Arg;
198 }
199 }
200
201 //===----------------------------------------------------------------------===//
202 // FunctionTemplateDecl Implementation
203 //===----------------------------------------------------------------------===//
204
DeallocateCommon(void * Ptr)205 void FunctionTemplateDecl::DeallocateCommon(void *Ptr) {
206 static_cast<Common *>(Ptr)->~Common();
207 }
208
Create(ASTContext & C,DeclContext * DC,SourceLocation L,DeclarationName Name,TemplateParameterList * Params,NamedDecl * Decl)209 FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
210 DeclContext *DC,
211 SourceLocation L,
212 DeclarationName Name,
213 TemplateParameterList *Params,
214 NamedDecl *Decl) {
215 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
216 return new (C) FunctionTemplateDecl(DC, L, Name, Params, Decl);
217 }
218
CreateDeserialized(ASTContext & C,unsigned ID)219 FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
220 unsigned ID) {
221 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionTemplateDecl));
222 return new (Mem) FunctionTemplateDecl(0, SourceLocation(), DeclarationName(),
223 0, 0);
224 }
225
226 RedeclarableTemplateDecl::CommonBase *
newCommon(ASTContext & C)227 FunctionTemplateDecl::newCommon(ASTContext &C) {
228 Common *CommonPtr = new (C) Common;
229 C.AddDeallocation(DeallocateCommon, CommonPtr);
230 return CommonPtr;
231 }
232
233 FunctionDecl *
findSpecialization(const TemplateArgument * Args,unsigned NumArgs,void * & InsertPos)234 FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args,
235 unsigned NumArgs, void *&InsertPos) {
236 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
237 }
238
addSpecialization(FunctionTemplateSpecializationInfo * Info,void * InsertPos)239 void FunctionTemplateDecl::addSpecialization(
240 FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
241 if (InsertPos)
242 getSpecializations().InsertNode(Info, InsertPos);
243 else
244 getSpecializations().GetOrInsertNode(Info);
245 if (ASTMutationListener *L = getASTMutationListener())
246 L->AddedCXXTemplateSpecialization(this, Info->Function);
247 }
248
249 std::pair<const TemplateArgument *, unsigned>
getInjectedTemplateArgs()250 FunctionTemplateDecl::getInjectedTemplateArgs() {
251 TemplateParameterList *Params = getTemplateParameters();
252 Common *CommonPtr = getCommonPtr();
253 if (!CommonPtr->InjectedArgs) {
254 CommonPtr->InjectedArgs
255 = new (getASTContext()) TemplateArgument [Params->size()];
256 GenerateInjectedTemplateArgs(getASTContext(), Params,
257 CommonPtr->InjectedArgs);
258 }
259
260 return std::make_pair(CommonPtr->InjectedArgs, Params->size());
261 }
262
263 //===----------------------------------------------------------------------===//
264 // ClassTemplateDecl Implementation
265 //===----------------------------------------------------------------------===//
266
DeallocateCommon(void * Ptr)267 void ClassTemplateDecl::DeallocateCommon(void *Ptr) {
268 static_cast<Common *>(Ptr)->~Common();
269 }
270
Create(ASTContext & C,DeclContext * DC,SourceLocation L,DeclarationName Name,TemplateParameterList * Params,NamedDecl * Decl,ClassTemplateDecl * PrevDecl)271 ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
272 DeclContext *DC,
273 SourceLocation L,
274 DeclarationName Name,
275 TemplateParameterList *Params,
276 NamedDecl *Decl,
277 ClassTemplateDecl *PrevDecl) {
278 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
279 ClassTemplateDecl *New = new (C) ClassTemplateDecl(DC, L, Name, Params, Decl);
280 New->setPreviousDeclaration(PrevDecl);
281 return New;
282 }
283
CreateDeserialized(ASTContext & C,unsigned ID)284 ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
285 unsigned ID) {
286 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ClassTemplateDecl));
287 return new (Mem) ClassTemplateDecl(EmptyShell());
288 }
289
LoadLazySpecializations()290 void ClassTemplateDecl::LoadLazySpecializations() {
291 Common *CommonPtr = getCommonPtr();
292 if (CommonPtr->LazySpecializations) {
293 ASTContext &Context = getASTContext();
294 uint32_t *Specs = CommonPtr->LazySpecializations;
295 CommonPtr->LazySpecializations = 0;
296 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
297 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
298 }
299 }
300
301 llvm::FoldingSet<ClassTemplateSpecializationDecl> &
getSpecializations()302 ClassTemplateDecl::getSpecializations() {
303 LoadLazySpecializations();
304 return getCommonPtr()->Specializations;
305 }
306
307 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &
getPartialSpecializations()308 ClassTemplateDecl::getPartialSpecializations() {
309 LoadLazySpecializations();
310 return getCommonPtr()->PartialSpecializations;
311 }
312
313 RedeclarableTemplateDecl::CommonBase *
newCommon(ASTContext & C)314 ClassTemplateDecl::newCommon(ASTContext &C) {
315 Common *CommonPtr = new (C) Common;
316 C.AddDeallocation(DeallocateCommon, CommonPtr);
317 return CommonPtr;
318 }
319
320 ClassTemplateSpecializationDecl *
findSpecialization(const TemplateArgument * Args,unsigned NumArgs,void * & InsertPos)321 ClassTemplateDecl::findSpecialization(const TemplateArgument *Args,
322 unsigned NumArgs, void *&InsertPos) {
323 return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
324 }
325
AddSpecialization(ClassTemplateSpecializationDecl * D,void * InsertPos)326 void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
327 void *InsertPos) {
328 if (InsertPos)
329 getSpecializations().InsertNode(D, InsertPos);
330 else {
331 ClassTemplateSpecializationDecl *Existing
332 = getSpecializations().GetOrInsertNode(D);
333 (void)Existing;
334 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
335 }
336 if (ASTMutationListener *L = getASTMutationListener())
337 L->AddedCXXTemplateSpecialization(this, D);
338 }
339
340 ClassTemplatePartialSpecializationDecl *
findPartialSpecialization(const TemplateArgument * Args,unsigned NumArgs,void * & InsertPos)341 ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
342 unsigned NumArgs,
343 void *&InsertPos) {
344 return findSpecializationImpl(getPartialSpecializations(), Args, NumArgs,
345 InsertPos);
346 }
347
AddPartialSpecialization(ClassTemplatePartialSpecializationDecl * D,void * InsertPos)348 void ClassTemplateDecl::AddPartialSpecialization(
349 ClassTemplatePartialSpecializationDecl *D,
350 void *InsertPos) {
351 if (InsertPos)
352 getPartialSpecializations().InsertNode(D, InsertPos);
353 else {
354 ClassTemplatePartialSpecializationDecl *Existing
355 = getPartialSpecializations().GetOrInsertNode(D);
356 (void)Existing;
357 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
358 }
359
360 if (ASTMutationListener *L = getASTMutationListener())
361 L->AddedCXXTemplateSpecialization(this, D);
362 }
363
getPartialSpecializations(SmallVectorImpl<ClassTemplatePartialSpecializationDecl * > & PS)364 void ClassTemplateDecl::getPartialSpecializations(
365 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
366 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs
367 = getPartialSpecializations();
368 PS.clear();
369 PS.resize(PartialSpecs.size());
370 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
371 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
372 P != PEnd; ++P) {
373 assert(!PS[P->getSequenceNumber()]);
374 PS[P->getSequenceNumber()] = P->getMostRecentDecl();
375 }
376 }
377
378 ClassTemplatePartialSpecializationDecl *
findPartialSpecialization(QualType T)379 ClassTemplateDecl::findPartialSpecialization(QualType T) {
380 ASTContext &Context = getASTContext();
381 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
382 partial_spec_iterator;
383 for (partial_spec_iterator P = getPartialSpecializations().begin(),
384 PEnd = getPartialSpecializations().end();
385 P != PEnd; ++P) {
386 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
387 return P->getMostRecentDecl();
388 }
389
390 return 0;
391 }
392
393 ClassTemplatePartialSpecializationDecl *
findPartialSpecInstantiatedFromMember(ClassTemplatePartialSpecializationDecl * D)394 ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
395 ClassTemplatePartialSpecializationDecl *D) {
396 Decl *DCanon = D->getCanonicalDecl();
397 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
398 P = getPartialSpecializations().begin(),
399 PEnd = getPartialSpecializations().end();
400 P != PEnd; ++P) {
401 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
402 return P->getMostRecentDecl();
403 }
404
405 return 0;
406 }
407
408 QualType
getInjectedClassNameSpecialization()409 ClassTemplateDecl::getInjectedClassNameSpecialization() {
410 Common *CommonPtr = getCommonPtr();
411 if (!CommonPtr->InjectedClassNameType.isNull())
412 return CommonPtr->InjectedClassNameType;
413
414 // C++0x [temp.dep.type]p2:
415 // The template argument list of a primary template is a template argument
416 // list in which the nth template argument has the value of the nth template
417 // parameter of the class template. If the nth template parameter is a
418 // template parameter pack (14.5.3), the nth template argument is a pack
419 // expansion (14.5.3) whose pattern is the name of the template parameter
420 // pack.
421 ASTContext &Context = getASTContext();
422 TemplateParameterList *Params = getTemplateParameters();
423 SmallVector<TemplateArgument, 16> TemplateArgs;
424 TemplateArgs.resize(Params->size());
425 GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data());
426 CommonPtr->InjectedClassNameType
427 = Context.getTemplateSpecializationType(TemplateName(this),
428 &TemplateArgs[0],
429 TemplateArgs.size());
430 return CommonPtr->InjectedClassNameType;
431 }
432
433 //===----------------------------------------------------------------------===//
434 // TemplateTypeParm Allocation/Deallocation Method Implementations
435 //===----------------------------------------------------------------------===//
436
437 TemplateTypeParmDecl *
Create(const ASTContext & C,DeclContext * DC,SourceLocation KeyLoc,SourceLocation NameLoc,unsigned D,unsigned P,IdentifierInfo * Id,bool Typename,bool ParameterPack)438 TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
439 SourceLocation KeyLoc, SourceLocation NameLoc,
440 unsigned D, unsigned P, IdentifierInfo *Id,
441 bool Typename, bool ParameterPack) {
442 TemplateTypeParmDecl *TTPDecl =
443 new (C) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
444 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
445 TTPDecl->TypeForDecl = TTPType.getTypePtr();
446 return TTPDecl;
447 }
448
449 TemplateTypeParmDecl *
CreateDeserialized(const ASTContext & C,unsigned ID)450 TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
451 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTypeParmDecl));
452 return new (Mem) TemplateTypeParmDecl(0, SourceLocation(), SourceLocation(),
453 0, false);
454 }
455
getDefaultArgumentLoc() const456 SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
457 return hasDefaultArgument()
458 ? DefaultArgument->getTypeLoc().getBeginLoc()
459 : SourceLocation();
460 }
461
getSourceRange() const462 SourceRange TemplateTypeParmDecl::getSourceRange() const {
463 if (hasDefaultArgument() && !defaultArgumentWasInherited())
464 return SourceRange(getLocStart(),
465 DefaultArgument->getTypeLoc().getEndLoc());
466 else
467 return TypeDecl::getSourceRange();
468 }
469
getDepth() const470 unsigned TemplateTypeParmDecl::getDepth() const {
471 return TypeForDecl->getAs<TemplateTypeParmType>()->getDepth();
472 }
473
getIndex() const474 unsigned TemplateTypeParmDecl::getIndex() const {
475 return TypeForDecl->getAs<TemplateTypeParmType>()->getIndex();
476 }
477
isParameterPack() const478 bool TemplateTypeParmDecl::isParameterPack() const {
479 return TypeForDecl->getAs<TemplateTypeParmType>()->isParameterPack();
480 }
481
482 //===----------------------------------------------------------------------===//
483 // NonTypeTemplateParmDecl Method Implementations
484 //===----------------------------------------------------------------------===//
485
NonTypeTemplateParmDecl(DeclContext * DC,SourceLocation StartLoc,SourceLocation IdLoc,unsigned D,unsigned P,IdentifierInfo * Id,QualType T,TypeSourceInfo * TInfo,const QualType * ExpandedTypes,unsigned NumExpandedTypes,TypeSourceInfo ** ExpandedTInfos)486 NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC,
487 SourceLocation StartLoc,
488 SourceLocation IdLoc,
489 unsigned D, unsigned P,
490 IdentifierInfo *Id,
491 QualType T,
492 TypeSourceInfo *TInfo,
493 const QualType *ExpandedTypes,
494 unsigned NumExpandedTypes,
495 TypeSourceInfo **ExpandedTInfos)
496 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
497 TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false),
498 ParameterPack(true), ExpandedParameterPack(true),
499 NumExpandedTypes(NumExpandedTypes)
500 {
501 if (ExpandedTypes && ExpandedTInfos) {
502 void **TypesAndInfos = reinterpret_cast<void **>(this + 1);
503 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
504 TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr();
505 TypesAndInfos[2*I + 1] = ExpandedTInfos[I];
506 }
507 }
508 }
509
510 NonTypeTemplateParmDecl *
Create(const ASTContext & C,DeclContext * DC,SourceLocation StartLoc,SourceLocation IdLoc,unsigned D,unsigned P,IdentifierInfo * Id,QualType T,bool ParameterPack,TypeSourceInfo * TInfo)511 NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
512 SourceLocation StartLoc, SourceLocation IdLoc,
513 unsigned D, unsigned P, IdentifierInfo *Id,
514 QualType T, bool ParameterPack,
515 TypeSourceInfo *TInfo) {
516 return new (C) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
517 T, ParameterPack, TInfo);
518 }
519
520 NonTypeTemplateParmDecl *
Create(const ASTContext & C,DeclContext * DC,SourceLocation StartLoc,SourceLocation IdLoc,unsigned D,unsigned P,IdentifierInfo * Id,QualType T,TypeSourceInfo * TInfo,const QualType * ExpandedTypes,unsigned NumExpandedTypes,TypeSourceInfo ** ExpandedTInfos)521 NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
522 SourceLocation StartLoc, SourceLocation IdLoc,
523 unsigned D, unsigned P,
524 IdentifierInfo *Id, QualType T,
525 TypeSourceInfo *TInfo,
526 const QualType *ExpandedTypes,
527 unsigned NumExpandedTypes,
528 TypeSourceInfo **ExpandedTInfos) {
529 unsigned Size = sizeof(NonTypeTemplateParmDecl)
530 + NumExpandedTypes * 2 * sizeof(void*);
531 void *Mem = C.Allocate(Size);
532 return new (Mem) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc,
533 D, P, Id, T, TInfo,
534 ExpandedTypes, NumExpandedTypes,
535 ExpandedTInfos);
536 }
537
538 NonTypeTemplateParmDecl *
CreateDeserialized(ASTContext & C,unsigned ID)539 NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
540 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NonTypeTemplateParmDecl));
541 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
542 SourceLocation(), 0, 0, 0,
543 QualType(), false, 0);
544 }
545
546 NonTypeTemplateParmDecl *
CreateDeserialized(ASTContext & C,unsigned ID,unsigned NumExpandedTypes)547 NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
548 unsigned NumExpandedTypes) {
549 unsigned Size = sizeof(NonTypeTemplateParmDecl)
550 + NumExpandedTypes * 2 * sizeof(void*);
551
552 void *Mem = AllocateDeserializedDecl(C, ID, Size);
553 return new (Mem) NonTypeTemplateParmDecl(0, SourceLocation(),
554 SourceLocation(), 0, 0, 0,
555 QualType(), 0, 0, NumExpandedTypes,
556 0);
557 }
558
getSourceRange() const559 SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
560 if (hasDefaultArgument() && !defaultArgumentWasInherited())
561 return SourceRange(getOuterLocStart(),
562 getDefaultArgument()->getSourceRange().getEnd());
563 return DeclaratorDecl::getSourceRange();
564 }
565
getDefaultArgumentLoc() const566 SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
567 return hasDefaultArgument()
568 ? getDefaultArgument()->getSourceRange().getBegin()
569 : SourceLocation();
570 }
571
572 //===----------------------------------------------------------------------===//
573 // TemplateTemplateParmDecl Method Implementations
574 //===----------------------------------------------------------------------===//
575
anchor()576 void TemplateTemplateParmDecl::anchor() { }
577
578 TemplateTemplateParmDecl *
Create(const ASTContext & C,DeclContext * DC,SourceLocation L,unsigned D,unsigned P,bool ParameterPack,IdentifierInfo * Id,TemplateParameterList * Params)579 TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
580 SourceLocation L, unsigned D, unsigned P,
581 bool ParameterPack, IdentifierInfo *Id,
582 TemplateParameterList *Params) {
583 return new (C) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
584 Params);
585 }
586
587 TemplateTemplateParmDecl *
CreateDeserialized(ASTContext & C,unsigned ID)588 TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
589 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TemplateTemplateParmDecl));
590 return new (Mem) TemplateTemplateParmDecl(0, SourceLocation(), 0, 0, false,
591 0, 0);
592 }
593
594 //===----------------------------------------------------------------------===//
595 // TemplateArgumentList Implementation
596 //===----------------------------------------------------------------------===//
597 TemplateArgumentList *
CreateCopy(ASTContext & Context,const TemplateArgument * Args,unsigned NumArgs)598 TemplateArgumentList::CreateCopy(ASTContext &Context,
599 const TemplateArgument *Args,
600 unsigned NumArgs) {
601 std::size_t Size = sizeof(TemplateArgumentList)
602 + NumArgs * sizeof(TemplateArgument);
603 void *Mem = Context.Allocate(Size);
604 TemplateArgument *StoredArgs
605 = reinterpret_cast<TemplateArgument *>(
606 static_cast<TemplateArgumentList *>(Mem) + 1);
607 std::uninitialized_copy(Args, Args + NumArgs, StoredArgs);
608 return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true);
609 }
610
611 FunctionTemplateSpecializationInfo *
Create(ASTContext & C,FunctionDecl * FD,FunctionTemplateDecl * Template,TemplateSpecializationKind TSK,const TemplateArgumentList * TemplateArgs,const TemplateArgumentListInfo * TemplateArgsAsWritten,SourceLocation POI)612 FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD,
613 FunctionTemplateDecl *Template,
614 TemplateSpecializationKind TSK,
615 const TemplateArgumentList *TemplateArgs,
616 const TemplateArgumentListInfo *TemplateArgsAsWritten,
617 SourceLocation POI) {
618 const ASTTemplateArgumentListInfo *ArgsAsWritten = 0;
619 if (TemplateArgsAsWritten)
620 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
621 *TemplateArgsAsWritten);
622
623 return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK,
624 TemplateArgs,
625 ArgsAsWritten,
626 POI);
627 }
628
629 //===----------------------------------------------------------------------===//
630 // TemplateDecl Implementation
631 //===----------------------------------------------------------------------===//
632
anchor()633 void TemplateDecl::anchor() { }
634
635 //===----------------------------------------------------------------------===//
636 // ClassTemplateSpecializationDecl Implementation
637 //===----------------------------------------------------------------------===//
638 ClassTemplateSpecializationDecl::
ClassTemplateSpecializationDecl(ASTContext & Context,Kind DK,TagKind TK,DeclContext * DC,SourceLocation StartLoc,SourceLocation IdLoc,ClassTemplateDecl * SpecializedTemplate,const TemplateArgument * Args,unsigned NumArgs,ClassTemplateSpecializationDecl * PrevDecl)639 ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
640 DeclContext *DC, SourceLocation StartLoc,
641 SourceLocation IdLoc,
642 ClassTemplateDecl *SpecializedTemplate,
643 const TemplateArgument *Args,
644 unsigned NumArgs,
645 ClassTemplateSpecializationDecl *PrevDecl)
646 : CXXRecordDecl(DK, TK, DC, StartLoc, IdLoc,
647 SpecializedTemplate->getIdentifier(),
648 PrevDecl),
649 SpecializedTemplate(SpecializedTemplate),
650 ExplicitInfo(0),
651 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
652 SpecializationKind(TSK_Undeclared) {
653 }
654
ClassTemplateSpecializationDecl(Kind DK)655 ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(Kind DK)
656 : CXXRecordDecl(DK, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0),
657 ExplicitInfo(0),
658 SpecializationKind(TSK_Undeclared) {
659 }
660
661 ClassTemplateSpecializationDecl *
Create(ASTContext & Context,TagKind TK,DeclContext * DC,SourceLocation StartLoc,SourceLocation IdLoc,ClassTemplateDecl * SpecializedTemplate,const TemplateArgument * Args,unsigned NumArgs,ClassTemplateSpecializationDecl * PrevDecl)662 ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
663 DeclContext *DC,
664 SourceLocation StartLoc,
665 SourceLocation IdLoc,
666 ClassTemplateDecl *SpecializedTemplate,
667 const TemplateArgument *Args,
668 unsigned NumArgs,
669 ClassTemplateSpecializationDecl *PrevDecl) {
670 ClassTemplateSpecializationDecl *Result
671 = new (Context)ClassTemplateSpecializationDecl(Context,
672 ClassTemplateSpecialization,
673 TK, DC, StartLoc, IdLoc,
674 SpecializedTemplate,
675 Args, NumArgs,
676 PrevDecl);
677 Context.getTypeDeclType(Result, PrevDecl);
678 return Result;
679 }
680
681 ClassTemplateSpecializationDecl *
CreateDeserialized(ASTContext & C,unsigned ID)682 ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
683 unsigned ID) {
684 void *Mem = AllocateDeserializedDecl(C, ID,
685 sizeof(ClassTemplateSpecializationDecl));
686 return new (Mem) ClassTemplateSpecializationDecl(ClassTemplateSpecialization);
687 }
688
689 void
getNameForDiagnostic(std::string & S,const PrintingPolicy & Policy,bool Qualified) const690 ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
691 const PrintingPolicy &Policy,
692 bool Qualified) const {
693 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
694
695 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
696 S += TemplateSpecializationType::PrintTemplateArgumentList(
697 TemplateArgs.data(),
698 TemplateArgs.size(),
699 Policy);
700 }
701
702 ClassTemplateDecl *
getSpecializedTemplate() const703 ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
704 if (SpecializedPartialSpecialization *PartialSpec
705 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
706 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
707 return SpecializedTemplate.get<ClassTemplateDecl*>();
708 }
709
710 SourceRange
getSourceRange() const711 ClassTemplateSpecializationDecl::getSourceRange() const {
712 if (ExplicitInfo) {
713 SourceLocation Begin = getExternLoc();
714 if (Begin.isInvalid())
715 Begin = getTemplateKeywordLoc();
716 SourceLocation End = getRBraceLoc();
717 if (End.isInvalid())
718 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
719 return SourceRange(Begin, End);
720 }
721 else {
722 // No explicit info available.
723 llvm::PointerUnion<ClassTemplateDecl *,
724 ClassTemplatePartialSpecializationDecl *>
725 inst_from = getInstantiatedFrom();
726 if (inst_from.isNull())
727 return getSpecializedTemplate()->getSourceRange();
728 if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>())
729 return ctd->getSourceRange();
730 return inst_from.get<ClassTemplatePartialSpecializationDecl*>()
731 ->getSourceRange();
732 }
733 }
734
735 //===----------------------------------------------------------------------===//
736 // ClassTemplatePartialSpecializationDecl Implementation
737 //===----------------------------------------------------------------------===//
anchor()738 void ClassTemplatePartialSpecializationDecl::anchor() { }
739
740 ClassTemplatePartialSpecializationDecl::
ClassTemplatePartialSpecializationDecl(ASTContext & Context,TagKind TK,DeclContext * DC,SourceLocation StartLoc,SourceLocation IdLoc,TemplateParameterList * Params,ClassTemplateDecl * SpecializedTemplate,const TemplateArgument * Args,unsigned NumArgs,TemplateArgumentLoc * ArgInfos,unsigned NumArgInfos,ClassTemplatePartialSpecializationDecl * PrevDecl,unsigned SequenceNumber)741 ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
742 DeclContext *DC,
743 SourceLocation StartLoc,
744 SourceLocation IdLoc,
745 TemplateParameterList *Params,
746 ClassTemplateDecl *SpecializedTemplate,
747 const TemplateArgument *Args,
748 unsigned NumArgs,
749 TemplateArgumentLoc *ArgInfos,
750 unsigned NumArgInfos,
751 ClassTemplatePartialSpecializationDecl *PrevDecl,
752 unsigned SequenceNumber)
753 : ClassTemplateSpecializationDecl(Context,
754 ClassTemplatePartialSpecialization,
755 TK, DC, StartLoc, IdLoc,
756 SpecializedTemplate,
757 Args, NumArgs, PrevDecl),
758 TemplateParams(Params), ArgsAsWritten(ArgInfos),
759 NumArgsAsWritten(NumArgInfos), SequenceNumber(SequenceNumber),
760 InstantiatedFromMember(0, false)
761 {
762 AdoptTemplateParameterList(Params, this);
763 }
764
765 ClassTemplatePartialSpecializationDecl *
766 ClassTemplatePartialSpecializationDecl::
Create(ASTContext & Context,TagKind TK,DeclContext * DC,SourceLocation StartLoc,SourceLocation IdLoc,TemplateParameterList * Params,ClassTemplateDecl * SpecializedTemplate,const TemplateArgument * Args,unsigned NumArgs,const TemplateArgumentListInfo & ArgInfos,QualType CanonInjectedType,ClassTemplatePartialSpecializationDecl * PrevDecl,unsigned SequenceNumber)767 Create(ASTContext &Context, TagKind TK,DeclContext *DC,
768 SourceLocation StartLoc, SourceLocation IdLoc,
769 TemplateParameterList *Params,
770 ClassTemplateDecl *SpecializedTemplate,
771 const TemplateArgument *Args,
772 unsigned NumArgs,
773 const TemplateArgumentListInfo &ArgInfos,
774 QualType CanonInjectedType,
775 ClassTemplatePartialSpecializationDecl *PrevDecl,
776 unsigned SequenceNumber) {
777 unsigned N = ArgInfos.size();
778 TemplateArgumentLoc *ClonedArgs = new (Context) TemplateArgumentLoc[N];
779 for (unsigned I = 0; I != N; ++I)
780 ClonedArgs[I] = ArgInfos[I];
781
782 ClassTemplatePartialSpecializationDecl *Result
783 = new (Context)ClassTemplatePartialSpecializationDecl(Context, TK, DC,
784 StartLoc, IdLoc,
785 Params,
786 SpecializedTemplate,
787 Args, NumArgs,
788 ClonedArgs, N,
789 PrevDecl,
790 SequenceNumber);
791 Result->setSpecializationKind(TSK_ExplicitSpecialization);
792
793 Context.getInjectedClassNameType(Result, CanonInjectedType);
794 return Result;
795 }
796
797 ClassTemplatePartialSpecializationDecl *
CreateDeserialized(ASTContext & C,unsigned ID)798 ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
799 unsigned ID) {
800 void *Mem = AllocateDeserializedDecl(C, ID,
801 sizeof(ClassTemplatePartialSpecializationDecl));
802 return new (Mem) ClassTemplatePartialSpecializationDecl();
803 }
804
805 //===----------------------------------------------------------------------===//
806 // FriendTemplateDecl Implementation
807 //===----------------------------------------------------------------------===//
808
anchor()809 void FriendTemplateDecl::anchor() { }
810
Create(ASTContext & Context,DeclContext * DC,SourceLocation L,unsigned NParams,TemplateParameterList ** Params,FriendUnion Friend,SourceLocation FLoc)811 FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
812 DeclContext *DC,
813 SourceLocation L,
814 unsigned NParams,
815 TemplateParameterList **Params,
816 FriendUnion Friend,
817 SourceLocation FLoc) {
818 FriendTemplateDecl *Result
819 = new (Context) FriendTemplateDecl(DC, L, NParams, Params, Friend, FLoc);
820 return Result;
821 }
822
CreateDeserialized(ASTContext & C,unsigned ID)823 FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
824 unsigned ID) {
825 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FriendTemplateDecl));
826 return new (Mem) FriendTemplateDecl(EmptyShell());
827 }
828
829 //===----------------------------------------------------------------------===//
830 // TypeAliasTemplateDecl Implementation
831 //===----------------------------------------------------------------------===//
832
Create(ASTContext & C,DeclContext * DC,SourceLocation L,DeclarationName Name,TemplateParameterList * Params,NamedDecl * Decl)833 TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
834 DeclContext *DC,
835 SourceLocation L,
836 DeclarationName Name,
837 TemplateParameterList *Params,
838 NamedDecl *Decl) {
839 AdoptTemplateParameterList(Params, DC);
840 return new (C) TypeAliasTemplateDecl(DC, L, Name, Params, Decl);
841 }
842
CreateDeserialized(ASTContext & C,unsigned ID)843 TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
844 unsigned ID) {
845 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasTemplateDecl));
846 return new (Mem) TypeAliasTemplateDecl(0, SourceLocation(), DeclarationName(),
847 0, 0);
848 }
849
DeallocateCommon(void * Ptr)850 void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) {
851 static_cast<Common *>(Ptr)->~Common();
852 }
853 RedeclarableTemplateDecl::CommonBase *
newCommon(ASTContext & C)854 TypeAliasTemplateDecl::newCommon(ASTContext &C) {
855 Common *CommonPtr = new (C) Common;
856 C.AddDeallocation(DeallocateCommon, CommonPtr);
857 return CommonPtr;
858 }
859
860 //===----------------------------------------------------------------------===//
861 // ClassScopeFunctionSpecializationDecl Implementation
862 //===----------------------------------------------------------------------===//
863
anchor()864 void ClassScopeFunctionSpecializationDecl::anchor() { }
865
866 ClassScopeFunctionSpecializationDecl *
CreateDeserialized(ASTContext & C,unsigned ID)867 ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
868 unsigned ID) {
869 void *Mem = AllocateDeserializedDecl(C, ID,
870 sizeof(ClassScopeFunctionSpecializationDecl));
871 return new (Mem) ClassScopeFunctionSpecializationDecl(0, SourceLocation(), 0);
872 }
873