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 "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "clang/Basic/OperatorKinds.h"
28 class DependentTemplateName;
29 class DiagnosticBuilder;
31 class NestedNameSpecifier;
32 class OverloadedTemplateStorage;
33 struct PrintingPolicy;
34 class QualifiedTemplateName;
36 class SubstTemplateTemplateParmPackStorage;
37 class TemplateArgument;
39 class TemplateTemplateParmDecl;
41 /// \brief Implementation class used to describe either a set of overloaded
42 /// template names or an already-substituted template template parameter pack.
43 class UncommonTemplateNameStorage {
47 /// \brief If true, this is an OverloadedTemplateStorage instance;
48 /// otherwise, it's a SubstTemplateTemplateParmPackStorage instance.
49 unsigned IsOverloadedStorage : 1;
51 /// \brief The number of stored templates or template arguments,
52 /// depending on which subclass we have.
56 void *PointerAlignment;
59 UncommonTemplateNameStorage(unsigned Size, bool OverloadedStorage) {
60 Bits.IsOverloadedStorage = OverloadedStorage;
65 unsigned size() const { return Bits.Size; }
67 OverloadedTemplateStorage *getAsOverloadedStorage() {
68 return Bits.IsOverloadedStorage
69 ? reinterpret_cast<OverloadedTemplateStorage *>(this)
73 SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
74 return Bits.IsOverloadedStorage
76 : reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this) ;
80 /// \brief A structure for storing the information associated with an
81 /// overloaded template name.
82 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
83 friend class ASTContext;
85 OverloadedTemplateStorage(unsigned Size)
86 : UncommonTemplateNameStorage(Size, true) { }
88 NamedDecl **getStorage() {
89 return reinterpret_cast<NamedDecl **>(this + 1);
91 NamedDecl * const *getStorage() const {
92 return reinterpret_cast<NamedDecl *const *>(this + 1);
96 typedef NamedDecl *const *iterator;
98 iterator begin() const { return getStorage(); }
99 iterator end() const { return getStorage() + size(); }
103 /// \brief A structure for storing an already-substituted template template
106 /// This kind of template names occurs when the parameter pack has been
107 /// provided with a template template argument pack in a context where its
108 /// enclosing pack expansion could not be fully expanded.
109 class SubstTemplateTemplateParmPackStorage
110 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
113 TemplateTemplateParmDecl *Parameter;
114 const TemplateArgument *Arguments;
117 SubstTemplateTemplateParmPackStorage(ASTContext &Context,
118 TemplateTemplateParmDecl *Parameter,
120 const TemplateArgument *Arguments)
121 : UncommonTemplateNameStorage(Size, false), Context(Context),
122 Parameter(Parameter), Arguments(Arguments) { }
124 /// \brief Retrieve the template template parameter pack being substituted.
125 TemplateTemplateParmDecl *getParameterPack() const {
129 /// \brief Retrieve the template template argument pack with which this
130 /// parameter was substituted.
131 TemplateArgument getArgumentPack() const;
133 void Profile(llvm::FoldingSetNodeID &ID);
135 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context,
136 TemplateTemplateParmDecl *Parameter,
137 const TemplateArgument &ArgPack);
140 /// \brief Represents a C++ template name within the type system.
142 /// A C++ template name refers to a template within the C++ type
143 /// system. In most cases, a template name is simply a reference to a
144 /// class template, e.g.
147 /// template<typename T> class X { };
152 /// Here, the 'X' in \c X<int> is a template name that refers to the
153 /// declaration of the class template X, above. Template names can
154 /// also refer to function templates, C++0x template aliases, etc.
156 /// Some template names are dependent. For example, consider:
159 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
160 /// typedef typename MetaFun::template apply<T1, T2>::type type;
164 /// Here, "apply" is treated as a template name within the typename
165 /// specifier in the typedef. "apply" is a nested template, and can
166 /// only be understood in the context of
168 typedef llvm::PointerUnion4<TemplateDecl *,
169 UncommonTemplateNameStorage *,
170 QualifiedTemplateName *,
171 DependentTemplateName *> StorageType;
175 explicit TemplateName(void *Ptr) {
176 Storage = StorageType::getFromOpaqueValue(Ptr);
180 // \brief Kind of name that is actually stored.
182 /// \brief A single template declaration.
184 /// \brief A set of overloaded template declarations.
186 /// \brief A qualified template name, where the qualification is kept
187 /// to describe the source code as written.
189 /// \brief A dependent template name that has not been resolved to a
190 /// template (or set of templates).
192 /// \brief A template template parameter pack that has been substituted for
193 /// a template template argument pack, but has not yet been expanded into
194 /// individual arguments.
195 SubstTemplateTemplateParmPack
198 TemplateName() : Storage() { }
199 explicit TemplateName(TemplateDecl *Template) : Storage(Template) { }
200 explicit TemplateName(OverloadedTemplateStorage *Storage)
201 : Storage(Storage) { }
202 explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage)
203 : Storage(Storage) { }
204 explicit TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) { }
205 explicit TemplateName(DependentTemplateName *Dep) : Storage(Dep) { }
207 /// \brief Determine whether this template name is NULL.
208 bool isNull() const { return Storage.isNull(); }
210 // \brief Get the kind of name that is actually stored.
211 NameKind getKind() const;
213 /// \brief Retrieve the underlying template declaration that
214 /// this template name refers to, if known.
216 /// \returns The template declaration that this template name refers
217 /// to, if any. If the template name does not refer to a specific
218 /// declaration because it is a dependent name, or if it refers to a
219 /// set of function templates, returns NULL.
220 TemplateDecl *getAsTemplateDecl() const;
222 /// \brief Retrieve the underlying, overloaded function template
223 // declarations that this template name refers to, if known.
225 /// \returns The set of overloaded function templates that this template
226 /// name refers to, if known. If the template name does not refer to a
227 /// specific set of function templates because it is a dependent name or
228 /// refers to a single template, returns NULL.
229 OverloadedTemplateStorage *getAsOverloadedTemplate() const {
230 if (UncommonTemplateNameStorage *Uncommon =
231 Storage.dyn_cast<UncommonTemplateNameStorage *>())
232 return Uncommon->getAsOverloadedStorage();
237 /// \brief Retrieve the substituted template template parameter pack, if
240 /// \returns The storage for the substituted template template parameter pack,
241 /// if known. Otherwise, returns NULL.
242 SubstTemplateTemplateParmPackStorage *
243 getAsSubstTemplateTemplateParmPack() const {
244 if (UncommonTemplateNameStorage *Uncommon =
245 Storage.dyn_cast<UncommonTemplateNameStorage *>())
246 return Uncommon->getAsSubstTemplateTemplateParmPack();
251 /// \brief Retrieve the underlying qualified template name
252 /// structure, if any.
253 QualifiedTemplateName *getAsQualifiedTemplateName() const {
254 return Storage.dyn_cast<QualifiedTemplateName *>();
257 /// \brief Retrieve the underlying dependent template name
258 /// structure, if any.
259 DependentTemplateName *getAsDependentTemplateName() const {
260 return Storage.dyn_cast<DependentTemplateName *>();
263 /// \brief Determines whether this is a dependent template name.
264 bool isDependent() const;
266 /// \brief Determines whether this template name contains an
267 /// unexpanded parameter pack (for C++0x variadic templates).
268 bool containsUnexpandedParameterPack() const;
270 /// \brief Print the template name.
272 /// \param OS the output stream to which the template name will be
275 /// \param SuppressNNS if true, don't print the
276 /// nested-name-specifier that precedes the template name (if it has
278 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy,
279 bool SuppressNNS = false) const;
281 /// \brief Debugging aid that dumps the template name to standard
285 void Profile(llvm::FoldingSetNodeID &ID) {
286 ID.AddPointer(Storage.getOpaqueValue());
289 /// \brief Retrieve the template name as a void pointer.
290 void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
292 /// \brief Build a template name from a void pointer.
293 static TemplateName getFromVoidPointer(void *Ptr) {
294 return TemplateName(Ptr);
298 /// Insertion operator for diagnostics. This allows sending TemplateName's
299 /// into a diagnostic with <<.
300 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
303 /// \brief Represents a template name that was expressed as a
306 /// This kind of template name refers to a template name that was
307 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
308 /// the nested name specifier is "std::" and the template name is the
309 /// declaration for "vector". The QualifiedTemplateName class is only
310 /// used to provide "sugar" for template names that were expressed
311 /// with a qualified name, and has no semantic meaning. In this
312 /// manner, it is to TemplateName what ElaboratedType is to Type,
313 /// providing extra syntactic sugar for downstream clients.
314 class QualifiedTemplateName : public llvm::FoldingSetNode {
315 /// \brief The nested name specifier that qualifies the template name.
317 /// The bit is used to indicate whether the "template" keyword was
318 /// present before the template name itself. Note that the
319 /// "template" keyword is always redundant in this case (otherwise,
320 /// the template name would be a dependent name and we would express
321 /// this name with DependentTemplateName).
322 llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
324 /// \brief The template declaration or set of overloaded function templates
325 /// that this qualified name refers to.
326 TemplateDecl *Template;
328 friend class ASTContext;
330 QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
331 TemplateDecl *Template)
332 : Qualifier(NNS, TemplateKeyword? 1 : 0),
333 Template(Template) { }
336 /// \brief Return the nested name specifier that qualifies this name.
337 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
339 /// \brief Whether the template name was prefixed by the "template"
341 bool hasTemplateKeyword() const { return Qualifier.getInt(); }
343 /// \brief The template declaration that this qualified name refers
345 TemplateDecl *getDecl() const { return Template; }
347 /// \brief The template declaration to which this qualified name
349 TemplateDecl *getTemplateDecl() const { return Template; }
351 void Profile(llvm::FoldingSetNodeID &ID) {
352 Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
355 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
356 bool TemplateKeyword, TemplateDecl *Template) {
358 ID.AddBoolean(TemplateKeyword);
359 ID.AddPointer(Template);
363 /// \brief Represents a dependent template name that cannot be
364 /// resolved prior to template instantiation.
366 /// This kind of template name refers to a dependent template name,
367 /// including its nested name specifier (if any). For example,
368 /// DependentTemplateName can refer to "MetaFun::template apply",
369 /// where "MetaFun::" is the nested name specifier and "apply" is the
370 /// template name referenced. The "template" keyword is implied.
371 class DependentTemplateName : public llvm::FoldingSetNode {
372 /// \brief The nested name specifier that qualifies the template
375 /// The bit stored in this qualifier describes whether the \c Name field
376 /// is interpreted as an IdentifierInfo pointer (when clear) or as an
377 /// overloaded operator kind (when set).
378 llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
380 /// \brief The dependent template name.
382 /// \brief The identifier template name.
384 /// Only valid when the bit on \c Qualifier is clear.
385 const IdentifierInfo *Identifier;
387 /// \brief The overloaded operator name.
389 /// Only valid when the bit on \c Qualifier is set.
390 OverloadedOperatorKind Operator;
393 /// \brief The canonical template name to which this dependent
394 /// template name refers.
396 /// The canonical template name for a dependent template name is
397 /// another dependent template name whose nested name specifier is
399 TemplateName CanonicalTemplateName;
401 friend class ASTContext;
403 DependentTemplateName(NestedNameSpecifier *Qualifier,
404 const IdentifierInfo *Identifier)
405 : Qualifier(Qualifier, false), Identifier(Identifier),
406 CanonicalTemplateName(this) { }
408 DependentTemplateName(NestedNameSpecifier *Qualifier,
409 const IdentifierInfo *Identifier,
411 : Qualifier(Qualifier, false), Identifier(Identifier),
412 CanonicalTemplateName(Canon) { }
414 DependentTemplateName(NestedNameSpecifier *Qualifier,
415 OverloadedOperatorKind Operator)
416 : Qualifier(Qualifier, true), Operator(Operator),
417 CanonicalTemplateName(this) { }
419 DependentTemplateName(NestedNameSpecifier *Qualifier,
420 OverloadedOperatorKind Operator,
422 : Qualifier(Qualifier, true), Operator(Operator),
423 CanonicalTemplateName(Canon) { }
426 /// \brief Return the nested name specifier that qualifies this name.
427 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
429 /// \brief Determine whether this template name refers to an identifier.
430 bool isIdentifier() const { return !Qualifier.getInt(); }
432 /// \brief Returns the identifier to which this template name refers.
433 const IdentifierInfo *getIdentifier() const {
434 assert(isIdentifier() && "Template name isn't an identifier?");
438 /// \brief Determine whether this template name refers to an overloaded
440 bool isOverloadedOperator() const { return Qualifier.getInt(); }
442 /// \brief Return the overloaded operator to which this template name refers.
443 OverloadedOperatorKind getOperator() const {
444 assert(isOverloadedOperator() &&
445 "Template name isn't an overloaded operator?");
449 void Profile(llvm::FoldingSetNodeID &ID) {
451 Profile(ID, getQualifier(), getIdentifier());
453 Profile(ID, getQualifier(), getOperator());
456 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
457 const IdentifierInfo *Identifier) {
459 ID.AddBoolean(false);
460 ID.AddPointer(Identifier);
463 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
464 OverloadedOperatorKind Operator) {
467 ID.AddInteger(Operator);
471 } // end namespace clang.
475 /// \brief The clang::TemplateName class is effectively a pointer.
477 class PointerLikeTypeTraits<clang::TemplateName> {
479 static inline void *getAsVoidPointer(clang::TemplateName TN) {
480 return TN.getAsVoidPointer();
483 static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
484 return clang::TemplateName::getFromVoidPointer(Ptr);
487 // No bits are available!
488 enum { NumLowBitsAvailable = 0 };
491 } // end namespace llvm.