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