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 //===----------------------------------------------------------------------===//
11 /// \brief Defines the C++ template declaration subclasses.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_DECLTEMPLATE_H
16 #define LLVM_CLANG_AST_DECLTEMPLATE_H
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/Redeclarable.h"
20 #include "clang/AST/TemplateBase.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/Support/Compiler.h"
27 class TemplateParameterList;
29 class RedeclarableTemplateDecl;
30 class FunctionTemplateDecl;
31 class ClassTemplateDecl;
32 class ClassTemplatePartialSpecializationDecl;
33 class TemplateTypeParmDecl;
34 class NonTypeTemplateParmDecl;
35 class TemplateTemplateParmDecl;
36 class TypeAliasTemplateDecl;
37 class VarTemplateDecl;
38 class VarTemplatePartialSpecializationDecl;
40 /// \brief Stores a template parameter of any kind.
41 typedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*,
42 TemplateTemplateParmDecl*> TemplateParameter;
44 /// \brief Stores a list of template parameters for a TemplateDecl and its
46 class TemplateParameterList {
47 /// The location of the 'template' keyword.
48 SourceLocation TemplateLoc;
50 /// The locations of the '<' and '>' angle brackets.
51 SourceLocation LAngleLoc, RAngleLoc;
53 /// The number of template parameters in this template
55 unsigned NumParams : 31;
57 /// Whether this template parameter list contains an unexpanded parameter
59 unsigned ContainsUnexpandedParameterPack : 1;
62 TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc,
63 NamedDecl **Params, unsigned NumParams,
64 SourceLocation RAngleLoc);
67 static TemplateParameterList *Create(const ASTContext &C,
68 SourceLocation TemplateLoc,
69 SourceLocation LAngleLoc,
72 SourceLocation RAngleLoc);
74 /// \brief Iterates through the template parameters in this list.
75 typedef NamedDecl** iterator;
77 /// \brief Iterates through the template parameters in this list.
78 typedef NamedDecl* const* const_iterator;
80 iterator begin() { return reinterpret_cast<NamedDecl **>(this + 1); }
81 const_iterator begin() const {
82 return reinterpret_cast<NamedDecl * const *>(this + 1);
84 iterator end() { return begin() + NumParams; }
85 const_iterator end() const { return begin() + NumParams; }
87 unsigned size() const { return NumParams; }
89 llvm::ArrayRef<NamedDecl*> asArray() {
90 return llvm::ArrayRef<NamedDecl*>(begin(), size());
92 llvm::ArrayRef<const NamedDecl*> asArray() const {
93 return llvm::ArrayRef<const NamedDecl*>(begin(), size());
96 NamedDecl* getParam(unsigned Idx) {
97 assert(Idx < size() && "Template parameter index out-of-range");
101 const NamedDecl* getParam(unsigned Idx) const {
102 assert(Idx < size() && "Template parameter index out-of-range");
106 /// \brief Returns the minimum number of arguments needed to form a
107 /// template specialization.
109 /// This may be fewer than the number of template parameters, if some of
110 /// the parameters have default arguments or if there is a parameter pack.
111 unsigned getMinRequiredArguments() const;
113 /// \brief Get the depth of this template parameter list in the set of
114 /// template parameter lists.
116 /// The first template parameter list in a declaration will have depth 0,
117 /// the second template parameter list will have depth 1, etc.
118 unsigned getDepth() const;
120 /// \brief Determine whether this template parameter list contains an
121 /// unexpanded parameter pack.
122 bool containsUnexpandedParameterPack() const {
123 return ContainsUnexpandedParameterPack;
126 SourceLocation getTemplateLoc() const { return TemplateLoc; }
127 SourceLocation getLAngleLoc() const { return LAngleLoc; }
128 SourceLocation getRAngleLoc() const { return RAngleLoc; }
130 SourceRange getSourceRange() const LLVM_READONLY {
131 return SourceRange(TemplateLoc, RAngleLoc);
135 /// \brief Stores a list of template parameters for a TemplateDecl and its
136 /// derived classes. Suitable for creating on the stack.
138 class FixedSizeTemplateParameterList : public TemplateParameterList {
139 NamedDecl *Params[N];
142 FixedSizeTemplateParameterList(SourceLocation TemplateLoc,
143 SourceLocation LAngleLoc,
144 NamedDecl **Params, SourceLocation RAngleLoc) :
145 TemplateParameterList(TemplateLoc, LAngleLoc, Params, N, RAngleLoc) {
149 /// \brief A template argument list.
150 class TemplateArgumentList {
151 /// \brief The template argument list.
153 /// The integer value will be non-zero to indicate that this
154 /// template argument list does own the pointer.
155 llvm::PointerIntPair<const TemplateArgument *, 1> Arguments;
157 /// \brief The number of template arguments in this template
159 unsigned NumArguments;
161 TemplateArgumentList(const TemplateArgumentList &Other) LLVM_DELETED_FUNCTION;
162 void operator=(const TemplateArgumentList &Other) LLVM_DELETED_FUNCTION;
164 TemplateArgumentList(const TemplateArgument *Args, unsigned NumArgs,
166 : Arguments(Args, Owned), NumArguments(NumArgs) { }
169 /// \brief Type used to indicate that the template argument list itself is a
170 /// stack object. It does not own its template arguments.
171 enum OnStackType { OnStack };
173 /// \brief Create a new template argument list that copies the given set of
174 /// template arguments.
175 static TemplateArgumentList *CreateCopy(ASTContext &Context,
176 const TemplateArgument *Args,
179 /// \brief Construct a new, temporary template argument list on the stack.
181 /// The template argument list does not own the template arguments
183 explicit TemplateArgumentList(OnStackType,
184 const TemplateArgument *Args, unsigned NumArgs)
185 : Arguments(Args, false), NumArguments(NumArgs) { }
187 /// \brief Produces a shallow copy of the given template argument list.
189 /// This operation assumes that the input argument list outlives it.
190 /// This takes the list as a pointer to avoid looking like a copy
191 /// constructor, since this really really isn't safe to use that
193 explicit TemplateArgumentList(const TemplateArgumentList *Other)
194 : Arguments(Other->data(), false), NumArguments(Other->size()) { }
196 /// \brief Retrieve the template argument at a given index.
197 const TemplateArgument &get(unsigned Idx) const {
198 assert(Idx < NumArguments && "Invalid template argument index");
202 /// \brief Retrieve the template argument at a given index.
203 const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); }
205 /// \brief Produce this as an array ref.
206 llvm::ArrayRef<TemplateArgument> asArray() const {
207 return llvm::ArrayRef<TemplateArgument>(data(), size());
210 /// \brief Retrieve the number of template arguments in this
211 /// template argument list.
212 unsigned size() const { return NumArguments; }
214 /// \brief Retrieve a pointer to the template argument list.
215 const TemplateArgument *data() const {
216 return Arguments.getPointer();
220 //===----------------------------------------------------------------------===//
221 // Kinds of Templates
222 //===----------------------------------------------------------------------===//
224 /// \brief The base class of all kinds of template declarations (e.g.,
225 /// class, function, etc.).
227 /// The TemplateDecl class stores the list of template parameters and a
228 /// reference to the templated scoped declaration: the underlying AST node.
229 class TemplateDecl : public NamedDecl {
230 virtual void anchor();
232 // This is probably never used.
233 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
234 DeclarationName Name)
235 : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(0) { }
237 // Construct a template decl with the given name and parameters.
238 // Used when there is not templated element (tt-params, alias?).
239 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
240 DeclarationName Name, TemplateParameterList *Params)
241 : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(Params) { }
243 // Construct a template decl with name, parameters, and templated element.
244 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
245 DeclarationName Name, TemplateParameterList *Params,
247 : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl),
248 TemplateParams(Params) { }
250 /// Get the list of template parameters
251 TemplateParameterList *getTemplateParameters() const {
252 return TemplateParams;
255 /// Get the underlying, templated declaration.
256 NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
258 // Implement isa/cast/dyncast/etc.
259 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
260 static bool classofKind(Kind K) {
261 return K >= firstTemplate && K <= lastTemplate;
264 SourceRange getSourceRange() const LLVM_READONLY {
265 return SourceRange(TemplateParams->getTemplateLoc(),
266 TemplatedDecl->getSourceRange().getEnd());
270 NamedDecl *TemplatedDecl;
271 TemplateParameterList* TemplateParams;
274 /// \brief Initialize the underlying templated declaration and
275 /// template parameters.
276 void init(NamedDecl *templatedDecl, TemplateParameterList* templateParams) {
277 assert(TemplatedDecl == 0 && "TemplatedDecl already set!");
278 assert(TemplateParams == 0 && "TemplateParams already set!");
279 TemplatedDecl = templatedDecl;
280 TemplateParams = templateParams;
284 /// \brief Provides information about a function template specialization,
285 /// which is a FunctionDecl that has been explicitly specialization or
286 /// instantiated from a function template.
287 class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode {
288 FunctionTemplateSpecializationInfo(FunctionDecl *FD,
289 FunctionTemplateDecl *Template,
290 TemplateSpecializationKind TSK,
291 const TemplateArgumentList *TemplateArgs,
292 const ASTTemplateArgumentListInfo *TemplateArgsAsWritten,
295 Template(Template, TSK - 1),
296 TemplateArguments(TemplateArgs),
297 TemplateArgumentsAsWritten(TemplateArgsAsWritten),
298 PointOfInstantiation(POI) { }
301 static FunctionTemplateSpecializationInfo *
302 Create(ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
303 TemplateSpecializationKind TSK,
304 const TemplateArgumentList *TemplateArgs,
305 const TemplateArgumentListInfo *TemplateArgsAsWritten,
308 /// \brief The function template specialization that this structure
310 FunctionDecl *Function;
312 /// \brief The function template from which this function template
313 /// specialization was generated.
315 /// The two bits are contain the top 4 values of TemplateSpecializationKind.
316 llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
318 /// \brief The template arguments used to produce the function template
319 /// specialization from the function template.
320 const TemplateArgumentList *TemplateArguments;
322 /// \brief The template arguments as written in the sources, if provided.
323 const ASTTemplateArgumentListInfo *TemplateArgumentsAsWritten;
325 /// \brief The point at which this function template specialization was
326 /// first instantiated.
327 SourceLocation PointOfInstantiation;
329 /// \brief Retrieve the template from which this function was specialized.
330 FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
332 /// \brief Determine what kind of template specialization this is.
333 TemplateSpecializationKind getTemplateSpecializationKind() const {
334 return (TemplateSpecializationKind)(Template.getInt() + 1);
337 bool isExplicitSpecialization() const {
338 return getTemplateSpecializationKind() == TSK_ExplicitSpecialization;
341 /// \brief True if this declaration is an explicit specialization,
342 /// explicit instantiation declaration, or explicit instantiation
344 bool isExplicitInstantiationOrSpecialization() const {
345 switch (getTemplateSpecializationKind()) {
346 case TSK_ExplicitSpecialization:
347 case TSK_ExplicitInstantiationDeclaration:
348 case TSK_ExplicitInstantiationDefinition:
352 case TSK_ImplicitInstantiation:
355 llvm_unreachable("bad template specialization kind");
358 /// \brief Set the template specialization kind.
359 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
360 assert(TSK != TSK_Undeclared &&
361 "Cannot encode TSK_Undeclared for a function template specialization");
362 Template.setInt(TSK - 1);
365 /// \brief Retrieve the first point of instantiation of this function
366 /// template specialization.
368 /// The point of instantiation may be an invalid source location if this
369 /// function has yet to be instantiated.
370 SourceLocation getPointOfInstantiation() const {
371 return PointOfInstantiation;
374 /// \brief Set the (first) point of instantiation of this function template
376 void setPointOfInstantiation(SourceLocation POI) {
377 PointOfInstantiation = POI;
380 void Profile(llvm::FoldingSetNodeID &ID) {
381 Profile(ID, TemplateArguments->data(),
382 TemplateArguments->size(),
383 Function->getASTContext());
387 Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
388 unsigned NumTemplateArgs, ASTContext &Context) {
389 ID.AddInteger(NumTemplateArgs);
390 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
391 TemplateArgs[Arg].Profile(ID, Context);
395 /// \brief Provides information a specialization of a member of a class
396 /// template, which may be a member function, static data member,
397 /// member class or member enumeration.
398 class MemberSpecializationInfo {
399 // The member declaration from which this member was instantiated, and the
400 // manner in which the instantiation occurred (in the lower two bits).
401 llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
403 // The point at which this member was first instantiated.
404 SourceLocation PointOfInstantiation;
408 MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK,
409 SourceLocation POI = SourceLocation())
410 : MemberAndTSK(IF, TSK - 1), PointOfInstantiation(POI) {
411 assert(TSK != TSK_Undeclared &&
412 "Cannot encode undeclared template specializations for members");
415 /// \brief Retrieve the member declaration from which this member was
417 NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
419 /// \brief Determine what kind of template specialization this is.
420 TemplateSpecializationKind getTemplateSpecializationKind() const {
421 return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
424 bool isExplicitSpecialization() const {
425 return getTemplateSpecializationKind() == TSK_ExplicitSpecialization;
428 /// \brief Set the template specialization kind.
429 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
430 assert(TSK != TSK_Undeclared &&
431 "Cannot encode undeclared template specializations for members");
432 MemberAndTSK.setInt(TSK - 1);
435 /// \brief Retrieve the first point of instantiation of this member.
436 /// If the point of instantiation is an invalid location, then this member
437 /// has not yet been instantiated.
438 SourceLocation getPointOfInstantiation() const {
439 return PointOfInstantiation;
442 /// \brief Set the first point of instantiation.
443 void setPointOfInstantiation(SourceLocation POI) {
444 PointOfInstantiation = POI;
448 /// \brief Provides information about a dependent function-template
449 /// specialization declaration.
451 /// Since explicit function template specialization and instantiation
452 /// declarations can only appear in namespace scope, and you can only
453 /// specialize a member of a fully-specialized class, the only way to
454 /// get one of these is in a friend declaration like the following:
457 /// template \<class T> void foo(T);
458 /// template \<class T> class A {
459 /// friend void foo<>(T);
462 class DependentFunctionTemplateSpecializationInfo {
464 /// The number of potential template candidates.
465 unsigned NumTemplates;
467 /// The number of template arguments.
472 // Force sizeof to be a multiple of sizeof(void*) so that the
473 // trailing data is aligned.
478 /// The locations of the left and right angle brackets.
479 SourceRange AngleLocs;
481 FunctionTemplateDecl * const *getTemplates() const {
482 return reinterpret_cast<FunctionTemplateDecl*const*>(this+1);
486 DependentFunctionTemplateSpecializationInfo(
487 const UnresolvedSetImpl &Templates,
488 const TemplateArgumentListInfo &TemplateArgs);
490 /// \brief Returns the number of function templates that this might
491 /// be a specialization of.
492 unsigned getNumTemplates() const {
493 return d.NumTemplates;
496 /// \brief Returns the i'th template candidate.
497 FunctionTemplateDecl *getTemplate(unsigned I) const {
498 assert(I < getNumTemplates() && "template index out of range");
499 return getTemplates()[I];
502 /// \brief Returns the explicit template arguments that were given.
503 const TemplateArgumentLoc *getTemplateArgs() const {
504 return reinterpret_cast<const TemplateArgumentLoc*>(
505 &getTemplates()[getNumTemplates()]);
508 /// \brief Returns the number of explicit template arguments that were given.
509 unsigned getNumTemplateArgs() const {
513 /// \brief Returns the nth template argument.
514 const TemplateArgumentLoc &getTemplateArg(unsigned I) const {
515 assert(I < getNumTemplateArgs() && "template arg index out of range");
516 return getTemplateArgs()[I];
519 SourceLocation getLAngleLoc() const {
520 return AngleLocs.getBegin();
523 SourceLocation getRAngleLoc() const {
524 return AngleLocs.getEnd();
528 /// Declaration of a redeclarable template.
529 class RedeclarableTemplateDecl : public TemplateDecl,
530 public Redeclarable<RedeclarableTemplateDecl>
532 typedef Redeclarable<RedeclarableTemplateDecl> redeclarable_base;
533 virtual RedeclarableTemplateDecl *getNextRedeclaration() {
534 return RedeclLink.getNext();
536 virtual RedeclarableTemplateDecl *getPreviousDeclImpl() {
537 return getPreviousDecl();
539 virtual RedeclarableTemplateDecl *getMostRecentDeclImpl() {
540 return getMostRecentDecl();
544 template <typename EntryType> struct SpecEntryTraits {
545 typedef EntryType DeclType;
547 static DeclType *getMostRecentDecl(EntryType *D) {
548 return D->getMostRecentDecl();
552 template <typename EntryType,
553 typename _SETraits = SpecEntryTraits<EntryType>,
554 typename _DeclType = typename _SETraits::DeclType>
555 class SpecIterator : public std::iterator<std::forward_iterator_tag,
556 _DeclType*, ptrdiff_t,
557 _DeclType*, _DeclType*> {
558 typedef _SETraits SETraits;
559 typedef _DeclType DeclType;
561 typedef typename llvm::FoldingSetVector<EntryType>::iterator
564 SetIteratorType SetIter;
567 SpecIterator() : SetIter() {}
568 SpecIterator(SetIteratorType SetIter) : SetIter(SetIter) {}
570 DeclType *operator*() const {
571 return SETraits::getMostRecentDecl(&*SetIter);
573 DeclType *operator->() const { return **this; }
575 SpecIterator &operator++() { ++SetIter; return *this; }
576 SpecIterator operator++(int) {
577 SpecIterator tmp(*this);
582 bool operator==(SpecIterator Other) const {
583 return SetIter == Other.SetIter;
585 bool operator!=(SpecIterator Other) const {
586 return SetIter != Other.SetIter;
590 template <typename EntryType>
591 static SpecIterator<EntryType>
592 makeSpecIterator(llvm::FoldingSetVector<EntryType> &Specs, bool isEnd) {
593 return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin());
596 template <class EntryType> typename SpecEntryTraits<EntryType>::DeclType*
597 findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
598 const TemplateArgument *Args, unsigned NumArgs,
602 CommonBase() : InstantiatedFromMember(0, false) { }
604 /// \brief The template from which this was most
605 /// directly instantiated (or null).
607 /// The boolean value indicates whether this template
608 /// was explicitly specialized.
609 llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool>
610 InstantiatedFromMember;
613 /// \brief Pointer to the common data shared by all declarations of this
615 mutable CommonBase *Common;
617 /// \brief Retrieves the "common" pointer shared by all (re-)declarations of
618 /// the same template. Calling this routine may implicitly allocate memory
619 /// for the common pointer.
620 CommonBase *getCommonPtr() const;
622 virtual CommonBase *newCommon(ASTContext &C) const = 0;
624 // Construct a template decl with name, parameters, and templated element.
625 RedeclarableTemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
626 DeclarationName Name, TemplateParameterList *Params,
628 : TemplateDecl(DK, DC, L, Name, Params, Decl), Common() { }
631 template <class decl_type> friend class RedeclarableTemplate;
633 /// \brief Retrieves the canonical declaration of this template.
634 RedeclarableTemplateDecl *getCanonicalDecl() { return getFirstDecl(); }
635 const RedeclarableTemplateDecl *getCanonicalDecl() const {
636 return getFirstDecl();
639 /// \brief Determines whether this template was a specialization of a
642 /// In the following example, the function template \c X<int>::f and the
643 /// member template \c X<int>::Inner are member specializations.
646 /// template<typename T>
648 /// template<typename U> void f(T, U);
649 /// template<typename U> struct Inner;
652 /// template<> template<typename T>
653 /// void X<int>::f(int, T);
654 /// template<> template<typename T>
655 /// struct X<int>::Inner { /* ... */ };
657 bool isMemberSpecialization() const {
658 return getCommonPtr()->InstantiatedFromMember.getInt();
661 /// \brief Note that this member template is a specialization.
662 void setMemberSpecialization() {
663 assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
664 "Only member templates can be member template specializations");
665 getCommonPtr()->InstantiatedFromMember.setInt(true);
668 /// \brief Retrieve the member template from which this template was
669 /// instantiated, or NULL if this template was not instantiated from a
672 /// A template is instantiated from a member template when the member
673 /// template itself is part of a class template (or member thereof). For
677 /// template<typename T>
679 /// template<typename U> void f(T, U);
682 /// void test(X<int> x) {
687 /// \c X<int>::f is a FunctionTemplateDecl that describes the function
691 /// template<typename U> void X<int>::f(int, U);
694 /// which was itself created during the instantiation of \c X<int>. Calling
695 /// getInstantiatedFromMemberTemplate() on this FunctionTemplateDecl will
696 /// retrieve the FunctionTemplateDecl for the original template \c f within
697 /// the class template \c X<T>, i.e.,
700 /// template<typename T>
701 /// template<typename U>
702 /// void X<T>::f(T, U);
704 RedeclarableTemplateDecl *getInstantiatedFromMemberTemplate() const {
705 return getCommonPtr()->InstantiatedFromMember.getPointer();
708 void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD) {
709 assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
710 getCommonPtr()->InstantiatedFromMember.setPointer(TD);
713 typedef redeclarable_base::redecl_iterator redecl_iterator;
714 using redeclarable_base::redecls_begin;
715 using redeclarable_base::redecls_end;
716 using redeclarable_base::getPreviousDecl;
717 using redeclarable_base::getMostRecentDecl;
718 using redeclarable_base::isFirstDecl;
720 // Implement isa/cast/dyncast/etc.
721 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
722 static bool classofKind(Kind K) {
723 return K >= firstRedeclarableTemplate && K <= lastRedeclarableTemplate;
726 friend class ASTReader;
727 friend class ASTDeclReader;
728 friend class ASTDeclWriter;
731 template <> struct RedeclarableTemplateDecl::
732 SpecEntryTraits<FunctionTemplateSpecializationInfo> {
733 typedef FunctionDecl DeclType;
736 getMostRecentDecl(FunctionTemplateSpecializationInfo *I) {
737 return I->Function->getMostRecentDecl();
741 /// Declaration of a template function.
742 class FunctionTemplateDecl : public RedeclarableTemplateDecl {
743 static void DeallocateCommon(void *Ptr);
746 /// \brief Data that is common to all of the declarations of a given
747 /// function template.
748 struct Common : CommonBase {
749 Common() : InjectedArgs(), LazySpecializations() { }
751 /// \brief The function template specializations for this function
752 /// template, including explicit specializations and instantiations.
753 llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> Specializations;
755 /// \brief The set of "injected" template arguments used within this
756 /// function template.
758 /// This pointer refers to the template arguments (there are as
759 /// many template arguments as template parameaters) for the function
760 /// template, and is allocated lazily, since most function templates do not
761 /// require the use of this information.
762 TemplateArgument *InjectedArgs;
764 /// \brief If non-null, points to an array of specializations known only
765 /// by their external declaration IDs.
767 /// The first value in the array is the number of of specializations
769 uint32_t *LazySpecializations;
772 FunctionTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
773 TemplateParameterList *Params, NamedDecl *Decl)
774 : RedeclarableTemplateDecl(FunctionTemplate, DC, L, Name, Params, Decl) { }
776 CommonBase *newCommon(ASTContext &C) const;
778 Common *getCommonPtr() const {
779 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
782 friend class FunctionDecl;
784 /// \brief Load any lazily-loaded specializations from the external source.
785 void LoadLazySpecializations() const;
787 /// \brief Retrieve the set of function template specializations of this
788 /// function template.
789 llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
790 getSpecializations() const;
792 /// \brief Add a specialization of this function template.
794 /// \param InsertPos Insert position in the FoldingSetVector, must have been
795 /// retrieved by an earlier call to findSpecialization().
796 void addSpecialization(FunctionTemplateSpecializationInfo* Info,
800 /// Get the underlying function declaration of the template.
801 FunctionDecl *getTemplatedDecl() const {
802 return static_cast<FunctionDecl*>(TemplatedDecl);
805 /// Returns whether this template declaration defines the primary
807 bool isThisDeclarationADefinition() const {
808 return getTemplatedDecl()->isThisDeclarationADefinition();
811 /// \brief Return the specialization with the provided arguments if it exists,
812 /// otherwise return the insertion point.
813 FunctionDecl *findSpecialization(const TemplateArgument *Args,
814 unsigned NumArgs, void *&InsertPos);
816 FunctionTemplateDecl *getCanonicalDecl() {
817 return cast<FunctionTemplateDecl>(
818 RedeclarableTemplateDecl::getCanonicalDecl());
820 const FunctionTemplateDecl *getCanonicalDecl() const {
821 return cast<FunctionTemplateDecl>(
822 RedeclarableTemplateDecl::getCanonicalDecl());
825 /// \brief Retrieve the previous declaration of this function template, or
826 /// NULL if no such declaration exists.
827 FunctionTemplateDecl *getPreviousDecl() {
828 return cast_or_null<FunctionTemplateDecl>(
829 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
832 /// \brief Retrieve the previous declaration of this function template, or
833 /// NULL if no such declaration exists.
834 const FunctionTemplateDecl *getPreviousDecl() const {
835 return cast_or_null<FunctionTemplateDecl>(
836 static_cast<const RedeclarableTemplateDecl *>(this)->getPreviousDecl());
839 FunctionTemplateDecl *getInstantiatedFromMemberTemplate() {
840 return cast_or_null<FunctionTemplateDecl>(
841 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
844 typedef SpecIterator<FunctionTemplateSpecializationInfo> spec_iterator;
846 spec_iterator spec_begin() const {
847 return makeSpecIterator(getSpecializations(), false);
850 spec_iterator spec_end() const {
851 return makeSpecIterator(getSpecializations(), true);
854 /// \brief Retrieve the "injected" template arguments that correspond to the
855 /// template parameters of this function template.
857 /// Although the C++ standard has no notion of the "injected" template
858 /// arguments for a function template, the notion is convenient when
859 /// we need to perform substitutions inside the definition of a function
861 ArrayRef<TemplateArgument> getInjectedTemplateArgs();
863 /// \brief Create a function template node.
864 static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
866 DeclarationName Name,
867 TemplateParameterList *Params,
870 /// \brief Create an empty function template node.
871 static FunctionTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
873 // Implement isa/cast/dyncast support
874 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
875 static bool classofKind(Kind K) { return K == FunctionTemplate; }
877 friend class ASTDeclReader;
878 friend class ASTDeclWriter;
881 //===----------------------------------------------------------------------===//
882 // Kinds of Template Parameters
883 //===----------------------------------------------------------------------===//
885 /// \brief Defines the position of a template parameter within a template
888 /// Because template parameter can be listed
889 /// sequentially for out-of-line template members, each template parameter is
890 /// given a Depth - the nesting of template parameter scopes - and a Position -
891 /// the occurrence within the parameter list.
892 /// This class is inheritedly privately by different kinds of template
893 /// parameters and is not part of the Decl hierarchy. Just a facility.
894 class TemplateParmPosition {
896 // FIXME: This should probably never be called, but it's here as
897 TemplateParmPosition()
898 : Depth(0), Position(0)
899 { /* llvm_unreachable("Cannot create positionless template parameter"); */ }
901 TemplateParmPosition(unsigned D, unsigned P)
902 : Depth(D), Position(P)
905 // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
911 /// Get the nesting depth of the template parameter.
912 unsigned getDepth() const { return Depth; }
913 void setDepth(unsigned D) { Depth = D; }
915 /// Get the position of the template parameter within its parameter list.
916 unsigned getPosition() const { return Position; }
917 void setPosition(unsigned P) { Position = P; }
919 /// Get the index of the template parameter within its parameter list.
920 unsigned getIndex() const { return Position; }
923 /// \brief Declaration of a template type parameter.
925 /// For example, "T" in
927 /// template<typename T> class vector;
929 class TemplateTypeParmDecl : public TypeDecl {
930 /// \brief Whether this template type parameter was declaration with
931 /// the 'typename' keyword.
933 /// If false, it was declared with the 'class' keyword.
936 /// \brief Whether this template type parameter inherited its
937 /// default argument.
938 bool InheritedDefault : 1;
940 /// \brief The default template argument, if any.
941 TypeSourceInfo *DefaultArgument;
943 TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
944 SourceLocation IdLoc, IdentifierInfo *Id,
946 : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename),
947 InheritedDefault(false), DefaultArgument() { }
949 /// Sema creates these on the stack during auto type deduction.
953 static TemplateTypeParmDecl *Create(const ASTContext &C, DeclContext *DC,
954 SourceLocation KeyLoc,
955 SourceLocation NameLoc,
956 unsigned D, unsigned P,
957 IdentifierInfo *Id, bool Typename,
959 static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
962 /// \brief Whether this template type parameter was declared with
963 /// the 'typename' keyword.
965 /// If not, it was declared with the 'class' keyword.
966 bool wasDeclaredWithTypename() const { return Typename; }
968 /// \brief Determine whether this template parameter has a default
970 bool hasDefaultArgument() const { return DefaultArgument != 0; }
972 /// \brief Retrieve the default argument, if any.
973 QualType getDefaultArgument() const { return DefaultArgument->getType(); }
975 /// \brief Retrieves the default argument's source information, if any.
976 TypeSourceInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
978 /// \brief Retrieves the location of the default argument declaration.
979 SourceLocation getDefaultArgumentLoc() const;
981 /// \brief Determines whether the default argument was inherited
982 /// from a previous declaration of this template.
983 bool defaultArgumentWasInherited() const { return InheritedDefault; }
985 /// \brief Set the default argument for this template parameter, and
986 /// whether that default argument was inherited from another
988 void setDefaultArgument(TypeSourceInfo *DefArg, bool Inherited) {
989 DefaultArgument = DefArg;
990 InheritedDefault = Inherited;
993 /// \brief Removes the default argument of this template parameter.
994 void removeDefaultArgument() {
996 InheritedDefault = false;
999 /// \brief Set whether this template type parameter was declared with
1000 /// the 'typename' or 'class' keyword.
1001 void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; }
1003 /// \brief Retrieve the depth of the template parameter.
1004 unsigned getDepth() const;
1006 /// \brief Retrieve the index of the template parameter.
1007 unsigned getIndex() const;
1009 /// \brief Returns whether this is a parameter pack.
1010 bool isParameterPack() const;
1012 SourceRange getSourceRange() const LLVM_READONLY;
1014 // Implement isa/cast/dyncast/etc.
1015 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1016 static bool classofKind(Kind K) { return K == TemplateTypeParm; }
1019 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
1022 /// template<int Size> class array { };
1024 class NonTypeTemplateParmDecl
1025 : public DeclaratorDecl, protected TemplateParmPosition {
1026 /// \brief The default template argument, if any, and whether or not
1027 /// it was inherited.
1028 llvm::PointerIntPair<Expr*, 1, bool> DefaultArgumentAndInherited;
1030 // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index
1031 // down here to save memory.
1033 /// \brief Whether this non-type template parameter is a parameter pack.
1036 /// \brief Whether this non-type template parameter is an "expanded"
1037 /// parameter pack, meaning that its type is a pack expansion and we
1038 /// already know the set of types that expansion expands to.
1039 bool ExpandedParameterPack;
1041 /// \brief The number of types in an expanded parameter pack.
1042 unsigned NumExpandedTypes;
1044 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1045 SourceLocation IdLoc, unsigned D, unsigned P,
1046 IdentifierInfo *Id, QualType T,
1047 bool ParameterPack, TypeSourceInfo *TInfo)
1048 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
1049 TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false),
1050 ParameterPack(ParameterPack), ExpandedParameterPack(false),
1054 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1055 SourceLocation IdLoc, unsigned D, unsigned P,
1056 IdentifierInfo *Id, QualType T,
1057 TypeSourceInfo *TInfo,
1058 const QualType *ExpandedTypes,
1059 unsigned NumExpandedTypes,
1060 TypeSourceInfo **ExpandedTInfos);
1062 friend class ASTDeclReader;
1065 static NonTypeTemplateParmDecl *
1066 Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1067 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1068 QualType T, bool ParameterPack, TypeSourceInfo *TInfo);
1070 static NonTypeTemplateParmDecl *
1071 Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1072 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1073 QualType T, TypeSourceInfo *TInfo,
1074 const QualType *ExpandedTypes, unsigned NumExpandedTypes,
1075 TypeSourceInfo **ExpandedTInfos);
1077 static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1079 static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1081 unsigned NumExpandedTypes);
1083 using TemplateParmPosition::getDepth;
1084 using TemplateParmPosition::setDepth;
1085 using TemplateParmPosition::getPosition;
1086 using TemplateParmPosition::setPosition;
1087 using TemplateParmPosition::getIndex;
1089 SourceRange getSourceRange() const LLVM_READONLY;
1091 /// \brief Determine whether this template parameter has a default
1093 bool hasDefaultArgument() const {
1094 return DefaultArgumentAndInherited.getPointer() != 0;
1097 /// \brief Retrieve the default argument, if any.
1098 Expr *getDefaultArgument() const {
1099 return DefaultArgumentAndInherited.getPointer();
1102 /// \brief Retrieve the location of the default argument, if any.
1103 SourceLocation getDefaultArgumentLoc() const;
1105 /// \brief Determines whether the default argument was inherited
1106 /// from a previous declaration of this template.
1107 bool defaultArgumentWasInherited() const {
1108 return DefaultArgumentAndInherited.getInt();
1111 /// \brief Set the default argument for this template parameter, and
1112 /// whether that default argument was inherited from another
1114 void setDefaultArgument(Expr *DefArg, bool Inherited) {
1115 DefaultArgumentAndInherited.setPointer(DefArg);
1116 DefaultArgumentAndInherited.setInt(Inherited);
1119 /// \brief Removes the default argument of this template parameter.
1120 void removeDefaultArgument() {
1121 DefaultArgumentAndInherited.setPointer(0);
1122 DefaultArgumentAndInherited.setInt(false);
1125 /// \brief Whether this parameter is a non-type template parameter pack.
1127 /// If the parameter is a parameter pack, the type may be a
1128 /// \c PackExpansionType. In the following example, the \c Dims parameter
1129 /// is a parameter pack (whose type is 'unsigned').
1132 /// template<typename T, unsigned ...Dims> struct multi_array;
1134 bool isParameterPack() const { return ParameterPack; }
1136 /// \brief Whether this parameter pack is a pack expansion.
1138 /// A non-type template parameter pack is a pack expansion if its type
1139 /// contains an unexpanded parameter pack. In this case, we will have
1140 /// built a PackExpansionType wrapping the type.
1141 bool isPackExpansion() const {
1142 return ParameterPack && getType()->getAs<PackExpansionType>();
1145 /// \brief Whether this parameter is a non-type template parameter pack
1146 /// that has a known list of different types at different positions.
1148 /// A parameter pack is an expanded parameter pack when the original
1149 /// parameter pack's type was itself a pack expansion, and that expansion
1150 /// has already been expanded. For example, given:
1153 /// template<typename ...Types>
1155 /// template<Types ...Values>
1156 /// struct Y { /* ... */ };
1160 /// The parameter pack \c Values has a \c PackExpansionType as its type,
1161 /// which expands \c Types. When \c Types is supplied with template arguments
1162 /// by instantiating \c X, the instantiation of \c Values becomes an
1163 /// expanded parameter pack. For example, instantiating
1164 /// \c X<int, unsigned int> results in \c Values being an expanded parameter
1165 /// pack with expansion types \c int and \c unsigned int.
1167 /// The \c getExpansionType() and \c getExpansionTypeSourceInfo() functions
1168 /// return the expansion types.
1169 bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1171 /// \brief Retrieves the number of expansion types in an expanded parameter
1173 unsigned getNumExpansionTypes() const {
1174 assert(ExpandedParameterPack && "Not an expansion parameter pack");
1175 return NumExpandedTypes;
1178 /// \brief Retrieve a particular expansion type within an expanded parameter
1180 QualType getExpansionType(unsigned I) const {
1181 assert(I < NumExpandedTypes && "Out-of-range expansion type index");
1182 void * const *TypesAndInfos = reinterpret_cast<void * const*>(this + 1);
1183 return QualType::getFromOpaquePtr(TypesAndInfos[2*I]);
1186 /// \brief Retrieve a particular expansion type source info within an
1187 /// expanded parameter pack.
1188 TypeSourceInfo *getExpansionTypeSourceInfo(unsigned I) const {
1189 assert(I < NumExpandedTypes && "Out-of-range expansion type index");
1190 void * const *TypesAndInfos = reinterpret_cast<void * const*>(this + 1);
1191 return static_cast<TypeSourceInfo *>(TypesAndInfos[2*I+1]);
1194 // Implement isa/cast/dyncast/etc.
1195 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1196 static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
1199 /// TemplateTemplateParmDecl - Declares a template template parameter,
1202 /// template <template <typename> class T> class container { };
1204 /// A template template parameter is a TemplateDecl because it defines the
1205 /// name of a template and the template parameters allowable for substitution.
1206 class TemplateTemplateParmDecl : public TemplateDecl,
1207 protected TemplateParmPosition
1209 virtual void anchor();
1211 /// DefaultArgument - The default template argument, if any.
1212 TemplateArgumentLoc DefaultArgument;
1213 /// Whether or not the default argument was inherited.
1214 bool DefaultArgumentWasInherited;
1216 /// \brief Whether this parameter is a parameter pack.
1219 /// \brief Whether this template template parameter is an "expanded"
1220 /// parameter pack, meaning that it is a pack expansion and we
1221 /// already know the set of template parameters that expansion expands to.
1222 bool ExpandedParameterPack;
1224 /// \brief The number of parameters in an expanded parameter pack.
1225 unsigned NumExpandedParams;
1227 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
1228 unsigned D, unsigned P, bool ParameterPack,
1229 IdentifierInfo *Id, TemplateParameterList *Params)
1230 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
1231 TemplateParmPosition(D, P), DefaultArgument(),
1232 DefaultArgumentWasInherited(false), ParameterPack(ParameterPack),
1233 ExpandedParameterPack(false), NumExpandedParams(0)
1236 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
1237 unsigned D, unsigned P,
1238 IdentifierInfo *Id, TemplateParameterList *Params,
1239 unsigned NumExpansions,
1240 TemplateParameterList * const *Expansions);
1243 static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1244 SourceLocation L, unsigned D,
1245 unsigned P, bool ParameterPack,
1247 TemplateParameterList *Params);
1248 static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1249 SourceLocation L, unsigned D,
1252 TemplateParameterList *Params,
1253 ArrayRef<TemplateParameterList *> Expansions);
1255 static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1257 static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1259 unsigned NumExpansions);
1261 using TemplateParmPosition::getDepth;
1262 using TemplateParmPosition::getPosition;
1263 using TemplateParmPosition::getIndex;
1265 /// \brief Whether this template template parameter is a template
1269 /// template<template <class T> ...MetaFunctions> struct Apply;
1271 bool isParameterPack() const { return ParameterPack; }
1273 /// \brief Whether this parameter pack is a pack expansion.
1275 /// A template template parameter pack is a pack expansion if its template
1276 /// parameter list contains an unexpanded parameter pack.
1277 bool isPackExpansion() const {
1278 return ParameterPack &&
1279 getTemplateParameters()->containsUnexpandedParameterPack();
1282 /// \brief Whether this parameter is a template template parameter pack that
1283 /// has a known list of different template parameter lists at different
1286 /// A parameter pack is an expanded parameter pack when the original parameter
1287 /// pack's template parameter list was itself a pack expansion, and that
1288 /// expansion has already been expanded. For exampe, given:
1291 /// template<typename...Types> struct Outer {
1292 /// template<template<Types> class...Templates> struct Inner;
1296 /// The parameter pack \c Templates is a pack expansion, which expands the
1297 /// pack \c Types. When \c Types is supplied with template arguments by
1298 /// instantiating \c Outer, the instantiation of \c Templates is an expanded
1300 bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1302 /// \brief Retrieves the number of expansion template parameters in
1303 /// an expanded parameter pack.
1304 unsigned getNumExpansionTemplateParameters() const {
1305 assert(ExpandedParameterPack && "Not an expansion parameter pack");
1306 return NumExpandedParams;
1309 /// \brief Retrieve a particular expansion type within an expanded parameter
1311 TemplateParameterList *getExpansionTemplateParameters(unsigned I) const {
1312 assert(I < NumExpandedParams && "Out-of-range expansion type index");
1313 return reinterpret_cast<TemplateParameterList *const *>(this + 1)[I];
1316 /// \brief Determine whether this template parameter has a default
1318 bool hasDefaultArgument() const {
1319 return !DefaultArgument.getArgument().isNull();
1322 /// \brief Retrieve the default argument, if any.
1323 const TemplateArgumentLoc &getDefaultArgument() const {
1324 return DefaultArgument;
1327 /// \brief Retrieve the location of the default argument, if any.
1328 SourceLocation getDefaultArgumentLoc() const;
1330 /// \brief Determines whether the default argument was inherited
1331 /// from a previous declaration of this template.
1332 bool defaultArgumentWasInherited() const {
1333 return DefaultArgumentWasInherited;
1336 /// \brief Set the default argument for this template parameter, and
1337 /// whether that default argument was inherited from another
1339 void setDefaultArgument(const TemplateArgumentLoc &DefArg, bool Inherited) {
1340 DefaultArgument = DefArg;
1341 DefaultArgumentWasInherited = Inherited;
1344 /// \brief Removes the default argument of this template parameter.
1345 void removeDefaultArgument() {
1346 DefaultArgument = TemplateArgumentLoc();
1347 DefaultArgumentWasInherited = false;
1350 SourceRange getSourceRange() const LLVM_READONLY {
1351 SourceLocation End = getLocation();
1352 if (hasDefaultArgument() && !defaultArgumentWasInherited())
1353 End = getDefaultArgument().getSourceRange().getEnd();
1354 return SourceRange(getTemplateParameters()->getTemplateLoc(), End);
1357 // Implement isa/cast/dyncast/etc.
1358 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1359 static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
1361 friend class ASTDeclReader;
1362 friend class ASTDeclWriter;
1365 /// \brief Represents a class template specialization, which refers to
1366 /// a class template with a given set of template arguments.
1368 /// Class template specializations represent both explicit
1369 /// specialization of class templates, as in the example below, and
1370 /// implicit instantiations of class templates.
1373 /// template<typename T> class array;
1376 /// class array<bool> { }; // class template specialization array<bool>
1378 class ClassTemplateSpecializationDecl
1379 : public CXXRecordDecl, public llvm::FoldingSetNode {
1381 /// \brief Structure that stores information about a class template
1382 /// specialization that was instantiated from a class template partial
1384 struct SpecializedPartialSpecialization {
1385 /// \brief The class template partial specialization from which this
1386 /// class template specialization was instantiated.
1387 ClassTemplatePartialSpecializationDecl *PartialSpecialization;
1389 /// \brief The template argument list deduced for the class template
1390 /// partial specialization itself.
1391 const TemplateArgumentList *TemplateArgs;
1394 /// \brief The template that this specialization specializes
1395 llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
1396 SpecializedTemplate;
1398 /// \brief Further info for explicit template specialization/instantiation.
1399 struct ExplicitSpecializationInfo {
1400 /// \brief The type-as-written.
1401 TypeSourceInfo *TypeAsWritten;
1402 /// \brief The location of the extern keyword.
1403 SourceLocation ExternLoc;
1404 /// \brief The location of the template keyword.
1405 SourceLocation TemplateKeywordLoc;
1407 ExplicitSpecializationInfo()
1408 : TypeAsWritten(0), ExternLoc(), TemplateKeywordLoc() {}
1411 /// \brief Further info for explicit template specialization/instantiation.
1412 /// Does not apply to implicit specializations.
1413 ExplicitSpecializationInfo *ExplicitInfo;
1415 /// \brief The template arguments used to describe this specialization.
1416 const TemplateArgumentList *TemplateArgs;
1418 /// \brief The point where this template was instantiated (if any)
1419 SourceLocation PointOfInstantiation;
1421 /// \brief The kind of specialization this declaration refers to.
1422 /// Really a value of type TemplateSpecializationKind.
1423 unsigned SpecializationKind : 3;
1426 ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
1427 DeclContext *DC, SourceLocation StartLoc,
1428 SourceLocation IdLoc,
1429 ClassTemplateDecl *SpecializedTemplate,
1430 const TemplateArgument *Args,
1432 ClassTemplateSpecializationDecl *PrevDecl);
1434 explicit ClassTemplateSpecializationDecl(Kind DK);
1437 static ClassTemplateSpecializationDecl *
1438 Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1439 SourceLocation StartLoc, SourceLocation IdLoc,
1440 ClassTemplateDecl *SpecializedTemplate,
1441 const TemplateArgument *Args,
1443 ClassTemplateSpecializationDecl *PrevDecl);
1444 static ClassTemplateSpecializationDecl *
1445 CreateDeserialized(ASTContext &C, unsigned ID);
1447 virtual void getNameForDiagnostic(raw_ostream &OS,
1448 const PrintingPolicy &Policy,
1449 bool Qualified) const;
1451 ClassTemplateSpecializationDecl *getMostRecentDecl() {
1452 CXXRecordDecl *Recent = static_cast<CXXRecordDecl *>(
1453 this)->getMostRecentDecl();
1454 while (!isa<ClassTemplateSpecializationDecl>(Recent)) {
1455 // FIXME: Does injected class name need to be in the redeclarations chain?
1456 assert(Recent->isInjectedClassName() && Recent->getPreviousDecl());
1457 Recent = Recent->getPreviousDecl();
1459 return cast<ClassTemplateSpecializationDecl>(Recent);
1462 /// \brief Retrieve the template that this specialization specializes.
1463 ClassTemplateDecl *getSpecializedTemplate() const;
1465 /// \brief Retrieve the template arguments of the class template
1467 const TemplateArgumentList &getTemplateArgs() const {
1468 return *TemplateArgs;
1471 /// \brief Determine the kind of specialization that this
1472 /// declaration represents.
1473 TemplateSpecializationKind getSpecializationKind() const {
1474 return static_cast<TemplateSpecializationKind>(SpecializationKind);
1477 bool isExplicitSpecialization() const {
1478 return getSpecializationKind() == TSK_ExplicitSpecialization;
1481 /// \brief True if this declaration is an explicit specialization,
1482 /// explicit instantiation declaration, or explicit instantiation
1484 bool isExplicitInstantiationOrSpecialization() const {
1485 switch (getTemplateSpecializationKind()) {
1486 case TSK_ExplicitSpecialization:
1487 case TSK_ExplicitInstantiationDeclaration:
1488 case TSK_ExplicitInstantiationDefinition:
1491 case TSK_Undeclared:
1492 case TSK_ImplicitInstantiation:
1495 llvm_unreachable("bad template specialization kind");
1498 void setSpecializationKind(TemplateSpecializationKind TSK) {
1499 SpecializationKind = TSK;
1502 /// \brief Get the point of instantiation (if any), or null if none.
1503 SourceLocation getPointOfInstantiation() const {
1504 return PointOfInstantiation;
1507 void setPointOfInstantiation(SourceLocation Loc) {
1508 assert(Loc.isValid() && "point of instantiation must be valid!");
1509 PointOfInstantiation = Loc;
1512 /// \brief If this class template specialization is an instantiation of
1513 /// a template (rather than an explicit specialization), return the
1514 /// class template or class template partial specialization from which it
1515 /// was instantiated.
1516 llvm::PointerUnion<ClassTemplateDecl *,
1517 ClassTemplatePartialSpecializationDecl *>
1518 getInstantiatedFrom() const {
1519 if (getSpecializationKind() != TSK_ImplicitInstantiation &&
1520 getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
1521 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
1522 return llvm::PointerUnion<ClassTemplateDecl *,
1523 ClassTemplatePartialSpecializationDecl *>();
1525 if (SpecializedPartialSpecialization *PartialSpec
1526 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
1527 return PartialSpec->PartialSpecialization;
1529 return SpecializedTemplate.get<ClassTemplateDecl*>();
1532 /// \brief Retrieve the class template or class template partial
1533 /// specialization which was specialized by this.
1534 llvm::PointerUnion<ClassTemplateDecl *,
1535 ClassTemplatePartialSpecializationDecl *>
1536 getSpecializedTemplateOrPartial() const {
1537 if (SpecializedPartialSpecialization *PartialSpec
1538 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
1539 return PartialSpec->PartialSpecialization;
1541 return SpecializedTemplate.get<ClassTemplateDecl*>();
1544 /// \brief Retrieve the set of template arguments that should be used
1545 /// to instantiate members of the class template or class template partial
1546 /// specialization from which this class template specialization was
1549 /// \returns For a class template specialization instantiated from the primary
1550 /// template, this function will return the same template arguments as
1551 /// getTemplateArgs(). For a class template specialization instantiated from
1552 /// a class template partial specialization, this function will return the
1553 /// deduced template arguments for the class template partial specialization
1555 const TemplateArgumentList &getTemplateInstantiationArgs() const {
1556 if (SpecializedPartialSpecialization *PartialSpec
1557 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
1558 return *PartialSpec->TemplateArgs;
1560 return getTemplateArgs();
1563 /// \brief Note that this class template specialization is actually an
1564 /// instantiation of the given class template partial specialization whose
1565 /// template arguments have been deduced.
1566 void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
1567 const TemplateArgumentList *TemplateArgs) {
1568 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1569 "Already set to a class template partial specialization!");
1570 SpecializedPartialSpecialization *PS
1571 = new (getASTContext()) SpecializedPartialSpecialization();
1572 PS->PartialSpecialization = PartialSpec;
1573 PS->TemplateArgs = TemplateArgs;
1574 SpecializedTemplate = PS;
1577 /// \brief Note that this class template specialization is an instantiation
1578 /// of the given class template.
1579 void setInstantiationOf(ClassTemplateDecl *TemplDecl) {
1580 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1581 "Previously set to a class template partial specialization!");
1582 SpecializedTemplate = TemplDecl;
1585 /// \brief Sets the type of this specialization as it was written by
1586 /// the user. This will be a class template specialization type.
1587 void setTypeAsWritten(TypeSourceInfo *T) {
1589 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1590 ExplicitInfo->TypeAsWritten = T;
1592 /// \brief Gets the type of this specialization as it was written by
1593 /// the user, if it was so written.
1594 TypeSourceInfo *getTypeAsWritten() const {
1595 return ExplicitInfo ? ExplicitInfo->TypeAsWritten : 0;
1598 /// \brief Gets the location of the extern keyword, if present.
1599 SourceLocation getExternLoc() const {
1600 return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
1602 /// \brief Sets the location of the extern keyword.
1603 void setExternLoc(SourceLocation Loc) {
1605 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1606 ExplicitInfo->ExternLoc = Loc;
1609 /// \brief Sets the location of the template keyword.
1610 void setTemplateKeywordLoc(SourceLocation Loc) {
1612 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1613 ExplicitInfo->TemplateKeywordLoc = Loc;
1615 /// \brief Gets the location of the template keyword, if present.
1616 SourceLocation getTemplateKeywordLoc() const {
1617 return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
1620 SourceRange getSourceRange() const LLVM_READONLY;
1622 void Profile(llvm::FoldingSetNodeID &ID) const {
1623 Profile(ID, TemplateArgs->data(), TemplateArgs->size(), getASTContext());
1627 Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
1628 unsigned NumTemplateArgs, ASTContext &Context) {
1629 ID.AddInteger(NumTemplateArgs);
1630 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
1631 TemplateArgs[Arg].Profile(ID, Context);
1634 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1635 static bool classofKind(Kind K) {
1636 return K >= firstClassTemplateSpecialization &&
1637 K <= lastClassTemplateSpecialization;
1640 friend class ASTDeclReader;
1641 friend class ASTDeclWriter;
1644 class ClassTemplatePartialSpecializationDecl
1645 : public ClassTemplateSpecializationDecl {
1646 virtual void anchor();
1648 /// \brief The list of template parameters
1649 TemplateParameterList* TemplateParams;
1651 /// \brief The source info for the template arguments as written.
1652 /// FIXME: redundant with TypeAsWritten?
1653 const ASTTemplateArgumentListInfo *ArgsAsWritten;
1655 /// \brief The class template partial specialization from which this
1656 /// class template partial specialization was instantiated.
1658 /// The boolean value will be true to indicate that this class template
1659 /// partial specialization was specialized at this level.
1660 llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
1661 InstantiatedFromMember;
1663 ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
1665 SourceLocation StartLoc,
1666 SourceLocation IdLoc,
1667 TemplateParameterList *Params,
1668 ClassTemplateDecl *SpecializedTemplate,
1669 const TemplateArgument *Args,
1671 const ASTTemplateArgumentListInfo *ArgsAsWritten,
1672 ClassTemplatePartialSpecializationDecl *PrevDecl);
1674 ClassTemplatePartialSpecializationDecl()
1675 : ClassTemplateSpecializationDecl(ClassTemplatePartialSpecialization),
1676 TemplateParams(0), ArgsAsWritten(0), InstantiatedFromMember(0, false) { }
1679 static ClassTemplatePartialSpecializationDecl *
1680 Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1681 SourceLocation StartLoc, SourceLocation IdLoc,
1682 TemplateParameterList *Params,
1683 ClassTemplateDecl *SpecializedTemplate,
1684 const TemplateArgument *Args,
1686 const TemplateArgumentListInfo &ArgInfos,
1687 QualType CanonInjectedType,
1688 ClassTemplatePartialSpecializationDecl *PrevDecl);
1690 static ClassTemplatePartialSpecializationDecl *
1691 CreateDeserialized(ASTContext &C, unsigned ID);
1693 ClassTemplatePartialSpecializationDecl *getMostRecentDecl() {
1694 return cast<ClassTemplatePartialSpecializationDecl>(
1695 static_cast<ClassTemplateSpecializationDecl *>(
1696 this)->getMostRecentDecl());
1699 /// Get the list of template parameters
1700 TemplateParameterList *getTemplateParameters() const {
1701 return TemplateParams;
1704 /// Get the template arguments as written.
1705 const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
1706 return ArgsAsWritten;
1709 /// \brief Retrieve the member class template partial specialization from
1710 /// which this particular class template partial specialization was
1714 /// template<typename T>
1716 /// template<typename U> struct Inner;
1717 /// template<typename U> struct Inner<U*> { }; // #1
1720 /// Outer<float>::Inner<int*> ii;
1723 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
1724 /// end up instantiating the partial specialization
1725 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
1726 /// template partial specialization \c Outer<T>::Inner<U*>. Given
1727 /// \c Outer<float>::Inner<U*>, this function would return
1728 /// \c Outer<T>::Inner<U*>.
1729 ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
1730 ClassTemplatePartialSpecializationDecl *First =
1731 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1732 return First->InstantiatedFromMember.getPointer();
1735 void setInstantiatedFromMember(
1736 ClassTemplatePartialSpecializationDecl *PartialSpec) {
1737 ClassTemplatePartialSpecializationDecl *First =
1738 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1739 First->InstantiatedFromMember.setPointer(PartialSpec);
1742 /// \brief Determines whether this class template partial specialization
1743 /// template was a specialization of a member partial specialization.
1745 /// In the following example, the member template partial specialization
1746 /// \c X<int>::Inner<T*> is a member specialization.
1749 /// template<typename T>
1751 /// template<typename U> struct Inner;
1752 /// template<typename U> struct Inner<U*>;
1755 /// template<> template<typename T>
1756 /// struct X<int>::Inner<T*> { /* ... */ };
1758 bool isMemberSpecialization() {
1759 ClassTemplatePartialSpecializationDecl *First =
1760 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1761 return First->InstantiatedFromMember.getInt();
1764 /// \brief Note that this member template is a specialization.
1765 void setMemberSpecialization() {
1766 ClassTemplatePartialSpecializationDecl *First =
1767 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1768 assert(First->InstantiatedFromMember.getPointer() &&
1769 "Only member templates can be member template specializations");
1770 return First->InstantiatedFromMember.setInt(true);
1773 /// Retrieves the injected specialization type for this partial
1774 /// specialization. This is not the same as the type-decl-type for
1775 /// this partial specialization, which is an InjectedClassNameType.
1776 QualType getInjectedSpecializationType() const {
1777 assert(getTypeForDecl() && "partial specialization has no type set!");
1778 return cast<InjectedClassNameType>(getTypeForDecl())
1779 ->getInjectedSpecializationType();
1782 // FIXME: Add Profile support!
1784 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1785 static bool classofKind(Kind K) {
1786 return K == ClassTemplatePartialSpecialization;
1789 friend class ASTDeclReader;
1790 friend class ASTDeclWriter;
1793 /// Declaration of a class template.
1794 class ClassTemplateDecl : public RedeclarableTemplateDecl {
1795 static void DeallocateCommon(void *Ptr);
1798 /// \brief Data that is common to all of the declarations of a given
1800 struct Common : CommonBase {
1801 Common() : LazySpecializations() { }
1803 /// \brief The class template specializations for this class
1804 /// template, including explicit specializations and instantiations.
1805 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations;
1807 /// \brief The class template partial specializations for this class
1809 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>
1810 PartialSpecializations;
1812 /// \brief The injected-class-name type for this class template.
1813 QualType InjectedClassNameType;
1815 /// \brief If non-null, points to an array of specializations (including
1816 /// partial specializations) known only by their external declaration IDs.
1818 /// The first value in the array is the number of of specializations/
1819 /// partial specializations that follow.
1820 uint32_t *LazySpecializations;
1823 /// \brief Load any lazily-loaded specializations from the external source.
1824 void LoadLazySpecializations() const;
1826 /// \brief Retrieve the set of specializations of this class template.
1827 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
1828 getSpecializations() const;
1830 /// \brief Retrieve the set of partial specializations of this class
1832 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
1833 getPartialSpecializations();
1835 ClassTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
1836 TemplateParameterList *Params, NamedDecl *Decl)
1837 : RedeclarableTemplateDecl(ClassTemplate, DC, L, Name, Params, Decl) { }
1839 ClassTemplateDecl(EmptyShell Empty)
1840 : RedeclarableTemplateDecl(ClassTemplate, 0, SourceLocation(),
1841 DeclarationName(), 0, 0) { }
1843 CommonBase *newCommon(ASTContext &C) const;
1845 Common *getCommonPtr() const {
1846 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
1850 /// \brief Get the underlying class declarations of the template.
1851 CXXRecordDecl *getTemplatedDecl() const {
1852 return static_cast<CXXRecordDecl *>(TemplatedDecl);
1855 /// \brief Returns whether this template declaration defines the primary
1857 bool isThisDeclarationADefinition() const {
1858 return getTemplatedDecl()->isThisDeclarationADefinition();
1861 /// \brief Create a class template node.
1862 static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
1864 DeclarationName Name,
1865 TemplateParameterList *Params,
1867 ClassTemplateDecl *PrevDecl);
1869 /// \brief Create an empty class template node.
1870 static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1872 /// \brief Return the specialization with the provided arguments if it exists,
1873 /// otherwise return the insertion point.
1874 ClassTemplateSpecializationDecl *
1875 findSpecialization(const TemplateArgument *Args, unsigned NumArgs,
1878 /// \brief Insert the specified specialization knowing that it is not already
1879 /// in. InsertPos must be obtained from findSpecialization.
1880 void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos);
1882 ClassTemplateDecl *getCanonicalDecl() {
1883 return cast<ClassTemplateDecl>(
1884 RedeclarableTemplateDecl::getCanonicalDecl());
1886 const ClassTemplateDecl *getCanonicalDecl() const {
1887 return cast<ClassTemplateDecl>(
1888 RedeclarableTemplateDecl::getCanonicalDecl());
1891 /// \brief Retrieve the previous declaration of this class template, or
1892 /// NULL if no such declaration exists.
1893 ClassTemplateDecl *getPreviousDecl() {
1894 return cast_or_null<ClassTemplateDecl>(
1895 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
1898 /// \brief Retrieve the previous declaration of this class template, or
1899 /// NULL if no such declaration exists.
1900 const ClassTemplateDecl *getPreviousDecl() const {
1901 return cast_or_null<ClassTemplateDecl>(
1902 static_cast<const RedeclarableTemplateDecl *>(
1903 this)->getPreviousDecl());
1906 ClassTemplateDecl *getMostRecentDecl() {
1907 return cast<ClassTemplateDecl>(
1908 static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
1910 const ClassTemplateDecl *getMostRecentDecl() const {
1911 return const_cast<ClassTemplateDecl*>(this)->getMostRecentDecl();
1914 ClassTemplateDecl *getInstantiatedFromMemberTemplate() {
1915 return cast_or_null<ClassTemplateDecl>(
1916 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
1919 /// \brief Return the partial specialization with the provided arguments if it
1920 /// exists, otherwise return the insertion point.
1921 ClassTemplatePartialSpecializationDecl *
1922 findPartialSpecialization(const TemplateArgument *Args, unsigned NumArgs,
1925 /// \brief Insert the specified partial specialization knowing that it is not
1926 /// already in. InsertPos must be obtained from findPartialSpecialization.
1927 void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D,
1930 /// \brief Retrieve the partial specializations as an ordered list.
1931 void getPartialSpecializations(
1932 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS);
1934 /// \brief Find a class template partial specialization with the given
1937 /// \param T a dependent type that names a specialization of this class
1940 /// \returns the class template partial specialization that exactly matches
1941 /// the type \p T, or NULL if no such partial specialization exists.
1942 ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
1944 /// \brief Find a class template partial specialization which was instantiated
1945 /// from the given member partial specialization.
1947 /// \param D a member class template partial specialization.
1949 /// \returns the class template partial specialization which was instantiated
1950 /// from the given member partial specialization, or NULL if no such partial
1951 /// specialization exists.
1952 ClassTemplatePartialSpecializationDecl *
1953 findPartialSpecInstantiatedFromMember(
1954 ClassTemplatePartialSpecializationDecl *D);
1956 /// \brief Retrieve the template specialization type of the
1957 /// injected-class-name for this class template.
1959 /// The injected-class-name for a class template \c X is \c
1960 /// X<template-args>, where \c template-args is formed from the
1961 /// template arguments that correspond to the template parameters of
1962 /// \c X. For example:
1965 /// template<typename T, int N>
1967 /// typedef array this_type; // "array" is equivalent to "array<T, N>"
1970 QualType getInjectedClassNameSpecialization();
1972 typedef SpecIterator<ClassTemplateSpecializationDecl> spec_iterator;
1974 spec_iterator spec_begin() const {
1975 return makeSpecIterator(getSpecializations(), false);
1978 spec_iterator spec_end() const {
1979 return makeSpecIterator(getSpecializations(), true);
1982 typedef SpecIterator<ClassTemplatePartialSpecializationDecl>
1983 partial_spec_iterator;
1985 partial_spec_iterator partial_spec_begin() {
1986 return makeSpecIterator(getPartialSpecializations(), false);
1989 partial_spec_iterator partial_spec_end() {
1990 return makeSpecIterator(getPartialSpecializations(), true);
1993 // Implement isa/cast/dyncast support
1994 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1995 static bool classofKind(Kind K) { return K == ClassTemplate; }
1997 friend class ASTDeclReader;
1998 friend class ASTDeclWriter;
2001 /// \brief Declaration of a friend template.
2005 /// template \<typename T> class A {
2006 /// friend class MyVector<T>; // not a friend template
2007 /// template \<typename U> friend class B; // not a friend template
2008 /// template \<typename U> friend class Foo<T>::Nested; // friend template
2012 /// \note This class is not currently in use. All of the above
2013 /// will yield a FriendDecl, not a FriendTemplateDecl.
2014 class FriendTemplateDecl : public Decl {
2015 virtual void anchor();
2017 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
2020 // The number of template parameters; always non-zero.
2023 // The parameter list.
2024 TemplateParameterList **Params;
2026 // The declaration that's a friend of this class.
2029 // Location of the 'friend' specifier.
2030 SourceLocation FriendLoc;
2033 FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
2035 TemplateParameterList **Params,
2037 SourceLocation FriendLoc)
2038 : Decl(Decl::FriendTemplate, DC, Loc),
2042 FriendLoc(FriendLoc)
2045 FriendTemplateDecl(EmptyShell Empty)
2046 : Decl(Decl::FriendTemplate, Empty),
2052 static FriendTemplateDecl *Create(ASTContext &Context,
2053 DeclContext *DC, SourceLocation Loc,
2055 TemplateParameterList **Params,
2057 SourceLocation FriendLoc);
2059 static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2061 /// If this friend declaration names a templated type (or
2062 /// a dependent member type of a templated type), return that
2063 /// type; otherwise return null.
2064 TypeSourceInfo *getFriendType() const {
2065 return Friend.dyn_cast<TypeSourceInfo*>();
2068 /// If this friend declaration names a templated function (or
2069 /// a member function of a templated type), return that type;
2070 /// otherwise return null.
2071 NamedDecl *getFriendDecl() const {
2072 return Friend.dyn_cast<NamedDecl*>();
2075 /// \brief Retrieves the location of the 'friend' keyword.
2076 SourceLocation getFriendLoc() const {
2080 TemplateParameterList *getTemplateParameterList(unsigned i) const {
2081 assert(i <= NumParams);
2085 unsigned getNumTemplateParameters() const {
2089 // Implement isa/cast/dyncast/etc.
2090 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2091 static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
2093 friend class ASTDeclReader;
2096 /// \brief Declaration of an alias template.
2100 /// template \<typename T> using V = std::map<T*, int, MyCompare<T>>;
2102 class TypeAliasTemplateDecl : public RedeclarableTemplateDecl {
2103 static void DeallocateCommon(void *Ptr);
2106 typedef CommonBase Common;
2108 TypeAliasTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
2109 TemplateParameterList *Params, NamedDecl *Decl)
2110 : RedeclarableTemplateDecl(TypeAliasTemplate, DC, L, Name, Params, Decl) { }
2112 CommonBase *newCommon(ASTContext &C) const;
2114 Common *getCommonPtr() {
2115 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2119 /// Get the underlying function declaration of the template.
2120 TypeAliasDecl *getTemplatedDecl() const {
2121 return static_cast<TypeAliasDecl*>(TemplatedDecl);
2125 TypeAliasTemplateDecl *getCanonicalDecl() {
2126 return cast<TypeAliasTemplateDecl>(
2127 RedeclarableTemplateDecl::getCanonicalDecl());
2129 const TypeAliasTemplateDecl *getCanonicalDecl() const {
2130 return cast<TypeAliasTemplateDecl>(
2131 RedeclarableTemplateDecl::getCanonicalDecl());
2134 /// \brief Retrieve the previous declaration of this function template, or
2135 /// NULL if no such declaration exists.
2136 TypeAliasTemplateDecl *getPreviousDecl() {
2137 return cast_or_null<TypeAliasTemplateDecl>(
2138 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2141 /// \brief Retrieve the previous declaration of this function template, or
2142 /// NULL if no such declaration exists.
2143 const TypeAliasTemplateDecl *getPreviousDecl() const {
2144 return cast_or_null<TypeAliasTemplateDecl>(
2145 static_cast<const RedeclarableTemplateDecl *>(
2146 this)->getPreviousDecl());
2149 TypeAliasTemplateDecl *getInstantiatedFromMemberTemplate() {
2150 return cast_or_null<TypeAliasTemplateDecl>(
2151 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
2155 /// \brief Create a function template node.
2156 static TypeAliasTemplateDecl *Create(ASTContext &C, DeclContext *DC,
2158 DeclarationName Name,
2159 TemplateParameterList *Params,
2162 /// \brief Create an empty alias template node.
2163 static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2165 // Implement isa/cast/dyncast support
2166 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2167 static bool classofKind(Kind K) { return K == TypeAliasTemplate; }
2169 friend class ASTDeclReader;
2170 friend class ASTDeclWriter;
2173 /// \brief Declaration of a function specialization at template class scope.
2175 /// This is a non standard extension needed to support MSVC.
2179 /// template <class T>
2181 /// template <class U> void foo(U a) { }
2182 /// template<> void foo(int a) { }
2186 /// "template<> foo(int a)" will be saved in Specialization as a normal
2187 /// CXXMethodDecl. Then during an instantiation of class A, it will be
2188 /// transformed into an actual function specialization.
2189 class ClassScopeFunctionSpecializationDecl : public Decl {
2190 virtual void anchor();
2192 ClassScopeFunctionSpecializationDecl(DeclContext *DC, SourceLocation Loc,
2193 CXXMethodDecl *FD, bool Args,
2194 TemplateArgumentListInfo TemplArgs)
2195 : Decl(Decl::ClassScopeFunctionSpecialization, DC, Loc),
2196 Specialization(FD), HasExplicitTemplateArgs(Args),
2197 TemplateArgs(TemplArgs) {}
2199 ClassScopeFunctionSpecializationDecl(EmptyShell Empty)
2200 : Decl(Decl::ClassScopeFunctionSpecialization, Empty) {}
2202 CXXMethodDecl *Specialization;
2203 bool HasExplicitTemplateArgs;
2204 TemplateArgumentListInfo TemplateArgs;
2207 CXXMethodDecl *getSpecialization() const { return Specialization; }
2208 bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
2209 const TemplateArgumentListInfo& templateArgs() const { return TemplateArgs; }
2211 static ClassScopeFunctionSpecializationDecl *Create(ASTContext &C,
2215 bool HasExplicitTemplateArgs,
2216 TemplateArgumentListInfo TemplateArgs) {
2217 return new (C) ClassScopeFunctionSpecializationDecl(DC , Loc, FD,
2218 HasExplicitTemplateArgs,
2222 static ClassScopeFunctionSpecializationDecl *
2223 CreateDeserialized(ASTContext &Context, unsigned ID);
2225 // Implement isa/cast/dyncast/etc.
2226 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2227 static bool classofKind(Kind K) {
2228 return K == Decl::ClassScopeFunctionSpecialization;
2231 friend class ASTDeclReader;
2232 friend class ASTDeclWriter;
2235 /// Implementation of inline functions that require the template declarations
2236 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
2239 /// \brief Represents a variable template specialization, which refers to
2240 /// a variable template with a given set of template arguments.
2242 /// Variable template specializations represent both explicit
2243 /// specializations of variable templates, as in the example below, and
2244 /// implicit instantiations of variable templates.
2247 /// template<typename T> constexpr T pi = T(3.1415926535897932385);
2250 /// constexpr float pi<float>; // variable template specialization pi<float>
2252 class VarTemplateSpecializationDecl : public VarDecl,
2253 public llvm::FoldingSetNode {
2255 /// \brief Structure that stores information about a variable template
2256 /// specialization that was instantiated from a variable template partial
2258 struct SpecializedPartialSpecialization {
2259 /// \brief The variable template partial specialization from which this
2260 /// variable template specialization was instantiated.
2261 VarTemplatePartialSpecializationDecl *PartialSpecialization;
2263 /// \brief The template argument list deduced for the variable template
2264 /// partial specialization itself.
2265 const TemplateArgumentList *TemplateArgs;
2268 /// \brief The template that this specialization specializes.
2269 llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *>
2270 SpecializedTemplate;
2272 /// \brief Further info for explicit template specialization/instantiation.
2273 struct ExplicitSpecializationInfo {
2274 /// \brief The type-as-written.
2275 TypeSourceInfo *TypeAsWritten;
2276 /// \brief The location of the extern keyword.
2277 SourceLocation ExternLoc;
2278 /// \brief The location of the template keyword.
2279 SourceLocation TemplateKeywordLoc;
2281 ExplicitSpecializationInfo()
2282 : TypeAsWritten(0), ExternLoc(), TemplateKeywordLoc() {}
2285 /// \brief Further info for explicit template specialization/instantiation.
2286 /// Does not apply to implicit specializations.
2287 ExplicitSpecializationInfo *ExplicitInfo;
2289 /// \brief The template arguments used to describe this specialization.
2290 const TemplateArgumentList *TemplateArgs;
2291 TemplateArgumentListInfo TemplateArgsInfo;
2293 /// \brief The point where this template was instantiated (if any).
2294 SourceLocation PointOfInstantiation;
2296 /// \brief The kind of specialization this declaration refers to.
2297 /// Really a value of type TemplateSpecializationKind.
2298 unsigned SpecializationKind : 3;
2301 VarTemplateSpecializationDecl(ASTContext &Context, Kind DK, DeclContext *DC,
2302 SourceLocation StartLoc, SourceLocation IdLoc,
2303 VarTemplateDecl *SpecializedTemplate,
2304 QualType T, TypeSourceInfo *TInfo,
2305 StorageClass S, const TemplateArgument *Args,
2308 explicit VarTemplateSpecializationDecl(Kind DK);
2311 static VarTemplateSpecializationDecl *
2312 Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2313 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
2314 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
2316 static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C,
2319 virtual void getNameForDiagnostic(raw_ostream &OS,
2320 const PrintingPolicy &Policy,
2321 bool Qualified) const;
2323 VarTemplateSpecializationDecl *getMostRecentDecl() {
2324 VarDecl *Recent = static_cast<VarDecl *>(this)->getMostRecentDecl();
2325 return cast<VarTemplateSpecializationDecl>(Recent);
2328 /// \brief Retrieve the template that this specialization specializes.
2329 VarTemplateDecl *getSpecializedTemplate() const;
2331 /// \brief Retrieve the template arguments of the variable template
2333 const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; }
2335 // TODO: Always set this when creating the new specialization?
2336 void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo);
2338 const TemplateArgumentListInfo &getTemplateArgsInfo() const {
2339 return TemplateArgsInfo;
2342 /// \brief Determine the kind of specialization that this
2343 /// declaration represents.
2344 TemplateSpecializationKind getSpecializationKind() const {
2345 return static_cast<TemplateSpecializationKind>(SpecializationKind);
2348 bool isExplicitSpecialization() const {
2349 return getSpecializationKind() == TSK_ExplicitSpecialization;
2352 /// \brief True if this declaration is an explicit specialization,
2353 /// explicit instantiation declaration, or explicit instantiation
2355 bool isExplicitInstantiationOrSpecialization() const {
2356 switch (getTemplateSpecializationKind()) {
2357 case TSK_ExplicitSpecialization:
2358 case TSK_ExplicitInstantiationDeclaration:
2359 case TSK_ExplicitInstantiationDefinition:
2362 case TSK_Undeclared:
2363 case TSK_ImplicitInstantiation:
2366 llvm_unreachable("bad template specialization kind");
2369 void setSpecializationKind(TemplateSpecializationKind TSK) {
2370 SpecializationKind = TSK;
2373 /// \brief Get the point of instantiation (if any), or null if none.
2374 SourceLocation getPointOfInstantiation() const {
2375 return PointOfInstantiation;
2378 void setPointOfInstantiation(SourceLocation Loc) {
2379 assert(Loc.isValid() && "point of instantiation must be valid!");
2380 PointOfInstantiation = Loc;
2383 /// \brief If this variable template specialization is an instantiation of
2384 /// a template (rather than an explicit specialization), return the
2385 /// variable template or variable template partial specialization from which
2386 /// it was instantiated.
2387 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2388 getInstantiatedFrom() const {
2389 if (getSpecializationKind() != TSK_ImplicitInstantiation &&
2390 getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
2391 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
2392 return llvm::PointerUnion<VarTemplateDecl *,
2393 VarTemplatePartialSpecializationDecl *>();
2395 if (SpecializedPartialSpecialization *PartialSpec =
2396 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2397 return PartialSpec->PartialSpecialization;
2399 return SpecializedTemplate.get<VarTemplateDecl *>();
2402 /// \brief Retrieve the variable template or variable template partial
2403 /// specialization which was specialized by this.
2404 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2405 getSpecializedTemplateOrPartial() const {
2406 if (SpecializedPartialSpecialization *PartialSpec =
2407 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2408 return PartialSpec->PartialSpecialization;
2410 return SpecializedTemplate.get<VarTemplateDecl *>();
2413 /// \brief Retrieve the set of template arguments that should be used
2414 /// to instantiate the initializer of the variable template or variable
2415 /// template partial specialization from which this variable template
2416 /// specialization was instantiated.
2418 /// \returns For a variable template specialization instantiated from the
2419 /// primary template, this function will return the same template arguments
2420 /// as getTemplateArgs(). For a variable template specialization instantiated
2421 /// from a variable template partial specialization, this function will the
2422 /// return deduced template arguments for the variable template partial
2423 /// specialization itself.
2424 const TemplateArgumentList &getTemplateInstantiationArgs() const {
2425 if (SpecializedPartialSpecialization *PartialSpec =
2426 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2427 return *PartialSpec->TemplateArgs;
2429 return getTemplateArgs();
2432 /// \brief Note that this variable template specialization is actually an
2433 /// instantiation of the given variable template partial specialization whose
2434 /// template arguments have been deduced.
2435 void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec,
2436 const TemplateArgumentList *TemplateArgs) {
2437 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2438 "Already set to a variable template partial specialization!");
2439 SpecializedPartialSpecialization *PS =
2440 new (getASTContext()) SpecializedPartialSpecialization();
2441 PS->PartialSpecialization = PartialSpec;
2442 PS->TemplateArgs = TemplateArgs;
2443 SpecializedTemplate = PS;
2446 /// \brief Note that this variable template specialization is an instantiation
2447 /// of the given variable template.
2448 void setInstantiationOf(VarTemplateDecl *TemplDecl) {
2449 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2450 "Previously set to a variable template partial specialization!");
2451 SpecializedTemplate = TemplDecl;
2454 /// \brief Sets the type of this specialization as it was written by
2456 void setTypeAsWritten(TypeSourceInfo *T) {
2458 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2459 ExplicitInfo->TypeAsWritten = T;
2461 /// \brief Gets the type of this specialization as it was written by
2462 /// the user, if it was so written.
2463 TypeSourceInfo *getTypeAsWritten() const {
2464 return ExplicitInfo ? ExplicitInfo->TypeAsWritten : 0;
2467 /// \brief Gets the location of the extern keyword, if present.
2468 SourceLocation getExternLoc() const {
2469 return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
2471 /// \brief Sets the location of the extern keyword.
2472 void setExternLoc(SourceLocation Loc) {
2474 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2475 ExplicitInfo->ExternLoc = Loc;
2478 /// \brief Sets the location of the template keyword.
2479 void setTemplateKeywordLoc(SourceLocation Loc) {
2481 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2482 ExplicitInfo->TemplateKeywordLoc = Loc;
2484 /// \brief Gets the location of the template keyword, if present.
2485 SourceLocation getTemplateKeywordLoc() const {
2486 return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
2489 void Profile(llvm::FoldingSetNodeID &ID) const {
2490 Profile(ID, TemplateArgs->data(), TemplateArgs->size(), getASTContext());
2493 static void Profile(llvm::FoldingSetNodeID &ID,
2494 const TemplateArgument *TemplateArgs,
2495 unsigned NumTemplateArgs, ASTContext &Context) {
2496 ID.AddInteger(NumTemplateArgs);
2497 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
2498 TemplateArgs[Arg].Profile(ID, Context);
2501 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2502 static bool classofKind(Kind K) {
2503 return K >= firstVarTemplateSpecialization &&
2504 K <= lastVarTemplateSpecialization;
2507 friend class ASTDeclReader;
2508 friend class ASTDeclWriter;
2511 class VarTemplatePartialSpecializationDecl
2512 : public VarTemplateSpecializationDecl {
2513 virtual void anchor();
2515 /// \brief The list of template parameters
2516 TemplateParameterList *TemplateParams;
2518 /// \brief The source info for the template arguments as written.
2519 /// FIXME: redundant with TypeAsWritten?
2520 const ASTTemplateArgumentListInfo *ArgsAsWritten;
2522 /// \brief The variable template partial specialization from which this
2523 /// variable template partial specialization was instantiated.
2525 /// The boolean value will be true to indicate that this variable template
2526 /// partial specialization was specialized at this level.
2527 llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool>
2528 InstantiatedFromMember;
2530 VarTemplatePartialSpecializationDecl(
2531 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2532 SourceLocation IdLoc, TemplateParameterList *Params,
2533 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
2534 StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
2535 const ASTTemplateArgumentListInfo *ArgInfos);
2537 VarTemplatePartialSpecializationDecl()
2538 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization),
2539 TemplateParams(0), ArgsAsWritten(0), InstantiatedFromMember(0, false) {}
2542 static VarTemplatePartialSpecializationDecl *
2543 Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2544 SourceLocation IdLoc, TemplateParameterList *Params,
2545 VarTemplateDecl *SpecializedTemplate, QualType T,
2546 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
2547 unsigned NumArgs, const TemplateArgumentListInfo &ArgInfos);
2549 static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C,
2552 VarTemplatePartialSpecializationDecl *getMostRecentDecl() {
2553 return cast<VarTemplatePartialSpecializationDecl>(
2554 static_cast<VarTemplateSpecializationDecl *>(
2555 this)->getMostRecentDecl());
2558 /// Get the list of template parameters
2559 TemplateParameterList *getTemplateParameters() const {
2560 return TemplateParams;
2563 /// Get the template arguments as written.
2564 const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
2565 return ArgsAsWritten;
2568 /// \brief Retrieve the member variable template partial specialization from
2569 /// which this particular variable template partial specialization was
2573 /// template<typename T>
2575 /// template<typename U> U Inner;
2576 /// template<typename U> U* Inner<U*> = (U*)(0); // #1
2579 /// template int* Outer<float>::Inner<int*>;
2582 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
2583 /// end up instantiating the partial specialization
2584 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the
2585 /// variable template partial specialization \c Outer<T>::Inner<U*>. Given
2586 /// \c Outer<float>::Inner<U*>, this function would return
2587 /// \c Outer<T>::Inner<U*>.
2588 VarTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
2589 VarTemplatePartialSpecializationDecl *First =
2590 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2591 return First->InstantiatedFromMember.getPointer();
2595 setInstantiatedFromMember(VarTemplatePartialSpecializationDecl *PartialSpec) {
2596 VarTemplatePartialSpecializationDecl *First =
2597 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2598 First->InstantiatedFromMember.setPointer(PartialSpec);
2601 /// \brief Determines whether this variable template partial specialization
2602 /// was a specialization of a member partial specialization.
2604 /// In the following example, the member template partial specialization
2605 /// \c X<int>::Inner<T*> is a member specialization.
2608 /// template<typename T>
2610 /// template<typename U> U Inner;
2611 /// template<typename U> U* Inner<U*> = (U*)(0);
2614 /// template<> template<typename T>
2615 /// U* X<int>::Inner<T*> = (T*)(0) + 1;
2617 bool isMemberSpecialization() {
2618 VarTemplatePartialSpecializationDecl *First =
2619 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2620 return First->InstantiatedFromMember.getInt();
2623 /// \brief Note that this member template is a specialization.
2624 void setMemberSpecialization() {
2625 VarTemplatePartialSpecializationDecl *First =
2626 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2627 assert(First->InstantiatedFromMember.getPointer() &&
2628 "Only member templates can be member template specializations");
2629 return First->InstantiatedFromMember.setInt(true);
2632 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2633 static bool classofKind(Kind K) {
2634 return K == VarTemplatePartialSpecialization;
2637 friend class ASTDeclReader;
2638 friend class ASTDeclWriter;
2641 /// Declaration of a variable template.
2642 class VarTemplateDecl : public RedeclarableTemplateDecl {
2643 static void DeallocateCommon(void *Ptr);
2646 /// \brief Data that is common to all of the declarations of a given
2647 /// variable template.
2648 struct Common : CommonBase {
2649 Common() : LazySpecializations() {}
2651 /// \brief The variable template specializations for this variable
2652 /// template, including explicit specializations and instantiations.
2653 llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations;
2655 /// \brief The variable template partial specializations for this variable
2657 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>
2658 PartialSpecializations;
2660 /// \brief If non-null, points to an array of specializations (including
2661 /// partial specializations) known ownly by their external declaration IDs.
2663 /// The first value in the array is the number of of specializations/
2664 /// partial specializations that follow.
2665 uint32_t *LazySpecializations;
2668 /// \brief Load any lazily-loaded specializations from the external source.
2669 void LoadLazySpecializations() const;
2671 /// \brief Retrieve the set of specializations of this variable template.
2672 llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
2673 getSpecializations() const;
2675 /// \brief Retrieve the set of partial specializations of this class
2677 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
2678 getPartialSpecializations();
2680 VarTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
2681 TemplateParameterList *Params, NamedDecl *Decl)
2682 : RedeclarableTemplateDecl(VarTemplate, DC, L, Name, Params, Decl) {}
2684 VarTemplateDecl(EmptyShell Empty)
2685 : RedeclarableTemplateDecl(VarTemplate, 0, SourceLocation(),
2686 DeclarationName(), 0, 0) {}
2688 CommonBase *newCommon(ASTContext &C) const;
2690 Common *getCommonPtr() const {
2691 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2695 /// \brief Get the underlying variable declarations of the template.
2696 VarDecl *getTemplatedDecl() const {
2697 return static_cast<VarDecl *>(TemplatedDecl);
2700 /// \brief Returns whether this template declaration defines the primary
2701 /// variable pattern.
2702 bool isThisDeclarationADefinition() const {
2703 return getTemplatedDecl()->isThisDeclarationADefinition();
2706 VarTemplateDecl *getDefinition();
2708 /// \brief Create a variable template node.
2709 static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC,
2710 SourceLocation L, DeclarationName Name,
2711 TemplateParameterList *Params, NamedDecl *Decl,
2712 VarTemplateDecl *PrevDecl);
2714 /// \brief Create an empty variable template node.
2715 static VarTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2717 /// \brief Return the specialization with the provided arguments if it exists,
2718 /// otherwise return the insertion point.
2719 VarTemplateSpecializationDecl *
2720 findSpecialization(const TemplateArgument *Args, unsigned NumArgs,
2723 /// \brief Insert the specified specialization knowing that it is not already
2724 /// in. InsertPos must be obtained from findSpecialization.
2725 void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos);
2727 VarTemplateDecl *getCanonicalDecl() {
2728 return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2730 const VarTemplateDecl *getCanonicalDecl() const {
2731 return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2734 /// \brief Retrieve the previous declaration of this variable template, or
2735 /// NULL if no such declaration exists.
2736 VarTemplateDecl *getPreviousDecl() {
2737 return cast_or_null<VarTemplateDecl>(
2738 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2741 /// \brief Retrieve the previous declaration of this variable template, or
2742 /// NULL if no such declaration exists.
2743 const VarTemplateDecl *getPreviousDecl() const {
2744 return cast_or_null<VarTemplateDecl>(
2745 static_cast<const RedeclarableTemplateDecl *>(
2746 this)->getPreviousDecl());
2749 VarTemplateDecl *getInstantiatedFromMemberTemplate() {
2750 return cast_or_null<VarTemplateDecl>(
2751 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
2754 /// \brief Return the partial specialization with the provided arguments if it
2755 /// exists, otherwise return the insertion point.
2756 VarTemplatePartialSpecializationDecl *
2757 findPartialSpecialization(const TemplateArgument *Args, unsigned NumArgs,
2760 /// \brief Insert the specified partial specialization knowing that it is not
2761 /// already in. InsertPos must be obtained from findPartialSpecialization.
2762 void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D,
2765 /// \brief Retrieve the partial specializations as an ordered list.
2766 void getPartialSpecializations(
2767 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS);
2769 /// \brief Find a variable template partial specialization which was
2771 /// from the given member partial specialization.
2773 /// \param D a member variable template partial specialization.
2775 /// \returns the variable template partial specialization which was
2777 /// from the given member partial specialization, or NULL if no such partial
2778 /// specialization exists.
2779 VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember(
2780 VarTemplatePartialSpecializationDecl *D);
2782 typedef SpecIterator<VarTemplateSpecializationDecl> spec_iterator;
2784 spec_iterator spec_begin() const {
2785 return makeSpecIterator(getSpecializations(), false);
2788 spec_iterator spec_end() const {
2789 return makeSpecIterator(getSpecializations(), true);
2792 typedef SpecIterator<VarTemplatePartialSpecializationDecl>
2793 partial_spec_iterator;
2795 partial_spec_iterator partial_spec_begin() {
2796 return makeSpecIterator(getPartialSpecializations(), false);
2799 partial_spec_iterator partial_spec_end() {
2800 return makeSpecIterator(getPartialSpecializations(), true);
2803 // Implement isa/cast/dyncast support
2804 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2805 static bool classofKind(Kind K) { return K == VarTemplate; }
2807 friend class ASTDeclReader;
2808 friend class ASTDeclWriter;
2811 } /* end of namespace clang */