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