DeclarationName.h revision 44b4321feab46299d3f5cfd404680884752a0fcf
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 "llvm/Bitcode/SerializationFwd.h"
19
20namespace llvm {
21  template <typename T> struct DenseMapInfo;
22}
23
24namespace clang {
25  class CXXSpecialName;
26  class CXXOperatorIdName;
27  class DeclarationNameExtra;
28  class IdentifierInfo;
29  class MultiKeywordSelector;
30
31/// DeclarationName - The name of a declaration. In the common case,
32/// this just stores an IdentifierInfo pointer to a normal
33/// name. However, it also provides encodings for Objective-C
34/// selectors (optimizing zero- and one-argument selectors, which make
35/// up 78% percent of all selectors in Cocoa.h) and special C++ names
36/// for constructors, destructors, and conversion functions.
37class DeclarationName {
38public:
39  /// NameKind - The kind of name this object contains.
40  enum NameKind {
41    Identifier,
42    ObjCZeroArgSelector,
43    ObjCOneArgSelector,
44    ObjCMultiArgSelector,
45    CXXConstructorName,
46    CXXDestructorName,
47    CXXConversionFunctionName,
48    CXXOperatorName
49  };
50
51private:
52  /// StoredNameKind - The kind of name that is actually stored in the
53  /// upper bits of the Ptr field. This is only used internally.
54  enum StoredNameKind {
55    StoredIdentifier = 0,
56    StoredObjCZeroArgSelector,
57    StoredObjCOneArgSelector,
58    StoredDeclarationNameExtra,
59    PtrMask = 0x03
60  };
61
62  /// Ptr - The lowest two bits are used to express what kind of name
63  /// we're actually storing, using the values of NameKind. Depending
64  /// on the kind of name this is, the upper bits of Ptr may have one
65  /// of several different meanings:
66  ///
67  ///   StoredIdentifier - The name is a normal identifier, and Ptr is
68  ///   a normal IdentifierInfo pointer.
69  ///
70  ///   StoredObjCZeroArgSelector - The name is an Objective-C
71  ///   selector with zero arguments, and Ptr is an IdentifierInfo
72  ///   pointer pointing to the selector name.
73  ///
74  ///   StoredObjCOneArgSelector - The name is an Objective-C selector
75  ///   with one argument, and Ptr is an IdentifierInfo pointer
76  ///   pointing to the selector name.
77  ///
78  ///   StoredDeclarationNameExtra - Ptr is actually a pointer to a
79  ///   DeclarationNameExtra structure, whose first value will tell us
80  ///   whether this is an Objective-C selector, C++ operator-id name,
81  ///   or special C++ name.
82  uintptr_t Ptr;
83
84  /// getStoredNameKind - Return the kind of object that is stored in
85  /// Ptr.
86  StoredNameKind getStoredNameKind() const {
87    return static_cast<StoredNameKind>(Ptr & PtrMask);
88  }
89
90  /// getExtra - Get the "extra" information associated with this
91  /// multi-argument selector or C++ special name.
92  DeclarationNameExtra *getExtra() const {
93    assert(getStoredNameKind() == StoredDeclarationNameExtra &&
94           "Declaration name does not store an Extra structure");
95    return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
96  }
97
98  /// getAsCXXSpecialName - If the stored pointer is actually a
99  /// CXXSpecialName, returns a pointer to it. Otherwise, returns
100  /// a NULL pointer.
101  CXXSpecialName *getAsCXXSpecialName() const {
102    if (getNameKind() >= CXXConstructorName &&
103        getNameKind() <= CXXConversionFunctionName)
104      return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
105    else
106      return 0;
107  }
108
109  /// getAsCXXOperatorIdName
110  CXXOperatorIdName *getAsCXXOperatorIdName() const {
111    if (getNameKind() == CXXOperatorName)
112      return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
113    else
114      return 0;
115  }
116
117  // Construct a declaration name from the name of a C++ constructor,
118  // destructor, or conversion function.
119  DeclarationName(CXXSpecialName *Name)
120    : Ptr(reinterpret_cast<uintptr_t>(Name)) {
121    assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
122    Ptr |= StoredDeclarationNameExtra;
123  }
124
125  // Construct a declaration name from the name of a C++ overloaded
126  // operator.
127  DeclarationName(CXXOperatorIdName *Name)
128    : Ptr(reinterpret_cast<uintptr_t>(Name)) {
129    assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
130    Ptr |= StoredDeclarationNameExtra;
131  }
132
133  // Construct a declaration name from a zero- or one-argument
134  // Objective-C selector.
135  DeclarationName(IdentifierInfo *II, unsigned numArgs)
136    : Ptr(reinterpret_cast<uintptr_t>(II)) {
137    assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
138    assert(numArgs < 2 && "Use MultiKeywordSelector for >= 2 arguments");
139    if (numArgs == 0)
140      Ptr |= StoredObjCZeroArgSelector;
141    else
142      Ptr |= StoredObjCOneArgSelector;
143  }
144
145  // Construct a declaration name from an Objective-C multiple-keyword
146  // selector.
147  DeclarationName(MultiKeywordSelector *SI)
148    : Ptr(reinterpret_cast<uintptr_t>(SI)) {
149    assert((Ptr & PtrMask) == 0 && "Improperly aligned MultiKeywordSelector");
150    Ptr |= StoredDeclarationNameExtra;
151  }
152
153  /// Construct a declaration name from a raw pointer.
154  DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
155
156  friend class DeclarationNameTable;
157
158  /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer
159  /// for this name as a void pointer.
160  void *getFETokenInfoAsVoid() const;
161
162public:
163  /// DeclarationName - Used to create an empty selector.
164  DeclarationName() : Ptr(0) { }
165
166  // Construct a declaration name from an IdentifierInfo *.
167  DeclarationName(IdentifierInfo *II) : Ptr(reinterpret_cast<uintptr_t>(II)) {
168    assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
169  }
170
171  // Construct a declaration name from an Objective-C selector.
172  DeclarationName(Selector Sel);
173
174  // operator bool() - Evaluates true when this declaration name is
175  // non-empty.
176  operator bool() const {
177    return ((Ptr & PtrMask) != 0) ||
178           (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
179  }
180
181  /// getNameKind - Determine what kind of name this is.
182  NameKind getNameKind() const;
183
184  /// getName - Retrieve the human-readable string for this name.
185  std::string getAsString() const;
186
187  /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
188  /// this declaration name, or NULL if this declaration name isn't a
189  /// simple identifier.
190  IdentifierInfo *getAsIdentifierInfo() const {
191    if (getNameKind() == Identifier)
192      return reinterpret_cast<IdentifierInfo *>(Ptr);
193    else
194      return 0;
195  }
196
197  /// getAsOpaqueInteger - Get the representation of this declaration
198  /// name as an opaque integer.
199  uintptr_t getAsOpaqueInteger() const { return Ptr; }
200
201  /// getAsOpaquePtr - Get the representation of this declaration name as
202  /// an opaque pointer.
203  void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
204
205  static DeclarationName getFromOpaqueInteger(uintptr_t P) {
206    DeclarationName N;
207    N.Ptr = P;
208    return N;
209  }
210
211  /// getCXXNameType - If this name is one of the C++ names (of a
212  /// constructor, destructor, or conversion function), return the
213  /// type associated with that name.
214  QualType getCXXNameType() const;
215
216  /// getCXXOverloadedOperator - If this name is the name of an
217  /// overloadable operator in C++ (e.g., @c operator+), retrieve the
218  /// kind of overloaded operator.
219  OverloadedOperatorKind getCXXOverloadedOperator() const;
220
221  /// getObjCSelector - Get the Objective-C selector stored in this
222  /// declaration name.
223  Selector getObjCSelector() const;
224
225  /// getFETokenInfo/setFETokenInfo - The language front-end is
226  /// allowed to associate arbitrary metadata with some kinds of
227  /// declaration names, including normal identifiers and C++
228  /// constructors, destructors, and conversion functions.
229  template<typename T>
230  T *getFETokenInfo() const { return static_cast<T*>(getFETokenInfoAsVoid()); }
231
232  void setFETokenInfo(void *T);
233
234  /// operator== - Determine whether the specified names are identical..
235  friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
236    return LHS.Ptr == RHS.Ptr;
237  }
238
239  /// operator!= - Determine whether the specified names are different.
240  friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
241    return LHS.Ptr != RHS.Ptr;
242  }
243
244  static DeclarationName getEmptyMarker() {
245    return DeclarationName(uintptr_t(-1));
246  }
247
248  static DeclarationName getTombstoneMarker() {
249    return DeclarationName(uintptr_t(-2));
250  }
251};
252
253/// Ordering on two declaration names. If both names are identifiers,
254/// this provides a lexicographical ordering.
255bool operator<(DeclarationName LHS, DeclarationName RHS);
256
257/// Ordering on two declaration names. If both names are identifiers,
258/// this provides a lexicographical ordering.
259inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
260  return RHS < LHS;
261}
262
263/// Ordering on two declaration names. If both names are identifiers,
264/// this provides a lexicographical ordering.
265inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
266  return !(RHS < LHS);
267}
268
269/// Ordering on two declaration names. If both names are identifiers,
270/// this provides a lexicographical ordering.
271inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
272  return !(LHS < RHS);
273}
274
275/// DeclarationNameTable - Used to store and retrieve DeclarationName
276/// instances for the various kinds of declaration names, e.g., normal
277/// identifiers, C++ constructor names, etc. This class contains
278/// uniqued versions of each of the C++ special names, which can be
279/// retrieved using its member functions (e.g.,
280/// getCXXConstructorName).
281class DeclarationNameTable {
282  void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
283  CXXOperatorIdName *CXXOperatorNames; // Operator names
284
285  DeclarationNameTable(const DeclarationNameTable&);            // NONCOPYABLE
286  DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE
287
288public:
289  DeclarationNameTable();
290  ~DeclarationNameTable();
291
292  /// getIdentifier - Create a declaration name that is a simple
293  /// identifier.
294  DeclarationName getIdentifier(IdentifierInfo *ID) {
295    return DeclarationName(ID);
296  }
297
298  /// getCXXConstructorName - Returns the name of a C++ constructor
299  /// for the given Type.
300  DeclarationName getCXXConstructorName(QualType Ty) {
301    return getCXXSpecialName(DeclarationName::CXXConstructorName, Ty);
302  }
303
304  /// getCXXDestructorName - Returns the name of a C++ destructor
305  /// for the given Type.
306  DeclarationName getCXXDestructorName(QualType Ty) {
307    return getCXXSpecialName(DeclarationName::CXXDestructorName, Ty);
308  }
309
310  /// getCXXConversionFunctionName - Returns the name of a C++
311  /// conversion function for the given Type.
312  DeclarationName getCXXConversionFunctionName(QualType Ty) {
313    return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
314  }
315
316  /// getCXXSpecialName - Returns a declaration name for special kind
317  /// of C++ name, e.g., for a constructor, destructor, or conversion
318  /// function.
319  DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
320                                    QualType Ty);
321
322  /// getCXXOperatorName - Get the name of the overloadable C++
323  /// operator corresponding to Op.
324  DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
325};
326
327/// Insertion operator for diagnostics.  This allows sending DeclarationName's
328/// into a diagnostic with <<.
329inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
330                                           DeclarationName N) {
331  DB.AddTaggedVal(N.getAsOpaqueInteger(),
332                  Diagnostic::ak_declarationname);
333  return DB;
334}
335
336
337}  // end namespace clang
338
339namespace llvm {
340/// Define DenseMapInfo so that DeclarationNames can be used as keys
341/// in DenseMap and DenseSets.
342template<>
343struct DenseMapInfo<clang::DeclarationName> {
344  static inline clang::DeclarationName getEmptyKey() {
345    return clang::DeclarationName::getEmptyMarker();
346  }
347
348  static inline clang::DeclarationName getTombstoneKey() {
349    return clang::DeclarationName::getTombstoneMarker();
350  }
351
352  static unsigned getHashValue(clang::DeclarationName);
353
354  static inline bool
355  isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
356    return LHS == RHS;
357  }
358
359  static inline bool isPod() { return true; }
360};
361
362}  // end namespace llvm
363
364#endif
365