blob: 219845996323c652787ac9d3735e87cc225a7603 [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
Adrian Prantl80e8ea92013-06-07 22:29:12 +000015#include "clang/Basic/CharInfo.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070016#include "clang/Basic/IdentifierTable.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070017#include "clang/Basic/LangOptions.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070018#include "clang/Basic/OperatorKinds.h"
Chris Lattner85994262007-10-05 20:15:24 +000019#include "llvm/ADT/DenseMap.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "llvm/ADT/FoldingSet.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070021#include "llvm/ADT/SmallString.h"
David Blaikie7530c032012-01-17 06:56:22 +000022#include "llvm/Support/ErrorHandling.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner3daed522009-03-02 22:20:04 +000024#include <cstdio>
Ted Kremenekc637e6b2007-10-23 22:18:37 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// IdentifierInfo Implementation
30//===----------------------------------------------------------------------===//
31
Ted Kremenekea9c26b2009-01-20 23:28:34 +000032IdentifierInfo::IdentifierInfo() {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 TokenID = tok::identifier;
Douglas Gregor5142af32008-11-06 16:32:23 +000034 ObjCOrBuiltinID = 0;
Chris Lattner4365a7e2007-10-07 07:09:52 +000035 HasMacro = false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +000036 HadMacro = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 IsExtension = false;
Richard Smith98d86b92011-10-11 19:57:52 +000038 IsCXX11CompatKeyword = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 IsPoisoned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000040 IsCPPOperatorKeyword = false;
Chris Lattner6a170eb2009-01-21 07:43:11 +000041 NeedsHandleIdentifier = false;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000042 IsFromAST = false;
Douglas Gregoreee242f2011-10-27 09:33:13 +000043 ChangedAfterLoad = false;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +000044 RevertedTokenID = false;
Douglas Gregoreee242f2011-10-27 09:33:13 +000045 OutOfDate = false;
Ted Kremenek32ad2ee2012-03-01 22:07:04 +000046 IsModulesImport = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070047 FETokenInfo = nullptr;
48 Entry = nullptr;
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
Reid Spencer5f016e22007-07-11 17:01:13 +000051//===----------------------------------------------------------------------===//
52// IdentifierTable Implementation
53//===----------------------------------------------------------------------===//
54
Douglas Gregor95f42922010-10-14 22:11:03 +000055IdentifierIterator::~IdentifierIterator() { }
56
Ted Kremenek72b1b152009-01-15 18:47:46 +000057IdentifierInfoLookup::~IdentifierInfoLookup() {}
58
Douglas Gregor95f42922010-10-14 22:11:03 +000059namespace {
60 /// \brief A simple identifier lookup iterator that represents an
61 /// empty sequence of identifiers.
62 class EmptyLookupIterator : public IdentifierIterator
63 {
64 public:
Stephen Hines651f13c2014-04-23 16:59:28 -070065 StringRef Next() override { return StringRef(); }
Douglas Gregor95f42922010-10-14 22:11:03 +000066 };
67}
68
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +000069IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
Douglas Gregor95f42922010-10-14 22:11:03 +000070 return new EmptyLookupIterator();
71}
72
Douglas Gregor8c5a7602009-04-25 23:30:02 +000073ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
74
Ted Kremenek72b1b152009-01-15 18:47:46 +000075IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
76 IdentifierInfoLookup* externalLookup)
77 : HashTable(8192), // Start with space for 8K identifiers.
78 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000079
80 // Populate the identifier table with info about keywords for the current
81 // language.
82 AddKeywords(LangOpts);
Ted Kremenekf15e1142012-03-01 22:53:32 +000083
84
85 // Add the '_experimental_modules_import' contextual keyword.
Douglas Gregor1b257af2012-12-11 22:11:52 +000086 get("import").setModulesImport(true);
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
89//===----------------------------------------------------------------------===//
90// Language Keyword Implementation
91//===----------------------------------------------------------------------===//
92
Eli Friedmaneb32fde2009-04-28 03:13:54 +000093// Constants for TokenKinds.def
94namespace {
95 enum {
Dylan Noblesmith67922922011-04-09 13:34:05 +000096 KEYC99 = 0x1,
97 KEYCXX = 0x2,
Richard Smith4e24f0f2013-01-02 12:01:23 +000098 KEYCXX11 = 0x4,
Dylan Noblesmith67922922011-04-09 13:34:05 +000099 KEYGNU = 0x8,
100 KEYMS = 0x10,
101 BOOLSUPPORT = 0x20,
102 KEYALTIVEC = 0x40,
103 KEYNOCXX = 0x80,
104 KEYBORLAND = 0x100,
105 KEYOPENCL = 0x200,
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000106 KEYC11 = 0x400,
John McCallf85e1932011-06-15 23:02:42 +0000107 KEYARC = 0x800,
Francois Picheta5a4cba2012-07-24 06:17:24 +0000108 KEYNOMS = 0x01000,
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000109 WCHARSUPPORT = 0x02000,
Stephen Hines651f13c2014-04-23 16:59:28 -0700110 HALFSUPPORT = 0x04000,
Francois Picheta5a4cba2012-07-24 06:17:24 +0000111 KEYALL = (0xffff & ~KEYNOMS) // Because KEYNOMS is used to exclude.
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000112 };
113}
114
Reid Spencer5f016e22007-07-11 17:01:13 +0000115/// AddKeyword - This method is used to associate a token ID with specific
116/// identifiers because they are language keywords. This causes the lexer to
117/// automatically map matching identifiers to specialized token codes.
118///
Richard Smith98d86b92011-10-11 19:57:52 +0000119/// The C90/C99/CPP/CPP0x flags are set to 3 if the token is a keyword in a
120/// future language standard, set to 2 if the token should be enabled in the
Nick Lewycky0be8fb52012-03-11 23:14:21 +0000121/// specified language, set to 1 if it is an extension in the specified
Richard Smith98d86b92011-10-11 19:57:52 +0000122/// language, and set to 0 if disabled in the specified language.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123static void AddKeyword(StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000124 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000126 unsigned AddResult = 0;
Dylan Noblesmith67922922011-04-09 13:34:05 +0000127 if (Flags == KEYALL) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000128 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
Richard Smith4e24f0f2013-01-02 12:01:23 +0000129 else if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000130 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000131 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Francois Pichet62ec1f22011-09-17 17:15:52 +0000132 else if (LangOpts.MicrosoftExt && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000133 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000134 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
Stephen Hines651f13c2014-04-23 16:59:28 -0700135 else if (LangOpts.Half && (Flags & HALFSUPPORT)) AddResult = 2;
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000136 else if (LangOpts.WChar && (Flags & WCHARSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000137 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000138 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000139 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000140 else if (LangOpts.C11 && (Flags & KEYC11)) AddResult = 2;
Fariborz Jahanian00852e42011-12-19 21:06:15 +0000141 // We treat bridge casts as objective-C keywords so we can warn on them
142 // in non-arc mode.
143 else if (LangOpts.ObjC2 && (Flags & KEYARC)) AddResult = 2;
Richard Smith4e24f0f2013-01-02 12:01:23 +0000144 else if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) AddResult = 3;
Richard Smith98d86b92011-10-11 19:57:52 +0000145
Stephen Hines651f13c2014-04-23 16:59:28 -0700146 // Don't add this keyword under MSVCCompat.
147 if (LangOpts.MSVCCompat && (Flags & KEYNOMS))
Francois Pichetdfd110c2012-07-22 11:32:41 +0000148 return;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000149 // Don't add this keyword if disabled in this language.
150 if (AddResult == 0) return;
151
Richard Smith98d86b92011-10-11 19:57:52 +0000152 IdentifierInfo &Info =
153 Table.get(Keyword, AddResult == 3 ? tok::identifier : TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000154 Info.setIsExtensionToken(AddResult == 1);
Richard Smith98d86b92011-10-11 19:57:52 +0000155 Info.setIsCXX11CompatKeyword(AddResult == 3);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156}
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
159/// representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000160static void AddCXXOperatorKeyword(StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 tok::TokenKind TokenCode,
162 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000163 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000164 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000165}
166
James Dennett6557d132012-06-15 21:27:44 +0000167/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
168/// or "property".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000169static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000170 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000172 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173}
174
175/// AddKeywords - Add all keywords to the symbol table.
176///
177void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 // Add keywords and tokens for the current language.
179#define KEYWORD(NAME, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000180 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000181 FLAGS, LangOpts, *this);
182#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000183 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000184 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
186 if (LangOpts.CXXOperatorNames) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188#define OBJC1_AT_KEYWORD(NAME) \
189 if (LangOpts.ObjC1) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000190 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191#define OBJC2_AT_KEYWORD(NAME) \
192 if (LangOpts.ObjC2) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000193 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000194#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000195#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000196
197 if (LangOpts.ParseUnknownAnytype)
198 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
199 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000200}
201
Chris Lattner387b98d2007-10-07 07:52:34 +0000202tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
203 // We use a perfect hash function here involving the length of the keyword,
204 // the first and third character. For preprocessor ID's there are no
205 // collisions (if there were, the switch below would complain about duplicate
206 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Chris Lattner387b98d2007-10-07 07:52:34 +0000208#define HASH(LEN, FIRST, THIRD) \
209 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
210#define CASE(LEN, FIRST, THIRD, NAME) \
211 case HASH(LEN, FIRST, THIRD): \
212 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattner387b98d2007-10-07 07:52:34 +0000214 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000215 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000216 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000217 switch (HASH(Len, Name[0], Name[2])) {
218 default: return tok::pp_not_keyword;
219 CASE( 2, 'i', '\0', if);
220 CASE( 4, 'e', 'i', elif);
221 CASE( 4, 'e', 's', else);
222 CASE( 4, 'l', 'n', line);
223 CASE( 4, 's', 'c', sccs);
224 CASE( 5, 'e', 'd', endif);
225 CASE( 5, 'e', 'r', error);
226 CASE( 5, 'i', 'e', ident);
227 CASE( 5, 'i', 'd', ifdef);
228 CASE( 5, 'u', 'd', undef);
229
230 CASE( 6, 'a', 's', assert);
231 CASE( 6, 'd', 'f', define);
232 CASE( 6, 'i', 'n', ifndef);
233 CASE( 6, 'i', 'p', import);
234 CASE( 6, 'p', 'a', pragma);
Douglas Gregor94ad28b2012-01-03 18:24:14 +0000235
Chris Lattner387b98d2007-10-07 07:52:34 +0000236 CASE( 7, 'd', 'f', defined);
237 CASE( 7, 'i', 'c', include);
238 CASE( 7, 'w', 'r', warning);
239
240 CASE( 8, 'u', 'a', unassert);
241 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Douglas Gregor1ac13c32012-01-03 19:48:16 +0000243 CASE(14, '_', 'p', __public_macro);
244
245 CASE(15, '_', 'p', __private_macro);
246
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000247 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000248#undef CASE
249#undef HASH
250 }
251}
Reid Spencer5f016e22007-07-11 17:01:13 +0000252
253//===----------------------------------------------------------------------===//
254// Stats Implementation
255//===----------------------------------------------------------------------===//
256
257/// PrintStats - Print statistics about how well the identifier table is doing
258/// at hashing identifiers.
259void IdentifierTable::PrintStats() const {
260 unsigned NumBuckets = HashTable.getNumBuckets();
261 unsigned NumIdentifiers = HashTable.getNumItems();
262 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
263 unsigned AverageIdentifierSize = 0;
264 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000267 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
269 unsigned IdLen = I->getKeyLength();
270 AverageIdentifierSize += IdLen;
271 if (MaxIdentifierLength < IdLen)
272 MaxIdentifierLength = IdLen;
273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 fprintf(stderr, "\n*** Identifier Table Stats:\n");
276 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
277 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
278 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
279 NumIdentifiers/(double)NumBuckets);
280 fprintf(stderr, "Ave identifier length: %f\n",
281 (AverageIdentifierSize/(double)NumIdentifiers));
282 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 // Compute statistics about the memory allocated for identifiers.
285 HashTable.getAllocator().PrintStats();
286}
Steve Naroff68d331a2007-09-27 14:38:14 +0000287
Steve Naroff29238a02007-10-05 18:42:47 +0000288//===----------------------------------------------------------------------===//
289// SelectorTable Implementation
290//===----------------------------------------------------------------------===//
291
Chris Lattner85994262007-10-05 20:15:24 +0000292unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
293 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
294}
295
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000296namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000297/// MultiKeywordSelector - One of these variable length records is kept for each
298/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000299/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000300/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000301class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000302 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000303 MultiKeywordSelector(unsigned nKeys) {
304 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
305 }
Mike Stump1eb44332009-09-09 15:08:12 +0000306public:
Steve Naroff29238a02007-10-05 18:42:47 +0000307 // Constructor for keyword selectors.
308 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
309 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000310 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Steve Naroff29238a02007-10-05 18:42:47 +0000312 // Fill in the trailing keyword array.
313 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
314 for (unsigned i = 0; i != nKeys; ++i)
315 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000316 }
317
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000318 // getName - Derive the full selector name and return it.
319 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000321 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Steve Naroff29238a02007-10-05 18:42:47 +0000323 typedef IdentifierInfo *const *keyword_iterator;
324 keyword_iterator keyword_begin() const {
325 return reinterpret_cast<keyword_iterator>(this+1);
326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327 keyword_iterator keyword_end() const {
328 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000329 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000330 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000331 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000332 return keyword_begin()[i];
333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000335 keyword_iterator ArgTys, unsigned NumArgs) {
336 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000337 for (unsigned i = 0; i != NumArgs; ++i)
338 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000339 }
340 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000341 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000342 }
343};
Chris Lattner85994262007-10-05 20:15:24 +0000344} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000345
346unsigned Selector::getNumArgs() const {
347 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor51603be2012-05-04 18:24:37 +0000348 if (IIF <= ZeroArg)
Steve Naroff29238a02007-10-05 18:42:47 +0000349 return 0;
350 if (IIF == OneArg)
351 return 1;
Douglas Gregor51603be2012-05-04 18:24:37 +0000352 // We point to a MultiKeywordSelector.
353 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump1eb44332009-09-09 15:08:12 +0000354 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000355}
356
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000357IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor51603be2012-05-04 18:24:37 +0000358 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000359 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000360 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000361 }
Douglas Gregor51603be2012-05-04 18:24:37 +0000362 // We point to a MultiKeywordSelector.
363 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroff29238a02007-10-05 18:42:47 +0000364 return SI->getIdentifierInfoForSlot(argIndex);
365}
366
Chris Lattner5f9e2722011-07-23 10:55:15 +0000367StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregor813d8342011-02-18 22:29:55 +0000368 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000369 return II? II->getName() : StringRef();
Douglas Gregor813d8342011-02-18 22:29:55 +0000370}
371
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000372std::string MultiKeywordSelector::getName() const {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000373 SmallString<256> Str;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000374 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000375 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
376 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000377 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000378 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000381 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000382}
383
Chris Lattner077bf5e2008-11-24 03:33:13 +0000384std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000385 if (InfoPtr == 0)
386 return "<null selector>";
387
Douglas Gregor51603be2012-05-04 18:24:37 +0000388 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000389 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek150ec292009-03-07 01:22:02 +0000391 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000392 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000393 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000394
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000395 if (!II)
396 return ":";
397
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000398 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Douglas Gregor51603be2012-05-04 18:24:37 +0000401 // We have a multiple keyword selector.
402 return getMultiKeywordSelector()->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000403}
404
Stephen Hines651f13c2014-04-23 16:59:28 -0700405void Selector::print(llvm::raw_ostream &OS) const {
406 OS << getAsString();
407}
408
John McCall85f3d762011-03-02 01:50:55 +0000409/// Interpreting the given string using the normal CamelCase
410/// conventions, determine whether the given string starts with the
411/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000412static bool startsWithWord(StringRef name, StringRef word) {
John McCall85f3d762011-03-02 01:50:55 +0000413 if (name.size() < word.size()) return false;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000414 return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
415 name.startswith(word));
John McCall85f3d762011-03-02 01:50:55 +0000416}
417
418ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
419 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
420 if (!first) return OMF_None;
421
Chris Lattner5f9e2722011-07-23 10:55:15 +0000422 StringRef name = first->getName();
John McCall85f3d762011-03-02 01:50:55 +0000423 if (sel.isUnarySelector()) {
424 if (name == "autorelease") return OMF_autorelease;
425 if (name == "dealloc") return OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000426 if (name == "finalize") return OMF_finalize;
John McCall85f3d762011-03-02 01:50:55 +0000427 if (name == "release") return OMF_release;
428 if (name == "retain") return OMF_retain;
429 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000430 if (name == "self") return OMF_self;
John McCall85f3d762011-03-02 01:50:55 +0000431 }
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000432
433 if (name == "performSelector") return OMF_performSelector;
John McCall85f3d762011-03-02 01:50:55 +0000434
435 // The other method families may begin with a prefix of underscores.
436 while (!name.empty() && name.front() == '_')
437 name = name.substr(1);
438
439 if (name.empty()) return OMF_None;
440 switch (name.front()) {
441 case 'a':
442 if (startsWithWord(name, "alloc")) return OMF_alloc;
443 break;
444 case 'c':
445 if (startsWithWord(name, "copy")) return OMF_copy;
446 break;
447 case 'i':
448 if (startsWithWord(name, "init")) return OMF_init;
449 break;
450 case 'm':
451 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
452 break;
453 case 'n':
454 if (startsWithWord(name, "new")) return OMF_new;
455 break;
456 default:
457 break;
458 }
459
460 return OMF_None;
461}
Steve Naroff29238a02007-10-05 18:42:47 +0000462
Fariborz Jahanian11638f72013-07-23 22:42:28 +0000463ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000464 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
465 if (!first) return OIT_None;
466
467 StringRef name = first->getName();
468
469 if (name.empty()) return OIT_None;
470 switch (name.front()) {
471 case 'a':
Fariborz Jahaniand0f29212013-08-29 16:22:26 +0000472 if (startsWithWord(name, "array")) return OIT_Array;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000473 break;
474 case 'd':
Fariborz Jahanian9fcbd5e2013-10-10 18:23:13 +0000475 if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000476 if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
477 break;
Fariborz Jahanian8d3794e2013-08-02 20:54:18 +0000478 case 's':
Fariborz Jahanian9fcbd5e2013-10-10 18:23:13 +0000479 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
480 if (startsWithWord(name, "standard")) return OIT_Singleton;
Fariborz Jahaniana346eb12013-09-18 20:35:47 +0000481 case 'i':
482 if (startsWithWord(name, "init")) return OIT_Init;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000483 default:
484 break;
485 }
486 return OIT_None;
487}
488
Chris Lattner5f7d2282009-03-04 05:35:38 +0000489namespace {
490 struct SelectorTableImpl {
491 llvm::FoldingSet<MultiKeywordSelector> Table;
492 llvm::BumpPtrAllocator Allocator;
493 };
494} // end anonymous namespace.
495
496static SelectorTableImpl &getSelectorTableImpl(void *P) {
497 return *static_cast<SelectorTableImpl*>(P);
498}
499
Adrian Prantl70446682013-06-10 21:36:55 +0000500SmallString<64>
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000501SelectorTable::constructSetterName(StringRef Name) {
Adrian Prantl70446682013-06-10 21:36:55 +0000502 SmallString<64> SetterName("set");
503 SetterName += Name;
504 SetterName[3] = toUppercase(SetterName[3]);
505 return SetterName;
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000506}
507
Adrian Prantl70446682013-06-10 21:36:55 +0000508Selector
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000509SelectorTable::constructSetterSelector(IdentifierTable &Idents,
510 SelectorTable &SelTable,
511 const IdentifierInfo *Name) {
512 IdentifierInfo *SetterName =
513 &Idents.get(constructSetterName(Name->getName()));
Benjamin Kramer8fe83e12012-02-04 13:45:25 +0000514 return SelTable.getUnarySelector(SetterName);
515}
516
Ted Kremenek97f55d62011-04-18 22:47:04 +0000517size_t SelectorTable::getTotalMemory() const {
518 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
519 return SelTabImpl.Allocator.getTotalMemory();
520}
Chris Lattner5f7d2282009-03-04 05:35:38 +0000521
Chris Lattnerff384912007-10-07 02:00:24 +0000522Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
523 if (nKeys < 2)
524 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Chris Lattner5f7d2282009-03-04 05:35:38 +0000526 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Steve Naroff29238a02007-10-05 18:42:47 +0000528 // Unique selector, to guarantee there is one per name.
529 llvm::FoldingSetNodeID ID;
530 MultiKeywordSelector::Profile(ID, IIV, nKeys);
531
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700532 void *InsertPos = nullptr;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000533 if (MultiKeywordSelector *SI =
534 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000535 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Steve Naroff29238a02007-10-05 18:42:47 +0000537 // MultiKeywordSelector objects are not allocated with new because they have a
538 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000539 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
540 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000541 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000542 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000543 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000544 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000545 return Selector(SI);
546}
547
Steve Naroff29238a02007-10-05 18:42:47 +0000548SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000549 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000550}
551
552SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000553 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000554}
555
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000556const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
557 switch (Operator) {
558 case OO_None:
559 case NUM_OVERLOADED_OPERATORS:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700560 return nullptr;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000561
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000562#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
563 case OO_##Name: return Spelling;
564#include "clang/Basic/OperatorKinds.def"
565 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000566
David Blaikie7530c032012-01-17 06:56:22 +0000567 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000568}