Home | History | Annotate | Download | only in AST
      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 /// \file
     11 /// \brief Defines the C++ template declaration subclasses.
     12 ///
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_AST_DECLTEMPLATE_H
     16 #define LLVM_CLANG_AST_DECLTEMPLATE_H
     17 
     18 #include "clang/AST/DeclCXX.h"
     19 #include "clang/AST/Redeclarable.h"
     20 #include "clang/AST/TemplateBase.h"
     21 #include "llvm/ADT/PointerUnion.h"
     22 #include "llvm/Support/Compiler.h"
     23 #include <limits>
     24 
     25 namespace clang {
     26 
     27 class TemplateParameterList;
     28 class TemplateDecl;
     29 class RedeclarableTemplateDecl;
     30 class FunctionTemplateDecl;
     31 class ClassTemplateDecl;
     32 class ClassTemplatePartialSpecializationDecl;
     33 class TemplateTypeParmDecl;
     34 class NonTypeTemplateParmDecl;
     35 class TemplateTemplateParmDecl;
     36 class TypeAliasTemplateDecl;
     37 class VarTemplateDecl;
     38 class VarTemplatePartialSpecializationDecl;
     39 
     40 /// \brief Stores a template parameter of any kind.
     41 typedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*,
     42                             TemplateTemplateParmDecl*> TemplateParameter;
     43 
     44 /// \brief Stores a list of template parameters for a TemplateDecl and its
     45 /// derived classes.
     46 class TemplateParameterList {
     47   /// The location of the 'template' keyword.
     48   SourceLocation TemplateLoc;
     49 
     50   /// The locations of the '<' and '>' angle brackets.
     51   SourceLocation LAngleLoc, RAngleLoc;
     52 
     53   /// The number of template parameters in this template
     54   /// parameter list.
     55   unsigned NumParams : 31;
     56 
     57   /// Whether this template parameter list contains an unexpanded parameter
     58   /// pack.
     59   unsigned ContainsUnexpandedParameterPack : 1;
     60 
     61 protected:
     62   TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc,
     63                         NamedDecl **Params, unsigned NumParams,
     64                         SourceLocation RAngleLoc);
     65 
     66 public:
     67   static TemplateParameterList *Create(const ASTContext &C,
     68                                        SourceLocation TemplateLoc,
     69                                        SourceLocation LAngleLoc,
     70                                        NamedDecl **Params,
     71                                        unsigned NumParams,
     72                                        SourceLocation RAngleLoc);
     73 
     74   /// \brief Iterates through the template parameters in this list.
     75   typedef NamedDecl** iterator;
     76 
     77   /// \brief Iterates through the template parameters in this list.
     78   typedef NamedDecl* const* const_iterator;
     79 
     80   iterator begin() { return reinterpret_cast<NamedDecl **>(this + 1); }
     81   const_iterator begin() const {
     82     return reinterpret_cast<NamedDecl * const *>(this + 1);
     83   }
     84   iterator end() { return begin() + NumParams; }
     85   const_iterator end() const { return begin() + NumParams; }
     86 
     87   unsigned size() const { return NumParams; }
     88 
     89   llvm::ArrayRef<NamedDecl*> asArray() {
     90     return llvm::ArrayRef<NamedDecl*>(begin(), size());
     91   }
     92   llvm::ArrayRef<const NamedDecl*> asArray() const {
     93     return llvm::ArrayRef<const NamedDecl*>(begin(), size());
     94   }
     95 
     96   NamedDecl* getParam(unsigned Idx) {
     97     assert(Idx < size() && "Template parameter index out-of-range");
     98     return begin()[Idx];
     99   }
    100 
    101   const NamedDecl* getParam(unsigned Idx) const {
    102     assert(Idx < size() && "Template parameter index out-of-range");
    103     return begin()[Idx];
    104   }
    105 
    106   /// \brief Returns the minimum number of arguments needed to form a
    107   /// template specialization.
    108   ///
    109   /// This may be fewer than the number of template parameters, if some of
    110   /// the parameters have default arguments or if there is a parameter pack.
    111   unsigned getMinRequiredArguments() const;
    112 
    113   /// \brief Get the depth of this template parameter list in the set of
    114   /// template parameter lists.
    115   ///
    116   /// The first template parameter list in a declaration will have depth 0,
    117   /// the second template parameter list will have depth 1, etc.
    118   unsigned getDepth() const;
    119 
    120   /// \brief Determine whether this template parameter list contains an
    121   /// unexpanded parameter pack.
    122   bool containsUnexpandedParameterPack() const {
    123     return ContainsUnexpandedParameterPack;
    124   }
    125 
    126   SourceLocation getTemplateLoc() const { return TemplateLoc; }
    127   SourceLocation getLAngleLoc() const { return LAngleLoc; }
    128   SourceLocation getRAngleLoc() const { return RAngleLoc; }
    129 
    130   SourceRange getSourceRange() const LLVM_READONLY {
    131     return SourceRange(TemplateLoc, RAngleLoc);
    132   }
    133 };
    134 
    135 /// \brief Stores a list of template parameters for a TemplateDecl and its
    136 /// derived classes. Suitable for creating on the stack.
    137 template<size_t N>
    138 class FixedSizeTemplateParameterList : public TemplateParameterList {
    139   NamedDecl *Params[N];
    140 
    141 public:
    142   FixedSizeTemplateParameterList(SourceLocation TemplateLoc,
    143                                  SourceLocation LAngleLoc,
    144                                  NamedDecl **Params, SourceLocation RAngleLoc) :
    145     TemplateParameterList(TemplateLoc, LAngleLoc, Params, N, RAngleLoc) {
    146   }
    147 };
    148 
    149 /// \brief A template argument list.
    150 class TemplateArgumentList {
    151   /// \brief The template argument list.
    152   ///
    153   /// The integer value will be non-zero to indicate that this
    154   /// template argument list does own the pointer.
    155   llvm::PointerIntPair<const TemplateArgument *, 1> Arguments;
    156 
    157   /// \brief The number of template arguments in this template
    158   /// argument list.
    159   unsigned NumArguments;
    160 
    161   TemplateArgumentList(const TemplateArgumentList &Other) LLVM_DELETED_FUNCTION;
    162   void operator=(const TemplateArgumentList &Other) LLVM_DELETED_FUNCTION;
    163 
    164   TemplateArgumentList(const TemplateArgument *Args, unsigned NumArgs,
    165                        bool Owned)
    166     : Arguments(Args, Owned), NumArguments(NumArgs) { }
    167 
    168 public:
    169   /// \brief Type used to indicate that the template argument list itself is a
    170   /// stack object. It does not own its template arguments.
    171   enum OnStackType { OnStack };
    172 
    173   /// \brief Create a new template argument list that copies the given set of
    174   /// template arguments.
    175   static TemplateArgumentList *CreateCopy(ASTContext &Context,
    176                                           const TemplateArgument *Args,
    177                                           unsigned NumArgs);
    178 
    179   /// \brief Construct a new, temporary template argument list on the stack.
    180   ///
    181   /// The template argument list does not own the template arguments
    182   /// provided.
    183   explicit TemplateArgumentList(OnStackType,
    184                                 const TemplateArgument *Args, unsigned NumArgs)
    185     : Arguments(Args, false), NumArguments(NumArgs) { }
    186 
    187   /// \brief Produces a shallow copy of the given template argument list.
    188   ///
    189   /// This operation assumes that the input argument list outlives it.
    190   /// This takes the list as a pointer to avoid looking like a copy
    191   /// constructor, since this really really isn't safe to use that
    192   /// way.
    193   explicit TemplateArgumentList(const TemplateArgumentList *Other)
    194     : Arguments(Other->data(), false), NumArguments(Other->size()) { }
    195 
    196   /// \brief Retrieve the template argument at a given index.
    197   const TemplateArgument &get(unsigned Idx) const {
    198     assert(Idx < NumArguments && "Invalid template argument index");
    199     return data()[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 Produce this as an array ref.
    206   llvm::ArrayRef<TemplateArgument> asArray() const {
    207     return llvm::ArrayRef<TemplateArgument>(data(), size());
    208   }
    209 
    210   /// \brief Retrieve the number of template arguments in this
    211   /// template argument list.
    212   unsigned size() const { return NumArguments; }
    213 
    214   /// \brief Retrieve a pointer to the template argument list.
    215   const TemplateArgument *data() const {
    216     return Arguments.getPointer();
    217   }
    218 };
    219 
    220 //===----------------------------------------------------------------------===//
    221 // Kinds of Templates
    222 //===----------------------------------------------------------------------===//
    223 
    224 /// \brief The base class of all kinds of template declarations (e.g.,
    225 /// class, function, etc.).
    226 ///
    227 /// The TemplateDecl class stores the list of template parameters and a
    228 /// reference to the templated scoped declaration: the underlying AST node.
    229 class TemplateDecl : public NamedDecl {
    230   virtual void anchor();
    231 protected:
    232   // This is probably never used.
    233   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
    234                DeclarationName Name)
    235     : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(0) { }
    236 
    237   // Construct a template decl with the given name and parameters.
    238   // Used when there is not templated element (tt-params, alias?).
    239   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
    240                DeclarationName Name, TemplateParameterList *Params)
    241     : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(Params) { }
    242 
    243   // Construct a template decl with name, parameters, and templated element.
    244   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
    245                DeclarationName Name, TemplateParameterList *Params,
    246                NamedDecl *Decl)
    247     : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl),
    248       TemplateParams(Params) { }
    249 public:
    250   /// Get the list of template parameters
    251   TemplateParameterList *getTemplateParameters() const {
    252     return TemplateParams;
    253   }
    254 
    255   /// Get the underlying, templated declaration.
    256   NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
    257 
    258   // Implement isa/cast/dyncast/etc.
    259   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
    260   static bool classofKind(Kind K) {
    261     return K >= firstTemplate && K <= lastTemplate;
    262   }
    263 
    264   SourceRange getSourceRange() const LLVM_READONLY {
    265     return SourceRange(TemplateParams->getTemplateLoc(),
    266                        TemplatedDecl->getSourceRange().getEnd());
    267   }
    268 
    269 protected:
    270   NamedDecl *TemplatedDecl;
    271   TemplateParameterList* TemplateParams;
    272 
    273 public:
    274   /// \brief Initialize the underlying templated declaration and
    275   /// template parameters.
    276   void init(NamedDecl *templatedDecl, TemplateParameterList* templateParams) {
    277     assert(TemplatedDecl == 0 && "TemplatedDecl already set!");
    278     assert(TemplateParams == 0 && "TemplateParams already set!");
    279     TemplatedDecl = templatedDecl;
    280     TemplateParams = templateParams;
    281   }
    282 };
    283 
    284 /// \brief Provides information about a function template specialization,
    285 /// which is a FunctionDecl that has been explicitly specialization or
    286 /// instantiated from a function template.
    287 class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode {
    288   FunctionTemplateSpecializationInfo(FunctionDecl *FD,
    289                                      FunctionTemplateDecl *Template,
    290                                      TemplateSpecializationKind TSK,
    291                                      const TemplateArgumentList *TemplateArgs,
    292                        const ASTTemplateArgumentListInfo *TemplateArgsAsWritten,
    293                                      SourceLocation POI)
    294   : Function(FD),
    295     Template(Template, TSK - 1),
    296     TemplateArguments(TemplateArgs),
    297     TemplateArgumentsAsWritten(TemplateArgsAsWritten),
    298     PointOfInstantiation(POI) { }
    299 
    300 public:
    301   static FunctionTemplateSpecializationInfo *
    302   Create(ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
    303          TemplateSpecializationKind TSK,
    304          const TemplateArgumentList *TemplateArgs,
    305          const TemplateArgumentListInfo *TemplateArgsAsWritten,
    306          SourceLocation POI);
    307 
    308   /// \brief The function template specialization that this structure
    309   /// describes.
    310   FunctionDecl *Function;
    311 
    312   /// \brief The function template from which this function template
    313   /// specialization was generated.
    314   ///
    315   /// The two bits are contain the top 4 values of TemplateSpecializationKind.
    316   llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
    317 
    318   /// \brief The template arguments used to produce the function template
    319   /// specialization from the function template.
    320   const TemplateArgumentList *TemplateArguments;
    321 
    322   /// \brief The template arguments as written in the sources, if provided.
    323   const ASTTemplateArgumentListInfo *TemplateArgumentsAsWritten;
    324 
    325   /// \brief The point at which this function template specialization was
    326   /// first instantiated.
    327   SourceLocation PointOfInstantiation;
    328 
    329   /// \brief Retrieve the template from which this function was specialized.
    330   FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
    331 
    332   /// \brief Determine what kind of template specialization this is.
    333   TemplateSpecializationKind getTemplateSpecializationKind() const {
    334     return (TemplateSpecializationKind)(Template.getInt() + 1);
    335   }
    336 
    337   bool isExplicitSpecialization() const {
    338     return getTemplateSpecializationKind() == TSK_ExplicitSpecialization;
    339   }
    340 
    341   /// \brief True if this declaration is an explicit specialization,
    342   /// explicit instantiation declaration, or explicit instantiation
    343   /// definition.
    344   bool isExplicitInstantiationOrSpecialization() const {
    345     switch (getTemplateSpecializationKind()) {
    346     case TSK_ExplicitSpecialization:
    347     case TSK_ExplicitInstantiationDeclaration:
    348     case TSK_ExplicitInstantiationDefinition:
    349       return true;
    350 
    351     case TSK_Undeclared:
    352     case TSK_ImplicitInstantiation:
    353       return false;
    354     }
    355     llvm_unreachable("bad template specialization kind");
    356   }
    357 
    358   /// \brief Set the template specialization kind.
    359   void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
    360     assert(TSK != TSK_Undeclared &&
    361          "Cannot encode TSK_Undeclared for a function template specialization");
    362     Template.setInt(TSK - 1);
    363   }
    364 
    365   /// \brief Retrieve the first point of instantiation of this function
    366   /// template specialization.
    367   ///
    368   /// The point of instantiation may be an invalid source location if this
    369   /// function has yet to be instantiated.
    370   SourceLocation getPointOfInstantiation() const {
    371     return PointOfInstantiation;
    372   }
    373 
    374   /// \brief Set the (first) point of instantiation of this function template
    375   /// specialization.
    376   void setPointOfInstantiation(SourceLocation POI) {
    377     PointOfInstantiation = POI;
    378   }
    379 
    380   void Profile(llvm::FoldingSetNodeID &ID) {
    381     Profile(ID, TemplateArguments->data(),
    382             TemplateArguments->size(),
    383             Function->getASTContext());
    384   }
    385 
    386   static void
    387   Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
    388           unsigned NumTemplateArgs, ASTContext &Context) {
    389     ID.AddInteger(NumTemplateArgs);
    390     for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
    391       TemplateArgs[Arg].Profile(ID, Context);
    392   }
    393 };
    394 
    395 /// \brief Provides information a specialization of a member of a class
    396 /// template, which may be a member function, static data member,
    397 /// member class or member enumeration.
    398 class MemberSpecializationInfo {
    399   // The member declaration from which this member was instantiated, and the
    400   // manner in which the instantiation occurred (in the lower two bits).
    401   llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
    402 
    403   // The point at which this member was first instantiated.
    404   SourceLocation PointOfInstantiation;
    405 
    406 public:
    407   explicit
    408   MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK,
    409                            SourceLocation POI = SourceLocation())
    410     : MemberAndTSK(IF, TSK - 1), PointOfInstantiation(POI) {
    411     assert(TSK != TSK_Undeclared &&
    412            "Cannot encode undeclared template specializations for members");
    413   }
    414 
    415   /// \brief Retrieve the member declaration from which this member was
    416   /// instantiated.
    417   NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
    418 
    419   /// \brief Determine what kind of template specialization this is.
    420   TemplateSpecializationKind getTemplateSpecializationKind() const {
    421     return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
    422   }
    423 
    424   bool isExplicitSpecialization() const {
    425     return getTemplateSpecializationKind() == TSK_ExplicitSpecialization;
    426   }
    427 
    428   /// \brief Set the template specialization kind.
    429   void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
    430     assert(TSK != TSK_Undeclared &&
    431            "Cannot encode undeclared template specializations for members");
    432     MemberAndTSK.setInt(TSK - 1);
    433   }
    434 
    435   /// \brief Retrieve the first point of instantiation of this member.
    436   /// If the point of instantiation is an invalid location, then this member
    437   /// has not yet been instantiated.
    438   SourceLocation getPointOfInstantiation() const {
    439     return PointOfInstantiation;
    440   }
    441 
    442   /// \brief Set the first point of instantiation.
    443   void setPointOfInstantiation(SourceLocation POI) {
    444     PointOfInstantiation = POI;
    445   }
    446 };
    447 
    448 /// \brief Provides information about a dependent function-template
    449 /// specialization declaration.
    450 ///
    451 /// Since explicit function template specialization and instantiation
    452 /// declarations can only appear in namespace scope, and you can only
    453 /// specialize a member of a fully-specialized class, the only way to
    454 /// get one of these is in a friend declaration like the following:
    455 ///
    456 /// \code
    457 ///   template \<class T> void foo(T);
    458 ///   template \<class T> class A {
    459 ///     friend void foo<>(T);
    460 ///   };
    461 /// \endcode
    462 class DependentFunctionTemplateSpecializationInfo {
    463   struct CA {
    464     /// The number of potential template candidates.
    465     unsigned NumTemplates;
    466 
    467     /// The number of template arguments.
    468     unsigned NumArgs;
    469   };
    470 
    471   union {
    472     // Force sizeof to be a multiple of sizeof(void*) so that the
    473     // trailing data is aligned.
    474     void *Aligner;
    475     struct CA d;
    476   };
    477 
    478   /// The locations of the left and right angle brackets.
    479   SourceRange AngleLocs;
    480 
    481   FunctionTemplateDecl * const *getTemplates() const {
    482     return reinterpret_cast<FunctionTemplateDecl*const*>(this+1);
    483   }
    484 
    485 public:
    486   DependentFunctionTemplateSpecializationInfo(
    487                                  const UnresolvedSetImpl &Templates,
    488                                  const TemplateArgumentListInfo &TemplateArgs);
    489 
    490   /// \brief Returns the number of function templates that this might
    491   /// be a specialization of.
    492   unsigned getNumTemplates() const {
    493     return d.NumTemplates;
    494   }
    495 
    496   /// \brief Returns the i'th template candidate.
    497   FunctionTemplateDecl *getTemplate(unsigned I) const {
    498     assert(I < getNumTemplates() && "template index out of range");
    499     return getTemplates()[I];
    500   }
    501 
    502   /// \brief Returns the explicit template arguments that were given.
    503   const TemplateArgumentLoc *getTemplateArgs() const {
    504     return reinterpret_cast<const TemplateArgumentLoc*>(
    505                                             &getTemplates()[getNumTemplates()]);
    506   }
    507 
    508   /// \brief Returns the number of explicit template arguments that were given.
    509   unsigned getNumTemplateArgs() const {
    510     return d.NumArgs;
    511   }
    512 
    513   /// \brief Returns the nth template argument.
    514   const TemplateArgumentLoc &getTemplateArg(unsigned I) const {
    515     assert(I < getNumTemplateArgs() && "template arg index out of range");
    516     return getTemplateArgs()[I];
    517   }
    518 
    519   SourceLocation getLAngleLoc() const {
    520     return AngleLocs.getBegin();
    521   }
    522 
    523   SourceLocation getRAngleLoc() const {
    524     return AngleLocs.getEnd();
    525   }
    526 };
    527 
    528 /// Declaration of a redeclarable template.
    529 class RedeclarableTemplateDecl : public TemplateDecl,
    530                                  public Redeclarable<RedeclarableTemplateDecl>
    531 {
    532   typedef Redeclarable<RedeclarableTemplateDecl> redeclarable_base;
    533   virtual RedeclarableTemplateDecl *getNextRedeclaration() {
    534     return RedeclLink.getNext();
    535   }
    536   virtual RedeclarableTemplateDecl *getPreviousDeclImpl() {
    537     return getPreviousDecl();
    538   }
    539   virtual RedeclarableTemplateDecl *getMostRecentDeclImpl() {
    540     return getMostRecentDecl();
    541   }
    542 
    543 protected:
    544   template <typename EntryType> struct SpecEntryTraits {
    545     typedef EntryType DeclType;
    546 
    547     static DeclType *getMostRecentDecl(EntryType *D) {
    548       return D->getMostRecentDecl();
    549     }
    550   };
    551 
    552   template <typename EntryType,
    553             typename _SETraits = SpecEntryTraits<EntryType>,
    554             typename _DeclType = typename _SETraits::DeclType>
    555   class SpecIterator : public std::iterator<std::forward_iterator_tag,
    556                                             _DeclType*, ptrdiff_t,
    557                                             _DeclType*, _DeclType*> {
    558     typedef _SETraits SETraits;
    559     typedef _DeclType DeclType;
    560 
    561     typedef typename llvm::FoldingSetVector<EntryType>::iterator
    562       SetIteratorType;
    563 
    564     SetIteratorType SetIter;
    565 
    566   public:
    567     SpecIterator() : SetIter() {}
    568     SpecIterator(SetIteratorType SetIter) : SetIter(SetIter) {}
    569 
    570     DeclType *operator*() const {
    571       return SETraits::getMostRecentDecl(&*SetIter);
    572     }
    573     DeclType *operator->() const { return **this; }
    574 
    575     SpecIterator &operator++() { ++SetIter; return *this; }
    576     SpecIterator operator++(int) {
    577       SpecIterator tmp(*this);
    578       ++(*this);
    579       return tmp;
    580     }
    581 
    582     bool operator==(SpecIterator Other) const {
    583       return SetIter == Other.SetIter;
    584     }
    585     bool operator!=(SpecIterator Other) const {
    586       return SetIter != Other.SetIter;
    587     }
    588   };
    589 
    590   template <typename EntryType>
    591   static SpecIterator<EntryType>
    592   makeSpecIterator(llvm::FoldingSetVector<EntryType> &Specs, bool isEnd) {
    593     return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin());
    594   }
    595 
    596   template <class EntryType> typename SpecEntryTraits<EntryType>::DeclType*
    597   findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
    598                          const TemplateArgument *Args, unsigned NumArgs,
    599                          void *&InsertPos);
    600 
    601   struct CommonBase {
    602     CommonBase() : InstantiatedFromMember(0, false) { }
    603 
    604     /// \brief The template from which this was most
    605     /// directly instantiated (or null).
    606     ///
    607     /// The boolean value indicates whether this template
    608     /// was explicitly specialized.
    609     llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool>
    610       InstantiatedFromMember;
    611   };
    612 
    613   /// \brief Pointer to the common data shared by all declarations of this
    614   /// template.
    615   mutable CommonBase *Common;
    616 
    617   /// \brief Retrieves the "common" pointer shared by all (re-)declarations of
    618   /// the same template. Calling this routine may implicitly allocate memory
    619   /// for the common pointer.
    620   CommonBase *getCommonPtr() const;
    621 
    622   virtual CommonBase *newCommon(ASTContext &C) const = 0;
    623 
    624   // Construct a template decl with name, parameters, and templated element.
    625   RedeclarableTemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
    626                            DeclarationName Name, TemplateParameterList *Params,
    627                            NamedDecl *Decl)
    628     : TemplateDecl(DK, DC, L, Name, Params, Decl), Common() { }
    629 
    630 public:
    631   template <class decl_type> friend class RedeclarableTemplate;
    632 
    633   /// \brief Retrieves the canonical declaration of this template.
    634   RedeclarableTemplateDecl *getCanonicalDecl() { return getFirstDeclaration(); }
    635   const RedeclarableTemplateDecl *getCanonicalDecl() const {
    636     return getFirstDeclaration();
    637   }
    638 
    639   /// \brief Determines whether this template was a specialization of a
    640   /// member template.
    641   ///
    642   /// In the following example, the function template \c X<int>::f and the
    643   /// member template \c X<int>::Inner are member specializations.
    644   ///
    645   /// \code
    646   /// template<typename T>
    647   /// struct X {
    648   ///   template<typename U> void f(T, U);
    649   ///   template<typename U> struct Inner;
    650   /// };
    651   ///
    652   /// template<> template<typename T>
    653   /// void X<int>::f(int, T);
    654   /// template<> template<typename T>
    655   /// struct X<int>::Inner { /* ... */ };
    656   /// \endcode
    657   bool isMemberSpecialization() const {
    658     return getCommonPtr()->InstantiatedFromMember.getInt();
    659   }
    660 
    661   /// \brief Note that this member template is a specialization.
    662   void setMemberSpecialization() {
    663     assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
    664            "Only member templates can be member template specializations");
    665     getCommonPtr()->InstantiatedFromMember.setInt(true);
    666   }
    667 
    668   /// \brief Retrieve the member template from which this template was
    669   /// instantiated, or NULL if this template was not instantiated from a
    670   /// member template.
    671   ///
    672   /// A template is instantiated from a member template when the member
    673   /// template itself is part of a class template (or member thereof). For
    674   /// example, given
    675   ///
    676   /// \code
    677   /// template<typename T>
    678   /// struct X {
    679   ///   template<typename U> void f(T, U);
    680   /// };
    681   ///
    682   /// void test(X<int> x) {
    683   ///   x.f(1, 'a');
    684   /// };
    685   /// \endcode
    686   ///
    687   /// \c X<int>::f is a FunctionTemplateDecl that describes the function
    688   /// template
    689   ///
    690   /// \code
    691   /// template<typename U> void X<int>::f(int, U);
    692   /// \endcode
    693   ///
    694   /// which was itself created during the instantiation of \c X<int>. Calling
    695   /// getInstantiatedFromMemberTemplate() on this FunctionTemplateDecl will
    696   /// retrieve the FunctionTemplateDecl for the original template \c f within
    697   /// the class template \c X<T>, i.e.,
    698   ///
    699   /// \code
    700   /// template<typename T>
    701   /// template<typename U>
    702   /// void X<T>::f(T, U);
    703   /// \endcode
    704   RedeclarableTemplateDecl *getInstantiatedFromMemberTemplate() const {
    705     return getCommonPtr()->InstantiatedFromMember.getPointer();
    706   }
    707 
    708   void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD) {
    709     assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
    710     getCommonPtr()->InstantiatedFromMember.setPointer(TD);
    711   }
    712 
    713   typedef redeclarable_base::redecl_iterator redecl_iterator;
    714   using redeclarable_base::redecls_begin;
    715   using redeclarable_base::redecls_end;
    716   using redeclarable_base::getPreviousDecl;
    717   using redeclarable_base::getMostRecentDecl;
    718 
    719   // Implement isa/cast/dyncast/etc.
    720   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
    721   static bool classofKind(Kind K) {
    722     return K >= firstRedeclarableTemplate && K <= lastRedeclarableTemplate;
    723   }
    724 
    725   friend class ASTReader;
    726   friend class ASTDeclReader;
    727   friend class ASTDeclWriter;
    728 };
    729 
    730 template <> struct RedeclarableTemplateDecl::
    731 SpecEntryTraits<FunctionTemplateSpecializationInfo> {
    732   typedef FunctionDecl DeclType;
    733 
    734   static DeclType *
    735   getMostRecentDecl(FunctionTemplateSpecializationInfo *I) {
    736     return I->Function->getMostRecentDecl();
    737   }
    738 };
    739 
    740 /// Declaration of a template function.
    741 class FunctionTemplateDecl : public RedeclarableTemplateDecl {
    742   static void DeallocateCommon(void *Ptr);
    743 
    744 protected:
    745   /// \brief Data that is common to all of the declarations of a given
    746   /// function template.
    747   struct Common : CommonBase {
    748     Common() : InjectedArgs(), LazySpecializations() { }
    749 
    750     /// \brief The function template specializations for this function
    751     /// template, including explicit specializations and instantiations.
    752     llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> Specializations;
    753 
    754     /// \brief The set of "injected" template arguments used within this
    755     /// function template.
    756     ///
    757     /// This pointer refers to the template arguments (there are as
    758     /// many template arguments as template parameaters) for the function
    759     /// template, and is allocated lazily, since most function templates do not
    760     /// require the use of this information.
    761     TemplateArgument *InjectedArgs;
    762 
    763     /// \brief If non-null, points to an array of specializations known only
    764     /// by their external declaration IDs.
    765     ///
    766     /// The first value in the array is the number of of specializations
    767     /// that follow.
    768     uint32_t *LazySpecializations;
    769   };
    770 
    771   FunctionTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
    772                        TemplateParameterList *Params, NamedDecl *Decl)
    773     : RedeclarableTemplateDecl(FunctionTemplate, DC, L, Name, Params, Decl) { }
    774 
    775   CommonBase *newCommon(ASTContext &C) const;
    776 
    777   Common *getCommonPtr() const {
    778     return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
    779   }
    780 
    781   friend class FunctionDecl;
    782 
    783   /// \brief Load any lazily-loaded specializations from the external source.
    784   void LoadLazySpecializations() const;
    785 
    786   /// \brief Retrieve the set of function template specializations of this
    787   /// function template.
    788   llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
    789   getSpecializations() const;
    790 
    791   /// \brief Add a specialization of this function template.
    792   ///
    793   /// \param InsertPos Insert position in the FoldingSetVector, must have been
    794   ///        retrieved by an earlier call to findSpecialization().
    795   void addSpecialization(FunctionTemplateSpecializationInfo* Info,
    796                          void *InsertPos);
    797 
    798 public:
    799   /// Get the underlying function declaration of the template.
    800   FunctionDecl *getTemplatedDecl() const {
    801     return static_cast<FunctionDecl*>(TemplatedDecl);
    802   }
    803 
    804   /// Returns whether this template declaration defines the primary
    805   /// pattern.
    806   bool isThisDeclarationADefinition() const {
    807     return getTemplatedDecl()->isThisDeclarationADefinition();
    808   }
    809 
    810   /// \brief Return the specialization with the provided arguments if it exists,
    811   /// otherwise return the insertion point.
    812   FunctionDecl *findSpecialization(const TemplateArgument *Args,
    813                                    unsigned NumArgs, void *&InsertPos);
    814 
    815   FunctionTemplateDecl *getCanonicalDecl() {
    816     return cast<FunctionTemplateDecl>(
    817              RedeclarableTemplateDecl::getCanonicalDecl());
    818   }
    819   const FunctionTemplateDecl *getCanonicalDecl() const {
    820     return cast<FunctionTemplateDecl>(
    821              RedeclarableTemplateDecl::getCanonicalDecl());
    822   }
    823 
    824   /// \brief Retrieve the previous declaration of this function template, or
    825   /// NULL if no such declaration exists.
    826   FunctionTemplateDecl *getPreviousDecl() {
    827     return cast_or_null<FunctionTemplateDecl>(
    828              RedeclarableTemplateDecl::getPreviousDecl());
    829   }
    830 
    831   /// \brief Retrieve the previous declaration of this function template, or
    832   /// NULL if no such declaration exists.
    833   const FunctionTemplateDecl *getPreviousDecl() const {
    834     return cast_or_null<FunctionTemplateDecl>(
    835              RedeclarableTemplateDecl::getPreviousDecl());
    836   }
    837 
    838   FunctionTemplateDecl *getInstantiatedFromMemberTemplate() {
    839     return cast_or_null<FunctionTemplateDecl>(
    840              RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
    841   }
    842 
    843   typedef SpecIterator<FunctionTemplateSpecializationInfo> spec_iterator;
    844 
    845   spec_iterator spec_begin() const {
    846     return makeSpecIterator(getSpecializations(), false);
    847   }
    848 
    849   spec_iterator spec_end() const {
    850     return makeSpecIterator(getSpecializations(), true);
    851   }
    852 
    853   /// \brief Retrieve the "injected" template arguments that correspond to the
    854   /// template parameters of this function template.
    855   ///
    856   /// Although the C++ standard has no notion of the "injected" template
    857   /// arguments for a function template, the notion is convenient when
    858   /// we need to perform substitutions inside the definition of a function
    859   /// template.
    860   ArrayRef<TemplateArgument> getInjectedTemplateArgs();
    861 
    862   /// \brief Create a function template node.
    863   static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
    864                                       SourceLocation L,
    865                                       DeclarationName Name,
    866                                       TemplateParameterList *Params,
    867                                       NamedDecl *Decl);
    868 
    869   /// \brief Create an empty function template node.
    870   static FunctionTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
    871 
    872   // Implement isa/cast/dyncast support
    873   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
    874   static bool classofKind(Kind K) { return K == FunctionTemplate; }
    875 
    876   friend class ASTDeclReader;
    877   friend class ASTDeclWriter;
    878 };
    879 
    880 //===----------------------------------------------------------------------===//
    881 // Kinds of Template Parameters
    882 //===----------------------------------------------------------------------===//
    883 
    884 /// \brief Defines the position of a template parameter within a template
    885 /// parameter list.
    886 ///
    887 /// Because template parameter can be listed
    888 /// sequentially for out-of-line template members, each template parameter is
    889 /// given a Depth - the nesting of template parameter scopes - and a Position -
    890 /// the occurrence within the parameter list.
    891 /// This class is inheritedly privately by different kinds of template
    892 /// parameters and is not part of the Decl hierarchy. Just a facility.
    893 class TemplateParmPosition {
    894 protected:
    895   // FIXME: This should probably never be called, but it's here as
    896   TemplateParmPosition()
    897     : Depth(0), Position(0)
    898   { /* llvm_unreachable("Cannot create positionless template parameter"); */ }
    899 
    900   TemplateParmPosition(unsigned D, unsigned P)
    901     : Depth(D), Position(P)
    902   { }
    903 
    904   // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
    905   // position? Maybe?
    906   unsigned Depth;
    907   unsigned Position;
    908 
    909 public:
    910   /// Get the nesting depth of the template parameter.
    911   unsigned getDepth() const { return Depth; }
    912   void setDepth(unsigned D) { Depth = D; }
    913 
    914   /// Get the position of the template parameter within its parameter list.
    915   unsigned getPosition() const { return Position; }
    916   void setPosition(unsigned P) { Position = P; }
    917 
    918   /// Get the index of the template parameter within its parameter list.
    919   unsigned getIndex() const { return Position; }
    920 };
    921 
    922 /// \brief Declaration of a template type parameter.
    923 ///
    924 /// For example, "T" in
    925 /// \code
    926 /// template<typename T> class vector;
    927 /// \endcode
    928 class TemplateTypeParmDecl : public TypeDecl {
    929   /// \brief Whether this template type parameter was declaration with
    930   /// the 'typename' keyword.
    931   ///
    932   /// If false, it was declared with the 'class' keyword.
    933   bool Typename : 1;
    934 
    935   /// \brief Whether this template type parameter inherited its
    936   /// default argument.
    937   bool InheritedDefault : 1;
    938 
    939   /// \brief The default template argument, if any.
    940   TypeSourceInfo *DefaultArgument;
    941 
    942   TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
    943                        SourceLocation IdLoc, IdentifierInfo *Id,
    944                        bool Typename)
    945     : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename),
    946       InheritedDefault(false), DefaultArgument() { }
    947 
    948   /// Sema creates these on the stack during auto type deduction.
    949   friend class Sema;
    950 
    951 public:
    952   static TemplateTypeParmDecl *Create(const ASTContext &C, DeclContext *DC,
    953                                       SourceLocation KeyLoc,
    954                                       SourceLocation NameLoc,
    955                                       unsigned D, unsigned P,
    956                                       IdentifierInfo *Id, bool Typename,
    957                                       bool ParameterPack);
    958   static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
    959                                                   unsigned ID);
    960 
    961   /// \brief Whether this template type parameter was declared with
    962   /// the 'typename' keyword.
    963   ///
    964   /// If not, it was declared with the 'class' keyword.
    965   bool wasDeclaredWithTypename() const { return Typename; }
    966 
    967   /// \brief Determine whether this template parameter has a default
    968   /// argument.
    969   bool hasDefaultArgument() const { return DefaultArgument != 0; }
    970 
    971   /// \brief Retrieve the default argument, if any.
    972   QualType getDefaultArgument() const { return DefaultArgument->getType(); }
    973 
    974   /// \brief Retrieves the default argument's source information, if any.
    975   TypeSourceInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
    976 
    977   /// \brief Retrieves the location of the default argument declaration.
    978   SourceLocation getDefaultArgumentLoc() const;
    979 
    980   /// \brief Determines whether the default argument was inherited
    981   /// from a previous declaration of this template.
    982   bool defaultArgumentWasInherited() const { return InheritedDefault; }
    983 
    984   /// \brief Set the default argument for this template parameter, and
    985   /// whether that default argument was inherited from another
    986   /// declaration.
    987   void setDefaultArgument(TypeSourceInfo *DefArg, bool Inherited) {
    988     DefaultArgument = DefArg;
    989     InheritedDefault = Inherited;
    990   }
    991 
    992   /// \brief Removes the default argument of this template parameter.
    993   void removeDefaultArgument() {
    994     DefaultArgument = 0;
    995     InheritedDefault = false;
    996   }
    997 
    998   /// \brief Set whether this template type parameter was declared with
    999   /// the 'typename' or 'class' keyword.
   1000   void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; }
   1001 
   1002   /// \brief Retrieve the depth of the template parameter.
   1003   unsigned getDepth() const;
   1004 
   1005   /// \brief Retrieve the index of the template parameter.
   1006   unsigned getIndex() const;
   1007 
   1008   /// \brief Returns whether this is a parameter pack.
   1009   bool isParameterPack() const;
   1010 
   1011   SourceRange getSourceRange() const LLVM_READONLY;
   1012 
   1013   // Implement isa/cast/dyncast/etc.
   1014   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   1015   static bool classofKind(Kind K) { return K == TemplateTypeParm; }
   1016 };
   1017 
   1018 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
   1019 /// e.g., "Size" in
   1020 /// @code
   1021 /// template<int Size> class array { };
   1022 /// @endcode
   1023 class NonTypeTemplateParmDecl
   1024   : public DeclaratorDecl, protected TemplateParmPosition {
   1025   /// \brief The default template argument, if any, and whether or not
   1026   /// it was inherited.
   1027   llvm::PointerIntPair<Expr*, 1, bool> DefaultArgumentAndInherited;
   1028 
   1029   // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index
   1030   // down here to save memory.
   1031 
   1032   /// \brief Whether this non-type template parameter is a parameter pack.
   1033   bool ParameterPack;
   1034 
   1035   /// \brief Whether this non-type template parameter is an "expanded"
   1036   /// parameter pack, meaning that its type is a pack expansion and we
   1037   /// already know the set of types that expansion expands to.
   1038   bool ExpandedParameterPack;
   1039 
   1040   /// \brief The number of types in an expanded parameter pack.
   1041   unsigned NumExpandedTypes;
   1042 
   1043   NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
   1044                           SourceLocation IdLoc, unsigned D, unsigned P,
   1045                           IdentifierInfo *Id, QualType T,
   1046                           bool ParameterPack, TypeSourceInfo *TInfo)
   1047     : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
   1048       TemplateParmPosition(D, P), DefaultArgumentAndInherited(0, false),
   1049       ParameterPack(ParameterPack), ExpandedParameterPack(false),
   1050       NumExpandedTypes(0)
   1051   { }
   1052 
   1053   NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
   1054                           SourceLocation IdLoc, unsigned D, unsigned P,
   1055                           IdentifierInfo *Id, QualType T,
   1056                           TypeSourceInfo *TInfo,
   1057                           const QualType *ExpandedTypes,
   1058                           unsigned NumExpandedTypes,
   1059                           TypeSourceInfo **ExpandedTInfos);
   1060 
   1061   friend class ASTDeclReader;
   1062 
   1063 public:
   1064   static NonTypeTemplateParmDecl *
   1065   Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
   1066          SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
   1067          QualType T, bool ParameterPack, TypeSourceInfo *TInfo);
   1068 
   1069   static NonTypeTemplateParmDecl *
   1070   Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
   1071          SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
   1072          QualType T, TypeSourceInfo *TInfo,
   1073          const QualType *ExpandedTypes, unsigned NumExpandedTypes,
   1074          TypeSourceInfo **ExpandedTInfos);
   1075 
   1076   static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
   1077                                                      unsigned ID);
   1078   static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
   1079                                                      unsigned ID,
   1080                                                      unsigned NumExpandedTypes);
   1081 
   1082   using TemplateParmPosition::getDepth;
   1083   using TemplateParmPosition::setDepth;
   1084   using TemplateParmPosition::getPosition;
   1085   using TemplateParmPosition::setPosition;
   1086   using TemplateParmPosition::getIndex;
   1087 
   1088   SourceRange getSourceRange() const LLVM_READONLY;
   1089 
   1090   /// \brief Determine whether this template parameter has a default
   1091   /// argument.
   1092   bool hasDefaultArgument() const {
   1093     return DefaultArgumentAndInherited.getPointer() != 0;
   1094   }
   1095 
   1096   /// \brief Retrieve the default argument, if any.
   1097   Expr *getDefaultArgument() const {
   1098     return DefaultArgumentAndInherited.getPointer();
   1099   }
   1100 
   1101   /// \brief Retrieve the location of the default argument, if any.
   1102   SourceLocation getDefaultArgumentLoc() const;
   1103 
   1104   /// \brief Determines whether the default argument was inherited
   1105   /// from a previous declaration of this template.
   1106   bool defaultArgumentWasInherited() const {
   1107     return DefaultArgumentAndInherited.getInt();
   1108   }
   1109 
   1110   /// \brief Set the default argument for this template parameter, and
   1111   /// whether that default argument was inherited from another
   1112   /// declaration.
   1113   void setDefaultArgument(Expr *DefArg, bool Inherited) {
   1114     DefaultArgumentAndInherited.setPointer(DefArg);
   1115     DefaultArgumentAndInherited.setInt(Inherited);
   1116   }
   1117 
   1118   /// \brief Removes the default argument of this template parameter.
   1119   void removeDefaultArgument() {
   1120     DefaultArgumentAndInherited.setPointer(0);
   1121     DefaultArgumentAndInherited.setInt(false);
   1122   }
   1123 
   1124   /// \brief Whether this parameter is a non-type template parameter pack.
   1125   ///
   1126   /// If the parameter is a parameter pack, the type may be a
   1127   /// \c PackExpansionType. In the following example, the \c Dims parameter
   1128   /// is a parameter pack (whose type is 'unsigned').
   1129   ///
   1130   /// \code
   1131   /// template<typename T, unsigned ...Dims> struct multi_array;
   1132   /// \endcode
   1133   bool isParameterPack() const { return ParameterPack; }
   1134 
   1135   /// \brief Whether this parameter pack is a pack expansion.
   1136   ///
   1137   /// A non-type template parameter pack is a pack expansion if its type
   1138   /// contains an unexpanded parameter pack. In this case, we will have
   1139   /// built a PackExpansionType wrapping the type.
   1140   bool isPackExpansion() const {
   1141     return ParameterPack && getType()->getAs<PackExpansionType>();
   1142   }
   1143 
   1144   /// \brief Whether this parameter is a non-type template parameter pack
   1145   /// that has a known list of different types at different positions.
   1146   ///
   1147   /// A parameter pack is an expanded parameter pack when the original
   1148   /// parameter pack's type was itself a pack expansion, and that expansion
   1149   /// has already been expanded. For example, given:
   1150   ///
   1151   /// \code
   1152   /// template<typename ...Types>
   1153   /// struct X {
   1154   ///   template<Types ...Values>
   1155   ///   struct Y { /* ... */ };
   1156   /// };
   1157   /// \endcode
   1158   ///
   1159   /// The parameter pack \c Values has a \c PackExpansionType as its type,
   1160   /// which expands \c Types. When \c Types is supplied with template arguments
   1161   /// by instantiating \c X, the instantiation of \c Values becomes an
   1162   /// expanded parameter pack. For example, instantiating
   1163   /// \c X<int, unsigned int> results in \c Values being an expanded parameter
   1164   /// pack with expansion types \c int and \c unsigned int.
   1165   ///
   1166   /// The \c getExpansionType() and \c getExpansionTypeSourceInfo() functions
   1167   /// return the expansion types.
   1168   bool isExpandedParameterPack() const { return ExpandedParameterPack; }
   1169 
   1170   /// \brief Retrieves the number of expansion types in an expanded parameter
   1171   /// pack.
   1172   unsigned getNumExpansionTypes() const {
   1173     assert(ExpandedParameterPack && "Not an expansion parameter pack");
   1174     return NumExpandedTypes;
   1175   }
   1176 
   1177   /// \brief Retrieve a particular expansion type within an expanded parameter
   1178   /// pack.
   1179   QualType getExpansionType(unsigned I) const {
   1180     assert(I < NumExpandedTypes && "Out-of-range expansion type index");
   1181     void * const *TypesAndInfos = reinterpret_cast<void * const*>(this + 1);
   1182     return QualType::getFromOpaquePtr(TypesAndInfos[2*I]);
   1183   }
   1184 
   1185   /// \brief Retrieve a particular expansion type source info within an
   1186   /// expanded parameter pack.
   1187   TypeSourceInfo *getExpansionTypeSourceInfo(unsigned I) const {
   1188     assert(I < NumExpandedTypes && "Out-of-range expansion type index");
   1189     void * const *TypesAndInfos = reinterpret_cast<void * const*>(this + 1);
   1190     return static_cast<TypeSourceInfo *>(TypesAndInfos[2*I+1]);
   1191   }
   1192 
   1193   // Implement isa/cast/dyncast/etc.
   1194   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   1195   static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
   1196 };
   1197 
   1198 /// TemplateTemplateParmDecl - Declares a template template parameter,
   1199 /// e.g., "T" in
   1200 /// @code
   1201 /// template <template <typename> class T> class container { };
   1202 /// @endcode
   1203 /// A template template parameter is a TemplateDecl because it defines the
   1204 /// name of a template and the template parameters allowable for substitution.
   1205 class TemplateTemplateParmDecl : public TemplateDecl,
   1206                                  protected TemplateParmPosition
   1207 {
   1208   virtual void anchor();
   1209 
   1210   /// DefaultArgument - The default template argument, if any.
   1211   TemplateArgumentLoc DefaultArgument;
   1212   /// Whether or not the default argument was inherited.
   1213   bool DefaultArgumentWasInherited;
   1214 
   1215   /// \brief Whether this parameter is a parameter pack.
   1216   bool ParameterPack;
   1217 
   1218   /// \brief Whether this template template parameter is an "expanded"
   1219   /// parameter pack, meaning that it is a pack expansion and we
   1220   /// already know the set of template parameters that expansion expands to.
   1221   bool ExpandedParameterPack;
   1222 
   1223   /// \brief The number of parameters in an expanded parameter pack.
   1224   unsigned NumExpandedParams;
   1225 
   1226   TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
   1227                            unsigned D, unsigned P, bool ParameterPack,
   1228                            IdentifierInfo *Id, TemplateParameterList *Params)
   1229     : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
   1230       TemplateParmPosition(D, P), DefaultArgument(),
   1231       DefaultArgumentWasInherited(false), ParameterPack(ParameterPack),
   1232       ExpandedParameterPack(false), NumExpandedParams(0)
   1233     { }
   1234 
   1235   TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
   1236                            unsigned D, unsigned P,
   1237                            IdentifierInfo *Id, TemplateParameterList *Params,
   1238                            unsigned NumExpansions,
   1239                            TemplateParameterList * const *Expansions);
   1240 
   1241 public:
   1242   static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
   1243                                           SourceLocation L, unsigned D,
   1244                                           unsigned P, bool ParameterPack,
   1245                                           IdentifierInfo *Id,
   1246                                           TemplateParameterList *Params);
   1247   static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
   1248                                           SourceLocation L, unsigned D,
   1249                                           unsigned P,
   1250                                           IdentifierInfo *Id,
   1251                                           TemplateParameterList *Params,
   1252                                  ArrayRef<TemplateParameterList *> Expansions);
   1253 
   1254   static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
   1255                                                       unsigned ID);
   1256   static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
   1257                                                       unsigned ID,
   1258                                                       unsigned NumExpansions);
   1259 
   1260   using TemplateParmPosition::getDepth;
   1261   using TemplateParmPosition::getPosition;
   1262   using TemplateParmPosition::getIndex;
   1263 
   1264   /// \brief Whether this template template parameter is a template
   1265   /// parameter pack.
   1266   ///
   1267   /// \code
   1268   /// template<template <class T> ...MetaFunctions> struct Apply;
   1269   /// \endcode
   1270   bool isParameterPack() const { return ParameterPack; }
   1271 
   1272   /// \brief Whether this parameter pack is a pack expansion.
   1273   ///
   1274   /// A template template parameter pack is a pack expansion if its template
   1275   /// parameter list contains an unexpanded parameter pack.
   1276   bool isPackExpansion() const {
   1277     return ParameterPack &&
   1278            getTemplateParameters()->containsUnexpandedParameterPack();
   1279   }
   1280 
   1281   /// \brief Whether this parameter is a template template parameter pack that
   1282   /// has a known list of different template parameter lists at different
   1283   /// positions.
   1284   ///
   1285   /// A parameter pack is an expanded parameter pack when the original parameter
   1286   /// pack's template parameter list was itself a pack expansion, and that
   1287   /// expansion has already been expanded. For exampe, given:
   1288   ///
   1289   /// \code
   1290   /// template<typename...Types> struct Outer {
   1291   ///   template<template<Types> class...Templates> struct Inner;
   1292   /// };
   1293   /// \endcode
   1294   ///
   1295   /// The parameter pack \c Templates is a pack expansion, which expands the
   1296   /// pack \c Types. When \c Types is supplied with template arguments by
   1297   /// instantiating \c Outer, the instantiation of \c Templates is an expanded
   1298   /// parameter pack.
   1299   bool isExpandedParameterPack() const { return ExpandedParameterPack; }
   1300 
   1301   /// \brief Retrieves the number of expansion template parameters in
   1302   /// an expanded parameter pack.
   1303   unsigned getNumExpansionTemplateParameters() const {
   1304     assert(ExpandedParameterPack && "Not an expansion parameter pack");
   1305     return NumExpandedParams;
   1306   }
   1307 
   1308   /// \brief Retrieve a particular expansion type within an expanded parameter
   1309   /// pack.
   1310   TemplateParameterList *getExpansionTemplateParameters(unsigned I) const {
   1311     assert(I < NumExpandedParams && "Out-of-range expansion type index");
   1312     return reinterpret_cast<TemplateParameterList *const *>(this + 1)[I];
   1313   }
   1314 
   1315   /// \brief Determine whether this template parameter has a default
   1316   /// argument.
   1317   bool hasDefaultArgument() const {
   1318     return !DefaultArgument.getArgument().isNull();
   1319   }
   1320 
   1321   /// \brief Retrieve the default argument, if any.
   1322   const TemplateArgumentLoc &getDefaultArgument() const {
   1323     return DefaultArgument;
   1324   }
   1325 
   1326   /// \brief Retrieve the location of the default argument, if any.
   1327   SourceLocation getDefaultArgumentLoc() const;
   1328 
   1329   /// \brief Determines whether the default argument was inherited
   1330   /// from a previous declaration of this template.
   1331   bool defaultArgumentWasInherited() const {
   1332     return DefaultArgumentWasInherited;
   1333   }
   1334 
   1335   /// \brief Set the default argument for this template parameter, and
   1336   /// whether that default argument was inherited from another
   1337   /// declaration.
   1338   void setDefaultArgument(const TemplateArgumentLoc &DefArg, bool Inherited) {
   1339     DefaultArgument = DefArg;
   1340     DefaultArgumentWasInherited = Inherited;
   1341   }
   1342 
   1343   /// \brief Removes the default argument of this template parameter.
   1344   void removeDefaultArgument() {
   1345     DefaultArgument = TemplateArgumentLoc();
   1346     DefaultArgumentWasInherited = false;
   1347   }
   1348 
   1349   SourceRange getSourceRange() const LLVM_READONLY {
   1350     SourceLocation End = getLocation();
   1351     if (hasDefaultArgument() && !defaultArgumentWasInherited())
   1352       End = getDefaultArgument().getSourceRange().getEnd();
   1353     return SourceRange(getTemplateParameters()->getTemplateLoc(), End);
   1354   }
   1355 
   1356   // Implement isa/cast/dyncast/etc.
   1357   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   1358   static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
   1359 
   1360   friend class ASTDeclReader;
   1361   friend class ASTDeclWriter;
   1362 };
   1363 
   1364 /// \brief Represents a class template specialization, which refers to
   1365 /// a class template with a given set of template arguments.
   1366 ///
   1367 /// Class template specializations represent both explicit
   1368 /// specialization of class templates, as in the example below, and
   1369 /// implicit instantiations of class templates.
   1370 ///
   1371 /// \code
   1372 /// template<typename T> class array;
   1373 ///
   1374 /// template<>
   1375 /// class array<bool> { }; // class template specialization array<bool>
   1376 /// \endcode
   1377 class ClassTemplateSpecializationDecl
   1378   : public CXXRecordDecl, public llvm::FoldingSetNode {
   1379 
   1380   /// \brief Structure that stores information about a class template
   1381   /// specialization that was instantiated from a class template partial
   1382   /// specialization.
   1383   struct SpecializedPartialSpecialization {
   1384     /// \brief The class template partial specialization from which this
   1385     /// class template specialization was instantiated.
   1386     ClassTemplatePartialSpecializationDecl *PartialSpecialization;
   1387 
   1388     /// \brief The template argument list deduced for the class template
   1389     /// partial specialization itself.
   1390     TemplateArgumentList *TemplateArgs;
   1391   };
   1392 
   1393   /// \brief The template that this specialization specializes
   1394   llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
   1395     SpecializedTemplate;
   1396 
   1397   /// \brief Further info for explicit template specialization/instantiation.
   1398   struct ExplicitSpecializationInfo {
   1399     /// \brief The type-as-written.
   1400     TypeSourceInfo *TypeAsWritten;
   1401     /// \brief The location of the extern keyword.
   1402     SourceLocation ExternLoc;
   1403     /// \brief The location of the template keyword.
   1404     SourceLocation TemplateKeywordLoc;
   1405 
   1406     ExplicitSpecializationInfo()
   1407       : TypeAsWritten(0), ExternLoc(), TemplateKeywordLoc() {}
   1408   };
   1409 
   1410   /// \brief Further info for explicit template specialization/instantiation.
   1411   /// Does not apply to implicit specializations.
   1412   ExplicitSpecializationInfo *ExplicitInfo;
   1413 
   1414   /// \brief The template arguments used to describe this specialization.
   1415   TemplateArgumentList *TemplateArgs;
   1416 
   1417   /// \brief The point where this template was instantiated (if any)
   1418   SourceLocation PointOfInstantiation;
   1419 
   1420   /// \brief The kind of specialization this declaration refers to.
   1421   /// Really a value of type TemplateSpecializationKind.
   1422   unsigned SpecializationKind : 3;
   1423 
   1424 protected:
   1425   ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
   1426                                   DeclContext *DC, SourceLocation StartLoc,
   1427                                   SourceLocation IdLoc,
   1428                                   ClassTemplateDecl *SpecializedTemplate,
   1429                                   const TemplateArgument *Args,
   1430                                   unsigned NumArgs,
   1431                                   ClassTemplateSpecializationDecl *PrevDecl);
   1432 
   1433   explicit ClassTemplateSpecializationDecl(Kind DK);
   1434 
   1435 public:
   1436   static ClassTemplateSpecializationDecl *
   1437   Create(ASTContext &Context, TagKind TK, DeclContext *DC,
   1438          SourceLocation StartLoc, SourceLocation IdLoc,
   1439          ClassTemplateDecl *SpecializedTemplate,
   1440          const TemplateArgument *Args,
   1441          unsigned NumArgs,
   1442          ClassTemplateSpecializationDecl *PrevDecl);
   1443   static ClassTemplateSpecializationDecl *
   1444   CreateDeserialized(ASTContext &C, unsigned ID);
   1445 
   1446   virtual void getNameForDiagnostic(raw_ostream &OS,
   1447                                     const PrintingPolicy &Policy,
   1448                                     bool Qualified) const;
   1449 
   1450   ClassTemplateSpecializationDecl *getMostRecentDecl() {
   1451     CXXRecordDecl *Recent
   1452         = cast<CXXRecordDecl>(CXXRecordDecl::getMostRecentDecl());
   1453     if (!isa<ClassTemplateSpecializationDecl>(Recent)) {
   1454       // FIXME: Does injected class name need to be in the redeclarations chain?
   1455       assert(Recent->isInjectedClassName() && Recent->getPreviousDecl());
   1456       Recent = Recent->getPreviousDecl();
   1457     }
   1458     return cast<ClassTemplateSpecializationDecl>(Recent);
   1459   }
   1460 
   1461   /// \brief Retrieve the template that this specialization specializes.
   1462   ClassTemplateDecl *getSpecializedTemplate() const;
   1463 
   1464   /// \brief Retrieve the template arguments of the class template
   1465   /// specialization.
   1466   const TemplateArgumentList &getTemplateArgs() const {
   1467     return *TemplateArgs;
   1468   }
   1469 
   1470   /// \brief Determine the kind of specialization that this
   1471   /// declaration represents.
   1472   TemplateSpecializationKind getSpecializationKind() const {
   1473     return static_cast<TemplateSpecializationKind>(SpecializationKind);
   1474   }
   1475 
   1476   bool isExplicitSpecialization() const {
   1477     return getSpecializationKind() == TSK_ExplicitSpecialization;
   1478   }
   1479 
   1480   /// \brief True if this declaration is an explicit specialization,
   1481   /// explicit instantiation declaration, or explicit instantiation
   1482   /// definition.
   1483   bool isExplicitInstantiationOrSpecialization() const {
   1484     switch (getTemplateSpecializationKind()) {
   1485     case TSK_ExplicitSpecialization:
   1486     case TSK_ExplicitInstantiationDeclaration:
   1487     case TSK_ExplicitInstantiationDefinition:
   1488       return true;
   1489 
   1490     case TSK_Undeclared:
   1491     case TSK_ImplicitInstantiation:
   1492       return false;
   1493     }
   1494     llvm_unreachable("bad template specialization kind");
   1495   }
   1496 
   1497   void setSpecializationKind(TemplateSpecializationKind TSK) {
   1498     SpecializationKind = TSK;
   1499   }
   1500 
   1501   /// \brief Get the point of instantiation (if any), or null if none.
   1502   SourceLocation getPointOfInstantiation() const {
   1503     return PointOfInstantiation;
   1504   }
   1505 
   1506   void setPointOfInstantiation(SourceLocation Loc) {
   1507     assert(Loc.isValid() && "point of instantiation must be valid!");
   1508     PointOfInstantiation = Loc;
   1509   }
   1510 
   1511   /// \brief If this class template specialization is an instantiation of
   1512   /// a template (rather than an explicit specialization), return the
   1513   /// class template or class template partial specialization from which it
   1514   /// was instantiated.
   1515   llvm::PointerUnion<ClassTemplateDecl *,
   1516                      ClassTemplatePartialSpecializationDecl *>
   1517   getInstantiatedFrom() const {
   1518     if (getSpecializationKind() != TSK_ImplicitInstantiation &&
   1519         getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
   1520         getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
   1521       return llvm::PointerUnion<ClassTemplateDecl *,
   1522                                 ClassTemplatePartialSpecializationDecl *>();
   1523 
   1524     if (SpecializedPartialSpecialization *PartialSpec
   1525           = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
   1526       return PartialSpec->PartialSpecialization;
   1527 
   1528     return SpecializedTemplate.get<ClassTemplateDecl*>();
   1529   }
   1530 
   1531   /// \brief Retrieve the class template or class template partial
   1532   /// specialization which was specialized by this.
   1533   llvm::PointerUnion<ClassTemplateDecl *,
   1534                      ClassTemplatePartialSpecializationDecl *>
   1535   getSpecializedTemplateOrPartial() const {
   1536     if (SpecializedPartialSpecialization *PartialSpec
   1537           = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
   1538       return PartialSpec->PartialSpecialization;
   1539 
   1540     return SpecializedTemplate.get<ClassTemplateDecl*>();
   1541   }
   1542 
   1543   /// \brief Retrieve the set of template arguments that should be used
   1544   /// to instantiate members of the class template or class template partial
   1545   /// specialization from which this class template specialization was
   1546   /// instantiated.
   1547   ///
   1548   /// \returns For a class template specialization instantiated from the primary
   1549   /// template, this function will return the same template arguments as
   1550   /// getTemplateArgs(). For a class template specialization instantiated from
   1551   /// a class template partial specialization, this function will return the
   1552   /// deduced template arguments for the class template partial specialization
   1553   /// itself.
   1554   const TemplateArgumentList &getTemplateInstantiationArgs() const {
   1555     if (SpecializedPartialSpecialization *PartialSpec
   1556         = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
   1557       return *PartialSpec->TemplateArgs;
   1558 
   1559     return getTemplateArgs();
   1560   }
   1561 
   1562   /// \brief Note that this class template specialization is actually an
   1563   /// instantiation of the given class template partial specialization whose
   1564   /// template arguments have been deduced.
   1565   void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
   1566                           TemplateArgumentList *TemplateArgs) {
   1567     assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
   1568            "Already set to a class template partial specialization!");
   1569     SpecializedPartialSpecialization *PS
   1570       = new (getASTContext()) SpecializedPartialSpecialization();
   1571     PS->PartialSpecialization = PartialSpec;
   1572     PS->TemplateArgs = TemplateArgs;
   1573     SpecializedTemplate = PS;
   1574   }
   1575 
   1576   /// \brief Note that this class template specialization is an instantiation
   1577   /// of the given class template.
   1578   void setInstantiationOf(ClassTemplateDecl *TemplDecl) {
   1579     assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
   1580            "Previously set to a class template partial specialization!");
   1581     SpecializedTemplate = TemplDecl;
   1582   }
   1583 
   1584   /// \brief Sets the type of this specialization as it was written by
   1585   /// the user. This will be a class template specialization type.
   1586   void setTypeAsWritten(TypeSourceInfo *T) {
   1587     if (!ExplicitInfo)
   1588       ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
   1589     ExplicitInfo->TypeAsWritten = T;
   1590   }
   1591   /// \brief Gets the type of this specialization as it was written by
   1592   /// the user, if it was so written.
   1593   TypeSourceInfo *getTypeAsWritten() const {
   1594     return ExplicitInfo ? ExplicitInfo->TypeAsWritten : 0;
   1595   }
   1596 
   1597   /// \brief Gets the location of the extern keyword, if present.
   1598   SourceLocation getExternLoc() const {
   1599     return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
   1600   }
   1601   /// \brief Sets the location of the extern keyword.
   1602   void setExternLoc(SourceLocation Loc) {
   1603     if (!ExplicitInfo)
   1604       ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
   1605     ExplicitInfo->ExternLoc = Loc;
   1606   }
   1607 
   1608   /// \brief Sets the location of the template keyword.
   1609   void setTemplateKeywordLoc(SourceLocation Loc) {
   1610     if (!ExplicitInfo)
   1611       ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
   1612     ExplicitInfo->TemplateKeywordLoc = Loc;
   1613   }
   1614   /// \brief Gets the location of the template keyword, if present.
   1615   SourceLocation getTemplateKeywordLoc() const {
   1616     return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
   1617   }
   1618 
   1619   SourceRange getSourceRange() const LLVM_READONLY;
   1620 
   1621   void Profile(llvm::FoldingSetNodeID &ID) const {
   1622     Profile(ID, TemplateArgs->data(), TemplateArgs->size(), getASTContext());
   1623   }
   1624 
   1625   static void
   1626   Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
   1627           unsigned NumTemplateArgs, ASTContext &Context) {
   1628     ID.AddInteger(NumTemplateArgs);
   1629     for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
   1630       TemplateArgs[Arg].Profile(ID, Context);
   1631   }
   1632 
   1633   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   1634   static bool classofKind(Kind K) {
   1635     return K >= firstClassTemplateSpecialization &&
   1636            K <= lastClassTemplateSpecialization;
   1637   }
   1638 
   1639   friend class ASTDeclReader;
   1640   friend class ASTDeclWriter;
   1641 };
   1642 
   1643 class ClassTemplatePartialSpecializationDecl
   1644   : public ClassTemplateSpecializationDecl {
   1645   virtual void anchor();
   1646 
   1647   /// \brief The list of template parameters
   1648   TemplateParameterList* TemplateParams;
   1649 
   1650   /// \brief The source info for the template arguments as written.
   1651   /// FIXME: redundant with TypeAsWritten?
   1652   TemplateArgumentLoc *ArgsAsWritten;
   1653   unsigned NumArgsAsWritten;
   1654 
   1655   /// \brief Sequence number indicating when this class template partial
   1656   /// specialization was added to the set of partial specializations for
   1657   /// its owning class template.
   1658   unsigned SequenceNumber;
   1659 
   1660   /// \brief The class template partial specialization from which this
   1661   /// class template partial specialization was instantiated.
   1662   ///
   1663   /// The boolean value will be true to indicate that this class template
   1664   /// partial specialization was specialized at this level.
   1665   llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
   1666       InstantiatedFromMember;
   1667 
   1668   ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
   1669                                          DeclContext *DC,
   1670                                          SourceLocation StartLoc,
   1671                                          SourceLocation IdLoc,
   1672                                          TemplateParameterList *Params,
   1673                                          ClassTemplateDecl *SpecializedTemplate,
   1674                                          const TemplateArgument *Args,
   1675                                          unsigned NumArgs,
   1676                                          TemplateArgumentLoc *ArgInfos,
   1677                                          unsigned NumArgInfos,
   1678                                ClassTemplatePartialSpecializationDecl *PrevDecl,
   1679                                          unsigned SequenceNumber);
   1680 
   1681   ClassTemplatePartialSpecializationDecl()
   1682     : ClassTemplateSpecializationDecl(ClassTemplatePartialSpecialization),
   1683       TemplateParams(0), ArgsAsWritten(0),
   1684       NumArgsAsWritten(0), SequenceNumber(0),
   1685       InstantiatedFromMember(0, false) { }
   1686 
   1687 public:
   1688   static ClassTemplatePartialSpecializationDecl *
   1689   Create(ASTContext &Context, TagKind TK, DeclContext *DC,
   1690          SourceLocation StartLoc, SourceLocation IdLoc,
   1691          TemplateParameterList *Params,
   1692          ClassTemplateDecl *SpecializedTemplate,
   1693          const TemplateArgument *Args,
   1694          unsigned NumArgs,
   1695          const TemplateArgumentListInfo &ArgInfos,
   1696          QualType CanonInjectedType,
   1697          ClassTemplatePartialSpecializationDecl *PrevDecl,
   1698          unsigned SequenceNumber);
   1699 
   1700   static ClassTemplatePartialSpecializationDecl *
   1701   CreateDeserialized(ASTContext &C, unsigned ID);
   1702 
   1703   ClassTemplatePartialSpecializationDecl *getMostRecentDecl() {
   1704     return cast<ClassTemplatePartialSpecializationDecl>(
   1705                    ClassTemplateSpecializationDecl::getMostRecentDecl());
   1706   }
   1707 
   1708   /// Get the list of template parameters
   1709   TemplateParameterList *getTemplateParameters() const {
   1710     return TemplateParams;
   1711   }
   1712 
   1713   /// Get the template arguments as written.
   1714   TemplateArgumentLoc *getTemplateArgsAsWritten() const {
   1715     return ArgsAsWritten;
   1716   }
   1717 
   1718   /// Get the number of template arguments as written.
   1719   unsigned getNumTemplateArgsAsWritten() const {
   1720     return NumArgsAsWritten;
   1721   }
   1722 
   1723   /// \brief Get the sequence number for this class template partial
   1724   /// specialization. Internal, only valid for specializations which
   1725   /// are in the specialized class template's folding set.
   1726   unsigned getSequenceNumber() const { return SequenceNumber; }
   1727 
   1728   /// \brief Retrieve the member class template partial specialization from
   1729   /// which this particular class template partial specialization was
   1730   /// instantiated.
   1731   ///
   1732   /// \code
   1733   /// template<typename T>
   1734   /// struct Outer {
   1735   ///   template<typename U> struct Inner;
   1736   ///   template<typename U> struct Inner<U*> { }; // #1
   1737   /// };
   1738   ///
   1739   /// Outer<float>::Inner<int*> ii;
   1740   /// \endcode
   1741   ///
   1742   /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
   1743   /// end up instantiating the partial specialization
   1744   /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
   1745   /// template partial specialization \c Outer<T>::Inner<U*>. Given
   1746   /// \c Outer<float>::Inner<U*>, this function would return
   1747   /// \c Outer<T>::Inner<U*>.
   1748   ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
   1749     ClassTemplatePartialSpecializationDecl *First
   1750       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
   1751     return First->InstantiatedFromMember.getPointer();
   1752   }
   1753 
   1754   void setInstantiatedFromMember(
   1755                           ClassTemplatePartialSpecializationDecl *PartialSpec) {
   1756     ClassTemplatePartialSpecializationDecl *First
   1757       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
   1758     First->InstantiatedFromMember.setPointer(PartialSpec);
   1759   }
   1760 
   1761   /// \brief Determines whether this class template partial specialization
   1762   /// template was a specialization of a member partial specialization.
   1763   ///
   1764   /// In the following example, the member template partial specialization
   1765   /// \c X<int>::Inner<T*> is a member specialization.
   1766   ///
   1767   /// \code
   1768   /// template<typename T>
   1769   /// struct X {
   1770   ///   template<typename U> struct Inner;
   1771   ///   template<typename U> struct Inner<U*>;
   1772   /// };
   1773   ///
   1774   /// template<> template<typename T>
   1775   /// struct X<int>::Inner<T*> { /* ... */ };
   1776   /// \endcode
   1777   bool isMemberSpecialization() {
   1778     ClassTemplatePartialSpecializationDecl *First
   1779       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
   1780     return First->InstantiatedFromMember.getInt();
   1781   }
   1782 
   1783   /// \brief Note that this member template is a specialization.
   1784   void setMemberSpecialization() {
   1785     ClassTemplatePartialSpecializationDecl *First
   1786       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
   1787     assert(First->InstantiatedFromMember.getPointer() &&
   1788            "Only member templates can be member template specializations");
   1789     return First->InstantiatedFromMember.setInt(true);
   1790   }
   1791 
   1792   /// Retrieves the injected specialization type for this partial
   1793   /// specialization.  This is not the same as the type-decl-type for
   1794   /// this partial specialization, which is an InjectedClassNameType.
   1795   QualType getInjectedSpecializationType() const {
   1796     assert(getTypeForDecl() && "partial specialization has no type set!");
   1797     return cast<InjectedClassNameType>(getTypeForDecl())
   1798              ->getInjectedSpecializationType();
   1799   }
   1800 
   1801   // FIXME: Add Profile support!
   1802 
   1803   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   1804   static bool classofKind(Kind K) {
   1805     return K == ClassTemplatePartialSpecialization;
   1806   }
   1807 
   1808   friend class ASTDeclReader;
   1809   friend class ASTDeclWriter;
   1810 };
   1811 
   1812 /// Declaration of a class template.
   1813 class ClassTemplateDecl : public RedeclarableTemplateDecl {
   1814   static void DeallocateCommon(void *Ptr);
   1815 
   1816 protected:
   1817   /// \brief Data that is common to all of the declarations of a given
   1818   /// class template.
   1819   struct Common : CommonBase {
   1820     Common() : LazySpecializations() { }
   1821 
   1822     /// \brief The class template specializations for this class
   1823     /// template, including explicit specializations and instantiations.
   1824     llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations;
   1825 
   1826     /// \brief The class template partial specializations for this class
   1827     /// template.
   1828     llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>
   1829       PartialSpecializations;
   1830 
   1831     /// \brief The injected-class-name type for this class template.
   1832     QualType InjectedClassNameType;
   1833 
   1834     /// \brief If non-null, points to an array of specializations (including
   1835     /// partial specializations) known only by their external declaration IDs.
   1836     ///
   1837     /// The first value in the array is the number of of specializations/
   1838     /// partial specializations that follow.
   1839     uint32_t *LazySpecializations;
   1840   };
   1841 
   1842   /// \brief Load any lazily-loaded specializations from the external source.
   1843   void LoadLazySpecializations() const;
   1844 
   1845   /// \brief Retrieve the set of specializations of this class template.
   1846   llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
   1847   getSpecializations() const;
   1848 
   1849   /// \brief Retrieve the set of partial specializations of this class
   1850   /// template.
   1851   llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
   1852   getPartialSpecializations();
   1853 
   1854   ClassTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
   1855                     TemplateParameterList *Params, NamedDecl *Decl)
   1856     : RedeclarableTemplateDecl(ClassTemplate, DC, L, Name, Params, Decl) { }
   1857 
   1858   ClassTemplateDecl(EmptyShell Empty)
   1859     : RedeclarableTemplateDecl(ClassTemplate, 0, SourceLocation(),
   1860                                DeclarationName(), 0, 0) { }
   1861 
   1862   CommonBase *newCommon(ASTContext &C) const;
   1863 
   1864   Common *getCommonPtr() const {
   1865     return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
   1866   }
   1867 
   1868 public:
   1869   /// \brief Get the underlying class declarations of the template.
   1870   CXXRecordDecl *getTemplatedDecl() const {
   1871     return static_cast<CXXRecordDecl *>(TemplatedDecl);
   1872   }
   1873 
   1874   /// \brief Returns whether this template declaration defines the primary
   1875   /// class pattern.
   1876   bool isThisDeclarationADefinition() const {
   1877     return getTemplatedDecl()->isThisDeclarationADefinition();
   1878   }
   1879 
   1880   /// \brief Create a class template node.
   1881   static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
   1882                                    SourceLocation L,
   1883                                    DeclarationName Name,
   1884                                    TemplateParameterList *Params,
   1885                                    NamedDecl *Decl,
   1886                                    ClassTemplateDecl *PrevDecl);
   1887 
   1888   /// \brief Create an empty class template node.
   1889   static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
   1890 
   1891   /// \brief Return the specialization with the provided arguments if it exists,
   1892   /// otherwise return the insertion point.
   1893   ClassTemplateSpecializationDecl *
   1894   findSpecialization(const TemplateArgument *Args, unsigned NumArgs,
   1895                      void *&InsertPos);
   1896 
   1897   /// \brief Insert the specified specialization knowing that it is not already
   1898   /// in. InsertPos must be obtained from findSpecialization.
   1899   void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos);
   1900 
   1901   ClassTemplateDecl *getCanonicalDecl() {
   1902     return cast<ClassTemplateDecl>(
   1903              RedeclarableTemplateDecl::getCanonicalDecl());
   1904   }
   1905   const ClassTemplateDecl *getCanonicalDecl() const {
   1906     return cast<ClassTemplateDecl>(
   1907              RedeclarableTemplateDecl::getCanonicalDecl());
   1908   }
   1909 
   1910   /// \brief Retrieve the previous declaration of this class template, or
   1911   /// NULL if no such declaration exists.
   1912   ClassTemplateDecl *getPreviousDecl() {
   1913     return cast_or_null<ClassTemplateDecl>(
   1914              RedeclarableTemplateDecl::getPreviousDecl());
   1915   }
   1916 
   1917   /// \brief Retrieve the previous declaration of this class template, or
   1918   /// NULL if no such declaration exists.
   1919   const ClassTemplateDecl *getPreviousDecl() const {
   1920     return cast_or_null<ClassTemplateDecl>(
   1921              RedeclarableTemplateDecl::getPreviousDecl());
   1922   }
   1923 
   1924   ClassTemplateDecl *getInstantiatedFromMemberTemplate() {
   1925     return cast_or_null<ClassTemplateDecl>(
   1926              RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
   1927   }
   1928 
   1929   /// \brief Return the partial specialization with the provided arguments if it
   1930   /// exists, otherwise return the insertion point.
   1931   ClassTemplatePartialSpecializationDecl *
   1932   findPartialSpecialization(const TemplateArgument *Args, unsigned NumArgs,
   1933                             void *&InsertPos);
   1934 
   1935   /// \brief Insert the specified partial specialization knowing that it is not
   1936   /// already in. InsertPos must be obtained from findPartialSpecialization.
   1937   void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D,
   1938                                 void *InsertPos);
   1939 
   1940   /// \brief Return the next partial specialization sequence number.
   1941   unsigned getNextPartialSpecSequenceNumber() {
   1942     // Do not load lazy specializations here. They get numbered as they are
   1943     // loaded.
   1944     return getCommonPtr()->PartialSpecializations.size();
   1945   }
   1946 
   1947   /// \brief Retrieve the partial specializations as an ordered list.
   1948   void getPartialSpecializations(
   1949           SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS);
   1950 
   1951   /// \brief Find a class template partial specialization with the given
   1952   /// type T.
   1953   ///
   1954   /// \param T a dependent type that names a specialization of this class
   1955   /// template.
   1956   ///
   1957   /// \returns the class template partial specialization that exactly matches
   1958   /// the type \p T, or NULL if no such partial specialization exists.
   1959   ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
   1960 
   1961   /// \brief Find a class template partial specialization which was instantiated
   1962   /// from the given member partial specialization.
   1963   ///
   1964   /// \param D a member class template partial specialization.
   1965   ///
   1966   /// \returns the class template partial specialization which was instantiated
   1967   /// from the given member partial specialization, or NULL if no such partial
   1968   /// specialization exists.
   1969   ClassTemplatePartialSpecializationDecl *
   1970   findPartialSpecInstantiatedFromMember(
   1971                                      ClassTemplatePartialSpecializationDecl *D);
   1972 
   1973   /// \brief Retrieve the template specialization type of the
   1974   /// injected-class-name for this class template.
   1975   ///
   1976   /// The injected-class-name for a class template \c X is \c
   1977   /// X<template-args>, where \c template-args is formed from the
   1978   /// template arguments that correspond to the template parameters of
   1979   /// \c X. For example:
   1980   ///
   1981   /// \code
   1982   /// template<typename T, int N>
   1983   /// struct array {
   1984   ///   typedef array this_type; // "array" is equivalent to "array<T, N>"
   1985   /// };
   1986   /// \endcode
   1987   QualType getInjectedClassNameSpecialization();
   1988 
   1989   typedef SpecIterator<ClassTemplateSpecializationDecl> spec_iterator;
   1990 
   1991   spec_iterator spec_begin() const {
   1992     return makeSpecIterator(getSpecializations(), false);
   1993   }
   1994 
   1995   spec_iterator spec_end() const {
   1996     return makeSpecIterator(getSpecializations(), true);
   1997   }
   1998 
   1999   typedef SpecIterator<ClassTemplatePartialSpecializationDecl>
   2000           partial_spec_iterator;
   2001 
   2002   partial_spec_iterator partial_spec_begin() {
   2003     return makeSpecIterator(getPartialSpecializations(), false);
   2004   }
   2005 
   2006   partial_spec_iterator partial_spec_end() {
   2007     return makeSpecIterator(getPartialSpecializations(), true);
   2008   }
   2009 
   2010   // Implement isa/cast/dyncast support
   2011   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   2012   static bool classofKind(Kind K) { return K == ClassTemplate; }
   2013 
   2014   friend class ASTDeclReader;
   2015   friend class ASTDeclWriter;
   2016 };
   2017 
   2018 /// \brief Declaration of a friend template.
   2019 ///
   2020 /// For example:
   2021 /// \code
   2022 /// template \<typename T> class A {
   2023 ///   friend class MyVector<T>; // not a friend template
   2024 ///   template \<typename U> friend class B; // not a friend template
   2025 ///   template \<typename U> friend class Foo<T>::Nested; // friend template
   2026 /// };
   2027 /// \endcode
   2028 ///
   2029 /// \note This class is not currently in use.  All of the above
   2030 /// will yield a FriendDecl, not a FriendTemplateDecl.
   2031 class FriendTemplateDecl : public Decl {
   2032   virtual void anchor();
   2033 public:
   2034   typedef llvm::PointerUnion<NamedDecl*,TypeSourceInfo*> FriendUnion;
   2035 
   2036 private:
   2037   // The number of template parameters;  always non-zero.
   2038   unsigned NumParams;
   2039 
   2040   // The parameter list.
   2041   TemplateParameterList **Params;
   2042 
   2043   // The declaration that's a friend of this class.
   2044   FriendUnion Friend;
   2045 
   2046   // Location of the 'friend' specifier.
   2047   SourceLocation FriendLoc;
   2048 
   2049 
   2050   FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
   2051                      unsigned NParams,
   2052                      TemplateParameterList **Params,
   2053                      FriendUnion Friend,
   2054                      SourceLocation FriendLoc)
   2055     : Decl(Decl::FriendTemplate, DC, Loc),
   2056       NumParams(NParams),
   2057       Params(Params),
   2058       Friend(Friend),
   2059       FriendLoc(FriendLoc)
   2060   {}
   2061 
   2062   FriendTemplateDecl(EmptyShell Empty)
   2063     : Decl(Decl::FriendTemplate, Empty),
   2064       NumParams(0),
   2065       Params(0)
   2066   {}
   2067 
   2068 public:
   2069   static FriendTemplateDecl *Create(ASTContext &Context,
   2070                                     DeclContext *DC, SourceLocation Loc,
   2071                                     unsigned NParams,
   2072                                     TemplateParameterList **Params,
   2073                                     FriendUnion Friend,
   2074                                     SourceLocation FriendLoc);
   2075 
   2076   static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
   2077 
   2078   /// If this friend declaration names a templated type (or
   2079   /// a dependent member type of a templated type), return that
   2080   /// type;  otherwise return null.
   2081   TypeSourceInfo *getFriendType() const {
   2082     return Friend.dyn_cast<TypeSourceInfo*>();
   2083   }
   2084 
   2085   /// If this friend declaration names a templated function (or
   2086   /// a member function of a templated type), return that type;
   2087   /// otherwise return null.
   2088   NamedDecl *getFriendDecl() const {
   2089     return Friend.dyn_cast<NamedDecl*>();
   2090   }
   2091 
   2092   /// \brief Retrieves the location of the 'friend' keyword.
   2093   SourceLocation getFriendLoc() const {
   2094     return FriendLoc;
   2095   }
   2096 
   2097   TemplateParameterList *getTemplateParameterList(unsigned i) const {
   2098     assert(i <= NumParams);
   2099     return Params[i];
   2100   }
   2101 
   2102   unsigned getNumTemplateParameters() const {
   2103     return NumParams;
   2104   }
   2105 
   2106   // Implement isa/cast/dyncast/etc.
   2107   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   2108   static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
   2109 
   2110   friend class ASTDeclReader;
   2111 };
   2112 
   2113 /// \brief Declaration of an alias template.
   2114 ///
   2115 /// For example:
   2116 /// \code
   2117 /// template \<typename T> using V = std::map<T*, int, MyCompare<T>>;
   2118 /// \endcode
   2119 class TypeAliasTemplateDecl : public RedeclarableTemplateDecl {
   2120   static void DeallocateCommon(void *Ptr);
   2121 
   2122 protected:
   2123   typedef CommonBase Common;
   2124 
   2125   TypeAliasTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
   2126                         TemplateParameterList *Params, NamedDecl *Decl)
   2127     : RedeclarableTemplateDecl(TypeAliasTemplate, DC, L, Name, Params, Decl) { }
   2128 
   2129   CommonBase *newCommon(ASTContext &C) const;
   2130 
   2131   Common *getCommonPtr() {
   2132     return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
   2133   }
   2134 
   2135 public:
   2136   /// Get the underlying function declaration of the template.
   2137   TypeAliasDecl *getTemplatedDecl() const {
   2138     return static_cast<TypeAliasDecl*>(TemplatedDecl);
   2139   }
   2140 
   2141 
   2142   TypeAliasTemplateDecl *getCanonicalDecl() {
   2143     return cast<TypeAliasTemplateDecl>(
   2144              RedeclarableTemplateDecl::getCanonicalDecl());
   2145   }
   2146   const TypeAliasTemplateDecl *getCanonicalDecl() const {
   2147     return cast<TypeAliasTemplateDecl>(
   2148              RedeclarableTemplateDecl::getCanonicalDecl());
   2149   }
   2150 
   2151   /// \brief Retrieve the previous declaration of this function template, or
   2152   /// NULL if no such declaration exists.
   2153   TypeAliasTemplateDecl *getPreviousDecl() {
   2154     return cast_or_null<TypeAliasTemplateDecl>(
   2155              RedeclarableTemplateDecl::getPreviousDecl());
   2156   }
   2157 
   2158   /// \brief Retrieve the previous declaration of this function template, or
   2159   /// NULL if no such declaration exists.
   2160   const TypeAliasTemplateDecl *getPreviousDecl() const {
   2161     return cast_or_null<TypeAliasTemplateDecl>(
   2162              RedeclarableTemplateDecl::getPreviousDecl());
   2163   }
   2164 
   2165   TypeAliasTemplateDecl *getInstantiatedFromMemberTemplate() {
   2166     return cast_or_null<TypeAliasTemplateDecl>(
   2167              RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
   2168   }
   2169 
   2170 
   2171   /// \brief Create a function template node.
   2172   static TypeAliasTemplateDecl *Create(ASTContext &C, DeclContext *DC,
   2173                                        SourceLocation L,
   2174                                        DeclarationName Name,
   2175                                        TemplateParameterList *Params,
   2176                                        NamedDecl *Decl);
   2177 
   2178   /// \brief Create an empty alias template node.
   2179   static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
   2180 
   2181   // Implement isa/cast/dyncast support
   2182   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   2183   static bool classofKind(Kind K) { return K == TypeAliasTemplate; }
   2184 
   2185   friend class ASTDeclReader;
   2186   friend class ASTDeclWriter;
   2187 };
   2188 
   2189 /// \brief Declaration of a function specialization at template class scope.
   2190 ///
   2191 /// This is a non standard extension needed to support MSVC.
   2192 ///
   2193 /// For example:
   2194 /// \code
   2195 /// template <class T>
   2196 /// class A {
   2197 ///    template <class U> void foo(U a) { }
   2198 ///    template<> void foo(int a) { }
   2199 /// }
   2200 /// \endcode
   2201 ///
   2202 /// "template<> foo(int a)" will be saved in Specialization as a normal
   2203 /// CXXMethodDecl. Then during an instantiation of class A, it will be
   2204 /// transformed into an actual function specialization.
   2205 class ClassScopeFunctionSpecializationDecl : public Decl {
   2206   virtual void anchor();
   2207 
   2208   ClassScopeFunctionSpecializationDecl(DeclContext *DC, SourceLocation Loc,
   2209                                        CXXMethodDecl *FD, bool Args,
   2210                                        TemplateArgumentListInfo TemplArgs)
   2211     : Decl(Decl::ClassScopeFunctionSpecialization, DC, Loc),
   2212       Specialization(FD), HasExplicitTemplateArgs(Args),
   2213       TemplateArgs(TemplArgs) {}
   2214 
   2215   ClassScopeFunctionSpecializationDecl(EmptyShell Empty)
   2216     : Decl(Decl::ClassScopeFunctionSpecialization, Empty) {}
   2217 
   2218   CXXMethodDecl *Specialization;
   2219   bool HasExplicitTemplateArgs;
   2220   TemplateArgumentListInfo TemplateArgs;
   2221 
   2222 public:
   2223   CXXMethodDecl *getSpecialization() const { return Specialization; }
   2224   bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
   2225   const TemplateArgumentListInfo& templateArgs() const { return TemplateArgs; }
   2226 
   2227   static ClassScopeFunctionSpecializationDecl *Create(ASTContext &C,
   2228                                                       DeclContext *DC,
   2229                                                       SourceLocation Loc,
   2230                                                       CXXMethodDecl *FD,
   2231                                                    bool HasExplicitTemplateArgs,
   2232                                         TemplateArgumentListInfo TemplateArgs) {
   2233     return new (C) ClassScopeFunctionSpecializationDecl(DC , Loc, FD,
   2234                                                         HasExplicitTemplateArgs,
   2235                                                         TemplateArgs);
   2236   }
   2237 
   2238   static ClassScopeFunctionSpecializationDecl *
   2239   CreateDeserialized(ASTContext &Context, unsigned ID);
   2240 
   2241   // Implement isa/cast/dyncast/etc.
   2242   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   2243   static bool classofKind(Kind K) {
   2244     return K == Decl::ClassScopeFunctionSpecialization;
   2245   }
   2246 
   2247   friend class ASTDeclReader;
   2248   friend class ASTDeclWriter;
   2249 };
   2250 
   2251 /// Implementation of inline functions that require the template declarations
   2252 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
   2253   : Function(FTD) { }
   2254 
   2255 /// \brief Represents a variable template specialization, which refers to
   2256 /// a variable template with a given set of template arguments.
   2257 ///
   2258 /// Variable template specializations represent both explicit
   2259 /// specializations of variable templates, as in the example below, and
   2260 /// implicit instantiations of variable templates.
   2261 ///
   2262 /// \code
   2263 /// template<typename T> constexpr T pi = T(3.1415926535897932385);
   2264 ///
   2265 /// template<>
   2266 /// constexpr float pi<float>; // variable template specialization pi<float>
   2267 /// \endcode
   2268 class VarTemplateSpecializationDecl : public VarDecl,
   2269                                       public llvm::FoldingSetNode {
   2270 
   2271   /// \brief Structure that stores information about a variable template
   2272   /// specialization that was instantiated from a variable template partial
   2273   /// specialization.
   2274   struct SpecializedPartialSpecialization {
   2275     /// \brief The variable template partial specialization from which this
   2276     /// variable template specialization was instantiated.
   2277     VarTemplatePartialSpecializationDecl *PartialSpecialization;
   2278 
   2279     /// \brief The template argument list deduced for the variable template
   2280     /// partial specialization itself.
   2281     TemplateArgumentList *TemplateArgs;
   2282   };
   2283 
   2284   /// \brief The template that this specialization specializes.
   2285   llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *>
   2286   SpecializedTemplate;
   2287 
   2288   /// \brief Further info for explicit template specialization/instantiation.
   2289   struct ExplicitSpecializationInfo {
   2290     /// \brief The type-as-written.
   2291     TypeSourceInfo *TypeAsWritten;
   2292     /// \brief The location of the extern keyword.
   2293     SourceLocation ExternLoc;
   2294     /// \brief The location of the template keyword.
   2295     SourceLocation TemplateKeywordLoc;
   2296 
   2297     ExplicitSpecializationInfo()
   2298         : TypeAsWritten(0), ExternLoc(), TemplateKeywordLoc() {}
   2299   };
   2300 
   2301   /// \brief Further info for explicit template specialization/instantiation.
   2302   /// Does not apply to implicit specializations.
   2303   ExplicitSpecializationInfo *ExplicitInfo;
   2304 
   2305   /// \brief The template arguments used to describe this specialization.
   2306   TemplateArgumentList *TemplateArgs;
   2307   TemplateArgumentListInfo TemplateArgsInfo;
   2308 
   2309   /// \brief The point where this template was instantiated (if any).
   2310   SourceLocation PointOfInstantiation;
   2311 
   2312   /// \brief The kind of specialization this declaration refers to.
   2313   /// Really a value of type TemplateSpecializationKind.
   2314   unsigned SpecializationKind : 3;
   2315 
   2316 protected:
   2317   VarTemplateSpecializationDecl(ASTContext &Context, Kind DK, DeclContext *DC,
   2318                                 SourceLocation StartLoc, SourceLocation IdLoc,
   2319                                 VarTemplateDecl *SpecializedTemplate,
   2320                                 QualType T, TypeSourceInfo *TInfo,
   2321                                 StorageClass S, const TemplateArgument *Args,
   2322                                 unsigned NumArgs);
   2323 
   2324   explicit VarTemplateSpecializationDecl(Kind DK);
   2325 
   2326 public:
   2327   static VarTemplateSpecializationDecl *
   2328   Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
   2329          SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
   2330          TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
   2331          unsigned NumArgs);
   2332   static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C,
   2333                                                            unsigned ID);
   2334 
   2335   virtual void getNameForDiagnostic(raw_ostream &OS,
   2336                                     const PrintingPolicy &Policy,
   2337                                     bool Qualified) const;
   2338 
   2339   VarTemplateSpecializationDecl *getMostRecentDecl() {
   2340     VarDecl *Recent = cast<VarDecl>(VarDecl::getMostRecentDecl());
   2341     return cast<VarTemplateSpecializationDecl>(Recent);
   2342   }
   2343 
   2344   /// \brief Retrieve the template that this specialization specializes.
   2345   VarTemplateDecl *getSpecializedTemplate() const;
   2346 
   2347   /// \brief Retrieve the template arguments of the variable template
   2348   /// specialization.
   2349   const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; }
   2350 
   2351   // TODO: Always set this when creating the new specialization?
   2352   void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo);
   2353 
   2354   const TemplateArgumentListInfo &getTemplateArgsInfo() const {
   2355     return TemplateArgsInfo;
   2356   }
   2357 
   2358   /// \brief Determine the kind of specialization that this
   2359   /// declaration represents.
   2360   TemplateSpecializationKind getSpecializationKind() const {
   2361     return static_cast<TemplateSpecializationKind>(SpecializationKind);
   2362   }
   2363 
   2364   bool isExplicitSpecialization() const {
   2365     return getSpecializationKind() == TSK_ExplicitSpecialization;
   2366   }
   2367 
   2368   /// \brief True if this declaration is an explicit specialization,
   2369   /// explicit instantiation declaration, or explicit instantiation
   2370   /// definition.
   2371   bool isExplicitInstantiationOrSpecialization() const {
   2372     switch (getTemplateSpecializationKind()) {
   2373     case TSK_ExplicitSpecialization:
   2374     case TSK_ExplicitInstantiationDeclaration:
   2375     case TSK_ExplicitInstantiationDefinition:
   2376       return true;
   2377 
   2378     case TSK_Undeclared:
   2379     case TSK_ImplicitInstantiation:
   2380       return false;
   2381     }
   2382     llvm_unreachable("bad template specialization kind");
   2383   }
   2384 
   2385   void setSpecializationKind(TemplateSpecializationKind TSK) {
   2386     SpecializationKind = TSK;
   2387   }
   2388 
   2389   /// \brief Get the point of instantiation (if any), or null if none.
   2390   SourceLocation getPointOfInstantiation() const {
   2391     return PointOfInstantiation;
   2392   }
   2393 
   2394   void setPointOfInstantiation(SourceLocation Loc) {
   2395     assert(Loc.isValid() && "point of instantiation must be valid!");
   2396     PointOfInstantiation = Loc;
   2397   }
   2398 
   2399   /// \brief If this variable template specialization is an instantiation of
   2400   /// a template (rather than an explicit specialization), return the
   2401   /// variable template or variable template partial specialization from which
   2402   /// it was instantiated.
   2403   llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
   2404   getInstantiatedFrom() const {
   2405     if (getSpecializationKind() != TSK_ImplicitInstantiation &&
   2406         getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
   2407         getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
   2408       return llvm::PointerUnion<VarTemplateDecl *,
   2409                                 VarTemplatePartialSpecializationDecl *>();
   2410 
   2411     if (SpecializedPartialSpecialization *PartialSpec =
   2412             SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
   2413       return PartialSpec->PartialSpecialization;
   2414 
   2415     return SpecializedTemplate.get<VarTemplateDecl *>();
   2416   }
   2417 
   2418   /// \brief Retrieve the variable template or variable template partial
   2419   /// specialization which was specialized by this.
   2420   llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
   2421   getSpecializedTemplateOrPartial() const {
   2422     if (SpecializedPartialSpecialization *PartialSpec =
   2423             SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
   2424       return PartialSpec->PartialSpecialization;
   2425 
   2426     return SpecializedTemplate.get<VarTemplateDecl *>();
   2427   }
   2428 
   2429   /// \brief Retrieve the set of template arguments that should be used
   2430   /// to instantiate the initializer of the variable template or variable
   2431   /// template partial specialization from which this variable template
   2432   /// specialization was instantiated.
   2433   ///
   2434   /// \returns For a variable template specialization instantiated from the
   2435   /// primary template, this function will return the same template arguments
   2436   /// as getTemplateArgs(). For a variable template specialization instantiated
   2437   /// from a variable template partial specialization, this function will the
   2438   /// return deduced template arguments for the variable template partial
   2439   /// specialization itself.
   2440   const TemplateArgumentList &getTemplateInstantiationArgs() const {
   2441     if (SpecializedPartialSpecialization *PartialSpec =
   2442             SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
   2443       return *PartialSpec->TemplateArgs;
   2444 
   2445     return getTemplateArgs();
   2446   }
   2447 
   2448   /// \brief Note that this variable template specialization is actually an
   2449   /// instantiation of the given variable template partial specialization whose
   2450   /// template arguments have been deduced.
   2451   void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec,
   2452                           TemplateArgumentList *TemplateArgs) {
   2453     assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
   2454            "Already set to a variable template partial specialization!");
   2455     SpecializedPartialSpecialization *PS =
   2456         new (getASTContext()) SpecializedPartialSpecialization();
   2457     PS->PartialSpecialization = PartialSpec;
   2458     PS->TemplateArgs = TemplateArgs;
   2459     SpecializedTemplate = PS;
   2460   }
   2461 
   2462   /// \brief Note that this variable template specialization is an instantiation
   2463   /// of the given variable template.
   2464   void setInstantiationOf(VarTemplateDecl *TemplDecl) {
   2465     assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
   2466            "Previously set to a variable template partial specialization!");
   2467     SpecializedTemplate = TemplDecl;
   2468   }
   2469 
   2470   /// \brief Sets the type of this specialization as it was written by
   2471   /// the user.
   2472   void setTypeAsWritten(TypeSourceInfo *T) {
   2473     if (!ExplicitInfo)
   2474       ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
   2475     ExplicitInfo->TypeAsWritten = T;
   2476   }
   2477   /// \brief Gets the type of this specialization as it was written by
   2478   /// the user, if it was so written.
   2479   TypeSourceInfo *getTypeAsWritten() const {
   2480     return ExplicitInfo ? ExplicitInfo->TypeAsWritten : 0;
   2481   }
   2482 
   2483   /// \brief Gets the location of the extern keyword, if present.
   2484   SourceLocation getExternLoc() const {
   2485     return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
   2486   }
   2487   /// \brief Sets the location of the extern keyword.
   2488   void setExternLoc(SourceLocation Loc) {
   2489     if (!ExplicitInfo)
   2490       ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
   2491     ExplicitInfo->ExternLoc = Loc;
   2492   }
   2493 
   2494   /// \brief Sets the location of the template keyword.
   2495   void setTemplateKeywordLoc(SourceLocation Loc) {
   2496     if (!ExplicitInfo)
   2497       ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
   2498     ExplicitInfo->TemplateKeywordLoc = Loc;
   2499   }
   2500   /// \brief Gets the location of the template keyword, if present.
   2501   SourceLocation getTemplateKeywordLoc() const {
   2502     return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
   2503   }
   2504 
   2505   void Profile(llvm::FoldingSetNodeID &ID) const {
   2506     Profile(ID, TemplateArgs->data(), TemplateArgs->size(), getASTContext());
   2507   }
   2508 
   2509   static void Profile(llvm::FoldingSetNodeID &ID,
   2510                       const TemplateArgument *TemplateArgs,
   2511                       unsigned NumTemplateArgs, ASTContext &Context) {
   2512     ID.AddInteger(NumTemplateArgs);
   2513     for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
   2514       TemplateArgs[Arg].Profile(ID, Context);
   2515   }
   2516 
   2517   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   2518   static bool classofKind(Kind K) {
   2519     return K >= firstVarTemplateSpecialization &&
   2520            K <= lastVarTemplateSpecialization;
   2521   }
   2522 
   2523   friend class ASTDeclReader;
   2524   friend class ASTDeclWriter;
   2525 };
   2526 
   2527 class VarTemplatePartialSpecializationDecl
   2528     : public VarTemplateSpecializationDecl {
   2529   virtual void anchor();
   2530 
   2531   /// \brief The list of template parameters
   2532   TemplateParameterList *TemplateParams;
   2533 
   2534   /// \brief The source info for the template arguments as written.
   2535   /// FIXME: redundant with TypeAsWritten?
   2536   TemplateArgumentLoc *ArgsAsWritten;
   2537   unsigned NumArgsAsWritten;
   2538 
   2539   /// \brief Sequence number indicating when this variable template partial
   2540   /// specialization was added to the set of partial specializations for
   2541   /// its owning variable template.
   2542   unsigned SequenceNumber;
   2543 
   2544   /// \brief The variable template partial specialization from which this
   2545   /// variable template partial specialization was instantiated.
   2546   ///
   2547   /// The boolean value will be true to indicate that this variable template
   2548   /// partial specialization was specialized at this level.
   2549   llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool>
   2550   InstantiatedFromMember;
   2551 
   2552   VarTemplatePartialSpecializationDecl(
   2553       ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
   2554       SourceLocation IdLoc, TemplateParameterList *Params,
   2555       VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
   2556       StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
   2557       TemplateArgumentLoc *ArgInfos, unsigned NumArgInfos,
   2558       unsigned SequenceNumber);
   2559 
   2560   VarTemplatePartialSpecializationDecl()
   2561       : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization),
   2562         TemplateParams(0), ArgsAsWritten(0), NumArgsAsWritten(0),
   2563         SequenceNumber(0), InstantiatedFromMember(0, false) {}
   2564 
   2565 public:
   2566   static VarTemplatePartialSpecializationDecl *
   2567   Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
   2568          SourceLocation IdLoc, TemplateParameterList *Params,
   2569          VarTemplateDecl *SpecializedTemplate, QualType T,
   2570          TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
   2571          unsigned NumArgs, const TemplateArgumentListInfo &ArgInfos,
   2572          unsigned SequenceNumber);
   2573 
   2574   static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C,
   2575                                                                   unsigned ID);
   2576 
   2577   VarTemplatePartialSpecializationDecl *getMostRecentDecl() {
   2578     return cast<VarTemplatePartialSpecializationDecl>(
   2579         VarTemplateSpecializationDecl::getMostRecentDecl());
   2580   }
   2581 
   2582   /// Get the list of template parameters
   2583   TemplateParameterList *getTemplateParameters() const {
   2584     return TemplateParams;
   2585   }
   2586 
   2587   /// Get the template arguments as written.
   2588   TemplateArgumentLoc *getTemplateArgsAsWritten() const {
   2589     return ArgsAsWritten;
   2590   }
   2591 
   2592   /// Get the number of template arguments as written.
   2593   unsigned getNumTemplateArgsAsWritten() const { return NumArgsAsWritten; }
   2594 
   2595   /// \brief Get the sequence number for this variable template partial
   2596   /// specialization.
   2597   unsigned getSequenceNumber() const { return SequenceNumber; }
   2598 
   2599   /// \brief Retrieve the member variable template partial specialization from
   2600   /// which this particular variable template partial specialization was
   2601   /// instantiated.
   2602   ///
   2603   /// \code
   2604   /// template<typename T>
   2605   /// struct Outer {
   2606   ///   template<typename U> U Inner;
   2607   ///   template<typename U> U* Inner<U*> = (U*)(0); // #1
   2608   /// };
   2609   ///
   2610   /// template int* Outer<float>::Inner<int*>;
   2611   /// \endcode
   2612   ///
   2613   /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
   2614   /// end up instantiating the partial specialization
   2615   /// \c Outer<float>::Inner<U*>, which itself was instantiated from the
   2616   /// variable template partial specialization \c Outer<T>::Inner<U*>. Given
   2617   /// \c Outer<float>::Inner<U*>, this function would return
   2618   /// \c Outer<T>::Inner<U*>.
   2619   VarTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
   2620     VarTemplatePartialSpecializationDecl *First =
   2621         cast<VarTemplatePartialSpecializationDecl>(getFirstDeclaration());
   2622     return First->InstantiatedFromMember.getPointer();
   2623   }
   2624 
   2625   void
   2626   setInstantiatedFromMember(VarTemplatePartialSpecializationDecl *PartialSpec) {
   2627     VarTemplatePartialSpecializationDecl *First =
   2628         cast<VarTemplatePartialSpecializationDecl>(getFirstDeclaration());
   2629     First->InstantiatedFromMember.setPointer(PartialSpec);
   2630   }
   2631 
   2632   /// \brief Determines whether this variable template partial specialization
   2633   /// was a specialization of a member partial specialization.
   2634   ///
   2635   /// In the following example, the member template partial specialization
   2636   /// \c X<int>::Inner<T*> is a member specialization.
   2637   ///
   2638   /// \code
   2639   /// template<typename T>
   2640   /// struct X {
   2641   ///   template<typename U> U Inner;
   2642   ///   template<typename U> U* Inner<U*> = (U*)(0);
   2643   /// };
   2644   ///
   2645   /// template<> template<typename T>
   2646   /// U* X<int>::Inner<T*> = (T*)(0) + 1;
   2647   /// \endcode
   2648   bool isMemberSpecialization() {
   2649     VarTemplatePartialSpecializationDecl *First =
   2650         cast<VarTemplatePartialSpecializationDecl>(getFirstDeclaration());
   2651     return First->InstantiatedFromMember.getInt();
   2652   }
   2653 
   2654   /// \brief Note that this member template is a specialization.
   2655   void setMemberSpecialization() {
   2656     VarTemplatePartialSpecializationDecl *First =
   2657         cast<VarTemplatePartialSpecializationDecl>(getFirstDeclaration());
   2658     assert(First->InstantiatedFromMember.getPointer() &&
   2659            "Only member templates can be member template specializations");
   2660     return First->InstantiatedFromMember.setInt(true);
   2661   }
   2662 
   2663   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   2664   static bool classofKind(Kind K) {
   2665     return K == VarTemplatePartialSpecialization;
   2666   }
   2667 
   2668   friend class ASTDeclReader;
   2669   friend class ASTDeclWriter;
   2670 };
   2671 
   2672 /// Declaration of a variable template.
   2673 class VarTemplateDecl : public RedeclarableTemplateDecl {
   2674   static void DeallocateCommon(void *Ptr);
   2675 
   2676 protected:
   2677   /// \brief Data that is common to all of the declarations of a given
   2678   /// variable template.
   2679   struct Common : CommonBase {
   2680     Common() : LazySpecializations() {}
   2681 
   2682     /// \brief The variable template specializations for this variable
   2683     /// template, including explicit specializations and instantiations.
   2684     llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations;
   2685 
   2686     /// \brief The variable template partial specializations for this variable
   2687     /// template.
   2688     llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>
   2689     PartialSpecializations;
   2690 
   2691     /// \brief If non-null, points to an array of specializations (including
   2692     /// partial specializations) known ownly by their external declaration IDs.
   2693     ///
   2694     /// The first value in the array is the number of of specializations/
   2695     /// partial specializations that follow.
   2696     uint32_t *LazySpecializations;
   2697   };
   2698 
   2699   /// \brief Load any lazily-loaded specializations from the external source.
   2700   void LoadLazySpecializations() const;
   2701 
   2702   /// \brief Retrieve the set of specializations of this variable template.
   2703   llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
   2704   getSpecializations() const;
   2705 
   2706   /// \brief Retrieve the set of partial specializations of this class
   2707   /// template.
   2708   llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
   2709   getPartialSpecializations();
   2710 
   2711   VarTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
   2712                   TemplateParameterList *Params, NamedDecl *Decl)
   2713       : RedeclarableTemplateDecl(VarTemplate, DC, L, Name, Params, Decl) {}
   2714 
   2715   VarTemplateDecl(EmptyShell Empty)
   2716       : RedeclarableTemplateDecl(VarTemplate, 0, SourceLocation(),
   2717                                  DeclarationName(), 0, 0) {}
   2718 
   2719   CommonBase *newCommon(ASTContext &C) const;
   2720 
   2721   Common *getCommonPtr() const {
   2722     return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
   2723   }
   2724 
   2725 public:
   2726   /// \brief Get the underlying variable declarations of the template.
   2727   VarDecl *getTemplatedDecl() const {
   2728     return static_cast<VarDecl *>(TemplatedDecl);
   2729   }
   2730 
   2731   /// \brief Returns whether this template declaration defines the primary
   2732   /// variable pattern.
   2733   bool isThisDeclarationADefinition() const {
   2734     return getTemplatedDecl()->isThisDeclarationADefinition();
   2735   }
   2736 
   2737   /// \brief Create a variable template node.
   2738   static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC,
   2739                                  SourceLocation L, DeclarationName Name,
   2740                                  TemplateParameterList *Params, NamedDecl *Decl,
   2741                                  VarTemplateDecl *PrevDecl);
   2742 
   2743   /// \brief Create an empty variable template node.
   2744   static VarTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
   2745 
   2746   /// \brief Return the specialization with the provided arguments if it exists,
   2747   /// otherwise return the insertion point.
   2748   VarTemplateSpecializationDecl *
   2749   findSpecialization(const TemplateArgument *Args, unsigned NumArgs,
   2750                      void *&InsertPos);
   2751 
   2752   /// \brief Insert the specified specialization knowing that it is not already
   2753   /// in. InsertPos must be obtained from findSpecialization.
   2754   void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos);
   2755 
   2756   VarTemplateDecl *getCanonicalDecl() {
   2757     return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
   2758   }
   2759   const VarTemplateDecl *getCanonicalDecl() const {
   2760     return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
   2761   }
   2762 
   2763   /// \brief Retrieve the previous declaration of this variable template, or
   2764   /// NULL if no such declaration exists.
   2765   VarTemplateDecl *getPreviousDecl() {
   2766     return cast_or_null<VarTemplateDecl>(
   2767         RedeclarableTemplateDecl::getPreviousDecl());
   2768   }
   2769 
   2770   /// \brief Retrieve the previous declaration of this variable template, or
   2771   /// NULL if no such declaration exists.
   2772   const VarTemplateDecl *getPreviousDecl() const {
   2773     return cast_or_null<VarTemplateDecl>(
   2774         RedeclarableTemplateDecl::getPreviousDecl());
   2775   }
   2776 
   2777   VarTemplateDecl *getInstantiatedFromMemberTemplate() {
   2778     return cast_or_null<VarTemplateDecl>(
   2779         RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate());
   2780   }
   2781 
   2782   /// \brief Return the partial specialization with the provided arguments if it
   2783   /// exists, otherwise return the insertion point.
   2784   VarTemplatePartialSpecializationDecl *
   2785   findPartialSpecialization(const TemplateArgument *Args, unsigned NumArgs,
   2786                             void *&InsertPos);
   2787 
   2788   /// \brief Insert the specified partial specialization knowing that it is not
   2789   /// already in. InsertPos must be obtained from findPartialSpecialization.
   2790   void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D,
   2791                                 void *InsertPos);
   2792 
   2793   /// \brief Return the next partial specialization sequence number.
   2794   unsigned getNextPartialSpecSequenceNumber() {
   2795     return getPartialSpecializations().size();
   2796   }
   2797 
   2798   /// \brief Retrieve the partial specializations as an ordered list.
   2799   void getPartialSpecializations(
   2800       SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS);
   2801 
   2802   /// \brief Find a variable template partial specialization which was
   2803   /// instantiated
   2804   /// from the given member partial specialization.
   2805   ///
   2806   /// \param D a member variable template partial specialization.
   2807   ///
   2808   /// \returns the variable template partial specialization which was
   2809   /// instantiated
   2810   /// from the given member partial specialization, or NULL if no such partial
   2811   /// specialization exists.
   2812   VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember(
   2813       VarTemplatePartialSpecializationDecl *D);
   2814 
   2815   typedef SpecIterator<VarTemplateSpecializationDecl> spec_iterator;
   2816 
   2817   spec_iterator spec_begin() const {
   2818     return makeSpecIterator(getSpecializations(), false);
   2819   }
   2820 
   2821   spec_iterator spec_end() const {
   2822     return makeSpecIterator(getSpecializations(), true);
   2823   }
   2824 
   2825   typedef SpecIterator<VarTemplatePartialSpecializationDecl>
   2826   partial_spec_iterator;
   2827 
   2828   partial_spec_iterator partial_spec_begin() {
   2829     return makeSpecIterator(getPartialSpecializations(), false);
   2830   }
   2831 
   2832   partial_spec_iterator partial_spec_end() {
   2833     return makeSpecIterator(getPartialSpecializations(), true);
   2834   }
   2835 
   2836   // Implement isa/cast/dyncast support
   2837   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
   2838   static bool classofKind(Kind K) { return K == VarTemplate; }
   2839 
   2840   friend class ASTDeclReader;
   2841   friend class ASTDeclWriter;
   2842 };
   2843 
   2844 } /* end of namespace clang */
   2845 
   2846 #endif
   2847