Coverage Report

Created: 2022-01-25 06:29

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