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/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"
28 class DependentTemplateName;
29 class DiagnosticBuilder;
32 class NestedNameSpecifier;
33 enum OverloadedOperatorKind : int;
34 class OverloadedTemplateStorage;
35 struct PrintingPolicy;
36 class QualifiedTemplateName;
37 class SubstTemplateTemplateParmPackStorage;
38 class SubstTemplateTemplateParmStorage;
39 class TemplateArgument;
41 class TemplateTemplateParmDecl;
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 {
49 SubstTemplateTemplateParm,
50 SubstTemplateTemplateParmPack
57 /// The number of stored templates or template arguments,
58 /// depending on which subclass we have.
64 void *PointerAlignment;
67 UncommonTemplateNameStorage(Kind kind, unsigned size) {
73 unsigned size() const { return Bits.Size; }
75 OverloadedTemplateStorage *getAsOverloadedStorage() {
76 return Bits.Kind == Overloaded
77 ? reinterpret_cast<OverloadedTemplateStorage *>(this)
81 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
82 return Bits.Kind == SubstTemplateTemplateParm
83 ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
87 SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
88 return Bits.Kind == SubstTemplateTemplateParmPack
89 ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
94 /// A structure for storing the information associated with an
95 /// overloaded template name.
96 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
97 friend class ASTContext;
99 OverloadedTemplateStorage(unsigned size)
100 : UncommonTemplateNameStorage(Overloaded, size) {}
102 NamedDecl **getStorage() {
103 return reinterpret_cast<NamedDecl **>(this + 1);
105 NamedDecl * const *getStorage() const {
106 return reinterpret_cast<NamedDecl *const *>(this + 1);
110 using iterator = NamedDecl *const *;
112 iterator begin() const { return getStorage(); }
113 iterator end() const { return getStorage() + size(); }
116 /// A structure for storing an already-substituted template template
119 /// This kind of template names occurs when the parameter pack has been
120 /// provided with a template template argument pack in a context where its
121 /// enclosing pack expansion could not be fully expanded.
122 class SubstTemplateTemplateParmPackStorage
123 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
125 TemplateTemplateParmDecl *Parameter;
126 const TemplateArgument *Arguments;
129 SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
131 const TemplateArgument *Arguments)
132 : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
133 Parameter(Parameter), Arguments(Arguments) {}
135 /// Retrieve the template template parameter pack being substituted.
136 TemplateTemplateParmDecl *getParameterPack() const {
140 /// Retrieve the template template argument pack with which this
141 /// parameter was substituted.
142 TemplateArgument getArgumentPack() const;
144 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
146 static void Profile(llvm::FoldingSetNodeID &ID,
148 TemplateTemplateParmDecl *Parameter,
149 const TemplateArgument &ArgPack);
152 /// Represents a C++ template name within the type system.
154 /// A C++ template name refers to a template within the C++ type
155 /// system. In most cases, a template name is simply a reference to a
156 /// class template, e.g.
159 /// template<typename T> class X { };
164 /// Here, the 'X' in \c X<int> is a template name that refers to the
165 /// declaration of the class template X, above. Template names can
166 /// also refer to function templates, C++0x template aliases, etc.
168 /// Some template names are dependent. For example, consider:
171 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
172 /// typedef typename MetaFun::template apply<T1, T2>::type type;
176 /// Here, "apply" is treated as a template name within the typename
177 /// specifier in the typedef. "apply" is a nested template, and can
178 /// only be understood in the context of
181 llvm::PointerUnion4<TemplateDecl *, UncommonTemplateNameStorage *,
182 QualifiedTemplateName *, DependentTemplateName *>;
186 explicit TemplateName(void *Ptr);
189 // Kind of name that is actually stored.
191 /// A single template declaration.
194 /// A set of overloaded template declarations.
197 /// A qualified template name, where the qualification is kept
198 /// to describe the source code as written.
201 /// A dependent template name that has not been resolved to a
202 /// template (or set of templates).
205 /// A template template parameter that has been substituted
206 /// for some other template name.
207 SubstTemplateTemplateParm,
209 /// A template template parameter pack that has been substituted for
210 /// a template template argument pack, but has not yet been expanded into
211 /// individual arguments.
212 SubstTemplateTemplateParmPack
215 TemplateName() = default;
216 explicit TemplateName(TemplateDecl *Template);
217 explicit TemplateName(OverloadedTemplateStorage *Storage);
218 explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
219 explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage);
220 explicit TemplateName(QualifiedTemplateName *Qual);
221 explicit TemplateName(DependentTemplateName *Dep);
223 /// Determine whether this template name is NULL.
226 // Get the kind of name that is actually stored.
227 NameKind getKind() const;
229 /// Retrieve the underlying template declaration that
230 /// this template name refers to, if known.
232 /// \returns The template declaration that this template name refers
233 /// to, if any. If the template name does not refer to a specific
234 /// declaration because it is a dependent name, or if it refers to a
235 /// set of function templates, returns NULL.
236 TemplateDecl *getAsTemplateDecl() const;
238 /// Retrieve the underlying, overloaded function template
239 // declarations that this template name refers to, if known.
241 /// \returns The set of overloaded function templates that this template
242 /// name refers to, if known. If the template name does not refer to a
243 /// specific set of function templates because it is a dependent name or
244 /// refers to a single template, returns NULL.
245 OverloadedTemplateStorage *getAsOverloadedTemplate() const;
247 /// Retrieve the substituted template template parameter, if
250 /// \returns The storage for the substituted template template parameter,
251 /// if known. Otherwise, returns NULL.
252 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const;
254 /// Retrieve the substituted template template parameter pack, if
257 /// \returns The storage for the substituted template template parameter pack,
258 /// if known. Otherwise, returns NULL.
259 SubstTemplateTemplateParmPackStorage *
260 getAsSubstTemplateTemplateParmPack() const;
262 /// Retrieve the underlying qualified template name
263 /// structure, if any.
264 QualifiedTemplateName *getAsQualifiedTemplateName() const;
266 /// Retrieve the underlying dependent template name
267 /// structure, if any.
268 DependentTemplateName *getAsDependentTemplateName() const;
270 TemplateName getUnderlying() const;
272 /// Get the template name to substitute when this template name is used as a
273 /// template template argument. This refers to the most recent declaration of
274 /// the template, including any default template arguments.
275 TemplateName getNameToSubstitute() const;
277 /// Determines whether this is a dependent template name.
278 bool isDependent() const;
280 /// Determines whether this is a template name that somehow
281 /// depends on a template parameter.
282 bool isInstantiationDependent() const;
284 /// Determines whether this template name contains an
285 /// unexpanded parameter pack (for C++0x variadic templates).
286 bool containsUnexpandedParameterPack() const;
288 /// Print the template name.
290 /// \param OS the output stream to which the template name will be
293 /// \param SuppressNNS if true, don't print the
294 /// nested-name-specifier that precedes the template name (if it has
296 void print(raw_ostream &OS, const PrintingPolicy &Policy,
297 bool SuppressNNS = false) const;
299 /// Debugging aid that dumps the template name.
300 void dump(raw_ostream &OS) const;
302 /// Debugging aid that dumps the template name to standard
306 void Profile(llvm::FoldingSetNodeID &ID) {
307 ID.AddPointer(Storage.getOpaqueValue());
310 /// Retrieve the template name as a void pointer.
311 void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
313 /// Build a template name from a void pointer.
314 static TemplateName getFromVoidPointer(void *Ptr) {
315 return TemplateName(Ptr);
319 /// Insertion operator for diagnostics. This allows sending TemplateName's
320 /// into a diagnostic with <<.
321 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
324 /// A structure for storing the information associated with a
325 /// substituted template template parameter.
326 class SubstTemplateTemplateParmStorage
327 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
328 friend class ASTContext;
330 TemplateTemplateParmDecl *Parameter;
331 TemplateName Replacement;
333 SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
334 TemplateName replacement)
335 : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
336 Parameter(parameter), Replacement(replacement) {}
339 TemplateTemplateParmDecl *getParameter() const { return Parameter; }
340 TemplateName getReplacement() const { return Replacement; }
342 void Profile(llvm::FoldingSetNodeID &ID);
344 static void Profile(llvm::FoldingSetNodeID &ID,
345 TemplateTemplateParmDecl *parameter,
346 TemplateName replacement);
349 inline TemplateName TemplateName::getUnderlying() const {
350 if (SubstTemplateTemplateParmStorage *subst
351 = getAsSubstTemplateTemplateParm())
352 return subst->getReplacement().getUnderlying();
356 /// Represents a template name that was expressed as a
359 /// This kind of template name refers to a template name that was
360 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
361 /// the nested name specifier is "std::" and the template name is the
362 /// declaration for "vector". The QualifiedTemplateName class is only
363 /// used to provide "sugar" for template names that were expressed
364 /// with a qualified name, and has no semantic meaning. In this
365 /// manner, it is to TemplateName what ElaboratedType is to Type,
366 /// providing extra syntactic sugar for downstream clients.
367 class QualifiedTemplateName : public llvm::FoldingSetNode {
368 friend class ASTContext;
370 /// The nested name specifier that qualifies the template name.
372 /// The bit is used to indicate whether the "template" keyword was
373 /// present before the template name itself. Note that the
374 /// "template" keyword is always redundant in this case (otherwise,
375 /// the template name would be a dependent name and we would express
376 /// this name with DependentTemplateName).
377 llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
379 /// The template declaration or set of overloaded function templates
380 /// that this qualified name refers to.
381 TemplateDecl *Template;
383 QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
384 TemplateDecl *Template)
385 : Qualifier(NNS, TemplateKeyword? 1 : 0), Template(Template) {}
388 /// Return the nested name specifier that qualifies this name.
389 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
391 /// Whether the template name was prefixed by the "template"
393 bool hasTemplateKeyword() const { return Qualifier.getInt(); }
395 /// The template declaration that this qualified name refers
397 TemplateDecl *getDecl() const { return Template; }
399 /// The template declaration to which this qualified name
401 TemplateDecl *getTemplateDecl() const { return Template; }
403 void Profile(llvm::FoldingSetNodeID &ID) {
404 Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
407 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
408 bool TemplateKeyword, TemplateDecl *Template) {
410 ID.AddBoolean(TemplateKeyword);
411 ID.AddPointer(Template);
415 /// Represents a dependent template name that cannot be
416 /// resolved prior to template instantiation.
418 /// This kind of template name refers to a dependent template name,
419 /// including its nested name specifier (if any). For example,
420 /// DependentTemplateName can refer to "MetaFun::template apply",
421 /// where "MetaFun::" is the nested name specifier and "apply" is the
422 /// template name referenced. The "template" keyword is implied.
423 class DependentTemplateName : public llvm::FoldingSetNode {
424 friend class ASTContext;
426 /// The nested name specifier that qualifies the template
429 /// The bit stored in this qualifier describes whether the \c Name field
430 /// is interpreted as an IdentifierInfo pointer (when clear) or as an
431 /// overloaded operator kind (when set).
432 llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
434 /// The dependent template name.
436 /// The identifier template name.
438 /// Only valid when the bit on \c Qualifier is clear.
439 const IdentifierInfo *Identifier;
441 /// The overloaded operator name.
443 /// Only valid when the bit on \c Qualifier is set.
444 OverloadedOperatorKind Operator;
447 /// The canonical template name to which this dependent
448 /// template name refers.
450 /// The canonical template name for a dependent template name is
451 /// another dependent template name whose nested name specifier is
453 TemplateName CanonicalTemplateName;
455 DependentTemplateName(NestedNameSpecifier *Qualifier,
456 const IdentifierInfo *Identifier)
457 : Qualifier(Qualifier, false), Identifier(Identifier),
458 CanonicalTemplateName(this) {}
460 DependentTemplateName(NestedNameSpecifier *Qualifier,
461 const IdentifierInfo *Identifier,
463 : Qualifier(Qualifier, false), Identifier(Identifier),
464 CanonicalTemplateName(Canon) {}
466 DependentTemplateName(NestedNameSpecifier *Qualifier,
467 OverloadedOperatorKind Operator)
468 : Qualifier(Qualifier, true), Operator(Operator),
469 CanonicalTemplateName(this) {}
471 DependentTemplateName(NestedNameSpecifier *Qualifier,
472 OverloadedOperatorKind Operator,
474 : Qualifier(Qualifier, true), Operator(Operator),
475 CanonicalTemplateName(Canon) {}
478 /// Return the nested name specifier that qualifies this name.
479 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
481 /// Determine whether this template name refers to an identifier.
482 bool isIdentifier() const { return !Qualifier.getInt(); }
484 /// Returns the identifier to which this template name refers.
485 const IdentifierInfo *getIdentifier() const {
486 assert(isIdentifier() && "Template name isn't an identifier?");
490 /// Determine whether this template name refers to an overloaded
492 bool isOverloadedOperator() const { return Qualifier.getInt(); }
494 /// Return the overloaded operator to which this template name refers.
495 OverloadedOperatorKind getOperator() const {
496 assert(isOverloadedOperator() &&
497 "Template name isn't an overloaded operator?");
501 void Profile(llvm::FoldingSetNodeID &ID) {
503 Profile(ID, getQualifier(), getIdentifier());
505 Profile(ID, getQualifier(), getOperator());
508 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
509 const IdentifierInfo *Identifier) {
511 ID.AddBoolean(false);
512 ID.AddPointer(Identifier);
515 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
516 OverloadedOperatorKind Operator) {
519 ID.AddInteger(Operator);
523 } // namespace clang.
527 /// The clang::TemplateName class is effectively a pointer.
529 struct PointerLikeTypeTraits<clang::TemplateName> {
530 static inline void *getAsVoidPointer(clang::TemplateName TN) {
531 return TN.getAsVoidPointer();
534 static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
535 return clang::TemplateName::getFromVoidPointer(Ptr);
538 // No bits are available!
539 enum { NumLowBitsAvailable = 0 };
544 #endif // LLVM_CLANG_AST_TEMPLATENAME_H