1//===- DeclarationName.h - Representation of declaration names --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the DeclarationName and DeclarationNameTable classes.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
15#define LLVM_CLANG_AST_DECLARATIONNAME_H
16
17#include "clang/AST/Type.h"
18#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/IdentifierTable.h"
20#include "clang/Basic/OperatorKinds.h"
21#include "clang/Basic/PartialDiagnostic.h"
22#include "clang/Basic/SourceLocation.h"
23#include "llvm/ADT/DenseMapInfo.h"
24#include "llvm/ADT/FoldingSet.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Support/type_traits.h"
27#include <cassert>
28#include <cstdint>
29#include <cstring>
30#include <string>
31
32namespace clang {
33
34class ASTContext;
35template <typename> class CanQual;
36class DeclarationName;
37class DeclarationNameTable;
38class MultiKeywordSelector;
39struct PrintingPolicy;
40class TemplateDecl;
41class TypeSourceInfo;
42class UsingDirectiveDecl;
43
44using CanQualType = CanQual<Type>;
45
46namespace detail {
47
48/// CXXSpecialNameExtra records the type associated with one of the "special"
49/// kinds of declaration names in C++, e.g., constructors, destructors, and
50/// conversion functions. Note that CXXSpecialName is used for C++ constructor,
51/// destructor and conversion functions, but the actual kind is not stored in
52/// CXXSpecialName. Instead we use three different FoldingSet<CXXSpecialName>
53/// in DeclarationNameTable.
54class alignas(IdentifierInfoAlignment) CXXSpecialNameExtra
55 : public llvm::FoldingSetNode {
56 friend class clang::DeclarationName;
57 friend class clang::DeclarationNameTable;
58
59 /// The type associated with this declaration name.
60 QualType Type;
61
62 /// Extra information associated with this declaration name that
63 /// can be used by the front end. All bits are really needed
64 /// so it is not possible to stash something in the low order bits.
65 void *FETokenInfo;
66
67 CXXSpecialNameExtra(QualType QT) : Type(QT), FETokenInfo(nullptr) {}
68
69public:
70 void Profile(llvm::FoldingSetNodeID &ID) {
71 ID.AddPointer(Type.getAsOpaquePtr());
72 }
73};
74
75/// Contains extra information for the name of a C++ deduction guide.
76class alignas(IdentifierInfoAlignment) CXXDeductionGuideNameExtra
77 : public detail::DeclarationNameExtra,
78 public llvm::FoldingSetNode {
79 friend class clang::DeclarationName;
80 friend class clang::DeclarationNameTable;
81
82 /// The template named by the deduction guide.
83 TemplateDecl *Template;
84
85 /// Extra information associated with this operator name that
86 /// can be used by the front end. All bits are really needed
87 /// so it is not possible to stash something in the low order bits.
88 void *FETokenInfo;
89
90 CXXDeductionGuideNameExtra(TemplateDecl *TD)
91 : DeclarationNameExtra(CXXDeductionGuideName), Template(TD),
92 FETokenInfo(nullptr) {}
93
94public:
95 void Profile(llvm::FoldingSetNodeID &ID) { ID.AddPointer(Template); }
96};
97
98/// Contains extra information for the name of an overloaded operator
99/// in C++, such as "operator+. This do not includes literal or conversion
100/// operators. For literal operators see CXXLiteralOperatorIdName and for
101/// conversion operators see CXXSpecialNameExtra.
102class alignas(IdentifierInfoAlignment) CXXOperatorIdName {
103 friend class clang::DeclarationName;
104 friend class clang::DeclarationNameTable;
105
106 /// The kind of this operator.
107 OverloadedOperatorKind Kind = OO_None;
108
109 /// Extra information associated with this operator name that
110 /// can be used by the front end. All bits are really needed
111 /// so it is not possible to stash something in the low order bits.
112 void *FETokenInfo = nullptr;
113};
114
115/// Contains the actual identifier that makes up the
116/// name of a C++ literal operator.
117class alignas(IdentifierInfoAlignment) CXXLiteralOperatorIdName
118 : public detail::DeclarationNameExtra,
119 public llvm::FoldingSetNode {
120 friend class clang::DeclarationName;
121 friend class clang::DeclarationNameTable;
122
123 IdentifierInfo *ID;
124
125 /// Extra information associated with this operator name that
126 /// can be used by the front end. All bits are really needed
127 /// so it is not possible to stash something in the low order bits.
128 void *FETokenInfo;
129
130 CXXLiteralOperatorIdName(IdentifierInfo *II)
131 : DeclarationNameExtra(CXXLiteralOperatorName), ID(II),
132 FETokenInfo(nullptr) {}
133
134public:
135 void Profile(llvm::FoldingSetNodeID &FSID) { FSID.AddPointer(ID); }
136};
137
138} // namespace detail
139
140/// The name of a declaration. In the common case, this just stores
141/// an IdentifierInfo pointer to a normal name. However, it also provides
142/// encodings for Objective-C selectors (optimizing zero- and one-argument
143/// selectors, which make up 78% percent of all selectors in Cocoa.h),
144/// special C++ names for constructors, destructors, and conversion functions,
145/// and C++ overloaded operators.
146class DeclarationName {
147 friend class DeclarationNameTable;
148 friend class NamedDecl;
149
150 /// StoredNameKind represent the kind of name that is actually stored in the
151 /// upper bits of the Ptr field. This is only used internally.
152 ///
153 /// NameKind, StoredNameKind, and DeclarationNameExtra::ExtraKind
154 /// must satisfy the following properties. These properties enable
155 /// efficient conversion between the various kinds.
156 ///
157 /// * The first seven enumerators of StoredNameKind must have the same
158 /// numerical value as the first seven enumerators of NameKind.
159 /// This enable efficient conversion between the two enumerations
160 /// in the usual case.
161 ///
162 /// * The enumerations values of DeclarationNameExtra::ExtraKind must start
163 /// at zero, and correspond to the numerical value of the first non-inline
164 /// enumeration values of NameKind minus an offset. This makes conversion
165 /// between DeclarationNameExtra::ExtraKind and NameKind possible with
166 /// a single addition/substraction.
167 ///
168 /// * The enumeration values of Selector::IdentifierInfoFlag must correspond
169 /// to the relevant enumeration values of StoredNameKind.
170 /// More specifically:
171 /// * ZeroArg == StoredObjCZeroArgSelector,
172 /// * OneArg == StoredObjCOneArgSelector,
173 /// * MultiArg == StoredDeclarationNameExtra
174 ///
175 /// * PtrMask must mask the low 3 bits of Ptr.
176 enum StoredNameKind {
177 StoredIdentifier = 0,
178 StoredObjCZeroArgSelector = Selector::ZeroArg,
179 StoredObjCOneArgSelector = Selector::OneArg,
180 StoredCXXConstructorName = 3,
181 StoredCXXDestructorName = 4,
182 StoredCXXConversionFunctionName = 5,
183 StoredCXXOperatorName = 6,
184 StoredDeclarationNameExtra = Selector::MultiArg,
185 PtrMask = 7,
186 UncommonNameKindOffset = 8
187 };
188
189 static_assert(alignof(IdentifierInfo) >= 8 &&
190 alignof(detail::DeclarationNameExtra) >= 8 &&
191 alignof(detail::CXXSpecialNameExtra) >= 8 &&
192 alignof(detail::CXXOperatorIdName) >= 8 &&
193 alignof(detail::CXXDeductionGuideNameExtra) >= 8 &&
194 alignof(detail::CXXLiteralOperatorIdName) >= 8,
195 "The various classes that DeclarationName::Ptr can point to"
196 " must be at least aligned to 8 bytes!");
197
198public:
199 /// The kind of the name stored in this DeclarationName.
200 /// The first 7 enumeration values are stored inline and correspond
201 /// to frequently used kinds. The rest is stored in DeclarationNameExtra
202 /// and correspond to infrequently used kinds.
203 enum NameKind {
204 Identifier = StoredIdentifier,
205 ObjCZeroArgSelector = StoredObjCZeroArgSelector,
206 ObjCOneArgSelector = StoredObjCOneArgSelector,
207 CXXConstructorName = StoredCXXConstructorName,
208 CXXDestructorName = StoredCXXDestructorName,
209 CXXConversionFunctionName = StoredCXXConversionFunctionName,
210 CXXOperatorName = StoredCXXOperatorName,
211 CXXDeductionGuideName = UncommonNameKindOffset +
212 detail::DeclarationNameExtra::CXXDeductionGuideName,
213 CXXLiteralOperatorName =
214 UncommonNameKindOffset +
215 detail::DeclarationNameExtra::CXXLiteralOperatorName,
216 CXXUsingDirective = UncommonNameKindOffset +
217 detail::DeclarationNameExtra::CXXUsingDirective,
218 ObjCMultiArgSelector = UncommonNameKindOffset +
219 detail::DeclarationNameExtra::ObjCMultiArgSelector
220 };
221
222private:
223 /// The lowest three bits of Ptr are used to express what kind of name
224 /// we're actually storing, using the values of StoredNameKind. Depending
225 /// on the kind of name this is, the upper bits of Ptr may have one
226 /// of several different meanings:
227 ///
228 /// StoredIdentifier - The name is a normal identifier, and Ptr is
229 /// a normal IdentifierInfo pointer.
230 ///
231 /// StoredObjCZeroArgSelector - The name is an Objective-C
232 /// selector with zero arguments, and Ptr is an IdentifierInfo
233 /// pointer pointing to the selector name.
234 ///
235 /// StoredObjCOneArgSelector - The name is an Objective-C selector
236 /// with one argument, and Ptr is an IdentifierInfo pointer
237 /// pointing to the selector name.
238 ///
239 /// StoredCXXConstructorName - The name of a C++ constructor,
240 /// Ptr points to a CXXSpecialNameExtra.
241 ///
242 /// StoredCXXDestructorName - The name of a C++ destructor,
243 /// Ptr points to a CXXSpecialNameExtra.
244 ///
245 /// StoredCXXConversionFunctionName - The name of a C++ conversion function,
246 /// Ptr points to a CXXSpecialNameExtra.
247 ///
248 /// StoredCXXOperatorName - The name of an overloaded C++ operator,
249 /// Ptr points to a CXXOperatorIdName.
250 ///
251 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
252 /// DeclarationNameExtra structure, whose first value will tell us
253 /// whether this is an Objective-C selector, C++ deduction guide,
254 /// C++ literal operator, or C++ using directive.
255 uintptr_t Ptr = 0;
256
257 StoredNameKind getStoredNameKind() const {
258 return static_cast<StoredNameKind>(Ptr & PtrMask);
259 }
260
261 void *getPtr() const { return reinterpret_cast<void *>(Ptr & ~PtrMask); }
262
263 void setPtrAndKind(const void *P, StoredNameKind Kind) {
264 uintptr_t PAsInteger = reinterpret_cast<uintptr_t>(P);
265 assert((Kind & ~PtrMask) == 0 &&
266 "Invalid StoredNameKind in setPtrAndKind!");
267 assert((PAsInteger & PtrMask) == 0 &&
268 "Improperly aligned pointer in setPtrAndKind!");
269 Ptr = PAsInteger | Kind;
270 }
271
272 /// Construct a declaration name from a DeclarationNameExtra.
273 DeclarationName(detail::DeclarationNameExtra *Name) {
274 setPtrAndKind(Name, StoredDeclarationNameExtra);
275 }
276
277 /// Construct a declaration name from a CXXSpecialNameExtra.
278 DeclarationName(detail::CXXSpecialNameExtra *Name,
279 StoredNameKind StoredKind) {
280 assert((StoredKind == StoredCXXConstructorName ||
281 StoredKind == StoredCXXDestructorName ||
282 StoredKind == StoredCXXConversionFunctionName) &&
283 "Invalid StoredNameKind when constructing a DeclarationName"
284 " from a CXXSpecialNameExtra!");
285 setPtrAndKind(Name, StoredKind);
286 }
287
288 /// Construct a DeclarationName from a CXXOperatorIdName.
289 DeclarationName(detail::CXXOperatorIdName *Name) {
290 setPtrAndKind(Name, StoredCXXOperatorName);
291 }
292
293 /// Assert that the stored pointer points to an IdentifierInfo and return it.
294 IdentifierInfo *castAsIdentifierInfo() const {
295 assert((getStoredNameKind() == StoredIdentifier) &&
296 "DeclarationName does not store an IdentifierInfo!");
297 return static_cast<IdentifierInfo *>(getPtr());
298 }
299
300 /// Assert that the stored pointer points to a DeclarationNameExtra
301 /// and return it.
302 detail::DeclarationNameExtra *castAsExtra() const {
303 assert((getStoredNameKind() == StoredDeclarationNameExtra) &&
304 "DeclarationName does not store an Extra structure!");
305 return static_cast<detail::DeclarationNameExtra *>(getPtr());
306 }
307
308 /// Assert that the stored pointer points to a CXXSpecialNameExtra
309 /// and return it.
310 detail::CXXSpecialNameExtra *castAsCXXSpecialNameExtra() const {
311 assert((getStoredNameKind() == StoredCXXConstructorName ||
312 getStoredNameKind() == StoredCXXDestructorName ||
313 getStoredNameKind() == StoredCXXConversionFunctionName) &&
314 "DeclarationName does not store a CXXSpecialNameExtra!");
315 return static_cast<detail::CXXSpecialNameExtra *>(getPtr());
316 }
317
318 /// Assert that the stored pointer points to a CXXOperatorIdName
319 /// and return it.
320 detail::CXXOperatorIdName *castAsCXXOperatorIdName() const {
321 assert((getStoredNameKind() == StoredCXXOperatorName) &&
322 "DeclarationName does not store a CXXOperatorIdName!");
323 return static_cast<detail::CXXOperatorIdName *>(getPtr());
324 }
325
326 /// Assert that the stored pointer points to a CXXDeductionGuideNameExtra
327 /// and return it.
328 detail::CXXDeductionGuideNameExtra *castAsCXXDeductionGuideNameExtra() const {
329 assert(getNameKind() == CXXDeductionGuideName &&
330 "DeclarationName does not store a CXXDeductionGuideNameExtra!");
331 return static_cast<detail::CXXDeductionGuideNameExtra *>(getPtr());
332 }
333
334 /// Assert that the stored pointer points to a CXXLiteralOperatorIdName
335 /// and return it.
336 detail::CXXLiteralOperatorIdName *castAsCXXLiteralOperatorIdName() const {
337 assert(getNameKind() == CXXLiteralOperatorName &&
338 "DeclarationName does not store a CXXLiteralOperatorIdName!");
339 return static_cast<detail::CXXLiteralOperatorIdName *>(getPtr());
340 }
341
342 /// Get and set the FETokenInfo in the less common cases where the
343 /// declaration name do not point to an identifier.
344 void *getFETokenInfoSlow() const;
345 void setFETokenInfoSlow(void *T);
346
347public:
348 /// Construct an empty declaration name.
349 DeclarationName() { setPtrAndKind(nullptr, StoredIdentifier); }
350
351 /// Construct a declaration name from an IdentifierInfo *.
352 DeclarationName(const IdentifierInfo *II) {
353 setPtrAndKind(II, StoredIdentifier);
354 }
355
356 /// Construct a declaration name from an Objective-C selector.
357 DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) {}
358
359 /// Returns the name for all C++ using-directives.
360 static DeclarationName getUsingDirectiveName() {
361 // Single instance of DeclarationNameExtra for using-directive
362 static detail::DeclarationNameExtra UDirExtra(
363 detail::DeclarationNameExtra::CXXUsingDirective);
364 return DeclarationName(&UDirExtra);
365 }
366
367 /// Evaluates true when this declaration name is non-empty.
368 explicit operator bool() const {
369 return getPtr() || (getStoredNameKind() != StoredIdentifier);
370 }
371
372 /// Evaluates true when this declaration name is empty.
373 bool isEmpty() const { return !*this; }
374
375 /// Predicate functions for querying what type of name this is.
376 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
377 bool isObjCZeroArgSelector() const {
378 return getStoredNameKind() == StoredObjCZeroArgSelector;
379 }
380 bool isObjCOneArgSelector() const {
381 return getStoredNameKind() == StoredObjCOneArgSelector;
382 }
383
384 /// Determine what kind of name this is.
385 NameKind getNameKind() const {
386 // We rely on the fact that the first 7 NameKind and StoredNameKind
387 // have the same numerical value. This makes the usual case efficient.
388 StoredNameKind StoredKind = getStoredNameKind();
389 if (StoredKind != StoredDeclarationNameExtra)
390 return static_cast<NameKind>(StoredKind);
391 // We have to consult DeclarationNameExtra. We rely on the fact that the
392 // enumeration values of ExtraKind correspond to the enumeration values of
393 // NameKind minus an offset of UncommonNameKindOffset.
394 unsigned ExtraKind = castAsExtra()->getKind();
395 return static_cast<NameKind>(UncommonNameKindOffset + ExtraKind);
396 }
397
398 /// Determines whether the name itself is dependent, e.g., because it
399 /// involves a C++ type that is itself dependent.
400 ///
401 /// Note that this does not capture all of the notions of "dependent name",
402 /// because an identifier can be a dependent name if it is used as the
403 /// callee in a call expression with dependent arguments.
404 bool isDependentName() const;
405
406 /// Retrieve the human-readable string for this name.
407 std::string getAsString() const;
408
409 /// Retrieve the IdentifierInfo * stored in this declaration name,
410 /// or null if this declaration name isn't a simple identifier.
411 IdentifierInfo *getAsIdentifierInfo() const {
412 if (isIdentifier())
413 return castAsIdentifierInfo();
414 return nullptr;
415 }
416
417 /// Get the representation of this declaration name as an opaque integer.
418 uintptr_t getAsOpaqueInteger() const { return Ptr; }
419
420 /// Get the representation of this declaration name as an opaque pointer.
421 void *getAsOpaquePtr() const { return reinterpret_cast<void *>(Ptr); }
422
423 /// Get a declaration name from an opaque pointer returned by getAsOpaquePtr.
424 static DeclarationName getFromOpaquePtr(void *P) {
425 DeclarationName N;
426 N.Ptr = reinterpret_cast<uintptr_t>(P);
427 return N;
428 }
429
430 /// Get a declaration name from an opaque integer
431 /// returned by getAsOpaqueInteger.
432 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
433 DeclarationName N;
434 N.Ptr = P;
435 return N;
436 }
437
438 /// If this name is one of the C++ names (of a constructor, destructor,
439 /// or conversion function), return the type associated with that name.
440 QualType getCXXNameType() const {
441 if (getStoredNameKind() == StoredCXXConstructorName ||
442 getStoredNameKind() == StoredCXXDestructorName ||
443 getStoredNameKind() == StoredCXXConversionFunctionName) {
444 assert(getPtr() && "getCXXNameType on a null DeclarationName!");
445 return castAsCXXSpecialNameExtra()->Type;
446 }
447 return QualType();
448 }
449
450 /// If this name is the name of a C++ deduction guide, return the
451 /// template associated with that name.
452 TemplateDecl *getCXXDeductionGuideTemplate() const {
453 if (getNameKind() == CXXDeductionGuideName) {
454 assert(getPtr() &&
455 "getCXXDeductionGuideTemplate on a null DeclarationName!");
456 return castAsCXXDeductionGuideNameExtra()->Template;
457 }
458 return nullptr;
459 }
460
461 /// If this name is the name of an overloadable operator in C++
462 /// (e.g., @c operator+), retrieve the kind of overloaded operator.
463 OverloadedOperatorKind getCXXOverloadedOperator() const {
464 if (getStoredNameKind() == StoredCXXOperatorName) {
465 assert(getPtr() && "getCXXOverloadedOperator on a null DeclarationName!");
466 return castAsCXXOperatorIdName()->Kind;
467 }
468 return OO_None;
469 }
470
471 /// If this name is the name of a literal operator,
472 /// retrieve the identifier associated with it.
473 IdentifierInfo *getCXXLiteralIdentifier() const {
474 if (getNameKind() == CXXLiteralOperatorName) {
475 assert(getPtr() && "getCXXLiteralIdentifier on a null DeclarationName!");
476 return castAsCXXLiteralOperatorIdName()->ID;
477 }
478 return nullptr;
479 }
480
481 /// Get the Objective-C selector stored in this declaration name.
482 Selector getObjCSelector() const {
483 assert((getNameKind() == ObjCZeroArgSelector ||
484 getNameKind() == ObjCOneArgSelector ||
485 getNameKind() == ObjCMultiArgSelector || !getPtr()) &&
486 "Not a selector!");
487 return Selector(Ptr);
488 }
489
490 /// Get and set FETokenInfo. The language front-end is allowed to associate
491 /// arbitrary metadata with some kinds of declaration names, including normal
492 /// identifiers and C++ constructors, destructors, and conversion functions.
493 void *getFETokenInfo() const {
494 assert(getPtr() && "getFETokenInfo on an empty DeclarationName!");
495 if (getStoredNameKind() == StoredIdentifier)
496 return castAsIdentifierInfo()->getFETokenInfo();
497 return getFETokenInfoSlow();
498 }
499
500 void setFETokenInfo(void *T) {
501 assert(getPtr() && "setFETokenInfo on an empty DeclarationName!");
502 if (getStoredNameKind() == StoredIdentifier)
503 castAsIdentifierInfo()->setFETokenInfo(T);
504 else
505 setFETokenInfoSlow(T);
506 }
507
508 /// Determine whether the specified names are identical.
509 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
510 return LHS.Ptr == RHS.Ptr;
511 }
512
513 /// Determine whether the specified names are different.
514 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
515 return LHS.Ptr != RHS.Ptr;
516 }
517
518 static DeclarationName getEmptyMarker() {
519 DeclarationName Name;
520 Name.Ptr = uintptr_t(-1);
521 return Name;
522 }
523
524 static DeclarationName getTombstoneMarker() {
525 DeclarationName Name;
526 Name.Ptr = uintptr_t(-2);
527 return Name;
528 }
529
530 static int compare(DeclarationName LHS, DeclarationName RHS);
531
532 void print(raw_ostream &OS, const PrintingPolicy &Policy);
533
534 void dump() const;
535};
536
537raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
538
539/// Ordering on two declaration names. If both names are identifiers,
540/// this provides a lexicographical ordering.
541inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
542 return DeclarationName::compare(LHS, RHS) < 0;
543}
544
545/// Ordering on two declaration names. If both names are identifiers,
546/// this provides a lexicographical ordering.
547inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
548 return DeclarationName::compare(LHS, RHS) > 0;
549}
550
551/// Ordering on two declaration names. If both names are identifiers,
552/// this provides a lexicographical ordering.
553inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
554 return DeclarationName::compare(LHS, RHS) <= 0;
555}
556
557/// Ordering on two declaration names. If both names are identifiers,
558/// this provides a lexicographical ordering.
559inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
560 return DeclarationName::compare(LHS, RHS) >= 0;
561}
562
563/// DeclarationNameTable is used to store and retrieve DeclarationName
564/// instances for the various kinds of declaration names, e.g., normal
565/// identifiers, C++ constructor names, etc. This class contains
566/// uniqued versions of each of the C++ special names, which can be
567/// retrieved using its member functions (e.g., getCXXConstructorName).
568class DeclarationNameTable {
569 /// Used to allocate elements in the FoldingSets below.
570 const ASTContext &Ctx;
571
572 /// Manage the uniqued CXXSpecialNameExtra representing C++ constructors.
573 /// getCXXConstructorName and getCXXSpecialName can be used to obtain
574 /// a DeclarationName from the corresponding type of the constructor.
575 llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConstructorNames;
576
577 /// Manage the uniqued CXXSpecialNameExtra representing C++ destructors.
578 /// getCXXDestructorName and getCXXSpecialName can be used to obtain
579 /// a DeclarationName from the corresponding type of the destructor.
580 llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXDestructorNames;
581
582 /// Manage the uniqued CXXSpecialNameExtra representing C++ conversion
583 /// functions. getCXXConversionFunctionName and getCXXSpecialName can be
584 /// used to obtain a DeclarationName from the corresponding type of the
585 /// conversion function.
586 llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConversionFunctionNames;
587
588 /// Manage the uniqued CXXOperatorIdName, which contain extra information
589 /// for the name of overloaded C++ operators. getCXXOperatorName
590 /// can be used to obtain a DeclarationName from the operator kind.
591 detail::CXXOperatorIdName CXXOperatorNames[NUM_OVERLOADED_OPERATORS];
592
593 /// Manage the uniqued CXXLiteralOperatorIdName, which contain extra
594 /// information for the name of C++ literal operators.
595 /// getCXXLiteralOperatorName can be used to obtain a DeclarationName
596 /// from the corresponding IdentifierInfo.
597 llvm::FoldingSet<detail::CXXLiteralOperatorIdName> CXXLiteralOperatorNames;
598
599 /// Manage the uniqued CXXDeductionGuideNameExtra, which contain
600 /// extra information for the name of a C++ deduction guide.
601 /// getCXXDeductionGuideName can be used to obtain a DeclarationName
602 /// from the corresponding template declaration.
603 llvm::FoldingSet<detail::CXXDeductionGuideNameExtra> CXXDeductionGuideNames;
604
605public:
606 DeclarationNameTable(const ASTContext &C);
607 DeclarationNameTable(const DeclarationNameTable &) = delete;
608 DeclarationNameTable &operator=(const DeclarationNameTable &) = delete;
609 DeclarationNameTable(DeclarationNameTable &&) = delete;
610 DeclarationNameTable &operator=(DeclarationNameTable &&) = delete;
611 ~DeclarationNameTable() = default;
612
613 /// Create a declaration name that is a simple identifier.
614 DeclarationName getIdentifier(const IdentifierInfo *ID) {
615 return DeclarationName(ID);
616 }
617
618 /// Returns the name of a C++ constructor for the given Type.
619 DeclarationName getCXXConstructorName(CanQualType Ty);
620
621 /// Returns the name of a C++ destructor for the given Type.
622 DeclarationName getCXXDestructorName(CanQualType Ty);
623
624 /// Returns the name of a C++ deduction guide for the given template.
625 DeclarationName getCXXDeductionGuideName(TemplateDecl *TD);
626
627 /// Returns the name of a C++ conversion function for the given Type.
628 DeclarationName getCXXConversionFunctionName(CanQualType Ty);
629
630 /// Returns a declaration name for special kind of C++ name,
631 /// e.g., for a constructor, destructor, or conversion function.
632 /// Kind must be one of:
633 /// * DeclarationName::CXXConstructorName,
634 /// * DeclarationName::CXXDestructorName or
635 /// * DeclarationName::CXXConversionFunctionName
636 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
637 CanQualType Ty);
638
639 /// Get the name of the overloadable C++ operator corresponding to Op.
640 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op) {
641 return DeclarationName(&CXXOperatorNames[Op]);
642 }
643
644 /// Get the name of the literal operator function with II as the identifier.
645 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
646};
647
648/// DeclarationNameLoc - Additional source/type location info
649/// for a declaration name. Needs a DeclarationName in order
650/// to be interpreted correctly.
651struct DeclarationNameLoc {
652 // The source location for identifier stored elsewhere.
653 // struct {} Identifier;
654
655 // Type info for constructors, destructors and conversion functions.
656 // Locations (if any) for the tilde (destructor) or operator keyword
657 // (conversion) are stored elsewhere.
658 struct NT {
659 TypeSourceInfo *TInfo;
660 };
661
662 // The location (if any) of the operator keyword is stored elsewhere.
663 struct CXXOpName {
664 unsigned BeginOpNameLoc;
665 unsigned EndOpNameLoc;
666 };
667
668 // The location (if any) of the operator keyword is stored elsewhere.
669 struct CXXLitOpName {
670 unsigned OpNameLoc;
671 };
672
673 // struct {} CXXUsingDirective;
674 // struct {} ObjCZeroArgSelector;
675 // struct {} ObjCOneArgSelector;
676 // struct {} ObjCMultiArgSelector;
677 union {
678 struct NT NamedType;
679 struct CXXOpName CXXOperatorName;
680 struct CXXLitOpName CXXLiteralOperatorName;
681 };
682
683 DeclarationNameLoc(DeclarationName Name);
684
685 // FIXME: this should go away once all DNLocs are properly initialized.
686 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
687};
688
689/// DeclarationNameInfo - A collector data type for bundling together
690/// a DeclarationName and the correspnding source/type location info.
691struct DeclarationNameInfo {
692private:
693 /// Name - The declaration name, also encoding name kind.
694 DeclarationName Name;
695
696 /// Loc - The main source location for the declaration name.
697 SourceLocation NameLoc;
698
699 /// Info - Further source/type location info for special kinds of names.
700 DeclarationNameLoc LocInfo;
701
702public:
703 // FIXME: remove it.
704 DeclarationNameInfo() = default;
705
706 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
707 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
708
709 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
710 DeclarationNameLoc LocInfo)
711 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
712
713 /// getName - Returns the embedded declaration name.
714 DeclarationName getName() const { return Name; }
715
716 /// setName - Sets the embedded declaration name.
717 void setName(DeclarationName N) { Name = N; }
718
719 /// getLoc - Returns the main location of the declaration name.
720 SourceLocation getLoc() const { return NameLoc; }
721
722 /// setLoc - Sets the main location of the declaration name.
723 void setLoc(SourceLocation L) { NameLoc = L; }
724
725 const DeclarationNameLoc &getInfo() const { return LocInfo; }
726 DeclarationNameLoc &getInfo() { return LocInfo; }
727 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
728
729 /// getNamedTypeInfo - Returns the source type info associated to
730 /// the name. Assumes it is a constructor, destructor or conversion.
731 TypeSourceInfo *getNamedTypeInfo() const {
732 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
733 Name.getNameKind() == DeclarationName::CXXDestructorName ||
734 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
735 return LocInfo.NamedType.TInfo;
736 }
737
738 /// setNamedTypeInfo - Sets the source type info associated to
739 /// the name. Assumes it is a constructor, destructor or conversion.
740 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
741 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
742 Name.getNameKind() == DeclarationName::CXXDestructorName ||
743 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
744 LocInfo.NamedType.TInfo = TInfo;
745 }
746
747 /// getCXXOperatorNameRange - Gets the range of the operator name
748 /// (without the operator keyword). Assumes it is a (non-literal) operator.
749 SourceRange getCXXOperatorNameRange() const {
750 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
751 return SourceRange(
752 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
753 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
754 );
755 }
756
757 /// setCXXOperatorNameRange - Sets the range of the operator name
758 /// (without the operator keyword). Assumes it is a C++ operator.
759 void setCXXOperatorNameRange(SourceRange R) {
760 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
761 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
762 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
763 }
764
765 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
766 /// operator name (not the operator keyword).
767 /// Assumes it is a literal operator.
768 SourceLocation getCXXLiteralOperatorNameLoc() const {
769 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
770 return SourceLocation::
771 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
772 }
773
774 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
775 /// operator name (not the operator keyword).
776 /// Assumes it is a literal operator.
777 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
778 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
779 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
780 }
781
782 /// Determine whether this name involves a template parameter.
783 bool isInstantiationDependent() const;
784
785 /// Determine whether this name contains an unexpanded
786 /// parameter pack.
787 bool containsUnexpandedParameterPack() const;
788
789 /// getAsString - Retrieve the human-readable string for this name.
790 std::string getAsString() const;
791
792 /// printName - Print the human-readable name to a stream.
793 void printName(raw_ostream &OS) const;
794
795 /// getBeginLoc - Retrieve the location of the first token.
796 SourceLocation getBeginLoc() const { return NameLoc; }
797
798 /// getSourceRange - The range of the declaration name.
799 SourceRange getSourceRange() const LLVM_READONLY {
800 return SourceRange(getBeginLoc(), getEndLoc());
801 }
802
803 SourceLocation getEndLoc() const LLVM_READONLY {
804 SourceLocation EndLoc = getEndLocPrivate();
805 return EndLoc.isValid() ? EndLoc : getBeginLoc();
806 }
807
808private:
809 SourceLocation getEndLocPrivate() const;
810};
811
812/// Insertion operator for diagnostics. This allows sending DeclarationName's
813/// into a diagnostic with <<.
814inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
815 DeclarationName N) {
816 DB.AddTaggedVal(N.getAsOpaqueInteger(),
817 DiagnosticsEngine::ak_declarationname);
818 return DB;
819}
820
821/// Insertion operator for partial diagnostics. This allows binding
822/// DeclarationName's into a partial diagnostic with <<.
823inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
824 DeclarationName N) {
825 PD.AddTaggedVal(N.getAsOpaqueInteger(),
826 DiagnosticsEngine::ak_declarationname);
827 return PD;
828}
829
830inline raw_ostream &operator<<(raw_ostream &OS,
831 DeclarationNameInfo DNInfo) {
832 DNInfo.printName(OS);
833 return OS;
834}
835
836} // namespace clang
837
838namespace llvm {
839
840/// Define DenseMapInfo so that DeclarationNames can be used as keys
841/// in DenseMap and DenseSets.
842template<>
843struct DenseMapInfo<clang::DeclarationName> {
844 static inline clang::DeclarationName getEmptyKey() {
845 return clang::DeclarationName::getEmptyMarker();
846 }
847
848 static inline clang::DeclarationName getTombstoneKey() {
849 return clang::DeclarationName::getTombstoneMarker();
850 }
851
852 static unsigned getHashValue(clang::DeclarationName Name) {
853 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
854 }
855
856 static inline bool
857 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
858 return LHS == RHS;
859 }
860};
861
862template <>
863struct isPodLike<clang::DeclarationName> { static const bool value = true; };
864
865} // namespace llvm
866
867#endif // LLVM_CLANG_AST_DECLARATIONNAME_H
868