clang  10.0.0git
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 
124  return llvm::makeArrayRef(begin(), end());
125  }
126 };
127 
128 /// A structure for storing an already-substituted template template
129 /// parameter pack.
130 ///
131 /// This kind of template names occurs when the parameter pack has been
132 /// provided with a template template argument pack in a context where its
133 /// enclosing pack expansion could not be fully expanded.
135  : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
136 {
138  const TemplateArgument *Arguments;
139 
140 public:
142  unsigned Size,
143  const TemplateArgument *Arguments)
145  Parameter(Parameter), Arguments(Arguments) {}
146 
147  /// Retrieve the template template parameter pack being substituted.
149  return Parameter;
150  }
151 
152  /// Retrieve the template template argument pack with which this
153  /// parameter was substituted.
154  TemplateArgument getArgumentPack() const;
155 
156  void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
157 
158  static void Profile(llvm::FoldingSetNodeID &ID,
159  ASTContext &Context,
160  TemplateTemplateParmDecl *Parameter,
161  const TemplateArgument &ArgPack);
162 };
163 
164 /// Represents a C++ template name within the type system.
165 ///
166 /// A C++ template name refers to a template within the C++ type
167 /// system. In most cases, a template name is simply a reference to a
168 /// class template, e.g.
169 ///
170 /// \code
171 /// template<typename T> class X { };
172 ///
173 /// X<int> xi;
174 /// \endcode
175 ///
176 /// Here, the 'X' in \c X<int> is a template name that refers to the
177 /// declaration of the class template X, above. Template names can
178 /// also refer to function templates, C++0x template aliases, etc.
179 ///
180 /// Some template names are dependent. For example, consider:
181 ///
182 /// \code
183 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
184 /// typedef typename MetaFun::template apply<T1, T2>::type type;
185 /// };
186 /// \endcode
187 ///
188 /// Here, "apply" is treated as a template name within the typename
189 /// specifier in the typedef. "apply" is a nested template, and can
190 /// only be understood in the context of
192  using StorageType =
193  llvm::PointerUnion<TemplateDecl *, UncommonTemplateNameStorage *,
195 
196  StorageType Storage;
197 
198  explicit TemplateName(void *Ptr);
199 
200 public:
201  // Kind of name that is actually stored.
202  enum NameKind {
203  /// A single template declaration.
205 
206  /// A set of overloaded template declarations.
208 
209  /// An unqualified-id that has been assumed to name a function template
210  /// that will be found by ADL.
212 
213  /// A qualified template name, where the qualification is kept
214  /// to describe the source code as written.
216 
217  /// A dependent template name that has not been resolved to a
218  /// template (or set of templates).
220 
221  /// A template template parameter that has been substituted
222  /// for some other template name.
224 
225  /// A template template parameter pack that has been substituted for
226  /// a template template argument pack, but has not yet been expanded into
227  /// individual arguments.
229  };
230 
231  TemplateName() = default;
232  explicit TemplateName(TemplateDecl *Template);
233  explicit TemplateName(OverloadedTemplateStorage *Storage);
234  explicit TemplateName(AssumedTemplateStorage *Storage);
237  explicit TemplateName(QualifiedTemplateName *Qual);
238  explicit TemplateName(DependentTemplateName *Dep);
239 
240  /// Determine whether this template name is NULL.
241  bool isNull() const;
242 
243  // Get the kind of name that is actually stored.
244  NameKind getKind() const;
245 
246  /// Retrieve the underlying template declaration that
247  /// this template name refers to, if known.
248  ///
249  /// \returns The template declaration that this template name refers
250  /// to, if any. If the template name does not refer to a specific
251  /// declaration because it is a dependent name, or if it refers to a
252  /// set of function templates, returns NULL.
253  TemplateDecl *getAsTemplateDecl() const;
254 
255  /// Retrieve the underlying, overloaded function template
256  /// declarations that this template name refers to, if known.
257  ///
258  /// \returns The set of overloaded function templates that this template
259  /// name refers to, if known. If the template name does not refer to a
260  /// specific set of function templates because it is a dependent name or
261  /// refers to a single template, returns NULL.
262  OverloadedTemplateStorage *getAsOverloadedTemplate() const;
263 
264  /// Retrieve information on a name that has been assumed to be a
265  /// template-name in order to permit a call via ADL.
267 
268  /// Retrieve the substituted template template parameter, if
269  /// known.
270  ///
271  /// \returns The storage for the substituted template template parameter,
272  /// if known. Otherwise, returns NULL.
274 
275  /// Retrieve the substituted template template parameter pack, if
276  /// known.
277  ///
278  /// \returns The storage for the substituted template template parameter pack,
279  /// if known. Otherwise, returns NULL.
282 
283  /// Retrieve the underlying qualified template name
284  /// structure, if any.
285  QualifiedTemplateName *getAsQualifiedTemplateName() const;
286 
287  /// Retrieve the underlying dependent template name
288  /// structure, if any.
289  DependentTemplateName *getAsDependentTemplateName() const;
290 
291  TemplateName getUnderlying() const;
292 
293  /// Get the template name to substitute when this template name is used as a
294  /// template template argument. This refers to the most recent declaration of
295  /// the template, including any default template arguments.
296  TemplateName getNameToSubstitute() const;
297 
298  /// Determines whether this is a dependent template name.
299  bool isDependent() const;
300 
301  /// Determines whether this is a template name that somehow
302  /// depends on a template parameter.
303  bool isInstantiationDependent() const;
304 
305  /// Determines whether this template name contains an
306  /// unexpanded parameter pack (for C++0x variadic templates).
307  bool containsUnexpandedParameterPack() const;
308 
309  /// Print the template name.
310  ///
311  /// \param OS the output stream to which the template name will be
312  /// printed.
313  ///
314  /// \param SuppressNNS if true, don't print the
315  /// nested-name-specifier that precedes the template name (if it has
316  /// one).
317  void print(raw_ostream &OS, const PrintingPolicy &Policy,
318  bool SuppressNNS = false) const;
319 
320  /// Debugging aid that dumps the template name.
321  void dump(raw_ostream &OS) const;
322 
323  /// Debugging aid that dumps the template name to standard
324  /// error.
325  void dump() const;
326 
327  void Profile(llvm::FoldingSetNodeID &ID) {
328  ID.AddPointer(Storage.getOpaqueValue());
329  }
330 
331  /// Retrieve the template name as a void pointer.
332  void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
333 
334  /// Build a template name from a void pointer.
335  static TemplateName getFromVoidPointer(void *Ptr) {
336  return TemplateName(Ptr);
337  }
338 };
339 
340 /// Insertion operator for diagnostics. This allows sending TemplateName's
341 /// into a diagnostic with <<.
343  TemplateName N);
345  TemplateName N);
346 
347 /// A structure for storing the information associated with a
348 /// substituted template template parameter.
350  : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
351  friend class ASTContext;
352 
354  TemplateName Replacement;
355 
357  TemplateName replacement)
359  Parameter(parameter), Replacement(replacement) {}
360 
361 public:
363  TemplateName getReplacement() const { return Replacement; }
364 
365  void Profile(llvm::FoldingSetNodeID &ID);
366 
367  static void Profile(llvm::FoldingSetNodeID &ID,
368  TemplateTemplateParmDecl *parameter,
369  TemplateName replacement);
370 };
371 
375  return subst->getReplacement().getUnderlying();
376  return *this;
377 }
378 
379 /// Represents a template name that was expressed as a
380 /// qualified name.
381 ///
382 /// This kind of template name refers to a template name that was
383 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
384 /// the nested name specifier is "std::" and the template name is the
385 /// declaration for "vector". The QualifiedTemplateName class is only
386 /// used to provide "sugar" for template names that were expressed
387 /// with a qualified name, and has no semantic meaning. In this
388 /// manner, it is to TemplateName what ElaboratedType is to Type,
389 /// providing extra syntactic sugar for downstream clients.
390 class QualifiedTemplateName : public llvm::FoldingSetNode {
391  friend class ASTContext;
392 
393  /// The nested name specifier that qualifies the template name.
394  ///
395  /// The bit is used to indicate whether the "template" keyword was
396  /// present before the template name itself. Note that the
397  /// "template" keyword is always redundant in this case (otherwise,
398  /// the template name would be a dependent name and we would express
399  /// this name with DependentTemplateName).
400  llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
401 
402  /// The template declaration or set of overloaded function templates
403  /// that this qualified name refers to.
404  TemplateDecl *Template;
405 
406  QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
407  TemplateDecl *Template)
408  : Qualifier(NNS, TemplateKeyword? 1 : 0), Template(Template) {}
409 
410 public:
411  /// Return the nested name specifier that qualifies this name.
412  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
413 
414  /// Whether the template name was prefixed by the "template"
415  /// keyword.
416  bool hasTemplateKeyword() const { return Qualifier.getInt(); }
417 
418  /// The template declaration that this qualified name refers
419  /// to.
420  TemplateDecl *getDecl() const { return Template; }
421 
422  /// The template declaration to which this qualified name
423  /// refers.
424  TemplateDecl *getTemplateDecl() const { return Template; }
425 
426  void Profile(llvm::FoldingSetNodeID &ID) {
427  Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
428  }
429 
430  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
431  bool TemplateKeyword, TemplateDecl *Template) {
432  ID.AddPointer(NNS);
433  ID.AddBoolean(TemplateKeyword);
434  ID.AddPointer(Template);
435  }
436 };
437 
438 /// Represents a dependent template name that cannot be
439 /// resolved prior to template instantiation.
440 ///
441 /// This kind of template name refers to a dependent template name,
442 /// including its nested name specifier (if any). For example,
443 /// DependentTemplateName can refer to "MetaFun::template apply",
444 /// where "MetaFun::" is the nested name specifier and "apply" is the
445 /// template name referenced. The "template" keyword is implied.
446 class DependentTemplateName : public llvm::FoldingSetNode {
447  friend class ASTContext;
448 
449  /// The nested name specifier that qualifies the template
450  /// name.
451  ///
452  /// The bit stored in this qualifier describes whether the \c Name field
453  /// is interpreted as an IdentifierInfo pointer (when clear) or as an
454  /// overloaded operator kind (when set).
455  llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
456 
457  /// The dependent template name.
458  union {
459  /// The identifier template name.
460  ///
461  /// Only valid when the bit on \c Qualifier is clear.
463 
464  /// The overloaded operator name.
465  ///
466  /// Only valid when the bit on \c Qualifier is set.
468  };
469 
470  /// The canonical template name to which this dependent
471  /// template name refers.
472  ///
473  /// The canonical template name for a dependent template name is
474  /// another dependent template name whose nested name specifier is
475  /// canonical.
476  TemplateName CanonicalTemplateName;
477 
479  const IdentifierInfo *Identifier)
480  : Qualifier(Qualifier, false), Identifier(Identifier),
481  CanonicalTemplateName(this) {}
482 
484  const IdentifierInfo *Identifier,
485  TemplateName Canon)
486  : Qualifier(Qualifier, false), Identifier(Identifier),
487  CanonicalTemplateName(Canon) {}
488 
490  OverloadedOperatorKind Operator)
491  : Qualifier(Qualifier, true), Operator(Operator),
492  CanonicalTemplateName(this) {}
493 
495  OverloadedOperatorKind Operator,
496  TemplateName Canon)
497  : Qualifier(Qualifier, true), Operator(Operator),
498  CanonicalTemplateName(Canon) {}
499 
500 public:
501  /// Return the nested name specifier that qualifies this name.
502  NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
503 
504  /// Determine whether this template name refers to an identifier.
505  bool isIdentifier() const { return !Qualifier.getInt(); }
506 
507  /// Returns the identifier to which this template name refers.
508  const IdentifierInfo *getIdentifier() const {
509  assert(isIdentifier() && "Template name isn't an identifier?");
510  return Identifier;
511  }
512 
513  /// Determine whether this template name refers to an overloaded
514  /// operator.
515  bool isOverloadedOperator() const { return Qualifier.getInt(); }
516 
517  /// Return the overloaded operator to which this template name refers.
519  assert(isOverloadedOperator() &&
520  "Template name isn't an overloaded operator?");
521  return Operator;
522  }
523 
524  void Profile(llvm::FoldingSetNodeID &ID) {
525  if (isIdentifier())
526  Profile(ID, getQualifier(), getIdentifier());
527  else
528  Profile(ID, getQualifier(), getOperator());
529  }
530 
531  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
532  const IdentifierInfo *Identifier) {
533  ID.AddPointer(NNS);
534  ID.AddBoolean(false);
535  ID.AddPointer(Identifier);
536  }
537 
538  static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
539  OverloadedOperatorKind Operator) {
540  ID.AddPointer(NNS);
541  ID.AddBoolean(true);
542  ID.AddInteger(Operator);
543  }
544 };
545 
546 } // namespace clang.
547 
548 namespace llvm {
549 
550 /// The clang::TemplateName class is effectively a pointer.
551 template<>
552 struct PointerLikeTypeTraits<clang::TemplateName> {
553  static inline void *getAsVoidPointer(clang::TemplateName TN) {
554  return TN.getAsVoidPointer();
555  }
556 
557  static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
559  }
560 
561  // No bits are available!
562  enum { NumLowBitsAvailable = 0 };
563 };
564 
565 } // namespace llvm.
566 
567 #endif // LLVM_CLANG_AST_TEMPLATENAME_H
TemplateTemplateParmDecl * getParameterPack() const
Retrieve the template template parameter pack being substituted.
Definition: TemplateName.h:148
static llvm::GlobalValue::DLLStorageClassTypes getStorage(CodeGenModule &CGM, StringRef Name)
Definition: CGObjCMac.cpp:6489
StringRef Identifier
Definition: Format.cpp:1833
void * getAsVoidPointer() const
Retrieve the template name as a void pointer.
Definition: TemplateName.h:332
bool hasTemplateKeyword() const
Whether the template name was prefixed by the "template" keyword.
Definition: TemplateName.h:416
OverloadedOperatorKind getOperator() const
Return the overloaded operator to which this template name refers.
Definition: TemplateName.h:518
NestedNameSpecifier * getQualifier() const
Return the nested name specifier that qualifies this name.
Definition: TemplateName.h:502
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
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:346
A template template parameter that has been substituted for some other template name.
Definition: TemplateName.h:223
const IdentifierInfo * getIdentifier() const
Returns the identifier to which this template name refers.
Definition: TemplateName.h:508
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:47
void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx, QualType Ty)
Definition: InterpFrame.cpp:62
One of these records is kept for each identifier that is lexed.
TemplateName getUnderlying() const
Definition: TemplateName.h:372
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
Represents a dependent template name that cannot be resolved prior to template instantiation.
Definition: TemplateName.h:446
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:215
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:211
SubstTemplateTemplateParmPackStorage * getAsSubstTemplateTemplateParmPack()
Definition: TemplateName.h:95
TemplateTemplateParmDecl * getParameter() const
Definition: TemplateName.h:362
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:524
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1053
A dependent template name that has not been resolved to a template (or set of templates).
Definition: TemplateName.h:219
TemplateDecl * getTemplateDecl() const
The template declaration to which this qualified name refers.
Definition: TemplateName.h:424
OverloadedOperatorKind Operator
The overloaded operator name.
Definition: TemplateName.h:467
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, OverloadedOperatorKind Operator)
Definition: TemplateName.h:538
A structure for storing the information associated with a substituted template template parameter...
Definition: TemplateName.h:349
Represents a C++ template name within the type system.
Definition: TemplateName.h:191
bool isIdentifier() const
Determine whether this template name refers to an identifier.
Definition: TemplateName.h:505
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, const IdentifierInfo *Identifier)
Definition: TemplateName.h:531
static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, bool TemplateKeyword, TemplateDecl *Template)
Definition: TemplateName.h:430
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:412
OverloadedTemplateStorage * getAsOverloadedStorage()
Definition: TemplateName.h:77
static TemplateName getFromVoidPointer(void *Ptr)
Build a template name from a void pointer.
Definition: TemplateName.h:335
#define false
Definition: stdbool.h:17
A structure for storing an already-substituted template template parameter pack.
Definition: TemplateName.h:134
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:426
static void * getAsVoidPointer(clang::TemplateName TN)
Definition: TemplateName.h:553
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:515
UncommonTemplateNameStorage(Kind kind, unsigned size)
Definition: TemplateName.h:69
void Profile(llvm::FoldingSetNodeID &ID)
Definition: TemplateName.h:327
static clang::TemplateName getFromVoidPointer(void *Ptr)
Definition: TemplateName.h:557
llvm::ArrayRef< NamedDecl * > decls() const
Definition: TemplateName.h:123
Represents a template argument.
Definition: TemplateBase.h:50
Represents a template name that was expressed as a qualified name.
Definition: TemplateName.h:390
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:402
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:141
const IdentifierInfo * Identifier
The identifier template name.
Definition: TemplateName.h:462
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:947
TemplateDecl * getDecl() const
The template declaration that this qualified name refers to.
Definition: TemplateName.h:420
A set of overloaded template declarations.
Definition: TemplateName.h:207
This represents a decl that may have a name.
Definition: Decl.h:223
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:204