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