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