1 //===-- DeclarationName.h - Representation of declaration names -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the DeclarationName and DeclarationNameTable classes.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
14 #define LLVM_CLANG_AST_DECLARATIONNAME_H
16 #include "clang/Basic/IdentifierTable.h"
17 #include "clang/AST/Type.h"
18 #include "clang/AST/CanonicalType.h"
19 #include "clang/Basic/PartialDiagnostic.h"
22 template <typename T> struct DenseMapInfo;
27 class CXXOperatorIdName;
28 class CXXLiteralOperatorIdName;
29 class DeclarationNameExtra;
31 class MultiKeywordSelector;
32 class UsingDirectiveDecl;
34 /// DeclarationName - The name of a declaration. In the common case,
35 /// this just stores an IdentifierInfo pointer to a normal
36 /// name. However, it also provides encodings for Objective-C
37 /// selectors (optimizing zero- and one-argument selectors, which make
38 /// up 78% percent of all selectors in Cocoa.h) and special C++ names
39 /// for constructors, destructors, and conversion functions.
40 class DeclarationName {
42 /// NameKind - The kind of name this object contains.
50 CXXConversionFunctionName,
52 CXXLiteralOperatorName,
57 /// StoredNameKind - The kind of name that is actually stored in the
58 /// upper bits of the Ptr field. This is only used internally.
61 StoredObjCZeroArgSelector,
62 StoredObjCOneArgSelector,
63 StoredDeclarationNameExtra,
67 /// Ptr - The lowest two bits are used to express what kind of name
68 /// we're actually storing, using the values of NameKind. Depending
69 /// on the kind of name this is, the upper bits of Ptr may have one
70 /// of several different meanings:
72 /// StoredIdentifier - The name is a normal identifier, and Ptr is
73 /// a normal IdentifierInfo pointer.
75 /// StoredObjCZeroArgSelector - The name is an Objective-C
76 /// selector with zero arguments, and Ptr is an IdentifierInfo
77 /// pointer pointing to the selector name.
79 /// StoredObjCOneArgSelector - The name is an Objective-C selector
80 /// with one argument, and Ptr is an IdentifierInfo pointer
81 /// pointing to the selector name.
83 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
84 /// DeclarationNameExtra structure, whose first value will tell us
85 /// whether this is an Objective-C selector, C++ operator-id name,
86 /// or special C++ name.
89 /// getStoredNameKind - Return the kind of object that is stored in
91 StoredNameKind getStoredNameKind() const {
92 return static_cast<StoredNameKind>(Ptr & PtrMask);
95 /// getExtra - Get the "extra" information associated with this
96 /// multi-argument selector or C++ special name.
97 DeclarationNameExtra *getExtra() const {
98 assert(getStoredNameKind() == StoredDeclarationNameExtra &&
99 "Declaration name does not store an Extra structure");
100 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
103 /// getAsCXXSpecialName - If the stored pointer is actually a
104 /// CXXSpecialName, returns a pointer to it. Otherwise, returns
106 CXXSpecialName *getAsCXXSpecialName() const {
107 if (getNameKind() >= CXXConstructorName &&
108 getNameKind() <= CXXConversionFunctionName)
109 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
113 /// getAsCXXOperatorIdName
114 CXXOperatorIdName *getAsCXXOperatorIdName() const {
115 if (getNameKind() == CXXOperatorName)
116 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
120 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
121 if (getNameKind() == CXXLiteralOperatorName)
122 return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
126 // Construct a declaration name from the name of a C++ constructor,
127 // destructor, or conversion function.
128 DeclarationName(CXXSpecialName *Name)
129 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
130 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
131 Ptr |= StoredDeclarationNameExtra;
134 // Construct a declaration name from the name of a C++ overloaded
136 DeclarationName(CXXOperatorIdName *Name)
137 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
138 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
139 Ptr |= StoredDeclarationNameExtra;
142 DeclarationName(CXXLiteralOperatorIdName *Name)
143 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
144 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
145 Ptr |= StoredDeclarationNameExtra;
148 /// Construct a declaration name from a raw pointer.
149 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
151 friend class DeclarationNameTable;
152 friend class NamedDecl;
154 /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer
155 /// for this name as a void pointer.
156 void *getFETokenInfoAsVoid() const;
159 /// DeclarationName - Used to create an empty selector.
160 DeclarationName() : Ptr(0) { }
162 // Construct a declaration name from an IdentifierInfo *.
163 DeclarationName(const IdentifierInfo *II)
164 : Ptr(reinterpret_cast<uintptr_t>(II)) {
165 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
168 // Construct a declaration name from an Objective-C selector.
169 DeclarationName(Selector Sel);
171 /// getUsingDirectiveName - Return name for all using-directives.
172 static DeclarationName getUsingDirectiveName();
174 // operator bool() - Evaluates true when this declaration name is
176 operator bool() const {
177 return ((Ptr & PtrMask) != 0) ||
178 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
181 /// Predicate functions for querying what type of name this is.
182 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
183 bool isObjCZeroArgSelector() const {
184 return getStoredNameKind() == StoredObjCZeroArgSelector;
186 bool isObjCOneArgSelector() const {
187 return getStoredNameKind() == StoredObjCOneArgSelector;
190 /// getNameKind - Determine what kind of name this is.
191 NameKind getNameKind() const;
193 /// \brief Determines whether the name itself is dependent, e.g., because it
194 /// involves a C++ type that is itself dependent.
196 /// Note that this does not capture all of the notions of "dependent name",
197 /// because an identifier can be a dependent name if it is used as the
198 /// callee in a call expression with dependent arguments.
199 bool isDependentName() const;
201 /// getNameAsString - Retrieve the human-readable string for this name.
202 std::string getAsString() const;
204 /// printName - Print the human-readable name to a stream.
205 void printName(llvm::raw_ostream &OS) const;
207 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
208 /// this declaration name, or NULL if this declaration name isn't a
209 /// simple identifier.
210 IdentifierInfo *getAsIdentifierInfo() const {
212 return reinterpret_cast<IdentifierInfo *>(Ptr);
216 /// getAsOpaqueInteger - Get the representation of this declaration
217 /// name as an opaque integer.
218 uintptr_t getAsOpaqueInteger() const { return Ptr; }
220 /// getAsOpaquePtr - Get the representation of this declaration name as
221 /// an opaque pointer.
222 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
224 static DeclarationName getFromOpaquePtr(void *P) {
226 N.Ptr = reinterpret_cast<uintptr_t> (P);
230 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
236 /// getCXXNameType - If this name is one of the C++ names (of a
237 /// constructor, destructor, or conversion function), return the
238 /// type associated with that name.
239 QualType getCXXNameType() const;
241 /// getCXXOverloadedOperator - If this name is the name of an
242 /// overloadable operator in C++ (e.g., @c operator+), retrieve the
243 /// kind of overloaded operator.
244 OverloadedOperatorKind getCXXOverloadedOperator() const;
246 /// getCXXLiteralIdentifier - If this name is the name of a literal
247 /// operator, retrieve the identifier associated with it.
248 IdentifierInfo *getCXXLiteralIdentifier() const;
250 /// getObjCSelector - Get the Objective-C selector stored in this
251 /// declaration name.
252 Selector getObjCSelector() const;
254 /// getFETokenInfo/setFETokenInfo - The language front-end is
255 /// allowed to associate arbitrary metadata with some kinds of
256 /// declaration names, including normal identifiers and C++
257 /// constructors, destructors, and conversion functions.
259 T *getFETokenInfo() const { return static_cast<T*>(getFETokenInfoAsVoid()); }
261 void setFETokenInfo(void *T);
263 /// operator== - Determine whether the specified names are identical..
264 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
265 return LHS.Ptr == RHS.Ptr;
268 /// operator!= - Determine whether the specified names are different.
269 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
270 return LHS.Ptr != RHS.Ptr;
273 static DeclarationName getEmptyMarker() {
274 return DeclarationName(uintptr_t(-1));
277 static DeclarationName getTombstoneMarker() {
278 return DeclarationName(uintptr_t(-2));
281 static int compare(DeclarationName LHS, DeclarationName RHS);
286 /// Ordering on two declaration names. If both names are identifiers,
287 /// this provides a lexicographical ordering.
288 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
289 return DeclarationName::compare(LHS, RHS) < 0;
292 /// Ordering on two declaration names. If both names are identifiers,
293 /// this provides a lexicographical ordering.
294 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
295 return DeclarationName::compare(LHS, RHS) > 0;
298 /// Ordering on two declaration names. If both names are identifiers,
299 /// this provides a lexicographical ordering.
300 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
301 return DeclarationName::compare(LHS, RHS) <= 0;
304 /// Ordering on two declaration names. If both names are identifiers,
305 /// this provides a lexicographical ordering.
306 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
307 return DeclarationName::compare(LHS, RHS) >= 0;
310 /// DeclarationNameTable - Used to store and retrieve DeclarationName
311 /// instances for the various kinds of declaration names, e.g., normal
312 /// identifiers, C++ constructor names, etc. This class contains
313 /// uniqued versions of each of the C++ special names, which can be
314 /// retrieved using its member functions (e.g.,
315 /// getCXXConstructorName).
316 class DeclarationNameTable {
318 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
319 CXXOperatorIdName *CXXOperatorNames; // Operator names
320 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
322 DeclarationNameTable(const DeclarationNameTable&); // NONCOPYABLE
323 DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE
326 DeclarationNameTable(ASTContext &C);
327 ~DeclarationNameTable();
329 /// getIdentifier - Create a declaration name that is a simple
331 DeclarationName getIdentifier(const IdentifierInfo *ID) {
332 return DeclarationName(ID);
335 /// getCXXConstructorName - Returns the name of a C++ constructor
336 /// for the given Type.
337 DeclarationName getCXXConstructorName(CanQualType Ty) {
338 return getCXXSpecialName(DeclarationName::CXXConstructorName,
339 Ty.getUnqualifiedType());
342 /// getCXXDestructorName - Returns the name of a C++ destructor
343 /// for the given Type.
344 DeclarationName getCXXDestructorName(CanQualType Ty) {
345 return getCXXSpecialName(DeclarationName::CXXDestructorName,
346 Ty.getUnqualifiedType());
349 /// getCXXConversionFunctionName - Returns the name of a C++
350 /// conversion function for the given Type.
351 DeclarationName getCXXConversionFunctionName(CanQualType Ty) {
352 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
355 /// getCXXSpecialName - Returns a declaration name for special kind
356 /// of C++ name, e.g., for a constructor, destructor, or conversion
358 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
361 /// getCXXOperatorName - Get the name of the overloadable C++
362 /// operator corresponding to Op.
363 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
365 /// getCXXLiteralOperatorName - Get the name of the literal operator function
366 /// with II as the identifier.
367 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
370 /// Insertion operator for diagnostics. This allows sending DeclarationName's
371 /// into a diagnostic with <<.
372 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
374 DB.AddTaggedVal(N.getAsOpaqueInteger(),
375 Diagnostic::ak_declarationname);
379 /// Insertion operator for partial diagnostics. This allows binding
380 /// DeclarationName's into a partial diagnostic with <<.
381 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
383 PD.AddTaggedVal(N.getAsOpaqueInteger(),
384 Diagnostic::ak_declarationname);
388 } // end namespace clang
391 /// Define DenseMapInfo so that DeclarationNames can be used as keys
392 /// in DenseMap and DenseSets.
394 struct DenseMapInfo<clang::DeclarationName> {
395 static inline clang::DeclarationName getEmptyKey() {
396 return clang::DeclarationName::getEmptyMarker();
399 static inline clang::DeclarationName getTombstoneKey() {
400 return clang::DeclarationName::getTombstoneMarker();
403 static unsigned getHashValue(clang::DeclarationName);
406 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
412 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
414 } // end namespace llvm