blob: 500e732eef3478ee6e4f4fead599bb7c7acb493b [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"
Adrian Prantl80e8ea92013-06-07 22:29:12 +000017#include "clang/Basic/CharInfo.h"
Chris Lattner85994262007-10-05 20:15:24 +000018#include "llvm/ADT/DenseMap.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "llvm/ADT/FoldingSet.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"
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;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +000034 HadMacro = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000035 IsExtension = false;
Richard Smith98d86b92011-10-11 19:57:52 +000036 IsCXX11CompatKeyword = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 IsPoisoned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000038 IsCPPOperatorKeyword = false;
Chris Lattner6a170eb2009-01-21 07:43:11 +000039 NeedsHandleIdentifier = false;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000040 IsFromAST = false;
Douglas Gregoreee242f2011-10-27 09:33:13 +000041 ChangedAfterLoad = false;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +000042 RevertedTokenID = false;
Douglas Gregoreee242f2011-10-27 09:33:13 +000043 OutOfDate = false;
Ted Kremenek32ad2ee2012-03-01 22:07:04 +000044 IsModulesImport = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000045 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000046 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000047}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049//===----------------------------------------------------------------------===//
50// IdentifierTable Implementation
51//===----------------------------------------------------------------------===//
52
Douglas Gregor95f42922010-10-14 22:11:03 +000053IdentifierIterator::~IdentifierIterator() { }
54
Ted Kremenek72b1b152009-01-15 18:47:46 +000055IdentifierInfoLookup::~IdentifierInfoLookup() {}
56
Douglas Gregor95f42922010-10-14 22:11:03 +000057namespace {
58 /// \brief A simple identifier lookup iterator that represents an
59 /// empty sequence of identifiers.
60 class EmptyLookupIterator : public IdentifierIterator
61 {
62 public:
Chris Lattner5f9e2722011-07-23 10:55:15 +000063 virtual StringRef Next() { return StringRef(); }
Douglas Gregor95f42922010-10-14 22:11:03 +000064 };
65}
66
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +000067IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
Douglas Gregor95f42922010-10-14 22:11:03 +000068 return new EmptyLookupIterator();
69}
70
Douglas Gregor8c5a7602009-04-25 23:30:02 +000071ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
72
Ted Kremenek72b1b152009-01-15 18:47:46 +000073IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
74 IdentifierInfoLookup* externalLookup)
75 : HashTable(8192), // Start with space for 8K identifiers.
76 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000077
78 // Populate the identifier table with info about keywords for the current
79 // language.
80 AddKeywords(LangOpts);
Ted Kremenekf15e1142012-03-01 22:53:32 +000081
82
83 // Add the '_experimental_modules_import' contextual keyword.
Douglas Gregor1b257af2012-12-11 22:11:52 +000084 get("import").setModulesImport(true);
Reid Spencer5f016e22007-07-11 17:01:13 +000085}
86
87//===----------------------------------------------------------------------===//
88// Language Keyword Implementation
89//===----------------------------------------------------------------------===//
90
Eli Friedmaneb32fde2009-04-28 03:13:54 +000091// Constants for TokenKinds.def
92namespace {
93 enum {
Dylan Noblesmith67922922011-04-09 13:34:05 +000094 KEYC99 = 0x1,
95 KEYCXX = 0x2,
Richard Smith4e24f0f2013-01-02 12:01:23 +000096 KEYCXX11 = 0x4,
Dylan Noblesmith67922922011-04-09 13:34:05 +000097 KEYGNU = 0x8,
98 KEYMS = 0x10,
99 BOOLSUPPORT = 0x20,
100 KEYALTIVEC = 0x40,
101 KEYNOCXX = 0x80,
102 KEYBORLAND = 0x100,
103 KEYOPENCL = 0x200,
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000104 KEYC11 = 0x400,
John McCallf85e1932011-06-15 23:02:42 +0000105 KEYARC = 0x800,
Francois Picheta5a4cba2012-07-24 06:17:24 +0000106 KEYNOMS = 0x01000,
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000107 WCHARSUPPORT = 0x02000,
Francois Picheta5a4cba2012-07-24 06:17:24 +0000108 KEYALL = (0xffff & ~KEYNOMS) // Because KEYNOMS is used to exclude.
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000109 };
110}
111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112/// AddKeyword - This method is used to associate a token ID with specific
113/// identifiers because they are language keywords. This causes the lexer to
114/// automatically map matching identifiers to specialized token codes.
115///
Richard Smith98d86b92011-10-11 19:57:52 +0000116/// The C90/C99/CPP/CPP0x flags are set to 3 if the token is a keyword in a
117/// future language standard, set to 2 if the token should be enabled in the
Nick Lewycky0be8fb52012-03-11 23:14:21 +0000118/// specified language, set to 1 if it is an extension in the specified
Richard Smith98d86b92011-10-11 19:57:52 +0000119/// language, and set to 0 if disabled in the specified language.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000120static void AddKeyword(StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000121 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000123 unsigned AddResult = 0;
Dylan Noblesmith67922922011-04-09 13:34:05 +0000124 if (Flags == KEYALL) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000125 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
Richard Smith4e24f0f2013-01-02 12:01:23 +0000126 else if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000127 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000128 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000129 else if (LangOpts.MicrosoftExt && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000130 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000131 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000132 else if (LangOpts.WChar && (Flags & WCHARSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000133 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000134 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000135 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000136 else if (LangOpts.C11 && (Flags & KEYC11)) AddResult = 2;
Fariborz Jahanian00852e42011-12-19 21:06:15 +0000137 // We treat bridge casts as objective-C keywords so we can warn on them
138 // in non-arc mode.
139 else if (LangOpts.ObjC2 && (Flags & KEYARC)) AddResult = 2;
Richard Smith4e24f0f2013-01-02 12:01:23 +0000140 else if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) AddResult = 3;
Richard Smith98d86b92011-10-11 19:57:52 +0000141
Francois Pichetdfd110c2012-07-22 11:32:41 +0000142 // Don't add this keyword under MicrosoftMode.
143 if (LangOpts.MicrosoftMode && (Flags & KEYNOMS))
144 return;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000145 // Don't add this keyword if disabled in this language.
146 if (AddResult == 0) return;
147
Richard Smith98d86b92011-10-11 19:57:52 +0000148 IdentifierInfo &Info =
149 Table.get(Keyword, AddResult == 3 ? tok::identifier : TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000150 Info.setIsExtensionToken(AddResult == 1);
Richard Smith98d86b92011-10-11 19:57:52 +0000151 Info.setIsCXX11CompatKeyword(AddResult == 3);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152}
153
Reid Spencer5f016e22007-07-11 17:01:13 +0000154/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
155/// representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000156static void AddCXXOperatorKeyword(StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 tok::TokenKind TokenCode,
158 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000159 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000160 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000161}
162
James Dennett6557d132012-06-15 21:27:44 +0000163/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
164/// or "property".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000165static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000166 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000168 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169}
170
171/// AddKeywords - Add all keywords to the symbol table.
172///
173void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 // Add keywords and tokens for the current language.
175#define KEYWORD(NAME, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000176 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000177 FLAGS, LangOpts, *this);
178#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000179 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000180 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
182 if (LangOpts.CXXOperatorNames) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000183 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000184#define OBJC1_AT_KEYWORD(NAME) \
185 if (LangOpts.ObjC1) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000186 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000187#define OBJC2_AT_KEYWORD(NAME) \
188 if (LangOpts.ObjC2) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000189 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000190#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000191#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000192
193 if (LangOpts.ParseUnknownAnytype)
194 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
195 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000196}
197
Chris Lattner387b98d2007-10-07 07:52:34 +0000198tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
199 // We use a perfect hash function here involving the length of the keyword,
200 // the first and third character. For preprocessor ID's there are no
201 // collisions (if there were, the switch below would complain about duplicate
202 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattner387b98d2007-10-07 07:52:34 +0000204#define HASH(LEN, FIRST, THIRD) \
205 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
206#define CASE(LEN, FIRST, THIRD, NAME) \
207 case HASH(LEN, FIRST, THIRD): \
208 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Chris Lattner387b98d2007-10-07 07:52:34 +0000210 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000211 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000212 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000213 switch (HASH(Len, Name[0], Name[2])) {
214 default: return tok::pp_not_keyword;
215 CASE( 2, 'i', '\0', if);
216 CASE( 4, 'e', 'i', elif);
217 CASE( 4, 'e', 's', else);
218 CASE( 4, 'l', 'n', line);
219 CASE( 4, 's', 'c', sccs);
220 CASE( 5, 'e', 'd', endif);
221 CASE( 5, 'e', 'r', error);
222 CASE( 5, 'i', 'e', ident);
223 CASE( 5, 'i', 'd', ifdef);
224 CASE( 5, 'u', 'd', undef);
225
226 CASE( 6, 'a', 's', assert);
227 CASE( 6, 'd', 'f', define);
228 CASE( 6, 'i', 'n', ifndef);
229 CASE( 6, 'i', 'p', import);
230 CASE( 6, 'p', 'a', pragma);
Douglas Gregor94ad28b2012-01-03 18:24:14 +0000231
Chris Lattner387b98d2007-10-07 07:52:34 +0000232 CASE( 7, 'd', 'f', defined);
233 CASE( 7, 'i', 'c', include);
234 CASE( 7, 'w', 'r', warning);
235
236 CASE( 8, 'u', 'a', unassert);
237 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Douglas Gregor1ac13c32012-01-03 19:48:16 +0000239 CASE(14, '_', 'p', __public_macro);
240
241 CASE(15, '_', 'p', __private_macro);
242
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000243 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000244#undef CASE
245#undef HASH
246 }
247}
Reid Spencer5f016e22007-07-11 17:01:13 +0000248
249//===----------------------------------------------------------------------===//
250// Stats Implementation
251//===----------------------------------------------------------------------===//
252
253/// PrintStats - Print statistics about how well the identifier table is doing
254/// at hashing identifiers.
255void IdentifierTable::PrintStats() const {
256 unsigned NumBuckets = HashTable.getNumBuckets();
257 unsigned NumIdentifiers = HashTable.getNumItems();
258 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
259 unsigned AverageIdentifierSize = 0;
260 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000263 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
265 unsigned IdLen = I->getKeyLength();
266 AverageIdentifierSize += IdLen;
267 if (MaxIdentifierLength < IdLen)
268 MaxIdentifierLength = IdLen;
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 fprintf(stderr, "\n*** Identifier Table Stats:\n");
272 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
273 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
274 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
275 NumIdentifiers/(double)NumBuckets);
276 fprintf(stderr, "Ave identifier length: %f\n",
277 (AverageIdentifierSize/(double)NumIdentifiers));
278 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // Compute statistics about the memory allocated for identifiers.
281 HashTable.getAllocator().PrintStats();
282}
Steve Naroff68d331a2007-09-27 14:38:14 +0000283
Steve Naroff29238a02007-10-05 18:42:47 +0000284//===----------------------------------------------------------------------===//
285// SelectorTable Implementation
286//===----------------------------------------------------------------------===//
287
Chris Lattner85994262007-10-05 20:15:24 +0000288unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
289 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
290}
291
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000292namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000293/// MultiKeywordSelector - One of these variable length records is kept for each
294/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000295/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000296/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000297class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000298 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000299 MultiKeywordSelector(unsigned nKeys) {
300 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302public:
Steve Naroff29238a02007-10-05 18:42:47 +0000303 // Constructor for keyword selectors.
304 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
305 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000306 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Steve Naroff29238a02007-10-05 18:42:47 +0000308 // Fill in the trailing keyword array.
309 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
310 for (unsigned i = 0; i != nKeys; ++i)
311 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000312 }
313
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000314 // getName - Derive the full selector name and return it.
315 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000317 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Steve Naroff29238a02007-10-05 18:42:47 +0000319 typedef IdentifierInfo *const *keyword_iterator;
320 keyword_iterator keyword_begin() const {
321 return reinterpret_cast<keyword_iterator>(this+1);
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323 keyword_iterator keyword_end() const {
324 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000325 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000326 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000327 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000328 return keyword_begin()[i];
329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000331 keyword_iterator ArgTys, unsigned NumArgs) {
332 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000333 for (unsigned i = 0; i != NumArgs; ++i)
334 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000335 }
336 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000337 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000338 }
339};
Chris Lattner85994262007-10-05 20:15:24 +0000340} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000341
342unsigned Selector::getNumArgs() const {
343 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor51603be2012-05-04 18:24:37 +0000344 if (IIF <= ZeroArg)
Steve Naroff29238a02007-10-05 18:42:47 +0000345 return 0;
346 if (IIF == OneArg)
347 return 1;
Douglas Gregor51603be2012-05-04 18:24:37 +0000348 // We point to a MultiKeywordSelector.
349 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump1eb44332009-09-09 15:08:12 +0000350 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000351}
352
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000353IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor51603be2012-05-04 18:24:37 +0000354 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000355 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000356 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000357 }
Douglas Gregor51603be2012-05-04 18:24:37 +0000358 // We point to a MultiKeywordSelector.
359 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroff29238a02007-10-05 18:42:47 +0000360 return SI->getIdentifierInfoForSlot(argIndex);
361}
362
Chris Lattner5f9e2722011-07-23 10:55:15 +0000363StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregor813d8342011-02-18 22:29:55 +0000364 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000365 return II? II->getName() : StringRef();
Douglas Gregor813d8342011-02-18 22:29:55 +0000366}
367
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000368std::string MultiKeywordSelector::getName() const {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000369 SmallString<256> Str;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000370 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000371 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
372 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000373 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000374 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000377 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000378}
379
Chris Lattner077bf5e2008-11-24 03:33:13 +0000380std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000381 if (InfoPtr == 0)
382 return "<null selector>";
383
Douglas Gregor51603be2012-05-04 18:24:37 +0000384 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000385 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek150ec292009-03-07 01:22:02 +0000387 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000388 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000389 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000390
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000391 if (!II)
392 return ":";
393
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000394 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Douglas Gregor51603be2012-05-04 18:24:37 +0000397 // We have a multiple keyword selector.
398 return getMultiKeywordSelector()->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000399}
400
John McCall85f3d762011-03-02 01:50:55 +0000401/// Interpreting the given string using the normal CamelCase
402/// conventions, determine whether the given string starts with the
403/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000404static bool startsWithWord(StringRef name, StringRef word) {
John McCall85f3d762011-03-02 01:50:55 +0000405 if (name.size() < word.size()) return false;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000406 return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
407 name.startswith(word));
John McCall85f3d762011-03-02 01:50:55 +0000408}
409
410ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
411 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
412 if (!first) return OMF_None;
413
Chris Lattner5f9e2722011-07-23 10:55:15 +0000414 StringRef name = first->getName();
John McCall85f3d762011-03-02 01:50:55 +0000415 if (sel.isUnarySelector()) {
416 if (name == "autorelease") return OMF_autorelease;
417 if (name == "dealloc") return OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000418 if (name == "finalize") return OMF_finalize;
John McCall85f3d762011-03-02 01:50:55 +0000419 if (name == "release") return OMF_release;
420 if (name == "retain") return OMF_retain;
421 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000422 if (name == "self") return OMF_self;
John McCall85f3d762011-03-02 01:50:55 +0000423 }
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000424
425 if (name == "performSelector") return OMF_performSelector;
John McCall85f3d762011-03-02 01:50:55 +0000426
427 // The other method families may begin with a prefix of underscores.
428 while (!name.empty() && name.front() == '_')
429 name = name.substr(1);
430
431 if (name.empty()) return OMF_None;
432 switch (name.front()) {
433 case 'a':
434 if (startsWithWord(name, "alloc")) return OMF_alloc;
435 break;
436 case 'c':
437 if (startsWithWord(name, "copy")) return OMF_copy;
438 break;
439 case 'i':
440 if (startsWithWord(name, "init")) return OMF_init;
441 break;
442 case 'm':
443 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
444 break;
445 case 'n':
446 if (startsWithWord(name, "new")) return OMF_new;
447 break;
448 default:
449 break;
450 }
451
452 return OMF_None;
453}
Steve Naroff29238a02007-10-05 18:42:47 +0000454
Fariborz Jahanian11638f72013-07-23 22:42:28 +0000455ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000456 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
457 if (!first) return OIT_None;
458
459 StringRef name = first->getName();
460
461 if (name.empty()) return OIT_None;
462 switch (name.front()) {
463 case 'a':
Fariborz Jahaniand0f29212013-08-29 16:22:26 +0000464 if (startsWithWord(name, "array")) return OIT_Array;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000465 break;
466 case 'd':
Fariborz Jahanian9fcbd5e2013-10-10 18:23:13 +0000467 if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000468 if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
469 break;
Fariborz Jahanian8d3794e2013-08-02 20:54:18 +0000470 case 's':
Fariborz Jahanian9fcbd5e2013-10-10 18:23:13 +0000471 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
472 if (startsWithWord(name, "standard")) return OIT_Singleton;
Fariborz Jahaniana346eb12013-09-18 20:35:47 +0000473 case 'i':
474 if (startsWithWord(name, "init")) return OIT_Init;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000475 default:
476 break;
477 }
478 return OIT_None;
479}
480
Chris Lattner5f7d2282009-03-04 05:35:38 +0000481namespace {
482 struct SelectorTableImpl {
483 llvm::FoldingSet<MultiKeywordSelector> Table;
484 llvm::BumpPtrAllocator Allocator;
485 };
486} // end anonymous namespace.
487
488static SelectorTableImpl &getSelectorTableImpl(void *P) {
489 return *static_cast<SelectorTableImpl*>(P);
490}
491
Adrian Prantl70446682013-06-10 21:36:55 +0000492SmallString<64>
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000493SelectorTable::constructSetterName(StringRef Name) {
Adrian Prantl70446682013-06-10 21:36:55 +0000494 SmallString<64> SetterName("set");
495 SetterName += Name;
496 SetterName[3] = toUppercase(SetterName[3]);
497 return SetterName;
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000498}
499
Adrian Prantl70446682013-06-10 21:36:55 +0000500Selector
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000501SelectorTable::constructSetterSelector(IdentifierTable &Idents,
502 SelectorTable &SelTable,
503 const IdentifierInfo *Name) {
504 IdentifierInfo *SetterName =
505 &Idents.get(constructSetterName(Name->getName()));
Benjamin Kramer8fe83e12012-02-04 13:45:25 +0000506 return SelTable.getUnarySelector(SetterName);
507}
508
Ted Kremenek97f55d62011-04-18 22:47:04 +0000509size_t SelectorTable::getTotalMemory() const {
510 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
511 return SelTabImpl.Allocator.getTotalMemory();
512}
Chris Lattner5f7d2282009-03-04 05:35:38 +0000513
Chris Lattnerff384912007-10-07 02:00:24 +0000514Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
515 if (nKeys < 2)
516 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattner5f7d2282009-03-04 05:35:38 +0000518 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Steve Naroff29238a02007-10-05 18:42:47 +0000520 // Unique selector, to guarantee there is one per name.
521 llvm::FoldingSetNodeID ID;
522 MultiKeywordSelector::Profile(ID, IIV, nKeys);
523
524 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000525 if (MultiKeywordSelector *SI =
526 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000527 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Steve Naroff29238a02007-10-05 18:42:47 +0000529 // MultiKeywordSelector objects are not allocated with new because they have a
530 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000531 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
532 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000533 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000534 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000535 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000536 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000537 return Selector(SI);
538}
539
Steve Naroff29238a02007-10-05 18:42:47 +0000540SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000541 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000542}
543
544SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000545 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000546}
547
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000548const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
549 switch (Operator) {
550 case OO_None:
551 case NUM_OVERLOADED_OPERATORS:
552 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000553
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000554#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
555 case OO_##Name: return Spelling;
556#include "clang/Basic/OperatorKinds.def"
557 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000558
David Blaikie7530c032012-01-17 06:56:22 +0000559 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000560}