Coverage Report

Created: 2020-02-25 14:32

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