1 //===- TemplateName.h - C++ Template Name Representation --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TemplateName interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_TEMPLATENAME_H
15 #define LLVM_CLANG_AST_TEMPLATENAME_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 struct PrintingPolicy;
35 class QualifiedTemplateName;
36 class SubstTemplateTemplateParmPackStorage;
37 class SubstTemplateTemplateParmStorage;
38 class TemplateArgument;
40 class TemplateTemplateParmDecl;
42 /// \brief Implementation class used to describe either a set of overloaded
43 /// template names or an already-substituted template template parameter pack.
44 class UncommonTemplateNameStorage {
48 SubstTemplateTemplateParm,
49 SubstTemplateTemplateParmPack
56 /// \brief The number of stored templates or template arguments,
57 /// depending on which subclass we have.
63 void *PointerAlignment;
66 UncommonTemplateNameStorage(Kind kind, unsigned size) {
72 unsigned size() const { return Bits.Size; }
74 OverloadedTemplateStorage *getAsOverloadedStorage() {
75 return Bits.Kind == Overloaded
76 ? reinterpret_cast<OverloadedTemplateStorage *>(this)
80 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
81 return Bits.Kind == SubstTemplateTemplateParm
82 ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
86 SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
87 return Bits.Kind == SubstTemplateTemplateParmPack
88 ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
93 /// \brief A structure for storing the information associated with an
94 /// overloaded template name.
95 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
96 friend class ASTContext;
98 OverloadedTemplateStorage(unsigned size)
99 : UncommonTemplateNameStorage(Overloaded, size) {}
101 NamedDecl **getStorage() {
102 return reinterpret_cast<NamedDecl **>(this + 1);
104 NamedDecl * const *getStorage() const {
105 return reinterpret_cast<NamedDecl *const *>(this + 1);
109 using iterator = NamedDecl *const *;
111 iterator begin() const { return getStorage(); }
112 iterator end() const { return getStorage() + size(); }
115 /// \brief A structure for storing an already-substituted template template
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.
121 class SubstTemplateTemplateParmPackStorage
122 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
124 TemplateTemplateParmDecl *Parameter;
125 const TemplateArgument *Arguments;
128 SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
130 const TemplateArgument *Arguments)
131 : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
132 Parameter(Parameter), Arguments(Arguments) {}
134 /// \brief Retrieve the template template parameter pack being substituted.
135 TemplateTemplateParmDecl *getParameterPack() const {
139 /// \brief Retrieve the template template argument pack with which this
140 /// parameter was substituted.
141 TemplateArgument getArgumentPack() const;
143 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
145 static void Profile(llvm::FoldingSetNodeID &ID,
147 TemplateTemplateParmDecl *Parameter,
148 const TemplateArgument &ArgPack);
151 /// \brief Represents a C++ template name within the type system.
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.
158 /// template<typename T> class X { };
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.
167 /// Some template names are dependent. For example, consider:
170 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
171 /// typedef typename MetaFun::template apply<T1, T2>::type type;
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
180 llvm::PointerUnion4<TemplateDecl *, UncommonTemplateNameStorage *,
181 QualifiedTemplateName *, DependentTemplateName *>;
185 explicit TemplateName(void *Ptr);
188 // \brief Kind of name that is actually stored.
190 /// \brief A single template declaration.
193 /// \brief A set of overloaded template declarations.
196 /// \brief A qualified template name, where the qualification is kept
197 /// to describe the source code as written.
200 /// \brief A dependent template name that has not been resolved to a
201 /// template (or set of templates).
204 /// \brief A template template parameter that has been substituted
205 /// for some other template name.
206 SubstTemplateTemplateParm,
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.
211 SubstTemplateTemplateParmPack
214 TemplateName() = default;
215 explicit TemplateName(TemplateDecl *Template);
216 explicit TemplateName(OverloadedTemplateStorage *Storage);
217 explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
218 explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage);
219 explicit TemplateName(QualifiedTemplateName *Qual);
220 explicit TemplateName(DependentTemplateName *Dep);
222 /// \brief Determine whether this template name is NULL.
225 // \brief Get the kind of name that is actually stored.
226 NameKind getKind() const;
228 /// \brief Retrieve the underlying template declaration that
229 /// this template name refers to, if known.
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;
237 /// \brief Retrieve the underlying, overloaded function template
238 // declarations that this template name refers to, if known.
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;
246 /// \brief Retrieve the substituted template template parameter, if
249 /// \returns The storage for the substituted template template parameter,
250 /// if known. Otherwise, returns NULL.
251 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const;
253 /// \brief Retrieve the substituted template template parameter pack, if
256 /// \returns The storage for the substituted template template parameter pack,
257 /// if known. Otherwise, returns NULL.
258 SubstTemplateTemplateParmPackStorage *
259 getAsSubstTemplateTemplateParmPack() const;
261 /// \brief Retrieve the underlying qualified template name
262 /// structure, if any.
263 QualifiedTemplateName *getAsQualifiedTemplateName() const;
265 /// \brief Retrieve the underlying dependent template name
266 /// structure, if any.
267 DependentTemplateName *getAsDependentTemplateName() const;
269 TemplateName getUnderlying() const;
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;
276 /// \brief Determines whether this is a dependent template name.
277 bool isDependent() const;
279 /// \brief Determines whether this is a template name that somehow
280 /// depends on a template parameter.
281 bool isInstantiationDependent() const;
283 /// \brief Determines whether this template name contains an
284 /// unexpanded parameter pack (for C++0x variadic templates).
285 bool containsUnexpandedParameterPack() const;
287 /// \brief Print the template name.
289 /// \param OS the output stream to which the template name will be
292 /// \param SuppressNNS if true, don't print the
293 /// nested-name-specifier that precedes the template name (if it has
295 void print(raw_ostream &OS, const PrintingPolicy &Policy,
296 bool SuppressNNS = false) const;
298 /// \brief Debugging aid that dumps the template name.
299 void dump(raw_ostream &OS) const;
301 /// \brief Debugging aid that dumps the template name to standard
305 void Profile(llvm::FoldingSetNodeID &ID) {
306 ID.AddPointer(Storage.getOpaqueValue());
309 /// \brief Retrieve the template name as a void pointer.
310 void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
312 /// \brief Build a template name from a void pointer.
313 static TemplateName getFromVoidPointer(void *Ptr) {
314 return TemplateName(Ptr);
318 /// Insertion operator for diagnostics. This allows sending TemplateName's
319 /// into a diagnostic with <<.
320 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
323 /// \brief A structure for storing the information associated with a
324 /// substituted template template parameter.
325 class SubstTemplateTemplateParmStorage
326 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
327 friend class ASTContext;
329 TemplateTemplateParmDecl *Parameter;
330 TemplateName Replacement;
332 SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
333 TemplateName replacement)
334 : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
335 Parameter(parameter), Replacement(replacement) {}
338 TemplateTemplateParmDecl *getParameter() const { return Parameter; }
339 TemplateName getReplacement() const { return Replacement; }
341 void Profile(llvm::FoldingSetNodeID &ID);
343 static void Profile(llvm::FoldingSetNodeID &ID,
344 TemplateTemplateParmDecl *parameter,
345 TemplateName replacement);
348 inline TemplateName TemplateName::getUnderlying() const {
349 if (SubstTemplateTemplateParmStorage *subst
350 = getAsSubstTemplateTemplateParm())
351 return subst->getReplacement().getUnderlying();
355 /// \brief Represents a template name that was expressed as a
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;
369 /// \brief The nested name specifier that qualifies the template name.
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;
378 /// \brief The template declaration or set of overloaded function templates
379 /// that this qualified name refers to.
380 TemplateDecl *Template;
382 QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
383 TemplateDecl *Template)
384 : Qualifier(NNS, TemplateKeyword? 1 : 0), Template(Template) {}
387 /// \brief Return the nested name specifier that qualifies this name.
388 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
390 /// \brief Whether the template name was prefixed by the "template"
392 bool hasTemplateKeyword() const { return Qualifier.getInt(); }
394 /// \brief The template declaration that this qualified name refers
396 TemplateDecl *getDecl() const { return Template; }
398 /// \brief The template declaration to which this qualified name
400 TemplateDecl *getTemplateDecl() const { return Template; }
402 void Profile(llvm::FoldingSetNodeID &ID) {
403 Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
406 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
407 bool TemplateKeyword, TemplateDecl *Template) {
409 ID.AddBoolean(TemplateKeyword);
410 ID.AddPointer(Template);
414 /// \brief Represents a dependent template name that cannot be
415 /// resolved prior to template instantiation.
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;
425 /// \brief The nested name specifier that qualifies the template
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;
433 /// \brief The dependent template name.
435 /// \brief The identifier template name.
437 /// Only valid when the bit on \c Qualifier is clear.
438 const IdentifierInfo *Identifier;
440 /// \brief The overloaded operator name.
442 /// Only valid when the bit on \c Qualifier is set.
443 OverloadedOperatorKind Operator;
446 /// \brief The canonical template name to which this dependent
447 /// template name refers.
449 /// The canonical template name for a dependent template name is
450 /// another dependent template name whose nested name specifier is
452 TemplateName CanonicalTemplateName;
454 DependentTemplateName(NestedNameSpecifier *Qualifier,
455 const IdentifierInfo *Identifier)
456 : Qualifier(Qualifier, false), Identifier(Identifier),
457 CanonicalTemplateName(this) {}
459 DependentTemplateName(NestedNameSpecifier *Qualifier,
460 const IdentifierInfo *Identifier,
462 : Qualifier(Qualifier, false), Identifier(Identifier),
463 CanonicalTemplateName(Canon) {}
465 DependentTemplateName(NestedNameSpecifier *Qualifier,
466 OverloadedOperatorKind Operator)
467 : Qualifier(Qualifier, true), Operator(Operator),
468 CanonicalTemplateName(this) {}
470 DependentTemplateName(NestedNameSpecifier *Qualifier,
471 OverloadedOperatorKind Operator,
473 : Qualifier(Qualifier, true), Operator(Operator),
474 CanonicalTemplateName(Canon) {}
477 /// \brief Return the nested name specifier that qualifies this name.
478 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
480 /// \brief Determine whether this template name refers to an identifier.
481 bool isIdentifier() const { return !Qualifier.getInt(); }
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?");
489 /// \brief Determine whether this template name refers to an overloaded
491 bool isOverloadedOperator() const { return Qualifier.getInt(); }
493 /// \brief Return the overloaded operator to which this template name refers.
494 OverloadedOperatorKind getOperator() const {
495 assert(isOverloadedOperator() &&
496 "Template name isn't an overloaded operator?");
500 void Profile(llvm::FoldingSetNodeID &ID) {
502 Profile(ID, getQualifier(), getIdentifier());
504 Profile(ID, getQualifier(), getOperator());
507 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
508 const IdentifierInfo *Identifier) {
510 ID.AddBoolean(false);
511 ID.AddPointer(Identifier);
514 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
515 OverloadedOperatorKind Operator) {
518 ID.AddInteger(Operator);
522 } // namespace clang.
526 /// \brief The clang::TemplateName class is effectively a pointer.
528 struct PointerLikeTypeTraits<clang::TemplateName> {
529 static inline void *getAsVoidPointer(clang::TemplateName TN) {
530 return TN.getAsVoidPointer();
533 static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
534 return clang::TemplateName::getFromVoidPointer(Ptr);
537 // No bits are available!
538 enum { NumLowBitsAvailable = 0 };
543 #endif // LLVM_CLANG_AST_TEMPLATENAME_H