blob: dd097047dce25b0c720023318fc87f74b466ebe1 [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 };
Stephen Hines176edba2014-12-01 14:53:08 -0800113
114 /// \brief How a keyword is treated in the selected standard.
115 enum KeywordStatus {
116 KS_Disabled, // Disabled
117 KS_Extension, // Is an extension
118 KS_Enabled, // Enabled
119 KS_Future // Is a keyword in future standard
120 };
121}
122
123/// \brief Translates flags as specified in TokenKinds.def into keyword status
124/// in the given language standard.
125static KeywordStatus GetKeywordStatus(const LangOptions &LangOpts,
126 unsigned Flags) {
127 if (Flags == KEYALL) return KS_Enabled;
128 if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled;
129 if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled;
130 if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled;
131 if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension;
132 if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension;
133 if (LangOpts.Borland && (Flags & KEYBORLAND)) return KS_Extension;
134 if (LangOpts.Bool && (Flags & BOOLSUPPORT)) return KS_Enabled;
135 if (LangOpts.Half && (Flags & HALFSUPPORT)) return KS_Enabled;
136 if (LangOpts.WChar && (Flags & WCHARSUPPORT)) return KS_Enabled;
137 if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) return KS_Enabled;
138 if (LangOpts.OpenCL && (Flags & KEYOPENCL)) return KS_Enabled;
139 if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled;
140 if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled;
141 // We treat bridge casts as objective-C keywords so we can warn on them
142 // in non-arc mode.
143 if (LangOpts.ObjC2 && (Flags & KEYARC)) return KS_Enabled;
144 if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) return KS_Future;
145 return KS_Disabled;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000146}
147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148/// AddKeyword - This method is used to associate a token ID with specific
149/// identifiers because they are language keywords. This causes the lexer to
150/// automatically map matching identifiers to specialized token codes.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000151static void AddKeyword(StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000152 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 const LangOptions &LangOpts, IdentifierTable &Table) {
Stephen Hines176edba2014-12-01 14:53:08 -0800154 KeywordStatus AddResult = GetKeywordStatus(LangOpts, Flags);
Richard Smith98d86b92011-10-11 19:57:52 +0000155
Stephen Hines651f13c2014-04-23 16:59:28 -0700156 // Don't add this keyword under MSVCCompat.
157 if (LangOpts.MSVCCompat && (Flags & KEYNOMS))
Francois Pichetdfd110c2012-07-22 11:32:41 +0000158 return;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000159 // Don't add this keyword if disabled in this language.
Stephen Hines176edba2014-12-01 14:53:08 -0800160 if (AddResult == KS_Disabled) return;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000161
Richard Smith98d86b92011-10-11 19:57:52 +0000162 IdentifierInfo &Info =
Stephen Hines176edba2014-12-01 14:53:08 -0800163 Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode);
164 Info.setIsExtensionToken(AddResult == KS_Extension);
165 Info.setIsCXX11CompatKeyword(AddResult == KS_Future);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166}
167
Reid Spencer5f016e22007-07-11 17:01:13 +0000168/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
169/// representations.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000170static void AddCXXOperatorKeyword(StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 tok::TokenKind TokenCode,
172 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000173 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000174 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000175}
176
James Dennett6557d132012-06-15 21:27:44 +0000177/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
178/// or "property".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000179static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000180 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000182 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000183}
184
185/// AddKeywords - Add all keywords to the symbol table.
186///
187void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // Add keywords and tokens for the current language.
189#define KEYWORD(NAME, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000190 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000191 FLAGS, LangOpts, *this);
192#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000193 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000194 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000195#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
196 if (LangOpts.CXXOperatorNames) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198#define OBJC1_AT_KEYWORD(NAME) \
199 if (LangOpts.ObjC1) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000200 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000201#define OBJC2_AT_KEYWORD(NAME) \
202 if (LangOpts.ObjC2) \
Chris Lattner5f9e2722011-07-23 10:55:15 +0000203 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCalla5fc4722011-04-09 22:50:59 +0000204#define TESTING_KEYWORD(NAME, FLAGS)
Reid Spencer5f016e22007-07-11 17:01:13 +0000205#include "clang/Basic/TokenKinds.def"
John McCalla5fc4722011-04-09 22:50:59 +0000206
207 if (LangOpts.ParseUnknownAnytype)
208 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
209 LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Chris Lattner387b98d2007-10-07 07:52:34 +0000212tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
213 // We use a perfect hash function here involving the length of the keyword,
214 // the first and third character. For preprocessor ID's there are no
215 // collisions (if there were, the switch below would complain about duplicate
216 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Chris Lattner387b98d2007-10-07 07:52:34 +0000218#define HASH(LEN, FIRST, THIRD) \
219 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
220#define CASE(LEN, FIRST, THIRD, NAME) \
221 case HASH(LEN, FIRST, THIRD): \
222 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattner387b98d2007-10-07 07:52:34 +0000224 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000225 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000226 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000227 switch (HASH(Len, Name[0], Name[2])) {
228 default: return tok::pp_not_keyword;
229 CASE( 2, 'i', '\0', if);
230 CASE( 4, 'e', 'i', elif);
231 CASE( 4, 'e', 's', else);
232 CASE( 4, 'l', 'n', line);
233 CASE( 4, 's', 'c', sccs);
234 CASE( 5, 'e', 'd', endif);
235 CASE( 5, 'e', 'r', error);
236 CASE( 5, 'i', 'e', ident);
237 CASE( 5, 'i', 'd', ifdef);
238 CASE( 5, 'u', 'd', undef);
239
240 CASE( 6, 'a', 's', assert);
241 CASE( 6, 'd', 'f', define);
242 CASE( 6, 'i', 'n', ifndef);
243 CASE( 6, 'i', 'p', import);
244 CASE( 6, 'p', 'a', pragma);
Douglas Gregor94ad28b2012-01-03 18:24:14 +0000245
Chris Lattner387b98d2007-10-07 07:52:34 +0000246 CASE( 7, 'd', 'f', defined);
247 CASE( 7, 'i', 'c', include);
248 CASE( 7, 'w', 'r', warning);
249
250 CASE( 8, 'u', 'a', unassert);
251 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Douglas Gregor1ac13c32012-01-03 19:48:16 +0000253 CASE(14, '_', 'p', __public_macro);
254
255 CASE(15, '_', 'p', __private_macro);
256
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000257 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000258#undef CASE
259#undef HASH
260 }
261}
Reid Spencer5f016e22007-07-11 17:01:13 +0000262
263//===----------------------------------------------------------------------===//
264// Stats Implementation
265//===----------------------------------------------------------------------===//
266
267/// PrintStats - Print statistics about how well the identifier table is doing
268/// at hashing identifiers.
269void IdentifierTable::PrintStats() const {
270 unsigned NumBuckets = HashTable.getNumBuckets();
271 unsigned NumIdentifiers = HashTable.getNumItems();
272 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
273 unsigned AverageIdentifierSize = 0;
274 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000277 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
279 unsigned IdLen = I->getKeyLength();
280 AverageIdentifierSize += IdLen;
281 if (MaxIdentifierLength < IdLen)
282 MaxIdentifierLength = IdLen;
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 fprintf(stderr, "\n*** Identifier Table Stats:\n");
286 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
287 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
288 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
289 NumIdentifiers/(double)NumBuckets);
290 fprintf(stderr, "Ave identifier length: %f\n",
291 (AverageIdentifierSize/(double)NumIdentifiers));
292 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 // Compute statistics about the memory allocated for identifiers.
295 HashTable.getAllocator().PrintStats();
296}
Steve Naroff68d331a2007-09-27 14:38:14 +0000297
Steve Naroff29238a02007-10-05 18:42:47 +0000298//===----------------------------------------------------------------------===//
299// SelectorTable Implementation
300//===----------------------------------------------------------------------===//
301
Chris Lattner85994262007-10-05 20:15:24 +0000302unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
303 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
304}
305
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000306namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000307/// MultiKeywordSelector - One of these variable length records is kept for each
308/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000309/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000310/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000311class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000312 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000313 MultiKeywordSelector(unsigned nKeys) {
314 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316public:
Steve Naroff29238a02007-10-05 18:42:47 +0000317 // Constructor for keyword selectors.
318 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
319 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000320 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Steve Naroff29238a02007-10-05 18:42:47 +0000322 // Fill in the trailing keyword array.
323 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
324 for (unsigned i = 0; i != nKeys; ++i)
325 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000326 }
327
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000328 // getName - Derive the full selector name and return it.
329 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000331 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Steve Naroff29238a02007-10-05 18:42:47 +0000333 typedef IdentifierInfo *const *keyword_iterator;
334 keyword_iterator keyword_begin() const {
335 return reinterpret_cast<keyword_iterator>(this+1);
336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337 keyword_iterator keyword_end() const {
338 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000339 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000340 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000341 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000342 return keyword_begin()[i];
343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000345 keyword_iterator ArgTys, unsigned NumArgs) {
346 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000347 for (unsigned i = 0; i != NumArgs; ++i)
348 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000349 }
350 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000351 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000352 }
353};
Chris Lattner85994262007-10-05 20:15:24 +0000354} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000355
356unsigned Selector::getNumArgs() const {
357 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor51603be2012-05-04 18:24:37 +0000358 if (IIF <= ZeroArg)
Steve Naroff29238a02007-10-05 18:42:47 +0000359 return 0;
360 if (IIF == OneArg)
361 return 1;
Douglas Gregor51603be2012-05-04 18:24:37 +0000362 // We point to a MultiKeywordSelector.
363 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump1eb44332009-09-09 15:08:12 +0000364 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000365}
366
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000367IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor51603be2012-05-04 18:24:37 +0000368 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000369 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000370 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000371 }
Douglas Gregor51603be2012-05-04 18:24:37 +0000372 // We point to a MultiKeywordSelector.
373 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroff29238a02007-10-05 18:42:47 +0000374 return SI->getIdentifierInfoForSlot(argIndex);
375}
376
Chris Lattner5f9e2722011-07-23 10:55:15 +0000377StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregor813d8342011-02-18 22:29:55 +0000378 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000379 return II? II->getName() : StringRef();
Douglas Gregor813d8342011-02-18 22:29:55 +0000380}
381
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000382std::string MultiKeywordSelector::getName() const {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000383 SmallString<256> Str;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000384 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000385 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
386 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000387 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000388 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000391 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000392}
393
Chris Lattner077bf5e2008-11-24 03:33:13 +0000394std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000395 if (InfoPtr == 0)
396 return "<null selector>";
397
Douglas Gregor51603be2012-05-04 18:24:37 +0000398 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000399 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremenek150ec292009-03-07 01:22:02 +0000401 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000402 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000403 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000404
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000405 if (!II)
406 return ":";
407
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000408 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000409 }
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Douglas Gregor51603be2012-05-04 18:24:37 +0000411 // We have a multiple keyword selector.
412 return getMultiKeywordSelector()->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000413}
414
Stephen Hines651f13c2014-04-23 16:59:28 -0700415void Selector::print(llvm::raw_ostream &OS) const {
416 OS << getAsString();
417}
418
John McCall85f3d762011-03-02 01:50:55 +0000419/// Interpreting the given string using the normal CamelCase
420/// conventions, determine whether the given string starts with the
421/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000422static bool startsWithWord(StringRef name, StringRef word) {
John McCall85f3d762011-03-02 01:50:55 +0000423 if (name.size() < word.size()) return false;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000424 return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
425 name.startswith(word));
John McCall85f3d762011-03-02 01:50:55 +0000426}
427
428ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
429 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
430 if (!first) return OMF_None;
431
Chris Lattner5f9e2722011-07-23 10:55:15 +0000432 StringRef name = first->getName();
John McCall85f3d762011-03-02 01:50:55 +0000433 if (sel.isUnarySelector()) {
434 if (name == "autorelease") return OMF_autorelease;
435 if (name == "dealloc") return OMF_dealloc;
Nico Weber80cb6e62011-08-28 22:35:17 +0000436 if (name == "finalize") return OMF_finalize;
John McCall85f3d762011-03-02 01:50:55 +0000437 if (name == "release") return OMF_release;
438 if (name == "retain") return OMF_retain;
439 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor926df6c2011-06-11 01:09:30 +0000440 if (name == "self") return OMF_self;
Stephen Hines176edba2014-12-01 14:53:08 -0800441 if (name == "initialize") return OMF_initialize;
John McCall85f3d762011-03-02 01:50:55 +0000442 }
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000443
444 if (name == "performSelector") return OMF_performSelector;
John McCall85f3d762011-03-02 01:50:55 +0000445
446 // The other method families may begin with a prefix of underscores.
447 while (!name.empty() && name.front() == '_')
448 name = name.substr(1);
449
450 if (name.empty()) return OMF_None;
451 switch (name.front()) {
452 case 'a':
453 if (startsWithWord(name, "alloc")) return OMF_alloc;
454 break;
455 case 'c':
456 if (startsWithWord(name, "copy")) return OMF_copy;
457 break;
458 case 'i':
459 if (startsWithWord(name, "init")) return OMF_init;
460 break;
461 case 'm':
462 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
463 break;
464 case 'n':
465 if (startsWithWord(name, "new")) return OMF_new;
466 break;
467 default:
468 break;
469 }
470
471 return OMF_None;
472}
Steve Naroff29238a02007-10-05 18:42:47 +0000473
Fariborz Jahanian11638f72013-07-23 22:42:28 +0000474ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000475 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
476 if (!first) return OIT_None;
477
478 StringRef name = first->getName();
479
480 if (name.empty()) return OIT_None;
481 switch (name.front()) {
482 case 'a':
Fariborz Jahaniand0f29212013-08-29 16:22:26 +0000483 if (startsWithWord(name, "array")) return OIT_Array;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000484 break;
485 case 'd':
Fariborz Jahanian9fcbd5e2013-10-10 18:23:13 +0000486 if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000487 if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
488 break;
Fariborz Jahanian8d3794e2013-08-02 20:54:18 +0000489 case 's':
Fariborz Jahanian9fcbd5e2013-10-10 18:23:13 +0000490 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
491 if (startsWithWord(name, "standard")) return OIT_Singleton;
Fariborz Jahaniana346eb12013-09-18 20:35:47 +0000492 case 'i':
493 if (startsWithWord(name, "init")) return OIT_Init;
Fariborz Jahanian8d092162013-07-23 19:31:17 +0000494 default:
495 break;
496 }
497 return OIT_None;
498}
499
Stephen Hines176edba2014-12-01 14:53:08 -0800500ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
501 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
502 if (!first) return SFF_None;
503
504 StringRef name = first->getName();
505
506 switch (name.front()) {
507 case 'a':
508 if (name == "appendFormat") return SFF_NSString;
509 break;
510
511 case 'i':
512 if (name == "initWithFormat") return SFF_NSString;
513 break;
514
515 case 'l':
516 if (name == "localizedStringWithFormat") return SFF_NSString;
517 break;
518
519 case 's':
520 if (name == "stringByAppendingFormat" ||
521 name == "stringWithFormat") return SFF_NSString;
522 break;
523 }
524 return SFF_None;
525}
526
Chris Lattner5f7d2282009-03-04 05:35:38 +0000527namespace {
528 struct SelectorTableImpl {
529 llvm::FoldingSet<MultiKeywordSelector> Table;
530 llvm::BumpPtrAllocator Allocator;
531 };
532} // end anonymous namespace.
533
534static SelectorTableImpl &getSelectorTableImpl(void *P) {
535 return *static_cast<SelectorTableImpl*>(P);
536}
537
Adrian Prantl70446682013-06-10 21:36:55 +0000538SmallString<64>
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000539SelectorTable::constructSetterName(StringRef Name) {
Adrian Prantl70446682013-06-10 21:36:55 +0000540 SmallString<64> SetterName("set");
541 SetterName += Name;
542 SetterName[3] = toUppercase(SetterName[3]);
543 return SetterName;
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000544}
545
Adrian Prantl70446682013-06-10 21:36:55 +0000546Selector
Adrian Prantl80e8ea92013-06-07 22:29:12 +0000547SelectorTable::constructSetterSelector(IdentifierTable &Idents,
548 SelectorTable &SelTable,
549 const IdentifierInfo *Name) {
550 IdentifierInfo *SetterName =
551 &Idents.get(constructSetterName(Name->getName()));
Benjamin Kramer8fe83e12012-02-04 13:45:25 +0000552 return SelTable.getUnarySelector(SetterName);
553}
554
Ted Kremenek97f55d62011-04-18 22:47:04 +0000555size_t SelectorTable::getTotalMemory() const {
556 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
557 return SelTabImpl.Allocator.getTotalMemory();
558}
Chris Lattner5f7d2282009-03-04 05:35:38 +0000559
Chris Lattnerff384912007-10-07 02:00:24 +0000560Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
561 if (nKeys < 2)
562 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Chris Lattner5f7d2282009-03-04 05:35:38 +0000564 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Steve Naroff29238a02007-10-05 18:42:47 +0000566 // Unique selector, to guarantee there is one per name.
567 llvm::FoldingSetNodeID ID;
568 MultiKeywordSelector::Profile(ID, IIV, nKeys);
569
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700570 void *InsertPos = nullptr;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000571 if (MultiKeywordSelector *SI =
572 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000573 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Steve Naroff29238a02007-10-05 18:42:47 +0000575 // MultiKeywordSelector objects are not allocated with new because they have a
576 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000577 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
578 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000579 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000580 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000581 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000582 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000583 return Selector(SI);
584}
585
Steve Naroff29238a02007-10-05 18:42:47 +0000586SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000587 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000588}
589
590SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000591 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000592}
593
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000594const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
595 switch (Operator) {
596 case OO_None:
597 case NUM_OVERLOADED_OPERATORS:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700598 return nullptr;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000599
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000600#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
601 case OO_##Name: return Spelling;
602#include "clang/Basic/OperatorKinds.def"
603 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000604
David Blaikie7530c032012-01-17 06:56:22 +0000605 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000606}