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"
20 #include "llvm/Support/Compiler.h"
23 template <typename T> struct DenseMapInfo;
28 class CXXOperatorIdName;
29 class CXXLiteralOperatorIdName;
30 class DeclarationNameExtra;
32 class MultiKeywordSelector;
33 class UsingDirectiveDecl;
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.
42 class DeclarationName {
44 /// NameKind - The kind of name this object contains.
52 CXXConversionFunctionName,
54 CXXLiteralOperatorName,
59 /// StoredNameKind - The kind of name that is actually stored in the
60 /// upper bits of the Ptr field. This is only used internally.
62 /// Note: The entries here are synchronized with the entries in Selector,
63 /// for efficient translation between the two.
66 StoredObjCZeroArgSelector = 0x01,
67 StoredObjCOneArgSelector = 0x02,
68 StoredDeclarationNameExtra = 0x03,
72 /// Ptr - The lowest two bits are used to express what kind of name
73 /// we're actually storing, using the values of NameKind. Depending
74 /// on the kind of name this is, the upper bits of Ptr may have one
75 /// of several different meanings:
77 /// StoredIdentifier - The name is a normal identifier, and Ptr is
78 /// a normal IdentifierInfo pointer.
80 /// StoredObjCZeroArgSelector - The name is an Objective-C
81 /// selector with zero arguments, and Ptr is an IdentifierInfo
82 /// pointer pointing to the selector name.
84 /// StoredObjCOneArgSelector - The name is an Objective-C selector
85 /// with one argument, and Ptr is an IdentifierInfo pointer
86 /// pointing to the selector name.
88 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
89 /// DeclarationNameExtra structure, whose first value will tell us
90 /// whether this is an Objective-C selector, C++ operator-id name,
91 /// or special C++ name.
94 /// getStoredNameKind - Return the kind of object that is stored in
96 StoredNameKind getStoredNameKind() const {
97 return static_cast<StoredNameKind>(Ptr & PtrMask);
100 /// getExtra - Get the "extra" information associated with this
101 /// multi-argument selector or C++ special name.
102 DeclarationNameExtra *getExtra() const {
103 assert(getStoredNameKind() == StoredDeclarationNameExtra &&
104 "Declaration name does not store an Extra structure");
105 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
108 /// getAsCXXSpecialName - If the stored pointer is actually a
109 /// CXXSpecialName, returns a pointer to it. Otherwise, returns
111 CXXSpecialName *getAsCXXSpecialName() const {
112 NameKind Kind = getNameKind();
113 if (Kind >= CXXConstructorName && Kind <= CXXConversionFunctionName)
114 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
118 /// getAsCXXOperatorIdName
119 CXXOperatorIdName *getAsCXXOperatorIdName() const {
120 if (getNameKind() == CXXOperatorName)
121 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
125 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
126 if (getNameKind() == CXXLiteralOperatorName)
127 return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
131 // Construct a declaration name from the name of a C++ constructor,
132 // destructor, or conversion function.
133 DeclarationName(CXXSpecialName *Name)
134 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
135 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
136 Ptr |= StoredDeclarationNameExtra;
139 // Construct a declaration name from the name of a C++ overloaded
141 DeclarationName(CXXOperatorIdName *Name)
142 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
143 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
144 Ptr |= StoredDeclarationNameExtra;
147 DeclarationName(CXXLiteralOperatorIdName *Name)
148 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
149 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
150 Ptr |= StoredDeclarationNameExtra;
153 /// Construct a declaration name from a raw pointer.
154 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
156 friend class DeclarationNameTable;
157 friend class NamedDecl;
159 /// getFETokenInfoAsVoidSlow - Retrieves the front end-specified pointer
160 /// for this name as a void pointer if it's not an identifier.
161 void *getFETokenInfoAsVoidSlow() const;
164 /// DeclarationName - Used to create an empty selector.
165 DeclarationName() : Ptr(0) { }
167 // Construct a declaration name from an IdentifierInfo *.
168 DeclarationName(const IdentifierInfo *II)
169 : Ptr(reinterpret_cast<uintptr_t>(II)) {
170 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
173 // Construct a declaration name from an Objective-C selector.
174 DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) { }
176 /// getUsingDirectiveName - Return name for all using-directives.
177 static DeclarationName getUsingDirectiveName();
179 // operator bool() - Evaluates true when this declaration name is
181 operator bool() const {
182 return ((Ptr & PtrMask) != 0) ||
183 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
186 /// Predicate functions for querying what type of name this is.
187 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
188 bool isObjCZeroArgSelector() const {
189 return getStoredNameKind() == StoredObjCZeroArgSelector;
191 bool isObjCOneArgSelector() const {
192 return getStoredNameKind() == StoredObjCOneArgSelector;
195 /// getNameKind - Determine what kind of name this is.
196 NameKind getNameKind() const;
198 /// \brief Determines whether the name itself is dependent, e.g., because it
199 /// involves a C++ type that is itself dependent.
201 /// Note that this does not capture all of the notions of "dependent name",
202 /// because an identifier can be a dependent name if it is used as the
203 /// callee in a call expression with dependent arguments.
204 bool isDependentName() const;
206 /// getNameAsString - Retrieve the human-readable string for this name.
207 std::string getAsString() const;
209 /// printName - Print the human-readable name to a stream.
210 void printName(raw_ostream &OS) const;
212 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
213 /// this declaration name, or NULL if this declaration name isn't a
214 /// simple identifier.
215 IdentifierInfo *getAsIdentifierInfo() const {
217 return reinterpret_cast<IdentifierInfo *>(Ptr);
221 /// getAsOpaqueInteger - Get the representation of this declaration
222 /// name as an opaque integer.
223 uintptr_t getAsOpaqueInteger() const { return Ptr; }
225 /// getAsOpaquePtr - Get the representation of this declaration name as
226 /// an opaque pointer.
227 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
229 static DeclarationName getFromOpaquePtr(void *P) {
231 N.Ptr = reinterpret_cast<uintptr_t> (P);
235 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
241 /// getCXXNameType - If this name is one of the C++ names (of a
242 /// constructor, destructor, or conversion function), return the
243 /// type associated with that name.
244 QualType getCXXNameType() const;
246 /// getCXXOverloadedOperator - If this name is the name of an
247 /// overloadable operator in C++ (e.g., @c operator+), retrieve the
248 /// kind of overloaded operator.
249 OverloadedOperatorKind getCXXOverloadedOperator() const;
251 /// getCXXLiteralIdentifier - If this name is the name of a literal
252 /// operator, retrieve the identifier associated with it.
253 IdentifierInfo *getCXXLiteralIdentifier() const;
255 /// getObjCSelector - Get the Objective-C selector stored in this
256 /// declaration name.
257 Selector getObjCSelector() const {
258 assert((getNameKind() == ObjCZeroArgSelector ||
259 getNameKind() == ObjCOneArgSelector ||
260 getNameKind() == ObjCMultiArgSelector ||
261 Ptr == 0) && "Not a selector!");
262 return Selector(Ptr);
265 /// getFETokenInfo/setFETokenInfo - The language front-end is
266 /// allowed to associate arbitrary metadata with some kinds of
267 /// declaration names, including normal identifiers and C++
268 /// constructors, destructors, and conversion functions.
270 T *getFETokenInfo() const {
271 if (const IdentifierInfo *Info = getAsIdentifierInfo())
272 return Info->getFETokenInfo<T>();
273 return static_cast<T*>(getFETokenInfoAsVoidSlow());
276 void setFETokenInfo(void *T);
278 /// operator== - Determine whether the specified names are identical..
279 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
280 return LHS.Ptr == RHS.Ptr;
283 /// operator!= - Determine whether the specified names are different.
284 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
285 return LHS.Ptr != RHS.Ptr;
288 static DeclarationName getEmptyMarker() {
289 return DeclarationName(uintptr_t(-1));
292 static DeclarationName getTombstoneMarker() {
293 return DeclarationName(uintptr_t(-2));
296 static int compare(DeclarationName LHS, DeclarationName RHS);
301 /// Ordering on two declaration names. If both names are identifiers,
302 /// this provides a lexicographical ordering.
303 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
304 return DeclarationName::compare(LHS, RHS) < 0;
307 /// Ordering on two declaration names. If both names are identifiers,
308 /// this provides a lexicographical ordering.
309 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
310 return DeclarationName::compare(LHS, RHS) > 0;
313 /// Ordering on two declaration names. If both names are identifiers,
314 /// this provides a lexicographical ordering.
315 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
316 return DeclarationName::compare(LHS, RHS) <= 0;
319 /// Ordering on two declaration names. If both names are identifiers,
320 /// this provides a lexicographical ordering.
321 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
322 return DeclarationName::compare(LHS, RHS) >= 0;
325 /// DeclarationNameTable - Used to store and retrieve DeclarationName
326 /// instances for the various kinds of declaration names, e.g., normal
327 /// identifiers, C++ constructor names, etc. This class contains
328 /// uniqued versions of each of the C++ special names, which can be
329 /// retrieved using its member functions (e.g.,
330 /// getCXXConstructorName).
331 class DeclarationNameTable {
332 const ASTContext &Ctx;
333 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
334 CXXOperatorIdName *CXXOperatorNames; // Operator names
335 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
337 DeclarationNameTable(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
338 void operator=(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
341 DeclarationNameTable(const ASTContext &C);
342 ~DeclarationNameTable();
344 /// getIdentifier - Create a declaration name that is a simple
346 DeclarationName getIdentifier(const IdentifierInfo *ID) {
347 return DeclarationName(ID);
350 /// getCXXConstructorName - Returns the name of a C++ constructor
351 /// for the given Type.
352 DeclarationName getCXXConstructorName(CanQualType Ty) {
353 return getCXXSpecialName(DeclarationName::CXXConstructorName,
354 Ty.getUnqualifiedType());
357 /// getCXXDestructorName - Returns the name of a C++ destructor
358 /// for the given Type.
359 DeclarationName getCXXDestructorName(CanQualType Ty) {
360 return getCXXSpecialName(DeclarationName::CXXDestructorName,
361 Ty.getUnqualifiedType());
364 /// getCXXConversionFunctionName - Returns the name of a C++
365 /// conversion function for the given Type.
366 DeclarationName getCXXConversionFunctionName(CanQualType Ty) {
367 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty);
370 /// getCXXSpecialName - Returns a declaration name for special kind
371 /// of C++ name, e.g., for a constructor, destructor, or conversion
373 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
376 /// getCXXOperatorName - Get the name of the overloadable C++
377 /// operator corresponding to Op.
378 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
380 /// getCXXLiteralOperatorName - Get the name of the literal operator function
381 /// with II as the identifier.
382 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
385 /// DeclarationNameLoc - Additional source/type location info
386 /// for a declaration name. Needs a DeclarationName in order
387 /// to be interpreted correctly.
388 struct DeclarationNameLoc {
390 // The source location for identifier stored elsewhere.
391 // struct {} Identifier;
393 // Type info for constructors, destructors and conversion functions.
394 // Locations (if any) for the tilde (destructor) or operator keyword
395 // (conversion) are stored elsewhere.
397 TypeSourceInfo* TInfo;
400 // The location (if any) of the operator keyword is stored elsewhere.
402 unsigned BeginOpNameLoc;
403 unsigned EndOpNameLoc;
406 // The location (if any) of the operator keyword is stored elsewhere.
409 } CXXLiteralOperatorName;
411 // struct {} CXXUsingDirective;
412 // struct {} ObjCZeroArgSelector;
413 // struct {} ObjCOneArgSelector;
414 // struct {} ObjCMultiArgSelector;
417 DeclarationNameLoc(DeclarationName Name);
418 // FIXME: this should go away once all DNLocs are properly initialized.
419 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
420 }; // struct DeclarationNameLoc
423 /// DeclarationNameInfo - A collector data type for bundling together
424 /// a DeclarationName and the correspnding source/type location info.
425 struct DeclarationNameInfo {
427 /// Name - The declaration name, also encoding name kind.
428 DeclarationName Name;
429 /// Loc - The main source location for the declaration name.
430 SourceLocation NameLoc;
431 /// Info - Further source/type location info for special kinds of names.
432 DeclarationNameLoc LocInfo;
436 DeclarationNameInfo() {}
438 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
439 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
441 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
442 DeclarationNameLoc LocInfo)
443 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
445 /// getName - Returns the embedded declaration name.
446 DeclarationName getName() const { return Name; }
447 /// setName - Sets the embedded declaration name.
448 void setName(DeclarationName N) { Name = N; }
450 /// getLoc - Returns the main location of the declaration name.
451 SourceLocation getLoc() const { return NameLoc; }
452 /// setLoc - Sets the main location of the declaration name.
453 void setLoc(SourceLocation L) { NameLoc = L; }
455 const DeclarationNameLoc &getInfo() const { return LocInfo; }
456 DeclarationNameLoc &getInfo() { return LocInfo; }
457 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
459 /// getNamedTypeInfo - Returns the source type info associated to
460 /// the name. Assumes it is a constructor, destructor or conversion.
461 TypeSourceInfo *getNamedTypeInfo() const {
462 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
463 Name.getNameKind() == DeclarationName::CXXDestructorName ||
464 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
465 return LocInfo.NamedType.TInfo;
467 /// setNamedTypeInfo - Sets the source type info associated to
468 /// the name. Assumes it is a constructor, destructor or conversion.
469 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
470 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
471 Name.getNameKind() == DeclarationName::CXXDestructorName ||
472 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
473 LocInfo.NamedType.TInfo = TInfo;
476 /// getCXXOperatorNameRange - Gets the range of the operator name
477 /// (without the operator keyword). Assumes it is a (non-literal) operator.
478 SourceRange getCXXOperatorNameRange() const {
479 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
481 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
482 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
485 /// setCXXOperatorNameRange - Sets the range of the operator name
486 /// (without the operator keyword). Assumes it is a C++ operator.
487 void setCXXOperatorNameRange(SourceRange R) {
488 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
489 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
490 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
493 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
494 /// operator name (not the operator keyword).
495 /// Assumes it is a literal operator.
496 SourceLocation getCXXLiteralOperatorNameLoc() const {
497 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
498 return SourceLocation::
499 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
501 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
502 /// operator name (not the operator keyword).
503 /// Assumes it is a literal operator.
504 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
505 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
506 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
509 /// \brief Determine whether this name involves a template parameter.
510 bool isInstantiationDependent() const;
512 /// \brief Determine whether this name contains an unexpanded
514 bool containsUnexpandedParameterPack() const;
516 /// getAsString - Retrieve the human-readable string for this name.
517 std::string getAsString() const;
519 /// printName - Print the human-readable name to a stream.
520 void printName(raw_ostream &OS) const;
522 /// getBeginLoc - Retrieve the location of the first token.
523 SourceLocation getBeginLoc() const { return NameLoc; }
524 /// getEndLoc - Retrieve the location of the last token.
525 SourceLocation getEndLoc() const;
526 /// getSourceRange - The range of the declaration name.
527 SourceRange getSourceRange() const LLVM_READONLY {
528 SourceLocation BeginLoc = getBeginLoc();
529 SourceLocation EndLoc = getEndLoc();
530 return SourceRange(BeginLoc, EndLoc.isValid() ? EndLoc : BeginLoc);
532 SourceLocation getLocStart() const LLVM_READONLY {
533 return getBeginLoc();
535 SourceLocation getLocEnd() const LLVM_READONLY {
536 SourceLocation EndLoc = getEndLoc();
537 return EndLoc.isValid() ? EndLoc : getLocStart();
541 /// Insertion operator for diagnostics. This allows sending DeclarationName's
542 /// into a diagnostic with <<.
543 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
545 DB.AddTaggedVal(N.getAsOpaqueInteger(),
546 DiagnosticsEngine::ak_declarationname);
550 /// Insertion operator for partial diagnostics. This allows binding
551 /// DeclarationName's into a partial diagnostic with <<.
552 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
554 PD.AddTaggedVal(N.getAsOpaqueInteger(),
555 DiagnosticsEngine::ak_declarationname);
559 inline raw_ostream &operator<<(raw_ostream &OS,
560 DeclarationNameInfo DNInfo) {
561 DNInfo.printName(OS);
565 } // end namespace clang
568 /// Define DenseMapInfo so that DeclarationNames can be used as keys
569 /// in DenseMap and DenseSets.
571 struct DenseMapInfo<clang::DeclarationName> {
572 static inline clang::DeclarationName getEmptyKey() {
573 return clang::DeclarationName::getEmptyMarker();
576 static inline clang::DeclarationName getTombstoneKey() {
577 return clang::DeclarationName::getTombstoneMarker();
580 static unsigned getHashValue(clang::DeclarationName Name) {
581 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
585 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
591 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
593 } // end namespace llvm