DeclarationName.h revision c470442fe144b93c2877134559d0067b3215289d
103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham//===-- DeclarationName.h - Representation of declaration names -*- C++ -*-===//
203c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham//
303c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham//                     The LLVM Compiler Infrastructure
403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham//
503c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham// This file is distributed under the University of Illinois Open Source
603c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham// License. See LICENSE.TXT for details.
703c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham//
803c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham//===----------------------------------------------------------------------===//
903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham//
1003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham// This file declares the DeclarationName and DeclarationNameTable classes.
1103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham//
1203c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham//===----------------------------------------------------------------------===//
1303c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham#ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
1403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham#define LLVM_CLANG_AST_DECLARATIONNAME_H
1503c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham
1603c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham#include "clang/Basic/IdentifierTable.h"
1703c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham#include "clang/AST/Type.h"
1803c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham#include "clang/AST/CanonicalType.h"
1903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham#include "clang/Basic/PartialDiagnostic.h"
2049ce8969d3154e1560106cfe530444c09410f217Greg Clayton#include "llvm/Support/Compiler.h"
2103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham
2203c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Inghamnamespace llvm {
2303c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  template <typename T> struct DenseMapInfo;
2403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham}
2503c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham
2603c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Inghamnamespace clang {
2703c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  class CXXSpecialName;
2803c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  class CXXOperatorIdName;
2903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  class CXXLiteralOperatorIdName;
3003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  class DeclarationNameExtra;
3103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  class IdentifierInfo;
3203c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  class MultiKeywordSelector;
3303c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  class UsingDirectiveDecl;
3403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  class TypeSourceInfo;
3503c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham
3603c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham/// DeclarationName - The name of a declaration. In the common case,
3703c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham/// this just stores an IdentifierInfo pointer to a normal
3803c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham/// name. However, it also provides encodings for Objective-C
3903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham/// selectors (optimizing zero- and one-argument selectors, which make
4003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham/// up 78% percent of all selectors in Cocoa.h) and special C++ names
4103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham/// for constructors, destructors, and conversion functions.
4203c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Inghamclass DeclarationName {
4303c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Inghampublic:
4403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  /// NameKind - The kind of name this object contains.
4503c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  enum NameKind {
4603c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    Identifier,
4703c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    ObjCZeroArgSelector,
4803c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    ObjCOneArgSelector,
4903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    ObjCMultiArgSelector,
5003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    CXXConstructorName,
5103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    CXXDestructorName,
5203c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    CXXConversionFunctionName,
5303c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    CXXOperatorName,
5403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    CXXLiteralOperatorName,
5503c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    CXXUsingDirective
5603c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  };
5703c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham
58952e9dc874944fcdbbb224f3ec4fc2c859376f64Greg Claytonprivate:
5903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  /// StoredNameKind - The kind of name that is actually stored in the
6003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  /// upper bits of the Ptr field. This is only used internally.
6103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  ///
62d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  /// Note: The entries here are synchronized with the entries in Selector,
63d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  /// for efficient translation between the two.
64d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  enum StoredNameKind {
653e11c7ec050648ba865f1d451f8cb46fd39072a8Andy Gibbs    StoredIdentifier = 0,
6603c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    StoredObjCZeroArgSelector = 0x01,
67d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham    StoredObjCOneArgSelector = 0x02,
68d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham    StoredDeclarationNameExtra = 0x03,
69d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham    PtrMask = 0x03
7003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  };
71d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham
72d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  /// Ptr - The lowest two bits are used to express what kind of name
73d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  /// we're actually storing, using the values of NameKind. Depending
74d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  /// on the kind of name this is, the upper bits of Ptr may have one
75d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  /// of several different meanings:
76d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///
77d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   StoredIdentifier - The name is a normal identifier, and Ptr is
78d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   a normal IdentifierInfo pointer.
7903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  ///
80d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   StoredObjCZeroArgSelector - The name is an Objective-C
81d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   selector with zero arguments, and Ptr is an IdentifierInfo
82d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   pointer pointing to the selector name.
83d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///
8403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  ///   StoredObjCOneArgSelector - The name is an Objective-C selector
85d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   with one argument, and Ptr is an IdentifierInfo pointer
86d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   pointing to the selector name.
8703c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  ///
88d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   StoredDeclarationNameExtra - Ptr is actually a pointer to a
89d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   DeclarationNameExtra structure, whose first value will tell us
90d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  ///   whether this is an Objective-C selector, C++ operator-id name,
9103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  ///   or special C++ name.
9203c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  uintptr_t Ptr;
93d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham
9403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  /// getStoredNameKind - Return the kind of object that is stored in
955f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea  /// Ptr.
96d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  StoredNameKind getStoredNameKind() const {
97d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham    return static_cast<StoredNameKind>(Ptr & PtrMask);
98d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  }
9903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham
10003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  /// getExtra - Get the "extra" information associated with this
101d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  /// multi-argument selector or C++ special name.
102d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  DeclarationNameExtra *getExtra() const {
103d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham    assert(getStoredNameKind() == StoredDeclarationNameExtra &&
1045f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea           "Declaration name does not store an Extra structure");
105d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham    return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
106d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  }
107d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham
108d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  /// getAsCXXSpecialName - If the stored pointer is actually a
109d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham  /// CXXSpecialName, returns a pointer to it. Otherwise, returns
11003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  /// a NULL pointer.
11103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  CXXSpecialName *getAsCXXSpecialName() const {
112d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham    NameKind Kind = getNameKind();
113d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham    if (Kind >= CXXConstructorName && Kind <= CXXConversionFunctionName)
11403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham      return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
11503c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    return 0;
11603c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  }
11703c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham
11803c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  /// getAsCXXOperatorIdName
11903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  CXXOperatorIdName *getAsCXXOperatorIdName() const {
12003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    if (getNameKind() == CXXOperatorName)
12103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham      return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
12203c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    return 0;
12303c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  }
12403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham
12503c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
126d6d47976b71187907c1cdeea86fabf7d5534314fJim Ingham    if (getNameKind() == CXXLiteralOperatorName)
12703c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham      return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
12803c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    return 0;
12903c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  }
13003c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham
13103c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  // Construct a declaration name from the name of a C++ constructor,
13203c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  // destructor, or conversion function.
13303c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham  DeclarationName(CXXSpecialName *Name)
13403c8ee5aeafcd6c43f10002a4f8096af01780f86Jim Ingham    : 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&);            // NONCOPYABLE
338  DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE
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