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