blob: 3d08bf807ebccdc1edbf2737ef9f594a3be386a8 [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,
94 KEYALL = 0x3ff
Eli Friedmaneb32fde2009-04-28 03:13:54 +000095 };
96}
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// AddKeyword - This method is used to associate a token ID with specific
99/// identifiers because they are language keywords. This causes the lexer to
100/// automatically map matching identifiers to specialized token codes.
101///
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000102/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000103/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000104/// in the specified language, and set to 0 if disabled in the
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000105/// specified language.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000106static void AddKeyword(llvm::StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000107 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000109 unsigned AddResult = 0;
Dylan Noblesmith67922922011-04-09 13:34:05 +0000110 if (Flags == KEYALL) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000111 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
112 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
113 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000114 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000115 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000116 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000117 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000118 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000119 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000120 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000121
122 // Don't add this keyword if disabled in this language.
123 if (AddResult == 0) return;
124
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000125 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000126 Info.setIsExtensionToken(AddResult == 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000127}
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
130/// representations.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000131static void AddCXXOperatorKeyword(llvm::StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 tok::TokenKind TokenCode,
133 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000134 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000135 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000136}
137
Mike Stump1eb44332009-09-09 15:08:12 +0000138/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000139/// "property".
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000140static void AddObjCKeyword(llvm::StringRef Name,
141 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000143 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
146/// AddKeywords - Add all keywords to the symbol table.
147///
148void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 // Add keywords and tokens for the current language.
150#define KEYWORD(NAME, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000151 AddKeyword(llvm::StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000152 FLAGS, LangOpts, *this);
153#define ALIAS(NAME, TOK, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000154 AddKeyword(llvm::StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000155 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
157 if (LangOpts.CXXOperatorNames) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000158 AddCXXOperatorKeyword(llvm::StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159#define OBJC1_AT_KEYWORD(NAME) \
160 if (LangOpts.ObjC1) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000161 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000162#define OBJC2_AT_KEYWORD(NAME) \
163 if (LangOpts.ObjC2) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000164 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000165#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000166#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000167
168 if (LangOpts.ParseUnknownAnytype)
169 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
170 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
Chris Lattner387b98d2007-10-07 07:52:34 +0000173tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
174 // We use a perfect hash function here involving the length of the keyword,
175 // the first and third character. For preprocessor ID's there are no
176 // collisions (if there were, the switch below would complain about duplicate
177 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner387b98d2007-10-07 07:52:34 +0000179#define HASH(LEN, FIRST, THIRD) \
180 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
181#define CASE(LEN, FIRST, THIRD, NAME) \
182 case HASH(LEN, FIRST, THIRD): \
183 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattner387b98d2007-10-07 07:52:34 +0000185 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000186 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000187 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000188 switch (HASH(Len, Name[0], Name[2])) {
189 default: return tok::pp_not_keyword;
190 CASE( 2, 'i', '\0', if);
191 CASE( 4, 'e', 'i', elif);
192 CASE( 4, 'e', 's', else);
193 CASE( 4, 'l', 'n', line);
194 CASE( 4, 's', 'c', sccs);
195 CASE( 5, 'e', 'd', endif);
196 CASE( 5, 'e', 'r', error);
197 CASE( 5, 'i', 'e', ident);
198 CASE( 5, 'i', 'd', ifdef);
199 CASE( 5, 'u', 'd', undef);
200
201 CASE( 6, 'a', 's', assert);
202 CASE( 6, 'd', 'f', define);
203 CASE( 6, 'i', 'n', ifndef);
204 CASE( 6, 'i', 'p', import);
205 CASE( 6, 'p', 'a', pragma);
206
207 CASE( 7, 'd', 'f', defined);
208 CASE( 7, 'i', 'c', include);
209 CASE( 7, 'w', 'r', warning);
210
211 CASE( 8, 'u', 'a', unassert);
212 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000214 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000215#undef CASE
216#undef HASH
217 }
218}
Reid Spencer5f016e22007-07-11 17:01:13 +0000219
220//===----------------------------------------------------------------------===//
221// Stats Implementation
222//===----------------------------------------------------------------------===//
223
224/// PrintStats - Print statistics about how well the identifier table is doing
225/// at hashing identifiers.
226void IdentifierTable::PrintStats() const {
227 unsigned NumBuckets = HashTable.getNumBuckets();
228 unsigned NumIdentifiers = HashTable.getNumItems();
229 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
230 unsigned AverageIdentifierSize = 0;
231 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000234 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
236 unsigned IdLen = I->getKeyLength();
237 AverageIdentifierSize += IdLen;
238 if (MaxIdentifierLength < IdLen)
239 MaxIdentifierLength = IdLen;
240 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 fprintf(stderr, "\n*** Identifier Table Stats:\n");
243 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
244 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
245 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
246 NumIdentifiers/(double)NumBuckets);
247 fprintf(stderr, "Ave identifier length: %f\n",
248 (AverageIdentifierSize/(double)NumIdentifiers));
249 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 // Compute statistics about the memory allocated for identifiers.
252 HashTable.getAllocator().PrintStats();
253}
Steve Naroff68d331a2007-09-27 14:38:14 +0000254
Steve Naroff29238a02007-10-05 18:42:47 +0000255//===----------------------------------------------------------------------===//
256// SelectorTable Implementation
257//===----------------------------------------------------------------------===//
258
Chris Lattner85994262007-10-05 20:15:24 +0000259unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
260 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
261}
262
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000263namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000264/// MultiKeywordSelector - One of these variable length records is kept for each
265/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000266/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000267/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000268class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000269 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000270 MultiKeywordSelector(unsigned nKeys) {
271 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
272 }
Mike Stump1eb44332009-09-09 15:08:12 +0000273public:
Steve Naroff29238a02007-10-05 18:42:47 +0000274 // Constructor for keyword selectors.
275 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
276 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000277 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Steve Naroff29238a02007-10-05 18:42:47 +0000279 // Fill in the trailing keyword array.
280 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
281 for (unsigned i = 0; i != nKeys; ++i)
282 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000283 }
284
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000285 // getName - Derive the full selector name and return it.
286 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000288 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Steve Naroff29238a02007-10-05 18:42:47 +0000290 typedef IdentifierInfo *const *keyword_iterator;
291 keyword_iterator keyword_begin() const {
292 return reinterpret_cast<keyword_iterator>(this+1);
293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294 keyword_iterator keyword_end() const {
295 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000296 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000297 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000298 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000299 return keyword_begin()[i];
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000302 keyword_iterator ArgTys, unsigned NumArgs) {
303 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000304 for (unsigned i = 0; i != NumArgs; ++i)
305 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000306 }
307 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000308 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000309 }
310};
Chris Lattner85994262007-10-05 20:15:24 +0000311} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000312
313unsigned Selector::getNumArgs() const {
314 unsigned IIF = getIdentifierInfoFlag();
315 if (IIF == ZeroArg)
316 return 0;
317 if (IIF == OneArg)
318 return 1;
319 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
320 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000321 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000322}
323
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000324IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000325 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000326 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000327 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000328 }
329 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
330 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
331 return SI->getIdentifierInfoForSlot(argIndex);
332}
333
Douglas Gregor813d8342011-02-18 22:29:55 +0000334llvm::StringRef Selector::getNameForSlot(unsigned int argIndex) const {
335 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
336 return II? II->getName() : llvm::StringRef();
337}
338
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000339std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000340 llvm::SmallString<256> Str;
341 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000342 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
343 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000344 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000345 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000348 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000349}
350
Chris Lattner077bf5e2008-11-24 03:33:13 +0000351std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000352 if (InfoPtr == 0)
353 return "<null selector>";
354
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000355 if (InfoPtr & ArgFlags) {
356 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek150ec292009-03-07 01:22:02 +0000358 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000359 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000360 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000361
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000362 if (!II)
363 return ":";
364
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000365 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000368 // We have a multiple keyword selector (no embedded flags).
369 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000370}
371
John McCall85f3d762011-03-02 01:50:55 +0000372/// Interpreting the given string using the normal CamelCase
373/// conventions, determine whether the given string starts with the
374/// given "word", which is assumed to end in a lowercase letter.
375static bool startsWithWord(llvm::StringRef name, llvm::StringRef word) {
376 if (name.size() < word.size()) return false;
377 return ((name.size() == word.size() ||
378 !islower(name[word.size()]))
379 && name.startswith(word));
380}
381
382ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
383 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
384 if (!first) return OMF_None;
385
386 llvm::StringRef name = first->getName();
387 if (sel.isUnarySelector()) {
388 if (name == "autorelease") return OMF_autorelease;
389 if (name == "dealloc") return OMF_dealloc;
390 if (name == "release") return OMF_release;
391 if (name == "retain") return OMF_retain;
392 if (name == "retainCount") return OMF_retainCount;
393 }
394
395 // The other method families may begin with a prefix of underscores.
396 while (!name.empty() && name.front() == '_')
397 name = name.substr(1);
398
399 if (name.empty()) return OMF_None;
400 switch (name.front()) {
401 case 'a':
402 if (startsWithWord(name, "alloc")) return OMF_alloc;
403 break;
404 case 'c':
405 if (startsWithWord(name, "copy")) return OMF_copy;
406 break;
407 case 'i':
408 if (startsWithWord(name, "init")) return OMF_init;
409 break;
410 case 'm':
411 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
412 break;
413 case 'n':
414 if (startsWithWord(name, "new")) return OMF_new;
415 break;
416 default:
417 break;
418 }
419
420 return OMF_None;
421}
Steve Naroff29238a02007-10-05 18:42:47 +0000422
Chris Lattner5f7d2282009-03-04 05:35:38 +0000423namespace {
424 struct SelectorTableImpl {
425 llvm::FoldingSet<MultiKeywordSelector> Table;
426 llvm::BumpPtrAllocator Allocator;
427 };
428} // end anonymous namespace.
429
430static SelectorTableImpl &getSelectorTableImpl(void *P) {
431 return *static_cast<SelectorTableImpl*>(P);
432}
433
434
Chris Lattnerff384912007-10-07 02:00:24 +0000435Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
436 if (nKeys < 2)
437 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattner5f7d2282009-03-04 05:35:38 +0000439 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Steve Naroff29238a02007-10-05 18:42:47 +0000441 // Unique selector, to guarantee there is one per name.
442 llvm::FoldingSetNodeID ID;
443 MultiKeywordSelector::Profile(ID, IIV, nKeys);
444
445 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000446 if (MultiKeywordSelector *SI =
447 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000448 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Steve Naroff29238a02007-10-05 18:42:47 +0000450 // MultiKeywordSelector objects are not allocated with new because they have a
451 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000452 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
453 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000454 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000455 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000456 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000457 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000458 return Selector(SI);
459}
460
Steve Naroff29238a02007-10-05 18:42:47 +0000461SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000462 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000463}
464
465SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000466 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000467}
468
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000469const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
470 switch (Operator) {
471 case OO_None:
472 case NUM_OVERLOADED_OPERATORS:
473 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000474
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000475#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
476 case OO_##Name: return Spelling;
477#include "clang/Basic/OperatorKinds.def"
478 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000479
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000480 return 0;
481}
482