DeclarationName.h revision 17828ca5857d5d9cadfffd339f888de58182c8f1
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/Basic/PartialDiagnostic.h"
18#include "llvm/Support/Compiler.h"
19
20namespace llvm {
21  template <typename T> struct DenseMapInfo;
22}
23
24namespace clang {
25  class ASTContext;
26  class CXXLiteralOperatorIdName;
27  class CXXOperatorIdName;
28  class CXXSpecialName;
29  class DeclarationNameExtra;
30  class IdentifierInfo;
31  class MultiKeywordSelector;
32  class QualType;
33  class Type;
34  class TypeSourceInfo;
35  class UsingDirectiveDecl;
36
37  template <typename> class CanQual;
38  typedef CanQual<Type> CanQualType;
39
40/// DeclarationName - The name of a declaration. In the common case,
41/// this just stores an IdentifierInfo pointer to a normal
42/// name. However, it also provides encodings for Objective-C
43/// selectors (optimizing zero- and one-argument selectors, which make
44/// up 78% percent of all selectors in Cocoa.h) and special C++ names
45/// for constructors, destructors, and conversion functions.
46class DeclarationName {
47public:
48  /// NameKind - The kind of name this object contains.
49  enum NameKind {
50    Identifier,
51    ObjCZeroArgSelector,
52    ObjCOneArgSelector,
53    ObjCMultiArgSelector,
54    CXXConstructorName,
55    CXXDestructorName,
56    CXXConversionFunctionName,
57    CXXOperatorName,
58    CXXLiteralOperatorName,
59    CXXUsingDirective
60  };
61
62private:
63  /// StoredNameKind - The kind of name that is actually stored in the
64  /// upper bits of the Ptr field. This is only used internally.
65  ///
66  /// Note: The entries here are synchronized with the entries in Selector,
67  /// for efficient translation between the two.
68  enum StoredNameKind {
69    StoredIdentifier = 0,
70    StoredObjCZeroArgSelector = 0x01,
71    StoredObjCOneArgSelector = 0x02,
72    StoredDeclarationNameExtra = 0x03,
73    PtrMask = 0x03
74  };
75
76  /// Ptr - The lowest two bits are used to express what kind of name
77  /// we're actually storing, using the values of NameKind. Depending
78  /// on the kind of name this is, the upper bits of Ptr may have one
79  /// of several different meanings:
80  ///
81  ///   StoredIdentifier - The name is a normal identifier, and Ptr is
82  ///   a normal IdentifierInfo pointer.
83  ///
84  ///   StoredObjCZeroArgSelector - The name is an Objective-C
85  ///   selector with zero arguments, and Ptr is an IdentifierInfo
86  ///   pointer pointing to the selector name.
87  ///
88  ///   StoredObjCOneArgSelector - The name is an Objective-C selector
89  ///   with one argument, and Ptr is an IdentifierInfo pointer
90  ///   pointing to the selector name.
91  ///
92  ///   StoredDeclarationNameExtra - Ptr is actually a pointer to a
93  ///   DeclarationNameExtra structure, whose first value will tell us
94  ///   whether this is an Objective-C selector, C++ operator-id name,
95  ///   or special C++ name.
96  uintptr_t Ptr;
97
98  /// getStoredNameKind - Return the kind of object that is stored in
99  /// Ptr.
100  StoredNameKind getStoredNameKind() const {
101    return static_cast<StoredNameKind>(Ptr & PtrMask);
102  }
103
104  /// getExtra - Get the "extra" information associated with this
105  /// multi-argument selector or C++ special name.
106  DeclarationNameExtra *getExtra() const {
107    assert(getStoredNameKind() == StoredDeclarationNameExtra &&
108           "Declaration name does not store an Extra structure");
109    return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
110  }
111
112  /// getAsCXXSpecialName - If the stored pointer is actually a
113  /// CXXSpecialName, returns a pointer to it. Otherwise, returns
114  /// a NULL pointer.
115  CXXSpecialName *getAsCXXSpecialName() const {
116    NameKind Kind = getNameKind();
117    if (Kind >= CXXConstructorName && Kind <= CXXConversionFunctionName)
118      return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
119    return 0;
120  }
121
122  /// getAsCXXOperatorIdName
123  CXXOperatorIdName *getAsCXXOperatorIdName() const {
124    if (getNameKind() == CXXOperatorName)
125      return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
126    return 0;
127  }
128
129  CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
130    if (getNameKind() == CXXLiteralOperatorName)
131      return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
132    return 0;
133  }
134
135  // Construct a declaration name from the name of a C++ constructor,
136  // destructor, or conversion function.
137  DeclarationName(CXXSpecialName *Name)
138    : Ptr(reinterpret_cast<uintptr_t>(Name)) {
139    assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
140    Ptr |= StoredDeclarationNameExtra;
141  }
142
143  // Construct a declaration name from the name of a C++ overloaded
144  // operator.
145  DeclarationName(CXXOperatorIdName *Name)
146    : Ptr(reinterpret_cast<uintptr_t>(Name)) {
147    assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
148    Ptr |= StoredDeclarationNameExtra;
149  }
150
151  DeclarationName(CXXLiteralOperatorIdName *Name)
152    : Ptr(reinterpret_cast<uintptr_t>(Name)) {
153    assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
154    Ptr |= StoredDeclarationNameExtra;
155  }
156
157  /// Construct a declaration name from a raw pointer.
158  DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
159
160  friend class DeclarationNameTable;
161  friend class NamedDecl;
162
163  /// getFETokenInfoAsVoidSlow - Retrieves the front end-specified pointer
164  /// for this name as a void pointer if it's not an identifier.
165  void *getFETokenInfoAsVoidSlow() const;
166
167public:
168  /// DeclarationName - Used to create an empty selector.
169  DeclarationName() : Ptr(0) { }
170
171  // Construct a declaration name from an IdentifierInfo *.
172  DeclarationName(const IdentifierInfo *II)
173    : Ptr(reinterpret_cast<uintptr_t>(II)) {
174    assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
175  }
176
177  // Construct a declaration name from an Objective-C selector.
178  DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) { }
179
180  /// getUsingDirectiveName - Return name for all using-directives.
181  static DeclarationName getUsingDirectiveName();
182
183  // operator bool() - Evaluates true when this declaration name is
184  // non-empty.
185  operator bool() const {
186    return ((Ptr & PtrMask) != 0) ||
187           (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
188  }
189
190  /// Predicate functions for querying what type of name this is.
191  bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
192  bool isObjCZeroArgSelector() const {
193    return getStoredNameKind() == StoredObjCZeroArgSelector;
194  }
195  bool isObjCOneArgSelector() const {
196    return getStoredNameKind() == StoredObjCOneArgSelector;
197  }
198
199  /// getNameKind - Determine what kind of name this is.
200  NameKind getNameKind() const;
201
202  /// \brief Determines whether the name itself is dependent, e.g., because it
203  /// involves a C++ type that is itself dependent.
204  ///
205  /// Note that this does not capture all of the notions of "dependent name",
206  /// because an identifier can be a dependent name if it is used as the
207  /// callee in a call expression with dependent arguments.
208  bool isDependentName() const;
209
210  /// getNameAsString - Retrieve the human-readable string for this name.
211  std::string getAsString() const;
212
213  /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
214  /// this declaration name, or NULL if this declaration name isn't a
215  /// simple identifier.
216  IdentifierInfo *getAsIdentifierInfo() const {
217    if (isIdentifier())
218      return reinterpret_cast<IdentifierInfo *>(Ptr);
219    return 0;
220  }
221
222  /// getAsOpaqueInteger - Get the representation of this declaration
223  /// name as an opaque integer.
224  uintptr_t getAsOpaqueInteger() const { return Ptr; }
225
226  /// getAsOpaquePtr - Get the representation of this declaration name as
227  /// an opaque pointer.
228  void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
229
230  static DeclarationName getFromOpaquePtr(void *P) {
231    DeclarationName N;
232    N.Ptr = reinterpret_cast<uintptr_t> (P);
233    return N;
234  }
235
236  static DeclarationName getFromOpaqueInteger(uintptr_t P) {
237    DeclarationName N;
238    N.Ptr = P;
239    return N;
240  }
241
242  /// getCXXNameType - If this name is one of the C++ names (of a
243  /// constructor, destructor, or conversion function), return the
244  /// type associated with that name.
245  QualType getCXXNameType() const;
246
247  /// getCXXOverloadedOperator - If this name is the name of an
248  /// overloadable operator in C++ (e.g., @c operator+), retrieve the
249  /// kind of overloaded operator.
250  OverloadedOperatorKind getCXXOverloadedOperator() const;
251
252  /// getCXXLiteralIdentifier - If this name is the name of a literal
253  /// operator, retrieve the identifier associated with it.
254  IdentifierInfo *getCXXLiteralIdentifier() const;
255
256  /// getObjCSelector - Get the Objective-C selector stored in this
257  /// declaration name.
258  Selector getObjCSelector() const {
259    assert((getNameKind() == ObjCZeroArgSelector ||
260            getNameKind() == ObjCOneArgSelector ||
261            getNameKind() == ObjCMultiArgSelector ||
262            Ptr == 0) && "Not a selector!");
263    return Selector(Ptr);
264  }
265
266  /// getFETokenInfo/setFETokenInfo - The language front-end is
267  /// allowed to associate arbitrary metadata with some kinds of
268  /// declaration names, including normal identifiers and C++
269  /// constructors, destructors, and conversion functions.
270  template<typename T>
271  T *getFETokenInfo() const {
272    if (const IdentifierInfo *Info = getAsIdentifierInfo())
273      return Info->getFETokenInfo<T>();
274    return static_cast<T*>(getFETokenInfoAsVoidSlow());
275  }
276
277  void setFETokenInfo(void *T);
278
279  /// operator== - Determine whether the specified names are identical..
280  friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
281    return LHS.Ptr == RHS.Ptr;
282  }
283
284  /// operator!= - Determine whether the specified names are different.
285  friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
286    return LHS.Ptr != RHS.Ptr;
287  }
288
289  static DeclarationName getEmptyMarker() {
290    return DeclarationName(uintptr_t(-1));
291  }
292
293  static DeclarationName getTombstoneMarker() {
294    return DeclarationName(uintptr_t(-2));
295  }
296
297  static int compare(DeclarationName LHS, DeclarationName RHS);
298
299  void dump() const;
300};
301
302raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
303
304/// Ordering on two declaration names. If both names are identifiers,
305/// this provides a lexicographical ordering.
306inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
307  return DeclarationName::compare(LHS, RHS) < 0;
308}
309
310/// Ordering on two declaration names. If both names are identifiers,
311/// this provides a lexicographical ordering.
312inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
313  return DeclarationName::compare(LHS, RHS) > 0;
314}
315
316/// Ordering on two declaration names. If both names are identifiers,
317/// this provides a lexicographical ordering.
318inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
319  return DeclarationName::compare(LHS, RHS) <= 0;
320}
321
322/// Ordering on two declaration names. If both names are identifiers,
323/// this provides a lexicographical ordering.
324inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
325  return DeclarationName::compare(LHS, RHS) >= 0;
326}
327
328/// DeclarationNameTable - Used to store and retrieve DeclarationName
329/// instances for the various kinds of declaration names, e.g., normal
330/// identifiers, C++ constructor names, etc. This class contains
331/// uniqued versions of each of the C++ special names, which can be
332/// retrieved using its member functions (e.g.,
333/// getCXXConstructorName).
334class DeclarationNameTable {
335  const ASTContext &Ctx;
336  void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
337  CXXOperatorIdName *CXXOperatorNames; // Operator names
338  void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
339
340  DeclarationNameTable(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
341  void operator=(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
342
343public:
344  DeclarationNameTable(const ASTContext &C);
345  ~DeclarationNameTable();
346
347  /// getIdentifier - Create a declaration name that is a simple
348  /// identifier.
349  DeclarationName getIdentifier(const IdentifierInfo *ID) {
350    return DeclarationName(ID);
351  }
352
353  /// getCXXConstructorName - Returns the name of a C++ constructor
354  /// for the given Type.
355  DeclarationName getCXXConstructorName(CanQualType Ty);
356
357  /// getCXXDestructorName - Returns the name of a C++ destructor
358  /// for the given Type.
359  DeclarationName getCXXDestructorName(CanQualType Ty);
360
361  /// getCXXConversionFunctionName - Returns the name of a C++
362  /// conversion function for the given Type.
363  DeclarationName getCXXConversionFunctionName(CanQualType Ty);
364
365  /// getCXXSpecialName - Returns a declaration name for special kind
366  /// of C++ name, e.g., for a constructor, destructor, or conversion
367  /// function.
368  DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
369                                    CanQualType Ty);
370
371  /// getCXXOperatorName - Get the name of the overloadable C++
372  /// operator corresponding to Op.
373  DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
374
375  /// getCXXLiteralOperatorName - Get the name of the literal operator function
376  /// with II as the identifier.
377  DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
378};
379
380/// DeclarationNameLoc - Additional source/type location info
381/// for a declaration name. Needs a DeclarationName in order
382/// to be interpreted correctly.
383struct DeclarationNameLoc {
384  // The source location for identifier stored elsewhere.
385  // struct {} Identifier;
386
387  // Type info for constructors, destructors and conversion functions.
388  // Locations (if any) for the tilde (destructor) or operator keyword
389  // (conversion) are stored elsewhere.
390  struct NT {
391    TypeSourceInfo* TInfo;
392  };
393
394  // The location (if any) of the operator keyword is stored elsewhere.
395  struct CXXOpName {
396    unsigned BeginOpNameLoc;
397    unsigned EndOpNameLoc;
398  };
399
400  // The location (if any) of the operator keyword is stored elsewhere.
401  struct CXXLitOpName {
402    unsigned OpNameLoc;
403  };
404
405  // struct {} CXXUsingDirective;
406  // struct {} ObjCZeroArgSelector;
407  // struct {} ObjCOneArgSelector;
408  // struct {} ObjCMultiArgSelector;
409  union {
410    struct NT NamedType;
411    struct CXXOpName CXXOperatorName;
412    struct CXXLitOpName CXXLiteralOperatorName;
413  };
414
415  DeclarationNameLoc(DeclarationName Name);
416  // FIXME: this should go away once all DNLocs are properly initialized.
417  DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
418}; // struct DeclarationNameLoc
419
420
421/// DeclarationNameInfo - A collector data type for bundling together
422/// a DeclarationName and the correspnding source/type location info.
423struct DeclarationNameInfo {
424private:
425  /// Name - The declaration name, also encoding name kind.
426  DeclarationName Name;
427  /// Loc - The main source location for the declaration name.
428  SourceLocation NameLoc;
429  /// Info - Further source/type location info for special kinds of names.
430  DeclarationNameLoc LocInfo;
431
432public:
433  // FIXME: remove it.
434  DeclarationNameInfo() {}
435
436  DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
437    : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
438
439  DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
440                      DeclarationNameLoc LocInfo)
441    : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
442
443  /// getName - Returns the embedded declaration name.
444  DeclarationName getName() const { return Name; }
445  /// setName - Sets the embedded declaration name.
446  void setName(DeclarationName N) { Name = N; }
447
448  /// getLoc - Returns the main location of the declaration name.
449  SourceLocation getLoc() const { return NameLoc; }
450  /// setLoc - Sets the main location of the declaration name.
451  void setLoc(SourceLocation L) { NameLoc = L; }
452
453  const DeclarationNameLoc &getInfo() const { return LocInfo; }
454  DeclarationNameLoc &getInfo() { return LocInfo; }
455  void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
456
457  /// getNamedTypeInfo - Returns the source type info associated to
458  /// the name. Assumes it is a constructor, destructor or conversion.
459  TypeSourceInfo *getNamedTypeInfo() const {
460    assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
461           Name.getNameKind() == DeclarationName::CXXDestructorName ||
462           Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
463    return LocInfo.NamedType.TInfo;
464  }
465  /// setNamedTypeInfo - Sets the source type info associated to
466  /// the name. Assumes it is a constructor, destructor or conversion.
467  void setNamedTypeInfo(TypeSourceInfo *TInfo) {
468    assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
469           Name.getNameKind() == DeclarationName::CXXDestructorName ||
470           Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
471    LocInfo.NamedType.TInfo = TInfo;
472  }
473
474  /// getCXXOperatorNameRange - Gets the range of the operator name
475  /// (without the operator keyword). Assumes it is a (non-literal) operator.
476  SourceRange getCXXOperatorNameRange() const {
477    assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
478    return SourceRange(
479     SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
480     SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
481                       );
482  }
483  /// setCXXOperatorNameRange - Sets the range of the operator name
484  /// (without the operator keyword). Assumes it is a C++ operator.
485  void setCXXOperatorNameRange(SourceRange R) {
486    assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
487    LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
488    LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
489  }
490
491  /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
492  /// operator name (not the operator keyword).
493  /// Assumes it is a literal operator.
494  SourceLocation getCXXLiteralOperatorNameLoc() const {
495    assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
496    return SourceLocation::
497      getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
498  }
499  /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
500  /// operator name (not the operator keyword).
501  /// Assumes it is a literal operator.
502  void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
503    assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
504    LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
505  }
506
507  /// \brief Determine whether this name involves a template parameter.
508  bool isInstantiationDependent() const;
509
510  /// \brief Determine whether this name contains an unexpanded
511  /// parameter pack.
512  bool containsUnexpandedParameterPack() const;
513
514  /// getAsString - Retrieve the human-readable string for this name.
515  std::string getAsString() const;
516
517  /// printName - Print the human-readable name to a stream.
518  void printName(raw_ostream &OS) const;
519
520  /// getBeginLoc - Retrieve the location of the first token.
521  SourceLocation getBeginLoc() const { return NameLoc; }
522  /// getEndLoc - Retrieve the location of the last token.
523  SourceLocation getEndLoc() const;
524  /// getSourceRange - The range of the declaration name.
525  SourceRange getSourceRange() const LLVM_READONLY {
526    return SourceRange(getLocStart(), getLocEnd());
527  }
528  SourceLocation getLocStart() const LLVM_READONLY {
529    return getBeginLoc();
530  }
531  SourceLocation getLocEnd() const LLVM_READONLY {
532    SourceLocation EndLoc = getEndLoc();
533    return EndLoc.isValid() ? EndLoc : getLocStart();
534  }
535};
536
537/// Insertion operator for diagnostics.  This allows sending DeclarationName's
538/// into a diagnostic with <<.
539inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
540                                           DeclarationName N) {
541  DB.AddTaggedVal(N.getAsOpaqueInteger(),
542                  DiagnosticsEngine::ak_declarationname);
543  return DB;
544}
545
546/// Insertion operator for partial diagnostics.  This allows binding
547/// DeclarationName's into a partial diagnostic with <<.
548inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
549                                           DeclarationName N) {
550  PD.AddTaggedVal(N.getAsOpaqueInteger(),
551                  DiagnosticsEngine::ak_declarationname);
552  return PD;
553}
554
555inline raw_ostream &operator<<(raw_ostream &OS,
556                                     DeclarationNameInfo DNInfo) {
557  DNInfo.printName(OS);
558  return OS;
559}
560
561}  // end namespace clang
562
563namespace llvm {
564/// Define DenseMapInfo so that DeclarationNames can be used as keys
565/// in DenseMap and DenseSets.
566template<>
567struct DenseMapInfo<clang::DeclarationName> {
568  static inline clang::DeclarationName getEmptyKey() {
569    return clang::DeclarationName::getEmptyMarker();
570  }
571
572  static inline clang::DeclarationName getTombstoneKey() {
573    return clang::DeclarationName::getTombstoneMarker();
574  }
575
576  static unsigned getHashValue(clang::DeclarationName Name) {
577    return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
578  }
579
580  static inline bool
581  isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
582    return LHS == RHS;
583  }
584};
585
586template <>
587struct isPodLike<clang::DeclarationName> { static const bool value = true; };
588
589}  // end namespace llvm
590
591#endif
592