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 ArrayRef<NamedDecl*> asArray() {
90 return llvm::makeArrayRef(begin(), end());
92 ArrayRef<const NamedDecl*> asArray() const {
93 return llvm::makeArrayRef(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 ArrayRef<TemplateArgument> asArray() const {
207 return llvm::makeArrayRef(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 void anchor() override;
232 // This is probably never used.
233 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
234 DeclarationName Name)
235 : NamedDecl(DK, DC, L, Name), TemplatedDecl(nullptr),
236 TemplateParams(nullptr) {}
238 // Construct a template decl with the given name and parameters.
239 // Used when there is not templated element (tt-params).
240 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
241 DeclarationName Name, TemplateParameterList *Params)
242 : NamedDecl(DK, DC, L, Name), TemplatedDecl(nullptr),
243 TemplateParams(Params) {}
245 // Construct a template decl with name, parameters, and templated element.
246 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
247 DeclarationName Name, TemplateParameterList *Params,
249 : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl),
250 TemplateParams(Params) { }
252 /// Get the list of template parameters
253 TemplateParameterList *getTemplateParameters() const {
254 return TemplateParams;
257 /// Get the underlying, templated declaration.
258 NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
260 // Implement isa/cast/dyncast/etc.
261 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
262 static bool classofKind(Kind K) {
263 return K >= firstTemplate && K <= lastTemplate;
266 SourceRange getSourceRange() const override LLVM_READONLY {
267 return SourceRange(TemplateParams->getTemplateLoc(),
268 TemplatedDecl->getSourceRange().getEnd());
272 NamedDecl *TemplatedDecl;
273 TemplateParameterList* TemplateParams;
276 /// \brief Initialize the underlying templated declaration and
277 /// template parameters.
278 void init(NamedDecl *templatedDecl, TemplateParameterList* templateParams) {
279 assert(!TemplatedDecl && "TemplatedDecl already set!");
280 assert(!TemplateParams && "TemplateParams already set!");
281 TemplatedDecl = templatedDecl;
282 TemplateParams = templateParams;
286 /// \brief Provides information about a function template specialization,
287 /// which is a FunctionDecl that has been explicitly specialization or
288 /// instantiated from a function template.
289 class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode {
290 FunctionTemplateSpecializationInfo(FunctionDecl *FD,
291 FunctionTemplateDecl *Template,
292 TemplateSpecializationKind TSK,
293 const TemplateArgumentList *TemplateArgs,
294 const ASTTemplateArgumentListInfo *TemplateArgsAsWritten,
297 Template(Template, TSK - 1),
298 TemplateArguments(TemplateArgs),
299 TemplateArgumentsAsWritten(TemplateArgsAsWritten),
300 PointOfInstantiation(POI) { }
303 static FunctionTemplateSpecializationInfo *
304 Create(ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
305 TemplateSpecializationKind TSK,
306 const TemplateArgumentList *TemplateArgs,
307 const TemplateArgumentListInfo *TemplateArgsAsWritten,
310 /// \brief The function template specialization that this structure
312 FunctionDecl *Function;
314 /// \brief The function template from which this function template
315 /// specialization was generated.
317 /// The two bits are contain the top 4 values of TemplateSpecializationKind.
318 llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
320 /// \brief The template arguments used to produce the function template
321 /// specialization from the function template.
322 const TemplateArgumentList *TemplateArguments;
324 /// \brief The template arguments as written in the sources, if provided.
325 const ASTTemplateArgumentListInfo *TemplateArgumentsAsWritten;
327 /// \brief The point at which this function template specialization was
328 /// first instantiated.
329 SourceLocation PointOfInstantiation;
331 /// \brief Retrieve the template from which this function was specialized.
332 FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
334 /// \brief Determine what kind of template specialization this is.
335 TemplateSpecializationKind getTemplateSpecializationKind() const {
336 return (TemplateSpecializationKind)(Template.getInt() + 1);
339 bool isExplicitSpecialization() const {
340 return getTemplateSpecializationKind() == TSK_ExplicitSpecialization;
343 /// \brief True if this declaration is an explicit specialization,
344 /// explicit instantiation declaration, or explicit instantiation
346 bool isExplicitInstantiationOrSpecialization() const {
347 switch (getTemplateSpecializationKind()) {
348 case TSK_ExplicitSpecialization:
349 case TSK_ExplicitInstantiationDeclaration:
350 case TSK_ExplicitInstantiationDefinition:
354 case TSK_ImplicitInstantiation:
357 llvm_unreachable("bad template specialization kind");
360 /// \brief Set the template specialization kind.
361 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
362 assert(TSK != TSK_Undeclared &&
363 "Cannot encode TSK_Undeclared for a function template specialization");
364 Template.setInt(TSK - 1);
367 /// \brief Retrieve the first point of instantiation of this function
368 /// template specialization.
370 /// The point of instantiation may be an invalid source location if this
371 /// function has yet to be instantiated.
372 SourceLocation getPointOfInstantiation() const {
373 return PointOfInstantiation;
376 /// \brief Set the (first) point of instantiation of this function template
378 void setPointOfInstantiation(SourceLocation POI) {
379 PointOfInstantiation = POI;
382 void Profile(llvm::FoldingSetNodeID &ID) {
383 Profile(ID, TemplateArguments->asArray(),
384 Function->getASTContext());
388 Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
389 ASTContext &Context) {
390 ID.AddInteger(TemplateArgs.size());
391 for (unsigned Arg = 0; Arg != TemplateArgs.size(); ++Arg)
392 TemplateArgs[Arg].Profile(ID, Context);
396 /// \brief Provides information a specialization of a member of a class
397 /// template, which may be a member function, static data member,
398 /// member class or member enumeration.
399 class MemberSpecializationInfo {
400 // The member declaration from which this member was instantiated, and the
401 // manner in which the instantiation occurred (in the lower two bits).
402 llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
404 // The point at which this member was first instantiated.
405 SourceLocation PointOfInstantiation;
409 MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK,
410 SourceLocation POI = SourceLocation())
411 : MemberAndTSK(IF, TSK - 1), PointOfInstantiation(POI) {
412 assert(TSK != TSK_Undeclared &&
413 "Cannot encode undeclared template specializations for members");
416 /// \brief Retrieve the member declaration from which this member was
418 NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
420 /// \brief Determine what kind of template specialization this is.
421 TemplateSpecializationKind getTemplateSpecializationKind() const {
422 return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
425 bool isExplicitSpecialization() const {
426 return getTemplateSpecializationKind() == TSK_ExplicitSpecialization;
429 /// \brief Set the template specialization kind.
430 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
431 assert(TSK != TSK_Undeclared &&
432 "Cannot encode undeclared template specializations for members");
433 MemberAndTSK.setInt(TSK - 1);
436 /// \brief Retrieve the first point of instantiation of this member.
437 /// If the point of instantiation is an invalid location, then this member
438 /// has not yet been instantiated.
439 SourceLocation getPointOfInstantiation() const {
440 return PointOfInstantiation;
443 /// \brief Set the first point of instantiation.
444 void setPointOfInstantiation(SourceLocation POI) {
445 PointOfInstantiation = POI;
449 /// \brief Provides information about a dependent function-template
450 /// specialization declaration.
452 /// Since explicit function template specialization and instantiation
453 /// declarations can only appear in namespace scope, and you can only
454 /// specialize a member of a fully-specialized class, the only way to
455 /// get one of these is in a friend declaration like the following:
458 /// template \<class T> void foo(T);
459 /// template \<class T> class A {
460 /// friend void foo<>(T);
463 class DependentFunctionTemplateSpecializationInfo {
465 /// The number of potential template candidates.
466 unsigned NumTemplates;
468 /// The number of template arguments.
473 // Force sizeof to be a multiple of sizeof(void*) so that the
474 // trailing data is aligned.
479 /// The locations of the left and right angle brackets.
480 SourceRange AngleLocs;
482 FunctionTemplateDecl * const *getTemplates() const {
483 return reinterpret_cast<FunctionTemplateDecl*const*>(this+1);
487 DependentFunctionTemplateSpecializationInfo(
488 const UnresolvedSetImpl &Templates,
489 const TemplateArgumentListInfo &TemplateArgs);
491 /// \brief Returns the number of function templates that this might
492 /// be a specialization of.
493 unsigned getNumTemplates() const {
494 return d.NumTemplates;
497 /// \brief Returns the i'th template candidate.
498 FunctionTemplateDecl *getTemplate(unsigned I) const {
499 assert(I < getNumTemplates() && "template index out of range");
500 return getTemplates()[I];
503 /// \brief Returns the explicit template arguments that were given.
504 const TemplateArgumentLoc *getTemplateArgs() const {
505 return reinterpret_cast<const TemplateArgumentLoc*>(
506 &getTemplates()[getNumTemplates()]);
509 /// \brief Returns the number of explicit template arguments that were given.
510 unsigned getNumTemplateArgs() const {
514 /// \brief Returns the nth template argument.
515 const TemplateArgumentLoc &getTemplateArg(unsigned I) const {
516 assert(I < getNumTemplateArgs() && "template arg index out of range");
517 return getTemplateArgs()[I];
520 SourceLocation getLAngleLoc() const {
521 return AngleLocs.getBegin();
524 SourceLocation getRAngleLoc() const {
525 return AngleLocs.getEnd();
529 /// Declaration of a redeclarable template.
530 class RedeclarableTemplateDecl : public TemplateDecl,
531 public Redeclarable<RedeclarableTemplateDecl>
533 typedef Redeclarable<RedeclarableTemplateDecl> redeclarable_base;
534 RedeclarableTemplateDecl *getNextRedeclarationImpl() override {
535 return getNextRedeclaration();
537 RedeclarableTemplateDecl *getPreviousDeclImpl() override {
538 return getPreviousDecl();
540 RedeclarableTemplateDecl *getMostRecentDeclImpl() override {
541 return getMostRecentDecl();
545 template <typename EntryType> struct SpecEntryTraits {
546 typedef EntryType DeclType;
548 static DeclType *getMostRecentDecl(EntryType *D) {
549 return D->getMostRecentDecl();
553 template <typename EntryType,
554 typename _SETraits = SpecEntryTraits<EntryType>,
555 typename _DeclType = typename _SETraits::DeclType>
556 class SpecIterator : public std::iterator<std::forward_iterator_tag,
557 _DeclType*, ptrdiff_t,
558 _DeclType*, _DeclType*> {
559 typedef _SETraits SETraits;
560 typedef _DeclType DeclType;
562 typedef typename llvm::FoldingSetVector<EntryType>::iterator
565 SetIteratorType SetIter;
568 SpecIterator() : SetIter() {}
569 SpecIterator(SetIteratorType SetIter) : SetIter(SetIter) {}
571 DeclType *operator*() const {
572 return SETraits::getMostRecentDecl(&*SetIter);
574 DeclType *operator->() const { return **this; }
576 SpecIterator &operator++() { ++SetIter; return *this; }
577 SpecIterator operator++(int) {
578 SpecIterator tmp(*this);
583 bool operator==(SpecIterator Other) const {
584 return SetIter == Other.SetIter;
586 bool operator!=(SpecIterator Other) const {
587 return SetIter != Other.SetIter;
591 template <typename EntryType>
592 static SpecIterator<EntryType>
593 makeSpecIterator(llvm::FoldingSetVector<EntryType> &Specs, bool isEnd) {
594 return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin());
597 template <class EntryType> typename SpecEntryTraits<EntryType>::DeclType*
598 findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
599 ArrayRef<TemplateArgument> Args, void *&InsertPos);
602 CommonBase() : InstantiatedFromMember(nullptr, 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, ASTContext &C, DeclContext *DC,
626 SourceLocation L, DeclarationName Name,
627 TemplateParameterList *Params, NamedDecl *Decl)
628 : TemplateDecl(DK, DC, L, Name, Params, Decl), redeclarable_base(C),
632 template <class decl_type> friend class RedeclarableTemplate;
634 /// \brief Retrieves the canonical declaration of this template.
635 RedeclarableTemplateDecl *getCanonicalDecl() override {
636 return getFirstDecl();
638 const RedeclarableTemplateDecl *getCanonicalDecl() const {
639 return getFirstDecl();
642 /// \brief Determines whether this template was a specialization of a
645 /// In the following example, the function template \c X<int>::f and the
646 /// member template \c X<int>::Inner are member specializations.
649 /// template<typename T>
651 /// template<typename U> void f(T, U);
652 /// template<typename U> struct Inner;
655 /// template<> template<typename T>
656 /// void X<int>::f(int, T);
657 /// template<> template<typename T>
658 /// struct X<int>::Inner { /* ... */ };
660 bool isMemberSpecialization() const {
661 return getCommonPtr()->InstantiatedFromMember.getInt();
664 /// \brief Note that this member template is a specialization.
665 void setMemberSpecialization() {
666 assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
667 "Only member templates can be member template specializations");
668 getCommonPtr()->InstantiatedFromMember.setInt(true);
671 /// \brief Retrieve the member template from which this template was
672 /// instantiated, or NULL if this template was not instantiated from a
675 /// A template is instantiated from a member template when the member
676 /// template itself is part of a class template (or member thereof). For
680 /// template<typename T>
682 /// template<typename U> void f(T, U);
685 /// void test(X<int> x) {
690 /// \c X<int>::f is a FunctionTemplateDecl that describes the function
694 /// template<typename U> void X<int>::f(int, U);
697 /// which was itself created during the instantiation of \c X<int>. Calling
698 /// getInstantiatedFromMemberTemplate() on this FunctionTemplateDecl will
699 /// retrieve the FunctionTemplateDecl for the original template \c f within
700 /// the class template \c X<T>, i.e.,
703 /// template<typename T>
704 /// template<typename U>
705 /// void X<T>::f(T, U);
707 RedeclarableTemplateDecl *getInstantiatedFromMemberTemplate() const {
708 return getCommonPtr()->InstantiatedFromMember.getPointer();
711 void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD) {
712 assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
713 getCommonPtr()->InstantiatedFromMember.setPointer(TD);
716 typedef redeclarable_base::redecl_range redecl_range;
717 typedef redeclarable_base::redecl_iterator redecl_iterator;
718 using redeclarable_base::redecls_begin;
719 using redeclarable_base::redecls_end;
720 using redeclarable_base::redecls;
721 using redeclarable_base::getPreviousDecl;
722 using redeclarable_base::getMostRecentDecl;
723 using redeclarable_base::isFirstDecl;
725 // Implement isa/cast/dyncast/etc.
726 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
727 static bool classofKind(Kind K) {
728 return K >= firstRedeclarableTemplate && K <= lastRedeclarableTemplate;
731 friend class ASTReader;
732 friend class ASTDeclReader;
733 friend class ASTDeclWriter;
736 template <> struct RedeclarableTemplateDecl::
737 SpecEntryTraits<FunctionTemplateSpecializationInfo> {
738 typedef FunctionDecl DeclType;
741 getMostRecentDecl(FunctionTemplateSpecializationInfo *I) {
742 return I->Function->getMostRecentDecl();
746 /// Declaration of a template function.
747 class FunctionTemplateDecl : public RedeclarableTemplateDecl {
748 static void DeallocateCommon(void *Ptr);
751 /// \brief Data that is common to all of the declarations of a given
752 /// function template.
753 struct Common : CommonBase {
754 Common() : InjectedArgs(), LazySpecializations() { }
756 /// \brief The function template specializations for this function
757 /// template, including explicit specializations and instantiations.
758 llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> Specializations;
760 /// \brief The set of "injected" template arguments used within this
761 /// function template.
763 /// This pointer refers to the template arguments (there are as
764 /// many template arguments as template parameaters) for the function
765 /// template, and is allocated lazily, since most function templates do not
766 /// require the use of this information.
767 TemplateArgument *InjectedArgs;
769 /// \brief If non-null, points to an array of specializations known only
770 /// by their external declaration IDs.
772 /// The first value in the array is the number of of specializations
774 uint32_t *LazySpecializations;
777 FunctionTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
778 DeclarationName Name, TemplateParameterList *Params,
780 : RedeclarableTemplateDecl(FunctionTemplate, C, DC, L, Name, Params,
783 CommonBase *newCommon(ASTContext &C) const override;
785 Common *getCommonPtr() const {
786 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
789 friend class FunctionDecl;
791 /// \brief Load any lazily-loaded specializations from the external source.
792 void LoadLazySpecializations() const;
794 /// \brief Retrieve the set of function template specializations of this
795 /// function template.
796 llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
797 getSpecializations() const;
799 /// \brief Add a specialization of this function template.
801 /// \param InsertPos Insert position in the FoldingSetVector, must have been
802 /// retrieved by an earlier call to findSpecialization().
803 void addSpecialization(FunctionTemplateSpecializationInfo* Info,
807 /// Get the underlying function declaration of the template.
808 FunctionDecl *getTemplatedDecl() const {
809 return static_cast<FunctionDecl*>(TemplatedDecl);
812 /// Returns whether this template declaration defines the primary
814 bool isThisDeclarationADefinition() const {
815 return getTemplatedDecl()->isThisDeclarationADefinition();
818 /// \brief Return the specialization with the provided arguments if it exists,
819 /// otherwise return the insertion point.
820 FunctionDecl *findSpecialization(ArrayRef<TemplateArgument> Args,
823 FunctionTemplateDecl *getCanonicalDecl() override {
824 return cast<FunctionTemplateDecl>(
825 RedeclarableTemplateDecl::getCanonicalDecl());
827 const FunctionTemplateDecl *getCanonicalDecl() const {
828 return cast<FunctionTemplateDecl>(
829 RedeclarableTemplateDecl::getCanonicalDecl());
832 /// \brief Retrieve the previous declaration of this function template, or
833 /// NULL if no such declaration exists.
834 FunctionTemplateDecl *getPreviousDecl() {
835 return cast_or_null<FunctionTemplateDecl>(
836 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
839 /// \brief Retrieve the previous declaration of this function template, or
840 /// NULL if no such declaration exists.
841 const FunctionTemplateDecl *getPreviousDecl() const {
842 return cast_or_null<FunctionTemplateDecl>(
843 static_cast<const RedeclarableTemplateDecl *>(this)->getPreviousDecl());
846 FunctionTemplateDecl *getInstantiatedFromMemberTemplate() {
847 return cast_or_null<FunctionTemplateDecl>(
848 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
851 typedef SpecIterator<FunctionTemplateSpecializationInfo> spec_iterator;
852 typedef llvm::iterator_range<spec_iterator> spec_range;
854 spec_range specializations() const {
855 return spec_range(spec_begin(), spec_end());
857 spec_iterator spec_begin() const {
858 return makeSpecIterator(getSpecializations(), false);
861 spec_iterator spec_end() const {
862 return makeSpecIterator(getSpecializations(), true);
865 /// \brief Retrieve the "injected" template arguments that correspond to the
866 /// template parameters of this function template.
868 /// Although the C++ standard has no notion of the "injected" template
869 /// arguments for a function template, the notion is convenient when
870 /// we need to perform substitutions inside the definition of a function
872 ArrayRef<TemplateArgument> getInjectedTemplateArgs();
874 /// \brief Create a function template node.
875 static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
877 DeclarationName Name,
878 TemplateParameterList *Params,
881 /// \brief Create an empty function template node.
882 static FunctionTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
884 // Implement isa/cast/dyncast support
885 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
886 static bool classofKind(Kind K) { return K == FunctionTemplate; }
888 friend class ASTDeclReader;
889 friend class ASTDeclWriter;
892 //===----------------------------------------------------------------------===//
893 // Kinds of Template Parameters
894 //===----------------------------------------------------------------------===//
896 /// \brief Defines the position of a template parameter within a template
899 /// Because template parameter can be listed
900 /// sequentially for out-of-line template members, each template parameter is
901 /// given a Depth - the nesting of template parameter scopes - and a Position -
902 /// the occurrence within the parameter list.
903 /// This class is inheritedly privately by different kinds of template
904 /// parameters and is not part of the Decl hierarchy. Just a facility.
905 class TemplateParmPosition {
906 TemplateParmPosition() LLVM_DELETED_FUNCTION;
909 TemplateParmPosition(unsigned D, unsigned P)
910 : Depth(D), Position(P)
913 // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
919 /// Get the nesting depth of the template parameter.
920 unsigned getDepth() const { return Depth; }
921 void setDepth(unsigned D) { Depth = D; }
923 /// Get the position of the template parameter within its parameter list.
924 unsigned getPosition() const { return Position; }
925 void setPosition(unsigned P) { Position = P; }
927 /// Get the index of the template parameter within its parameter list.
928 unsigned getIndex() const { return Position; }
931 /// \brief Declaration of a template type parameter.
933 /// For example, "T" in
935 /// template<typename T> class vector;
937 class TemplateTypeParmDecl : public TypeDecl {
938 /// \brief Whether this template type parameter was declaration with
939 /// the 'typename' keyword.
941 /// If false, it was declared with the 'class' keyword.
944 /// \brief Whether this template type parameter inherited its
945 /// default argument.
946 bool InheritedDefault : 1;
948 /// \brief The default template argument, if any.
949 TypeSourceInfo *DefaultArgument;
951 TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
952 SourceLocation IdLoc, IdentifierInfo *Id,
954 : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename),
955 InheritedDefault(false), DefaultArgument() { }
957 /// Sema creates these on the stack during auto type deduction.
961 static TemplateTypeParmDecl *Create(const ASTContext &C, DeclContext *DC,
962 SourceLocation KeyLoc,
963 SourceLocation NameLoc,
964 unsigned D, unsigned P,
965 IdentifierInfo *Id, bool Typename,
967 static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
970 /// \brief Whether this template type parameter was declared with
971 /// the 'typename' keyword.
973 /// If not, it was declared with the 'class' keyword.
974 bool wasDeclaredWithTypename() const { return Typename; }
976 /// \brief Determine whether this template parameter has a default
978 bool hasDefaultArgument() const { return DefaultArgument != nullptr; }
980 /// \brief Retrieve the default argument, if any.
981 QualType getDefaultArgument() const { return DefaultArgument->getType(); }
983 /// \brief Retrieves the default argument's source information, if any.
984 TypeSourceInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
986 /// \brief Retrieves the location of the default argument declaration.
987 SourceLocation getDefaultArgumentLoc() const;
989 /// \brief Determines whether the default argument was inherited
990 /// from a previous declaration of this template.
991 bool defaultArgumentWasInherited() const { return InheritedDefault; }
993 /// \brief Set the default argument for this template parameter, and
994 /// whether that default argument was inherited from another
996 void setDefaultArgument(TypeSourceInfo *DefArg, bool Inherited) {
997 DefaultArgument = DefArg;
998 InheritedDefault = Inherited;
1001 /// \brief Removes the default argument of this template parameter.
1002 void removeDefaultArgument() {
1003 DefaultArgument = nullptr;
1004 InheritedDefault = false;
1007 /// \brief Set whether this template type parameter was declared with
1008 /// the 'typename' or 'class' keyword.
1009 void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; }
1011 /// \brief Retrieve the depth of the template parameter.
1012 unsigned getDepth() const;
1014 /// \brief Retrieve the index of the template parameter.
1015 unsigned getIndex() const;
1017 /// \brief Returns whether this is a parameter pack.
1018 bool isParameterPack() const;
1020 SourceRange getSourceRange() const override LLVM_READONLY;
1022 // Implement isa/cast/dyncast/etc.
1023 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1024 static bool classofKind(Kind K) { return K == TemplateTypeParm; }
1027 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
1030 /// template<int Size> class array { };
1032 class NonTypeTemplateParmDecl
1033 : public DeclaratorDecl, protected TemplateParmPosition {
1034 /// \brief The default template argument, if any, and whether or not
1035 /// it was inherited.
1036 llvm::PointerIntPair<Expr*, 1, bool> DefaultArgumentAndInherited;
1038 // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index
1039 // down here to save memory.
1041 /// \brief Whether this non-type template parameter is a parameter pack.
1044 /// \brief Whether this non-type template parameter is an "expanded"
1045 /// parameter pack, meaning that its type is a pack expansion and we
1046 /// already know the set of types that expansion expands to.
1047 bool ExpandedParameterPack;
1049 /// \brief The number of types in an expanded parameter pack.
1050 unsigned NumExpandedTypes;
1052 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1053 SourceLocation IdLoc, unsigned D, unsigned P,
1054 IdentifierInfo *Id, QualType T,
1055 bool ParameterPack, TypeSourceInfo *TInfo)
1056 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
1057 TemplateParmPosition(D, P), DefaultArgumentAndInherited(nullptr, false),
1058 ParameterPack(ParameterPack), ExpandedParameterPack(false),
1062 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1063 SourceLocation IdLoc, unsigned D, unsigned P,
1064 IdentifierInfo *Id, QualType T,
1065 TypeSourceInfo *TInfo,
1066 const QualType *ExpandedTypes,
1067 unsigned NumExpandedTypes,
1068 TypeSourceInfo **ExpandedTInfos);
1070 friend class ASTDeclReader;
1073 static NonTypeTemplateParmDecl *
1074 Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1075 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1076 QualType T, bool ParameterPack, TypeSourceInfo *TInfo);
1078 static NonTypeTemplateParmDecl *
1079 Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1080 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1081 QualType T, TypeSourceInfo *TInfo,
1082 const QualType *ExpandedTypes, unsigned NumExpandedTypes,
1083 TypeSourceInfo **ExpandedTInfos);
1085 static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1087 static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1089 unsigned NumExpandedTypes);
1091 using TemplateParmPosition::getDepth;
1092 using TemplateParmPosition::setDepth;
1093 using TemplateParmPosition::getPosition;
1094 using TemplateParmPosition::setPosition;
1095 using TemplateParmPosition::getIndex;
1097 SourceRange getSourceRange() const override LLVM_READONLY;
1099 /// \brief Determine whether this template parameter has a default
1101 bool hasDefaultArgument() const {
1102 return DefaultArgumentAndInherited.getPointer() != nullptr;
1105 /// \brief Retrieve the default argument, if any.
1106 Expr *getDefaultArgument() const {
1107 return DefaultArgumentAndInherited.getPointer();
1110 /// \brief Retrieve the location of the default argument, if any.
1111 SourceLocation getDefaultArgumentLoc() const;
1113 /// \brief Determines whether the default argument was inherited
1114 /// from a previous declaration of this template.
1115 bool defaultArgumentWasInherited() const {
1116 return DefaultArgumentAndInherited.getInt();
1119 /// \brief Set the default argument for this template parameter, and
1120 /// whether that default argument was inherited from another
1122 void setDefaultArgument(Expr *DefArg, bool Inherited) {
1123 DefaultArgumentAndInherited.setPointer(DefArg);
1124 DefaultArgumentAndInherited.setInt(Inherited);
1127 /// \brief Removes the default argument of this template parameter.
1128 void removeDefaultArgument() {
1129 DefaultArgumentAndInherited.setPointer(nullptr);
1130 DefaultArgumentAndInherited.setInt(false);
1133 /// \brief Whether this parameter is a non-type template parameter pack.
1135 /// If the parameter is a parameter pack, the type may be a
1136 /// \c PackExpansionType. In the following example, the \c Dims parameter
1137 /// is a parameter pack (whose type is 'unsigned').
1140 /// template<typename T, unsigned ...Dims> struct multi_array;
1142 bool isParameterPack() const { return ParameterPack; }
1144 /// \brief Whether this parameter pack is a pack expansion.
1146 /// A non-type template parameter pack is a pack expansion if its type
1147 /// contains an unexpanded parameter pack. In this case, we will have
1148 /// built a PackExpansionType wrapping the type.
1149 bool isPackExpansion() const {
1150 return ParameterPack && getType()->getAs<PackExpansionType>();
1153 /// \brief Whether this parameter is a non-type template parameter pack
1154 /// that has a known list of different types at different positions.
1156 /// A parameter pack is an expanded parameter pack when the original
1157 /// parameter pack's type was itself a pack expansion, and that expansion
1158 /// has already been expanded. For example, given:
1161 /// template<typename ...Types>
1163 /// template<Types ...Values>
1164 /// struct Y { /* ... */ };
1168 /// The parameter pack \c Values has a \c PackExpansionType as its type,
1169 /// which expands \c Types. When \c Types is supplied with template arguments
1170 /// by instantiating \c X, the instantiation of \c Values becomes an
1171 /// expanded parameter pack. For example, instantiating
1172 /// \c X<int, unsigned int> results in \c Values being an expanded parameter
1173 /// pack with expansion types \c int and \c unsigned int.
1175 /// The \c getExpansionType() and \c getExpansionTypeSourceInfo() functions
1176 /// return the expansion types.
1177 bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1179 /// \brief Retrieves the number of expansion types in an expanded parameter
1181 unsigned getNumExpansionTypes() const {
1182 assert(ExpandedParameterPack && "Not an expansion parameter pack");
1183 return NumExpandedTypes;
1186 /// \brief Retrieve a particular expansion type within an expanded parameter
1188 QualType getExpansionType(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 QualType::getFromOpaquePtr(TypesAndInfos[2*I]);
1194 /// \brief Retrieve a particular expansion type source info within an
1195 /// expanded parameter pack.
1196 TypeSourceInfo *getExpansionTypeSourceInfo(unsigned I) const {
1197 assert(I < NumExpandedTypes && "Out-of-range expansion type index");
1198 void * const *TypesAndInfos = reinterpret_cast<void * const*>(this + 1);
1199 return static_cast<TypeSourceInfo *>(TypesAndInfos[2*I+1]);
1202 // Implement isa/cast/dyncast/etc.
1203 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1204 static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
1207 /// TemplateTemplateParmDecl - Declares a template template parameter,
1210 /// template <template <typename> class T> class container { };
1212 /// A template template parameter is a TemplateDecl because it defines the
1213 /// name of a template and the template parameters allowable for substitution.
1214 class TemplateTemplateParmDecl : public TemplateDecl,
1215 protected TemplateParmPosition
1217 void anchor() override;
1219 /// DefaultArgument - The default template argument, if any.
1220 TemplateArgumentLoc DefaultArgument;
1221 /// Whether or not the default argument was inherited.
1222 bool DefaultArgumentWasInherited;
1224 /// \brief Whether this parameter is a parameter pack.
1227 /// \brief Whether this template template parameter is an "expanded"
1228 /// parameter pack, meaning that it is a pack expansion and we
1229 /// already know the set of template parameters that expansion expands to.
1230 bool ExpandedParameterPack;
1232 /// \brief The number of parameters in an expanded parameter pack.
1233 unsigned NumExpandedParams;
1235 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
1236 unsigned D, unsigned P, bool ParameterPack,
1237 IdentifierInfo *Id, TemplateParameterList *Params)
1238 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
1239 TemplateParmPosition(D, P), DefaultArgument(),
1240 DefaultArgumentWasInherited(false), ParameterPack(ParameterPack),
1241 ExpandedParameterPack(false), NumExpandedParams(0)
1244 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
1245 unsigned D, unsigned P,
1246 IdentifierInfo *Id, TemplateParameterList *Params,
1247 unsigned NumExpansions,
1248 TemplateParameterList * const *Expansions);
1251 static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1252 SourceLocation L, unsigned D,
1253 unsigned P, bool ParameterPack,
1255 TemplateParameterList *Params);
1256 static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1257 SourceLocation L, unsigned D,
1260 TemplateParameterList *Params,
1261 ArrayRef<TemplateParameterList *> Expansions);
1263 static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1265 static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1267 unsigned NumExpansions);
1269 using TemplateParmPosition::getDepth;
1270 using TemplateParmPosition::getPosition;
1271 using TemplateParmPosition::getIndex;
1273 /// \brief Whether this template template parameter is a template
1277 /// template<template <class T> ...MetaFunctions> struct Apply;
1279 bool isParameterPack() const { return ParameterPack; }
1281 /// \brief Whether this parameter pack is a pack expansion.
1283 /// A template template parameter pack is a pack expansion if its template
1284 /// parameter list contains an unexpanded parameter pack.
1285 bool isPackExpansion() const {
1286 return ParameterPack &&
1287 getTemplateParameters()->containsUnexpandedParameterPack();
1290 /// \brief Whether this parameter is a template template parameter pack that
1291 /// has a known list of different template parameter lists at different
1294 /// A parameter pack is an expanded parameter pack when the original parameter
1295 /// pack's template parameter list was itself a pack expansion, and that
1296 /// expansion has already been expanded. For exampe, given:
1299 /// template<typename...Types> struct Outer {
1300 /// template<template<Types> class...Templates> struct Inner;
1304 /// The parameter pack \c Templates is a pack expansion, which expands the
1305 /// pack \c Types. When \c Types is supplied with template arguments by
1306 /// instantiating \c Outer, the instantiation of \c Templates is an expanded
1308 bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1310 /// \brief Retrieves the number of expansion template parameters in
1311 /// an expanded parameter pack.
1312 unsigned getNumExpansionTemplateParameters() const {
1313 assert(ExpandedParameterPack && "Not an expansion parameter pack");
1314 return NumExpandedParams;
1317 /// \brief Retrieve a particular expansion type within an expanded parameter
1319 TemplateParameterList *getExpansionTemplateParameters(unsigned I) const {
1320 assert(I < NumExpandedParams && "Out-of-range expansion type index");
1321 return reinterpret_cast<TemplateParameterList *const *>(this + 1)[I];
1324 /// \brief Determine whether this template parameter has a default
1326 bool hasDefaultArgument() const {
1327 return !DefaultArgument.getArgument().isNull();
1330 /// \brief Retrieve the default argument, if any.
1331 const TemplateArgumentLoc &getDefaultArgument() const {
1332 return DefaultArgument;
1335 /// \brief Retrieve the location of the default argument, if any.
1336 SourceLocation getDefaultArgumentLoc() const;
1338 /// \brief Determines whether the default argument was inherited
1339 /// from a previous declaration of this template.
1340 bool defaultArgumentWasInherited() const {
1341 return DefaultArgumentWasInherited;
1344 /// \brief Set the default argument for this template parameter, and
1345 /// whether that default argument was inherited from another
1347 void setDefaultArgument(const TemplateArgumentLoc &DefArg, bool Inherited) {
1348 DefaultArgument = DefArg;
1349 DefaultArgumentWasInherited = Inherited;
1352 /// \brief Removes the default argument of this template parameter.
1353 void removeDefaultArgument() {
1354 DefaultArgument = TemplateArgumentLoc();
1355 DefaultArgumentWasInherited = false;
1358 SourceRange getSourceRange() const override LLVM_READONLY {
1359 SourceLocation End = getLocation();
1360 if (hasDefaultArgument() && !defaultArgumentWasInherited())
1361 End = getDefaultArgument().getSourceRange().getEnd();
1362 return SourceRange(getTemplateParameters()->getTemplateLoc(), End);
1365 // Implement isa/cast/dyncast/etc.
1366 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1367 static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
1369 friend class ASTDeclReader;
1370 friend class ASTDeclWriter;
1373 /// \brief Represents a class template specialization, which refers to
1374 /// a class template with a given set of template arguments.
1376 /// Class template specializations represent both explicit
1377 /// specialization of class templates, as in the example below, and
1378 /// implicit instantiations of class templates.
1381 /// template<typename T> class array;
1384 /// class array<bool> { }; // class template specialization array<bool>
1386 class ClassTemplateSpecializationDecl
1387 : public CXXRecordDecl, public llvm::FoldingSetNode {
1389 /// \brief Structure that stores information about a class template
1390 /// specialization that was instantiated from a class template partial
1392 struct SpecializedPartialSpecialization {
1393 /// \brief The class template partial specialization from which this
1394 /// class template specialization was instantiated.
1395 ClassTemplatePartialSpecializationDecl *PartialSpecialization;
1397 /// \brief The template argument list deduced for the class template
1398 /// partial specialization itself.
1399 const TemplateArgumentList *TemplateArgs;
1402 /// \brief The template that this specialization specializes
1403 llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
1404 SpecializedTemplate;
1406 /// \brief Further info for explicit template specialization/instantiation.
1407 struct ExplicitSpecializationInfo {
1408 /// \brief The type-as-written.
1409 TypeSourceInfo *TypeAsWritten;
1410 /// \brief The location of the extern keyword.
1411 SourceLocation ExternLoc;
1412 /// \brief The location of the template keyword.
1413 SourceLocation TemplateKeywordLoc;
1415 ExplicitSpecializationInfo()
1416 : TypeAsWritten(nullptr), ExternLoc(), TemplateKeywordLoc() {}
1419 /// \brief Further info for explicit template specialization/instantiation.
1420 /// Does not apply to implicit specializations.
1421 ExplicitSpecializationInfo *ExplicitInfo;
1423 /// \brief The template arguments used to describe this specialization.
1424 const TemplateArgumentList *TemplateArgs;
1426 /// \brief The point where this template was instantiated (if any)
1427 SourceLocation PointOfInstantiation;
1429 /// \brief The kind of specialization this declaration refers to.
1430 /// Really a value of type TemplateSpecializationKind.
1431 unsigned SpecializationKind : 3;
1434 ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
1435 DeclContext *DC, SourceLocation StartLoc,
1436 SourceLocation IdLoc,
1437 ClassTemplateDecl *SpecializedTemplate,
1438 const TemplateArgument *Args,
1440 ClassTemplateSpecializationDecl *PrevDecl);
1442 explicit ClassTemplateSpecializationDecl(ASTContext &C, Kind DK);
1445 static ClassTemplateSpecializationDecl *
1446 Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1447 SourceLocation StartLoc, SourceLocation IdLoc,
1448 ClassTemplateDecl *SpecializedTemplate,
1449 const TemplateArgument *Args,
1451 ClassTemplateSpecializationDecl *PrevDecl);
1452 static ClassTemplateSpecializationDecl *
1453 CreateDeserialized(ASTContext &C, unsigned ID);
1455 void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
1456 bool Qualified) const override;
1458 // FIXME: This is broken. CXXRecordDecl::getMostRecentDecl() returns a
1459 // different "most recent" declaration from this function for the same
1460 // declaration, because we don't override getMostRecentDeclImpl(). But
1461 // it's not clear that we should override that, because the most recent
1462 // declaration as a CXXRecordDecl sometimes is the injected-class-name.
1463 ClassTemplateSpecializationDecl *getMostRecentDecl() {
1464 CXXRecordDecl *Recent = static_cast<CXXRecordDecl *>(
1465 this)->getMostRecentDecl();
1466 while (!isa<ClassTemplateSpecializationDecl>(Recent)) {
1467 // FIXME: Does injected class name need to be in the redeclarations chain?
1468 assert(Recent->isInjectedClassName() && Recent->getPreviousDecl());
1469 Recent = Recent->getPreviousDecl();
1471 return cast<ClassTemplateSpecializationDecl>(Recent);
1474 /// \brief Retrieve the template that this specialization specializes.
1475 ClassTemplateDecl *getSpecializedTemplate() const;
1477 /// \brief Retrieve the template arguments of the class template
1479 const TemplateArgumentList &getTemplateArgs() const {
1480 return *TemplateArgs;
1483 /// \brief Determine the kind of specialization that this
1484 /// declaration represents.
1485 TemplateSpecializationKind getSpecializationKind() const {
1486 return static_cast<TemplateSpecializationKind>(SpecializationKind);
1489 bool isExplicitSpecialization() const {
1490 return getSpecializationKind() == TSK_ExplicitSpecialization;
1493 /// \brief True if this declaration is an explicit specialization,
1494 /// explicit instantiation declaration, or explicit instantiation
1496 bool isExplicitInstantiationOrSpecialization() const {
1497 switch (getTemplateSpecializationKind()) {
1498 case TSK_ExplicitSpecialization:
1499 case TSK_ExplicitInstantiationDeclaration:
1500 case TSK_ExplicitInstantiationDefinition:
1503 case TSK_Undeclared:
1504 case TSK_ImplicitInstantiation:
1507 llvm_unreachable("bad template specialization kind");
1510 void setSpecializationKind(TemplateSpecializationKind TSK) {
1511 SpecializationKind = TSK;
1514 /// \brief Get the point of instantiation (if any), or null if none.
1515 SourceLocation getPointOfInstantiation() const {
1516 return PointOfInstantiation;
1519 void setPointOfInstantiation(SourceLocation Loc) {
1520 assert(Loc.isValid() && "point of instantiation must be valid!");
1521 PointOfInstantiation = Loc;
1524 /// \brief If this class template specialization is an instantiation of
1525 /// a template (rather than an explicit specialization), return the
1526 /// class template or class template partial specialization from which it
1527 /// was instantiated.
1528 llvm::PointerUnion<ClassTemplateDecl *,
1529 ClassTemplatePartialSpecializationDecl *>
1530 getInstantiatedFrom() const {
1531 if (!isTemplateInstantiation(getSpecializationKind()))
1532 return llvm::PointerUnion<ClassTemplateDecl *,
1533 ClassTemplatePartialSpecializationDecl *>();
1535 return getSpecializedTemplateOrPartial();
1538 /// \brief Retrieve the class template or class template partial
1539 /// specialization which was specialized by this.
1540 llvm::PointerUnion<ClassTemplateDecl *,
1541 ClassTemplatePartialSpecializationDecl *>
1542 getSpecializedTemplateOrPartial() const {
1543 if (SpecializedPartialSpecialization *PartialSpec
1544 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
1545 return PartialSpec->PartialSpecialization;
1547 return SpecializedTemplate.get<ClassTemplateDecl*>();
1550 /// \brief Retrieve the set of template arguments that should be used
1551 /// to instantiate members of the class template or class template partial
1552 /// specialization from which this class template specialization was
1555 /// \returns For a class template specialization instantiated from the primary
1556 /// template, this function will return the same template arguments as
1557 /// getTemplateArgs(). For a class template specialization instantiated from
1558 /// a class template partial specialization, this function will return the
1559 /// deduced template arguments for the class template partial specialization
1561 const TemplateArgumentList &getTemplateInstantiationArgs() const {
1562 if (SpecializedPartialSpecialization *PartialSpec
1563 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
1564 return *PartialSpec->TemplateArgs;
1566 return getTemplateArgs();
1569 /// \brief Note that this class template specialization is actually an
1570 /// instantiation of the given class template partial specialization whose
1571 /// template arguments have been deduced.
1572 void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
1573 const TemplateArgumentList *TemplateArgs) {
1574 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1575 "Already set to a class template partial specialization!");
1576 SpecializedPartialSpecialization *PS
1577 = new (getASTContext()) SpecializedPartialSpecialization();
1578 PS->PartialSpecialization = PartialSpec;
1579 PS->TemplateArgs = TemplateArgs;
1580 SpecializedTemplate = PS;
1583 /// \brief Note that this class template specialization is an instantiation
1584 /// of the given class template.
1585 void setInstantiationOf(ClassTemplateDecl *TemplDecl) {
1586 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1587 "Previously set to a class template partial specialization!");
1588 SpecializedTemplate = TemplDecl;
1591 /// \brief Sets the type of this specialization as it was written by
1592 /// the user. This will be a class template specialization type.
1593 void setTypeAsWritten(TypeSourceInfo *T) {
1595 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1596 ExplicitInfo->TypeAsWritten = T;
1598 /// \brief Gets the type of this specialization as it was written by
1599 /// the user, if it was so written.
1600 TypeSourceInfo *getTypeAsWritten() const {
1601 return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
1604 /// \brief Gets the location of the extern keyword, if present.
1605 SourceLocation getExternLoc() const {
1606 return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
1608 /// \brief Sets the location of the extern keyword.
1609 void setExternLoc(SourceLocation Loc) {
1611 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1612 ExplicitInfo->ExternLoc = Loc;
1615 /// \brief Sets the location of the template keyword.
1616 void setTemplateKeywordLoc(SourceLocation Loc) {
1618 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1619 ExplicitInfo->TemplateKeywordLoc = Loc;
1621 /// \brief Gets the location of the template keyword, if present.
1622 SourceLocation getTemplateKeywordLoc() const {
1623 return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
1626 SourceRange getSourceRange() const override LLVM_READONLY;
1628 void Profile(llvm::FoldingSetNodeID &ID) const {
1629 Profile(ID, TemplateArgs->asArray(), getASTContext());
1633 Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
1634 ASTContext &Context) {
1635 ID.AddInteger(TemplateArgs.size());
1636 for (unsigned Arg = 0; Arg != TemplateArgs.size(); ++Arg)
1637 TemplateArgs[Arg].Profile(ID, Context);
1640 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1641 static bool classofKind(Kind K) {
1642 return K >= firstClassTemplateSpecialization &&
1643 K <= lastClassTemplateSpecialization;
1646 friend class ASTDeclReader;
1647 friend class ASTDeclWriter;
1650 class ClassTemplatePartialSpecializationDecl
1651 : public ClassTemplateSpecializationDecl {
1652 void anchor() override;
1654 /// \brief The list of template parameters
1655 TemplateParameterList* TemplateParams;
1657 /// \brief The source info for the template arguments as written.
1658 /// FIXME: redundant with TypeAsWritten?
1659 const ASTTemplateArgumentListInfo *ArgsAsWritten;
1661 /// \brief The class template partial specialization from which this
1662 /// class template partial specialization was instantiated.
1664 /// The boolean value will be true to indicate that this class template
1665 /// partial specialization was specialized at this level.
1666 llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
1667 InstantiatedFromMember;
1669 ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
1671 SourceLocation StartLoc,
1672 SourceLocation IdLoc,
1673 TemplateParameterList *Params,
1674 ClassTemplateDecl *SpecializedTemplate,
1675 const TemplateArgument *Args,
1677 const ASTTemplateArgumentListInfo *ArgsAsWritten,
1678 ClassTemplatePartialSpecializationDecl *PrevDecl);
1680 ClassTemplatePartialSpecializationDecl(ASTContext &C)
1681 : ClassTemplateSpecializationDecl(C, ClassTemplatePartialSpecialization),
1682 TemplateParams(nullptr), ArgsAsWritten(nullptr),
1683 InstantiatedFromMember(nullptr, false) {}
1686 static ClassTemplatePartialSpecializationDecl *
1687 Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1688 SourceLocation StartLoc, SourceLocation IdLoc,
1689 TemplateParameterList *Params,
1690 ClassTemplateDecl *SpecializedTemplate,
1691 const TemplateArgument *Args,
1693 const TemplateArgumentListInfo &ArgInfos,
1694 QualType CanonInjectedType,
1695 ClassTemplatePartialSpecializationDecl *PrevDecl);
1697 static ClassTemplatePartialSpecializationDecl *
1698 CreateDeserialized(ASTContext &C, unsigned ID);
1700 ClassTemplatePartialSpecializationDecl *getMostRecentDecl() {
1701 return cast<ClassTemplatePartialSpecializationDecl>(
1702 static_cast<ClassTemplateSpecializationDecl *>(
1703 this)->getMostRecentDecl());
1706 /// Get the list of template parameters
1707 TemplateParameterList *getTemplateParameters() const {
1708 return TemplateParams;
1711 /// Get the template arguments as written.
1712 const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
1713 return ArgsAsWritten;
1716 /// \brief Retrieve the member class template partial specialization from
1717 /// which this particular class template partial specialization was
1721 /// template<typename T>
1723 /// template<typename U> struct Inner;
1724 /// template<typename U> struct Inner<U*> { }; // #1
1727 /// Outer<float>::Inner<int*> ii;
1730 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
1731 /// end up instantiating the partial specialization
1732 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
1733 /// template partial specialization \c Outer<T>::Inner<U*>. Given
1734 /// \c Outer<float>::Inner<U*>, this function would return
1735 /// \c Outer<T>::Inner<U*>.
1736 ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
1737 ClassTemplatePartialSpecializationDecl *First =
1738 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1739 return First->InstantiatedFromMember.getPointer();
1742 void setInstantiatedFromMember(
1743 ClassTemplatePartialSpecializationDecl *PartialSpec) {
1744 ClassTemplatePartialSpecializationDecl *First =
1745 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1746 First->InstantiatedFromMember.setPointer(PartialSpec);
1749 /// \brief Determines whether this class template partial specialization
1750 /// template was a specialization of a member partial specialization.
1752 /// In the following example, the member template partial specialization
1753 /// \c X<int>::Inner<T*> is a member specialization.
1756 /// template<typename T>
1758 /// template<typename U> struct Inner;
1759 /// template<typename U> struct Inner<U*>;
1762 /// template<> template<typename T>
1763 /// struct X<int>::Inner<T*> { /* ... */ };
1765 bool isMemberSpecialization() {
1766 ClassTemplatePartialSpecializationDecl *First =
1767 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1768 return First->InstantiatedFromMember.getInt();
1771 /// \brief Note that this member template is a specialization.
1772 void setMemberSpecialization() {
1773 ClassTemplatePartialSpecializationDecl *First =
1774 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1775 assert(First->InstantiatedFromMember.getPointer() &&
1776 "Only member templates can be member template specializations");
1777 return First->InstantiatedFromMember.setInt(true);
1780 /// Retrieves the injected specialization type for this partial
1781 /// specialization. This is not the same as the type-decl-type for
1782 /// this partial specialization, which is an InjectedClassNameType.
1783 QualType getInjectedSpecializationType() const {
1784 assert(getTypeForDecl() && "partial specialization has no type set!");
1785 return cast<InjectedClassNameType>(getTypeForDecl())
1786 ->getInjectedSpecializationType();
1789 // FIXME: Add Profile support!
1791 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1792 static bool classofKind(Kind K) {
1793 return K == ClassTemplatePartialSpecialization;
1796 friend class ASTDeclReader;
1797 friend class ASTDeclWriter;
1800 /// Declaration of a class template.
1801 class ClassTemplateDecl : public RedeclarableTemplateDecl {
1802 static void DeallocateCommon(void *Ptr);
1805 /// \brief Data that is common to all of the declarations of a given
1807 struct Common : CommonBase {
1808 Common() : LazySpecializations() { }
1810 /// \brief The class template specializations for this class
1811 /// template, including explicit specializations and instantiations.
1812 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations;
1814 /// \brief The class template partial specializations for this class
1816 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>
1817 PartialSpecializations;
1819 /// \brief The injected-class-name type for this class template.
1820 QualType InjectedClassNameType;
1822 /// \brief If non-null, points to an array of specializations (including
1823 /// partial specializations) known only by their external declaration IDs.
1825 /// The first value in the array is the number of of specializations/
1826 /// partial specializations that follow.
1827 uint32_t *LazySpecializations;
1830 /// \brief Load any lazily-loaded specializations from the external source.
1831 void LoadLazySpecializations() const;
1833 /// \brief Retrieve the set of specializations of this class template.
1834 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
1835 getSpecializations() const;
1837 /// \brief Retrieve the set of partial specializations of this class
1839 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
1840 getPartialSpecializations();
1842 ClassTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
1843 DeclarationName Name, TemplateParameterList *Params,
1845 : RedeclarableTemplateDecl(ClassTemplate, C, DC, L, Name, Params, Decl) {}
1847 CommonBase *newCommon(ASTContext &C) const override;
1849 Common *getCommonPtr() const {
1850 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
1854 /// \brief Get the underlying class declarations of the template.
1855 CXXRecordDecl *getTemplatedDecl() const {
1856 return static_cast<CXXRecordDecl *>(TemplatedDecl);
1859 /// \brief Returns whether this template declaration defines the primary
1861 bool isThisDeclarationADefinition() const {
1862 return getTemplatedDecl()->isThisDeclarationADefinition();
1865 /// \brief Create a class template node.
1866 static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
1868 DeclarationName Name,
1869 TemplateParameterList *Params,
1871 ClassTemplateDecl *PrevDecl);
1873 /// \brief Create an empty class template node.
1874 static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1876 /// \brief Return the specialization with the provided arguments if it exists,
1877 /// otherwise return the insertion point.
1878 ClassTemplateSpecializationDecl *
1879 findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
1881 /// \brief Insert the specified specialization knowing that it is not already
1882 /// in. InsertPos must be obtained from findSpecialization.
1883 void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos);
1885 ClassTemplateDecl *getCanonicalDecl() override {
1886 return cast<ClassTemplateDecl>(
1887 RedeclarableTemplateDecl::getCanonicalDecl());
1889 const ClassTemplateDecl *getCanonicalDecl() const {
1890 return cast<ClassTemplateDecl>(
1891 RedeclarableTemplateDecl::getCanonicalDecl());
1894 /// \brief Retrieve the previous declaration of this class template, or
1895 /// NULL if no such declaration exists.
1896 ClassTemplateDecl *getPreviousDecl() {
1897 return cast_or_null<ClassTemplateDecl>(
1898 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
1901 /// \brief Retrieve the previous declaration of this class template, or
1902 /// NULL if no such declaration exists.
1903 const ClassTemplateDecl *getPreviousDecl() const {
1904 return cast_or_null<ClassTemplateDecl>(
1905 static_cast<const RedeclarableTemplateDecl *>(
1906 this)->getPreviousDecl());
1909 ClassTemplateDecl *getMostRecentDecl() {
1910 return cast<ClassTemplateDecl>(
1911 static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
1913 const ClassTemplateDecl *getMostRecentDecl() const {
1914 return const_cast<ClassTemplateDecl*>(this)->getMostRecentDecl();
1917 ClassTemplateDecl *getInstantiatedFromMemberTemplate() {
1918 return cast_or_null<ClassTemplateDecl>(
1919 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
1922 /// \brief Return the partial specialization with the provided arguments if it
1923 /// exists, otherwise return the insertion point.
1924 ClassTemplatePartialSpecializationDecl *
1925 findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
1927 /// \brief Insert the specified partial specialization knowing that it is not
1928 /// already in. InsertPos must be obtained from findPartialSpecialization.
1929 void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D,
1932 /// \brief Retrieve the partial specializations as an ordered list.
1933 void getPartialSpecializations(
1934 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS);
1936 /// \brief Find a class template partial specialization with the given
1939 /// \param T a dependent type that names a specialization of this class
1942 /// \returns the class template partial specialization that exactly matches
1943 /// the type \p T, or NULL if no such partial specialization exists.
1944 ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
1946 /// \brief Find a class template partial specialization which was instantiated
1947 /// from the given member partial specialization.
1949 /// \param D a member class template partial specialization.
1951 /// \returns the class template partial specialization which was instantiated
1952 /// from the given member partial specialization, or NULL if no such partial
1953 /// specialization exists.
1954 ClassTemplatePartialSpecializationDecl *
1955 findPartialSpecInstantiatedFromMember(
1956 ClassTemplatePartialSpecializationDecl *D);
1958 /// \brief Retrieve the template specialization type of the
1959 /// injected-class-name for this class template.
1961 /// The injected-class-name for a class template \c X is \c
1962 /// X<template-args>, where \c template-args is formed from the
1963 /// template arguments that correspond to the template parameters of
1964 /// \c X. For example:
1967 /// template<typename T, int N>
1969 /// typedef array this_type; // "array" is equivalent to "array<T, N>"
1972 QualType getInjectedClassNameSpecialization();
1974 typedef SpecIterator<ClassTemplateSpecializationDecl> spec_iterator;
1975 typedef llvm::iterator_range<spec_iterator> spec_range;
1977 spec_range specializations() const {
1978 return spec_range(spec_begin(), spec_end());
1981 spec_iterator spec_begin() const {
1982 return makeSpecIterator(getSpecializations(), false);
1985 spec_iterator spec_end() const {
1986 return makeSpecIterator(getSpecializations(), true);
1989 // Implement isa/cast/dyncast support
1990 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1991 static bool classofKind(Kind K) { return K == ClassTemplate; }
1993 friend class ASTDeclReader;
1994 friend class ASTDeclWriter;
1997 /// \brief Declaration of a friend template.
2001 /// template \<typename T> class A {
2002 /// friend class MyVector<T>; // not a friend template
2003 /// template \<typename U> friend class B; // not a friend template
2004 /// template \<typename U> friend class Foo<T>::Nested; // friend template
2008 /// \note This class is not currently in use. All of the above
2009 /// will yield a FriendDecl, not a FriendTemplateDecl.
2010 class FriendTemplateDecl : public Decl {
2011 virtual void anchor();
2013 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
2016 // The number of template parameters; always non-zero.
2019 // The parameter list.
2020 TemplateParameterList **Params;
2022 // The declaration that's a friend of this class.
2025 // Location of the 'friend' specifier.
2026 SourceLocation FriendLoc;
2029 FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
2031 TemplateParameterList **Params,
2033 SourceLocation FriendLoc)
2034 : Decl(Decl::FriendTemplate, DC, Loc),
2038 FriendLoc(FriendLoc)
2041 FriendTemplateDecl(EmptyShell Empty)
2042 : Decl(Decl::FriendTemplate, Empty),
2048 static FriendTemplateDecl *Create(ASTContext &Context,
2049 DeclContext *DC, SourceLocation Loc,
2051 TemplateParameterList **Params,
2053 SourceLocation FriendLoc);
2055 static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2057 /// If this friend declaration names a templated type (or
2058 /// a dependent member type of a templated type), return that
2059 /// type; otherwise return null.
2060 TypeSourceInfo *getFriendType() const {
2061 return Friend.dyn_cast<TypeSourceInfo*>();
2064 /// If this friend declaration names a templated function (or
2065 /// a member function of a templated type), return that type;
2066 /// otherwise return null.
2067 NamedDecl *getFriendDecl() const {
2068 return Friend.dyn_cast<NamedDecl*>();
2071 /// \brief Retrieves the location of the 'friend' keyword.
2072 SourceLocation getFriendLoc() const {
2076 TemplateParameterList *getTemplateParameterList(unsigned i) const {
2077 assert(i <= NumParams);
2081 unsigned getNumTemplateParameters() const {
2085 // Implement isa/cast/dyncast/etc.
2086 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2087 static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
2089 friend class ASTDeclReader;
2092 /// \brief Declaration of an alias template.
2096 /// template \<typename T> using V = std::map<T*, int, MyCompare<T>>;
2098 class TypeAliasTemplateDecl : public RedeclarableTemplateDecl {
2099 static void DeallocateCommon(void *Ptr);
2102 typedef CommonBase Common;
2104 TypeAliasTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
2105 DeclarationName Name, TemplateParameterList *Params,
2107 : RedeclarableTemplateDecl(TypeAliasTemplate, C, DC, L, Name, Params,
2110 CommonBase *newCommon(ASTContext &C) const override;
2112 Common *getCommonPtr() {
2113 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2117 /// Get the underlying function declaration of the template.
2118 TypeAliasDecl *getTemplatedDecl() const {
2119 return static_cast<TypeAliasDecl*>(TemplatedDecl);
2123 TypeAliasTemplateDecl *getCanonicalDecl() override {
2124 return cast<TypeAliasTemplateDecl>(
2125 RedeclarableTemplateDecl::getCanonicalDecl());
2127 const TypeAliasTemplateDecl *getCanonicalDecl() const {
2128 return cast<TypeAliasTemplateDecl>(
2129 RedeclarableTemplateDecl::getCanonicalDecl());
2132 /// \brief Retrieve the previous declaration of this function template, or
2133 /// NULL if no such declaration exists.
2134 TypeAliasTemplateDecl *getPreviousDecl() {
2135 return cast_or_null<TypeAliasTemplateDecl>(
2136 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2139 /// \brief Retrieve the previous declaration of this function template, or
2140 /// NULL if no such declaration exists.
2141 const TypeAliasTemplateDecl *getPreviousDecl() const {
2142 return cast_or_null<TypeAliasTemplateDecl>(
2143 static_cast<const RedeclarableTemplateDecl *>(
2144 this)->getPreviousDecl());
2147 TypeAliasTemplateDecl *getInstantiatedFromMemberTemplate() {
2148 return cast_or_null<TypeAliasTemplateDecl>(
2149 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
2153 /// \brief Create a function template node.
2154 static TypeAliasTemplateDecl *Create(ASTContext &C, DeclContext *DC,
2156 DeclarationName Name,
2157 TemplateParameterList *Params,
2160 /// \brief Create an empty alias template node.
2161 static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2163 // Implement isa/cast/dyncast support
2164 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2165 static bool classofKind(Kind K) { return K == TypeAliasTemplate; }
2167 friend class ASTDeclReader;
2168 friend class ASTDeclWriter;
2171 /// \brief Declaration of a function specialization at template class scope.
2173 /// This is a non-standard extension needed to support MSVC.
2177 /// template <class T>
2179 /// template <class U> void foo(U a) { }
2180 /// template<> void foo(int a) { }
2184 /// "template<> foo(int a)" will be saved in Specialization as a normal
2185 /// CXXMethodDecl. Then during an instantiation of class A, it will be
2186 /// transformed into an actual function specialization.
2187 class ClassScopeFunctionSpecializationDecl : public Decl {
2188 virtual void anchor();
2190 ClassScopeFunctionSpecializationDecl(DeclContext *DC, SourceLocation Loc,
2191 CXXMethodDecl *FD, bool Args,
2192 TemplateArgumentListInfo TemplArgs)
2193 : Decl(Decl::ClassScopeFunctionSpecialization, DC, Loc),
2194 Specialization(FD), HasExplicitTemplateArgs(Args),
2195 TemplateArgs(TemplArgs) {}
2197 ClassScopeFunctionSpecializationDecl(EmptyShell Empty)
2198 : Decl(Decl::ClassScopeFunctionSpecialization, Empty) {}
2200 CXXMethodDecl *Specialization;
2201 bool HasExplicitTemplateArgs;
2202 TemplateArgumentListInfo TemplateArgs;
2205 CXXMethodDecl *getSpecialization() const { return Specialization; }
2206 bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
2207 const TemplateArgumentListInfo& templateArgs() const { return TemplateArgs; }
2209 static ClassScopeFunctionSpecializationDecl *Create(ASTContext &C,
2213 bool HasExplicitTemplateArgs,
2214 TemplateArgumentListInfo TemplateArgs) {
2215 return new (C, DC) ClassScopeFunctionSpecializationDecl(
2216 DC, Loc, FD, HasExplicitTemplateArgs, TemplateArgs);
2219 static ClassScopeFunctionSpecializationDecl *
2220 CreateDeserialized(ASTContext &Context, unsigned ID);
2222 // Implement isa/cast/dyncast/etc.
2223 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2224 static bool classofKind(Kind K) {
2225 return K == Decl::ClassScopeFunctionSpecialization;
2228 friend class ASTDeclReader;
2229 friend class ASTDeclWriter;
2232 /// Implementation of inline functions that require the template declarations
2233 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
2236 /// \brief Represents a variable template specialization, which refers to
2237 /// a variable template with a given set of template arguments.
2239 /// Variable template specializations represent both explicit
2240 /// specializations of variable templates, as in the example below, and
2241 /// implicit instantiations of variable templates.
2244 /// template<typename T> constexpr T pi = T(3.1415926535897932385);
2247 /// constexpr float pi<float>; // variable template specialization pi<float>
2249 class VarTemplateSpecializationDecl : public VarDecl,
2250 public llvm::FoldingSetNode {
2252 /// \brief Structure that stores information about a variable template
2253 /// specialization that was instantiated from a variable template partial
2255 struct SpecializedPartialSpecialization {
2256 /// \brief The variable template partial specialization from which this
2257 /// variable template specialization was instantiated.
2258 VarTemplatePartialSpecializationDecl *PartialSpecialization;
2260 /// \brief The template argument list deduced for the variable template
2261 /// partial specialization itself.
2262 const TemplateArgumentList *TemplateArgs;
2265 /// \brief The template that this specialization specializes.
2266 llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *>
2267 SpecializedTemplate;
2269 /// \brief Further info for explicit template specialization/instantiation.
2270 struct ExplicitSpecializationInfo {
2271 /// \brief The type-as-written.
2272 TypeSourceInfo *TypeAsWritten;
2273 /// \brief The location of the extern keyword.
2274 SourceLocation ExternLoc;
2275 /// \brief The location of the template keyword.
2276 SourceLocation TemplateKeywordLoc;
2278 ExplicitSpecializationInfo()
2279 : TypeAsWritten(nullptr), ExternLoc(), TemplateKeywordLoc() {}
2282 /// \brief Further info for explicit template specialization/instantiation.
2283 /// Does not apply to implicit specializations.
2284 ExplicitSpecializationInfo *ExplicitInfo;
2286 /// \brief The template arguments used to describe this specialization.
2287 const TemplateArgumentList *TemplateArgs;
2288 TemplateArgumentListInfo TemplateArgsInfo;
2290 /// \brief The point where this template was instantiated (if any).
2291 SourceLocation PointOfInstantiation;
2293 /// \brief The kind of specialization this declaration refers to.
2294 /// Really a value of type TemplateSpecializationKind.
2295 unsigned SpecializationKind : 3;
2298 VarTemplateSpecializationDecl(Kind DK, ASTContext &Context, DeclContext *DC,
2299 SourceLocation StartLoc, SourceLocation IdLoc,
2300 VarTemplateDecl *SpecializedTemplate,
2301 QualType T, TypeSourceInfo *TInfo,
2302 StorageClass S, const TemplateArgument *Args,
2305 explicit VarTemplateSpecializationDecl(Kind DK, ASTContext &Context);
2308 static VarTemplateSpecializationDecl *
2309 Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2310 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
2311 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
2313 static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C,
2316 void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
2317 bool Qualified) const override;
2319 VarTemplateSpecializationDecl *getMostRecentDecl() {
2320 VarDecl *Recent = static_cast<VarDecl *>(this)->getMostRecentDecl();
2321 return cast<VarTemplateSpecializationDecl>(Recent);
2324 /// \brief Retrieve the template that this specialization specializes.
2325 VarTemplateDecl *getSpecializedTemplate() const;
2327 /// \brief Retrieve the template arguments of the variable template
2329 const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; }
2331 // TODO: Always set this when creating the new specialization?
2332 void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo);
2334 const TemplateArgumentListInfo &getTemplateArgsInfo() const {
2335 return TemplateArgsInfo;
2338 /// \brief Determine the kind of specialization that this
2339 /// declaration represents.
2340 TemplateSpecializationKind getSpecializationKind() const {
2341 return static_cast<TemplateSpecializationKind>(SpecializationKind);
2344 bool isExplicitSpecialization() const {
2345 return getSpecializationKind() == TSK_ExplicitSpecialization;
2348 /// \brief True if this declaration is an explicit specialization,
2349 /// explicit instantiation declaration, or explicit instantiation
2351 bool isExplicitInstantiationOrSpecialization() const {
2352 switch (getTemplateSpecializationKind()) {
2353 case TSK_ExplicitSpecialization:
2354 case TSK_ExplicitInstantiationDeclaration:
2355 case TSK_ExplicitInstantiationDefinition:
2358 case TSK_Undeclared:
2359 case TSK_ImplicitInstantiation:
2362 llvm_unreachable("bad template specialization kind");
2365 void setSpecializationKind(TemplateSpecializationKind TSK) {
2366 SpecializationKind = TSK;
2369 /// \brief Get the point of instantiation (if any), or null if none.
2370 SourceLocation getPointOfInstantiation() const {
2371 return PointOfInstantiation;
2374 void setPointOfInstantiation(SourceLocation Loc) {
2375 assert(Loc.isValid() && "point of instantiation must be valid!");
2376 PointOfInstantiation = Loc;
2379 /// \brief If this variable template specialization is an instantiation of
2380 /// a template (rather than an explicit specialization), return the
2381 /// variable template or variable template partial specialization from which
2382 /// it was instantiated.
2383 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2384 getInstantiatedFrom() const {
2385 if (getSpecializationKind() != TSK_ImplicitInstantiation &&
2386 getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
2387 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
2388 return llvm::PointerUnion<VarTemplateDecl *,
2389 VarTemplatePartialSpecializationDecl *>();
2391 if (SpecializedPartialSpecialization *PartialSpec =
2392 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2393 return PartialSpec->PartialSpecialization;
2395 return SpecializedTemplate.get<VarTemplateDecl *>();
2398 /// \brief Retrieve the variable template or variable template partial
2399 /// specialization which was specialized by this.
2400 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2401 getSpecializedTemplateOrPartial() const {
2402 if (SpecializedPartialSpecialization *PartialSpec =
2403 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2404 return PartialSpec->PartialSpecialization;
2406 return SpecializedTemplate.get<VarTemplateDecl *>();
2409 /// \brief Retrieve the set of template arguments that should be used
2410 /// to instantiate the initializer of the variable template or variable
2411 /// template partial specialization from which this variable template
2412 /// specialization was instantiated.
2414 /// \returns For a variable template specialization instantiated from the
2415 /// primary template, this function will return the same template arguments
2416 /// as getTemplateArgs(). For a variable template specialization instantiated
2417 /// from a variable template partial specialization, this function will the
2418 /// return deduced template arguments for the variable template partial
2419 /// specialization itself.
2420 const TemplateArgumentList &getTemplateInstantiationArgs() const {
2421 if (SpecializedPartialSpecialization *PartialSpec =
2422 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2423 return *PartialSpec->TemplateArgs;
2425 return getTemplateArgs();
2428 /// \brief Note that this variable template specialization is actually an
2429 /// instantiation of the given variable template partial specialization whose
2430 /// template arguments have been deduced.
2431 void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec,
2432 const TemplateArgumentList *TemplateArgs) {
2433 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2434 "Already set to a variable template partial specialization!");
2435 SpecializedPartialSpecialization *PS =
2436 new (getASTContext()) SpecializedPartialSpecialization();
2437 PS->PartialSpecialization = PartialSpec;
2438 PS->TemplateArgs = TemplateArgs;
2439 SpecializedTemplate = PS;
2442 /// \brief Note that this variable template specialization is an instantiation
2443 /// of the given variable template.
2444 void setInstantiationOf(VarTemplateDecl *TemplDecl) {
2445 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2446 "Previously set to a variable template partial specialization!");
2447 SpecializedTemplate = TemplDecl;
2450 /// \brief Sets the type of this specialization as it was written by
2452 void setTypeAsWritten(TypeSourceInfo *T) {
2454 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2455 ExplicitInfo->TypeAsWritten = T;
2457 /// \brief Gets the type of this specialization as it was written by
2458 /// the user, if it was so written.
2459 TypeSourceInfo *getTypeAsWritten() const {
2460 return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
2463 /// \brief Gets the location of the extern keyword, if present.
2464 SourceLocation getExternLoc() const {
2465 return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
2467 /// \brief Sets the location of the extern keyword.
2468 void setExternLoc(SourceLocation Loc) {
2470 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2471 ExplicitInfo->ExternLoc = Loc;
2474 /// \brief Sets the location of the template keyword.
2475 void setTemplateKeywordLoc(SourceLocation Loc) {
2477 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2478 ExplicitInfo->TemplateKeywordLoc = Loc;
2480 /// \brief Gets the location of the template keyword, if present.
2481 SourceLocation getTemplateKeywordLoc() const {
2482 return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
2485 void Profile(llvm::FoldingSetNodeID &ID) const {
2486 Profile(ID, TemplateArgs->asArray(), getASTContext());
2489 static void Profile(llvm::FoldingSetNodeID &ID,
2490 ArrayRef<TemplateArgument> TemplateArgs,
2491 ASTContext &Context) {
2492 ID.AddInteger(TemplateArgs.size());
2493 for (unsigned Arg = 0; Arg != TemplateArgs.size(); ++Arg)
2494 TemplateArgs[Arg].Profile(ID, Context);
2497 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2498 static bool classofKind(Kind K) {
2499 return K >= firstVarTemplateSpecialization &&
2500 K <= lastVarTemplateSpecialization;
2503 friend class ASTDeclReader;
2504 friend class ASTDeclWriter;
2507 class VarTemplatePartialSpecializationDecl
2508 : public VarTemplateSpecializationDecl {
2509 void anchor() override;
2511 /// \brief The list of template parameters
2512 TemplateParameterList *TemplateParams;
2514 /// \brief The source info for the template arguments as written.
2515 /// FIXME: redundant with TypeAsWritten?
2516 const ASTTemplateArgumentListInfo *ArgsAsWritten;
2518 /// \brief The variable template partial specialization from which this
2519 /// variable template partial specialization was instantiated.
2521 /// The boolean value will be true to indicate that this variable template
2522 /// partial specialization was specialized at this level.
2523 llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool>
2524 InstantiatedFromMember;
2526 VarTemplatePartialSpecializationDecl(
2527 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2528 SourceLocation IdLoc, TemplateParameterList *Params,
2529 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
2530 StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
2531 const ASTTemplateArgumentListInfo *ArgInfos);
2533 VarTemplatePartialSpecializationDecl(ASTContext &Context)
2534 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context),
2535 TemplateParams(nullptr), ArgsAsWritten(nullptr),
2536 InstantiatedFromMember(nullptr, false) {}
2539 static VarTemplatePartialSpecializationDecl *
2540 Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2541 SourceLocation IdLoc, TemplateParameterList *Params,
2542 VarTemplateDecl *SpecializedTemplate, QualType T,
2543 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
2544 unsigned NumArgs, const TemplateArgumentListInfo &ArgInfos);
2546 static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C,
2549 VarTemplatePartialSpecializationDecl *getMostRecentDecl() {
2550 return cast<VarTemplatePartialSpecializationDecl>(
2551 static_cast<VarTemplateSpecializationDecl *>(
2552 this)->getMostRecentDecl());
2555 /// Get the list of template parameters
2556 TemplateParameterList *getTemplateParameters() const {
2557 return TemplateParams;
2560 /// Get the template arguments as written.
2561 const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
2562 return ArgsAsWritten;
2565 /// \brief Retrieve the member variable template partial specialization from
2566 /// which this particular variable template partial specialization was
2570 /// template<typename T>
2572 /// template<typename U> U Inner;
2573 /// template<typename U> U* Inner<U*> = (U*)(0); // #1
2576 /// template int* Outer<float>::Inner<int*>;
2579 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
2580 /// end up instantiating the partial specialization
2581 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the
2582 /// variable template partial specialization \c Outer<T>::Inner<U*>. Given
2583 /// \c Outer<float>::Inner<U*>, this function would return
2584 /// \c Outer<T>::Inner<U*>.
2585 VarTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
2586 VarTemplatePartialSpecializationDecl *First =
2587 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2588 return First->InstantiatedFromMember.getPointer();
2592 setInstantiatedFromMember(VarTemplatePartialSpecializationDecl *PartialSpec) {
2593 VarTemplatePartialSpecializationDecl *First =
2594 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2595 First->InstantiatedFromMember.setPointer(PartialSpec);
2598 /// \brief Determines whether this variable template partial specialization
2599 /// was a specialization of a member partial specialization.
2601 /// In the following example, the member template partial specialization
2602 /// \c X<int>::Inner<T*> is a member specialization.
2605 /// template<typename T>
2607 /// template<typename U> U Inner;
2608 /// template<typename U> U* Inner<U*> = (U*)(0);
2611 /// template<> template<typename T>
2612 /// U* X<int>::Inner<T*> = (T*)(0) + 1;
2614 bool isMemberSpecialization() {
2615 VarTemplatePartialSpecializationDecl *First =
2616 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2617 return First->InstantiatedFromMember.getInt();
2620 /// \brief Note that this member template is a specialization.
2621 void setMemberSpecialization() {
2622 VarTemplatePartialSpecializationDecl *First =
2623 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2624 assert(First->InstantiatedFromMember.getPointer() &&
2625 "Only member templates can be member template specializations");
2626 return First->InstantiatedFromMember.setInt(true);
2629 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2630 static bool classofKind(Kind K) {
2631 return K == VarTemplatePartialSpecialization;
2634 friend class ASTDeclReader;
2635 friend class ASTDeclWriter;
2638 /// Declaration of a variable template.
2639 class VarTemplateDecl : public RedeclarableTemplateDecl {
2640 static void DeallocateCommon(void *Ptr);
2643 /// \brief Data that is common to all of the declarations of a given
2644 /// variable template.
2645 struct Common : CommonBase {
2646 Common() : LazySpecializations() {}
2648 /// \brief The variable template specializations for this variable
2649 /// template, including explicit specializations and instantiations.
2650 llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations;
2652 /// \brief The variable template partial specializations for this variable
2654 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>
2655 PartialSpecializations;
2657 /// \brief If non-null, points to an array of specializations (including
2658 /// partial specializations) known ownly by their external declaration IDs.
2660 /// The first value in the array is the number of of specializations/
2661 /// partial specializations that follow.
2662 uint32_t *LazySpecializations;
2665 /// \brief Load any lazily-loaded specializations from the external source.
2666 void LoadLazySpecializations() const;
2668 /// \brief Retrieve the set of specializations of this variable template.
2669 llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
2670 getSpecializations() const;
2672 /// \brief Retrieve the set of partial specializations of this class
2674 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
2675 getPartialSpecializations();
2677 VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
2678 DeclarationName Name, TemplateParameterList *Params,
2680 : RedeclarableTemplateDecl(VarTemplate, C, DC, L, Name, Params, Decl) {}
2682 CommonBase *newCommon(ASTContext &C) const override;
2684 Common *getCommonPtr() const {
2685 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2689 /// \brief Get the underlying variable declarations of the template.
2690 VarDecl *getTemplatedDecl() const {
2691 return static_cast<VarDecl *>(TemplatedDecl);
2694 /// \brief Returns whether this template declaration defines the primary
2695 /// variable pattern.
2696 bool isThisDeclarationADefinition() const {
2697 return getTemplatedDecl()->isThisDeclarationADefinition();
2700 VarTemplateDecl *getDefinition();
2702 /// \brief Create a variable template node.
2703 static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC,
2704 SourceLocation L, DeclarationName Name,
2705 TemplateParameterList *Params,
2708 /// \brief Create an empty variable template node.
2709 static VarTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2711 /// \brief Return the specialization with the provided arguments if it exists,
2712 /// otherwise return the insertion point.
2713 VarTemplateSpecializationDecl *
2714 findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2716 /// \brief Insert the specified specialization knowing that it is not already
2717 /// in. InsertPos must be obtained from findSpecialization.
2718 void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos);
2720 VarTemplateDecl *getCanonicalDecl() override {
2721 return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2723 const VarTemplateDecl *getCanonicalDecl() const {
2724 return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2727 /// \brief Retrieve the previous declaration of this variable template, or
2728 /// NULL if no such declaration exists.
2729 VarTemplateDecl *getPreviousDecl() {
2730 return cast_or_null<VarTemplateDecl>(
2731 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2734 /// \brief Retrieve the previous declaration of this variable template, or
2735 /// NULL if no such declaration exists.
2736 const VarTemplateDecl *getPreviousDecl() const {
2737 return cast_or_null<VarTemplateDecl>(
2738 static_cast<const RedeclarableTemplateDecl *>(
2739 this)->getPreviousDecl());
2742 VarTemplateDecl *getInstantiatedFromMemberTemplate() {
2743 return cast_or_null<VarTemplateDecl>(
2744 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
2747 /// \brief Return the partial specialization with the provided arguments if it
2748 /// exists, otherwise return the insertion point.
2749 VarTemplatePartialSpecializationDecl *
2750 findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2752 /// \brief Insert the specified partial specialization knowing that it is not
2753 /// already in. InsertPos must be obtained from findPartialSpecialization.
2754 void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D,
2757 /// \brief Retrieve the partial specializations as an ordered list.
2758 void getPartialSpecializations(
2759 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS);
2761 /// \brief Find a variable template partial specialization which was
2763 /// from the given member partial specialization.
2765 /// \param D a member variable template partial specialization.
2767 /// \returns the variable template partial specialization which was
2769 /// from the given member partial specialization, or NULL if no such partial
2770 /// specialization exists.
2771 VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember(
2772 VarTemplatePartialSpecializationDecl *D);
2774 typedef SpecIterator<VarTemplateSpecializationDecl> spec_iterator;
2775 typedef llvm::iterator_range<spec_iterator> spec_range;
2777 spec_range specializations() const {
2778 return spec_range(spec_begin(), spec_end());
2781 spec_iterator spec_begin() const {
2782 return makeSpecIterator(getSpecializations(), false);
2785 spec_iterator spec_end() const {
2786 return makeSpecIterator(getSpecializations(), true);
2789 // Implement isa/cast/dyncast support
2790 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2791 static bool classofKind(Kind K) { return K == VarTemplate; }
2793 friend class ASTDeclReader;
2794 friend class ASTDeclWriter;
2797 } /* end of namespace clang */