blob: dcd1e8b8dca0e8b60b84659c77837ed5f6f4d248 [file] [log] [blame]
Chris Lattnerec659fc2006-10-29 22:09:44 +00001//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000010// This file implements the IdentifierInfo, IdentifierVisitor, and
Chris Lattner91cbf112006-07-03 04:28:52 +000011// IdentifierTable interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14
Adrian Prantla4ce9062013-06-07 22:29:12 +000015#include "clang/Basic/CharInfo.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000016#include "clang/Basic/IdentifierTable.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000017#include "clang/Basic/LangOptions.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000018#include "clang/Basic/OperatorKinds.h"
Douglas Gregor813a0662015-06-19 18:14:38 +000019#include "clang/Basic/Specifiers.h"
Chris Lattnerdadc7622007-10-05 20:15:24 +000020#include "llvm/ADT/DenseMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "llvm/ADT/FoldingSet.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000022#include "llvm/ADT/SmallString.h"
David Blaikie8a40f702012-01-17 06:56:22 +000023#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000025#include <cstdio>
Ted Kremenekf25f4a32007-10-23 22:18:37 +000026
Chris Lattner22eb9722006-06-18 05:43:12 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000030// IdentifierInfo Implementation
Chris Lattner22eb9722006-06-18 05:43:12 +000031//===----------------------------------------------------------------------===//
32
Ted Kremenek52f73ca2009-01-20 23:28:34 +000033IdentifierInfo::IdentifierInfo() {
Chris Lattner3bc804e2006-10-28 23:46:24 +000034 TokenID = tok::identifier;
Douglas Gregor2ad7ee92008-11-06 16:32:23 +000035 ObjCOrBuiltinID = 0;
Chris Lattnera441ca62007-10-07 07:09:52 +000036 HasMacro = false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +000037 HadMacro = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000038 IsExtension = false;
Richard Smith31d51842015-05-14 04:00:59 +000039 IsFutureCompatKeyword = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000040 IsPoisoned = false;
Chris Lattner5b9f4892006-11-21 17:23:33 +000041 IsCPPOperatorKeyword = false;
Chris Lattnerad89ec02009-01-21 07:43:11 +000042 NeedsHandleIdentifier = false;
Sebastian Redld44cd6a2010-08-18 23:57:06 +000043 IsFromAST = false;
Douglas Gregor935bc7a22011-10-27 09:33:13 +000044 ChangedAfterLoad = false;
Richard Smithd79514e2016-02-05 19:03:40 +000045 FEChangedAfterLoad = false;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +000046 RevertedTokenID = false;
Douglas Gregor935bc7a22011-10-27 09:33:13 +000047 OutOfDate = false;
Ted Kremenekc1e4dd02012-03-01 22:07:04 +000048 IsModulesImport = false;
Craig Topperf1186c52014-05-08 06:41:40 +000049 FETokenInfo = nullptr;
50 Entry = nullptr;
Chris Lattner3bc804e2006-10-28 23:46:24 +000051}
52
Chris Lattner91cbf112006-07-03 04:28:52 +000053//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +000054// IdentifierTable Implementation
55//===----------------------------------------------------------------------===//
56
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000057IdentifierIterator::~IdentifierIterator() { }
Douglas Gregor57756ea2010-10-14 22:11:03 +000058
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000059IdentifierInfoLookup::~IdentifierInfoLookup() {}
Ted Kremeneka705b042009-01-15 18:47:46 +000060
Douglas Gregor57756ea2010-10-14 22:11:03 +000061namespace {
62 /// \brief A simple identifier lookup iterator that represents an
63 /// empty sequence of identifiers.
64 class EmptyLookupIterator : public IdentifierIterator
65 {
66 public:
Craig Topper3164f332014-03-11 03:39:26 +000067 StringRef Next() override { return StringRef(); }
Douglas Gregor57756ea2010-10-14 22:11:03 +000068 };
69}
70
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +000071IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
Douglas Gregor57756ea2010-10-14 22:11:03 +000072 return new EmptyLookupIterator();
73}
74
Ted Kremeneka705b042009-01-15 18:47:46 +000075IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
76 IdentifierInfoLookup* externalLookup)
77 : HashTable(8192), // Start with space for 8K identifiers.
78 ExternalLookup(externalLookup) {
Chris Lattnerda933aa2006-10-29 23:49:15 +000079
Chris Lattnerf2e3ac32006-10-27 03:59:10 +000080 // Populate the identifier table with info about keywords for the current
81 // language.
Chris Lattner25e0d542006-10-18 06:07:05 +000082 AddKeywords(LangOpts);
Ted Kremenek94666032012-03-01 22:53:32 +000083
84
85 // Add the '_experimental_modules_import' contextual keyword.
Douglas Gregorc50d4922012-12-11 22:11:52 +000086 get("import").setModulesImport(true);
Chris Lattner91cbf112006-07-03 04:28:52 +000087}
Chris Lattner22eb9722006-06-18 05:43:12 +000088
Chris Lattner25e0d542006-10-18 06:07:05 +000089//===----------------------------------------------------------------------===//
90// Language Keyword Implementation
91//===----------------------------------------------------------------------===//
92
Eli Friedman2b680b42009-04-28 03:13:54 +000093// Constants for TokenKinds.def
94namespace {
95 enum {
Dylan Noblesmith92c07c22011-04-09 13:34:05 +000096 KEYC99 = 0x1,
97 KEYCXX = 0x2,
Richard Smith89645bc2013-01-02 12:01:23 +000098 KEYCXX11 = 0x4,
Dylan Noblesmith92c07c22011-04-09 13:34:05 +000099 KEYGNU = 0x8,
100 KEYMS = 0x10,
101 BOOLSUPPORT = 0x20,
102 KEYALTIVEC = 0x40,
103 KEYNOCXX = 0x80,
104 KEYBORLAND = 0x100,
105 KEYOPENCL = 0x200,
Benjamin Kramere56f3932011-12-23 17:00:35 +0000106 KEYC11 = 0x400,
John McCall31168b02011-06-15 23:02:42 +0000107 KEYARC = 0x800,
David Majnemer28aae9c2015-03-18 04:15:23 +0000108 KEYNOMS18 = 0x01000,
Anastasia Stulovab1152f12015-03-18 12:55:29 +0000109 KEYNOOPENCL = 0x02000,
110 WCHARSUPPORT = 0x04000,
111 HALFSUPPORT = 0x08000,
Hubert Tong6e04f982015-06-05 01:10:24 +0000112 KEYCONCEPTS = 0x10000,
Douglas Gregorab209d82015-07-07 03:58:42 +0000113 KEYOBJC2 = 0x20000,
Ulrich Weigand3c5038a2015-07-30 14:08:36 +0000114 KEYZVECTOR = 0x40000,
Richard Smitheb7927e2015-10-22 03:52:15 +0000115 KEYCOROUTINES = 0x80000,
Richard Smithc7bf3802016-07-23 02:32:21 +0000116 KEYMODULES = 0x100000,
117 KEYALL = (0x1fffff & ~KEYNOMS18 &
Anastasia Stulovab1152f12015-03-18 12:55:29 +0000118 ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
Eli Friedman2b680b42009-04-28 03:13:54 +0000119 };
Serge Pavlov77af3812014-10-29 10:59:18 +0000120
121 /// \brief How a keyword is treated in the selected standard.
122 enum KeywordStatus {
123 KS_Disabled, // Disabled
124 KS_Extension, // Is an extension
125 KS_Enabled, // Enabled
126 KS_Future // Is a keyword in future standard
127 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000128}
Serge Pavlov77af3812014-10-29 10:59:18 +0000129
130/// \brief Translates flags as specified in TokenKinds.def into keyword status
131/// in the given language standard.
Serge Pavlov83cf0782014-12-11 12:18:08 +0000132static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
Serge Pavlov77af3812014-10-29 10:59:18 +0000133 unsigned Flags) {
134 if (Flags == KEYALL) return KS_Enabled;
135 if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled;
136 if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled;
137 if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled;
138 if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension;
139 if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension;
140 if (LangOpts.Borland && (Flags & KEYBORLAND)) return KS_Extension;
141 if (LangOpts.Bool && (Flags & BOOLSUPPORT)) return KS_Enabled;
142 if (LangOpts.Half && (Flags & HALFSUPPORT)) return KS_Enabled;
143 if (LangOpts.WChar && (Flags & WCHARSUPPORT)) return KS_Enabled;
144 if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) return KS_Enabled;
145 if (LangOpts.OpenCL && (Flags & KEYOPENCL)) return KS_Enabled;
146 if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled;
147 if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled;
148 // We treat bridge casts as objective-C keywords so we can warn on them
149 // in non-arc mode.
150 if (LangOpts.ObjC2 && (Flags & KEYARC)) return KS_Enabled;
Douglas Gregorab209d82015-07-07 03:58:42 +0000151 if (LangOpts.ObjC2 && (Flags & KEYOBJC2)) return KS_Enabled;
Richard Smithc7bf3802016-07-23 02:32:21 +0000152 if (LangOpts.ConceptsTS && (Flags & KEYCONCEPTS)) return KS_Enabled;
Gor Nishanov4ffb4342016-10-02 03:31:58 +0000153 if (LangOpts.CoroutinesTS && (Flags & KEYCOROUTINES)) return KS_Enabled;
Richard Smithc7bf3802016-07-23 02:32:21 +0000154 if (LangOpts.ModulesTS && (Flags & KEYMODULES)) return KS_Enabled;
Serge Pavlov77af3812014-10-29 10:59:18 +0000155 if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) return KS_Future;
156 return KS_Disabled;
Eli Friedman2b680b42009-04-28 03:13:54 +0000157}
158
Chris Lattner25e0d542006-10-18 06:07:05 +0000159/// AddKeyword - This method is used to associate a token ID with specific
160/// identifiers because they are language keywords. This causes the lexer to
161/// automatically map matching identifiers to specialized token codes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000162static void AddKeyword(StringRef Keyword,
Eli Friedman2b680b42009-04-28 03:13:54 +0000163 tok::TokenKind TokenCode, unsigned Flags,
Chris Lattner25e0d542006-10-18 06:07:05 +0000164 const LangOptions &LangOpts, IdentifierTable &Table) {
Serge Pavlov83cf0782014-12-11 12:18:08 +0000165 KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
Richard Smith4dd85d62011-10-11 19:57:52 +0000166
Alp Tokerbfa39342014-01-14 12:51:41 +0000167 // Don't add this keyword under MSVCCompat.
David Majnemer28aae9c2015-03-18 04:15:23 +0000168 if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) &&
David Majnemerb710a932015-05-11 03:57:49 +0000169 !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
Anastasia Stulovab1152f12015-03-18 12:55:29 +0000170 return;
171
172 // Don't add this keyword under OpenCL.
173 if (LangOpts.OpenCL && (Flags & KEYNOOPENCL))
174 return;
175
Eli Friedman2b680b42009-04-28 03:13:54 +0000176 // Don't add this keyword if disabled in this language.
Serge Pavlov77af3812014-10-29 10:59:18 +0000177 if (AddResult == KS_Disabled) return;
Eli Friedman2b680b42009-04-28 03:13:54 +0000178
Richard Smith4dd85d62011-10-11 19:57:52 +0000179 IdentifierInfo &Info =
Serge Pavlov77af3812014-10-29 10:59:18 +0000180 Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode);
181 Info.setIsExtensionToken(AddResult == KS_Extension);
Richard Smith31d51842015-05-14 04:00:59 +0000182 Info.setIsFutureCompatKeyword(AddResult == KS_Future);
Chris Lattner25e0d542006-10-18 06:07:05 +0000183}
184
Chris Lattner5b9f4892006-11-21 17:23:33 +0000185/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
186/// representations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000187static void AddCXXOperatorKeyword(StringRef Keyword,
Chris Lattner5b9f4892006-11-21 17:23:33 +0000188 tok::TokenKind TokenCode,
189 IdentifierTable &Table) {
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000190 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekf25f4a32007-10-23 22:18:37 +0000191 Info.setIsCPlusPlusOperatorKeyword();
Chris Lattner5b9f4892006-11-21 17:23:33 +0000192}
193
James Dennett0d8a3f82012-06-15 21:27:44 +0000194/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
195/// or "property".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000196static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000197 tok::ObjCKeywordKind ObjCID,
Chris Lattner25e0d542006-10-18 06:07:05 +0000198 IdentifierTable &Table) {
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000199 Table.get(Name).setObjCKeywordID(ObjCID);
Chris Lattner25e0d542006-10-18 06:07:05 +0000200}
201
202/// AddKeywords - Add all keywords to the symbol table.
203///
204void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Chris Lattner25e0d542006-10-18 06:07:05 +0000205 // Add keywords and tokens for the current language.
206#define KEYWORD(NAME, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000207 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000208 FLAGS, LangOpts, *this);
209#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000210 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000211 FLAGS, LangOpts, *this);
Chris Lattner5b9f4892006-11-21 17:23:33 +0000212#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
Chris Lattner3e7592e2006-12-04 07:48:37 +0000213 if (LangOpts.CXXOperatorNames) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000214 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000215#define OBJC1_AT_KEYWORD(NAME) \
216 if (LangOpts.ObjC1) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000217 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000218#define OBJC2_AT_KEYWORD(NAME) \
219 if (LangOpts.ObjC2) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000220 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCall39439732011-04-09 22:50:59 +0000221#define TESTING_KEYWORD(NAME, FLAGS)
Chris Lattner25e0d542006-10-18 06:07:05 +0000222#include "clang/Basic/TokenKinds.def"
John McCall39439732011-04-09 22:50:59 +0000223
224 if (LangOpts.ParseUnknownAnytype)
225 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
226 LangOpts, *this);
Aaron Ballman674cf262015-05-26 19:44:52 +0000227
Saleem Abdulrasoold170c4b2015-10-04 17:51:05 +0000228 if (LangOpts.DeclSpecKeyword)
Aaron Ballman674cf262015-05-26 19:44:52 +0000229 AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000230}
231
Serge Pavlov83cf0782014-12-11 12:18:08 +0000232/// \brief Checks if the specified token kind represents a keyword in the
233/// specified language.
234/// \returns Status of the keyword in the language.
235static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
236 tok::TokenKind K) {
237 switch (K) {
238#define KEYWORD(NAME, FLAGS) \
239 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
240#include "clang/Basic/TokenKinds.def"
241 default: return KS_Disabled;
242 }
243}
244
245/// \brief Returns true if the identifier represents a keyword in the
246/// specified language.
247bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) {
248 switch (getTokenKwStatus(LangOpts, getTokenID())) {
249 case KS_Enabled:
250 case KS_Extension:
251 return true;
252 default:
253 return false;
254 }
255}
256
Chris Lattnerff067ce2007-10-07 07:52:34 +0000257tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
258 // We use a perfect hash function here involving the length of the keyword,
259 // the first and third character. For preprocessor ID's there are no
260 // collisions (if there were, the switch below would complain about duplicate
261 // case values). Note that this depends on 'if' being null terminated.
Mike Stump11289f42009-09-09 15:08:12 +0000262
Chris Lattnerff067ce2007-10-07 07:52:34 +0000263#define HASH(LEN, FIRST, THIRD) \
264 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
265#define CASE(LEN, FIRST, THIRD, NAME) \
266 case HASH(LEN, FIRST, THIRD): \
267 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump11289f42009-09-09 15:08:12 +0000268
Chris Lattnerff067ce2007-10-07 07:52:34 +0000269 unsigned Len = getLength();
Chris Lattnerd2b8ce42007-10-10 20:59:57 +0000270 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000271 const char *Name = getNameStart();
Chris Lattnerff067ce2007-10-07 07:52:34 +0000272 switch (HASH(Len, Name[0], Name[2])) {
273 default: return tok::pp_not_keyword;
274 CASE( 2, 'i', '\0', if);
275 CASE( 4, 'e', 'i', elif);
276 CASE( 4, 'e', 's', else);
277 CASE( 4, 'l', 'n', line);
278 CASE( 4, 's', 'c', sccs);
279 CASE( 5, 'e', 'd', endif);
280 CASE( 5, 'e', 'r', error);
281 CASE( 5, 'i', 'e', ident);
282 CASE( 5, 'i', 'd', ifdef);
283 CASE( 5, 'u', 'd', undef);
284
285 CASE( 6, 'a', 's', assert);
286 CASE( 6, 'd', 'f', define);
287 CASE( 6, 'i', 'n', ifndef);
288 CASE( 6, 'i', 'p', import);
289 CASE( 6, 'p', 'a', pragma);
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000290
Chris Lattnerff067ce2007-10-07 07:52:34 +0000291 CASE( 7, 'd', 'f', defined);
292 CASE( 7, 'i', 'c', include);
293 CASE( 7, 'w', 'r', warning);
294
295 CASE( 8, 'u', 'a', unassert);
296 CASE(12, 'i', 'c', include_next);
Mike Stump11289f42009-09-09 15:08:12 +0000297
Douglas Gregor663b48f2012-01-03 19:48:16 +0000298 CASE(14, '_', 'p', __public_macro);
299
300 CASE(15, '_', 'p', __private_macro);
301
Chris Lattner14a7f392009-04-08 18:24:34 +0000302 CASE(16, '_', 'i', __include_macros);
Chris Lattnerff067ce2007-10-07 07:52:34 +0000303#undef CASE
304#undef HASH
305 }
306}
Chris Lattner25e0d542006-10-18 06:07:05 +0000307
308//===----------------------------------------------------------------------===//
309// Stats Implementation
310//===----------------------------------------------------------------------===//
311
Chris Lattner22eb9722006-06-18 05:43:12 +0000312/// PrintStats - Print statistics about how well the identifier table is doing
313/// at hashing identifiers.
314void IdentifierTable::PrintStats() const {
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000315 unsigned NumBuckets = HashTable.getNumBuckets();
316 unsigned NumIdentifiers = HashTable.getNumItems();
317 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
Chris Lattner22eb9722006-06-18 05:43:12 +0000318 unsigned AverageIdentifierSize = 0;
319 unsigned MaxIdentifierLength = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000320
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000321 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000322 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000323 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
324 unsigned IdLen = I->getKeyLength();
325 AverageIdentifierSize += IdLen;
326 if (MaxIdentifierLength < IdLen)
327 MaxIdentifierLength = IdLen;
328 }
Mike Stump11289f42009-09-09 15:08:12 +0000329
Chris Lattner23b7eb62007-06-15 23:05:46 +0000330 fprintf(stderr, "\n*** Identifier Table Stats:\n");
331 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
332 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
333 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
334 NumIdentifiers/(double)NumBuckets);
335 fprintf(stderr, "Ave identifier length: %f\n",
336 (AverageIdentifierSize/(double)NumIdentifiers));
337 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump11289f42009-09-09 15:08:12 +0000338
Chris Lattner22eb9722006-06-18 05:43:12 +0000339 // Compute statistics about the memory allocated for identifiers.
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000340 HashTable.getAllocator().PrintStats();
Chris Lattner22eb9722006-06-18 05:43:12 +0000341}
Steve Narofff73590d2007-09-27 14:38:14 +0000342
Steve Naroffe61bfa82007-10-05 18:42:47 +0000343//===----------------------------------------------------------------------===//
344// SelectorTable Implementation
345//===----------------------------------------------------------------------===//
346
Chris Lattnerdadc7622007-10-05 20:15:24 +0000347unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
348 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
349}
350
Douglas Gregor77324f32008-11-17 14:58:09 +0000351namespace clang {
Steve Naroffe61bfa82007-10-05 18:42:47 +0000352/// MultiKeywordSelector - One of these variable length records is kept for each
353/// selector containing more than one keyword. We use a folding set
Mike Stump11289f42009-09-09 15:08:12 +0000354/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroffe61bfa82007-10-05 18:42:47 +0000355/// this class is provided strictly through Selector.
Mike Stump11289f42009-09-09 15:08:12 +0000356class MultiKeywordSelector
Douglas Gregor77324f32008-11-17 14:58:09 +0000357 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor77324f32008-11-17 14:58:09 +0000358 MultiKeywordSelector(unsigned nKeys) {
359 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
360 }
Mike Stump11289f42009-09-09 15:08:12 +0000361public:
Steve Naroffe61bfa82007-10-05 18:42:47 +0000362 // Constructor for keyword selectors.
363 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
364 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor77324f32008-11-17 14:58:09 +0000365 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump11289f42009-09-09 15:08:12 +0000366
Steve Naroffe61bfa82007-10-05 18:42:47 +0000367 // Fill in the trailing keyword array.
368 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
369 for (unsigned i = 0; i != nKeys; ++i)
370 KeyInfo[i] = IIV[i];
Mike Stump11289f42009-09-09 15:08:12 +0000371 }
372
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000373 // getName - Derive the full selector name and return it.
374 std::string getName() const;
Mike Stump11289f42009-09-09 15:08:12 +0000375
Douglas Gregor77324f32008-11-17 14:58:09 +0000376 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Steve Naroffe61bfa82007-10-05 18:42:47 +0000378 typedef IdentifierInfo *const *keyword_iterator;
379 keyword_iterator keyword_begin() const {
380 return reinterpret_cast<keyword_iterator>(this+1);
381 }
Mike Stump11289f42009-09-09 15:08:12 +0000382 keyword_iterator keyword_end() const {
383 return keyword_begin()+getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000384 }
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000385 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor77324f32008-11-17 14:58:09 +0000386 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroffe61bfa82007-10-05 18:42:47 +0000387 return keyword_begin()[i];
388 }
Mike Stump11289f42009-09-09 15:08:12 +0000389 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroffe61bfa82007-10-05 18:42:47 +0000390 keyword_iterator ArgTys, unsigned NumArgs) {
391 ID.AddInteger(NumArgs);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000392 for (unsigned i = 0; i != NumArgs; ++i)
393 ID.AddPointer(ArgTys[i]);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000394 }
395 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000396 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000397 }
398};
Chris Lattnerdadc7622007-10-05 20:15:24 +0000399} // end namespace clang.
Steve Naroffe61bfa82007-10-05 18:42:47 +0000400
401unsigned Selector::getNumArgs() const {
402 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor93a586f2012-05-04 18:24:37 +0000403 if (IIF <= ZeroArg)
Steve Naroffe61bfa82007-10-05 18:42:47 +0000404 return 0;
405 if (IIF == OneArg)
406 return 1;
Douglas Gregor93a586f2012-05-04 18:24:37 +0000407 // We point to a MultiKeywordSelector.
408 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump11289f42009-09-09 15:08:12 +0000409 return SI->getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000410}
411
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000412IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor93a586f2012-05-04 18:24:37 +0000413 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000414 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor70091b82009-04-26 22:20:50 +0000415 return getAsIdentifierInfo();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000416 }
Douglas Gregor93a586f2012-05-04 18:24:37 +0000417 // We point to a MultiKeywordSelector.
418 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000419 return SI->getIdentifierInfoForSlot(argIndex);
420}
421
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000422StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +0000423 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000424 return II? II->getName() : StringRef();
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +0000425}
426
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000427std::string MultiKeywordSelector::getName() const {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000428 SmallString<256> Str;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000429 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000430 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
431 if (*I)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000432 OS << (*I)->getName();
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000433 OS << ':';
Steve Naroffe61bfa82007-10-05 18:42:47 +0000434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000436 return OS.str();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000437}
438
Chris Lattnere4b95692008-11-24 03:33:13 +0000439std::string Selector::getAsString() const {
Douglas Gregor70091b82009-04-26 22:20:50 +0000440 if (InfoPtr == 0)
441 return "<null selector>";
442
Douglas Gregor93a586f2012-05-04 18:24:37 +0000443 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenek5f080b42009-03-06 23:36:28 +0000444 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000445
Ted Kremenek0666a6c2009-03-07 01:22:02 +0000446 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenek5f080b42009-03-06 23:36:28 +0000447 if (getNumArgs() == 0)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000448 return II->getName();
Ted Kremenek5f080b42009-03-06 23:36:28 +0000449
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000450 if (!II)
451 return ":";
452
Daniel Dunbar07d07852009-10-18 21:17:35 +0000453 return II->getName().str() + ":";
Steve Naroffe61bfa82007-10-05 18:42:47 +0000454 }
Mike Stump11289f42009-09-09 15:08:12 +0000455
Douglas Gregor93a586f2012-05-04 18:24:37 +0000456 // We have a multiple keyword selector.
457 return getMultiKeywordSelector()->getName();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000458}
459
Aaron Ballmanb190f972014-01-03 17:59:55 +0000460void Selector::print(llvm::raw_ostream &OS) const {
461 OS << getAsString();
462}
463
John McCallb4526252011-03-02 01:50:55 +0000464/// Interpreting the given string using the normal CamelCase
465/// conventions, determine whether the given string starts with the
466/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000467static bool startsWithWord(StringRef name, StringRef word) {
John McCallb4526252011-03-02 01:50:55 +0000468 if (name.size() < word.size()) return false;
Jordan Rosea7d03842013-02-08 22:30:41 +0000469 return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
470 name.startswith(word));
John McCallb4526252011-03-02 01:50:55 +0000471}
472
473ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
474 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
475 if (!first) return OMF_None;
476
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000477 StringRef name = first->getName();
John McCallb4526252011-03-02 01:50:55 +0000478 if (sel.isUnarySelector()) {
479 if (name == "autorelease") return OMF_autorelease;
480 if (name == "dealloc") return OMF_dealloc;
Nico Weber1fb82662011-08-28 22:35:17 +0000481 if (name == "finalize") return OMF_finalize;
John McCallb4526252011-03-02 01:50:55 +0000482 if (name == "release") return OMF_release;
483 if (name == "retain") return OMF_retain;
484 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor33823722011-06-11 01:09:30 +0000485 if (name == "self") return OMF_self;
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000486 if (name == "initialize") return OMF_initialize;
John McCallb4526252011-03-02 01:50:55 +0000487 }
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000488
489 if (name == "performSelector") return OMF_performSelector;
John McCallb4526252011-03-02 01:50:55 +0000490
491 // The other method families may begin with a prefix of underscores.
492 while (!name.empty() && name.front() == '_')
493 name = name.substr(1);
494
495 if (name.empty()) return OMF_None;
496 switch (name.front()) {
497 case 'a':
498 if (startsWithWord(name, "alloc")) return OMF_alloc;
499 break;
500 case 'c':
501 if (startsWithWord(name, "copy")) return OMF_copy;
502 break;
503 case 'i':
504 if (startsWithWord(name, "init")) return OMF_init;
505 break;
506 case 'm':
507 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
508 break;
509 case 'n':
510 if (startsWithWord(name, "new")) return OMF_new;
511 break;
512 default:
513 break;
514 }
515
516 return OMF_None;
517}
Steve Naroffe61bfa82007-10-05 18:42:47 +0000518
Fariborz Jahanian71221352013-07-23 22:42:28 +0000519ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000520 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
521 if (!first) return OIT_None;
522
523 StringRef name = first->getName();
524
525 if (name.empty()) return OIT_None;
526 switch (name.front()) {
527 case 'a':
Fariborz Jahanian4ccdc732013-08-29 16:22:26 +0000528 if (startsWithWord(name, "array")) return OIT_Array;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000529 break;
530 case 'd':
Fariborz Jahanian7c87b432013-10-10 18:23:13 +0000531 if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000532 if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
533 break;
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000534 case 's':
Fariborz Jahanian7c87b432013-10-10 18:23:13 +0000535 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
536 if (startsWithWord(name, "standard")) return OIT_Singleton;
Fariborz Jahanian1c900bc2013-09-18 20:35:47 +0000537 case 'i':
538 if (startsWithWord(name, "init")) return OIT_Init;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000539 default:
540 break;
541 }
542 return OIT_None;
543}
544
Fariborz Jahanian6485fe42014-09-09 23:10:54 +0000545ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
546 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
547 if (!first) return SFF_None;
548
549 StringRef name = first->getName();
550
551 switch (name.front()) {
552 case 'a':
553 if (name == "appendFormat") return SFF_NSString;
554 break;
555
556 case 'i':
557 if (name == "initWithFormat") return SFF_NSString;
558 break;
559
560 case 'l':
561 if (name == "localizedStringWithFormat") return SFF_NSString;
562 break;
563
564 case 's':
565 if (name == "stringByAppendingFormat" ||
566 name == "stringWithFormat") return SFF_NSString;
567 break;
568 }
569 return SFF_None;
570}
571
Chris Lattner1a849942009-03-04 05:35:38 +0000572namespace {
573 struct SelectorTableImpl {
574 llvm::FoldingSet<MultiKeywordSelector> Table;
575 llvm::BumpPtrAllocator Allocator;
576 };
577} // end anonymous namespace.
578
579static SelectorTableImpl &getSelectorTableImpl(void *P) {
580 return *static_cast<SelectorTableImpl*>(P);
581}
582
Adrian Prantl6e77c962013-06-10 21:36:55 +0000583SmallString<64>
Adrian Prantla4ce9062013-06-07 22:29:12 +0000584SelectorTable::constructSetterName(StringRef Name) {
Adrian Prantl6e77c962013-06-10 21:36:55 +0000585 SmallString<64> SetterName("set");
586 SetterName += Name;
587 SetterName[3] = toUppercase(SetterName[3]);
588 return SetterName;
Adrian Prantla4ce9062013-06-07 22:29:12 +0000589}
590
Adrian Prantl6e77c962013-06-10 21:36:55 +0000591Selector
Adrian Prantla4ce9062013-06-07 22:29:12 +0000592SelectorTable::constructSetterSelector(IdentifierTable &Idents,
593 SelectorTable &SelTable,
594 const IdentifierInfo *Name) {
595 IdentifierInfo *SetterName =
596 &Idents.get(constructSetterName(Name->getName()));
Benjamin Kramer49038022012-02-04 13:45:25 +0000597 return SelTable.getUnarySelector(SetterName);
598}
599
Ted Kremenek1c2239e2011-04-18 22:47:04 +0000600size_t SelectorTable::getTotalMemory() const {
601 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
602 return SelTabImpl.Allocator.getTotalMemory();
603}
Chris Lattner1a849942009-03-04 05:35:38 +0000604
Chris Lattner5700fab2007-10-07 02:00:24 +0000605Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
606 if (nKeys < 2)
607 return Selector(IIV[0], nKeys);
Mike Stump11289f42009-09-09 15:08:12 +0000608
Chris Lattner1a849942009-03-04 05:35:38 +0000609 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump11289f42009-09-09 15:08:12 +0000610
Steve Naroffe61bfa82007-10-05 18:42:47 +0000611 // Unique selector, to guarantee there is one per name.
612 llvm::FoldingSetNodeID ID;
613 MultiKeywordSelector::Profile(ID, IIV, nKeys);
614
Craig Topperf1186c52014-05-08 06:41:40 +0000615 void *InsertPos = nullptr;
Chris Lattner1a849942009-03-04 05:35:38 +0000616 if (MultiKeywordSelector *SI =
617 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe61bfa82007-10-05 18:42:47 +0000618 return Selector(SI);
Mike Stump11289f42009-09-09 15:08:12 +0000619
Steve Naroffe61bfa82007-10-05 18:42:47 +0000620 // MultiKeywordSelector objects are not allocated with new because they have a
621 // variable size array (for parameter types) at the end of them.
Chris Lattner1a849942009-03-04 05:35:38 +0000622 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
623 MultiKeywordSelector *SI =
Mike Stump11289f42009-09-09 15:08:12 +0000624 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner5c0b4052010-10-30 05:14:06 +0000625 llvm::alignOf<MultiKeywordSelector>());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000626 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner1a849942009-03-04 05:35:38 +0000627 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000628 return Selector(SI);
629}
630
Steve Naroffe61bfa82007-10-05 18:42:47 +0000631SelectorTable::SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000632 Impl = new SelectorTableImpl();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000633}
634
635SelectorTable::~SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000636 delete &getSelectorTableImpl(Impl);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000637}
638
Douglas Gregor71395fa2009-11-04 00:56:37 +0000639const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
640 switch (Operator) {
641 case OO_None:
642 case NUM_OVERLOADED_OPERATORS:
Craig Topperf1186c52014-05-08 06:41:40 +0000643 return nullptr;
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000644
Douglas Gregor71395fa2009-11-04 00:56:37 +0000645#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
646 case OO_##Name: return Spelling;
647#include "clang/Basic/OperatorKinds.def"
648 }
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000649
David Blaikie8a40f702012-01-17 06:56:22 +0000650 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregor71395fa2009-11-04 00:56:37 +0000651}
Douglas Gregor813a0662015-06-19 18:14:38 +0000652
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000653StringRef clang::getNullabilitySpelling(NullabilityKind kind,
654 bool isContextSensitive) {
Douglas Gregor813a0662015-06-19 18:14:38 +0000655 switch (kind) {
656 case NullabilityKind::NonNull:
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000657 return isContextSensitive ? "nonnull" : "_Nonnull";
Douglas Gregor813a0662015-06-19 18:14:38 +0000658
659 case NullabilityKind::Nullable:
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000660 return isContextSensitive ? "nullable" : "_Nullable";
Douglas Gregor813a0662015-06-19 18:14:38 +0000661
662 case NullabilityKind::Unspecified:
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000663 return isContextSensitive ? "null_unspecified" : "_Null_unspecified";
Douglas Gregor813a0662015-06-19 18:14:38 +0000664 }
Nico Weber708aabd2015-06-20 00:06:30 +0000665 llvm_unreachable("Unknown nullability kind.");
Douglas Gregor813a0662015-06-19 18:14:38 +0000666}