TemplateName.h revision 198092
1254721Semaste//===--- TemplateName.h - C++ Template Name Representation-------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste//
10254721Semaste//  This file defines the TemplateName interface and subclasses.
11254721Semaste//
12254721Semaste//===----------------------------------------------------------------------===//
13254721Semaste
14254721Semaste#ifndef LLVM_CLANG_AST_TEMPLATENAME_H
15254721Semaste#define LLVM_CLANG_AST_TEMPLATENAME_H
16254721Semaste
17254721Semaste#include "llvm/ADT/FoldingSet.h"
18254721Semaste#include "llvm/ADT/PointerUnion.h"
19254721Semaste
20254721Semastenamespace llvm {
21254721Semaste  class raw_ostream;
22254721Semaste}
23254721Semaste
24254721Semastenamespace clang {
25254721Semaste
26254721Semasteclass DependentTemplateName;
27254721Semasteclass IdentifierInfo;
28254721Semasteclass NestedNameSpecifier;
29254721Semastestruct PrintingPolicy;
30254721Semasteclass QualifiedTemplateName;
31254721Semasteclass NamedDecl;
32254721Semasteclass TemplateDecl;
33254721Semasteclass OverloadedFunctionDecl;
34254721Semaste
35254721Semaste/// \brief Represents a C++ template name within the type system.
36254721Semaste///
37254721Semaste/// A C++ template name refers to a template within the C++ type
38254721Semaste/// system. In most cases, a template name is simply a reference to a
39254721Semaste/// class template, e.g.
40254721Semaste///
41254721Semaste/// \code
42254721Semaste/// template<typename T> class X { };
43254721Semaste///
44254721Semaste/// X<int> xi;
45254721Semaste/// \endcode
46254721Semaste///
47254721Semaste/// Here, the 'X' in \c X<int> is a template name that refers to the
48254721Semaste/// declaration of the class template X, above. Template names can
49254721Semaste/// also refer to function templates, C++0x template aliases, etc.
50254721Semaste///
51254721Semaste/// Some template names are dependent. For example, consider:
52254721Semaste///
53254721Semaste/// \code
54254721Semaste/// template<typename MetaFun, typename T1, typename T2> struct apply2 {
55254721Semaste///   typedef typename MetaFun::template apply<T1, T2>::type type;
56254721Semaste/// };
57254721Semaste/// \endcode
58254721Semaste///
59254721Semaste/// Here, "apply" is treated as a template name within the typename
60254721Semaste/// specifier in the typedef. "apply" is a nested template, and can
61254721Semaste/// only be understood in the context of
62254721Semasteclass TemplateName {
63254721Semaste  typedef llvm::PointerUnion4<TemplateDecl *, OverloadedFunctionDecl *,
64254721Semaste                              QualifiedTemplateName *,
65254721Semaste                              DependentTemplateName *> StorageType;
66254721Semaste
67254721Semaste  StorageType Storage;
68254721Semaste
69254721Semaste  explicit TemplateName(void *Ptr) {
70254721Semaste    Storage = StorageType::getFromOpaqueValue(Ptr);
71254721Semaste  }
72254721Semaste
73254721Semastepublic:
74254721Semaste  TemplateName() : Storage() { }
75254721Semaste  explicit TemplateName(TemplateDecl *Template) : Storage(Template) { }
76254721Semaste  explicit TemplateName(OverloadedFunctionDecl *FunctionTemplates)
77254721Semaste    : Storage(FunctionTemplates) { }
78254721Semaste  explicit TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) { }
79254721Semaste  explicit TemplateName(DependentTemplateName *Dep) : Storage(Dep) { }
80254721Semaste
81254721Semaste  /// \brief Determine whether this template name is NULL.
82254721Semaste  bool isNull() const { return Storage.isNull(); }
83254721Semaste
84254721Semaste  /// \brief Retrieve the the underlying template declaration that
85254721Semaste  /// this template name refers to, if known.
86254721Semaste  ///
87254721Semaste  /// \returns The template declaration that this template name refers
88254721Semaste  /// to, if any. If the template name does not refer to a specific
89254721Semaste  /// declaration because it is a dependent name, or if it refers to a
90254721Semaste  /// set of function templates, returns NULL.
91254721Semaste  TemplateDecl *getAsTemplateDecl() const;
92254721Semaste
93254721Semaste  /// \brief Retrieve the the underlying, overloaded function template
94254721Semaste  // declarations that this template name refers to, if known.
95254721Semaste  ///
96254721Semaste  /// \returns The set of overloaded function templates that this template
97254721Semaste  /// name refers to, if known. If the template name does not refer to a
98254721Semaste  /// specific set of function templates because it is a dependent name or
99254721Semaste  /// refers to a single template, returns NULL.
100254721Semaste  OverloadedFunctionDecl *getAsOverloadedFunctionDecl() const;
101254721Semaste
102254721Semaste  /// \brief Retrieve the underlying qualified template name
103254721Semaste  /// structure, if any.
104254721Semaste  QualifiedTemplateName *getAsQualifiedTemplateName() const {
105254721Semaste    return Storage.dyn_cast<QualifiedTemplateName *>();
106254721Semaste  }
107254721Semaste
108254721Semaste  /// \brief Retrieve the underlying dependent template name
109254721Semaste  /// structure, if any.
110254721Semaste  DependentTemplateName *getAsDependentTemplateName() const {
111254721Semaste    return Storage.dyn_cast<DependentTemplateName *>();
112254721Semaste  }
113254721Semaste
114254721Semaste  /// \brief Determines whether this is a dependent template name.
115254721Semaste  bool isDependent() const;
116254721Semaste
117254721Semaste  /// \brief Print the template name.
118254721Semaste  ///
119254721Semaste  /// \param OS the output stream to which the template name will be
120254721Semaste  /// printed.
121254721Semaste  ///
122254721Semaste  /// \param SuppressNNS if true, don't print the
123254721Semaste  /// nested-name-specifier that precedes the template name (if it has
124254721Semaste  /// one).
125254721Semaste  void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy,
126254721Semaste             bool SuppressNNS = false) const;
127254721Semaste
128254721Semaste  /// \brief Debugging aid that dumps the template name to standard
129254721Semaste  /// error.
130254721Semaste  void dump() const;
131254721Semaste
132254721Semaste  void Profile(llvm::FoldingSetNodeID &ID) {
133254721Semaste    ID.AddPointer(Storage.getOpaqueValue());
134254721Semaste  }
135254721Semaste
136254721Semaste  /// \brief Retrieve the template name as a void pointer.
137254721Semaste  void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
138254721Semaste
139254721Semaste  /// \brief Build a template name from a void pointer.
140254721Semaste  static TemplateName getFromVoidPointer(void *Ptr) {
141254721Semaste    return TemplateName(Ptr);
142254721Semaste  }
143254721Semaste};
144254721Semaste
145254721Semaste/// \brief Represents a template name that was expressed as a
146254721Semaste/// qualified name.
147254721Semaste///
148254721Semaste/// This kind of template name refers to a template name that was
149254721Semaste/// preceded by a nested name specifier, e.g., \c std::vector. Here,
150254721Semaste/// the nested name specifier is "std::" and the template name is the
151254721Semaste/// declaration for "vector". The QualifiedTemplateName class is only
152254721Semaste/// used to provide "sugar" for template names that were expressed
153254721Semaste/// with a qualified name, and has no semantic meaning. In this
154254721Semaste/// manner, it is to TemplateName what QualifiedNameType is to Type,
155254721Semaste/// providing extra syntactic sugar for downstream clients.
156254721Semasteclass QualifiedTemplateName : public llvm::FoldingSetNode {
157254721Semaste  /// \brief The nested name specifier that qualifies the template name.
158254721Semaste  ///
159254721Semaste  /// The bit is used to indicate whether the "template" keyword was
160254721Semaste  /// present before the template name itself. Note that the
161254721Semaste  /// "template" keyword is always redundant in this case (otherwise,
162254721Semaste  /// the template name would be a dependent name and we would express
163254721Semaste  /// this name with DependentTemplateName).
164254721Semaste  llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
165254721Semaste
166254721Semaste  /// \brief The template declaration or set of overloaded function templates
167254721Semaste  /// that this qualified name refers to.
168254721Semaste  NamedDecl *Template;
169254721Semaste
170254721Semaste  friend class ASTContext;
171254721Semaste
172254721Semaste  QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
173254721Semaste                        TemplateDecl *Template)
174254721Semaste    : Qualifier(NNS, TemplateKeyword? 1 : 0),
175254721Semaste      Template(reinterpret_cast<NamedDecl *>(Template)) { }
176254721Semaste
177254721Semaste  QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
178254721Semaste                        OverloadedFunctionDecl *Template)
179254721Semaste  : Qualifier(NNS, TemplateKeyword? 1 : 0),
180254721Semaste    Template(reinterpret_cast<NamedDecl *>(Template)) { }
181254721Semaste
182254721Semastepublic:
183254721Semaste  /// \brief Return the nested name specifier that qualifies this name.
184254721Semaste  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
185254721Semaste
186254721Semaste  /// \brief Whether the template name was prefixed by the "template"
187254721Semaste  /// keyword.
188254721Semaste  bool hasTemplateKeyword() const { return Qualifier.getInt(); }
189254721Semaste
190254721Semaste  /// \brief The template declaration or set of overloaded functions that
191254721Semaste  /// that qualified name refers to.
192254721Semaste  NamedDecl *getDecl() const { return Template; }
193254721Semaste
194254721Semaste  /// \brief The template declaration to which this qualified name
195254721Semaste  /// refers, or NULL if this qualified name refers to a set of overloaded
196254721Semaste  /// function templates.
197254721Semaste  TemplateDecl *getTemplateDecl() const;
198254721Semaste
199254721Semaste  /// \brief The set of overloaded function tempaltes to which this qualified
200254721Semaste  /// name refers, or NULL if this qualified name refers to a single
201254721Semaste  /// template declaration.
202254721Semaste  OverloadedFunctionDecl *getOverloadedFunctionDecl() const;
203254721Semaste
204254721Semaste  void Profile(llvm::FoldingSetNodeID &ID) {
205254721Semaste    Profile(ID, getQualifier(), hasTemplateKeyword(), getDecl());
206254721Semaste  }
207254721Semaste
208254721Semaste  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
209254721Semaste                      bool TemplateKeyword, NamedDecl *Template) {
210254721Semaste    ID.AddPointer(NNS);
211254721Semaste    ID.AddBoolean(TemplateKeyword);
212254721Semaste    ID.AddPointer(Template);
213254721Semaste  }
214254721Semaste};
215254721Semaste
216254721Semaste/// \brief Represents a dependent template name that cannot be
217254721Semaste/// resolved prior to template instantiation.
218254721Semaste///
219254721Semaste/// This kind of template name refers to a dependent template name,
220254721Semaste/// including its nested name specifier (if any). For example,
221254721Semaste/// DependentTemplateName can refer to "MetaFun::template apply",
222254721Semaste/// where "MetaFun::" is the nested name specifier and "apply" is the
223254721Semaste/// template name referenced. The "template" keyword is implied.
224254721Semasteclass DependentTemplateName : public llvm::FoldingSetNode {
225254721Semaste  /// \brief The nested name specifier that qualifies the template
226254721Semaste  /// name.
227254721Semaste  NestedNameSpecifier *Qualifier;
228254721Semaste
229254721Semaste  /// \brief The dependent template name.
230254721Semaste  const IdentifierInfo *Name;
231254721Semaste
232254721Semaste  /// \brief The canonical template name to which this dependent
233254721Semaste  /// template name refers.
234254721Semaste  ///
235254721Semaste  /// The canonical template name for a dependent template name is
236254721Semaste  /// another dependent template name whose nested name specifier is
237254721Semaste  /// canonical.
238254721Semaste  TemplateName CanonicalTemplateName;
239254721Semaste
240254721Semaste  friend class ASTContext;
241254721Semaste
242254721Semaste  DependentTemplateName(NestedNameSpecifier *Qualifier,
243254721Semaste                        const IdentifierInfo *Name)
244254721Semaste    : Qualifier(Qualifier), Name(Name), CanonicalTemplateName(this) { }
245254721Semaste
246254721Semaste  DependentTemplateName(NestedNameSpecifier *Qualifier,
247254721Semaste                        const IdentifierInfo *Name,
248254721Semaste                        TemplateName Canon)
249254721Semaste    : Qualifier(Qualifier), Name(Name), CanonicalTemplateName(Canon) { }
250254721Semaste
251254721Semastepublic:
252254721Semaste  /// \brief Return the nested name specifier that qualifies this name.
253254721Semaste  NestedNameSpecifier *getQualifier() const { return Qualifier; }
254254721Semaste
255254721Semaste  /// \brief Return the name to which this dependent template name
256254721Semaste  /// refers.
257254721Semaste  const IdentifierInfo *getName() const { return Name; }
258254721Semaste
259254721Semaste  void Profile(llvm::FoldingSetNodeID &ID) {
260254721Semaste    Profile(ID, getQualifier(), getName());
261254721Semaste  }
262254721Semaste
263254721Semaste  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
264254721Semaste                      const IdentifierInfo *Name) {
265254721Semaste    ID.AddPointer(NNS);
266254721Semaste    ID.AddPointer(Name);
267254721Semaste  }
268254721Semaste};
269254721Semaste
270254721Semaste} // end namespace clang.
271254721Semaste
272254721Semastenamespace llvm {
273254721Semaste
274254721Semaste/// \brief The clang::TemplateName class is effectively a pointer.
275254721Semastetemplate<>
276254721Semasteclass PointerLikeTypeTraits<clang::TemplateName> {
277254721Semastepublic:
278254721Semaste  static inline void *getAsVoidPointer(clang::TemplateName TN) {
279254721Semaste    return TN.getAsVoidPointer();
280254721Semaste  }
281254721Semaste
282254721Semaste  static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
283254721Semaste    return clang::TemplateName::getFromVoidPointer(Ptr);
284254721Semaste  }
285254721Semaste
286254721Semaste  // No bits are available!
287254721Semaste  enum { NumLowBitsAvailable = 0 };
288254721Semaste};
289254721Semaste
290254721Semaste} // end namespace llvm.
291254721Semaste
292254721Semaste#endif
293254721Semaste