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