xref: /freebsd/contrib/llvm-project/clang/include/clang/AST/TemplateName.h (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
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 
16 #include "clang/AST/DependenceFlags.h"
17 #include "clang/AST/NestedNameSpecifier.h"
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/ADT/PointerIntPair.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/Support/PointerLikeTypeTraits.h"
23 #include <cassert>
24 
25 namespace clang {
26 
27 class ASTContext;
28 class DependentTemplateName;
29 class IdentifierInfo;
30 class NamedDecl;
31 class NestedNameSpecifier;
32 enum OverloadedOperatorKind : int;
33 class OverloadedTemplateStorage;
34 class AssumedTemplateStorage;
35 struct PrintingPolicy;
36 class QualifiedTemplateName;
37 class SubstTemplateTemplateParmPackStorage;
38 class SubstTemplateTemplateParmStorage;
39 class TemplateArgument;
40 class TemplateDecl;
41 class TemplateTemplateParmDecl;
42 
43 /// Implementation class used to describe either a set of overloaded
44 /// template names or an already-substituted template template parameter pack.
45 class UncommonTemplateNameStorage {
46 protected:
47   enum Kind {
48     Overloaded,
49     Assumed, // defined in DeclarationName.h
50     SubstTemplateTemplateParm,
51     SubstTemplateTemplateParmPack
52   };
53 
54   struct BitsTag {
55     /// A Kind.
56     unsigned Kind : 2;
57 
58     /// The number of stored templates or template arguments,
59     /// depending on which subclass we have.
60     unsigned Size : 30;
61   };
62 
63   union {
64     struct BitsTag Bits;
65     void *PointerAlignment;
66   };
67 
68   UncommonTemplateNameStorage(Kind kind, unsigned size) {
69     Bits.Kind = kind;
70     Bits.Size = size;
71   }
72 
73 public:
74   unsigned size() const { return Bits.Size; }
75 
76   OverloadedTemplateStorage *getAsOverloadedStorage()  {
77     return Bits.Kind == Overloaded
78              ? reinterpret_cast<OverloadedTemplateStorage *>(this)
79              : nullptr;
80   }
81 
82   AssumedTemplateStorage *getAsAssumedTemplateName()  {
83     return Bits.Kind == Assumed
84              ? reinterpret_cast<AssumedTemplateStorage *>(this)
85              : nullptr;
86   }
87 
88   SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
89     return Bits.Kind == SubstTemplateTemplateParm
90              ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
91              : nullptr;
92   }
93 
94   SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
95     return Bits.Kind == SubstTemplateTemplateParmPack
96              ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
97              : nullptr;
98   }
99 };
100 
101 /// A structure for storing the information associated with an
102 /// overloaded template name.
103 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
104   friend class ASTContext;
105 
106   OverloadedTemplateStorage(unsigned size)
107       : UncommonTemplateNameStorage(Overloaded, size) {}
108 
109   NamedDecl **getStorage() {
110     return reinterpret_cast<NamedDecl **>(this + 1);
111   }
112   NamedDecl * const *getStorage() const {
113     return reinterpret_cast<NamedDecl *const *>(this + 1);
114   }
115 
116 public:
117   using iterator = NamedDecl *const *;
118 
119   iterator begin() const { return getStorage(); }
120   iterator end() const { return getStorage() + size(); }
121 
122   llvm::ArrayRef<NamedDecl*> decls() const {
123     return llvm::makeArrayRef(begin(), end());
124   }
125 };
126 
127 /// A structure for storing an already-substituted template template
128 /// parameter pack.
129 ///
130 /// This kind of template names occurs when the parameter pack has been
131 /// provided with a template template argument pack in a context where its
132 /// enclosing pack expansion could not be fully expanded.
133 class SubstTemplateTemplateParmPackStorage
134   : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
135 {
136   TemplateTemplateParmDecl *Parameter;
137   const TemplateArgument *Arguments;
138 
139 public:
140   SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
141                                        unsigned Size,
142                                        const TemplateArgument *Arguments)
143       : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
144         Parameter(Parameter), Arguments(Arguments) {}
145 
146   /// Retrieve the template template parameter pack being substituted.
147   TemplateTemplateParmDecl *getParameterPack() const {
148     return Parameter;
149   }
150 
151   /// Retrieve the template template argument pack with which this
152   /// parameter was substituted.
153   TemplateArgument getArgumentPack() const;
154 
155   void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
156 
157   static void Profile(llvm::FoldingSetNodeID &ID,
158                       ASTContext &Context,
159                       TemplateTemplateParmDecl *Parameter,
160                       const TemplateArgument &ArgPack);
161 };
162 
163 /// Represents a C++ template name within the type system.
164 ///
165 /// A C++ template name refers to a template within the C++ type
166 /// system. In most cases, a template name is simply a reference to a
167 /// class template, e.g.
168 ///
169 /// \code
170 /// template<typename T> class X { };
171 ///
172 /// X<int> xi;
173 /// \endcode
174 ///
175 /// Here, the 'X' in \c X<int> is a template name that refers to the
176 /// declaration of the class template X, above. Template names can
177 /// also refer to function templates, C++0x template aliases, etc.
178 ///
179 /// Some template names are dependent. For example, consider:
180 ///
181 /// \code
182 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
183 ///   typedef typename MetaFun::template apply<T1, T2>::type type;
184 /// };
185 /// \endcode
186 ///
187 /// Here, "apply" is treated as a template name within the typename
188 /// specifier in the typedef. "apply" is a nested template, and can
189 /// only be understood in the context of
190 class TemplateName {
191   using StorageType =
192       llvm::PointerUnion<TemplateDecl *, UncommonTemplateNameStorage *,
193                          QualifiedTemplateName *, DependentTemplateName *>;
194 
195   StorageType Storage;
196 
197   explicit TemplateName(void *Ptr);
198 
199 public:
200   // Kind of name that is actually stored.
201   enum NameKind {
202     /// A single template declaration.
203     Template,
204 
205     /// A set of overloaded template declarations.
206     OverloadedTemplate,
207 
208     /// An unqualified-id that has been assumed to name a function template
209     /// that will be found by ADL.
210     AssumedTemplate,
211 
212     /// A qualified template name, where the qualification is kept
213     /// to describe the source code as written.
214     QualifiedTemplate,
215 
216     /// A dependent template name that has not been resolved to a
217     /// template (or set of templates).
218     DependentTemplate,
219 
220     /// A template template parameter that has been substituted
221     /// for some other template name.
222     SubstTemplateTemplateParm,
223 
224     /// A template template parameter pack that has been substituted for
225     /// a template template argument pack, but has not yet been expanded into
226     /// individual arguments.
227     SubstTemplateTemplateParmPack
228   };
229 
230   TemplateName() = default;
231   explicit TemplateName(TemplateDecl *Template);
232   explicit TemplateName(OverloadedTemplateStorage *Storage);
233   explicit TemplateName(AssumedTemplateStorage *Storage);
234   explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
235   explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage);
236   explicit TemplateName(QualifiedTemplateName *Qual);
237   explicit TemplateName(DependentTemplateName *Dep);
238 
239   /// Determine whether this template name is NULL.
240   bool isNull() const;
241 
242   // Get the kind of name that is actually stored.
243   NameKind getKind() const;
244 
245   /// Retrieve the underlying template declaration that
246   /// this template name refers to, if known.
247   ///
248   /// \returns The template declaration that this template name refers
249   /// to, if any. If the template name does not refer to a specific
250   /// declaration because it is a dependent name, or if it refers to a
251   /// set of function templates, returns NULL.
252   TemplateDecl *getAsTemplateDecl() const;
253 
254   /// Retrieve the underlying, overloaded function template
255   /// declarations that this template name refers to, if known.
256   ///
257   /// \returns The set of overloaded function templates that this template
258   /// name refers to, if known. If the template name does not refer to a
259   /// specific set of function templates because it is a dependent name or
260   /// refers to a single template, returns NULL.
261   OverloadedTemplateStorage *getAsOverloadedTemplate() const;
262 
263   /// Retrieve information on a name that has been assumed to be a
264   /// template-name in order to permit a call via ADL.
265   AssumedTemplateStorage *getAsAssumedTemplateName() const;
266 
267   /// Retrieve the substituted template template parameter, if
268   /// known.
269   ///
270   /// \returns The storage for the substituted template template parameter,
271   /// if known. Otherwise, returns NULL.
272   SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const;
273 
274   /// Retrieve the substituted template template parameter pack, if
275   /// known.
276   ///
277   /// \returns The storage for the substituted template template parameter pack,
278   /// if known. Otherwise, returns NULL.
279   SubstTemplateTemplateParmPackStorage *
280   getAsSubstTemplateTemplateParmPack() const;
281 
282   /// Retrieve the underlying qualified template name
283   /// structure, if any.
284   QualifiedTemplateName *getAsQualifiedTemplateName() const;
285 
286   /// Retrieve the underlying dependent template name
287   /// structure, if any.
288   DependentTemplateName *getAsDependentTemplateName() const;
289 
290   TemplateName getUnderlying() const;
291 
292   /// Get the template name to substitute when this template name is used as a
293   /// template template argument. This refers to the most recent declaration of
294   /// the template, including any default template arguments.
295   TemplateName getNameToSubstitute() const;
296 
297   TemplateNameDependence getDependence() const;
298 
299   /// Determines whether this is a dependent template name.
300   bool isDependent() const;
301 
302   /// Determines whether this is a template name that somehow
303   /// depends on a template parameter.
304   bool isInstantiationDependent() const;
305 
306   /// Determines whether this template name contains an
307   /// unexpanded parameter pack (for C++0x variadic templates).
308   bool containsUnexpandedParameterPack() const;
309 
310   enum class Qualified { None, AsWritten, Fully };
311   /// Print the template name.
312   ///
313   /// \param OS the output stream to which the template name will be
314   /// printed.
315   ///
316   /// \param Qual print the (Qualified::None) simple name,
317   /// (Qualified::AsWritten) any written (possibly partial) qualifier, or
318   /// (Qualified::Fully) the fully qualified name.
319   void print(raw_ostream &OS, const PrintingPolicy &Policy,
320              Qualified Qual = Qualified::AsWritten) const;
321 
322   /// Debugging aid that dumps the template name.
323   void dump(raw_ostream &OS) const;
324 
325   /// Debugging aid that dumps the template name to standard
326   /// error.
327   void dump() const;
328 
329   void Profile(llvm::FoldingSetNodeID &ID) {
330     ID.AddPointer(Storage.getOpaqueValue());
331   }
332 
333   /// Retrieve the template name as a void pointer.
334   void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
335 
336   /// Build a template name from a void pointer.
337   static TemplateName getFromVoidPointer(void *Ptr) {
338     return TemplateName(Ptr);
339   }
340 };
341 
342 /// Insertion operator for diagnostics.  This allows sending TemplateName's
343 /// into a diagnostic with <<.
344 const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
345                                       TemplateName N);
346 
347 /// A structure for storing the information associated with a
348 /// substituted template template parameter.
349 class SubstTemplateTemplateParmStorage
350   : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
351   friend class ASTContext;
352 
353   TemplateTemplateParmDecl *Parameter;
354   TemplateName Replacement;
355 
356   SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
357                                    TemplateName replacement)
358       : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
359         Parameter(parameter), Replacement(replacement) {}
360 
361 public:
362   TemplateTemplateParmDecl *getParameter() const { return Parameter; }
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 
372 inline TemplateName TemplateName::getUnderlying() const {
373   if (SubstTemplateTemplateParmStorage *subst
374         = getAsSubstTemplateTemplateParm())
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.
462     const IdentifierInfo *Identifier;
463 
464     /// The overloaded operator name.
465     ///
466     /// Only valid when the bit on \c Qualifier is set.
467     OverloadedOperatorKind Operator;
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 
478   DependentTemplateName(NestedNameSpecifier *Qualifier,
479                         const IdentifierInfo *Identifier)
480       : Qualifier(Qualifier, false), Identifier(Identifier),
481         CanonicalTemplateName(this) {}
482 
483   DependentTemplateName(NestedNameSpecifier *Qualifier,
484                         const IdentifierInfo *Identifier,
485                         TemplateName Canon)
486       : Qualifier(Qualifier, false), Identifier(Identifier),
487         CanonicalTemplateName(Canon) {}
488 
489   DependentTemplateName(NestedNameSpecifier *Qualifier,
490                         OverloadedOperatorKind Operator)
491       : Qualifier(Qualifier, true), Operator(Operator),
492         CanonicalTemplateName(this) {}
493 
494   DependentTemplateName(NestedNameSpecifier *Qualifier,
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.
518   OverloadedOperatorKind getOperator() const {
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) {
558     return clang::TemplateName::getFromVoidPointer(Ptr);
559   }
560 
561   // No bits are available!
562   static constexpr int NumLowBitsAvailable = 0;
563 };
564 
565 } // namespace llvm.
566 
567 #endif // LLVM_CLANG_AST_TEMPLATENAME_H
568