]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/DeclTemplate.h
MFV hostapd & wpa_supplicant 0.6.10.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / 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   llvm::SmallVector<TemplateArgument, 4> 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   unsigned flatSize() const { return FlatArgs.size(); }
135   const TemplateArgument *getFlatArguments() const { return FlatArgs.data(); }
136
137   unsigned structuredSize() const {
138     // If we don't have any structured args, just reuse the flat size.
139     if (!StructuredArgs)
140       return flatSize();
141
142     return NumStructuredArgs;
143   }
144   const TemplateArgument *getStructuredArguments() const {
145     // If we don't have any structured args, just reuse the flat args.
146     if (!StructuredArgs)
147       return getFlatArguments();
148
149     return StructuredArgs;
150   }
151 };
152
153 /// \brief A template argument list.
154 ///
155 /// FIXME: In the future, this class will be extended to support
156 /// variadic templates and member templates, which will make some of
157 /// the function names below make more sense.
158 class TemplateArgumentList {
159   /// \brief The template argument list.
160   ///
161   /// The integer value will be non-zero to indicate that this
162   /// template argument list does own the pointer.
163   llvm::PointerIntPair<const TemplateArgument *, 1> FlatArguments;
164
165   /// \brief The number of template arguments in this template
166   /// argument list.
167   unsigned NumFlatArguments;
168
169   llvm::PointerIntPair<const TemplateArgument *, 1> StructuredArguments;
170   unsigned NumStructuredArguments;
171
172   TemplateArgumentList(const TemplateArgumentList &Other); // DO NOT IMPL
173   void operator=(const TemplateArgumentList &Other); // DO NOT IMPL
174 public:
175   /// TemplateArgumentList - If this constructor is passed "true" for 'TakeArgs'
176   /// it copies them into a locally new[]'d array.  If passed "false", then it
177   /// just references the array passed in.  This is only safe if the builder
178   /// outlives it, but saves a copy.
179   TemplateArgumentList(ASTContext &Context,
180                        TemplateArgumentListBuilder &Builder,
181                        bool TakeArgs);
182
183   /// Produces a shallow copy of the given template argument list.  This
184   /// assumes that the input argument list outlives it.  This takes the list as
185   /// a pointer to avoid looking like a copy constructor, since this really
186   /// really isn't safe to use that way.
187   explicit TemplateArgumentList(const TemplateArgumentList *Other);
188   
189   /// Used to release the memory associated with a TemplateArgumentList
190   ///  object.  FIXME: This is currently not called anywhere, but the
191   ///  memory will still be freed when using a BumpPtrAllocator.
192   void Destroy(ASTContext &C);
193
194   ~TemplateArgumentList();
195
196   /// \brief Retrieve the template argument at a given index.
197   const TemplateArgument &get(unsigned Idx) const {
198     assert(Idx < NumFlatArguments && "Invalid template argument index");
199     return getFlatArgumentList()[Idx];
200   }
201
202   /// \brief Retrieve the template argument at a given index.
203   const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); }
204
205   /// \brief Retrieve the number of template arguments in this
206   /// template argument list.
207   unsigned size() const { return NumFlatArguments; }
208
209   /// \brief Retrieve the number of template arguments in the
210   /// flattened template argument list.
211   unsigned flat_size() const { return NumFlatArguments; }
212
213   /// \brief Retrieve the flattened template argument list.
214   const TemplateArgument *getFlatArgumentList() const {
215     return FlatArguments.getPointer();
216   }
217 };
218
219 //===----------------------------------------------------------------------===//
220 // Kinds of Templates
221 //===----------------------------------------------------------------------===//
222
223 /// TemplateDecl - The base class of all kinds of template declarations (e.g.,
224 /// class, function, etc.). The TemplateDecl class stores the list of template
225 /// parameters and a reference to the templated scoped declaration: the
226 /// underlying AST node.
227 class TemplateDecl : public NamedDecl {
228 protected:
229   // This is probably never used.
230   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
231                DeclarationName Name)
232     : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(0) { }
233
234   // Construct a template decl with the given name and parameters.
235   // Used when there is not templated element (tt-params, alias?).
236   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
237                DeclarationName Name, TemplateParameterList *Params)
238     : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(Params) { }
239
240   // Construct a template decl with name, parameters, and templated element.
241   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
242                DeclarationName Name, TemplateParameterList *Params,
243                NamedDecl *Decl)
244     : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl),
245       TemplateParams(Params) { }
246 public:
247   ~TemplateDecl();
248
249   /// Get the list of template parameters
250   TemplateParameterList *getTemplateParameters() const {
251     return TemplateParams;
252   }
253
254   /// Get the underlying, templated declaration.
255   NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
256
257   // Implement isa/cast/dyncast/etc.
258   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
259   static bool classof(const TemplateDecl *D) { return true; }
260   static bool classof(const FunctionTemplateDecl *D) { return true; }
261   static bool classof(const ClassTemplateDecl *D) { return true; }
262   static bool classof(const TemplateTemplateParmDecl *D) { return true; }
263   static bool classofKind(Kind K) {
264     return K >= TemplateFirst && K <= TemplateLast;
265   }
266
267 protected:
268   NamedDecl *TemplatedDecl;
269   TemplateParameterList* TemplateParams;
270 };
271
272 /// \brief Provides information about a function template specialization,
273 /// which is a FunctionDecl that has been explicitly specialization or
274 /// instantiated from a function template.
275 class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode {
276 public:
277   /// \brief The function template specialization that this structure
278   /// describes.
279   FunctionDecl *Function;
280
281   /// \brief The function template from which this function template
282   /// specialization was generated.
283   ///
284   /// The two bits are contain the top 4 values of TemplateSpecializationKind.
285   llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
286
287   /// \brief The template arguments used to produce the function template
288   /// specialization from the function template.
289   const TemplateArgumentList *TemplateArguments;
290
291   /// \brief The template arguments as written in the sources, if provided.
292   const TemplateArgumentListInfo *TemplateArgumentsAsWritten;
293
294   /// \brief The point at which this function template specialization was
295   /// first instantiated. 
296   SourceLocation PointOfInstantiation;
297   
298   /// \brief Retrieve the template from which this function was specialized.
299   FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
300
301   /// \brief Determine what kind of template specialization this is.
302   TemplateSpecializationKind getTemplateSpecializationKind() const {
303     return (TemplateSpecializationKind)(Template.getInt() + 1);
304   }
305
306   /// \brief Set the template specialization kind.
307   void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
308     assert(TSK != TSK_Undeclared &&
309          "Cannot encode TSK_Undeclared for a function template specialization");
310     Template.setInt(TSK - 1);
311   }
312
313   /// \brief Retrieve the first point of instantiation of this function
314   /// template specialization.
315   ///
316   /// The point of instantiation may be an invalid source location if this
317   /// function has yet to be instantiated.
318   SourceLocation getPointOfInstantiation() const { 
319     return PointOfInstantiation; 
320   }
321   
322   /// \brief Set the (first) point of instantiation of this function template
323   /// specialization.
324   void setPointOfInstantiation(SourceLocation POI) {
325     PointOfInstantiation = POI;
326   }
327   
328   void Profile(llvm::FoldingSetNodeID &ID) {
329     Profile(ID, TemplateArguments->getFlatArgumentList(),
330             TemplateArguments->flat_size(),
331             Function->getASTContext());
332   }
333
334   static void
335   Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
336           unsigned NumTemplateArgs, ASTContext &Context) {
337     ID.AddInteger(NumTemplateArgs);
338     for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
339       TemplateArgs[Arg].Profile(ID, Context);
340   }
341 };
342
343 /// \brief Provides information a specialization of a member of a class 
344 /// template, which may be a member function, static data member, or
345 /// member class.
346 class MemberSpecializationInfo {
347   // The member declaration from which this member was instantiated, and the
348   // manner in which the instantiation occurred (in the lower two bits).
349   llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
350   
351   // The point at which this member was first instantiated.
352   SourceLocation PointOfInstantiation;
353   
354 public:
355   explicit 
356   MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK)
357     : MemberAndTSK(IF, TSK - 1), PointOfInstantiation() {
358     assert(TSK != TSK_Undeclared && 
359            "Cannot encode undeclared template specializations for members");
360   }
361   
362   /// \brief Retrieve the member declaration from which this member was
363   /// instantiated.
364   NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
365   
366   /// \brief Determine what kind of template specialization this is.
367   TemplateSpecializationKind getTemplateSpecializationKind() const {
368     return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
369   }
370   
371   /// \brief Set the template specialization kind.
372   void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
373     assert(TSK != TSK_Undeclared && 
374            "Cannot encode undeclared template specializations for members");
375     MemberAndTSK.setInt(TSK - 1);
376   }
377   
378   /// \brief Retrieve the first point of instantiation of this member. 
379   /// If the point of instantiation is an invalid location, then this member
380   /// has not yet been instantiated.
381   SourceLocation getPointOfInstantiation() const { 
382     return PointOfInstantiation; 
383   }
384   
385   /// \brief Set the first point of instantiation.
386   void setPointOfInstantiation(SourceLocation POI) {
387     PointOfInstantiation = POI;
388   }
389 };
390
391 /// \brief Provides information about a dependent function-template
392 /// specialization declaration.  Since explicit function template
393 /// specialization and instantiation declarations can only appear in
394 /// namespace scope, and you can only specialize a member of a
395 /// fully-specialized class, the only way to get one of these is in
396 /// a friend declaration like the following:
397 ///
398 ///   template <class T> void foo(T);
399 ///   template <class T> class A {
400 ///     friend void foo<>(T);
401 ///   };
402 class DependentFunctionTemplateSpecializationInfo {
403   union {
404     // Force sizeof to be a multiple of sizeof(void*) so that the
405     // trailing data is aligned.
406     void *Aligner; 
407
408     struct {
409       /// The number of potential template candidates.
410       unsigned NumTemplates;
411
412       /// The number of template arguments.
413       unsigned NumArgs;      
414     } d;
415   };
416
417   /// The locations of the left and right angle brackets.
418   SourceRange AngleLocs;
419
420   FunctionTemplateDecl * const *getTemplates() const {
421     return reinterpret_cast<FunctionTemplateDecl*const*>(this+1);
422   }
423
424   const TemplateArgumentLoc *getTemplateArgs() const {
425     return reinterpret_cast<const TemplateArgumentLoc*>(
426              &getTemplates()[getNumTemplates()]);
427   }
428
429 public:
430   DependentFunctionTemplateSpecializationInfo(
431                                  const UnresolvedSetImpl &Templates,
432                                  const TemplateArgumentListInfo &TemplateArgs);
433
434   /// \brief Returns the number of function templates that this might
435   /// be a specialization of.
436   unsigned getNumTemplates() const {
437     return d.NumTemplates;
438   }
439
440   /// \brief Returns the i'th template candidate.
441   FunctionTemplateDecl *getTemplate(unsigned I) const {
442     assert(I < getNumTemplates() && "template index out of range");
443     return getTemplates()[I];
444   }
445
446   /// \brief Returns the number of explicit template arguments that were given.
447   unsigned getNumTemplateArgs() const {
448     return d.NumArgs;
449   }
450
451   /// \brief Returns the nth template argument.
452   const TemplateArgumentLoc &getTemplateArg(unsigned I) const {
453     assert(I < getNumTemplateArgs() && "template arg index out of range");
454     return getTemplateArgs()[I];
455   }
456
457   SourceLocation getLAngleLoc() const {
458     return AngleLocs.getBegin();
459   }
460
461   SourceLocation getRAngleLoc() const {
462     return AngleLocs.getEnd();
463   }
464 };
465   
466 /// Declaration of a template function.
467 class FunctionTemplateDecl : public TemplateDecl {
468   static void DeallocateCommon(void *Ptr);
469                         
470 protected:
471   /// \brief Data that is common to all of the declarations of a given
472   /// function template.
473   struct Common {
474     Common() : InstantiatedFromMember(0, false) { }
475
476     /// \brief The function template specializations for this function
477     /// template, including explicit specializations and instantiations.
478     llvm::FoldingSet<FunctionTemplateSpecializationInfo> Specializations;
479
480     /// \brief The member function template from which this was most
481     /// directly instantiated (or null).
482     ///
483     /// The boolean value indicates whether this member function template
484     /// was explicitly specialized.
485     llvm::PointerIntPair<FunctionTemplateDecl*, 1, bool> InstantiatedFromMember;
486   };
487
488   /// \brief A pointer to the previous declaration (if this is a redeclaration)
489   /// or to the data that is common to all declarations of this function
490   /// template.
491   llvm::PointerUnion<Common*, FunctionTemplateDecl*> CommonOrPrev;
492
493   /// \brief Retrieves the "common" pointer shared by all
494   /// (re-)declarations of the same function template. Calling this routine
495   /// may implicitly allocate memory for the common pointer.
496   Common *getCommonPtr();
497
498   FunctionTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
499                        TemplateParameterList *Params, NamedDecl *Decl)
500     : TemplateDecl(FunctionTemplate, DC, L, Name, Params, Decl),
501       CommonOrPrev((Common*)0) { }
502
503 public:
504   void Destroy(ASTContext &C);
505
506   /// Get the underlying function declaration of the template.
507   FunctionDecl *getTemplatedDecl() const {
508     return static_cast<FunctionDecl*>(TemplatedDecl);
509   }
510
511   /// \brief Retrieve the set of function template specializations of this
512   /// function template.
513   llvm::FoldingSet<FunctionTemplateSpecializationInfo> &getSpecializations() {
514     return getCommonPtr()->Specializations;
515   }
516
517   /// \brief Retrieve the previous declaration of this function template, or
518   /// NULL if no such declaration exists.
519   const FunctionTemplateDecl *getPreviousDeclaration() const {
520     return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>();
521   }
522
523   /// \brief Retrieve the previous declaration of this function template, or
524   /// NULL if no such declaration exists.
525   FunctionTemplateDecl *getPreviousDeclaration() {
526     return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>();
527   }
528
529   /// \brief Set the previous declaration of this function template.
530   void setPreviousDeclaration(FunctionTemplateDecl *Prev) {
531     if (Prev)
532       CommonOrPrev = Prev;
533   }
534
535   virtual FunctionTemplateDecl *getCanonicalDecl();
536
537   /// \brief Retrieve the member function template that this function template
538   /// was instantiated from.
539   ///
540   /// This routine will return non-NULL for member function templates of
541   /// class templates.  For example, given:
542   ///
543   /// \code
544   /// template <typename T>
545   /// struct X {
546   ///   template <typename U> void f();
547   /// };
548   /// \endcode
549   ///
550   /// X<int>::A<float> is a CXXMethodDecl (whose parent is X<int>, a
551   /// ClassTemplateSpecializationDecl) for which getPrimaryTemplate() will
552   /// return X<int>::f, a FunctionTemplateDecl (whose parent is again
553   /// X<int>) for which getInstantiatedFromMemberTemplate() will return
554   /// X<T>::f, a FunctionTemplateDecl (whose parent is X<T>, a
555   /// ClassTemplateDecl).
556   ///
557   /// \returns NULL if this is not an instantiation of a member function
558   /// template.
559   FunctionTemplateDecl *getInstantiatedFromMemberTemplate() {
560     return getCommonPtr()->InstantiatedFromMember.getPointer();
561   }
562
563   void setInstantiatedFromMemberTemplate(FunctionTemplateDecl *FTD) {
564     assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
565     getCommonPtr()->InstantiatedFromMember.setPointer(FTD);
566   }
567
568   /// \brief Determines whether this template was a specialization of a 
569   /// member template.
570   ///
571   /// In the following example, the function template \c X<int>::f is a 
572   /// member specialization.
573   ///
574   /// \code
575   /// template<typename T>
576   /// struct X {
577   ///   template<typename U> void f(T, U);
578   /// };
579   ///
580   /// template<> template<typename T>
581   /// void X<int>::f(int, T);
582   /// \endcode
583   bool isMemberSpecialization() {
584     return getCommonPtr()->InstantiatedFromMember.getInt();
585   }
586   
587   /// \brief Note that this member template is a specialization.
588   void setMemberSpecialization() {
589     assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
590            "Only member templates can be member template specializations");
591     getCommonPtr()->InstantiatedFromMember.setInt(true);
592   }
593   
594   /// Create a template function node.
595   static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
596                                       SourceLocation L,
597                                       DeclarationName Name,
598                                       TemplateParameterList *Params,
599                                       NamedDecl *Decl);
600
601   // Implement isa/cast/dyncast support
602   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
603   static bool classof(const FunctionTemplateDecl *D) { return true; }
604   static bool classofKind(Kind K) { return K == FunctionTemplate; }
605 };
606
607 //===----------------------------------------------------------------------===//
608 // Kinds of Template Parameters
609 //===----------------------------------------------------------------------===//
610
611 /// The TemplateParmPosition class defines the position of a template parameter
612 /// within a template parameter list. Because template parameter can be listed
613 /// sequentially for out-of-line template members, each template parameter is
614 /// given a Depth - the nesting of template parameter scopes - and a Position -
615 /// the occurrence within the parameter list.
616 /// This class is inheritedly privately by different kinds of template
617 /// parameters and is not part of the Decl hierarchy. Just a facility.
618 class TemplateParmPosition {
619 protected:
620   // FIXME: This should probably never be called, but it's here as
621   TemplateParmPosition()
622     : Depth(0), Position(0)
623   { /* assert(0 && "Cannot create positionless template parameter"); */ }
624
625   TemplateParmPosition(unsigned D, unsigned P)
626     : Depth(D), Position(P)
627   { }
628
629   // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
630   // position? Maybe?
631   unsigned Depth;
632   unsigned Position;
633
634 public:
635   /// Get the nesting depth of the template parameter.
636   unsigned getDepth() const { return Depth; }
637
638   /// Get the position of the template parameter within its parameter list.
639   unsigned getPosition() const { return Position; }
640
641   /// Get the index of the template parameter within its parameter list.
642   unsigned getIndex() const { return Position; }
643 };
644
645 /// TemplateTypeParmDecl - Declaration of a template type parameter,
646 /// e.g., "T" in
647 /// @code
648 /// template<typename T> class vector;
649 /// @endcode
650 class TemplateTypeParmDecl : public TypeDecl {
651   /// \brief Whether this template type parameter was declaration with
652   /// the 'typename' keyword. If false, it was declared with the
653   /// 'class' keyword.
654   bool Typename : 1;
655
656   /// \brief Whether this template type parameter inherited its
657   /// default argument.
658   bool InheritedDefault : 1;
659
660   /// \brief Whether this is a parameter pack.
661   bool ParameterPack : 1;
662
663   /// \brief The default template argument, if any.
664   TypeSourceInfo *DefaultArgument;
665
666   TemplateTypeParmDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
667                        bool Typename, QualType Type, bool ParameterPack)
668     : TypeDecl(TemplateTypeParm, DC, L, Id), Typename(Typename),
669       InheritedDefault(false), ParameterPack(ParameterPack), DefaultArgument() {
670     TypeForDecl = Type.getTypePtr();
671   }
672
673 public:
674   static TemplateTypeParmDecl *Create(ASTContext &C, DeclContext *DC,
675                                       SourceLocation L, unsigned D, unsigned P,
676                                       IdentifierInfo *Id, bool Typename,
677                                       bool ParameterPack);
678
679   /// \brief Whether this template type parameter was declared with
680   /// the 'typename' keyword. If not, it was declared with the 'class'
681   /// keyword.
682   bool wasDeclaredWithTypename() const { return Typename; }
683
684   /// \brief Determine whether this template parameter has a default
685   /// argument.
686   bool hasDefaultArgument() const { return DefaultArgument != 0; }
687
688   /// \brief Retrieve the default argument, if any.
689   QualType getDefaultArgument() const { return DefaultArgument->getType(); }
690
691   /// \brief Retrieves the default argument's source information, if any.
692   TypeSourceInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
693
694   /// \brief Retrieves the location of the default argument declaration.
695   SourceLocation getDefaultArgumentLoc() const;
696
697   /// \brief Determines whether the default argument was inherited
698   /// from a previous declaration of this template.
699   bool defaultArgumentWasInherited() const { return InheritedDefault; }
700
701   /// \brief Set the default argument for this template parameter, and
702   /// whether that default argument was inherited from another
703   /// declaration.
704   void setDefaultArgument(TypeSourceInfo *DefArg, bool Inherited) {
705     DefaultArgument = DefArg;
706     InheritedDefault = Inherited;
707   }
708
709   /// \brief Removes the default argument of this template parameter.
710   void removeDefaultArgument() {
711     DefaultArgument = 0;
712     InheritedDefault = false;
713   }
714
715   /// \brief Retrieve the depth of the template parameter.
716   unsigned getDepth() const;
717   
718   /// \brief Retrieve the index of the template parameter.
719   unsigned getIndex() const;
720
721   /// \brief Returns whether this is a parameter pack.
722   bool isParameterPack() const { return ParameterPack; }
723
724   // Implement isa/cast/dyncast/etc.
725   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
726   static bool classof(const TemplateTypeParmDecl *D) { return true; }
727   static bool classofKind(Kind K) { return K == TemplateTypeParm; }
728 };
729
730 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
731 /// e.g., "Size" in
732 /// @code
733 /// template<int Size> class array { };
734 /// @endcode
735 class NonTypeTemplateParmDecl
736   : public VarDecl, protected TemplateParmPosition {
737   /// \brief The default template argument, if any.
738   Expr *DefaultArgument;
739
740   NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation L, unsigned D,
741                           unsigned P, IdentifierInfo *Id, QualType T,
742                           TypeSourceInfo *TInfo)
743     : VarDecl(NonTypeTemplateParm, DC, L, Id, T, TInfo, VarDecl::None,
744               VarDecl::None),
745       TemplateParmPosition(D, P), DefaultArgument(0)
746   { }
747
748 public:
749   static NonTypeTemplateParmDecl *
750   Create(ASTContext &C, DeclContext *DC, SourceLocation L, unsigned D,
751          unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo);
752
753   using TemplateParmPosition::getDepth;
754   using TemplateParmPosition::getPosition;
755   using TemplateParmPosition::getIndex;
756
757   /// \brief Determine whether this template parameter has a default
758   /// argument.
759   bool hasDefaultArgument() const { return DefaultArgument; }
760
761   /// \brief Retrieve the default argument, if any.
762   Expr *getDefaultArgument() const { return DefaultArgument; }
763
764   /// \brief Retrieve the location of the default argument, if any.
765   SourceLocation getDefaultArgumentLoc() const;
766
767   /// \brief Set the default argument for this template parameter.
768   void setDefaultArgument(Expr *DefArg) {
769     DefaultArgument = DefArg;
770   }
771
772   // Implement isa/cast/dyncast/etc.
773   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
774   static bool classof(const NonTypeTemplateParmDecl *D) { return true; }
775   static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
776 };
777
778 /// TemplateTemplateParmDecl - Declares a template template parameter,
779 /// e.g., "T" in
780 /// @code
781 /// template <template <typename> class T> class container { };
782 /// @endcode
783 /// A template template parameter is a TemplateDecl because it defines the
784 /// name of a template and the template parameters allowable for substitution.
785 class TemplateTemplateParmDecl
786   : public TemplateDecl, protected TemplateParmPosition {
787
788   /// \brief The default template argument, if any.
789   TemplateArgumentLoc DefaultArgument;
790
791   TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
792                            unsigned D, unsigned P,
793                            IdentifierInfo *Id, TemplateParameterList *Params)
794     : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
795       TemplateParmPosition(D, P), DefaultArgument()
796     { }
797
798 public:
799   static TemplateTemplateParmDecl *Create(ASTContext &C, DeclContext *DC,
800                                           SourceLocation L, unsigned D,
801                                           unsigned P, IdentifierInfo *Id,
802                                           TemplateParameterList *Params);
803
804   using TemplateParmPosition::getDepth;
805   using TemplateParmPosition::getPosition;
806   using TemplateParmPosition::getIndex;
807
808   /// \brief Determine whether this template parameter has a default
809   /// argument.
810   bool hasDefaultArgument() const { 
811     return !DefaultArgument.getArgument().isNull(); 
812   }
813
814   /// \brief Retrieve the default argument, if any.
815   const TemplateArgumentLoc &getDefaultArgument() const { 
816     return DefaultArgument; 
817   }
818
819   /// \brief Set the default argument for this template parameter.
820   void setDefaultArgument(const TemplateArgumentLoc &DefArg) {
821     DefaultArgument = DefArg;
822   }
823
824   // Implement isa/cast/dyncast/etc.
825   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
826   static bool classof(const TemplateTemplateParmDecl *D) { return true; }
827   static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
828 };
829
830 /// \brief Represents a class template specialization, which refers to
831 /// a class template with a given set of template arguments.
832 ///
833 /// Class template specializations represent both explicit
834 /// specialization of class templates, as in the example below, and
835 /// implicit instantiations of class templates.
836 ///
837 /// \code
838 /// template<typename T> class array;
839 ///
840 /// template<>
841 /// class array<bool> { }; // class template specialization array<bool>
842 /// \endcode
843 class ClassTemplateSpecializationDecl
844   : public CXXRecordDecl, public llvm::FoldingSetNode {
845
846   /// \brief Structure that stores information about a class template
847   /// specialization that was instantiated from a class template partial
848   /// specialization.
849   struct SpecializedPartialSpecialization {
850     /// \brief The class template partial specialization from which this
851     /// class template specialization was instantiated.
852     ClassTemplatePartialSpecializationDecl *PartialSpecialization;
853
854     /// \brief The template argument list deduced for the class template
855     /// partial specialization itself.
856     TemplateArgumentList *TemplateArgs;
857   };
858
859   /// \brief The template that this specialization specializes
860   llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
861     SpecializedTemplate;
862
863   /// \brief The type-as-written of an explicit template specialization.
864   /// Does not apply to implicit specializations.
865   TypeSourceInfo *TypeAsWritten;
866
867   /// \brief The template arguments used to describe this specialization.
868   TemplateArgumentList TemplateArgs;
869
870   /// \brief The point where this template was instantiated (if any)
871   SourceLocation PointOfInstantiation;
872
873   /// \brief The kind of specialization this declaration refers to.
874   /// Really a value of type TemplateSpecializationKind.
875   unsigned SpecializationKind : 3;
876
877 protected:
878   ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
879                                   DeclContext *DC, SourceLocation L,
880                                   ClassTemplateDecl *SpecializedTemplate,
881                                   TemplateArgumentListBuilder &Builder,
882                                   ClassTemplateSpecializationDecl *PrevDecl);
883
884 public:
885   static ClassTemplateSpecializationDecl *
886   Create(ASTContext &Context, TagKind TK, DeclContext *DC, SourceLocation L,
887          ClassTemplateDecl *SpecializedTemplate,
888          TemplateArgumentListBuilder &Builder,
889          ClassTemplateSpecializationDecl *PrevDecl);
890
891   virtual void Destroy(ASTContext& C);
892
893   virtual void getNameForDiagnostic(std::string &S,
894                                     const PrintingPolicy &Policy,
895                                     bool Qualified) const;
896
897   /// \brief Retrieve the template that this specialization specializes.
898   ClassTemplateDecl *getSpecializedTemplate() const;
899
900   /// \brief Retrieve the template arguments of the class template
901   /// specialization.
902   const TemplateArgumentList &getTemplateArgs() const {
903     return TemplateArgs;
904   }
905
906   /// \brief Determine the kind of specialization that this
907   /// declaration represents.
908   TemplateSpecializationKind getSpecializationKind() const {
909     return static_cast<TemplateSpecializationKind>(SpecializationKind);
910   }
911
912   void setSpecializationKind(TemplateSpecializationKind TSK) {
913     SpecializationKind = TSK;
914   }
915
916   /// \brief Get the point of instantiation (if any), or null if none.
917   SourceLocation getPointOfInstantiation() const {
918     return PointOfInstantiation;
919   }
920
921   void setPointOfInstantiation(SourceLocation Loc) {
922     assert(Loc.isValid() && "point of instantiation must be valid!");
923     PointOfInstantiation = Loc;
924   }
925
926   /// \brief If this class template specialization is an instantiation of
927   /// a template (rather than an explicit specialization), return the
928   /// class template or class template partial specialization from which it
929   /// was instantiated.
930   llvm::PointerUnion<ClassTemplateDecl *,
931                      ClassTemplatePartialSpecializationDecl *>
932   getInstantiatedFrom() const {
933     if (getSpecializationKind() != TSK_ImplicitInstantiation &&
934         getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
935         getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
936       return (ClassTemplateDecl*)0;
937
938     if (SpecializedPartialSpecialization *PartialSpec
939           = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
940       return PartialSpec->PartialSpecialization;
941
942     return const_cast<ClassTemplateDecl*>(
943                              SpecializedTemplate.get<ClassTemplateDecl*>());
944   }
945
946   /// \brief Retrieve the set of template arguments that should be used
947   /// to instantiate members of the class template or class template partial
948   /// specialization from which this class template specialization was
949   /// instantiated.
950   ///
951   /// \returns For a class template specialization instantiated from the primary
952   /// template, this function will return the same template arguments as
953   /// getTemplateArgs(). For a class template specialization instantiated from
954   /// a class template partial specialization, this function will return the
955   /// deduced template arguments for the class template partial specialization
956   /// itself.
957   const TemplateArgumentList &getTemplateInstantiationArgs() const {
958     if (SpecializedPartialSpecialization *PartialSpec
959         = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
960       return *PartialSpec->TemplateArgs;
961
962     return getTemplateArgs();
963   }
964
965   /// \brief Note that this class template specialization is actually an
966   /// instantiation of the given class template partial specialization whose
967   /// template arguments have been deduced.
968   void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
969                           TemplateArgumentList *TemplateArgs) {
970     SpecializedPartialSpecialization *PS
971       = new (getASTContext()) SpecializedPartialSpecialization();
972     PS->PartialSpecialization = PartialSpec;
973     PS->TemplateArgs = TemplateArgs;
974     SpecializedTemplate = PS;
975   }
976
977   /// \brief Sets the type of this specialization as it was written by
978   /// the user. This will be a class template specialization type.
979   void setTypeAsWritten(TypeSourceInfo *T) {
980     TypeAsWritten = T;
981   }
982
983   /// \brief Gets the type of this specialization as it was written by
984   /// the user, if it was so written.
985   TypeSourceInfo *getTypeAsWritten() const {
986     return TypeAsWritten;
987   }
988
989   void Profile(llvm::FoldingSetNodeID &ID) const {
990     Profile(ID, TemplateArgs.getFlatArgumentList(), TemplateArgs.flat_size(),
991             getASTContext());
992   }
993
994   static void
995   Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
996           unsigned NumTemplateArgs, ASTContext &Context) {
997     ID.AddInteger(NumTemplateArgs);
998     for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
999       TemplateArgs[Arg].Profile(ID, Context);
1000   }
1001
1002   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1003   static bool classofKind(Kind K) {
1004     return K == ClassTemplateSpecialization ||
1005            K == ClassTemplatePartialSpecialization;
1006   }
1007
1008   static bool classof(const ClassTemplateSpecializationDecl *) {
1009     return true;
1010   }
1011
1012   static bool classof(const ClassTemplatePartialSpecializationDecl *) {
1013     return true;
1014   }
1015 };
1016
1017 class ClassTemplatePartialSpecializationDecl
1018   : public ClassTemplateSpecializationDecl {
1019   /// \brief The list of template parameters
1020   TemplateParameterList* TemplateParams;
1021
1022   /// \brief The source info for the template arguments as written.
1023   /// FIXME: redundant with TypeAsWritten?
1024   TemplateArgumentLoc *ArgsAsWritten;
1025   unsigned NumArgsAsWritten;
1026
1027   /// \brief Sequence number indicating when this class template partial
1028   /// specialization was added to the set of partial specializations for
1029   /// its owning class template.
1030   unsigned SequenceNumber;
1031     
1032   /// \brief The class template partial specialization from which this 
1033   /// class template partial specialization was instantiated.
1034   ///
1035   /// The boolean value will be true to indicate that this class template
1036   /// partial specialization was specialized at this level.
1037   llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
1038       InstantiatedFromMember;
1039     
1040   ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
1041                                          DeclContext *DC, SourceLocation L,
1042                                          TemplateParameterList *Params,
1043                                          ClassTemplateDecl *SpecializedTemplate,
1044                                          TemplateArgumentListBuilder &Builder,
1045                                          TemplateArgumentLoc *ArgInfos,
1046                                          unsigned NumArgInfos,
1047                                ClassTemplatePartialSpecializationDecl *PrevDecl,
1048                                          unsigned SequenceNumber)
1049     : ClassTemplateSpecializationDecl(Context,
1050                                       ClassTemplatePartialSpecialization,
1051                                       TK, DC, L, SpecializedTemplate, Builder,
1052                                       PrevDecl),
1053       TemplateParams(Params), ArgsAsWritten(ArgInfos),
1054       NumArgsAsWritten(NumArgInfos), SequenceNumber(SequenceNumber),
1055       InstantiatedFromMember(0, false) { }
1056
1057 public:
1058   static ClassTemplatePartialSpecializationDecl *
1059   Create(ASTContext &Context, TagKind TK,DeclContext *DC, SourceLocation L,
1060          TemplateParameterList *Params,
1061          ClassTemplateDecl *SpecializedTemplate,
1062          TemplateArgumentListBuilder &Builder,
1063          const TemplateArgumentListInfo &ArgInfos,
1064          QualType CanonInjectedType,
1065          ClassTemplatePartialSpecializationDecl *PrevDecl,
1066          unsigned SequenceNumber);
1067
1068   /// Get the list of template parameters
1069   TemplateParameterList *getTemplateParameters() const {
1070     return TemplateParams;
1071   }
1072
1073   /// Get the template arguments as written.
1074   TemplateArgumentLoc *getTemplateArgsAsWritten() const {
1075     return ArgsAsWritten;
1076   }
1077
1078   /// Get the number of template arguments as written.
1079   unsigned getNumTemplateArgsAsWritten() const {
1080     return NumArgsAsWritten;
1081   }
1082
1083   /// \brief Get the sequence number for this class template partial
1084   /// specialization.
1085   unsigned getSequenceNumber() const { return SequenceNumber; }
1086     
1087   /// \brief Retrieve the member class template partial specialization from
1088   /// which this particular class template partial specialization was
1089   /// instantiated.
1090   ///
1091   /// \code
1092   /// template<typename T>
1093   /// struct Outer {
1094   ///   template<typename U> struct Inner;
1095   ///   template<typename U> struct Inner<U*> { }; // #1
1096   /// };
1097   ///
1098   /// Outer<float>::Inner<int*> ii;
1099   /// \endcode
1100   ///
1101   /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
1102   /// end up instantiating the partial specialization 
1103   /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class 
1104   /// template partial specialization \c Outer<T>::Inner<U*>. Given 
1105   /// \c Outer<float>::Inner<U*>, this function would return
1106   /// \c Outer<T>::Inner<U*>.
1107   ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
1108     ClassTemplatePartialSpecializationDecl *First
1109       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1110     return First->InstantiatedFromMember.getPointer();
1111   }
1112   
1113   void setInstantiatedFromMember(
1114                           ClassTemplatePartialSpecializationDecl *PartialSpec) {
1115     ClassTemplatePartialSpecializationDecl *First
1116       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1117     First->InstantiatedFromMember.setPointer(PartialSpec);
1118   }
1119     
1120   /// \brief Determines whether this class template partial specialization 
1121   /// template was a specialization of a member partial specialization.
1122   ///
1123   /// In the following example, the member template partial specialization
1124   /// \c X<int>::Inner<T*> is a member specialization.
1125   ///
1126   /// \code
1127   /// template<typename T>
1128   /// struct X {
1129   ///   template<typename U> struct Inner;
1130   ///   template<typename U> struct Inner<U*>;
1131   /// };
1132   ///
1133   /// template<> template<typename T>
1134   /// struct X<int>::Inner<T*> { /* ... */ };
1135   /// \endcode
1136   bool isMemberSpecialization() {
1137     ClassTemplatePartialSpecializationDecl *First
1138       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1139     return First->InstantiatedFromMember.getInt();
1140   }
1141   
1142   /// \brief Note that this member template is a specialization.
1143   void setMemberSpecialization() {
1144     ClassTemplatePartialSpecializationDecl *First
1145       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
1146     assert(First->InstantiatedFromMember.getPointer() &&
1147            "Only member templates can be member template specializations");
1148     return First->InstantiatedFromMember.setInt(true);
1149   }
1150
1151   /// Retrieves the injected specialization type for this partial
1152   /// specialization.  This is not the same as the type-decl-type for
1153   /// this partial specialization, which is an InjectedClassNameType.
1154   QualType getInjectedSpecializationType() const {
1155     assert(getTypeForDecl() && "partial specialization has no type set!");
1156     return cast<InjectedClassNameType>(getTypeForDecl())
1157              ->getInjectedSpecializationType();
1158   }
1159     
1160   // FIXME: Add Profile support!
1161
1162   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1163   static bool classofKind(Kind K) {
1164     return K == ClassTemplatePartialSpecialization;
1165   }
1166
1167   static bool classof(const ClassTemplatePartialSpecializationDecl *) {
1168     return true;
1169   }
1170 };
1171
1172 /// Declaration of a class template.
1173 class ClassTemplateDecl : public TemplateDecl {
1174   static void DeallocateCommon(void *Ptr);
1175   
1176 protected:
1177   /// \brief Data that is common to all of the declarations of a given
1178   /// class template.
1179   struct Common {
1180     Common() : InstantiatedFromMember(0, 0) {}
1181
1182     /// \brief The class template specializations for this class
1183     /// template, including explicit specializations and instantiations.
1184     llvm::FoldingSet<ClassTemplateSpecializationDecl> Specializations;
1185
1186     /// \brief The class template partial specializations for this class
1187     /// template.
1188     llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>
1189       PartialSpecializations;
1190
1191     /// \brief The injected-class-name type for this class template.
1192     QualType InjectedClassNameType;
1193
1194     /// \brief The templated member class from which this was most
1195     /// directly instantiated (or null).
1196     ///
1197     /// The boolean value indicates whether this member class template
1198     /// was explicitly specialized.
1199     llvm::PointerIntPair<ClassTemplateDecl *, 1, bool> InstantiatedFromMember;
1200   };
1201
1202   // FIXME: Combine PreviousDeclaration with CommonPtr, as in 
1203   // FunctionTemplateDecl.
1204   
1205   /// \brief Previous declaration of this class template.
1206   ClassTemplateDecl *PreviousDeclaration;
1207
1208   /// \brief Pointer to the data that is common to all of the
1209   /// declarations of this class template.
1210   ///
1211   /// The first declaration of a class template (e.g., the declaration
1212   /// with no "previous declaration") owns this pointer.
1213   Common *CommonPtr;
1214
1215   ClassTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
1216                     TemplateParameterList *Params, NamedDecl *Decl,
1217                     ClassTemplateDecl *PrevDecl, Common *CommonPtr)
1218     : TemplateDecl(ClassTemplate, DC, L, Name, Params, Decl),
1219       PreviousDeclaration(PrevDecl), CommonPtr(CommonPtr) { }
1220
1221   ~ClassTemplateDecl();
1222
1223 public:
1224   /// Get the underlying class declarations of the template.
1225   CXXRecordDecl *getTemplatedDecl() const {
1226     return static_cast<CXXRecordDecl *>(TemplatedDecl);
1227   }
1228
1229   /// \brief Retrieve the previous declaration of this template.
1230   ClassTemplateDecl *getPreviousDeclaration() const {
1231     return PreviousDeclaration;
1232   }
1233
1234   virtual ClassTemplateDecl *getCanonicalDecl();
1235
1236   /// Create a class template node.
1237   static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
1238                                    SourceLocation L,
1239                                    DeclarationName Name,
1240                                    TemplateParameterList *Params,
1241                                    NamedDecl *Decl,
1242                                    ClassTemplateDecl *PrevDecl);
1243
1244   /// \brief Retrieve the set of specializations of this class template.
1245   llvm::FoldingSet<ClassTemplateSpecializationDecl> &getSpecializations() {
1246     return CommonPtr->Specializations;
1247   }
1248
1249   /// \brief Retrieve the set of partial specializations of this class
1250   /// template.
1251   llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &
1252   getPartialSpecializations() {
1253     return CommonPtr->PartialSpecializations;
1254   }
1255
1256   /// \brief Retrieve the partial specializations as an ordered list.
1257   void getPartialSpecializations(
1258           llvm::SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS);
1259   
1260   /// \brief Find a class template partial specialization with the given
1261   /// type T.
1262   ///
1263   /// \brief A dependent type that names a specialization of this class
1264   /// template.
1265   ///
1266   /// \returns the class template partial specialization that exactly matches
1267   /// the type \p T, or NULL if no such partial specialization exists.
1268   ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
1269
1270   /// \brief Retrieve the template specialization type of the
1271   /// injected-class-name for this class template.
1272   ///
1273   /// The injected-class-name for a class template \c X is \c
1274   /// X<template-args>, where \c template-args is formed from the
1275   /// template arguments that correspond to the template parameters of
1276   /// \c X. For example:
1277   ///
1278   /// \code
1279   /// template<typename T, int N>
1280   /// struct array {
1281   ///   typedef array this_type; // "array" is equivalent to "array<T, N>"
1282   /// };
1283   /// \endcode
1284   QualType getInjectedClassNameSpecialization(ASTContext &Context);
1285
1286   /// \brief Retrieve the member class template that this class template was
1287   /// derived from.
1288   ///
1289   /// This routine will return non-NULL for templated member classes of
1290   /// class templates.  For example, given:
1291   ///
1292   /// \code
1293   /// template <typename T>
1294   /// struct X {
1295   ///   template <typename U> struct A {};
1296   /// };
1297   /// \endcode
1298   ///
1299   /// X<int>::A<float> is a ClassTemplateSpecializationDecl (whose parent
1300   /// is X<int>, also a CTSD) for which getSpecializedTemplate() will
1301   /// return X<int>::A<U>, a TemplateClassDecl (whose parent is again
1302   /// X<int>) for which getInstantiatedFromMemberTemplate() will return
1303   /// X<T>::A<U>, a TemplateClassDecl (whose parent is X<T>, also a TCD).
1304   ///
1305   /// \returns null if this is not an instantiation of a member class template.
1306   ClassTemplateDecl *getInstantiatedFromMemberTemplate() const {
1307     return CommonPtr->InstantiatedFromMember.getPointer();
1308   }
1309
1310   void setInstantiatedFromMemberTemplate(ClassTemplateDecl *CTD) {
1311     assert(!CommonPtr->InstantiatedFromMember.getPointer());
1312     CommonPtr->InstantiatedFromMember.setPointer(CTD);
1313   }
1314
1315   /// \brief Determines whether this template was a specialization of a 
1316   /// member template.
1317   ///
1318   /// In the following example, the member template \c X<int>::Inner is a 
1319   /// member specialization.
1320   ///
1321   /// \code
1322   /// template<typename T>
1323   /// struct X {
1324   ///   template<typename U> struct Inner;
1325   /// };
1326   ///
1327   /// template<> template<typename T>
1328   /// struct X<int>::Inner { /* ... */ };
1329   /// \endcode
1330   bool isMemberSpecialization() {
1331     return CommonPtr->InstantiatedFromMember.getInt();
1332   }
1333   
1334   /// \brief Note that this member template is a specialization.
1335   void setMemberSpecialization() {
1336     assert(CommonPtr->InstantiatedFromMember.getPointer() &&
1337            "Only member templates can be member template specializations");
1338     CommonPtr->InstantiatedFromMember.setInt(true);
1339   }
1340   
1341   // Implement isa/cast/dyncast support
1342   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1343   static bool classof(const ClassTemplateDecl *D) { return true; }
1344   static bool classofKind(Kind K) { return K == ClassTemplate; }
1345
1346   virtual void Destroy(ASTContext& C);
1347 };
1348
1349 /// Declaration of a friend template.  For example:
1350 ///
1351 /// template <typename T> class A {
1352 ///   friend class MyVector<T>; // not a friend template
1353 ///   template <typename U> friend class B; // friend template
1354 ///   template <typename U> friend class Foo<T>::Nested; // friend template
1355 class FriendTemplateDecl : public Decl {
1356 public:
1357   typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
1358
1359 private:
1360   // The number of template parameters;  always non-zero.
1361   unsigned NumParams;
1362
1363   // The parameter list.
1364   TemplateParameterList **Params;
1365
1366   // The declaration that's a friend of this class.
1367   FriendUnion Friend;
1368
1369   // Location of the 'friend' specifier.
1370   SourceLocation FriendLoc;
1371
1372
1373   FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
1374                      unsigned NParams, 
1375                      TemplateParameterList **Params,
1376                      FriendUnion Friend,
1377                      SourceLocation FriendLoc)
1378     : Decl(Decl::FriendTemplate, DC, Loc),
1379       NumParams(NParams),
1380       Params(Params),
1381       Friend(Friend),
1382       FriendLoc(FriendLoc)
1383   {}
1384
1385 public:
1386   static FriendTemplateDecl *Create(ASTContext &Context,
1387                                     DeclContext *DC, SourceLocation Loc,
1388                                     unsigned NParams, 
1389                                     TemplateParameterList **Params,
1390                                     FriendUnion Friend,
1391                                     SourceLocation FriendLoc);
1392
1393   /// If this friend declaration names a templated type (or
1394   /// a dependent member type of a templated type), return that
1395   /// type;  otherwise return null.
1396   TypeSourceInfo *getFriendType() const {
1397     return Friend.dyn_cast<TypeSourceInfo*>();
1398   }
1399
1400   /// If this friend declaration names a templated function (or
1401   /// a member function of a templated type), return that type;
1402   /// otherwise return null.
1403   NamedDecl *getFriendDecl() const {
1404     return Friend.dyn_cast<NamedDecl*>();
1405   }
1406
1407   /// Retrieves the location of the 'friend' keyword.
1408   SourceLocation getFriendLoc() const {
1409     return FriendLoc;
1410   }
1411
1412   TemplateParameterList *getTemplateParameterList(unsigned i) const {
1413     assert(i <= NumParams);
1414     return Params[i];
1415   }
1416
1417   unsigned getNumTemplateParameters() const {
1418     return NumParams;
1419   }
1420
1421   // Implement isa/cast/dyncast/etc.
1422   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1423   static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
1424   static bool classof(const FriendTemplateDecl *D) { return true; }
1425 };
1426
1427 /// Implementation of inline functions that require the template declarations
1428 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
1429   : Function(FTD) { }
1430
1431 } /* end of namespace clang */
1432
1433 #endif