blob: 0993b2335e4bc19b39dc0b1cb31dd26cd3d0d90a [file] [log] [blame]
Chris Lattnerec659fc2006-10-29 22:09:44 +00001//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000010// This file implements the IdentifierInfo, IdentifierVisitor, and
Chris Lattner91cbf112006-07-03 04:28:52 +000011// IdentifierTable interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattneref6b1362007-10-07 08:58:51 +000015#include "clang/Basic/IdentifierTable.h"
Adrian Prantla4ce9062013-06-07 22:29:12 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000017#include "clang/Basic/LangOptions.h"
Chris Lattnerdadc7622007-10-05 20:15:24 +000018#include "llvm/ADT/DenseMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "llvm/ADT/FoldingSet.h"
David Blaikie8a40f702012-01-17 06:56:22 +000020#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000022#include <cstdio>
Ted Kremenekf25f4a32007-10-23 22:18:37 +000023
Chris Lattner22eb9722006-06-18 05:43:12 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000027// IdentifierInfo Implementation
Chris Lattner22eb9722006-06-18 05:43:12 +000028//===----------------------------------------------------------------------===//
29
Ted Kremenek52f73ca2009-01-20 23:28:34 +000030IdentifierInfo::IdentifierInfo() {
Chris Lattner3bc804e2006-10-28 23:46:24 +000031 TokenID = tok::identifier;
Douglas Gregor2ad7ee92008-11-06 16:32:23 +000032 ObjCOrBuiltinID = 0;
Chris Lattnera441ca62007-10-07 07:09:52 +000033 HasMacro = false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +000034 HadMacro = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000035 IsExtension = false;
Richard Smith4dd85d62011-10-11 19:57:52 +000036 IsCXX11CompatKeyword = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000037 IsPoisoned = false;
Chris Lattner5b9f4892006-11-21 17:23:33 +000038 IsCPPOperatorKeyword = false;
Chris Lattnerad89ec02009-01-21 07:43:11 +000039 NeedsHandleIdentifier = false;
Sebastian Redld44cd6a2010-08-18 23:57:06 +000040 IsFromAST = false;
Douglas Gregor935bc7a22011-10-27 09:33:13 +000041 ChangedAfterLoad = false;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +000042 RevertedTokenID = false;
Douglas Gregor935bc7a22011-10-27 09:33:13 +000043 OutOfDate = false;
Ted Kremenekc1e4dd02012-03-01 22:07:04 +000044 IsModulesImport = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000045 FETokenInfo = 0;
Ted Kremenek52f73ca2009-01-20 23:28:34 +000046 Entry = 0;
Chris Lattner3bc804e2006-10-28 23:46:24 +000047}
48
Chris Lattner91cbf112006-07-03 04:28:52 +000049//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +000050// IdentifierTable Implementation
51//===----------------------------------------------------------------------===//
52
Douglas Gregor57756ea2010-10-14 22:11:03 +000053IdentifierIterator::~IdentifierIterator() { }
54
Ted Kremeneka705b042009-01-15 18:47:46 +000055IdentifierInfoLookup::~IdentifierInfoLookup() {}
56
Douglas Gregor57756ea2010-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:
Craig Topper3164f332014-03-11 03:39:26 +000063 StringRef Next() override { return StringRef(); }
Douglas Gregor57756ea2010-10-14 22:11:03 +000064 };
65}
66
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +000067IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
Douglas Gregor57756ea2010-10-14 22:11:03 +000068 return new EmptyLookupIterator();
69}
70
Douglas Gregor99734e72009-04-25 23:30:02 +000071ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
72
Ted Kremeneka705b042009-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) {
Chris Lattnerda933aa2006-10-29 23:49:15 +000077
Chris Lattnerf2e3ac32006-10-27 03:59:10 +000078 // Populate the identifier table with info about keywords for the current
79 // language.
Chris Lattner25e0d542006-10-18 06:07:05 +000080 AddKeywords(LangOpts);
Ted Kremenek94666032012-03-01 22:53:32 +000081
82
83 // Add the '_experimental_modules_import' contextual keyword.
Douglas Gregorc50d4922012-12-11 22:11:52 +000084 get("import").setModulesImport(true);
Chris Lattner91cbf112006-07-03 04:28:52 +000085}
Chris Lattner22eb9722006-06-18 05:43:12 +000086
Chris Lattner25e0d542006-10-18 06:07:05 +000087//===----------------------------------------------------------------------===//
88// Language Keyword Implementation
89//===----------------------------------------------------------------------===//
90
Eli Friedman2b680b42009-04-28 03:13:54 +000091// Constants for TokenKinds.def
92namespace {
93 enum {
Dylan Noblesmith92c07c22011-04-09 13:34:05 +000094 KEYC99 = 0x1,
95 KEYCXX = 0x2,
Richard Smith89645bc2013-01-02 12:01:23 +000096 KEYCXX11 = 0x4,
Dylan Noblesmith92c07c22011-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 Kramere56f3932011-12-23 17:00:35 +0000104 KEYC11 = 0x400,
John McCall31168b02011-06-15 23:02:42 +0000105 KEYARC = 0x800,
Francois Pichet1c7207c2012-07-24 06:17:24 +0000106 KEYNOMS = 0x01000,
Abramo Bagnara73bf7f52012-09-05 17:30:57 +0000107 WCHARSUPPORT = 0x02000,
Yunzhong Gaofcdc45f2014-03-18 17:55:18 +0000108 HALFSUPPORT = 0x04000,
Francois Pichet1c7207c2012-07-24 06:17:24 +0000109 KEYALL = (0xffff & ~KEYNOMS) // Because KEYNOMS is used to exclude.
Eli Friedman2b680b42009-04-28 03:13:54 +0000110 };
111}
112
Chris Lattner25e0d542006-10-18 06:07:05 +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 Smith4dd85d62011-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 Lewycky74655a52012-03-11 23:14:21 +0000119/// specified language, set to 1 if it is an extension in the specified
Richard Smith4dd85d62011-10-11 19:57:52 +0000120/// language, and set to 0 if disabled in the specified language.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000121static void AddKeyword(StringRef Keyword,
Eli Friedman2b680b42009-04-28 03:13:54 +0000122 tok::TokenKind TokenCode, unsigned Flags,
Chris Lattner25e0d542006-10-18 06:07:05 +0000123 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedman2b680b42009-04-28 03:13:54 +0000124 unsigned AddResult = 0;
Dylan Noblesmith92c07c22011-04-09 13:34:05 +0000125 if (Flags == KEYALL) AddResult = 2;
Eli Friedman2b680b42009-04-28 03:13:54 +0000126 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
Richard Smith89645bc2013-01-02 12:01:23 +0000127 else if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) AddResult = 2;
Eli Friedman2b680b42009-04-28 03:13:54 +0000128 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carruthe03aa552010-04-17 20:17:31 +0000129 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Francois Pichet0706d202011-09-17 17:15:52 +0000130 else if (LangOpts.MicrosoftExt && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000131 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnerc61089a2009-06-30 01:26:17 +0000132 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
Yunzhong Gaofcdc45f2014-03-18 17:55:18 +0000133 else if (LangOpts.Half && (Flags && HALFSUPPORT)) AddResult = 2;
Abramo Bagnara73bf7f52012-09-05 17:30:57 +0000134 else if (LangOpts.WChar && (Flags & WCHARSUPPORT)) AddResult = 2;
John Thompson22334602010-02-05 00:12:22 +0000135 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000136 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregorf10c97f2010-10-13 20:00:38 +0000137 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Benjamin Kramere56f3932011-12-23 17:00:35 +0000138 else if (LangOpts.C11 && (Flags & KEYC11)) AddResult = 2;
Fariborz Jahanian103ae5c2011-12-19 21:06:15 +0000139 // We treat bridge casts as objective-C keywords so we can warn on them
140 // in non-arc mode.
141 else if (LangOpts.ObjC2 && (Flags & KEYARC)) AddResult = 2;
Richard Smith89645bc2013-01-02 12:01:23 +0000142 else if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) AddResult = 3;
Richard Smith4dd85d62011-10-11 19:57:52 +0000143
Alp Tokerbfa39342014-01-14 12:51:41 +0000144 // Don't add this keyword under MSVCCompat.
145 if (LangOpts.MSVCCompat && (Flags & KEYNOMS))
Francois Pichet0e2b8432012-07-22 11:32:41 +0000146 return;
Eli Friedman2b680b42009-04-28 03:13:54 +0000147 // Don't add this keyword if disabled in this language.
148 if (AddResult == 0) return;
149
Richard Smith4dd85d62011-10-11 19:57:52 +0000150 IdentifierInfo &Info =
151 Table.get(Keyword, AddResult == 3 ? tok::identifier : TokenCode);
Eli Friedman2b680b42009-04-28 03:13:54 +0000152 Info.setIsExtensionToken(AddResult == 1);
Richard Smith4dd85d62011-10-11 19:57:52 +0000153 Info.setIsCXX11CompatKeyword(AddResult == 3);
Chris Lattner25e0d542006-10-18 06:07:05 +0000154}
155
Chris Lattner5b9f4892006-11-21 17:23:33 +0000156/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
157/// representations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000158static void AddCXXOperatorKeyword(StringRef Keyword,
Chris Lattner5b9f4892006-11-21 17:23:33 +0000159 tok::TokenKind TokenCode,
160 IdentifierTable &Table) {
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000161 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekf25f4a32007-10-23 22:18:37 +0000162 Info.setIsCPlusPlusOperatorKeyword();
Chris Lattner5b9f4892006-11-21 17:23:33 +0000163}
164
James Dennett0d8a3f82012-06-15 21:27:44 +0000165/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
166/// or "property".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000167static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000168 tok::ObjCKeywordKind ObjCID,
Chris Lattner25e0d542006-10-18 06:07:05 +0000169 IdentifierTable &Table) {
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000170 Table.get(Name).setObjCKeywordID(ObjCID);
Chris Lattner25e0d542006-10-18 06:07:05 +0000171}
172
173/// AddKeywords - Add all keywords to the symbol table.
174///
175void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Chris Lattner25e0d542006-10-18 06:07:05 +0000176 // Add keywords and tokens for the current language.
177#define KEYWORD(NAME, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000178 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000179 FLAGS, LangOpts, *this);
180#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000181 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000182 FLAGS, LangOpts, *this);
Chris Lattner5b9f4892006-11-21 17:23:33 +0000183#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
Chris Lattner3e7592e2006-12-04 07:48:37 +0000184 if (LangOpts.CXXOperatorNames) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000185 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000186#define OBJC1_AT_KEYWORD(NAME) \
187 if (LangOpts.ObjC1) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000188 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000189#define OBJC2_AT_KEYWORD(NAME) \
190 if (LangOpts.ObjC2) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000191 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCall39439732011-04-09 22:50:59 +0000192#define TESTING_KEYWORD(NAME, FLAGS)
Chris Lattner25e0d542006-10-18 06:07:05 +0000193#include "clang/Basic/TokenKinds.def"
John McCall39439732011-04-09 22:50:59 +0000194
195 if (LangOpts.ParseUnknownAnytype)
196 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
197 LangOpts, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000198}
199
Chris Lattnerff067ce2007-10-07 07:52:34 +0000200tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
201 // We use a perfect hash function here involving the length of the keyword,
202 // the first and third character. For preprocessor ID's there are no
203 // collisions (if there were, the switch below would complain about duplicate
204 // case values). Note that this depends on 'if' being null terminated.
Mike Stump11289f42009-09-09 15:08:12 +0000205
Chris Lattnerff067ce2007-10-07 07:52:34 +0000206#define HASH(LEN, FIRST, THIRD) \
207 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
208#define CASE(LEN, FIRST, THIRD, NAME) \
209 case HASH(LEN, FIRST, THIRD): \
210 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump11289f42009-09-09 15:08:12 +0000211
Chris Lattnerff067ce2007-10-07 07:52:34 +0000212 unsigned Len = getLength();
Chris Lattnerd2b8ce42007-10-10 20:59:57 +0000213 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000214 const char *Name = getNameStart();
Chris Lattnerff067ce2007-10-07 07:52:34 +0000215 switch (HASH(Len, Name[0], Name[2])) {
216 default: return tok::pp_not_keyword;
217 CASE( 2, 'i', '\0', if);
218 CASE( 4, 'e', 'i', elif);
219 CASE( 4, 'e', 's', else);
220 CASE( 4, 'l', 'n', line);
221 CASE( 4, 's', 'c', sccs);
222 CASE( 5, 'e', 'd', endif);
223 CASE( 5, 'e', 'r', error);
224 CASE( 5, 'i', 'e', ident);
225 CASE( 5, 'i', 'd', ifdef);
226 CASE( 5, 'u', 'd', undef);
227
228 CASE( 6, 'a', 's', assert);
229 CASE( 6, 'd', 'f', define);
230 CASE( 6, 'i', 'n', ifndef);
231 CASE( 6, 'i', 'p', import);
232 CASE( 6, 'p', 'a', pragma);
Douglas Gregor0bf886d2012-01-03 18:24:14 +0000233
Chris Lattnerff067ce2007-10-07 07:52:34 +0000234 CASE( 7, 'd', 'f', defined);
235 CASE( 7, 'i', 'c', include);
236 CASE( 7, 'w', 'r', warning);
237
238 CASE( 8, 'u', 'a', unassert);
239 CASE(12, 'i', 'c', include_next);
Mike Stump11289f42009-09-09 15:08:12 +0000240
Douglas Gregor663b48f2012-01-03 19:48:16 +0000241 CASE(14, '_', 'p', __public_macro);
242
243 CASE(15, '_', 'p', __private_macro);
244
Chris Lattner14a7f392009-04-08 18:24:34 +0000245 CASE(16, '_', 'i', __include_macros);
Chris Lattnerff067ce2007-10-07 07:52:34 +0000246#undef CASE
247#undef HASH
248 }
249}
Chris Lattner25e0d542006-10-18 06:07:05 +0000250
251//===----------------------------------------------------------------------===//
252// Stats Implementation
253//===----------------------------------------------------------------------===//
254
Chris Lattner22eb9722006-06-18 05:43:12 +0000255/// PrintStats - Print statistics about how well the identifier table is doing
256/// at hashing identifiers.
257void IdentifierTable::PrintStats() const {
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000258 unsigned NumBuckets = HashTable.getNumBuckets();
259 unsigned NumIdentifiers = HashTable.getNumItems();
260 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
Chris Lattner22eb9722006-06-18 05:43:12 +0000261 unsigned AverageIdentifierSize = 0;
262 unsigned MaxIdentifierLength = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000263
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000264 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000265 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000266 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
267 unsigned IdLen = I->getKeyLength();
268 AverageIdentifierSize += IdLen;
269 if (MaxIdentifierLength < IdLen)
270 MaxIdentifierLength = IdLen;
271 }
Mike Stump11289f42009-09-09 15:08:12 +0000272
Chris Lattner23b7eb62007-06-15 23:05:46 +0000273 fprintf(stderr, "\n*** Identifier Table Stats:\n");
274 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
275 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
276 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
277 NumIdentifiers/(double)NumBuckets);
278 fprintf(stderr, "Ave identifier length: %f\n",
279 (AverageIdentifierSize/(double)NumIdentifiers));
280 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump11289f42009-09-09 15:08:12 +0000281
Chris Lattner22eb9722006-06-18 05:43:12 +0000282 // Compute statistics about the memory allocated for identifiers.
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000283 HashTable.getAllocator().PrintStats();
Chris Lattner22eb9722006-06-18 05:43:12 +0000284}
Steve Narofff73590d2007-09-27 14:38:14 +0000285
Steve Naroffe61bfa82007-10-05 18:42:47 +0000286//===----------------------------------------------------------------------===//
287// SelectorTable Implementation
288//===----------------------------------------------------------------------===//
289
Chris Lattnerdadc7622007-10-05 20:15:24 +0000290unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
291 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
292}
293
Douglas Gregor77324f32008-11-17 14:58:09 +0000294namespace clang {
Steve Naroffe61bfa82007-10-05 18:42:47 +0000295/// MultiKeywordSelector - One of these variable length records is kept for each
296/// selector containing more than one keyword. We use a folding set
Mike Stump11289f42009-09-09 15:08:12 +0000297/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroffe61bfa82007-10-05 18:42:47 +0000298/// this class is provided strictly through Selector.
Mike Stump11289f42009-09-09 15:08:12 +0000299class MultiKeywordSelector
Douglas Gregor77324f32008-11-17 14:58:09 +0000300 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor77324f32008-11-17 14:58:09 +0000301 MultiKeywordSelector(unsigned nKeys) {
302 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
303 }
Mike Stump11289f42009-09-09 15:08:12 +0000304public:
Steve Naroffe61bfa82007-10-05 18:42:47 +0000305 // Constructor for keyword selectors.
306 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
307 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor77324f32008-11-17 14:58:09 +0000308 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump11289f42009-09-09 15:08:12 +0000309
Steve Naroffe61bfa82007-10-05 18:42:47 +0000310 // Fill in the trailing keyword array.
311 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
312 for (unsigned i = 0; i != nKeys; ++i)
313 KeyInfo[i] = IIV[i];
Mike Stump11289f42009-09-09 15:08:12 +0000314 }
315
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000316 // getName - Derive the full selector name and return it.
317 std::string getName() const;
Mike Stump11289f42009-09-09 15:08:12 +0000318
Douglas Gregor77324f32008-11-17 14:58:09 +0000319 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump11289f42009-09-09 15:08:12 +0000320
Steve Naroffe61bfa82007-10-05 18:42:47 +0000321 typedef IdentifierInfo *const *keyword_iterator;
322 keyword_iterator keyword_begin() const {
323 return reinterpret_cast<keyword_iterator>(this+1);
324 }
Mike Stump11289f42009-09-09 15:08:12 +0000325 keyword_iterator keyword_end() const {
326 return keyword_begin()+getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000327 }
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000328 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor77324f32008-11-17 14:58:09 +0000329 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroffe61bfa82007-10-05 18:42:47 +0000330 return keyword_begin()[i];
331 }
Mike Stump11289f42009-09-09 15:08:12 +0000332 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroffe61bfa82007-10-05 18:42:47 +0000333 keyword_iterator ArgTys, unsigned NumArgs) {
334 ID.AddInteger(NumArgs);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000335 for (unsigned i = 0; i != NumArgs; ++i)
336 ID.AddPointer(ArgTys[i]);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000337 }
338 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000339 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000340 }
341};
Chris Lattnerdadc7622007-10-05 20:15:24 +0000342} // end namespace clang.
Steve Naroffe61bfa82007-10-05 18:42:47 +0000343
344unsigned Selector::getNumArgs() const {
345 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor93a586f2012-05-04 18:24:37 +0000346 if (IIF <= ZeroArg)
Steve Naroffe61bfa82007-10-05 18:42:47 +0000347 return 0;
348 if (IIF == OneArg)
349 return 1;
Douglas Gregor93a586f2012-05-04 18:24:37 +0000350 // We point to a MultiKeywordSelector.
351 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump11289f42009-09-09 15:08:12 +0000352 return SI->getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000353}
354
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000355IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor93a586f2012-05-04 18:24:37 +0000356 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000357 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor70091b82009-04-26 22:20:50 +0000358 return getAsIdentifierInfo();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000359 }
Douglas Gregor93a586f2012-05-04 18:24:37 +0000360 // We point to a MultiKeywordSelector.
361 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000362 return SI->getIdentifierInfoForSlot(argIndex);
363}
364
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000365StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +0000366 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000367 return II? II->getName() : StringRef();
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +0000368}
369
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000370std::string MultiKeywordSelector::getName() const {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000371 SmallString<256> Str;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000372 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000373 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
374 if (*I)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000375 OS << (*I)->getName();
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000376 OS << ':';
Steve Naroffe61bfa82007-10-05 18:42:47 +0000377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000379 return OS.str();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000380}
381
Chris Lattnere4b95692008-11-24 03:33:13 +0000382std::string Selector::getAsString() const {
Douglas Gregor70091b82009-04-26 22:20:50 +0000383 if (InfoPtr == 0)
384 return "<null selector>";
385
Douglas Gregor93a586f2012-05-04 18:24:37 +0000386 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenek5f080b42009-03-06 23:36:28 +0000387 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenek0666a6c2009-03-07 01:22:02 +0000389 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenek5f080b42009-03-06 23:36:28 +0000390 if (getNumArgs() == 0)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000391 return II->getName();
Ted Kremenek5f080b42009-03-06 23:36:28 +0000392
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000393 if (!II)
394 return ":";
395
Daniel Dunbar07d07852009-10-18 21:17:35 +0000396 return II->getName().str() + ":";
Steve Naroffe61bfa82007-10-05 18:42:47 +0000397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Douglas Gregor93a586f2012-05-04 18:24:37 +0000399 // We have a multiple keyword selector.
400 return getMultiKeywordSelector()->getName();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000401}
402
Aaron Ballmanb190f972014-01-03 17:59:55 +0000403void Selector::print(llvm::raw_ostream &OS) const {
404 OS << getAsString();
405}
406
John McCallb4526252011-03-02 01:50:55 +0000407/// Interpreting the given string using the normal CamelCase
408/// conventions, determine whether the given string starts with the
409/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000410static bool startsWithWord(StringRef name, StringRef word) {
John McCallb4526252011-03-02 01:50:55 +0000411 if (name.size() < word.size()) return false;
Jordan Rosea7d03842013-02-08 22:30:41 +0000412 return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
413 name.startswith(word));
John McCallb4526252011-03-02 01:50:55 +0000414}
415
416ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
417 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
418 if (!first) return OMF_None;
419
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000420 StringRef name = first->getName();
John McCallb4526252011-03-02 01:50:55 +0000421 if (sel.isUnarySelector()) {
422 if (name == "autorelease") return OMF_autorelease;
423 if (name == "dealloc") return OMF_dealloc;
Nico Weber1fb82662011-08-28 22:35:17 +0000424 if (name == "finalize") return OMF_finalize;
John McCallb4526252011-03-02 01:50:55 +0000425 if (name == "release") return OMF_release;
426 if (name == "retain") return OMF_retain;
427 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor33823722011-06-11 01:09:30 +0000428 if (name == "self") return OMF_self;
John McCallb4526252011-03-02 01:50:55 +0000429 }
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000430
431 if (name == "performSelector") return OMF_performSelector;
John McCallb4526252011-03-02 01:50:55 +0000432
433 // The other method families may begin with a prefix of underscores.
434 while (!name.empty() && name.front() == '_')
435 name = name.substr(1);
436
437 if (name.empty()) return OMF_None;
438 switch (name.front()) {
439 case 'a':
440 if (startsWithWord(name, "alloc")) return OMF_alloc;
441 break;
442 case 'c':
443 if (startsWithWord(name, "copy")) return OMF_copy;
444 break;
445 case 'i':
446 if (startsWithWord(name, "init")) return OMF_init;
447 break;
448 case 'm':
449 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
450 break;
451 case 'n':
452 if (startsWithWord(name, "new")) return OMF_new;
453 break;
454 default:
455 break;
456 }
457
458 return OMF_None;
459}
Steve Naroffe61bfa82007-10-05 18:42:47 +0000460
Fariborz Jahanian71221352013-07-23 22:42:28 +0000461ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000462 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
463 if (!first) return OIT_None;
464
465 StringRef name = first->getName();
466
467 if (name.empty()) return OIT_None;
468 switch (name.front()) {
469 case 'a':
Fariborz Jahanian4ccdc732013-08-29 16:22:26 +0000470 if (startsWithWord(name, "array")) return OIT_Array;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000471 break;
472 case 'd':
Fariborz Jahanian7c87b432013-10-10 18:23:13 +0000473 if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000474 if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
475 break;
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000476 case 's':
Fariborz Jahanian7c87b432013-10-10 18:23:13 +0000477 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
478 if (startsWithWord(name, "standard")) return OIT_Singleton;
Fariborz Jahanian1c900bc2013-09-18 20:35:47 +0000479 case 'i':
480 if (startsWithWord(name, "init")) return OIT_Init;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000481 default:
482 break;
483 }
484 return OIT_None;
485}
486
Chris Lattner1a849942009-03-04 05:35:38 +0000487namespace {
488 struct SelectorTableImpl {
489 llvm::FoldingSet<MultiKeywordSelector> Table;
490 llvm::BumpPtrAllocator Allocator;
491 };
492} // end anonymous namespace.
493
494static SelectorTableImpl &getSelectorTableImpl(void *P) {
495 return *static_cast<SelectorTableImpl*>(P);
496}
497
Adrian Prantl6e77c962013-06-10 21:36:55 +0000498SmallString<64>
Adrian Prantla4ce9062013-06-07 22:29:12 +0000499SelectorTable::constructSetterName(StringRef Name) {
Adrian Prantl6e77c962013-06-10 21:36:55 +0000500 SmallString<64> SetterName("set");
501 SetterName += Name;
502 SetterName[3] = toUppercase(SetterName[3]);
503 return SetterName;
Adrian Prantla4ce9062013-06-07 22:29:12 +0000504}
505
Adrian Prantl6e77c962013-06-10 21:36:55 +0000506Selector
Adrian Prantla4ce9062013-06-07 22:29:12 +0000507SelectorTable::constructSetterSelector(IdentifierTable &Idents,
508 SelectorTable &SelTable,
509 const IdentifierInfo *Name) {
510 IdentifierInfo *SetterName =
511 &Idents.get(constructSetterName(Name->getName()));
Benjamin Kramer49038022012-02-04 13:45:25 +0000512 return SelTable.getUnarySelector(SetterName);
513}
514
Ted Kremenek1c2239e2011-04-18 22:47:04 +0000515size_t SelectorTable::getTotalMemory() const {
516 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
517 return SelTabImpl.Allocator.getTotalMemory();
518}
Chris Lattner1a849942009-03-04 05:35:38 +0000519
Chris Lattner5700fab2007-10-07 02:00:24 +0000520Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
521 if (nKeys < 2)
522 return Selector(IIV[0], nKeys);
Mike Stump11289f42009-09-09 15:08:12 +0000523
Chris Lattner1a849942009-03-04 05:35:38 +0000524 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump11289f42009-09-09 15:08:12 +0000525
Steve Naroffe61bfa82007-10-05 18:42:47 +0000526 // Unique selector, to guarantee there is one per name.
527 llvm::FoldingSetNodeID ID;
528 MultiKeywordSelector::Profile(ID, IIV, nKeys);
529
530 void *InsertPos = 0;
Chris Lattner1a849942009-03-04 05:35:38 +0000531 if (MultiKeywordSelector *SI =
532 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe61bfa82007-10-05 18:42:47 +0000533 return Selector(SI);
Mike Stump11289f42009-09-09 15:08:12 +0000534
Steve Naroffe61bfa82007-10-05 18:42:47 +0000535 // MultiKeywordSelector objects are not allocated with new because they have a
536 // variable size array (for parameter types) at the end of them.
Chris Lattner1a849942009-03-04 05:35:38 +0000537 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
538 MultiKeywordSelector *SI =
Mike Stump11289f42009-09-09 15:08:12 +0000539 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner5c0b4052010-10-30 05:14:06 +0000540 llvm::alignOf<MultiKeywordSelector>());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000541 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner1a849942009-03-04 05:35:38 +0000542 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000543 return Selector(SI);
544}
545
Steve Naroffe61bfa82007-10-05 18:42:47 +0000546SelectorTable::SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000547 Impl = new SelectorTableImpl();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000548}
549
550SelectorTable::~SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000551 delete &getSelectorTableImpl(Impl);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000552}
553
Douglas Gregor71395fa2009-11-04 00:56:37 +0000554const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
555 switch (Operator) {
556 case OO_None:
557 case NUM_OVERLOADED_OPERATORS:
558 return 0;
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000559
Douglas Gregor71395fa2009-11-04 00:56:37 +0000560#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
561 case OO_##Name: return Spelling;
562#include "clang/Basic/OperatorKinds.def"
563 }
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000564
David Blaikie8a40f702012-01-17 06:56:22 +0000565 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregor71395fa2009-11-04 00:56:37 +0000566}