Home | History | Annotate | Download | only in AST
      1 //===--- TemplateName.h - C++ Template Name Representation-------*- 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 TemplateName interface and subclasses.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_AST_TEMPLATENAME_H
     15 #define LLVM_CLANG_AST_TEMPLATENAME_H
     16 
     17 #include "clang/AST/NestedNameSpecifier.h"
     18 #include "clang/Basic/LLVM.h"
     19 #include "llvm/ADT/FoldingSet.h"
     20 #include "llvm/ADT/PointerUnion.h"
     21 
     22 namespace clang {
     23 
     24 class ASTContext;
     25 class DependentTemplateName;
     26 class DiagnosticBuilder;
     27 class IdentifierInfo;
     28 class NamedDecl;
     29 class NestedNameSpecifier;
     30 enum OverloadedOperatorKind : int;
     31 class OverloadedTemplateStorage;
     32 struct PrintingPolicy;
     33 class QualifiedTemplateName;
     34 class SubstTemplateTemplateParmPackStorage;
     35 class SubstTemplateTemplateParmStorage;
     36 class TemplateArgument;
     37 class TemplateDecl;
     38 class TemplateTemplateParmDecl;
     39 
     40 /// \brief Implementation class used to describe either a set of overloaded
     41 /// template names or an already-substituted template template parameter pack.
     42 class UncommonTemplateNameStorage {
     43 protected:
     44   enum Kind {
     45     Overloaded,
     46     SubstTemplateTemplateParm,
     47     SubstTemplateTemplateParmPack
     48   };
     49 
     50   struct BitsTag {
     51     /// \brief A Kind.
     52     unsigned Kind : 2;
     53 
     54     /// \brief The number of stored templates or template arguments,
     55     /// depending on which subclass we have.
     56     unsigned Size : 30;
     57   };
     58 
     59   union {
     60     struct BitsTag Bits;
     61     void *PointerAlignment;
     62   };
     63 
     64   UncommonTemplateNameStorage(Kind kind, unsigned size) {
     65     Bits.Kind = kind;
     66     Bits.Size = size;
     67   }
     68 
     69 public:
     70   unsigned size() const { return Bits.Size; }
     71 
     72   OverloadedTemplateStorage *getAsOverloadedStorage()  {
     73     return Bits.Kind == Overloaded
     74              ? reinterpret_cast<OverloadedTemplateStorage *>(this)
     75              : nullptr;
     76   }
     77 
     78   SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
     79     return Bits.Kind == SubstTemplateTemplateParm
     80              ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
     81              : nullptr;
     82   }
     83 
     84   SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
     85     return Bits.Kind == SubstTemplateTemplateParmPack
     86              ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
     87              : nullptr;
     88   }
     89 };
     90 
     91 /// \brief A structure for storing the information associated with an
     92 /// overloaded template name.
     93 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
     94   friend class ASTContext;
     95 
     96   OverloadedTemplateStorage(unsigned size)
     97     : UncommonTemplateNameStorage(Overloaded, size) { }
     98 
     99   NamedDecl **getStorage() {
    100     return reinterpret_cast<NamedDecl **>(this + 1);
    101   }
    102   NamedDecl * const *getStorage() const {
    103     return reinterpret_cast<NamedDecl *const *>(this + 1);
    104   }
    105 
    106 public:
    107   typedef NamedDecl *const *iterator;
    108 
    109   iterator begin() const { return getStorage(); }
    110   iterator end() const { return getStorage() + size(); }
    111 };
    112 
    113 /// \brief A structure for storing an already-substituted template template
    114 /// parameter pack.
    115 ///
    116 /// This kind of template names occurs when the parameter pack has been
    117 /// provided with a template template argument pack in a context where its
    118 /// enclosing pack expansion could not be fully expanded.
    119 class SubstTemplateTemplateParmPackStorage
    120   : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
    121 {
    122   TemplateTemplateParmDecl *Parameter;
    123   const TemplateArgument *Arguments;
    124 
    125 public:
    126   SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
    127                                        unsigned Size,
    128                                        const TemplateArgument *Arguments)
    129     : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
    130       Parameter(Parameter), Arguments(Arguments) { }
    131 
    132   /// \brief Retrieve the template template parameter pack being substituted.
    133   TemplateTemplateParmDecl *getParameterPack() const {
    134     return Parameter;
    135   }
    136 
    137   /// \brief Retrieve the template template argument pack with which this
    138   /// parameter was substituted.
    139   TemplateArgument getArgumentPack() const;
    140 
    141   void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
    142 
    143   static void Profile(llvm::FoldingSetNodeID &ID,
    144                       ASTContext &Context,
    145                       TemplateTemplateParmDecl *Parameter,
    146                       const TemplateArgument &ArgPack);
    147 };
    148 
    149 /// \brief Represents a C++ template name within the type system.
    150 ///
    151 /// A C++ template name refers to a template within the C++ type
    152 /// system. In most cases, a template name is simply a reference to a
    153 /// class template, e.g.
    154 ///
    155 /// \code
    156 /// template<typename T> class X { };
    157 ///
    158 /// X<int> xi;
    159 /// \endcode
    160 ///
    161 /// Here, the 'X' in \c X<int> is a template name that refers to the
    162 /// declaration of the class template X, above. Template names can
    163 /// also refer to function templates, C++0x template aliases, etc.
    164 ///
    165 /// Some template names are dependent. For example, consider:
    166 ///
    167 /// \code
    168 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
    169 ///   typedef typename MetaFun::template apply<T1, T2>::type type;
    170 /// };
    171 /// \endcode
    172 ///
    173 /// Here, "apply" is treated as a template name within the typename
    174 /// specifier in the typedef. "apply" is a nested template, and can
    175 /// only be understood in the context of
    176 class TemplateName {
    177   typedef llvm::PointerUnion4<TemplateDecl *,
    178                               UncommonTemplateNameStorage *,
    179                               QualifiedTemplateName *,
    180                               DependentTemplateName *> StorageType;
    181 
    182   StorageType Storage;
    183 
    184   explicit TemplateName(void *Ptr);
    185 
    186 public:
    187   // \brief Kind of name that is actually stored.
    188   enum NameKind {
    189     /// \brief A single template declaration.
    190     Template,
    191     /// \brief A set of overloaded template declarations.
    192     OverloadedTemplate,
    193     /// \brief A qualified template name, where the qualification is kept
    194     /// to describe the source code as written.
    195     QualifiedTemplate,
    196     /// \brief A dependent template name that has not been resolved to a
    197     /// template (or set of templates).
    198     DependentTemplate,
    199     /// \brief A template template parameter that has been substituted
    200     /// for some other template name.
    201     SubstTemplateTemplateParm,
    202     /// \brief A template template parameter pack that has been substituted for
    203     /// a template template argument pack, but has not yet been expanded into
    204     /// individual arguments.
    205     SubstTemplateTemplateParmPack
    206   };
    207 
    208   TemplateName() : Storage() { }
    209   explicit TemplateName(TemplateDecl *Template);
    210   explicit TemplateName(OverloadedTemplateStorage *Storage);
    211   explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
    212   explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage);
    213   explicit TemplateName(QualifiedTemplateName *Qual);
    214   explicit TemplateName(DependentTemplateName *Dep);
    215 
    216   /// \brief Determine whether this template name is NULL.
    217   bool isNull() const;
    218 
    219   // \brief Get the kind of name that is actually stored.
    220   NameKind getKind() const;
    221 
    222   /// \brief Retrieve the underlying template declaration that
    223   /// this template name refers to, if known.
    224   ///
    225   /// \returns The template declaration that this template name refers
    226   /// to, if any. If the template name does not refer to a specific
    227   /// declaration because it is a dependent name, or if it refers to a
    228   /// set of function templates, returns NULL.
    229   TemplateDecl *getAsTemplateDecl() const;
    230 
    231   /// \brief Retrieve the underlying, overloaded function template
    232   // declarations that this template name refers to, if known.
    233   ///
    234   /// \returns The set of overloaded function templates that this template
    235   /// name refers to, if known. If the template name does not refer to a
    236   /// specific set of function templates because it is a dependent name or
    237   /// refers to a single template, returns NULL.
    238   OverloadedTemplateStorage *getAsOverloadedTemplate() const;
    239 
    240   /// \brief Retrieve the substituted template template parameter, if
    241   /// known.
    242   ///
    243   /// \returns The storage for the substituted template template parameter,
    244   /// if known. Otherwise, returns NULL.
    245   SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const;
    246 
    247   /// \brief Retrieve the substituted template template parameter pack, if
    248   /// known.
    249   ///
    250   /// \returns The storage for the substituted template template parameter pack,
    251   /// if known. Otherwise, returns NULL.
    252   SubstTemplateTemplateParmPackStorage *
    253   getAsSubstTemplateTemplateParmPack() const;
    254 
    255   /// \brief Retrieve the underlying qualified template name
    256   /// structure, if any.
    257   QualifiedTemplateName *getAsQualifiedTemplateName() const;
    258 
    259   /// \brief Retrieve the underlying dependent template name
    260   /// structure, if any.
    261   DependentTemplateName *getAsDependentTemplateName() const;
    262 
    263   TemplateName getUnderlying() const;
    264 
    265   /// Get the template name to substitute when this template name is used as a
    266   /// template template argument. This refers to the most recent declaration of
    267   /// the template, including any default template arguments.
    268   TemplateName getNameToSubstitute() const;
    269 
    270   /// \brief Determines whether this is a dependent template name.
    271   bool isDependent() const;
    272 
    273   /// \brief Determines whether this is a template name that somehow
    274   /// depends on a template parameter.
    275   bool isInstantiationDependent() const;
    276 
    277   /// \brief Determines whether this template name contains an
    278   /// unexpanded parameter pack (for C++0x variadic templates).
    279   bool containsUnexpandedParameterPack() const;
    280 
    281   /// \brief Print the template name.
    282   ///
    283   /// \param OS the output stream to which the template name will be
    284   /// printed.
    285   ///
    286   /// \param SuppressNNS if true, don't print the
    287   /// nested-name-specifier that precedes the template name (if it has
    288   /// one).
    289   void print(raw_ostream &OS, const PrintingPolicy &Policy,
    290              bool SuppressNNS = false) const;
    291 
    292   /// \brief Debugging aid that dumps the template name.
    293   void dump(raw_ostream &OS) const;
    294 
    295   /// \brief Debugging aid that dumps the template name to standard
    296   /// error.
    297   void dump() const;
    298 
    299   void Profile(llvm::FoldingSetNodeID &ID) {
    300     ID.AddPointer(Storage.getOpaqueValue());
    301   }
    302 
    303   /// \brief Retrieve the template name as a void pointer.
    304   void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
    305 
    306   /// \brief Build a template name from a void pointer.
    307   static TemplateName getFromVoidPointer(void *Ptr) {
    308     return TemplateName(Ptr);
    309   }
    310 };
    311 
    312 /// Insertion operator for diagnostics.  This allows sending TemplateName's
    313 /// into a diagnostic with <<.
    314 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
    315                                     TemplateName N);
    316 
    317 /// \brief A structure for storing the information associated with a
    318 /// substituted template template parameter.
    319 class SubstTemplateTemplateParmStorage
    320   : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
    321   friend class ASTContext;
    322 
    323   TemplateTemplateParmDecl *Parameter;
    324   TemplateName Replacement;
    325 
    326   SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
    327                                    TemplateName replacement)
    328     : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
    329       Parameter(parameter), Replacement(replacement) {}
    330 
    331 public:
    332   TemplateTemplateParmDecl *getParameter() const { return Parameter; }
    333   TemplateName getReplacement() const { return Replacement; }
    334 
    335   void Profile(llvm::FoldingSetNodeID &ID);
    336 
    337   static void Profile(llvm::FoldingSetNodeID &ID,
    338                       TemplateTemplateParmDecl *parameter,
    339                       TemplateName replacement);
    340 };
    341 
    342 inline TemplateName TemplateName::getUnderlying() const {
    343   if (SubstTemplateTemplateParmStorage *subst
    344         = getAsSubstTemplateTemplateParm())
    345     return subst->getReplacement().getUnderlying();
    346   return *this;
    347 }
    348 
    349 /// \brief Represents a template name that was expressed as a
    350 /// qualified name.
    351 ///
    352 /// This kind of template name refers to a template name that was
    353 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
    354 /// the nested name specifier is "std::" and the template name is the
    355 /// declaration for "vector". The QualifiedTemplateName class is only
    356 /// used to provide "sugar" for template names that were expressed
    357 /// with a qualified name, and has no semantic meaning. In this
    358 /// manner, it is to TemplateName what ElaboratedType is to Type,
    359 /// providing extra syntactic sugar for downstream clients.
    360 class QualifiedTemplateName : public llvm::FoldingSetNode {
    361   /// \brief The nested name specifier that qualifies the template name.
    362   ///
    363   /// The bit is used to indicate whether the "template" keyword was
    364   /// present before the template name itself. Note that the
    365   /// "template" keyword is always redundant in this case (otherwise,
    366   /// the template name would be a dependent name and we would express
    367   /// this name with DependentTemplateName).
    368   llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
    369 
    370   /// \brief The template declaration or set of overloaded function templates
    371   /// that this qualified name refers to.
    372   TemplateDecl *Template;
    373 
    374   friend class ASTContext;
    375 
    376   QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
    377                         TemplateDecl *Template)
    378     : Qualifier(NNS, TemplateKeyword? 1 : 0),
    379       Template(Template) { }
    380 
    381 public:
    382   /// \brief Return the nested name specifier that qualifies this name.
    383   NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
    384 
    385   /// \brief Whether the template name was prefixed by the "template"
    386   /// keyword.
    387   bool hasTemplateKeyword() const { return Qualifier.getInt(); }
    388 
    389   /// \brief The template declaration that this qualified name refers
    390   /// to.
    391   TemplateDecl *getDecl() const { return Template; }
    392 
    393   /// \brief The template declaration to which this qualified name
    394   /// refers.
    395   TemplateDecl *getTemplateDecl() const { return Template; }
    396 
    397   void Profile(llvm::FoldingSetNodeID &ID) {
    398     Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
    399   }
    400 
    401   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
    402                       bool TemplateKeyword, TemplateDecl *Template) {
    403     ID.AddPointer(NNS);
    404     ID.AddBoolean(TemplateKeyword);
    405     ID.AddPointer(Template);
    406   }
    407 };
    408 
    409 /// \brief Represents a dependent template name that cannot be
    410 /// resolved prior to template instantiation.
    411 ///
    412 /// This kind of template name refers to a dependent template name,
    413 /// including its nested name specifier (if any). For example,
    414 /// DependentTemplateName can refer to "MetaFun::template apply",
    415 /// where "MetaFun::" is the nested name specifier and "apply" is the
    416 /// template name referenced. The "template" keyword is implied.
    417 class DependentTemplateName : public llvm::FoldingSetNode {
    418   /// \brief The nested name specifier that qualifies the template
    419   /// name.
    420   ///
    421   /// The bit stored in this qualifier describes whether the \c Name field
    422   /// is interpreted as an IdentifierInfo pointer (when clear) or as an
    423   /// overloaded operator kind (when set).
    424   llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
    425 
    426   /// \brief The dependent template name.
    427   union {
    428     /// \brief The identifier template name.
    429     ///
    430     /// Only valid when the bit on \c Qualifier is clear.
    431     const IdentifierInfo *Identifier;
    432 
    433     /// \brief The overloaded operator name.
    434     ///
    435     /// Only valid when the bit on \c Qualifier is set.
    436     OverloadedOperatorKind Operator;
    437   };
    438 
    439   /// \brief The canonical template name to which this dependent
    440   /// template name refers.
    441   ///
    442   /// The canonical template name for a dependent template name is
    443   /// another dependent template name whose nested name specifier is
    444   /// canonical.
    445   TemplateName CanonicalTemplateName;
    446 
    447   friend class ASTContext;
    448 
    449   DependentTemplateName(NestedNameSpecifier *Qualifier,
    450                         const IdentifierInfo *Identifier)
    451     : Qualifier(Qualifier, false), Identifier(Identifier),
    452       CanonicalTemplateName(this) { }
    453 
    454   DependentTemplateName(NestedNameSpecifier *Qualifier,
    455                         const IdentifierInfo *Identifier,
    456                         TemplateName Canon)
    457     : Qualifier(Qualifier, false), Identifier(Identifier),
    458       CanonicalTemplateName(Canon) { }
    459 
    460   DependentTemplateName(NestedNameSpecifier *Qualifier,
    461                         OverloadedOperatorKind Operator)
    462   : Qualifier(Qualifier, true), Operator(Operator),
    463     CanonicalTemplateName(this) { }
    464 
    465   DependentTemplateName(NestedNameSpecifier *Qualifier,
    466                         OverloadedOperatorKind Operator,
    467                         TemplateName Canon)
    468   : Qualifier(Qualifier, true), Operator(Operator),
    469     CanonicalTemplateName(Canon) { }
    470 
    471 public:
    472   /// \brief Return the nested name specifier that qualifies this name.
    473   NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
    474 
    475   /// \brief Determine whether this template name refers to an identifier.
    476   bool isIdentifier() const { return !Qualifier.getInt(); }
    477 
    478   /// \brief Returns the identifier to which this template name refers.
    479   const IdentifierInfo *getIdentifier() const {
    480     assert(isIdentifier() && "Template name isn't an identifier?");
    481     return Identifier;
    482   }
    483 
    484   /// \brief Determine whether this template name refers to an overloaded
    485   /// operator.
    486   bool isOverloadedOperator() const { return Qualifier.getInt(); }
    487 
    488   /// \brief Return the overloaded operator to which this template name refers.
    489   OverloadedOperatorKind getOperator() const {
    490     assert(isOverloadedOperator() &&
    491            "Template name isn't an overloaded operator?");
    492     return Operator;
    493   }
    494 
    495   void Profile(llvm::FoldingSetNodeID &ID) {
    496     if (isIdentifier())
    497       Profile(ID, getQualifier(), getIdentifier());
    498     else
    499       Profile(ID, getQualifier(), getOperator());
    500   }
    501 
    502   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
    503                       const IdentifierInfo *Identifier) {
    504     ID.AddPointer(NNS);
    505     ID.AddBoolean(false);
    506     ID.AddPointer(Identifier);
    507   }
    508 
    509   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
    510                       OverloadedOperatorKind Operator) {
    511     ID.AddPointer(NNS);
    512     ID.AddBoolean(true);
    513     ID.AddInteger(Operator);
    514   }
    515 };
    516 
    517 } // end namespace clang.
    518 
    519 namespace llvm {
    520 
    521 /// \brief The clang::TemplateName class is effectively a pointer.
    522 template<>
    523 struct PointerLikeTypeTraits<clang::TemplateName> {
    524   static inline void *getAsVoidPointer(clang::TemplateName TN) {
    525     return TN.getAsVoidPointer();
    526   }
    527 
    528   static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
    529     return clang::TemplateName::getFromVoidPointer(Ptr);
    530   }
    531 
    532   // No bits are available!
    533   enum { NumLowBitsAvailable = 0 };
    534 };
    535 
    536 } // end namespace llvm.
    537 
    538 #endif
    539