clang API Documentation

TemplateName.h
Go to the documentation of this file.
00001 //===--- TemplateName.h - C++ Template Name Representation-------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the TemplateName interface and subclasses.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_AST_TEMPLATENAME_H
00015 #define LLVM_CLANG_AST_TEMPLATENAME_H
00016 
00017 #include "clang/Basic/LLVM.h"
00018 #include "llvm/ADT/FoldingSet.h"
00019 #include "llvm/ADT/PointerUnion.h"
00020 
00021 namespace clang {
00022   
00023 class ASTContext;
00024 class DependentTemplateName;
00025 class DiagnosticBuilder;
00026 class IdentifierInfo;
00027 class NamedDecl;
00028 class NestedNameSpecifier;
00029 enum OverloadedOperatorKind : int;
00030 class OverloadedTemplateStorage;
00031 struct PrintingPolicy;
00032 class QualifiedTemplateName;
00033 class SubstTemplateTemplateParmPackStorage;
00034 class SubstTemplateTemplateParmStorage;
00035 class TemplateArgument;
00036 class TemplateDecl;
00037 class TemplateTemplateParmDecl;
00038   
00039 /// \brief Implementation class used to describe either a set of overloaded
00040 /// template names or an already-substituted template template parameter pack.
00041 class UncommonTemplateNameStorage {
00042 protected:
00043   enum Kind {
00044     Overloaded,
00045     SubstTemplateTemplateParm,
00046     SubstTemplateTemplateParmPack
00047   };
00048 
00049   struct BitsTag {
00050     /// \brief A Kind.
00051     unsigned Kind : 2;
00052     
00053     /// \brief The number of stored templates or template arguments,
00054     /// depending on which subclass we have.
00055     unsigned Size : 30;
00056   };
00057 
00058   union {
00059     struct BitsTag Bits;
00060     void *PointerAlignment;
00061   };
00062   
00063   UncommonTemplateNameStorage(Kind kind, unsigned size) {
00064     Bits.Kind = kind;
00065     Bits.Size = size;
00066   }
00067   
00068 public:
00069   unsigned size() const { return Bits.Size; }
00070   
00071   OverloadedTemplateStorage *getAsOverloadedStorage()  {
00072     return Bits.Kind == Overloaded
00073              ? reinterpret_cast<OverloadedTemplateStorage *>(this) 
00074              : nullptr;
00075   }
00076   
00077   SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
00078     return Bits.Kind == SubstTemplateTemplateParm
00079              ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
00080              : nullptr;
00081   }
00082 
00083   SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
00084     return Bits.Kind == SubstTemplateTemplateParmPack
00085              ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
00086              : nullptr;
00087   }
00088 };
00089   
00090 /// \brief A structure for storing the information associated with an
00091 /// overloaded template name.
00092 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
00093   friend class ASTContext;
00094 
00095   OverloadedTemplateStorage(unsigned size) 
00096     : UncommonTemplateNameStorage(Overloaded, size) { }
00097 
00098   NamedDecl **getStorage() {
00099     return reinterpret_cast<NamedDecl **>(this + 1);
00100   }
00101   NamedDecl * const *getStorage() const {
00102     return reinterpret_cast<NamedDecl *const *>(this + 1);
00103   }
00104 
00105 public:
00106   typedef NamedDecl *const *iterator;
00107 
00108   iterator begin() const { return getStorage(); }
00109   iterator end() const { return getStorage() + size(); }
00110 };
00111 
00112 /// \brief A structure for storing an already-substituted template template
00113 /// parameter pack.
00114 ///
00115 /// This kind of template names occurs when the parameter pack has been 
00116 /// provided with a template template argument pack in a context where its
00117 /// enclosing pack expansion could not be fully expanded.
00118 class SubstTemplateTemplateParmPackStorage
00119   : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
00120 {
00121   TemplateTemplateParmDecl *Parameter;
00122   const TemplateArgument *Arguments;
00123   
00124 public:
00125   SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
00126                                        unsigned Size, 
00127                                        const TemplateArgument *Arguments)
00128     : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
00129       Parameter(Parameter), Arguments(Arguments) { }
00130   
00131   /// \brief Retrieve the template template parameter pack being substituted.
00132   TemplateTemplateParmDecl *getParameterPack() const {
00133     return Parameter;
00134   }
00135   
00136   /// \brief Retrieve the template template argument pack with which this
00137   /// parameter was substituted.
00138   TemplateArgument getArgumentPack() const;
00139   
00140   void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
00141   
00142   static void Profile(llvm::FoldingSetNodeID &ID,
00143                       ASTContext &Context,
00144                       TemplateTemplateParmDecl *Parameter,
00145                       const TemplateArgument &ArgPack);
00146 };
00147 
00148 /// \brief Represents a C++ template name within the type system.
00149 ///
00150 /// A C++ template name refers to a template within the C++ type
00151 /// system. In most cases, a template name is simply a reference to a
00152 /// class template, e.g.
00153 ///
00154 /// \code
00155 /// template<typename T> class X { };
00156 ///
00157 /// X<int> xi;
00158 /// \endcode
00159 ///
00160 /// Here, the 'X' in \c X<int> is a template name that refers to the
00161 /// declaration of the class template X, above. Template names can
00162 /// also refer to function templates, C++0x template aliases, etc.
00163 ///
00164 /// Some template names are dependent. For example, consider:
00165 ///
00166 /// \code
00167 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
00168 ///   typedef typename MetaFun::template apply<T1, T2>::type type;
00169 /// };
00170 /// \endcode
00171 ///
00172 /// Here, "apply" is treated as a template name within the typename
00173 /// specifier in the typedef. "apply" is a nested template, and can
00174 /// only be understood in the context of
00175 class TemplateName {
00176   typedef llvm::PointerUnion4<TemplateDecl *,
00177                               UncommonTemplateNameStorage *,
00178                               QualifiedTemplateName *,
00179                               DependentTemplateName *> StorageType;
00180 
00181   StorageType Storage;
00182 
00183   explicit TemplateName(void *Ptr) {
00184     Storage = StorageType::getFromOpaqueValue(Ptr);
00185   }
00186 
00187 public:
00188   // \brief Kind of name that is actually stored.
00189   enum NameKind {
00190     /// \brief A single template declaration.
00191     Template,
00192     /// \brief A set of overloaded template declarations.
00193     OverloadedTemplate,
00194     /// \brief A qualified template name, where the qualification is kept 
00195     /// to describe the source code as written.
00196     QualifiedTemplate,
00197     /// \brief A dependent template name that has not been resolved to a 
00198     /// template (or set of templates).
00199     DependentTemplate,
00200     /// \brief A template template parameter that has been substituted
00201     /// for some other template name.
00202     SubstTemplateTemplateParm,
00203     /// \brief A template template parameter pack that has been substituted for 
00204     /// a template template argument pack, but has not yet been expanded into
00205     /// individual arguments.
00206     SubstTemplateTemplateParmPack
00207   };
00208 
00209   TemplateName() : Storage() { }
00210   explicit TemplateName(TemplateDecl *Template) : Storage(Template) { }
00211   explicit TemplateName(OverloadedTemplateStorage *Storage)
00212     : Storage(Storage) { }
00213   explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
00214   explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
00215     : Storage(Storage) { }
00216   explicit TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) { }
00217   explicit TemplateName(DependentTemplateName *Dep) : Storage(Dep) { }
00218 
00219   /// \brief Determine whether this template name is NULL.
00220   bool isNull() const { return Storage.isNull(); }
00221   
00222   // \brief Get the kind of name that is actually stored.
00223   NameKind getKind() const;
00224 
00225   /// \brief Retrieve the underlying template declaration that
00226   /// this template name refers to, if known.
00227   ///
00228   /// \returns The template declaration that this template name refers
00229   /// to, if any. If the template name does not refer to a specific
00230   /// declaration because it is a dependent name, or if it refers to a
00231   /// set of function templates, returns NULL.
00232   TemplateDecl *getAsTemplateDecl() const;
00233 
00234   /// \brief Retrieve the underlying, overloaded function template
00235   // declarations that this template name refers to, if known.
00236   ///
00237   /// \returns The set of overloaded function templates that this template
00238   /// name refers to, if known. If the template name does not refer to a
00239   /// specific set of function templates because it is a dependent name or
00240   /// refers to a single template, returns NULL.
00241   OverloadedTemplateStorage *getAsOverloadedTemplate() const {
00242     if (UncommonTemplateNameStorage *Uncommon = 
00243                               Storage.dyn_cast<UncommonTemplateNameStorage *>())
00244       return Uncommon->getAsOverloadedStorage();
00245     
00246     return nullptr;
00247   }
00248 
00249   /// \brief Retrieve the substituted template template parameter, if 
00250   /// known.
00251   ///
00252   /// \returns The storage for the substituted template template parameter,
00253   /// if known. Otherwise, returns NULL.
00254   SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const {
00255     if (UncommonTemplateNameStorage *uncommon = 
00256           Storage.dyn_cast<UncommonTemplateNameStorage *>())
00257       return uncommon->getAsSubstTemplateTemplateParm();
00258     
00259     return nullptr;
00260   }
00261 
00262   /// \brief Retrieve the substituted template template parameter pack, if 
00263   /// known.
00264   ///
00265   /// \returns The storage for the substituted template template parameter pack,
00266   /// if known. Otherwise, returns NULL.
00267   SubstTemplateTemplateParmPackStorage *
00268   getAsSubstTemplateTemplateParmPack() const {
00269     if (UncommonTemplateNameStorage *Uncommon = 
00270         Storage.dyn_cast<UncommonTemplateNameStorage *>())
00271       return Uncommon->getAsSubstTemplateTemplateParmPack();
00272     
00273     return nullptr;
00274   }
00275 
00276   /// \brief Retrieve the underlying qualified template name
00277   /// structure, if any.
00278   QualifiedTemplateName *getAsQualifiedTemplateName() const {
00279     return Storage.dyn_cast<QualifiedTemplateName *>();
00280   }
00281 
00282   /// \brief Retrieve the underlying dependent template name
00283   /// structure, if any.
00284   DependentTemplateName *getAsDependentTemplateName() const {
00285     return Storage.dyn_cast<DependentTemplateName *>();
00286   }
00287 
00288   TemplateName getUnderlying() const;
00289 
00290   /// \brief Determines whether this is a dependent template name.
00291   bool isDependent() const;
00292 
00293   /// \brief Determines whether this is a template name that somehow
00294   /// depends on a template parameter.
00295   bool isInstantiationDependent() const;
00296 
00297   /// \brief Determines whether this template name contains an
00298   /// unexpanded parameter pack (for C++0x variadic templates).
00299   bool containsUnexpandedParameterPack() const;
00300 
00301   /// \brief Print the template name.
00302   ///
00303   /// \param OS the output stream to which the template name will be
00304   /// printed.
00305   ///
00306   /// \param SuppressNNS if true, don't print the
00307   /// nested-name-specifier that precedes the template name (if it has
00308   /// one).
00309   void print(raw_ostream &OS, const PrintingPolicy &Policy,
00310              bool SuppressNNS = false) const;
00311 
00312   /// \brief Debugging aid that dumps the template name.
00313   void dump(raw_ostream &OS) const;
00314 
00315   /// \brief Debugging aid that dumps the template name to standard
00316   /// error.
00317   void dump() const;
00318 
00319   void Profile(llvm::FoldingSetNodeID &ID) {
00320     ID.AddPointer(Storage.getOpaqueValue());
00321   }
00322 
00323   /// \brief Retrieve the template name as a void pointer.
00324   void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
00325 
00326   /// \brief Build a template name from a void pointer.
00327   static TemplateName getFromVoidPointer(void *Ptr) {
00328     return TemplateName(Ptr);
00329   }
00330 };
00331 
00332 /// Insertion operator for diagnostics.  This allows sending TemplateName's
00333 /// into a diagnostic with <<.
00334 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
00335                                     TemplateName N);
00336 
00337 /// \brief A structure for storing the information associated with a
00338 /// substituted template template parameter.
00339 class SubstTemplateTemplateParmStorage
00340   : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
00341   friend class ASTContext;
00342 
00343   TemplateTemplateParmDecl *Parameter;
00344   TemplateName Replacement;
00345 
00346   SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
00347                                    TemplateName replacement)
00348     : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
00349       Parameter(parameter), Replacement(replacement) {}
00350 
00351 public:
00352   TemplateTemplateParmDecl *getParameter() const { return Parameter; }
00353   TemplateName getReplacement() const { return Replacement; }
00354 
00355   void Profile(llvm::FoldingSetNodeID &ID);
00356   
00357   static void Profile(llvm::FoldingSetNodeID &ID,
00358                       TemplateTemplateParmDecl *parameter,
00359                       TemplateName replacement);
00360 };
00361 
00362 inline TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
00363   : Storage(Storage) { }
00364 
00365 inline TemplateName TemplateName::getUnderlying() const {
00366   if (SubstTemplateTemplateParmStorage *subst
00367         = getAsSubstTemplateTemplateParm())
00368     return subst->getReplacement().getUnderlying();
00369   return *this;
00370 }
00371 
00372 /// \brief Represents a template name that was expressed as a
00373 /// qualified name.
00374 ///
00375 /// This kind of template name refers to a template name that was
00376 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
00377 /// the nested name specifier is "std::" and the template name is the
00378 /// declaration for "vector". The QualifiedTemplateName class is only
00379 /// used to provide "sugar" for template names that were expressed
00380 /// with a qualified name, and has no semantic meaning. In this
00381 /// manner, it is to TemplateName what ElaboratedType is to Type,
00382 /// providing extra syntactic sugar for downstream clients.
00383 class QualifiedTemplateName : public llvm::FoldingSetNode {
00384   /// \brief The nested name specifier that qualifies the template name.
00385   ///
00386   /// The bit is used to indicate whether the "template" keyword was
00387   /// present before the template name itself. Note that the
00388   /// "template" keyword is always redundant in this case (otherwise,
00389   /// the template name would be a dependent name and we would express
00390   /// this name with DependentTemplateName).
00391   llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
00392 
00393   /// \brief The template declaration or set of overloaded function templates
00394   /// that this qualified name refers to.
00395   TemplateDecl *Template;
00396 
00397   friend class ASTContext;
00398 
00399   QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
00400                         TemplateDecl *Template)
00401     : Qualifier(NNS, TemplateKeyword? 1 : 0),
00402       Template(Template) { }
00403 
00404 public:
00405   /// \brief Return the nested name specifier that qualifies this name.
00406   NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
00407 
00408   /// \brief Whether the template name was prefixed by the "template"
00409   /// keyword.
00410   bool hasTemplateKeyword() const { return Qualifier.getInt(); }
00411 
00412   /// \brief The template declaration that this qualified name refers
00413   /// to.
00414   TemplateDecl *getDecl() const { return Template; }
00415 
00416   /// \brief The template declaration to which this qualified name
00417   /// refers.
00418   TemplateDecl *getTemplateDecl() const { return Template; }
00419 
00420   void Profile(llvm::FoldingSetNodeID &ID) {
00421     Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
00422   }
00423 
00424   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
00425                       bool TemplateKeyword, TemplateDecl *Template) {
00426     ID.AddPointer(NNS);
00427     ID.AddBoolean(TemplateKeyword);
00428     ID.AddPointer(Template);
00429   }
00430 };
00431 
00432 /// \brief Represents a dependent template name that cannot be
00433 /// resolved prior to template instantiation.
00434 ///
00435 /// This kind of template name refers to a dependent template name,
00436 /// including its nested name specifier (if any). For example,
00437 /// DependentTemplateName can refer to "MetaFun::template apply",
00438 /// where "MetaFun::" is the nested name specifier and "apply" is the
00439 /// template name referenced. The "template" keyword is implied.
00440 class DependentTemplateName : public llvm::FoldingSetNode {
00441   /// \brief The nested name specifier that qualifies the template
00442   /// name.
00443   ///
00444   /// The bit stored in this qualifier describes whether the \c Name field
00445   /// is interpreted as an IdentifierInfo pointer (when clear) or as an
00446   /// overloaded operator kind (when set).
00447   llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
00448 
00449   /// \brief The dependent template name.
00450   union {
00451     /// \brief The identifier template name.
00452     ///
00453     /// Only valid when the bit on \c Qualifier is clear.
00454     const IdentifierInfo *Identifier;
00455     
00456     /// \brief The overloaded operator name.
00457     ///
00458     /// Only valid when the bit on \c Qualifier is set.
00459     OverloadedOperatorKind Operator;
00460   };
00461 
00462   /// \brief The canonical template name to which this dependent
00463   /// template name refers.
00464   ///
00465   /// The canonical template name for a dependent template name is
00466   /// another dependent template name whose nested name specifier is
00467   /// canonical.
00468   TemplateName CanonicalTemplateName;
00469 
00470   friend class ASTContext;
00471 
00472   DependentTemplateName(NestedNameSpecifier *Qualifier,
00473                         const IdentifierInfo *Identifier)
00474     : Qualifier(Qualifier, false), Identifier(Identifier), 
00475       CanonicalTemplateName(this) { }
00476 
00477   DependentTemplateName(NestedNameSpecifier *Qualifier,
00478                         const IdentifierInfo *Identifier,
00479                         TemplateName Canon)
00480     : Qualifier(Qualifier, false), Identifier(Identifier), 
00481       CanonicalTemplateName(Canon) { }
00482 
00483   DependentTemplateName(NestedNameSpecifier *Qualifier,
00484                         OverloadedOperatorKind Operator)
00485   : Qualifier(Qualifier, true), Operator(Operator), 
00486     CanonicalTemplateName(this) { }
00487   
00488   DependentTemplateName(NestedNameSpecifier *Qualifier,
00489                         OverloadedOperatorKind Operator,
00490                         TemplateName Canon)
00491   : Qualifier(Qualifier, true), Operator(Operator), 
00492     CanonicalTemplateName(Canon) { }
00493   
00494 public:
00495   /// \brief Return the nested name specifier that qualifies this name.
00496   NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
00497 
00498   /// \brief Determine whether this template name refers to an identifier.
00499   bool isIdentifier() const { return !Qualifier.getInt(); }
00500 
00501   /// \brief Returns the identifier to which this template name refers.
00502   const IdentifierInfo *getIdentifier() const { 
00503     assert(isIdentifier() && "Template name isn't an identifier?");
00504     return Identifier;
00505   }
00506   
00507   /// \brief Determine whether this template name refers to an overloaded
00508   /// operator.
00509   bool isOverloadedOperator() const { return Qualifier.getInt(); }
00510   
00511   /// \brief Return the overloaded operator to which this template name refers.
00512   OverloadedOperatorKind getOperator() const { 
00513     assert(isOverloadedOperator() &&
00514            "Template name isn't an overloaded operator?");
00515     return Operator; 
00516   }
00517   
00518   void Profile(llvm::FoldingSetNodeID &ID) {
00519     if (isIdentifier())
00520       Profile(ID, getQualifier(), getIdentifier());
00521     else
00522       Profile(ID, getQualifier(), getOperator());
00523   }
00524 
00525   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
00526                       const IdentifierInfo *Identifier) {
00527     ID.AddPointer(NNS);
00528     ID.AddBoolean(false);
00529     ID.AddPointer(Identifier);
00530   }
00531 
00532   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
00533                       OverloadedOperatorKind Operator) {
00534     ID.AddPointer(NNS);
00535     ID.AddBoolean(true);
00536     ID.AddInteger(Operator);
00537   }
00538 };
00539 
00540 } // end namespace clang.
00541 
00542 namespace llvm {
00543 
00544 /// \brief The clang::TemplateName class is effectively a pointer.
00545 template<>
00546 class PointerLikeTypeTraits<clang::TemplateName> {
00547 public:
00548   static inline void *getAsVoidPointer(clang::TemplateName TN) {
00549     return TN.getAsVoidPointer();
00550   }
00551 
00552   static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
00553     return clang::TemplateName::getFromVoidPointer(Ptr);
00554   }
00555 
00556   // No bits are available!
00557   enum { NumLowBitsAvailable = 0 };
00558 };
00559 
00560 } // end namespace llvm.
00561 
00562 #endif