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 CXXDeductionGuideNameExtra;
27 class CXXLiteralOperatorIdName;
28 class CXXOperatorIdName;
30 class DeclarationNameExtra;
32 class MultiKeywordSelector;
33 enum OverloadedOperatorKind : int;
34 struct PrintingPolicy;
39 class UsingDirectiveDecl;
41 template <typename> class CanQual;
42 typedef CanQual<Type> CanQualType;
44 /// DeclarationName - The name of a declaration. In the common case,
45 /// this just stores an IdentifierInfo pointer to a normal
46 /// name. However, it also provides encodings for Objective-C
47 /// selectors (optimizing zero- and one-argument selectors, which make
48 /// up 78% percent of all selectors in Cocoa.h) and special C++ names
49 /// for constructors, destructors, and conversion functions.
50 class DeclarationName {
52 /// NameKind - The kind of name this object contains.
60 CXXConversionFunctionName,
61 CXXDeductionGuideName,
63 CXXLiteralOperatorName,
66 static const unsigned NumNameKinds = CXXUsingDirective + 1;
69 /// StoredNameKind - The kind of name that is actually stored in the
70 /// upper bits of the Ptr field. This is only used internally.
72 /// Note: The entries here are synchronized with the entries in Selector,
73 /// for efficient translation between the two.
76 StoredObjCZeroArgSelector = 0x01,
77 StoredObjCOneArgSelector = 0x02,
78 StoredDeclarationNameExtra = 0x03,
82 /// Ptr - The lowest two bits are used to express what kind of name
83 /// we're actually storing, using the values of NameKind. Depending
84 /// on the kind of name this is, the upper bits of Ptr may have one
85 /// of several different meanings:
87 /// StoredIdentifier - The name is a normal identifier, and Ptr is
88 /// a normal IdentifierInfo pointer.
90 /// StoredObjCZeroArgSelector - The name is an Objective-C
91 /// selector with zero arguments, and Ptr is an IdentifierInfo
92 /// pointer pointing to the selector name.
94 /// StoredObjCOneArgSelector - The name is an Objective-C selector
95 /// with one argument, and Ptr is an IdentifierInfo pointer
96 /// pointing to the selector name.
98 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
99 /// DeclarationNameExtra structure, whose first value will tell us
100 /// whether this is an Objective-C selector, C++ operator-id name,
101 /// or special C++ name.
104 /// getStoredNameKind - Return the kind of object that is stored in
106 StoredNameKind getStoredNameKind() const {
107 return static_cast<StoredNameKind>(Ptr & PtrMask);
110 /// getExtra - Get the "extra" information associated with this
111 /// multi-argument selector or C++ special name.
112 DeclarationNameExtra *getExtra() const {
113 assert(getStoredNameKind() == StoredDeclarationNameExtra &&
114 "Declaration name does not store an Extra structure");
115 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
118 /// getAsCXXSpecialName - If the stored pointer is actually a
119 /// CXXSpecialName, returns a pointer to it. Otherwise, returns
121 CXXSpecialName *getAsCXXSpecialName() const {
122 NameKind Kind = getNameKind();
123 if (Kind >= CXXConstructorName && Kind <= CXXConversionFunctionName)
124 return reinterpret_cast<CXXSpecialName *>(getExtra());
128 /// If the stored pointer is actually a CXXDeductionGuideNameExtra, returns a
129 /// pointer to it. Otherwise, returns a NULL pointer.
130 CXXDeductionGuideNameExtra *getAsCXXDeductionGuideNameExtra() const {
131 if (getNameKind() == CXXDeductionGuideName)
132 return reinterpret_cast<CXXDeductionGuideNameExtra *>(getExtra());
136 /// getAsCXXOperatorIdName
137 CXXOperatorIdName *getAsCXXOperatorIdName() const {
138 if (getNameKind() == CXXOperatorName)
139 return reinterpret_cast<CXXOperatorIdName *>(getExtra());
143 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
144 if (getNameKind() == CXXLiteralOperatorName)
145 return reinterpret_cast<CXXLiteralOperatorIdName *>(getExtra());
149 // Construct a declaration name from the name of a C++ constructor,
150 // destructor, or conversion function.
151 DeclarationName(DeclarationNameExtra *Name)
152 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
153 assert((Ptr & PtrMask) == 0 && "Improperly aligned DeclarationNameExtra");
154 Ptr |= StoredDeclarationNameExtra;
157 /// Construct a declaration name from a raw pointer.
158 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
160 friend class DeclarationNameTable;
161 friend class NamedDecl;
163 /// getFETokenInfoAsVoidSlow - Retrieves the front end-specified pointer
164 /// for this name as a void pointer if it's not an identifier.
165 void *getFETokenInfoAsVoidSlow() const;
168 /// DeclarationName - Used to create an empty selector.
169 DeclarationName() : Ptr(0) { }
171 // Construct a declaration name from an IdentifierInfo *.
172 DeclarationName(const IdentifierInfo *II)
173 : Ptr(reinterpret_cast<uintptr_t>(II)) {
174 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
177 // Construct a declaration name from an Objective-C selector.
178 DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) { }
180 /// getUsingDirectiveName - Return name for all using-directives.
181 static DeclarationName getUsingDirectiveName();
183 // operator bool() - Evaluates true when this declaration name is
185 explicit operator bool() const {
186 return ((Ptr & PtrMask) != 0) ||
187 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
190 /// \brief Evaluates true when this declaration name is empty.
191 bool isEmpty() const {
195 /// Predicate functions for querying what type of name this is.
196 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
197 bool isObjCZeroArgSelector() const {
198 return getStoredNameKind() == StoredObjCZeroArgSelector;
200 bool isObjCOneArgSelector() const {
201 return getStoredNameKind() == StoredObjCOneArgSelector;
204 /// getNameKind - Determine what kind of name this is.
205 NameKind getNameKind() const;
207 /// \brief Determines whether the name itself is dependent, e.g., because it
208 /// involves a C++ type that is itself dependent.
210 /// Note that this does not capture all of the notions of "dependent name",
211 /// because an identifier can be a dependent name if it is used as the
212 /// callee in a call expression with dependent arguments.
213 bool isDependentName() const;
215 /// getNameAsString - Retrieve the human-readable string for this name.
216 std::string getAsString() const;
218 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
219 /// this declaration name, or NULL if this declaration name isn't a
220 /// simple identifier.
221 IdentifierInfo *getAsIdentifierInfo() const {
223 return reinterpret_cast<IdentifierInfo *>(Ptr);
227 /// getAsOpaqueInteger - Get the representation of this declaration
228 /// name as an opaque integer.
229 uintptr_t getAsOpaqueInteger() const { return Ptr; }
231 /// getAsOpaquePtr - Get the representation of this declaration name as
232 /// an opaque pointer.
233 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
235 static DeclarationName getFromOpaquePtr(void *P) {
237 N.Ptr = reinterpret_cast<uintptr_t> (P);
241 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
247 /// getCXXNameType - If this name is one of the C++ names (of a
248 /// constructor, destructor, or conversion function), return the
249 /// type associated with that name.
250 QualType getCXXNameType() const;
252 /// If this name is the name of a C++ deduction guide, return the
253 /// template associated with that name.
254 TemplateDecl *getCXXDeductionGuideTemplate() const;
256 /// getCXXOverloadedOperator - If this name is the name of an
257 /// overloadable operator in C++ (e.g., @c operator+), retrieve the
258 /// kind of overloaded operator.
259 OverloadedOperatorKind getCXXOverloadedOperator() const;
261 /// getCXXLiteralIdentifier - If this name is the name of a literal
262 /// operator, retrieve the identifier associated with it.
263 IdentifierInfo *getCXXLiteralIdentifier() const;
265 /// getObjCSelector - Get the Objective-C selector stored in this
266 /// declaration name.
267 Selector getObjCSelector() const {
268 assert((getNameKind() == ObjCZeroArgSelector ||
269 getNameKind() == ObjCOneArgSelector ||
270 getNameKind() == ObjCMultiArgSelector ||
271 Ptr == 0) && "Not a selector!");
272 return Selector(Ptr);
275 /// getFETokenInfo/setFETokenInfo - The language front-end is
276 /// allowed to associate arbitrary metadata with some kinds of
277 /// declaration names, including normal identifiers and C++
278 /// constructors, destructors, and conversion functions.
280 T *getFETokenInfo() const {
281 if (const IdentifierInfo *Info = getAsIdentifierInfo())
282 return Info->getFETokenInfo<T>();
283 return static_cast<T*>(getFETokenInfoAsVoidSlow());
286 void setFETokenInfo(void *T);
288 /// operator== - Determine whether the specified names are identical..
289 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
290 return LHS.Ptr == RHS.Ptr;
293 /// operator!= - Determine whether the specified names are different.
294 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
295 return LHS.Ptr != RHS.Ptr;
298 static DeclarationName getEmptyMarker() {
299 return DeclarationName(uintptr_t(-1));
302 static DeclarationName getTombstoneMarker() {
303 return DeclarationName(uintptr_t(-2));
306 static int compare(DeclarationName LHS, DeclarationName RHS);
308 void print(raw_ostream &OS, const PrintingPolicy &Policy);
313 raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
315 /// Ordering on two declaration names. If both names are identifiers,
316 /// this provides a lexicographical ordering.
317 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
318 return DeclarationName::compare(LHS, RHS) < 0;
321 /// Ordering on two declaration names. If both names are identifiers,
322 /// this provides a lexicographical ordering.
323 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
324 return DeclarationName::compare(LHS, RHS) > 0;
327 /// Ordering on two declaration names. If both names are identifiers,
328 /// this provides a lexicographical ordering.
329 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
330 return DeclarationName::compare(LHS, RHS) <= 0;
333 /// Ordering on two declaration names. If both names are identifiers,
334 /// this provides a lexicographical ordering.
335 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
336 return DeclarationName::compare(LHS, RHS) >= 0;
339 /// DeclarationNameTable - Used to store and retrieve DeclarationName
340 /// instances for the various kinds of declaration names, e.g., normal
341 /// identifiers, C++ constructor names, etc. This class contains
342 /// uniqued versions of each of the C++ special names, which can be
343 /// retrieved using its member functions (e.g.,
344 /// getCXXConstructorName).
345 class DeclarationNameTable {
346 const ASTContext &Ctx;
347 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
348 CXXOperatorIdName *CXXOperatorNames; // Operator names
349 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
350 void *CXXDeductionGuideNames; // FoldingSet<CXXDeductionGuideNameExtra> *
352 DeclarationNameTable(const DeclarationNameTable&) = delete;
353 void operator=(const DeclarationNameTable&) = delete;
356 DeclarationNameTable(const ASTContext &C);
357 ~DeclarationNameTable();
359 /// getIdentifier - Create a declaration name that is a simple
361 DeclarationName getIdentifier(const IdentifierInfo *ID) {
362 return DeclarationName(ID);
365 /// getCXXConstructorName - Returns the name of a C++ constructor
366 /// for the given Type.
367 DeclarationName getCXXConstructorName(CanQualType Ty);
369 /// getCXXDestructorName - Returns the name of a C++ destructor
370 /// for the given Type.
371 DeclarationName getCXXDestructorName(CanQualType Ty);
373 /// Returns the name of a C++ deduction guide for the given template.
374 DeclarationName getCXXDeductionGuideName(TemplateDecl *TD);
376 /// getCXXConversionFunctionName - Returns the name of a C++
377 /// conversion function for the given Type.
378 DeclarationName getCXXConversionFunctionName(CanQualType Ty);
380 /// getCXXSpecialName - Returns a declaration name for special kind
381 /// of C++ name, e.g., for a constructor, destructor, or conversion
383 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
386 /// getCXXOperatorName - Get the name of the overloadable C++
387 /// operator corresponding to Op.
388 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
390 /// getCXXLiteralOperatorName - Get the name of the literal operator function
391 /// with II as the identifier.
392 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
395 /// DeclarationNameLoc - Additional source/type location info
396 /// for a declaration name. Needs a DeclarationName in order
397 /// to be interpreted correctly.
398 struct DeclarationNameLoc {
399 // The source location for identifier stored elsewhere.
400 // struct {} Identifier;
402 // Type info for constructors, destructors and conversion functions.
403 // Locations (if any) for the tilde (destructor) or operator keyword
404 // (conversion) are stored elsewhere.
406 TypeSourceInfo *TInfo;
409 // The location (if any) of the operator keyword is stored elsewhere.
411 unsigned BeginOpNameLoc;
412 unsigned EndOpNameLoc;
415 // The location (if any) of the operator keyword is stored elsewhere.
416 struct CXXLitOpName {
420 // struct {} CXXUsingDirective;
421 // struct {} ObjCZeroArgSelector;
422 // struct {} ObjCOneArgSelector;
423 // struct {} ObjCMultiArgSelector;
426 struct CXXOpName CXXOperatorName;
427 struct CXXLitOpName CXXLiteralOperatorName;
430 DeclarationNameLoc(DeclarationName Name);
431 // FIXME: this should go away once all DNLocs are properly initialized.
432 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
433 }; // struct DeclarationNameLoc
436 /// DeclarationNameInfo - A collector data type for bundling together
437 /// a DeclarationName and the correspnding source/type location info.
438 struct DeclarationNameInfo {
440 /// Name - The declaration name, also encoding name kind.
441 DeclarationName Name;
442 /// Loc - The main source location for the declaration name.
443 SourceLocation NameLoc;
444 /// Info - Further source/type location info for special kinds of names.
445 DeclarationNameLoc LocInfo;
449 DeclarationNameInfo() {}
451 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
452 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
454 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
455 DeclarationNameLoc LocInfo)
456 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
458 /// getName - Returns the embedded declaration name.
459 DeclarationName getName() const { return Name; }
460 /// setName - Sets the embedded declaration name.
461 void setName(DeclarationName N) { Name = N; }
463 /// getLoc - Returns the main location of the declaration name.
464 SourceLocation getLoc() const { return NameLoc; }
465 /// setLoc - Sets the main location of the declaration name.
466 void setLoc(SourceLocation L) { NameLoc = L; }
468 const DeclarationNameLoc &getInfo() const { return LocInfo; }
469 DeclarationNameLoc &getInfo() { return LocInfo; }
470 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
472 /// getNamedTypeInfo - Returns the source type info associated to
473 /// the name. Assumes it is a constructor, destructor or conversion.
474 TypeSourceInfo *getNamedTypeInfo() const {
475 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
476 Name.getNameKind() == DeclarationName::CXXDestructorName ||
477 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
478 return LocInfo.NamedType.TInfo;
480 /// setNamedTypeInfo - Sets the source type info associated to
481 /// the name. Assumes it is a constructor, destructor or conversion.
482 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
483 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
484 Name.getNameKind() == DeclarationName::CXXDestructorName ||
485 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
486 LocInfo.NamedType.TInfo = TInfo;
489 /// getCXXOperatorNameRange - Gets the range of the operator name
490 /// (without the operator keyword). Assumes it is a (non-literal) operator.
491 SourceRange getCXXOperatorNameRange() const {
492 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
494 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
495 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
498 /// setCXXOperatorNameRange - Sets the range of the operator name
499 /// (without the operator keyword). Assumes it is a C++ operator.
500 void setCXXOperatorNameRange(SourceRange R) {
501 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
502 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
503 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
506 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
507 /// operator name (not the operator keyword).
508 /// Assumes it is a literal operator.
509 SourceLocation getCXXLiteralOperatorNameLoc() const {
510 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
511 return SourceLocation::
512 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
514 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
515 /// operator name (not the operator keyword).
516 /// Assumes it is a literal operator.
517 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
518 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
519 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
522 /// \brief Determine whether this name involves a template parameter.
523 bool isInstantiationDependent() const;
525 /// \brief Determine whether this name contains an unexpanded
527 bool containsUnexpandedParameterPack() const;
529 /// getAsString - Retrieve the human-readable string for this name.
530 std::string getAsString() const;
532 /// printName - Print the human-readable name to a stream.
533 void printName(raw_ostream &OS) const;
535 /// getBeginLoc - Retrieve the location of the first token.
536 SourceLocation getBeginLoc() const { return NameLoc; }
537 /// getEndLoc - Retrieve the location of the last token.
538 SourceLocation getEndLoc() const;
539 /// getSourceRange - The range of the declaration name.
540 SourceRange getSourceRange() const LLVM_READONLY {
541 return SourceRange(getLocStart(), getLocEnd());
543 SourceLocation getLocStart() const LLVM_READONLY {
544 return getBeginLoc();
546 SourceLocation getLocEnd() const LLVM_READONLY {
547 SourceLocation EndLoc = getEndLoc();
548 return EndLoc.isValid() ? EndLoc : getLocStart();
552 /// Insertion operator for diagnostics. This allows sending DeclarationName's
553 /// into a diagnostic with <<.
554 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
556 DB.AddTaggedVal(N.getAsOpaqueInteger(),
557 DiagnosticsEngine::ak_declarationname);
561 /// Insertion operator for partial diagnostics. This allows binding
562 /// DeclarationName's into a partial diagnostic with <<.
563 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
565 PD.AddTaggedVal(N.getAsOpaqueInteger(),
566 DiagnosticsEngine::ak_declarationname);
570 inline raw_ostream &operator<<(raw_ostream &OS,
571 DeclarationNameInfo DNInfo) {
572 DNInfo.printName(OS);
576 } // end namespace clang
579 /// Define DenseMapInfo so that DeclarationNames can be used as keys
580 /// in DenseMap and DenseSets.
582 struct DenseMapInfo<clang::DeclarationName> {
583 static inline clang::DeclarationName getEmptyKey() {
584 return clang::DeclarationName::getEmptyMarker();
587 static inline clang::DeclarationName getTombstoneKey() {
588 return clang::DeclarationName::getTombstoneMarker();
591 static unsigned getHashValue(clang::DeclarationName Name) {
592 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
596 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
602 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
604 } // end namespace llvm