Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Basic/IdentifierTable.h
Line
Count
Source (jump to first uncovered line)
1
//===- IdentifierTable.h - Hash table for identifier lookup -----*- 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
/// \file
10
/// Defines the clang::IdentifierInfo, clang::IdentifierTable, and
11
/// clang::Selector interfaces.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
16
#define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
17
18
#include "clang/Basic/LLVM.h"
19
#include "clang/Basic/TokenKinds.h"
20
#include "llvm/ADT/DenseMapInfo.h"
21
#include "llvm/ADT/SmallString.h"
22
#include "llvm/ADT/StringMap.h"
23
#include "llvm/ADT/StringRef.h"
24
#include "llvm/Support/Allocator.h"
25
#include "llvm/Support/PointerLikeTypeTraits.h"
26
#include "llvm/Support/type_traits.h"
27
#include <cassert>
28
#include <cstddef>
29
#include <cstdint>
30
#include <cstring>
31
#include <string>
32
#include <utility>
33
34
namespace clang {
35
36
class DeclarationName;
37
class DeclarationNameTable;
38
class IdentifierInfo;
39
class LangOptions;
40
class MultiKeywordSelector;
41
class SourceLocation;
42
43
enum class ReservedIdentifierStatus {
44
  NotReserved = 0,
45
  StartsWithUnderscoreAtGlobalScope,
46
  StartsWithUnderscoreAndIsExternC,
47
  StartsWithDoubleUnderscore,
48
  StartsWithUnderscoreFollowedByCapitalLetter,
49
  ContainsDoubleUnderscore,
50
};
51
52
/// Determine whether an identifier is reserved for use as a name at global
53
/// scope. Such identifiers might be implementation-specific global functions
54
/// or variables.
55
46.3M
inline bool isReservedAtGlobalScope(ReservedIdentifierStatus Status) {
56
46.3M
  return Status != ReservedIdentifierStatus::NotReserved;
57
46.3M
}
58
59
/// Determine whether an identifier is reserved in all contexts. Such
60
/// identifiers might be implementation-specific keywords or macros, for
61
/// example.
62
18.6M
inline bool isReservedInAllContexts(ReservedIdentifierStatus Status) {
63
18.6M
  return Status != ReservedIdentifierStatus::NotReserved &&
64
18.6M
         
Status != ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope14.6M
&&
65
18.6M
         
Status != ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC13.1M
;
66
18.6M
}
67
68
/// A simple pair of identifier info and location.
69
using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>;
70
71
/// IdentifierInfo and other related classes are aligned to
72
/// 8 bytes so that DeclarationName can use the lower 3 bits
73
/// of a pointer to one of these classes.
74
enum { IdentifierInfoAlignment = 8 };
75
76
static constexpr int ObjCOrBuiltinIDBits = 16;
77
78
/// One of these records is kept for each identifier that
79
/// is lexed.  This contains information about whether the token was \#define'd,
80
/// is a language keyword, or if it is a front-end token of some sort (e.g. a
81
/// variable or function name).  The preprocessor keeps this information in a
82
/// set, and all tok::identifier tokens have a pointer to one of these.
83
/// It is aligned to 8 bytes because DeclarationName needs the lower 3 bits.
84
class alignas(IdentifierInfoAlignment) IdentifierInfo {
85
  friend class IdentifierTable;
86
87
  // Front-end token ID or tok::identifier.
88
  unsigned TokenID : 9;
89
90
  // ObjC keyword ('protocol' in '@protocol') or builtin (__builtin_inf).
91
  // First NUM_OBJC_KEYWORDS values are for Objective-C,
92
  // the remaining values are for builtins.
93
  unsigned ObjCOrBuiltinID : ObjCOrBuiltinIDBits;
94
95
  // True if there is a #define for this.
96
  unsigned HasMacro : 1;
97
98
  // True if there was a #define for this.
99
  unsigned HadMacro : 1;
100
101
  // True if the identifier is a language extension.
102
  unsigned IsExtension : 1;
103
104
  // True if the identifier is a keyword in a newer or proposed Standard.
105
  unsigned IsFutureCompatKeyword : 1;
106
107
  // True if the identifier is poisoned.
108
  unsigned IsPoisoned : 1;
109
110
  // True if the identifier is a C++ operator keyword.
111
  unsigned IsCPPOperatorKeyword : 1;
112
113
  // Internal bit set by the member function RecomputeNeedsHandleIdentifier.
114
  // See comment about RecomputeNeedsHandleIdentifier for more info.
115
  unsigned NeedsHandleIdentifier : 1;
116
117
  // True if the identifier was loaded (at least partially) from an AST file.
118
  unsigned IsFromAST : 1;
119
120
  // True if the identifier has changed from the definition
121
  // loaded from an AST file.
122
  unsigned ChangedAfterLoad : 1;
123
124
  // True if the identifier's frontend information has changed from the
125
  // definition loaded from an AST file.
126
  unsigned FEChangedAfterLoad : 1;
127
128
  // True if revertTokenIDToIdentifier was called.
129
  unsigned RevertedTokenID : 1;
130
131
  // True if there may be additional information about
132
  // this identifier stored externally.
133
  unsigned OutOfDate : 1;
134
135
  // True if this is the 'import' contextual keyword.
136
  unsigned IsModulesImport : 1;
137
138
  // True if this is a mangled OpenMP variant name.
139
  unsigned IsMangledOpenMPVariantName : 1;
140
141
  // True if this is a deprecated macro.
142
  unsigned IsDeprecatedMacro : 1;
143
144
  // True if this macro is unsafe in headers.
145
  unsigned IsRestrictExpansion : 1;
146
147
  // True if this macro is final.
148
  unsigned IsFinal : 1;
149
150
  // 22 bits left in a 64-bit word.
151
152
  // Managed by the language front-end.
153
  void *FETokenInfo = nullptr;
154
155
  llvm::StringMapEntry<IdentifierInfo *> *Entry = nullptr;
156
157
  IdentifierInfo()
158
      : TokenID(tok::identifier), ObjCOrBuiltinID(0), HasMacro(false),
159
        HadMacro(false), IsExtension(false), IsFutureCompatKeyword(false),
160
        IsPoisoned(false), IsCPPOperatorKeyword(false),
161
        NeedsHandleIdentifier(false), IsFromAST(false), ChangedAfterLoad(false),
162
        FEChangedAfterLoad(false), RevertedTokenID(false), OutOfDate(false),
163
        IsModulesImport(false), IsMangledOpenMPVariantName(false),
164
426M
        IsDeprecatedMacro(false), IsRestrictExpansion(false), IsFinal(false) {}
165
166
public:
167
  IdentifierInfo(const IdentifierInfo &) = delete;
168
  IdentifierInfo &operator=(const IdentifierInfo &) = delete;
169
  IdentifierInfo(IdentifierInfo &&) = delete;
170
  IdentifierInfo &operator=(IdentifierInfo &&) = delete;
171
172
  /// Return true if this is the identifier for the specified string.
173
  ///
174
  /// This is intended to be used for string literals only: II->isStr("foo").
175
  template <std::size_t StrLen>
176
230M
  bool isStr(const char (&Str)[StrLen]) const {
177
230M
    return getLength() == StrLen-1 &&
178
230M
           
memcmp(getNameStart(), Str, StrLen-1) == 014.5M
;
179
230M
  }
bool clang::IdentifierInfo::isStr<5ul>(char const (&) [5ul]) const
Line
Count
Source
176
106M
  bool isStr(const char (&Str)[StrLen]) const {
177
106M
    return getLength() == StrLen-1 &&
178
106M
           
memcmp(getNameStart(), Str, StrLen-1) == 01.57M
;
179
106M
  }
bool clang::IdentifierInfo::isStr<20ul>(char const (&) [20ul]) const
Line
Count
Source
176
26.1k
  bool isStr(const char (&Str)[StrLen]) const {
177
26.1k
    return getLength() == StrLen-1 &&
178
26.1k
           
memcmp(getNameStart(), Str, StrLen-1) == 01.18k
;
179
26.1k
  }
bool clang::IdentifierInfo::isStr<7ul>(char const (&) [7ul]) const
Line
Count
Source
176
9.57M
  bool isStr(const char (&Str)[StrLen]) const {
177
9.57M
    return getLength() == StrLen-1 &&
178
9.57M
           
memcmp(getNameStart(), Str, StrLen-1) == 0563k
;
179
9.57M
  }
bool clang::IdentifierInfo::isStr<8ul>(char const (&) [8ul]) const
Line
Count
Source
176
23.3M
  bool isStr(const char (&Str)[StrLen]) const {
177
23.3M
    return getLength() == StrLen-1 &&
178
23.3M
           
memcmp(getNameStart(), Str, StrLen-1) == 02.91M
;
179
23.3M
  }
bool clang::IdentifierInfo::isStr<6ul>(char const (&) [6ul]) const
Line
Count
Source
176
9.17M
  bool isStr(const char (&Str)[StrLen]) const {
177
9.17M
    return getLength() == StrLen-1 &&
178
9.17M
           
memcmp(getNameStart(), Str, StrLen-1) == 03.32M
;
179
9.17M
  }
bool clang::IdentifierInfo::isStr<4ul>(char const (&) [4ul]) const
Line
Count
Source
176
4.28M
  bool isStr(const char (&Str)[StrLen]) const {
177
4.28M
    return getLength() == StrLen-1 &&
178
4.28M
           
memcmp(getNameStart(), Str, StrLen-1) == 02.21M
;
179
4.28M
  }
bool clang::IdentifierInfo::isStr<22ul>(char const (&) [22ul]) const
Line
Count
Source
176
42
  bool isStr(const char (&Str)[StrLen]) const {
177
42
    return getLength() == StrLen-1 &&
178
42
           
memcmp(getNameStart(), Str, StrLen-1) == 022
;
179
42
  }
bool clang::IdentifierInfo::isStr<13ul>(char const (&) [13ul]) const
Line
Count
Source
176
7.47k
  bool isStr(const char (&Str)[StrLen]) const {
177
7.47k
    return getLength() == StrLen-1 &&
178
7.47k
           
memcmp(getNameStart(), Str, StrLen-1) == 050
;
179
7.47k
  }
bool clang::IdentifierInfo::isStr<10ul>(char const (&) [10ul]) const
Line
Count
Source
176
24.0M
  bool isStr(const char (&Str)[StrLen]) const {
177
24.0M
    return getLength() == StrLen-1 &&
178
24.0M
           
memcmp(getNameStart(), Str, StrLen-1) == 01.38M
;
179
24.0M
  }
bool clang::IdentifierInfo::isStr<12ul>(char const (&) [12ul]) const
Line
Count
Source
176
1.84M
  bool isStr(const char (&Str)[StrLen]) const {
177
1.84M
    return getLength() == StrLen-1 &&
178
1.84M
           
memcmp(getNameStart(), Str, StrLen-1) == 015.2k
;
179
1.84M
  }
bool clang::IdentifierInfo::isStr<9ul>(char const (&) [9ul]) const
Line
Count
Source
176
24.5M
  bool isStr(const char (&Str)[StrLen]) const {
177
24.5M
    return getLength() == StrLen-1 &&
178
24.5M
           
memcmp(getNameStart(), Str, StrLen-1) == 02.07M
;
179
24.5M
  }
bool clang::IdentifierInfo::isStr<16ul>(char const (&) [16ul]) const
Line
Count
Source
176
79.4k
  bool isStr(const char (&Str)[StrLen]) const {
177
79.4k
    return getLength() == StrLen-1 &&
178
79.4k
           
memcmp(getNameStart(), Str, StrLen-1) == 013.8k
;
179
79.4k
  }
bool clang::IdentifierInfo::isStr<17ul>(char const (&) [17ul]) const
Line
Count
Source
176
79.2k
  bool isStr(const char (&Str)[StrLen]) const {
177
79.2k
    return getLength() == StrLen-1 &&
178
79.2k
           
memcmp(getNameStart(), Str, StrLen-1) == 022
;
179
79.2k
  }
bool clang::IdentifierInfo::isStr<11ul>(char const (&) [11ul]) const
Line
Count
Source
176
3.24M
  bool isStr(const char (&Str)[StrLen]) const {
177
3.24M
    return getLength() == StrLen-1 &&
178
3.24M
           
memcmp(getNameStart(), Str, StrLen-1) == 0129k
;
179
3.24M
  }
bool clang::IdentifierInfo::isStr<15ul>(char const (&) [15ul]) const
Line
Count
Source
176
26.6k
  bool isStr(const char (&Str)[StrLen]) const {
177
26.6k
    return getLength() == StrLen-1 &&
178
26.6k
           
memcmp(getNameStart(), Str, StrLen-1) == 0200
;
179
26.6k
  }
bool clang::IdentifierInfo::isStr<3ul>(char const (&) [3ul]) const
Line
Count
Source
176
1.38k
  bool isStr(const char (&Str)[StrLen]) const {
177
1.38k
    return getLength() == StrLen-1 &&
178
1.38k
           
memcmp(getNameStart(), Str, StrLen-1) == 0954
;
179
1.38k
  }
bool clang::IdentifierInfo::isStr<2ul>(char const (&) [2ul]) const
Line
Count
Source
176
240k
  bool isStr(const char (&Str)[StrLen]) const {
177
240k
    return getLength() == StrLen-1 &&
178
240k
           
memcmp(getNameStart(), Str, StrLen-1) == 01.44k
;
179
240k
  }
bool clang::IdentifierInfo::isStr<18ul>(char const (&) [18ul]) const
Line
Count
Source
176
429k
  bool isStr(const char (&Str)[StrLen]) const {
177
429k
    return getLength() == StrLen-1 &&
178
429k
           
memcmp(getNameStart(), Str, StrLen-1) == 0155
;
179
429k
  }
bool clang::IdentifierInfo::isStr<19ul>(char const (&) [19ul]) const
Line
Count
Source
176
2.17k
  bool isStr(const char (&Str)[StrLen]) const {
177
2.17k
    return getLength() == StrLen-1 &&
178
2.17k
           
memcmp(getNameStart(), Str, StrLen-1) == 019
;
179
2.17k
  }
bool clang::IdentifierInfo::isStr<21ul>(char const (&) [21ul]) const
Line
Count
Source
176
85
  bool isStr(const char (&Str)[StrLen]) const {
177
85
    return getLength() == StrLen-1 &&
178
85
           
memcmp(getNameStart(), Str, StrLen-1) == 010
;
179
85
  }
bool clang::IdentifierInfo::isStr<29ul>(char const (&) [29ul]) const
Line
Count
Source
176
23.2M
  bool isStr(const char (&Str)[StrLen]) const {
177
23.2M
    return getLength() == StrLen-1 &&
178
23.2M
           
memcmp(getNameStart(), Str, StrLen-1) == 0302k
;
179
23.2M
  }
bool clang::IdentifierInfo::isStr<14ul>(char const (&) [14ul]) const
Line
Count
Source
176
1.89k
  bool isStr(const char (&Str)[StrLen]) const {
177
1.89k
    return getLength() == StrLen-1 &&
178
1.89k
           
memcmp(getNameStart(), Str, StrLen-1) == 01.09k
;
179
1.89k
  }
bool clang::IdentifierInfo::isStr<24ul>(char const (&) [24ul]) const
Line
Count
Source
176
1.62k
  bool isStr(const char (&Str)[StrLen]) const {
177
1.62k
    return getLength() == StrLen-1 &&
178
1.62k
           
memcmp(getNameStart(), Str, StrLen-1) == 022
;
179
1.62k
  }
bool clang::IdentifierInfo::isStr<27ul>(char const (&) [27ul]) const
Line
Count
Source
176
25.0k
  bool isStr(const char (&Str)[StrLen]) const {
177
25.0k
    return getLength() == StrLen-1 &&
178
25.0k
           
memcmp(getNameStart(), Str, StrLen-1) == 031
;
179
25.0k
  }
bool clang::IdentifierInfo::isStr<30ul>(char const (&) [30ul]) const
Line
Count
Source
176
73
  bool isStr(const char (&Str)[StrLen]) const {
177
73
    return getLength() == StrLen-1 &&
178
73
           
memcmp(getNameStart(), Str, StrLen-1) == 03
;
179
73
  }
180
181
  /// Return true if this is the identifier for the specified StringRef.
182
36.3k
  bool isStr(llvm::StringRef Str) const {
183
36.3k
    llvm::StringRef ThisStr(getNameStart(), getLength());
184
36.3k
    return ThisStr == Str;
185
36.3k
  }
186
187
  /// Return the beginning of the actual null-terminated string for this
188
  /// identifier.
189
1.93G
  const char *getNameStart() const { return Entry->getKeyData(); }
190
191
  /// Efficiently return the length of this identifier info.
192
2.15G
  unsigned getLength() const { return Entry->getKeyLength(); }
193
194
  /// Return the actual identifier string.
195
1.77G
  StringRef getName() const {
196
1.77G
    return StringRef(getNameStart(), getLength());
197
1.77G
  }
198
199
  /// Return true if this identifier is \#defined to some other value.
200
  /// \note The current definition may be in a module and not currently visible.
201
855M
  bool hasMacroDefinition() const {
202
855M
    return HasMacro;
203
855M
  }
204
49.8M
  void setHasMacroDefinition(bool Val) {
205
49.8M
    if (HasMacro == Val) 
return897k
;
206
207
48.9M
    HasMacro = Val;
208
48.9M
    if (Val) {
209
48.8M
      NeedsHandleIdentifier = true;
210
48.8M
      HadMacro = true;
211
48.8M
    } else {
212
      // If this is a final macro, make the deprecation and header unsafe bits
213
      // stick around after the undefinition so they apply to any redefinitions.
214
81.2k
      if (!IsFinal) {
215
        // Because calling the setters of these calls recomputes, just set them
216
        // manually to avoid recomputing a bunch of times.
217
81.2k
        IsDeprecatedMacro = false;
218
81.2k
        IsRestrictExpansion = false;
219
81.2k
      }
220
81.2k
      RecomputeNeedsHandleIdentifier();
221
81.2k
    }
222
48.9M
  }
223
  /// Returns true if this identifier was \#defined to some value at any
224
  /// moment. In this case there should be an entry for the identifier in the
225
  /// macro history table in Preprocessor.
226
87.1M
  bool hadMacroDefinition() const {
227
87.1M
    return HadMacro;
228
87.1M
  }
229
230
70.7M
  bool isDeprecatedMacro() const { return IsDeprecatedMacro; }
231
232
31
  void setIsDeprecatedMacro(bool Val) {
233
31
    if (IsDeprecatedMacro == Val)
234
1
      return;
235
30
    IsDeprecatedMacro = Val;
236
30
    if (Val)
237
30
      NeedsHandleIdentifier = true;
238
0
    else
239
0
      RecomputeNeedsHandleIdentifier();
240
30
  }
241
242
70.7M
  bool isRestrictExpansion() const { return IsRestrictExpansion; }
243
244
7
  void setIsRestrictExpansion(bool Val) {
245
7
    if (IsRestrictExpansion == Val)
246
1
      return;
247
6
    IsRestrictExpansion = Val;
248
6
    if (Val)
249
6
      NeedsHandleIdentifier = true;
250
0
    else
251
0
      RecomputeNeedsHandleIdentifier();
252
6
  }
253
254
640k
  bool isFinal() const { return IsFinal; }
255
256
3
  void setIsFinal(bool Val) { IsFinal = Val; }
257
258
  /// If this is a source-language token (e.g. 'for'), this API
259
  /// can be used to cause the lexer to map identifiers to source-language
260
  /// tokens.
261
715M
  tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; }
262
263
  /// True if revertTokenIDToIdentifier() was called.
264
45.3M
  bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; }
265
266
  /// Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2
267
  /// compatibility.
268
  ///
269
  /// TokenID is normally read-only but there are 2 instances where we revert it
270
  /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens
271
  /// using this method so we can inform serialization about it.
272
129k
  void revertTokenIDToIdentifier() {
273
129k
    assert(TokenID != tok::identifier && "Already at tok::identifier");
274
0
    TokenID = tok::identifier;
275
129k
    RevertedTokenID = true;
276
129k
  }
277
5
  void revertIdentifierToTokenID(tok::TokenKind TK) {
278
5
    assert(TokenID == tok::identifier && "Should be at tok::identifier");
279
0
    TokenID = TK;
280
5
    RevertedTokenID = false;
281
5
  }
282
283
  /// Return the preprocessor keyword ID for this identifier.
284
  ///
285
  /// For example, "define" will return tok::pp_define.
286
  tok::PPKeywordKind getPPKeywordID() const;
287
288
  /// Return the Objective-C keyword ID for the this identifier.
289
  ///
290
  /// For example, 'class' will return tok::objc_class if ObjC is enabled.
291
1.33M
  tok::ObjCKeywordKind getObjCKeywordID() const {
292
1.33M
    if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS)
293
1.33M
      return tok::ObjCKeywordKind(ObjCOrBuiltinID);
294
6
    else
295
6
      return tok::objc_not_keyword;
296
1.33M
  }
297
2.63M
  void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; }
298
299
  /// Return a value indicating whether this is a builtin function.
300
  ///
301
  /// 0 is not-built-in. 1+ are specific builtin functions.
302
320M
  unsigned getBuiltinID() const {
303
320M
    if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
304
88.4M
      return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
305
231M
    else
306
231M
      return 0;
307
320M
  }
308
271M
  void setBuiltinID(unsigned ID) {
309
271M
    ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS;
310
271M
    assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID
311
271M
           && "ID too large for field!");
312
271M
  }
313
314
56.7M
  unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; }
315
624k
  void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; }
316
317
  /// get/setExtension - Initialize information about whether or not this
318
  /// language token is an extension.  This controls extension warnings, and is
319
  /// only valid if a custom token ID is set.
320
126M
  bool isExtensionToken() const { return IsExtension; }
321
41.6M
  void setIsExtensionToken(bool Val) {
322
41.6M
    IsExtension = Val;
323
41.6M
    if (Val)
324
3.76M
      NeedsHandleIdentifier = true;
325
37.8M
    else
326
37.8M
      RecomputeNeedsHandleIdentifier();
327
41.6M
  }
328
329
  /// is/setIsFutureCompatKeyword - Initialize information about whether or not
330
  /// this language token is a keyword in a newer or proposed Standard. This
331
  /// controls compatibility warnings, and is only true when not parsing the
332
  /// corresponding Standard. Once a compatibility problem has been diagnosed
333
  /// with this keyword, the flag will be cleared.
334
108M
  bool isFutureCompatKeyword() const { return IsFutureCompatKeyword; }
335
41.6M
  void setIsFutureCompatKeyword(bool Val) {
336
41.6M
    IsFutureCompatKeyword = Val;
337
41.6M
    if (Val)
338
783k
      NeedsHandleIdentifier = true;
339
40.8M
    else
340
40.8M
      RecomputeNeedsHandleIdentifier();
341
41.6M
  }
342
343
  /// setIsPoisoned - Mark this identifier as poisoned.  After poisoning, the
344
  /// Preprocessor will emit an error every time this token is used.
345
91.2M
  void setIsPoisoned(bool Value = true) {
346
91.2M
    IsPoisoned = Value;
347
91.2M
    if (Value)
348
90.5M
      NeedsHandleIdentifier = true;
349
688k
    else
350
688k
      RecomputeNeedsHandleIdentifier();
351
91.2M
  }
352
353
  /// Return true if this token has been poisoned.
354
563M
  bool isPoisoned() const { return IsPoisoned; }
355
356
  /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether
357
  /// this identifier is a C++ alternate representation of an operator.
358
1.31M
  void setIsCPlusPlusOperatorKeyword(bool Val = true) {
359
1.31M
    IsCPPOperatorKeyword = Val;
360
1.31M
  }
361
66.8M
  bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; }
362
363
  /// Return true if this token is a keyword in the specified language.
364
  bool isKeyword(const LangOptions &LangOpts) const;
365
366
  /// Return true if this token is a C++ keyword in the specified
367
  /// language.
368
  bool isCPlusPlusKeyword(const LangOptions &LangOpts) const;
369
370
  /// Get and set FETokenInfo. The language front-end is allowed to associate
371
  /// arbitrary metadata with this token.
372
496M
  void *getFETokenInfo() const { return FETokenInfo; }
373
84.9M
  void setFETokenInfo(void *T) { FETokenInfo = T; }
374
375
  /// Return true if the Preprocessor::HandleIdentifier must be called
376
  /// on a token of this identifier.
377
  ///
378
  /// If this returns false, we know that HandleIdentifier will not affect
379
  /// the token.
380
698M
  bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
381
382
  /// Return true if the identifier in its current state was loaded
383
  /// from an AST file.
384
169M
  bool isFromAST() const { return IsFromAST; }
385
386
17.3M
  void setIsFromAST() { IsFromAST = true; }
387
388
  /// Determine whether this identifier has changed since it was loaded
389
  /// from an AST file.
390
1.98M
  bool hasChangedSinceDeserialization() const {
391
1.98M
    return ChangedAfterLoad;
392
1.98M
  }
393
394
  /// Note that this identifier has changed since it was loaded from
395
  /// an AST file.
396
98.7k
  void setChangedSinceDeserialization() {
397
98.7k
    ChangedAfterLoad = true;
398
98.7k
  }
399
400
  /// Determine whether the frontend token information for this
401
  /// identifier has changed since it was loaded from an AST file.
402
120k
  bool hasFETokenInfoChangedSinceDeserialization() const {
403
120k
    return FEChangedAfterLoad;
404
120k
  }
405
406
  /// Note that the frontend token information for this identifier has
407
  /// changed since it was loaded from an AST file.
408
478k
  void setFETokenInfoChangedSinceDeserialization() {
409
478k
    FEChangedAfterLoad = true;
410
478k
  }
411
412
  /// Determine whether the information for this identifier is out of
413
  /// date with respect to the external source.
414
923M
  bool isOutOfDate() const { return OutOfDate; }
415
416
  /// Set whether the information for this identifier is out of
417
  /// date with respect to the external source.
418
28.7M
  void setOutOfDate(bool OOD) {
419
28.7M
    OutOfDate = OOD;
420
28.7M
    if (OOD)
421
22.2M
      NeedsHandleIdentifier = true;
422
6.49M
    else
423
6.49M
      RecomputeNeedsHandleIdentifier();
424
28.7M
  }
425
426
  /// Determine whether this is the contextual keyword \c import.
427
83.0M
  bool isModulesImport() const { return IsModulesImport; }
428
429
  /// Set whether this identifier is the contextual keyword \c import.
430
171k
  void setModulesImport(bool I) {
431
171k
    IsModulesImport = I;
432
171k
    if (I)
433
171k
      NeedsHandleIdentifier = true;
434
0
    else
435
0
      RecomputeNeedsHandleIdentifier();
436
171k
  }
437
438
  /// Determine whether this is the mangled name of an OpenMP variant.
439
3.85M
  bool isMangledOpenMPVariantName() const { return IsMangledOpenMPVariantName; }
440
441
  /// Set whether this is the mangled name of an OpenMP variant.
442
7.20k
  void setMangledOpenMPVariantName(bool I) { IsMangledOpenMPVariantName = I; }
443
444
  /// Return true if this identifier is an editor placeholder.
445
  ///
446
  /// Editor placeholders are produced by the code-completion engine and are
447
  /// represented as characters between '<#' and '#>' in the source code. An
448
  /// example of auto-completed call with a placeholder parameter is shown
449
  /// below:
450
  /// \code
451
  ///   function(<#int x#>);
452
  /// \endcode
453
18.3M
  bool isEditorPlaceholder() const {
454
18.3M
    return getName().startswith("<#") && 
getName().endswith("#>")27
;
455
18.3M
  }
456
457
  /// Determine whether \p this is a name reserved for the implementation (C99
458
  /// 7.1.3, C++ [lib.global.names]).
459
  ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const;
460
461
  /// If the identifier is an "uglified" reserved name, return a cleaned form.
462
  /// e.g. _Foo => Foo. Otherwise, just returns the name.
463
  StringRef deuglifiedName() const;
464
465
  /// Provide less than operator for lexicographical sorting.
466
579M
  bool operator<(const IdentifierInfo &RHS) const {
467
579M
    return getName() < RHS.getName();
468
579M
  }
469
470
private:
471
  /// The Preprocessor::HandleIdentifier does several special (but rare)
472
  /// things to identifiers of various sorts.  For example, it changes the
473
  /// \c for keyword token from tok::identifier to tok::for.
474
  ///
475
  /// This method is very tied to the definition of HandleIdentifier.  Any
476
  /// change to it should be reflected here.
477
86.0M
  void RecomputeNeedsHandleIdentifier() {
478
86.0M
    NeedsHandleIdentifier = isPoisoned() || 
hasMacroDefinition()86.0M
||
479
86.0M
                            
isExtensionToken()85.8M
||
isFutureCompatKeyword()82.0M
||
480
86.0M
                            
isOutOfDate()82.0M
||
isModulesImport()82.0M
;
481
86.0M
  }
482
};
483
484
/// An RAII object for [un]poisoning an identifier within a scope.
485
///
486
/// \p II is allowed to be null, in which case objects of this type have
487
/// no effect.
488
class PoisonIdentifierRAIIObject {
489
  IdentifierInfo *const II;
490
  const bool OldValue;
491
492
public:
493
  PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
494
28.7M
    : II(II), OldValue(II ? II->isPoisoned() : false) {
495
28.7M
    if(II)
496
432
      II->setIsPoisoned(NewValue);
497
28.7M
  }
498
499
28.7M
  ~PoisonIdentifierRAIIObject() {
500
28.7M
    if(II)
501
432
      II->setIsPoisoned(OldValue);
502
28.7M
  }
503
};
504
505
/// An iterator that walks over all of the known identifiers
506
/// in the lookup table.
507
///
508
/// Since this iterator uses an abstract interface via virtual
509
/// functions, it uses an object-oriented interface rather than the
510
/// more standard C++ STL iterator interface. In this OO-style
511
/// iteration, the single function \c Next() provides dereference,
512
/// advance, and end-of-sequence checking in a single
513
/// operation. Subclasses of this iterator type will provide the
514
/// actual functionality.
515
class IdentifierIterator {
516
protected:
517
384
  IdentifierIterator() = default;
518
519
public:
520
  IdentifierIterator(const IdentifierIterator &) = delete;
521
  IdentifierIterator &operator=(const IdentifierIterator &) = delete;
522
523
  virtual ~IdentifierIterator();
524
525
  /// Retrieve the next string in the identifier table and
526
  /// advances the iterator for the following string.
527
  ///
528
  /// \returns The next string in the identifier table. If there is
529
  /// no such string, returns an empty \c StringRef.
530
  virtual StringRef Next() = 0;
531
};
532
533
/// Provides lookups to, and iteration over, IdentiferInfo objects.
534
class IdentifierInfoLookup {
535
public:
536
  virtual ~IdentifierInfoLookup();
537
538
  /// Return the IdentifierInfo for the specified named identifier.
539
  ///
540
  /// Unlike the version in IdentifierTable, this returns a pointer instead
541
  /// of a reference.  If the pointer is null then the IdentifierInfo cannot
542
  /// be found.
543
  virtual IdentifierInfo* get(StringRef Name) = 0;
544
545
  /// Retrieve an iterator into the set of all identifiers
546
  /// known to this identifier lookup source.
547
  ///
548
  /// This routine provides access to all of the identifiers known to
549
  /// the identifier lookup, allowing access to the contents of the
550
  /// identifiers without introducing the overhead of constructing
551
  /// IdentifierInfo objects for each.
552
  ///
553
  /// \returns A new iterator into the set of known identifiers. The
554
  /// caller is responsible for deleting this iterator.
555
  virtual IdentifierIterator *getIdentifiers();
556
};
557
558
/// Implements an efficient mapping from strings to IdentifierInfo nodes.
559
///
560
/// This has no other purpose, but this is an extremely performance-critical
561
/// piece of the code, as each occurrence of every identifier goes through
562
/// here when lexed.
563
class IdentifierTable {
564
  // Shark shows that using MallocAllocator is *much* slower than using this
565
  // BumpPtrAllocator!
566
  using HashTableTy = llvm::StringMap<IdentifierInfo *, llvm::BumpPtrAllocator>;
567
  HashTableTy HashTable;
568
569
  IdentifierInfoLookup* ExternalLookup;
570
571
public:
572
  /// Create the identifier table.
573
  explicit IdentifierTable(IdentifierInfoLookup *ExternalLookup = nullptr);
574
575
  /// Create the identifier table, populating it with info about the
576
  /// language keywords for the language specified by \p LangOpts.
577
  explicit IdentifierTable(const LangOptions &LangOpts,
578
                           IdentifierInfoLookup *ExternalLookup = nullptr);
579
580
  /// Set the external identifier lookup mechanism.
581
18.5k
  void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) {
582
18.5k
    ExternalLookup = IILookup;
583
18.5k
  }
584
585
  /// Retrieve the external identifier lookup object, if any.
586
5.08M
  IdentifierInfoLookup *getExternalIdentifierLookup() const {
587
5.08M
    return ExternalLookup;
588
5.08M
  }
589
590
426M
  llvm::BumpPtrAllocator& getAllocator() {
591
426M
    return HashTable.getAllocator();
592
426M
  }
593
594
  /// Return the identifier token info for the specified named
595
  /// identifier.
596
848M
  IdentifierInfo &get(StringRef Name) {
597
848M
    auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
598
599
848M
    IdentifierInfo *&II = Entry.second;
600
848M
    if (II) 
return *II436M
;
601
602
    // No entry; if we have an external lookup, look there first.
603
411M
    if (ExternalLookup) {
604
3.48M
      II = ExternalLookup->get(Name);
605
3.48M
      if (II)
606
726k
        return *II;
607
3.48M
    }
608
609
    // Lookups failed, make a new IdentifierInfo.
610
410M
    void *Mem = getAllocator().Allocate<IdentifierInfo>();
611
410M
    II = new (Mem) IdentifierInfo();
612
613
    // Make sure getName() knows how to find the IdentifierInfo
614
    // contents.
615
410M
    II->Entry = &Entry;
616
617
410M
    return *II;
618
411M
  }
619
620
42.9M
  IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
621
42.9M
    IdentifierInfo &II = get(Name);
622
42.9M
    II.TokenID = TokenCode;
623
42.9M
    assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large");
624
0
    return II;
625
42.9M
  }
626
627
  /// Gets an IdentifierInfo for the given name without consulting
628
  ///        external sources.
629
  ///
630
  /// This is a version of get() meant for external sources that want to
631
  /// introduce or modify an identifier. If they called get(), they would
632
  /// likely end up in a recursion.
633
17.4M
  IdentifierInfo &getOwn(StringRef Name) {
634
17.4M
    auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
635
636
17.4M
    IdentifierInfo *&II = Entry.second;
637
17.4M
    if (II)
638
1.83M
      return *II;
639
640
    // Lookups failed, make a new IdentifierInfo.
641
15.5M
    void *Mem = getAllocator().Allocate<IdentifierInfo>();
642
15.5M
    II = new (Mem) IdentifierInfo();
643
644
    // Make sure getName() knows how to find the IdentifierInfo
645
    // contents.
646
15.5M
    II->Entry = &Entry;
647
648
    // If this is the 'import' contextual keyword, mark it as such.
649
15.5M
    if (Name.equals("import"))
650
0
      II->setModulesImport(true);
651
652
15.5M
    return *II;
653
17.4M
  }
654
655
  using iterator = HashTableTy::const_iterator;
656
  using const_iterator = HashTableTy::const_iterator;
657
658
27.4k
  iterator begin() const { return HashTable.begin(); }
659
27.5k
  iterator end() const   { return HashTable.end(); }
660
0
  unsigned size() const  { return HashTable.size(); }
661
662
57
  iterator find(StringRef Name) const { return HashTable.find(Name); }
663
664
  /// Print some statistics to stderr that indicate how well the
665
  /// hashing is doing.
666
  void PrintStats() const;
667
668
  /// Populate the identifier table with info about the language keywords
669
  /// for the language specified by \p LangOpts.
670
  void AddKeywords(const LangOptions &LangOpts);
671
};
672
673
/// A family of Objective-C methods.
674
///
675
/// These families have no inherent meaning in the language, but are
676
/// nonetheless central enough in the existing implementations to
677
/// merit direct AST support.  While, in theory, arbitrary methods can
678
/// be considered to form families, we focus here on the methods
679
/// involving allocation and retain-count management, as these are the
680
/// most "core" and the most likely to be useful to diverse clients
681
/// without extra information.
682
///
683
/// Both selectors and actual method declarations may be classified
684
/// into families.  Method families may impose additional restrictions
685
/// beyond their selector name; for example, a method called '_init'
686
/// that returns void is not considered to be in the 'init' family
687
/// (but would be if it returned 'id').  It is also possible to
688
/// explicitly change or remove a method's family.  Therefore the
689
/// method's family should be considered the single source of truth.
690
enum ObjCMethodFamily {
691
  /// No particular method family.
692
  OMF_None,
693
694
  // Selectors in these families may have arbitrary arity, may be
695
  // written with arbitrary leading underscores, and may have
696
  // additional CamelCase "words" in their first selector chunk
697
  // following the family name.
698
  OMF_alloc,
699
  OMF_copy,
700
  OMF_init,
701
  OMF_mutableCopy,
702
  OMF_new,
703
704
  // These families are singletons consisting only of the nullary
705
  // selector with the given name.
706
  OMF_autorelease,
707
  OMF_dealloc,
708
  OMF_finalize,
709
  OMF_release,
710
  OMF_retain,
711
  OMF_retainCount,
712
  OMF_self,
713
  OMF_initialize,
714
715
  // performSelector families
716
  OMF_performSelector
717
};
718
719
/// Enough bits to store any enumerator in ObjCMethodFamily or
720
/// InvalidObjCMethodFamily.
721
enum { ObjCMethodFamilyBitWidth = 4 };
722
723
/// An invalid value of ObjCMethodFamily.
724
enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 };
725
726
/// A family of Objective-C methods.
727
///
728
/// These are family of methods whose result type is initially 'id', but
729
/// but are candidate for the result type to be changed to 'instancetype'.
730
enum ObjCInstanceTypeFamily {
731
  OIT_None,
732
  OIT_Array,
733
  OIT_Dictionary,
734
  OIT_Singleton,
735
  OIT_Init,
736
  OIT_ReturnsSelf
737
};
738
739
enum ObjCStringFormatFamily {
740
  SFF_None,
741
  SFF_NSString,
742
  SFF_CFString
743
};
744
745
/// Smart pointer class that efficiently represents Objective-C method
746
/// names.
747
///
748
/// This class will either point to an IdentifierInfo or a
749
/// MultiKeywordSelector (which is private). This enables us to optimize
750
/// selectors that take no arguments and selectors that take 1 argument, which
751
/// accounts for 78% of all selectors in Cocoa.h.
752
class Selector {
753
  friend class Diagnostic;
754
  friend class SelectorTable; // only the SelectorTable can create these
755
  friend class DeclarationName; // and the AST's DeclarationName.
756
757
  enum IdentifierInfoFlag {
758
    // Empty selector = 0. Note that these enumeration values must
759
    // correspond to the enumeration values of DeclarationName::StoredNameKind
760
    ZeroArg  = 0x01,
761
    OneArg   = 0x02,
762
    MultiArg = 0x07,
763
    ArgFlags = 0x07
764
  };
765
766
  /// A pointer to the MultiKeywordSelector or IdentifierInfo. We use the low
767
  /// three bits of InfoPtr to store an IdentifierInfoFlag. Note that in any
768
  /// case IdentifierInfo and MultiKeywordSelector are already aligned to
769
  /// 8 bytes even on 32 bits archs because of DeclarationName.
770
  uintptr_t InfoPtr = 0;
771
772
1.22M
  Selector(IdentifierInfo *II, unsigned nArgs) {
773
1.22M
    InfoPtr = reinterpret_cast<uintptr_t>(II);
774
1.22M
    assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
775
0
    assert(nArgs < 2 && "nArgs not equal to 0/1");
776
0
    InfoPtr |= nArgs+1;
777
1.22M
  }
778
779
324k
  Selector(MultiKeywordSelector *SI) {
780
324k
    InfoPtr = reinterpret_cast<uintptr_t>(SI);
781
324k
    assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
782
0
    InfoPtr |= MultiArg;
783
324k
  }
784
785
1.80M
  IdentifierInfo *getAsIdentifierInfo() const {
786
1.80M
    if (getIdentifierInfoFlag() < MultiArg)
787
1.80M
      return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
788
0
    return nullptr;
789
1.80M
  }
790
791
2.21M
  MultiKeywordSelector *getMultiKeywordSelector() const {
792
2.21M
    return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags);
793
2.21M
  }
794
795
7.92M
  unsigned getIdentifierInfoFlag() const {
796
7.92M
    return InfoPtr & ArgFlags;
797
7.92M
  }
798
799
  static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
800
801
  static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel);
802
803
public:
804
  /// The default ctor should only be used when creating data structures that
805
  ///  will contain selectors.
806
4.22M
  Selector() = default;
807
33.9M
  explicit Selector(uintptr_t V) : InfoPtr(V) {}
808
809
  /// operator==/!= - Indicate whether the specified selectors are identical.
810
84.2M
  bool operator==(Selector RHS) const {
811
84.2M
    return InfoPtr == RHS.InfoPtr;
812
84.2M
  }
813
58.0k
  bool operator!=(Selector RHS) const {
814
58.0k
    return InfoPtr != RHS.InfoPtr;
815
58.0k
  }
816
817
7.51M
  void *getAsOpaquePtr() const {
818
7.51M
    return reinterpret_cast<void*>(InfoPtr);
819
7.51M
  }
820
821
  /// Determine whether this is the empty selector.
822
66.7k
  bool isNull() const { return InfoPtr == 0; }
823
824
  // Predicates to identify the selector type.
825
936
  bool isKeywordSelector() const {
826
936
    return getIdentifierInfoFlag() != ZeroArg;
827
936
  }
828
829
268k
  bool isUnarySelector() const {
830
268k
    return getIdentifierInfoFlag() == ZeroArg;
831
268k
  }
832
833
  /// If this selector is the specific keyword selector described by Names.
834
  bool isKeywordSelector(ArrayRef<StringRef> Names) const;
835
836
  /// If this selector is the specific unary selector described by Name.
837
  bool isUnarySelector(StringRef Name) const;
838
839
  unsigned getNumArgs() const;
840
841
  /// Retrieve the identifier at a given position in the selector.
842
  ///
843
  /// Note that the identifier pointer returned may be NULL. Clients that only
844
  /// care about the text of the identifier string, and not the specific,
845
  /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
846
  /// an empty string when the identifier pointer would be NULL.
847
  ///
848
  /// \param argIndex The index for which we want to retrieve the identifier.
849
  /// This index shall be less than \c getNumArgs() unless this is a keyword
850
  /// selector, in which case 0 is the only permissible value.
851
  ///
852
  /// \returns the uniqued identifier for this slot, or NULL if this slot has
853
  /// no corresponding identifier.
854
  IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
855
856
  /// Retrieve the name at a given position in the selector.
857
  ///
858
  /// \param argIndex The index for which we want to retrieve the name.
859
  /// This index shall be less than \c getNumArgs() unless this is a keyword
860
  /// selector, in which case 0 is the only permissible value.
861
  ///
862
  /// \returns the name for this slot, which may be the empty string if no
863
  /// name was supplied.
864
  StringRef getNameForSlot(unsigned argIndex) const;
865
866
  /// Derive the full selector name (e.g. "foo:bar:") and return
867
  /// it as an std::string.
868
  std::string getAsString() const;
869
870
  /// Prints the full selector name (e.g. "foo:bar:").
871
  void print(llvm::raw_ostream &OS) const;
872
873
  void dump() const;
874
875
  /// Derive the conventional family of this method.
876
243k
  ObjCMethodFamily getMethodFamily() const {
877
243k
    return getMethodFamilyImpl(*this);
878
243k
  }
879
880
25.3k
  ObjCStringFormatFamily getStringFormatFamily() const {
881
25.3k
    return getStringFormatFamilyImpl(*this);
882
25.3k
  }
883
884
9.02M
  static Selector getEmptyMarker() {
885
9.02M
    return Selector(uintptr_t(-1));
886
9.02M
  }
887
888
7.15M
  static Selector getTombstoneMarker() {
889
7.15M
    return Selector(uintptr_t(-2));
890
7.15M
  }
891
892
  static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel);
893
};
894
895
/// This table allows us to fully hide how we implement
896
/// multi-keyword caching.
897
class SelectorTable {
898
  // Actually a SelectorTableImpl
899
  void *Impl;
900
901
public:
902
  SelectorTable();
903
  SelectorTable(const SelectorTable &) = delete;
904
  SelectorTable &operator=(const SelectorTable &) = delete;
905
  ~SelectorTable();
906
907
  /// Can create any sort of selector.
908
  ///
909
  /// \p NumArgs indicates whether this is a no argument selector "foo", a
910
  /// single argument selector "foo:" or multi-argument "foo:bar:".
911
  Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
912
913
369k
  Selector getUnarySelector(IdentifierInfo *ID) {
914
369k
    return Selector(ID, 1);
915
369k
  }
916
917
480k
  Selector getNullarySelector(IdentifierInfo *ID) {
918
480k
    return Selector(ID, 0);
919
480k
  }
920
921
  /// Return the total amount of memory allocated for managing selectors.
922
  size_t getTotalMemory() const;
923
924
  /// Return the default setter name for the given identifier.
925
  ///
926
  /// This is "set" + \p Name where the initial character of \p Name
927
  /// has been capitalized.
928
  static SmallString<64> constructSetterName(StringRef Name);
929
930
  /// Return the default setter selector for the given identifier.
931
  ///
932
  /// This is "set" + \p Name where the initial character of \p Name
933
  /// has been capitalized.
934
  static Selector constructSetterSelector(IdentifierTable &Idents,
935
                                          SelectorTable &SelTable,
936
                                          const IdentifierInfo *Name);
937
938
  /// Return the property name for the given setter selector.
939
  static std::string getPropertyNameFromSetterSelector(Selector Sel);
940
};
941
942
namespace detail {
943
944
/// DeclarationNameExtra is used as a base of various uncommon special names.
945
/// This class is needed since DeclarationName has not enough space to store
946
/// the kind of every possible names. Therefore the kind of common names is
947
/// stored directly in DeclarationName, and the kind of uncommon names is
948
/// stored in DeclarationNameExtra. It is aligned to 8 bytes because
949
/// DeclarationName needs the lower 3 bits to store the kind of common names.
950
/// DeclarationNameExtra is tightly coupled to DeclarationName and any change
951
/// here is very likely to require changes in DeclarationName(Table).
952
class alignas(IdentifierInfoAlignment) DeclarationNameExtra {
953
  friend class clang::DeclarationName;
954
  friend class clang::DeclarationNameTable;
955
956
protected:
957
  /// The kind of "extra" information stored in the DeclarationName. See
958
  /// @c ExtraKindOrNumArgs for an explanation of how these enumerator values
959
  /// are used. Note that DeclarationName depends on the numerical values
960
  /// of the enumerators in this enum. See DeclarationName::StoredNameKind
961
  /// for more info.
962
  enum ExtraKind {
963
    CXXDeductionGuideName,
964
    CXXLiteralOperatorName,
965
    CXXUsingDirective,
966
    ObjCMultiArgSelector
967
  };
968
969
  /// ExtraKindOrNumArgs has one of the following meaning:
970
  ///  * The kind of an uncommon C++ special name. This DeclarationNameExtra
971
  ///    is in this case in fact either a CXXDeductionGuideNameExtra or
972
  ///    a CXXLiteralOperatorIdName.
973
  ///
974
  ///  * It may be also name common to C++ using-directives (CXXUsingDirective),
975
  ///
976
  ///  * Otherwise it is ObjCMultiArgSelector+NumArgs, where NumArgs is
977
  ///    the number of arguments in the Objective-C selector, in which
978
  ///    case the DeclarationNameExtra is also a MultiKeywordSelector.
979
  unsigned ExtraKindOrNumArgs;
980
981
24.9k
  DeclarationNameExtra(ExtraKind Kind) : ExtraKindOrNumArgs(Kind) {}
982
  DeclarationNameExtra(unsigned NumArgs)
983
243k
      : ExtraKindOrNumArgs(ObjCMultiArgSelector + NumArgs) {}
984
985
  /// Return the corresponding ExtraKind.
986
7.82M
  ExtraKind getKind() const {
987
7.82M
    return static_cast<ExtraKind>(ExtraKindOrNumArgs >
988
7.82M
                                          (unsigned)ObjCMultiArgSelector
989
7.82M
                                      ? 
(unsigned)ObjCMultiArgSelector7.62M
990
7.82M
                                      : 
ExtraKindOrNumArgs202k
);
991
7.82M
  }
992
993
  /// Return the number of arguments in an ObjC selector. Only valid when this
994
  /// is indeed an ObjCMultiArgSelector.
995
2.84M
  unsigned getNumArgs() const {
996
2.84M
    assert(ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector &&
997
2.84M
           "getNumArgs called but this is not an ObjC selector!");
998
0
    return ExtraKindOrNumArgs - (unsigned)ObjCMultiArgSelector;
999
2.84M
  }
1000
};
1001
1002
} // namespace detail
1003
1004
}  // namespace clang
1005
1006
namespace llvm {
1007
1008
/// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
1009
/// DenseSets.
1010
template <>
1011
struct DenseMapInfo<clang::Selector> {
1012
9.02M
  static clang::Selector getEmptyKey() {
1013
9.02M
    return clang::Selector::getEmptyMarker();
1014
9.02M
  }
1015
1016
7.15M
  static clang::Selector getTombstoneKey() {
1017
7.15M
    return clang::Selector::getTombstoneMarker();
1018
7.15M
  }
1019
1020
  static unsigned getHashValue(clang::Selector S);
1021
1022
84.1M
  static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
1023
84.1M
    return LHS == RHS;
1024
84.1M
  }
1025
};
1026
1027
template<>
1028
struct PointerLikeTypeTraits<clang::Selector> {
1029
168k
  static const void *getAsVoidPointer(clang::Selector P) {
1030
168k
    return P.getAsOpaquePtr();
1031
168k
  }
1032
1033
0
  static clang::Selector getFromVoidPointer(const void *P) {
1034
0
    return clang::Selector(reinterpret_cast<uintptr_t>(P));
1035
0
  }
1036
1037
  static constexpr int NumLowBitsAvailable = 0;
1038
};
1039
1040
// Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
1041
// are not guaranteed to be 8-byte aligned.
1042
template<>
1043
struct PointerLikeTypeTraits<clang::IdentifierInfo*> {
1044
2.24k
  static void *getAsVoidPointer(clang::IdentifierInfo* P) {
1045
2.24k
    return P;
1046
2.24k
  }
1047
1048
22
  static clang::IdentifierInfo *getFromVoidPointer(void *P) {
1049
22
    return static_cast<clang::IdentifierInfo*>(P);
1050
22
  }
1051
1052
  static constexpr int NumLowBitsAvailable = 1;
1053
};
1054
1055
template<>
1056
struct PointerLikeTypeTraits<const clang::IdentifierInfo*> {
1057
377k
  static const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
1058
377k
    return P;
1059
377k
  }
1060
1061
0
  static const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
1062
0
    return static_cast<const clang::IdentifierInfo*>(P);
1063
0
  }
1064
1065
  static constexpr int NumLowBitsAvailable = 1;
1066
};
1067
1068
} // namespace llvm
1069
1070
#endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H