]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/include/clang/AST/DeclarationName.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / include / clang / AST / DeclarationName.h
1 //===-- DeclarationName.h - Representation of declaration names -*- 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 declares the DeclarationName and DeclarationNameTable classes.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
14 #define LLVM_CLANG_AST_DECLARATIONNAME_H
15
16 #include "clang/Basic/IdentifierTable.h"
17 #include "clang/AST/Type.h"
18 #include "clang/AST/CanonicalType.h"
19 #include "clang/Basic/PartialDiagnostic.h"
20
21 namespace llvm {
22   template <typename T> struct DenseMapInfo;
23 }
24
25 namespace clang {
26   class CXXSpecialName;
27   class CXXOperatorIdName;
28   class CXXLiteralOperatorIdName;
29   class DeclarationNameExtra;
30   class IdentifierInfo;
31   class MultiKeywordSelector;
32   class UsingDirectiveDecl;
33   class TypeSourceInfo;
34
35 /// DeclarationName - The name of a declaration. In the common case,
36 /// this just stores an IdentifierInfo pointer to a normal
37 /// name. However, it also provides encodings for Objective-C
38 /// selectors (optimizing zero- and one-argument selectors, which make
39 /// up 78% percent of all selectors in Cocoa.h) and special C++ names
40 /// for constructors, destructors, and conversion functions.
41 class DeclarationName {
42 public:
43   /// NameKind - The kind of name this object contains.
44   enum NameKind {
45     Identifier,
46     ObjCZeroArgSelector,
47     ObjCOneArgSelector,
48     ObjCMultiArgSelector,
49     CXXConstructorName,
50     CXXDestructorName,
51     CXXConversionFunctionName,
52     CXXOperatorName,
53     CXXLiteralOperatorName,
54     CXXUsingDirective
55   };
56
57 private:
58   /// StoredNameKind - The kind of name that is actually stored in the
59   /// upper bits of the Ptr field. This is only used internally.
60   enum StoredNameKind {
61     StoredIdentifier = 0,
62     StoredObjCZeroArgSelector,
63     StoredObjCOneArgSelector,
64     StoredDeclarationNameExtra,
65     PtrMask = 0x03
66   };
67
68   /// Ptr - The lowest two bits are used to express what kind of name
69   /// we're actually storing, using the values of NameKind. Depending
70   /// on the kind of name this is, the upper bits of Ptr may have one
71   /// of several different meanings:
72   ///
73   ///   StoredIdentifier - The name is a normal identifier, and Ptr is
74   ///   a normal IdentifierInfo pointer.
75   ///
76   ///   StoredObjCZeroArgSelector - The name is an Objective-C
77   ///   selector with zero arguments, and Ptr is an IdentifierInfo
78   ///   pointer pointing to the selector name.
79   ///
80   ///   StoredObjCOneArgSelector - The name is an Objective-C selector
81   ///   with one argument, and Ptr is an IdentifierInfo pointer
82   ///   pointing to the selector name.
83   ///
84   ///   StoredDeclarationNameExtra - Ptr is actually a pointer to a
85   ///   DeclarationNameExtra structure, whose first value will tell us
86   ///   whether this is an Objective-C selector, C++ operator-id name,
87   ///   or special C++ name.
88   uintptr_t Ptr;
89
90   /// getStoredNameKind - Return the kind of object that is stored in
91   /// Ptr.
92   StoredNameKind getStoredNameKind() const {
93     return static_cast<StoredNameKind>(Ptr & PtrMask);
94   }
95
96   /// getExtra - Get the "extra" information associated with this
97   /// multi-argument selector or C++ special name.
98   DeclarationNameExtra *getExtra() const {
99     assert(getStoredNameKind() == StoredDeclarationNameExtra &&
100            "Declaration name does not store an Extra structure");
101     return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
102   }
103
104   /// getAsCXXSpecialName - If the stored pointer is actually a
105   /// CXXSpecialName, returns a pointer to it. Otherwise, returns
106   /// a NULL pointer.
107   CXXSpecialName *getAsCXXSpecialName() const {
108     if (getNameKind() >= CXXConstructorName &&
109         getNameKind() <= CXXConversionFunctionName)
110       return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
111     return 0;
112   }
113
114   /// getAsCXXOperatorIdName
115   CXXOperatorIdName *getAsCXXOperatorIdName() const {
116     if (getNameKind() == CXXOperatorName)
117       return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
118     return 0;
119   }
120
121   CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
122     if (getNameKind() == CXXLiteralOperatorName)
123       return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
124     return 0;
125   }
126
127   // Construct a declaration name from the name of a C++ constructor,
128   // destructor, or conversion function.
129   DeclarationName(CXXSpecialName *Name)
130     : Ptr(reinterpret_cast<uintptr_t>(Name)) {
131     assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
132     Ptr |= StoredDeclarationNameExtra;
133   }
134
135   // Construct a declaration name from the name of a C++ overloaded
136   // operator.
137   DeclarationName(CXXOperatorIdName *Name)
138     : Ptr(reinterpret_cast<uintptr_t>(Name)) {
139     assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
140     Ptr |= StoredDeclarationNameExtra;
141   }
142
143   DeclarationName(CXXLiteralOperatorIdName *Name)
144     : Ptr(reinterpret_cast<uintptr_t>(Name)) {
145     assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
146     Ptr |= StoredDeclarationNameExtra;
147   }
148
149   /// Construct a declaration name from a raw pointer.
150   DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
151
152   friend class DeclarationNameTable;
153   friend class NamedDecl;
154
155   /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer
156   /// for this name as a void pointer.
157   void *getFETokenInfoAsVoid() const;
158
159 public:
160   /// DeclarationName - Used to create an empty selector.
161   DeclarationName() : Ptr(0) { }
162
163   // Construct a declaration name from an IdentifierInfo *.
164   DeclarationName(const IdentifierInfo *II)
165     : Ptr(reinterpret_cast<uintptr_t>(II)) {
166     assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
167   }
168
169   // Construct a declaration name from an Objective-C selector.
170   DeclarationName(Selector Sel);
171
172   /// getUsingDirectiveName - Return name for all using-directives.
173   static DeclarationName getUsingDirectiveName();
174
175   // operator bool() - Evaluates true when this declaration name is
176   // non-empty.
177   operator bool() const {
178     return ((Ptr & PtrMask) != 0) ||
179            (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
180   }
181
182   /// Predicate functions for querying what type of name this is.
183   bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
184   bool isObjCZeroArgSelector() const {
185     return getStoredNameKind() == StoredObjCZeroArgSelector;
186   }
187   bool isObjCOneArgSelector() const {
188     return getStoredNameKind() == StoredObjCOneArgSelector;
189   }
190
191   /// getNameKind - Determine what kind of name this is.
192   NameKind getNameKind() const;
193
194   /// \brief Determines whether the name itself is dependent, e.g., because it 
195   /// involves a C++ type that is itself dependent.
196   ///
197   /// Note that this does not capture all of the notions of "dependent name",
198   /// because an identifier can be a dependent name if it is used as the 
199   /// callee in a call expression with dependent arguments.
200   bool isDependentName() const;
201   
202   /// getNameAsString - Retrieve the human-readable string for this name.
203   std::string getAsString() const;
204
205   /// printName - Print the human-readable name to a stream.
206   void printName(raw_ostream &OS) const;
207
208   /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
209   /// this declaration name, or NULL if this declaration name isn't a
210   /// simple identifier.
211   IdentifierInfo *getAsIdentifierInfo() const {
212     if (isIdentifier())
213       return reinterpret_cast<IdentifierInfo *>(Ptr);
214     return 0;
215   }
216
217   /// getAsOpaqueInteger - Get the representation of this declaration
218   /// name as an opaque integer.
219   uintptr_t getAsOpaqueInteger() const { return Ptr; }
220
221   /// getAsOpaquePtr - Get the representation of this declaration name as
222   /// an opaque pointer.
223   void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
224
225   static DeclarationName getFromOpaquePtr(void *P) {
226     DeclarationName N;
227     N.Ptr = reinterpret_cast<uintptr_t> (P);
228     return N;
229   }
230
231   static DeclarationName getFromOpaqueInteger(uintptr_t P) {
232     DeclarationName N;
233     N.Ptr = P;
234     return N;
235   }
236
237   /// getCXXNameType - If this name is one of the C++ names (of a
238   /// constructor, destructor, or conversion function), return the
239   /// type associated with that name.
240   QualType getCXXNameType() const;
241
242   /// getCXXOverloadedOperator - If this name is the name of an
243   /// overloadable operator in C++ (e.g., @c operator+), retrieve the
244   /// kind of overloaded operator.
245   OverloadedOperatorKind getCXXOverloadedOperator() const;
246
247   /// getCXXLiteralIdentifier - If this name is the name of a literal
248   /// operator, retrieve the identifier associated with it.
249   IdentifierInfo *getCXXLiteralIdentifier() const;
250
251   /// getObjCSelector - Get the Objective-C selector stored in this
252   /// declaration name.
253   Selector getObjCSelector() const;
254
255   /// getFETokenInfo/setFETokenInfo - The language front-end is
256   /// allowed to associate arbitrary metadata with some kinds of
257   /// declaration names, including normal identifiers and C++
258   /// constructors, destructors, and conversion functions.
259   template<typename T>
260   T *getFETokenInfo() const { return static_cast<T*>(getFETokenInfoAsVoid()); }
261
262   void setFETokenInfo(void *T);
263
264   /// operator== - Determine whether the specified names are identical..
265   friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
266     return LHS.Ptr == RHS.Ptr;
267   }
268
269   /// operator!= - Determine whether the specified names are different.
270   friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
271     return LHS.Ptr != RHS.Ptr;
272   }
273
274   static DeclarationName getEmptyMarker() {
275     return DeclarationName(uintptr_t(-1));
276   }
277
278   static DeclarationName getTombstoneMarker() {
279     return DeclarationName(uintptr_t(-2));
280   }
281
282   static int compare(DeclarationName LHS, DeclarationName RHS);
283   
284   void dump() const;
285 };
286
287 /// Ordering on two declaration names. If both names are identifiers,
288 /// this provides a lexicographical ordering.
289 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
290   return DeclarationName::compare(LHS, RHS) < 0;
291 }
292
293 /// Ordering on two declaration names. If both names are identifiers,
294 /// this provides a lexicographical ordering.
295 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
296   return DeclarationName::compare(LHS, RHS) > 0;
297 }
298
299 /// Ordering on two declaration names. If both names are identifiers,
300 /// this provides a lexicographical ordering.
301 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
302   return DeclarationName::compare(LHS, RHS) <= 0;
303 }
304
305 /// Ordering on two declaration names. If both names are identifiers,
306 /// this provides a lexicographical ordering.
307 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
308   return DeclarationName::compare(LHS, RHS) >= 0;
309 }
310
311 /// DeclarationNameTable - Used to store and retrieve DeclarationName
312 /// instances for the various kinds of declaration names, e.g., normal
313 /// identifiers, C++ constructor names, etc. This class contains
314 /// uniqued versions of each of the C++ special names, which can be
315 /// retrieved using its member functions (e.g.,
316 /// getCXXConstructorName).
317 class DeclarationNameTable {
318   const ASTContext &Ctx;
319   void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
320   CXXOperatorIdName *CXXOperatorNames; // Operator names
321   void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
322
323   DeclarationNameTable(const DeclarationNameTable&);            // NONCOPYABLE
324   DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE
325
326 public:
327   DeclarationNameTable(const ASTContext &C);
328   ~DeclarationNameTable();
329
330   /// getIdentifier - Create a declaration name that is a simple
331   /// identifier.
332   DeclarationName getIdentifier(const IdentifierInfo *ID) {
333     return DeclarationName(ID);
334   }
335
336   /// getCXXConstructorName - Returns the name of a C++ constructor
337   /// for the given Type.
338   DeclarationName getCXXConstructorName(CanQualType Ty) {
339     return getCXXSpecialName(DeclarationName::CXXConstructorName, 
340                              Ty.getUnqualifiedType());
341   }
342
343   /// getCXXDestructorName - Returns the name of a C++ destructor
344   /// for the given Type.
345   DeclarationName getCXXDestructorName(CanQualType Ty) {
346     return getCXXSpecialName(DeclarationName::CXXDestructorName, 
347                              Ty.getUnqualifiedType());
348   }
349
350   /// getCXXConversionFunctionName - Returns the name of a C++
351   /// conversion function for the given Type.
352   DeclarationName getCXXConversionFunctionName(CanQualType Ty) {
353     return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
354   }
355
356   /// getCXXSpecialName - Returns a declaration name for special kind
357   /// of C++ name, e.g., for a constructor, destructor, or conversion
358   /// function.
359   DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
360                                     CanQualType Ty);
361
362   /// getCXXOperatorName - Get the name of the overloadable C++
363   /// operator corresponding to Op.
364   DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
365
366   /// getCXXLiteralOperatorName - Get the name of the literal operator function
367   /// with II as the identifier.
368   DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
369 };
370
371 /// DeclarationNameLoc - Additional source/type location info
372 /// for a declaration name. Needs a DeclarationName in order
373 /// to be interpreted correctly.
374 struct DeclarationNameLoc {
375   union {
376     // The source location for identifier stored elsewhere.
377     // struct {} Identifier;
378
379     // Type info for constructors, destructors and conversion functions.
380     // Locations (if any) for the tilde (destructor) or operator keyword
381     // (conversion) are stored elsewhere.
382     struct {
383       TypeSourceInfo* TInfo;
384     } NamedType;
385
386     // The location (if any) of the operator keyword is stored elsewhere.
387     struct {
388       unsigned BeginOpNameLoc;
389       unsigned EndOpNameLoc;
390     } CXXOperatorName;
391
392     // The location (if any) of the operator keyword is stored elsewhere.
393     struct {
394       unsigned OpNameLoc;
395     } CXXLiteralOperatorName;
396
397     // struct {} CXXUsingDirective;
398     // struct {} ObjCZeroArgSelector;
399     // struct {} ObjCOneArgSelector;
400     // struct {} ObjCMultiArgSelector;
401   };
402
403   DeclarationNameLoc(DeclarationName Name);
404   // FIXME: this should go away once all DNLocs are properly initialized.
405   DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
406 }; // struct DeclarationNameLoc
407
408
409 /// DeclarationNameInfo - A collector data type for bundling together
410 /// a DeclarationName and the correspnding source/type location info.
411 struct DeclarationNameInfo {
412 private:
413   /// Name - The declaration name, also encoding name kind.
414   DeclarationName Name;
415   /// Loc - The main source location for the declaration name.
416   SourceLocation NameLoc;
417   /// Info - Further source/type location info for special kinds of names.
418   DeclarationNameLoc LocInfo;
419
420 public:
421   // FIXME: remove it.
422   DeclarationNameInfo() {}
423
424   DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
425     : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
426
427   DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
428                       DeclarationNameLoc LocInfo)
429     : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
430
431   /// getName - Returns the embedded declaration name.
432   DeclarationName getName() const { return Name; }
433   /// setName - Sets the embedded declaration name.
434   void setName(DeclarationName N) { Name = N; }
435
436   /// getLoc - Returns the main location of the declaration name.
437   SourceLocation getLoc() const { return NameLoc; }
438   /// setLoc - Sets the main location of the declaration name.
439   void setLoc(SourceLocation L) { NameLoc = L; }
440
441   const DeclarationNameLoc &getInfo() const { return LocInfo; }
442   DeclarationNameLoc &getInfo() { return LocInfo; }
443   void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
444
445   /// getNamedTypeInfo - Returns the source type info associated to
446   /// the name. Assumes it is a constructor, destructor or conversion.
447   TypeSourceInfo *getNamedTypeInfo() const {
448     assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
449            Name.getNameKind() == DeclarationName::CXXDestructorName ||
450            Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
451     return LocInfo.NamedType.TInfo;
452   }
453   /// setNamedTypeInfo - Sets the source type info associated to
454   /// the name. Assumes it is a constructor, destructor or conversion.
455   void setNamedTypeInfo(TypeSourceInfo *TInfo) {
456     assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
457            Name.getNameKind() == DeclarationName::CXXDestructorName ||
458            Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
459     LocInfo.NamedType.TInfo = TInfo;
460   }
461
462   /// getCXXOperatorNameRange - Gets the range of the operator name
463   /// (without the operator keyword). Assumes it is a (non-literal) operator.
464   SourceRange getCXXOperatorNameRange() const {
465     assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
466     return SourceRange(
467      SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
468      SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
469                        );
470   }
471   /// setCXXOperatorNameRange - Sets the range of the operator name
472   /// (without the operator keyword). Assumes it is a C++ operator.
473   void setCXXOperatorNameRange(SourceRange R) {
474     assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
475     LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
476     LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
477   }
478
479   /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
480   /// operator name (not the operator keyword).
481   /// Assumes it is a literal operator.
482   SourceLocation getCXXLiteralOperatorNameLoc() const {
483     assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
484     return SourceLocation::
485       getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
486   }
487   /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
488   /// operator name (not the operator keyword).
489   /// Assumes it is a literal operator.
490   void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
491     assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
492     LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
493   }
494
495   /// \brief Determine whether this name involves a template parameter.
496   bool isInstantiationDependent() const;
497   
498   /// \brief Determine whether this name contains an unexpanded
499   /// parameter pack.
500   bool containsUnexpandedParameterPack() const;
501
502   /// getAsString - Retrieve the human-readable string for this name.
503   std::string getAsString() const;
504
505   /// printName - Print the human-readable name to a stream.
506   void printName(raw_ostream &OS) const;
507
508   /// getBeginLoc - Retrieve the location of the first token.
509   SourceLocation getBeginLoc() const { return NameLoc; }
510   /// getEndLoc - Retrieve the location of the last token.
511   SourceLocation getEndLoc() const;
512   /// getSourceRange - The range of the declaration name.
513   SourceRange getSourceRange() const {
514     return SourceRange(getBeginLoc(), getEndLoc());
515   }
516 };
517
518 /// Insertion operator for diagnostics.  This allows sending DeclarationName's
519 /// into a diagnostic with <<.
520 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
521                                            DeclarationName N) {
522   DB.AddTaggedVal(N.getAsOpaqueInteger(),
523                   DiagnosticsEngine::ak_declarationname);
524   return DB;
525 }
526
527 /// Insertion operator for partial diagnostics.  This allows binding
528 /// DeclarationName's into a partial diagnostic with <<.
529 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
530                                            DeclarationName N) {
531   PD.AddTaggedVal(N.getAsOpaqueInteger(),
532                   DiagnosticsEngine::ak_declarationname);
533   return PD;
534 }
535
536 inline raw_ostream &operator<<(raw_ostream &OS,
537                                      DeclarationNameInfo DNInfo) {
538   DNInfo.printName(OS);
539   return OS;
540 }
541
542 }  // end namespace clang
543
544 namespace llvm {
545 /// Define DenseMapInfo so that DeclarationNames can be used as keys
546 /// in DenseMap and DenseSets.
547 template<>
548 struct DenseMapInfo<clang::DeclarationName> {
549   static inline clang::DeclarationName getEmptyKey() {
550     return clang::DeclarationName::getEmptyMarker();
551   }
552
553   static inline clang::DeclarationName getTombstoneKey() {
554     return clang::DeclarationName::getTombstoneMarker();
555   }
556
557   static unsigned getHashValue(clang::DeclarationName);
558
559   static inline bool
560   isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
561     return LHS == RHS;
562   }
563 };
564
565 template <>
566 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
567
568 }  // end namespace llvm
569
570 #endif