blob: 49cd0ac615a3c399c6c6e5f540c69a05766900f1 [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"
Chris Lattner85994262007-10-05 20:15:24 +000017#include "llvm/ADT/DenseMap.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "llvm/ADT/FoldingSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000019#include "llvm/ADT/SmallString.h"
David Blaikie7530c032012-01-17 06:56:22 +000020#include "llvm/Support/ErrorHandling.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "llvm/Support/raw_ostream.h"
Joerg Sonnenberger7094dee2012-08-10 10:58:18 +000022#include <cctype>
Chris Lattner3daed522009-03-02 22:20:04 +000023#include <cstdio>
Ted Kremenekc637e6b2007-10-23 22:18:37 +000024
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// IdentifierInfo Implementation
29//===----------------------------------------------------------------------===//
30
Ted Kremenekea9c26b2009-01-20 23:28:34 +000031IdentifierInfo::IdentifierInfo() {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 TokenID = tok::identifier;
Douglas Gregor5142af32008-11-06 16:32:23 +000033 ObjCOrBuiltinID = 0;
Chris Lattner4365a7e2007-10-07 07:09:52 +000034 HasMacro = false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +000035 HadMacro = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 IsExtension = false;
Richard Smith98d86b92011-10-11 19:57:52 +000037 IsCXX11CompatKeyword = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000038 IsPoisoned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 IsCPPOperatorKeyword = false;
Chris Lattner6a170eb2009-01-21 07:43:11 +000040 NeedsHandleIdentifier = false;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000041 IsFromAST = false;
Douglas Gregoreee242f2011-10-27 09:33:13 +000042 ChangedAfterLoad = false;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +000043 RevertedTokenID = false;
Douglas Gregoreee242f2011-10-27 09:33:13 +000044 OutOfDate = false;
Ted Kremenek32ad2ee2012-03-01 22:07:04 +000045 IsModulesImport = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000046 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000047 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050//===----------------------------------------------------------------------===//
51// IdentifierTable Implementation
52//===----------------------------------------------------------------------===//
53
Douglas Gregor95f42922010-10-14 22:11:03 +000054IdentifierIterator::~IdentifierIterator() { }
55
Ted Kremenek72b1b152009-01-15 18:47:46 +000056IdentifierInfoLookup::~IdentifierInfoLookup() {}
57
Douglas Gregor95f42922010-10-14 22:11:03 +000058namespace {
59 /// \brief A simple identifier lookup iterator that represents an
60 /// empty sequence of identifiers.
61 class EmptyLookupIterator : public IdentifierIterator
62 {
63 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000064 virtual StringRef Next() { return StringRef(); }
Douglas Gregor95f42922010-10-14 22:11:03 +000065 };
66}
67
68IdentifierIterator *IdentifierInfoLookup::getIdentifiers() const {
69 return new EmptyLookupIterator();
70}
71
Douglas Gregor8c5a7602009-04-25 23:30:02 +000072ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
73
Ted Kremenek72b1b152009-01-15 18:47:46 +000074IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
75 IdentifierInfoLookup* externalLookup)
76 : HashTable(8192), // Start with space for 8K identifiers.
77 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000078
79 // Populate the identifier table with info about keywords for the current
80 // language.
81 AddKeywords(LangOpts);
Ted Kremenekf15e1142012-03-01 22:53:32 +000082
83
84 // Add the '_experimental_modules_import' contextual keyword.
Douglas Gregor1b257af2012-12-11 22:11:52 +000085 get("import").setModulesImport(true);
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
88//===----------------------------------------------------------------------===//
89// Language Keyword Implementation
90//===----------------------------------------------------------------------===//
91
Eli Friedmaneb32fde2009-04-28 03:13:54 +000092// Constants for TokenKinds.def
93namespace {
94 enum {
Dylan Noblesmith67922922011-04-09 13:34:05 +000095 KEYC99 = 0x1,
96 KEYCXX = 0x2,
97 KEYCXX0X = 0x4,
98 KEYGNU = 0x8,
99 KEYMS = 0x10,
100 BOOLSUPPORT = 0x20,
101 KEYALTIVEC = 0x40,
102 KEYNOCXX = 0x80,
103 KEYBORLAND = 0x100,
104 KEYOPENCL = 0x200,
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000105 KEYC11 = 0x400,
John McCallf85e1932011-06-15 23:02:42 +0000106 KEYARC = 0x800,
Francois Picheta5a4cba2012-07-24 06:17:24 +0000107 KEYNOMS = 0x01000,
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000108 WCHARSUPPORT = 0x02000,
Francois Picheta5a4cba2012-07-24 06:17:24 +0000109 KEYALL = (0xffff & ~KEYNOMS) // Because KEYNOMS is used to exclude.
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000110 };
111}
112
Reid Spencer5f016e22007-07-11 17:01:13 +0000113/// AddKeyword - This method is used to associate a token ID with specific
114/// identifiers because they are language keywords. This causes the lexer to
115/// automatically map matching identifiers to specialized token codes.
116///
Richard Smith98d86b92011-10-11 19:57:52 +0000117/// The C90/C99/CPP/CPP0x flags are set to 3 if the token is a keyword in a
118/// future language standard, set to 2 if the token should be enabled in the
Nick Lewycky0be8fb52012-03-11 23:14:21 +0000119/// specified language, set to 1 if it is an extension in the specified
Richard Smith98d86b92011-10-11 19:57:52 +0000120/// language, and set to 0 if disabled in the specified language.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121static void AddKeyword(StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000122 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000124 unsigned AddResult = 0;
Dylan Noblesmith67922922011-04-09 13:34:05 +0000125 if (Flags == KEYALL) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000126 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
127 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
128 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000129 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000130 else if (LangOpts.MicrosoftExt && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000131 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000132 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000133 else if (LangOpts.WChar && (Flags & WCHARSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000134 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000135 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000136 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000137 else if (LangOpts.C11 && (Flags & KEYC11)) AddResult = 2;
Fariborz Jahanian00852e42011-12-19 21:06:15 +0000138 // We treat bridge casts as objective-C keywords so we can warn on them
139 // in non-arc mode.
140 else if (LangOpts.ObjC2 && (Flags & KEYARC)) AddResult = 2;
Richard Smith98d86b92011-10-11 19:57:52 +0000141 else if (LangOpts.CPlusPlus && (Flags & KEYCXX0X)) AddResult = 3;
142
Francois Pichetdfd110c2012-07-22 11:32:41 +0000143 // Don't add this keyword under MicrosoftMode.
144 if (LangOpts.MicrosoftMode && (Flags & KEYNOMS))
145 return;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000146 // Don't add this keyword if disabled in this language.
147 if (AddResult == 0) return;
148
Richard Smith98d86b92011-10-11 19:57:52 +0000149 IdentifierInfo &Info =
150 Table.get(Keyword, AddResult == 3 ? tok::identifier : TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000151 Info.setIsExtensionToken(AddResult == 1);
Richard Smith98d86b92011-10-11 19:57:52 +0000152 Info.setIsCXX11CompatKeyword(AddResult == 3);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153}
154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
156/// representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000157static void AddCXXOperatorKeyword(StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 tok::TokenKind TokenCode,
159 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000160 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000161 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000162}
163
James Dennett6557d132012-06-15 21:27:44 +0000164/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
165/// or "property".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000166static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000167 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000169 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000170}
171
172/// AddKeywords - Add all keywords to the symbol table.
173///
174void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 // Add keywords and tokens for the current language.
176#define KEYWORD(NAME, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000177 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000178 FLAGS, LangOpts, *this);
179#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000180 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000181 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
183 if (LangOpts.CXXOperatorNames) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000184 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185#define OBJC1_AT_KEYWORD(NAME) \
186 if (LangOpts.ObjC1) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188#define OBJC2_AT_KEYWORD(NAME) \
189 if (LangOpts.ObjC2) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000190 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000191#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000192#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000193
194 if (LangOpts.ParseUnknownAnytype)
195 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
196 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197}
198
Chris Lattner387b98d2007-10-07 07:52:34 +0000199tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
200 // We use a perfect hash function here involving the length of the keyword,
201 // the first and third character. For preprocessor ID's there are no
202 // collisions (if there were, the switch below would complain about duplicate
203 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattner387b98d2007-10-07 07:52:34 +0000205#define HASH(LEN, FIRST, THIRD) \
206 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
207#define CASE(LEN, FIRST, THIRD, NAME) \
208 case HASH(LEN, FIRST, THIRD): \
209 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattner387b98d2007-10-07 07:52:34 +0000211 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000212 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000213 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000214 switch (HASH(Len, Name[0], Name[2])) {
215 default: return tok::pp_not_keyword;
216 CASE( 2, 'i', '\0', if);
217 CASE( 4, 'e', 'i', elif);
218 CASE( 4, 'e', 's', else);
219 CASE( 4, 'l', 'n', line);
220 CASE( 4, 's', 'c', sccs);
221 CASE( 5, 'e', 'd', endif);
222 CASE( 5, 'e', 'r', error);
223 CASE( 5, 'i', 'e', ident);
224 CASE( 5, 'i', 'd', ifdef);
225 CASE( 5, 'u', 'd', undef);
226
227 CASE( 6, 'a', 's', assert);
228 CASE( 6, 'd', 'f', define);
229 CASE( 6, 'i', 'n', ifndef);
230 CASE( 6, 'i', 'p', import);
231 CASE( 6, 'p', 'a', pragma);
Douglas Gregor94ad28b2012-01-03 18:24:14 +0000232
Chris Lattner387b98d2007-10-07 07:52:34 +0000233 CASE( 7, 'd', 'f', defined);
234 CASE( 7, 'i', 'c', include);
235 CASE( 7, 'w', 'r', warning);
236
237 CASE( 8, 'u', 'a', unassert);
238 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Douglas Gregor1ac13c32012-01-03 19:48:16 +0000240 CASE(14, '_', 'p', __public_macro);
241
242 CASE(15, '_', 'p', __private_macro);
243
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000244 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000245#undef CASE
246#undef HASH
247 }
248}
Reid Spencer5f016e22007-07-11 17:01:13 +0000249
250//===----------------------------------------------------------------------===//
251// Stats Implementation
252//===----------------------------------------------------------------------===//
253
254/// PrintStats - Print statistics about how well the identifier table is doing
255/// at hashing identifiers.
256void IdentifierTable::PrintStats() const {
257 unsigned NumBuckets = HashTable.getNumBuckets();
258 unsigned NumIdentifiers = HashTable.getNumItems();
259 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
260 unsigned AverageIdentifierSize = 0;
261 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000264 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
266 unsigned IdLen = I->getKeyLength();
267 AverageIdentifierSize += IdLen;
268 if (MaxIdentifierLength < IdLen)
269 MaxIdentifierLength = IdLen;
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 fprintf(stderr, "\n*** Identifier Table Stats:\n");
273 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
274 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
275 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
276 NumIdentifiers/(double)NumBuckets);
277 fprintf(stderr, "Ave identifier length: %f\n",
278 (AverageIdentifierSize/(double)NumIdentifiers));
279 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 // Compute statistics about the memory allocated for identifiers.
282 HashTable.getAllocator().PrintStats();
283}
Steve Naroff68d331a2007-09-27 14:38:14 +0000284
Steve Naroff29238a02007-10-05 18:42:47 +0000285//===----------------------------------------------------------------------===//
286// SelectorTable Implementation
287//===----------------------------------------------------------------------===//
288
Chris Lattner85994262007-10-05 20:15:24 +0000289unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
290 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
291}
292
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000293namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000294/// MultiKeywordSelector - One of these variable length records is kept for each
295/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000296/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000297/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000298class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000299 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000300 MultiKeywordSelector(unsigned nKeys) {
301 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303public:
Steve Naroff29238a02007-10-05 18:42:47 +0000304 // Constructor for keyword selectors.
305 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
306 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000307 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Steve Naroff29238a02007-10-05 18:42:47 +0000309 // Fill in the trailing keyword array.
310 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
311 for (unsigned i = 0; i != nKeys; ++i)
312 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000313 }
314
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000315 // getName - Derive the full selector name and return it.
316 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000318 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Steve Naroff29238a02007-10-05 18:42:47 +0000320 typedef IdentifierInfo *const *keyword_iterator;
321 keyword_iterator keyword_begin() const {
322 return reinterpret_cast<keyword_iterator>(this+1);
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324 keyword_iterator keyword_end() const {
325 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000326 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000327 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000328 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000329 return keyword_begin()[i];
330 }
Mike Stump1eb44332009-09-09 15:08:12 +0000331 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000332 keyword_iterator ArgTys, unsigned NumArgs) {
333 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000334 for (unsigned i = 0; i != NumArgs; ++i)
335 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000336 }
337 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000338 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000339 }
340};
Chris Lattner85994262007-10-05 20:15:24 +0000341} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000342
343unsigned Selector::getNumArgs() const {
344 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor51603be2012-05-04 18:24:37 +0000345 if (IIF <= ZeroArg)
Steve Naroff29238a02007-10-05 18:42:47 +0000346 return 0;
347 if (IIF == OneArg)
348 return 1;
Douglas Gregor51603be2012-05-04 18:24:37 +0000349 // We point to a MultiKeywordSelector.
350 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump1eb44332009-09-09 15:08:12 +0000351 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000352}
353
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000354IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor51603be2012-05-04 18:24:37 +0000355 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000356 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000357 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000358 }
Douglas Gregor51603be2012-05-04 18:24:37 +0000359 // We point to a MultiKeywordSelector.
360 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroff29238a02007-10-05 18:42:47 +0000361 return SI->getIdentifierInfoForSlot(argIndex);
362}
363
Chris Lattner5f9e2722011-07-23 10:55:15 +0000364StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregor813d8342011-02-18 22:29:55 +0000365 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000366 return II? II->getName() : StringRef();
Douglas Gregor813d8342011-02-18 22:29:55 +0000367}
368
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000369std::string MultiKeywordSelector::getName() const {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000370 SmallString<256> Str;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000371 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000372 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
373 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000374 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000375 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000378 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000379}
380
Chris Lattner077bf5e2008-11-24 03:33:13 +0000381std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000382 if (InfoPtr == 0)
383 return "<null selector>";
384
Douglas Gregor51603be2012-05-04 18:24:37 +0000385 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000386 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek150ec292009-03-07 01:22:02 +0000388 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000389 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000390 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000391
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000392 if (!II)
393 return ":";
394
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000395 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Douglas Gregor51603be2012-05-04 18:24:37 +0000398 // We have a multiple keyword selector.
399 return getMultiKeywordSelector()->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000400}
401
John McCall85f3d762011-03-02 01:50:55 +0000402/// Interpreting the given string using the normal CamelCase
403/// conventions, determine whether the given string starts with the
404/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000405static bool startsWithWord(StringRef name, StringRef word) {
John McCall85f3d762011-03-02 01:50:55 +0000406 if (name.size() < word.size()) return false;
407 return ((name.size() == word.size() ||
408 !islower(name[word.size()]))
409 && name.startswith(word));
410}
411
412ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
413 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
414 if (!first) return OMF_None;
415
Chris Lattner5f9e2722011-07-23 10:55:15 +0000416 StringRef name = first->getName();
John McCall85f3d762011-03-02 01:50:55 +0000417 if (sel.isUnarySelector()) {
418 if (name == "autorelease") return OMF_autorelease;
419 if (name == "dealloc") return OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000420 if (name == "finalize") return OMF_finalize;
John McCall85f3d762011-03-02 01:50:55 +0000421 if (name == "release") return OMF_release;
422 if (name == "retain") return OMF_retain;
423 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000424 if (name == "self") return OMF_self;
John McCall85f3d762011-03-02 01:50:55 +0000425 }
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000426
427 if (name == "performSelector") return OMF_performSelector;
John McCall85f3d762011-03-02 01:50:55 +0000428
429 // The other method families may begin with a prefix of underscores.
430 while (!name.empty() && name.front() == '_')
431 name = name.substr(1);
432
433 if (name.empty()) return OMF_None;
434 switch (name.front()) {
435 case 'a':
436 if (startsWithWord(name, "alloc")) return OMF_alloc;
437 break;
438 case 'c':
439 if (startsWithWord(name, "copy")) return OMF_copy;
440 break;
441 case 'i':
442 if (startsWithWord(name, "init")) return OMF_init;
443 break;
444 case 'm':
445 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
446 break;
447 case 'n':
448 if (startsWithWord(name, "new")) return OMF_new;
449 break;
450 default:
451 break;
452 }
453
454 return OMF_None;
455}
Steve Naroff29238a02007-10-05 18:42:47 +0000456
Chris Lattner5f7d2282009-03-04 05:35:38 +0000457namespace {
458 struct SelectorTableImpl {
459 llvm::FoldingSet<MultiKeywordSelector> Table;
460 llvm::BumpPtrAllocator Allocator;
461 };
462} // end anonymous namespace.
463
464static SelectorTableImpl &getSelectorTableImpl(void *P) {
465 return *static_cast<SelectorTableImpl*>(P);
466}
467
Benjamin Kramer8fe83e12012-02-04 13:45:25 +0000468/*static*/ Selector
469SelectorTable::constructSetterName(IdentifierTable &Idents,
470 SelectorTable &SelTable,
471 const IdentifierInfo *Name) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000472 SmallString<100> SelectorName;
Benjamin Kramer8fe83e12012-02-04 13:45:25 +0000473 SelectorName = "set";
474 SelectorName += Name->getName();
475 SelectorName[3] = toupper(SelectorName[3]);
476 IdentifierInfo *SetterName = &Idents.get(SelectorName);
477 return SelTable.getUnarySelector(SetterName);
478}
479
Ted Kremenek97f55d62011-04-18 22:47:04 +0000480size_t SelectorTable::getTotalMemory() const {
481 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
482 return SelTabImpl.Allocator.getTotalMemory();
483}
Chris Lattner5f7d2282009-03-04 05:35:38 +0000484
Chris Lattnerff384912007-10-07 02:00:24 +0000485Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
486 if (nKeys < 2)
487 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattner5f7d2282009-03-04 05:35:38 +0000489 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Steve Naroff29238a02007-10-05 18:42:47 +0000491 // Unique selector, to guarantee there is one per name.
492 llvm::FoldingSetNodeID ID;
493 MultiKeywordSelector::Profile(ID, IIV, nKeys);
494
495 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000496 if (MultiKeywordSelector *SI =
497 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000498 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Steve Naroff29238a02007-10-05 18:42:47 +0000500 // MultiKeywordSelector objects are not allocated with new because they have a
501 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000502 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
503 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000504 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000505 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000506 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000507 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000508 return Selector(SI);
509}
510
Steve Naroff29238a02007-10-05 18:42:47 +0000511SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000512 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000513}
514
515SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000516 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000517}
518
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000519const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
520 switch (Operator) {
521 case OO_None:
522 case NUM_OVERLOADED_OPERATORS:
523 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000524
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000525#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
526 case OO_##Name: return Spelling;
527#include "clang/Basic/OperatorKinds.def"
528 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000529
David Blaikie7530c032012-01-17 06:56:22 +0000530 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000531}