blob: 54f449fe3ea2a80fa8c3c7a5829af61a2d6bc086 [file] [log] [blame]
Eugene Zelenko918e0ca2017-11-03 22:35:271//===- IdentifierTable.cpp - Hash table for identifier lookup -------------===//
Chris Lattner22eb9722006-06-18 05:43:122//
Chandler Carruth2946cd72019-01-19 08:50:563// 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
Chris Lattner22eb9722006-06-18 05:43:126//
7//===----------------------------------------------------------------------===//
8//
Chris Lattnerc79f6fb2006-07-04 17:53:219// This file implements the IdentifierInfo, IdentifierVisitor, and
Chris Lattner91cbf112006-07-03 04:28:5210// IdentifierTable interfaces.
Chris Lattner22eb9722006-06-18 05:43:1211//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramerf3ca26982014-05-10 16:31:5514#include "clang/Basic/IdentifierTable.h"
Eugene Zelenko918e0ca2017-11-03 22:35:2715#include "clang/Basic/CharInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:4616#include "clang/Basic/LangOptions.h"
Benjamin Kramerf3ca26982014-05-10 16:31:5517#include "clang/Basic/OperatorKinds.h"
Douglas Gregor813a0662015-06-19 18:14:3818#include "clang/Basic/Specifiers.h"
Eugene Zelenko918e0ca2017-11-03 22:35:2719#include "clang/Basic/TokenKinds.h"
20#include "llvm/ADT/DenseMapInfo.h"
Chandler Carruth3a022472012-12-04 09:13:3321#include "llvm/ADT/FoldingSet.h"
Benjamin Kramerf3ca26982014-05-10 16:31:5522#include "llvm/ADT/SmallString.h"
Eugene Zelenko918e0ca2017-11-03 22:35:2723#include "llvm/ADT/StringMap.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/Support/Allocator.h"
David Blaikie8a40f702012-01-17 06:56:2226#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:3327#include "llvm/Support/raw_ostream.h"
Eugene Zelenko918e0ca2017-11-03 22:35:2728#include <cassert>
Chris Lattnerc25d8a72009-03-02 22:20:0429#include <cstdio>
Eugene Zelenko918e0ca2017-11-03 22:35:2730#include <cstring>
31#include <string>
Ted Kremenekf25f4a32007-10-23 22:18:3732
Chris Lattner22eb9722006-06-18 05:43:1233using namespace clang;
34
35//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:1236// IdentifierTable Implementation
37//===----------------------------------------------------------------------===//
38
Eugene Zelenko918e0ca2017-11-03 22:35:2739IdentifierIterator::~IdentifierIterator() = default;
Douglas Gregor57756ea2010-10-14 22:11:0340
Eugene Zelenko918e0ca2017-11-03 22:35:2741IdentifierInfoLookup::~IdentifierInfoLookup() = default;
Ted Kremeneka705b042009-01-15 18:47:4642
Douglas Gregor57756ea2010-10-14 22:11:0343namespace {
Eugene Zelenko918e0ca2017-11-03 22:35:2744
Adrian Prantl9fc8faf2018-05-09 01:00:0145/// A simple identifier lookup iterator that represents an
Eugene Zelenko918e0ca2017-11-03 22:35:2746/// empty sequence of identifiers.
47class EmptyLookupIterator : public IdentifierIterator
48{
49public:
50 StringRef Next() override { return StringRef(); }
51};
52
53} // namespace
Douglas Gregor57756ea2010-10-14 22:11:0354
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:5555IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
Douglas Gregor57756ea2010-10-14 22:11:0356 return new EmptyLookupIterator();
57}
58
Aaron Ballmand742dc22018-04-16 21:07:0859IdentifierTable::IdentifierTable(IdentifierInfoLookup *ExternalLookup)
60 : HashTable(8192), // Start with space for 8K identifiers.
61 ExternalLookup(ExternalLookup) {}
62
Ted Kremeneka705b042009-01-15 18:47:4663IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
Aaron Ballmand742dc22018-04-16 21:07:0864 IdentifierInfoLookup *ExternalLookup)
65 : IdentifierTable(ExternalLookup) {
Chris Lattnerf2e3ac32006-10-27 03:59:1066 // Populate the identifier table with info about keywords for the current
67 // language.
Chris Lattner25e0d542006-10-18 06:07:0568 AddKeywords(LangOpts);
Chris Lattner91cbf112006-07-03 04:28:5269}
Chris Lattner22eb9722006-06-18 05:43:1270
Chris Lattner25e0d542006-10-18 06:07:0571//===----------------------------------------------------------------------===//
72// Language Keyword Implementation
73//===----------------------------------------------------------------------===//
74
Eli Friedman2b680b42009-04-28 03:13:5475// Constants for TokenKinds.def
76namespace {
Eugene Zelenko918e0ca2017-11-03 22:35:2777
Eli Friedman2b680b42009-04-28 03:13:5478 enum {
Erik Pilkingtona7641d92018-10-30 20:51:2879 KEYC99 = 0x1,
80 KEYCXX = 0x2,
81 KEYCXX11 = 0x4,
82 KEYGNU = 0x8,
83 KEYMS = 0x10,
84 BOOLSUPPORT = 0x20,
85 KEYALTIVEC = 0x40,
86 KEYNOCXX = 0x80,
87 KEYBORLAND = 0x100,
88 KEYOPENCLC = 0x200,
89 KEYC11 = 0x400,
90 KEYNOMS18 = 0x800,
91 KEYNOOPENCL = 0x1000,
92 WCHARSUPPORT = 0x2000,
93 HALFSUPPORT = 0x4000,
94 CHAR8SUPPORT = 0x8000,
95 KEYCONCEPTS = 0x10000,
96 KEYOBJC = 0x20000,
97 KEYZVECTOR = 0x40000,
98 KEYCOROUTINES = 0x80000,
99 KEYMODULES = 0x100000,
100 KEYCXX2A = 0x200000,
101 KEYOPENCLCXX = 0x400000,
Reid Klecknerc1648f22019-02-15 19:59:45102 KEYMSCOMPAT = 0x800000,
Richard Smith6c74e322017-08-13 21:32:33103 KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX2A,
Sven van Haastregt2ca6ba12018-05-09 13:16:17104 KEYALL = (0xffffff & ~KEYNOMS18 &
Anastasia Stulovab1152f12015-03-18 12:55:29105 ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
Eli Friedman2b680b42009-04-28 03:13:54106 };
Serge Pavlov77af3812014-10-29 10:59:18107
Adrian Prantl9fc8faf2018-05-09 01:00:01108 /// How a keyword is treated in the selected standard.
Serge Pavlov77af3812014-10-29 10:59:18109 enum KeywordStatus {
110 KS_Disabled, // Disabled
111 KS_Extension, // Is an extension
112 KS_Enabled, // Enabled
113 KS_Future // Is a keyword in future standard
114 };
Eugene Zelenko918e0ca2017-11-03 22:35:27115
116} // namespace
Serge Pavlov77af3812014-10-29 10:59:18117
Adrian Prantl9fc8faf2018-05-09 01:00:01118/// Translates flags as specified in TokenKinds.def into keyword status
Serge Pavlov77af3812014-10-29 10:59:18119/// in the given language standard.
Serge Pavlov83cf0782014-12-11 12:18:08120static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
Serge Pavlov77af3812014-10-29 10:59:18121 unsigned Flags) {
122 if (Flags == KEYALL) return KS_Enabled;
123 if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled;
124 if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled;
Richard Smith6c74e322017-08-13 21:32:33125 if (LangOpts.CPlusPlus2a && (Flags & KEYCXX2A)) return KS_Enabled;
Serge Pavlov77af3812014-10-29 10:59:18126 if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled;
127 if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension;
128 if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension;
Reid Klecknerc1648f22019-02-15 19:59:45129 if (LangOpts.MSVCCompat && (Flags & KEYMSCOMPAT)) return KS_Enabled;
Serge Pavlov77af3812014-10-29 10:59:18130 if (LangOpts.Borland && (Flags & KEYBORLAND)) return KS_Extension;
131 if (LangOpts.Bool && (Flags & BOOLSUPPORT)) return KS_Enabled;
132 if (LangOpts.Half && (Flags & HALFSUPPORT)) return KS_Enabled;
133 if (LangOpts.WChar && (Flags & WCHARSUPPORT)) return KS_Enabled;
Richard Smith3a8244d2018-05-01 05:02:45134 if (LangOpts.Char8 && (Flags & CHAR8SUPPORT)) return KS_Enabled;
Serge Pavlov77af3812014-10-29 10:59:18135 if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) return KS_Enabled;
Ulrich Weigand29270712018-10-16 14:57:20136 if (LangOpts.ZVector && (Flags & KEYZVECTOR)) return KS_Enabled;
Sven van Haastregt2ca6ba12018-05-09 13:16:17137 if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLC))
138 return KS_Enabled;
139 if (LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLCXX)) return KS_Enabled;
Serge Pavlov77af3812014-10-29 10:59:18140 if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled;
141 if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled;
142 // We treat bridge casts as objective-C keywords so we can warn on them
143 // in non-arc mode.
Erik Pilkingtonfa983902018-10-30 20:31:30144 if (LangOpts.ObjC && (Flags & KEYOBJC)) return KS_Enabled;
Saar Raz67c608a2020-01-23 22:43:22145 if (LangOpts.CPlusPlus2a && (Flags & KEYCONCEPTS)) return KS_Enabled;
Richard Smith10ab78e2019-02-23 21:06:26146 if (LangOpts.Coroutines && (Flags & KEYCOROUTINES)) return KS_Enabled;
Richard Smithc7bf3802016-07-23 02:32:21147 if (LangOpts.ModulesTS && (Flags & KEYMODULES)) return KS_Enabled;
Richard Smith6c74e322017-08-13 21:32:33148 if (LangOpts.CPlusPlus && (Flags & KEYALLCXX)) return KS_Future;
Serge Pavlov77af3812014-10-29 10:59:18149 return KS_Disabled;
Eli Friedman2b680b42009-04-28 03:13:54150}
151
Chris Lattner25e0d542006-10-18 06:07:05152/// AddKeyword - This method is used to associate a token ID with specific
153/// identifiers because they are language keywords. This causes the lexer to
154/// automatically map matching identifiers to specialized token codes.
Chris Lattner0e62c1c2011-07-23 10:55:15155static void AddKeyword(StringRef Keyword,
Eli Friedman2b680b42009-04-28 03:13:54156 tok::TokenKind TokenCode, unsigned Flags,
Chris Lattner25e0d542006-10-18 06:07:05157 const LangOptions &LangOpts, IdentifierTable &Table) {
Serge Pavlov83cf0782014-12-11 12:18:08158 KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
Richard Smith4dd85d62011-10-11 19:57:52159
Alp Tokerbfa39342014-01-14 12:51:41160 // Don't add this keyword under MSVCCompat.
David Majnemer28aae9c2015-03-18 04:15:23161 if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) &&
David Majnemerb710a932015-05-11 03:57:49162 !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
Anastasia Stulovab1152f12015-03-18 12:55:29163 return;
164
165 // Don't add this keyword under OpenCL.
166 if (LangOpts.OpenCL && (Flags & KEYNOOPENCL))
167 return;
168
Eli Friedman2b680b42009-04-28 03:13:54169 // Don't add this keyword if disabled in this language.
Serge Pavlov77af3812014-10-29 10:59:18170 if (AddResult == KS_Disabled) return;
Eli Friedman2b680b42009-04-28 03:13:54171
Richard Smith4dd85d62011-10-11 19:57:52172 IdentifierInfo &Info =
Serge Pavlov77af3812014-10-29 10:59:18173 Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode);
174 Info.setIsExtensionToken(AddResult == KS_Extension);
Richard Smith31d51842015-05-14 04:00:59175 Info.setIsFutureCompatKeyword(AddResult == KS_Future);
Chris Lattner25e0d542006-10-18 06:07:05176}
177
Chris Lattner5b9f4892006-11-21 17:23:33178/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
179/// representations.
Chris Lattner0e62c1c2011-07-23 10:55:15180static void AddCXXOperatorKeyword(StringRef Keyword,
Chris Lattner5b9f4892006-11-21 17:23:33181 tok::TokenKind TokenCode,
182 IdentifierTable &Table) {
Argyrios Kyrtzidis3084a612010-08-11 22:55:12183 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekf25f4a32007-10-23 22:18:37184 Info.setIsCPlusPlusOperatorKeyword();
Chris Lattner5b9f4892006-11-21 17:23:33185}
186
James Dennett0d8a3f82012-06-15 21:27:44187/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
188/// or "property".
Chris Lattner0e62c1c2011-07-23 10:55:15189static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37190 tok::ObjCKeywordKind ObjCID,
Chris Lattner25e0d542006-10-18 06:07:05191 IdentifierTable &Table) {
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37192 Table.get(Name).setObjCKeywordID(ObjCID);
Chris Lattner25e0d542006-10-18 06:07:05193}
194
195/// AddKeywords - Add all keywords to the symbol table.
196///
197void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Chris Lattner25e0d542006-10-18 06:07:05198 // Add keywords and tokens for the current language.
199#define KEYWORD(NAME, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15200 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedman2b680b42009-04-28 03:13:54201 FLAGS, LangOpts, *this);
202#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15203 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedman2b680b42009-04-28 03:13:54204 FLAGS, LangOpts, *this);
Chris Lattner5b9f4892006-11-21 17:23:33205#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
Chris Lattner3e7592e2006-12-04 07:48:37206 if (LangOpts.CXXOperatorNames) \
Chris Lattner0e62c1c2011-07-23 10:55:15207 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Erik Pilkingtonfa983902018-10-30 20:31:30208#define OBJC_AT_KEYWORD(NAME) \
209 if (LangOpts.ObjC) \
Chris Lattner0e62c1c2011-07-23 10:55:15210 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCall39439732011-04-09 22:50:59211#define TESTING_KEYWORD(NAME, FLAGS)
Chris Lattner25e0d542006-10-18 06:07:05212#include "clang/Basic/TokenKinds.def"
John McCall39439732011-04-09 22:50:59213
214 if (LangOpts.ParseUnknownAnytype)
215 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
216 LangOpts, *this);
Aaron Ballman674cf262015-05-26 19:44:52217
Saleem Abdulrasoold170c4b2015-10-04 17:51:05218 if (LangOpts.DeclSpecKeyword)
Aaron Ballman674cf262015-05-26 19:44:52219 AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
Aaron Ballmand742dc22018-04-16 21:07:08220
Richard Smith8af8b862019-04-11 21:18:23221 // Add the 'import' contextual keyword.
Aaron Ballmand742dc22018-04-16 21:07:08222 get("import").setModulesImport(true);
Chris Lattner25e0d542006-10-18 06:07:05223}
224
Adrian Prantl9fc8faf2018-05-09 01:00:01225/// Checks if the specified token kind represents a keyword in the
Serge Pavlov83cf0782014-12-11 12:18:08226/// specified language.
227/// \returns Status of the keyword in the language.
228static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
229 tok::TokenKind K) {
230 switch (K) {
231#define KEYWORD(NAME, FLAGS) \
232 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
233#include "clang/Basic/TokenKinds.def"
234 default: return KS_Disabled;
235 }
236}
237
Adrian Prantl9fc8faf2018-05-09 01:00:01238/// Returns true if the identifier represents a keyword in the
Serge Pavlov83cf0782014-12-11 12:18:08239/// specified language.
Alex Lorenzf1278212017-04-11 15:01:53240bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const {
Serge Pavlov83cf0782014-12-11 12:18:08241 switch (getTokenKwStatus(LangOpts, getTokenID())) {
242 case KS_Enabled:
243 case KS_Extension:
244 return true;
245 default:
246 return false;
247 }
248}
249
Adrian Prantl9fc8faf2018-05-09 01:00:01250/// Returns true if the identifier represents a C++ keyword in the
Alex Lorenzf1278212017-04-11 15:01:53251/// specified language.
252bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
253 if (!LangOpts.CPlusPlus || !isKeyword(LangOpts))
254 return false;
255 // This is a C++ keyword if this identifier is not a keyword when checked
256 // using LangOptions without C++ support.
257 LangOptions LangOptsNoCPP = LangOpts;
258 LangOptsNoCPP.CPlusPlus = false;
259 LangOptsNoCPP.CPlusPlus11 = false;
Richard Smith6c74e322017-08-13 21:32:33260 LangOptsNoCPP.CPlusPlus2a = false;
Alex Lorenzf1278212017-04-11 15:01:53261 return !isKeyword(LangOptsNoCPP);
262}
263
Chris Lattnerff067ce2007-10-07 07:52:34264tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
265 // We use a perfect hash function here involving the length of the keyword,
266 // the first and third character. For preprocessor ID's there are no
267 // collisions (if there were, the switch below would complain about duplicate
268 // case values). Note that this depends on 'if' being null terminated.
Mike Stump11289f42009-09-09 15:08:12269
Chris Lattnerff067ce2007-10-07 07:52:34270#define HASH(LEN, FIRST, THIRD) \
271 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
272#define CASE(LEN, FIRST, THIRD, NAME) \
273 case HASH(LEN, FIRST, THIRD): \
274 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump11289f42009-09-09 15:08:12275
Chris Lattnerff067ce2007-10-07 07:52:34276 unsigned Len = getLength();
Chris Lattnerd2b8ce42007-10-10 20:59:57277 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar1c0761d2009-10-17 18:13:02278 const char *Name = getNameStart();
Chris Lattnerff067ce2007-10-07 07:52:34279 switch (HASH(Len, Name[0], Name[2])) {
280 default: return tok::pp_not_keyword;
281 CASE( 2, 'i', '\0', if);
282 CASE( 4, 'e', 'i', elif);
283 CASE( 4, 'e', 's', else);
284 CASE( 4, 'l', 'n', line);
285 CASE( 4, 's', 'c', sccs);
286 CASE( 5, 'e', 'd', endif);
287 CASE( 5, 'e', 'r', error);
288 CASE( 5, 'i', 'e', ident);
289 CASE( 5, 'i', 'd', ifdef);
290 CASE( 5, 'u', 'd', undef);
291
292 CASE( 6, 'a', 's', assert);
293 CASE( 6, 'd', 'f', define);
294 CASE( 6, 'i', 'n', ifndef);
295 CASE( 6, 'i', 'p', import);
296 CASE( 6, 'p', 'a', pragma);
Fangrui Song6907ce22018-07-30 19:24:48297
Chris Lattnerff067ce2007-10-07 07:52:34298 CASE( 7, 'd', 'f', defined);
299 CASE( 7, 'i', 'c', include);
300 CASE( 7, 'w', 'r', warning);
301
302 CASE( 8, 'u', 'a', unassert);
303 CASE(12, 'i', 'c', include_next);
Mike Stump11289f42009-09-09 15:08:12304
Douglas Gregor663b48f2012-01-03 19:48:16305 CASE(14, '_', 'p', __public_macro);
Fangrui Song6907ce22018-07-30 19:24:48306
Douglas Gregor663b48f2012-01-03 19:48:16307 CASE(15, '_', 'p', __private_macro);
308
Chris Lattner14a7f392009-04-08 18:24:34309 CASE(16, '_', 'i', __include_macros);
Chris Lattnerff067ce2007-10-07 07:52:34310#undef CASE
311#undef HASH
312 }
313}
Chris Lattner25e0d542006-10-18 06:07:05314
315//===----------------------------------------------------------------------===//
316// Stats Implementation
317//===----------------------------------------------------------------------===//
318
Chris Lattner22eb9722006-06-18 05:43:12319/// PrintStats - Print statistics about how well the identifier table is doing
320/// at hashing identifiers.
321void IdentifierTable::PrintStats() const {
Chris Lattner2b9e19b2006-10-29 23:43:13322 unsigned NumBuckets = HashTable.getNumBuckets();
323 unsigned NumIdentifiers = HashTable.getNumItems();
324 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
Chris Lattner22eb9722006-06-18 05:43:12325 unsigned AverageIdentifierSize = 0;
326 unsigned MaxIdentifierLength = 0;
Mike Stump11289f42009-09-09 15:08:12327
Chris Lattner2b9e19b2006-10-29 23:43:13328 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenek52f73ca2009-01-20 23:28:34329 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Chris Lattnerb055f2d2007-02-11 08:19:57330 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
331 unsigned IdLen = I->getKeyLength();
332 AverageIdentifierSize += IdLen;
333 if (MaxIdentifierLength < IdLen)
334 MaxIdentifierLength = IdLen;
335 }
Mike Stump11289f42009-09-09 15:08:12336
Chris Lattner23b7eb62007-06-15 23:05:46337 fprintf(stderr, "\n*** Identifier Table Stats:\n");
338 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
339 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
340 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
341 NumIdentifiers/(double)NumBuckets);
342 fprintf(stderr, "Ave identifier length: %f\n",
343 (AverageIdentifierSize/(double)NumIdentifiers));
344 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump11289f42009-09-09 15:08:12345
Chris Lattner22eb9722006-06-18 05:43:12346 // Compute statistics about the memory allocated for identifiers.
Chris Lattner2b9e19b2006-10-29 23:43:13347 HashTable.getAllocator().PrintStats();
Chris Lattner22eb9722006-06-18 05:43:12348}
Steve Narofff73590d2007-09-27 14:38:14349
Steve Naroffe61bfa82007-10-05 18:42:47350//===----------------------------------------------------------------------===//
351// SelectorTable Implementation
352//===----------------------------------------------------------------------===//
353
Chris Lattnerdadc7622007-10-05 20:15:24354unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
355 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
356}
357
Douglas Gregor77324f32008-11-17 14:58:09358namespace clang {
Eugene Zelenko918e0ca2017-11-03 22:35:27359
Bruno Ricci366ba732018-09-21 12:53:22360/// One of these variable length records is kept for each
Steve Naroffe61bfa82007-10-05 18:42:47361/// selector containing more than one keyword. We use a folding set
Mike Stump11289f42009-09-09 15:08:12362/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroffe61bfa82007-10-05 18:42:47363/// this class is provided strictly through Selector.
Bruno Ricci366ba732018-09-21 12:53:22364class alignas(IdentifierInfoAlignment) MultiKeywordSelector
365 : public detail::DeclarationNameExtra,
366 public llvm::FoldingSetNode {
367 MultiKeywordSelector(unsigned nKeys) : DeclarationNameExtra(nKeys) {}
Eugene Zelenko918e0ca2017-11-03 22:35:27368
Mike Stump11289f42009-09-09 15:08:12369public:
Steve Naroffe61bfa82007-10-05 18:42:47370 // Constructor for keyword selectors.
Bruno Ricci366ba732018-09-21 12:53:22371 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV)
372 : DeclarationNameExtra(nKeys) {
Steve Naroffe61bfa82007-10-05 18:42:47373 assert((nKeys > 1) && "not a multi-keyword selector");
Mike Stump11289f42009-09-09 15:08:12374
Steve Naroffe61bfa82007-10-05 18:42:47375 // Fill in the trailing keyword array.
Bruno Ricci366ba732018-09-21 12:53:22376 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this + 1);
Steve Naroffe61bfa82007-10-05 18:42:47377 for (unsigned i = 0; i != nKeys; ++i)
378 KeyInfo[i] = IIV[i];
Mike Stump11289f42009-09-09 15:08:12379 }
380
Chris Lattnerf7f34d02007-10-07 01:33:16381 // getName - Derive the full selector name and return it.
382 std::string getName() const;
Mike Stump11289f42009-09-09 15:08:12383
Bruno Ricci366ba732018-09-21 12:53:22384 using DeclarationNameExtra::getNumArgs;
Mike Stump11289f42009-09-09 15:08:12385
Eugene Zelenko918e0ca2017-11-03 22:35:27386 using keyword_iterator = IdentifierInfo *const *;
387
Steve Naroffe61bfa82007-10-05 18:42:47388 keyword_iterator keyword_begin() const {
Bruno Ricci366ba732018-09-21 12:53:22389 return reinterpret_cast<keyword_iterator>(this + 1);
Steve Naroffe61bfa82007-10-05 18:42:47390 }
Eugene Zelenko918e0ca2017-11-03 22:35:27391
Mike Stump11289f42009-09-09 15:08:12392 keyword_iterator keyword_end() const {
Bruno Ricci366ba732018-09-21 12:53:22393 return keyword_begin() + getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47394 }
Eugene Zelenko918e0ca2017-11-03 22:35:27395
Chris Lattnerf7f34d02007-10-07 01:33:16396 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor77324f32008-11-17 14:58:09397 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroffe61bfa82007-10-05 18:42:47398 return keyword_begin()[i];
399 }
Eugene Zelenko918e0ca2017-11-03 22:35:27400
Bruno Ricci366ba732018-09-21 12:53:22401 static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys,
402 unsigned NumArgs) {
Steve Naroffe61bfa82007-10-05 18:42:47403 ID.AddInteger(NumArgs);
Chris Lattnerf7f34d02007-10-07 01:33:16404 for (unsigned i = 0; i != NumArgs; ++i)
405 ID.AddPointer(ArgTys[i]);
Steve Naroffe61bfa82007-10-05 18:42:47406 }
Eugene Zelenko918e0ca2017-11-03 22:35:27407
Steve Naroffe61bfa82007-10-05 18:42:47408 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor77324f32008-11-17 14:58:09409 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroffe61bfa82007-10-05 18:42:47410 }
411};
Eugene Zelenko918e0ca2017-11-03 22:35:27412
413} // namespace clang.
Steve Naroffe61bfa82007-10-05 18:42:47414
Erik Pilkingtonaa385562019-08-14 16:57:11415bool Selector::isKeywordSelector(ArrayRef<StringRef> Names) const {
416 assert(!Names.empty() && "must have >= 1 selector slots");
417 if (getNumArgs() != Names.size())
418 return false;
419 for (unsigned I = 0, E = Names.size(); I != E; ++I) {
420 if (getNameForSlot(I) != Names[I])
421 return false;
422 }
423 return true;
424}
425
426bool Selector::isUnarySelector(StringRef Name) const {
427 return isUnarySelector() && getNameForSlot(0) == Name;
428}
429
Steve Naroffe61bfa82007-10-05 18:42:47430unsigned Selector::getNumArgs() const {
431 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor93a586f2012-05-04 18:24:37432 if (IIF <= ZeroArg)
Steve Naroffe61bfa82007-10-05 18:42:47433 return 0;
434 if (IIF == OneArg)
435 return 1;
Douglas Gregor93a586f2012-05-04 18:24:37436 // We point to a MultiKeywordSelector.
437 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump11289f42009-09-09 15:08:12438 return SI->getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47439}
440
Chris Lattnerf7f34d02007-10-07 01:33:16441IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor93a586f2012-05-04 18:24:37442 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf7f34d02007-10-07 01:33:16443 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor70091b82009-04-26 22:20:50444 return getAsIdentifierInfo();
Steve Naroffe61bfa82007-10-05 18:42:47445 }
Eugene Zelenko918e0ca2017-11-03 22:35:27446
Douglas Gregor93a586f2012-05-04 18:24:37447 // We point to a MultiKeywordSelector.
448 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroffe61bfa82007-10-05 18:42:47449 return SI->getIdentifierInfoForSlot(argIndex);
450}
451
Chris Lattner0e62c1c2011-07-23 10:55:15452StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregoraf2a6ae2011-02-18 22:29:55453 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Bruno Ricci366ba732018-09-21 12:53:22454 return II ? II->getName() : StringRef();
Douglas Gregoraf2a6ae2011-02-18 22:29:55455}
456
Chris Lattnerf7f34d02007-10-07 01:33:16457std::string MultiKeywordSelector::getName() const {
Dylan Noblesmith2c1dd272012-02-05 02:13:05458 SmallString<256> Str;
Daniel Dunbar1c0761d2009-10-17 18:13:02459 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf7f34d02007-10-07 01:33:16460 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
461 if (*I)
Daniel Dunbar07d07852009-10-18 21:17:35462 OS << (*I)->getName();
Daniel Dunbar1c0761d2009-10-17 18:13:02463 OS << ':';
Steve Naroffe61bfa82007-10-05 18:42:47464 }
Mike Stump11289f42009-09-09 15:08:12465
Benjamin Krameradcd0262020-01-28 19:23:46466 return std::string(OS.str());
Steve Naroffe61bfa82007-10-05 18:42:47467}
468
Chris Lattnere4b95692008-11-24 03:33:13469std::string Selector::getAsString() const {
Douglas Gregor70091b82009-04-26 22:20:50470 if (InfoPtr == 0)
471 return "<null selector>";
472
Douglas Gregor93a586f2012-05-04 18:24:37473 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenek5f080b42009-03-06 23:36:28474 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12475
Chandler Carruth59666772016-11-04 06:32:57476 if (getNumArgs() == 0) {
477 assert(II && "If the number of arguments is 0 then II is guaranteed to "
478 "not be null.");
Benjamin Krameradcd0262020-01-28 19:23:46479 return std::string(II->getName());
Chandler Carruth59666772016-11-04 06:32:57480 }
Ted Kremenek5f080b42009-03-06 23:36:28481
Daniel Dunbar1c0761d2009-10-17 18:13:02482 if (!II)
483 return ":";
484
Daniel Dunbar07d07852009-10-18 21:17:35485 return II->getName().str() + ":";
Steve Naroffe61bfa82007-10-05 18:42:47486 }
Mike Stump11289f42009-09-09 15:08:12487
Douglas Gregor93a586f2012-05-04 18:24:37488 // We have a multiple keyword selector.
489 return getMultiKeywordSelector()->getName();
Steve Naroffe61bfa82007-10-05 18:42:47490}
491
Aaron Ballmanb190f972014-01-03 17:59:55492void Selector::print(llvm::raw_ostream &OS) const {
493 OS << getAsString();
494}
495
Aditya Kumar69958212018-05-31 14:45:32496LLVM_DUMP_METHOD void Selector::dump() const { print(llvm::errs()); }
497
John McCallb4526252011-03-02 01:50:55498/// Interpreting the given string using the normal CamelCase
499/// conventions, determine whether the given string starts with the
500/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner0e62c1c2011-07-23 10:55:15501static bool startsWithWord(StringRef name, StringRef word) {
John McCallb4526252011-03-02 01:50:55502 if (name.size() < word.size()) return false;
Jordan Rosea7d03842013-02-08 22:30:41503 return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
504 name.startswith(word));
John McCallb4526252011-03-02 01:50:55505}
506
507ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
508 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
509 if (!first) return OMF_None;
510
Chris Lattner0e62c1c2011-07-23 10:55:15511 StringRef name = first->getName();
John McCallb4526252011-03-02 01:50:55512 if (sel.isUnarySelector()) {
513 if (name == "autorelease") return OMF_autorelease;
514 if (name == "dealloc") return OMF_dealloc;
Nico Weber1fb82662011-08-28 22:35:17515 if (name == "finalize") return OMF_finalize;
John McCallb4526252011-03-02 01:50:55516 if (name == "release") return OMF_release;
517 if (name == "retain") return OMF_retain;
518 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor33823722011-06-11 01:09:30519 if (name == "self") return OMF_self;
Fariborz Jahanian78e9deb2014-08-22 16:57:26520 if (name == "initialize") return OMF_initialize;
John McCallb4526252011-03-02 01:50:55521 }
Alex Lorenz0e23c612017-03-06 15:58:34522
523 if (name == "performSelector" || name == "performSelectorInBackground" ||
524 name == "performSelectorOnMainThread")
525 return OMF_performSelector;
John McCallb4526252011-03-02 01:50:55526
527 // The other method families may begin with a prefix of underscores.
528 while (!name.empty() && name.front() == '_')
529 name = name.substr(1);
530
531 if (name.empty()) return OMF_None;
532 switch (name.front()) {
533 case 'a':
534 if (startsWithWord(name, "alloc")) return OMF_alloc;
535 break;
536 case 'c':
537 if (startsWithWord(name, "copy")) return OMF_copy;
538 break;
539 case 'i':
540 if (startsWithWord(name, "init")) return OMF_init;
541 break;
542 case 'm':
543 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
544 break;
545 case 'n':
546 if (startsWithWord(name, "new")) return OMF_new;
547 break;
548 default:
549 break;
550 }
551
552 return OMF_None;
553}
Steve Naroffe61bfa82007-10-05 18:42:47554
Fariborz Jahanian71221352013-07-23 22:42:28555ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
Fariborz Jahanian4f3a64f2013-07-23 19:31:17556 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
557 if (!first) return OIT_None;
Fangrui Song6907ce22018-07-30 19:24:48558
Fariborz Jahanian4f3a64f2013-07-23 19:31:17559 StringRef name = first->getName();
Fangrui Song6907ce22018-07-30 19:24:48560
Fariborz Jahanian4f3a64f2013-07-23 19:31:17561 if (name.empty()) return OIT_None;
562 switch (name.front()) {
563 case 'a':
Fariborz Jahanian4ccdc732013-08-29 16:22:26564 if (startsWithWord(name, "array")) return OIT_Array;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17565 break;
566 case 'd':
Fariborz Jahanian7c87b432013-10-10 18:23:13567 if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17568 if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
569 break;
Fariborz Jahanian9275c682013-08-02 20:54:18570 case 's':
Fariborz Jahanian7c87b432013-10-10 18:23:13571 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
572 if (startsWithWord(name, "standard")) return OIT_Singleton;
Galina Kistanovaddcd2812017-06-03 06:40:10573 break;
Fariborz Jahanian1c900bc2013-09-18 20:35:47574 case 'i':
575 if (startsWithWord(name, "init")) return OIT_Init;
Reid Kleckner4dc0b1a2018-11-01 19:54:45576 break;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17577 default:
578 break;
579 }
580 return OIT_None;
581}
582
Fariborz Jahanian6485fe42014-09-09 23:10:54583ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
584 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
585 if (!first) return SFF_None;
Fangrui Song6907ce22018-07-30 19:24:48586
Fariborz Jahanian6485fe42014-09-09 23:10:54587 StringRef name = first->getName();
Fangrui Song6907ce22018-07-30 19:24:48588
Fariborz Jahanian6485fe42014-09-09 23:10:54589 switch (name.front()) {
590 case 'a':
591 if (name == "appendFormat") return SFF_NSString;
592 break;
Fangrui Song6907ce22018-07-30 19:24:48593
Fariborz Jahanian6485fe42014-09-09 23:10:54594 case 'i':
595 if (name == "initWithFormat") return SFF_NSString;
596 break;
Fangrui Song6907ce22018-07-30 19:24:48597
Fariborz Jahanian6485fe42014-09-09 23:10:54598 case 'l':
599 if (name == "localizedStringWithFormat") return SFF_NSString;
600 break;
Fangrui Song6907ce22018-07-30 19:24:48601
Fariborz Jahanian6485fe42014-09-09 23:10:54602 case 's':
603 if (name == "stringByAppendingFormat" ||
604 name == "stringWithFormat") return SFF_NSString;
605 break;
606 }
607 return SFF_None;
608}
609
Chris Lattner1a849942009-03-04 05:35:38610namespace {
Eugene Zelenko918e0ca2017-11-03 22:35:27611
612struct SelectorTableImpl {
613 llvm::FoldingSet<MultiKeywordSelector> Table;
614 llvm::BumpPtrAllocator Allocator;
615};
616
617} // namespace
Chris Lattner1a849942009-03-04 05:35:38618
619static SelectorTableImpl &getSelectorTableImpl(void *P) {
620 return *static_cast<SelectorTableImpl*>(P);
621}
622
Adrian Prantl6e77c962013-06-10 21:36:55623SmallString<64>
Adrian Prantla4ce9062013-06-07 22:29:12624SelectorTable::constructSetterName(StringRef Name) {
Adrian Prantl6e77c962013-06-10 21:36:55625 SmallString<64> SetterName("set");
626 SetterName += Name;
627 SetterName[3] = toUppercase(SetterName[3]);
628 return SetterName;
Adrian Prantla4ce9062013-06-07 22:29:12629}
630
Adrian Prantl6e77c962013-06-10 21:36:55631Selector
Adrian Prantla4ce9062013-06-07 22:29:12632SelectorTable::constructSetterSelector(IdentifierTable &Idents,
633 SelectorTable &SelTable,
634 const IdentifierInfo *Name) {
635 IdentifierInfo *SetterName =
636 &Idents.get(constructSetterName(Name->getName()));
Benjamin Kramer49038022012-02-04 13:45:25637 return SelTable.getUnarySelector(SetterName);
638}
639
Alex Lorenz5a5a6542018-05-23 00:52:20640std::string SelectorTable::getPropertyNameFromSetterSelector(Selector Sel) {
641 StringRef Name = Sel.getNameForSlot(0);
642 assert(Name.startswith("set") && "invalid setter name");
643 return (Twine(toLowercase(Name[3])) + Name.drop_front(4)).str();
644}
645
Ted Kremenek1c2239e2011-04-18 22:47:04646size_t SelectorTable::getTotalMemory() const {
647 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
648 return SelTabImpl.Allocator.getTotalMemory();
649}
Chris Lattner1a849942009-03-04 05:35:38650
Chris Lattner5700fab2007-10-07 02:00:24651Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
652 if (nKeys < 2)
653 return Selector(IIV[0], nKeys);
Mike Stump11289f42009-09-09 15:08:12654
Chris Lattner1a849942009-03-04 05:35:38655 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump11289f42009-09-09 15:08:12656
Steve Naroffe61bfa82007-10-05 18:42:47657 // Unique selector, to guarantee there is one per name.
658 llvm::FoldingSetNodeID ID;
659 MultiKeywordSelector::Profile(ID, IIV, nKeys);
660
Craig Topperf1186c52014-05-08 06:41:40661 void *InsertPos = nullptr;
Chris Lattner1a849942009-03-04 05:35:38662 if (MultiKeywordSelector *SI =
663 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe61bfa82007-10-05 18:42:47664 return Selector(SI);
Mike Stump11289f42009-09-09 15:08:12665
Steve Naroffe61bfa82007-10-05 18:42:47666 // MultiKeywordSelector objects are not allocated with new because they have a
667 // variable size array (for parameter types) at the end of them.
Chris Lattner1a849942009-03-04 05:35:38668 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
669 MultiKeywordSelector *SI =
Benjamin Kramerc3f89252016-10-20 14:27:22670 (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate(
671 Size, alignof(MultiKeywordSelector));
Steve Naroffe61bfa82007-10-05 18:42:47672 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner1a849942009-03-04 05:35:38673 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroffe61bfa82007-10-05 18:42:47674 return Selector(SI);
675}
676
Steve Naroffe61bfa82007-10-05 18:42:47677SelectorTable::SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38678 Impl = new SelectorTableImpl();
Steve Naroffe61bfa82007-10-05 18:42:47679}
680
681SelectorTable::~SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38682 delete &getSelectorTableImpl(Impl);
Steve Naroffe61bfa82007-10-05 18:42:47683}
684
Douglas Gregor71395fa2009-11-04 00:56:37685const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
686 switch (Operator) {
687 case OO_None:
688 case NUM_OVERLOADED_OPERATORS:
Craig Topperf1186c52014-05-08 06:41:40689 return nullptr;
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37690
Douglas Gregor71395fa2009-11-04 00:56:37691#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
692 case OO_##Name: return Spelling;
693#include "clang/Basic/OperatorKinds.def"
694 }
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37695
David Blaikie8a40f702012-01-17 06:56:22696 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregor71395fa2009-11-04 00:56:37697}
Douglas Gregor813a0662015-06-19 18:14:38698
Douglas Gregoraea7afd2015-06-24 22:02:08699StringRef clang::getNullabilitySpelling(NullabilityKind kind,
700 bool isContextSensitive) {
Douglas Gregor813a0662015-06-19 18:14:38701 switch (kind) {
702 case NullabilityKind::NonNull:
Douglas Gregoraea7afd2015-06-24 22:02:08703 return isContextSensitive ? "nonnull" : "_Nonnull";
Douglas Gregor813a0662015-06-19 18:14:38704
705 case NullabilityKind::Nullable:
Douglas Gregoraea7afd2015-06-24 22:02:08706 return isContextSensitive ? "nullable" : "_Nullable";
Douglas Gregor813a0662015-06-19 18:14:38707
708 case NullabilityKind::Unspecified:
Douglas Gregoraea7afd2015-06-24 22:02:08709 return isContextSensitive ? "null_unspecified" : "_Null_unspecified";
Douglas Gregor813a0662015-06-19 18:14:38710 }
Nico Weber708aabd2015-06-20 00:06:30711 llvm_unreachable("Unknown nullability kind.");
Douglas Gregor813a0662015-06-19 18:14:38712}