clang  3.9.0
TemplateName.h
Go to the documentation of this file.
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 
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.
43 protected:
44  enum Kind {
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;
62  };
63 
65  Bits.Kind = kind;
66  Bits.Size = size;
67  }
68 
69 public:
70  unsigned size() const { return Bits.Size; }
71 
73  return Bits.Kind == Overloaded
74  ? reinterpret_cast<OverloadedTemplateStorage *>(this)
75  : nullptr;
76  }
77 
80  ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
81  : nullptr;
82  }
83 
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.
94  friend class ASTContext;
95 
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.
120  : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
121 {
122  TemplateTemplateParmDecl *Parameter;
123  const TemplateArgument *Arguments;
124 
125 public:
127  unsigned Size,
128  const TemplateArgument *Arguments)
130  Parameter(Parameter), Arguments(Arguments) { }
131 
132  /// \brief Retrieve the template template parameter pack being substituted.
134  return Parameter;
135  }
136 
137  /// \brief Retrieve the template template argument pack with which this
138  /// parameter was substituted.
140 
141  void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
142 
143  static void Profile(llvm::FoldingSetNodeID &ID,
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
177  typedef llvm::PointerUnion4<TemplateDecl *,
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.
191  /// \brief A set of overloaded template declarations.
193  /// \brief A qualified template name, where the qualification is kept
194  /// to describe the source code as written.
196  /// \brief A dependent template name that has not been resolved to a
197  /// template (or set of templates).
199  /// \brief A template template parameter that has been substituted
200  /// for some other template name.
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.
206  };
207 
208  TemplateName() : Storage() { }
210  explicit TemplateName(OverloadedTemplateStorage *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.
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.
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.
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.
254 
255  /// \brief Retrieve the underlying qualified template name
256  /// structure, if any.
258 
259  /// \brief Retrieve the underlying dependent template name
260  /// structure, if any.
262 
263  TemplateName getUnderlying() const;
264 
265  /// \brief Determines whether this is a dependent template name.
266  bool isDependent() const;
267 
268  /// \brief Determines whether this is a template name that somehow
269  /// depends on a template parameter.
270  bool isInstantiationDependent() const;
271 
272  /// \brief Determines whether this template name contains an
273  /// unexpanded parameter pack (for C++0x variadic templates).
274  bool containsUnexpandedParameterPack() const;
275 
276  /// \brief Print the template name.
277  ///
278  /// \param OS the output stream to which the template name will be
279  /// printed.
280  ///
281  /// \param SuppressNNS if true, don't print the
282  /// nested-name-specifier that precedes the template name (if it has
283  /// one).
284  void print(raw_ostream &OS, const PrintingPolicy &Policy,
285  bool SuppressNNS = false) const;
286 
287  /// \brief Debugging aid that dumps the template name.
288  void dump(raw_ostream &OS) const;
289 
290  /// \brief Debugging aid that dumps the template name to standard
291  /// error.
292  void dump() const;
293 
294  void Profile(llvm::FoldingSetNodeID &ID) {
295  ID.AddPointer(Storage.getOpaqueValue());
296  }
297 
298  /// \brief Retrieve the template name as a void pointer.
299  void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
300 
301  /// \brief Build a template name from a void pointer.
302  static TemplateName getFromVoidPointer(void *Ptr) {
303  return TemplateName(Ptr);
304  }
305 };
306 
307 /// Insertion operator for diagnostics. This allows sending TemplateName's
308 /// into a diagnostic with <<.
309 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
310  TemplateName N);
311 
312 /// \brief A structure for storing the information associated with a
313 /// substituted template template parameter.
315  : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
316  friend class ASTContext;
317 
318  TemplateTemplateParmDecl *Parameter;
320 
322  TemplateName replacement)
324  Parameter(parameter), Replacement(replacement) {}
325 
326 public:
327  TemplateTemplateParmDecl *getParameter() const { return Parameter; }
329 
330  void Profile(llvm::FoldingSetNodeID &ID);
331 
332  static void Profile(llvm::FoldingSetNodeID &ID,
333  TemplateTemplateParmDecl *parameter,
334  TemplateName replacement);
335 };
336 
340  return subst->getReplacement().getUnderlying();
341  return *this;
342 }
343 
344 /// \brief Represents a template name that was expressed as a
345 /// qualified name.
346 ///
347 /// This kind of template name refers to a template name that was
348 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
349 /// the nested name specifier is "std::" and the template name is the
350 /// declaration for "vector". The QualifiedTemplateName class is only
351 /// used to provide "sugar" for template names that were expressed
352 /// with a qualified name, and has no semantic meaning. In this
353 /// manner, it is to TemplateName what ElaboratedType is to Type,
354 /// providing extra syntactic sugar for downstream clients.
355 class QualifiedTemplateName : public llvm::FoldingSetNode {
356  /// \brief The nested name specifier that qualifies the template name.
357  ///
358  /// The bit is used to indicate whether the "template" keyword was
359  /// present before the template name itself. Note that the
360  /// "template" keyword is always redundant in this case (otherwise,
361  /// the template name would be a dependent name and we would express
362  /// this name with DependentTemplateName).
363  llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
364 
365  /// \brief The template declaration or set of overloaded function templates
366  /// that this qualified name refers to.
367  TemplateDecl *Template;
368 
369  friend class ASTContext;
370 
371  QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
372  TemplateDecl *Template)
373  : Qualifier(NNS, TemplateKeyword? 1 : 0),
374  Template(Template) { }
375 
376 public:
377  /// \brief Return the nested name specifier that qualifies this name.
378  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
379 
380  /// \brief Whether the template name was prefixed by the "template"
381  /// keyword.
382  bool hasTemplateKeyword() const { return Qualifier.getInt(); }
383 
384  /// \brief The template declaration that this qualified name refers
385  /// to.
386  TemplateDecl *getDecl() const { return Template; }
387 
388  /// \brief The template declaration to which this qualified name
389  /// refers.
390  TemplateDecl *getTemplateDecl() const { return Template; }
391 
392  void Profile(llvm::FoldingSetNodeID &ID) {
394  }
395 
396  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
397  bool TemplateKeyword, TemplateDecl *Template) {
398  ID.AddPointer(NNS);
399  ID.AddBoolean(TemplateKeyword);
400  ID.AddPointer(Template);
401  }
402 };
403 
404 /// \brief Represents a dependent template name that cannot be
405 /// resolved prior to template instantiation.
406 ///
407 /// This kind of template name refers to a dependent template name,
408 /// including its nested name specifier (if any). For example,
409 /// DependentTemplateName can refer to "MetaFun::template apply",
410 /// where "MetaFun::" is the nested name specifier and "apply" is the
411 /// template name referenced. The "template" keyword is implied.
412 class DependentTemplateName : public llvm::FoldingSetNode {
413  /// \brief The nested name specifier that qualifies the template
414  /// name.
415  ///
416  /// The bit stored in this qualifier describes whether the \c Name field
417  /// is interpreted as an IdentifierInfo pointer (when clear) or as an
418  /// overloaded operator kind (when set).
419  llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
420 
421  /// \brief The dependent template name.
422  union {
423  /// \brief The identifier template name.
424  ///
425  /// Only valid when the bit on \c Qualifier is clear.
427 
428  /// \brief The overloaded operator name.
429  ///
430  /// Only valid when the bit on \c Qualifier is set.
432  };
433 
434  /// \brief The canonical template name to which this dependent
435  /// template name refers.
436  ///
437  /// The canonical template name for a dependent template name is
438  /// another dependent template name whose nested name specifier is
439  /// canonical.
440  TemplateName CanonicalTemplateName;
441 
442  friend class ASTContext;
443 
445  const IdentifierInfo *Identifier)
446  : Qualifier(Qualifier, false), Identifier(Identifier),
447  CanonicalTemplateName(this) { }
448 
450  const IdentifierInfo *Identifier,
451  TemplateName Canon)
452  : Qualifier(Qualifier, false), Identifier(Identifier),
453  CanonicalTemplateName(Canon) { }
454 
455  DependentTemplateName(NestedNameSpecifier *Qualifier,
457  : Qualifier(Qualifier, true), Operator(Operator),
458  CanonicalTemplateName(this) { }
459 
460  DependentTemplateName(NestedNameSpecifier *Qualifier,
462  TemplateName Canon)
463  : Qualifier(Qualifier, true), Operator(Operator),
464  CanonicalTemplateName(Canon) { }
465 
466 public:
467  /// \brief Return the nested name specifier that qualifies this name.
468  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
469 
470  /// \brief Determine whether this template name refers to an identifier.
471  bool isIdentifier() const { return !Qualifier.getInt(); }
472 
473  /// \brief Returns the identifier to which this template name refers.
474  const IdentifierInfo *getIdentifier() const {
475  assert(isIdentifier() && "Template name isn't an identifier?");
476  return Identifier;
477  }
478 
479  /// \brief Determine whether this template name refers to an overloaded
480  /// operator.
481  bool isOverloadedOperator() const { return Qualifier.getInt(); }
482 
483  /// \brief Return the overloaded operator to which this template name refers.
485  assert(isOverloadedOperator() &&
486  "Template name isn't an overloaded operator?");
487  return Operator;
488  }
489 
490  void Profile(llvm::FoldingSetNodeID &ID) {
491  if (isIdentifier())
493  else
494  Profile(ID, getQualifier(), getOperator());
495  }
496 
497  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
498  const IdentifierInfo *Identifier) {
499  ID.AddPointer(NNS);
500  ID.AddBoolean(false);
501  ID.AddPointer(Identifier);
502  }
503 
504  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
506  ID.AddPointer(NNS);
507  ID.AddBoolean(true);
508  ID.AddInteger(Operator);
509  }
510 };
511 
512 } // end namespace clang.
513 
514 namespace llvm {
515 
516 /// \brief The clang::TemplateName class is effectively a pointer.
517 template<>
518 class PointerLikeTypeTraits<clang::TemplateName> {
519 public:
520  static inline void *getAsVoidPointer(clang::TemplateName TN) {
521  return TN.getAsVoidPointer();
522  }
523 
524  static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
526  }
527 
528  // No bits are available!
529  enum { NumLowBitsAvailable = 0 };
530 };
531 
532 } // end namespace llvm.
533 
534 #endif
static llvm::GlobalValue::DLLStorageClassTypes getStorage(CodeGenModule &CGM, StringRef Name)
Definition: CGObjCMac.cpp:5981
void print(raw_ostream &OS, const PrintingPolicy &Policy, bool SuppressNNS=false) const
Print the template name.
unsigned Size
The number of stored templates or template arguments, depending on which subclass we have...
Definition: TemplateName.h:56
TemplateDecl * getAsTemplateDecl() const
Retrieve the underlying template declaration that this template name refers to, if known...
OverloadedOperatorKind getOperator() const
Return the overloaded operator to which this template name refers.
Definition: TemplateName.h:484
static clang::TemplateName getFromVoidPointer(void *Ptr)
Definition: TemplateName.h:524
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:198
TemplateTemplateParmDecl * getParameterPack() const
Retrieve the template template parameter pack being substituted.
Definition: TemplateName.h:133
A template template parameter that has been substituted for some other template name.
Definition: TemplateName.h:201
void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context)
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:38
TemplateTemplateParmDecl * getParameter() const
Definition: TemplateName.h:327
const IdentifierInfo * getIdentifier() const
Returns the identifier to which this template name refers.
Definition: TemplateName.h:474
One of these records is kept for each identifier that is lexed.
OverloadedTemplateStorage * getAsOverloadedTemplate() const
Retrieve the underlying, overloaded function template.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
Represents a dependent template name that cannot be resolved prior to template instantiation.
Definition: TemplateName.h:412
bool isIdentifier() const
Determine whether this template name refers to an identifier.
Definition: TemplateName.h:471
SubstTemplateTemplateParmStorage * getAsSubstTemplateTemplateParm()
Definition: TemplateName.h:78
NameKind getKind() const
A qualified template name, where the qualification is kept to describe the source code as written...
Definition: TemplateName.h:195
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
TemplateArgument getArgumentPack() const
Retrieve the template template argument pack with which this parameter was substituted.
QualifiedTemplateName * getAsQualifiedTemplateName() const
Retrieve the underlying qualified template name structure, if any.
TemplateDecl * getTemplateDecl() const
The template declaration to which this qualified name refers.
Definition: TemplateName.h:390
SubstTemplateTemplateParmPackStorage * getAsSubstTemplateTemplateParmPack()
Definition: TemplateName.h:84
NestedNameSpecifier * getQualifier() const
Return the nested name specifier that qualifies this name.
Definition: TemplateName.h:468
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:490
TemplateName getUnderlying() const
Definition: TemplateName.h:337
bool containsUnexpandedParameterPack() const
Determines whether this template name contains an unexpanded parameter pack (for C++0x variadic templ...
A dependent template name that has not been resolved to a template (or set of templates).
Definition: TemplateName.h:198
ASTContext * Context
static void * getAsVoidPointer(clang::TemplateName TN)
Definition: TemplateName.h:520
friend class ASTContext
Definition: Type.h:4178
OverloadedOperatorKind Operator
The overloaded operator name.
Definition: TemplateName.h:431
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, OverloadedOperatorKind Operator)
Definition: TemplateName.h:504
bool isInstantiationDependent() const
Determines whether this is a template name that somehow depends on a template parameter.
void Profile(llvm::FoldingSetNodeID &ID)
A structure for storing the information associated with a substituted template template parameter...
Definition: TemplateName.h:314
SubstTemplateTemplateParmPackStorage * getAsSubstTemplateTemplateParmPack() const
Retrieve the substituted template template parameter pack, if known.
Represents a C++ template name within the type system.
Definition: TemplateName.h:176
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, const IdentifierInfo *Identifier)
Definition: TemplateName.h:497
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, bool TemplateKeyword, TemplateDecl *Template)
Definition: TemplateName.h:396
TemplateTemplateParmDecl - Declares a template template parameter, e.g., "T" in.
A template template parameter pack that has been substituted for a template template argument pack...
Definition: TemplateName.h:205
OverloadedTemplateStorage * getAsOverloadedStorage()
Definition: TemplateName.h:72
static TemplateName getFromVoidPointer(void *Ptr)
Build a template name from a void pointer.
Definition: TemplateName.h:302
#define false
Definition: stdbool.h:33
A structure for storing an already-substituted template template parameter pack.
Definition: TemplateName.h:119
const std::string ID
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:392
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
UncommonTemplateNameStorage(Kind kind, unsigned size)
Definition: TemplateName.h:64
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:294
SubstTemplateTemplateParmStorage * getAsSubstTemplateTemplateParm() const
Retrieve the substituted template template parameter, if known.
Represents a template argument.
Definition: TemplateBase.h:40
Represents a template name that was expressed as a qualified name.
Definition: TemplateName.h:355
void * getAsVoidPointer() const
Retrieve the template name as a void pointer.
Definition: TemplateName.h:299
bool hasTemplateKeyword() const
Whether the template name was prefixed by the "template" keyword.
Definition: TemplateName.h:382
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:330
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
Definition: OperatorKinds.h:22
bool isDependent() const
Determines whether this is a dependent template name.
bool isOverloadedOperator() const
Determine whether this template name refers to an overloaded operator.
Definition: TemplateName.h:481
SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter, unsigned Size, const TemplateArgument *Arguments)
Definition: TemplateName.h:126
const IdentifierInfo * Identifier
The identifier template name.
Definition: TemplateName.h:426
bool isNull() const
Determine whether this template name is NULL.
TemplateDecl * getDecl() const
The template declaration that this qualified name refers to.
Definition: TemplateName.h:386
const Expr * Replacement
Definition: AttributeList.h:58
DependentTemplateName * getAsDependentTemplateName() const
Retrieve the underlying dependent template name structure, if any.
A structure for storing the information associated with an overloaded template name.
Definition: TemplateName.h:93
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:43
NestedNameSpecifier * getQualifier() const
Return the nested name specifier that qualifies this name.
Definition: TemplateName.h:378
#define true
Definition: stdbool.h:32
A set of overloaded template declarations.
Definition: TemplateName.h:192
NamedDecl - This represents a decl with a name.
Definition: Decl.h:213
void dump() const
Debugging aid that dumps the template name to standard error.
Implementation class used to describe either a set of overloaded template names or an already-substit...
Definition: TemplateName.h:42
A single template declaration.
Definition: TemplateName.h:190