Coverage Report

Created: 2018-09-21 05:35

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
/// \file
11
/// Defines the clang::IdentifierInfo, clang::IdentifierTable, and
12
/// clang::Selector interfaces.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
17
#define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
18
19
#include "clang/Basic/LLVM.h"
20
#include "clang/Basic/TokenKinds.h"
21
#include "llvm/ADT/DenseMapInfo.h"
22
#include "llvm/ADT/SmallString.h"
23
#include "llvm/ADT/StringMap.h"
24
#include "llvm/ADT/StringRef.h"
25
#include "llvm/Support/Allocator.h"
26
#include "llvm/Support/PointerLikeTypeTraits.h"
27
#include "llvm/Support/type_traits.h"
28
#include <cassert>
29
#include <cstddef>
30
#include <cstdint>
31
#include <cstring>
32
#include <string>
33
#include <utility>
34
35
namespace clang {
36
37
class IdentifierInfo;
38
class LangOptions;
39
class MultiKeywordSelector;
40
class SourceLocation;
41
42
/// A simple pair of identifier info and location.
43
using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>;
44
45
/// One of these records is kept for each identifier that
46
/// is lexed.  This contains information about whether the token was \#define'd,
47
/// is a language keyword, or if it is a front-end token of some sort (e.g. a
48
/// variable or function name).  The preprocessor keeps this information in a
49
/// set, and all tok::identifier tokens have a pointer to one of these.
50
class IdentifierInfo {
51
  friend class IdentifierTable;
52
53
  // Front-end token ID or tok::identifier.
54
  unsigned TokenID : 9;
55
56
  // ObjC keyword ('protocol' in '@protocol') or builtin (__builtin_inf).
57
  // First NUM_OBJC_KEYWORDS values are for Objective-C,
58
  // the remaining values are for builtins.
59
  unsigned ObjCOrBuiltinID : 13;
60
61
  // True if there is a #define for this.
62
  unsigned HasMacro : 1;
63
64
  // True if there was a #define for this.
65
  unsigned HadMacro : 1;
66
67
  // True if the identifier is a language extension.
68
  unsigned IsExtension : 1;
69
70
  // True if the identifier is a keyword in a newer or proposed Standard.
71
  unsigned IsFutureCompatKeyword : 1;
72
73
  // True if the identifier is poisoned.
74
  unsigned IsPoisoned : 1;
75
76
  // True if the identifier is a C++ operator keyword.
77
  unsigned IsCPPOperatorKeyword : 1;
78
79
  // Internal bit set by the member function RecomputeNeedsHandleIdentifier.
80
  // See comment about RecomputeNeedsHandleIdentifier for more info.
81
  unsigned NeedsHandleIdentifier : 1;
82
83
  // True if the identifier was loaded (at least partially) from an AST file.
84
  unsigned IsFromAST : 1;
85
86
  // True if the identifier has changed from the definition
87
  // loaded from an AST file.
88
  unsigned ChangedAfterLoad : 1;
89
90
  // True if the identifier's frontend information has changed from the
91
  // definition loaded from an AST file.
92
  unsigned FEChangedAfterLoad : 1;
93
94
  // True if revertTokenIDToIdentifier was called.
95
  unsigned RevertedTokenID : 1;
96
97
  // True if there may be additional information about
98
  // this identifier stored externally.
99
  unsigned OutOfDate : 1;
100
101
  // True if this is the 'import' contextual keyword.
102
  unsigned IsModulesImport : 1;
103
104
  // 29 bits left in a 64-bit word.
105
106
  // Managed by the language front-end.
107
  void *FETokenInfo = nullptr;
108
109
  llvm::StringMapEntry<IdentifierInfo *> *Entry = nullptr;
110
111
public:
112
  IdentifierInfo();
113
  IdentifierInfo(const IdentifierInfo &) = delete;
114
  IdentifierInfo &operator=(const IdentifierInfo &) = delete;
115
116
  /// Return true if this is the identifier for the specified string.
117
  ///
118
  /// This is intended to be used for string literals only: II->isStr("foo").
119
  template <std::size_t StrLen>
120
47.7M
  bool isStr(const char (&Str)[StrLen]) const {
121
47.7M
    return getLength() == StrLen-1 &&
122
47.7M
           
memcmp(getNameStart(), Str, StrLen-1) == 04.70M
;
123
47.7M
  }
bool clang::IdentifierInfo::isStr<8ul>(char const (&) [8ul]) const
Line
Count
Source
120
9.95M
  bool isStr(const char (&Str)[StrLen]) const {
121
9.95M
    return getLength() == StrLen-1 &&
122
9.95M
           
memcmp(getNameStart(), Str, StrLen-1) == 02.07M
;
123
9.95M
  }
bool clang::IdentifierInfo::isStr<5ul>(char const (&) [5ul]) const
Line
Count
Source
120
14.6M
  bool isStr(const char (&Str)[StrLen]) const {
121
14.6M
    return getLength() == StrLen-1 &&
122
14.6M
           
memcmp(getNameStart(), Str, StrLen-1) == 01.07M
;
123
14.6M
  }
bool clang::IdentifierInfo::isStr<4ul>(char const (&) [4ul]) const
Line
Count
Source
120
3.29M
  bool isStr(const char (&Str)[StrLen]) const {
121
3.29M
    return getLength() == StrLen-1 &&
122
3.29M
           
memcmp(getNameStart(), Str, StrLen-1) == 0535k
;
123
3.29M
  }
bool clang::IdentifierInfo::isStr<7ul>(char const (&) [7ul]) const
Line
Count
Source
120
5.33M
  bool isStr(const char (&Str)[StrLen]) const {
121
5.33M
    return getLength() == StrLen-1 &&
122
5.33M
           
memcmp(getNameStart(), Str, StrLen-1) == 0303k
;
123
5.33M
  }
bool clang::IdentifierInfo::isStr<9ul>(char const (&) [9ul]) const
Line
Count
Source
120
3.16M
  bool isStr(const char (&Str)[StrLen]) const {
121
3.16M
    return getLength() == StrLen-1 &&
122
3.16M
           
memcmp(getNameStart(), Str, StrLen-1) == 0248k
;
123
3.16M
  }
bool clang::IdentifierInfo::isStr<14ul>(char const (&) [14ul]) const
Line
Count
Source
120
2.70k
  bool isStr(const char (&Str)[StrLen]) const {
121
2.70k
    return getLength() == StrLen-1 &&
122
2.70k
           
memcmp(getNameStart(), Str, StrLen-1) == 0401
;
123
2.70k
  }
bool clang::IdentifierInfo::isStr<24ul>(char const (&) [24ul]) const
Line
Count
Source
120
3.84k
  bool isStr(const char (&Str)[StrLen]) const {
121
3.84k
    return getLength() == StrLen-1 &&
122
3.84k
           
memcmp(getNameStart(), Str, StrLen-1) == 059
;
123
3.84k
  }
bool clang::IdentifierInfo::isStr<11ul>(char const (&) [11ul]) const
Line
Count
Source
120
1.65M
  bool isStr(const char (&Str)[StrLen]) const {
121
1.65M
    return getLength() == StrLen-1 &&
122
1.65M
           
memcmp(getNameStart(), Str, StrLen-1) == 082.9k
;
123
1.65M
  }
bool clang::IdentifierInfo::isStr<10ul>(char const (&) [10ul]) const
Line
Count
Source
120
3.21M
  bool isStr(const char (&Str)[StrLen]) const {
121
3.21M
    return getLength() == StrLen-1 &&
122
3.21M
           
memcmp(getNameStart(), Str, StrLen-1) == 0263k
;
123
3.21M
  }
bool clang::IdentifierInfo::isStr<18ul>(char const (&) [18ul]) const
Line
Count
Source
120
632k
  bool isStr(const char (&Str)[StrLen]) const {
121
632k
    return getLength() == StrLen-1 &&
122
632k
           
memcmp(getNameStart(), Str, StrLen-1) == 06.33k
;
123
632k
  }
bool clang::IdentifierInfo::isStr<19ul>(char const (&) [19ul]) const
Line
Count
Source
120
1.82k
  bool isStr(const char (&Str)[StrLen]) const {
121
1.82k
    return getLength() == StrLen-1 &&
122
1.82k
           
memcmp(getNameStart(), Str, StrLen-1) == 023
;
123
1.82k
  }
bool clang::IdentifierInfo::isStr<20ul>(char const (&) [20ul]) const
Line
Count
Source
120
22.7k
  bool isStr(const char (&Str)[StrLen]) const {
121
22.7k
    return getLength() == StrLen-1 &&
122
22.7k
           
memcmp(getNameStart(), Str, StrLen-1) == 04.64k
;
123
22.7k
  }
bool clang::IdentifierInfo::isStr<27ul>(char const (&) [27ul]) const
Line
Count
Source
120
17.8k
  bool isStr(const char (&Str)[StrLen]) const {
121
17.8k
    return getLength() == StrLen-1 &&
122
17.8k
           
memcmp(getNameStart(), Str, StrLen-1) == 029
;
123
17.8k
  }
bool clang::IdentifierInfo::isStr<15ul>(char const (&) [15ul]) const
Line
Count
Source
120
18.8k
  bool isStr(const char (&Str)[StrLen]) const {
121
18.8k
    return getLength() == StrLen-1 &&
122
18.8k
           
memcmp(getNameStart(), Str, StrLen-1) == 0152
;
123
18.8k
  }
bool clang::IdentifierInfo::isStr<17ul>(char const (&) [17ul]) const
Line
Count
Source
120
4.93k
  bool isStr(const char (&Str)[StrLen]) const {
121
4.93k
    return getLength() == StrLen-1 &&
122
4.93k
           
memcmp(getNameStart(), Str, StrLen-1) == 012
;
123
4.93k
  }
bool clang::IdentifierInfo::isStr<30ul>(char const (&) [30ul]) const
Line
Count
Source
120
58
  bool isStr(const char (&Str)[StrLen]) const {
121
58
    return getLength() == StrLen-1 &&
122
58
           
memcmp(getNameStart(), Str, StrLen-1) == 02
;
123
58
  }
bool clang::IdentifierInfo::isStr<21ul>(char const (&) [21ul]) const
Line
Count
Source
120
71
  bool isStr(const char (&Str)[StrLen]) const {
121
71
    return getLength() == StrLen-1 &&
122
71
           
memcmp(getNameStart(), Str, StrLen-1) == 010
;
123
71
  }
bool clang::IdentifierInfo::isStr<13ul>(char const (&) [13ul]) const
Line
Count
Source
120
1.73k
  bool isStr(const char (&Str)[StrLen]) const {
121
1.73k
    return getLength() == StrLen-1 &&
122
1.73k
           
memcmp(getNameStart(), Str, StrLen-1) == 055
;
123
1.73k
  }
bool clang::IdentifierInfo::isStr<16ul>(char const (&) [16ul]) const
Line
Count
Source
120
9.48k
  bool isStr(const char (&Str)[StrLen]) const {
121
9.48k
    return getLength() == StrLen-1 &&
122
9.48k
           
memcmp(getNameStart(), Str, StrLen-1) == 0897
;
123
9.48k
  }
bool clang::IdentifierInfo::isStr<6ul>(char const (&) [6ul]) const
Line
Count
Source
120
1.31M
  bool isStr(const char (&Str)[StrLen]) const {
121
1.31M
    return getLength() == StrLen-1 &&
122
1.31M
           
memcmp(getNameStart(), Str, StrLen-1) == 065.1k
;
123
1.31M
  }
bool clang::IdentifierInfo::isStr<12ul>(char const (&) [12ul]) const
Line
Count
Source
120
1.31M
  bool isStr(const char (&Str)[StrLen]) const {
121
1.31M
    return getLength() == StrLen-1 &&
122
1.31M
           
memcmp(getNameStart(), Str, StrLen-1) == 07.99k
;
123
1.31M
  }
bool clang::IdentifierInfo::isStr<3ul>(char const (&) [3ul]) const
Line
Count
Source
120
660
  bool isStr(const char (&Str)[StrLen]) const {
121
660
    return getLength() == StrLen-1 &&
122
660
           
memcmp(getNameStart(), Str, StrLen-1) == 0260
;
123
660
  }
bool clang::IdentifierInfo::isStr<29ul>(char const (&) [29ul]) const
Line
Count
Source
120
3.06M
  bool isStr(const char (&Str)[StrLen]) const {
121
3.06M
    return getLength() == StrLen-1 &&
122
3.06M
           
memcmp(getNameStart(), Str, StrLen-1) == 028.9k
;
123
3.06M
  }
bool clang::IdentifierInfo::isStr<2ul>(char const (&) [2ul]) const
Line
Count
Source
120
15.5k
  bool isStr(const char (&Str)[StrLen]) const {
121
15.5k
    return getLength() == StrLen-1 &&
122
15.5k
           
memcmp(getNameStart(), Str, StrLen-1) == 056
;
123
15.5k
  }
124
125
  /// Return true if this is the identifier for the specified StringRef.
126
1.11k
  bool isStr(llvm::StringRef Str) const {
127
1.11k
    llvm::StringRef ThisStr(getNameStart(), getLength());
128
1.11k
    return ThisStr == Str;
129
1.11k
  }
130
131
  /// Return the beginning of the actual null-terminated string for this
132
  /// identifier.
133
714M
  const char *getNameStart() const {
134
714M
    if (Entry) 
return Entry->getKeyData()714M
;
135
15
    // FIXME: This is gross. It would be best not to embed specific details
136
15
    // of the PTH file format here.
137
15
    // The 'this' pointer really points to a
138
15
    // std::pair<IdentifierInfo, const char*>, where internal pointer
139
15
    // points to the external string data.
140
15
    using actualtype = std::pair<IdentifierInfo, const char *>;
141
15
142
15
    return ((const actualtype*) this)->second;
143
15
  }
144
145
  /// Efficiently return the length of this identifier info.
146
761M
  unsigned getLength() const {
147
761M
    if (Entry) 
return Entry->getKeyLength()761M
;
148
9
    // FIXME: This is gross. It would be best not to embed specific details
149
9
    // of the PTH file format here.
150
9
    // The 'this' pointer really points to a
151
9
    // std::pair<IdentifierInfo, const char*>, where internal pointer
152
9
    // points to the external string data.
153
9
    using actualtype = std::pair<IdentifierInfo, const char *>;
154
9
155
9
    const char* p = ((const actualtype*) this)->second - 2;
156
9
    return (((unsigned) p[0]) | (((unsigned) p[1]) << 8)) - 1;
157
9
  }
158
159
  /// Return the actual identifier string.
160
642M
  StringRef getName() const {
161
642M
    return StringRef(getNameStart(), getLength());
162
642M
  }
163
164
  /// Return true if this identifier is \#defined to some other value.
165
  /// \note The current definition may be in a module and not currently visible.
166
196M
  bool hasMacroDefinition() const {
167
196M
    return HasMacro;
168
196M
  }
169
24.4M
  void setHasMacroDefinition(bool Val) {
170
24.4M
    if (HasMacro == Val) 
return572k
;
171
23.9M
172
23.9M
    HasMacro = Val;
173
23.9M
    if (Val) {
174
23.7M
      NeedsHandleIdentifier = true;
175
23.7M
      HadMacro = true;
176
23.7M
    } else {
177
127k
      RecomputeNeedsHandleIdentifier();
178
127k
    }
179
23.9M
  }
180
  /// Returns true if this identifier was \#defined to some value at any
181
  /// moment. In this case there should be an entry for the identifier in the
182
  /// macro history table in Preprocessor.
183
12.6M
  bool hadMacroDefinition() const {
184
12.6M
    return HadMacro;
185
12.6M
  }
186
187
  /// If this is a source-language token (e.g. 'for'), this API
188
  /// can be used to cause the lexer to map identifiers to source-language
189
  /// tokens.
190
253M
  tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; }
191
192
  /// True if revertTokenIDToIdentifier() was called.
193
13.4M
  bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; }
194
195
  /// Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2
196
  /// compatibility.
197
  ///
198
  /// TokenID is normally read-only but there are 2 instances where we revert it
199
  /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens
200
  /// using this method so we can inform serialization about it.
201
161
  void revertTokenIDToIdentifier() {
202
161
    assert(TokenID != tok::identifier && "Already at tok::identifier");
203
161
    TokenID = tok::identifier;
204
161
    RevertedTokenID = true;
205
161
  }
206
2
  void revertIdentifierToTokenID(tok::TokenKind TK) {
207
2
    assert(TokenID == tok::identifier && "Should be at tok::identifier");
208
2
    TokenID = TK;
209
2
    RevertedTokenID = false;
210
2
  }
211
212
  /// Return the preprocessor keyword ID for this identifier.
213
  ///
214
  /// For example, "define" will return tok::pp_define.
215
  tok::PPKeywordKind getPPKeywordID() const;
216
217
  /// Return the Objective-C keyword ID for the this identifier.
218
  ///
219
  /// For example, 'class' will return tok::objc_class if ObjC is enabled.
220
391k
  tok::ObjCKeywordKind getObjCKeywordID() const {
221
391k
    if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS)
222
391k
      return tok::ObjCKeywordKind(ObjCOrBuiltinID);
223
6
    else
224
6
      return tok::objc_not_keyword;
225
391k
  }
226
912k
  void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; }
227
228
  /// True if setNotBuiltin() was called.
229
10.3M
  bool hasRevertedBuiltin() const {
230
10.3M
    return ObjCOrBuiltinID == tok::NUM_OBJC_KEYWORDS;
231
10.3M
  }
232
233
  /// Revert the identifier to a non-builtin identifier. We do this if
234
  /// the name of a known builtin library function is used to declare that
235
  /// function, but an unexpected type is specified.
236
779
  void revertBuiltin() {
237
779
    setBuiltinID(0);
238
779
  }
239
240
  /// Return a value indicating whether this is a builtin function.
241
  ///
242
  /// 0 is not-built-in. 1+ are specific builtin functions.
243
118M
  unsigned getBuiltinID() const {
244
118M
    if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
245
14.2M
      return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
246
104M
    else
247
104M
      return 0;
248
118M
  }
249
90.5M
  void setBuiltinID(unsigned ID) {
250
90.5M
    ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS;
251
90.5M
    assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID
252
90.5M
           && "ID too large for field!");
253
90.5M
  }
254
255
20.9M
  unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; }
256
365k
  void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; }
257
258
  /// get/setExtension - Initialize information about whether or not this
259
  /// language token is an extension.  This controls extension warnings, and is
260
  /// only valid if a custom token ID is set.
261
40.9M
  bool isExtensionToken() const { return IsExtension; }
262
14.8M
  void setIsExtensionToken(bool Val) {
263
14.8M
    IsExtension = Val;
264
14.8M
    if (Val)
265
1.44M
      NeedsHandleIdentifier = true;
266
13.3M
    else
267
13.3M
      RecomputeNeedsHandleIdentifier();
268
14.8M
  }
269
270
  /// is/setIsFutureCompatKeyword - Initialize information about whether or not
271
  /// this language token is a keyword in a newer or proposed Standard. This
272
  /// controls compatibility warnings, and is only true when not parsing the
273
  /// corresponding Standard. Once a compatibility problem has been diagnosed
274
  /// with this keyword, the flag will be cleared.
275
34.1M
  bool isFutureCompatKeyword() const { return IsFutureCompatKeyword; }
276
14.8M
  void setIsFutureCompatKeyword(bool Val) {
277
14.8M
    IsFutureCompatKeyword = Val;
278
14.8M
    if (Val)
279
73.7k
      NeedsHandleIdentifier = true;
280
14.7M
    else
281
14.7M
      RecomputeNeedsHandleIdentifier();
282
14.8M
  }
283
284
  /// setIsPoisoned - Mark this identifier as poisoned.  After poisoning, the
285
  /// Preprocessor will emit an error every time this token is used.
286
23.3M
  void setIsPoisoned(bool Value = true) {
287
23.3M
    IsPoisoned = Value;
288
23.3M
    if (Value)
289
23.2M
      NeedsHandleIdentifier = true;
290
66.8k
    else
291
66.8k
      RecomputeNeedsHandleIdentifier();
292
23.3M
  }
293
294
  /// Return true if this token has been poisoned.
295
123M
  bool isPoisoned() const { return IsPoisoned; }
296
297
  /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether
298
  /// this identifier is a C++ alternate representation of an operator.
299
541k
  void setIsCPlusPlusOperatorKeyword(bool Val = true) {
300
541k
    IsCPPOperatorKeyword = Val;
301
541k
  }
302
33.5M
  bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; }
303
304
  /// Return true if this token is a keyword in the specified language.
305
  bool isKeyword(const LangOptions &LangOpts) const;
306
307
  /// Return true if this token is a C++ keyword in the specified
308
  /// language.
309
  bool isCPlusPlusKeyword(const LangOptions &LangOpts) const;
310
311
  /// getFETokenInfo/setFETokenInfo - The language front-end is allowed to
312
  /// associate arbitrary metadata with this token.
313
  template<typename T>
314
172M
  T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }
315
46.9M
  void setFETokenInfo(void *T) { FETokenInfo = T; }
316
317
  /// Return true if the Preprocessor::HandleIdentifier must be called
318
  /// on a token of this identifier.
319
  ///
320
  /// If this returns false, we know that HandleIdentifier will not affect
321
  /// the token.
322
241M
  bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
323
324
  /// Return true if the identifier in its current state was loaded
325
  /// from an AST file.
326
78.2M
  bool isFromAST() const { return IsFromAST; }
327
328
425k
  void setIsFromAST() { IsFromAST = true; }
329
330
  /// Determine whether this identifier has changed since it was loaded
331
  /// from an AST file.
332
35.4k
  bool hasChangedSinceDeserialization() const {
333
35.4k
    return ChangedAfterLoad;
334
35.4k
  }
335
336
  /// Note that this identifier has changed since it was loaded from
337
  /// an AST file.
338
11.6k
  void setChangedSinceDeserialization() {
339
11.6k
    ChangedAfterLoad = true;
340
11.6k
  }
341
342
  /// Determine whether the frontend token information for this
343
  /// identifier has changed since it was loaded from an AST file.
344
11.4k
  bool hasFETokenInfoChangedSinceDeserialization() const {
345
11.4k
    return FEChangedAfterLoad;
346
11.4k
  }
347
348
  /// Note that the frontend token information for this identifier has
349
  /// changed since it was loaded from an AST file.
350
11.1k
  void setFETokenInfoChangedSinceDeserialization() {
351
11.1k
    FEChangedAfterLoad = true;
352
11.1k
  }
353
354
  /// Determine whether the information for this identifier is out of
355
  /// date with respect to the external source.
356
289M
  bool isOutOfDate() const { return OutOfDate; }
357
358
  /// Set whether the information for this identifier is out of
359
  /// date with respect to the external source.
360
4.47M
  void setOutOfDate(bool OOD) {
361
4.47M
    OutOfDate = OOD;
362
4.47M
    if (OOD)
363
2.96M
      NeedsHandleIdentifier = true;
364
1.51M
    else
365
1.51M
      RecomputeNeedsHandleIdentifier();
366
4.47M
  }
367
368
  /// Determine whether this is the contextual keyword \c import.
369
28.3M
  bool isModulesImport() const { return IsModulesImport; }
370
371
  /// Set whether this identifier is the contextual keyword \c import.
372
71.0k
  void setModulesImport(bool I) {
373
71.0k
    IsModulesImport = I;
374
71.0k
    if (I)
375
71.0k
      NeedsHandleIdentifier = true;
376
0
    else
377
0
      RecomputeNeedsHandleIdentifier();
378
71.0k
  }
379
380
  /// Return true if this identifier is an editor placeholder.
381
  ///
382
  /// Editor placeholders are produced by the code-completion engine and are
383
  /// represented as characters between '<#' and '#>' in the source code. An
384
  /// example of auto-completed call with a placeholder parameter is shown
385
  /// below:
386
  /// \code
387
  ///   function(<#int x#>);
388
  /// \endcode
389
16.4M
  bool isEditorPlaceholder() const {
390
16.4M
    return getName().startswith("<#") && 
getName().endswith("#>")25
;
391
16.4M
  }
392
393
  /// Provide less than operator for lexicographical sorting.
394
273M
  bool operator<(const IdentifierInfo &RHS) const {
395
273M
    return getName() < RHS.getName();
396
273M
  }
397
398
private:
399
  /// The Preprocessor::HandleIdentifier does several special (but rare)
400
  /// things to identifiers of various sorts.  For example, it changes the
401
  /// \c for keyword token from tok::identifier to tok::for.
402
  ///
403
  /// This method is very tied to the definition of HandleIdentifier.  Any
404
  /// change to it should be reflected here.
405
29.8M
  void RecomputeNeedsHandleIdentifier() {
406
29.8M
    NeedsHandleIdentifier = isPoisoned() || 
hasMacroDefinition()29.8M
||
407
29.8M
                            
isExtensionToken()29.7M
||
isFutureCompatKeyword()28.3M
||
408
29.8M
                            
isOutOfDate()28.3M
||
isModulesImport()28.3M
;
409
29.8M
  }
410
};
411
412
/// An RAII object for [un]poisoning an identifier within a scope.
413
///
414
/// \p II is allowed to be null, in which case objects of this type have
415
/// no effect.
416
class PoisonIdentifierRAIIObject {
417
  IdentifierInfo *const II;
418
  const bool OldValue;
419
420
public:
421
  PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
422
17.5M
    : II(II), OldValue(II ? II->isPoisoned() : false) {
423
17.5M
    if(II)
424
432
      II->setIsPoisoned(NewValue);
425
17.5M
  }
426
427
17.5M
  ~PoisonIdentifierRAIIObject() {
428
17.5M
    if(II)
429
432
      II->setIsPoisoned(OldValue);
430
17.5M
  }
431
};
432
433
/// An iterator that walks over all of the known identifiers
434
/// in the lookup table.
435
///
436
/// Since this iterator uses an abstract interface via virtual
437
/// functions, it uses an object-oriented interface rather than the
438
/// more standard C++ STL iterator interface. In this OO-style
439
/// iteration, the single function \c Next() provides dereference,
440
/// advance, and end-of-sequence checking in a single
441
/// operation. Subclasses of this iterator type will provide the
442
/// actual functionality.
443
class IdentifierIterator {
444
protected:
445
338
  IdentifierIterator() = default;
446
447
public:
448
  IdentifierIterator(const IdentifierIterator &) = delete;
449
  IdentifierIterator &operator=(const IdentifierIterator &) = delete;
450
451
  virtual ~IdentifierIterator();
452
453
  /// Retrieve the next string in the identifier table and
454
  /// advances the iterator for the following string.
455
  ///
456
  /// \returns The next string in the identifier table. If there is
457
  /// no such string, returns an empty \c StringRef.
458
  virtual StringRef Next() = 0;
459
};
460
461
/// Provides lookups to, and iteration over, IdentiferInfo objects.
462
class IdentifierInfoLookup {
463
public:
464
  virtual ~IdentifierInfoLookup();
465
466
  /// Return the IdentifierInfo for the specified named identifier.
467
  ///
468
  /// Unlike the version in IdentifierTable, this returns a pointer instead
469
  /// of a reference.  If the pointer is null then the IdentifierInfo cannot
470
  /// be found.
471
  virtual IdentifierInfo* get(StringRef Name) = 0;
472
473
  /// Retrieve an iterator into the set of all identifiers
474
  /// known to this identifier lookup source.
475
  ///
476
  /// This routine provides access to all of the identifiers known to
477
  /// the identifier lookup, allowing access to the contents of the
478
  /// identifiers without introducing the overhead of constructing
479
  /// IdentifierInfo objects for each.
480
  ///
481
  /// \returns A new iterator into the set of known identifiers. The
482
  /// caller is responsible for deleting this iterator.
483
  virtual IdentifierIterator *getIdentifiers();
484
};
485
486
/// Implements an efficient mapping from strings to IdentifierInfo nodes.
487
///
488
/// This has no other purpose, but this is an extremely performance-critical
489
/// piece of the code, as each occurrence of every identifier goes through
490
/// here when lexed.
491
class IdentifierTable {
492
  // Shark shows that using MallocAllocator is *much* slower than using this
493
  // BumpPtrAllocator!
494
  using HashTableTy = llvm::StringMap<IdentifierInfo *, llvm::BumpPtrAllocator>;
495
  HashTableTy HashTable;
496
497
  IdentifierInfoLookup* ExternalLookup;
498
499
public:
500
  /// Create the identifier table.
501
  explicit IdentifierTable(IdentifierInfoLookup *ExternalLookup = nullptr);
502
503
  /// Create the identifier table, populating it with info about the
504
  /// language keywords for the language specified by \p LangOpts.
505
  explicit IdentifierTable(const LangOptions &LangOpts,
506
                           IdentifierInfoLookup *ExternalLookup = nullptr);
507
508
  /// Set the external identifier lookup mechanism.
509
5.96k
  void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) {
510
5.96k
    ExternalLookup = IILookup;
511
5.96k
  }
512
513
  /// Retrieve the external identifier lookup object, if any.
514
3.31k
  IdentifierInfoLookup *getExternalIdentifierLookup() const {
515
3.31k
    return ExternalLookup;
516
3.31k
  }
517
518
142M
  llvm::BumpPtrAllocator& getAllocator() {
519
142M
    return HashTable.getAllocator();
520
142M
  }
521
522
  /// Return the identifier token info for the specified named
523
  /// identifier.
524
325M
  IdentifierInfo &get(StringRef Name) {
525
325M
    auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
526
325M
527
325M
    IdentifierInfo *&II = Entry.second;
528
325M
    if (II) 
return *II183M
;
529
142M
530
142M
    // No entry; if we have an external lookup, look there first.
531
142M
    if (ExternalLookup) {
532
593k
      II = ExternalLookup->get(Name);
533
593k
      if (II)
534
404k
        return *II;
535
142M
    }
536
142M
537
142M
    // Lookups failed, make a new IdentifierInfo.
538
142M
    void *Mem = getAllocator().Allocate<IdentifierInfo>();
539
142M
    II = new (Mem) IdentifierInfo();
540
142M
541
142M
    // Make sure getName() knows how to find the IdentifierInfo
542
142M
    // contents.
543
142M
    II->Entry = &Entry;
544
142M
545
142M
    return *II;
546
142M
  }
547
548
15.3M
  IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
549
15.3M
    IdentifierInfo &II = get(Name);
550
15.3M
    II.TokenID = TokenCode;
551
15.3M
    assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large");
552
15.3M
    return II;
553
15.3M
  }
554
555
  /// Gets an IdentifierInfo for the given name without consulting
556
  ///        external sources.
557
  ///
558
  /// This is a version of get() meant for external sources that want to
559
  /// introduce or modify an identifier. If they called get(), they would
560
  /// likely end up in a recursion.
561
419k
  IdentifierInfo &getOwn(StringRef Name) {
562
419k
    auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
563
419k
564
419k
    IdentifierInfo *&II = Entry.second;
565
419k
    if (II)
566
14.1k
      return *II;
567
405k
568
405k
    // Lookups failed, make a new IdentifierInfo.
569
405k
    void *Mem = getAllocator().Allocate<IdentifierInfo>();
570
405k
    II = new (Mem) IdentifierInfo();
571
405k
572
405k
    // Make sure getName() knows how to find the IdentifierInfo
573
405k
    // contents.
574
405k
    II->Entry = &Entry;
575
405k
576
405k
    // If this is the 'import' contextual keyword, mark it as such.
577
405k
    if (Name.equals("import"))
578
0
      II->setModulesImport(true);
579
405k
580
405k
    return *II;
581
405k
  }
582
583
  using iterator = HashTableTy::const_iterator;
584
  using const_iterator = HashTableTy::const_iterator;
585
586
16.1k
  iterator begin() const { return HashTable.begin(); }
587
16.1k
  iterator end() const   { return HashTable.end(); }
588
0
  unsigned size() const  { return HashTable.size(); }
589
590
  /// Print some statistics to stderr that indicate how well the
591
  /// hashing is doing.
592
  void PrintStats() const;
593
594
  /// Populate the identifier table with info about the language keywords
595
  /// for the language specified by \p LangOpts.
596
  void AddKeywords(const LangOptions &LangOpts);
597
};
598
599
/// A family of Objective-C methods.
600
///
601
/// These families have no inherent meaning in the language, but are
602
/// nonetheless central enough in the existing implementations to
603
/// merit direct AST support.  While, in theory, arbitrary methods can
604
/// be considered to form families, we focus here on the methods
605
/// involving allocation and retain-count management, as these are the
606
/// most "core" and the most likely to be useful to diverse clients
607
/// without extra information.
608
///
609
/// Both selectors and actual method declarations may be classified
610
/// into families.  Method families may impose additional restrictions
611
/// beyond their selector name; for example, a method called '_init'
612
/// that returns void is not considered to be in the 'init' family
613
/// (but would be if it returned 'id').  It is also possible to
614
/// explicitly change or remove a method's family.  Therefore the
615
/// method's family should be considered the single source of truth.
616
enum ObjCMethodFamily {
617
  /// No particular method family.
618
  OMF_None,
619
620
  // Selectors in these families may have arbitrary arity, may be
621
  // written with arbitrary leading underscores, and may have
622
  // additional CamelCase "words" in their first selector chunk
623
  // following the family name.
624
  OMF_alloc,
625
  OMF_copy,
626
  OMF_init,
627
  OMF_mutableCopy,
628
  OMF_new,
629
630
  // These families are singletons consisting only of the nullary
631
  // selector with the given name.
632
  OMF_autorelease,
633
  OMF_dealloc,
634
  OMF_finalize,
635
  OMF_release,
636
  OMF_retain,
637
  OMF_retainCount,
638
  OMF_self,
639
  OMF_initialize,
640
641
  // performSelector families
642
  OMF_performSelector
643
};
644
645
/// Enough bits to store any enumerator in ObjCMethodFamily or
646
/// InvalidObjCMethodFamily.
647
enum { ObjCMethodFamilyBitWidth = 4 };
648
649
/// An invalid value of ObjCMethodFamily.
650
enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 };
651
652
/// A family of Objective-C methods.
653
///
654
/// These are family of methods whose result type is initially 'id', but
655
/// but are candidate for the result type to be changed to 'instancetype'.
656
enum ObjCInstanceTypeFamily {
657
  OIT_None,
658
  OIT_Array,
659
  OIT_Dictionary,
660
  OIT_Singleton,
661
  OIT_Init,
662
  OIT_ReturnsSelf
663
};
664
665
enum ObjCStringFormatFamily {
666
  SFF_None,
667
  SFF_NSString,
668
  SFF_CFString
669
};
670
671
/// Smart pointer class that efficiently represents Objective-C method
672
/// names.
673
///
674
/// This class will either point to an IdentifierInfo or a
675
/// MultiKeywordSelector (which is private). This enables us to optimize
676
/// selectors that take no arguments and selectors that take 1 argument, which
677
/// accounts for 78% of all selectors in Cocoa.h.
678
0
class Selector {
Unexecuted instantiation: clang::Selector::operator=(clang::Selector const&)
Unexecuted instantiation: clang::Selector::operator=(clang::Selector&&)
679
  friend class Diagnostic;
680
681
  enum IdentifierInfoFlag {
682
    // Empty selector = 0.
683
    ZeroArg  = 0x1,
684
    OneArg   = 0x2,
685
    MultiArg = 0x3,
686
    ArgFlags = ZeroArg|OneArg
687
  };
688
689
  // a pointer to the MultiKeywordSelector or IdentifierInfo.
690
  uintptr_t InfoPtr = 0;
691
692
906k
  Selector(IdentifierInfo *II, unsigned nArgs) {
693
906k
    InfoPtr = reinterpret_cast<uintptr_t>(II);
694
906k
    assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
695
906k
    assert(nArgs < 2 && "nArgs not equal to 0/1");
696
906k
    InfoPtr |= nArgs+1;
697
906k
  }
698
699
1.61M
  Selector(MultiKeywordSelector *SI) {
700
1.61M
    InfoPtr = reinterpret_cast<uintptr_t>(SI);
701
1.61M
    assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
702
1.61M
    InfoPtr |= MultiArg;
703
1.61M
  }
704
705
130k
  IdentifierInfo *getAsIdentifierInfo() const {
706
130k
    if (getIdentifierInfoFlag() < MultiArg)
707
130k
      return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
708
0
    return nullptr;
709
0
  }
710
711
126k
  MultiKeywordSelector *getMultiKeywordSelector() const {
712
126k
    return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags);
713
126k
  }
714
715
601k
  unsigned getIdentifierInfoFlag() const {
716
601k
    return InfoPtr & ArgFlags;
717
601k
  }
718
719
  static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
720
721
  static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel);
722
723
public:
724
  friend class SelectorTable; // only the SelectorTable can create these
725
  friend class DeclarationName; // and the AST's DeclarationName.
726
727
  /// The default ctor should only be used when creating data structures that
728
  ///  will contain selectors.
729
2.07M
  Selector() = default;
730
1.98M
  Selector(uintptr_t V) : InfoPtr(V) {}
731
732
  /// operator==/!= - Indicate whether the specified selectors are identical.
733
4.33M
  bool operator==(Selector RHS) const {
734
4.33M
    return InfoPtr == RHS.InfoPtr;
735
4.33M
  }
736
7.14k
  bool operator!=(Selector RHS) const {
737
7.14k
    return InfoPtr != RHS.InfoPtr;
738
7.14k
  }
739
740
461k
  void *getAsOpaquePtr() const {
741
461k
    return reinterpret_cast<void*>(InfoPtr);
742
461k
  }
743
744
  /// Determine whether this is the empty selector.
745
28.9k
  bool isNull() const { return InfoPtr == 0; }
746
747
  // Predicates to identify the selector type.
748
556
  bool isKeywordSelector() const {
749
556
    return getIdentifierInfoFlag() != ZeroArg;
750
556
  }
751
752
45.5k
  bool isUnarySelector() const {
753
45.5k
    return getIdentifierInfoFlag() == ZeroArg;
754
45.5k
  }
755
756
  unsigned getNumArgs() const;
757
758
  /// Retrieve the identifier at a given position in the selector.
759
  ///
760
  /// Note that the identifier pointer returned may be NULL. Clients that only
761
  /// care about the text of the identifier string, and not the specific,
762
  /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
763
  /// an empty string when the identifier pointer would be NULL.
764
  ///
765
  /// \param argIndex The index for which we want to retrieve the identifier.
766
  /// This index shall be less than \c getNumArgs() unless this is a keyword
767
  /// selector, in which case 0 is the only permissible value.
768
  ///
769
  /// \returns the uniqued identifier for this slot, or NULL if this slot has
770
  /// no corresponding identifier.
771
  IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
772
773
  /// Retrieve the name at a given position in the selector.
774
  ///
775
  /// \param argIndex The index for which we want to retrieve the name.
776
  /// This index shall be less than \c getNumArgs() unless this is a keyword
777
  /// selector, in which case 0 is the only permissible value.
778
  ///
779
  /// \returns the name for this slot, which may be the empty string if no
780
  /// name was supplied.
781
  StringRef getNameForSlot(unsigned argIndex) const;
782
783
  /// Derive the full selector name (e.g. "foo:bar:") and return
784
  /// it as an std::string.
785
  std::string getAsString() const;
786
787
  /// Prints the full selector name (e.g. "foo:bar:").
788
  void print(llvm::raw_ostream &OS) const;
789
790
  void dump() const;
791
792
  /// Derive the conventional family of this method.
793
41.3k
  ObjCMethodFamily getMethodFamily() const {
794
41.3k
    return getMethodFamilyImpl(*this);
795
41.3k
  }
796
797
12.7k
  ObjCStringFormatFamily getStringFormatFamily() const {
798
12.7k
    return getStringFormatFamilyImpl(*this);
799
12.7k
  }
800
801
600k
  static Selector getEmptyMarker() {
802
600k
    return Selector(uintptr_t(-1));
803
600k
  }
804
805
447k
  static Selector getTombstoneMarker() {
806
447k
    return Selector(uintptr_t(-2));
807
447k
  }
808
809
  static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel);
810
};
811
812
/// This table allows us to fully hide how we implement
813
/// multi-keyword caching.
814
class SelectorTable {
815
  // Actually a SelectorTableImpl
816
  void *Impl;
817
818
public:
819
  SelectorTable();
820
  SelectorTable(const SelectorTable &) = delete;
821
  SelectorTable &operator=(const SelectorTable &) = delete;
822
  ~SelectorTable();
823
824
  /// Can create any sort of selector.
825
  ///
826
  /// \p NumArgs indicates whether this is a no argument selector "foo", a
827
  /// single argument selector "foo:" or multi-argument "foo:bar:".
828
  Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
829
830
25.4k
  Selector getUnarySelector(IdentifierInfo *ID) {
831
25.4k
    return Selector(ID, 1);
832
25.4k
  }
833
834
43.9k
  Selector getNullarySelector(IdentifierInfo *ID) {
835
43.9k
    return Selector(ID, 0);
836
43.9k
  }
837
838
  /// Return the total amount of memory allocated for managing selectors.
839
  size_t getTotalMemory() const;
840
841
  /// Return the default setter name for the given identifier.
842
  ///
843
  /// This is "set" + \p Name where the initial character of \p Name
844
  /// has been capitalized.
845
  static SmallString<64> constructSetterName(StringRef Name);
846
847
  /// Return the default setter selector for the given identifier.
848
  ///
849
  /// This is "set" + \p Name where the initial character of \p Name
850
  /// has been capitalized.
851
  static Selector constructSetterSelector(IdentifierTable &Idents,
852
                                          SelectorTable &SelTable,
853
                                          const IdentifierInfo *Name);
854
855
  /// Return the property name for the given setter selector.
856
  static std::string getPropertyNameFromSetterSelector(Selector Sel);
857
};
858
859
/// DeclarationNameExtra - Common base of the MultiKeywordSelector,
860
/// CXXSpecialName, and CXXOperatorIdName classes, all of which are
861
/// private classes that describe different kinds of names.
862
class DeclarationNameExtra {
863
public:
864
  /// ExtraKind - The kind of "extra" information stored in the
865
  /// DeclarationName. See @c ExtraKindOrNumArgs for an explanation of
866
  /// how these enumerator values are used.
867
  enum ExtraKind {
868
    CXXConstructor = 0,
869
    CXXDestructor,
870
    CXXConversionFunction,
871
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
872
    CXXOperator##Name,
873
#include "clang/Basic/OperatorKinds.def"
874
    CXXDeductionGuide,
875
    CXXLiteralOperator,
876
    CXXUsingDirective,
877
    NUM_EXTRA_KINDS
878
  };
879
880
  /// ExtraKindOrNumArgs - Either the kind of C++ special name or
881
  /// operator-id (if the value is one of the CXX* enumerators of
882
  /// ExtraKind), in which case the DeclarationNameExtra is also a
883
  /// CXXSpecialName, (for CXXConstructor, CXXDestructor, or
884
  /// CXXConversionFunction) CXXOperatorIdName, or CXXLiteralOperatorName,
885
  /// it may be also name common to C++ using-directives (CXXUsingDirective),
886
  /// otherwise it is NUM_EXTRA_KINDS+NumArgs, where NumArgs is the number of
887
  /// arguments in the Objective-C selector, in which case the
888
  /// DeclarationNameExtra is also a MultiKeywordSelector.
889
  unsigned ExtraKindOrNumArgs;
890
};
891
892
}  // namespace clang
893
894
namespace llvm {
895
896
/// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
897
/// DenseSets.
898
template <>
899
struct DenseMapInfo<clang::Selector> {
900
600k
  static clang::Selector getEmptyKey() {
901
600k
    return clang::Selector::getEmptyMarker();
902
600k
  }
903
904
447k
  static clang::Selector getTombstoneKey() {
905
447k
    return clang::Selector::getTombstoneMarker();
906
447k
  }
907
908
  static unsigned getHashValue(clang::Selector S);
909
910
4.26M
  static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
911
4.26M
    return LHS == RHS;
912
4.26M
  }
913
};
914
915
template <>
916
struct isPodLike<clang::Selector> { static const bool value = true; };
917
918
template<>
919
struct PointerLikeTypeTraits<clang::Selector> {
920
41.9k
  static const void *getAsVoidPointer(clang::Selector P) {
921
41.9k
    return P.getAsOpaquePtr();
922
41.9k
  }
923
924
  static clang::Selector getFromVoidPointer(const void *P) {
925
    return clang::Selector(reinterpret_cast<uintptr_t>(P));
926
  }
927
928
  enum { NumLowBitsAvailable = 0 };
929
};
930
931
// Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
932
// are not guaranteed to be 8-byte aligned.
933
template<>
934
struct PointerLikeTypeTraits<clang::IdentifierInfo*> {
935
1.48k
  static void *getAsVoidPointer(clang::IdentifierInfo* P) {
936
1.48k
    return P;
937
1.48k
  }
938
939
18
  static clang::IdentifierInfo *getFromVoidPointer(void *P) {
940
18
    return static_cast<clang::IdentifierInfo*>(P);
941
18
  }
942
943
  enum { NumLowBitsAvailable = 1 };
944
};
945
946
template<>
947
struct PointerLikeTypeTraits<const clang::IdentifierInfo*> {
948
37.5k
  static const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
949
37.5k
    return P;
950
37.5k
  }
951
952
  static const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
953
    return static_cast<const clang::IdentifierInfo*>(P);
954
  }
955
956
  enum { NumLowBitsAvailable = 1 };
957
};
958
959
} // namespace llvm
960
961
#endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H