blob: 613b43fce95f0e927ac848c0fe885de6dc2d9139 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the IdentifierInfo, IdentifierVisitor, and
11// IdentifierTable interfaces.
12//
13//===----------------------------------------------------------------------===//
14
Adrian Prantl80e8ea92013-06-07 22:29:12 +000015#include "clang/Basic/CharInfo.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070016#include "clang/Basic/IdentifierTable.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070017#include "clang/Basic/LangOptions.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070018#include "clang/Basic/OperatorKinds.h"
Chris Lattner85994262007-10-05 20:15:24 +000019#include "llvm/ADT/DenseMap.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "llvm/ADT/FoldingSet.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070021#include "llvm/ADT/SmallString.h"
David Blaikie7530c032012-01-17 06:56:22 +000022#include "llvm/Support/ErrorHandling.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner3daed522009-03-02 22:20:04 +000024#include <cstdio>
Ted Kremenekc637e6b2007-10-23 22:18:37 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// IdentifierInfo Implementation
30//===----------------------------------------------------------------------===//
31
Ted Kremenekea9c26b2009-01-20 23:28:34 +000032IdentifierInfo::IdentifierInfo() {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 TokenID = tok::identifier;
Douglas Gregor5142af32008-11-06 16:32:23 +000034 ObjCOrBuiltinID = 0;
Chris Lattner4365a7e2007-10-07 07:09:52 +000035 HasMacro = false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +000036 HadMacro = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 IsExtension = false;
Richard Smith98d86b92011-10-11 19:57:52 +000038 IsCXX11CompatKeyword = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 IsPoisoned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000040 IsCPPOperatorKeyword = false;
Chris Lattner6a170eb2009-01-21 07:43:11 +000041 NeedsHandleIdentifier = false;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000042 IsFromAST = false;
Douglas Gregoreee242f2011-10-27 09:33:13 +000043 ChangedAfterLoad = false;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +000044 RevertedTokenID = false;
Douglas Gregoreee242f2011-10-27 09:33:13 +000045 OutOfDate = false;
Ted Kremenek32ad2ee2012-03-01 22:07:04 +000046 IsModulesImport = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070047 FETokenInfo = nullptr;
48 Entry = nullptr;
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
Reid Spencer5f016e22007-07-11 17:01:13 +000051//===----------------------------------------------------------------------===//
52// IdentifierTable Implementation
53//===----------------------------------------------------------------------===//
54
Douglas Gregor95f42922010-10-14 22:11:03 +000055IdentifierIterator::~IdentifierIterator() { }
56
Ted Kremenek72b1b152009-01-15 18:47:46 +000057IdentifierInfoLookup::~IdentifierInfoLookup() {}
58
Douglas Gregor95f42922010-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:
Stephen Hines651f13c2014-04-23 16:59:28 -070065 StringRef Next() override { return StringRef(); }
Douglas Gregor95f42922010-10-14 22:11:03 +000066 };
67}
68
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +000069IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
Douglas Gregor95f42922010-10-14 22:11:03 +000070 return new EmptyLookupIterator();
71}
72
Douglas Gregor8c5a7602009-04-25 23:30:02 +000073ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
74
Ted Kremenek72b1b152009-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) {
Reid Spencer5f016e22007-07-11 17:01:13 +000079
80 // Populate the identifier table with info about keywords for the current
81 // language.
82 AddKeywords(LangOpts);
Ted Kremenekf15e1142012-03-01 22:53:32 +000083
84
85 // Add the '_experimental_modules_import' contextual keyword.
Douglas Gregor1b257af2012-12-11 22:11:52 +000086 get("import").setModulesImport(true);
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
89//===----------------------------------------------------------------------===//
90// Language Keyword Implementation
91//===----------------------------------------------------------------------===//
92
Eli Friedmaneb32fde2009-04-28 03:13:54 +000093// Constants for TokenKinds.def
94namespace {
95 enum {
Dylan Noblesmith67922922011-04-09 13:34:05 +000096 KEYC99 = 0x1,
97 KEYCXX = 0x2,
Richard Smith4e24f0f2013-01-02 12:01:23 +000098 KEYCXX11 = 0x4,
Dylan Noblesmith67922922011-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 Kramerffbe9b92011-12-23 17:00:35 +0000106 KEYC11 = 0x400,
John McCallf85e1932011-06-15 23:02:42 +0000107 KEYARC = 0x800,
Francois Picheta5a4cba2012-07-24 06:17:24 +0000108 KEYNOMS = 0x01000,
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000109 WCHARSUPPORT = 0x02000,
Stephen Hines651f13c2014-04-23 16:59:28 -0700110 HALFSUPPORT = 0x04000,
Francois Picheta5a4cba2012-07-24 06:17:24 +0000111 KEYALL = (0xffff & ~KEYNOMS) // Because KEYNOMS is used to exclude.
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000112 };
Stephen Hines176edba2014-12-01 14:53:08 -0800113
114 /// \brief How a keyword is treated in the selected standard.
115 enum KeywordStatus {
116 KS_Disabled, // Disabled
117 KS_Extension, // Is an extension
118 KS_Enabled, // Enabled
119 KS_Future // Is a keyword in future standard
120 };
121}
122
123/// \brief Translates flags as specified in TokenKinds.def into keyword status
124/// in the given language standard.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700125static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
Stephen Hines176edba2014-12-01 14:53:08 -0800126 unsigned Flags) {
127 if (Flags == KEYALL) return KS_Enabled;
128 if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled;
129 if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled;
130 if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled;
131 if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension;
132 if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension;
133 if (LangOpts.Borland && (Flags & KEYBORLAND)) return KS_Extension;
134 if (LangOpts.Bool && (Flags & BOOLSUPPORT)) return KS_Enabled;
135 if (LangOpts.Half && (Flags & HALFSUPPORT)) return KS_Enabled;
136 if (LangOpts.WChar && (Flags & WCHARSUPPORT)) return KS_Enabled;
137 if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) return KS_Enabled;
138 if (LangOpts.OpenCL && (Flags & KEYOPENCL)) return KS_Enabled;
139 if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled;
140 if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled;
141 // We treat bridge casts as objective-C keywords so we can warn on them
142 // in non-arc mode.
143 if (LangOpts.ObjC2 && (Flags & KEYARC)) return KS_Enabled;
144 if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) return KS_Future;
145 return KS_Disabled;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000146}
147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148/// AddKeyword - This method is used to associate a token ID with specific
149/// identifiers because they are language keywords. This causes the lexer to
150/// automatically map matching identifiers to specialized token codes.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000151static void AddKeyword(StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000152 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 const LangOptions &LangOpts, IdentifierTable &Table) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700154 KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
Richard Smith98d86b92011-10-11 19:57:52 +0000155
Stephen Hines651f13c2014-04-23 16:59:28 -0700156 // Don't add this keyword under MSVCCompat.
157 if (LangOpts.MSVCCompat && (Flags & KEYNOMS))
Francois Pichetdfd110c2012-07-22 11:32:41 +0000158 return;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000159 // Don't add this keyword if disabled in this language.
Stephen Hines176edba2014-12-01 14:53:08 -0800160 if (AddResult == KS_Disabled) return;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000161
Richard Smith98d86b92011-10-11 19:57:52 +0000162 IdentifierInfo &Info =
Stephen Hines176edba2014-12-01 14:53:08 -0800163 Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode);
164 Info.setIsExtensionToken(AddResult == KS_Extension);
165 Info.setIsCXX11CompatKeyword(AddResult == KS_Future);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166}
167
Reid Spencer5f016e22007-07-11 17:01:13 +0000168/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
169/// representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000170static void AddCXXOperatorKeyword(StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 tok::TokenKind TokenCode,
172 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000173 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000174 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000175}
176
James Dennett6557d132012-06-15 21:27:44 +0000177/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
178/// or "property".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000179static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000180 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000182 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000183}
184
185/// AddKeywords - Add all keywords to the symbol table.
186///
187void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // Add keywords and tokens for the current language.
189#define KEYWORD(NAME, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000190 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000191 FLAGS, LangOpts, *this);
192#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000193 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000194 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000195#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
196 if (LangOpts.CXXOperatorNames) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198#define OBJC1_AT_KEYWORD(NAME) \
199 if (LangOpts.ObjC1) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000200 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000201#define OBJC2_AT_KEYWORD(NAME) \
202 if (LangOpts.ObjC2) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000203 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000204#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000205#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000206
207 if (LangOpts.ParseUnknownAnytype)
208 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
209 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700212/// \brief Checks if the specified token kind represents a keyword in the
213/// specified language.
214/// \returns Status of the keyword in the language.
215static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
216 tok::TokenKind K) {
217 switch (K) {
218#define KEYWORD(NAME, FLAGS) \
219 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
220#include "clang/Basic/TokenKinds.def"
221 default: return KS_Disabled;
222 }
223}
224
225/// \brief Returns true if the identifier represents a keyword in the
226/// specified language.
227bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) {
228 switch (getTokenKwStatus(LangOpts, getTokenID())) {
229 case KS_Enabled:
230 case KS_Extension:
231 return true;
232 default:
233 return false;
234 }
235}
236
Chris Lattner387b98d2007-10-07 07:52:34 +0000237tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
238 // We use a perfect hash function here involving the length of the keyword,
239 // the first and third character. For preprocessor ID's there are no
240 // collisions (if there were, the switch below would complain about duplicate
241 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Chris Lattner387b98d2007-10-07 07:52:34 +0000243#define HASH(LEN, FIRST, THIRD) \
244 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
245#define CASE(LEN, FIRST, THIRD, NAME) \
246 case HASH(LEN, FIRST, THIRD): \
247 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattner387b98d2007-10-07 07:52:34 +0000249 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000250 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000251 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000252 switch (HASH(Len, Name[0], Name[2])) {
253 default: return tok::pp_not_keyword;
254 CASE( 2, 'i', '\0', if);
255 CASE( 4, 'e', 'i', elif);
256 CASE( 4, 'e', 's', else);
257 CASE( 4, 'l', 'n', line);
258 CASE( 4, 's', 'c', sccs);
259 CASE( 5, 'e', 'd', endif);
260 CASE( 5, 'e', 'r', error);
261 CASE( 5, 'i', 'e', ident);
262 CASE( 5, 'i', 'd', ifdef);
263 CASE( 5, 'u', 'd', undef);
264
265 CASE( 6, 'a', 's', assert);
266 CASE( 6, 'd', 'f', define);
267 CASE( 6, 'i', 'n', ifndef);
268 CASE( 6, 'i', 'p', import);
269 CASE( 6, 'p', 'a', pragma);
Douglas Gregor94ad28b2012-01-03 18:24:14 +0000270
Chris Lattner387b98d2007-10-07 07:52:34 +0000271 CASE( 7, 'd', 'f', defined);
272 CASE( 7, 'i', 'c', include);
273 CASE( 7, 'w', 'r', warning);
274
275 CASE( 8, 'u', 'a', unassert);
276 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Douglas Gregor1ac13c32012-01-03 19:48:16 +0000278 CASE(14, '_', 'p', __public_macro);
279
280 CASE(15, '_', 'p', __private_macro);
281
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000282 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000283#undef CASE
284#undef HASH
285 }
286}
Reid Spencer5f016e22007-07-11 17:01:13 +0000287
288//===----------------------------------------------------------------------===//
289// Stats Implementation
290//===----------------------------------------------------------------------===//
291
292/// PrintStats - Print statistics about how well the identifier table is doing
293/// at hashing identifiers.
294void IdentifierTable::PrintStats() const {
295 unsigned NumBuckets = HashTable.getNumBuckets();
296 unsigned NumIdentifiers = HashTable.getNumItems();
297 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
298 unsigned AverageIdentifierSize = 0;
299 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000302 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
304 unsigned IdLen = I->getKeyLength();
305 AverageIdentifierSize += IdLen;
306 if (MaxIdentifierLength < IdLen)
307 MaxIdentifierLength = IdLen;
308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 fprintf(stderr, "\n*** Identifier Table Stats:\n");
311 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
312 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
313 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
314 NumIdentifiers/(double)NumBuckets);
315 fprintf(stderr, "Ave identifier length: %f\n",
316 (AverageIdentifierSize/(double)NumIdentifiers));
317 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Compute statistics about the memory allocated for identifiers.
320 HashTable.getAllocator().PrintStats();
321}
Steve Naroff68d331a2007-09-27 14:38:14 +0000322
Steve Naroff29238a02007-10-05 18:42:47 +0000323//===----------------------------------------------------------------------===//
324// SelectorTable Implementation
325//===----------------------------------------------------------------------===//
326
Chris Lattner85994262007-10-05 20:15:24 +0000327unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
328 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
329}
330
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000331namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000332/// MultiKeywordSelector - One of these variable length records is kept for each
333/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000334/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000335/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000336class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000337 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000338 MultiKeywordSelector(unsigned nKeys) {
339 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341public:
Steve Naroff29238a02007-10-05 18:42:47 +0000342 // Constructor for keyword selectors.
343 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
344 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000345 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Steve Naroff29238a02007-10-05 18:42:47 +0000347 // Fill in the trailing keyword array.
348 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
349 for (unsigned i = 0; i != nKeys; ++i)
350 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000351 }
352
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000353 // getName - Derive the full selector name and return it.
354 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000356 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Steve Naroff29238a02007-10-05 18:42:47 +0000358 typedef IdentifierInfo *const *keyword_iterator;
359 keyword_iterator keyword_begin() const {
360 return reinterpret_cast<keyword_iterator>(this+1);
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362 keyword_iterator keyword_end() const {
363 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000364 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000365 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000366 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000367 return keyword_begin()[i];
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000370 keyword_iterator ArgTys, unsigned NumArgs) {
371 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000372 for (unsigned i = 0; i != NumArgs; ++i)
373 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000374 }
375 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000376 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000377 }
378};
Chris Lattner85994262007-10-05 20:15:24 +0000379} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000380
381unsigned Selector::getNumArgs() const {
382 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor51603be2012-05-04 18:24:37 +0000383 if (IIF <= ZeroArg)
Steve Naroff29238a02007-10-05 18:42:47 +0000384 return 0;
385 if (IIF == OneArg)
386 return 1;
Douglas Gregor51603be2012-05-04 18:24:37 +0000387 // We point to a MultiKeywordSelector.
388 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump1eb44332009-09-09 15:08:12 +0000389 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000390}
391
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000392IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor51603be2012-05-04 18:24:37 +0000393 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000394 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000395 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000396 }
Douglas Gregor51603be2012-05-04 18:24:37 +0000397 // We point to a MultiKeywordSelector.
398 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroff29238a02007-10-05 18:42:47 +0000399 return SI->getIdentifierInfoForSlot(argIndex);
400}
401
Chris Lattner5f9e2722011-07-23 10:55:15 +0000402StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregor813d8342011-02-18 22:29:55 +0000403 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000404 return II? II->getName() : StringRef();
Douglas Gregor813d8342011-02-18 22:29:55 +0000405}
406
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000407std::string MultiKeywordSelector::getName() const {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000408 SmallString<256> Str;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000409 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000410 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
411 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000412 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000413 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000416 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000417}
418
Chris Lattner077bf5e2008-11-24 03:33:13 +0000419std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000420 if (InfoPtr == 0)
421 return "<null selector>";
422
Douglas Gregor51603be2012-05-04 18:24:37 +0000423 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000424 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Ted Kremenek150ec292009-03-07 01:22:02 +0000426 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000427 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000428 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000429
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000430 if (!II)
431 return ":";
432
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000433 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Douglas Gregor51603be2012-05-04 18:24:37 +0000436 // We have a multiple keyword selector.
437 return getMultiKeywordSelector()->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000438}
439
Stephen Hines651f13c2014-04-23 16:59:28 -0700440void Selector::print(llvm::raw_ostream &OS) const {
441 OS << getAsString();
442}
443
John McCall85f3d762011-03-02 01:50:55 +0000444/// Interpreting the given string using the normal CamelCase
445/// conventions, determine whether the given string starts with the
446/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000447static bool startsWithWord(StringRef name, StringRef word) {
John McCall85f3d762011-03-02 01:50:55 +0000448 if (name.size() < word.size()) return false;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000449 return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
450 name.startswith(word));
John McCall85f3d762011-03-02 01:50:55 +0000451}
452
453ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
454 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
455 if (!first) return OMF_None;
456
Chris Lattner5f9e2722011-07-23 10:55:15 +0000457 StringRef name = first->getName();
John McCall85f3d762011-03-02 01:50:55 +0000458 if (sel.isUnarySelector()) {
459 if (name == "autorelease") return OMF_autorelease;
460 if (name == "dealloc") return OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000461 if (name == "finalize") return OMF_finalize;
John McCall85f3d762011-03-02 01:50:55 +0000462 if (name == "release") return OMF_release;
463 if (name == "retain") return OMF_retain;
464 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000465 if (name == "self") return OMF_self;
Stephen Hines176edba2014-12-01 14:53:08 -0800466 if (name == "initialize") return OMF_initialize;
John McCall85f3d762011-03-02 01:50:55 +0000467 }
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000468
469 if (name == "performSelector") return OMF_performSelector;
John McCall85f3d762011-03-02 01:50:55 +0000470
471 // The other method families may begin with a prefix of underscores.
472 while (!name.empty() && name.front() == '_')
473 name = name.substr(1);
474
475 if (name.empty()) return OMF_None;
476 switch (name.front()) {
477 case 'a':
478 if (startsWithWord(name, "alloc")) return OMF_alloc;
479 break;
480 case 'c':
481 if (startsWithWord(name, "copy")) return OMF_copy;
482 break;
483 case 'i':
484 if (startsWithWord(name, "init")) return OMF_init;
485 break;
486 case 'm':
487 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
488 break;
489 case 'n':
490 if (startsWithWord(name, "new")) return OMF_new;
491 break;
492 default:
493 break;
494 }
495
496 return OMF_None;
497}
Steve Naroff29238a02007-10-05 18:42:47 +0000498
Fariborz Jahanian11638f72013-07-23 22:42:28 +0000499ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000500 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
501 if (!first) return OIT_None;
502
503 StringRef name = first->getName();
504
505 if (name.empty()) return OIT_None;
506 switch (name.front()) {
507 case 'a':
Fariborz Jahaniand0f29212013-08-29 16:22:26 +0000508 if (startsWithWord(name, "array")) return OIT_Array;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000509 break;
510 case 'd':
Fariborz Jahanian9fcbd5e2013-10-10 18:23:13 +0000511 if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000512 if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
513 break;
Fariborz Jahanian8d3794e2013-08-02 20:54:18 +0000514 case 's':
Fariborz Jahanian9fcbd5e2013-10-10 18:23:13 +0000515 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
516 if (startsWithWord(name, "standard")) return OIT_Singleton;
Fariborz Jahaniana346eb12013-09-18 20:35:47 +0000517 case 'i':
518 if (startsWithWord(name, "init")) return OIT_Init;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000519 default:
520 break;
521 }
522 return OIT_None;
523}
524
Stephen Hines176edba2014-12-01 14:53:08 -0800525ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
526 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
527 if (!first) return SFF_None;
528
529 StringRef name = first->getName();
530
531 switch (name.front()) {
532 case 'a':
533 if (name == "appendFormat") return SFF_NSString;
534 break;
535
536 case 'i':
537 if (name == "initWithFormat") return SFF_NSString;
538 break;
539
540 case 'l':
541 if (name == "localizedStringWithFormat") return SFF_NSString;
542 break;
543
544 case 's':
545 if (name == "stringByAppendingFormat" ||
546 name == "stringWithFormat") return SFF_NSString;
547 break;
548 }
549 return SFF_None;
550}
551
Chris Lattner5f7d2282009-03-04 05:35:38 +0000552namespace {
553 struct SelectorTableImpl {
554 llvm::FoldingSet<MultiKeywordSelector> Table;
555 llvm::BumpPtrAllocator Allocator;
556 };
557} // end anonymous namespace.
558
559static SelectorTableImpl &getSelectorTableImpl(void *P) {
560 return *static_cast<SelectorTableImpl*>(P);
561}
562
Adrian Prantl70446682013-06-10 21:36:55 +0000563SmallString<64>
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000564SelectorTable::constructSetterName(StringRef Name) {
Adrian Prantl70446682013-06-10 21:36:55 +0000565 SmallString<64> SetterName("set");
566 SetterName += Name;
567 SetterName[3] = toUppercase(SetterName[3]);
568 return SetterName;
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000569}
570
Adrian Prantl70446682013-06-10 21:36:55 +0000571Selector
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000572SelectorTable::constructSetterSelector(IdentifierTable &Idents,
573 SelectorTable &SelTable,
574 const IdentifierInfo *Name) {
575 IdentifierInfo *SetterName =
576 &Idents.get(constructSetterName(Name->getName()));
Benjamin Kramer8fe83e12012-02-04 13:45:25 +0000577 return SelTable.getUnarySelector(SetterName);
578}
579
Ted Kremenek97f55d62011-04-18 22:47:04 +0000580size_t SelectorTable::getTotalMemory() const {
581 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
582 return SelTabImpl.Allocator.getTotalMemory();
583}
Chris Lattner5f7d2282009-03-04 05:35:38 +0000584
Chris Lattnerff384912007-10-07 02:00:24 +0000585Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
586 if (nKeys < 2)
587 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Chris Lattner5f7d2282009-03-04 05:35:38 +0000589 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Steve Naroff29238a02007-10-05 18:42:47 +0000591 // Unique selector, to guarantee there is one per name.
592 llvm::FoldingSetNodeID ID;
593 MultiKeywordSelector::Profile(ID, IIV, nKeys);
594
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700595 void *InsertPos = nullptr;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000596 if (MultiKeywordSelector *SI =
597 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000598 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Steve Naroff29238a02007-10-05 18:42:47 +0000600 // MultiKeywordSelector objects are not allocated with new because they have a
601 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000602 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
603 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000604 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000605 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000606 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000607 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000608 return Selector(SI);
609}
610
Steve Naroff29238a02007-10-05 18:42:47 +0000611SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000612 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000613}
614
615SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000616 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000617}
618
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000619const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
620 switch (Operator) {
621 case OO_None:
622 case NUM_OVERLOADED_OPERATORS:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700623 return nullptr;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000624
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000625#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
626 case OO_##Name: return Spelling;
627#include "clang/Basic/OperatorKinds.def"
628 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000629
David Blaikie7530c032012-01-17 06:56:22 +0000630 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000631}