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