blob: 38f09a08af3c1a87693b79a3be503e8817895065 [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
Chris Lattnerc7229c32007-10-07 08:58:51 +000015#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Basic/LangOptions.h"
Steve Naroff29238a02007-10-05 18:42:47 +000017#include "llvm/ADT/FoldingSet.h"
Chris Lattner85994262007-10-05 20:15:24 +000018#include "llvm/ADT/DenseMap.h"
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +000019#include "llvm/ADT/StringRef.h"
John McCallf85e1932011-06-15 23:02:42 +000020#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar76b61cc2009-10-17 18:13:02 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner3daed522009-03-02 22:20:04 +000022#include <cstdio>
Ted Kremenekc637e6b2007-10-23 22:18:37 +000023
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// IdentifierInfo Implementation
28//===----------------------------------------------------------------------===//
29
Ted Kremenekea9c26b2009-01-20 23:28:34 +000030IdentifierInfo::IdentifierInfo() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 TokenID = tok::identifier;
Douglas Gregor5142af32008-11-06 16:32:23 +000032 ObjCOrBuiltinID = 0;
Chris Lattner4365a7e2007-10-07 07:09:52 +000033 HasMacro = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000034 IsExtension = false;
Richard Smith98d86b92011-10-11 19:57:52 +000035 IsCXX11CompatKeyword = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 IsPoisoned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 IsCPPOperatorKeyword = false;
Chris Lattner6a170eb2009-01-21 07:43:11 +000038 NeedsHandleIdentifier = false;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000039 IsFromAST = false;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +000040 RevertedTokenID = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000041 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000042 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000043}
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045//===----------------------------------------------------------------------===//
46// IdentifierTable Implementation
47//===----------------------------------------------------------------------===//
48
Douglas Gregor95f42922010-10-14 22:11:03 +000049IdentifierIterator::~IdentifierIterator() { }
50
Ted Kremenek72b1b152009-01-15 18:47:46 +000051IdentifierInfoLookup::~IdentifierInfoLookup() {}
52
Douglas Gregor95f42922010-10-14 22:11:03 +000053namespace {
54 /// \brief A simple identifier lookup iterator that represents an
55 /// empty sequence of identifiers.
56 class EmptyLookupIterator : public IdentifierIterator
57 {
58 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000059 virtual StringRef Next() { return StringRef(); }
Douglas Gregor95f42922010-10-14 22:11:03 +000060 };
61}
62
63IdentifierIterator *IdentifierInfoLookup::getIdentifiers() const {
64 return new EmptyLookupIterator();
65}
66
Douglas Gregor8c5a7602009-04-25 23:30:02 +000067ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
68
Ted Kremenek72b1b152009-01-15 18:47:46 +000069IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
70 IdentifierInfoLookup* externalLookup)
71 : HashTable(8192), // Start with space for 8K identifiers.
72 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000073
74 // Populate the identifier table with info about keywords for the current
75 // language.
76 AddKeywords(LangOpts);
77}
78
79//===----------------------------------------------------------------------===//
80// Language Keyword Implementation
81//===----------------------------------------------------------------------===//
82
Eli Friedmaneb32fde2009-04-28 03:13:54 +000083// Constants for TokenKinds.def
84namespace {
85 enum {
Dylan Noblesmith67922922011-04-09 13:34:05 +000086 KEYC99 = 0x1,
87 KEYCXX = 0x2,
88 KEYCXX0X = 0x4,
89 KEYGNU = 0x8,
90 KEYMS = 0x10,
91 BOOLSUPPORT = 0x20,
92 KEYALTIVEC = 0x40,
93 KEYNOCXX = 0x80,
94 KEYBORLAND = 0x100,
95 KEYOPENCL = 0x200,
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +000096 KEYC1X = 0x400,
John McCallf85e1932011-06-15 23:02:42 +000097 KEYARC = 0x800,
98 KEYALL = 0x0fff
Eli Friedmaneb32fde2009-04-28 03:13:54 +000099 };
100}
101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// AddKeyword - This method is used to associate a token ID with specific
103/// identifiers because they are language keywords. This causes the lexer to
104/// automatically map matching identifiers to specialized token codes.
105///
Richard Smith98d86b92011-10-11 19:57:52 +0000106/// The C90/C99/CPP/CPP0x flags are set to 3 if the token is a keyword in a
107/// future language standard, set to 2 if the token should be enabled in the
108/// specified langauge, set to 1 if it is an extension in the specified
109/// language, and set to 0 if disabled in the specified language.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000110static void AddKeyword(StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000111 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000113 unsigned AddResult = 0;
Dylan Noblesmith67922922011-04-09 13:34:05 +0000114 if (Flags == KEYALL) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000115 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
116 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
117 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000118 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000119 else if (LangOpts.MicrosoftExt && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000120 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000121 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000122 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000123 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000124 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +0000125 else if (LangOpts.C1X && (Flags & KEYC1X)) AddResult = 2;
John McCallf85e1932011-06-15 23:02:42 +0000126 else if (LangOpts.ObjCAutoRefCount && (Flags & KEYARC)) AddResult = 2;
Richard Smith98d86b92011-10-11 19:57:52 +0000127 else if (LangOpts.CPlusPlus && (Flags & KEYCXX0X)) AddResult = 3;
128
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000129 // Don't add this keyword if disabled in this language.
130 if (AddResult == 0) return;
131
Richard Smith98d86b92011-10-11 19:57:52 +0000132 IdentifierInfo &Info =
133 Table.get(Keyword, AddResult == 3 ? tok::identifier : TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000134 Info.setIsExtensionToken(AddResult == 1);
Richard Smith98d86b92011-10-11 19:57:52 +0000135 Info.setIsCXX11CompatKeyword(AddResult == 3);
Reid Spencer5f016e22007-07-11 17:01:13 +0000136}
137
Reid Spencer5f016e22007-07-11 17:01:13 +0000138/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
139/// representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000140static void AddCXXOperatorKeyword(StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 tok::TokenKind TokenCode,
142 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000143 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000144 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000145}
146
Mike Stump1eb44332009-09-09 15:08:12 +0000147/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000148/// "property".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000149static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000150 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000152 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153}
154
155/// AddKeywords - Add all keywords to the symbol table.
156///
157void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // Add keywords and tokens for the current language.
159#define KEYWORD(NAME, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000160 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000161 FLAGS, LangOpts, *this);
162#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000163 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000164 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
166 if (LangOpts.CXXOperatorNames) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000167 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168#define OBJC1_AT_KEYWORD(NAME) \
169 if (LangOpts.ObjC1) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000170 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171#define OBJC2_AT_KEYWORD(NAME) \
172 if (LangOpts.ObjC2) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000173 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000174#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000175#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000176
177 if (LangOpts.ParseUnknownAnytype)
178 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
179 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
Chris Lattner387b98d2007-10-07 07:52:34 +0000182tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
183 // We use a perfect hash function here involving the length of the keyword,
184 // the first and third character. For preprocessor ID's there are no
185 // collisions (if there were, the switch below would complain about duplicate
186 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattner387b98d2007-10-07 07:52:34 +0000188#define HASH(LEN, FIRST, THIRD) \
189 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
190#define CASE(LEN, FIRST, THIRD, NAME) \
191 case HASH(LEN, FIRST, THIRD): \
192 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Chris Lattner387b98d2007-10-07 07:52:34 +0000194 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000195 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000196 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000197 switch (HASH(Len, Name[0], Name[2])) {
198 default: return tok::pp_not_keyword;
199 CASE( 2, 'i', '\0', if);
200 CASE( 4, 'e', 'i', elif);
201 CASE( 4, 'e', 's', else);
202 CASE( 4, 'l', 'n', line);
203 CASE( 4, 's', 'c', sccs);
204 CASE( 5, 'e', 'd', endif);
205 CASE( 5, 'e', 'r', error);
206 CASE( 5, 'i', 'e', ident);
207 CASE( 5, 'i', 'd', ifdef);
208 CASE( 5, 'u', 'd', undef);
209
210 CASE( 6, 'a', 's', assert);
211 CASE( 6, 'd', 'f', define);
212 CASE( 6, 'i', 'n', ifndef);
213 CASE( 6, 'i', 'p', import);
214 CASE( 6, 'p', 'a', pragma);
215
216 CASE( 7, 'd', 'f', defined);
217 CASE( 7, 'i', 'c', include);
218 CASE( 7, 'w', 'r', warning);
219
220 CASE( 8, 'u', 'a', unassert);
221 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000223 CASE(16, '_', 'i', __include_macros);
Douglas Gregor7143aab2011-09-01 17:04:32 +0000224 CASE(16, '_', 'e', __export_macro__);
Chris Lattner387b98d2007-10-07 07:52:34 +0000225#undef CASE
226#undef HASH
227 }
228}
Reid Spencer5f016e22007-07-11 17:01:13 +0000229
230//===----------------------------------------------------------------------===//
231// Stats Implementation
232//===----------------------------------------------------------------------===//
233
234/// PrintStats - Print statistics about how well the identifier table is doing
235/// at hashing identifiers.
236void IdentifierTable::PrintStats() const {
237 unsigned NumBuckets = HashTable.getNumBuckets();
238 unsigned NumIdentifiers = HashTable.getNumItems();
239 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
240 unsigned AverageIdentifierSize = 0;
241 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000244 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
246 unsigned IdLen = I->getKeyLength();
247 AverageIdentifierSize += IdLen;
248 if (MaxIdentifierLength < IdLen)
249 MaxIdentifierLength = IdLen;
250 }
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 fprintf(stderr, "\n*** Identifier Table Stats:\n");
253 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
254 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
255 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
256 NumIdentifiers/(double)NumBuckets);
257 fprintf(stderr, "Ave identifier length: %f\n",
258 (AverageIdentifierSize/(double)NumIdentifiers));
259 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 // Compute statistics about the memory allocated for identifiers.
262 HashTable.getAllocator().PrintStats();
263}
Steve Naroff68d331a2007-09-27 14:38:14 +0000264
Steve Naroff29238a02007-10-05 18:42:47 +0000265//===----------------------------------------------------------------------===//
266// SelectorTable Implementation
267//===----------------------------------------------------------------------===//
268
Chris Lattner85994262007-10-05 20:15:24 +0000269unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
270 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
271}
272
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000273namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000274/// MultiKeywordSelector - One of these variable length records is kept for each
275/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000276/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000277/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000278class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000279 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000280 MultiKeywordSelector(unsigned nKeys) {
281 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283public:
Steve Naroff29238a02007-10-05 18:42:47 +0000284 // Constructor for keyword selectors.
285 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
286 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000287 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Steve Naroff29238a02007-10-05 18:42:47 +0000289 // Fill in the trailing keyword array.
290 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
291 for (unsigned i = 0; i != nKeys; ++i)
292 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000293 }
294
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000295 // getName - Derive the full selector name and return it.
296 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000298 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Steve Naroff29238a02007-10-05 18:42:47 +0000300 typedef IdentifierInfo *const *keyword_iterator;
301 keyword_iterator keyword_begin() const {
302 return reinterpret_cast<keyword_iterator>(this+1);
303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304 keyword_iterator keyword_end() const {
305 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000306 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000307 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000308 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000309 return keyword_begin()[i];
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000312 keyword_iterator ArgTys, unsigned NumArgs) {
313 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000314 for (unsigned i = 0; i != NumArgs; ++i)
315 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000316 }
317 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000318 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000319 }
320};
Chris Lattner85994262007-10-05 20:15:24 +0000321} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000322
323unsigned Selector::getNumArgs() const {
324 unsigned IIF = getIdentifierInfoFlag();
325 if (IIF == ZeroArg)
326 return 0;
327 if (IIF == OneArg)
328 return 1;
329 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
330 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000331 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000332}
333
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000334IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000335 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000336 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000337 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000338 }
339 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
340 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
341 return SI->getIdentifierInfoForSlot(argIndex);
342}
343
Chris Lattner5f9e2722011-07-23 10:55:15 +0000344StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregor813d8342011-02-18 22:29:55 +0000345 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000346 return II? II->getName() : StringRef();
Douglas Gregor813d8342011-02-18 22:29:55 +0000347}
348
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000349std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000350 llvm::SmallString<256> Str;
351 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000352 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
353 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000354 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000355 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000358 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000359}
360
Chris Lattner077bf5e2008-11-24 03:33:13 +0000361std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000362 if (InfoPtr == 0)
363 return "<null selector>";
364
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000365 if (InfoPtr & ArgFlags) {
366 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek150ec292009-03-07 01:22:02 +0000368 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000369 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000370 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000371
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000372 if (!II)
373 return ":";
374
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000375 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000378 // We have a multiple keyword selector (no embedded flags).
379 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000380}
381
John McCall85f3d762011-03-02 01:50:55 +0000382/// Interpreting the given string using the normal CamelCase
383/// conventions, determine whether the given string starts with the
384/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000385static bool startsWithWord(StringRef name, StringRef word) {
John McCall85f3d762011-03-02 01:50:55 +0000386 if (name.size() < word.size()) return false;
387 return ((name.size() == word.size() ||
388 !islower(name[word.size()]))
389 && name.startswith(word));
390}
391
392ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
393 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
394 if (!first) return OMF_None;
395
Chris Lattner5f9e2722011-07-23 10:55:15 +0000396 StringRef name = first->getName();
John McCall85f3d762011-03-02 01:50:55 +0000397 if (sel.isUnarySelector()) {
398 if (name == "autorelease") return OMF_autorelease;
399 if (name == "dealloc") return OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000400 if (name == "finalize") return OMF_finalize;
John McCall85f3d762011-03-02 01:50:55 +0000401 if (name == "release") return OMF_release;
402 if (name == "retain") return OMF_retain;
403 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000404 if (name == "self") return OMF_self;
John McCall85f3d762011-03-02 01:50:55 +0000405 }
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000406
407 if (name == "performSelector") return OMF_performSelector;
John McCall85f3d762011-03-02 01:50:55 +0000408
409 // The other method families may begin with a prefix of underscores.
410 while (!name.empty() && name.front() == '_')
411 name = name.substr(1);
412
413 if (name.empty()) return OMF_None;
414 switch (name.front()) {
415 case 'a':
416 if (startsWithWord(name, "alloc")) return OMF_alloc;
417 break;
418 case 'c':
419 if (startsWithWord(name, "copy")) return OMF_copy;
420 break;
421 case 'i':
422 if (startsWithWord(name, "init")) return OMF_init;
423 break;
424 case 'm':
425 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
426 break;
427 case 'n':
428 if (startsWithWord(name, "new")) return OMF_new;
429 break;
430 default:
431 break;
432 }
433
434 return OMF_None;
435}
Steve Naroff29238a02007-10-05 18:42:47 +0000436
Chris Lattner5f7d2282009-03-04 05:35:38 +0000437namespace {
438 struct SelectorTableImpl {
439 llvm::FoldingSet<MultiKeywordSelector> Table;
440 llvm::BumpPtrAllocator Allocator;
441 };
442} // end anonymous namespace.
443
444static SelectorTableImpl &getSelectorTableImpl(void *P) {
445 return *static_cast<SelectorTableImpl*>(P);
446}
447
Ted Kremenek97f55d62011-04-18 22:47:04 +0000448size_t SelectorTable::getTotalMemory() const {
449 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
450 return SelTabImpl.Allocator.getTotalMemory();
451}
Chris Lattner5f7d2282009-03-04 05:35:38 +0000452
Chris Lattnerff384912007-10-07 02:00:24 +0000453Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
454 if (nKeys < 2)
455 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner5f7d2282009-03-04 05:35:38 +0000457 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Steve Naroff29238a02007-10-05 18:42:47 +0000459 // Unique selector, to guarantee there is one per name.
460 llvm::FoldingSetNodeID ID;
461 MultiKeywordSelector::Profile(ID, IIV, nKeys);
462
463 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000464 if (MultiKeywordSelector *SI =
465 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000466 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Steve Naroff29238a02007-10-05 18:42:47 +0000468 // MultiKeywordSelector objects are not allocated with new because they have a
469 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000470 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
471 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000472 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000473 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000474 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000475 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000476 return Selector(SI);
477}
478
Steve Naroff29238a02007-10-05 18:42:47 +0000479SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000480 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000481}
482
483SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000484 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000485}
486
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000487const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
488 switch (Operator) {
489 case OO_None:
490 case NUM_OVERLOADED_OPERATORS:
491 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000492
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000493#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
494 case OO_##Name: return Spelling;
495#include "clang/Basic/OperatorKinds.def"
496 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000497
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000498 return 0;
499}