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/PointerUnion.h"
25 class DependentTemplateName;
26 class DiagnosticBuilder;
29 class NestedNameSpecifier;
30 enum OverloadedOperatorKind : int;
31 class OverloadedTemplateStorage;
32 struct PrintingPolicy;
33 class QualifiedTemplateName;
34 class SubstTemplateTemplateParmPackStorage;
35 class SubstTemplateTemplateParmStorage;
36 class TemplateArgument;
38 class TemplateTemplateParmDecl;
40 /// \brief Implementation class used to describe either a set of overloaded
41 /// template names or an already-substituted template template parameter pack.
42 class UncommonTemplateNameStorage {
46 SubstTemplateTemplateParm,
47 SubstTemplateTemplateParmPack
54 /// \brief The number of stored templates or template arguments,
55 /// depending on which subclass we have.
61 void *PointerAlignment;
64 UncommonTemplateNameStorage(Kind kind, unsigned size) {
70 unsigned size() const { return Bits.Size; }
72 OverloadedTemplateStorage *getAsOverloadedStorage() {
73 return Bits.Kind == Overloaded
74 ? reinterpret_cast<OverloadedTemplateStorage *>(this)
78 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
79 return Bits.Kind == SubstTemplateTemplateParm
80 ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
84 SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
85 return Bits.Kind == SubstTemplateTemplateParmPack
86 ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
91 /// \brief A structure for storing the information associated with an
92 /// overloaded template name.
93 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
94 friend class ASTContext;
96 OverloadedTemplateStorage(unsigned size)
97 : UncommonTemplateNameStorage(Overloaded, size) { }
99 NamedDecl **getStorage() {
100 return reinterpret_cast<NamedDecl **>(this + 1);
102 NamedDecl * const *getStorage() const {
103 return reinterpret_cast<NamedDecl *const *>(this + 1);
107 typedef NamedDecl *const *iterator;
109 iterator begin() const { return getStorage(); }
110 iterator end() const { return getStorage() + size(); }
113 /// \brief A structure for storing an already-substituted template template
116 /// This kind of template names occurs when the parameter pack has been
117 /// provided with a template template argument pack in a context where its
118 /// enclosing pack expansion could not be fully expanded.
119 class SubstTemplateTemplateParmPackStorage
120 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
122 TemplateTemplateParmDecl *Parameter;
123 const TemplateArgument *Arguments;
126 SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
128 const TemplateArgument *Arguments)
129 : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
130 Parameter(Parameter), Arguments(Arguments) { }
132 /// \brief Retrieve the template template parameter pack being substituted.
133 TemplateTemplateParmDecl *getParameterPack() const {
137 /// \brief Retrieve the template template argument pack with which this
138 /// parameter was substituted.
139 TemplateArgument getArgumentPack() const;
141 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
143 static void Profile(llvm::FoldingSetNodeID &ID,
145 TemplateTemplateParmDecl *Parameter,
146 const TemplateArgument &ArgPack);
149 /// \brief Represents a C++ template name within the type system.
151 /// A C++ template name refers to a template within the C++ type
152 /// system. In most cases, a template name is simply a reference to a
153 /// class template, e.g.
156 /// template<typename T> class X { };
161 /// Here, the 'X' in \c X<int> is a template name that refers to the
162 /// declaration of the class template X, above. Template names can
163 /// also refer to function templates, C++0x template aliases, etc.
165 /// Some template names are dependent. For example, consider:
168 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
169 /// typedef typename MetaFun::template apply<T1, T2>::type type;
173 /// Here, "apply" is treated as a template name within the typename
174 /// specifier in the typedef. "apply" is a nested template, and can
175 /// only be understood in the context of
177 typedef llvm::PointerUnion4<TemplateDecl *,
178 UncommonTemplateNameStorage *,
179 QualifiedTemplateName *,
180 DependentTemplateName *> StorageType;
184 explicit TemplateName(void *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);
210 explicit TemplateName(OverloadedTemplateStorage *Storage);
211 explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
212 explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage);
213 explicit TemplateName(QualifiedTemplateName *Qual);
214 explicit TemplateName(DependentTemplateName *Dep);
216 /// \brief Determine whether this template name is NULL.
219 // \brief Get the kind of name that is actually stored.
220 NameKind getKind() const;
222 /// \brief Retrieve the underlying template declaration that
223 /// this template name refers to, if known.
225 /// \returns The template declaration that this template name refers
226 /// to, if any. If the template name does not refer to a specific
227 /// declaration because it is a dependent name, or if it refers to a
228 /// set of function templates, returns NULL.
229 TemplateDecl *getAsTemplateDecl() const;
231 /// \brief Retrieve the underlying, overloaded function template
232 // declarations that this template name refers to, if known.
234 /// \returns The set of overloaded function templates that this template
235 /// name refers to, if known. If the template name does not refer to a
236 /// specific set of function templates because it is a dependent name or
237 /// refers to a single template, returns NULL.
238 OverloadedTemplateStorage *getAsOverloadedTemplate() const;
240 /// \brief Retrieve the substituted template template parameter, if
243 /// \returns The storage for the substituted template template parameter,
244 /// if known. Otherwise, returns NULL.
245 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const;
247 /// \brief Retrieve the substituted template template parameter pack, if
250 /// \returns The storage for the substituted template template parameter pack,
251 /// if known. Otherwise, returns NULL.
252 SubstTemplateTemplateParmPackStorage *
253 getAsSubstTemplateTemplateParmPack() const;
255 /// \brief Retrieve the underlying qualified template name
256 /// structure, if any.
257 QualifiedTemplateName *getAsQualifiedTemplateName() const;
259 /// \brief Retrieve the underlying dependent template name
260 /// structure, if any.
261 DependentTemplateName *getAsDependentTemplateName() const;
263 TemplateName getUnderlying() const;
265 /// \brief Determines whether this is a dependent template name.
266 bool isDependent() const;
268 /// \brief Determines whether this is a template name that somehow
269 /// depends on a template parameter.
270 bool isInstantiationDependent() const;
272 /// \brief Determines whether this template name contains an
273 /// unexpanded parameter pack (for C++0x variadic templates).
274 bool containsUnexpandedParameterPack() const;
276 /// \brief Print the template name.
278 /// \param OS the output stream to which the template name will be
281 /// \param SuppressNNS if true, don't print the
282 /// nested-name-specifier that precedes the template name (if it has
284 void print(raw_ostream &OS, const PrintingPolicy &Policy,
285 bool SuppressNNS = false) const;
287 /// \brief Debugging aid that dumps the template name.
288 void dump(raw_ostream &OS) const;
290 /// \brief Debugging aid that dumps the template name to standard
294 void Profile(llvm::FoldingSetNodeID &ID) {
295 ID.AddPointer(Storage.getOpaqueValue());
298 /// \brief Retrieve the template name as a void pointer.
299 void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
301 /// \brief Build a template name from a void pointer.
302 static TemplateName getFromVoidPointer(void *Ptr) {
303 return TemplateName(Ptr);
307 /// Insertion operator for diagnostics. This allows sending TemplateName's
308 /// into a diagnostic with <<.
309 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
312 /// \brief A structure for storing the information associated with a
313 /// substituted template template parameter.
314 class SubstTemplateTemplateParmStorage
315 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
316 friend class ASTContext;
318 TemplateTemplateParmDecl *Parameter;
319 TemplateName Replacement;
321 SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
322 TemplateName replacement)
323 : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
324 Parameter(parameter), Replacement(replacement) {}
327 TemplateTemplateParmDecl *getParameter() const { return Parameter; }
328 TemplateName getReplacement() const { return Replacement; }
330 void Profile(llvm::FoldingSetNodeID &ID);
332 static void Profile(llvm::FoldingSetNodeID &ID,
333 TemplateTemplateParmDecl *parameter,
334 TemplateName replacement);
337 inline TemplateName TemplateName::getUnderlying() const {
338 if (SubstTemplateTemplateParmStorage *subst
339 = getAsSubstTemplateTemplateParm())
340 return subst->getReplacement().getUnderlying();
344 /// \brief Represents a template name that was expressed as a
347 /// This kind of template name refers to a template name that was
348 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
349 /// the nested name specifier is "std::" and the template name is the
350 /// declaration for "vector". The QualifiedTemplateName class is only
351 /// used to provide "sugar" for template names that were expressed
352 /// with a qualified name, and has no semantic meaning. In this
353 /// manner, it is to TemplateName what ElaboratedType is to Type,
354 /// providing extra syntactic sugar for downstream clients.
355 class QualifiedTemplateName : public llvm::FoldingSetNode {
356 /// \brief The nested name specifier that qualifies the template name.
358 /// The bit is used to indicate whether the "template" keyword was
359 /// present before the template name itself. Note that the
360 /// "template" keyword is always redundant in this case (otherwise,
361 /// the template name would be a dependent name and we would express
362 /// this name with DependentTemplateName).
363 llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
365 /// \brief The template declaration or set of overloaded function templates
366 /// that this qualified name refers to.
367 TemplateDecl *Template;
369 friend class ASTContext;
371 QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
372 TemplateDecl *Template)
373 : Qualifier(NNS, TemplateKeyword? 1 : 0),
374 Template(Template) { }
377 /// \brief Return the nested name specifier that qualifies this name.
378 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
380 /// \brief Whether the template name was prefixed by the "template"
382 bool hasTemplateKeyword() const { return Qualifier.getInt(); }
384 /// \brief The template declaration that this qualified name refers
386 TemplateDecl *getDecl() const { return Template; }
388 /// \brief The template declaration to which this qualified name
390 TemplateDecl *getTemplateDecl() const { return Template; }
392 void Profile(llvm::FoldingSetNodeID &ID) {
393 Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
396 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
397 bool TemplateKeyword, TemplateDecl *Template) {
399 ID.AddBoolean(TemplateKeyword);
400 ID.AddPointer(Template);
404 /// \brief Represents a dependent template name that cannot be
405 /// resolved prior to template instantiation.
407 /// This kind of template name refers to a dependent template name,
408 /// including its nested name specifier (if any). For example,
409 /// DependentTemplateName can refer to "MetaFun::template apply",
410 /// where "MetaFun::" is the nested name specifier and "apply" is the
411 /// template name referenced. The "template" keyword is implied.
412 class DependentTemplateName : public llvm::FoldingSetNode {
413 /// \brief The nested name specifier that qualifies the template
416 /// The bit stored in this qualifier describes whether the \c Name field
417 /// is interpreted as an IdentifierInfo pointer (when clear) or as an
418 /// overloaded operator kind (when set).
419 llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
421 /// \brief The dependent template name.
423 /// \brief The identifier template name.
425 /// Only valid when the bit on \c Qualifier is clear.
426 const IdentifierInfo *Identifier;
428 /// \brief The overloaded operator name.
430 /// Only valid when the bit on \c Qualifier is set.
431 OverloadedOperatorKind Operator;
434 /// \brief The canonical template name to which this dependent
435 /// template name refers.
437 /// The canonical template name for a dependent template name is
438 /// another dependent template name whose nested name specifier is
440 TemplateName CanonicalTemplateName;
442 friend class ASTContext;
444 DependentTemplateName(NestedNameSpecifier *Qualifier,
445 const IdentifierInfo *Identifier)
446 : Qualifier(Qualifier, false), Identifier(Identifier),
447 CanonicalTemplateName(this) { }
449 DependentTemplateName(NestedNameSpecifier *Qualifier,
450 const IdentifierInfo *Identifier,
452 : Qualifier(Qualifier, false), Identifier(Identifier),
453 CanonicalTemplateName(Canon) { }
455 DependentTemplateName(NestedNameSpecifier *Qualifier,
456 OverloadedOperatorKind Operator)
457 : Qualifier(Qualifier, true), Operator(Operator),
458 CanonicalTemplateName(this) { }
460 DependentTemplateName(NestedNameSpecifier *Qualifier,
461 OverloadedOperatorKind Operator,
463 : Qualifier(Qualifier, true), Operator(Operator),
464 CanonicalTemplateName(Canon) { }
467 /// \brief Return the nested name specifier that qualifies this name.
468 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
470 /// \brief Determine whether this template name refers to an identifier.
471 bool isIdentifier() const { return !Qualifier.getInt(); }
473 /// \brief Returns the identifier to which this template name refers.
474 const IdentifierInfo *getIdentifier() const {
475 assert(isIdentifier() && "Template name isn't an identifier?");
479 /// \brief Determine whether this template name refers to an overloaded
481 bool isOverloadedOperator() const { return Qualifier.getInt(); }
483 /// \brief Return the overloaded operator to which this template name refers.
484 OverloadedOperatorKind getOperator() const {
485 assert(isOverloadedOperator() &&
486 "Template name isn't an overloaded operator?");
490 void Profile(llvm::FoldingSetNodeID &ID) {
492 Profile(ID, getQualifier(), getIdentifier());
494 Profile(ID, getQualifier(), getOperator());
497 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
498 const IdentifierInfo *Identifier) {
500 ID.AddBoolean(false);
501 ID.AddPointer(Identifier);
504 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
505 OverloadedOperatorKind Operator) {
508 ID.AddInteger(Operator);
512 } // end namespace clang.
516 /// \brief The clang::TemplateName class is effectively a pointer.
518 class PointerLikeTypeTraits<clang::TemplateName> {
520 static inline void *getAsVoidPointer(clang::TemplateName TN) {
521 return TN.getAsVoidPointer();
524 static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
525 return clang::TemplateName::getFromVoidPointer(Ptr);
528 // No bits are available!
529 enum { NumLowBitsAvailable = 0 };
532 } // end namespace llvm.