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) = delete;
162 void operator=(const TemplateArgumentList &Other) = delete;
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 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 *getDecl(EntryType *D) {
551 static ArrayRef<TemplateArgument> getTemplateArgs(EntryType *D) {
552 return D->getTemplateArgs().asArray();
556 template <typename EntryType, typename SETraits = SpecEntryTraits<EntryType>,
557 typename DeclType = typename SETraits::DeclType>
559 : llvm::iterator_adaptor_base<
560 SpecIterator<EntryType, SETraits, DeclType>,
561 typename llvm::FoldingSetVector<EntryType>::iterator,
562 typename std::iterator_traits<typename llvm::FoldingSetVector<
563 EntryType>::iterator>::iterator_category,
564 DeclType *, ptrdiff_t, DeclType *, DeclType *> {
566 explicit SpecIterator(
567 typename llvm::FoldingSetVector<EntryType>::iterator SetIter)
568 : SpecIterator::iterator_adaptor_base(std::move(SetIter)) {}
570 DeclType *operator*() const {
571 return SETraits::getDecl(&*this->I)->getMostRecentDecl();
573 DeclType *operator->() const { return **this; }
576 template <typename EntryType>
577 static SpecIterator<EntryType>
578 makeSpecIterator(llvm::FoldingSetVector<EntryType> &Specs, bool isEnd) {
579 return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin());
582 template <class EntryType> typename SpecEntryTraits<EntryType>::DeclType*
583 findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
584 ArrayRef<TemplateArgument> Args, void *&InsertPos);
586 template <class Derived, class EntryType>
587 void addSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
588 EntryType *Entry, void *InsertPos);
591 CommonBase() : InstantiatedFromMember(nullptr, false) { }
593 /// \brief The template from which this was most
594 /// directly instantiated (or null).
596 /// The boolean value indicates whether this template
597 /// was explicitly specialized.
598 llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool>
599 InstantiatedFromMember;
602 /// \brief Pointer to the common data shared by all declarations of this
604 mutable CommonBase *Common;
606 /// \brief Retrieves the "common" pointer shared by all (re-)declarations of
607 /// the same template. Calling this routine may implicitly allocate memory
608 /// for the common pointer.
609 CommonBase *getCommonPtr() const;
611 virtual CommonBase *newCommon(ASTContext &C) const = 0;
613 // Construct a template decl with name, parameters, and templated element.
614 RedeclarableTemplateDecl(Kind DK, ASTContext &C, DeclContext *DC,
615 SourceLocation L, DeclarationName Name,
616 TemplateParameterList *Params, NamedDecl *Decl)
617 : TemplateDecl(DK, DC, L, Name, Params, Decl), redeclarable_base(C),
621 template <class decl_type> friend class RedeclarableTemplate;
623 /// \brief Retrieves the canonical declaration of this template.
624 RedeclarableTemplateDecl *getCanonicalDecl() override {
625 return getFirstDecl();
627 const RedeclarableTemplateDecl *getCanonicalDecl() const {
628 return getFirstDecl();
631 /// \brief Determines whether this template was a specialization of a
634 /// In the following example, the function template \c X<int>::f and the
635 /// member template \c X<int>::Inner are member specializations.
638 /// template<typename T>
640 /// template<typename U> void f(T, U);
641 /// template<typename U> struct Inner;
644 /// template<> template<typename T>
645 /// void X<int>::f(int, T);
646 /// template<> template<typename T>
647 /// struct X<int>::Inner { /* ... */ };
649 bool isMemberSpecialization() const {
650 return getCommonPtr()->InstantiatedFromMember.getInt();
653 /// \brief Note that this member template is a specialization.
654 void setMemberSpecialization() {
655 assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
656 "Only member templates can be member template specializations");
657 getCommonPtr()->InstantiatedFromMember.setInt(true);
660 /// \brief Retrieve the member template from which this template was
661 /// instantiated, or NULL if this template was not instantiated from a
664 /// A template is instantiated from a member template when the member
665 /// template itself is part of a class template (or member thereof). For
669 /// template<typename T>
671 /// template<typename U> void f(T, U);
674 /// void test(X<int> x) {
679 /// \c X<int>::f is a FunctionTemplateDecl that describes the function
683 /// template<typename U> void X<int>::f(int, U);
686 /// which was itself created during the instantiation of \c X<int>. Calling
687 /// getInstantiatedFromMemberTemplate() on this FunctionTemplateDecl will
688 /// retrieve the FunctionTemplateDecl for the original template \c f within
689 /// the class template \c X<T>, i.e.,
692 /// template<typename T>
693 /// template<typename U>
694 /// void X<T>::f(T, U);
696 RedeclarableTemplateDecl *getInstantiatedFromMemberTemplate() const {
697 return getCommonPtr()->InstantiatedFromMember.getPointer();
700 void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD) {
701 assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
702 getCommonPtr()->InstantiatedFromMember.setPointer(TD);
705 typedef redeclarable_base::redecl_range redecl_range;
706 typedef redeclarable_base::redecl_iterator redecl_iterator;
707 using redeclarable_base::redecls_begin;
708 using redeclarable_base::redecls_end;
709 using redeclarable_base::redecls;
710 using redeclarable_base::getPreviousDecl;
711 using redeclarable_base::getMostRecentDecl;
712 using redeclarable_base::isFirstDecl;
714 // Implement isa/cast/dyncast/etc.
715 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
716 static bool classofKind(Kind K) {
717 return K >= firstRedeclarableTemplate && K <= lastRedeclarableTemplate;
720 friend class ASTReader;
721 friend class ASTDeclReader;
722 friend class ASTDeclWriter;
725 template <> struct RedeclarableTemplateDecl::
726 SpecEntryTraits<FunctionTemplateSpecializationInfo> {
727 typedef FunctionDecl DeclType;
729 static DeclType *getDecl(FunctionTemplateSpecializationInfo *I) {
732 static ArrayRef<TemplateArgument>
733 getTemplateArgs(FunctionTemplateSpecializationInfo *I) {
734 return I->TemplateArguments->asArray();
738 /// Declaration of a template function.
739 class FunctionTemplateDecl : public RedeclarableTemplateDecl {
740 static void DeallocateCommon(void *Ptr);
743 /// \brief Data that is common to all of the declarations of a given
744 /// function template.
745 struct Common : CommonBase {
746 Common() : InjectedArgs(), LazySpecializations() { }
748 /// \brief The function template specializations for this function
749 /// template, including explicit specializations and instantiations.
750 llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> Specializations;
752 /// \brief The set of "injected" template arguments used within this
753 /// function template.
755 /// This pointer refers to the template arguments (there are as
756 /// many template arguments as template parameaters) for the function
757 /// template, and is allocated lazily, since most function templates do not
758 /// require the use of this information.
759 TemplateArgument *InjectedArgs;
761 /// \brief If non-null, points to an array of specializations known only
762 /// by their external declaration IDs.
764 /// The first value in the array is the number of of specializations
766 uint32_t *LazySpecializations;
769 FunctionTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
770 DeclarationName Name, TemplateParameterList *Params,
772 : RedeclarableTemplateDecl(FunctionTemplate, C, DC, L, Name, Params,
775 CommonBase *newCommon(ASTContext &C) const override;
777 Common *getCommonPtr() const {
778 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
781 friend class FunctionDecl;
783 /// \brief Retrieve the set of function template specializations of this
784 /// function template.
785 llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
786 getSpecializations() const;
788 /// \brief Add a specialization of this function template.
790 /// \param InsertPos Insert position in the FoldingSetVector, must have been
791 /// retrieved by an earlier call to findSpecialization().
792 void addSpecialization(FunctionTemplateSpecializationInfo* Info,
796 /// \brief Load any lazily-loaded specializations from the external source.
797 void LoadLazySpecializations() const;
799 /// Get the underlying function declaration of the template.
800 FunctionDecl *getTemplatedDecl() const {
801 return static_cast<FunctionDecl*>(TemplatedDecl);
804 /// Returns whether this template declaration defines the primary
806 bool isThisDeclarationADefinition() const {
807 return getTemplatedDecl()->isThisDeclarationADefinition();
810 /// \brief Return the specialization with the provided arguments if it exists,
811 /// otherwise return the insertion point.
812 FunctionDecl *findSpecialization(ArrayRef<TemplateArgument> Args,
815 FunctionTemplateDecl *getCanonicalDecl() override {
816 return cast<FunctionTemplateDecl>(
817 RedeclarableTemplateDecl::getCanonicalDecl());
819 const FunctionTemplateDecl *getCanonicalDecl() const {
820 return cast<FunctionTemplateDecl>(
821 RedeclarableTemplateDecl::getCanonicalDecl());
824 /// \brief Retrieve the previous declaration of this function template, or
825 /// NULL if no such declaration exists.
826 FunctionTemplateDecl *getPreviousDecl() {
827 return cast_or_null<FunctionTemplateDecl>(
828 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
831 /// \brief Retrieve the previous declaration of this function template, or
832 /// NULL if no such declaration exists.
833 const FunctionTemplateDecl *getPreviousDecl() const {
834 return cast_or_null<FunctionTemplateDecl>(
835 static_cast<const RedeclarableTemplateDecl *>(this)->getPreviousDecl());
838 FunctionTemplateDecl *getMostRecentDecl() {
839 return cast<FunctionTemplateDecl>(
840 static_cast<RedeclarableTemplateDecl *>(this)
841 ->getMostRecentDecl());
843 const FunctionTemplateDecl *getMostRecentDecl() const {
844 return const_cast<FunctionTemplateDecl*>(this)->getMostRecentDecl();
847 FunctionTemplateDecl *getInstantiatedFromMemberTemplate() {
848 return cast_or_null<FunctionTemplateDecl>(
849 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
852 typedef SpecIterator<FunctionTemplateSpecializationInfo> spec_iterator;
853 typedef llvm::iterator_range<spec_iterator> spec_range;
855 spec_range specializations() const {
856 return spec_range(spec_begin(), spec_end());
858 spec_iterator spec_begin() const {
859 return makeSpecIterator(getSpecializations(), false);
862 spec_iterator spec_end() const {
863 return makeSpecIterator(getSpecializations(), true);
866 /// \brief Retrieve the "injected" template arguments that correspond to the
867 /// template parameters of this function template.
869 /// Although the C++ standard has no notion of the "injected" template
870 /// arguments for a function template, the notion is convenient when
871 /// we need to perform substitutions inside the definition of a function
873 ArrayRef<TemplateArgument> getInjectedTemplateArgs();
875 /// \brief Create a function template node.
876 static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
878 DeclarationName Name,
879 TemplateParameterList *Params,
882 /// \brief Create an empty function template node.
883 static FunctionTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
885 // Implement isa/cast/dyncast support
886 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
887 static bool classofKind(Kind K) { return K == FunctionTemplate; }
889 friend class ASTDeclReader;
890 friend class ASTDeclWriter;
893 //===----------------------------------------------------------------------===//
894 // Kinds of Template Parameters
895 //===----------------------------------------------------------------------===//
897 /// \brief Defines the position of a template parameter within a template
900 /// Because template parameter can be listed
901 /// sequentially for out-of-line template members, each template parameter is
902 /// given a Depth - the nesting of template parameter scopes - and a Position -
903 /// the occurrence within the parameter list.
904 /// This class is inheritedly privately by different kinds of template
905 /// parameters and is not part of the Decl hierarchy. Just a facility.
906 class TemplateParmPosition {
907 TemplateParmPosition() = delete;
910 TemplateParmPosition(unsigned D, unsigned P)
911 : Depth(D), Position(P)
914 // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
920 /// Get the nesting depth of the template parameter.
921 unsigned getDepth() const { return Depth; }
922 void setDepth(unsigned D) { Depth = D; }
924 /// Get the position of the template parameter within its parameter list.
925 unsigned getPosition() const { return Position; }
926 void setPosition(unsigned P) { Position = P; }
928 /// Get the index of the template parameter within its parameter list.
929 unsigned getIndex() const { return Position; }
932 /// \brief Declaration of a template type parameter.
934 /// For example, "T" in
936 /// template<typename T> class vector;
938 class TemplateTypeParmDecl : public TypeDecl {
939 /// \brief Whether this template type parameter was declaration with
940 /// the 'typename' keyword.
942 /// If false, it was declared with the 'class' keyword.
945 /// \brief Whether this template type parameter inherited its
946 /// default argument.
947 bool InheritedDefault : 1;
949 /// \brief The default template argument, if any.
950 TypeSourceInfo *DefaultArgument;
952 TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
953 SourceLocation IdLoc, IdentifierInfo *Id,
955 : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename),
956 InheritedDefault(false), DefaultArgument() { }
958 /// Sema creates these on the stack during auto type deduction.
962 static TemplateTypeParmDecl *Create(const ASTContext &C, DeclContext *DC,
963 SourceLocation KeyLoc,
964 SourceLocation NameLoc,
965 unsigned D, unsigned P,
966 IdentifierInfo *Id, bool Typename,
968 static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
971 /// \brief Whether this template type parameter was declared with
972 /// the 'typename' keyword.
974 /// If not, it was declared with the 'class' keyword.
975 bool wasDeclaredWithTypename() const { return Typename; }
977 /// \brief Determine whether this template parameter has a default
979 bool hasDefaultArgument() const { return DefaultArgument != nullptr; }
981 /// \brief Retrieve the default argument, if any.
982 QualType getDefaultArgument() const { return DefaultArgument->getType(); }
984 /// \brief Retrieves the default argument's source information, if any.
985 TypeSourceInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
987 /// \brief Retrieves the location of the default argument declaration.
988 SourceLocation getDefaultArgumentLoc() const;
990 /// \brief Determines whether the default argument was inherited
991 /// from a previous declaration of this template.
992 bool defaultArgumentWasInherited() const { return InheritedDefault; }
994 /// \brief Set the default argument for this template parameter, and
995 /// whether that default argument was inherited from another
997 void setDefaultArgument(TypeSourceInfo *DefArg, bool Inherited) {
998 DefaultArgument = DefArg;
999 InheritedDefault = Inherited;
1002 /// \brief Removes the default argument of this template parameter.
1003 void removeDefaultArgument() {
1004 DefaultArgument = nullptr;
1005 InheritedDefault = false;
1008 /// \brief Set whether this template type parameter was declared with
1009 /// the 'typename' or 'class' keyword.
1010 void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; }
1012 /// \brief Retrieve the depth of the template parameter.
1013 unsigned getDepth() const;
1015 /// \brief Retrieve the index of the template parameter.
1016 unsigned getIndex() const;
1018 /// \brief Returns whether this is a parameter pack.
1019 bool isParameterPack() const;
1021 SourceRange getSourceRange() const override LLVM_READONLY;
1023 // Implement isa/cast/dyncast/etc.
1024 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1025 static bool classofKind(Kind K) { return K == TemplateTypeParm; }
1028 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
1031 /// template<int Size> class array { };
1033 class NonTypeTemplateParmDecl
1034 : public DeclaratorDecl, protected TemplateParmPosition {
1035 /// \brief The default template argument, if any, and whether or not
1036 /// it was inherited.
1037 llvm::PointerIntPair<Expr*, 1, bool> DefaultArgumentAndInherited;
1039 // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index
1040 // down here to save memory.
1042 /// \brief Whether this non-type template parameter is a parameter pack.
1045 /// \brief Whether this non-type template parameter is an "expanded"
1046 /// parameter pack, meaning that its type is a pack expansion and we
1047 /// already know the set of types that expansion expands to.
1048 bool ExpandedParameterPack;
1050 /// \brief The number of types in an expanded parameter pack.
1051 unsigned NumExpandedTypes;
1053 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1054 SourceLocation IdLoc, unsigned D, unsigned P,
1055 IdentifierInfo *Id, QualType T,
1056 bool ParameterPack, TypeSourceInfo *TInfo)
1057 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
1058 TemplateParmPosition(D, P), DefaultArgumentAndInherited(nullptr, false),
1059 ParameterPack(ParameterPack), ExpandedParameterPack(false),
1063 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1064 SourceLocation IdLoc, unsigned D, unsigned P,
1065 IdentifierInfo *Id, QualType T,
1066 TypeSourceInfo *TInfo,
1067 const QualType *ExpandedTypes,
1068 unsigned NumExpandedTypes,
1069 TypeSourceInfo **ExpandedTInfos);
1071 friend class ASTDeclReader;
1074 static NonTypeTemplateParmDecl *
1075 Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1076 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1077 QualType T, bool ParameterPack, TypeSourceInfo *TInfo);
1079 static NonTypeTemplateParmDecl *
1080 Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1081 SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1082 QualType T, TypeSourceInfo *TInfo,
1083 const QualType *ExpandedTypes, unsigned NumExpandedTypes,
1084 TypeSourceInfo **ExpandedTInfos);
1086 static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1088 static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1090 unsigned NumExpandedTypes);
1092 using TemplateParmPosition::getDepth;
1093 using TemplateParmPosition::setDepth;
1094 using TemplateParmPosition::getPosition;
1095 using TemplateParmPosition::setPosition;
1096 using TemplateParmPosition::getIndex;
1098 SourceRange getSourceRange() const override LLVM_READONLY;
1100 /// \brief Determine whether this template parameter has a default
1102 bool hasDefaultArgument() const {
1103 return DefaultArgumentAndInherited.getPointer() != nullptr;
1106 /// \brief Retrieve the default argument, if any.
1107 Expr *getDefaultArgument() const {
1108 return DefaultArgumentAndInherited.getPointer();
1111 /// \brief Retrieve the location of the default argument, if any.
1112 SourceLocation getDefaultArgumentLoc() const;
1114 /// \brief Determines whether the default argument was inherited
1115 /// from a previous declaration of this template.
1116 bool defaultArgumentWasInherited() const {
1117 return DefaultArgumentAndInherited.getInt();
1120 /// \brief Set the default argument for this template parameter, and
1121 /// whether that default argument was inherited from another
1123 void setDefaultArgument(Expr *DefArg, bool Inherited) {
1124 DefaultArgumentAndInherited.setPointer(DefArg);
1125 DefaultArgumentAndInherited.setInt(Inherited);
1128 /// \brief Removes the default argument of this template parameter.
1129 void removeDefaultArgument() {
1130 DefaultArgumentAndInherited.setPointer(nullptr);
1131 DefaultArgumentAndInherited.setInt(false);
1134 /// \brief Whether this parameter is a non-type template parameter pack.
1136 /// If the parameter is a parameter pack, the type may be a
1137 /// \c PackExpansionType. In the following example, the \c Dims parameter
1138 /// is a parameter pack (whose type is 'unsigned').
1141 /// template<typename T, unsigned ...Dims> struct multi_array;
1143 bool isParameterPack() const { return ParameterPack; }
1145 /// \brief Whether this parameter pack is a pack expansion.
1147 /// A non-type template parameter pack is a pack expansion if its type
1148 /// contains an unexpanded parameter pack. In this case, we will have
1149 /// built a PackExpansionType wrapping the type.
1150 bool isPackExpansion() const {
1151 return ParameterPack && getType()->getAs<PackExpansionType>();
1154 /// \brief Whether this parameter is a non-type template parameter pack
1155 /// that has a known list of different types at different positions.
1157 /// A parameter pack is an expanded parameter pack when the original
1158 /// parameter pack's type was itself a pack expansion, and that expansion
1159 /// has already been expanded. For example, given:
1162 /// template<typename ...Types>
1164 /// template<Types ...Values>
1165 /// struct Y { /* ... */ };
1169 /// The parameter pack \c Values has a \c PackExpansionType as its type,
1170 /// which expands \c Types. When \c Types is supplied with template arguments
1171 /// by instantiating \c X, the instantiation of \c Values becomes an
1172 /// expanded parameter pack. For example, instantiating
1173 /// \c X<int, unsigned int> results in \c Values being an expanded parameter
1174 /// pack with expansion types \c int and \c unsigned int.
1176 /// The \c getExpansionType() and \c getExpansionTypeSourceInfo() functions
1177 /// return the expansion types.
1178 bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1180 /// \brief Retrieves the number of expansion types in an expanded parameter
1182 unsigned getNumExpansionTypes() const {
1183 assert(ExpandedParameterPack && "Not an expansion parameter pack");
1184 return NumExpandedTypes;
1187 /// \brief Retrieve a particular expansion type within an expanded parameter
1189 QualType getExpansionType(unsigned I) const {
1190 assert(I < NumExpandedTypes && "Out-of-range expansion type index");
1191 void * const *TypesAndInfos = reinterpret_cast<void * const*>(this + 1);
1192 return QualType::getFromOpaquePtr(TypesAndInfos[2*I]);
1195 /// \brief Retrieve a particular expansion type source info within an
1196 /// expanded parameter pack.
1197 TypeSourceInfo *getExpansionTypeSourceInfo(unsigned I) const {
1198 assert(I < NumExpandedTypes && "Out-of-range expansion type index");
1199 void * const *TypesAndInfos = reinterpret_cast<void * const*>(this + 1);
1200 return static_cast<TypeSourceInfo *>(TypesAndInfos[2*I+1]);
1203 // Implement isa/cast/dyncast/etc.
1204 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1205 static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
1208 /// TemplateTemplateParmDecl - Declares a template template parameter,
1211 /// template <template <typename> class T> class container { };
1213 /// A template template parameter is a TemplateDecl because it defines the
1214 /// name of a template and the template parameters allowable for substitution.
1215 class TemplateTemplateParmDecl : public TemplateDecl,
1216 protected TemplateParmPosition
1218 void anchor() override;
1220 /// DefaultArgument - The default template argument, if any.
1221 TemplateArgumentLoc DefaultArgument;
1222 /// Whether or not the default argument was inherited.
1223 bool DefaultArgumentWasInherited;
1225 /// \brief Whether this parameter is a parameter pack.
1228 /// \brief Whether this template template parameter is an "expanded"
1229 /// parameter pack, meaning that it is a pack expansion and we
1230 /// already know the set of template parameters that expansion expands to.
1231 bool ExpandedParameterPack;
1233 /// \brief The number of parameters in an expanded parameter pack.
1234 unsigned NumExpandedParams;
1236 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
1237 unsigned D, unsigned P, bool ParameterPack,
1238 IdentifierInfo *Id, TemplateParameterList *Params)
1239 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
1240 TemplateParmPosition(D, P), DefaultArgument(),
1241 DefaultArgumentWasInherited(false), ParameterPack(ParameterPack),
1242 ExpandedParameterPack(false), NumExpandedParams(0)
1245 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
1246 unsigned D, unsigned P,
1247 IdentifierInfo *Id, TemplateParameterList *Params,
1248 unsigned NumExpansions,
1249 TemplateParameterList * const *Expansions);
1252 static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1253 SourceLocation L, unsigned D,
1254 unsigned P, bool ParameterPack,
1256 TemplateParameterList *Params);
1257 static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1258 SourceLocation L, unsigned D,
1261 TemplateParameterList *Params,
1262 ArrayRef<TemplateParameterList *> Expansions);
1264 static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1266 static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1268 unsigned NumExpansions);
1270 using TemplateParmPosition::getDepth;
1271 using TemplateParmPosition::getPosition;
1272 using TemplateParmPosition::getIndex;
1274 /// \brief Whether this template template parameter is a template
1278 /// template<template <class T> ...MetaFunctions> struct Apply;
1280 bool isParameterPack() const { return ParameterPack; }
1282 /// \brief Whether this parameter pack is a pack expansion.
1284 /// A template template parameter pack is a pack expansion if its template
1285 /// parameter list contains an unexpanded parameter pack.
1286 bool isPackExpansion() const {
1287 return ParameterPack &&
1288 getTemplateParameters()->containsUnexpandedParameterPack();
1291 /// \brief Whether this parameter is a template template parameter pack that
1292 /// has a known list of different template parameter lists at different
1295 /// A parameter pack is an expanded parameter pack when the original parameter
1296 /// pack's template parameter list was itself a pack expansion, and that
1297 /// expansion has already been expanded. For exampe, given:
1300 /// template<typename...Types> struct Outer {
1301 /// template<template<Types> class...Templates> struct Inner;
1305 /// The parameter pack \c Templates is a pack expansion, which expands the
1306 /// pack \c Types. When \c Types is supplied with template arguments by
1307 /// instantiating \c Outer, the instantiation of \c Templates is an expanded
1309 bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1311 /// \brief Retrieves the number of expansion template parameters in
1312 /// an expanded parameter pack.
1313 unsigned getNumExpansionTemplateParameters() const {
1314 assert(ExpandedParameterPack && "Not an expansion parameter pack");
1315 return NumExpandedParams;
1318 /// \brief Retrieve a particular expansion type within an expanded parameter
1320 TemplateParameterList *getExpansionTemplateParameters(unsigned I) const {
1321 assert(I < NumExpandedParams && "Out-of-range expansion type index");
1322 return reinterpret_cast<TemplateParameterList *const *>(this + 1)[I];
1325 /// \brief Determine whether this template parameter has a default
1327 bool hasDefaultArgument() const {
1328 return !DefaultArgument.getArgument().isNull();
1331 /// \brief Retrieve the default argument, if any.
1332 const TemplateArgumentLoc &getDefaultArgument() const {
1333 return DefaultArgument;
1336 /// \brief Retrieve the location of the default argument, if any.
1337 SourceLocation getDefaultArgumentLoc() const;
1339 /// \brief Determines whether the default argument was inherited
1340 /// from a previous declaration of this template.
1341 bool defaultArgumentWasInherited() const {
1342 return DefaultArgumentWasInherited;
1345 /// \brief Set the default argument for this template parameter, and
1346 /// whether that default argument was inherited from another
1348 void setDefaultArgument(const TemplateArgumentLoc &DefArg, bool Inherited) {
1349 DefaultArgument = DefArg;
1350 DefaultArgumentWasInherited = Inherited;
1353 /// \brief Removes the default argument of this template parameter.
1354 void removeDefaultArgument() {
1355 DefaultArgument = TemplateArgumentLoc();
1356 DefaultArgumentWasInherited = false;
1359 SourceRange getSourceRange() const override LLVM_READONLY {
1360 SourceLocation End = getLocation();
1361 if (hasDefaultArgument() && !defaultArgumentWasInherited())
1362 End = getDefaultArgument().getSourceRange().getEnd();
1363 return SourceRange(getTemplateParameters()->getTemplateLoc(), End);
1366 // Implement isa/cast/dyncast/etc.
1367 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1368 static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
1370 friend class ASTDeclReader;
1371 friend class ASTDeclWriter;
1374 /// \brief Represents a class template specialization, which refers to
1375 /// a class template with a given set of template arguments.
1377 /// Class template specializations represent both explicit
1378 /// specialization of class templates, as in the example below, and
1379 /// implicit instantiations of class templates.
1382 /// template<typename T> class array;
1385 /// class array<bool> { }; // class template specialization array<bool>
1387 class ClassTemplateSpecializationDecl
1388 : public CXXRecordDecl, public llvm::FoldingSetNode {
1390 /// \brief Structure that stores information about a class template
1391 /// specialization that was instantiated from a class template partial
1393 struct SpecializedPartialSpecialization {
1394 /// \brief The class template partial specialization from which this
1395 /// class template specialization was instantiated.
1396 ClassTemplatePartialSpecializationDecl *PartialSpecialization;
1398 /// \brief The template argument list deduced for the class template
1399 /// partial specialization itself.
1400 const TemplateArgumentList *TemplateArgs;
1403 /// \brief The template that this specialization specializes
1404 llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
1405 SpecializedTemplate;
1407 /// \brief Further info for explicit template specialization/instantiation.
1408 struct ExplicitSpecializationInfo {
1409 /// \brief The type-as-written.
1410 TypeSourceInfo *TypeAsWritten;
1411 /// \brief The location of the extern keyword.
1412 SourceLocation ExternLoc;
1413 /// \brief The location of the template keyword.
1414 SourceLocation TemplateKeywordLoc;
1416 ExplicitSpecializationInfo()
1417 : TypeAsWritten(nullptr), ExternLoc(), TemplateKeywordLoc() {}
1420 /// \brief Further info for explicit template specialization/instantiation.
1421 /// Does not apply to implicit specializations.
1422 ExplicitSpecializationInfo *ExplicitInfo;
1424 /// \brief The template arguments used to describe this specialization.
1425 const TemplateArgumentList *TemplateArgs;
1427 /// \brief The point where this template was instantiated (if any)
1428 SourceLocation PointOfInstantiation;
1430 /// \brief The kind of specialization this declaration refers to.
1431 /// Really a value of type TemplateSpecializationKind.
1432 unsigned SpecializationKind : 3;
1435 ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
1436 DeclContext *DC, SourceLocation StartLoc,
1437 SourceLocation IdLoc,
1438 ClassTemplateDecl *SpecializedTemplate,
1439 const TemplateArgument *Args,
1441 ClassTemplateSpecializationDecl *PrevDecl);
1443 explicit ClassTemplateSpecializationDecl(ASTContext &C, Kind DK);
1446 static ClassTemplateSpecializationDecl *
1447 Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1448 SourceLocation StartLoc, SourceLocation IdLoc,
1449 ClassTemplateDecl *SpecializedTemplate,
1450 const TemplateArgument *Args,
1452 ClassTemplateSpecializationDecl *PrevDecl);
1453 static ClassTemplateSpecializationDecl *
1454 CreateDeserialized(ASTContext &C, unsigned ID);
1456 void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
1457 bool Qualified) const override;
1459 // FIXME: This is broken. CXXRecordDecl::getMostRecentDecl() returns a
1460 // different "most recent" declaration from this function for the same
1461 // declaration, because we don't override getMostRecentDeclImpl(). But
1462 // it's not clear that we should override that, because the most recent
1463 // declaration as a CXXRecordDecl sometimes is the injected-class-name.
1464 ClassTemplateSpecializationDecl *getMostRecentDecl() {
1465 CXXRecordDecl *Recent = static_cast<CXXRecordDecl *>(
1466 this)->getMostRecentDecl();
1467 while (!isa<ClassTemplateSpecializationDecl>(Recent)) {
1468 // FIXME: Does injected class name need to be in the redeclarations chain?
1469 assert(Recent->isInjectedClassName() && Recent->getPreviousDecl());
1470 Recent = Recent->getPreviousDecl();
1472 return cast<ClassTemplateSpecializationDecl>(Recent);
1475 /// \brief Retrieve the template that this specialization specializes.
1476 ClassTemplateDecl *getSpecializedTemplate() const;
1478 /// \brief Retrieve the template arguments of the class template
1480 const TemplateArgumentList &getTemplateArgs() const {
1481 return *TemplateArgs;
1484 /// \brief Determine the kind of specialization that this
1485 /// declaration represents.
1486 TemplateSpecializationKind getSpecializationKind() const {
1487 return static_cast<TemplateSpecializationKind>(SpecializationKind);
1490 bool isExplicitSpecialization() const {
1491 return getSpecializationKind() == TSK_ExplicitSpecialization;
1494 /// \brief True if this declaration is an explicit specialization,
1495 /// explicit instantiation declaration, or explicit instantiation
1497 bool isExplicitInstantiationOrSpecialization() const {
1498 switch (getTemplateSpecializationKind()) {
1499 case TSK_ExplicitSpecialization:
1500 case TSK_ExplicitInstantiationDeclaration:
1501 case TSK_ExplicitInstantiationDefinition:
1504 case TSK_Undeclared:
1505 case TSK_ImplicitInstantiation:
1508 llvm_unreachable("bad template specialization kind");
1511 void setSpecializationKind(TemplateSpecializationKind TSK) {
1512 SpecializationKind = TSK;
1515 /// \brief Get the point of instantiation (if any), or null if none.
1516 SourceLocation getPointOfInstantiation() const {
1517 return PointOfInstantiation;
1520 void setPointOfInstantiation(SourceLocation Loc) {
1521 assert(Loc.isValid() && "point of instantiation must be valid!");
1522 PointOfInstantiation = Loc;
1525 /// \brief If this class template specialization is an instantiation of
1526 /// a template (rather than an explicit specialization), return the
1527 /// class template or class template partial specialization from which it
1528 /// was instantiated.
1529 llvm::PointerUnion<ClassTemplateDecl *,
1530 ClassTemplatePartialSpecializationDecl *>
1531 getInstantiatedFrom() const {
1532 if (!isTemplateInstantiation(getSpecializationKind()))
1533 return llvm::PointerUnion<ClassTemplateDecl *,
1534 ClassTemplatePartialSpecializationDecl *>();
1536 return getSpecializedTemplateOrPartial();
1539 /// \brief Retrieve the class template or class template partial
1540 /// specialization which was specialized by this.
1541 llvm::PointerUnion<ClassTemplateDecl *,
1542 ClassTemplatePartialSpecializationDecl *>
1543 getSpecializedTemplateOrPartial() const {
1544 if (SpecializedPartialSpecialization *PartialSpec
1545 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
1546 return PartialSpec->PartialSpecialization;
1548 return SpecializedTemplate.get<ClassTemplateDecl*>();
1551 /// \brief Retrieve the set of template arguments that should be used
1552 /// to instantiate members of the class template or class template partial
1553 /// specialization from which this class template specialization was
1556 /// \returns For a class template specialization instantiated from the primary
1557 /// template, this function will return the same template arguments as
1558 /// getTemplateArgs(). For a class template specialization instantiated from
1559 /// a class template partial specialization, this function will return the
1560 /// deduced template arguments for the class template partial specialization
1562 const TemplateArgumentList &getTemplateInstantiationArgs() const {
1563 if (SpecializedPartialSpecialization *PartialSpec
1564 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
1565 return *PartialSpec->TemplateArgs;
1567 return getTemplateArgs();
1570 /// \brief Note that this class template specialization is actually an
1571 /// instantiation of the given class template partial specialization whose
1572 /// template arguments have been deduced.
1573 void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
1574 const TemplateArgumentList *TemplateArgs) {
1575 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1576 "Already set to a class template partial specialization!");
1577 SpecializedPartialSpecialization *PS
1578 = new (getASTContext()) SpecializedPartialSpecialization();
1579 PS->PartialSpecialization = PartialSpec;
1580 PS->TemplateArgs = TemplateArgs;
1581 SpecializedTemplate = PS;
1584 /// \brief Note that this class template specialization is an instantiation
1585 /// of the given class template.
1586 void setInstantiationOf(ClassTemplateDecl *TemplDecl) {
1587 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1588 "Previously set to a class template partial specialization!");
1589 SpecializedTemplate = TemplDecl;
1592 /// \brief Sets the type of this specialization as it was written by
1593 /// the user. This will be a class template specialization type.
1594 void setTypeAsWritten(TypeSourceInfo *T) {
1596 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1597 ExplicitInfo->TypeAsWritten = T;
1599 /// \brief Gets the type of this specialization as it was written by
1600 /// the user, if it was so written.
1601 TypeSourceInfo *getTypeAsWritten() const {
1602 return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
1605 /// \brief Gets the location of the extern keyword, if present.
1606 SourceLocation getExternLoc() const {
1607 return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
1609 /// \brief Sets the location of the extern keyword.
1610 void setExternLoc(SourceLocation Loc) {
1612 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1613 ExplicitInfo->ExternLoc = Loc;
1616 /// \brief Sets the location of the template keyword.
1617 void setTemplateKeywordLoc(SourceLocation Loc) {
1619 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1620 ExplicitInfo->TemplateKeywordLoc = Loc;
1622 /// \brief Gets the location of the template keyword, if present.
1623 SourceLocation getTemplateKeywordLoc() const {
1624 return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
1627 SourceRange getSourceRange() const override LLVM_READONLY;
1629 void Profile(llvm::FoldingSetNodeID &ID) const {
1630 Profile(ID, TemplateArgs->asArray(), getASTContext());
1634 Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
1635 ASTContext &Context) {
1636 ID.AddInteger(TemplateArgs.size());
1637 for (unsigned Arg = 0; Arg != TemplateArgs.size(); ++Arg)
1638 TemplateArgs[Arg].Profile(ID, Context);
1641 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1642 static bool classofKind(Kind K) {
1643 return K >= firstClassTemplateSpecialization &&
1644 K <= lastClassTemplateSpecialization;
1647 friend class ASTDeclReader;
1648 friend class ASTDeclWriter;
1651 class ClassTemplatePartialSpecializationDecl
1652 : public ClassTemplateSpecializationDecl {
1653 void anchor() override;
1655 /// \brief The list of template parameters
1656 TemplateParameterList* TemplateParams;
1658 /// \brief The source info for the template arguments as written.
1659 /// FIXME: redundant with TypeAsWritten?
1660 const ASTTemplateArgumentListInfo *ArgsAsWritten;
1662 /// \brief The class template partial specialization from which this
1663 /// class template partial specialization was instantiated.
1665 /// The boolean value will be true to indicate that this class template
1666 /// partial specialization was specialized at this level.
1667 llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
1668 InstantiatedFromMember;
1670 ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
1672 SourceLocation StartLoc,
1673 SourceLocation IdLoc,
1674 TemplateParameterList *Params,
1675 ClassTemplateDecl *SpecializedTemplate,
1676 const TemplateArgument *Args,
1678 const ASTTemplateArgumentListInfo *ArgsAsWritten,
1679 ClassTemplatePartialSpecializationDecl *PrevDecl);
1681 ClassTemplatePartialSpecializationDecl(ASTContext &C)
1682 : ClassTemplateSpecializationDecl(C, ClassTemplatePartialSpecialization),
1683 TemplateParams(nullptr), ArgsAsWritten(nullptr),
1684 InstantiatedFromMember(nullptr, false) {}
1687 static ClassTemplatePartialSpecializationDecl *
1688 Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1689 SourceLocation StartLoc, SourceLocation IdLoc,
1690 TemplateParameterList *Params,
1691 ClassTemplateDecl *SpecializedTemplate,
1692 const TemplateArgument *Args,
1694 const TemplateArgumentListInfo &ArgInfos,
1695 QualType CanonInjectedType,
1696 ClassTemplatePartialSpecializationDecl *PrevDecl);
1698 static ClassTemplatePartialSpecializationDecl *
1699 CreateDeserialized(ASTContext &C, unsigned ID);
1701 ClassTemplatePartialSpecializationDecl *getMostRecentDecl() {
1702 return cast<ClassTemplatePartialSpecializationDecl>(
1703 static_cast<ClassTemplateSpecializationDecl *>(
1704 this)->getMostRecentDecl());
1707 /// Get the list of template parameters
1708 TemplateParameterList *getTemplateParameters() const {
1709 return TemplateParams;
1712 /// Get the template arguments as written.
1713 const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
1714 return ArgsAsWritten;
1717 /// \brief Retrieve the member class template partial specialization from
1718 /// which this particular class template partial specialization was
1722 /// template<typename T>
1724 /// template<typename U> struct Inner;
1725 /// template<typename U> struct Inner<U*> { }; // #1
1728 /// Outer<float>::Inner<int*> ii;
1731 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
1732 /// end up instantiating the partial specialization
1733 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
1734 /// template partial specialization \c Outer<T>::Inner<U*>. Given
1735 /// \c Outer<float>::Inner<U*>, this function would return
1736 /// \c Outer<T>::Inner<U*>.
1737 ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
1738 ClassTemplatePartialSpecializationDecl *First =
1739 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1740 return First->InstantiatedFromMember.getPointer();
1743 void setInstantiatedFromMember(
1744 ClassTemplatePartialSpecializationDecl *PartialSpec) {
1745 ClassTemplatePartialSpecializationDecl *First =
1746 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1747 First->InstantiatedFromMember.setPointer(PartialSpec);
1750 /// \brief Determines whether this class template partial specialization
1751 /// template was a specialization of a member partial specialization.
1753 /// In the following example, the member template partial specialization
1754 /// \c X<int>::Inner<T*> is a member specialization.
1757 /// template<typename T>
1759 /// template<typename U> struct Inner;
1760 /// template<typename U> struct Inner<U*>;
1763 /// template<> template<typename T>
1764 /// struct X<int>::Inner<T*> { /* ... */ };
1766 bool isMemberSpecialization() {
1767 ClassTemplatePartialSpecializationDecl *First =
1768 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1769 return First->InstantiatedFromMember.getInt();
1772 /// \brief Note that this member template is a specialization.
1773 void setMemberSpecialization() {
1774 ClassTemplatePartialSpecializationDecl *First =
1775 cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1776 assert(First->InstantiatedFromMember.getPointer() &&
1777 "Only member templates can be member template specializations");
1778 return First->InstantiatedFromMember.setInt(true);
1781 /// Retrieves the injected specialization type for this partial
1782 /// specialization. This is not the same as the type-decl-type for
1783 /// this partial specialization, which is an InjectedClassNameType.
1784 QualType getInjectedSpecializationType() const {
1785 assert(getTypeForDecl() && "partial specialization has no type set!");
1786 return cast<InjectedClassNameType>(getTypeForDecl())
1787 ->getInjectedSpecializationType();
1790 // FIXME: Add Profile support!
1792 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1793 static bool classofKind(Kind K) {
1794 return K == ClassTemplatePartialSpecialization;
1797 friend class ASTDeclReader;
1798 friend class ASTDeclWriter;
1801 /// Declaration of a class template.
1802 class ClassTemplateDecl : public RedeclarableTemplateDecl {
1803 static void DeallocateCommon(void *Ptr);
1806 /// \brief Data that is common to all of the declarations of a given
1808 struct Common : CommonBase {
1809 Common() : LazySpecializations() { }
1811 /// \brief The class template specializations for this class
1812 /// template, including explicit specializations and instantiations.
1813 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations;
1815 /// \brief The class template partial specializations for this class
1817 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>
1818 PartialSpecializations;
1820 /// \brief The injected-class-name type for this class template.
1821 QualType InjectedClassNameType;
1823 /// \brief If non-null, points to an array of specializations (including
1824 /// partial specializations) known only by their external declaration IDs.
1826 /// The first value in the array is the number of of specializations/
1827 /// partial specializations that follow.
1828 uint32_t *LazySpecializations;
1831 /// \brief Retrieve the set of specializations of this class template.
1832 llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
1833 getSpecializations() const;
1835 /// \brief Retrieve the set of partial specializations of this class
1837 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
1838 getPartialSpecializations();
1840 ClassTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
1841 DeclarationName Name, TemplateParameterList *Params,
1843 : RedeclarableTemplateDecl(ClassTemplate, C, DC, L, Name, Params, Decl) {}
1845 CommonBase *newCommon(ASTContext &C) const override;
1847 Common *getCommonPtr() const {
1848 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
1852 /// \brief Load any lazily-loaded specializations from the external source.
1853 void LoadLazySpecializations() const;
1855 /// \brief Get the underlying class declarations of the template.
1856 CXXRecordDecl *getTemplatedDecl() const {
1857 return static_cast<CXXRecordDecl *>(TemplatedDecl);
1860 /// \brief Returns whether this template declaration defines the primary
1862 bool isThisDeclarationADefinition() const {
1863 return getTemplatedDecl()->isThisDeclarationADefinition();
1866 /// \brief Create a class template node.
1867 static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
1869 DeclarationName Name,
1870 TemplateParameterList *Params,
1872 ClassTemplateDecl *PrevDecl);
1874 /// \brief Create an empty class template node.
1875 static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1877 /// \brief Return the specialization with the provided arguments if it exists,
1878 /// otherwise return the insertion point.
1879 ClassTemplateSpecializationDecl *
1880 findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
1882 /// \brief Insert the specified specialization knowing that it is not already
1883 /// in. InsertPos must be obtained from findSpecialization.
1884 void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos);
1886 ClassTemplateDecl *getCanonicalDecl() override {
1887 return cast<ClassTemplateDecl>(
1888 RedeclarableTemplateDecl::getCanonicalDecl());
1890 const ClassTemplateDecl *getCanonicalDecl() const {
1891 return cast<ClassTemplateDecl>(
1892 RedeclarableTemplateDecl::getCanonicalDecl());
1895 /// \brief Retrieve the previous declaration of this class template, or
1896 /// NULL if no such declaration exists.
1897 ClassTemplateDecl *getPreviousDecl() {
1898 return cast_or_null<ClassTemplateDecl>(
1899 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
1902 /// \brief Retrieve the previous declaration of this class template, or
1903 /// NULL if no such declaration exists.
1904 const ClassTemplateDecl *getPreviousDecl() const {
1905 return cast_or_null<ClassTemplateDecl>(
1906 static_cast<const RedeclarableTemplateDecl *>(
1907 this)->getPreviousDecl());
1910 ClassTemplateDecl *getMostRecentDecl() {
1911 return cast<ClassTemplateDecl>(
1912 static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
1914 const ClassTemplateDecl *getMostRecentDecl() const {
1915 return const_cast<ClassTemplateDecl*>(this)->getMostRecentDecl();
1918 ClassTemplateDecl *getInstantiatedFromMemberTemplate() {
1919 return cast_or_null<ClassTemplateDecl>(
1920 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
1923 /// \brief Return the partial specialization with the provided arguments if it
1924 /// exists, otherwise return the insertion point.
1925 ClassTemplatePartialSpecializationDecl *
1926 findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
1928 /// \brief Insert the specified partial specialization knowing that it is not
1929 /// already in. InsertPos must be obtained from findPartialSpecialization.
1930 void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D,
1933 /// \brief Retrieve the partial specializations as an ordered list.
1934 void getPartialSpecializations(
1935 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS);
1937 /// \brief Find a class template partial specialization with the given
1940 /// \param T a dependent type that names a specialization of this class
1943 /// \returns the class template partial specialization that exactly matches
1944 /// the type \p T, or NULL if no such partial specialization exists.
1945 ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
1947 /// \brief Find a class template partial specialization which was instantiated
1948 /// from the given member partial specialization.
1950 /// \param D a member class template partial specialization.
1952 /// \returns the class template partial specialization which was instantiated
1953 /// from the given member partial specialization, or NULL if no such partial
1954 /// specialization exists.
1955 ClassTemplatePartialSpecializationDecl *
1956 findPartialSpecInstantiatedFromMember(
1957 ClassTemplatePartialSpecializationDecl *D);
1959 /// \brief Retrieve the template specialization type of the
1960 /// injected-class-name for this class template.
1962 /// The injected-class-name for a class template \c X is \c
1963 /// X<template-args>, where \c template-args is formed from the
1964 /// template arguments that correspond to the template parameters of
1965 /// \c X. For example:
1968 /// template<typename T, int N>
1970 /// typedef array this_type; // "array" is equivalent to "array<T, N>"
1973 QualType getInjectedClassNameSpecialization();
1975 typedef SpecIterator<ClassTemplateSpecializationDecl> spec_iterator;
1976 typedef llvm::iterator_range<spec_iterator> spec_range;
1978 spec_range specializations() const {
1979 return spec_range(spec_begin(), spec_end());
1982 spec_iterator spec_begin() const {
1983 return makeSpecIterator(getSpecializations(), false);
1986 spec_iterator spec_end() const {
1987 return makeSpecIterator(getSpecializations(), true);
1990 // Implement isa/cast/dyncast support
1991 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1992 static bool classofKind(Kind K) { return K == ClassTemplate; }
1994 friend class ASTDeclReader;
1995 friend class ASTDeclWriter;
1998 /// \brief Declaration of a friend template.
2002 /// template \<typename T> class A {
2003 /// friend class MyVector<T>; // not a friend template
2004 /// template \<typename U> friend class B; // not a friend template
2005 /// template \<typename U> friend class Foo<T>::Nested; // friend template
2009 /// \note This class is not currently in use. All of the above
2010 /// will yield a FriendDecl, not a FriendTemplateDecl.
2011 class FriendTemplateDecl : public Decl {
2012 virtual void anchor();
2014 typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
2017 // The number of template parameters; always non-zero.
2020 // The parameter list.
2021 TemplateParameterList **Params;
2023 // The declaration that's a friend of this class.
2026 // Location of the 'friend' specifier.
2027 SourceLocation FriendLoc;
2030 FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
2032 TemplateParameterList **Params,
2034 SourceLocation FriendLoc)
2035 : Decl(Decl::FriendTemplate, DC, Loc),
2039 FriendLoc(FriendLoc)
2042 FriendTemplateDecl(EmptyShell Empty)
2043 : Decl(Decl::FriendTemplate, Empty),
2049 static FriendTemplateDecl *Create(ASTContext &Context,
2050 DeclContext *DC, SourceLocation Loc,
2052 TemplateParameterList **Params,
2054 SourceLocation FriendLoc);
2056 static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2058 /// If this friend declaration names a templated type (or
2059 /// a dependent member type of a templated type), return that
2060 /// type; otherwise return null.
2061 TypeSourceInfo *getFriendType() const {
2062 return Friend.dyn_cast<TypeSourceInfo*>();
2065 /// If this friend declaration names a templated function (or
2066 /// a member function of a templated type), return that type;
2067 /// otherwise return null.
2068 NamedDecl *getFriendDecl() const {
2069 return Friend.dyn_cast<NamedDecl*>();
2072 /// \brief Retrieves the location of the 'friend' keyword.
2073 SourceLocation getFriendLoc() const {
2077 TemplateParameterList *getTemplateParameterList(unsigned i) const {
2078 assert(i <= NumParams);
2082 unsigned getNumTemplateParameters() const {
2086 // Implement isa/cast/dyncast/etc.
2087 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2088 static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
2090 friend class ASTDeclReader;
2093 /// \brief Declaration of an alias template.
2097 /// template \<typename T> using V = std::map<T*, int, MyCompare<T>>;
2099 class TypeAliasTemplateDecl : public RedeclarableTemplateDecl {
2100 static void DeallocateCommon(void *Ptr);
2103 typedef CommonBase Common;
2105 TypeAliasTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
2106 DeclarationName Name, TemplateParameterList *Params,
2108 : RedeclarableTemplateDecl(TypeAliasTemplate, C, DC, L, Name, Params,
2111 CommonBase *newCommon(ASTContext &C) const override;
2113 Common *getCommonPtr() {
2114 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2118 /// Get the underlying function declaration of the template.
2119 TypeAliasDecl *getTemplatedDecl() const {
2120 return static_cast<TypeAliasDecl*>(TemplatedDecl);
2124 TypeAliasTemplateDecl *getCanonicalDecl() override {
2125 return cast<TypeAliasTemplateDecl>(
2126 RedeclarableTemplateDecl::getCanonicalDecl());
2128 const TypeAliasTemplateDecl *getCanonicalDecl() const {
2129 return cast<TypeAliasTemplateDecl>(
2130 RedeclarableTemplateDecl::getCanonicalDecl());
2133 /// \brief Retrieve the previous declaration of this function template, or
2134 /// NULL if no such declaration exists.
2135 TypeAliasTemplateDecl *getPreviousDecl() {
2136 return cast_or_null<TypeAliasTemplateDecl>(
2137 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2140 /// \brief Retrieve the previous declaration of this function template, or
2141 /// NULL if no such declaration exists.
2142 const TypeAliasTemplateDecl *getPreviousDecl() const {
2143 return cast_or_null<TypeAliasTemplateDecl>(
2144 static_cast<const RedeclarableTemplateDecl *>(
2145 this)->getPreviousDecl());
2148 TypeAliasTemplateDecl *getInstantiatedFromMemberTemplate() {
2149 return cast_or_null<TypeAliasTemplateDecl>(
2150 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
2154 /// \brief Create a function template node.
2155 static TypeAliasTemplateDecl *Create(ASTContext &C, DeclContext *DC,
2157 DeclarationName Name,
2158 TemplateParameterList *Params,
2161 /// \brief Create an empty alias template node.
2162 static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2164 // Implement isa/cast/dyncast support
2165 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2166 static bool classofKind(Kind K) { return K == TypeAliasTemplate; }
2168 friend class ASTDeclReader;
2169 friend class ASTDeclWriter;
2172 /// \brief Declaration of a function specialization at template class scope.
2174 /// This is a non-standard extension needed to support MSVC.
2178 /// template <class T>
2180 /// template <class U> void foo(U a) { }
2181 /// template<> void foo(int a) { }
2185 /// "template<> foo(int a)" will be saved in Specialization as a normal
2186 /// CXXMethodDecl. Then during an instantiation of class A, it will be
2187 /// transformed into an actual function specialization.
2188 class ClassScopeFunctionSpecializationDecl : public Decl {
2189 virtual void anchor();
2191 ClassScopeFunctionSpecializationDecl(DeclContext *DC, SourceLocation Loc,
2192 CXXMethodDecl *FD, bool Args,
2193 TemplateArgumentListInfo TemplArgs)
2194 : Decl(Decl::ClassScopeFunctionSpecialization, DC, Loc),
2195 Specialization(FD), HasExplicitTemplateArgs(Args),
2196 TemplateArgs(TemplArgs) {}
2198 ClassScopeFunctionSpecializationDecl(EmptyShell Empty)
2199 : Decl(Decl::ClassScopeFunctionSpecialization, Empty) {}
2201 CXXMethodDecl *Specialization;
2202 bool HasExplicitTemplateArgs;
2203 TemplateArgumentListInfo TemplateArgs;
2206 CXXMethodDecl *getSpecialization() const { return Specialization; }
2207 bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
2208 const TemplateArgumentListInfo& templateArgs() const { return TemplateArgs; }
2210 static ClassScopeFunctionSpecializationDecl *Create(ASTContext &C,
2214 bool HasExplicitTemplateArgs,
2215 TemplateArgumentListInfo TemplateArgs) {
2216 return new (C, DC) ClassScopeFunctionSpecializationDecl(
2217 DC, Loc, FD, HasExplicitTemplateArgs, TemplateArgs);
2220 static ClassScopeFunctionSpecializationDecl *
2221 CreateDeserialized(ASTContext &Context, unsigned ID);
2223 // Implement isa/cast/dyncast/etc.
2224 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2225 static bool classofKind(Kind K) {
2226 return K == Decl::ClassScopeFunctionSpecialization;
2229 friend class ASTDeclReader;
2230 friend class ASTDeclWriter;
2233 /// Implementation of inline functions that require the template declarations
2234 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
2237 /// \brief Represents a variable template specialization, which refers to
2238 /// a variable template with a given set of template arguments.
2240 /// Variable template specializations represent both explicit
2241 /// specializations of variable templates, as in the example below, and
2242 /// implicit instantiations of variable templates.
2245 /// template<typename T> constexpr T pi = T(3.1415926535897932385);
2248 /// constexpr float pi<float>; // variable template specialization pi<float>
2250 class VarTemplateSpecializationDecl : public VarDecl,
2251 public llvm::FoldingSetNode {
2253 /// \brief Structure that stores information about a variable template
2254 /// specialization that was instantiated from a variable template partial
2256 struct SpecializedPartialSpecialization {
2257 /// \brief The variable template partial specialization from which this
2258 /// variable template specialization was instantiated.
2259 VarTemplatePartialSpecializationDecl *PartialSpecialization;
2261 /// \brief The template argument list deduced for the variable template
2262 /// partial specialization itself.
2263 const TemplateArgumentList *TemplateArgs;
2266 /// \brief The template that this specialization specializes.
2267 llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *>
2268 SpecializedTemplate;
2270 /// \brief Further info for explicit template specialization/instantiation.
2271 struct ExplicitSpecializationInfo {
2272 /// \brief The type-as-written.
2273 TypeSourceInfo *TypeAsWritten;
2274 /// \brief The location of the extern keyword.
2275 SourceLocation ExternLoc;
2276 /// \brief The location of the template keyword.
2277 SourceLocation TemplateKeywordLoc;
2279 ExplicitSpecializationInfo()
2280 : TypeAsWritten(nullptr), ExternLoc(), TemplateKeywordLoc() {}
2283 /// \brief Further info for explicit template specialization/instantiation.
2284 /// Does not apply to implicit specializations.
2285 ExplicitSpecializationInfo *ExplicitInfo;
2287 /// \brief The template arguments used to describe this specialization.
2288 const TemplateArgumentList *TemplateArgs;
2289 TemplateArgumentListInfo TemplateArgsInfo;
2291 /// \brief The point where this template was instantiated (if any).
2292 SourceLocation PointOfInstantiation;
2294 /// \brief The kind of specialization this declaration refers to.
2295 /// Really a value of type TemplateSpecializationKind.
2296 unsigned SpecializationKind : 3;
2299 VarTemplateSpecializationDecl(Kind DK, ASTContext &Context, DeclContext *DC,
2300 SourceLocation StartLoc, SourceLocation IdLoc,
2301 VarTemplateDecl *SpecializedTemplate,
2302 QualType T, TypeSourceInfo *TInfo,
2303 StorageClass S, const TemplateArgument *Args,
2306 explicit VarTemplateSpecializationDecl(Kind DK, ASTContext &Context);
2309 static VarTemplateSpecializationDecl *
2310 Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2311 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
2312 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
2314 static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C,
2317 void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
2318 bool Qualified) const override;
2320 VarTemplateSpecializationDecl *getMostRecentDecl() {
2321 VarDecl *Recent = static_cast<VarDecl *>(this)->getMostRecentDecl();
2322 return cast<VarTemplateSpecializationDecl>(Recent);
2325 /// \brief Retrieve the template that this specialization specializes.
2326 VarTemplateDecl *getSpecializedTemplate() const;
2328 /// \brief Retrieve the template arguments of the variable template
2330 const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; }
2332 // TODO: Always set this when creating the new specialization?
2333 void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo);
2335 const TemplateArgumentListInfo &getTemplateArgsInfo() const {
2336 return TemplateArgsInfo;
2339 /// \brief Determine the kind of specialization that this
2340 /// declaration represents.
2341 TemplateSpecializationKind getSpecializationKind() const {
2342 return static_cast<TemplateSpecializationKind>(SpecializationKind);
2345 bool isExplicitSpecialization() const {
2346 return getSpecializationKind() == TSK_ExplicitSpecialization;
2349 /// \brief True if this declaration is an explicit specialization,
2350 /// explicit instantiation declaration, or explicit instantiation
2352 bool isExplicitInstantiationOrSpecialization() const {
2353 switch (getTemplateSpecializationKind()) {
2354 case TSK_ExplicitSpecialization:
2355 case TSK_ExplicitInstantiationDeclaration:
2356 case TSK_ExplicitInstantiationDefinition:
2359 case TSK_Undeclared:
2360 case TSK_ImplicitInstantiation:
2363 llvm_unreachable("bad template specialization kind");
2366 void setSpecializationKind(TemplateSpecializationKind TSK) {
2367 SpecializationKind = TSK;
2370 /// \brief Get the point of instantiation (if any), or null if none.
2371 SourceLocation getPointOfInstantiation() const {
2372 return PointOfInstantiation;
2375 void setPointOfInstantiation(SourceLocation Loc) {
2376 assert(Loc.isValid() && "point of instantiation must be valid!");
2377 PointOfInstantiation = Loc;
2380 /// \brief If this variable template specialization is an instantiation of
2381 /// a template (rather than an explicit specialization), return the
2382 /// variable template or variable template partial specialization from which
2383 /// it was instantiated.
2384 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2385 getInstantiatedFrom() const {
2386 if (getSpecializationKind() != TSK_ImplicitInstantiation &&
2387 getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
2388 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
2389 return llvm::PointerUnion<VarTemplateDecl *,
2390 VarTemplatePartialSpecializationDecl *>();
2392 if (SpecializedPartialSpecialization *PartialSpec =
2393 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2394 return PartialSpec->PartialSpecialization;
2396 return SpecializedTemplate.get<VarTemplateDecl *>();
2399 /// \brief Retrieve the variable template or variable template partial
2400 /// specialization which was specialized by this.
2401 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2402 getSpecializedTemplateOrPartial() const {
2403 if (SpecializedPartialSpecialization *PartialSpec =
2404 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2405 return PartialSpec->PartialSpecialization;
2407 return SpecializedTemplate.get<VarTemplateDecl *>();
2410 /// \brief Retrieve the set of template arguments that should be used
2411 /// to instantiate the initializer of the variable template or variable
2412 /// template partial specialization from which this variable template
2413 /// specialization was instantiated.
2415 /// \returns For a variable template specialization instantiated from the
2416 /// primary template, this function will return the same template arguments
2417 /// as getTemplateArgs(). For a variable template specialization instantiated
2418 /// from a variable template partial specialization, this function will the
2419 /// return deduced template arguments for the variable template partial
2420 /// specialization itself.
2421 const TemplateArgumentList &getTemplateInstantiationArgs() const {
2422 if (SpecializedPartialSpecialization *PartialSpec =
2423 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2424 return *PartialSpec->TemplateArgs;
2426 return getTemplateArgs();
2429 /// \brief Note that this variable template specialization is actually an
2430 /// instantiation of the given variable template partial specialization whose
2431 /// template arguments have been deduced.
2432 void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec,
2433 const TemplateArgumentList *TemplateArgs) {
2434 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2435 "Already set to a variable template partial specialization!");
2436 SpecializedPartialSpecialization *PS =
2437 new (getASTContext()) SpecializedPartialSpecialization();
2438 PS->PartialSpecialization = PartialSpec;
2439 PS->TemplateArgs = TemplateArgs;
2440 SpecializedTemplate = PS;
2443 /// \brief Note that this variable template specialization is an instantiation
2444 /// of the given variable template.
2445 void setInstantiationOf(VarTemplateDecl *TemplDecl) {
2446 assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2447 "Previously set to a variable template partial specialization!");
2448 SpecializedTemplate = TemplDecl;
2451 /// \brief Sets the type of this specialization as it was written by
2453 void setTypeAsWritten(TypeSourceInfo *T) {
2455 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2456 ExplicitInfo->TypeAsWritten = T;
2458 /// \brief Gets the type of this specialization as it was written by
2459 /// the user, if it was so written.
2460 TypeSourceInfo *getTypeAsWritten() const {
2461 return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
2464 /// \brief Gets the location of the extern keyword, if present.
2465 SourceLocation getExternLoc() const {
2466 return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
2468 /// \brief Sets the location of the extern keyword.
2469 void setExternLoc(SourceLocation Loc) {
2471 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2472 ExplicitInfo->ExternLoc = Loc;
2475 /// \brief Sets the location of the template keyword.
2476 void setTemplateKeywordLoc(SourceLocation Loc) {
2478 ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2479 ExplicitInfo->TemplateKeywordLoc = Loc;
2481 /// \brief Gets the location of the template keyword, if present.
2482 SourceLocation getTemplateKeywordLoc() const {
2483 return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
2486 void Profile(llvm::FoldingSetNodeID &ID) const {
2487 Profile(ID, TemplateArgs->asArray(), getASTContext());
2490 static void Profile(llvm::FoldingSetNodeID &ID,
2491 ArrayRef<TemplateArgument> TemplateArgs,
2492 ASTContext &Context) {
2493 ID.AddInteger(TemplateArgs.size());
2494 for (unsigned Arg = 0; Arg != TemplateArgs.size(); ++Arg)
2495 TemplateArgs[Arg].Profile(ID, Context);
2498 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2499 static bool classofKind(Kind K) {
2500 return K >= firstVarTemplateSpecialization &&
2501 K <= lastVarTemplateSpecialization;
2504 friend class ASTDeclReader;
2505 friend class ASTDeclWriter;
2508 class VarTemplatePartialSpecializationDecl
2509 : public VarTemplateSpecializationDecl {
2510 void anchor() override;
2512 /// \brief The list of template parameters
2513 TemplateParameterList *TemplateParams;
2515 /// \brief The source info for the template arguments as written.
2516 /// FIXME: redundant with TypeAsWritten?
2517 const ASTTemplateArgumentListInfo *ArgsAsWritten;
2519 /// \brief The variable template partial specialization from which this
2520 /// variable template partial specialization was instantiated.
2522 /// The boolean value will be true to indicate that this variable template
2523 /// partial specialization was specialized at this level.
2524 llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool>
2525 InstantiatedFromMember;
2527 VarTemplatePartialSpecializationDecl(
2528 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2529 SourceLocation IdLoc, TemplateParameterList *Params,
2530 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
2531 StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
2532 const ASTTemplateArgumentListInfo *ArgInfos);
2534 VarTemplatePartialSpecializationDecl(ASTContext &Context)
2535 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context),
2536 TemplateParams(nullptr), ArgsAsWritten(nullptr),
2537 InstantiatedFromMember(nullptr, false) {}
2540 static VarTemplatePartialSpecializationDecl *
2541 Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2542 SourceLocation IdLoc, TemplateParameterList *Params,
2543 VarTemplateDecl *SpecializedTemplate, QualType T,
2544 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
2545 unsigned NumArgs, const TemplateArgumentListInfo &ArgInfos);
2547 static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C,
2550 VarTemplatePartialSpecializationDecl *getMostRecentDecl() {
2551 return cast<VarTemplatePartialSpecializationDecl>(
2552 static_cast<VarTemplateSpecializationDecl *>(
2553 this)->getMostRecentDecl());
2556 /// Get the list of template parameters
2557 TemplateParameterList *getTemplateParameters() const {
2558 return TemplateParams;
2561 /// Get the template arguments as written.
2562 const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const {
2563 return ArgsAsWritten;
2566 /// \brief Retrieve the member variable template partial specialization from
2567 /// which this particular variable template partial specialization was
2571 /// template<typename T>
2573 /// template<typename U> U Inner;
2574 /// template<typename U> U* Inner<U*> = (U*)(0); // #1
2577 /// template int* Outer<float>::Inner<int*>;
2580 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
2581 /// end up instantiating the partial specialization
2582 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the
2583 /// variable template partial specialization \c Outer<T>::Inner<U*>. Given
2584 /// \c Outer<float>::Inner<U*>, this function would return
2585 /// \c Outer<T>::Inner<U*>.
2586 VarTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
2587 VarTemplatePartialSpecializationDecl *First =
2588 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2589 return First->InstantiatedFromMember.getPointer();
2593 setInstantiatedFromMember(VarTemplatePartialSpecializationDecl *PartialSpec) {
2594 VarTemplatePartialSpecializationDecl *First =
2595 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2596 First->InstantiatedFromMember.setPointer(PartialSpec);
2599 /// \brief Determines whether this variable template partial specialization
2600 /// was a specialization of a member partial specialization.
2602 /// In the following example, the member template partial specialization
2603 /// \c X<int>::Inner<T*> is a member specialization.
2606 /// template<typename T>
2608 /// template<typename U> U Inner;
2609 /// template<typename U> U* Inner<U*> = (U*)(0);
2612 /// template<> template<typename T>
2613 /// U* X<int>::Inner<T*> = (T*)(0) + 1;
2615 bool isMemberSpecialization() {
2616 VarTemplatePartialSpecializationDecl *First =
2617 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2618 return First->InstantiatedFromMember.getInt();
2621 /// \brief Note that this member template is a specialization.
2622 void setMemberSpecialization() {
2623 VarTemplatePartialSpecializationDecl *First =
2624 cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2625 assert(First->InstantiatedFromMember.getPointer() &&
2626 "Only member templates can be member template specializations");
2627 return First->InstantiatedFromMember.setInt(true);
2630 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2631 static bool classofKind(Kind K) {
2632 return K == VarTemplatePartialSpecialization;
2635 friend class ASTDeclReader;
2636 friend class ASTDeclWriter;
2639 /// Declaration of a variable template.
2640 class VarTemplateDecl : public RedeclarableTemplateDecl {
2641 static void DeallocateCommon(void *Ptr);
2644 /// \brief Data that is common to all of the declarations of a given
2645 /// variable template.
2646 struct Common : CommonBase {
2647 Common() : LazySpecializations() {}
2649 /// \brief The variable template specializations for this variable
2650 /// template, including explicit specializations and instantiations.
2651 llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations;
2653 /// \brief The variable template partial specializations for this variable
2655 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>
2656 PartialSpecializations;
2658 /// \brief If non-null, points to an array of specializations (including
2659 /// partial specializations) known ownly by their external declaration IDs.
2661 /// The first value in the array is the number of of specializations/
2662 /// partial specializations that follow.
2663 uint32_t *LazySpecializations;
2666 /// \brief Retrieve the set of specializations of this variable template.
2667 llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
2668 getSpecializations() const;
2670 /// \brief Retrieve the set of partial specializations of this class
2672 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
2673 getPartialSpecializations();
2675 VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L,
2676 DeclarationName Name, TemplateParameterList *Params,
2678 : RedeclarableTemplateDecl(VarTemplate, C, DC, L, Name, Params, Decl) {}
2680 CommonBase *newCommon(ASTContext &C) const override;
2682 Common *getCommonPtr() const {
2683 return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2687 /// \brief Load any lazily-loaded specializations from the external source.
2688 void LoadLazySpecializations() const;
2690 /// \brief Get the underlying variable declarations of the template.
2691 VarDecl *getTemplatedDecl() const {
2692 return static_cast<VarDecl *>(TemplatedDecl);
2695 /// \brief Returns whether this template declaration defines the primary
2696 /// variable pattern.
2697 bool isThisDeclarationADefinition() const {
2698 return getTemplatedDecl()->isThisDeclarationADefinition();
2701 VarTemplateDecl *getDefinition();
2703 /// \brief Create a variable template node.
2704 static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC,
2705 SourceLocation L, DeclarationName Name,
2706 TemplateParameterList *Params,
2709 /// \brief Create an empty variable template node.
2710 static VarTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2712 /// \brief Return the specialization with the provided arguments if it exists,
2713 /// otherwise return the insertion point.
2714 VarTemplateSpecializationDecl *
2715 findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2717 /// \brief Insert the specified specialization knowing that it is not already
2718 /// in. InsertPos must be obtained from findSpecialization.
2719 void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos);
2721 VarTemplateDecl *getCanonicalDecl() override {
2722 return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2724 const VarTemplateDecl *getCanonicalDecl() const {
2725 return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2728 /// \brief Retrieve the previous declaration of this variable template, or
2729 /// NULL if no such declaration exists.
2730 VarTemplateDecl *getPreviousDecl() {
2731 return cast_or_null<VarTemplateDecl>(
2732 static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2735 /// \brief Retrieve the previous declaration of this variable template, or
2736 /// NULL if no such declaration exists.
2737 const VarTemplateDecl *getPreviousDecl() const {
2738 return cast_or_null<VarTemplateDecl>(
2739 static_cast<const RedeclarableTemplateDecl *>(
2740 this)->getPreviousDecl());
2743 VarTemplateDecl *getMostRecentDecl() {
2744 return cast<VarTemplateDecl>(
2745 static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
2747 const VarTemplateDecl *getMostRecentDecl() const {
2748 return const_cast<VarTemplateDecl *>(this)->getMostRecentDecl();
2751 VarTemplateDecl *getInstantiatedFromMemberTemplate() {
2752 return cast_or_null<VarTemplateDecl>(
2753 RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
2756 /// \brief Return the partial specialization with the provided arguments if it
2757 /// exists, otherwise return the insertion point.
2758 VarTemplatePartialSpecializationDecl *
2759 findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2761 /// \brief Insert the specified partial specialization knowing that it is not
2762 /// already in. InsertPos must be obtained from findPartialSpecialization.
2763 void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D,
2766 /// \brief Retrieve the partial specializations as an ordered list.
2767 void getPartialSpecializations(
2768 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS);
2770 /// \brief Find a variable template partial specialization which was
2772 /// from the given member partial specialization.
2774 /// \param D a member variable template partial specialization.
2776 /// \returns the variable template partial specialization which was
2778 /// from the given member partial specialization, or NULL if no such partial
2779 /// specialization exists.
2780 VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember(
2781 VarTemplatePartialSpecializationDecl *D);
2783 typedef SpecIterator<VarTemplateSpecializationDecl> spec_iterator;
2784 typedef llvm::iterator_range<spec_iterator> spec_range;
2786 spec_range specializations() const {
2787 return spec_range(spec_begin(), spec_end());
2790 spec_iterator spec_begin() const {
2791 return makeSpecIterator(getSpecializations(), false);
2794 spec_iterator spec_end() const {
2795 return makeSpecIterator(getSpecializations(), true);
2798 // Implement isa/cast/dyncast support
2799 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2800 static bool classofKind(Kind K) { return K == VarTemplate; }
2802 friend class ASTDeclReader;
2803 friend class ASTDeclWriter;
2806 } /* end of namespace clang */