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