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.
63 StoredObjCZeroArgSelector,
64 StoredObjCOneArgSelector,
65 StoredDeclarationNameExtra,
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:
74 /// StoredIdentifier - The name is a normal identifier, and Ptr is
75 /// a normal IdentifierInfo pointer.
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.
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.
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.
91 /// getStoredNameKind - Return the kind of object that is stored in
93 StoredNameKind getStoredNameKind() const {
94 return static_cast<StoredNameKind>(Ptr & PtrMask);
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);
105 /// getAsCXXSpecialName - If the stored pointer is actually a
106 /// CXXSpecialName, returns a pointer to it. Otherwise, returns
108 CXXSpecialName *getAsCXXSpecialName() const {
109 if (getNameKind() >= CXXConstructorName &&
110 getNameKind() <= CXXConversionFunctionName)
111 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
115 /// getAsCXXOperatorIdName
116 CXXOperatorIdName *getAsCXXOperatorIdName() const {
117 if (getNameKind() == CXXOperatorName)
118 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
122 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
123 if (getNameKind() == CXXLiteralOperatorName)
124 return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
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;
136 // Construct a declaration name from the name of a C++ overloaded
138 DeclarationName(CXXOperatorIdName *Name)
139 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
140 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
141 Ptr |= StoredDeclarationNameExtra;
144 DeclarationName(CXXLiteralOperatorIdName *Name)
145 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
146 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
147 Ptr |= StoredDeclarationNameExtra;
150 /// Construct a declaration name from a raw pointer.
151 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
153 friend class DeclarationNameTable;
154 friend class NamedDecl;
156 /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer
157 /// for this name as a void pointer.
158 void *getFETokenInfoAsVoid() const;
161 /// DeclarationName - Used to create an empty selector.
162 DeclarationName() : Ptr(0) { }
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");
170 // Construct a declaration name from an Objective-C selector.
171 DeclarationName(Selector Sel);
173 /// getUsingDirectiveName - Return name for all using-directives.
174 static DeclarationName getUsingDirectiveName();
176 // operator bool() - Evaluates true when this declaration name is
178 operator bool() const {
179 return ((Ptr & PtrMask) != 0) ||
180 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
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;
188 bool isObjCOneArgSelector() const {
189 return getStoredNameKind() == StoredObjCOneArgSelector;
192 /// getNameKind - Determine what kind of name this is.
193 NameKind getNameKind() const;
195 /// \brief Determines whether the name itself is dependent, e.g., because it
196 /// involves a C++ type that is itself dependent.
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;
203 /// getNameAsString - Retrieve the human-readable string for this name.
204 std::string getAsString() const;
206 /// printName - Print the human-readable name to a stream.
207 void printName(raw_ostream &OS) const;
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 {
214 return reinterpret_cast<IdentifierInfo *>(Ptr);
218 /// getAsOpaqueInteger - Get the representation of this declaration
219 /// name as an opaque integer.
220 uintptr_t getAsOpaqueInteger() const { return Ptr; }
222 /// getAsOpaquePtr - Get the representation of this declaration name as
223 /// an opaque pointer.
224 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
226 static DeclarationName getFromOpaquePtr(void *P) {
228 N.Ptr = reinterpret_cast<uintptr_t> (P);
232 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
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;
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;
248 /// getCXXLiteralIdentifier - If this name is the name of a literal
249 /// operator, retrieve the identifier associated with it.
250 IdentifierInfo *getCXXLiteralIdentifier() const;
252 /// getObjCSelector - Get the Objective-C selector stored in this
253 /// declaration name.
254 Selector getObjCSelector() const;
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.
261 T *getFETokenInfo() const { return static_cast<T*>(getFETokenInfoAsVoid()); }
263 void setFETokenInfo(void *T);
265 /// operator== - Determine whether the specified names are identical..
266 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
267 return LHS.Ptr == RHS.Ptr;
270 /// operator!= - Determine whether the specified names are different.
271 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
272 return LHS.Ptr != RHS.Ptr;
275 static DeclarationName getEmptyMarker() {
276 return DeclarationName(uintptr_t(-1));
279 static DeclarationName getTombstoneMarker() {
280 return DeclarationName(uintptr_t(-2));
283 static int compare(DeclarationName LHS, DeclarationName RHS);
288 /// Ordering on two declaration names. If both names are identifiers,
289 /// this provides a lexicographical ordering.
290 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
291 return DeclarationName::compare(LHS, RHS) < 0;
294 /// Ordering on two declaration names. If both names are identifiers,
295 /// this provides a lexicographical ordering.
296 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
297 return DeclarationName::compare(LHS, RHS) > 0;
300 /// Ordering on two declaration names. If both names are identifiers,
301 /// this provides a lexicographical ordering.
302 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
303 return DeclarationName::compare(LHS, RHS) <= 0;
306 /// Ordering on two declaration names. If both names are identifiers,
307 /// this provides a lexicographical ordering.
308 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
309 return DeclarationName::compare(LHS, RHS) >= 0;
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).
318 class DeclarationNameTable {
319 const ASTContext &Ctx;
320 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
321 CXXOperatorIdName *CXXOperatorNames; // Operator names
322 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
324 DeclarationNameTable(const DeclarationNameTable&); // NONCOPYABLE
325 DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE
328 DeclarationNameTable(const ASTContext &C);
329 ~DeclarationNameTable();
331 /// getIdentifier - Create a declaration name that is a simple
333 DeclarationName getIdentifier(const IdentifierInfo *ID) {
334 return DeclarationName(ID);
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());
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());
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);
357 /// getCXXSpecialName - Returns a declaration name for special kind
358 /// of C++ name, e.g., for a constructor, destructor, or conversion
360 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
363 /// getCXXOperatorName - Get the name of the overloadable C++
364 /// operator corresponding to Op.
365 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
367 /// getCXXLiteralOperatorName - Get the name of the literal operator function
368 /// with II as the identifier.
369 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
372 /// DeclarationNameLoc - Additional source/type location info
373 /// for a declaration name. Needs a DeclarationName in order
374 /// to be interpreted correctly.
375 struct DeclarationNameLoc {
377 // The source location for identifier stored elsewhere.
378 // struct {} Identifier;
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.
384 TypeSourceInfo* TInfo;
387 // The location (if any) of the operator keyword is stored elsewhere.
389 unsigned BeginOpNameLoc;
390 unsigned EndOpNameLoc;
393 // The location (if any) of the operator keyword is stored elsewhere.
396 } CXXLiteralOperatorName;
398 // struct {} CXXUsingDirective;
399 // struct {} ObjCZeroArgSelector;
400 // struct {} ObjCOneArgSelector;
401 // struct {} ObjCMultiArgSelector;
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
410 /// DeclarationNameInfo - A collector data type for bundling together
411 /// a DeclarationName and the correspnding source/type location info.
412 struct DeclarationNameInfo {
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;
423 DeclarationNameInfo() {}
425 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
426 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
428 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
429 DeclarationNameLoc LocInfo)
430 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
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; }
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; }
442 const DeclarationNameLoc &getInfo() const { return LocInfo; }
443 DeclarationNameLoc &getInfo() { return LocInfo; }
444 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
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;
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;
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);
468 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
469 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
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();
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);
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();
496 /// \brief Determine whether this name involves a template parameter.
497 bool isInstantiationDependent() const;
499 /// \brief Determine whether this name contains an unexpanded
501 bool containsUnexpandedParameterPack() const;
503 /// getAsString - Retrieve the human-readable string for this name.
504 std::string getAsString() const;
506 /// printName - Print the human-readable name to a stream.
507 void printName(raw_ostream &OS) const;
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);
519 SourceLocation getLocStart() const LLVM_READONLY {
520 return getBeginLoc();
522 SourceLocation getLocEnd() const LLVM_READONLY {
523 SourceLocation EndLoc = getEndLoc();
524 return EndLoc.isValid() ? EndLoc : getLocStart();
528 /// Insertion operator for diagnostics. This allows sending DeclarationName's
529 /// into a diagnostic with <<.
530 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
532 DB.AddTaggedVal(N.getAsOpaqueInteger(),
533 DiagnosticsEngine::ak_declarationname);
537 /// Insertion operator for partial diagnostics. This allows binding
538 /// DeclarationName's into a partial diagnostic with <<.
539 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
541 PD.AddTaggedVal(N.getAsOpaqueInteger(),
542 DiagnosticsEngine::ak_declarationname);
546 inline raw_ostream &operator<<(raw_ostream &OS,
547 DeclarationNameInfo DNInfo) {
548 DNInfo.printName(OS);
552 } // end namespace clang
555 /// Define DenseMapInfo so that DeclarationNames can be used as keys
556 /// in DenseMap and DenseSets.
558 struct DenseMapInfo<clang::DeclarationName> {
559 static inline clang::DeclarationName getEmptyKey() {
560 return clang::DeclarationName::getEmptyMarker();
563 static inline clang::DeclarationName getTombstoneKey() {
564 return clang::DeclarationName::getTombstoneMarker();
567 static unsigned getHashValue(clang::DeclarationName);
570 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
576 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
578 } // end namespace llvm