DeclarationName.h revision 7247c88d1e41514a41085f83ebf03dd5220e054a
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  LLVM_EXPLICIT operator bool() const {
186    return ((Ptr & PtrMask) != 0) ||
187           (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
188  }
189
190  /// \brief Evaluates true when this declaration name is empty.
191  bool isEmpty() const {
192    return !*this;
193  }
194
195  /// Predicate functions for querying what type of name this is.
196  bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
197  bool isObjCZeroArgSelector() const {
198    return getStoredNameKind() == StoredObjCZeroArgSelector;
199  }
200  bool isObjCOneArgSelector() const {
201    return getStoredNameKind() == StoredObjCOneArgSelector;
202  }
203
204  /// getNameKind - Determine what kind of name this is.
205  NameKind getNameKind() const;
206
207  /// \brief Determines whether the name itself is dependent, e.g., because it
208  /// involves a C++ type that is itself dependent.
209  ///
210  /// Note that this does not capture all of the notions of "dependent name",
211  /// because an identifier can be a dependent name if it is used as the
212  /// callee in a call expression with dependent arguments.
213  bool isDependentName() const;
214
215  /// getNameAsString - Retrieve the human-readable string for this name.
216  std::string getAsString() const;
217
218  /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
219  /// this declaration name, or NULL if this declaration name isn't a
220  /// simple identifier.
221  IdentifierInfo *getAsIdentifierInfo() const {
222    if (isIdentifier())
223      return reinterpret_cast<IdentifierInfo *>(Ptr);
224    return 0;
225  }
226
227  /// getAsOpaqueInteger - Get the representation of this declaration
228  /// name as an opaque integer.
229  uintptr_t getAsOpaqueInteger() const { return Ptr; }
230
231  /// getAsOpaquePtr - Get the representation of this declaration name as
232  /// an opaque pointer.
233  void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
234
235  static DeclarationName getFromOpaquePtr(void *P) {
236    DeclarationName N;
237    N.Ptr = reinterpret_cast<uintptr_t> (P);
238    return N;
239  }
240
241  static DeclarationName getFromOpaqueInteger(uintptr_t P) {
242    DeclarationName N;
243    N.Ptr = P;
244    return N;
245  }
246
247  /// getCXXNameType - If this name is one of the C++ names (of a
248  /// constructor, destructor, or conversion function), return the
249  /// type associated with that name.
250  QualType getCXXNameType() const;
251
252  /// getCXXOverloadedOperator - If this name is the name of an
253  /// overloadable operator in C++ (e.g., @c operator+), retrieve the
254  /// kind of overloaded operator.
255  OverloadedOperatorKind getCXXOverloadedOperator() const;
256
257  /// getCXXLiteralIdentifier - If this name is the name of a literal
258  /// operator, retrieve the identifier associated with it.
259  IdentifierInfo *getCXXLiteralIdentifier() const;
260
261  /// getObjCSelector - Get the Objective-C selector stored in this
262  /// declaration name.
263  Selector getObjCSelector() const {
264    assert((getNameKind() == ObjCZeroArgSelector ||
265            getNameKind() == ObjCOneArgSelector ||
266            getNameKind() == ObjCMultiArgSelector ||
267            Ptr == 0) && "Not a selector!");
268    return Selector(Ptr);
269  }
270
271  /// getFETokenInfo/setFETokenInfo - The language front-end is
272  /// allowed to associate arbitrary metadata with some kinds of
273  /// declaration names, including normal identifiers and C++
274  /// constructors, destructors, and conversion functions.
275  template<typename T>
276  T *getFETokenInfo() const {
277    if (const IdentifierInfo *Info = getAsIdentifierInfo())
278      return Info->getFETokenInfo<T>();
279    return static_cast<T*>(getFETokenInfoAsVoidSlow());
280  }
281
282  void setFETokenInfo(void *T);
283
284  /// operator== - Determine whether the specified names are identical..
285  friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
286    return LHS.Ptr == RHS.Ptr;
287  }
288
289  /// operator!= - Determine whether the specified names are different.
290  friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
291    return LHS.Ptr != RHS.Ptr;
292  }
293
294  static DeclarationName getEmptyMarker() {
295    return DeclarationName(uintptr_t(-1));
296  }
297
298  static DeclarationName getTombstoneMarker() {
299    return DeclarationName(uintptr_t(-2));
300  }
301
302  static int compare(DeclarationName LHS, DeclarationName RHS);
303
304  void dump() const;
305};
306
307raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
308
309/// Ordering on two declaration names. If both names are identifiers,
310/// this provides a lexicographical ordering.
311inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
312  return DeclarationName::compare(LHS, RHS) < 0;
313}
314
315/// Ordering on two declaration names. If both names are identifiers,
316/// this provides a lexicographical ordering.
317inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
318  return DeclarationName::compare(LHS, RHS) > 0;
319}
320
321/// Ordering on two declaration names. If both names are identifiers,
322/// this provides a lexicographical ordering.
323inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
324  return DeclarationName::compare(LHS, RHS) <= 0;
325}
326
327/// Ordering on two declaration names. If both names are identifiers,
328/// this provides a lexicographical ordering.
329inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
330  return DeclarationName::compare(LHS, RHS) >= 0;
331}
332
333/// DeclarationNameTable - Used to store and retrieve DeclarationName
334/// instances for the various kinds of declaration names, e.g., normal
335/// identifiers, C++ constructor names, etc. This class contains
336/// uniqued versions of each of the C++ special names, which can be
337/// retrieved using its member functions (e.g.,
338/// getCXXConstructorName).
339class DeclarationNameTable {
340  const ASTContext &Ctx;
341  void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
342  CXXOperatorIdName *CXXOperatorNames; // Operator names
343  void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
344
345  DeclarationNameTable(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
346  void operator=(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
347
348public:
349  DeclarationNameTable(const ASTContext &C);
350  ~DeclarationNameTable();
351
352  /// getIdentifier - Create a declaration name that is a simple
353  /// identifier.
354  DeclarationName getIdentifier(const IdentifierInfo *ID) {
355    return DeclarationName(ID);
356  }
357
358  /// getCXXConstructorName - Returns the name of a C++ constructor
359  /// for the given Type.
360  DeclarationName getCXXConstructorName(CanQualType Ty);
361
362  /// getCXXDestructorName - Returns the name of a C++ destructor
363  /// for the given Type.
364  DeclarationName getCXXDestructorName(CanQualType Ty);
365
366  /// getCXXConversionFunctionName - Returns the name of a C++
367  /// conversion function for the given Type.
368  DeclarationName getCXXConversionFunctionName(CanQualType Ty);
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  // The source location for identifier stored elsewhere.
390  // struct {} Identifier;
391
392  // Type info for constructors, destructors and conversion functions.
393  // Locations (if any) for the tilde (destructor) or operator keyword
394  // (conversion) are stored elsewhere.
395  struct NT {
396    TypeSourceInfo* TInfo;
397  };
398
399  // The location (if any) of the operator keyword is stored elsewhere.
400  struct CXXOpName {
401    unsigned BeginOpNameLoc;
402    unsigned EndOpNameLoc;
403  };
404
405  // The location (if any) of the operator keyword is stored elsewhere.
406  struct CXXLitOpName {
407    unsigned OpNameLoc;
408  };
409
410  // struct {} CXXUsingDirective;
411  // struct {} ObjCZeroArgSelector;
412  // struct {} ObjCOneArgSelector;
413  // struct {} ObjCMultiArgSelector;
414  union {
415    struct NT NamedType;
416    struct CXXOpName CXXOperatorName;
417    struct CXXLitOpName CXXLiteralOperatorName;
418  };
419
420  DeclarationNameLoc(DeclarationName Name);
421  // FIXME: this should go away once all DNLocs are properly initialized.
422  DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
423}; // struct DeclarationNameLoc
424
425
426/// DeclarationNameInfo - A collector data type for bundling together
427/// a DeclarationName and the correspnding source/type location info.
428struct DeclarationNameInfo {
429private:
430  /// Name - The declaration name, also encoding name kind.
431  DeclarationName Name;
432  /// Loc - The main source location for the declaration name.
433  SourceLocation NameLoc;
434  /// Info - Further source/type location info for special kinds of names.
435  DeclarationNameLoc LocInfo;
436
437public:
438  // FIXME: remove it.
439  DeclarationNameInfo() {}
440
441  DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
442    : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
443
444  DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
445                      DeclarationNameLoc LocInfo)
446    : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
447
448  /// getName - Returns the embedded declaration name.
449  DeclarationName getName() const { return Name; }
450  /// setName - Sets the embedded declaration name.
451  void setName(DeclarationName N) { Name = N; }
452
453  /// getLoc - Returns the main location of the declaration name.
454  SourceLocation getLoc() const { return NameLoc; }
455  /// setLoc - Sets the main location of the declaration name.
456  void setLoc(SourceLocation L) { NameLoc = L; }
457
458  const DeclarationNameLoc &getInfo() const { return LocInfo; }
459  DeclarationNameLoc &getInfo() { return LocInfo; }
460  void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
461
462  /// getNamedTypeInfo - Returns the source type info associated to
463  /// the name. Assumes it is a constructor, destructor or conversion.
464  TypeSourceInfo *getNamedTypeInfo() const {
465    assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
466           Name.getNameKind() == DeclarationName::CXXDestructorName ||
467           Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
468    return LocInfo.NamedType.TInfo;
469  }
470  /// setNamedTypeInfo - Sets the source type info associated to
471  /// the name. Assumes it is a constructor, destructor or conversion.
472  void setNamedTypeInfo(TypeSourceInfo *TInfo) {
473    assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
474           Name.getNameKind() == DeclarationName::CXXDestructorName ||
475           Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
476    LocInfo.NamedType.TInfo = TInfo;
477  }
478
479  /// getCXXOperatorNameRange - Gets the range of the operator name
480  /// (without the operator keyword). Assumes it is a (non-literal) operator.
481  SourceRange getCXXOperatorNameRange() const {
482    assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
483    return SourceRange(
484     SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
485     SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
486                       );
487  }
488  /// setCXXOperatorNameRange - Sets the range of the operator name
489  /// (without the operator keyword). Assumes it is a C++ operator.
490  void setCXXOperatorNameRange(SourceRange R) {
491    assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
492    LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
493    LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
494  }
495
496  /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
497  /// operator name (not the operator keyword).
498  /// Assumes it is a literal operator.
499  SourceLocation getCXXLiteralOperatorNameLoc() const {
500    assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
501    return SourceLocation::
502      getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
503  }
504  /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
505  /// operator name (not the operator keyword).
506  /// Assumes it is a literal operator.
507  void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
508    assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
509    LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
510  }
511
512  /// \brief Determine whether this name involves a template parameter.
513  bool isInstantiationDependent() const;
514
515  /// \brief Determine whether this name contains an unexpanded
516  /// parameter pack.
517  bool containsUnexpandedParameterPack() const;
518
519  /// getAsString - Retrieve the human-readable string for this name.
520  std::string getAsString() const;
521
522  /// printName - Print the human-readable name to a stream.
523  void printName(raw_ostream &OS) const;
524
525  /// getBeginLoc - Retrieve the location of the first token.
526  SourceLocation getBeginLoc() const { return NameLoc; }
527  /// getEndLoc - Retrieve the location of the last token.
528  SourceLocation getEndLoc() const;
529  /// getSourceRange - The range of the declaration name.
530  SourceRange getSourceRange() const LLVM_READONLY {
531    return SourceRange(getLocStart(), getLocEnd());
532  }
533  SourceLocation getLocStart() const LLVM_READONLY {
534    return getBeginLoc();
535  }
536  SourceLocation getLocEnd() const LLVM_READONLY {
537    SourceLocation EndLoc = getEndLoc();
538    return EndLoc.isValid() ? EndLoc : getLocStart();
539  }
540};
541
542/// Insertion operator for diagnostics.  This allows sending DeclarationName's
543/// into a diagnostic with <<.
544inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
545                                           DeclarationName N) {
546  DB.AddTaggedVal(N.getAsOpaqueInteger(),
547                  DiagnosticsEngine::ak_declarationname);
548  return DB;
549}
550
551/// Insertion operator for partial diagnostics.  This allows binding
552/// DeclarationName's into a partial diagnostic with <<.
553inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
554                                           DeclarationName N) {
555  PD.AddTaggedVal(N.getAsOpaqueInteger(),
556                  DiagnosticsEngine::ak_declarationname);
557  return PD;
558}
559
560inline raw_ostream &operator<<(raw_ostream &OS,
561                                     DeclarationNameInfo DNInfo) {
562  DNInfo.printName(OS);
563  return OS;
564}
565
566}  // end namespace clang
567
568namespace llvm {
569/// Define DenseMapInfo so that DeclarationNames can be used as keys
570/// in DenseMap and DenseSets.
571template<>
572struct DenseMapInfo<clang::DeclarationName> {
573  static inline clang::DeclarationName getEmptyKey() {
574    return clang::DeclarationName::getEmptyMarker();
575  }
576
577  static inline clang::DeclarationName getTombstoneKey() {
578    return clang::DeclarationName::getTombstoneMarker();
579  }
580
581  static unsigned getHashValue(clang::DeclarationName Name) {
582    return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
583  }
584
585  static inline bool
586  isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
587    return LHS == RHS;
588  }
589};
590
591template <>
592struct isPodLike<clang::DeclarationName> { static const bool value = true; };
593
594}  // end namespace llvm
595
596#endif
597