zcov: / include/clang/AST/DeclTemplate.h


Files: 1 Branches Taken: 58.1% 25 / 43
Generated: 2010-02-10 01:31 Branches Executed: 79.1% 34 / 43
Line Coverage: 94.3% 248 / 263


Programs: 63 Runs 96455


       1                 : //===-- DeclTemplate.h - Classes for representing C++ templates -*- C++ -*-===//
       2                 : //
       3                 : //                     The LLVM Compiler Infrastructure
       4                 : //
       5                 : // This file is distributed under the University of Illinois Open Source
       6                 : // License. See LICENSE.TXT for details.
       7                 : //
       8                 : //===----------------------------------------------------------------------===//
       9                 : //
      10                 : //  This file defines the C++ template declaration subclasses.
      11                 : //
      12                 : //===----------------------------------------------------------------------===//
      13                 : 
      14                 : #ifndef LLVM_CLANG_AST_DECLTEMPLATE_H
      15                 : #define LLVM_CLANG_AST_DECLTEMPLATE_H
      16                 : 
      17                 : #include "clang/AST/DeclCXX.h"
      18                 : #include "clang/AST/TemplateBase.h"
      19                 : #include "llvm/ADT/PointerUnion.h"
      20                 : #include <limits>
      21                 : 
      22                 : namespace clang {
      23                 : 
      24                 : class TemplateParameterList;
      25                 : class TemplateDecl;
      26                 : class FunctionTemplateDecl;
      27                 : class ClassTemplateDecl;
      28                 : class ClassTemplatePartialSpecializationDecl;
      29                 : class TemplateTypeParmDecl;
      30                 : class NonTypeTemplateParmDecl;
      31                 : class TemplateTemplateParmDecl;
      32                 : 
      33                 : /// \brief Stores a template parameter of any kind.
      34                 : typedef llvm::PointerUnion3<TemplateTypeParmDecl*, NonTypeTemplateParmDecl*,
      35                 :                             TemplateTemplateParmDecl*> TemplateParameter;
      36                 : 
      37                 : /// TemplateParameterList - Stores a list of template parameters for a
      38                 : /// TemplateDecl and its derived classes.
      39                 : class TemplateParameterList {
      40                 :   /// The location of the 'template' keyword.
      41                 :   SourceLocation TemplateLoc;
      42                 : 
      43                 :   /// The locations of the '<' and '>' angle brackets.
      44                 :   SourceLocation LAngleLoc, RAngleLoc;
      45                 : 
      46                 :   /// The number of template parameters in this template
      47                 :   /// parameter list.
      48                 :   unsigned NumParams;
      49                 : 
      50                 :   TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc,
      51                 :                         NamedDecl **Params, unsigned NumParams,
      52                 :                         SourceLocation RAngleLoc);
      53                 : 
      54                 : public:
      55                 :   static TemplateParameterList *Create(ASTContext &C,
      56                 :                                        SourceLocation TemplateLoc,
      57                 :                                        SourceLocation LAngleLoc,
      58                 :                                        NamedDecl **Params,
      59                 :                                        unsigned NumParams,
      60                 :                                        SourceLocation RAngleLoc);
      61                 : 
      62                 :   /// iterator - Iterates through the template parameters in this list.
      63                 :   typedef NamedDecl** iterator;
      64                 : 
      65                 :   /// const_iterator - Iterates through the template parameters in this list.
      66                 :   typedef NamedDecl* const* const_iterator;
      67                 : 
      68            30920:   iterator begin() { return reinterpret_cast<NamedDecl **>(this + 1); }
      69             2305:   const_iterator begin() const {
      70             2305:     return reinterpret_cast<NamedDecl * const *>(this + 1);
      71                 :   }
      72            12091:   iterator end() { return begin() + NumParams; }
      73             1084:   const_iterator end() const { return begin() + NumParams; }
      74                 : 
      75            29369:   unsigned size() const { return NumParams; }
      76                 : 
      77             5576:   NamedDecl* getParam(unsigned Idx) {
                     5576: branch 1 taken
                        0: branch 2 not taken
      78             5576:     assert(Idx < size() && "Template parameter index out-of-range");
      79             5576:     return begin()[Idx];
      80                 :   }
      81                 : 
      82              137:   const NamedDecl* getParam(unsigned Idx) const {
                        0: branch 2 not taken
      83              137:     assert(Idx < size() && "Template parameter index out-of-range");
      84              137:     return begin()[Idx];
      85                 :   }
      86                 : 
      87                 :   /// \btief Returns the minimum number of arguments needed to form a
      88                 :   /// template specialization. This may be fewer than the number of
      89                 :   /// template parameters, if some of the parameters have default
      90                 :   /// arguments or if there is a parameter pack.
      91                 :   unsigned getMinRequiredArguments() const;
      92                 : 
      93                 :   /// \brief Get the depth of this template parameter list in the set of
      94                 :   /// template parameter lists.
      95                 :   ///
      96                 :   /// The first template parameter list in a declaration will have depth 0,
      97                 :   /// the second template parameter list will have depth 1, etc.
      98                 :   unsigned getDepth() const;
      99                 :   
     100             2033:   SourceLocation getTemplateLoc() const { return TemplateLoc; }
     101              177:   SourceLocation getLAngleLoc() const { return LAngleLoc; }
     102              193:   SourceLocation getRAngleLoc() const { return RAngleLoc; }
     103                 : 
     104                5:   SourceRange getSourceRange() const {
     105                5:     return SourceRange(TemplateLoc, RAngleLoc);
     106                 :   }
     107                 : };
     108                 : 
     109                 : /// \brief A helper class for making template argument lists.
     110                 : class TemplateArgumentListBuilder {
     111                 :   TemplateArgument *StructuredArgs;
     112                 :   unsigned MaxStructuredArgs;
     113                 :   unsigned NumStructuredArgs;
     114                 : 
     115                 :   TemplateArgument *FlatArgs;
     116                 :   unsigned MaxFlatArgs;
     117                 :   unsigned NumFlatArgs;
     118                 : 
     119                 :   bool AddingToPack;
     120                 :   unsigned PackBeginIndex;
     121                 : 
     122                 : public:
     123                 :   TemplateArgumentListBuilder(const TemplateParameterList *Parameters,
     124             5331:                               unsigned NumTemplateArgs)
     125                 :   : StructuredArgs(0), MaxStructuredArgs(Parameters->size()),
     126                 :   NumStructuredArgs(0), FlatArgs(0),
     127                 :   MaxFlatArgs(std::max(MaxStructuredArgs, NumTemplateArgs)), NumFlatArgs(0),
     128             5331:   AddingToPack(false), PackBeginIndex(0) { }
     129                 : 
     130                 :   void Append(const TemplateArgument& Arg);
     131                 :   void BeginPack();
     132                 :   void EndPack();
     133                 : 
     134                 :   void ReleaseArgs();
     135                 : 
     136            14217:   unsigned flatSize() const {
     137            14217:     return NumFlatArgs;
     138                 :   }
     139            17298:   const TemplateArgument *getFlatArguments() const {
     140            17298:     return FlatArgs;
     141                 :   }
     142                 : 
     143             6824:   unsigned structuredSize() const {
     144                 :     // If we don't have any structured args, just reuse the flat size.
                     6809: branch 0 taken
                       15: branch 1 taken
     145             6824:     if (!StructuredArgs)
     146             6809:       return flatSize();
     147                 : 
     148               15:     return NumStructuredArgs;
     149                 :   }
     150             6454:   const TemplateArgument *getStructuredArguments() const {
     151                 :     // If we don't have any structured args, just reuse the flat args.
                     6440: branch 0 taken
                       14: branch 1 taken
     152             6454:     if (!StructuredArgs)
     153             6440:       return getFlatArguments();
     154                 : 
     155               14:     return StructuredArgs;
     156                 :   }
     157                 : };
     158                 : 
     159                 : /// \brief A template argument list.
     160                 : ///
     161                 : /// FIXME: In the future, this class will be extended to support
     162                 : /// variadic templates and member templates, which will make some of
     163                 : /// the function names below make more sense.
     164                 : class TemplateArgumentList {
     165                 :   /// \brief The template argument list.
     166                 :   ///
     167                 :   /// The integer value will be non-zero to indicate that this
     168                 :   /// template argument list does not own the pointer.
     169                 :   llvm::PointerIntPair<const TemplateArgument *, 1> FlatArguments;
     170                 : 
     171                 :   /// \brief The number of template arguments in this template
     172                 :   /// argument list.
     173                 :   unsigned NumFlatArguments;
     174                 : 
     175                 :   llvm::PointerIntPair<const TemplateArgument *, 1> StructuredArguments;
     176                 :   unsigned NumStructuredArguments;
     177                 : 
     178                 : public:
     179                 :   TemplateArgumentList(ASTContext &Context,
     180                 :                        TemplateArgumentListBuilder &Builder,
     181                 :                        bool TakeArgs);
     182                 : 
     183                 :   /// \brief Produces a shallow copy of the given template argument list
     184                 :   TemplateArgumentList(const TemplateArgumentList &Other);
     185                 :   
     186                 :   ~TemplateArgumentList();
     187                 : 
     188                 :   /// \brief Retrieve the template argument at a given index.
     189            15034:   const TemplateArgument &get(unsigned Idx) const {
                        0: branch 0 not taken
                    15034: branch 1 taken
     190            15034:     assert(Idx < NumFlatArguments && "Invalid template argument index");
     191            15034:     return getFlatArgumentList()[Idx];
     192                 :   }
     193                 : 
     194                 :   /// \brief Retrieve the template argument at a given index.
     195             4284:   const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); }
     196                 : 
     197                 :   /// \brief Retrieve the number of template arguments in this
     198                 :   /// template argument list.
     199            18166:   unsigned size() const { return NumFlatArguments; }
     200                 : 
     201                 :   /// \brief Retrieve the number of template arguments in the
     202                 :   /// flattened template argument list.
     203             5196:   unsigned flat_size() const { return NumFlatArguments; }
     204                 : 
     205                 :   /// \brief Retrieve the flattened template argument list.
     206            20187:   const TemplateArgument *getFlatArgumentList() const {
     207            20187:     return FlatArguments.getPointer();
     208                 :   }
     209                 : };
     210                 : 
     211                 : //===----------------------------------------------------------------------===//
     212                 : // Kinds of Templates
     213                 : //===----------------------------------------------------------------------===//
     214                 : 
     215                 : /// TemplateDecl - The base class of all kinds of template declarations (e.g.,
     216                 : /// class, function, etc.). The TemplateDecl class stores the list of template
     217                 : /// parameters and a reference to the templated scoped declaration: the
     218                 : /// underlying AST node.
     219                 : class TemplateDecl : public NamedDecl {
     220                 : protected:
     221                 :   // This is probably never used.
     222                 :   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
     223                 :                DeclarationName Name)
     224                 :     : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(0) { }
     225                 : 
     226                 :   // Construct a template decl with the given name and parameters.
     227                 :   // Used when there is not templated element (tt-params, alias?).
     228                 :   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
     229               99:                DeclarationName Name, TemplateParameterList *Params)
     230               99:     : NamedDecl(DK, DC, L, Name), TemplatedDecl(0), TemplateParams(Params) { }
     231                 : 
     232                 :   // Construct a template decl with name, parameters, and templated element.
     233                 :   TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
     234                 :                DeclarationName Name, TemplateParameterList *Params,
     235             1540:                NamedDecl *Decl)
     236                 :     : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl),
     237             1540:       TemplateParams(Params) { }
     238                 : public:
     239                 :   ~TemplateDecl();
     240                 : 
     241                 :   /// Get the list of template parameters
     242            17488:   TemplateParameterList *getTemplateParameters() const {
     243            17488:     return TemplateParams;
     244                 :   }
     245                 : 
     246                 :   /// Get the underlying, templated declaration.
     247             4091:   NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
     248                 : 
     249                 :   // Implement isa/cast/dyncast/etc.
     250            76900:   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
     251                 :   static bool classof(const TemplateDecl *D) { return true; }
     252                 :   static bool classof(const FunctionTemplateDecl *D) { return true; }
     253                 :   static bool classof(const ClassTemplateDecl *D) { return true; }
     254                 :   static bool classof(const TemplateTemplateParmDecl *D) { return true; }
     255            76900:   static bool classofKind(Kind K) {
                    18635: branch 0 taken
                    58265: branch 1 taken
                    18521: branch 2 taken
                      114: branch 3 taken
     256            76900:     return K >= TemplateFirst && K <= TemplateLast;
     257                 :   }
     258                 : 
     259                 : protected:
     260                 :   NamedDecl *TemplatedDecl;
     261                 :   TemplateParameterList* TemplateParams;
     262                 : };
     263                 : 
     264                 : /// \brief Provides information about a function template specialization,
     265                 : /// which is a FunctionDecl that has been explicitly specialization or
     266                 : /// instantiated from a function template.
     267              516: class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode {
     268                 : public:
     269                 :   /// \brief The function template specialization that this structure
     270                 :   /// describes.
     271                 :   FunctionDecl *Function;
     272                 : 
     273                 :   /// \brief The function template from which this function template
     274                 :   /// specialization was generated.
     275                 :   ///
     276                 :   /// The two bits are contain the top 4 values of TemplateSpecializationKind.
     277                 :   llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
     278                 : 
     279                 :   /// \brief The template arguments used to produce the function template
     280                 :   /// specialization from the function template.
     281                 :   const TemplateArgumentList *TemplateArguments;
     282                 : 
     283                 :   /// \brief The point at which this function template specialization was
     284                 :   /// first instantiated. 
     285                 :   SourceLocation PointOfInstantiation;
     286                 :   
     287                 :   /// \brief Retrieve the template from which this function was specialized.
     288              179:   FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
     289                 : 
     290                 :   /// \brief Determine what kind of template specialization this is.
     291             1461:   TemplateSpecializationKind getTemplateSpecializationKind() const {
     292             1461:     return (TemplateSpecializationKind)(Template.getInt() + 1);
     293                 :   }
     294                 : 
     295                 :   /// \brief Set the template specialization kind.
     296              119:   void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
     297                 :     assert(TSK != TSK_Undeclared &&
                        0: branch 0 not taken
                        0: branch 1 not taken
     298              119:          "Cannot encode TSK_Undeclared for a function template specialization");
     299              119:     Template.setInt(TSK - 1);
     300              119:   }
     301                 : 
     302                 :   /// \brief Retrieve the first point of instantiation of this function
     303                 :   /// template specialization.
     304                 :   ///
     305                 :   /// The point of instantiation may be an invalid source location if this
     306                 :   /// function has yet to be instantiated.
     307              405:   SourceLocation getPointOfInstantiation() const { 
     308              405:     return PointOfInstantiation; 
     309                 :   }
     310                 :   
     311                 :   /// \brief Set the (first) point of instantiation of this function template
     312                 :   /// specialization.
     313              354:   void setPointOfInstantiation(SourceLocation POI) {
     314              354:     PointOfInstantiation = POI;
     315              354:   }
     316                 :   
     317              619:   void Profile(llvm::FoldingSetNodeID &ID) {
     318                 :     Profile(ID, TemplateArguments->getFlatArgumentList(),
     319                 :             TemplateArguments->flat_size(),
     320              619:             Function->getASTContext());
     321              619:   }
     322                 : 
     323                 :   static void
     324                 :   Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
     325             1579:           unsigned NumTemplateArgs, ASTContext &Context) {
     326             1579:     ID.AddInteger(NumTemplateArgs);
                     1747: branch 0 taken
                     1579: branch 1 taken
     327             3326:     for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
     328             1747:       TemplateArgs[Arg].Profile(ID, Context);
     329             1579:   }
     330                 : };
     331                 : 
     332                 : /// \brief Provides information a specialization of a member of a class 
     333                 : /// template, which may be a member function, static data member, or
     334                 : /// member class.
     335                 : class MemberSpecializationInfo {
     336                 :   // The member declaration from which this member was instantiated, and the
     337                 :   // manner in which the instantiation occurred (in the lower two bits).
     338                 :   llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
     339                 :   
     340                 :   // The point at which this member was first instantiated.
     341                 :   SourceLocation PointOfInstantiation;
     342                 :   
     343                 : public:
     344                 :   explicit 
     345             1001:   MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK)
     346             1001:     : MemberAndTSK(IF, TSK - 1), PointOfInstantiation() {
     347                 :     assert(TSK != TSK_Undeclared && 
                        0: branch 0 not taken
                     1001: branch 1 taken
     348             1001:            "Cannot encode undeclared template specializations for members");
     349             1001:   }
     350                 :   
     351                 :   /// \brief Retrieve the member declaration from which this member was
     352                 :   /// instantiated.
     353             1775:   NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
     354                 :   
     355                 :   /// \brief Determine what kind of template specialization this is.
     356             2499:   TemplateSpecializationKind getTemplateSpecializationKind() const {
     357             2499:     return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
     358                 :   }
     359                 :   
     360                 :   /// \brief Set the template specialization kind.
     361              372:   void setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
     362                 :     assert(TSK != TSK_Undeclared && 
                      372: branch 1 taken
     363              372:            "Cannot encode undeclared template specializations for members");
     364              372:     MemberAndTSK.setInt(TSK - 1);
     365              372:   }
     366                 :   
     367                 :   /// \brief Retrieve the first point of instantiation of this member. 
     368                 :   /// If the point of instantiation is an invalid location, then this member
     369                 :   /// has not yet been instantiated.
     370              895:   SourceLocation getPointOfInstantiation() const { 
     371              895:     return PointOfInstantiation; 
     372                 :   }
     373                 :   
     374                 :   /// \brief Set the first point of instantiation.
     375              570:   void setPointOfInstantiation(SourceLocation POI) {
     376              570:     PointOfInstantiation = POI;
     377              570:   }
     378                 : };
     379                 :   
     380                 : /// Declaration of a template function.
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 5 not taken
                        0: branch 6 not taken
     381                0: class FunctionTemplateDecl : public TemplateDecl {
     382                 : protected:
     383                 :   /// \brief Data that is common to all of the declarations of a given
     384                 :   /// function template.
     385                 :   struct Common {
     386              359:     Common() : InstantiatedFromMember(0, false) { }
     387                 : 
     388                 :     /// \brief The function template specializations for this function
     389                 :     /// template, including explicit specializations and instantiations.
     390                 :     llvm::FoldingSet<FunctionTemplateSpecializationInfo> Specializations;
     391                 : 
     392                 :     /// \brief The member function template from which this was most
     393                 :     /// directly instantiated (or null).
     394                 :     ///
     395                 :     /// The boolean value indicates whether this member function template
     396                 :     /// was explicitly specialized.
     397                 :     llvm::PointerIntPair<FunctionTemplateDecl*, 1, bool> InstantiatedFromMember;
     398                 :   };
     399                 : 
     400                 :   /// \brief A pointer to the previous declaration (if this is a redeclaration)
     401                 :   /// or to the data that is common to all declarations of this function
     402                 :   /// template.
     403                 :   llvm::PointerUnion<Common*, FunctionTemplateDecl*> CommonOrPrev;
     404                 : 
     405                 :   /// \brief Retrieves the "common" pointer shared by all
     406                 :   /// (re-)declarations of the same function template. Calling this routine
     407                 :   /// may implicitly allocate memory for the common pointer.
     408                 :   Common *getCommonPtr();
     409                 : 
     410                 :   FunctionTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
     411              504:                        TemplateParameterList *Params, NamedDecl *Decl)
     412                 :     : TemplateDecl(FunctionTemplate, DC, L, Name, Params, Decl),
     413              504:       CommonOrPrev((Common*)0) { }
     414                 : 
     415                 : public:
     416                 :   void Destroy(ASTContext &C);
     417                 : 
     418                 :   /// Get the underlying function declaration of the template.
     419             6268:   FunctionDecl *getTemplatedDecl() const {
     420             6268:     return static_cast<FunctionDecl*>(TemplatedDecl);
     421                 :   }
     422                 : 
     423                 :   /// \brief Retrieve the set of function template specializations of this
     424                 :   /// function template.
     425             1562:   llvm::FoldingSet<FunctionTemplateSpecializationInfo> &getSpecializations() {
     426             1562:     return getCommonPtr()->Specializations;
     427                 :   }
     428                 : 
     429                 :   /// \brief Retrieve the previous declaration of this function template, or
     430                 :   /// NULL if no such declaration exists.
     431                 :   const FunctionTemplateDecl *getPreviousDeclaration() const {
     432                 :     return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>();
     433                 :   }
     434                 : 
     435                 :   /// \brief Retrieve the previous declaration of this function template, or
     436                 :   /// NULL if no such declaration exists.
     437             9281:   FunctionTemplateDecl *getPreviousDeclaration() {
     438             9281:     return CommonOrPrev.dyn_cast<FunctionTemplateDecl*>();
     439                 :   }
     440                 : 
     441                 :   /// \brief Set the previous declaration of this function template.
     442               35:   void setPreviousDeclaration(FunctionTemplateDecl *Prev) {
                       35: branch 0 taken
                        0: branch 1 not taken
     443               35:     if (Prev)
     444               35:       CommonOrPrev = Prev;
     445               35:   }
     446                 : 
     447                 :   virtual FunctionTemplateDecl *getCanonicalDecl();
     448                 : 
     449                 :   /// \brief Retrieve the member function template that this function template
     450                 :   /// was instantiated from.
     451                 :   ///
     452                 :   /// This routine will return non-NULL for member function templates of
     453                 :   /// class templates.  For example, given:
     454                 :   ///
     455                 :   /// \code
     456                 :   /// template <typename T>
     457                 :   /// struct X {
     458                 :   ///   template <typename U> void f();
     459                 :   /// };
     460                 :   /// \endcode
     461                 :   ///
     462                 :   /// X<int>::A<float> is a CXXMethodDecl (whose parent is X<int>, a
     463                 :   /// ClassTemplateSpecializationDecl) for which getPrimaryTemplate() will
     464                 :   /// return X<int>::f, a FunctionTemplateDecl (whose parent is again
     465                 :   /// X<int>) for which getInstantiatedFromMemberTemplate() will return
     466                 :   /// X<T>::f, a FunctionTemplateDecl (whose parent is X<T>, a
     467                 :   /// ClassTemplateDecl).
     468                 :   ///
     469                 :   /// \returns NULL if this is not an instantiation of a member function
     470                 :   /// template.
     471              524:   FunctionTemplateDecl *getInstantiatedFromMemberTemplate() {
     472              524:     return getCommonPtr()->InstantiatedFromMember.getPointer();
     473                 :   }
     474                 : 
     475               52:   void setInstantiatedFromMemberTemplate(FunctionTemplateDecl *FTD) {
                        0: branch 2 not taken
                       52: branch 3 taken
     476               52:     assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
     477               52:     getCommonPtr()->InstantiatedFromMember.setPointer(FTD);
     478               52:   }
     479                 : 
     480                 :   /// \brief Determines whether this template was a specialization of a 
     481                 :   /// member template.
     482                 :   ///
     483                 :   /// In the following example, the function template \c X<int>::f is a 
     484                 :   /// member specialization.
     485                 :   ///
     486                 :   /// \code
     487                 :   /// template<typename T>
     488                 :   /// struct X {
     489                 :   ///   template<typename U> void f(T, U);
     490                 :   /// };
     491                 :   ///
     492                 :   /// template<> template<typename T>
     493                 :   /// void X<int>::f(int, T);
     494                 :   /// \endcode
     495              249:   bool isMemberSpecialization() {
     496              249:     return getCommonPtr()->InstantiatedFromMember.getInt();
     497                 :   }
     498                 :   
     499                 :   /// \brief Note that this member template is a specialization.
     500                4:   void setMemberSpecialization() {
     501                 :     assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
                        4: branch 2 taken
                        0: branch 3 not taken
     502                4:            "Only member templates can be member template specializations");
     503                4:     getCommonPtr()->InstantiatedFromMember.setInt(true);
     504                4:   }
     505                 :   
     506                 :   /// Create a template function node.
     507                 :   static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
     508                 :                                       SourceLocation L,
     509                 :                                       DeclarationName Name,
     510                 :                                       TemplateParameterList *Params,
     511                 :                                       NamedDecl *Decl);
     512                 : 
     513                 :   // Implement isa/cast/dyncast support
     514           315430:   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
     515                 :   static bool classof(const FunctionTemplateDecl *D) { return true; }
     516           315430:   static bool classofKind(Kind K) { return K == FunctionTemplate; }
     517                 : };
     518                 : 
     519                 : //===----------------------------------------------------------------------===//
     520                 : // Kinds of Template Parameters
     521                 : //===----------------------------------------------------------------------===//
     522                 : 
     523                 : /// The TemplateParmPosition class defines the position of a template parameter
     524                 : /// within a template parameter list. Because template parameter can be listed
     525                 : /// sequentially for out-of-line template members, each template parameter is
     526                 : /// given a Depth - the nesting of template parameter scopes - and a Position -
     527                 : /// the occurrence within the parameter list.
     528                 : /// This class is inheritedly privately by different kinds of template
     529                 : /// parameters and is not part of the Decl hierarchy. Just a facility.
     530                 : class TemplateParmPosition {
     531                 : protected:
     532                 :   // FIXME: This should probably never be called, but it's here as
     533                 :   TemplateParmPosition()
     534                 :     : Depth(0), Position(0)
     535                 :   { /* assert(0 && "Cannot create positionless template parameter"); */ }
     536                 : 
     537              535:   TemplateParmPosition(unsigned D, unsigned P)
     538              535:     : Depth(D), Position(P)
     539              535:   { }
     540                 : 
     541                 :   // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
     542                 :   // position? Maybe?
     543                 :   unsigned Depth;
     544                 :   unsigned Position;
     545                 : 
     546                 : public:
     547                 :   /// Get the nesting depth of the template parameter.
     548             4296:   unsigned getDepth() const { return Depth; }
     549                 : 
     550                 :   /// Get the position of the template parameter within its parameter list.
     551             2343:   unsigned getPosition() const { return Position; }
     552                 : 
     553                 :   /// Get the index of the template parameter within its parameter list.
     554             1346:   unsigned getIndex() const { return Position; }
     555                 : };
     556                 : 
     557                 : /// TemplateTypeParmDecl - Declaration of a template type parameter,
     558                 : /// e.g., "T" in
     559                 : /// @code
     560                 : /// template<typename T> class vector;
     561                 : /// @endcode
                        0: branch 1 not taken
     562                0: class TemplateTypeParmDecl : public TypeDecl {
     563                 :   /// \brief Whether this template type parameter was declaration with
     564                 :   /// the 'typename' keyword. If false, it was declared with the
     565                 :   /// 'class' keyword.
     566                 :   bool Typename : 1;
     567                 : 
     568                 :   /// \brief Whether this template type parameter inherited its
     569                 :   /// default argument.
     570                 :   bool InheritedDefault : 1;
     571                 : 
     572                 :   /// \brief Whether this is a parameter pack.
     573                 :   bool ParameterPack : 1;
     574                 : 
     575                 :   /// \brief The default template argument, if any.
     576                 :   TypeSourceInfo *DefaultArgument;
     577                 : 
     578                 :   TemplateTypeParmDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
     579             1868:                        bool Typename, QualType Type, bool ParameterPack)
     580                 :     : TypeDecl(TemplateTypeParm, DC, L, Id), Typename(Typename),
     581             1868:       InheritedDefault(false), ParameterPack(ParameterPack), DefaultArgument() {
     582             1868:     TypeForDecl = Type.getTypePtr();
     583             1868:   }
     584                 : 
     585                 : public:
     586                 :   static TemplateTypeParmDecl *Create(ASTContext &C, DeclContext *DC,
     587                 :                                       SourceLocation L, unsigned D, unsigned P,
     588                 :                                       IdentifierInfo *Id, bool Typename,
     589                 :                                       bool ParameterPack);
     590                 : 
     591                 :   /// \brief Whether this template type parameter was declared with
     592                 :   /// the 'typename' keyword. If not, it was declared with the 'class'
     593                 :   /// keyword.
     594              164:   bool wasDeclaredWithTypename() const { return Typename; }
     595                 : 
     596                 :   /// \brief Determine whether this template parameter has a default
     597                 :   /// argument.
     598             6770:   bool hasDefaultArgument() const { return DefaultArgument != 0; }
     599                 : 
     600                 :   /// \brief Retrieve the default argument, if any.
     601                0:   QualType getDefaultArgument() const { return DefaultArgument->getType(); }
     602                 : 
     603                 :   /// \brief Retrieves the default argument's source information, if any.
     604              146:   TypeSourceInfo *getDefaultArgumentInfo() const { return DefaultArgument; }
     605                 : 
     606                 :   /// \brief Retrieves the location of the default argument declaration.
     607                 :   SourceLocation getDefaultArgumentLoc() const;
     608                 : 
     609                 :   /// \brief Determines whether the default argument was inherited
     610                 :   /// from a previous declaration of this template.
     611                 :   bool defaultArgumentWasInherited() const { return InheritedDefault; }
     612                 : 
     613                 :   /// \brief Set the default argument for this template parameter, and
     614                 :   /// whether that default argument was inherited from another
     615                 :   /// declaration.
     616               58:   void setDefaultArgument(TypeSourceInfo *DefArg, bool Inherited) {
     617               58:     DefaultArgument = DefArg;
     618               58:     InheritedDefault = Inherited;
     619               58:   }
     620                 : 
     621                 :   /// \brief Removes the default argument of this template parameter.
     622                1:   void removeDefaultArgument() {
     623                1:     DefaultArgument = 0;
     624                1:     InheritedDefault = false;
     625                1:   }
     626                 : 
     627                 :   /// \brief Retrieve the depth of the template parameter.
     628                 :   unsigned getDepth() const;
     629                 :   
     630                 :   /// \brief Retrieve the index of the template parameter.
     631                 :   unsigned getIndex() const;
     632                 : 
     633                 :   /// \brief Returns whether this is a parameter pack.
     634            11701:   bool isParameterPack() const { return ParameterPack; }
     635                 : 
     636                 :   // Implement isa/cast/dyncast/etc.
     637            61458:   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
     638                 :   static bool classof(const TemplateTypeParmDecl *D) { return true; }
     639            61458:   static bool classofKind(Kind K) { return K == TemplateTypeParm; }
     640                 : };
     641                 : 
     642                 : /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
     643                 : /// e.g., "Size" in
     644                 : /// @code
     645                 : /// template<int Size> class array { };
     646                 : /// @endcode
     647                 : class NonTypeTemplateParmDecl
     648                3:   : public VarDecl, protected TemplateParmPosition {
     649                 :   /// \brief The default template argument, if any.
     650                 :   Expr *DefaultArgument;
     651                 : 
     652                 :   NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation L, unsigned D,
     653                 :                           unsigned P, IdentifierInfo *Id, QualType T,
     654              436:                           TypeSourceInfo *TInfo)
     655                 :     : VarDecl(NonTypeTemplateParm, DC, L, Id, T, TInfo, VarDecl::None),
     656              436:       TemplateParmPosition(D, P), DefaultArgument(0)
     657              436:   { }
     658                 : 
     659                 : public:
     660                 :   static NonTypeTemplateParmDecl *
     661                 :   Create(ASTContext &C, DeclContext *DC, SourceLocation L, unsigned D,
     662                 :          unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo);
     663                 : 
     664                 :   using TemplateParmPosition::getDepth;
     665                 :   using TemplateParmPosition::getPosition;
     666                 :   using TemplateParmPosition::getIndex;
     667                 : 
     668                 :   /// \brief Determine whether this template parameter has a default
     669                 :   /// argument.
     670             1877:   bool hasDefaultArgument() const { return DefaultArgument; }
     671                 : 
     672                 :   /// \brief Retrieve the default argument, if any.
     673              102:   Expr *getDefaultArgument() const { return DefaultArgument; }
     674                 : 
     675                 :   /// \brief Retrieve the location of the default argument, if any.
     676                 :   SourceLocation getDefaultArgumentLoc() const;
     677                 : 
     678                 :   /// \brief Set the default argument for this template parameter.
     679               72:   void setDefaultArgument(Expr *DefArg) {
     680               72:     DefaultArgument = DefArg;
     681               72:   }
     682                 : 
     683                 :   // Implement isa/cast/dyncast/etc.
     684            90559:   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
     685                 :   static bool classof(const NonTypeTemplateParmDecl *D) { return true; }
     686            90559:   static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
     687                 : };
     688                 : 
     689                 : /// TemplateTemplateParmDecl - Declares a template template parameter,
     690                 : /// e.g., "T" in
     691                 : /// @code
     692                 : /// template <template <typename> class T> class container { };
     693                 : /// @endcode
     694                 : /// A template template parameter is a TemplateDecl because it defines the
     695                 : /// name of a template and the template parameters allowable for substitution.
     696                 : class TemplateTemplateParmDecl
     697                0:   : public TemplateDecl, protected TemplateParmPosition {
     698                 : 
     699                 :   /// \brief The default template argument, if any.
     700                 :   TemplateArgumentLoc DefaultArgument;
     701                 : 
     702                 :   TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L,
     703                 :                            unsigned D, unsigned P,
     704               99:                            IdentifierInfo *Id, TemplateParameterList *Params)
     705                 :     : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
     706               99:       TemplateParmPosition(D, P), DefaultArgument()
     707               99:     { }
     708                 : 
     709                 : public:
     710                 :   static TemplateTemplateParmDecl *Create(ASTContext &C, DeclContext *DC,
     711                 :                                           SourceLocation L, unsigned D,
     712                 :                                           unsigned P, IdentifierInfo *Id,
     713                 :                                           TemplateParameterList *Params);
     714                 : 
     715                 :   using TemplateParmPosition::getDepth;
     716                 :   using TemplateParmPosition::getPosition;
     717                 :   using TemplateParmPosition::getIndex;
     718                 : 
     719                 :   /// \brief Determine whether this template parameter has a default
     720                 :   /// argument.
     721              166:   bool hasDefaultArgument() const { 
     722              166:     return !DefaultArgument.getArgument().isNull(); 
     723                 :   }
     724                 : 
     725                 :   /// \brief Retrieve the default argument, if any.
     726               89:   const TemplateArgumentLoc &getDefaultArgument() const { 
     727               89:     return DefaultArgument; 
     728                 :   }
     729                 : 
     730                 :   /// \brief Set the default argument for this template parameter.
     731               58:   void setDefaultArgument(const TemplateArgumentLoc &DefArg) {
     732               58:     DefaultArgument = DefArg;
     733               58:   }
     734                 : 
     735                 :   // Implement isa/cast/dyncast/etc.
     736            19843:   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
     737                 :   static bool classof(const TemplateTemplateParmDecl *D) { return true; }
     738            19843:   static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
     739                 : };
     740                 : 
     741                 : /// \brief Represents a class template specialization, which refers to
     742                 : /// a class template with a given set of template arguments.
     743                 : ///
     744                 : /// Class template specializations represent both explicit
     745                 : /// specialization of class templates, as in the example below, and
     746                 : /// implicit instantiations of class templates.
     747                 : ///
     748                 : /// \code
     749                 : /// template<typename T> class array;
     750                 : ///
     751                 : /// template<>
     752                 : /// class array<bool> { }; // class template specialization array<bool>
     753                 : /// \endcode
     754                 : class ClassTemplateSpecializationDecl
     755                0:   : public CXXRecordDecl, public llvm::FoldingSetNode {
     756                 : 
     757                 :   /// \brief Structure that stores information about a class template
     758                 :   /// specialization that was instantiated from a class template partial
     759                 :   /// specialization.
     760                 :   struct SpecializedPartialSpecialization {
     761                 :     /// \brief The class template partial specialization from which this
     762                 :     /// class template specialization was instantiated.
     763                 :     ClassTemplatePartialSpecializationDecl *PartialSpecialization;
     764                 : 
     765                 :     /// \brief The template argument list deduced for the class template
     766                 :     /// partial specialization itself.
     767                 :     TemplateArgumentList *TemplateArgs;
     768                 :   };
     769                 : 
     770                 :   /// \brief The template that this specialization specializes
     771                 :   llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
     772                 :     SpecializedTemplate;
     773                 : 
     774                 :   /// \brief The template arguments used to describe this specialization.
     775                 :   TemplateArgumentList TemplateArgs;
     776                 : 
     777                 :   /// \brief The point where this template was instantiated (if any)
     778                 :   SourceLocation PointOfInstantiation;
     779                 : 
     780                 :   /// \brief The kind of specialization this declaration refers to.
     781                 :   /// Really a value of type TemplateSpecializationKind.
     782                 :   unsigned SpecializationKind : 3;
     783                 : 
     784                 : protected:
     785                 :   ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK,
     786                 :                                   DeclContext *DC, SourceLocation L,
     787                 :                                   ClassTemplateDecl *SpecializedTemplate,
     788                 :                                   TemplateArgumentListBuilder &Builder,
     789                 :                                   ClassTemplateSpecializationDecl *PrevDecl);
     790                 : 
     791                 : public:
     792                 :   static ClassTemplateSpecializationDecl *
     793                 :   Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
     794                 :          ClassTemplateDecl *SpecializedTemplate,
     795                 :          TemplateArgumentListBuilder &Builder,
     796                 :          ClassTemplateSpecializationDecl *PrevDecl);
     797                 : 
     798                 :   virtual void Destroy(ASTContext& C);
     799                 : 
     800                 :   virtual void getNameForDiagnostic(std::string &S,
     801                 :                                     const PrintingPolicy &Policy,
     802                 :                                     bool Qualified) const;
     803                 : 
     804                 :   /// \brief Retrieve the template that this specialization specializes.
     805                 :   ClassTemplateDecl *getSpecializedTemplate() const;
     806                 : 
     807                 :   /// \brief Retrieve the template arguments of the class template
     808                 :   /// specialization.
     809             5731:   const TemplateArgumentList &getTemplateArgs() const {
     810             5731:     return TemplateArgs;
     811                 :   }
     812                 : 
     813                 :   /// \brief Determine the kind of specialization that this
     814                 :   /// declaration represents.
     815             4960:   TemplateSpecializationKind getSpecializationKind() const {
     816             4960:     return static_cast<TemplateSpecializationKind>(SpecializationKind);
     817                 :   }
     818                 : 
     819             1664:   void setSpecializationKind(TemplateSpecializationKind TSK) {
     820             1664:     SpecializationKind = TSK;
     821             1664:   }
     822                 : 
     823                 :   /// \brief Get the point of instantiation (if any), or null if none.
     824               30:   SourceLocation getPointOfInstantiation() const {
     825               30:     return PointOfInstantiation;
     826                 :   }
     827                 : 
     828             1381:   void setPointOfInstantiation(SourceLocation Loc) {
                     1381: branch 1 taken
                        0: branch 2 not taken
     829             1381:     assert(Loc.isValid() && "point of instantiation must be valid!");
     830             1381:     PointOfInstantiation = Loc;
     831             1381:   }
     832                 : 
     833                 :   /// \brief If this class template specialization is an instantiation of
     834                 :   /// a template (rather than an explicit specialization), return the
     835                 :   /// class template or class template partial specialization from which it
     836                 :   /// was instantiated.
     837                 :   llvm::PointerUnion<ClassTemplateDecl *,
     838                 :                      ClassTemplatePartialSpecializationDecl *>
     839                 :   getInstantiatedFrom() const {
     840                 :     if (getSpecializationKind() != TSK_ImplicitInstantiation &&
     841                 :         getSpecializationKind() != TSK_ExplicitInstantiationDefinition &&
     842                 :         getSpecializationKind() != TSK_ExplicitInstantiationDeclaration)
     843                 :       return (ClassTemplateDecl*)0;
     844                 : 
     845                 :     if (SpecializedPartialSpecialization *PartialSpec
     846                 :           = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
     847                 :       return PartialSpec->PartialSpecialization;
     848                 : 
     849                 :     return const_cast<ClassTemplateDecl*>(
     850                 :                              SpecializedTemplate.get<ClassTemplateDecl*>());
     851                 :   }
     852                 : 
     853                 :   /// \brief Retrieve the set of template arguments that should be used
     854                 :   /// to instantiate members of the class template or class template partial
     855                 :   /// specialization from which this class template specialization was
     856                 :   /// instantiated.
     857                 :   ///
     858                 :   /// \returns For a class template specialization instantiated from the primary
     859                 :   /// template, this function will return the same template arguments as
     860                 :   /// getTemplateArgs(). For a class template specialization instantiated from
     861                 :   /// a class template partial specialization, this function will return the
     862                 :   /// deduced template arguments for the class template partial specialization
     863                 :   /// itself.
     864             2169:   const TemplateArgumentList &getTemplateInstantiationArgs() const {
                      230: branch 0 taken
                     1939: branch 1 taken
     865             2169:     if (SpecializedPartialSpecialization *PartialSpec
     866             2169:         = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
     867              230:       return *PartialSpec->TemplateArgs;
     868                 : 
     869             1939:     return getTemplateArgs();
     870                 :   }
     871                 : 
     872                 :   /// \brief Note that this class template specialization is actually an
     873                 :   /// instantiation of the given class template partial specialization whose
     874                 :   /// template arguments have been deduced.
     875                 :   void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec,
     876              229:                           TemplateArgumentList *TemplateArgs) {
     877                 :     SpecializedPartialSpecialization *PS
                      229: branch 2 taken
                        0: branch 3 not taken
     878              229:       = new (getASTContext()) SpecializedPartialSpecialization();
     879              229:     PS->PartialSpecialization = PartialSpec;
     880              229:     PS->TemplateArgs = TemplateArgs;
     881              229:     SpecializedTemplate = PS;
     882              229:   }
     883                 : 
     884                 :   /// \brief Sets the type of this specialization as it was written by
     885                 :   /// the user. This will be a class template specialization type.
     886              414:   void setTypeAsWritten(QualType T) {
     887              414:     TypeForDecl = T.getTypePtr();
     888              414:   }
     889                 : 
     890             2142:   void Profile(llvm::FoldingSetNodeID &ID) const {
     891                 :     Profile(ID, TemplateArgs.getFlatArgumentList(), TemplateArgs.flat_size(),
     892             2142:             getASTContext());
     893             2142:   }
     894                 : 
     895                 :   static void
     896                 :   Profile(llvm::FoldingSetNodeID &ID, const TemplateArgument *TemplateArgs,
     897             5116:           unsigned NumTemplateArgs, ASTContext &Context) {
     898             5116:     ID.AddInteger(NumTemplateArgs);
                        0: branch 0 not taken
                        0: branch 1 not taken
     899            12386:     for (unsigned Arg = 0; Arg != NumTemplateArgs; ++Arg)
     900             7270:       TemplateArgs[Arg].Profile(ID, Context);
     901             5116:   }
     902                 : 
     903           182279:   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
     904           189361:   static bool classofKind(Kind K) {
     905                 :     return K == ClassTemplateSpecialization ||
                   170197: branch 0 taken
                    19164: branch 1 taken
                      542: branch 2 taken
                   169655: branch 3 taken
     906           189361:            K == ClassTemplatePartialSpecialization;
     907                 :   }
     908                 : 
     909                 :   static bool classof(const ClassTemplateSpecializationDecl *) {
     910                 :     return true;
     911                 :   }
     912                 : 
     913                 :   static bool classof(const ClassTemplatePartialSpecializationDecl *) {
     914                 :     return true;
     915                 :   }
     916                 : };
     917                 : 
     918                 : class ClassTemplatePartialSpecializationDecl
     919                0:   : public ClassTemplateSpecializationDecl {
     920                 :   /// \brief The list of template parameters
     921                 :   TemplateParameterList* TemplateParams;
     922                 : 
     923                 :   /// \brief The source info for the template arguments as written.
     924                 :   TemplateArgumentLoc *ArgsAsWritten;
     925                 :   unsigned NumArgsAsWritten;
     926                 : 
     927                 :   /// \brief The class template partial specialization from which this 
     928                 :   /// class template partial specialization was instantiated.
     929                 :   ///
     930                 :   /// The boolean value will be true to indicate that this class template
     931                 :   /// partial specialization was specialized at this level.
     932                 :   llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
     933                 :       InstantiatedFromMember;
     934                 :     
     935                 :   ClassTemplatePartialSpecializationDecl(ASTContext &Context,
     936                 :                                          DeclContext *DC, SourceLocation L,
     937                 :                                          TemplateParameterList *Params,
     938                 :                                          ClassTemplateDecl *SpecializedTemplate,
     939                 :                                          TemplateArgumentListBuilder &Builder,
     940                 :                                          TemplateArgumentLoc *ArgInfos,
     941                 :                                          unsigned NumArgInfos,
     942               95:                                ClassTemplatePartialSpecializationDecl *PrevDecl)
     943                 :     : ClassTemplateSpecializationDecl(Context,
     944                 :                                       ClassTemplatePartialSpecialization,
     945                 :                                       DC, L, SpecializedTemplate, Builder,
     946                 :                                       PrevDecl),
     947                 :       TemplateParams(Params), ArgsAsWritten(ArgInfos),
     948               95:       NumArgsAsWritten(NumArgInfos), InstantiatedFromMember(0, false) { }
     949                 : 
     950                 : public:
     951                 :   static ClassTemplatePartialSpecializationDecl *
     952                 :   Create(ASTContext &Context, DeclContext *DC, SourceLocation L,
     953                 :          TemplateParameterList *Params,
     954                 :          ClassTemplateDecl *SpecializedTemplate,
     955                 :          TemplateArgumentListBuilder &Builder,
     956                 :          const TemplateArgumentListInfo &ArgInfos,
     957                 :          ClassTemplatePartialSpecializationDecl *PrevDecl);
     958                 : 
     959                 :   /// Get the list of template parameters
     960             2143:   TemplateParameterList *getTemplateParameters() const {
     961             2143:     return TemplateParams;
     962                 :   }
     963                 : 
     964                 :   /// Get the template arguments as written.
     965              251:   TemplateArgumentLoc *getTemplateArgsAsWritten() const {
     966              251:     return ArgsAsWritten;
     967                 :   }
     968                 : 
     969                 :   /// Get the number of template arguments as written.
     970              251:   unsigned getNumTemplateArgsAsWritten() const {
     971              251:     return NumArgsAsWritten;
     972                 :   }
     973                 : 
     974                 :   /// \brief Retrieve the member class template partial specialization from
     975                 :   /// which this particular class template partial specialization was
     976                 :   /// instantiated.
     977                 :   ///
     978                 :   /// \code
     979                 :   /// template<typename T>
     980                 :   /// struct Outer {
     981                 :   ///   template<typename U> struct Inner;
     982                 :   ///   template<typename U> struct Inner<U*> { }; // #1
     983                 :   /// };
     984                 :   ///
     985                 :   /// Outer<float>::Inner<int*> ii;
     986                 :   /// \endcode
     987                 :   ///
     988                 :   /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
     989                 :   /// end up instantiating the partial specialization 
     990                 :   /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class 
     991                 :   /// template partial specialization \c Outer<T>::Inner<U*>. Given 
     992                 :   /// \c Outer<float>::Inner<U*>, this function would return
     993                 :   /// \c Outer<T>::Inner<U*>.
     994              244:   ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() {
     995                 :     ClassTemplatePartialSpecializationDecl *First
     996              244:       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
     997              244:     return First->InstantiatedFromMember.getPointer();
     998                 :   }
     999                 :   
    1000                 :   void setInstantiatedFromMember(
    1001                6:                           ClassTemplatePartialSpecializationDecl *PartialSpec) {
    1002                 :     ClassTemplatePartialSpecializationDecl *First
    1003                6:       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
    1004                6:     First->InstantiatedFromMember.setPointer(PartialSpec);
    1005                6:   }
    1006                 :     
    1007                 :   /// \brief Determines whether this class template partial specialization 
    1008                 :   /// template was a specialization of a member partial specialization.
    1009                 :   ///
    1010                 :   /// In the following example, the member template partial specialization
    1011                 :   /// \c X<int>::Inner<T*> is a member specialization.
    1012                 :   ///
    1013                 :   /// \code
    1014                 :   /// template<typename T>
    1015                 :   /// struct X {
    1016                 :   ///   template<typename U> struct Inner;
    1017                 :   ///   template<typename U> struct Inner<U*>;
    1018                 :   /// };
    1019                 :   ///
    1020                 :   /// template<> template<typename T>
    1021                 :   /// struct X<int>::Inner<T*> { /* ... */ };
    1022                 :   /// \endcode
    1023                5:   bool isMemberSpecialization() {
    1024                 :     ClassTemplatePartialSpecializationDecl *First
    1025                5:       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
    1026                5:     return First->InstantiatedFromMember.getInt();
    1027                 :   }
    1028                 :   
    1029                 :   /// \brief Note that this member template is a specialization.
    1030                1:   void setMemberSpecialization() {
    1031                 :     ClassTemplatePartialSpecializationDecl *First
    1032                1:       = cast<ClassTemplatePartialSpecializationDecl>(getFirstDeclaration());
    1033                 :     assert(First->InstantiatedFromMember.getPointer() &&
    1034                1:            "Only member templates can be member template specializations");
    1035                1:     return First->InstantiatedFromMember.setInt(true);
    1036                 :   }
    1037                 :     
    1038                 :   // FIXME: Add Profile support!
    1039                 : 
    1040              930:   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
    1041           100572:   static bool classofKind(Kind K) {
    1042           100572:     return K == ClassTemplatePartialSpecialization;
    1043                 :   }
    1044                 : 
    1045                 :   static bool classof(const ClassTemplatePartialSpecializationDecl *) {
    1046                 :     return true;
    1047                 :   }
    1048                 : };
    1049                 : 
    1050                 : /// Declaration of a class template.
    1051                 : class ClassTemplateDecl : public TemplateDecl {
    1052                 : protected:
    1053                 :   /// \brief Data that is common to all of the declarations of a given
    1054                 :   /// class template.
    1055                0:   struct Common {
    1056              995:     Common() : InstantiatedFromMember(0, 0) {}
    1057                 : 
    1058                 :     /// \brief The class template specializations for this class
    1059                 :     /// template, including explicit specializations and instantiations.
    1060                 :     llvm::FoldingSet<ClassTemplateSpecializationDecl> Specializations;
    1061                 : 
    1062                 :     /// \brief The class template partial specializations for this class
    1063                 :     /// template.
    1064                 :     llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>
    1065                 :       PartialSpecializations;
    1066                 : 
    1067                 :     /// \brief The injected-class-name type for this class template.
    1068                 :     QualType InjectedClassNameType;
    1069                 : 
    1070                 :     /// \brief The templated member class from which this was most
    1071                 :     /// directly instantiated (or null).
    1072                 :     ///
    1073                 :     /// The boolean value indicates whether this member class template
    1074                 :     /// was explicitly specialized.
    1075                 :     llvm::PointerIntPair<ClassTemplateDecl *, 1, bool> InstantiatedFromMember;
    1076                 :   };
    1077                 : 
    1078                 :   // FIXME: Combine PreviousDeclaration with CommonPtr, as in 
    1079                 :   // FunctionTemplateDecl.
    1080                 :   
    1081                 :   /// \brief Previous declaration of this class template.
    1082                 :   ClassTemplateDecl *PreviousDeclaration;
    1083                 : 
    1084                 :   /// \brief Pointer to the data that is common to all of the
    1085                 :   /// declarations of this class template.
    1086                 :   ///
    1087                 :   /// The first declaration of a class template (e.g., the declaration
    1088                 :   /// with no "previous declaration") owns this pointer.
    1089                 :   Common *CommonPtr;
    1090                 : 
    1091                 :   ClassTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
    1092                 :                     TemplateParameterList *Params, NamedDecl *Decl,
    1093             1036:                     ClassTemplateDecl *PrevDecl, Common *CommonPtr)
    1094                 :     : TemplateDecl(ClassTemplate, DC, L, Name, Params, Decl),
    1095             1036:       PreviousDeclaration(PrevDecl), CommonPtr(CommonPtr) { }
    1096                 : 
    1097                 :   ~ClassTemplateDecl();
    1098                 : 
    1099                 : public:
    1100                 :   /// Get the underlying class declarations of the template.
    1101             4006:   CXXRecordDecl *getTemplatedDecl() const {
    1102             4006:     return static_cast<CXXRecordDecl *>(TemplatedDecl);
    1103                 :   }
    1104                 : 
    1105                 :   /// \brief Retrieve the previous declaration of this template.
    1106             3152:   ClassTemplateDecl *getPreviousDeclaration() const {
    1107             3152:     return PreviousDeclaration;
    1108                 :   }
    1109                 : 
    1110                 :   virtual ClassTemplateDecl *getCanonicalDecl();
    1111                 : 
    1112                 :   /// Create a class template node.
    1113                 :   static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
    1114                 :                                    SourceLocation L,
    1115                 :                                    DeclarationName Name,
    1116                 :                                    TemplateParameterList *Params,
    1117                 :                                    NamedDecl *Decl,
    1118                 :                                    ClassTemplateDecl *PrevDecl);
    1119                 : 
    1120                 :   /// \brief Retrieve the set of specializations of this class template.
    1121             4495:   llvm::FoldingSet<ClassTemplateSpecializationDecl> &getSpecializations() {
    1122             4495:     return CommonPtr->Specializations;
    1123                 :   }
    1124                 : 
    1125                 :   /// \brief Retrieve the set of partial specializations of this class
    1126                 :   /// template.
    1127                 :   llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &
    1128             3188:   getPartialSpecializations() {
    1129             3188:     return CommonPtr->PartialSpecializations;
    1130                 :   }
    1131                 : 
    1132                 :   /// \brief Find a class template partial specialization with the given
    1133                 :   /// type T.
    1134                 :   ///
    1135                 :   /// \brief A dependent type that names a specialization of this class
    1136                 :   /// template.
    1137                 :   ///
    1138                 :   /// \returns the class template partial specialization that exactly matches
    1139                 :   /// the type \p T, or NULL if no such partial specialization exists.
    1140                 :   ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
    1141                 : 
    1142                 :   /// \brief Retrieve the type of the injected-class-name for this
    1143                 :   /// class template.
    1144                 :   ///
    1145                 :   /// The injected-class-name for a class template \c X is \c
    1146                 :   /// X<template-args>, where \c template-args is formed from the
    1147                 :   /// template arguments that correspond to the template parameters of
    1148                 :   /// \c X. For example:
    1149                 :   ///
    1150                 :   /// \code
    1151                 :   /// template<typename T, int N>
    1152                 :   /// struct array {
    1153                 :   ///   typedef array this_type; // "array" is equivalent to "array<T, N>"
    1154                 :   /// };
    1155                 :   /// \endcode
    1156                 :   QualType getInjectedClassNameType(ASTContext &Context);
    1157                 : 
    1158                 :   /// \brief Retrieve the member class template that this class template was
    1159                 :   /// derived from.
    1160                 :   ///
    1161                 :   /// This routine will return non-NULL for templated member classes of
    1162                 :   /// class templates.  For example, given:
    1163                 :   ///
    1164                 :   /// \code
    1165                 :   /// template <typename T>
    1166                 :   /// struct X {
    1167                 :   ///   template <typename U> struct A {};
    1168                 :   /// };
    1169                 :   /// \endcode
    1170                 :   ///
    1171                 :   /// X<int>::A<float> is a ClassTemplateSpecializationDecl (whose parent
    1172                 :   /// is X<int>, also a CTSD) for which getSpecializedTemplate() will
    1173                 :   /// return X<int>::A<U>, a TemplateClassDecl (whose parent is again
    1174                 :   /// X<int>) for which getInstantiatedFromMemberTemplate() will return
    1175                 :   /// X<T>::A<U>, a TemplateClassDecl (whose parent is X<T>, also a TCD).
    1176                 :   ///
    1177                 :   /// \returns null if this is not an instantiation of a member class template.
    1178             1295:   ClassTemplateDecl *getInstantiatedFromMemberTemplate() const {
    1179             1295:     return CommonPtr->InstantiatedFromMember.getPointer();
    1180                 :   }
    1181                 : 
    1182               71:   void setInstantiatedFromMemberTemplate(ClassTemplateDecl *CTD) {
    1183               71:     assert(!CommonPtr->InstantiatedFromMember.getPointer());
    1184               71:     CommonPtr->InstantiatedFromMember.setPointer(CTD);
    1185               71:   }
    1186                 : 
    1187                 :   /// \brief Determines whether this template was a specialization of a 
    1188                 :   /// member template.
    1189                 :   ///
    1190                 :   /// In the following example, the member template \c X<int>::Inner is a 
    1191                 :   /// member specialization.
    1192                 :   ///
    1193                 :   /// \code
    1194                 :   /// template<typename T>
    1195                 :   /// struct X {
    1196                 :   ///   template<typename U> struct Inner;
    1197                 :   /// };
    1198                 :   ///
    1199                 :   /// template<> template<typename T>
    1200                 :   /// struct X<int>::Inner { /* ... */ };
    1201                 :   /// \endcode
    1202             2205:   bool isMemberSpecialization() {
    1203             2205:     return CommonPtr->InstantiatedFromMember.getInt();
    1204                 :   }
    1205                 :   
    1206                 :   /// \brief Note that this member template is a specialization.
    1207                2:   void setMemberSpecialization() {
    1208                 :     assert(CommonPtr->InstantiatedFromMember.getPointer() &&
    1209                2:            "Only member templates can be member template specializations");
    1210                2:     CommonPtr->InstantiatedFromMember.setInt(true);
    1211                2:   }
    1212                 :   
    1213                 :   // Implement isa/cast/dyncast support
    1214            18319:   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
    1215                 :   static bool classof(const ClassTemplateDecl *D) { return true; }
    1216            18319:   static bool classofKind(Kind K) { return K == ClassTemplate; }
    1217                 : 
    1218                 :   virtual void Destroy(ASTContext& C);
    1219                 : };
    1220                 : 
    1221                 : /// Declaration of a friend template.  For example:
    1222                 : ///
    1223                 : /// template <typename T> class A {
    1224                 : ///   friend class MyVector<T>; // not a friend template
    1225                 : ///   template <typename U> friend class B; // friend template
    1226                 : ///   template <typename U> friend class Foo<T>::Nested; // friend template
    1227                0: class FriendTemplateDecl : public Decl {
    1228                 : public:
    1229                 :   typedef llvm::PointerUnion<NamedDecl*,Type*> FriendUnion;
    1230                 : 
    1231                 : private:
    1232                 :   // The number of template parameters;  always non-zero.
    1233                 :   unsigned NumParams;
    1234                 : 
    1235                 :   // The parameter list.
    1236                 :   TemplateParameterList **Params;
    1237                 : 
    1238                 :   // The declaration that's a friend of this class.
    1239                 :   FriendUnion Friend;
    1240                 : 
    1241                 :   // Location of the 'friend' specifier.
    1242                 :   SourceLocation FriendLoc;
    1243                 : 
    1244                 : 
    1245                 :   FriendTemplateDecl(DeclContext *DC, SourceLocation Loc,
    1246                 :                      unsigned NParams, 
    1247                 :                      TemplateParameterList **Params,
    1248                 :                      FriendUnion Friend,
    1249                0:                      SourceLocation FriendLoc)
    1250                 :     : Decl(Decl::FriendTemplate, DC, Loc),
    1251                 :       NumParams(NParams),
    1252                 :       Params(Params),
    1253                 :       Friend(Friend),
    1254                0:       FriendLoc(FriendLoc)
    1255                0:   {}
    1256                 : 
    1257                 : public:
    1258                 :   static FriendTemplateDecl *Create(ASTContext &Context,
    1259                 :                                     DeclContext *DC, SourceLocation Loc,
    1260                 :                                     unsigned NParams, 
    1261                 :                                     TemplateParameterList **Params,
    1262                 :                                     FriendUnion Friend,
    1263                 :                                     SourceLocation FriendLoc);
    1264                 : 
    1265                 :   /// If this friend declaration names a templated type (or
    1266                 :   /// a dependent member type of a templated type), return that
    1267                 :   /// type;  otherwise return null.
    1268                 :   Type *getFriendType() const {
    1269                 :     return Friend.dyn_cast<Type*>();
    1270                 :   }
    1271                 : 
    1272                 :   /// If this friend declaration names a templated function (or
    1273                 :   /// a member function of a templated type), return that type;
    1274                 :   /// otherwise return null.
    1275                0:   NamedDecl *getFriendDecl() const {
    1276                0:     return Friend.dyn_cast<NamedDecl*>();
    1277                 :   }
    1278                 : 
    1279                 :   /// Retrieves the location of the 'friend' keyword.
    1280                 :   SourceLocation getFriendLoc() const {
    1281                 :     return FriendLoc;
    1282                 :   }
    1283                 : 
    1284                 :   TemplateParameterList *getTemplateParameterList(unsigned i) const {
    1285                 :     assert(i <= NumParams);
    1286                 :     return Params[i];
    1287                 :   }
    1288                 : 
    1289                 :   unsigned getNumTemplateParameters() const {
    1290                 :     return NumParams;
    1291                 :   }
    1292                 : 
    1293                 :   // Implement isa/cast/dyncast/etc.
    1294                0:   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
    1295                0:   static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
    1296                 :   static bool classof(const FriendTemplateDecl *D) { return true; }
    1297                 : };
    1298                 : 
    1299                 : /// Implementation of inline functions that require the template declarations
    1300                 : inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
    1301                 :   : Function(FTD) { }
    1302                 : 
    1303                 : } /* end of namespace clang */
    1304                 : 
    1305                 : #endif

Generated: 2010-02-10 01:31 by zcov