TemplateName.h revision 210299
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 "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/PointerUnion.h"
19#include "clang/Basic/OperatorKinds.h"
20
21namespace llvm {
22  class raw_ostream;
23}
24
25namespace clang {
26
27class DependentTemplateName;
28class DiagnosticBuilder;
29class IdentifierInfo;
30class NestedNameSpecifier;
31struct PrintingPolicy;
32class QualifiedTemplateName;
33class NamedDecl;
34class TemplateDecl;
35
36/// \brief A structure for storing the information associated with an
37/// overloaded template name.
38class OverloadedTemplateStorage {
39  union {
40    unsigned Size;
41    NamedDecl *Storage[1];
42  };
43
44  friend class ASTContext;
45
46  OverloadedTemplateStorage(unsigned Size) : Size(Size) {}
47
48  NamedDecl **getStorage() {
49    return &Storage[1];
50  }
51  NamedDecl * const *getStorage() const {
52    return &Storage[1];
53  }
54
55public:
56  typedef NamedDecl *const *iterator;
57
58  unsigned size() const { return Size; }
59
60  iterator begin() const { return getStorage(); }
61  iterator end() const { return getStorage() + size(); }
62};
63
64/// \brief Represents a C++ template name within the type system.
65///
66/// A C++ template name refers to a template within the C++ type
67/// system. In most cases, a template name is simply a reference to a
68/// class template, e.g.
69///
70/// \code
71/// template<typename T> class X { };
72///
73/// X<int> xi;
74/// \endcode
75///
76/// Here, the 'X' in \c X<int> is a template name that refers to the
77/// declaration of the class template X, above. Template names can
78/// also refer to function templates, C++0x template aliases, etc.
79///
80/// Some template names are dependent. For example, consider:
81///
82/// \code
83/// template<typename MetaFun, typename T1, typename T2> struct apply2 {
84///   typedef typename MetaFun::template apply<T1, T2>::type type;
85/// };
86/// \endcode
87///
88/// Here, "apply" is treated as a template name within the typename
89/// specifier in the typedef. "apply" is a nested template, and can
90/// only be understood in the context of
91class TemplateName {
92  typedef llvm::PointerUnion4<TemplateDecl *,
93                              OverloadedTemplateStorage *,
94                              QualifiedTemplateName *,
95                              DependentTemplateName *> StorageType;
96
97  StorageType Storage;
98
99  explicit TemplateName(void *Ptr) {
100    Storage = StorageType::getFromOpaqueValue(Ptr);
101  }
102
103public:
104  // \brief Kind of name that is actually stored.
105  enum NameKind {
106    Template,
107    OverloadedTemplate,
108    QualifiedTemplate,
109    DependentTemplate
110  };
111
112  TemplateName() : Storage() { }
113  explicit TemplateName(TemplateDecl *Template) : Storage(Template) { }
114  explicit TemplateName(OverloadedTemplateStorage *Storage)
115    : Storage(Storage) { }
116  explicit TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) { }
117  explicit TemplateName(DependentTemplateName *Dep) : Storage(Dep) { }
118
119  /// \brief Determine whether this template name is NULL.
120  bool isNull() const { return Storage.isNull(); }
121
122  // \brief Get the kind of name that is actually stored.
123  NameKind getKind() const;
124
125  /// \brief Retrieve the the underlying template declaration that
126  /// this template name refers to, if known.
127  ///
128  /// \returns The template declaration that this template name refers
129  /// to, if any. If the template name does not refer to a specific
130  /// declaration because it is a dependent name, or if it refers to a
131  /// set of function templates, returns NULL.
132  TemplateDecl *getAsTemplateDecl() const;
133
134  /// \brief Retrieve the the underlying, overloaded function template
135  // declarations that this template name refers to, if known.
136  ///
137  /// \returns The set of overloaded function templates that this template
138  /// name refers to, if known. If the template name does not refer to a
139  /// specific set of function templates because it is a dependent name or
140  /// refers to a single template, returns NULL.
141  OverloadedTemplateStorage *getAsOverloadedTemplate() const {
142    return Storage.dyn_cast<OverloadedTemplateStorage *>();
143  }
144
145  /// \brief Retrieve the underlying qualified template name
146  /// structure, if any.
147  QualifiedTemplateName *getAsQualifiedTemplateName() const {
148    return Storage.dyn_cast<QualifiedTemplateName *>();
149  }
150
151  /// \brief Retrieve the underlying dependent template name
152  /// structure, if any.
153  DependentTemplateName *getAsDependentTemplateName() const {
154    return Storage.dyn_cast<DependentTemplateName *>();
155  }
156
157  /// \brief Determines whether this is a dependent template name.
158  bool isDependent() const;
159
160  /// \brief Print the template name.
161  ///
162  /// \param OS the output stream to which the template name will be
163  /// printed.
164  ///
165  /// \param SuppressNNS if true, don't print the
166  /// nested-name-specifier that precedes the template name (if it has
167  /// one).
168  void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy,
169             bool SuppressNNS = false) const;
170
171  /// \brief Debugging aid that dumps the template name to standard
172  /// error.
173  void dump() const;
174
175  void Profile(llvm::FoldingSetNodeID &ID) {
176    ID.AddPointer(Storage.getOpaqueValue());
177  }
178
179  /// \brief Retrieve the template name as a void pointer.
180  void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
181
182  /// \brief Build a template name from a void pointer.
183  static TemplateName getFromVoidPointer(void *Ptr) {
184    return TemplateName(Ptr);
185  }
186};
187
188/// Insertion operator for diagnostics.  This allows sending TemplateName's
189/// into a diagnostic with <<.
190const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
191                                    TemplateName N);
192
193/// \brief Represents a template name that was expressed as a
194/// qualified name.
195///
196/// This kind of template name refers to a template name that was
197/// preceded by a nested name specifier, e.g., \c std::vector. Here,
198/// the nested name specifier is "std::" and the template name is the
199/// declaration for "vector". The QualifiedTemplateName class is only
200/// used to provide "sugar" for template names that were expressed
201/// with a qualified name, and has no semantic meaning. In this
202/// manner, it is to TemplateName what ElaboratedType is to Type,
203/// providing extra syntactic sugar for downstream clients.
204class QualifiedTemplateName : public llvm::FoldingSetNode {
205  /// \brief The nested name specifier that qualifies the template name.
206  ///
207  /// The bit is used to indicate whether the "template" keyword was
208  /// present before the template name itself. Note that the
209  /// "template" keyword is always redundant in this case (otherwise,
210  /// the template name would be a dependent name and we would express
211  /// this name with DependentTemplateName).
212  llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
213
214  /// \brief The template declaration or set of overloaded function templates
215  /// that this qualified name refers to.
216  TemplateDecl *Template;
217
218  friend class ASTContext;
219
220  QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
221                        TemplateDecl *Template)
222    : Qualifier(NNS, TemplateKeyword? 1 : 0),
223      Template(Template) { }
224
225public:
226  /// \brief Return the nested name specifier that qualifies this name.
227  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
228
229  /// \brief Whether the template name was prefixed by the "template"
230  /// keyword.
231  bool hasTemplateKeyword() const { return Qualifier.getInt(); }
232
233  /// \brief The template declaration that this qualified name refers
234  /// to.
235  TemplateDecl *getDecl() const { return Template; }
236
237  /// \brief The template declaration to which this qualified name
238  /// refers.
239  TemplateDecl *getTemplateDecl() const { return Template; }
240
241  void Profile(llvm::FoldingSetNodeID &ID) {
242    Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
243  }
244
245  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
246                      bool TemplateKeyword, TemplateDecl *Template) {
247    ID.AddPointer(NNS);
248    ID.AddBoolean(TemplateKeyword);
249    ID.AddPointer(Template);
250  }
251};
252
253/// \brief Represents a dependent template name that cannot be
254/// resolved prior to template instantiation.
255///
256/// This kind of template name refers to a dependent template name,
257/// including its nested name specifier (if any). For example,
258/// DependentTemplateName can refer to "MetaFun::template apply",
259/// where "MetaFun::" is the nested name specifier and "apply" is the
260/// template name referenced. The "template" keyword is implied.
261class DependentTemplateName : public llvm::FoldingSetNode {
262  /// \brief The nested name specifier that qualifies the template
263  /// name.
264  ///
265  /// The bit stored in this qualifier describes whether the \c Name field
266  /// is interpreted as an IdentifierInfo pointer (when clear) or as an
267  /// overloaded operator kind (when set).
268  llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
269
270  /// \brief The dependent template name.
271  union {
272    /// \brief The identifier template name.
273    ///
274    /// Only valid when the bit on \c Qualifier is clear.
275    const IdentifierInfo *Identifier;
276
277    /// \brief The overloaded operator name.
278    ///
279    /// Only valid when the bit on \c Qualifier is set.
280    OverloadedOperatorKind Operator;
281  };
282
283  /// \brief The canonical template name to which this dependent
284  /// template name refers.
285  ///
286  /// The canonical template name for a dependent template name is
287  /// another dependent template name whose nested name specifier is
288  /// canonical.
289  TemplateName CanonicalTemplateName;
290
291  friend class ASTContext;
292
293  DependentTemplateName(NestedNameSpecifier *Qualifier,
294                        const IdentifierInfo *Identifier)
295    : Qualifier(Qualifier, false), Identifier(Identifier),
296      CanonicalTemplateName(this) { }
297
298  DependentTemplateName(NestedNameSpecifier *Qualifier,
299                        const IdentifierInfo *Identifier,
300                        TemplateName Canon)
301    : Qualifier(Qualifier, false), Identifier(Identifier),
302      CanonicalTemplateName(Canon) { }
303
304  DependentTemplateName(NestedNameSpecifier *Qualifier,
305                        OverloadedOperatorKind Operator)
306  : Qualifier(Qualifier, true), Operator(Operator),
307    CanonicalTemplateName(this) { }
308
309  DependentTemplateName(NestedNameSpecifier *Qualifier,
310                        OverloadedOperatorKind Operator,
311                        TemplateName Canon)
312  : Qualifier(Qualifier, true), Operator(Operator),
313    CanonicalTemplateName(Canon) { }
314
315public:
316  /// \brief Return the nested name specifier that qualifies this name.
317  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
318
319  /// \brief Determine whether this template name refers to an identifier.
320  bool isIdentifier() const { return !Qualifier.getInt(); }
321
322  /// \brief Returns the identifier to which this template name refers.
323  const IdentifierInfo *getIdentifier() const {
324    assert(isIdentifier() && "Template name isn't an identifier?");
325    return Identifier;
326  }
327
328  /// \brief Determine whether this template name refers to an overloaded
329  /// operator.
330  bool isOverloadedOperator() const { return Qualifier.getInt(); }
331
332  /// \brief Return the overloaded operator to which this template name refers.
333  OverloadedOperatorKind getOperator() const {
334    assert(isOverloadedOperator() &&
335           "Template name isn't an overloaded operator?");
336    return Operator;
337  }
338
339  void Profile(llvm::FoldingSetNodeID &ID) {
340    if (isIdentifier())
341      Profile(ID, getQualifier(), getIdentifier());
342    else
343      Profile(ID, getQualifier(), getOperator());
344  }
345
346  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
347                      const IdentifierInfo *Identifier) {
348    ID.AddPointer(NNS);
349    ID.AddBoolean(false);
350    ID.AddPointer(Identifier);
351  }
352
353  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
354                      OverloadedOperatorKind Operator) {
355    ID.AddPointer(NNS);
356    ID.AddBoolean(true);
357    ID.AddInteger(Operator);
358  }
359};
360
361} // end namespace clang.
362
363namespace llvm {
364
365/// \brief The clang::TemplateName class is effectively a pointer.
366template<>
367class PointerLikeTypeTraits<clang::TemplateName> {
368public:
369  static inline void *getAsVoidPointer(clang::TemplateName TN) {
370    return TN.getAsVoidPointer();
371  }
372
373  static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
374    return clang::TemplateName::getFromVoidPointer(Ptr);
375  }
376
377  // No bits are available!
378  enum { NumLowBitsAvailable = 0 };
379};
380
381} // end namespace llvm.
382
383#endif
384