Coverage Report

Created: 2018-07-22 10:17

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