]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/AST/DeclTemplate.h
Update clang to r86025.
[FreeBSD/FreeBSD.git] / include / clang / AST / DeclTemplate.h
1 //===-- DeclTemplate.h - Classes for representing C++ templates -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the C++ template declaration subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_DECLTEMPLATE_H
15 #define LLVM_CLANG_AST_DECLTEMPLATE_H
16
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/TemplateBase.h"
19 #include "llvm/ADT/PointerUnion.h"
20 #include <limits>
21
22 namespace clang {
23
24 class TemplateParameterList;
25 class TemplateDecl;
26 class FunctionTemplateDecl;
27 class ClassTemplateDecl;
28 class ClassTemplatePartialSpecializationDecl;
29 class TemplateTypeParmDecl;
30 class NonTypeTemplateParmDecl;
31 class TemplateTemplateParmDecl;
32
33 /// \brief Stores a template parameter of any kind.
34 typedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*,
35                             TemplateTemplateParmDecl*> TemplateParameter;
36
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;
42
43   /// The locations of the '<' and '>' angle brackets.
44   SourceLocation LAngleLoc, RAngleLoc;
45
46   /// The number of template parameters in this template
47   /// parameter list.
48   unsigned NumParams;
49
50   TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc,
51                         NamedDecl **Params, unsigned NumParams,
52                         SourceLocation RAngleLoc);
53
54 public:
55   static TemplateParameterList *Create(ASTContext &C,
56                                        SourceLocation TemplateLoc,
57                                        SourceLocation LAngleLoc,
58                                        NamedDecl **Params,
59                                        unsigned NumParams,
60                                        SourceLocation RAngleLoc);
61
62   /// iterator - Iterates through the template parameters in this list.
63   typedef NamedDecl** iterator;
64
65   /// const_iterator - Iterates through the template parameters in this list.
66   typedef NamedDecl* const* const_iterator;
67
68   iterator begin() { return reinterpret_cast<NamedDecl **>(this + 1); }
69   const_iterator begin() const {
70     return reinterpret_cast<NamedDecl * const *>(this + 1);
71   }
72   iterator end() { return begin() + NumParams; }
73   const_iterator end() const { return begin() + NumParams; }
74
75   unsigned size() const { return NumParams; }
76
77   NamedDecl* getParam(unsigned Idx) {
78     assert(Idx < size() && "Template parameter index out-of-range");
79     return begin()[Idx];
80   }
81
82   const NamedDecl* getParam(unsigned Idx) const {
83     assert(Idx < size() && "Template parameter index out-of-range");
84     return begin()[Idx];
85   }
86
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;
92
93   /// \brief Get the depth of this template parameter list in the set of
94   /// template parameter lists.
95   ///
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;
99   
100   SourceLocation getTemplateLoc() const { return TemplateLoc; }
101   SourceLocation getLAngleLoc() const { return LAngleLoc; }
102   SourceLocation getRAngleLoc() const { return RAngleLoc; }
103
104   SourceRange getSourceRange() const {
105     return SourceRange(TemplateLoc, RAngleLoc);
106   }
107 };
108
109 /// \brief A helper class for making template argument lists.
110 class TemplateArgumentListBuilder {
111   TemplateArgument *StructuredArgs;
112   unsigned MaxStructuredArgs;
113   unsigned NumStructuredArgs;
114
115   TemplateArgument *FlatArgs;
116   unsigned MaxFlatArgs;
117   unsigned NumFlatArgs;
118
119   bool AddingToPack;
120   unsigned PackBeginIndex;
121
122 public:
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) { }
129
130   void Append(const TemplateArgument& Arg);
131   void BeginPack();
132   void EndPack();
133
134   void ReleaseArgs();
135
136   unsigned flatSize() const {
137     return NumFlatArgs;
138   }
139   const TemplateArgument *getFlatArguments() const {
140     return FlatArgs;
141   }
142
143   unsigned structuredSize() const {
144     // If we don't have any structured args, just reuse the flat size.
145     if (!StructuredArgs)
146       return flatSize();
147
148     return NumStructuredArgs;
149   }
150   const TemplateArgument *getStructuredArguments() const {
151     // If we don't have any structured args, just reuse the flat args.
152     if (!StructuredArgs)
153       return getFlatArguments();
154
155     return StructuredArgs;
156   }
157 };
158
159 /// \brief A template argument list.
160 ///
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.
166   ///
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;
170
171   /// \brief The number of template arguments in this template
172   /// argument list.
173   unsigned NumFlatArguments;
174
175   llvm::PointerIntPair<const TemplateArgument *, 1> StructuredArguments;
176   unsigned NumStructuredArguments;
177
178 public:
179   TemplateArgumentList(ASTContext &Context,
180                        TemplateArgumentListBuilder &Builder,
181                        bool TakeArgs);
182
183   /// \brief Produces a shallow copy of the given template argument list
184   TemplateArgumentList(const TemplateArgumentList &Other);
185   
186   ~TemplateArgumentList();
187
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];
192   }
193
194   /// \brief Retrieve the template argument at a given index.
195   const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); }
196
197   /// \brief Retrieve the number of template arguments in this
198   /// template argument list.
199   unsigned size() const { return NumFlatArguments; }
200
201   /// \brief Retrieve the number of template arguments in the
202   /// flattened template argument list.
203   unsigned flat_size() const { return NumFlatArguments; }
204
205   /// \brief Retrieve the flattened template argument list.
206   const TemplateArgument *getFlatArgumentList() const {
207     return FlatArguments.getPointer();
208   }
209 };
210
211 //===----------------------------------------------------------------------===//
212 // Kinds of Templates
213 //===----------------------------------------------------------------------===//
214
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 {
220 protected:
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) { }
225
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) { }
231
232   // Construct a template decl with name, parameters, and templated element.
233   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
234                DeclarationName Name, TemplateParameterList *Params,
235                NamedDecl *Decl)
236     : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl),
237       TemplateParams(Params) { }
238 public:
239   ~TemplateDecl();
240
241   /// Get the list of template parameters
242   TemplateParameterList *getTemplateParameters() const {
243     return TemplateParams;
244   }
245
246   /// Get the underlying, templated declaration.
247   NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
248
249   // Implement isa/cast/dyncast/etc.
250   static bool classof(const Decl *D) {
251       return D->getKind() >= TemplateFirst && D->getKind() <= TemplateLast;
252   }
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; }
257
258 protected:
259   NamedDecl *TemplatedDecl;
260   TemplateParameterList* TemplateParams;
261 };
262
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 {
267 public:
268   /// \brief The function template specialization that this structure
269   /// describes.
270   FunctionDecl *Function;
271
272   /// \brief The function template from which this function template
273   /// specialization was generated.
274   ///
275   /// The two bits are contain the top 4 values of TemplateSpecializationKind.
276   llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
277
278   /// \brief The template arguments used to produce the function template
279   /// specialization from the function template.
280   const TemplateArgumentList *TemplateArguments;
281
282   /// \brief The point at which this function template specialization was
283   /// first instantiated. 
284   SourceLocation PointOfInstantiation;
285   
286   /// \brief Retrieve the template from which this function was specialized.
287   FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
288
289   /// \brief Determine what kind of template specialization this is.
290   TemplateSpecializationKind getTemplateSpecializationKind() const {
291     return (TemplateSpecializationKind)(Template.getInt() + 1);
292   }
293
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);
299   }
300
301   /// \brief Retrieve the first point of instantiation of this function
302   /// template specialization.
303   ///
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; 
308   }
309   
310   /// \brief Set the (first) point of instantiation of this function template
311   /// specialization.
312   void setPointOfInstantiation(SourceLocation POI) {
313     PointOfInstantiation = POI;
314   }
315   
316   void Profile(llvm::FoldingSetNodeID &ID) {
317     Profile(ID, TemplateArguments->getFlatArgumentList(),
318             TemplateArguments->flat_size(),
319             Function->getASTContext());
320   }
321
322   static void
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);
328   }
329 };
330
331 /// \brief Provides information a specialization of a member of a class 
332 /// template, which may be a member function, static data member, or
333 /// member class.
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;
338   
339   // The point at which this member was first instantiated.
340   SourceLocation PointOfInstantiation;
341   
342 public:
343   explicit 
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");
348   }
349   
350   /// \brief Retrieve the member declaration from which this member was
351   /// instantiated.
352   NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
353   
354   /// \brief Determine what kind of template specialization this is.
355   TemplateSpecializationKind getTemplateSpecializationKind() const {
356     return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
357   }
358   
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);
364   }
365   
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; 
371   }
372   
373   /// \brief Set the first point of instantiation.
374   void setPointOfInstantiation(SourceLocation POI) {
375     PointOfInstantiation = POI;
376   }
377 };
378   
379 /// Declaration of a template function.
380 class FunctionTemplateDecl : public TemplateDecl {
381 protected:
382   /// \brief Data that is common to all of the declarations of a given
383   /// function template.
384   struct Common {
385     Common() : InstantiatedFromMember(0, false) { }
386
387     /// \brief The function template specializations for this function
388     /// template, including explicit specializations and instantiations.
389     llvm::FoldingSet<FunctionTemplateSpecializationInfo> Specializations;
390
391     /// \brief The member function template from which this was most
392     /// directly instantiated (or null).
393     ///
394     /// The boolean value indicates whether this member function template
395     /// was explicitly specialized.
396     llvm::PointerIntPair<FunctionTemplateDecl*, 1, bool> InstantiatedFromMember;
397   };
398
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
401   /// template.
402   llvm::PointerUnion<Common*, FunctionTemplateDecl*> CommonOrPrev;
403
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();
408
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) { }
413
414 public:
415   void Destroy(ASTContext &C);
416
417   /// Get the underlying function declaration of the template.
418   FunctionDecl *getTemplatedDecl() const {
419     return static_cast<FunctionDecl*>(TemplatedDecl);
420   }
421
422   /// \brief Retrieve the set of function template specializations of this
423   /// function template.
424   llvm::FoldingSet<FunctionTemplateSpecializationInfo> &getSpecializations() {
425     return getCommonPtr()->Specializations;
426   }
427
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*>();
432   }
433
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*>();
438   }
439
440   /// \brief Set the previous declaration of this function template.
441   void setPreviousDeclaration(FunctionTemplateDecl *Prev) {
442     if (Prev)
443       CommonOrPrev = Prev;
444   }
445
446   virtual FunctionTemplateDecl *getCanonicalDecl();
447
448   /// \brief Retrieve the member function template that this function template
449   /// was instantiated from.
450   ///
451   /// This routine will return non-NULL for member function templates of
452   /// class templates.  For example, given:
453   ///
454   /// \code
455   /// template <typename T>
456   /// struct X {
457   ///   template <typename U> void f();
458   /// };
459   /// \endcode
460   ///
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).
467   ///
468   /// \returns NULL if this is not an instantiation of a member function
469   /// template.
470   FunctionTemplateDecl *getInstantiatedFromMemberTemplate() {
471     return getCommonPtr()->InstantiatedFromMember.getPointer();
472   }
473
474   void setInstantiatedFromMemberTemplate(FunctionTemplateDecl *FTD) {
475     assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
476     getCommonPtr()->InstantiatedFromMember.setPointer(FTD);
477   }
478
479   /// \brief Determines whether this template was a specialization of a 
480   /// member template.
481   ///
482   /// In the following example, the function template \c X<int>::f is a 
483   /// member specialization.
484   ///
485   /// \code
486   /// template<typename T>
487   /// struct X {
488   ///   template<typename U> void f(T, U);
489   /// };
490   ///
491   /// template<> template<typename T>
492   /// void X<int>::f(int, T);
493   /// \endcode
494   bool isMemberSpecialization() {
495     return getCommonPtr()->InstantiatedFromMember.getInt();
496   }
497   
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);
503   }
504   
505   /// Create a template function node.
506   static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
507                                       SourceLocation L,
508                                       DeclarationName Name,
509                                       TemplateParameterList *Params,
510                                       NamedDecl *Decl);
511
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)
516   { return true; }
517 };
518
519 //===----------------------------------------------------------------------===//
520 // Kinds of Template Parameters
521 //===----------------------------------------------------------------------===//
522
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 {
531 protected:
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"); */ }
536
537   TemplateParmPosition(unsigned D, unsigned P)
538     : Depth(D), Position(P)
539   { }
540
541   // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
542   // position? Maybe?
543   unsigned Depth;
544   unsigned Position;
545
546 public:
547   /// Get the nesting depth of the template parameter.
548   unsigned getDepth() const { return Depth; }
549
550   /// Get the position of the template parameter within its parameter list.
551   unsigned getPosition() const { return Position; }
552
553   /// Get the index of the template parameter within its parameter list.
554   unsigned getIndex() const { return Position; }
555 };
556
557 /// TemplateTypeParmDecl - Declaration of a template type parameter,
558 /// e.g., "T" in
559 /// @code
560 /// template<typename T> class vector;
561 /// @endcode
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
565   /// 'class' keyword.
566   bool Typename : 1;
567
568   /// \brief Whether this template type parameter inherited its
569   /// default argument.
570   bool InheritedDefault : 1;
571
572   /// \brief Whether this is a parameter pack.
573   bool ParameterPack : 1;
574
575   /// \brief The default template argument, if any.
576   DeclaratorInfo *DefaultArgument;
577
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();
583   }
584
585 public:
586   static TemplateTypeParmDecl *Create(ASTContext &C, DeclContext *DC,
587                                       SourceLocation L, unsigned D, unsigned P,
588                                       IdentifierInfo *Id, bool Typename,
589                                       bool ParameterPack);
590
591   /// \brief Whether this template type parameter was declared with
592   /// the 'typename' keyword. If not, it was declared with the 'class'
593   /// keyword.
594   bool wasDeclaredWithTypename() const { return Typename; }
595
596   /// \brief Determine whether this template parameter has a default
597   /// argument.
598   bool hasDefaultArgument() const { return DefaultArgument != 0; }
599
600   /// \brief Retrieve the default argument, if any.
601   QualType getDefaultArgument() const { return DefaultArgument->getType(); }
602
603   /// \brief Retrieves the default argument's source information, if any.
604   DeclaratorInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
605
606   /// \brief Retrieves the location of the default argument declaration.
607   SourceLocation getDefaultArgumentLoc() const;
608
609   /// \brief Determines whether the default argument was inherited
610   /// from a previous declaration of this template.
611   bool defaultArgumentWasInherited() const { return InheritedDefault; }
612
613   /// \brief Set the default argument for this template parameter, and
614   /// whether that default argument was inherited from another
615   /// declaration.
616   void setDefaultArgument(DeclaratorInfo *DefArg, bool Inherited) {
617     DefaultArgument = DefArg;
618     InheritedDefault = Inherited;
619   }
620
621   /// \brief Removes the default argument of this template parameter.
622   void removeDefaultArgument() {
623     DefaultArgument = 0;
624     InheritedDefault = false;
625   }
626
627   /// \brief Retrieve the depth of the template parameter.
628   unsigned getDepth() const;
629   
630   /// \brief Retrieve the index of the template parameter.
631   unsigned getIndex() const;
632
633   /// \brief Returns whether this is a parameter pack.
634   bool isParameterPack() const { return ParameterPack; }
635
636   // Implement isa/cast/dyncast/etc.
637   static bool classof(const Decl *D) {
638     return D->getKind() == TemplateTypeParm;
639   }
640   static bool classof(const TemplateTypeParmDecl *D) { return true; }
641 };
642
643 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
644 /// e.g., "Size" in
645 /// @code
646 /// template<int Size> class array { };
647 /// @endcode
648 class NonTypeTemplateParmDecl
649   : public VarDecl, protected TemplateParmPosition {
650   /// \brief The default template argument, if any.
651   Expr *DefaultArgument;
652
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)
658   { }
659
660 public:
661   static NonTypeTemplateParmDecl *
662   Create(ASTContext &C, DeclContext *DC, SourceLocation L, unsigned D,
663          unsigned P, IdentifierInfo *Id, QualType T, DeclaratorInfo *DInfo);
664
665   using TemplateParmPosition::getDepth;
666   using TemplateParmPosition::getPosition;
667   using TemplateParmPosition::getIndex;
668
669   /// \brief Determine whether this template parameter has a default
670   /// argument.
671   bool hasDefaultArgument() const { return DefaultArgument; }
672
673   /// \brief Retrieve the default argument, if any.
674   Expr *getDefaultArgument() const { return DefaultArgument; }
675
676   /// \brief Retrieve the location of the default argument, if any.
677   SourceLocation getDefaultArgumentLoc() const;
678
679   /// \brief Set the default argument for this template parameter.
680   void setDefaultArgument(Expr *DefArg) {
681     DefaultArgument = DefArg;
682   }
683
684   // Implement isa/cast/dyncast/etc.
685   static bool classof(const Decl *D) {
686     return D->getKind() == NonTypeTemplateParm;
687   }
688   static bool classof(const NonTypeTemplateParmDecl *D) { return true; }
689 };
690
691 /// TemplateTemplateParmDecl - Declares a template template parameter,
692 /// e.g., "T" in
693 /// @code
694 /// template <template <typename> class T> class container { };
695 /// @endcode
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 {
700
701   /// \brief The default template argument, if any.
702   Expr *DefaultArgument;
703
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)
709     { }
710
711 public:
712   static TemplateTemplateParmDecl *Create(ASTContext &C, DeclContext *DC,
713                                           SourceLocation L, unsigned D,
714                                           unsigned P, IdentifierInfo *Id,
715                                           TemplateParameterList *Params);
716
717   using TemplateParmPosition::getDepth;
718   using TemplateParmPosition::getPosition;
719   using TemplateParmPosition::getIndex;
720
721   /// \brief Determine whether this template parameter has a default
722   /// argument.
723   bool hasDefaultArgument() const { return DefaultArgument; }
724
725   /// \brief Retrieve the default argument, if any.
726   Expr *getDefaultArgument() const { return DefaultArgument; }
727
728   /// \brief Retrieve the location of the default argument, if any.
729   SourceLocation getDefaultArgumentLoc() const;
730
731   /// \brief Set the default argument for this template parameter.
732   void setDefaultArgument(Expr *DefArg) {
733     DefaultArgument = DefArg;
734   }
735
736   // Implement isa/cast/dyncast/etc.
737   static bool classof(const Decl *D) {
738     return D->getKind() == TemplateTemplateParm;
739   }
740   static bool classof(const TemplateTemplateParmDecl *D) { return true; }
741 };
742
743 /// \brief Represents a class template specialization, which refers to
744 /// a class template with a given set of template arguments.
745 ///
746 /// Class template specializations represent both explicit
747 /// specialization of class templates, as in the example below, and
748 /// implicit instantiations of class templates.
749 ///
750 /// \code
751 /// template<typename T> class array;
752 ///
753 /// template<>
754 /// class array<bool> { }; // class template specialization array<bool>
755 /// \endcode
756 class ClassTemplateSpecializationDecl
757   : public CXXRecordDecl, public llvm::FoldingSetNode {
758
759   /// \brief Structure that stores information about a class template
760   /// specialization that was instantiated from a class template partial
761   /// specialization.
762   struct SpecializedPartialSpecialization {
763     /// \brief The class template partial specialization from which this
764     /// class template specialization was instantiated.
765     ClassTemplatePartialSpecializationDecl *PartialSpecialization;
766
767     /// \brief The template argument list deduced for the class template
768     /// partial specialization itself.
769     TemplateArgumentList *TemplateArgs;
770   };
771
772   /// \brief The template that this specialization specializes
773   llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
774     SpecializedTemplate;
775
776   /// \brief The template arguments used to describe this specialization.
777   TemplateArgumentList TemplateArgs;
778
779   /// \brief The point where this template was instantiated (if any)
780   SourceLocation PointOfInstantiation;
781
782   /// \brief The kind of specialization this declaration refers to.
783   /// Really a value of type TemplateSpecializationKind.
784   unsigned SpecializationKind : 3;
785
786 protected:
787   ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK,
788                                   DeclContext *DC, SourceLocation L,
789                                   ClassTemplateDecl *SpecializedTemplate,
790                                   TemplateArgumentListBuilder &Builder,
791                                   ClassTemplateSpecializationDecl *PrevDecl);
792
793 public:
794   static ClassTemplateSpecializationDecl *
795   Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
796          ClassTemplateDecl *SpecializedTemplate,
797          TemplateArgumentListBuilder &Builder,
798          ClassTemplateSpecializationDecl *PrevDecl);
799
800   virtual void Destroy(ASTContext& C);
801
802   virtual void getNameForDiagnostic(std::string &S,
803                                     const PrintingPolicy &Policy,
804                                     bool Qualified) const;
805
806   /// \brief Retrieve the template that this specialization specializes.
807   ClassTemplateDecl *getSpecializedTemplate() const;
808
809   /// \brief Retrieve the template arguments of the class template
810   /// specialization.
811   const TemplateArgumentList &getTemplateArgs() const {
812     return TemplateArgs;
813   }
814
815   /// \brief Determine the kind of specialization that this
816   /// declaration represents.
817   TemplateSpecializationKind getSpecializationKind() const {
818     return static_cast<TemplateSpecializationKind>(SpecializationKind);
819   }
820
821   void setSpecializationKind(TemplateSpecializationKind TSK) {
822     SpecializationKind = TSK;
823   }
824
825   /// \brief Get the point of instantiation (if any), or null if none.
826   SourceLocation getPointOfInstantiation() const {
827     return PointOfInstantiation;
828   }
829
830   void setPointOfInstantiation(SourceLocation Loc) {
831     assert(Loc.isValid() && "point of instantiation must be valid!");
832     PointOfInstantiation = Loc;
833   }
834
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;
846
847     if (SpecializedPartialSpecialization *PartialSpec
848           = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
849       return PartialSpec->PartialSpecialization;
850
851     return const_cast<ClassTemplateDecl*>(
852                              SpecializedTemplate.get<ClassTemplateDecl*>());
853   }
854
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
858   /// instantiated.
859   ///
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
865   /// itself.
866   const TemplateArgumentList &getTemplateInstantiationArgs() const {
867     if (SpecializedPartialSpecialization *PartialSpec
868         = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
869       return *PartialSpec->TemplateArgs;
870
871     return getTemplateArgs();
872   }
873
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;
884   }
885
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();
890   }
891
892   void Profile(llvm::FoldingSetNodeID &ID) const {
893     Profile(ID, TemplateArgs.getFlatArgumentList(), TemplateArgs.flat_size(),
894             getASTContext());
895   }
896
897   static void
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);
903   }
904
905   static bool classof(const Decl *D) {
906     return D->getKind() == ClassTemplateSpecialization ||
907            D->getKind() == ClassTemplatePartialSpecialization;
908   }
909
910   static bool classof(const ClassTemplateSpecializationDecl *) {
911     return true;
912   }
913
914   static bool classof(const ClassTemplatePartialSpecializationDecl *) {
915     return true;
916   }
917 };
918
919 class ClassTemplatePartialSpecializationDecl
920   : public ClassTemplateSpecializationDecl {
921   /// \brief The list of template parameters
922   TemplateParameterList* TemplateParams;
923
924   /// \brief The source info for the template arguments as written.
925   TemplateArgumentLoc *ArgsAsWritten;
926   unsigned NumArgsAsWritten;
927
928   /// \brief The class template partial specialization from which this 
929   /// class template partial specialization was instantiated.
930   ///
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;
935     
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,
947                                       PrevDecl),
948       TemplateParams(Params), ArgsAsWritten(ArgInfos),
949       NumArgsAsWritten(NumArgInfos), InstantiatedFromMember(0, false) { }
950
951 public:
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);
960
961   /// Get the list of template parameters
962   TemplateParameterList *getTemplateParameters() const {
963     return TemplateParams;
964   }
965
966   /// Get the template arguments as written.
967   TemplateArgumentLoc *getTemplateArgsAsWritten() const {
968     return ArgsAsWritten;
969   }
970
971   /// Get the number of template arguments as written.
972   unsigned getNumTemplateArgsAsWritten() const {
973     return NumArgsAsWritten;
974   }
975
976   /// \brief Retrieve the member class template partial specialization from
977   /// which this particular class template partial specialization was
978   /// instantiated.
979   ///
980   /// \code
981   /// template<typename T>
982   /// struct Outer {
983   ///   template<typename U> struct Inner;
984   ///   template<typename U> struct Inner<U*> { }; // #1
985   /// };
986   ///
987   /// Outer<float>::Inner<int*> ii;
988   /// \endcode
989   ///
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();
1000   }
1001   
1002   void setInstantiatedFromMember(
1003                           ClassTemplatePartialSpecializationDecl *PartialSpec) {
1004     ClassTemplatePartialSpecializationDecl *First
1005       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1006     First->InstantiatedFromMember.setPointer(PartialSpec);
1007   }
1008     
1009   /// \brief Determines whether this class template partial specialization 
1010   /// template was a specialization of a member partial specialization.
1011   ///
1012   /// In the following example, the member template partial specialization
1013   /// \c X<int>::Inner<T*> is a member specialization.
1014   ///
1015   /// \code
1016   /// template<typename T>
1017   /// struct X {
1018   ///   template<typename U> struct Inner;
1019   ///   template<typename U> struct Inner<U*>;
1020   /// };
1021   ///
1022   /// template<> template<typename T>
1023   /// struct X<int>::Inner<T*> { /* ... */ };
1024   /// \endcode
1025   bool isMemberSpecialization() {
1026     ClassTemplatePartialSpecializationDecl *First
1027       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1028     return First->InstantiatedFromMember.getInt();
1029   }
1030   
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);
1038   }
1039     
1040   // FIXME: Add Profile support!
1041
1042   static bool classof(const Decl *D) {
1043     return D->getKind() == ClassTemplatePartialSpecialization;
1044   }
1045
1046   static bool classof(const ClassTemplatePartialSpecializationDecl *) {
1047     return true;
1048   }
1049 };
1050
1051 /// Declaration of a class template.
1052 class ClassTemplateDecl : public TemplateDecl {
1053 protected:
1054   /// \brief Data that is common to all of the declarations of a given
1055   /// class template.
1056   struct Common {
1057     Common() : InstantiatedFromMember(0, 0) {}
1058
1059     /// \brief The class template specializations for this class
1060     /// template, including explicit specializations and instantiations.
1061     llvm::FoldingSet<ClassTemplateSpecializationDecl> Specializations;
1062
1063     /// \brief The class template partial specializations for this class
1064     /// template.
1065     llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>
1066       PartialSpecializations;
1067
1068     /// \brief The injected-class-name type for this class template.
1069     QualType InjectedClassNameType;
1070
1071     /// \brief The templated member class from which this was most
1072     /// directly instantiated (or null).
1073     ///
1074     /// The boolean value indicates whether this member class template
1075     /// was explicitly specialized.
1076     llvm::PointerIntPair<ClassTemplateDecl *, 1, bool> InstantiatedFromMember;
1077   };
1078
1079   // FIXME: Combine PreviousDeclaration with CommonPtr, as in 
1080   // FunctionTemplateDecl.
1081   
1082   /// \brief Previous declaration of this class template.
1083   ClassTemplateDecl *PreviousDeclaration;
1084
1085   /// \brief Pointer to the data that is common to all of the
1086   /// declarations of this class template.
1087   ///
1088   /// The first declaration of a class template (e.g., the declaration
1089   /// with no "previous declaration") owns this pointer.
1090   Common *CommonPtr;
1091
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) { }
1097
1098   ~ClassTemplateDecl();
1099
1100 public:
1101   /// Get the underlying class declarations of the template.
1102   CXXRecordDecl *getTemplatedDecl() const {
1103     return static_cast<CXXRecordDecl *>(TemplatedDecl);
1104   }
1105
1106   /// \brief Retrieve the previous declaration of this template.
1107   ClassTemplateDecl *getPreviousDeclaration() const {
1108     return PreviousDeclaration;
1109   }
1110
1111   virtual ClassTemplateDecl *getCanonicalDecl();
1112
1113   /// Create a class template node.
1114   static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
1115                                    SourceLocation L,
1116                                    DeclarationName Name,
1117                                    TemplateParameterList *Params,
1118                                    NamedDecl *Decl,
1119                                    ClassTemplateDecl *PrevDecl);
1120
1121   /// \brief Retrieve the set of specializations of this class template.
1122   llvm::FoldingSet<ClassTemplateSpecializationDecl> &getSpecializations() {
1123     return CommonPtr->Specializations;
1124   }
1125
1126   /// \brief Retrieve the set of partial specializations of this class
1127   /// template.
1128   llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &
1129   getPartialSpecializations() {
1130     return CommonPtr->PartialSpecializations;
1131   }
1132
1133   /// \brief Find a class template partial specialization with the given
1134   /// type T.
1135   ///
1136   /// \brief A dependent type that names a specialization of this class
1137   /// template.
1138   ///
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);
1142
1143   /// \brief Retrieve the type of the injected-class-name for this
1144   /// class template.
1145   ///
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:
1150   ///
1151   /// \code
1152   /// template<typename T, int N>
1153   /// struct array {
1154   ///   typedef array this_type; // "array" is equivalent to "array<T, N>"
1155   /// };
1156   /// \endcode
1157   QualType getInjectedClassNameType(ASTContext &Context);
1158
1159   /// \brief Retrieve the member class template that this class template was
1160   /// derived from.
1161   ///
1162   /// This routine will return non-NULL for templated member classes of
1163   /// class templates.  For example, given:
1164   ///
1165   /// \code
1166   /// template <typename T>
1167   /// struct X {
1168   ///   template <typename U> struct A {};
1169   /// };
1170   /// \endcode
1171   ///
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).
1177   ///
1178   /// \returns null if this is not an instantiation of a member class template.
1179   ClassTemplateDecl *getInstantiatedFromMemberTemplate() const {
1180     return CommonPtr->InstantiatedFromMember.getPointer();
1181   }
1182
1183   void setInstantiatedFromMemberTemplate(ClassTemplateDecl *CTD) {
1184     assert(!CommonPtr->InstantiatedFromMember.getPointer());
1185     CommonPtr->InstantiatedFromMember.setPointer(CTD);
1186   }
1187
1188   /// \brief Determines whether this template was a specialization of a 
1189   /// member template.
1190   ///
1191   /// In the following example, the member template \c X<int>::Inner is a 
1192   /// member specialization.
1193   ///
1194   /// \code
1195   /// template<typename T>
1196   /// struct X {
1197   ///   template<typename U> struct Inner;
1198   /// };
1199   ///
1200   /// template<> template<typename T>
1201   /// struct X<int>::Inner { /* ... */ };
1202   /// \endcode
1203   bool isMemberSpecialization() {
1204     return CommonPtr->InstantiatedFromMember.getInt();
1205   }
1206   
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);
1212   }
1213   
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)
1218   { return true; }
1219
1220   virtual void Destroy(ASTContext& C);
1221 };
1222
1223 /// Declaration of a friend template.  For example:
1224 ///
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 {
1230 public:
1231   typedef llvm::PointerUnion<NamedDecl*,Type*> FriendUnion;
1232
1233 private:
1234   // The number of template parameters;  always non-zero.
1235   unsigned NumParams;
1236
1237   // The parameter list.
1238   TemplateParameterList **Params;
1239
1240   // The declaration that's a friend of this class.
1241   FriendUnion Friend;
1242
1243   // Location of the 'friend' specifier.
1244   SourceLocation FriendLoc;
1245
1246
1247   FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
1248                      unsigned NParams, 
1249                      TemplateParameterList **Params,
1250                      FriendUnion Friend,
1251                      SourceLocation FriendLoc)
1252     : Decl(Decl::FriendTemplate, DC, Loc),
1253       NumParams(NParams),
1254       Params(Params),
1255       Friend(Friend),
1256       FriendLoc(FriendLoc)
1257   {}
1258
1259 public:
1260   static FriendTemplateDecl *Create(ASTContext &Context,
1261                                     DeclContext *DC, SourceLocation Loc,
1262                                     unsigned NParams, 
1263                                     TemplateParameterList **Params,
1264                                     FriendUnion Friend,
1265                                     SourceLocation FriendLoc);
1266
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*>();
1272   }
1273
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*>();
1279   }
1280
1281   /// Retrieves the location of the 'friend' keyword.
1282   SourceLocation getFriendLoc() const {
1283     return FriendLoc;
1284   }
1285
1286   TemplateParameterList *getTemplateParameterList(unsigned i) const {
1287     assert(i <= NumParams);
1288     return Params[i];
1289   }
1290
1291   unsigned getNumTemplateParameters() const {
1292     return NumParams;
1293   }
1294
1295   // Implement isa/cast/dyncast/etc.
1296   static bool classof(const Decl *D) {
1297     return D->getKind() == Decl::FriendTemplate;
1298   }
1299   static bool classof(const FriendTemplateDecl *D) { return true; }
1300 };
1301
1302 /// Implementation of inline functions that require the template declarations
1303 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
1304   : Function(FTD) { }
1305
1306 } /* end of namespace clang */
1307
1308 #endif