clang  6.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/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 {
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.
139  TemplateArgument getArgumentPack() const;
140 
141  void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
142 
143  static void Profile(llvm::FoldingSetNodeID &ID,
144  ASTContext &Context,
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() { }
209  explicit TemplateName(TemplateDecl *Template);
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.
229  TemplateDecl *getAsTemplateDecl() const;
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.
238  OverloadedTemplateStorage *getAsOverloadedTemplate() const;
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.
257  QualifiedTemplateName *getAsQualifiedTemplateName() const;
258 
259  /// \brief Retrieve the underlying dependent template name
260  /// structure, if any.
261  DependentTemplateName *getAsDependentTemplateName() const;
262 
263  TemplateName getUnderlying() const;
264 
265  /// Get the template name to substitute when this template name is used as a
266  /// template template argument. This refers to the most recent declaration of
267  /// the template, including any default template arguments.
268  TemplateName getNameToSubstitute() const;
269 
270  /// \brief Determines whether this is a dependent template name.
271  bool isDependent() const;
272 
273  /// \brief Determines whether this is a template name that somehow
274  /// depends on a template parameter.
275  bool isInstantiationDependent() const;
276 
277  /// \brief Determines whether this template name contains an
278  /// unexpanded parameter pack (for C++0x variadic templates).
279  bool containsUnexpandedParameterPack() const;
280 
281  /// \brief Print the template name.
282  ///
283  /// \param OS the output stream to which the template name will be
284  /// printed.
285  ///
286  /// \param SuppressNNS if true, don't print the
287  /// nested-name-specifier that precedes the template name (if it has
288  /// one).
289  void print(raw_ostream &OS, const PrintingPolicy &Policy,
290  bool SuppressNNS = false) const;
291 
292  /// \brief Debugging aid that dumps the template name.
293  void dump(raw_ostream &OS) const;
294 
295  /// \brief Debugging aid that dumps the template name to standard
296  /// error.
297  void dump() const;
298 
299  void Profile(llvm::FoldingSetNodeID &ID) {
300  ID.AddPointer(Storage.getOpaqueValue());
301  }
302 
303  /// \brief Retrieve the template name as a void pointer.
304  void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
305 
306  /// \brief Build a template name from a void pointer.
307  static TemplateName getFromVoidPointer(void *Ptr) {
308  return TemplateName(Ptr);
309  }
310 };
311 
312 /// Insertion operator for diagnostics. This allows sending TemplateName's
313 /// into a diagnostic with <<.
315  TemplateName N);
316 
317 /// \brief A structure for storing the information associated with a
318 /// substituted template template parameter.
320  : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
321  friend class ASTContext;
322 
325 
327  TemplateName replacement)
329  Parameter(parameter), Replacement(replacement) {}
330 
331 public:
334 
335  void Profile(llvm::FoldingSetNodeID &ID);
336 
337  static void Profile(llvm::FoldingSetNodeID &ID,
338  TemplateTemplateParmDecl *parameter,
339  TemplateName replacement);
340 };
341 
345  return subst->getReplacement().getUnderlying();
346  return *this;
347 }
348 
349 /// \brief Represents a template name that was expressed as a
350 /// qualified name.
351 ///
352 /// This kind of template name refers to a template name that was
353 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
354 /// the nested name specifier is "std::" and the template name is the
355 /// declaration for "vector". The QualifiedTemplateName class is only
356 /// used to provide "sugar" for template names that were expressed
357 /// with a qualified name, and has no semantic meaning. In this
358 /// manner, it is to TemplateName what ElaboratedType is to Type,
359 /// providing extra syntactic sugar for downstream clients.
360 class QualifiedTemplateName : public llvm::FoldingSetNode {
361  /// \brief The nested name specifier that qualifies the template name.
362  ///
363  /// The bit is used to indicate whether the "template" keyword was
364  /// present before the template name itself. Note that the
365  /// "template" keyword is always redundant in this case (otherwise,
366  /// the template name would be a dependent name and we would express
367  /// this name with DependentTemplateName).
368  llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
369 
370  /// \brief The template declaration or set of overloaded function templates
371  /// that this qualified name refers to.
372  TemplateDecl *Template;
373 
374  friend class ASTContext;
375 
376  QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
377  TemplateDecl *Template)
378  : Qualifier(NNS, TemplateKeyword? 1 : 0),
379  Template(Template) { }
380 
381 public:
382  /// \brief Return the nested name specifier that qualifies this name.
383  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
384 
385  /// \brief Whether the template name was prefixed by the "template"
386  /// keyword.
387  bool hasTemplateKeyword() const { return Qualifier.getInt(); }
388 
389  /// \brief The template declaration that this qualified name refers
390  /// to.
391  TemplateDecl *getDecl() const { return Template; }
392 
393  /// \brief The template declaration to which this qualified name
394  /// refers.
395  TemplateDecl *getTemplateDecl() const { return Template; }
396 
397  void Profile(llvm::FoldingSetNodeID &ID) {
398  Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
399  }
400 
401  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
402  bool TemplateKeyword, TemplateDecl *Template) {
403  ID.AddPointer(NNS);
404  ID.AddBoolean(TemplateKeyword);
405  ID.AddPointer(Template);
406  }
407 };
408 
409 /// \brief Represents a dependent template name that cannot be
410 /// resolved prior to template instantiation.
411 ///
412 /// This kind of template name refers to a dependent template name,
413 /// including its nested name specifier (if any). For example,
414 /// DependentTemplateName can refer to "MetaFun::template apply",
415 /// where "MetaFun::" is the nested name specifier and "apply" is the
416 /// template name referenced. The "template" keyword is implied.
417 class DependentTemplateName : public llvm::FoldingSetNode {
418  /// \brief The nested name specifier that qualifies the template
419  /// name.
420  ///
421  /// The bit stored in this qualifier describes whether the \c Name field
422  /// is interpreted as an IdentifierInfo pointer (when clear) or as an
423  /// overloaded operator kind (when set).
424  llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
425 
426  /// \brief The dependent template name.
427  union {
428  /// \brief The identifier template name.
429  ///
430  /// Only valid when the bit on \c Qualifier is clear.
432 
433  /// \brief The overloaded operator name.
434  ///
435  /// Only valid when the bit on \c Qualifier is set.
437  };
438 
439  /// \brief The canonical template name to which this dependent
440  /// template name refers.
441  ///
442  /// The canonical template name for a dependent template name is
443  /// another dependent template name whose nested name specifier is
444  /// canonical.
445  TemplateName CanonicalTemplateName;
446 
447  friend class ASTContext;
448 
450  const IdentifierInfo *Identifier)
451  : Qualifier(Qualifier, false), Identifier(Identifier),
452  CanonicalTemplateName(this) { }
453 
455  const IdentifierInfo *Identifier,
456  TemplateName Canon)
457  : Qualifier(Qualifier, false), Identifier(Identifier),
458  CanonicalTemplateName(Canon) { }
459 
461  OverloadedOperatorKind Operator)
462  : Qualifier(Qualifier, true), Operator(Operator),
463  CanonicalTemplateName(this) { }
464 
466  OverloadedOperatorKind Operator,
467  TemplateName Canon)
468  : Qualifier(Qualifier, true), Operator(Operator),
469  CanonicalTemplateName(Canon) { }
470 
471 public:
472  /// \brief Return the nested name specifier that qualifies this name.
473  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
474 
475  /// \brief Determine whether this template name refers to an identifier.
476  bool isIdentifier() const { return !Qualifier.getInt(); }
477 
478  /// \brief Returns the identifier to which this template name refers.
479  const IdentifierInfo *getIdentifier() const {
480  assert(isIdentifier() && "Template name isn't an identifier?");
481  return Identifier;
482  }
483 
484  /// \brief Determine whether this template name refers to an overloaded
485  /// operator.
486  bool isOverloadedOperator() const { return Qualifier.getInt(); }
487 
488  /// \brief Return the overloaded operator to which this template name refers.
490  assert(isOverloadedOperator() &&
491  "Template name isn't an overloaded operator?");
492  return Operator;
493  }
494 
495  void Profile(llvm::FoldingSetNodeID &ID) {
496  if (isIdentifier())
497  Profile(ID, getQualifier(), getIdentifier());
498  else
499  Profile(ID, getQualifier(), getOperator());
500  }
501 
502  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
503  const IdentifierInfo *Identifier) {
504  ID.AddPointer(NNS);
505  ID.AddBoolean(false);
506  ID.AddPointer(Identifier);
507  }
508 
509  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
510  OverloadedOperatorKind Operator) {
511  ID.AddPointer(NNS);
512  ID.AddBoolean(true);
513  ID.AddInteger(Operator);
514  }
515 };
516 
517 } // end namespace clang.
518 
519 namespace llvm {
520 
521 /// \brief The clang::TemplateName class is effectively a pointer.
522 template<>
523 struct PointerLikeTypeTraits<clang::TemplateName> {
524  static inline void *getAsVoidPointer(clang::TemplateName TN) {
525  return TN.getAsVoidPointer();
526  }
527 
528  static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
530  }
531 
532  // No bits are available!
533  enum { NumLowBitsAvailable = 0 };
534 };
535 
536 } // end namespace llvm.
537 
538 #endif
TemplateTemplateParmDecl * getParameterPack() const
Retrieve the template template parameter pack being substituted.
Definition: TemplateName.h:133
static llvm::GlobalValue::DLLStorageClassTypes getStorage(CodeGenModule &CGM, StringRef Name)
Definition: CGObjCMac.cpp:6220
void * getAsVoidPointer() const
Retrieve the template name as a void pointer.
Definition: TemplateName.h:304
bool hasTemplateKeyword() const
Whether the template name was prefixed by the "template" keyword.
Definition: TemplateName.h:387
OverloadedOperatorKind getOperator() const
Return the overloaded operator to which this template name refers.
Definition: TemplateName.h:489
NestedNameSpecifier * getQualifier() const
Return the nested name specifier that qualifies this name.
Definition: TemplateName.h:473
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
unsigned Size
The number of stored templates or template arguments, depending on which subclass we have...
Definition: TemplateName.h:56
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:195
A template template parameter that has been substituted for some other template name.
Definition: TemplateName.h:201
const IdentifierInfo * getIdentifier() const
Returns the identifier to which this template name refers.
Definition: TemplateName.h:479
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:342
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
Represents a dependent template name that cannot be resolved prior to template instantiation.
Definition: TemplateName.h:417
SubstTemplateTemplateParmStorage * getAsSubstTemplateTemplateParm()
Definition: TemplateName.h:78
A qualified template name, where the qualification is kept to describe the source code as written...
Definition: TemplateName.h:195
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:84
TemplateTemplateParmDecl * getParameter() const
Definition: TemplateName.h:332
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:495
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:953
A dependent template name that has not been resolved to a template (or set of templates).
Definition: TemplateName.h:198
TemplateDecl * getTemplateDecl() const
The template declaration to which this qualified name refers.
Definition: TemplateName.h:395
OverloadedOperatorKind Operator
The overloaded operator name.
Definition: TemplateName.h:436
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, OverloadedOperatorKind Operator)
Definition: TemplateName.h:509
A structure for storing the information associated with a substituted template template parameter...
Definition: TemplateName.h:319
Represents a C++ template name within the type system.
Definition: TemplateName.h:176
bool isIdentifier() const
Determine whether this template name refers to an identifier.
Definition: TemplateName.h:476
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, const IdentifierInfo *Identifier)
Definition: TemplateName.h:502
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, bool TemplateKeyword, TemplateDecl *Template)
Definition: TemplateName.h:401
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:383
OverloadedTemplateStorage * getAsOverloadedStorage()
Definition: TemplateName.h:72
static TemplateName getFromVoidPointer(void *Ptr)
Build a template name from a void pointer.
Definition: TemplateName.h:307
#define false
Definition: stdbool.h:33
A structure for storing an already-substituted template template parameter pack.
Definition: TemplateName.h:119
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:397
static void * getAsVoidPointer(clang::TemplateName TN)
Definition: TemplateName.h:524
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:486
UncommonTemplateNameStorage(Kind kind, unsigned size)
Definition: TemplateName.h:64
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:299
static clang::TemplateName getFromVoidPointer(void *Ptr)
Definition: TemplateName.h:528
Represents a template argument.
Definition: TemplateBase.h:40
Represents a template name that was expressed as a qualified name.
Definition: TemplateName.h:360
Dataflow Directional Tag Classes.
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:378
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:126
const IdentifierInfo * Identifier
The identifier template name.
Definition: TemplateName.h:431
const Expr * Replacement
Definition: AttributeList.h:59
The parameter type of a method or function.
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:61
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:896
TemplateDecl * getDecl() const
The template declaration that this qualified name refers to.
Definition: TemplateName.h:391
#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
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