clang  9.0.0svn
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/PointerIntPair.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/Support/PointerLikeTypeTraits.h"
23 #include <cassert>
24 
25 namespace clang {
26 
27 class ASTContext;
28 class DependentTemplateName;
29 class DiagnosticBuilder;
30 class IdentifierInfo;
31 class NamedDecl;
32 class NestedNameSpecifier;
33 enum OverloadedOperatorKind : int;
34 class OverloadedTemplateStorage;
35 struct PrintingPolicy;
36 class QualifiedTemplateName;
37 class SubstTemplateTemplateParmPackStorage;
38 class SubstTemplateTemplateParmStorage;
39 class TemplateArgument;
40 class TemplateDecl;
41 class TemplateTemplateParmDecl;
42 
43 /// Implementation class used to describe either a set of overloaded
44 /// template names or an already-substituted template template parameter pack.
46 protected:
47  enum Kind {
51  };
52 
53  struct BitsTag {
54  /// A Kind.
55  unsigned Kind : 2;
56 
57  /// The number of stored templates or template arguments,
58  /// depending on which subclass we have.
59  unsigned Size : 30;
60  };
61 
62  union {
63  struct BitsTag Bits;
65  };
66 
68  Bits.Kind = kind;
69  Bits.Size = size;
70  }
71 
72 public:
73  unsigned size() const { return Bits.Size; }
74 
76  return Bits.Kind == Overloaded
77  ? reinterpret_cast<OverloadedTemplateStorage *>(this)
78  : nullptr;
79  }
80 
83  ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
84  : nullptr;
85  }
86 
89  ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
90  : nullptr;
91  }
92 };
93 
94 /// A structure for storing the information associated with an
95 /// overloaded template name.
97  friend class ASTContext;
98 
101 
102  NamedDecl **getStorage() {
103  return reinterpret_cast<NamedDecl **>(this + 1);
104  }
105  NamedDecl * const *getStorage() const {
106  return reinterpret_cast<NamedDecl *const *>(this + 1);
107  }
108 
109 public:
110  using iterator = NamedDecl *const *;
111 
112  iterator begin() const { return getStorage(); }
113  iterator end() const { return getStorage() + size(); }
114 };
115 
116 /// A structure for storing an already-substituted template template
117 /// parameter pack.
118 ///
119 /// This kind of template names occurs when the parameter pack has been
120 /// provided with a template template argument pack in a context where its
121 /// enclosing pack expansion could not be fully expanded.
123  : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
124 {
126  const TemplateArgument *Arguments;
127 
128 public:
130  unsigned Size,
131  const TemplateArgument *Arguments)
133  Parameter(Parameter), Arguments(Arguments) {}
134 
135  /// Retrieve the template template parameter pack being substituted.
137  return Parameter;
138  }
139 
140  /// Retrieve the template template argument pack with which this
141  /// parameter was substituted.
142  TemplateArgument getArgumentPack() const;
143 
144  void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
145 
146  static void Profile(llvm::FoldingSetNodeID &ID,
147  ASTContext &Context,
148  TemplateTemplateParmDecl *Parameter,
149  const TemplateArgument &ArgPack);
150 };
151 
152 /// Represents a C++ template name within the type system.
153 ///
154 /// A C++ template name refers to a template within the C++ type
155 /// system. In most cases, a template name is simply a reference to a
156 /// class template, e.g.
157 ///
158 /// \code
159 /// template<typename T> class X { };
160 ///
161 /// X<int> xi;
162 /// \endcode
163 ///
164 /// Here, the 'X' in \c X<int> is a template name that refers to the
165 /// declaration of the class template X, above. Template names can
166 /// also refer to function templates, C++0x template aliases, etc.
167 ///
168 /// Some template names are dependent. For example, consider:
169 ///
170 /// \code
171 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
172 /// typedef typename MetaFun::template apply<T1, T2>::type type;
173 /// };
174 /// \endcode
175 ///
176 /// Here, "apply" is treated as a template name within the typename
177 /// specifier in the typedef. "apply" is a nested template, and can
178 /// only be understood in the context of
180  using StorageType =
181  llvm::PointerUnion4<TemplateDecl *, UncommonTemplateNameStorage *,
183 
184  StorageType Storage;
185 
186  explicit TemplateName(void *Ptr);
187 
188 public:
189  // Kind of name that is actually stored.
190  enum NameKind {
191  /// A single template declaration.
193 
194  /// A set of overloaded template declarations.
196 
197  /// A qualified template name, where the qualification is kept
198  /// to describe the source code as written.
200 
201  /// A dependent template name that has not been resolved to a
202  /// template (or set of templates).
204 
205  /// A template template parameter that has been substituted
206  /// for some other template name.
208 
209  /// A template template parameter pack that has been substituted for
210  /// a template template argument pack, but has not yet been expanded into
211  /// individual arguments.
213  };
214 
215  TemplateName() = default;
216  explicit TemplateName(TemplateDecl *Template);
217  explicit TemplateName(OverloadedTemplateStorage *Storage);
220  explicit TemplateName(QualifiedTemplateName *Qual);
221  explicit TemplateName(DependentTemplateName *Dep);
222 
223  /// Determine whether this template name is NULL.
224  bool isNull() const;
225 
226  // Get the kind of name that is actually stored.
227  NameKind getKind() const;
228 
229  /// Retrieve the underlying template declaration that
230  /// this template name refers to, if known.
231  ///
232  /// \returns The template declaration that this template name refers
233  /// to, if any. If the template name does not refer to a specific
234  /// declaration because it is a dependent name, or if it refers to a
235  /// set of function templates, returns NULL.
236  TemplateDecl *getAsTemplateDecl() const;
237 
238  /// Retrieve the underlying, overloaded function template
239  // declarations that this template name refers to, if known.
240  ///
241  /// \returns The set of overloaded function templates that this template
242  /// name refers to, if known. If the template name does not refer to a
243  /// specific set of function templates because it is a dependent name or
244  /// refers to a single template, returns NULL.
245  OverloadedTemplateStorage *getAsOverloadedTemplate() const;
246 
247  /// Retrieve the substituted template template parameter, if
248  /// known.
249  ///
250  /// \returns The storage for the substituted template template parameter,
251  /// if known. Otherwise, returns NULL.
253 
254  /// Retrieve the substituted template template parameter pack, if
255  /// known.
256  ///
257  /// \returns The storage for the substituted template template parameter pack,
258  /// if known. Otherwise, returns NULL.
261 
262  /// Retrieve the underlying qualified template name
263  /// structure, if any.
264  QualifiedTemplateName *getAsQualifiedTemplateName() const;
265 
266  /// Retrieve the underlying dependent template name
267  /// structure, if any.
268  DependentTemplateName *getAsDependentTemplateName() const;
269 
270  TemplateName getUnderlying() const;
271 
272  /// Get the template name to substitute when this template name is used as a
273  /// template template argument. This refers to the most recent declaration of
274  /// the template, including any default template arguments.
275  TemplateName getNameToSubstitute() const;
276 
277  /// Determines whether this is a dependent template name.
278  bool isDependent() const;
279 
280  /// Determines whether this is a template name that somehow
281  /// depends on a template parameter.
282  bool isInstantiationDependent() const;
283 
284  /// Determines whether this template name contains an
285  /// unexpanded parameter pack (for C++0x variadic templates).
286  bool containsUnexpandedParameterPack() const;
287 
288  /// Print the template name.
289  ///
290  /// \param OS the output stream to which the template name will be
291  /// printed.
292  ///
293  /// \param SuppressNNS if true, don't print the
294  /// nested-name-specifier that precedes the template name (if it has
295  /// one).
296  void print(raw_ostream &OS, const PrintingPolicy &Policy,
297  bool SuppressNNS = false) const;
298 
299  /// Debugging aid that dumps the template name.
300  void dump(raw_ostream &OS) const;
301 
302  /// Debugging aid that dumps the template name to standard
303  /// error.
304  void dump() const;
305 
306  void Profile(llvm::FoldingSetNodeID &ID) {
307  ID.AddPointer(Storage.getOpaqueValue());
308  }
309 
310  /// Retrieve the template name as a void pointer.
311  void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
312 
313  /// Build a template name from a void pointer.
314  static TemplateName getFromVoidPointer(void *Ptr) {
315  return TemplateName(Ptr);
316  }
317 };
318 
319 /// Insertion operator for diagnostics. This allows sending TemplateName's
320 /// into a diagnostic with <<.
322  TemplateName N);
323 
324 /// A structure for storing the information associated with a
325 /// substituted template template parameter.
327  : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
328  friend class ASTContext;
329 
331  TemplateName Replacement;
332 
334  TemplateName replacement)
336  Parameter(parameter), Replacement(replacement) {}
337 
338 public:
340  TemplateName getReplacement() const { return Replacement; }
341 
342  void Profile(llvm::FoldingSetNodeID &ID);
343 
344  static void Profile(llvm::FoldingSetNodeID &ID,
345  TemplateTemplateParmDecl *parameter,
346  TemplateName replacement);
347 };
348 
352  return subst->getReplacement().getUnderlying();
353  return *this;
354 }
355 
356 /// Represents a template name that was expressed as a
357 /// qualified name.
358 ///
359 /// This kind of template name refers to a template name that was
360 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
361 /// the nested name specifier is "std::" and the template name is the
362 /// declaration for "vector". The QualifiedTemplateName class is only
363 /// used to provide "sugar" for template names that were expressed
364 /// with a qualified name, and has no semantic meaning. In this
365 /// manner, it is to TemplateName what ElaboratedType is to Type,
366 /// providing extra syntactic sugar for downstream clients.
367 class QualifiedTemplateName : public llvm::FoldingSetNode {
368  friend class ASTContext;
369 
370  /// The nested name specifier that qualifies the template name.
371  ///
372  /// The bit is used to indicate whether the "template" keyword was
373  /// present before the template name itself. Note that the
374  /// "template" keyword is always redundant in this case (otherwise,
375  /// the template name would be a dependent name and we would express
376  /// this name with DependentTemplateName).
377  llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
378 
379  /// The template declaration or set of overloaded function templates
380  /// that this qualified name refers to.
381  TemplateDecl *Template;
382 
383  QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
384  TemplateDecl *Template)
385  : Qualifier(NNS, TemplateKeyword? 1 : 0), Template(Template) {}
386 
387 public:
388  /// Return the nested name specifier that qualifies this name.
389  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
390 
391  /// Whether the template name was prefixed by the "template"
392  /// keyword.
393  bool hasTemplateKeyword() const { return Qualifier.getInt(); }
394 
395  /// The template declaration that this qualified name refers
396  /// to.
397  TemplateDecl *getDecl() const { return Template; }
398 
399  /// The template declaration to which this qualified name
400  /// refers.
401  TemplateDecl *getTemplateDecl() const { return Template; }
402 
403  void Profile(llvm::FoldingSetNodeID &ID) {
404  Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
405  }
406 
407  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
408  bool TemplateKeyword, TemplateDecl *Template) {
409  ID.AddPointer(NNS);
410  ID.AddBoolean(TemplateKeyword);
411  ID.AddPointer(Template);
412  }
413 };
414 
415 /// Represents a dependent template name that cannot be
416 /// resolved prior to template instantiation.
417 ///
418 /// This kind of template name refers to a dependent template name,
419 /// including its nested name specifier (if any). For example,
420 /// DependentTemplateName can refer to "MetaFun::template apply",
421 /// where "MetaFun::" is the nested name specifier and "apply" is the
422 /// template name referenced. The "template" keyword is implied.
423 class DependentTemplateName : public llvm::FoldingSetNode {
424  friend class ASTContext;
425 
426  /// The nested name specifier that qualifies the template
427  /// name.
428  ///
429  /// The bit stored in this qualifier describes whether the \c Name field
430  /// is interpreted as an IdentifierInfo pointer (when clear) or as an
431  /// overloaded operator kind (when set).
432  llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
433 
434  /// The dependent template name.
435  union {
436  /// The identifier template name.
437  ///
438  /// Only valid when the bit on \c Qualifier is clear.
440 
441  /// The overloaded operator name.
442  ///
443  /// Only valid when the bit on \c Qualifier is set.
445  };
446 
447  /// The canonical template name to which this dependent
448  /// template name refers.
449  ///
450  /// The canonical template name for a dependent template name is
451  /// another dependent template name whose nested name specifier is
452  /// canonical.
453  TemplateName CanonicalTemplateName;
454 
456  const IdentifierInfo *Identifier)
457  : Qualifier(Qualifier, false), Identifier(Identifier),
458  CanonicalTemplateName(this) {}
459 
461  const IdentifierInfo *Identifier,
462  TemplateName Canon)
463  : Qualifier(Qualifier, false), Identifier(Identifier),
464  CanonicalTemplateName(Canon) {}
465 
467  OverloadedOperatorKind Operator)
468  : Qualifier(Qualifier, true), Operator(Operator),
469  CanonicalTemplateName(this) {}
470 
472  OverloadedOperatorKind Operator,
473  TemplateName Canon)
474  : Qualifier(Qualifier, true), Operator(Operator),
475  CanonicalTemplateName(Canon) {}
476 
477 public:
478  /// Return the nested name specifier that qualifies this name.
479  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
480 
481  /// Determine whether this template name refers to an identifier.
482  bool isIdentifier() const { return !Qualifier.getInt(); }
483 
484  /// Returns the identifier to which this template name refers.
485  const IdentifierInfo *getIdentifier() const {
486  assert(isIdentifier() && "Template name isn't an identifier?");
487  return Identifier;
488  }
489 
490  /// Determine whether this template name refers to an overloaded
491  /// operator.
492  bool isOverloadedOperator() const { return Qualifier.getInt(); }
493 
494  /// Return the overloaded operator to which this template name refers.
496  assert(isOverloadedOperator() &&
497  "Template name isn't an overloaded operator?");
498  return Operator;
499  }
500 
501  void Profile(llvm::FoldingSetNodeID &ID) {
502  if (isIdentifier())
503  Profile(ID, getQualifier(), getIdentifier());
504  else
505  Profile(ID, getQualifier(), getOperator());
506  }
507 
508  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
509  const IdentifierInfo *Identifier) {
510  ID.AddPointer(NNS);
511  ID.AddBoolean(false);
512  ID.AddPointer(Identifier);
513  }
514 
515  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
516  OverloadedOperatorKind Operator) {
517  ID.AddPointer(NNS);
518  ID.AddBoolean(true);
519  ID.AddInteger(Operator);
520  }
521 };
522 
523 } // namespace clang.
524 
525 namespace llvm {
526 
527 /// The clang::TemplateName class is effectively a pointer.
528 template<>
529 struct PointerLikeTypeTraits<clang::TemplateName> {
530  static inline void *getAsVoidPointer(clang::TemplateName TN) {
531  return TN.getAsVoidPointer();
532  }
533 
534  static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
536  }
537 
538  // No bits are available!
539  enum { NumLowBitsAvailable = 0 };
540 };
541 
542 } // namespace llvm.
543 
544 #endif // LLVM_CLANG_AST_TEMPLATENAME_H
TemplateTemplateParmDecl * getParameterPack() const
Retrieve the template template parameter pack being substituted.
Definition: TemplateName.h:136
static llvm::GlobalValue::DLLStorageClassTypes getStorage(CodeGenModule &CGM, StringRef Name)
Definition: CGObjCMac.cpp:6286
StringRef Identifier
Definition: Format.cpp:1636
void * getAsVoidPointer() const
Retrieve the template name as a void pointer.
Definition: TemplateName.h:311
bool hasTemplateKeyword() const
Whether the template name was prefixed by the "template" keyword.
Definition: TemplateName.h:393
OverloadedOperatorKind getOperator() const
Return the overloaded operator to which this template name refers.
Definition: TemplateName.h:495
NestedNameSpecifier * getQualifier() const
Return the nested name specifier that qualifies this name.
Definition: TemplateName.h:479
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
unsigned Size
The number of stored templates or template arguments, depending on which subclass we have...
Definition: TemplateName.h:59
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:336
A template template parameter that has been substituted for some other template name.
Definition: TemplateName.h:207
const IdentifierInfo * getIdentifier() const
Returns the identifier to which this template name refers.
Definition: TemplateName.h:485
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:38
One of these records is kept for each identifier that is lexed.
TemplateName getUnderlying() const
Definition: TemplateName.h:349
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
Represents a dependent template name that cannot be resolved prior to template instantiation.
Definition: TemplateName.h:423
SubstTemplateTemplateParmStorage * getAsSubstTemplateTemplateParm()
Definition: TemplateName.h:81
A qualified template name, where the qualification is kept to describe the source code as written...
Definition: TemplateName.h:199
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
SubstTemplateTemplateParmPackStorage * getAsSubstTemplateTemplateParmPack()
Definition: TemplateName.h:87
TemplateTemplateParmDecl * getParameter() const
Definition: TemplateName.h:339
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:501
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1043
A dependent template name that has not been resolved to a template (or set of templates).
Definition: TemplateName.h:203
TemplateDecl * getTemplateDecl() const
The template declaration to which this qualified name refers.
Definition: TemplateName.h:401
OverloadedOperatorKind Operator
The overloaded operator name.
Definition: TemplateName.h:444
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, OverloadedOperatorKind Operator)
Definition: TemplateName.h:515
A structure for storing the information associated with a substituted template template parameter...
Definition: TemplateName.h:326
Represents a C++ template name within the type system.
Definition: TemplateName.h:179
bool isIdentifier() const
Determine whether this template name refers to an identifier.
Definition: TemplateName.h:482
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, const IdentifierInfo *Identifier)
Definition: TemplateName.h:508
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, bool TemplateKeyword, TemplateDecl *Template)
Definition: TemplateName.h:407
TemplateTemplateParmDecl - Declares a template template parameter, e.g., "T" in.
static StringRef getIdentifier(const Token &Tok)
NestedNameSpecifier * getQualifier() const
Return the nested name specifier that qualifies this name.
Definition: TemplateName.h:389
OverloadedTemplateStorage * getAsOverloadedStorage()
Definition: TemplateName.h:75
static TemplateName getFromVoidPointer(void *Ptr)
Build a template name from a void pointer.
Definition: TemplateName.h:314
#define false
Definition: stdbool.h:33
A structure for storing an already-substituted template template parameter pack.
Definition: TemplateName.h:122
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:403
static void * getAsVoidPointer(clang::TemplateName TN)
Definition: TemplateName.h:530
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
bool isOverloadedOperator() const
Determine whether this template name refers to an overloaded operator.
Definition: TemplateName.h:492
UncommonTemplateNameStorage(Kind kind, unsigned size)
Definition: TemplateName.h:67
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:306
static clang::TemplateName getFromVoidPointer(void *Ptr)
Definition: TemplateName.h:534
Represents a template argument.
Definition: TemplateBase.h:51
Represents a template name that was expressed as a qualified name.
Definition: TemplateName.h:367
Dataflow Directional Tag Classes.
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:399
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
Definition: OperatorKinds.h:22
SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter, unsigned Size, const TemplateArgument *Arguments)
Definition: TemplateName.h:129
const IdentifierInfo * Identifier
The identifier template name.
Definition: TemplateName.h:439
The parameter type of a method or function.
A structure for storing the information associated with an overloaded template name.
Definition: TemplateName.h:96
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:61
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:954
TemplateDecl * getDecl() const
The template declaration that this qualified name refers to.
Definition: TemplateName.h:397
A set of overloaded template declarations.
Definition: TemplateName.h:195
This represents a decl that may have a name.
Definition: Decl.h:249
Implementation class used to describe either a set of overloaded template names or an already-substit...
Definition: TemplateName.h:45
A single template declaration.
Definition: TemplateName.h:192