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;
35 /// DeclarationName - The name of a declaration. In the common case,
36 /// this just stores an IdentifierInfo pointer to a normal
37 /// name. However, it also provides encodings for Objective-C
38 /// selectors (optimizing zero- and one-argument selectors, which make
39 /// up 78% percent of all selectors in Cocoa.h) and special C++ names
40 /// for constructors, destructors, and conversion functions.
41 class DeclarationName {
43 /// NameKind - The kind of name this object contains.
51 CXXConversionFunctionName,
53 CXXLiteralOperatorName,
58 /// StoredNameKind - The kind of name that is actually stored in the
59 /// upper bits of the Ptr field. This is only used internally.
62 StoredObjCZeroArgSelector,
63 StoredObjCOneArgSelector,
64 StoredDeclarationNameExtra,
68 /// Ptr - The lowest two bits are used to express what kind of name
69 /// we're actually storing, using the values of NameKind. Depending
70 /// on the kind of name this is, the upper bits of Ptr may have one
71 /// of several different meanings:
73 /// StoredIdentifier - The name is a normal identifier, and Ptr is
74 /// a normal IdentifierInfo pointer.
76 /// StoredObjCZeroArgSelector - The name is an Objective-C
77 /// selector with zero arguments, and Ptr is an IdentifierInfo
78 /// pointer pointing to the selector name.
80 /// StoredObjCOneArgSelector - The name is an Objective-C selector
81 /// with one argument, and Ptr is an IdentifierInfo pointer
82 /// pointing to the selector name.
84 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
85 /// DeclarationNameExtra structure, whose first value will tell us
86 /// whether this is an Objective-C selector, C++ operator-id name,
87 /// or special C++ name.
90 /// getStoredNameKind - Return the kind of object that is stored in
92 StoredNameKind getStoredNameKind() const {
93 return static_cast<StoredNameKind>(Ptr & PtrMask);
96 /// getExtra - Get the "extra" information associated with this
97 /// multi-argument selector or C++ special name.
98 DeclarationNameExtra *getExtra() const {
99 assert(getStoredNameKind() == StoredDeclarationNameExtra &&
100 "Declaration name does not store an Extra structure");
101 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
104 /// getAsCXXSpecialName - If the stored pointer is actually a
105 /// CXXSpecialName, returns a pointer to it. Otherwise, returns
107 CXXSpecialName *getAsCXXSpecialName() const {
108 if (getNameKind() >= CXXConstructorName &&
109 getNameKind() <= CXXConversionFunctionName)
110 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
114 /// getAsCXXOperatorIdName
115 CXXOperatorIdName *getAsCXXOperatorIdName() const {
116 if (getNameKind() == CXXOperatorName)
117 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
121 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
122 if (getNameKind() == CXXLiteralOperatorName)
123 return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
127 // Construct a declaration name from the name of a C++ constructor,
128 // destructor, or conversion function.
129 DeclarationName(CXXSpecialName *Name)
130 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
131 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
132 Ptr |= StoredDeclarationNameExtra;
135 // Construct a declaration name from the name of a C++ overloaded
137 DeclarationName(CXXOperatorIdName *Name)
138 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
139 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
140 Ptr |= StoredDeclarationNameExtra;
143 DeclarationName(CXXLiteralOperatorIdName *Name)
144 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
145 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
146 Ptr |= StoredDeclarationNameExtra;
149 /// Construct a declaration name from a raw pointer.
150 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
152 friend class DeclarationNameTable;
153 friend class NamedDecl;
155 /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer
156 /// for this name as a void pointer.
157 void *getFETokenInfoAsVoid() const;
160 /// DeclarationName - Used to create an empty selector.
161 DeclarationName() : Ptr(0) { }
163 // Construct a declaration name from an IdentifierInfo *.
164 DeclarationName(const IdentifierInfo *II)
165 : Ptr(reinterpret_cast<uintptr_t>(II)) {
166 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
169 // Construct a declaration name from an Objective-C selector.
170 DeclarationName(Selector Sel);
172 /// getUsingDirectiveName - Return name for all using-directives.
173 static DeclarationName getUsingDirectiveName();
175 // operator bool() - Evaluates true when this declaration name is
177 operator bool() const {
178 return ((Ptr & PtrMask) != 0) ||
179 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
182 /// Predicate functions for querying what type of name this is.
183 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
184 bool isObjCZeroArgSelector() const {
185 return getStoredNameKind() == StoredObjCZeroArgSelector;
187 bool isObjCOneArgSelector() const {
188 return getStoredNameKind() == StoredObjCOneArgSelector;
191 /// getNameKind - Determine what kind of name this is.
192 NameKind getNameKind() const;
194 /// \brief Determines whether the name itself is dependent, e.g., because it
195 /// involves a C++ type that is itself dependent.
197 /// Note that this does not capture all of the notions of "dependent name",
198 /// because an identifier can be a dependent name if it is used as the
199 /// callee in a call expression with dependent arguments.
200 bool isDependentName() const;
202 /// getNameAsString - Retrieve the human-readable string for this name.
203 std::string getAsString() const;
205 /// printName - Print the human-readable name to a stream.
206 void printName(raw_ostream &OS) const;
208 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
209 /// this declaration name, or NULL if this declaration name isn't a
210 /// simple identifier.
211 IdentifierInfo *getAsIdentifierInfo() const {
213 return reinterpret_cast<IdentifierInfo *>(Ptr);
217 /// getAsOpaqueInteger - Get the representation of this declaration
218 /// name as an opaque integer.
219 uintptr_t getAsOpaqueInteger() const { return Ptr; }
221 /// getAsOpaquePtr - Get the representation of this declaration name as
222 /// an opaque pointer.
223 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
225 static DeclarationName getFromOpaquePtr(void *P) {
227 N.Ptr = reinterpret_cast<uintptr_t> (P);
231 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
237 /// getCXXNameType - If this name is one of the C++ names (of a
238 /// constructor, destructor, or conversion function), return the
239 /// type associated with that name.
240 QualType getCXXNameType() const;
242 /// getCXXOverloadedOperator - If this name is the name of an
243 /// overloadable operator in C++ (e.g., @c operator+), retrieve the
244 /// kind of overloaded operator.
245 OverloadedOperatorKind getCXXOverloadedOperator() const;
247 /// getCXXLiteralIdentifier - If this name is the name of a literal
248 /// operator, retrieve the identifier associated with it.
249 IdentifierInfo *getCXXLiteralIdentifier() const;
251 /// getObjCSelector - Get the Objective-C selector stored in this
252 /// declaration name.
253 Selector getObjCSelector() const;
255 /// getFETokenInfo/setFETokenInfo - The language front-end is
256 /// allowed to associate arbitrary metadata with some kinds of
257 /// declaration names, including normal identifiers and C++
258 /// constructors, destructors, and conversion functions.
260 T *getFETokenInfo() const { return static_cast<T*>(getFETokenInfoAsVoid()); }
262 void setFETokenInfo(void *T);
264 /// operator== - Determine whether the specified names are identical..
265 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
266 return LHS.Ptr == RHS.Ptr;
269 /// operator!= - Determine whether the specified names are different.
270 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
271 return LHS.Ptr != RHS.Ptr;
274 static DeclarationName getEmptyMarker() {
275 return DeclarationName(uintptr_t(-1));
278 static DeclarationName getTombstoneMarker() {
279 return DeclarationName(uintptr_t(-2));
282 static int compare(DeclarationName LHS, DeclarationName RHS);
287 /// Ordering on two declaration names. If both names are identifiers,
288 /// this provides a lexicographical ordering.
289 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
290 return DeclarationName::compare(LHS, RHS) < 0;
293 /// Ordering on two declaration names. If both names are identifiers,
294 /// this provides a lexicographical ordering.
295 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
296 return DeclarationName::compare(LHS, RHS) > 0;
299 /// Ordering on two declaration names. If both names are identifiers,
300 /// this provides a lexicographical ordering.
301 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
302 return DeclarationName::compare(LHS, RHS) <= 0;
305 /// Ordering on two declaration names. If both names are identifiers,
306 /// this provides a lexicographical ordering.
307 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
308 return DeclarationName::compare(LHS, RHS) >= 0;
311 /// DeclarationNameTable - Used to store and retrieve DeclarationName
312 /// instances for the various kinds of declaration names, e.g., normal
313 /// identifiers, C++ constructor names, etc. This class contains
314 /// uniqued versions of each of the C++ special names, which can be
315 /// retrieved using its member functions (e.g.,
316 /// getCXXConstructorName).
317 class DeclarationNameTable {
318 const ASTContext &Ctx;
319 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
320 CXXOperatorIdName *CXXOperatorNames; // Operator names
321 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
323 DeclarationNameTable(const DeclarationNameTable&); // NONCOPYABLE
324 DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE
327 DeclarationNameTable(const ASTContext &C);
328 ~DeclarationNameTable();
330 /// getIdentifier - Create a declaration name that is a simple
332 DeclarationName getIdentifier(const IdentifierInfo *ID) {
333 return DeclarationName(ID);
336 /// getCXXConstructorName - Returns the name of a C++ constructor
337 /// for the given Type.
338 DeclarationName getCXXConstructorName(CanQualType Ty) {
339 return getCXXSpecialName(DeclarationName::CXXConstructorName,
340 Ty.getUnqualifiedType());
343 /// getCXXDestructorName - Returns the name of a C++ destructor
344 /// for the given Type.
345 DeclarationName getCXXDestructorName(CanQualType Ty) {
346 return getCXXSpecialName(DeclarationName::CXXDestructorName,
347 Ty.getUnqualifiedType());
350 /// getCXXConversionFunctionName - Returns the name of a C++
351 /// conversion function for the given Type.
352 DeclarationName getCXXConversionFunctionName(CanQualType Ty) {
353 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
356 /// getCXXSpecialName - Returns a declaration name for special kind
357 /// of C++ name, e.g., for a constructor, destructor, or conversion
359 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
362 /// getCXXOperatorName - Get the name of the overloadable C++
363 /// operator corresponding to Op.
364 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
366 /// getCXXLiteralOperatorName - Get the name of the literal operator function
367 /// with II as the identifier.
368 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
371 /// DeclarationNameLoc - Additional source/type location info
372 /// for a declaration name. Needs a DeclarationName in order
373 /// to be interpreted correctly.
374 struct DeclarationNameLoc {
376 // The source location for identifier stored elsewhere.
377 // struct {} Identifier;
379 // Type info for constructors, destructors and conversion functions.
380 // Locations (if any) for the tilde (destructor) or operator keyword
381 // (conversion) are stored elsewhere.
383 TypeSourceInfo* TInfo;
386 // The location (if any) of the operator keyword is stored elsewhere.
388 unsigned BeginOpNameLoc;
389 unsigned EndOpNameLoc;
392 // The location (if any) of the operator keyword is stored elsewhere.
395 } CXXLiteralOperatorName;
397 // struct {} CXXUsingDirective;
398 // struct {} ObjCZeroArgSelector;
399 // struct {} ObjCOneArgSelector;
400 // struct {} ObjCMultiArgSelector;
403 DeclarationNameLoc(DeclarationName Name);
404 // FIXME: this should go away once all DNLocs are properly initialized.
405 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
406 }; // struct DeclarationNameLoc
409 /// DeclarationNameInfo - A collector data type for bundling together
410 /// a DeclarationName and the correspnding source/type location info.
411 struct DeclarationNameInfo {
413 /// Name - The declaration name, also encoding name kind.
414 DeclarationName Name;
415 /// Loc - The main source location for the declaration name.
416 SourceLocation NameLoc;
417 /// Info - Further source/type location info for special kinds of names.
418 DeclarationNameLoc LocInfo;
422 DeclarationNameInfo() {}
424 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
425 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
427 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
428 DeclarationNameLoc LocInfo)
429 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
431 /// getName - Returns the embedded declaration name.
432 DeclarationName getName() const { return Name; }
433 /// setName - Sets the embedded declaration name.
434 void setName(DeclarationName N) { Name = N; }
436 /// getLoc - Returns the main location of the declaration name.
437 SourceLocation getLoc() const { return NameLoc; }
438 /// setLoc - Sets the main location of the declaration name.
439 void setLoc(SourceLocation L) { NameLoc = L; }
441 const DeclarationNameLoc &getInfo() const { return LocInfo; }
442 DeclarationNameLoc &getInfo() { return LocInfo; }
443 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
445 /// getNamedTypeInfo - Returns the source type info associated to
446 /// the name. Assumes it is a constructor, destructor or conversion.
447 TypeSourceInfo *getNamedTypeInfo() const {
448 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
449 Name.getNameKind() == DeclarationName::CXXDestructorName ||
450 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
451 return LocInfo.NamedType.TInfo;
453 /// setNamedTypeInfo - Sets the source type info associated to
454 /// the name. Assumes it is a constructor, destructor or conversion.
455 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
456 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
457 Name.getNameKind() == DeclarationName::CXXDestructorName ||
458 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
459 LocInfo.NamedType.TInfo = TInfo;
462 /// getCXXOperatorNameRange - Gets the range of the operator name
463 /// (without the operator keyword). Assumes it is a (non-literal) operator.
464 SourceRange getCXXOperatorNameRange() const {
465 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
467 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
468 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
471 /// setCXXOperatorNameRange - Sets the range of the operator name
472 /// (without the operator keyword). Assumes it is a C++ operator.
473 void setCXXOperatorNameRange(SourceRange R) {
474 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
475 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
476 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
479 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
480 /// operator name (not the operator keyword).
481 /// Assumes it is a literal operator.
482 SourceLocation getCXXLiteralOperatorNameLoc() const {
483 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
484 return SourceLocation::
485 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
487 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
488 /// operator name (not the operator keyword).
489 /// Assumes it is a literal operator.
490 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
491 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
492 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
495 /// \brief Determine whether this name involves a template parameter.
496 bool isInstantiationDependent() const;
498 /// \brief Determine whether this name contains an unexpanded
500 bool containsUnexpandedParameterPack() const;
502 /// getAsString - Retrieve the human-readable string for this name.
503 std::string getAsString() const;
505 /// printName - Print the human-readable name to a stream.
506 void printName(raw_ostream &OS) const;
508 /// getBeginLoc - Retrieve the location of the first token.
509 SourceLocation getBeginLoc() const { return NameLoc; }
510 /// getEndLoc - Retrieve the location of the last token.
511 SourceLocation getEndLoc() const;
512 /// getSourceRange - The range of the declaration name.
513 SourceRange getSourceRange() const {
514 return SourceRange(getBeginLoc(), getEndLoc());
518 /// Insertion operator for diagnostics. This allows sending DeclarationName's
519 /// into a diagnostic with <<.
520 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
522 DB.AddTaggedVal(N.getAsOpaqueInteger(),
523 DiagnosticsEngine::ak_declarationname);
527 /// Insertion operator for partial diagnostics. This allows binding
528 /// DeclarationName's into a partial diagnostic with <<.
529 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
531 PD.AddTaggedVal(N.getAsOpaqueInteger(),
532 DiagnosticsEngine::ak_declarationname);
536 inline raw_ostream &operator<<(raw_ostream &OS,
537 DeclarationNameInfo DNInfo) {
538 DNInfo.printName(OS);
542 } // end namespace clang
545 /// Define DenseMapInfo so that DeclarationNames can be used as keys
546 /// in DenseMap and DenseSets.
548 struct DenseMapInfo<clang::DeclarationName> {
549 static inline clang::DeclarationName getEmptyKey() {
550 return clang::DeclarationName::getEmptyMarker();
553 static inline clang::DeclarationName getTombstoneKey() {
554 return clang::DeclarationName::getTombstoneMarker();
557 static unsigned getHashValue(clang::DeclarationName);
560 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
566 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
568 } // end namespace llvm