blob: 0103318405c6172d164deebe10b772e694dcec53 [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;
35 IsPoisoned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 IsCPPOperatorKeyword = false;
Chris Lattner6a170eb2009-01-21 07:43:11 +000037 NeedsHandleIdentifier = false;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000038 IsFromAST = false;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +000039 RevertedTokenID = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000040 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000041 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000042}
43
Reid Spencer5f016e22007-07-11 17:01:13 +000044//===----------------------------------------------------------------------===//
45// IdentifierTable Implementation
46//===----------------------------------------------------------------------===//
47
Douglas Gregor95f42922010-10-14 22:11:03 +000048IdentifierIterator::~IdentifierIterator() { }
49
Ted Kremenek72b1b152009-01-15 18:47:46 +000050IdentifierInfoLookup::~IdentifierInfoLookup() {}
51
Douglas Gregor95f42922010-10-14 22:11:03 +000052namespace {
53 /// \brief A simple identifier lookup iterator that represents an
54 /// empty sequence of identifiers.
55 class EmptyLookupIterator : public IdentifierIterator
56 {
57 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000058 virtual StringRef Next() { return StringRef(); }
Douglas Gregor95f42922010-10-14 22:11:03 +000059 };
60}
61
62IdentifierIterator *IdentifierInfoLookup::getIdentifiers() const {
63 return new EmptyLookupIterator();
64}
65
Douglas Gregor8c5a7602009-04-25 23:30:02 +000066ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
67
Ted Kremenek72b1b152009-01-15 18:47:46 +000068IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
69 IdentifierInfoLookup* externalLookup)
70 : HashTable(8192), // Start with space for 8K identifiers.
71 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000072
73 // Populate the identifier table with info about keywords for the current
74 // language.
75 AddKeywords(LangOpts);
76}
77
78//===----------------------------------------------------------------------===//
79// Language Keyword Implementation
80//===----------------------------------------------------------------------===//
81
Eli Friedmaneb32fde2009-04-28 03:13:54 +000082// Constants for TokenKinds.def
83namespace {
84 enum {
Dylan Noblesmith67922922011-04-09 13:34:05 +000085 KEYC99 = 0x1,
86 KEYCXX = 0x2,
87 KEYCXX0X = 0x4,
88 KEYGNU = 0x8,
89 KEYMS = 0x10,
90 BOOLSUPPORT = 0x20,
91 KEYALTIVEC = 0x40,
92 KEYNOCXX = 0x80,
93 KEYBORLAND = 0x100,
94 KEYOPENCL = 0x200,
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +000095 KEYC1X = 0x400,
John McCallf85e1932011-06-15 23:02:42 +000096 KEYARC = 0x800,
97 KEYALL = 0x0fff
Eli Friedmaneb32fde2009-04-28 03:13:54 +000098 };
99}
100
Reid Spencer5f016e22007-07-11 17:01:13 +0000101/// AddKeyword - This method is used to associate a token ID with specific
102/// identifiers because they are language keywords. This causes the lexer to
103/// automatically map matching identifiers to specialized token codes.
104///
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000105/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000106/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000107/// in the specified language, and set to 0 if disabled in the
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000108/// specified language.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000109static void AddKeyword(StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000110 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000112 unsigned AddResult = 0;
Dylan Noblesmith67922922011-04-09 13:34:05 +0000113 if (Flags == KEYALL) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000114 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
115 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
116 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000117 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000118 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000119 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000120 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000121 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000122 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000123 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +0000124 else if (LangOpts.C1X && (Flags & KEYC1X)) AddResult = 2;
John McCallf85e1932011-06-15 23:02:42 +0000125 else if (LangOpts.ObjCAutoRefCount && (Flags & KEYARC)) AddResult = 2;
126
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000127 // Don't add this keyword if disabled in this language.
128 if (AddResult == 0) return;
129
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000130 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000131 Info.setIsExtensionToken(AddResult == 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000132}
133
Reid Spencer5f016e22007-07-11 17:01:13 +0000134/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
135/// representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000136static void AddCXXOperatorKeyword(StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 tok::TokenKind TokenCode,
138 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000139 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000140 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000141}
142
Mike Stump1eb44332009-09-09 15:08:12 +0000143/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000144/// "property".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000145static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000146 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000148 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000149}
150
151/// AddKeywords - Add all keywords to the symbol table.
152///
153void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 // Add keywords and tokens for the current language.
155#define KEYWORD(NAME, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000156 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000157 FLAGS, LangOpts, *this);
158#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000159 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000160 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
162 if (LangOpts.CXXOperatorNames) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000163 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164#define OBJC1_AT_KEYWORD(NAME) \
165 if (LangOpts.ObjC1) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000166 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000167#define OBJC2_AT_KEYWORD(NAME) \
168 if (LangOpts.ObjC2) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000169 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000170#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000171#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000172
173 if (LangOpts.ParseUnknownAnytype)
174 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
175 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
Chris Lattner387b98d2007-10-07 07:52:34 +0000178tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
179 // We use a perfect hash function here involving the length of the keyword,
180 // the first and third character. For preprocessor ID's there are no
181 // collisions (if there were, the switch below would complain about duplicate
182 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Chris Lattner387b98d2007-10-07 07:52:34 +0000184#define HASH(LEN, FIRST, THIRD) \
185 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
186#define CASE(LEN, FIRST, THIRD, NAME) \
187 case HASH(LEN, FIRST, THIRD): \
188 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Chris Lattner387b98d2007-10-07 07:52:34 +0000190 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000191 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000192 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000193 switch (HASH(Len, Name[0], Name[2])) {
194 default: return tok::pp_not_keyword;
195 CASE( 2, 'i', '\0', if);
196 CASE( 4, 'e', 'i', elif);
197 CASE( 4, 'e', 's', else);
198 CASE( 4, 'l', 'n', line);
199 CASE( 4, 's', 'c', sccs);
200 CASE( 5, 'e', 'd', endif);
201 CASE( 5, 'e', 'r', error);
202 CASE( 5, 'i', 'e', ident);
203 CASE( 5, 'i', 'd', ifdef);
204 CASE( 5, 'u', 'd', undef);
205
206 CASE( 6, 'a', 's', assert);
207 CASE( 6, 'd', 'f', define);
208 CASE( 6, 'i', 'n', ifndef);
209 CASE( 6, 'i', 'p', import);
210 CASE( 6, 'p', 'a', pragma);
211
212 CASE( 7, 'd', 'f', defined);
213 CASE( 7, 'i', 'c', include);
214 CASE( 7, 'w', 'r', warning);
215
216 CASE( 8, 'u', 'a', unassert);
217 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000219 CASE(16, '_', 'i', __include_macros);
Douglas Gregor7143aab2011-09-01 17:04:32 +0000220 CASE(16, '_', 'e', __export_macro__);
Chris Lattner387b98d2007-10-07 07:52:34 +0000221#undef CASE
222#undef HASH
223 }
224}
Reid Spencer5f016e22007-07-11 17:01:13 +0000225
226//===----------------------------------------------------------------------===//
227// Stats Implementation
228//===----------------------------------------------------------------------===//
229
230/// PrintStats - Print statistics about how well the identifier table is doing
231/// at hashing identifiers.
232void IdentifierTable::PrintStats() const {
233 unsigned NumBuckets = HashTable.getNumBuckets();
234 unsigned NumIdentifiers = HashTable.getNumItems();
235 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
236 unsigned AverageIdentifierSize = 0;
237 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000240 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
242 unsigned IdLen = I->getKeyLength();
243 AverageIdentifierSize += IdLen;
244 if (MaxIdentifierLength < IdLen)
245 MaxIdentifierLength = IdLen;
246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 fprintf(stderr, "\n*** Identifier Table Stats:\n");
249 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
250 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
251 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
252 NumIdentifiers/(double)NumBuckets);
253 fprintf(stderr, "Ave identifier length: %f\n",
254 (AverageIdentifierSize/(double)NumIdentifiers));
255 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 // Compute statistics about the memory allocated for identifiers.
258 HashTable.getAllocator().PrintStats();
259}
Steve Naroff68d331a2007-09-27 14:38:14 +0000260
Steve Naroff29238a02007-10-05 18:42:47 +0000261//===----------------------------------------------------------------------===//
262// SelectorTable Implementation
263//===----------------------------------------------------------------------===//
264
Chris Lattner85994262007-10-05 20:15:24 +0000265unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
266 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
267}
268
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000269namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000270/// MultiKeywordSelector - One of these variable length records is kept for each
271/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000272/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000273/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000274class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000275 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000276 MultiKeywordSelector(unsigned nKeys) {
277 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279public:
Steve Naroff29238a02007-10-05 18:42:47 +0000280 // Constructor for keyword selectors.
281 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
282 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000283 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Steve Naroff29238a02007-10-05 18:42:47 +0000285 // Fill in the trailing keyword array.
286 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
287 for (unsigned i = 0; i != nKeys; ++i)
288 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000289 }
290
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000291 // getName - Derive the full selector name and return it.
292 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000294 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Steve Naroff29238a02007-10-05 18:42:47 +0000296 typedef IdentifierInfo *const *keyword_iterator;
297 keyword_iterator keyword_begin() const {
298 return reinterpret_cast<keyword_iterator>(this+1);
299 }
Mike Stump1eb44332009-09-09 15:08:12 +0000300 keyword_iterator keyword_end() const {
301 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000302 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000303 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000304 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000305 return keyword_begin()[i];
306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000308 keyword_iterator ArgTys, unsigned NumArgs) {
309 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000310 for (unsigned i = 0; i != NumArgs; ++i)
311 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000312 }
313 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000314 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000315 }
316};
Chris Lattner85994262007-10-05 20:15:24 +0000317} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000318
319unsigned Selector::getNumArgs() const {
320 unsigned IIF = getIdentifierInfoFlag();
321 if (IIF == ZeroArg)
322 return 0;
323 if (IIF == OneArg)
324 return 1;
325 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
326 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000327 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000328}
329
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000330IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000331 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000332 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000333 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000334 }
335 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
336 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
337 return SI->getIdentifierInfoForSlot(argIndex);
338}
339
Chris Lattner5f9e2722011-07-23 10:55:15 +0000340StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregor813d8342011-02-18 22:29:55 +0000341 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000342 return II? II->getName() : StringRef();
Douglas Gregor813d8342011-02-18 22:29:55 +0000343}
344
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000345std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000346 llvm::SmallString<256> Str;
347 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000348 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
349 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000350 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000351 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000354 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000355}
356
Chris Lattner077bf5e2008-11-24 03:33:13 +0000357std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000358 if (InfoPtr == 0)
359 return "<null selector>";
360
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000361 if (InfoPtr & ArgFlags) {
362 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek150ec292009-03-07 01:22:02 +0000364 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000365 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000366 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000367
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000368 if (!II)
369 return ":";
370
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000371 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000374 // We have a multiple keyword selector (no embedded flags).
375 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000376}
377
John McCall85f3d762011-03-02 01:50:55 +0000378/// Interpreting the given string using the normal CamelCase
379/// conventions, determine whether the given string starts with the
380/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000381static bool startsWithWord(StringRef name, StringRef word) {
John McCall85f3d762011-03-02 01:50:55 +0000382 if (name.size() < word.size()) return false;
383 return ((name.size() == word.size() ||
384 !islower(name[word.size()]))
385 && name.startswith(word));
386}
387
388ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
389 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
390 if (!first) return OMF_None;
391
Chris Lattner5f9e2722011-07-23 10:55:15 +0000392 StringRef name = first->getName();
John McCall85f3d762011-03-02 01:50:55 +0000393 if (sel.isUnarySelector()) {
394 if (name == "autorelease") return OMF_autorelease;
395 if (name == "dealloc") return OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000396 if (name == "finalize") return OMF_finalize;
John McCall85f3d762011-03-02 01:50:55 +0000397 if (name == "release") return OMF_release;
398 if (name == "retain") return OMF_retain;
399 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000400 if (name == "self") return OMF_self;
John McCall85f3d762011-03-02 01:50:55 +0000401 }
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000402
403 if (name == "performSelector") return OMF_performSelector;
John McCall85f3d762011-03-02 01:50:55 +0000404
405 // The other method families may begin with a prefix of underscores.
406 while (!name.empty() && name.front() == '_')
407 name = name.substr(1);
408
409 if (name.empty()) return OMF_None;
410 switch (name.front()) {
411 case 'a':
412 if (startsWithWord(name, "alloc")) return OMF_alloc;
413 break;
414 case 'c':
415 if (startsWithWord(name, "copy")) return OMF_copy;
416 break;
417 case 'i':
418 if (startsWithWord(name, "init")) return OMF_init;
419 break;
420 case 'm':
421 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
422 break;
423 case 'n':
424 if (startsWithWord(name, "new")) return OMF_new;
425 break;
426 default:
427 break;
428 }
429
430 return OMF_None;
431}
Steve Naroff29238a02007-10-05 18:42:47 +0000432
Chris Lattner5f7d2282009-03-04 05:35:38 +0000433namespace {
434 struct SelectorTableImpl {
435 llvm::FoldingSet<MultiKeywordSelector> Table;
436 llvm::BumpPtrAllocator Allocator;
437 };
438} // end anonymous namespace.
439
440static SelectorTableImpl &getSelectorTableImpl(void *P) {
441 return *static_cast<SelectorTableImpl*>(P);
442}
443
Ted Kremenek97f55d62011-04-18 22:47:04 +0000444size_t SelectorTable::getTotalMemory() const {
445 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
446 return SelTabImpl.Allocator.getTotalMemory();
447}
Chris Lattner5f7d2282009-03-04 05:35:38 +0000448
Chris Lattnerff384912007-10-07 02:00:24 +0000449Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
450 if (nKeys < 2)
451 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner5f7d2282009-03-04 05:35:38 +0000453 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Steve Naroff29238a02007-10-05 18:42:47 +0000455 // Unique selector, to guarantee there is one per name.
456 llvm::FoldingSetNodeID ID;
457 MultiKeywordSelector::Profile(ID, IIV, nKeys);
458
459 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000460 if (MultiKeywordSelector *SI =
461 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000462 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Steve Naroff29238a02007-10-05 18:42:47 +0000464 // MultiKeywordSelector objects are not allocated with new because they have a
465 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000466 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
467 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000468 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000469 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000470 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000471 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000472 return Selector(SI);
473}
474
Steve Naroff29238a02007-10-05 18:42:47 +0000475SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000476 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000477}
478
479SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000480 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000481}
482
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000483const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
484 switch (Operator) {
485 case OO_None:
486 case NUM_OVERLOADED_OPERATORS:
487 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000488
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000489#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
490 case OO_##Name: return Spelling;
491#include "clang/Basic/OperatorKinds.def"
492 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000493
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000494 return 0;
495}
496