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/Basic/PartialDiagnostic.h"
18 #include "llvm/Support/Compiler.h"
21 template <typename T> struct DenseMapInfo;
26 class CXXLiteralOperatorIdName;
27 class CXXOperatorIdName;
29 class DeclarationNameExtra;
31 class MultiKeywordSelector;
32 enum OverloadedOperatorKind : int;
33 struct PrintingPolicy;
37 class UsingDirectiveDecl;
39 template <typename> class CanQual;
40 typedef CanQual<Type> CanQualType;
42 /// DeclarationName - The name of a declaration. In the common case,
43 /// this just stores an IdentifierInfo pointer to a normal
44 /// name. However, it also provides encodings for Objective-C
45 /// selectors (optimizing zero- and one-argument selectors, which make
46 /// up 78% percent of all selectors in Cocoa.h) and special C++ names
47 /// for constructors, destructors, and conversion functions.
48 class DeclarationName {
50 /// NameKind - The kind of name this object contains.
58 CXXConversionFunctionName,
60 CXXLiteralOperatorName,
63 static const unsigned NumNameKinds = CXXUsingDirective + 1;
66 /// StoredNameKind - The kind of name that is actually stored in the
67 /// upper bits of the Ptr field. This is only used internally.
69 /// Note: The entries here are synchronized with the entries in Selector,
70 /// for efficient translation between the two.
73 StoredObjCZeroArgSelector = 0x01,
74 StoredObjCOneArgSelector = 0x02,
75 StoredDeclarationNameExtra = 0x03,
79 /// Ptr - The lowest two bits are used to express what kind of name
80 /// we're actually storing, using the values of NameKind. Depending
81 /// on the kind of name this is, the upper bits of Ptr may have one
82 /// of several different meanings:
84 /// StoredIdentifier - The name is a normal identifier, and Ptr is
85 /// a normal IdentifierInfo pointer.
87 /// StoredObjCZeroArgSelector - The name is an Objective-C
88 /// selector with zero arguments, and Ptr is an IdentifierInfo
89 /// pointer pointing to the selector name.
91 /// StoredObjCOneArgSelector - The name is an Objective-C selector
92 /// with one argument, and Ptr is an IdentifierInfo pointer
93 /// pointing to the selector name.
95 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
96 /// DeclarationNameExtra structure, whose first value will tell us
97 /// whether this is an Objective-C selector, C++ operator-id name,
98 /// or special C++ name.
101 /// getStoredNameKind - Return the kind of object that is stored in
103 StoredNameKind getStoredNameKind() const {
104 return static_cast<StoredNameKind>(Ptr & PtrMask);
107 /// getExtra - Get the "extra" information associated with this
108 /// multi-argument selector or C++ special name.
109 DeclarationNameExtra *getExtra() const {
110 assert(getStoredNameKind() == StoredDeclarationNameExtra &&
111 "Declaration name does not store an Extra structure");
112 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
115 /// getAsCXXSpecialName - If the stored pointer is actually a
116 /// CXXSpecialName, returns a pointer to it. Otherwise, returns
118 CXXSpecialName *getAsCXXSpecialName() const {
119 NameKind Kind = getNameKind();
120 if (Kind >= CXXConstructorName && Kind <= CXXConversionFunctionName)
121 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
125 /// getAsCXXOperatorIdName
126 CXXOperatorIdName *getAsCXXOperatorIdName() const {
127 if (getNameKind() == CXXOperatorName)
128 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
132 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
133 if (getNameKind() == CXXLiteralOperatorName)
134 return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
138 // Construct a declaration name from the name of a C++ constructor,
139 // destructor, or conversion function.
140 DeclarationName(CXXSpecialName *Name)
141 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
142 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
143 Ptr |= StoredDeclarationNameExtra;
146 // Construct a declaration name from the name of a C++ overloaded
148 DeclarationName(CXXOperatorIdName *Name)
149 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
150 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
151 Ptr |= StoredDeclarationNameExtra;
154 DeclarationName(CXXLiteralOperatorIdName *Name)
155 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
156 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
157 Ptr |= StoredDeclarationNameExtra;
160 /// Construct a declaration name from a raw pointer.
161 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
163 friend class DeclarationNameTable;
164 friend class NamedDecl;
166 /// getFETokenInfoAsVoidSlow - Retrieves the front end-specified pointer
167 /// for this name as a void pointer if it's not an identifier.
168 void *getFETokenInfoAsVoidSlow() const;
171 /// DeclarationName - Used to create an empty selector.
172 DeclarationName() : Ptr(0) { }
174 // Construct a declaration name from an IdentifierInfo *.
175 DeclarationName(const IdentifierInfo *II)
176 : Ptr(reinterpret_cast<uintptr_t>(II)) {
177 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
180 // Construct a declaration name from an Objective-C selector.
181 DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) { }
183 /// getUsingDirectiveName - Return name for all using-directives.
184 static DeclarationName getUsingDirectiveName();
186 // operator bool() - Evaluates true when this declaration name is
188 explicit operator bool() const {
189 return ((Ptr & PtrMask) != 0) ||
190 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
193 /// \brief Evaluates true when this declaration name is empty.
194 bool isEmpty() const {
198 /// Predicate functions for querying what type of name this is.
199 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
200 bool isObjCZeroArgSelector() const {
201 return getStoredNameKind() == StoredObjCZeroArgSelector;
203 bool isObjCOneArgSelector() const {
204 return getStoredNameKind() == StoredObjCOneArgSelector;
207 /// getNameKind - Determine what kind of name this is.
208 NameKind getNameKind() const;
210 /// \brief Determines whether the name itself is dependent, e.g., because it
211 /// involves a C++ type that is itself dependent.
213 /// Note that this does not capture all of the notions of "dependent name",
214 /// because an identifier can be a dependent name if it is used as the
215 /// callee in a call expression with dependent arguments.
216 bool isDependentName() const;
218 /// getNameAsString - Retrieve the human-readable string for this name.
219 std::string getAsString() const;
221 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
222 /// this declaration name, or NULL if this declaration name isn't a
223 /// simple identifier.
224 IdentifierInfo *getAsIdentifierInfo() const {
226 return reinterpret_cast<IdentifierInfo *>(Ptr);
230 /// getAsOpaqueInteger - Get the representation of this declaration
231 /// name as an opaque integer.
232 uintptr_t getAsOpaqueInteger() const { return Ptr; }
234 /// getAsOpaquePtr - Get the representation of this declaration name as
235 /// an opaque pointer.
236 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
238 static DeclarationName getFromOpaquePtr(void *P) {
240 N.Ptr = reinterpret_cast<uintptr_t> (P);
244 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
250 /// getCXXNameType - If this name is one of the C++ names (of a
251 /// constructor, destructor, or conversion function), return the
252 /// type associated with that name.
253 QualType getCXXNameType() const;
255 /// getCXXOverloadedOperator - If this name is the name of an
256 /// overloadable operator in C++ (e.g., @c operator+), retrieve the
257 /// kind of overloaded operator.
258 OverloadedOperatorKind getCXXOverloadedOperator() const;
260 /// getCXXLiteralIdentifier - If this name is the name of a literal
261 /// operator, retrieve the identifier associated with it.
262 IdentifierInfo *getCXXLiteralIdentifier() const;
264 /// getObjCSelector - Get the Objective-C selector stored in this
265 /// declaration name.
266 Selector getObjCSelector() const {
267 assert((getNameKind() == ObjCZeroArgSelector ||
268 getNameKind() == ObjCOneArgSelector ||
269 getNameKind() == ObjCMultiArgSelector ||
270 Ptr == 0) && "Not a selector!");
271 return Selector(Ptr);
274 /// getFETokenInfo/setFETokenInfo - The language front-end is
275 /// allowed to associate arbitrary metadata with some kinds of
276 /// declaration names, including normal identifiers and C++
277 /// constructors, destructors, and conversion functions.
279 T *getFETokenInfo() const {
280 if (const IdentifierInfo *Info = getAsIdentifierInfo())
281 return Info->getFETokenInfo<T>();
282 return static_cast<T*>(getFETokenInfoAsVoidSlow());
285 void setFETokenInfo(void *T);
287 /// operator== - Determine whether the specified names are identical..
288 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
289 return LHS.Ptr == RHS.Ptr;
292 /// operator!= - Determine whether the specified names are different.
293 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
294 return LHS.Ptr != RHS.Ptr;
297 static DeclarationName getEmptyMarker() {
298 return DeclarationName(uintptr_t(-1));
301 static DeclarationName getTombstoneMarker() {
302 return DeclarationName(uintptr_t(-2));
305 static int compare(DeclarationName LHS, DeclarationName RHS);
307 void print(raw_ostream &OS, const PrintingPolicy &Policy);
312 raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
314 /// Ordering on two declaration names. If both names are identifiers,
315 /// this provides a lexicographical ordering.
316 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
317 return DeclarationName::compare(LHS, RHS) < 0;
320 /// Ordering on two declaration names. If both names are identifiers,
321 /// this provides a lexicographical ordering.
322 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
323 return DeclarationName::compare(LHS, RHS) > 0;
326 /// Ordering on two declaration names. If both names are identifiers,
327 /// this provides a lexicographical ordering.
328 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
329 return DeclarationName::compare(LHS, RHS) <= 0;
332 /// Ordering on two declaration names. If both names are identifiers,
333 /// this provides a lexicographical ordering.
334 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
335 return DeclarationName::compare(LHS, RHS) >= 0;
338 /// DeclarationNameTable - Used to store and retrieve DeclarationName
339 /// instances for the various kinds of declaration names, e.g., normal
340 /// identifiers, C++ constructor names, etc. This class contains
341 /// uniqued versions of each of the C++ special names, which can be
342 /// retrieved using its member functions (e.g.,
343 /// getCXXConstructorName).
344 class DeclarationNameTable {
345 const ASTContext &Ctx;
346 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
347 CXXOperatorIdName *CXXOperatorNames; // Operator names
348 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
350 DeclarationNameTable(const DeclarationNameTable&) = delete;
351 void operator=(const DeclarationNameTable&) = delete;
354 DeclarationNameTable(const ASTContext &C);
355 ~DeclarationNameTable();
357 /// getIdentifier - Create a declaration name that is a simple
359 DeclarationName getIdentifier(const IdentifierInfo *ID) {
360 return DeclarationName(ID);
363 /// getCXXConstructorName - Returns the name of a C++ constructor
364 /// for the given Type.
365 DeclarationName getCXXConstructorName(CanQualType Ty);
367 /// getCXXDestructorName - Returns the name of a C++ destructor
368 /// for the given Type.
369 DeclarationName getCXXDestructorName(CanQualType Ty);
371 /// getCXXConversionFunctionName - Returns the name of a C++
372 /// conversion function for the given Type.
373 DeclarationName getCXXConversionFunctionName(CanQualType Ty);
375 /// getCXXSpecialName - Returns a declaration name for special kind
376 /// of C++ name, e.g., for a constructor, destructor, or conversion
378 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
381 /// getCXXOperatorName - Get the name of the overloadable C++
382 /// operator corresponding to Op.
383 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
385 /// getCXXLiteralOperatorName - Get the name of the literal operator function
386 /// with II as the identifier.
387 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
390 /// DeclarationNameLoc - Additional source/type location info
391 /// for a declaration name. Needs a DeclarationName in order
392 /// to be interpreted correctly.
393 struct DeclarationNameLoc {
394 // The source location for identifier stored elsewhere.
395 // struct {} Identifier;
397 // Type info for constructors, destructors and conversion functions.
398 // Locations (if any) for the tilde (destructor) or operator keyword
399 // (conversion) are stored elsewhere.
401 TypeSourceInfo *TInfo;
404 // The location (if any) of the operator keyword is stored elsewhere.
406 unsigned BeginOpNameLoc;
407 unsigned EndOpNameLoc;
410 // The location (if any) of the operator keyword is stored elsewhere.
411 struct CXXLitOpName {
415 // struct {} CXXUsingDirective;
416 // struct {} ObjCZeroArgSelector;
417 // struct {} ObjCOneArgSelector;
418 // struct {} ObjCMultiArgSelector;
421 struct CXXOpName CXXOperatorName;
422 struct CXXLitOpName CXXLiteralOperatorName;
425 DeclarationNameLoc(DeclarationName Name);
426 // FIXME: this should go away once all DNLocs are properly initialized.
427 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
428 }; // struct DeclarationNameLoc
431 /// DeclarationNameInfo - A collector data type for bundling together
432 /// a DeclarationName and the correspnding source/type location info.
433 struct DeclarationNameInfo {
435 /// Name - The declaration name, also encoding name kind.
436 DeclarationName Name;
437 /// Loc - The main source location for the declaration name.
438 SourceLocation NameLoc;
439 /// Info - Further source/type location info for special kinds of names.
440 DeclarationNameLoc LocInfo;
444 DeclarationNameInfo() {}
446 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
447 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
449 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
450 DeclarationNameLoc LocInfo)
451 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
453 /// getName - Returns the embedded declaration name.
454 DeclarationName getName() const { return Name; }
455 /// setName - Sets the embedded declaration name.
456 void setName(DeclarationName N) { Name = N; }
458 /// getLoc - Returns the main location of the declaration name.
459 SourceLocation getLoc() const { return NameLoc; }
460 /// setLoc - Sets the main location of the declaration name.
461 void setLoc(SourceLocation L) { NameLoc = L; }
463 const DeclarationNameLoc &getInfo() const { return LocInfo; }
464 DeclarationNameLoc &getInfo() { return LocInfo; }
465 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
467 /// getNamedTypeInfo - Returns the source type info associated to
468 /// the name. Assumes it is a constructor, destructor or conversion.
469 TypeSourceInfo *getNamedTypeInfo() const {
470 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
471 Name.getNameKind() == DeclarationName::CXXDestructorName ||
472 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
473 return LocInfo.NamedType.TInfo;
475 /// setNamedTypeInfo - Sets the source type info associated to
476 /// the name. Assumes it is a constructor, destructor or conversion.
477 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
478 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
479 Name.getNameKind() == DeclarationName::CXXDestructorName ||
480 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
481 LocInfo.NamedType.TInfo = TInfo;
484 /// getCXXOperatorNameRange - Gets the range of the operator name
485 /// (without the operator keyword). Assumes it is a (non-literal) operator.
486 SourceRange getCXXOperatorNameRange() const {
487 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
489 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
490 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
493 /// setCXXOperatorNameRange - Sets the range of the operator name
494 /// (without the operator keyword). Assumes it is a C++ operator.
495 void setCXXOperatorNameRange(SourceRange R) {
496 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
497 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
498 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
501 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
502 /// operator name (not the operator keyword).
503 /// Assumes it is a literal operator.
504 SourceLocation getCXXLiteralOperatorNameLoc() const {
505 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
506 return SourceLocation::
507 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
509 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
510 /// operator name (not the operator keyword).
511 /// Assumes it is a literal operator.
512 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
513 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
514 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
517 /// \brief Determine whether this name involves a template parameter.
518 bool isInstantiationDependent() const;
520 /// \brief Determine whether this name contains an unexpanded
522 bool containsUnexpandedParameterPack() const;
524 /// getAsString - Retrieve the human-readable string for this name.
525 std::string getAsString() const;
527 /// printName - Print the human-readable name to a stream.
528 void printName(raw_ostream &OS) const;
530 /// getBeginLoc - Retrieve the location of the first token.
531 SourceLocation getBeginLoc() const { return NameLoc; }
532 /// getEndLoc - Retrieve the location of the last token.
533 SourceLocation getEndLoc() const;
534 /// getSourceRange - The range of the declaration name.
535 SourceRange getSourceRange() const LLVM_READONLY {
536 return SourceRange(getLocStart(), getLocEnd());
538 SourceLocation getLocStart() const LLVM_READONLY {
539 return getBeginLoc();
541 SourceLocation getLocEnd() const LLVM_READONLY {
542 SourceLocation EndLoc = getEndLoc();
543 return EndLoc.isValid() ? EndLoc : getLocStart();
547 /// Insertion operator for diagnostics. This allows sending DeclarationName's
548 /// into a diagnostic with <<.
549 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
551 DB.AddTaggedVal(N.getAsOpaqueInteger(),
552 DiagnosticsEngine::ak_declarationname);
556 /// Insertion operator for partial diagnostics. This allows binding
557 /// DeclarationName's into a partial diagnostic with <<.
558 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
560 PD.AddTaggedVal(N.getAsOpaqueInteger(),
561 DiagnosticsEngine::ak_declarationname);
565 inline raw_ostream &operator<<(raw_ostream &OS,
566 DeclarationNameInfo DNInfo) {
567 DNInfo.printName(OS);
571 } // end namespace clang
574 /// Define DenseMapInfo so that DeclarationNames can be used as keys
575 /// in DenseMap and DenseSets.
577 struct DenseMapInfo<clang::DeclarationName> {
578 static inline clang::DeclarationName getEmptyKey() {
579 return clang::DeclarationName::getEmptyMarker();
582 static inline clang::DeclarationName getTombstoneKey() {
583 return clang::DeclarationName::getTombstoneMarker();
586 static unsigned getHashValue(clang::DeclarationName Name) {
587 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
591 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
597 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
599 } // end namespace llvm