1 //===- TemplateName.h - C++ Template Name Representation --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the TemplateName interface and subclasses.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_TEMPLATENAME_H
14 #define LLVM_CLANG_AST_TEMPLATENAME_H
16 #include "clang/AST/NestedNameSpecifier.h"
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"
27 class DependentTemplateName;
28 class DiagnosticBuilder;
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;
42 class TemplateTemplateParmDecl;
44 /// Implementation class used to describe either a set of overloaded
45 /// template names or an already-substituted template template parameter pack.
46 class UncommonTemplateNameStorage {
50 Assumed, // defined in DeclarationName.h
51 SubstTemplateTemplateParm,
52 SubstTemplateTemplateParmPack
59 /// The number of stored templates or template arguments,
60 /// depending on which subclass we have.
66 void *PointerAlignment;
69 UncommonTemplateNameStorage(Kind kind, unsigned size) {
75 unsigned size() const { return Bits.Size; }
77 OverloadedTemplateStorage *getAsOverloadedStorage() {
78 return Bits.Kind == Overloaded
79 ? reinterpret_cast<OverloadedTemplateStorage *>(this)
83 AssumedTemplateStorage *getAsAssumedTemplateName() {
84 return Bits.Kind == Assumed
85 ? reinterpret_cast<AssumedTemplateStorage *>(this)
89 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
90 return Bits.Kind == SubstTemplateTemplateParm
91 ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
95 SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
96 return Bits.Kind == SubstTemplateTemplateParmPack
97 ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
102 /// A structure for storing the information associated with an
103 /// overloaded template name.
104 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
105 friend class ASTContext;
107 OverloadedTemplateStorage(unsigned size)
108 : UncommonTemplateNameStorage(Overloaded, size) {}
110 NamedDecl **getStorage() {
111 return reinterpret_cast<NamedDecl **>(this + 1);
113 NamedDecl * const *getStorage() const {
114 return reinterpret_cast<NamedDecl *const *>(this + 1);
118 using iterator = NamedDecl *const *;
120 iterator begin() const { return getStorage(); }
121 iterator end() const { return getStorage() + size(); }
124 /// A structure for storing an already-substituted template template
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.
130 class SubstTemplateTemplateParmPackStorage
131 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
133 TemplateTemplateParmDecl *Parameter;
134 const TemplateArgument *Arguments;
137 SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
139 const TemplateArgument *Arguments)
140 : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
141 Parameter(Parameter), Arguments(Arguments) {}
143 /// Retrieve the template template parameter pack being substituted.
144 TemplateTemplateParmDecl *getParameterPack() const {
148 /// Retrieve the template template argument pack with which this
149 /// parameter was substituted.
150 TemplateArgument getArgumentPack() const;
152 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
154 static void Profile(llvm::FoldingSetNodeID &ID,
156 TemplateTemplateParmDecl *Parameter,
157 const TemplateArgument &ArgPack);
160 /// Represents a C++ template name within the type system.
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.
167 /// template<typename T> class X { };
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.
176 /// Some template names are dependent. For example, consider:
179 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
180 /// typedef typename MetaFun::template apply<T1, T2>::type type;
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
189 llvm::PointerUnion4<TemplateDecl *, UncommonTemplateNameStorage *,
190 QualifiedTemplateName *, DependentTemplateName *>;
194 explicit TemplateName(void *Ptr);
197 // Kind of name that is actually stored.
199 /// A single template declaration.
202 /// A set of overloaded template declarations.
205 /// An unqualified-id that has been assumed to name a function template
206 /// that will be found by ADL.
209 /// A qualified template name, where the qualification is kept
210 /// to describe the source code as written.
213 /// A dependent template name that has not been resolved to a
214 /// template (or set of templates).
217 /// A template template parameter that has been substituted
218 /// for some other template name.
219 SubstTemplateTemplateParm,
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.
224 SubstTemplateTemplateParmPack
227 TemplateName() = default;
228 explicit TemplateName(TemplateDecl *Template);
229 explicit TemplateName(OverloadedTemplateStorage *Storage);
230 explicit TemplateName(AssumedTemplateStorage *Storage);
231 explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
232 explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage);
233 explicit TemplateName(QualifiedTemplateName *Qual);
234 explicit TemplateName(DependentTemplateName *Dep);
236 /// Determine whether this template name is NULL.
239 // Get the kind of name that is actually stored.
240 NameKind getKind() const;
242 /// Retrieve the underlying template declaration that
243 /// this template name refers to, if known.
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;
251 /// Retrieve the underlying, overloaded function template
252 /// declarations that this template name refers to, if known.
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;
260 /// Retrieve information on a name that has been assumed to be a
261 /// template-name in order to permit a call via ADL.
262 AssumedTemplateStorage *getAsAssumedTemplateName() const;
264 /// Retrieve the substituted template template parameter, if
267 /// \returns The storage for the substituted template template parameter,
268 /// if known. Otherwise, returns NULL.
269 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const;
271 /// Retrieve the substituted template template parameter pack, if
274 /// \returns The storage for the substituted template template parameter pack,
275 /// if known. Otherwise, returns NULL.
276 SubstTemplateTemplateParmPackStorage *
277 getAsSubstTemplateTemplateParmPack() const;
279 /// Retrieve the underlying qualified template name
280 /// structure, if any.
281 QualifiedTemplateName *getAsQualifiedTemplateName() const;
283 /// Retrieve the underlying dependent template name
284 /// structure, if any.
285 DependentTemplateName *getAsDependentTemplateName() const;
287 TemplateName getUnderlying() const;
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;
294 /// Determines whether this is a dependent template name.
295 bool isDependent() const;
297 /// Determines whether this is a template name that somehow
298 /// depends on a template parameter.
299 bool isInstantiationDependent() const;
301 /// Determines whether this template name contains an
302 /// unexpanded parameter pack (for C++0x variadic templates).
303 bool containsUnexpandedParameterPack() const;
305 /// Print the template name.
307 /// \param OS the output stream to which the template name will be
310 /// \param SuppressNNS if true, don't print the
311 /// nested-name-specifier that precedes the template name (if it has
313 void print(raw_ostream &OS, const PrintingPolicy &Policy,
314 bool SuppressNNS = false) const;
316 /// Debugging aid that dumps the template name.
317 void dump(raw_ostream &OS) const;
319 /// Debugging aid that dumps the template name to standard
323 void Profile(llvm::FoldingSetNodeID &ID) {
324 ID.AddPointer(Storage.getOpaqueValue());
327 /// Retrieve the template name as a void pointer.
328 void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
330 /// Build a template name from a void pointer.
331 static TemplateName getFromVoidPointer(void *Ptr) {
332 return TemplateName(Ptr);
336 /// Insertion operator for diagnostics. This allows sending TemplateName's
337 /// into a diagnostic with <<.
338 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
340 const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
343 /// A structure for storing the information associated with a
344 /// substituted template template parameter.
345 class SubstTemplateTemplateParmStorage
346 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
347 friend class ASTContext;
349 TemplateTemplateParmDecl *Parameter;
350 TemplateName Replacement;
352 SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
353 TemplateName replacement)
354 : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
355 Parameter(parameter), Replacement(replacement) {}
358 TemplateTemplateParmDecl *getParameter() const { return Parameter; }
359 TemplateName getReplacement() const { return Replacement; }
361 void Profile(llvm::FoldingSetNodeID &ID);
363 static void Profile(llvm::FoldingSetNodeID &ID,
364 TemplateTemplateParmDecl *parameter,
365 TemplateName replacement);
368 inline TemplateName TemplateName::getUnderlying() const {
369 if (SubstTemplateTemplateParmStorage *subst
370 = getAsSubstTemplateTemplateParm())
371 return subst->getReplacement().getUnderlying();
375 /// Represents a template name that was expressed as a
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;
389 /// The nested name specifier that qualifies the template name.
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;
398 /// The template declaration or set of overloaded function templates
399 /// that this qualified name refers to.
400 TemplateDecl *Template;
402 QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
403 TemplateDecl *Template)
404 : Qualifier(NNS, TemplateKeyword? 1 : 0), Template(Template) {}
407 /// Return the nested name specifier that qualifies this name.
408 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
410 /// Whether the template name was prefixed by the "template"
412 bool hasTemplateKeyword() const { return Qualifier.getInt(); }
414 /// The template declaration that this qualified name refers
416 TemplateDecl *getDecl() const { return Template; }
418 /// The template declaration to which this qualified name
420 TemplateDecl *getTemplateDecl() const { return Template; }
422 void Profile(llvm::FoldingSetNodeID &ID) {
423 Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
426 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
427 bool TemplateKeyword, TemplateDecl *Template) {
429 ID.AddBoolean(TemplateKeyword);
430 ID.AddPointer(Template);
434 /// Represents a dependent template name that cannot be
435 /// resolved prior to template instantiation.
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;
445 /// The nested name specifier that qualifies the template
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;
453 /// The dependent template name.
455 /// The identifier template name.
457 /// Only valid when the bit on \c Qualifier is clear.
458 const IdentifierInfo *Identifier;
460 /// The overloaded operator name.
462 /// Only valid when the bit on \c Qualifier is set.
463 OverloadedOperatorKind Operator;
466 /// The canonical template name to which this dependent
467 /// template name refers.
469 /// The canonical template name for a dependent template name is
470 /// another dependent template name whose nested name specifier is
472 TemplateName CanonicalTemplateName;
474 DependentTemplateName(NestedNameSpecifier *Qualifier,
475 const IdentifierInfo *Identifier)
476 : Qualifier(Qualifier, false), Identifier(Identifier),
477 CanonicalTemplateName(this) {}
479 DependentTemplateName(NestedNameSpecifier *Qualifier,
480 const IdentifierInfo *Identifier,
482 : Qualifier(Qualifier, false), Identifier(Identifier),
483 CanonicalTemplateName(Canon) {}
485 DependentTemplateName(NestedNameSpecifier *Qualifier,
486 OverloadedOperatorKind Operator)
487 : Qualifier(Qualifier, true), Operator(Operator),
488 CanonicalTemplateName(this) {}
490 DependentTemplateName(NestedNameSpecifier *Qualifier,
491 OverloadedOperatorKind Operator,
493 : Qualifier(Qualifier, true), Operator(Operator),
494 CanonicalTemplateName(Canon) {}
497 /// Return the nested name specifier that qualifies this name.
498 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
500 /// Determine whether this template name refers to an identifier.
501 bool isIdentifier() const { return !Qualifier.getInt(); }
503 /// Returns the identifier to which this template name refers.
504 const IdentifierInfo *getIdentifier() const {
505 assert(isIdentifier() && "Template name isn't an identifier?");
509 /// Determine whether this template name refers to an overloaded
511 bool isOverloadedOperator() const { return Qualifier.getInt(); }
513 /// Return the overloaded operator to which this template name refers.
514 OverloadedOperatorKind getOperator() const {
515 assert(isOverloadedOperator() &&
516 "Template name isn't an overloaded operator?");
520 void Profile(llvm::FoldingSetNodeID &ID) {
522 Profile(ID, getQualifier(), getIdentifier());
524 Profile(ID, getQualifier(), getOperator());
527 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
528 const IdentifierInfo *Identifier) {
530 ID.AddBoolean(false);
531 ID.AddPointer(Identifier);
534 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
535 OverloadedOperatorKind Operator) {
538 ID.AddInteger(Operator);
542 } // namespace clang.
546 /// The clang::TemplateName class is effectively a pointer.
548 struct PointerLikeTypeTraits<clang::TemplateName> {
549 static inline void *getAsVoidPointer(clang::TemplateName TN) {
550 return TN.getAsVoidPointer();
553 static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
554 return clang::TemplateName::getFromVoidPointer(Ptr);
557 // No bits are available!
558 enum { NumLowBitsAvailable = 0 };
563 #endif // LLVM_CLANG_AST_TEMPLATENAME_H