blob: bbea23ab8da7885975d1f6674a7a3923437c8f2d [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"
Daniel Dunbar76b61cc2009-10-17 18:13:02 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner3daed522009-03-02 22:20:04 +000021#include <cstdio>
Ted Kremenekc637e6b2007-10-23 22:18:37 +000022
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// IdentifierInfo Implementation
27//===----------------------------------------------------------------------===//
28
Ted Kremenekea9c26b2009-01-20 23:28:34 +000029IdentifierInfo::IdentifierInfo() {
Reid Spencer5f016e22007-07-11 17:01:13 +000030 TokenID = tok::identifier;
Douglas Gregor5142af32008-11-06 16:32:23 +000031 ObjCOrBuiltinID = 0;
Chris Lattner4365a7e2007-10-07 07:09:52 +000032 HasMacro = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000033 IsExtension = false;
34 IsPoisoned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000035 IsCPPOperatorKeyword = false;
Chris Lattner6a170eb2009-01-21 07:43:11 +000036 NeedsHandleIdentifier = false;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000037 IsFromAST = false;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +000038 RevertedTokenID = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000040 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000041}
42
Reid Spencer5f016e22007-07-11 17:01:13 +000043//===----------------------------------------------------------------------===//
44// IdentifierTable Implementation
45//===----------------------------------------------------------------------===//
46
Douglas Gregor95f42922010-10-14 22:11:03 +000047IdentifierIterator::~IdentifierIterator() { }
48
Ted Kremenek72b1b152009-01-15 18:47:46 +000049IdentifierInfoLookup::~IdentifierInfoLookup() {}
50
Douglas Gregor95f42922010-10-14 22:11:03 +000051namespace {
52 /// \brief A simple identifier lookup iterator that represents an
53 /// empty sequence of identifiers.
54 class EmptyLookupIterator : public IdentifierIterator
55 {
56 public:
57 virtual llvm::StringRef Next() { return llvm::StringRef(); }
58 };
59}
60
61IdentifierIterator *IdentifierInfoLookup::getIdentifiers() const {
62 return new EmptyLookupIterator();
63}
64
Douglas Gregor8c5a7602009-04-25 23:30:02 +000065ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
66
Ted Kremenek72b1b152009-01-15 18:47:46 +000067IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
68 IdentifierInfoLookup* externalLookup)
69 : HashTable(8192), // Start with space for 8K identifiers.
70 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000071
72 // Populate the identifier table with info about keywords for the current
73 // language.
74 AddKeywords(LangOpts);
75}
76
77//===----------------------------------------------------------------------===//
78// Language Keyword Implementation
79//===----------------------------------------------------------------------===//
80
Eli Friedmaneb32fde2009-04-28 03:13:54 +000081// Constants for TokenKinds.def
82namespace {
83 enum {
Dylan Noblesmith67922922011-04-09 13:34:05 +000084 KEYC99 = 0x1,
85 KEYCXX = 0x2,
86 KEYCXX0X = 0x4,
87 KEYGNU = 0x8,
88 KEYMS = 0x10,
89 BOOLSUPPORT = 0x20,
90 KEYALTIVEC = 0x40,
91 KEYNOCXX = 0x80,
92 KEYBORLAND = 0x100,
93 KEYOPENCL = 0x200,
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +000094 KEYC1X = 0x400,
95 KEYALL = 0x7ff
Eli Friedmaneb32fde2009-04-28 03:13:54 +000096 };
97}
98
Reid Spencer5f016e22007-07-11 17:01:13 +000099/// AddKeyword - This method is used to associate a token ID with specific
100/// identifiers because they are language keywords. This causes the lexer to
101/// automatically map matching identifiers to specialized token codes.
102///
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000103/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000104/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000105/// in the specified language, and set to 0 if disabled in the
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000106/// specified language.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000107static void AddKeyword(llvm::StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000108 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000110 unsigned AddResult = 0;
Dylan Noblesmith67922922011-04-09 13:34:05 +0000111 if (Flags == KEYALL) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000112 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
113 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
114 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000115 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000116 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000117 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000118 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000119 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000120 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000121 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +0000122 else if (LangOpts.C1X && (Flags & KEYC1X)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000123
124 // Don't add this keyword if disabled in this language.
125 if (AddResult == 0) return;
126
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000127 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000128 Info.setIsExtensionToken(AddResult == 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129}
130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
132/// representations.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000133static void AddCXXOperatorKeyword(llvm::StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 tok::TokenKind TokenCode,
135 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000136 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000137 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000138}
139
Mike Stump1eb44332009-09-09 15:08:12 +0000140/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000141/// "property".
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000142static void AddObjCKeyword(llvm::StringRef Name,
143 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000145 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000146}
147
148/// AddKeywords - Add all keywords to the symbol table.
149///
150void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 // Add keywords and tokens for the current language.
152#define KEYWORD(NAME, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000153 AddKeyword(llvm::StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000154 FLAGS, LangOpts, *this);
155#define ALIAS(NAME, TOK, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000156 AddKeyword(llvm::StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000157 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
159 if (LangOpts.CXXOperatorNames) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000160 AddCXXOperatorKeyword(llvm::StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161#define OBJC1_AT_KEYWORD(NAME) \
162 if (LangOpts.ObjC1) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000163 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164#define OBJC2_AT_KEYWORD(NAME) \
165 if (LangOpts.ObjC2) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000166 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000167#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000168#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000169
170 if (LangOpts.ParseUnknownAnytype)
171 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
172 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173}
174
Chris Lattner387b98d2007-10-07 07:52:34 +0000175tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
176 // We use a perfect hash function here involving the length of the keyword,
177 // the first and third character. For preprocessor ID's there are no
178 // collisions (if there were, the switch below would complain about duplicate
179 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner387b98d2007-10-07 07:52:34 +0000181#define HASH(LEN, FIRST, THIRD) \
182 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
183#define CASE(LEN, FIRST, THIRD, NAME) \
184 case HASH(LEN, FIRST, THIRD): \
185 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattner387b98d2007-10-07 07:52:34 +0000187 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000188 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000189 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000190 switch (HASH(Len, Name[0], Name[2])) {
191 default: return tok::pp_not_keyword;
192 CASE( 2, 'i', '\0', if);
193 CASE( 4, 'e', 'i', elif);
194 CASE( 4, 'e', 's', else);
195 CASE( 4, 'l', 'n', line);
196 CASE( 4, 's', 'c', sccs);
197 CASE( 5, 'e', 'd', endif);
198 CASE( 5, 'e', 'r', error);
199 CASE( 5, 'i', 'e', ident);
200 CASE( 5, 'i', 'd', ifdef);
201 CASE( 5, 'u', 'd', undef);
202
203 CASE( 6, 'a', 's', assert);
204 CASE( 6, 'd', 'f', define);
205 CASE( 6, 'i', 'n', ifndef);
206 CASE( 6, 'i', 'p', import);
207 CASE( 6, 'p', 'a', pragma);
208
209 CASE( 7, 'd', 'f', defined);
210 CASE( 7, 'i', 'c', include);
211 CASE( 7, 'w', 'r', warning);
212
213 CASE( 8, 'u', 'a', unassert);
214 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000216 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000217#undef CASE
218#undef HASH
219 }
220}
Reid Spencer5f016e22007-07-11 17:01:13 +0000221
222//===----------------------------------------------------------------------===//
223// Stats Implementation
224//===----------------------------------------------------------------------===//
225
226/// PrintStats - Print statistics about how well the identifier table is doing
227/// at hashing identifiers.
228void IdentifierTable::PrintStats() const {
229 unsigned NumBuckets = HashTable.getNumBuckets();
230 unsigned NumIdentifiers = HashTable.getNumItems();
231 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
232 unsigned AverageIdentifierSize = 0;
233 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000236 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
238 unsigned IdLen = I->getKeyLength();
239 AverageIdentifierSize += IdLen;
240 if (MaxIdentifierLength < IdLen)
241 MaxIdentifierLength = IdLen;
242 }
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 fprintf(stderr, "\n*** Identifier Table Stats:\n");
245 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
246 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
247 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
248 NumIdentifiers/(double)NumBuckets);
249 fprintf(stderr, "Ave identifier length: %f\n",
250 (AverageIdentifierSize/(double)NumIdentifiers));
251 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 // Compute statistics about the memory allocated for identifiers.
254 HashTable.getAllocator().PrintStats();
255}
Steve Naroff68d331a2007-09-27 14:38:14 +0000256
Steve Naroff29238a02007-10-05 18:42:47 +0000257//===----------------------------------------------------------------------===//
258// SelectorTable Implementation
259//===----------------------------------------------------------------------===//
260
Chris Lattner85994262007-10-05 20:15:24 +0000261unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
262 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
263}
264
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000265namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000266/// MultiKeywordSelector - One of these variable length records is kept for each
267/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000268/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000269/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000270class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000271 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000272 MultiKeywordSelector(unsigned nKeys) {
273 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275public:
Steve Naroff29238a02007-10-05 18:42:47 +0000276 // Constructor for keyword selectors.
277 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
278 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000279 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Steve Naroff29238a02007-10-05 18:42:47 +0000281 // Fill in the trailing keyword array.
282 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
283 for (unsigned i = 0; i != nKeys; ++i)
284 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000285 }
286
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000287 // getName - Derive the full selector name and return it.
288 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000290 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Steve Naroff29238a02007-10-05 18:42:47 +0000292 typedef IdentifierInfo *const *keyword_iterator;
293 keyword_iterator keyword_begin() const {
294 return reinterpret_cast<keyword_iterator>(this+1);
295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296 keyword_iterator keyword_end() const {
297 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000298 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000299 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000300 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000301 return keyword_begin()[i];
302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000304 keyword_iterator ArgTys, unsigned NumArgs) {
305 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000306 for (unsigned i = 0; i != NumArgs; ++i)
307 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000308 }
309 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000310 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000311 }
312};
Chris Lattner85994262007-10-05 20:15:24 +0000313} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000314
315unsigned Selector::getNumArgs() const {
316 unsigned IIF = getIdentifierInfoFlag();
317 if (IIF == ZeroArg)
318 return 0;
319 if (IIF == OneArg)
320 return 1;
321 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
322 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000323 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000324}
325
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000326IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000327 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000328 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000329 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000330 }
331 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
332 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
333 return SI->getIdentifierInfoForSlot(argIndex);
334}
335
Douglas Gregor813d8342011-02-18 22:29:55 +0000336llvm::StringRef Selector::getNameForSlot(unsigned int argIndex) const {
337 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
338 return II? II->getName() : llvm::StringRef();
339}
340
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000341std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000342 llvm::SmallString<256> Str;
343 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000344 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
345 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000346 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000347 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000350 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000351}
352
Chris Lattner077bf5e2008-11-24 03:33:13 +0000353std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000354 if (InfoPtr == 0)
355 return "<null selector>";
356
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000357 if (InfoPtr & ArgFlags) {
358 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek150ec292009-03-07 01:22:02 +0000360 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000361 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000362 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000363
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000364 if (!II)
365 return ":";
366
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000367 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000370 // We have a multiple keyword selector (no embedded flags).
371 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000372}
373
John McCall85f3d762011-03-02 01:50:55 +0000374/// Interpreting the given string using the normal CamelCase
375/// conventions, determine whether the given string starts with the
376/// given "word", which is assumed to end in a lowercase letter.
377static bool startsWithWord(llvm::StringRef name, llvm::StringRef word) {
378 if (name.size() < word.size()) return false;
379 return ((name.size() == word.size() ||
380 !islower(name[word.size()]))
381 && name.startswith(word));
382}
383
384ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
385 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
386 if (!first) return OMF_None;
387
388 llvm::StringRef name = first->getName();
389 if (sel.isUnarySelector()) {
390 if (name == "autorelease") return OMF_autorelease;
391 if (name == "dealloc") return OMF_dealloc;
392 if (name == "release") return OMF_release;
393 if (name == "retain") return OMF_retain;
394 if (name == "retainCount") return OMF_retainCount;
395 }
396
397 // The other method families may begin with a prefix of underscores.
398 while (!name.empty() && name.front() == '_')
399 name = name.substr(1);
400
401 if (name.empty()) return OMF_None;
402 switch (name.front()) {
403 case 'a':
404 if (startsWithWord(name, "alloc")) return OMF_alloc;
405 break;
406 case 'c':
407 if (startsWithWord(name, "copy")) return OMF_copy;
408 break;
409 case 'i':
410 if (startsWithWord(name, "init")) return OMF_init;
411 break;
412 case 'm':
413 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
414 break;
415 case 'n':
416 if (startsWithWord(name, "new")) return OMF_new;
417 break;
418 default:
419 break;
420 }
421
422 return OMF_None;
423}
Steve Naroff29238a02007-10-05 18:42:47 +0000424
Chris Lattner5f7d2282009-03-04 05:35:38 +0000425namespace {
426 struct SelectorTableImpl {
427 llvm::FoldingSet<MultiKeywordSelector> Table;
428 llvm::BumpPtrAllocator Allocator;
429 };
430} // end anonymous namespace.
431
432static SelectorTableImpl &getSelectorTableImpl(void *P) {
433 return *static_cast<SelectorTableImpl*>(P);
434}
435
436
Chris Lattnerff384912007-10-07 02:00:24 +0000437Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
438 if (nKeys < 2)
439 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Chris Lattner5f7d2282009-03-04 05:35:38 +0000441 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Steve Naroff29238a02007-10-05 18:42:47 +0000443 // Unique selector, to guarantee there is one per name.
444 llvm::FoldingSetNodeID ID;
445 MultiKeywordSelector::Profile(ID, IIV, nKeys);
446
447 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000448 if (MultiKeywordSelector *SI =
449 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000450 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Steve Naroff29238a02007-10-05 18:42:47 +0000452 // MultiKeywordSelector objects are not allocated with new because they have a
453 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000454 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
455 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000456 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000457 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000458 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000459 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000460 return Selector(SI);
461}
462
Steve Naroff29238a02007-10-05 18:42:47 +0000463SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000464 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000465}
466
467SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000468 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000469}
470
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000471const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
472 switch (Operator) {
473 case OO_None:
474 case NUM_OVERLOADED_OPERATORS:
475 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000476
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000477#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
478 case OO_##Name: return Spelling;
479#include "clang/Basic/OperatorKinds.def"
480 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000481
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000482 return 0;
483}
484