1 //===-- DeclTemplate.h - Classes for representing C++ templates -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the C++ template declaration subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLTEMPLATE_H
15 #define LLVM_CLANG_AST_DECLTEMPLATE_H
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/TemplateBase.h"
19 #include "llvm/ADT/PointerUnion.h"
24 class TemplateParameterList;
26 class FunctionTemplateDecl;
27 class ClassTemplateDecl;
28 class ClassTemplatePartialSpecializationDecl;
29 class TemplateTypeParmDecl;
30 class NonTypeTemplateParmDecl;
31 class TemplateTemplateParmDecl;
33 /// \brief Stores a template parameter of any kind.
34 typedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*,
35 TemplateTemplateParmDecl*> TemplateParameter;
37 /// TemplateParameterList - Stores a list of template parameters for a
38 /// TemplateDecl and its derived classes.
39 class TemplateParameterList {
40 /// The location of the 'template' keyword.
41 SourceLocation TemplateLoc;
43 /// The locations of the '<' and '>' angle brackets.
44 SourceLocation LAngleLoc, RAngleLoc;
46 /// The number of template parameters in this template
50 TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc,
51 NamedDecl **Params, unsigned NumParams,
52 SourceLocation RAngleLoc);
55 static TemplateParameterList *Create(ASTContext &C,
56 SourceLocation TemplateLoc,
57 SourceLocation LAngleLoc,
60 SourceLocation RAngleLoc);
62 /// iterator - Iterates through the template parameters in this list.
63 typedef NamedDecl** iterator;
65 /// const_iterator - Iterates through the template parameters in this list.
66 typedef NamedDecl* const* const_iterator;
68 iterator begin() { return reinterpret_cast<NamedDecl **>(this + 1); }
69 const_iterator begin() const {
70 return reinterpret_cast<NamedDecl * const *>(this + 1);
72 iterator end() { return begin() + NumParams; }
73 const_iterator end() const { return begin() + NumParams; }
75 unsigned size() const { return NumParams; }
77 NamedDecl* getParam(unsigned Idx) {
78 assert(Idx < size() && "Template parameter index out-of-range");
82 const NamedDecl* getParam(unsigned Idx) const {
83 assert(Idx < size() && "Template parameter index out-of-range");
87 /// \btief Returns the minimum number of arguments needed to form a
88 /// template specialization. This may be fewer than the number of
89 /// template parameters, if some of the parameters have default
90 /// arguments or if there is a parameter pack.
91 unsigned getMinRequiredArguments() const;
93 /// \brief Get the depth of this template parameter list in the set of
94 /// template parameter lists.
96 /// The first template parameter list in a declaration will have depth 0,
97 /// the second template parameter list will have depth 1, etc.
98 unsigned getDepth() const;
100 SourceLocation getTemplateLoc() const { return TemplateLoc; }
101 SourceLocation getLAngleLoc() const { return LAngleLoc; }
102 SourceLocation getRAngleLoc() const { return RAngleLoc; }
104 SourceRange getSourceRange() const {
105 return SourceRange(TemplateLoc, RAngleLoc);
109 /// \brief A helper class for making template argument lists.
110 class TemplateArgumentListBuilder {
111 TemplateArgument *StructuredArgs;
112 unsigned MaxStructuredArgs;
113 unsigned NumStructuredArgs;
115 TemplateArgument *FlatArgs;
116 unsigned MaxFlatArgs;
117 unsigned NumFlatArgs;
120 unsigned PackBeginIndex;
123 TemplateArgumentListBuilder(const TemplateParameterList *Parameters,
124 unsigned NumTemplateArgs)
125 : StructuredArgs(0), MaxStructuredArgs(Parameters->size()),
126 NumStructuredArgs(0), FlatArgs(0),
127 MaxFlatArgs(std::max(MaxStructuredArgs, NumTemplateArgs)), NumFlatArgs(0),
128 AddingToPack(false), PackBeginIndex(0) { }
130 void Append(const TemplateArgument& Arg);
136 unsigned flatSize() const {
139 const TemplateArgument *getFlatArguments() const {
143 unsigned structuredSize() const {
144 // If we don't have any structured args, just reuse the flat size.
148 return NumStructuredArgs;
150 const TemplateArgument *getStructuredArguments() const {
151 // If we don't have any structured args, just reuse the flat args.
153 return getFlatArguments();
155 return StructuredArgs;
159 /// \brief A template argument list.
161 /// FIXME: In the future, this class will be extended to support
162 /// variadic templates and member templates, which will make some of
163 /// the function names below make more sense.
164 class TemplateArgumentList {
165 /// \brief The template argument list.
167 /// The integer value will be non-zero to indicate that this
168 /// template argument list does not own the pointer.
169 llvm::PointerIntPair<const TemplateArgument *, 1> FlatArguments;
171 /// \brief The number of template arguments in this template
173 unsigned NumFlatArguments;
175 llvm::PointerIntPair<const TemplateArgument *, 1> StructuredArguments;
176 unsigned NumStructuredArguments;
179 TemplateArgumentList(ASTContext &Context,
180 TemplateArgumentListBuilder &Builder,
183 /// \brief Produces a shallow copy of the given template argument list
184 TemplateArgumentList(const TemplateArgumentList &Other);
186 ~TemplateArgumentList();
188 /// \brief Retrieve the template argument at a given index.
189 const TemplateArgument &get(unsigned Idx) const {
190 assert(Idx < NumFlatArguments && "Invalid template argument index");
191 return getFlatArgumentList()[Idx];
194 /// \brief Retrieve the template argument at a given index.
195 const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); }
197 /// \brief Retrieve the number of template arguments in this
198 /// template argument list.
199 unsigned size() const { return NumFlatArguments; }
201 /// \brief Retrieve the number of template arguments in the
202 /// flattened template argument list.
203 unsigned flat_size() const { return NumFlatArguments; }
205 /// \brief Retrieve the flattened template argument list.
206 const TemplateArgument *getFlatArgumentList() const {
207 return FlatArguments.getPointer();
211 //===----------------------------------------------------------------------===//
212 // Kinds of Templates
213 //===----------------------------------------------------------------------===//
215 /// TemplateDecl - The base class of all kinds of template declarations (e.g.,
216 /// class, function, etc.). The TemplateDecl class stores the list of template
217 /// parameters and a reference to the templated scoped declaration: the
218 /// underlying AST node.
219 class TemplateDecl : public NamedDecl {
221 // This is probably never used.
222 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
223 DeclarationName Name)
224 : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(0) { }
226 // Construct a template decl with the given name and parameters.
227 // Used when there is not templated element (tt-params, alias?).
228 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
229 DeclarationName Name, TemplateParameterList *Params)
230 : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(Params) { }
232 // Construct a template decl with name, parameters, and templated element.
233 TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
234 DeclarationName Name, TemplateParameterList *Params,
236 : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl),
237 TemplateParams(Params) { }
241 /// Get the list of template parameters
242 TemplateParameterList *getTemplateParameters() const {
243 return TemplateParams;
246 /// Get the underlying, templated declaration.
247 NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
249 // Implement isa/cast/dyncast/etc.
250 static bool classof(const Decl *D) {
251 return D->getKind() >= TemplateFirst && D->getKind() <= TemplateLast;
253 static bool classof(const TemplateDecl *D) { return true; }
254 static bool classof(const FunctionTemplateDecl *D) { return true; }
255 static bool classof(const ClassTemplateDecl *D) { return true; }
256 static bool classof(const TemplateTemplateParmDecl *D) { return true; }
259 NamedDecl *TemplatedDecl;
260 TemplateParameterList* TemplateParams;
263 /// \brief Provides information about a function template specialization,
264 /// which is a FunctionDecl that has been explicitly specialization or
265 /// instantiated from a function template.
266 class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode {
268 /// \brief The function template specialization that this structure
270 FunctionDecl *Function;
272 /// \brief The function template from which this function template
273 /// specialization was generated.
275 /// The two bits are contain the top 4 values of TemplateSpecializationKind.
276 llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
278 /// \brief The template arguments used to produce the function template
279 /// specialization from the function template.
280 const TemplateArgumentList *TemplateArguments;
282 /// \brief The point at which this function template specialization was
283 /// first instantiated.
284 SourceLocation PointOfInstantiation;
286 /// \brief Retrieve the template from which this function was specialized.
287 FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
289 /// \brief Determine what kind of template specialization this is.
290 TemplateSpecializationKind getTemplateSpecializationKind() const {
291 return (TemplateSpecializationKind)(Template.getInt() + 1);
294 /// \brief Set the template specialization kind.
295 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
296 assert(TSK != TSK_Undeclared &&
297 "Cannot encode TSK_Undeclared for a function template specialization");
298 Template.setInt(TSK - 1);
301 /// \brief Retrieve the first point of instantiation of this function
302 /// template specialization.
304 /// The point of instantiation may be an invalid source location if this
305 /// function has yet to be instantiated.
306 SourceLocation getPointOfInstantiation() const {
307 return PointOfInstantiation;
310 /// \brief Set the (first) point of instantiation of this function template
312 void setPointOfInstantiation(SourceLocation POI) {
313 PointOfInstantiation = POI;
316 void Profile(llvm::FoldingSetNodeID &ID) {
317 Profile(ID, TemplateArguments->getFlatArgumentList(),
318 TemplateArguments->flat_size(),
319 Function->getASTContext());
323 Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
324 unsigned NumTemplateArgs, ASTContext &Context) {
325 ID.AddInteger(NumTemplateArgs);
326 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
327 TemplateArgs[Arg].Profile(ID, Context);
331 /// \brief Provides information a specialization of a member of a class
332 /// template, which may be a member function, static data member, or
334 class MemberSpecializationInfo {
335 // The member declaration from which this member was instantiated, and the
336 // manner in which the instantiation occurred (in the lower two bits).
337 llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
339 // The point at which this member was first instantiated.
340 SourceLocation PointOfInstantiation;
344 MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK)
345 : MemberAndTSK(IF, TSK - 1), PointOfInstantiation() {
346 assert(TSK != TSK_Undeclared &&
347 "Cannot encode undeclared template specializations for members");
350 /// \brief Retrieve the member declaration from which this member was
352 NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
354 /// \brief Determine what kind of template specialization this is.
355 TemplateSpecializationKind getTemplateSpecializationKind() const {
356 return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
359 /// \brief Set the template specialization kind.
360 void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
361 assert(TSK != TSK_Undeclared &&
362 "Cannot encode undeclared template specializations for members");
363 MemberAndTSK.setInt(TSK - 1);
366 /// \brief Retrieve the first point of instantiation of this member.
367 /// If the point of instantiation is an invalid location, then this member
368 /// has not yet been instantiated.
369 SourceLocation getPointOfInstantiation() const {
370 return PointOfInstantiation;
373 /// \brief Set the first point of instantiation.
374 void setPointOfInstantiation(SourceLocation POI) {
375 PointOfInstantiation = POI;
379 /// Declaration of a template function.
380 class FunctionTemplateDecl : public TemplateDecl {
382 /// \brief Data that is common to all of the declarations of a given
383 /// function template.
385 Common() : InstantiatedFromMember(0, false) { }
387 /// \brief The function template specializations for this function
388 /// template, including explicit specializations and instantiations.
389 llvm::FoldingSet<FunctionTemplateSpecializationInfo> Specializations;
391 /// \brief The member function template from which this was most
392 /// directly instantiated (or null).
394 /// The boolean value indicates whether this member function template
395 /// was explicitly specialized.
396 llvm::PointerIntPair<FunctionTemplateDecl*, 1, bool> InstantiatedFromMember;
399 /// \brief A pointer to the previous declaration (if this is a redeclaration)
400 /// or to the data that is common to all declarations of this function
402 llvm::PointerUnion<Common*, FunctionTemplateDecl*> CommonOrPrev;
404 /// \brief Retrieves the "common" pointer shared by all
405 /// (re-)declarations of the same function template. Calling this routine
406 /// may implicitly allocate memory for the common pointer.
407 Common *getCommonPtr();
409 FunctionTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
410 TemplateParameterList *Params, NamedDecl *Decl)
411 : TemplateDecl(FunctionTemplate, DC, L, Name, Params, Decl),
412 CommonOrPrev((Common*)0) { }
415 void Destroy(ASTContext &C);
417 /// Get the underlying function declaration of the template.
418 FunctionDecl *getTemplatedDecl() const {
419 return static_cast<FunctionDecl*>(TemplatedDecl);
422 /// \brief Retrieve the set of function template specializations of this
423 /// function template.
424 llvm::FoldingSet<FunctionTemplateSpecializationInfo> &getSpecializations() {
425 return getCommonPtr()->Specializations;
428 /// \brief Retrieve the previous declaration of this function template, or
429 /// NULL if no such declaration exists.
430 const FunctionTemplateDecl *getPreviousDeclaration() const {
431 return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>();
434 /// \brief Retrieve the previous declaration of this function template, or
435 /// NULL if no such declaration exists.
436 FunctionTemplateDecl *getPreviousDeclaration() {
437 return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>();
440 /// \brief Set the previous declaration of this function template.
441 void setPreviousDeclaration(FunctionTemplateDecl *Prev) {
446 virtual FunctionTemplateDecl *getCanonicalDecl();
448 /// \brief Retrieve the member function template that this function template
449 /// was instantiated from.
451 /// This routine will return non-NULL for member function templates of
452 /// class templates. For example, given:
455 /// template <typename T>
457 /// template <typename U> void f();
461 /// X<int>::A<float> is a CXXMethodDecl (whose parent is X<int>, a
462 /// ClassTemplateSpecializationDecl) for which getPrimaryTemplate() will
463 /// return X<int>::f, a FunctionTemplateDecl (whose parent is again
464 /// X<int>) for which getInstantiatedFromMemberTemplate() will return
465 /// X<T>::f, a FunctionTemplateDecl (whose parent is X<T>, a
466 /// ClassTemplateDecl).
468 /// \returns NULL if this is not an instantiation of a member function
470 FunctionTemplateDecl *getInstantiatedFromMemberTemplate() {
471 return getCommonPtr()->InstantiatedFromMember.getPointer();
474 void setInstantiatedFromMemberTemplate(FunctionTemplateDecl *FTD) {
475 assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
476 getCommonPtr()->InstantiatedFromMember.setPointer(FTD);
479 /// \brief Determines whether this template was a specialization of a
482 /// In the following example, the function template \c X<int>::f is a
483 /// member specialization.
486 /// template<typename T>
488 /// template<typename U> void f(T, U);
491 /// template<> template<typename T>
492 /// void X<int>::f(int, T);
494 bool isMemberSpecialization() {
495 return getCommonPtr()->InstantiatedFromMember.getInt();
498 /// \brief Note that this member template is a specialization.
499 void setMemberSpecialization() {
500 assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
501 "Only member templates can be member template specializations");
502 getCommonPtr()->InstantiatedFromMember.setInt(true);
505 /// Create a template function node.
506 static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
508 DeclarationName Name,
509 TemplateParameterList *Params,
512 // Implement isa/cast/dyncast support
513 static bool classof(const Decl *D)
514 { return D->getKind() == FunctionTemplate; }
515 static bool classof(const FunctionTemplateDecl *D)
519 //===----------------------------------------------------------------------===//
520 // Kinds of Template Parameters
521 //===----------------------------------------------------------------------===//
523 /// The TemplateParmPosition class defines the position of a template parameter
524 /// within a template parameter list. Because template parameter can be listed
525 /// sequentially for out-of-line template members, each template parameter is
526 /// given a Depth - the nesting of template parameter scopes - and a Position -
527 /// the occurrence within the parameter list.
528 /// This class is inheritedly privately by different kinds of template
529 /// parameters and is not part of the Decl hierarchy. Just a facility.
530 class TemplateParmPosition {
532 // FIXME: This should probably never be called, but it's here as
533 TemplateParmPosition()
534 : Depth(0), Position(0)
535 { /* assert(0 && "Cannot create positionless template parameter"); */ }
537 TemplateParmPosition(unsigned D, unsigned P)
538 : Depth(D), Position(P)
541 // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
547 /// Get the nesting depth of the template parameter.
548 unsigned getDepth() const { return Depth; }
550 /// Get the position of the template parameter within its parameter list.
551 unsigned getPosition() const { return Position; }
553 /// Get the index of the template parameter within its parameter list.
554 unsigned getIndex() const { return Position; }
557 /// TemplateTypeParmDecl - Declaration of a template type parameter,
560 /// template<typename T> class vector;
562 class TemplateTypeParmDecl : public TypeDecl {
563 /// \brief Whether this template type parameter was declaration with
564 /// the 'typename' keyword. If false, it was declared with the
568 /// \brief Whether this template type parameter inherited its
569 /// default argument.
570 bool InheritedDefault : 1;
572 /// \brief Whether this is a parameter pack.
573 bool ParameterPack : 1;
575 /// \brief The default template argument, if any.
576 DeclaratorInfo *DefaultArgument;
578 TemplateTypeParmDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
579 bool Typename, QualType Type, bool ParameterPack)
580 : TypeDecl(TemplateTypeParm, DC, L, Id), Typename(Typename),
581 InheritedDefault(false), ParameterPack(ParameterPack), DefaultArgument() {
582 TypeForDecl = Type.getTypePtr();
586 static TemplateTypeParmDecl *Create(ASTContext &C, DeclContext *DC,
587 SourceLocation L, unsigned D, unsigned P,
588 IdentifierInfo *Id, bool Typename,
591 /// \brief Whether this template type parameter was declared with
592 /// the 'typename' keyword. If not, it was declared with the 'class'
594 bool wasDeclaredWithTypename() const { return Typename; }
596 /// \brief Determine whether this template parameter has a default
598 bool hasDefaultArgument() const { return DefaultArgument != 0; }
600 /// \brief Retrieve the default argument, if any.
601 QualType getDefaultArgument() const { return DefaultArgument->getType(); }
603 /// \brief Retrieves the default argument's source information, if any.
604 DeclaratorInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
606 /// \brief Retrieves the location of the default argument declaration.
607 SourceLocation getDefaultArgumentLoc() const;
609 /// \brief Determines whether the default argument was inherited
610 /// from a previous declaration of this template.
611 bool defaultArgumentWasInherited() const { return InheritedDefault; }
613 /// \brief Set the default argument for this template parameter, and
614 /// whether that default argument was inherited from another
616 void setDefaultArgument(DeclaratorInfo *DefArg, bool Inherited) {
617 DefaultArgument = DefArg;
618 InheritedDefault = Inherited;
621 /// \brief Removes the default argument of this template parameter.
622 void removeDefaultArgument() {
624 InheritedDefault = false;
627 /// \brief Retrieve the depth of the template parameter.
628 unsigned getDepth() const;
630 /// \brief Retrieve the index of the template parameter.
631 unsigned getIndex() const;
633 /// \brief Returns whether this is a parameter pack.
634 bool isParameterPack() const { return ParameterPack; }
636 // Implement isa/cast/dyncast/etc.
637 static bool classof(const Decl *D) {
638 return D->getKind() == TemplateTypeParm;
640 static bool classof(const TemplateTypeParmDecl *D) { return true; }
643 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
646 /// template<int Size> class array { };
648 class NonTypeTemplateParmDecl
649 : public VarDecl, protected TemplateParmPosition {
650 /// \brief The default template argument, if any.
651 Expr *DefaultArgument;
653 NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation L, unsigned D,
654 unsigned P, IdentifierInfo *Id, QualType T,
655 DeclaratorInfo *DInfo)
656 : VarDecl(NonTypeTemplateParm, DC, L, Id, T, DInfo, VarDecl::None),
657 TemplateParmPosition(D, P), DefaultArgument(0)
661 static NonTypeTemplateParmDecl *
662 Create(ASTContext &C, DeclContext *DC, SourceLocation L, unsigned D,
663 unsigned P, IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo);
665 using TemplateParmPosition::getDepth;
666 using TemplateParmPosition::getPosition;
667 using TemplateParmPosition::getIndex;
669 /// \brief Determine whether this template parameter has a default
671 bool hasDefaultArgument() const { return DefaultArgument; }
673 /// \brief Retrieve the default argument, if any.
674 Expr *getDefaultArgument() const { return DefaultArgument; }
676 /// \brief Retrieve the location of the default argument, if any.
677 SourceLocation getDefaultArgumentLoc() const;
679 /// \brief Set the default argument for this template parameter.
680 void setDefaultArgument(Expr *DefArg) {
681 DefaultArgument = DefArg;
684 // Implement isa/cast/dyncast/etc.
685 static bool classof(const Decl *D) {
686 return D->getKind() == NonTypeTemplateParm;
688 static bool classof(const NonTypeTemplateParmDecl *D) { return true; }
691 /// TemplateTemplateParmDecl - Declares a template template parameter,
694 /// template <template <typename> class T> class container { };
696 /// A template template parameter is a TemplateDecl because it defines the
697 /// name of a template and the template parameters allowable for substitution.
698 class TemplateTemplateParmDecl
699 : public TemplateDecl, protected TemplateParmPosition {
701 /// \brief The default template argument, if any.
702 Expr *DefaultArgument;
704 TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
705 unsigned D, unsigned P,
706 IdentifierInfo *Id, TemplateParameterList *Params)
707 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
708 TemplateParmPosition(D, P), DefaultArgument(0)
712 static TemplateTemplateParmDecl *Create(ASTContext &C, DeclContext *DC,
713 SourceLocation L, unsigned D,
714 unsigned P, IdentifierInfo *Id,
715 TemplateParameterList *Params);
717 using TemplateParmPosition::getDepth;
718 using TemplateParmPosition::getPosition;
719 using TemplateParmPosition::getIndex;
721 /// \brief Determine whether this template parameter has a default
723 bool hasDefaultArgument() const { return DefaultArgument; }
725 /// \brief Retrieve the default argument, if any.
726 Expr *getDefaultArgument() const { return DefaultArgument; }
728 /// \brief Retrieve the location of the default argument, if any.
729 SourceLocation getDefaultArgumentLoc() const;
731 /// \brief Set the default argument for this template parameter.
732 void setDefaultArgument(Expr *DefArg) {
733 DefaultArgument = DefArg;
736 // Implement isa/cast/dyncast/etc.
737 static bool classof(const Decl *D) {
738 return D->getKind() == TemplateTemplateParm;
740 static bool classof(const TemplateTemplateParmDecl *D) { return true; }
743 /// \brief Represents a class template specialization, which refers to
744 /// a class template with a given set of template arguments.
746 /// Class template specializations represent both explicit
747 /// specialization of class templates, as in the example below, and
748 /// implicit instantiations of class templates.
751 /// template<typename T> class array;
754 /// class array<bool> { }; // class template specialization array<bool>
756 class ClassTemplateSpecializationDecl
757 : public CXXRecordDecl, public llvm::FoldingSetNode {
759 /// \brief Structure that stores information about a class template
760 /// specialization that was instantiated from a class template partial
762 struct SpecializedPartialSpecialization {
763 /// \brief The class template partial specialization from which this
764 /// class template specialization was instantiated.
765 ClassTemplatePartialSpecializationDecl *PartialSpecialization;
767 /// \brief The template argument list deduced for the class template
768 /// partial specialization itself.
769 TemplateArgumentList *TemplateArgs;
772 /// \brief The template that this specialization specializes
773 llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
776 /// \brief The template arguments used to describe this specialization.
777 TemplateArgumentList TemplateArgs;
779 /// \brief The point where this template was instantiated (if any)
780 SourceLocation PointOfInstantiation;
782 /// \brief The kind of specialization this declaration refers to.
783 /// Really a value of type TemplateSpecializationKind.
784 unsigned SpecializationKind : 3;
787 ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK,
788 DeclContext *DC, SourceLocation L,
789 ClassTemplateDecl *SpecializedTemplate,
790 TemplateArgumentListBuilder &Builder,
791 ClassTemplateSpecializationDecl *PrevDecl);
794 static ClassTemplateSpecializationDecl *
795 Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
796 ClassTemplateDecl *SpecializedTemplate,
797 TemplateArgumentListBuilder &Builder,
798 ClassTemplateSpecializationDecl *PrevDecl);
800 virtual void Destroy(ASTContext& C);
802 virtual void getNameForDiagnostic(std::string &S,
803 const PrintingPolicy &Policy,
804 bool Qualified) const;
806 /// \brief Retrieve the template that this specialization specializes.
807 ClassTemplateDecl *getSpecializedTemplate() const;
809 /// \brief Retrieve the template arguments of the class template
811 const TemplateArgumentList &getTemplateArgs() const {
815 /// \brief Determine the kind of specialization that this
816 /// declaration represents.
817 TemplateSpecializationKind getSpecializationKind() const {
818 return static_cast<TemplateSpecializationKind>(SpecializationKind);
821 void setSpecializationKind(TemplateSpecializationKind TSK) {
822 SpecializationKind = TSK;
825 /// \brief Get the point of instantiation (if any), or null if none.
826 SourceLocation getPointOfInstantiation() const {
827 return PointOfInstantiation;
830 void setPointOfInstantiation(SourceLocation Loc) {
831 assert(Loc.isValid() && "point of instantiation must be valid!");
832 PointOfInstantiation = Loc;
835 /// \brief If this class template specialization is an instantiation of
836 /// a template (rather than an explicit specialization), return the
837 /// class template or class template partial specialization from which it
838 /// was instantiated.
839 llvm::PointerUnion<ClassTemplateDecl *,
840 ClassTemplatePartialSpecializationDecl *>
841 getInstantiatedFrom() const {
842 if (getSpecializationKind() != TSK_ImplicitInstantiation &&
843 getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
844 getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
845 return (ClassTemplateDecl*)0;
847 if (SpecializedPartialSpecialization *PartialSpec
848 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
849 return PartialSpec->PartialSpecialization;
851 return const_cast<ClassTemplateDecl*>(
852 SpecializedTemplate.get<ClassTemplateDecl*>());
855 /// \brief Retrieve the set of template arguments that should be used
856 /// to instantiate members of the class template or class template partial
857 /// specialization from which this class template specialization was
860 /// \returns For a class template specialization instantiated from the primary
861 /// template, this function will return the same template arguments as
862 /// getTemplateArgs(). For a class template specialization instantiated from
863 /// a class template partial specialization, this function will return the
864 /// deduced template arguments for the class template partial specialization
866 const TemplateArgumentList &getTemplateInstantiationArgs() const {
867 if (SpecializedPartialSpecialization *PartialSpec
868 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
869 return *PartialSpec->TemplateArgs;
871 return getTemplateArgs();
874 /// \brief Note that this class template specialization is actually an
875 /// instantiation of the given class template partial specialization whose
876 /// template arguments have been deduced.
877 void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
878 TemplateArgumentList *TemplateArgs) {
879 SpecializedPartialSpecialization *PS
880 = new (getASTContext()) SpecializedPartialSpecialization();
881 PS->PartialSpecialization = PartialSpec;
882 PS->TemplateArgs = TemplateArgs;
883 SpecializedTemplate = PS;
886 /// \brief Sets the type of this specialization as it was written by
887 /// the user. This will be a class template specialization type.
888 void setTypeAsWritten(QualType T) {
889 TypeForDecl = T.getTypePtr();
892 void Profile(llvm::FoldingSetNodeID &ID) const {
893 Profile(ID, TemplateArgs.getFlatArgumentList(), TemplateArgs.flat_size(),
898 Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
899 unsigned NumTemplateArgs, ASTContext &Context) {
900 ID.AddInteger(NumTemplateArgs);
901 for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
902 TemplateArgs[Arg].Profile(ID, Context);
905 static bool classof(const Decl *D) {
906 return D->getKind() == ClassTemplateSpecialization ||
907 D->getKind() == ClassTemplatePartialSpecialization;
910 static bool classof(const ClassTemplateSpecializationDecl *) {
914 static bool classof(const ClassTemplatePartialSpecializationDecl *) {
919 class ClassTemplatePartialSpecializationDecl
920 : public ClassTemplateSpecializationDecl {
921 /// \brief The list of template parameters
922 TemplateParameterList* TemplateParams;
924 /// \brief The source info for the template arguments as written.
925 TemplateArgumentLoc *ArgsAsWritten;
926 unsigned NumArgsAsWritten;
928 /// \brief The class template partial specialization from which this
929 /// class template partial specialization was instantiated.
931 /// The boolean value will be true to indicate that this class template
932 /// partial specialization was specialized at this level.
933 llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
934 InstantiatedFromMember;
936 ClassTemplatePartialSpecializationDecl(ASTContext &Context,
937 DeclContext *DC, SourceLocation L,
938 TemplateParameterList *Params,
939 ClassTemplateDecl *SpecializedTemplate,
940 TemplateArgumentListBuilder &Builder,
941 TemplateArgumentLoc *ArgInfos,
942 unsigned NumArgInfos,
943 ClassTemplatePartialSpecializationDecl *PrevDecl)
944 : ClassTemplateSpecializationDecl(Context,
945 ClassTemplatePartialSpecialization,
946 DC, L, SpecializedTemplate, Builder,
948 TemplateParams(Params), ArgsAsWritten(ArgInfos),
949 NumArgsAsWritten(NumArgInfos), InstantiatedFromMember(0, false) { }
952 static ClassTemplatePartialSpecializationDecl *
953 Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
954 TemplateParameterList *Params,
955 ClassTemplateDecl *SpecializedTemplate,
956 TemplateArgumentListBuilder &Builder,
957 TemplateArgumentLoc *ArgInfos,
958 unsigned NumArgInfos,
959 ClassTemplatePartialSpecializationDecl *PrevDecl);
961 /// Get the list of template parameters
962 TemplateParameterList *getTemplateParameters() const {
963 return TemplateParams;
966 /// Get the template arguments as written.
967 TemplateArgumentLoc *getTemplateArgsAsWritten() const {
968 return ArgsAsWritten;
971 /// Get the number of template arguments as written.
972 unsigned getNumTemplateArgsAsWritten() const {
973 return NumArgsAsWritten;
976 /// \brief Retrieve the member class template partial specialization from
977 /// which this particular class template partial specialization was
981 /// template<typename T>
983 /// template<typename U> struct Inner;
984 /// template<typename U> struct Inner<U*> { }; // #1
987 /// Outer<float>::Inner<int*> ii;
990 /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
991 /// end up instantiating the partial specialization
992 /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
993 /// template partial specialization \c Outer<T>::Inner<U*>. Given
994 /// \c Outer<float>::Inner<U*>, this function would return
995 /// \c Outer<T>::Inner<U*>.
996 ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
997 ClassTemplatePartialSpecializationDecl *First
998 = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
999 return First->InstantiatedFromMember.getPointer();
1002 void setInstantiatedFromMember(
1003 ClassTemplatePartialSpecializationDecl *PartialSpec) {
1004 ClassTemplatePartialSpecializationDecl *First
1005 = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1006 First->InstantiatedFromMember.setPointer(PartialSpec);
1009 /// \brief Determines whether this class template partial specialization
1010 /// template was a specialization of a member partial specialization.
1012 /// In the following example, the member template partial specialization
1013 /// \c X<int>::Inner<T*> is a member specialization.
1016 /// template<typename T>
1018 /// template<typename U> struct Inner;
1019 /// template<typename U> struct Inner<U*>;
1022 /// template<> template<typename T>
1023 /// struct X<int>::Inner<T*> { /* ... */ };
1025 bool isMemberSpecialization() {
1026 ClassTemplatePartialSpecializationDecl *First
1027 = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1028 return First->InstantiatedFromMember.getInt();
1031 /// \brief Note that this member template is a specialization.
1032 void setMemberSpecialization() {
1033 ClassTemplatePartialSpecializationDecl *First
1034 = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1035 assert(First->InstantiatedFromMember.getPointer() &&
1036 "Only member templates can be member template specializations");
1037 return First->InstantiatedFromMember.setInt(true);
1040 // FIXME: Add Profile support!
1042 static bool classof(const Decl *D) {
1043 return D->getKind() == ClassTemplatePartialSpecialization;
1046 static bool classof(const ClassTemplatePartialSpecializationDecl *) {
1051 /// Declaration of a class template.
1052 class ClassTemplateDecl : public TemplateDecl {
1054 /// \brief Data that is common to all of the declarations of a given
1057 Common() : InstantiatedFromMember(0, 0) {}
1059 /// \brief The class template specializations for this class
1060 /// template, including explicit specializations and instantiations.
1061 llvm::FoldingSet<ClassTemplateSpecializationDecl> Specializations;
1063 /// \brief The class template partial specializations for this class
1065 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>
1066 PartialSpecializations;
1068 /// \brief The injected-class-name type for this class template.
1069 QualType InjectedClassNameType;
1071 /// \brief The templated member class from which this was most
1072 /// directly instantiated (or null).
1074 /// The boolean value indicates whether this member class template
1075 /// was explicitly specialized.
1076 llvm::PointerIntPair<ClassTemplateDecl *, 1, bool> InstantiatedFromMember;
1079 // FIXME: Combine PreviousDeclaration with CommonPtr, as in
1080 // FunctionTemplateDecl.
1082 /// \brief Previous declaration of this class template.
1083 ClassTemplateDecl *PreviousDeclaration;
1085 /// \brief Pointer to the data that is common to all of the
1086 /// declarations of this class template.
1088 /// The first declaration of a class template (e.g., the declaration
1089 /// with no "previous declaration") owns this pointer.
1092 ClassTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
1093 TemplateParameterList *Params, NamedDecl *Decl,
1094 ClassTemplateDecl *PrevDecl, Common *CommonPtr)
1095 : TemplateDecl(ClassTemplate, DC, L, Name, Params, Decl),
1096 PreviousDeclaration(PrevDecl), CommonPtr(CommonPtr) { }
1098 ~ClassTemplateDecl();
1101 /// Get the underlying class declarations of the template.
1102 CXXRecordDecl *getTemplatedDecl() const {
1103 return static_cast<CXXRecordDecl *>(TemplatedDecl);
1106 /// \brief Retrieve the previous declaration of this template.
1107 ClassTemplateDecl *getPreviousDeclaration() const {
1108 return PreviousDeclaration;
1111 virtual ClassTemplateDecl *getCanonicalDecl();
1113 /// Create a class template node.
1114 static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
1116 DeclarationName Name,
1117 TemplateParameterList *Params,
1119 ClassTemplateDecl *PrevDecl);
1121 /// \brief Retrieve the set of specializations of this class template.
1122 llvm::FoldingSet<ClassTemplateSpecializationDecl> &getSpecializations() {
1123 return CommonPtr->Specializations;
1126 /// \brief Retrieve the set of partial specializations of this class
1128 llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &
1129 getPartialSpecializations() {
1130 return CommonPtr->PartialSpecializations;
1133 /// \brief Find a class template partial specialization with the given
1136 /// \brief A dependent type that names a specialization of this class
1139 /// \returns the class template partial specialization that exactly matches
1140 /// the type \p T, or NULL if no such partial specialization exists.
1141 ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
1143 /// \brief Retrieve the type of the injected-class-name for this
1146 /// The injected-class-name for a class template \c X is \c
1147 /// X<template-args>, where \c template-args is formed from the
1148 /// template arguments that correspond to the template parameters of
1149 /// \c X. For example:
1152 /// template<typename T, int N>
1154 /// typedef array this_type; // "array" is equivalent to "array<T, N>"
1157 QualType getInjectedClassNameType(ASTContext &Context);
1159 /// \brief Retrieve the member class template that this class template was
1162 /// This routine will return non-NULL for templated member classes of
1163 /// class templates. For example, given:
1166 /// template <typename T>
1168 /// template <typename U> struct A {};
1172 /// X<int>::A<float> is a ClassTemplateSpecializationDecl (whose parent
1173 /// is X<int>, also a CTSD) for which getSpecializedTemplate() will
1174 /// return X<int>::A<U>, a TemplateClassDecl (whose parent is again
1175 /// X<int>) for which getInstantiatedFromMemberTemplate() will return
1176 /// X<T>::A<U>, a TemplateClassDecl (whose parent is X<T>, also a TCD).
1178 /// \returns null if this is not an instantiation of a member class template.
1179 ClassTemplateDecl *getInstantiatedFromMemberTemplate() const {
1180 return CommonPtr->InstantiatedFromMember.getPointer();
1183 void setInstantiatedFromMemberTemplate(ClassTemplateDecl *CTD) {
1184 assert(!CommonPtr->InstantiatedFromMember.getPointer());
1185 CommonPtr->InstantiatedFromMember.setPointer(CTD);
1188 /// \brief Determines whether this template was a specialization of a
1189 /// member template.
1191 /// In the following example, the member template \c X<int>::Inner is a
1192 /// member specialization.
1195 /// template<typename T>
1197 /// template<typename U> struct Inner;
1200 /// template<> template<typename T>
1201 /// struct X<int>::Inner { /* ... */ };
1203 bool isMemberSpecialization() {
1204 return CommonPtr->InstantiatedFromMember.getInt();
1207 /// \brief Note that this member template is a specialization.
1208 void setMemberSpecialization() {
1209 assert(CommonPtr->InstantiatedFromMember.getPointer() &&
1210 "Only member templates can be member template specializations");
1211 CommonPtr->InstantiatedFromMember.setInt(true);
1214 // Implement isa/cast/dyncast support
1215 static bool classof(const Decl *D)
1216 { return D->getKind() == ClassTemplate; }
1217 static bool classof(const ClassTemplateDecl *D)
1220 virtual void Destroy(ASTContext& C);
1223 /// Declaration of a friend template. For example:
1225 /// template <typename T> class A {
1226 /// friend class MyVector<T>; // not a friend template
1227 /// template <typename U> friend class B; // friend template
1228 /// template <typename U> friend class Foo<T>::Nested; // friend template
1229 class FriendTemplateDecl : public Decl {
1231 typedef llvm::PointerUnion<NamedDecl*,Type*> FriendUnion;
1234 // The number of template parameters; always non-zero.
1237 // The parameter list.
1238 TemplateParameterList **Params;
1240 // The declaration that's a friend of this class.
1243 // Location of the 'friend' specifier.
1244 SourceLocation FriendLoc;
1247 FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
1249 TemplateParameterList **Params,
1251 SourceLocation FriendLoc)
1252 : Decl(Decl::FriendTemplate, DC, Loc),
1256 FriendLoc(FriendLoc)
1260 static FriendTemplateDecl *Create(ASTContext &Context,
1261 DeclContext *DC, SourceLocation Loc,
1263 TemplateParameterList **Params,
1265 SourceLocation FriendLoc);
1267 /// If this friend declaration names a templated type (or
1268 /// a dependent member type of a templated type), return that
1269 /// type; otherwise return null.
1270 Type *getFriendType() const {
1271 return Friend.dyn_cast<Type*>();
1274 /// If this friend declaration names a templated function (or
1275 /// a member function of a templated type), return that type;
1276 /// otherwise return null.
1277 NamedDecl *getFriendDecl() const {
1278 return Friend.dyn_cast<NamedDecl*>();
1281 /// Retrieves the location of the 'friend' keyword.
1282 SourceLocation getFriendLoc() const {
1286 TemplateParameterList *getTemplateParameterList(unsigned i) const {
1287 assert(i <= NumParams);
1291 unsigned getNumTemplateParameters() const {
1295 // Implement isa/cast/dyncast/etc.
1296 static bool classof(const Decl *D) {
1297 return D->getKind() == Decl::FriendTemplate;
1299 static bool classof(const FriendTemplateDecl *D) { return true; }
1302 /// Implementation of inline functions that require the template declarations
1303 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
1306 } /* end of namespace clang */