]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/TemplateName.h
Update dialog to version 1.1-20110302.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / TemplateName.h
1 //===--- TemplateName.h - C++ Template Name Representation-------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the TemplateName interface and subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_TEMPLATENAME_H
15 #define LLVM_CLANG_AST_TEMPLATENAME_H
16
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "clang/Basic/OperatorKinds.h"
20
21 namespace llvm {
22   class raw_ostream;
23 }
24
25 namespace clang {
26   
27 class ASTContext;
28 class DependentTemplateName;
29 class DiagnosticBuilder;
30 class IdentifierInfo;
31 class NestedNameSpecifier;
32 class OverloadedTemplateStorage;
33 struct PrintingPolicy;
34 class QualifiedTemplateName;
35 class NamedDecl;
36 class SubstTemplateTemplateParmPackStorage;
37 class TemplateArgument;
38 class TemplateDecl;
39 class TemplateTemplateParmDecl;
40   
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 {
44 protected:
45   union {
46     struct {
47       /// \brief If true, this is an OverloadedTemplateStorage instance; 
48       /// otherwise, it's a SubstTemplateTemplateParmPackStorage instance.
49       unsigned IsOverloadedStorage : 1;
50       
51       /// \brief The number of stored templates or template arguments,
52       /// depending on which subclass we have.
53       unsigned Size : 31;
54     } Bits;
55     
56     void *PointerAlignment;
57   };
58   
59   UncommonTemplateNameStorage(unsigned Size, bool OverloadedStorage) {
60     Bits.IsOverloadedStorage = OverloadedStorage;
61     Bits.Size = Size;
62   }
63   
64 public:
65   unsigned size() const { return Bits.Size; }
66   
67   OverloadedTemplateStorage *getAsOverloadedStorage()  {
68     return Bits.IsOverloadedStorage
69              ? reinterpret_cast<OverloadedTemplateStorage *>(this) 
70              : 0;
71   }
72   
73   SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
74     return Bits.IsOverloadedStorage
75              ? 0
76              : reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this) ;
77   }
78 };
79   
80 /// \brief A structure for storing the information associated with an
81 /// overloaded template name.
82 class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
83   friend class ASTContext;
84
85   OverloadedTemplateStorage(unsigned Size) 
86     : UncommonTemplateNameStorage(Size, true) { }
87
88   NamedDecl **getStorage() {
89     return reinterpret_cast<NamedDecl **>(this + 1);
90   }
91   NamedDecl * const *getStorage() const {
92     return reinterpret_cast<NamedDecl *const *>(this + 1);
93   }
94
95 public:
96   typedef NamedDecl *const *iterator;
97
98   iterator begin() const { return getStorage(); }
99   iterator end() const { return getStorage() + size(); }
100 };
101   
102   
103 /// \brief A structure for storing an already-substituted template template
104 /// parameter pack.
105 ///
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
111 {
112   ASTContext &Context;
113   TemplateTemplateParmDecl *Parameter;
114   const TemplateArgument *Arguments;
115   
116 public:
117   SubstTemplateTemplateParmPackStorage(ASTContext &Context,
118                                        TemplateTemplateParmDecl *Parameter,
119                                        unsigned Size, 
120                                        const TemplateArgument *Arguments)
121     : UncommonTemplateNameStorage(Size, false), Context(Context),
122       Parameter(Parameter), Arguments(Arguments) { }
123   
124   /// \brief Retrieve the template template parameter pack being substituted.
125   TemplateTemplateParmDecl *getParameterPack() const {
126     return Parameter;
127   }
128   
129   /// \brief Retrieve the template template argument pack with which this
130   /// parameter was substituted.
131   TemplateArgument getArgumentPack() const;
132   
133   void Profile(llvm::FoldingSetNodeID &ID);
134   
135   static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context,
136                       TemplateTemplateParmDecl *Parameter,
137                       const TemplateArgument &ArgPack);
138 };
139
140 /// \brief Represents a C++ template name within the type system.
141 ///
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.
145 ///
146 /// \code
147 /// template<typename T> class X { };
148 ///
149 /// X<int> xi;
150 /// \endcode
151 ///
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.
155 ///
156 /// Some template names are dependent. For example, consider:
157 ///
158 /// \code
159 /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
160 ///   typedef typename MetaFun::template apply<T1, T2>::type type;
161 /// };
162 /// \endcode
163 ///
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
167 class TemplateName {
168   typedef llvm::PointerUnion4<TemplateDecl *,
169                               UncommonTemplateNameStorage *,
170                               QualifiedTemplateName *,
171                               DependentTemplateName *> StorageType;
172
173   StorageType Storage;
174
175   explicit TemplateName(void *Ptr) {
176     Storage = StorageType::getFromOpaqueValue(Ptr);
177   }
178
179 public:
180   // \brief Kind of name that is actually stored.
181   enum NameKind {
182     /// \brief A single template declaration.
183     Template,
184     /// \brief A set of overloaded template declarations.
185     OverloadedTemplate,
186     /// \brief A qualified template name, where the qualification is kept 
187     /// to describe the source code as written.
188     QualifiedTemplate,
189     /// \brief A dependent template name that has not been resolved to a 
190     /// template (or set of templates).
191     DependentTemplate,
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
196   };
197
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) { }
206
207   /// \brief Determine whether this template name is NULL.
208   bool isNull() const { return Storage.isNull(); }
209   
210   // \brief Get the kind of name that is actually stored.
211   NameKind getKind() const;
212
213   /// \brief Retrieve the underlying template declaration that
214   /// this template name refers to, if known.
215   ///
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;
221
222   /// \brief Retrieve the underlying, overloaded function template
223   // declarations that this template name refers to, if known.
224   ///
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();
233     
234     return 0;
235   }
236
237   /// \brief Retrieve the substituted template template parameter pack, if 
238   /// known.
239   ///
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();
247     
248     return 0;
249   }
250
251   /// \brief Retrieve the underlying qualified template name
252   /// structure, if any.
253   QualifiedTemplateName *getAsQualifiedTemplateName() const {
254     return Storage.dyn_cast<QualifiedTemplateName *>();
255   }
256
257   /// \brief Retrieve the underlying dependent template name
258   /// structure, if any.
259   DependentTemplateName *getAsDependentTemplateName() const {
260     return Storage.dyn_cast<DependentTemplateName *>();
261   }
262
263   /// \brief Determines whether this is a dependent template name.
264   bool isDependent() const;
265
266   /// \brief Determines whether this template name contains an
267   /// unexpanded parameter pack (for C++0x variadic templates).
268   bool containsUnexpandedParameterPack() const;
269
270   /// \brief Print the template name.
271   ///
272   /// \param OS the output stream to which the template name will be
273   /// printed.
274   ///
275   /// \param SuppressNNS if true, don't print the
276   /// nested-name-specifier that precedes the template name (if it has
277   /// one).
278   void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy,
279              bool SuppressNNS = false) const;
280
281   /// \brief Debugging aid that dumps the template name to standard
282   /// error.
283   void dump() const;
284
285   void Profile(llvm::FoldingSetNodeID &ID) {
286     ID.AddPointer(Storage.getOpaqueValue());
287   }
288
289   /// \brief Retrieve the template name as a void pointer.
290   void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
291
292   /// \brief Build a template name from a void pointer.
293   static TemplateName getFromVoidPointer(void *Ptr) {
294     return TemplateName(Ptr);
295   }
296 };
297
298 /// Insertion operator for diagnostics.  This allows sending TemplateName's
299 /// into a diagnostic with <<.
300 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
301                                     TemplateName N);
302
303 /// \brief Represents a template name that was expressed as a
304 /// qualified name.
305 ///
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.
316   ///
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;
323
324   /// \brief The template declaration or set of overloaded function templates
325   /// that this qualified name refers to.
326   TemplateDecl *Template;
327
328   friend class ASTContext;
329
330   QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
331                         TemplateDecl *Template)
332     : Qualifier(NNS, TemplateKeyword? 1 : 0),
333       Template(Template) { }
334
335 public:
336   /// \brief Return the nested name specifier that qualifies this name.
337   NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
338
339   /// \brief Whether the template name was prefixed by the "template"
340   /// keyword.
341   bool hasTemplateKeyword() const { return Qualifier.getInt(); }
342
343   /// \brief The template declaration that this qualified name refers
344   /// to.
345   TemplateDecl *getDecl() const { return Template; }
346
347   /// \brief The template declaration to which this qualified name
348   /// refers.
349   TemplateDecl *getTemplateDecl() const { return Template; }
350
351   void Profile(llvm::FoldingSetNodeID &ID) {
352     Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
353   }
354
355   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
356                       bool TemplateKeyword, TemplateDecl *Template) {
357     ID.AddPointer(NNS);
358     ID.AddBoolean(TemplateKeyword);
359     ID.AddPointer(Template);
360   }
361 };
362
363 /// \brief Represents a dependent template name that cannot be
364 /// resolved prior to template instantiation.
365 ///
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
373   /// name.
374   ///
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;
379
380   /// \brief The dependent template name.
381   union {
382     /// \brief The identifier template name.
383     ///
384     /// Only valid when the bit on \c Qualifier is clear.
385     const IdentifierInfo *Identifier;
386     
387     /// \brief The overloaded operator name.
388     ///
389     /// Only valid when the bit on \c Qualifier is set.
390     OverloadedOperatorKind Operator;
391   };
392
393   /// \brief The canonical template name to which this dependent
394   /// template name refers.
395   ///
396   /// The canonical template name for a dependent template name is
397   /// another dependent template name whose nested name specifier is
398   /// canonical.
399   TemplateName CanonicalTemplateName;
400
401   friend class ASTContext;
402
403   DependentTemplateName(NestedNameSpecifier *Qualifier,
404                         const IdentifierInfo *Identifier)
405     : Qualifier(Qualifier, false), Identifier(Identifier), 
406       CanonicalTemplateName(this) { }
407
408   DependentTemplateName(NestedNameSpecifier *Qualifier,
409                         const IdentifierInfo *Identifier,
410                         TemplateName Canon)
411     : Qualifier(Qualifier, false), Identifier(Identifier), 
412       CanonicalTemplateName(Canon) { }
413
414   DependentTemplateName(NestedNameSpecifier *Qualifier,
415                         OverloadedOperatorKind Operator)
416   : Qualifier(Qualifier, true), Operator(Operator), 
417     CanonicalTemplateName(this) { }
418   
419   DependentTemplateName(NestedNameSpecifier *Qualifier,
420                         OverloadedOperatorKind Operator,
421                         TemplateName Canon)
422   : Qualifier(Qualifier, true), Operator(Operator), 
423     CanonicalTemplateName(Canon) { }
424   
425 public:
426   /// \brief Return the nested name specifier that qualifies this name.
427   NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
428
429   /// \brief Determine whether this template name refers to an identifier.
430   bool isIdentifier() const { return !Qualifier.getInt(); }
431
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?");
435     return Identifier;
436   }
437   
438   /// \brief Determine whether this template name refers to an overloaded
439   /// operator.
440   bool isOverloadedOperator() const { return Qualifier.getInt(); }
441   
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?");
446     return Operator; 
447   }
448   
449   void Profile(llvm::FoldingSetNodeID &ID) {
450     if (isIdentifier())
451       Profile(ID, getQualifier(), getIdentifier());
452     else
453       Profile(ID, getQualifier(), getOperator());
454   }
455
456   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
457                       const IdentifierInfo *Identifier) {
458     ID.AddPointer(NNS);
459     ID.AddBoolean(false);
460     ID.AddPointer(Identifier);
461   }
462
463   static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
464                       OverloadedOperatorKind Operator) {
465     ID.AddPointer(NNS);
466     ID.AddBoolean(true);
467     ID.AddInteger(Operator);
468   }
469 };
470
471 } // end namespace clang.
472
473 namespace llvm {
474
475 /// \brief The clang::TemplateName class is effectively a pointer.
476 template<>
477 class PointerLikeTypeTraits<clang::TemplateName> {
478 public:
479   static inline void *getAsVoidPointer(clang::TemplateName TN) {
480     return TN.getAsVoidPointer();
481   }
482
483   static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
484     return clang::TemplateName::getFromVoidPointer(Ptr);
485   }
486
487   // No bits are available!
488   enum { NumLowBitsAvailable = 0 };
489 };
490
491 } // end namespace llvm.
492
493 #endif