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"
27 class DependentTemplateName;
28 class DiagnosticBuilder;
30 class NestedNameSpecifier;
31 struct PrintingPolicy;
32 class QualifiedTemplateName;
36 /// \brief A structure for storing the information associated with an
37 /// overloaded template name.
38 class OverloadedTemplateStorage {
41 NamedDecl *Storage[1];
44 friend class ASTContext;
46 OverloadedTemplateStorage(unsigned Size) : Size(Size) {}
48 NamedDecl **getStorage() {
51 NamedDecl * const *getStorage() const {
56 typedef NamedDecl *const *iterator;
58 unsigned size() const { return Size; }
60 iterator begin() const { return getStorage(); }
61 iterator end() const { return getStorage() + size(); }
64 /// \brief Represents a C++ template name within the type system.
66 /// A C++ template name refers to a template within the C++ type
67 /// system. In most cases, a template name is simply a reference to a
68 /// class template, e.g.
71 /// template<typename T> class X { };
76 /// Here, the 'X' in \c X<int> is a template name that refers to the
77 /// declaration of the class template X, above. Template names can
78 /// also refer to function templates, C++0x template aliases, etc.
80 /// Some template names are dependent. For example, consider:
83 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
84 /// typedef typename MetaFun::template apply<T1, T2>::type type;
88 /// Here, "apply" is treated as a template name within the typename
89 /// specifier in the typedef. "apply" is a nested template, and can
90 /// only be understood in the context of
92 typedef llvm::PointerUnion4<TemplateDecl *,
93 OverloadedTemplateStorage *,
94 QualifiedTemplateName *,
95 DependentTemplateName *> StorageType;
99 explicit TemplateName(void *Ptr) {
100 Storage = StorageType::getFromOpaqueValue(Ptr);
104 // \brief Kind of name that is actually stored.
112 TemplateName() : Storage() { }
113 explicit TemplateName(TemplateDecl *Template) : Storage(Template) { }
114 explicit TemplateName(OverloadedTemplateStorage *Storage)
115 : Storage(Storage) { }
116 explicit TemplateName(QualifiedTemplateName *Qual) : Storage(Qual) { }
117 explicit TemplateName(DependentTemplateName *Dep) : Storage(Dep) { }
119 /// \brief Determine whether this template name is NULL.
120 bool isNull() const { return Storage.isNull(); }
122 // \brief Get the kind of name that is actually stored.
123 NameKind getKind() const;
125 /// \brief Retrieve the the underlying template declaration that
126 /// this template name refers to, if known.
128 /// \returns The template declaration that this template name refers
129 /// to, if any. If the template name does not refer to a specific
130 /// declaration because it is a dependent name, or if it refers to a
131 /// set of function templates, returns NULL.
132 TemplateDecl *getAsTemplateDecl() const;
134 /// \brief Retrieve the the underlying, overloaded function template
135 // declarations that this template name refers to, if known.
137 /// \returns The set of overloaded function templates that this template
138 /// name refers to, if known. If the template name does not refer to a
139 /// specific set of function templates because it is a dependent name or
140 /// refers to a single template, returns NULL.
141 OverloadedTemplateStorage *getAsOverloadedTemplate() const {
142 return Storage.dyn_cast<OverloadedTemplateStorage *>();
145 /// \brief Retrieve the underlying qualified template name
146 /// structure, if any.
147 QualifiedTemplateName *getAsQualifiedTemplateName() const {
148 return Storage.dyn_cast<QualifiedTemplateName *>();
151 /// \brief Retrieve the underlying dependent template name
152 /// structure, if any.
153 DependentTemplateName *getAsDependentTemplateName() const {
154 return Storage.dyn_cast<DependentTemplateName *>();
157 /// \brief Determines whether this is a dependent template name.
158 bool isDependent() const;
160 /// \brief Print the template name.
162 /// \param OS the output stream to which the template name will be
165 /// \param SuppressNNS if true, don't print the
166 /// nested-name-specifier that precedes the template name (if it has
168 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy,
169 bool SuppressNNS = false) const;
171 /// \brief Debugging aid that dumps the template name to standard
175 void Profile(llvm::FoldingSetNodeID &ID) {
176 ID.AddPointer(Storage.getOpaqueValue());
179 /// \brief Retrieve the template name as a void pointer.
180 void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
182 /// \brief Build a template name from a void pointer.
183 static TemplateName getFromVoidPointer(void *Ptr) {
184 return TemplateName(Ptr);
188 /// Insertion operator for diagnostics. This allows sending TemplateName's
189 /// into a diagnostic with <<.
190 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
193 /// \brief Represents a template name that was expressed as a
196 /// This kind of template name refers to a template name that was
197 /// preceded by a nested name specifier, e.g., \c std::vector. Here,
198 /// the nested name specifier is "std::" and the template name is the
199 /// declaration for "vector". The QualifiedTemplateName class is only
200 /// used to provide "sugar" for template names that were expressed
201 /// with a qualified name, and has no semantic meaning. In this
202 /// manner, it is to TemplateName what ElaboratedType is to Type,
203 /// providing extra syntactic sugar for downstream clients.
204 class QualifiedTemplateName : public llvm::FoldingSetNode {
205 /// \brief The nested name specifier that qualifies the template name.
207 /// The bit is used to indicate whether the "template" keyword was
208 /// present before the template name itself. Note that the
209 /// "template" keyword is always redundant in this case (otherwise,
210 /// the template name would be a dependent name and we would express
211 /// this name with DependentTemplateName).
212 llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
214 /// \brief The template declaration or set of overloaded function templates
215 /// that this qualified name refers to.
216 TemplateDecl *Template;
218 friend class ASTContext;
220 QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
221 TemplateDecl *Template)
222 : Qualifier(NNS, TemplateKeyword? 1 : 0),
223 Template(Template) { }
226 /// \brief Return the nested name specifier that qualifies this name.
227 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
229 /// \brief Whether the template name was prefixed by the "template"
231 bool hasTemplateKeyword() const { return Qualifier.getInt(); }
233 /// \brief The template declaration that this qualified name refers
235 TemplateDecl *getDecl() const { return Template; }
237 /// \brief The template declaration to which this qualified name
239 TemplateDecl *getTemplateDecl() const { return Template; }
241 void Profile(llvm::FoldingSetNodeID &ID) {
242 Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
245 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
246 bool TemplateKeyword, TemplateDecl *Template) {
248 ID.AddBoolean(TemplateKeyword);
249 ID.AddPointer(Template);
253 /// \brief Represents a dependent template name that cannot be
254 /// resolved prior to template instantiation.
256 /// This kind of template name refers to a dependent template name,
257 /// including its nested name specifier (if any). For example,
258 /// DependentTemplateName can refer to "MetaFun::template apply",
259 /// where "MetaFun::" is the nested name specifier and "apply" is the
260 /// template name referenced. The "template" keyword is implied.
261 class DependentTemplateName : public llvm::FoldingSetNode {
262 /// \brief The nested name specifier that qualifies the template
265 /// The bit stored in this qualifier describes whether the \c Name field
266 /// is interpreted as an IdentifierInfo pointer (when clear) or as an
267 /// overloaded operator kind (when set).
268 llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
270 /// \brief The dependent template name.
272 /// \brief The identifier template name.
274 /// Only valid when the bit on \c Qualifier is clear.
275 const IdentifierInfo *Identifier;
277 /// \brief The overloaded operator name.
279 /// Only valid when the bit on \c Qualifier is set.
280 OverloadedOperatorKind Operator;
283 /// \brief The canonical template name to which this dependent
284 /// template name refers.
286 /// The canonical template name for a dependent template name is
287 /// another dependent template name whose nested name specifier is
289 TemplateName CanonicalTemplateName;
291 friend class ASTContext;
293 DependentTemplateName(NestedNameSpecifier *Qualifier,
294 const IdentifierInfo *Identifier)
295 : Qualifier(Qualifier, false), Identifier(Identifier),
296 CanonicalTemplateName(this) { }
298 DependentTemplateName(NestedNameSpecifier *Qualifier,
299 const IdentifierInfo *Identifier,
301 : Qualifier(Qualifier, false), Identifier(Identifier),
302 CanonicalTemplateName(Canon) { }
304 DependentTemplateName(NestedNameSpecifier *Qualifier,
305 OverloadedOperatorKind Operator)
306 : Qualifier(Qualifier, true), Operator(Operator),
307 CanonicalTemplateName(this) { }
309 DependentTemplateName(NestedNameSpecifier *Qualifier,
310 OverloadedOperatorKind Operator,
312 : Qualifier(Qualifier, true), Operator(Operator),
313 CanonicalTemplateName(Canon) { }
316 /// \brief Return the nested name specifier that qualifies this name.
317 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
319 /// \brief Determine whether this template name refers to an identifier.
320 bool isIdentifier() const { return !Qualifier.getInt(); }
322 /// \brief Returns the identifier to which this template name refers.
323 const IdentifierInfo *getIdentifier() const {
324 assert(isIdentifier() && "Template name isn't an identifier?");
328 /// \brief Determine whether this template name refers to an overloaded
330 bool isOverloadedOperator() const { return Qualifier.getInt(); }
332 /// \brief Return the overloaded operator to which this template name refers.
333 OverloadedOperatorKind getOperator() const {
334 assert(isOverloadedOperator() &&
335 "Template name isn't an overloaded operator?");
339 void Profile(llvm::FoldingSetNodeID &ID) {
341 Profile(ID, getQualifier(), getIdentifier());
343 Profile(ID, getQualifier(), getOperator());
346 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
347 const IdentifierInfo *Identifier) {
349 ID.AddBoolean(false);
350 ID.AddPointer(Identifier);
353 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
354 OverloadedOperatorKind Operator) {
357 ID.AddInteger(Operator);
361 } // end namespace clang.
365 /// \brief The clang::TemplateName class is effectively a pointer.
367 class PointerLikeTypeTraits<clang::TemplateName> {
369 static inline void *getAsVoidPointer(clang::TemplateName TN) {
370 return TN.getAsVoidPointer();
373 static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
374 return clang::TemplateName::getFromVoidPointer(Ptr);
377 // No bits are available!
378 enum { NumLowBitsAvailable = 0 };
381 } // end namespace llvm.