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/PointerUnion.h"
20 #include "clang/Basic/OperatorKinds.h"
25 class DependentTemplateName;
26 class DiagnosticBuilder;
28 class NestedNameSpecifier;
29 class OverloadedTemplateStorage;
30 struct PrintingPolicy;
31 class QualifiedTemplateName;
33 class SubstTemplateTemplateParmStorage;
34 class SubstTemplateTemplateParmPackStorage;
35 class TemplateArgument;
37 class TemplateTemplateParmDecl;
39 /// \brief Implementation class used to describe either a set of overloaded
40 /// template names or an already-substituted template template parameter pack.
41 class UncommonTemplateNameStorage {
45 SubstTemplateTemplateParm,
46 SubstTemplateTemplateParmPack
54 /// \brief The number of stored templates or template arguments,
55 /// depending on which subclass we have.
59 void *PointerAlignment;
62 UncommonTemplateNameStorage(Kind kind, unsigned size) {
68 unsigned size() const { return Bits.Size; }
70 OverloadedTemplateStorage *getAsOverloadedStorage() {
71 return Bits.Kind == Overloaded
72 ? reinterpret_cast<OverloadedTemplateStorage *>(this)
76 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
77 return Bits.Kind == SubstTemplateTemplateParm
78 ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
82 SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
83 return Bits.Kind == SubstTemplateTemplateParmPack
84 ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
89 /// \brief A structure for storing the information associated with an
90 /// overloaded template name.
91 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
92 friend class ASTContext;
94 OverloadedTemplateStorage(unsigned size)
95 : UncommonTemplateNameStorage(Overloaded, size) { }
97 NamedDecl **getStorage() {
98 return reinterpret_cast<NamedDecl **>(this + 1);
100 NamedDecl * const *getStorage() const {
101 return reinterpret_cast<NamedDecl *const *>(this + 1);
105 typedef NamedDecl *const *iterator;
107 iterator begin() const { return getStorage(); }
108 iterator end() const { return getStorage() + size(); }
111 /// \brief A structure for storing an already-substituted template template
114 /// This kind of template names occurs when the parameter pack has been
115 /// provided with a template template argument pack in a context where its
116 /// enclosing pack expansion could not be fully expanded.
117 class SubstTemplateTemplateParmPackStorage
118 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
120 TemplateTemplateParmDecl *Parameter;
121 const TemplateArgument *Arguments;
124 SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
126 const TemplateArgument *Arguments)
127 : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
128 Parameter(Parameter), Arguments(Arguments) { }
130 /// \brief Retrieve the template template parameter pack being substituted.
131 TemplateTemplateParmDecl *getParameterPack() const {
135 /// \brief Retrieve the template template argument pack with which this
136 /// parameter was substituted.
137 TemplateArgument getArgumentPack() const;
139 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
141 static void Profile(llvm::FoldingSetNodeID &ID,
143 TemplateTemplateParmDecl *Parameter,
144 const TemplateArgument &ArgPack);
147 /// \brief Represents a C++ template name within the type system.
149 /// A C++ template name refers to a template within the C++ type
150 /// system. In most cases, a template name is simply a reference to a
151 /// class template, e.g.
154 /// template<typename T> class X { };
159 /// Here, the 'X' in \c X<int> is a template name that refers to the
160 /// declaration of the class template X, above. Template names can
161 /// also refer to function templates, C++0x template aliases, etc.
163 /// Some template names are dependent. For example, consider:
166 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
167 /// typedef typename MetaFun::template apply<T1, T2>::type type;
171 /// Here, "apply" is treated as a template name within the typename
172 /// specifier in the typedef. "apply" is a nested template, and can
173 /// only be understood in the context of
175 typedef llvm::PointerUnion4<TemplateDecl *,
176 UncommonTemplateNameStorage *,
177 QualifiedTemplateName *,
178 DependentTemplateName *> StorageType;
182 explicit TemplateName(void *Ptr) {
183 Storage = StorageType::getFromOpaqueValue(Ptr);
187 // \brief Kind of name that is actually stored.
189 /// \brief A single template declaration.
191 /// \brief A set of overloaded template declarations.
193 /// \brief A qualified template name, where the qualification is kept
194 /// to describe the source code as written.
196 /// \brief A dependent template name that has not been resolved to a
197 /// template (or set of templates).
199 /// \brief A template template parameter that has been substituted
200 /// for some other template name.
201 SubstTemplateTemplateParm,
202 /// \brief A template template parameter pack that has been substituted for
203 /// a template template argument pack, but has not yet been expanded into
204 /// individual arguments.
205 SubstTemplateTemplateParmPack
208 TemplateName() : Storage() { }
209 explicit TemplateName(TemplateDecl *Template) : Storage(Template) { }
210 explicit TemplateName(OverloadedTemplateStorage *Storage)
211 : Storage(Storage) { }
212 explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
213 explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
214 : Storage(Storage) { }
215 explicit TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) { }
216 explicit TemplateName(DependentTemplateName *Dep) : Storage(Dep) { }
218 /// \brief Determine whether this template name is NULL.
219 bool isNull() const { return Storage.isNull(); }
221 // \brief Get the kind of name that is actually stored.
222 NameKind getKind() const;
224 /// \brief Retrieve the underlying template declaration that
225 /// this template name refers to, if known.
227 /// \returns The template declaration that this template name refers
228 /// to, if any. If the template name does not refer to a specific
229 /// declaration because it is a dependent name, or if it refers to a
230 /// set of function templates, returns NULL.
231 TemplateDecl *getAsTemplateDecl() const;
233 /// \brief Retrieve the underlying, overloaded function template
234 // declarations that this template name refers to, if known.
236 /// \returns The set of overloaded function templates that this template
237 /// name refers to, if known. If the template name does not refer to a
238 /// specific set of function templates because it is a dependent name or
239 /// refers to a single template, returns NULL.
240 OverloadedTemplateStorage *getAsOverloadedTemplate() const {
241 if (UncommonTemplateNameStorage *Uncommon =
242 Storage.dyn_cast<UncommonTemplateNameStorage *>())
243 return Uncommon->getAsOverloadedStorage();
248 /// \brief Retrieve the substituted template template parameter, if
251 /// \returns The storage for the substituted template template parameter,
252 /// if known. Otherwise, returns NULL.
253 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const {
254 if (UncommonTemplateNameStorage *uncommon =
255 Storage.dyn_cast<UncommonTemplateNameStorage *>())
256 return uncommon->getAsSubstTemplateTemplateParm();
261 /// \brief Retrieve the substituted template template parameter pack, if
264 /// \returns The storage for the substituted template template parameter pack,
265 /// if known. Otherwise, returns NULL.
266 SubstTemplateTemplateParmPackStorage *
267 getAsSubstTemplateTemplateParmPack() const {
268 if (UncommonTemplateNameStorage *Uncommon =
269 Storage.dyn_cast<UncommonTemplateNameStorage *>())
270 return Uncommon->getAsSubstTemplateTemplateParmPack();
275 /// \brief Retrieve the underlying qualified template name
276 /// structure, if any.
277 QualifiedTemplateName *getAsQualifiedTemplateName() const {
278 return Storage.dyn_cast<QualifiedTemplateName *>();
281 /// \brief Retrieve the underlying dependent template name
282 /// structure, if any.
283 DependentTemplateName *getAsDependentTemplateName() const {
284 return Storage.dyn_cast<DependentTemplateName *>();
287 TemplateName getUnderlying() const;
289 /// \brief Determines whether this is a dependent template name.
290 bool isDependent() const;
292 /// \brief Determines whether this is a template name that somehow
293 /// depends on a template parameter.
294 bool isInstantiationDependent() const;
296 /// \brief Determines whether this template name contains an
297 /// unexpanded parameter pack (for C++0x variadic templates).
298 bool containsUnexpandedParameterPack() const;
300 /// \brief Print the template name.
302 /// \param OS the output stream to which the template name will be
305 /// \param SuppressNNS if true, don't print the
306 /// nested-name-specifier that precedes the template name (if it has
308 void print(raw_ostream &OS, const PrintingPolicy &Policy,
309 bool SuppressNNS = false) const;
311 /// \brief Debugging aid that dumps the template name to standard
315 void Profile(llvm::FoldingSetNodeID &ID) {
316 ID.AddPointer(Storage.getOpaqueValue());
319 /// \brief Retrieve the template name as a void pointer.
320 void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
322 /// \brief Build a template name from a void pointer.
323 static TemplateName getFromVoidPointer(void *Ptr) {
324 return TemplateName(Ptr);
328 /// Insertion operator for diagnostics. This allows sending TemplateName's
329 /// into a diagnostic with <<.
330 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
333 /// \brief A structure for storing the information associated with a
334 /// substituted template template parameter.
335 class SubstTemplateTemplateParmStorage
336 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
337 friend class ASTContext;
339 TemplateTemplateParmDecl *Parameter;
340 TemplateName Replacement;
342 SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
343 TemplateName replacement)
344 : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
345 Parameter(parameter), Replacement(replacement) {}
348 TemplateTemplateParmDecl *getParameter() const { return Parameter; }
349 TemplateName getReplacement() const { return Replacement; }
351 void Profile(llvm::FoldingSetNodeID &ID);
353 static void Profile(llvm::FoldingSetNodeID &ID,
354 TemplateTemplateParmDecl *parameter,
355 TemplateName replacement);
358 inline TemplateName::TemplateName(SubstTemplateTemplateParmStorage *Storage)
359 : Storage(Storage) { }
361 inline TemplateName TemplateName::getUnderlying() const {
362 if (SubstTemplateTemplateParmStorage *subst
363 = getAsSubstTemplateTemplateParm())
364 return subst->getReplacement().getUnderlying();
368 /// \brief Represents a template name that was expressed as a
371 /// This kind of template name refers to a template name that was
372 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
373 /// the nested name specifier is "std::" and the template name is the
374 /// declaration for "vector". The QualifiedTemplateName class is only
375 /// used to provide "sugar" for template names that were expressed
376 /// with a qualified name, and has no semantic meaning. In this
377 /// manner, it is to TemplateName what ElaboratedType is to Type,
378 /// providing extra syntactic sugar for downstream clients.
379 class QualifiedTemplateName : public llvm::FoldingSetNode {
380 /// \brief The nested name specifier that qualifies the template name.
382 /// The bit is used to indicate whether the "template" keyword was
383 /// present before the template name itself. Note that the
384 /// "template" keyword is always redundant in this case (otherwise,
385 /// the template name would be a dependent name and we would express
386 /// this name with DependentTemplateName).
387 llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
389 /// \brief The template declaration or set of overloaded function templates
390 /// that this qualified name refers to.
391 TemplateDecl *Template;
393 friend class ASTContext;
395 QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
396 TemplateDecl *Template)
397 : Qualifier(NNS, TemplateKeyword? 1 : 0),
398 Template(Template) { }
401 /// \brief Return the nested name specifier that qualifies this name.
402 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
404 /// \brief Whether the template name was prefixed by the "template"
406 bool hasTemplateKeyword() const { return Qualifier.getInt(); }
408 /// \brief The template declaration that this qualified name refers
410 TemplateDecl *getDecl() const { return Template; }
412 /// \brief The template declaration to which this qualified name
414 TemplateDecl *getTemplateDecl() const { return Template; }
416 void Profile(llvm::FoldingSetNodeID &ID) {
417 Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
420 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
421 bool TemplateKeyword, TemplateDecl *Template) {
423 ID.AddBoolean(TemplateKeyword);
424 ID.AddPointer(Template);
428 /// \brief Represents a dependent template name that cannot be
429 /// resolved prior to template instantiation.
431 /// This kind of template name refers to a dependent template name,
432 /// including its nested name specifier (if any). For example,
433 /// DependentTemplateName can refer to "MetaFun::template apply",
434 /// where "MetaFun::" is the nested name specifier and "apply" is the
435 /// template name referenced. The "template" keyword is implied.
436 class DependentTemplateName : public llvm::FoldingSetNode {
437 /// \brief The nested name specifier that qualifies the template
440 /// The bit stored in this qualifier describes whether the \c Name field
441 /// is interpreted as an IdentifierInfo pointer (when clear) or as an
442 /// overloaded operator kind (when set).
443 llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
445 /// \brief The dependent template name.
447 /// \brief The identifier template name.
449 /// Only valid when the bit on \c Qualifier is clear.
450 const IdentifierInfo *Identifier;
452 /// \brief The overloaded operator name.
454 /// Only valid when the bit on \c Qualifier is set.
455 OverloadedOperatorKind Operator;
458 /// \brief The canonical template name to which this dependent
459 /// template name refers.
461 /// The canonical template name for a dependent template name is
462 /// another dependent template name whose nested name specifier is
464 TemplateName CanonicalTemplateName;
466 friend class ASTContext;
468 DependentTemplateName(NestedNameSpecifier *Qualifier,
469 const IdentifierInfo *Identifier)
470 : Qualifier(Qualifier, false), Identifier(Identifier),
471 CanonicalTemplateName(this) { }
473 DependentTemplateName(NestedNameSpecifier *Qualifier,
474 const IdentifierInfo *Identifier,
476 : Qualifier(Qualifier, false), Identifier(Identifier),
477 CanonicalTemplateName(Canon) { }
479 DependentTemplateName(NestedNameSpecifier *Qualifier,
480 OverloadedOperatorKind Operator)
481 : Qualifier(Qualifier, true), Operator(Operator),
482 CanonicalTemplateName(this) { }
484 DependentTemplateName(NestedNameSpecifier *Qualifier,
485 OverloadedOperatorKind Operator,
487 : Qualifier(Qualifier, true), Operator(Operator),
488 CanonicalTemplateName(Canon) { }
491 /// \brief Return the nested name specifier that qualifies this name.
492 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
494 /// \brief Determine whether this template name refers to an identifier.
495 bool isIdentifier() const { return !Qualifier.getInt(); }
497 /// \brief Returns the identifier to which this template name refers.
498 const IdentifierInfo *getIdentifier() const {
499 assert(isIdentifier() && "Template name isn't an identifier?");
503 /// \brief Determine whether this template name refers to an overloaded
505 bool isOverloadedOperator() const { return Qualifier.getInt(); }
507 /// \brief Return the overloaded operator to which this template name refers.
508 OverloadedOperatorKind getOperator() const {
509 assert(isOverloadedOperator() &&
510 "Template name isn't an overloaded operator?");
514 void Profile(llvm::FoldingSetNodeID &ID) {
516 Profile(ID, getQualifier(), getIdentifier());
518 Profile(ID, getQualifier(), getOperator());
521 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
522 const IdentifierInfo *Identifier) {
524 ID.AddBoolean(false);
525 ID.AddPointer(Identifier);
528 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
529 OverloadedOperatorKind Operator) {
532 ID.AddInteger(Operator);
536 } // end namespace clang.
540 /// \brief The clang::TemplateName class is effectively a pointer.
542 class PointerLikeTypeTraits<clang::TemplateName> {
544 static inline void *getAsVoidPointer(clang::TemplateName TN) {
545 return TN.getAsVoidPointer();
548 static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
549 return clang::TemplateName::getFromVoidPointer(Ptr);
552 // No bits are available!
553 enum { NumLowBitsAvailable = 0 };
556 } // end namespace llvm.