blob: 4368ff712c4ce6c267d0efef5193d7b509e104b0 [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;
Reid Spencer5f016e22007-07-11 17:01:13 +000043 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000044 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000045}
46
Reid Spencer5f016e22007-07-11 17:01:13 +000047//===----------------------------------------------------------------------===//
48// IdentifierTable Implementation
49//===----------------------------------------------------------------------===//
50
Douglas Gregor95f42922010-10-14 22:11:03 +000051IdentifierIterator::~IdentifierIterator() { }
52
Ted Kremenek72b1b152009-01-15 18:47:46 +000053IdentifierInfoLookup::~IdentifierInfoLookup() {}
54
Douglas Gregor95f42922010-10-14 22:11:03 +000055namespace {
56 /// \brief A simple identifier lookup iterator that represents an
57 /// empty sequence of identifiers.
58 class EmptyLookupIterator : public IdentifierIterator
59 {
60 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000061 virtual StringRef Next() { return StringRef(); }
Douglas Gregor95f42922010-10-14 22:11:03 +000062 };
63}
64
65IdentifierIterator *IdentifierInfoLookup::getIdentifiers() const {
66 return new EmptyLookupIterator();
67}
68
Douglas Gregor8c5a7602009-04-25 23:30:02 +000069ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
70
Ted Kremenek72b1b152009-01-15 18:47:46 +000071IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
72 IdentifierInfoLookup* externalLookup)
73 : HashTable(8192), // Start with space for 8K identifiers.
74 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000075
76 // Populate the identifier table with info about keywords for the current
77 // language.
78 AddKeywords(LangOpts);
79}
80
81//===----------------------------------------------------------------------===//
82// Language Keyword Implementation
83//===----------------------------------------------------------------------===//
84
Eli Friedmaneb32fde2009-04-28 03:13:54 +000085// Constants for TokenKinds.def
86namespace {
87 enum {
Dylan Noblesmith67922922011-04-09 13:34:05 +000088 KEYC99 = 0x1,
89 KEYCXX = 0x2,
90 KEYCXX0X = 0x4,
91 KEYGNU = 0x8,
92 KEYMS = 0x10,
93 BOOLSUPPORT = 0x20,
94 KEYALTIVEC = 0x40,
95 KEYNOCXX = 0x80,
96 KEYBORLAND = 0x100,
97 KEYOPENCL = 0x200,
Benjamin Kramerffbe9b92011-12-23 17:00:35 +000098 KEYC11 = 0x400,
John McCallf85e1932011-06-15 23:02:42 +000099 KEYARC = 0x800,
100 KEYALL = 0x0fff
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000101 };
102}
103
Reid Spencer5f016e22007-07-11 17:01:13 +0000104/// AddKeyword - This method is used to associate a token ID with specific
105/// identifiers because they are language keywords. This causes the lexer to
106/// automatically map matching identifiers to specialized token codes.
107///
Richard Smith98d86b92011-10-11 19:57:52 +0000108/// The C90/C99/CPP/CPP0x flags are set to 3 if the token is a keyword in a
109/// future language standard, set to 2 if the token should be enabled in the
110/// specified langauge, set to 1 if it is an extension in the specified
111/// language, and set to 0 if disabled in the specified language.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000112static void AddKeyword(StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000113 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000115 unsigned AddResult = 0;
Dylan Noblesmith67922922011-04-09 13:34:05 +0000116 if (Flags == KEYALL) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000117 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
118 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
119 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000120 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000121 else if (LangOpts.MicrosoftExt && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000122 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000123 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000124 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000125 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000126 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000127 else if (LangOpts.C11 && (Flags & KEYC11)) AddResult = 2;
Fariborz Jahanian00852e42011-12-19 21:06:15 +0000128 // We treat bridge casts as objective-C keywords so we can warn on them
129 // in non-arc mode.
130 else if (LangOpts.ObjC2 && (Flags & KEYARC)) AddResult = 2;
Richard Smith98d86b92011-10-11 19:57:52 +0000131 else if (LangOpts.CPlusPlus && (Flags & KEYCXX0X)) AddResult = 3;
132
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000133 // Don't add this keyword if disabled in this language.
134 if (AddResult == 0) return;
135
Richard Smith98d86b92011-10-11 19:57:52 +0000136 IdentifierInfo &Info =
137 Table.get(Keyword, AddResult == 3 ? tok::identifier : TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000138 Info.setIsExtensionToken(AddResult == 1);
Richard Smith98d86b92011-10-11 19:57:52 +0000139 Info.setIsCXX11CompatKeyword(AddResult == 3);
Reid Spencer5f016e22007-07-11 17:01:13 +0000140}
141
Reid Spencer5f016e22007-07-11 17:01:13 +0000142/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
143/// representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000144static void AddCXXOperatorKeyword(StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 tok::TokenKind TokenCode,
146 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000147 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000148 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000149}
150
Mike Stump1eb44332009-09-09 15:08:12 +0000151/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000152/// "property".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000153static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000154 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000156 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157}
158
159/// AddKeywords - Add all keywords to the symbol table.
160///
161void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 // Add keywords and tokens for the current language.
163#define KEYWORD(NAME, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000164 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000165 FLAGS, LangOpts, *this);
166#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000167 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000168 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
170 if (LangOpts.CXXOperatorNames) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000171 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000172#define OBJC1_AT_KEYWORD(NAME) \
173 if (LangOpts.ObjC1) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000174 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000175#define OBJC2_AT_KEYWORD(NAME) \
176 if (LangOpts.ObjC2) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000177 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000178#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000179#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000180
181 if (LangOpts.ParseUnknownAnytype)
182 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
183 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000184}
185
Chris Lattner387b98d2007-10-07 07:52:34 +0000186tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
187 // We use a perfect hash function here involving the length of the keyword,
188 // the first and third character. For preprocessor ID's there are no
189 // collisions (if there were, the switch below would complain about duplicate
190 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Chris Lattner387b98d2007-10-07 07:52:34 +0000192#define HASH(LEN, FIRST, THIRD) \
193 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
194#define CASE(LEN, FIRST, THIRD, NAME) \
195 case HASH(LEN, FIRST, THIRD): \
196 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Chris Lattner387b98d2007-10-07 07:52:34 +0000198 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000199 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000200 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000201 switch (HASH(Len, Name[0], Name[2])) {
202 default: return tok::pp_not_keyword;
203 CASE( 2, 'i', '\0', if);
204 CASE( 4, 'e', 'i', elif);
205 CASE( 4, 'e', 's', else);
206 CASE( 4, 'l', 'n', line);
207 CASE( 4, 's', 'c', sccs);
208 CASE( 5, 'e', 'd', endif);
209 CASE( 5, 'e', 'r', error);
210 CASE( 5, 'i', 'e', ident);
211 CASE( 5, 'i', 'd', ifdef);
212 CASE( 5, 'u', 'd', undef);
213
214 CASE( 6, 'a', 's', assert);
215 CASE( 6, 'd', 'f', define);
216 CASE( 6, 'i', 'n', ifndef);
217 CASE( 6, 'i', 'p', import);
218 CASE( 6, 'p', 'a', pragma);
Douglas Gregor94ad28b2012-01-03 18:24:14 +0000219 CASE( 6, 'p', 'b', public);
220
Chris Lattner387b98d2007-10-07 07:52:34 +0000221 CASE( 7, 'd', 'f', defined);
222 CASE( 7, 'i', 'c', include);
Douglas Gregor94ad28b2012-01-03 18:24:14 +0000223 CASE( 7, 'p', 'i', private);
Chris Lattner387b98d2007-10-07 07:52:34 +0000224 CASE( 7, 'w', 'r', warning);
225
226 CASE( 8, 'u', 'a', unassert);
227 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000229 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000230#undef CASE
231#undef HASH
232 }
233}
Reid Spencer5f016e22007-07-11 17:01:13 +0000234
235//===----------------------------------------------------------------------===//
236// Stats Implementation
237//===----------------------------------------------------------------------===//
238
239/// PrintStats - Print statistics about how well the identifier table is doing
240/// at hashing identifiers.
241void IdentifierTable::PrintStats() const {
242 unsigned NumBuckets = HashTable.getNumBuckets();
243 unsigned NumIdentifiers = HashTable.getNumItems();
244 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
245 unsigned AverageIdentifierSize = 0;
246 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000249 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
251 unsigned IdLen = I->getKeyLength();
252 AverageIdentifierSize += IdLen;
253 if (MaxIdentifierLength < IdLen)
254 MaxIdentifierLength = IdLen;
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 fprintf(stderr, "\n*** Identifier Table Stats:\n");
258 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
259 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
260 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
261 NumIdentifiers/(double)NumBuckets);
262 fprintf(stderr, "Ave identifier length: %f\n",
263 (AverageIdentifierSize/(double)NumIdentifiers));
264 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 // Compute statistics about the memory allocated for identifiers.
267 HashTable.getAllocator().PrintStats();
268}
Steve Naroff68d331a2007-09-27 14:38:14 +0000269
Steve Naroff29238a02007-10-05 18:42:47 +0000270//===----------------------------------------------------------------------===//
271// SelectorTable Implementation
272//===----------------------------------------------------------------------===//
273
Chris Lattner85994262007-10-05 20:15:24 +0000274unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
275 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
276}
277
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000278namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000279/// MultiKeywordSelector - One of these variable length records is kept for each
280/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000281/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000282/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000283class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000284 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000285 MultiKeywordSelector(unsigned nKeys) {
286 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288public:
Steve Naroff29238a02007-10-05 18:42:47 +0000289 // Constructor for keyword selectors.
290 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
291 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000292 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Steve Naroff29238a02007-10-05 18:42:47 +0000294 // Fill in the trailing keyword array.
295 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
296 for (unsigned i = 0; i != nKeys; ++i)
297 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000298 }
299
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000300 // getName - Derive the full selector name and return it.
301 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000303 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Steve Naroff29238a02007-10-05 18:42:47 +0000305 typedef IdentifierInfo *const *keyword_iterator;
306 keyword_iterator keyword_begin() const {
307 return reinterpret_cast<keyword_iterator>(this+1);
308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309 keyword_iterator keyword_end() const {
310 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000311 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000312 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000313 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000314 return keyword_begin()[i];
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000317 keyword_iterator ArgTys, unsigned NumArgs) {
318 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000319 for (unsigned i = 0; i != NumArgs; ++i)
320 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000321 }
322 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000323 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000324 }
325};
Chris Lattner85994262007-10-05 20:15:24 +0000326} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000327
328unsigned Selector::getNumArgs() const {
329 unsigned IIF = getIdentifierInfoFlag();
330 if (IIF == ZeroArg)
331 return 0;
332 if (IIF == OneArg)
333 return 1;
334 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
335 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000336 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000337}
338
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000339IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000340 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000341 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000342 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000343 }
344 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
345 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
346 return SI->getIdentifierInfoForSlot(argIndex);
347}
348
Chris Lattner5f9e2722011-07-23 10:55:15 +0000349StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregor813d8342011-02-18 22:29:55 +0000350 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000351 return II? II->getName() : StringRef();
Douglas Gregor813d8342011-02-18 22:29:55 +0000352}
353
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000354std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000355 llvm::SmallString<256> Str;
356 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000357 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
358 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000359 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000360 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000363 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000364}
365
Chris Lattner077bf5e2008-11-24 03:33:13 +0000366std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000367 if (InfoPtr == 0)
368 return "<null selector>";
369
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000370 if (InfoPtr & ArgFlags) {
371 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek150ec292009-03-07 01:22:02 +0000373 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000374 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000375 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000376
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000377 if (!II)
378 return ":";
379
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000380 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000383 // We have a multiple keyword selector (no embedded flags).
384 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000385}
386
John McCall85f3d762011-03-02 01:50:55 +0000387/// Interpreting the given string using the normal CamelCase
388/// conventions, determine whether the given string starts with the
389/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000390static bool startsWithWord(StringRef name, StringRef word) {
John McCall85f3d762011-03-02 01:50:55 +0000391 if (name.size() < word.size()) return false;
392 return ((name.size() == word.size() ||
393 !islower(name[word.size()]))
394 && name.startswith(word));
395}
396
397ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
398 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
399 if (!first) return OMF_None;
400
Chris Lattner5f9e2722011-07-23 10:55:15 +0000401 StringRef name = first->getName();
John McCall85f3d762011-03-02 01:50:55 +0000402 if (sel.isUnarySelector()) {
403 if (name == "autorelease") return OMF_autorelease;
404 if (name == "dealloc") return OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000405 if (name == "finalize") return OMF_finalize;
John McCall85f3d762011-03-02 01:50:55 +0000406 if (name == "release") return OMF_release;
407 if (name == "retain") return OMF_retain;
408 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000409 if (name == "self") return OMF_self;
John McCall85f3d762011-03-02 01:50:55 +0000410 }
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000411
412 if (name == "performSelector") return OMF_performSelector;
John McCall85f3d762011-03-02 01:50:55 +0000413
414 // The other method families may begin with a prefix of underscores.
415 while (!name.empty() && name.front() == '_')
416 name = name.substr(1);
417
418 if (name.empty()) return OMF_None;
419 switch (name.front()) {
420 case 'a':
421 if (startsWithWord(name, "alloc")) return OMF_alloc;
422 break;
423 case 'c':
424 if (startsWithWord(name, "copy")) return OMF_copy;
425 break;
426 case 'i':
427 if (startsWithWord(name, "init")) return OMF_init;
428 break;
429 case 'm':
430 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
431 break;
432 case 'n':
433 if (startsWithWord(name, "new")) return OMF_new;
434 break;
435 default:
436 break;
437 }
438
439 return OMF_None;
440}
Steve Naroff29238a02007-10-05 18:42:47 +0000441
Chris Lattner5f7d2282009-03-04 05:35:38 +0000442namespace {
443 struct SelectorTableImpl {
444 llvm::FoldingSet<MultiKeywordSelector> Table;
445 llvm::BumpPtrAllocator Allocator;
446 };
447} // end anonymous namespace.
448
449static SelectorTableImpl &getSelectorTableImpl(void *P) {
450 return *static_cast<SelectorTableImpl*>(P);
451}
452
Ted Kremenek97f55d62011-04-18 22:47:04 +0000453size_t SelectorTable::getTotalMemory() const {
454 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
455 return SelTabImpl.Allocator.getTotalMemory();
456}
Chris Lattner5f7d2282009-03-04 05:35:38 +0000457
Chris Lattnerff384912007-10-07 02:00:24 +0000458Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
459 if (nKeys < 2)
460 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattner5f7d2282009-03-04 05:35:38 +0000462 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Steve Naroff29238a02007-10-05 18:42:47 +0000464 // Unique selector, to guarantee there is one per name.
465 llvm::FoldingSetNodeID ID;
466 MultiKeywordSelector::Profile(ID, IIV, nKeys);
467
468 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000469 if (MultiKeywordSelector *SI =
470 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000471 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Steve Naroff29238a02007-10-05 18:42:47 +0000473 // MultiKeywordSelector objects are not allocated with new because they have a
474 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000475 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
476 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000477 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000478 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000479 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000480 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000481 return Selector(SI);
482}
483
Steve Naroff29238a02007-10-05 18:42:47 +0000484SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000485 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000486}
487
488SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000489 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000490}
491
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000492const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
493 switch (Operator) {
494 case OO_None:
495 case NUM_OVERLOADED_OPERATORS:
496 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000497
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000498#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
499 case OO_##Name: return Spelling;
500#include "clang/Basic/OperatorKinds.def"
501 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000502
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000503 return 0;
504}