blob: bd2840db4c70e896e9ba9d3d78704434f15e0bd9 [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"
Chris Lattnerdadc7622007-10-05 20:15:24 +000019#include "llvm/ADT/DenseMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "llvm/ADT/FoldingSet.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000021#include "llvm/ADT/SmallString.h"
David Blaikie8a40f702012-01-17 06:56:22 +000022#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000024#include <cstdio>
Ted Kremenekf25f4a32007-10-23 22:18:37 +000025
Chris Lattner22eb9722006-06-18 05:43:12 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000029// IdentifierInfo Implementation
Chris Lattner22eb9722006-06-18 05:43:12 +000030//===----------------------------------------------------------------------===//
31
Ted Kremenek52f73ca2009-01-20 23:28:34 +000032IdentifierInfo::IdentifierInfo() {
Chris Lattner3bc804e2006-10-28 23:46:24 +000033 TokenID = tok::identifier;
Douglas Gregor2ad7ee92008-11-06 16:32:23 +000034 ObjCOrBuiltinID = 0;
Chris Lattnera441ca62007-10-07 07:09:52 +000035 HasMacro = false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +000036 HadMacro = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000037 IsExtension = false;
Richard Smith4dd85d62011-10-11 19:57:52 +000038 IsCXX11CompatKeyword = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000039 IsPoisoned = false;
Chris Lattner5b9f4892006-11-21 17:23:33 +000040 IsCPPOperatorKeyword = false;
Chris Lattnerad89ec02009-01-21 07:43:11 +000041 NeedsHandleIdentifier = false;
Sebastian Redld44cd6a2010-08-18 23:57:06 +000042 IsFromAST = false;
Douglas Gregor935bc7a22011-10-27 09:33:13 +000043 ChangedAfterLoad = false;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +000044 RevertedTokenID = false;
Douglas Gregor935bc7a22011-10-27 09:33:13 +000045 OutOfDate = false;
Ted Kremenekc1e4dd02012-03-01 22:07:04 +000046 IsModulesImport = false;
Craig Topperf1186c52014-05-08 06:41:40 +000047 FETokenInfo = nullptr;
48 Entry = nullptr;
Chris Lattner3bc804e2006-10-28 23:46:24 +000049}
50
Chris Lattner91cbf112006-07-03 04:28:52 +000051//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +000052// IdentifierTable Implementation
53//===----------------------------------------------------------------------===//
54
Douglas Gregor57756ea2010-10-14 22:11:03 +000055IdentifierIterator::~IdentifierIterator() { }
56
Ted Kremeneka705b042009-01-15 18:47:46 +000057IdentifierInfoLookup::~IdentifierInfoLookup() {}
58
Douglas Gregor57756ea2010-10-14 22:11:03 +000059namespace {
60 /// \brief A simple identifier lookup iterator that represents an
61 /// empty sequence of identifiers.
62 class EmptyLookupIterator : public IdentifierIterator
63 {
64 public:
Craig Topper3164f332014-03-11 03:39:26 +000065 StringRef Next() override { return StringRef(); }
Douglas Gregor57756ea2010-10-14 22:11:03 +000066 };
67}
68
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +000069IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
Douglas Gregor57756ea2010-10-14 22:11:03 +000070 return new EmptyLookupIterator();
71}
72
Douglas Gregor99734e72009-04-25 23:30:02 +000073ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
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,
112 KEYALL = (0xffff & ~KEYNOMS18 &
113 ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
Eli Friedman2b680b42009-04-28 03:13:54 +0000114 };
Serge Pavlov77af3812014-10-29 10:59:18 +0000115
116 /// \brief How a keyword is treated in the selected standard.
117 enum KeywordStatus {
118 KS_Disabled, // Disabled
119 KS_Extension, // Is an extension
120 KS_Enabled, // Enabled
121 KS_Future // Is a keyword in future standard
122 };
123}
124
125/// \brief Translates flags as specified in TokenKinds.def into keyword status
126/// in the given language standard.
Serge Pavlov83cf0782014-12-11 12:18:08 +0000127static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
Serge Pavlov77af3812014-10-29 10:59:18 +0000128 unsigned Flags) {
129 if (Flags == KEYALL) return KS_Enabled;
130 if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled;
131 if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled;
132 if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled;
133 if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension;
134 if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension;
135 if (LangOpts.Borland && (Flags & KEYBORLAND)) return KS_Extension;
136 if (LangOpts.Bool && (Flags & BOOLSUPPORT)) return KS_Enabled;
137 if (LangOpts.Half && (Flags & HALFSUPPORT)) return KS_Enabled;
138 if (LangOpts.WChar && (Flags & WCHARSUPPORT)) return KS_Enabled;
139 if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) return KS_Enabled;
140 if (LangOpts.OpenCL && (Flags & KEYOPENCL)) return KS_Enabled;
141 if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled;
142 if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled;
143 // We treat bridge casts as objective-C keywords so we can warn on them
144 // in non-arc mode.
145 if (LangOpts.ObjC2 && (Flags & KEYARC)) return KS_Enabled;
146 if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) return KS_Future;
147 return KS_Disabled;
Eli Friedman2b680b42009-04-28 03:13:54 +0000148}
149
Chris Lattner25e0d542006-10-18 06:07:05 +0000150/// AddKeyword - This method is used to associate a token ID with specific
151/// identifiers because they are language keywords. This causes the lexer to
152/// automatically map matching identifiers to specialized token codes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000153static void AddKeyword(StringRef Keyword,
Eli Friedman2b680b42009-04-28 03:13:54 +0000154 tok::TokenKind TokenCode, unsigned Flags,
Chris Lattner25e0d542006-10-18 06:07:05 +0000155 const LangOptions &LangOpts, IdentifierTable &Table) {
Serge Pavlov83cf0782014-12-11 12:18:08 +0000156 KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
Richard Smith4dd85d62011-10-11 19:57:52 +0000157
Alp Tokerbfa39342014-01-14 12:51:41 +0000158 // Don't add this keyword under MSVCCompat.
David Majnemer28aae9c2015-03-18 04:15:23 +0000159 if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) &&
160 !LangOpts.isCompatibleWithMSVC(19))
Anastasia Stulovab1152f12015-03-18 12:55:29 +0000161 return;
162
163 // Don't add this keyword under OpenCL.
164 if (LangOpts.OpenCL && (Flags & KEYNOOPENCL))
165 return;
166
Eli Friedman2b680b42009-04-28 03:13:54 +0000167 // Don't add this keyword if disabled in this language.
Serge Pavlov77af3812014-10-29 10:59:18 +0000168 if (AddResult == KS_Disabled) return;
Eli Friedman2b680b42009-04-28 03:13:54 +0000169
Richard Smith4dd85d62011-10-11 19:57:52 +0000170 IdentifierInfo &Info =
Serge Pavlov77af3812014-10-29 10:59:18 +0000171 Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode);
172 Info.setIsExtensionToken(AddResult == KS_Extension);
173 Info.setIsCXX11CompatKeyword(AddResult == KS_Future);
Chris Lattner25e0d542006-10-18 06:07:05 +0000174}
175
Chris Lattner5b9f4892006-11-21 17:23:33 +0000176/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
177/// representations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000178static void AddCXXOperatorKeyword(StringRef Keyword,
Chris Lattner5b9f4892006-11-21 17:23:33 +0000179 tok::TokenKind TokenCode,
180 IdentifierTable &Table) {
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000181 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekf25f4a32007-10-23 22:18:37 +0000182 Info.setIsCPlusPlusOperatorKeyword();
Chris Lattner5b9f4892006-11-21 17:23:33 +0000183}
184
James Dennett0d8a3f82012-06-15 21:27:44 +0000185/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
186/// or "property".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000187static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000188 tok::ObjCKeywordKind ObjCID,
Chris Lattner25e0d542006-10-18 06:07:05 +0000189 IdentifierTable &Table) {
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000190 Table.get(Name).setObjCKeywordID(ObjCID);
Chris Lattner25e0d542006-10-18 06:07:05 +0000191}
192
193/// AddKeywords - Add all keywords to the symbol table.
194///
195void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Chris Lattner25e0d542006-10-18 06:07:05 +0000196 // Add keywords and tokens for the current language.
197#define KEYWORD(NAME, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000198 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000199 FLAGS, LangOpts, *this);
200#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000201 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000202 FLAGS, LangOpts, *this);
Chris Lattner5b9f4892006-11-21 17:23:33 +0000203#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
Chris Lattner3e7592e2006-12-04 07:48:37 +0000204 if (LangOpts.CXXOperatorNames) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000205 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000206#define OBJC1_AT_KEYWORD(NAME) \
207 if (LangOpts.ObjC1) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000208 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000209#define OBJC2_AT_KEYWORD(NAME) \
210 if (LangOpts.ObjC2) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000211 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCall39439732011-04-09 22:50:59 +0000212#define TESTING_KEYWORD(NAME, FLAGS)
Chris Lattner25e0d542006-10-18 06:07:05 +0000213#include "clang/Basic/TokenKinds.def"
John McCall39439732011-04-09 22:50:59 +0000214
215 if (LangOpts.ParseUnknownAnytype)
216 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
217 LangOpts, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000218}
219
Serge Pavlov83cf0782014-12-11 12:18:08 +0000220/// \brief Checks if the specified token kind represents a keyword in the
221/// specified language.
222/// \returns Status of the keyword in the language.
223static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
224 tok::TokenKind K) {
225 switch (K) {
226#define KEYWORD(NAME, FLAGS) \
227 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
228#include "clang/Basic/TokenKinds.def"
229 default: return KS_Disabled;
230 }
231}
232
233/// \brief Returns true if the identifier represents a keyword in the
234/// specified language.
235bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) {
236 switch (getTokenKwStatus(LangOpts, getTokenID())) {
237 case KS_Enabled:
238 case KS_Extension:
239 return true;
240 default:
241 return false;
242 }
243}
244
Chris Lattnerff067ce2007-10-07 07:52:34 +0000245tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
246 // We use a perfect hash function here involving the length of the keyword,
247 // the first and third character. For preprocessor ID's there are no
248 // collisions (if there were, the switch below would complain about duplicate
249 // case values). Note that this depends on 'if' being null terminated.
Mike Stump11289f42009-09-09 15:08:12 +0000250
Chris Lattnerff067ce2007-10-07 07:52:34 +0000251#define HASH(LEN, FIRST, THIRD) \
252 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
253#define CASE(LEN, FIRST, THIRD, NAME) \
254 case HASH(LEN, FIRST, THIRD): \
255 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump11289f42009-09-09 15:08:12 +0000256
Chris Lattnerff067ce2007-10-07 07:52:34 +0000257 unsigned Len = getLength();
Chris Lattnerd2b8ce42007-10-10 20:59:57 +0000258 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000259 const char *Name = getNameStart();
Chris Lattnerff067ce2007-10-07 07:52:34 +0000260 switch (HASH(Len, Name[0], Name[2])) {
261 default: return tok::pp_not_keyword;
262 CASE( 2, 'i', '\0', if);
263 CASE( 4, 'e', 'i', elif);
264 CASE( 4, 'e', 's', else);
265 CASE( 4, 'l', 'n', line);
266 CASE( 4, 's', 'c', sccs);
267 CASE( 5, 'e', 'd', endif);
268 CASE( 5, 'e', 'r', error);
269 CASE( 5, 'i', 'e', ident);
270 CASE( 5, 'i', 'd', ifdef);
271 CASE( 5, 'u', 'd', undef);
272
273 CASE( 6, 'a', 's', assert);
274 CASE( 6, 'd', 'f', define);
275 CASE( 6, 'i', 'n', ifndef);
276 CASE( 6, 'i', 'p', import);
277 CASE( 6, 'p', 'a', pragma);
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000278
Chris Lattnerff067ce2007-10-07 07:52:34 +0000279 CASE( 7, 'd', 'f', defined);
280 CASE( 7, 'i', 'c', include);
281 CASE( 7, 'w', 'r', warning);
282
283 CASE( 8, 'u', 'a', unassert);
284 CASE(12, 'i', 'c', include_next);
Mike Stump11289f42009-09-09 15:08:12 +0000285
Douglas Gregor663b48f2012-01-03 19:48:16 +0000286 CASE(14, '_', 'p', __public_macro);
287
288 CASE(15, '_', 'p', __private_macro);
289
Chris Lattner14a7f392009-04-08 18:24:34 +0000290 CASE(16, '_', 'i', __include_macros);
Chris Lattnerff067ce2007-10-07 07:52:34 +0000291#undef CASE
292#undef HASH
293 }
294}
Chris Lattner25e0d542006-10-18 06:07:05 +0000295
296//===----------------------------------------------------------------------===//
297// Stats Implementation
298//===----------------------------------------------------------------------===//
299
Chris Lattner22eb9722006-06-18 05:43:12 +0000300/// PrintStats - Print statistics about how well the identifier table is doing
301/// at hashing identifiers.
302void IdentifierTable::PrintStats() const {
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000303 unsigned NumBuckets = HashTable.getNumBuckets();
304 unsigned NumIdentifiers = HashTable.getNumItems();
305 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
Chris Lattner22eb9722006-06-18 05:43:12 +0000306 unsigned AverageIdentifierSize = 0;
307 unsigned MaxIdentifierLength = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000309 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000310 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000311 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
312 unsigned IdLen = I->getKeyLength();
313 AverageIdentifierSize += IdLen;
314 if (MaxIdentifierLength < IdLen)
315 MaxIdentifierLength = IdLen;
316 }
Mike Stump11289f42009-09-09 15:08:12 +0000317
Chris Lattner23b7eb62007-06-15 23:05:46 +0000318 fprintf(stderr, "\n*** Identifier Table Stats:\n");
319 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
320 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
321 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
322 NumIdentifiers/(double)NumBuckets);
323 fprintf(stderr, "Ave identifier length: %f\n",
324 (AverageIdentifierSize/(double)NumIdentifiers));
325 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattner22eb9722006-06-18 05:43:12 +0000327 // Compute statistics about the memory allocated for identifiers.
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000328 HashTable.getAllocator().PrintStats();
Chris Lattner22eb9722006-06-18 05:43:12 +0000329}
Steve Narofff73590d2007-09-27 14:38:14 +0000330
Steve Naroffe61bfa82007-10-05 18:42:47 +0000331//===----------------------------------------------------------------------===//
332// SelectorTable Implementation
333//===----------------------------------------------------------------------===//
334
Chris Lattnerdadc7622007-10-05 20:15:24 +0000335unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
336 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
337}
338
Douglas Gregor77324f32008-11-17 14:58:09 +0000339namespace clang {
Steve Naroffe61bfa82007-10-05 18:42:47 +0000340/// MultiKeywordSelector - One of these variable length records is kept for each
341/// selector containing more than one keyword. We use a folding set
Mike Stump11289f42009-09-09 15:08:12 +0000342/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroffe61bfa82007-10-05 18:42:47 +0000343/// this class is provided strictly through Selector.
Mike Stump11289f42009-09-09 15:08:12 +0000344class MultiKeywordSelector
Douglas Gregor77324f32008-11-17 14:58:09 +0000345 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor77324f32008-11-17 14:58:09 +0000346 MultiKeywordSelector(unsigned nKeys) {
347 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
348 }
Mike Stump11289f42009-09-09 15:08:12 +0000349public:
Steve Naroffe61bfa82007-10-05 18:42:47 +0000350 // Constructor for keyword selectors.
351 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
352 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor77324f32008-11-17 14:58:09 +0000353 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Steve Naroffe61bfa82007-10-05 18:42:47 +0000355 // Fill in the trailing keyword array.
356 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
357 for (unsigned i = 0; i != nKeys; ++i)
358 KeyInfo[i] = IIV[i];
Mike Stump11289f42009-09-09 15:08:12 +0000359 }
360
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000361 // getName - Derive the full selector name and return it.
362 std::string getName() const;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Douglas Gregor77324f32008-11-17 14:58:09 +0000364 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Steve Naroffe61bfa82007-10-05 18:42:47 +0000366 typedef IdentifierInfo *const *keyword_iterator;
367 keyword_iterator keyword_begin() const {
368 return reinterpret_cast<keyword_iterator>(this+1);
369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370 keyword_iterator keyword_end() const {
371 return keyword_begin()+getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000372 }
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000373 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor77324f32008-11-17 14:58:09 +0000374 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroffe61bfa82007-10-05 18:42:47 +0000375 return keyword_begin()[i];
376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroffe61bfa82007-10-05 18:42:47 +0000378 keyword_iterator ArgTys, unsigned NumArgs) {
379 ID.AddInteger(NumArgs);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000380 for (unsigned i = 0; i != NumArgs; ++i)
381 ID.AddPointer(ArgTys[i]);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000382 }
383 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000384 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000385 }
386};
Chris Lattnerdadc7622007-10-05 20:15:24 +0000387} // end namespace clang.
Steve Naroffe61bfa82007-10-05 18:42:47 +0000388
389unsigned Selector::getNumArgs() const {
390 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor93a586f2012-05-04 18:24:37 +0000391 if (IIF <= ZeroArg)
Steve Naroffe61bfa82007-10-05 18:42:47 +0000392 return 0;
393 if (IIF == OneArg)
394 return 1;
Douglas Gregor93a586f2012-05-04 18:24:37 +0000395 // We point to a MultiKeywordSelector.
396 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump11289f42009-09-09 15:08:12 +0000397 return SI->getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000398}
399
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000400IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor93a586f2012-05-04 18:24:37 +0000401 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000402 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor70091b82009-04-26 22:20:50 +0000403 return getAsIdentifierInfo();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000404 }
Douglas Gregor93a586f2012-05-04 18:24:37 +0000405 // We point to a MultiKeywordSelector.
406 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000407 return SI->getIdentifierInfoForSlot(argIndex);
408}
409
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000410StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +0000411 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000412 return II? II->getName() : StringRef();
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +0000413}
414
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000415std::string MultiKeywordSelector::getName() const {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000416 SmallString<256> Str;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000417 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000418 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
419 if (*I)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000420 OS << (*I)->getName();
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000421 OS << ':';
Steve Naroffe61bfa82007-10-05 18:42:47 +0000422 }
Mike Stump11289f42009-09-09 15:08:12 +0000423
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000424 return OS.str();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000425}
426
Chris Lattnere4b95692008-11-24 03:33:13 +0000427std::string Selector::getAsString() const {
Douglas Gregor70091b82009-04-26 22:20:50 +0000428 if (InfoPtr == 0)
429 return "<null selector>";
430
Douglas Gregor93a586f2012-05-04 18:24:37 +0000431 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenek5f080b42009-03-06 23:36:28 +0000432 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000433
Ted Kremenek0666a6c2009-03-07 01:22:02 +0000434 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenek5f080b42009-03-06 23:36:28 +0000435 if (getNumArgs() == 0)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000436 return II->getName();
Ted Kremenek5f080b42009-03-06 23:36:28 +0000437
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000438 if (!II)
439 return ":";
440
Daniel Dunbar07d07852009-10-18 21:17:35 +0000441 return II->getName().str() + ":";
Steve Naroffe61bfa82007-10-05 18:42:47 +0000442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Douglas Gregor93a586f2012-05-04 18:24:37 +0000444 // We have a multiple keyword selector.
445 return getMultiKeywordSelector()->getName();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000446}
447
Aaron Ballmanb190f972014-01-03 17:59:55 +0000448void Selector::print(llvm::raw_ostream &OS) const {
449 OS << getAsString();
450}
451
John McCallb4526252011-03-02 01:50:55 +0000452/// Interpreting the given string using the normal CamelCase
453/// conventions, determine whether the given string starts with the
454/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000455static bool startsWithWord(StringRef name, StringRef word) {
John McCallb4526252011-03-02 01:50:55 +0000456 if (name.size() < word.size()) return false;
Jordan Rosea7d03842013-02-08 22:30:41 +0000457 return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
458 name.startswith(word));
John McCallb4526252011-03-02 01:50:55 +0000459}
460
461ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
462 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
463 if (!first) return OMF_None;
464
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000465 StringRef name = first->getName();
John McCallb4526252011-03-02 01:50:55 +0000466 if (sel.isUnarySelector()) {
467 if (name == "autorelease") return OMF_autorelease;
468 if (name == "dealloc") return OMF_dealloc;
Nico Weber1fb82662011-08-28 22:35:17 +0000469 if (name == "finalize") return OMF_finalize;
John McCallb4526252011-03-02 01:50:55 +0000470 if (name == "release") return OMF_release;
471 if (name == "retain") return OMF_retain;
472 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor33823722011-06-11 01:09:30 +0000473 if (name == "self") return OMF_self;
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000474 if (name == "initialize") return OMF_initialize;
John McCallb4526252011-03-02 01:50:55 +0000475 }
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000476
477 if (name == "performSelector") return OMF_performSelector;
John McCallb4526252011-03-02 01:50:55 +0000478
479 // The other method families may begin with a prefix of underscores.
480 while (!name.empty() && name.front() == '_')
481 name = name.substr(1);
482
483 if (name.empty()) return OMF_None;
484 switch (name.front()) {
485 case 'a':
486 if (startsWithWord(name, "alloc")) return OMF_alloc;
487 break;
488 case 'c':
489 if (startsWithWord(name, "copy")) return OMF_copy;
490 break;
491 case 'i':
492 if (startsWithWord(name, "init")) return OMF_init;
493 break;
494 case 'm':
495 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
496 break;
497 case 'n':
498 if (startsWithWord(name, "new")) return OMF_new;
499 break;
500 default:
501 break;
502 }
503
504 return OMF_None;
505}
Steve Naroffe61bfa82007-10-05 18:42:47 +0000506
Fariborz Jahanian71221352013-07-23 22:42:28 +0000507ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000508 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
509 if (!first) return OIT_None;
510
511 StringRef name = first->getName();
512
513 if (name.empty()) return OIT_None;
514 switch (name.front()) {
515 case 'a':
Fariborz Jahanian4ccdc732013-08-29 16:22:26 +0000516 if (startsWithWord(name, "array")) return OIT_Array;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000517 break;
518 case 'd':
Fariborz Jahanian7c87b432013-10-10 18:23:13 +0000519 if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000520 if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
521 break;
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000522 case 's':
Fariborz Jahanian7c87b432013-10-10 18:23:13 +0000523 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
524 if (startsWithWord(name, "standard")) return OIT_Singleton;
Fariborz Jahanian1c900bc2013-09-18 20:35:47 +0000525 case 'i':
526 if (startsWithWord(name, "init")) return OIT_Init;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000527 default:
528 break;
529 }
530 return OIT_None;
531}
532
Fariborz Jahanian6485fe42014-09-09 23:10:54 +0000533ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
534 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
535 if (!first) return SFF_None;
536
537 StringRef name = first->getName();
538
539 switch (name.front()) {
540 case 'a':
541 if (name == "appendFormat") return SFF_NSString;
542 break;
543
544 case 'i':
545 if (name == "initWithFormat") return SFF_NSString;
546 break;
547
548 case 'l':
549 if (name == "localizedStringWithFormat") return SFF_NSString;
550 break;
551
552 case 's':
553 if (name == "stringByAppendingFormat" ||
554 name == "stringWithFormat") return SFF_NSString;
555 break;
556 }
557 return SFF_None;
558}
559
Chris Lattner1a849942009-03-04 05:35:38 +0000560namespace {
561 struct SelectorTableImpl {
562 llvm::FoldingSet<MultiKeywordSelector> Table;
563 llvm::BumpPtrAllocator Allocator;
564 };
565} // end anonymous namespace.
566
567static SelectorTableImpl &getSelectorTableImpl(void *P) {
568 return *static_cast<SelectorTableImpl*>(P);
569}
570
Adrian Prantl6e77c962013-06-10 21:36:55 +0000571SmallString<64>
Adrian Prantla4ce9062013-06-07 22:29:12 +0000572SelectorTable::constructSetterName(StringRef Name) {
Adrian Prantl6e77c962013-06-10 21:36:55 +0000573 SmallString<64> SetterName("set");
574 SetterName += Name;
575 SetterName[3] = toUppercase(SetterName[3]);
576 return SetterName;
Adrian Prantla4ce9062013-06-07 22:29:12 +0000577}
578
Adrian Prantl6e77c962013-06-10 21:36:55 +0000579Selector
Adrian Prantla4ce9062013-06-07 22:29:12 +0000580SelectorTable::constructSetterSelector(IdentifierTable &Idents,
581 SelectorTable &SelTable,
582 const IdentifierInfo *Name) {
583 IdentifierInfo *SetterName =
584 &Idents.get(constructSetterName(Name->getName()));
Benjamin Kramer49038022012-02-04 13:45:25 +0000585 return SelTable.getUnarySelector(SetterName);
586}
587
Ted Kremenek1c2239e2011-04-18 22:47:04 +0000588size_t SelectorTable::getTotalMemory() const {
589 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
590 return SelTabImpl.Allocator.getTotalMemory();
591}
Chris Lattner1a849942009-03-04 05:35:38 +0000592
Chris Lattner5700fab2007-10-07 02:00:24 +0000593Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
594 if (nKeys < 2)
595 return Selector(IIV[0], nKeys);
Mike Stump11289f42009-09-09 15:08:12 +0000596
Chris Lattner1a849942009-03-04 05:35:38 +0000597 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump11289f42009-09-09 15:08:12 +0000598
Steve Naroffe61bfa82007-10-05 18:42:47 +0000599 // Unique selector, to guarantee there is one per name.
600 llvm::FoldingSetNodeID ID;
601 MultiKeywordSelector::Profile(ID, IIV, nKeys);
602
Craig Topperf1186c52014-05-08 06:41:40 +0000603 void *InsertPos = nullptr;
Chris Lattner1a849942009-03-04 05:35:38 +0000604 if (MultiKeywordSelector *SI =
605 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe61bfa82007-10-05 18:42:47 +0000606 return Selector(SI);
Mike Stump11289f42009-09-09 15:08:12 +0000607
Steve Naroffe61bfa82007-10-05 18:42:47 +0000608 // MultiKeywordSelector objects are not allocated with new because they have a
609 // variable size array (for parameter types) at the end of them.
Chris Lattner1a849942009-03-04 05:35:38 +0000610 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
611 MultiKeywordSelector *SI =
Mike Stump11289f42009-09-09 15:08:12 +0000612 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner5c0b4052010-10-30 05:14:06 +0000613 llvm::alignOf<MultiKeywordSelector>());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000614 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner1a849942009-03-04 05:35:38 +0000615 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000616 return Selector(SI);
617}
618
Steve Naroffe61bfa82007-10-05 18:42:47 +0000619SelectorTable::SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000620 Impl = new SelectorTableImpl();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000621}
622
623SelectorTable::~SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000624 delete &getSelectorTableImpl(Impl);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000625}
626
Douglas Gregor71395fa2009-11-04 00:56:37 +0000627const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
628 switch (Operator) {
629 case OO_None:
630 case NUM_OVERLOADED_OPERATORS:
Craig Topperf1186c52014-05-08 06:41:40 +0000631 return nullptr;
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000632
Douglas Gregor71395fa2009-11-04 00:56:37 +0000633#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
634 case OO_##Name: return Spelling;
635#include "clang/Basic/OperatorKinds.def"
636 }
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000637
David Blaikie8a40f702012-01-17 06:56:22 +0000638 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregor71395fa2009-11-04 00:56:37 +0000639}