1 //===-- DeclTemplate.h - Classes for representing C++ templates -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the C++ template declaration subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLTEMPLATE_H
15 #define LLVM_CLANG_AST_DECLTEMPLATE_H
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/TemplateBase.h"
19 #include "llvm/ADT/PointerUnion.h"
24 class TemplateParameterList
;
26 class RedeclarableTemplateDecl
;
27 class FunctionTemplateDecl
;
28 class ClassTemplateDecl
;
29 class ClassTemplatePartialSpecializationDecl
;
30 class TemplateTypeParmDecl
;
31 class NonTypeTemplateParmDecl
;
32 class TemplateTemplateParmDecl
;
34 /// \brief Stores a template parameter of any kind.
35 typedef llvm::PointerUnion3
<TemplateTypeParmDecl
*, NonTypeTemplateParmDecl
*,
36 TemplateTemplateParmDecl
*> TemplateParameter
;
38 /// TemplateParameterList - Stores a list of template parameters for a
39 /// TemplateDecl and its derived classes.
40 class TemplateParameterList
{
41 /// The location of the 'template' keyword.
42 SourceLocation TemplateLoc
;
44 /// The locations of the '<' and '>' angle brackets.
45 SourceLocation LAngleLoc
, RAngleLoc
;
47 /// The number of template parameters in this template
51 TemplateParameterList(SourceLocation TemplateLoc
, SourceLocation LAngleLoc
,
52 NamedDecl
**Params
, unsigned NumParams
,
53 SourceLocation RAngleLoc
);
56 static TemplateParameterList
*Create(ASTContext
&C
,
57 SourceLocation TemplateLoc
,
58 SourceLocation LAngleLoc
,
61 SourceLocation RAngleLoc
);
63 /// iterator - Iterates through the template parameters in this list.
64 typedef NamedDecl
** iterator
;
66 /// const_iterator - Iterates through the template parameters in this list.
67 typedef NamedDecl
* const* const_iterator
;
69 iterator
begin() { return reinterpret_cast<NamedDecl
**>(this + 1); }
70 const_iterator
begin() const {
71 return reinterpret_cast<NamedDecl
* const *>(this + 1);
73 iterator
end() { return begin() + NumParams
; }
74 const_iterator
end() const { return begin() + NumParams
; }
76 unsigned size() const { return NumParams
; }
78 NamedDecl
* getParam(unsigned Idx
) {
79 assert(Idx
< size() && "Template parameter index out-of-range");
83 const NamedDecl
* getParam(unsigned Idx
) const {
84 assert(Idx
< size() && "Template parameter index out-of-range");
88 /// \btief Returns the minimum number of arguments needed to form a
89 /// template specialization. This may be fewer than the number of
90 /// template parameters, if some of the parameters have default
91 /// arguments or if there is a parameter pack.
92 unsigned getMinRequiredArguments() const;
94 /// \brief Get the depth of this template parameter list in the set of
95 /// template parameter lists.
97 /// The first template parameter list in a declaration will have depth 0,
98 /// the second template parameter list will have depth 1, etc.
99 unsigned getDepth() const;
101 SourceLocation
getTemplateLoc() const { return TemplateLoc
; }
102 SourceLocation
getLAngleLoc() const { return LAngleLoc
; }
103 SourceLocation
getRAngleLoc() const { return RAngleLoc
; }
105 SourceRange
getSourceRange() const {
106 return SourceRange(TemplateLoc
, RAngleLoc
);
110 /// \brief A helper class for making template argument lists.
111 class TemplateArgumentListBuilder
{
112 TemplateArgument
*StructuredArgs
;
113 unsigned MaxStructuredArgs
;
114 unsigned NumStructuredArgs
;
116 llvm::SmallVector
<TemplateArgument
, 4> FlatArgs
;
117 unsigned MaxFlatArgs
;
118 unsigned NumFlatArgs
;
121 unsigned PackBeginIndex
;
124 TemplateArgumentListBuilder(const TemplateParameterList
*Parameters
,
125 unsigned NumTemplateArgs
)
126 : StructuredArgs(0), MaxStructuredArgs(Parameters
->size()),
127 NumStructuredArgs(0), FlatArgs(0),
128 MaxFlatArgs(std::max(MaxStructuredArgs
, NumTemplateArgs
)), NumFlatArgs(0),
129 AddingToPack(false), PackBeginIndex(0) { }
131 void Append(const TemplateArgument
&Arg
);
135 unsigned flatSize() const { return FlatArgs
.size(); }
136 const TemplateArgument
*getFlatArguments() const { return FlatArgs
.data(); }
138 unsigned structuredSize() const {
139 // If we don't have any structured args, just reuse the flat size.
143 return NumStructuredArgs
;
145 const TemplateArgument
*getStructuredArguments() const {
146 // If we don't have any structured args, just reuse the flat args.
148 return getFlatArguments();
150 return StructuredArgs
;
154 /// \brief A template argument list.
156 /// FIXME: In the future, this class will be extended to support
157 /// variadic templates and member templates, which will make some of
158 /// the function names below make more sense.
159 class TemplateArgumentList
{
160 /// \brief The template argument list.
162 /// The integer value will be non-zero to indicate that this
163 /// template argument list does own the pointer.
164 llvm::PointerIntPair
<const TemplateArgument
*, 1> FlatArguments
;
166 /// \brief The number of template arguments in this template
168 unsigned NumFlatArguments
;
170 llvm::PointerIntPair
<const TemplateArgument
*, 1> StructuredArguments
;
171 unsigned NumStructuredArguments
;
173 TemplateArgumentList(const TemplateArgumentList
&Other
); // DO NOT IMPL
174 void operator=(const TemplateArgumentList
&Other
); // DO NOT IMPL
176 /// TemplateArgumentList - If this constructor is passed "true" for 'TakeArgs'
177 /// it copies them into a locally new[]'d array. If passed "false", then it
178 /// just references the array passed in. This is only safe if the builder
179 /// outlives it, but saves a copy.
180 TemplateArgumentList(ASTContext
&Context
,
181 TemplateArgumentListBuilder
&Builder
,
184 /// TemplateArgumentList - It copies the template arguments into a locally
186 TemplateArgumentList(ASTContext
&Context
,
187 const TemplateArgument
*Args
, unsigned NumArgs
);
189 /// Produces a shallow copy of the given template argument list. This
190 /// assumes that the input argument list outlives it. This takes the list as
191 /// a pointer to avoid looking like a copy constructor, since this really
192 /// really isn't safe to use that way.
193 explicit TemplateArgumentList(const TemplateArgumentList
*Other
);
195 TemplateArgumentList() : NumFlatArguments(0), NumStructuredArguments(0) { }
197 /// \brief Copies the template arguments into a locally new[]'d array.
198 void init(ASTContext
&Context
,
199 const TemplateArgument
*Args
, unsigned NumArgs
);
201 /// \brief Retrieve the template argument at a given index.
202 const TemplateArgument
&get(unsigned Idx
) const {
203 assert(Idx
< NumFlatArguments
&& "Invalid template argument index");
204 return getFlatArgumentList()[Idx
];
207 /// \brief Retrieve the template argument at a given index.
208 const TemplateArgument
&operator[](unsigned Idx
) const { return get(Idx
); }
210 /// \brief Retrieve the number of template arguments in this
211 /// template argument list.
212 unsigned size() const { return NumFlatArguments
; }
214 /// \brief Retrieve the number of template arguments in the
215 /// flattened template argument list.
216 unsigned flat_size() const { return NumFlatArguments
; }
218 /// \brief Retrieve the flattened template argument list.
219 const TemplateArgument
*getFlatArgumentList() const {
220 return FlatArguments
.getPointer();
224 //===----------------------------------------------------------------------===//
225 // Kinds of Templates
226 //===----------------------------------------------------------------------===//
228 /// TemplateDecl - The base class of all kinds of template declarations (e.g.,
229 /// class, function, etc.). The TemplateDecl class stores the list of template
230 /// parameters and a reference to the templated scoped declaration: the
231 /// underlying AST node.
232 class TemplateDecl
: public NamedDecl
{
234 // This is probably never used.
235 TemplateDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
236 DeclarationName Name
)
237 : NamedDecl(DK
, DC
, L
, Name
), TemplatedDecl(0), TemplateParams(0) { }
239 // Construct a template decl with the given name and parameters.
240 // Used when there is not templated element (tt-params, alias?).
241 TemplateDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
242 DeclarationName Name
, TemplateParameterList
*Params
)
243 : NamedDecl(DK
, DC
, L
, Name
), TemplatedDecl(0), TemplateParams(Params
) { }
245 // Construct a template decl with name, parameters, and templated element.
246 TemplateDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
247 DeclarationName Name
, TemplateParameterList
*Params
,
249 : NamedDecl(DK
, DC
, L
, Name
), TemplatedDecl(Decl
),
250 TemplateParams(Params
) { }
252 /// Get the list of template parameters
253 TemplateParameterList
*getTemplateParameters() const {
254 return TemplateParams
;
257 /// Get the underlying, templated declaration.
258 NamedDecl
*getTemplatedDecl() const { return TemplatedDecl
; }
260 // Implement isa/cast/dyncast/etc.
261 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
262 static bool classof(const TemplateDecl
*D
) { return true; }
263 static bool classof(const RedeclarableTemplateDecl
*D
) { return true; }
264 static bool classof(const FunctionTemplateDecl
*D
) { return true; }
265 static bool classof(const ClassTemplateDecl
*D
) { return true; }
266 static bool classof(const TemplateTemplateParmDecl
*D
) { return true; }
267 static bool classofKind(Kind K
) {
268 return K
>= firstTemplate
&& K
<= lastTemplate
;
271 SourceRange
getSourceRange() const {
272 return SourceRange(TemplateParams
->getTemplateLoc(),
273 TemplatedDecl
->getSourceRange().getEnd());
277 NamedDecl
*TemplatedDecl
;
278 TemplateParameterList
* TemplateParams
;
281 /// \brief Initialize the underlying templated declaration and
282 /// template parameters.
283 void init(NamedDecl
*templatedDecl
, TemplateParameterList
* templateParams
) {
284 assert(TemplatedDecl
== 0 && "TemplatedDecl already set!");
285 assert(TemplateParams
== 0 && "TemplateParams already set!");
286 TemplatedDecl
= templatedDecl
;
287 TemplateParams
= templateParams
;
291 /// \brief Provides information about a function template specialization,
292 /// which is a FunctionDecl that has been explicitly specialization or
293 /// instantiated from a function template.
294 class FunctionTemplateSpecializationInfo
: public llvm::FoldingSetNode
{
295 FunctionTemplateSpecializationInfo(FunctionDecl
*FD
,
296 FunctionTemplateDecl
*Template
,
297 TemplateSpecializationKind TSK
,
298 const TemplateArgumentList
*TemplateArgs
,
299 const TemplateArgumentListInfo
*TemplateArgsAsWritten
,
302 Template(Template
, TSK
- 1),
303 TemplateArguments(TemplateArgs
),
304 TemplateArgumentsAsWritten(TemplateArgsAsWritten
),
305 PointOfInstantiation(POI
) { }
308 static FunctionTemplateSpecializationInfo
*
309 Create(ASTContext
&C
, FunctionDecl
*FD
, FunctionTemplateDecl
*Template
,
310 TemplateSpecializationKind TSK
,
311 const TemplateArgumentList
*TemplateArgs
,
312 const TemplateArgumentListInfo
*TemplateArgsAsWritten
,
313 SourceLocation POI
) {
314 return new (C
) FunctionTemplateSpecializationInfo(FD
, Template
, TSK
,
316 TemplateArgsAsWritten
,
320 /// \brief The function template specialization that this structure
322 FunctionDecl
*Function
;
324 /// \brief The function template from which this function template
325 /// specialization was generated.
327 /// The two bits are contain the top 4 values of TemplateSpecializationKind.
328 llvm::PointerIntPair
<FunctionTemplateDecl
*, 2> Template
;
330 /// \brief The template arguments used to produce the function template
331 /// specialization from the function template.
332 const TemplateArgumentList
*TemplateArguments
;
334 /// \brief The template arguments as written in the sources, if provided.
335 const TemplateArgumentListInfo
*TemplateArgumentsAsWritten
;
337 /// \brief The point at which this function template specialization was
338 /// first instantiated.
339 SourceLocation PointOfInstantiation
;
341 /// \brief Retrieve the template from which this function was specialized.
342 FunctionTemplateDecl
*getTemplate() const { return Template
.getPointer(); }
344 /// \brief Determine what kind of template specialization this is.
345 TemplateSpecializationKind
getTemplateSpecializationKind() const {
346 return (TemplateSpecializationKind
)(Template
.getInt() + 1);
349 /// \brief Set the template specialization kind.
350 void setTemplateSpecializationKind(TemplateSpecializationKind TSK
) {
351 assert(TSK
!= TSK_Undeclared
&&
352 "Cannot encode TSK_Undeclared for a function template specialization");
353 Template
.setInt(TSK
- 1);
356 /// \brief Retrieve the first point of instantiation of this function
357 /// template specialization.
359 /// The point of instantiation may be an invalid source location if this
360 /// function has yet to be instantiated.
361 SourceLocation
getPointOfInstantiation() const {
362 return PointOfInstantiation
;
365 /// \brief Set the (first) point of instantiation of this function template
367 void setPointOfInstantiation(SourceLocation POI
) {
368 PointOfInstantiation
= POI
;
371 void Profile(llvm::FoldingSetNodeID
&ID
) {
372 Profile(ID
, TemplateArguments
->getFlatArgumentList(),
373 TemplateArguments
->flat_size(),
374 Function
->getASTContext());
378 Profile(llvm::FoldingSetNodeID
&ID
, const TemplateArgument
*TemplateArgs
,
379 unsigned NumTemplateArgs
, ASTContext
&Context
) {
380 ID
.AddInteger(NumTemplateArgs
);
381 for (unsigned Arg
= 0; Arg
!= NumTemplateArgs
; ++Arg
)
382 TemplateArgs
[Arg
].Profile(ID
, Context
);
386 /// \brief Provides information a specialization of a member of a class
387 /// template, which may be a member function, static data member, or
389 class MemberSpecializationInfo
{
390 // The member declaration from which this member was instantiated, and the
391 // manner in which the instantiation occurred (in the lower two bits).
392 llvm::PointerIntPair
<NamedDecl
*, 2> MemberAndTSK
;
394 // The point at which this member was first instantiated.
395 SourceLocation PointOfInstantiation
;
399 MemberSpecializationInfo(NamedDecl
*IF
, TemplateSpecializationKind TSK
,
400 SourceLocation POI
= SourceLocation())
401 : MemberAndTSK(IF
, TSK
- 1), PointOfInstantiation(POI
) {
402 assert(TSK
!= TSK_Undeclared
&&
403 "Cannot encode undeclared template specializations for members");
406 /// \brief Retrieve the member declaration from which this member was
408 NamedDecl
*getInstantiatedFrom() const { return MemberAndTSK
.getPointer(); }
410 /// \brief Determine what kind of template specialization this is.
411 TemplateSpecializationKind
getTemplateSpecializationKind() const {
412 return (TemplateSpecializationKind
)(MemberAndTSK
.getInt() + 1);
415 /// \brief Set the template specialization kind.
416 void setTemplateSpecializationKind(TemplateSpecializationKind TSK
) {
417 assert(TSK
!= TSK_Undeclared
&&
418 "Cannot encode undeclared template specializations for members");
419 MemberAndTSK
.setInt(TSK
- 1);
422 /// \brief Retrieve the first point of instantiation of this member.
423 /// If the point of instantiation is an invalid location, then this member
424 /// has not yet been instantiated.
425 SourceLocation
getPointOfInstantiation() const {
426 return PointOfInstantiation
;
429 /// \brief Set the first point of instantiation.
430 void setPointOfInstantiation(SourceLocation POI
) {
431 PointOfInstantiation
= POI
;
435 /// \brief Provides information about a dependent function-template
436 /// specialization declaration. Since explicit function template
437 /// specialization and instantiation declarations can only appear in
438 /// namespace scope, and you can only specialize a member of a
439 /// fully-specialized class, the only way to get one of these is in
440 /// a friend declaration like the following:
442 /// template <class T> void foo(T);
443 /// template <class T> class A {
444 /// friend void foo<>(T);
446 class DependentFunctionTemplateSpecializationInfo
{
448 // Force sizeof to be a multiple of sizeof(void*) so that the
449 // trailing data is aligned.
453 /// The number of potential template candidates.
454 unsigned NumTemplates
;
456 /// The number of template arguments.
461 /// The locations of the left and right angle brackets.
462 SourceRange AngleLocs
;
464 FunctionTemplateDecl
* const *getTemplates() const {
465 return reinterpret_cast<FunctionTemplateDecl
*const*>(this+1);
468 const TemplateArgumentLoc
*getTemplateArgs() const {
469 return reinterpret_cast<const TemplateArgumentLoc
*>(
470 &getTemplates()[getNumTemplates()]);
474 DependentFunctionTemplateSpecializationInfo(
475 const UnresolvedSetImpl
&Templates
,
476 const TemplateArgumentListInfo
&TemplateArgs
);
478 /// \brief Returns the number of function templates that this might
479 /// be a specialization of.
480 unsigned getNumTemplates() const {
481 return d
.NumTemplates
;
484 /// \brief Returns the i'th template candidate.
485 FunctionTemplateDecl
*getTemplate(unsigned I
) const {
486 assert(I
< getNumTemplates() && "template index out of range");
487 return getTemplates()[I
];
490 /// \brief Returns the number of explicit template arguments that were given.
491 unsigned getNumTemplateArgs() const {
495 /// \brief Returns the nth template argument.
496 const TemplateArgumentLoc
&getTemplateArg(unsigned I
) const {
497 assert(I
< getNumTemplateArgs() && "template arg index out of range");
498 return getTemplateArgs()[I
];
501 SourceLocation
getLAngleLoc() const {
502 return AngleLocs
.getBegin();
505 SourceLocation
getRAngleLoc() const {
506 return AngleLocs
.getEnd();
510 /// Declaration of a redeclarable template.
511 class RedeclarableTemplateDecl
: public TemplateDecl
{
513 RedeclarableTemplateDecl
*getPreviousDeclarationImpl() {
514 return CommonOrPrev
.dyn_cast
<RedeclarableTemplateDecl
*>();
517 RedeclarableTemplateDecl
*getCanonicalDeclImpl();
519 void setPreviousDeclarationImpl(RedeclarableTemplateDecl
*Prev
);
521 RedeclarableTemplateDecl
*getInstantiatedFromMemberTemplateImpl() {
522 return getCommonPtr()->InstantiatedFromMember
.getPointer();
525 void setInstantiatedFromMemberTemplateImpl(RedeclarableTemplateDecl
*TD
) {
526 assert(!getCommonPtr()->InstantiatedFromMember
.getPointer());
527 getCommonPtr()->InstantiatedFromMember
.setPointer(TD
);
531 template <typename EntryType
> struct SpecEntryTraits
{
532 typedef EntryType DeclType
;
534 static DeclType
*getMostRecentDeclaration(EntryType
*D
) {
535 return D
->getMostRecentDeclaration();
539 template <typename EntryType
,
540 typename _SETraits
= SpecEntryTraits
<EntryType
>,
541 typename _DeclType
= typename
_SETraits::DeclType
>
542 class SpecIterator
: public std::iterator
<std::forward_iterator_tag
,
543 _DeclType
*, ptrdiff_t,
544 _DeclType
*, _DeclType
*> {
545 typedef _SETraits SETraits
;
546 typedef _DeclType DeclType
;
548 typedef typename
llvm::FoldingSet
<EntryType
>::iterator SetIteratorType
;
550 SetIteratorType SetIter
;
553 SpecIterator() : SetIter() {}
554 SpecIterator(SetIteratorType SetIter
) : SetIter(SetIter
) {}
556 DeclType
*operator*() const {
557 return SETraits::getMostRecentDeclaration(&*SetIter
);
559 DeclType
*operator->() const { return **this; }
561 SpecIterator
&operator++() { ++SetIter
; return *this; }
562 SpecIterator
operator++(int) {
563 SpecIterator
tmp(*this);
568 bool operator==(SpecIterator Other
) const {
569 return SetIter
== Other
.SetIter
;
571 bool operator!=(SpecIterator Other
) const {
572 return SetIter
!= Other
.SetIter
;
576 template <typename EntryType
>
577 SpecIterator
<EntryType
> makeSpecIterator(llvm::FoldingSet
<EntryType
> &Specs
,
579 return SpecIterator
<EntryType
>(isEnd
? Specs
.end() : Specs
.begin());
582 template <class EntryType
> typename SpecEntryTraits
<EntryType
>::DeclType
*
583 findSpecializationImpl(llvm::FoldingSet
<EntryType
> &Specs
,
584 const TemplateArgument
*Args
, unsigned NumArgs
,
588 CommonBase() : InstantiatedFromMember(0, false) { }
590 /// \brief The template from which this was most
591 /// directly instantiated (or null).
593 /// The boolean value indicates whether this template
594 /// was explicitly specialized.
595 llvm::PointerIntPair
<RedeclarableTemplateDecl
*, 1, bool>
596 InstantiatedFromMember
;
598 /// \brief The latest declaration of this template.
599 RedeclarableTemplateDecl
*Latest
;
602 /// \brief A pointer to the previous declaration (if this is a redeclaration)
603 /// or to the data that is common to all declarations of this template.
604 llvm::PointerUnion
<CommonBase
*, RedeclarableTemplateDecl
*> CommonOrPrev
;
606 /// \brief Retrieves the "common" pointer shared by all (re-)declarations of
607 /// the same template. Calling this routine may implicitly allocate memory
608 /// for the common pointer.
609 CommonBase
*getCommonPtr();
611 virtual CommonBase
*newCommon(ASTContext
&C
) = 0;
613 // Construct a template decl with name, parameters, and templated element.
614 RedeclarableTemplateDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
615 DeclarationName Name
, TemplateParameterList
*Params
,
617 : TemplateDecl(DK
, DC
, L
, Name
, Params
, Decl
),
618 CommonOrPrev((CommonBase
*)0) { }
621 template <class decl_type
> friend class RedeclarableTemplate
;
623 RedeclarableTemplateDecl
*getCanonicalDecl() {
624 return getCanonicalDeclImpl();
627 /// \brief Retrieve the previous declaration of this template, or
628 /// NULL if no such declaration exists.
629 RedeclarableTemplateDecl
*getPreviousDeclaration() {
630 return getPreviousDeclarationImpl();
633 /// \brief Retrieve the previous declaration of this template, or
634 /// NULL if no such declaration exists.
635 const RedeclarableTemplateDecl
*getPreviousDeclaration() const {
637 const_cast<RedeclarableTemplateDecl
*>(this)->getPreviousDeclaration();
640 /// \brief Retrieve the first declaration of this template, or itself
641 /// if this the first one.
642 RedeclarableTemplateDecl
*getFirstDeclaration() {
643 return getCanonicalDecl();
646 /// \brief Retrieve the first declaration of this template, or itself
647 /// if this the first one.
648 const RedeclarableTemplateDecl
*getFirstDeclaration() const {
650 const_cast<RedeclarableTemplateDecl
*>(this)->getFirstDeclaration();
653 /// \brief Retrieve the most recent declaration of this template, or itself
654 /// if this the most recent one.
655 RedeclarableTemplateDecl
*getMostRecentDeclaration() {
656 return getCommonPtr()->Latest
;
659 /// \brief Retrieve the most recent declaration of this template, or itself
660 /// if this the most recent one.
661 const RedeclarableTemplateDecl
*getMostRecentDeclaration() const {
663 const_cast<RedeclarableTemplateDecl
*>(this)->getMostRecentDeclaration();
666 /// \brief Determines whether this template was a specialization of a
669 /// In the following example, the function template \c X<int>::f and the
670 /// member template \c X<int>::Inner are member specializations.
673 /// template<typename T>
675 /// template<typename U> void f(T, U);
676 /// template<typename U> struct Inner;
679 /// template<> template<typename T>
680 /// void X<int>::f(int, T);
681 /// template<> template<typename T>
682 /// struct X<int>::Inner { /* ... */ };
684 bool isMemberSpecialization() {
685 return getCommonPtr()->InstantiatedFromMember
.getInt();
688 /// \brief Note that this member template is a specialization.
689 void setMemberSpecialization() {
690 assert(getCommonPtr()->InstantiatedFromMember
.getPointer() &&
691 "Only member templates can be member template specializations");
692 getCommonPtr()->InstantiatedFromMember
.setInt(true);
695 /// \brief Retrieve the previous declaration of this template, or
696 /// NULL if no such declaration exists.
697 RedeclarableTemplateDecl
*getInstantiatedFromMemberTemplate() {
698 return getInstantiatedFromMemberTemplateImpl();
701 virtual RedeclarableTemplateDecl
*getNextRedeclaration();
703 // Implement isa/cast/dyncast/etc.
704 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
705 static bool classof(const RedeclarableTemplateDecl
*D
) { return true; }
706 static bool classof(const FunctionTemplateDecl
*D
) { return true; }
707 static bool classof(const ClassTemplateDecl
*D
) { return true; }
708 static bool classofKind(Kind K
) {
709 return K
>= firstRedeclarableTemplate
&& K
<= lastRedeclarableTemplate
;
712 friend class ASTDeclReader
;
713 friend class ASTDeclWriter
;
716 template <class decl_type
>
717 class RedeclarableTemplate
{
718 RedeclarableTemplateDecl
*thisDecl() {
719 return static_cast<decl_type
*>(this);
723 /// \brief Retrieve the previous declaration of this function template, or
724 /// NULL if no such declaration exists.
725 decl_type
*getPreviousDeclaration() {
726 return static_cast<decl_type
*>(thisDecl()->getPreviousDeclarationImpl());
729 /// \brief Retrieve the previous declaration of this function template, or
730 /// NULL if no such declaration exists.
731 const decl_type
*getPreviousDeclaration() const {
732 return const_cast<RedeclarableTemplate
*>(this)->getPreviousDeclaration();
735 /// \brief Set the previous declaration of this function template.
736 void setPreviousDeclaration(decl_type
*Prev
) {
737 thisDecl()->setPreviousDeclarationImpl(Prev
);
740 decl_type
*getCanonicalDecl() {
741 return static_cast<decl_type
*>(thisDecl()->getCanonicalDeclImpl());
744 const decl_type
*getCanonicalDecl() const {
745 return const_cast<RedeclarableTemplate
*>(this)->getCanonicalDecl();
748 /// \brief Retrieve the member template that this template was instantiated
751 /// This routine will return non-NULL for member templates of
752 /// class templates. For example, given:
755 /// template <typename T>
757 /// template <typename U> void f();
758 /// template <typename U> struct A {};
762 /// X<int>::f<float> is a CXXMethodDecl (whose parent is X<int>, a
763 /// ClassTemplateSpecializationDecl) for which getPrimaryTemplate() will
764 /// return X<int>::f, a FunctionTemplateDecl (whose parent is again
765 /// X<int>) for which getInstantiatedFromMemberTemplate() will return
766 /// X<T>::f, a FunctionTemplateDecl (whose parent is X<T>, a
767 /// ClassTemplateDecl).
769 /// X<int>::A<float> is a ClassTemplateSpecializationDecl (whose parent
770 /// is X<int>, also a CTSD) for which getSpecializedTemplate() will
771 /// return X<int>::A<U>, a ClassTemplateDecl (whose parent is again
772 /// X<int>) for which getInstantiatedFromMemberTemplate() will return
773 /// X<T>::A<U>, a ClassTemplateDecl (whose parent is X<T>, also a CTD).
775 /// \returns NULL if this is not an instantiation of a member template.
776 decl_type
*getInstantiatedFromMemberTemplate() {
777 return static_cast<decl_type
*>(
778 thisDecl()->getInstantiatedFromMemberTemplateImpl());
781 void setInstantiatedFromMemberTemplate(decl_type
*TD
) {
782 thisDecl()->setInstantiatedFromMemberTemplateImpl(TD
);
786 template <> struct RedeclarableTemplateDecl::
787 SpecEntryTraits
<FunctionTemplateSpecializationInfo
> {
788 typedef FunctionDecl DeclType
;
791 getMostRecentDeclaration(FunctionTemplateSpecializationInfo
*I
) {
792 return I
->Function
->getMostRecentDeclaration();
796 /// Declaration of a template function.
797 class FunctionTemplateDecl
: public RedeclarableTemplateDecl
,
798 public RedeclarableTemplate
<FunctionTemplateDecl
> {
799 static void DeallocateCommon(void *Ptr
);
802 typedef RedeclarableTemplate
<FunctionTemplateDecl
> redeclarable_base
;
804 /// \brief Data that is common to all of the declarations of a given
805 /// function template.
806 struct Common
: CommonBase
{
807 /// \brief The function template specializations for this function
808 /// template, including explicit specializations and instantiations.
809 llvm::FoldingSet
<FunctionTemplateSpecializationInfo
> Specializations
;
812 FunctionTemplateDecl(DeclContext
*DC
, SourceLocation L
, DeclarationName Name
,
813 TemplateParameterList
*Params
, NamedDecl
*Decl
)
814 : RedeclarableTemplateDecl(FunctionTemplate
, DC
, L
, Name
, Params
, Decl
) { }
816 CommonBase
*newCommon(ASTContext
&C
);
818 Common
*getCommonPtr() {
819 return static_cast<Common
*>(RedeclarableTemplateDecl::getCommonPtr());
822 friend class FunctionDecl
;
824 /// \brief Retrieve the set of function template specializations of this
825 /// function template.
826 llvm::FoldingSet
<FunctionTemplateSpecializationInfo
> &getSpecializations() {
827 return getCommonPtr()->Specializations
;
831 /// Get the underlying function declaration of the template.
832 FunctionDecl
*getTemplatedDecl() const {
833 return static_cast<FunctionDecl
*>(TemplatedDecl
);
836 /// Returns whether this template declaration defines the primary
838 bool isThisDeclarationADefinition() const {
839 return getTemplatedDecl()->isThisDeclarationADefinition();
842 /// \brief Return the specialization with the provided arguments if it exists,
843 /// otherwise return the insertion point.
844 FunctionDecl
*findSpecialization(const TemplateArgument
*Args
,
845 unsigned NumArgs
, void *&InsertPos
);
847 FunctionTemplateDecl
*getCanonicalDecl() {
848 return redeclarable_base::getCanonicalDecl();
850 const FunctionTemplateDecl
*getCanonicalDecl() const {
851 return redeclarable_base::getCanonicalDecl();
854 /// \brief Retrieve the previous declaration of this function template, or
855 /// NULL if no such declaration exists.
856 FunctionTemplateDecl
*getPreviousDeclaration() {
857 return redeclarable_base::getPreviousDeclaration();
860 /// \brief Retrieve the previous declaration of this function template, or
861 /// NULL if no such declaration exists.
862 const FunctionTemplateDecl
*getPreviousDeclaration() const {
863 return redeclarable_base::getPreviousDeclaration();
866 FunctionTemplateDecl
*getInstantiatedFromMemberTemplate() {
867 return redeclarable_base::getInstantiatedFromMemberTemplate();
870 typedef SpecIterator
<FunctionTemplateSpecializationInfo
> spec_iterator
;
872 spec_iterator
spec_begin() {
873 return makeSpecIterator(getSpecializations(), false);
876 spec_iterator
spec_end() {
877 return makeSpecIterator(getSpecializations(), true);
880 /// Create a template function node.
881 static FunctionTemplateDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
883 DeclarationName Name
,
884 TemplateParameterList
*Params
,
887 // Implement isa/cast/dyncast support
888 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
889 static bool classof(const FunctionTemplateDecl
*D
) { return true; }
890 static bool classofKind(Kind K
) { return K
== FunctionTemplate
; }
892 friend class ASTDeclReader
;
893 friend class ASTDeclWriter
;
896 //===----------------------------------------------------------------------===//
897 // Kinds of Template Parameters
898 //===----------------------------------------------------------------------===//
900 /// The TemplateParmPosition class defines the position of a template parameter
901 /// within a template parameter list. Because template parameter can be listed
902 /// sequentially for out-of-line template members, each template parameter is
903 /// given a Depth - the nesting of template parameter scopes - and a Position -
904 /// the occurrence within the parameter list.
905 /// This class is inheritedly privately by different kinds of template
906 /// parameters and is not part of the Decl hierarchy. Just a facility.
907 class TemplateParmPosition
{
909 // FIXME: This should probably never be called, but it's here as
910 TemplateParmPosition()
911 : Depth(0), Position(0)
912 { /* assert(0 && "Cannot create positionless template parameter"); */ }
914 TemplateParmPosition(unsigned D
, unsigned P
)
915 : Depth(D
), Position(P
)
918 // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
924 /// Get the nesting depth of the template parameter.
925 unsigned getDepth() const { return Depth
; }
926 void setDepth(unsigned D
) { Depth
= D
; }
928 /// Get the position of the template parameter within its parameter list.
929 unsigned getPosition() const { return Position
; }
930 void setPosition(unsigned P
) { Position
= P
; }
932 /// Get the index of the template parameter within its parameter list.
933 unsigned getIndex() const { return Position
; }
936 /// TemplateTypeParmDecl - Declaration of a template type parameter,
939 /// template<typename T> class vector;
941 class TemplateTypeParmDecl
: public TypeDecl
{
942 /// \brief Whether this template type parameter was declaration with
943 /// the 'typename' keyword. If false, it was declared with the
947 /// \brief Whether this template type parameter inherited its
948 /// default argument.
949 bool InheritedDefault
: 1;
951 /// \brief Whether this is a parameter pack.
952 bool ParameterPack
: 1;
954 /// \brief The default template argument, if any.
955 TypeSourceInfo
*DefaultArgument
;
957 TemplateTypeParmDecl(DeclContext
*DC
, SourceLocation L
, IdentifierInfo
*Id
,
958 bool Typename
, QualType Type
, bool ParameterPack
)
959 : TypeDecl(TemplateTypeParm
, DC
, L
, Id
), Typename(Typename
),
960 InheritedDefault(false), ParameterPack(ParameterPack
), DefaultArgument() {
961 TypeForDecl
= Type
.getTypePtr();
965 static TemplateTypeParmDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
966 SourceLocation L
, unsigned D
, unsigned P
,
967 IdentifierInfo
*Id
, bool Typename
,
969 static TemplateTypeParmDecl
*Create(ASTContext
&C
, EmptyShell Empty
);
971 /// \brief Whether this template type parameter was declared with
972 /// the 'typename' keyword. If not, it was declared with the 'class'
974 bool wasDeclaredWithTypename() const { return Typename
; }
976 /// \brief Determine whether this template parameter has a default
978 bool hasDefaultArgument() const { return DefaultArgument
!= 0; }
980 /// \brief Retrieve the default argument, if any.
981 QualType
getDefaultArgument() const { return DefaultArgument
->getType(); }
983 /// \brief Retrieves the default argument's source information, if any.
984 TypeSourceInfo
*getDefaultArgumentInfo() const { return DefaultArgument
; }
986 /// \brief Retrieves the location of the default argument declaration.
987 SourceLocation
getDefaultArgumentLoc() const;
989 /// \brief Determines whether the default argument was inherited
990 /// from a previous declaration of this template.
991 bool defaultArgumentWasInherited() const { return InheritedDefault
; }
993 /// \brief Set the default argument for this template parameter, and
994 /// whether that default argument was inherited from another
996 void setDefaultArgument(TypeSourceInfo
*DefArg
, bool Inherited
) {
997 DefaultArgument
= DefArg
;
998 InheritedDefault
= Inherited
;
1001 /// \brief Removes the default argument of this template parameter.
1002 void removeDefaultArgument() {
1003 DefaultArgument
= 0;
1004 InheritedDefault
= false;
1007 /// \brief Set whether this template type parameter was declared with
1008 /// the 'typename' or 'class' keyword.
1009 void setDeclaredWithTypename(bool withTypename
) { Typename
= withTypename
; }
1011 /// \brief Set whether this is a parameter pack.
1012 void setParameterPack(bool isParamPack
) { ParameterPack
= isParamPack
; }
1014 /// \brief Retrieve the depth of the template parameter.
1015 unsigned getDepth() const;
1017 /// \brief Retrieve the index of the template parameter.
1018 unsigned getIndex() const;
1020 /// \brief Returns whether this is a parameter pack.
1021 bool isParameterPack() const { return ParameterPack
; }
1023 // Implement isa/cast/dyncast/etc.
1024 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1025 static bool classof(const TemplateTypeParmDecl
*D
) { return true; }
1026 static bool classofKind(Kind K
) { return K
== TemplateTypeParm
; }
1029 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
1032 /// template<int Size> class array { };
1034 class NonTypeTemplateParmDecl
1035 : public VarDecl
, protected TemplateParmPosition
{
1036 /// \brief The default template argument, if any, and whether or not
1037 /// it was inherited.
1038 llvm::PointerIntPair
<Expr
*, 1, bool> DefaultArgumentAndInherited
;
1040 NonTypeTemplateParmDecl(DeclContext
*DC
, SourceLocation L
, unsigned D
,
1041 unsigned P
, IdentifierInfo
*Id
, QualType T
,
1042 TypeSourceInfo
*TInfo
)
1043 : VarDecl(NonTypeTemplateParm
, DC
, L
, Id
, T
, TInfo
, SC_None
, SC_None
),
1044 TemplateParmPosition(D
, P
), DefaultArgumentAndInherited(0, false)
1048 static NonTypeTemplateParmDecl
*
1049 Create(ASTContext
&C
, DeclContext
*DC
, SourceLocation L
, unsigned D
,
1050 unsigned P
, IdentifierInfo
*Id
, QualType T
, TypeSourceInfo
*TInfo
);
1052 using TemplateParmPosition::getDepth
;
1053 using TemplateParmPosition::setDepth
;
1054 using TemplateParmPosition::getPosition
;
1055 using TemplateParmPosition::setPosition
;
1056 using TemplateParmPosition::getIndex
;
1058 /// \brief Determine whether this template parameter has a default
1060 bool hasDefaultArgument() const {
1061 return DefaultArgumentAndInherited
.getPointer() != 0;
1064 /// \brief Retrieve the default argument, if any.
1065 Expr
*getDefaultArgument() const {
1066 return DefaultArgumentAndInherited
.getPointer();
1069 /// \brief Retrieve the location of the default argument, if any.
1070 SourceLocation
getDefaultArgumentLoc() const;
1072 /// \brief Determines whether the default argument was inherited
1073 /// from a previous declaration of this template.
1074 bool defaultArgumentWasInherited() const {
1075 return DefaultArgumentAndInherited
.getInt();
1078 /// \brief Set the default argument for this template parameter, and
1079 /// whether that default argument was inherited from another
1081 void setDefaultArgument(Expr
*DefArg
, bool Inherited
) {
1082 DefaultArgumentAndInherited
.setPointer(DefArg
);
1083 DefaultArgumentAndInherited
.setInt(Inherited
);
1086 /// \brief Removes the default argument of this template parameter.
1087 void removeDefaultArgument() {
1088 DefaultArgumentAndInherited
.setPointer(0);
1089 DefaultArgumentAndInherited
.setInt(false);
1092 // Implement isa/cast/dyncast/etc.
1093 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1094 static bool classof(const NonTypeTemplateParmDecl
*D
) { return true; }
1095 static bool classofKind(Kind K
) { return K
== NonTypeTemplateParm
; }
1098 /// TemplateTemplateParmDecl - Declares a template template parameter,
1101 /// template <template <typename> class T> class container { };
1103 /// A template template parameter is a TemplateDecl because it defines the
1104 /// name of a template and the template parameters allowable for substitution.
1105 class TemplateTemplateParmDecl
1106 : public TemplateDecl
, protected TemplateParmPosition
{
1108 /// DefaultArgument - The default template argument, if any.
1109 TemplateArgumentLoc DefaultArgument
;
1110 /// Whether or not the default argument was inherited.
1111 bool DefaultArgumentWasInherited
;
1113 TemplateTemplateParmDecl(DeclContext
*DC
, SourceLocation L
,
1114 unsigned D
, unsigned P
,
1115 IdentifierInfo
*Id
, TemplateParameterList
*Params
)
1116 : TemplateDecl(TemplateTemplateParm
, DC
, L
, Id
, Params
),
1117 TemplateParmPosition(D
, P
), DefaultArgument(),
1118 DefaultArgumentWasInherited(false)
1122 static TemplateTemplateParmDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
1123 SourceLocation L
, unsigned D
,
1124 unsigned P
, IdentifierInfo
*Id
,
1125 TemplateParameterList
*Params
);
1127 using TemplateParmPosition::getDepth
;
1128 using TemplateParmPosition::getPosition
;
1129 using TemplateParmPosition::getIndex
;
1131 /// \brief Determine whether this template parameter has a default
1133 bool hasDefaultArgument() const {
1134 return !DefaultArgument
.getArgument().isNull();
1137 /// \brief Retrieve the default argument, if any.
1138 const TemplateArgumentLoc
&getDefaultArgument() const {
1139 return DefaultArgument
;
1142 /// \brief Retrieve the location of the default argument, if any.
1143 SourceLocation
getDefaultArgumentLoc() const;
1145 /// \brief Determines whether the default argument was inherited
1146 /// from a previous declaration of this template.
1147 bool defaultArgumentWasInherited() const {
1148 return DefaultArgumentWasInherited
;
1151 /// \brief Set the default argument for this template parameter, and
1152 /// whether that default argument was inherited from another
1154 void setDefaultArgument(const TemplateArgumentLoc
&DefArg
, bool Inherited
) {
1155 DefaultArgument
= DefArg
;
1156 DefaultArgumentWasInherited
= Inherited
;
1159 /// \brief Removes the default argument of this template parameter.
1160 void removeDefaultArgument() {
1161 DefaultArgument
= TemplateArgumentLoc();
1162 DefaultArgumentWasInherited
= false;
1165 SourceRange
getSourceRange() const {
1166 SourceLocation End
= getLocation();
1167 if (hasDefaultArgument() && !defaultArgumentWasInherited())
1168 End
= getDefaultArgument().getSourceRange().getEnd();
1169 return SourceRange(getTemplateParameters()->getTemplateLoc(), End
);
1172 // Implement isa/cast/dyncast/etc.
1173 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1174 static bool classof(const TemplateTemplateParmDecl
*D
) { return true; }
1175 static bool classofKind(Kind K
) { return K
== TemplateTemplateParm
; }
1177 friend class ASTDeclReader
;
1178 friend class ASTDeclWriter
;
1181 /// \brief Represents a class template specialization, which refers to
1182 /// a class template with a given set of template arguments.
1184 /// Class template specializations represent both explicit
1185 /// specialization of class templates, as in the example below, and
1186 /// implicit instantiations of class templates.
1189 /// template<typename T> class array;
1192 /// class array<bool> { }; // class template specialization array<bool>
1194 class ClassTemplateSpecializationDecl
1195 : public CXXRecordDecl
, public llvm::FoldingSetNode
{
1197 /// \brief Structure that stores information about a class template
1198 /// specialization that was instantiated from a class template partial
1200 struct SpecializedPartialSpecialization
{
1201 /// \brief The class template partial specialization from which this
1202 /// class template specialization was instantiated.
1203 ClassTemplatePartialSpecializationDecl
*PartialSpecialization
;
1205 /// \brief The template argument list deduced for the class template
1206 /// partial specialization itself.
1207 TemplateArgumentList
*TemplateArgs
;
1210 /// \brief The template that this specialization specializes
1211 llvm::PointerUnion
<ClassTemplateDecl
*, SpecializedPartialSpecialization
*>
1212 SpecializedTemplate
;
1214 /// \brief Further info for explicit template specialization/instantiation.
1215 struct ExplicitSpecializationInfo
{
1216 /// \brief The type-as-written.
1217 TypeSourceInfo
*TypeAsWritten
;
1218 /// \brief The location of the extern keyword.
1219 SourceLocation ExternLoc
;
1220 /// \brief The location of the template keyword.
1221 SourceLocation TemplateKeywordLoc
;
1223 ExplicitSpecializationInfo()
1224 : TypeAsWritten(0), ExternLoc(), TemplateKeywordLoc() {}
1227 /// \brief Further info for explicit template specialization/instantiation.
1228 /// Does not apply to implicit specializations.
1229 ExplicitSpecializationInfo
*ExplicitInfo
;
1231 /// \brief The template arguments used to describe this specialization.
1232 TemplateArgumentList TemplateArgs
;
1234 /// \brief The point where this template was instantiated (if any)
1235 SourceLocation PointOfInstantiation
;
1237 /// \brief The kind of specialization this declaration refers to.
1238 /// Really a value of type TemplateSpecializationKind.
1239 unsigned SpecializationKind
: 3;
1242 ClassTemplateSpecializationDecl(ASTContext
&Context
, Kind DK
, TagKind TK
,
1243 DeclContext
*DC
, SourceLocation L
,
1244 ClassTemplateDecl
*SpecializedTemplate
,
1245 TemplateArgumentListBuilder
&Builder
,
1246 ClassTemplateSpecializationDecl
*PrevDecl
);
1248 explicit ClassTemplateSpecializationDecl(Kind DK
);
1251 static ClassTemplateSpecializationDecl
*
1252 Create(ASTContext
&Context
, TagKind TK
, DeclContext
*DC
, SourceLocation L
,
1253 ClassTemplateDecl
*SpecializedTemplate
,
1254 TemplateArgumentListBuilder
&Builder
,
1255 ClassTemplateSpecializationDecl
*PrevDecl
);
1256 static ClassTemplateSpecializationDecl
*
1257 Create(ASTContext
&Context
, EmptyShell Empty
);
1259 virtual void getNameForDiagnostic(std::string
&S
,
1260 const PrintingPolicy
&Policy
,
1261 bool Qualified
) const;
1263 ClassTemplateSpecializationDecl
*getMostRecentDeclaration() {
1264 CXXRecordDecl
*Recent
1265 = cast
<CXXRecordDecl
>(CXXRecordDecl::getMostRecentDeclaration());
1266 if (!isa
<ClassTemplateSpecializationDecl
>(Recent
)) {
1267 // FIXME: Does injected class name need to be in the redeclarations chain?
1268 assert(Recent
->isInjectedClassName() && Recent
->getPreviousDeclaration());
1269 Recent
= Recent
->getPreviousDeclaration();
1271 return cast
<ClassTemplateSpecializationDecl
>(Recent
);
1274 /// \brief Retrieve the template that this specialization specializes.
1275 ClassTemplateDecl
*getSpecializedTemplate() const;
1277 /// \brief Retrieve the template arguments of the class template
1279 const TemplateArgumentList
&getTemplateArgs() const {
1280 return TemplateArgs
;
1283 /// \brief Determine the kind of specialization that this
1284 /// declaration represents.
1285 TemplateSpecializationKind
getSpecializationKind() const {
1286 return static_cast<TemplateSpecializationKind
>(SpecializationKind
);
1289 void setSpecializationKind(TemplateSpecializationKind TSK
) {
1290 SpecializationKind
= TSK
;
1293 /// \brief Get the point of instantiation (if any), or null if none.
1294 SourceLocation
getPointOfInstantiation() const {
1295 return PointOfInstantiation
;
1298 void setPointOfInstantiation(SourceLocation Loc
) {
1299 assert(Loc
.isValid() && "point of instantiation must be valid!");
1300 PointOfInstantiation
= Loc
;
1303 /// \brief If this class template specialization is an instantiation of
1304 /// a template (rather than an explicit specialization), return the
1305 /// class template or class template partial specialization from which it
1306 /// was instantiated.
1307 llvm::PointerUnion
<ClassTemplateDecl
*,
1308 ClassTemplatePartialSpecializationDecl
*>
1309 getInstantiatedFrom() const {
1310 if (getSpecializationKind() != TSK_ImplicitInstantiation
&&
1311 getSpecializationKind() != TSK_ExplicitInstantiationDefinition
&&
1312 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration
)
1313 return llvm::PointerUnion
<ClassTemplateDecl
*,
1314 ClassTemplatePartialSpecializationDecl
*>();
1316 if (SpecializedPartialSpecialization
*PartialSpec
1317 = SpecializedTemplate
.dyn_cast
<SpecializedPartialSpecialization
*>())
1318 return PartialSpec
->PartialSpecialization
;
1320 return const_cast<ClassTemplateDecl
*>(
1321 SpecializedTemplate
.get
<ClassTemplateDecl
*>());
1324 /// \brief Retrieve the class template or class template partial
1325 /// specialization which was specialized by this.
1326 llvm::PointerUnion
<ClassTemplateDecl
*,
1327 ClassTemplatePartialSpecializationDecl
*>
1328 getSpecializedTemplateOrPartial() const {
1329 if (SpecializedPartialSpecialization
*PartialSpec
1330 = SpecializedTemplate
.dyn_cast
<SpecializedPartialSpecialization
*>())
1331 return PartialSpec
->PartialSpecialization
;
1333 return const_cast<ClassTemplateDecl
*>(
1334 SpecializedTemplate
.get
<ClassTemplateDecl
*>());
1337 /// \brief Retrieve the set of template arguments that should be used
1338 /// to instantiate members of the class template or class template partial
1339 /// specialization from which this class template specialization was
1342 /// \returns For a class template specialization instantiated from the primary
1343 /// template, this function will return the same template arguments as
1344 /// getTemplateArgs(). For a class template specialization instantiated from
1345 /// a class template partial specialization, this function will return the
1346 /// deduced template arguments for the class template partial specialization
1348 const TemplateArgumentList
&getTemplateInstantiationArgs() const {
1349 if (SpecializedPartialSpecialization
*PartialSpec
1350 = SpecializedTemplate
.dyn_cast
<SpecializedPartialSpecialization
*>())
1351 return *PartialSpec
->TemplateArgs
;
1353 return getTemplateArgs();
1356 /// \brief Note that this class template specialization is actually an
1357 /// instantiation of the given class template partial specialization whose
1358 /// template arguments have been deduced.
1359 void setInstantiationOf(ClassTemplatePartialSpecializationDecl
*PartialSpec
,
1360 TemplateArgumentList
*TemplateArgs
) {
1361 assert(!SpecializedTemplate
.is
<SpecializedPartialSpecialization
*>() &&
1362 "Already set to a class template partial specialization!");
1363 SpecializedPartialSpecialization
*PS
1364 = new (getASTContext()) SpecializedPartialSpecialization();
1365 PS
->PartialSpecialization
= PartialSpec
;
1366 PS
->TemplateArgs
= TemplateArgs
;
1367 SpecializedTemplate
= PS
;
1370 /// \brief Note that this class template specialization is an instantiation
1371 /// of the given class template.
1372 void setInstantiationOf(ClassTemplateDecl
*TemplDecl
) {
1373 assert(!SpecializedTemplate
.is
<SpecializedPartialSpecialization
*>() &&
1374 "Previously set to a class template partial specialization!");
1375 SpecializedTemplate
= TemplDecl
;
1378 /// \brief Sets the type of this specialization as it was written by
1379 /// the user. This will be a class template specialization type.
1380 void setTypeAsWritten(TypeSourceInfo
*T
) {
1382 ExplicitInfo
= new (getASTContext()) ExplicitSpecializationInfo
;
1383 ExplicitInfo
->TypeAsWritten
= T
;
1385 /// \brief Gets the type of this specialization as it was written by
1386 /// the user, if it was so written.
1387 TypeSourceInfo
*getTypeAsWritten() const {
1388 return ExplicitInfo
? ExplicitInfo
->TypeAsWritten
: 0;
1391 /// \brief Gets the location of the extern keyword, if present.
1392 SourceLocation
getExternLoc() const {
1393 return ExplicitInfo
? ExplicitInfo
->ExternLoc
: SourceLocation();
1395 /// \brief Sets the location of the extern keyword.
1396 void setExternLoc(SourceLocation Loc
) {
1398 ExplicitInfo
= new (getASTContext()) ExplicitSpecializationInfo
;
1399 ExplicitInfo
->ExternLoc
= Loc
;
1402 /// \brief Sets the location of the template keyword.
1403 void setTemplateKeywordLoc(SourceLocation Loc
) {
1405 ExplicitInfo
= new (getASTContext()) ExplicitSpecializationInfo
;
1406 ExplicitInfo
->TemplateKeywordLoc
= Loc
;
1408 /// \brief Gets the location of the template keyword, if present.
1409 SourceLocation
getTemplateKeywordLoc() const {
1410 return ExplicitInfo
? ExplicitInfo
->TemplateKeywordLoc
: SourceLocation();
1413 SourceLocation
getInnerLocStart() const { return getTemplateKeywordLoc(); }
1415 void Profile(llvm::FoldingSetNodeID
&ID
) const {
1416 Profile(ID
, TemplateArgs
.getFlatArgumentList(), TemplateArgs
.flat_size(),
1421 Profile(llvm::FoldingSetNodeID
&ID
, const TemplateArgument
*TemplateArgs
,
1422 unsigned NumTemplateArgs
, ASTContext
&Context
) {
1423 ID
.AddInteger(NumTemplateArgs
);
1424 for (unsigned Arg
= 0; Arg
!= NumTemplateArgs
; ++Arg
)
1425 TemplateArgs
[Arg
].Profile(ID
, Context
);
1428 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1429 static bool classofKind(Kind K
) {
1430 return K
>= firstClassTemplateSpecialization
&&
1431 K
<= lastClassTemplateSpecialization
;
1434 static bool classof(const ClassTemplateSpecializationDecl
*) {
1438 static bool classof(const ClassTemplatePartialSpecializationDecl
*) {
1442 friend class ASTDeclReader
;
1443 friend class ASTDeclWriter
;
1446 class ClassTemplatePartialSpecializationDecl
1447 : public ClassTemplateSpecializationDecl
{
1448 /// \brief The list of template parameters
1449 TemplateParameterList
* TemplateParams
;
1451 /// \brief The source info for the template arguments as written.
1452 /// FIXME: redundant with TypeAsWritten?
1453 TemplateArgumentLoc
*ArgsAsWritten
;
1454 unsigned NumArgsAsWritten
;
1456 /// \brief Sequence number indicating when this class template partial
1457 /// specialization was added to the set of partial specializations for
1458 /// its owning class template.
1459 unsigned SequenceNumber
;
1461 /// \brief The class template partial specialization from which this
1462 /// class template partial specialization was instantiated.
1464 /// The boolean value will be true to indicate that this class template
1465 /// partial specialization was specialized at this level.
1466 llvm::PointerIntPair
<ClassTemplatePartialSpecializationDecl
*, 1, bool>
1467 InstantiatedFromMember
;
1469 ClassTemplatePartialSpecializationDecl(ASTContext
&Context
, TagKind TK
,
1470 DeclContext
*DC
, SourceLocation L
,
1471 TemplateParameterList
*Params
,
1472 ClassTemplateDecl
*SpecializedTemplate
,
1473 TemplateArgumentListBuilder
&Builder
,
1474 TemplateArgumentLoc
*ArgInfos
,
1475 unsigned NumArgInfos
,
1476 ClassTemplatePartialSpecializationDecl
*PrevDecl
,
1477 unsigned SequenceNumber
)
1478 : ClassTemplateSpecializationDecl(Context
,
1479 ClassTemplatePartialSpecialization
,
1480 TK
, DC
, L
, SpecializedTemplate
, Builder
,
1482 TemplateParams(Params
), ArgsAsWritten(ArgInfos
),
1483 NumArgsAsWritten(NumArgInfos
), SequenceNumber(SequenceNumber
),
1484 InstantiatedFromMember(0, false) { }
1486 ClassTemplatePartialSpecializationDecl()
1487 : ClassTemplateSpecializationDecl(ClassTemplatePartialSpecialization
),
1488 TemplateParams(0), ArgsAsWritten(0),
1489 NumArgsAsWritten(0), SequenceNumber(0),
1490 InstantiatedFromMember(0, false) { }
1493 static ClassTemplatePartialSpecializationDecl
*
1494 Create(ASTContext
&Context
, TagKind TK
,DeclContext
*DC
, SourceLocation L
,
1495 TemplateParameterList
*Params
,
1496 ClassTemplateDecl
*SpecializedTemplate
,
1497 TemplateArgumentListBuilder
&Builder
,
1498 const TemplateArgumentListInfo
&ArgInfos
,
1499 QualType CanonInjectedType
,
1500 ClassTemplatePartialSpecializationDecl
*PrevDecl
,
1501 unsigned SequenceNumber
);
1503 static ClassTemplatePartialSpecializationDecl
*
1504 Create(ASTContext
&Context
, EmptyShell Empty
);
1506 ClassTemplatePartialSpecializationDecl
*getMostRecentDeclaration() {
1507 return cast
<ClassTemplatePartialSpecializationDecl
>(
1508 ClassTemplateSpecializationDecl::getMostRecentDeclaration());
1511 /// Get the list of template parameters
1512 TemplateParameterList
*getTemplateParameters() const {
1513 return TemplateParams
;
1516 /// Get the template arguments as written.
1517 TemplateArgumentLoc
*getTemplateArgsAsWritten() const {
1518 return ArgsAsWritten
;
1521 /// Get the number of template arguments as written.
1522 unsigned getNumTemplateArgsAsWritten() const {
1523 return NumArgsAsWritten
;
1526 /// \brief Get the sequence number for this class template partial
1528 unsigned getSequenceNumber() const { return SequenceNumber
; }
1530 /// \brief Retrieve the member class template partial specialization from
1531 /// which this particular class template partial specialization was
1535 /// template<typename T>
1537 /// template<typename U> struct Inner;
1538 /// template<typename U> struct Inner<U*> { }; // #1
1541 /// Outer<float>::Inner<int*> ii;
1544 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
1545 /// end up instantiating the partial specialization
1546 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
1547 /// template partial specialization \c Outer<T>::Inner<U*>. Given
1548 /// \c Outer<float>::Inner<U*>, this function would return
1549 /// \c Outer<T>::Inner<U*>.
1550 ClassTemplatePartialSpecializationDecl
*getInstantiatedFromMember() {
1551 ClassTemplatePartialSpecializationDecl
*First
1552 = cast
<ClassTemplatePartialSpecializationDecl
>(getFirstDeclaration());
1553 return First
->InstantiatedFromMember
.getPointer();
1556 void setInstantiatedFromMember(
1557 ClassTemplatePartialSpecializationDecl
*PartialSpec
) {
1558 ClassTemplatePartialSpecializationDecl
*First
1559 = cast
<ClassTemplatePartialSpecializationDecl
>(getFirstDeclaration());
1560 First
->InstantiatedFromMember
.setPointer(PartialSpec
);
1563 /// \brief Determines whether this class template partial specialization
1564 /// template was a specialization of a member partial specialization.
1566 /// In the following example, the member template partial specialization
1567 /// \c X<int>::Inner<T*> is a member specialization.
1570 /// template<typename T>
1572 /// template<typename U> struct Inner;
1573 /// template<typename U> struct Inner<U*>;
1576 /// template<> template<typename T>
1577 /// struct X<int>::Inner<T*> { /* ... */ };
1579 bool isMemberSpecialization() {
1580 ClassTemplatePartialSpecializationDecl
*First
1581 = cast
<ClassTemplatePartialSpecializationDecl
>(getFirstDeclaration());
1582 return First
->InstantiatedFromMember
.getInt();
1585 /// \brief Note that this member template is a specialization.
1586 void setMemberSpecialization() {
1587 ClassTemplatePartialSpecializationDecl
*First
1588 = cast
<ClassTemplatePartialSpecializationDecl
>(getFirstDeclaration());
1589 assert(First
->InstantiatedFromMember
.getPointer() &&
1590 "Only member templates can be member template specializations");
1591 return First
->InstantiatedFromMember
.setInt(true);
1594 /// Retrieves the injected specialization type for this partial
1595 /// specialization. This is not the same as the type-decl-type for
1596 /// this partial specialization, which is an InjectedClassNameType.
1597 QualType
getInjectedSpecializationType() const {
1598 assert(getTypeForDecl() && "partial specialization has no type set!");
1599 return cast
<InjectedClassNameType
>(getTypeForDecl())
1600 ->getInjectedSpecializationType();
1603 // FIXME: Add Profile support!
1605 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1606 static bool classofKind(Kind K
) {
1607 return K
== ClassTemplatePartialSpecialization
;
1610 static bool classof(const ClassTemplatePartialSpecializationDecl
*) {
1614 friend class ASTDeclReader
;
1615 friend class ASTDeclWriter
;
1618 /// Declaration of a class template.
1619 class ClassTemplateDecl
: public RedeclarableTemplateDecl
,
1620 public RedeclarableTemplate
<ClassTemplateDecl
> {
1621 static void DeallocateCommon(void *Ptr
);
1624 typedef RedeclarableTemplate
<ClassTemplateDecl
> redeclarable_base
;
1626 /// \brief Data that is common to all of the declarations of a given
1628 struct Common
: CommonBase
{
1629 Common() : LazySpecializations() { }
1631 /// \brief The class template specializations for this class
1632 /// template, including explicit specializations and instantiations.
1633 llvm::FoldingSet
<ClassTemplateSpecializationDecl
> Specializations
;
1635 /// \brief The class template partial specializations for this class
1637 llvm::FoldingSet
<ClassTemplatePartialSpecializationDecl
>
1638 PartialSpecializations
;
1640 /// \brief The injected-class-name type for this class template.
1641 QualType InjectedClassNameType
;
1643 /// \brief If non-null, points to an array of specializations (including
1644 /// partial specializations) known ownly by their external declaration IDs.
1646 /// The first value in the array is the number of of specializations/
1647 /// partial specializations that follow.
1648 uint32_t *LazySpecializations
;
1651 /// \brief Load any lazily-loaded specializations from the external source.
1652 void LoadLazySpecializations();
1654 /// \brief Retrieve the set of specializations of this class template.
1655 llvm::FoldingSet
<ClassTemplateSpecializationDecl
> &getSpecializations();
1657 /// \brief Retrieve the set of partial specializations of this class
1659 llvm::FoldingSet
<ClassTemplatePartialSpecializationDecl
> &
1660 getPartialSpecializations();
1662 ClassTemplateDecl(DeclContext
*DC
, SourceLocation L
, DeclarationName Name
,
1663 TemplateParameterList
*Params
, NamedDecl
*Decl
)
1664 : RedeclarableTemplateDecl(ClassTemplate
, DC
, L
, Name
, Params
, Decl
) { }
1666 CommonBase
*newCommon(ASTContext
&C
);
1668 Common
*getCommonPtr() {
1669 return static_cast<Common
*>(RedeclarableTemplateDecl::getCommonPtr());
1673 /// Get the underlying class declarations of the template.
1674 CXXRecordDecl
*getTemplatedDecl() const {
1675 return static_cast<CXXRecordDecl
*>(TemplatedDecl
);
1678 /// Returns whether this template declaration defines the primary
1680 bool isThisDeclarationADefinition() const {
1681 return getTemplatedDecl()->isThisDeclarationADefinition();
1684 /// Create a class template node.
1685 static ClassTemplateDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
1687 DeclarationName Name
,
1688 TemplateParameterList
*Params
,
1690 ClassTemplateDecl
*PrevDecl
);
1692 /// \brief Return the specialization with the provided arguments if it exists,
1693 /// otherwise return the insertion point.
1694 ClassTemplateSpecializationDecl
*
1695 findSpecialization(const TemplateArgument
*Args
, unsigned NumArgs
,
1698 /// \brief Insert the specified specialization knowing that it is not already
1699 /// in. InsertPos must be obtained from findSpecialization.
1700 void AddSpecialization(ClassTemplateSpecializationDecl
*D
, void *InsertPos
);
1702 ClassTemplateDecl
*getCanonicalDecl() {
1703 return redeclarable_base::getCanonicalDecl();
1705 const ClassTemplateDecl
*getCanonicalDecl() const {
1706 return redeclarable_base::getCanonicalDecl();
1709 /// \brief Retrieve the previous declaration of this class template, or
1710 /// NULL if no such declaration exists.
1711 ClassTemplateDecl
*getPreviousDeclaration() {
1712 return redeclarable_base::getPreviousDeclaration();
1715 /// \brief Retrieve the previous declaration of this class template, or
1716 /// NULL if no such declaration exists.
1717 const ClassTemplateDecl
*getPreviousDeclaration() const {
1718 return redeclarable_base::getPreviousDeclaration();
1721 ClassTemplateDecl
*getInstantiatedFromMemberTemplate() {
1722 return redeclarable_base::getInstantiatedFromMemberTemplate();
1725 /// \brief Return the partial specialization with the provided arguments if it
1726 /// exists, otherwise return the insertion point.
1727 ClassTemplatePartialSpecializationDecl
*
1728 findPartialSpecialization(const TemplateArgument
*Args
, unsigned NumArgs
,
1731 /// \brief Insert the specified partial specialization knowing that it is not
1732 /// already in. InsertPos must be obtained from findPartialSpecialization.
1733 void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl
*D
,
1736 /// \brief Return the next partial specialization sequence number.
1737 unsigned getNextPartialSpecSequenceNumber() {
1738 return getPartialSpecializations().size();
1741 /// \brief Retrieve the partial specializations as an ordered list.
1742 void getPartialSpecializations(
1743 llvm::SmallVectorImpl
<ClassTemplatePartialSpecializationDecl
*> &PS
);
1745 /// \brief Find a class template partial specialization with the given
1748 /// \param T a dependent type that names a specialization of this class
1751 /// \returns the class template partial specialization that exactly matches
1752 /// the type \p T, or NULL if no such partial specialization exists.
1753 ClassTemplatePartialSpecializationDecl
*findPartialSpecialization(QualType T
);
1755 /// \brief Find a class template partial specialization which was instantiated
1756 /// from the given member partial specialization.
1758 /// \param D a member class template partial specialization.
1760 /// \returns the class template partial specialization which was instantiated
1761 /// from the given member partial specialization, or NULL if no such partial
1762 /// specialization exists.
1763 ClassTemplatePartialSpecializationDecl
*
1764 findPartialSpecInstantiatedFromMember(
1765 ClassTemplatePartialSpecializationDecl
*D
);
1767 /// \brief Retrieve the template specialization type of the
1768 /// injected-class-name for this class template.
1770 /// The injected-class-name for a class template \c X is \c
1771 /// X<template-args>, where \c template-args is formed from the
1772 /// template arguments that correspond to the template parameters of
1773 /// \c X. For example:
1776 /// template<typename T, int N>
1778 /// typedef array this_type; // "array" is equivalent to "array<T, N>"
1781 QualType
getInjectedClassNameSpecialization();
1783 typedef SpecIterator
<ClassTemplateSpecializationDecl
> spec_iterator
;
1785 spec_iterator
spec_begin() {
1786 return makeSpecIterator(getSpecializations(), false);
1789 spec_iterator
spec_end() {
1790 return makeSpecIterator(getSpecializations(), true);
1793 typedef SpecIterator
<ClassTemplatePartialSpecializationDecl
>
1794 partial_spec_iterator
;
1796 partial_spec_iterator
partial_spec_begin() {
1797 return makeSpecIterator(getPartialSpecializations(), false);
1800 partial_spec_iterator
partial_spec_end() {
1801 return makeSpecIterator(getPartialSpecializations(), true);
1804 // Implement isa/cast/dyncast support
1805 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1806 static bool classof(const ClassTemplateDecl
*D
) { return true; }
1807 static bool classofKind(Kind K
) { return K
== ClassTemplate
; }
1809 friend class ASTDeclReader
;
1810 friend class ASTDeclWriter
;
1813 /// Declaration of a friend template. For example:
1815 /// template <typename T> class A {
1816 /// friend class MyVector<T>; // not a friend template
1817 /// template <typename U> friend class B; // not a friend template
1818 /// template <typename U> friend class Foo<T>::Nested; // friend template
1820 /// NOTE: This class is not currently in use. All of the above
1821 /// will yield a FriendDecl, not a FriendTemplateDecl.
1822 class FriendTemplateDecl
: public Decl
{
1824 typedef llvm::PointerUnion
<NamedDecl
*,TypeSourceInfo
*> FriendUnion
;
1827 // The number of template parameters; always non-zero.
1830 // The parameter list.
1831 TemplateParameterList
**Params
;
1833 // The declaration that's a friend of this class.
1836 // Location of the 'friend' specifier.
1837 SourceLocation FriendLoc
;
1840 FriendTemplateDecl(DeclContext
*DC
, SourceLocation Loc
,
1842 TemplateParameterList
**Params
,
1844 SourceLocation FriendLoc
)
1845 : Decl(Decl::FriendTemplate
, DC
, Loc
),
1849 FriendLoc(FriendLoc
)
1852 FriendTemplateDecl(EmptyShell Empty
)
1853 : Decl(Decl::FriendTemplate
, Empty
),
1859 static FriendTemplateDecl
*Create(ASTContext
&Context
,
1860 DeclContext
*DC
, SourceLocation Loc
,
1862 TemplateParameterList
**Params
,
1864 SourceLocation FriendLoc
);
1866 static FriendTemplateDecl
*Create(ASTContext
&Context
, EmptyShell Empty
);
1868 /// If this friend declaration names a templated type (or
1869 /// a dependent member type of a templated type), return that
1870 /// type; otherwise return null.
1871 TypeSourceInfo
*getFriendType() const {
1872 return Friend
.dyn_cast
<TypeSourceInfo
*>();
1875 /// If this friend declaration names a templated function (or
1876 /// a member function of a templated type), return that type;
1877 /// otherwise return null.
1878 NamedDecl
*getFriendDecl() const {
1879 return Friend
.dyn_cast
<NamedDecl
*>();
1882 /// Retrieves the location of the 'friend' keyword.
1883 SourceLocation
getFriendLoc() const {
1887 TemplateParameterList
*getTemplateParameterList(unsigned i
) const {
1888 assert(i
<= NumParams
);
1892 unsigned getNumTemplateParameters() const {
1896 // Implement isa/cast/dyncast/etc.
1897 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1898 static bool classofKind(Kind K
) { return K
== Decl::FriendTemplate
; }
1899 static bool classof(const FriendTemplateDecl
*D
) { return true; }
1901 friend class ASTDeclReader
;
1904 /// Implementation of inline functions that require the template declarations
1905 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl
*FTD
)
1908 } /* end of namespace clang */