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