DeclarationName.h revision be2fa7ebf01259b63dc52fe46c8d101c18e72269
1579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson//===-- DeclarationName.h - Representation of declaration names -*- C++ -*-===//
2579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson//
3579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson//                     The LLVM Compiler Infrastructure
4579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson//
5579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson// This file is distributed under the University of Illinois Open Source
6579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson// License. See LICENSE.TXT for details.
7579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson//
8579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson//===----------------------------------------------------------------------===//
9579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson//
10579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson// This file declares the DeclarationName and DeclarationNameTable classes.
11579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson//
12579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson//===----------------------------------------------------------------------===//
13579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson#ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
14579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson#define LLVM_CLANG_AST_DECLARATIONNAME_H
15579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
16579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson#include "clang/Basic/IdentifierTable.h"
17579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson#include "clang/AST/Type.h"
18579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson#include "clang/AST/CanonicalType.h"
19579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson#include "clang/Basic/PartialDiagnostic.h"
20579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson#include "llvm/Support/Compiler.h"
21579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
22579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonnamespace llvm {
23579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  template <typename T> struct DenseMapInfo;
24579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson}
25579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
26579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonnamespace clang {
27579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  class CXXSpecialName;
28579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  class CXXOperatorIdName;
29579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  class CXXLiteralOperatorIdName;
30579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  class DeclarationNameExtra;
31579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  class IdentifierInfo;
32579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  class MultiKeywordSelector;
33579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  class UsingDirectiveDecl;
34579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  class TypeSourceInfo;
35579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
36579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/// DeclarationName - The name of a declaration. In the common case,
37579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/// this just stores an IdentifierInfo pointer to a normal
38579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/// name. However, it also provides encodings for Objective-C
39579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/// selectors (optimizing zero- and one-argument selectors, which make
40579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/// up 78% percent of all selectors in Cocoa.h) and special C++ names
41579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/// for constructors, destructors, and conversion functions.
42579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonclass DeclarationName {
43579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonpublic:
44579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// NameKind - The kind of name this object contains.
45579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  enum NameKind {
46579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    Identifier,
47579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    ObjCZeroArgSelector,
48579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    ObjCOneArgSelector,
49579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    ObjCMultiArgSelector,
50579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    CXXConstructorName,
51579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    CXXDestructorName,
52579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    CXXConversionFunctionName,
53579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    CXXOperatorName,
54579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    CXXLiteralOperatorName,
55579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    CXXUsingDirective
56579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  };
57579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
58579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonprivate:
59579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// StoredNameKind - The kind of name that is actually stored in the
60579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// upper bits of the Ptr field. This is only used internally.
61579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///
62579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// Note: The entries here are synchronized with the entries in Selector,
63579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// for efficient translation between the two.
64579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  enum StoredNameKind {
65579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    StoredIdentifier = 0,
66579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    StoredObjCZeroArgSelector = 0x01,
67579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    StoredObjCOneArgSelector = 0x02,
68579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    StoredDeclarationNameExtra = 0x03,
69579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    PtrMask = 0x03
70579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  };
71579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
72579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// Ptr - The lowest two bits are used to express what kind of name
73579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// we're actually storing, using the values of NameKind. Depending
74579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// on the kind of name this is, the upper bits of Ptr may have one
75579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// of several different meanings:
76579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///
77579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   StoredIdentifier - The name is a normal identifier, and Ptr is
78579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   a normal IdentifierInfo pointer.
79579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///
80579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   StoredObjCZeroArgSelector - The name is an Objective-C
81579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   selector with zero arguments, and Ptr is an IdentifierInfo
82579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   pointer pointing to the selector name.
83579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///
84579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   StoredObjCOneArgSelector - The name is an Objective-C selector
85579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   with one argument, and Ptr is an IdentifierInfo pointer
86579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   pointing to the selector name.
87579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///
88579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   StoredDeclarationNameExtra - Ptr is actually a pointer to a
89579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   DeclarationNameExtra structure, whose first value will tell us
90579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   whether this is an Objective-C selector, C++ operator-id name,
91579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  ///   or special C++ name.
92579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  uintptr_t Ptr;
93579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
94579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// getStoredNameKind - Return the kind of object that is stored in
95579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// Ptr.
96579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  StoredNameKind getStoredNameKind() const {
97579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    return static_cast<StoredNameKind>(Ptr & PtrMask);
98579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  }
99579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
100579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// getExtra - Get the "extra" information associated with this
101579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// multi-argument selector or C++ special name.
102579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  DeclarationNameExtra *getExtra() const {
103579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    assert(getStoredNameKind() == StoredDeclarationNameExtra &&
104579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson           "Declaration name does not store an Extra structure");
105579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
106579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  }
107579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson
108579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// getAsCXXSpecialName - If the stored pointer is actually a
109579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// CXXSpecialName, returns a pointer to it. Otherwise, returns
110579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  /// a NULL pointer.
111579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson  CXXSpecialName *getAsCXXSpecialName() const {
112579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    NameKind Kind = getNameKind();
113579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson    if (Kind >= CXXConstructorName && Kind <= CXXConversionFunctionName)
114579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson      return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
115    return 0;
116  }
117
118  /// getAsCXXOperatorIdName
119  CXXOperatorIdName *getAsCXXOperatorIdName() const {
120    if (getNameKind() == CXXOperatorName)
121      return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
122    return 0;
123  }
124
125  CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
126    if (getNameKind() == CXXLiteralOperatorName)
127      return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
128    return 0;
129  }
130
131  // Construct a declaration name from the name of a C++ constructor,
132  // destructor, or conversion function.
133  DeclarationName(CXXSpecialName *Name)
134    : Ptr(reinterpret_cast<uintptr_t>(Name)) {
135    assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
136    Ptr |= StoredDeclarationNameExtra;
137  }
138
139  // Construct a declaration name from the name of a C++ overloaded
140  // operator.
141  DeclarationName(CXXOperatorIdName *Name)
142    : Ptr(reinterpret_cast<uintptr_t>(Name)) {
143    assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
144    Ptr |= StoredDeclarationNameExtra;
145  }
146
147  DeclarationName(CXXLiteralOperatorIdName *Name)
148    : Ptr(reinterpret_cast<uintptr_t>(Name)) {
149    assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
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  friend class NamedDecl;
158
159  /// getFETokenInfoAsVoidSlow - Retrieves the front end-specified pointer
160  /// for this name as a void pointer if it's not an identifier.
161  void *getFETokenInfoAsVoidSlow() const;
162
163public:
164  /// DeclarationName - Used to create an empty selector.
165  DeclarationName() : Ptr(0) { }
166
167  // Construct a declaration name from an IdentifierInfo *.
168  DeclarationName(const IdentifierInfo *II)
169    : Ptr(reinterpret_cast<uintptr_t>(II)) {
170    assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
171  }
172
173  // Construct a declaration name from an Objective-C selector.
174  DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) { }
175
176  /// getUsingDirectiveName - Return name for all using-directives.
177  static DeclarationName getUsingDirectiveName();
178
179  // operator bool() - Evaluates true when this declaration name is
180  // non-empty.
181  operator bool() const {
182    return ((Ptr & PtrMask) != 0) ||
183           (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
184  }
185
186  /// Predicate functions for querying what type of name this is.
187  bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
188  bool isObjCZeroArgSelector() const {
189    return getStoredNameKind() == StoredObjCZeroArgSelector;
190  }
191  bool isObjCOneArgSelector() const {
192    return getStoredNameKind() == StoredObjCOneArgSelector;
193  }
194
195  /// getNameKind - Determine what kind of name this is.
196  NameKind getNameKind() const;
197
198  /// \brief Determines whether the name itself is dependent, e.g., because it
199  /// involves a C++ type that is itself dependent.
200  ///
201  /// Note that this does not capture all of the notions of "dependent name",
202  /// because an identifier can be a dependent name if it is used as the
203  /// callee in a call expression with dependent arguments.
204  bool isDependentName() const;
205
206  /// getNameAsString - Retrieve the human-readable string for this name.
207  std::string getAsString() const;
208
209  /// printName - Print the human-readable name to a stream.
210  void printName(raw_ostream &OS) const;
211
212  /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
213  /// this declaration name, or NULL if this declaration name isn't a
214  /// simple identifier.
215  IdentifierInfo *getAsIdentifierInfo() const {
216    if (isIdentifier())
217      return reinterpret_cast<IdentifierInfo *>(Ptr);
218    return 0;
219  }
220
221  /// getAsOpaqueInteger - Get the representation of this declaration
222  /// name as an opaque integer.
223  uintptr_t getAsOpaqueInteger() const { return Ptr; }
224
225  /// getAsOpaquePtr - Get the representation of this declaration name as
226  /// an opaque pointer.
227  void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
228
229  static DeclarationName getFromOpaquePtr(void *P) {
230    DeclarationName N;
231    N.Ptr = reinterpret_cast<uintptr_t> (P);
232    return N;
233  }
234
235  static DeclarationName getFromOpaqueInteger(uintptr_t P) {
236    DeclarationName N;
237    N.Ptr = P;
238    return N;
239  }
240
241  /// getCXXNameType - If this name is one of the C++ names (of a
242  /// constructor, destructor, or conversion function), return the
243  /// type associated with that name.
244  QualType getCXXNameType() const;
245
246  /// getCXXOverloadedOperator - If this name is the name of an
247  /// overloadable operator in C++ (e.g., @c operator+), retrieve the
248  /// kind of overloaded operator.
249  OverloadedOperatorKind getCXXOverloadedOperator() const;
250
251  /// getCXXLiteralIdentifier - If this name is the name of a literal
252  /// operator, retrieve the identifier associated with it.
253  IdentifierInfo *getCXXLiteralIdentifier() const;
254
255  /// getObjCSelector - Get the Objective-C selector stored in this
256  /// declaration name.
257  Selector getObjCSelector() const {
258    assert((getNameKind() == ObjCZeroArgSelector ||
259            getNameKind() == ObjCOneArgSelector ||
260            getNameKind() == ObjCMultiArgSelector ||
261            Ptr == 0) && "Not a selector!");
262    return Selector(Ptr);
263  }
264
265  /// getFETokenInfo/setFETokenInfo - The language front-end is
266  /// allowed to associate arbitrary metadata with some kinds of
267  /// declaration names, including normal identifiers and C++
268  /// constructors, destructors, and conversion functions.
269  template<typename T>
270  T *getFETokenInfo() const {
271    if (const IdentifierInfo *Info = getAsIdentifierInfo())
272      return Info->getFETokenInfo<T>();
273    return static_cast<T*>(getFETokenInfoAsVoidSlow());
274  }
275
276  void setFETokenInfo(void *T);
277
278  /// operator== - Determine whether the specified names are identical..
279  friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
280    return LHS.Ptr == RHS.Ptr;
281  }
282
283  /// operator!= - Determine whether the specified names are different.
284  friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
285    return LHS.Ptr != RHS.Ptr;
286  }
287
288  static DeclarationName getEmptyMarker() {
289    return DeclarationName(uintptr_t(-1));
290  }
291
292  static DeclarationName getTombstoneMarker() {
293    return DeclarationName(uintptr_t(-2));
294  }
295
296  static int compare(DeclarationName LHS, DeclarationName RHS);
297
298  void dump() const;
299};
300
301/// Ordering on two declaration names. If both names are identifiers,
302/// this provides a lexicographical ordering.
303inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
304  return DeclarationName::compare(LHS, RHS) < 0;
305}
306
307/// Ordering on two declaration names. If both names are identifiers,
308/// this provides a lexicographical ordering.
309inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
310  return DeclarationName::compare(LHS, RHS) > 0;
311}
312
313/// Ordering on two declaration names. If both names are identifiers,
314/// this provides a lexicographical ordering.
315inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
316  return DeclarationName::compare(LHS, RHS) <= 0;
317}
318
319/// Ordering on two declaration names. If both names are identifiers,
320/// this provides a lexicographical ordering.
321inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
322  return DeclarationName::compare(LHS, RHS) >= 0;
323}
324
325/// DeclarationNameTable - Used to store and retrieve DeclarationName
326/// instances for the various kinds of declaration names, e.g., normal
327/// identifiers, C++ constructor names, etc. This class contains
328/// uniqued versions of each of the C++ special names, which can be
329/// retrieved using its member functions (e.g.,
330/// getCXXConstructorName).
331class DeclarationNameTable {
332  const ASTContext &Ctx;
333  void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
334  CXXOperatorIdName *CXXOperatorNames; // Operator names
335  void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
336
337  DeclarationNameTable(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
338  void operator=(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
339
340public:
341  DeclarationNameTable(const ASTContext &C);
342  ~DeclarationNameTable();
343
344  /// getIdentifier - Create a declaration name that is a simple
345  /// identifier.
346  DeclarationName getIdentifier(const IdentifierInfo *ID) {
347    return DeclarationName(ID);
348  }
349
350  /// getCXXConstructorName - Returns the name of a C++ constructor
351  /// for the given Type.
352  DeclarationName getCXXConstructorName(CanQualType Ty) {
353    return getCXXSpecialName(DeclarationName::CXXConstructorName,
354                             Ty.getUnqualifiedType());
355  }
356
357  /// getCXXDestructorName - Returns the name of a C++ destructor
358  /// for the given Type.
359  DeclarationName getCXXDestructorName(CanQualType Ty) {
360    return getCXXSpecialName(DeclarationName::CXXDestructorName,
361                             Ty.getUnqualifiedType());
362  }
363
364  /// getCXXConversionFunctionName - Returns the name of a C++
365  /// conversion function for the given Type.
366  DeclarationName getCXXConversionFunctionName(CanQualType Ty) {
367    return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
368  }
369
370  /// getCXXSpecialName - Returns a declaration name for special kind
371  /// of C++ name, e.g., for a constructor, destructor, or conversion
372  /// function.
373  DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
374                                    CanQualType Ty);
375
376  /// getCXXOperatorName - Get the name of the overloadable C++
377  /// operator corresponding to Op.
378  DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
379
380  /// getCXXLiteralOperatorName - Get the name of the literal operator function
381  /// with II as the identifier.
382  DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
383};
384
385/// DeclarationNameLoc - Additional source/type location info
386/// for a declaration name. Needs a DeclarationName in order
387/// to be interpreted correctly.
388struct DeclarationNameLoc {
389  union {
390    // The source location for identifier stored elsewhere.
391    // struct {} Identifier;
392
393    // Type info for constructors, destructors and conversion functions.
394    // Locations (if any) for the tilde (destructor) or operator keyword
395    // (conversion) are stored elsewhere.
396    struct {
397      TypeSourceInfo* TInfo;
398    } NamedType;
399
400    // The location (if any) of the operator keyword is stored elsewhere.
401    struct {
402      unsigned BeginOpNameLoc;
403      unsigned EndOpNameLoc;
404    } CXXOperatorName;
405
406    // The location (if any) of the operator keyword is stored elsewhere.
407    struct {
408      unsigned OpNameLoc;
409    } CXXLiteralOperatorName;
410
411    // struct {} CXXUsingDirective;
412    // struct {} ObjCZeroArgSelector;
413    // struct {} ObjCOneArgSelector;
414    // struct {} ObjCMultiArgSelector;
415  };
416
417  DeclarationNameLoc(DeclarationName Name);
418  // FIXME: this should go away once all DNLocs are properly initialized.
419  DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
420}; // struct DeclarationNameLoc
421
422
423/// DeclarationNameInfo - A collector data type for bundling together
424/// a DeclarationName and the correspnding source/type location info.
425struct DeclarationNameInfo {
426private:
427  /// Name - The declaration name, also encoding name kind.
428  DeclarationName Name;
429  /// Loc - The main source location for the declaration name.
430  SourceLocation NameLoc;
431  /// Info - Further source/type location info for special kinds of names.
432  DeclarationNameLoc LocInfo;
433
434public:
435  // FIXME: remove it.
436  DeclarationNameInfo() {}
437
438  DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
439    : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
440
441  DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
442                      DeclarationNameLoc LocInfo)
443    : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
444
445  /// getName - Returns the embedded declaration name.
446  DeclarationName getName() const { return Name; }
447  /// setName - Sets the embedded declaration name.
448  void setName(DeclarationName N) { Name = N; }
449
450  /// getLoc - Returns the main location of the declaration name.
451  SourceLocation getLoc() const { return NameLoc; }
452  /// setLoc - Sets the main location of the declaration name.
453  void setLoc(SourceLocation L) { NameLoc = L; }
454
455  const DeclarationNameLoc &getInfo() const { return LocInfo; }
456  DeclarationNameLoc &getInfo() { return LocInfo; }
457  void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
458
459  /// getNamedTypeInfo - Returns the source type info associated to
460  /// the name. Assumes it is a constructor, destructor or conversion.
461  TypeSourceInfo *getNamedTypeInfo() const {
462    assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
463           Name.getNameKind() == DeclarationName::CXXDestructorName ||
464           Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
465    return LocInfo.NamedType.TInfo;
466  }
467  /// setNamedTypeInfo - Sets the source type info associated to
468  /// the name. Assumes it is a constructor, destructor or conversion.
469  void setNamedTypeInfo(TypeSourceInfo *TInfo) {
470    assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
471           Name.getNameKind() == DeclarationName::CXXDestructorName ||
472           Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
473    LocInfo.NamedType.TInfo = TInfo;
474  }
475
476  /// getCXXOperatorNameRange - Gets the range of the operator name
477  /// (without the operator keyword). Assumes it is a (non-literal) operator.
478  SourceRange getCXXOperatorNameRange() const {
479    assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
480    return SourceRange(
481     SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
482     SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
483                       );
484  }
485  /// setCXXOperatorNameRange - Sets the range of the operator name
486  /// (without the operator keyword). Assumes it is a C++ operator.
487  void setCXXOperatorNameRange(SourceRange R) {
488    assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
489    LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
490    LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
491  }
492
493  /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
494  /// operator name (not the operator keyword).
495  /// Assumes it is a literal operator.
496  SourceLocation getCXXLiteralOperatorNameLoc() const {
497    assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
498    return SourceLocation::
499      getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
500  }
501  /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
502  /// operator name (not the operator keyword).
503  /// Assumes it is a literal operator.
504  void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
505    assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
506    LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
507  }
508
509  /// \brief Determine whether this name involves a template parameter.
510  bool isInstantiationDependent() const;
511
512  /// \brief Determine whether this name contains an unexpanded
513  /// parameter pack.
514  bool containsUnexpandedParameterPack() const;
515
516  /// getAsString - Retrieve the human-readable string for this name.
517  std::string getAsString() const;
518
519  /// printName - Print the human-readable name to a stream.
520  void printName(raw_ostream &OS) const;
521
522  /// getBeginLoc - Retrieve the location of the first token.
523  SourceLocation getBeginLoc() const { return NameLoc; }
524  /// getEndLoc - Retrieve the location of the last token.
525  SourceLocation getEndLoc() const;
526  /// getSourceRange - The range of the declaration name.
527  SourceRange getSourceRange() const LLVM_READONLY {
528    SourceLocation BeginLoc = getBeginLoc();
529    SourceLocation EndLoc = getEndLoc();
530    return SourceRange(BeginLoc, EndLoc.isValid() ? EndLoc : BeginLoc);
531  }
532  SourceLocation getLocStart() const LLVM_READONLY {
533    return getBeginLoc();
534  }
535  SourceLocation getLocEnd() const LLVM_READONLY {
536    SourceLocation EndLoc = getEndLoc();
537    return EndLoc.isValid() ? EndLoc : getLocStart();
538  }
539};
540
541/// Insertion operator for diagnostics.  This allows sending DeclarationName's
542/// into a diagnostic with <<.
543inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
544                                           DeclarationName N) {
545  DB.AddTaggedVal(N.getAsOpaqueInteger(),
546                  DiagnosticsEngine::ak_declarationname);
547  return DB;
548}
549
550/// Insertion operator for partial diagnostics.  This allows binding
551/// DeclarationName's into a partial diagnostic with <<.
552inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
553                                           DeclarationName N) {
554  PD.AddTaggedVal(N.getAsOpaqueInteger(),
555                  DiagnosticsEngine::ak_declarationname);
556  return PD;
557}
558
559inline raw_ostream &operator<<(raw_ostream &OS,
560                                     DeclarationNameInfo DNInfo) {
561  DNInfo.printName(OS);
562  return OS;
563}
564
565}  // end namespace clang
566
567namespace llvm {
568/// Define DenseMapInfo so that DeclarationNames can be used as keys
569/// in DenseMap and DenseSets.
570template<>
571struct DenseMapInfo<clang::DeclarationName> {
572  static inline clang::DeclarationName getEmptyKey() {
573    return clang::DeclarationName::getEmptyMarker();
574  }
575
576  static inline clang::DeclarationName getTombstoneKey() {
577    return clang::DeclarationName::getTombstoneMarker();
578  }
579
580  static unsigned getHashValue(clang::DeclarationName Name) {
581    return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
582  }
583
584  static inline bool
585  isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
586    return LHS == RHS;
587  }
588};
589
590template <>
591struct isPodLike<clang::DeclarationName> { static const bool value = true; };
592
593}  // end namespace llvm
594
595#endif
596