blob: c191456b1ea169a04d1adb1775c9d2d89c1bb0fe [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;
Douglas Gregoreee242f2011-10-27 09:33:13 +000040 ChangedAfterLoad = false;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +000041 RevertedTokenID = false;
Douglas Gregoreee242f2011-10-27 09:33:13 +000042 OutOfDate = false;
Douglas Gregorc13a34b2012-01-03 19:32:59 +000043 IsImport = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000044 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000045 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
Reid Spencer5f016e22007-07-11 17:01:13 +000048//===----------------------------------------------------------------------===//
49// IdentifierTable Implementation
50//===----------------------------------------------------------------------===//
51
Douglas Gregor95f42922010-10-14 22:11:03 +000052IdentifierIterator::~IdentifierIterator() { }
53
Ted Kremenek72b1b152009-01-15 18:47:46 +000054IdentifierInfoLookup::~IdentifierInfoLookup() {}
55
Douglas Gregor95f42922010-10-14 22:11:03 +000056namespace {
57 /// \brief A simple identifier lookup iterator that represents an
58 /// empty sequence of identifiers.
59 class EmptyLookupIterator : public IdentifierIterator
60 {
61 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000062 virtual StringRef Next() { return StringRef(); }
Douglas Gregor95f42922010-10-14 22:11:03 +000063 };
64}
65
66IdentifierIterator *IdentifierInfoLookup::getIdentifiers() const {
67 return new EmptyLookupIterator();
68}
69
Douglas Gregor8c5a7602009-04-25 23:30:02 +000070ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
71
Ted Kremenek72b1b152009-01-15 18:47:46 +000072IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
73 IdentifierInfoLookup* externalLookup)
74 : HashTable(8192), // Start with space for 8K identifiers.
75 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000076
77 // Populate the identifier table with info about keywords for the current
78 // language.
79 AddKeywords(LangOpts);
80}
81
82//===----------------------------------------------------------------------===//
83// Language Keyword Implementation
84//===----------------------------------------------------------------------===//
85
Eli Friedmaneb32fde2009-04-28 03:13:54 +000086// Constants for TokenKinds.def
87namespace {
88 enum {
Dylan Noblesmith67922922011-04-09 13:34:05 +000089 KEYC99 = 0x1,
90 KEYCXX = 0x2,
91 KEYCXX0X = 0x4,
92 KEYGNU = 0x8,
93 KEYMS = 0x10,
94 BOOLSUPPORT = 0x20,
95 KEYALTIVEC = 0x40,
96 KEYNOCXX = 0x80,
97 KEYBORLAND = 0x100,
98 KEYOPENCL = 0x200,
Benjamin Kramerffbe9b92011-12-23 17:00:35 +000099 KEYC11 = 0x400,
John McCallf85e1932011-06-15 23:02:42 +0000100 KEYARC = 0x800,
101 KEYALL = 0x0fff
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000102 };
103}
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105/// AddKeyword - This method is used to associate a token ID with specific
106/// identifiers because they are language keywords. This causes the lexer to
107/// automatically map matching identifiers to specialized token codes.
108///
Richard Smith98d86b92011-10-11 19:57:52 +0000109/// The C90/C99/CPP/CPP0x flags are set to 3 if the token is a keyword in a
110/// future language standard, set to 2 if the token should be enabled in the
111/// specified langauge, set to 1 if it is an extension in the specified
112/// language, and set to 0 if disabled in the specified language.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000113static void AddKeyword(StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000114 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000116 unsigned AddResult = 0;
Dylan Noblesmith67922922011-04-09 13:34:05 +0000117 if (Flags == KEYALL) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000118 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
119 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
120 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000121 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000122 else if (LangOpts.MicrosoftExt && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000123 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000124 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000125 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000126 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000127 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000128 else if (LangOpts.C11 && (Flags & KEYC11)) AddResult = 2;
Fariborz Jahanian00852e42011-12-19 21:06:15 +0000129 // We treat bridge casts as objective-C keywords so we can warn on them
130 // in non-arc mode.
131 else if (LangOpts.ObjC2 && (Flags & KEYARC)) AddResult = 2;
Richard Smith98d86b92011-10-11 19:57:52 +0000132 else if (LangOpts.CPlusPlus && (Flags & KEYCXX0X)) AddResult = 3;
133
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000134 // Don't add this keyword if disabled in this language.
135 if (AddResult == 0) return;
136
Richard Smith98d86b92011-10-11 19:57:52 +0000137 IdentifierInfo &Info =
138 Table.get(Keyword, AddResult == 3 ? tok::identifier : TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000139 Info.setIsExtensionToken(AddResult == 1);
Richard Smith98d86b92011-10-11 19:57:52 +0000140 Info.setIsCXX11CompatKeyword(AddResult == 3);
Reid Spencer5f016e22007-07-11 17:01:13 +0000141}
142
Reid Spencer5f016e22007-07-11 17:01:13 +0000143/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
144/// representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000145static void AddCXXOperatorKeyword(StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 tok::TokenKind TokenCode,
147 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000148 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000149 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000150}
151
Mike Stump1eb44332009-09-09 15:08:12 +0000152/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000153/// "property".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000154static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000155 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000157 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158}
159
160/// AddKeywords - Add all keywords to the symbol table.
161///
162void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 // Add keywords and tokens for the current language.
164#define KEYWORD(NAME, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000165 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000166 FLAGS, LangOpts, *this);
167#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000168 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000169 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000170#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
171 if (LangOpts.CXXOperatorNames) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000172 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173#define OBJC1_AT_KEYWORD(NAME) \
174 if (LangOpts.ObjC1) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176#define OBJC2_AT_KEYWORD(NAME) \
177 if (LangOpts.ObjC2) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000178 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000179#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000180#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000181
182 if (LangOpts.ParseUnknownAnytype)
183 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
184 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185}
186
Chris Lattner387b98d2007-10-07 07:52:34 +0000187tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
188 // We use a perfect hash function here involving the length of the keyword,
189 // the first and third character. For preprocessor ID's there are no
190 // collisions (if there were, the switch below would complain about duplicate
191 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattner387b98d2007-10-07 07:52:34 +0000193#define HASH(LEN, FIRST, THIRD) \
194 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
195#define CASE(LEN, FIRST, THIRD, NAME) \
196 case HASH(LEN, FIRST, THIRD): \
197 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Chris Lattner387b98d2007-10-07 07:52:34 +0000199 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000200 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000201 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000202 switch (HASH(Len, Name[0], Name[2])) {
203 default: return tok::pp_not_keyword;
204 CASE( 2, 'i', '\0', if);
205 CASE( 4, 'e', 'i', elif);
206 CASE( 4, 'e', 's', else);
207 CASE( 4, 'l', 'n', line);
208 CASE( 4, 's', 'c', sccs);
209 CASE( 5, 'e', 'd', endif);
210 CASE( 5, 'e', 'r', error);
211 CASE( 5, 'i', 'e', ident);
212 CASE( 5, 'i', 'd', ifdef);
213 CASE( 5, 'u', 'd', undef);
214
215 CASE( 6, 'a', 's', assert);
216 CASE( 6, 'd', 'f', define);
217 CASE( 6, 'i', 'n', ifndef);
218 CASE( 6, 'i', 'p', import);
219 CASE( 6, 'p', 'a', pragma);
Douglas Gregor94ad28b2012-01-03 18:24:14 +0000220
Chris Lattner387b98d2007-10-07 07:52:34 +0000221 CASE( 7, 'd', 'f', defined);
222 CASE( 7, 'i', 'c', include);
223 CASE( 7, 'w', 'r', warning);
224
225 CASE( 8, 'u', 'a', unassert);
226 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Douglas Gregor1ac13c32012-01-03 19:48:16 +0000228 CASE(14, '_', 'p', __public_macro);
229
230 CASE(15, '_', 'p', __private_macro);
231
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000232 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000233#undef CASE
234#undef HASH
235 }
236}
Reid Spencer5f016e22007-07-11 17:01:13 +0000237
238//===----------------------------------------------------------------------===//
239// Stats Implementation
240//===----------------------------------------------------------------------===//
241
242/// PrintStats - Print statistics about how well the identifier table is doing
243/// at hashing identifiers.
244void IdentifierTable::PrintStats() const {
245 unsigned NumBuckets = HashTable.getNumBuckets();
246 unsigned NumIdentifiers = HashTable.getNumItems();
247 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
248 unsigned AverageIdentifierSize = 0;
249 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000252 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
254 unsigned IdLen = I->getKeyLength();
255 AverageIdentifierSize += IdLen;
256 if (MaxIdentifierLength < IdLen)
257 MaxIdentifierLength = IdLen;
258 }
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 fprintf(stderr, "\n*** Identifier Table Stats:\n");
261 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
262 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
263 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
264 NumIdentifiers/(double)NumBuckets);
265 fprintf(stderr, "Ave identifier length: %f\n",
266 (AverageIdentifierSize/(double)NumIdentifiers));
267 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // Compute statistics about the memory allocated for identifiers.
270 HashTable.getAllocator().PrintStats();
271}
Steve Naroff68d331a2007-09-27 14:38:14 +0000272
Steve Naroff29238a02007-10-05 18:42:47 +0000273//===----------------------------------------------------------------------===//
274// SelectorTable Implementation
275//===----------------------------------------------------------------------===//
276
Chris Lattner85994262007-10-05 20:15:24 +0000277unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
278 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
279}
280
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000281namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000282/// MultiKeywordSelector - One of these variable length records is kept for each
283/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000284/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000285/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000286class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000287 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000288 MultiKeywordSelector(unsigned nKeys) {
289 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291public:
Steve Naroff29238a02007-10-05 18:42:47 +0000292 // Constructor for keyword selectors.
293 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
294 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000295 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Steve Naroff29238a02007-10-05 18:42:47 +0000297 // Fill in the trailing keyword array.
298 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
299 for (unsigned i = 0; i != nKeys; ++i)
300 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000301 }
302
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000303 // getName - Derive the full selector name and return it.
304 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000306 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Steve Naroff29238a02007-10-05 18:42:47 +0000308 typedef IdentifierInfo *const *keyword_iterator;
309 keyword_iterator keyword_begin() const {
310 return reinterpret_cast<keyword_iterator>(this+1);
311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312 keyword_iterator keyword_end() const {
313 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000314 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000315 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000316 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000317 return keyword_begin()[i];
318 }
Mike Stump1eb44332009-09-09 15:08:12 +0000319 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000320 keyword_iterator ArgTys, unsigned NumArgs) {
321 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000322 for (unsigned i = 0; i != NumArgs; ++i)
323 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000324 }
325 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000326 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000327 }
328};
Chris Lattner85994262007-10-05 20:15:24 +0000329} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000330
331unsigned Selector::getNumArgs() const {
332 unsigned IIF = getIdentifierInfoFlag();
333 if (IIF == ZeroArg)
334 return 0;
335 if (IIF == OneArg)
336 return 1;
337 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
338 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000339 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000340}
341
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000342IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000343 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000344 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000345 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000346 }
347 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
348 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
349 return SI->getIdentifierInfoForSlot(argIndex);
350}
351
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregor813d8342011-02-18 22:29:55 +0000353 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000354 return II? II->getName() : StringRef();
Douglas Gregor813d8342011-02-18 22:29:55 +0000355}
356
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000357std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000358 llvm::SmallString<256> Str;
359 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000360 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
361 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000362 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000363 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000366 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000367}
368
Chris Lattner077bf5e2008-11-24 03:33:13 +0000369std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000370 if (InfoPtr == 0)
371 return "<null selector>";
372
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000373 if (InfoPtr & ArgFlags) {
374 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek150ec292009-03-07 01:22:02 +0000376 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000377 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000378 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000379
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000380 if (!II)
381 return ":";
382
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000383 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000386 // We have a multiple keyword selector (no embedded flags).
387 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000388}
389
John McCall85f3d762011-03-02 01:50:55 +0000390/// Interpreting the given string using the normal CamelCase
391/// conventions, determine whether the given string starts with the
392/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000393static bool startsWithWord(StringRef name, StringRef word) {
John McCall85f3d762011-03-02 01:50:55 +0000394 if (name.size() < word.size()) return false;
395 return ((name.size() == word.size() ||
396 !islower(name[word.size()]))
397 && name.startswith(word));
398}
399
400ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
401 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
402 if (!first) return OMF_None;
403
Chris Lattner5f9e2722011-07-23 10:55:15 +0000404 StringRef name = first->getName();
John McCall85f3d762011-03-02 01:50:55 +0000405 if (sel.isUnarySelector()) {
406 if (name == "autorelease") return OMF_autorelease;
407 if (name == "dealloc") return OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000408 if (name == "finalize") return OMF_finalize;
John McCall85f3d762011-03-02 01:50:55 +0000409 if (name == "release") return OMF_release;
410 if (name == "retain") return OMF_retain;
411 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000412 if (name == "self") return OMF_self;
John McCall85f3d762011-03-02 01:50:55 +0000413 }
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000414
415 if (name == "performSelector") return OMF_performSelector;
John McCall85f3d762011-03-02 01:50:55 +0000416
417 // The other method families may begin with a prefix of underscores.
418 while (!name.empty() && name.front() == '_')
419 name = name.substr(1);
420
421 if (name.empty()) return OMF_None;
422 switch (name.front()) {
423 case 'a':
424 if (startsWithWord(name, "alloc")) return OMF_alloc;
425 break;
426 case 'c':
427 if (startsWithWord(name, "copy")) return OMF_copy;
428 break;
429 case 'i':
430 if (startsWithWord(name, "init")) return OMF_init;
431 break;
432 case 'm':
433 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
434 break;
435 case 'n':
436 if (startsWithWord(name, "new")) return OMF_new;
437 break;
438 default:
439 break;
440 }
441
442 return OMF_None;
443}
Steve Naroff29238a02007-10-05 18:42:47 +0000444
Chris Lattner5f7d2282009-03-04 05:35:38 +0000445namespace {
446 struct SelectorTableImpl {
447 llvm::FoldingSet<MultiKeywordSelector> Table;
448 llvm::BumpPtrAllocator Allocator;
449 };
450} // end anonymous namespace.
451
452static SelectorTableImpl &getSelectorTableImpl(void *P) {
453 return *static_cast<SelectorTableImpl*>(P);
454}
455
Ted Kremenek97f55d62011-04-18 22:47:04 +0000456size_t SelectorTable::getTotalMemory() const {
457 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
458 return SelTabImpl.Allocator.getTotalMemory();
459}
Chris Lattner5f7d2282009-03-04 05:35:38 +0000460
Chris Lattnerff384912007-10-07 02:00:24 +0000461Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
462 if (nKeys < 2)
463 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Chris Lattner5f7d2282009-03-04 05:35:38 +0000465 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Steve Naroff29238a02007-10-05 18:42:47 +0000467 // Unique selector, to guarantee there is one per name.
468 llvm::FoldingSetNodeID ID;
469 MultiKeywordSelector::Profile(ID, IIV, nKeys);
470
471 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000472 if (MultiKeywordSelector *SI =
473 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000474 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Steve Naroff29238a02007-10-05 18:42:47 +0000476 // MultiKeywordSelector objects are not allocated with new because they have a
477 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000478 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
479 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000480 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000481 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000482 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000483 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000484 return Selector(SI);
485}
486
Steve Naroff29238a02007-10-05 18:42:47 +0000487SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000488 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000489}
490
491SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000492 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000493}
494
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000495const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
496 switch (Operator) {
497 case OO_None:
498 case NUM_OVERLOADED_OPERATORS:
499 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000500
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000501#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
502 case OO_##Name: return Spelling;
503#include "clang/Basic/OperatorKinds.def"
504 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000505
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000506 return 0;
507}