blob: 8993e6713fbebd324cf977ed0e9a7ba9d4dc8e96 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the IdentifierInfo, IdentifierVisitor, and
11// IdentifierTable interfaces.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerc7229c32007-10-07 08:58:51 +000015#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Basic/LangOptions.h"
Steve Naroff29238a02007-10-05 18:42:47 +000017#include "llvm/ADT/FoldingSet.h"
Chris Lattner85994262007-10-05 20:15:24 +000018#include "llvm/ADT/DenseMap.h"
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +000019#include "llvm/ADT/StringRef.h"
Daniel Dunbar76b61cc2009-10-17 18:13:02 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner3daed522009-03-02 22:20:04 +000021#include <cstdio>
Ted Kremenekc637e6b2007-10-23 22:18:37 +000022
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// IdentifierInfo Implementation
27//===----------------------------------------------------------------------===//
28
Ted Kremenekea9c26b2009-01-20 23:28:34 +000029IdentifierInfo::IdentifierInfo() {
Reid Spencer5f016e22007-07-11 17:01:13 +000030 TokenID = tok::identifier;
Douglas Gregor5142af32008-11-06 16:32:23 +000031 ObjCOrBuiltinID = 0;
Chris Lattner4365a7e2007-10-07 07:09:52 +000032 HasMacro = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000033 IsExtension = false;
34 IsPoisoned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000035 IsCPPOperatorKeyword = false;
Chris Lattner6a170eb2009-01-21 07:43:11 +000036 NeedsHandleIdentifier = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000038 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000039}
40
Reid Spencer5f016e22007-07-11 17:01:13 +000041//===----------------------------------------------------------------------===//
42// IdentifierTable Implementation
43//===----------------------------------------------------------------------===//
44
Ted Kremenek72b1b152009-01-15 18:47:46 +000045IdentifierInfoLookup::~IdentifierInfoLookup() {}
46
Douglas Gregor8c5a7602009-04-25 23:30:02 +000047ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
48
Ted Kremenek72b1b152009-01-15 18:47:46 +000049IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
50 IdentifierInfoLookup* externalLookup)
51 : HashTable(8192), // Start with space for 8K identifiers.
52 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000053
54 // Populate the identifier table with info about keywords for the current
55 // language.
56 AddKeywords(LangOpts);
57}
58
59//===----------------------------------------------------------------------===//
60// Language Keyword Implementation
61//===----------------------------------------------------------------------===//
62
Eli Friedmaneb32fde2009-04-28 03:13:54 +000063// Constants for TokenKinds.def
64namespace {
65 enum {
66 KEYALL = 1,
67 KEYC99 = 2,
68 KEYCXX = 4,
69 KEYCXX0X = 8,
70 KEYGNU = 16,
Nate Begeman72b60e32009-06-25 23:25:15 +000071 KEYMS = 32,
John Thompson82287d12010-02-05 00:12:22 +000072 BOOLSUPPORT = 64,
Chris Lattner34d7c4d2010-05-21 20:22:37 +000073 KEYALTIVEC = 128,
74 KEYNOMS = 256
Eli Friedmaneb32fde2009-04-28 03:13:54 +000075 };
76}
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078/// AddKeyword - This method is used to associate a token ID with specific
79/// identifiers because they are language keywords. This causes the lexer to
80/// automatically map matching identifiers to specialized token codes.
81///
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000082/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattnerd4b80f12007-07-16 04:18:29 +000083/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000084/// in the specified language, and set to 0 if disabled in the
Chris Lattnerd4b80f12007-07-16 04:18:29 +000085/// specified language.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +000086static void AddKeyword(llvm::StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +000087 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +000088 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +000089 unsigned AddResult = 0;
90 if (Flags & KEYALL) AddResult = 2;
91 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
92 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
93 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000094 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedmaneb32fde2009-04-28 03:13:54 +000095 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +000096 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +000097 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Chris Lattner34d7c4d2010-05-21 20:22:37 +000098 else if (!LangOpts.Microsoft && (Flags & KEYNOMS)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +000099
100 // Don't add this keyword if disabled in this language.
101 if (AddResult == 0) return;
102
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000103 IdentifierInfo &Info = Table.get(Keyword);
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 Info.setTokenID(TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000105 Info.setIsExtensionToken(AddResult == 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000106}
107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
109/// representations.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000110static void AddCXXOperatorKeyword(llvm::StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 tok::TokenKind TokenCode,
112 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000113 IdentifierInfo &Info = Table.get(Keyword);
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 Info.setTokenID(TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000115 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000116}
117
Mike Stump1eb44332009-09-09 15:08:12 +0000118/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000119/// "property".
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000120static void AddObjCKeyword(llvm::StringRef Name,
121 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000123 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000124}
125
126/// AddKeywords - Add all keywords to the symbol table.
127///
128void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 // Add keywords and tokens for the current language.
130#define KEYWORD(NAME, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000131 AddKeyword(llvm::StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000132 FLAGS, LangOpts, *this);
133#define ALIAS(NAME, TOK, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000134 AddKeyword(llvm::StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000135 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000136#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
137 if (LangOpts.CXXOperatorNames) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000138 AddCXXOperatorKeyword(llvm::StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000139#define OBJC1_AT_KEYWORD(NAME) \
140 if (LangOpts.ObjC1) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000141 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000142#define OBJC2_AT_KEYWORD(NAME) \
143 if (LangOpts.ObjC2) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000144 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000145#include "clang/Basic/TokenKinds.def"
146}
147
Chris Lattner387b98d2007-10-07 07:52:34 +0000148tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
149 // We use a perfect hash function here involving the length of the keyword,
150 // the first and third character. For preprocessor ID's there are no
151 // collisions (if there were, the switch below would complain about duplicate
152 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Chris Lattner387b98d2007-10-07 07:52:34 +0000154#define HASH(LEN, FIRST, THIRD) \
155 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
156#define CASE(LEN, FIRST, THIRD, NAME) \
157 case HASH(LEN, FIRST, THIRD): \
158 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Chris Lattner387b98d2007-10-07 07:52:34 +0000160 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000161 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000162 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000163 switch (HASH(Len, Name[0], Name[2])) {
164 default: return tok::pp_not_keyword;
165 CASE( 2, 'i', '\0', if);
166 CASE( 4, 'e', 'i', elif);
167 CASE( 4, 'e', 's', else);
168 CASE( 4, 'l', 'n', line);
169 CASE( 4, 's', 'c', sccs);
170 CASE( 5, 'e', 'd', endif);
171 CASE( 5, 'e', 'r', error);
172 CASE( 5, 'i', 'e', ident);
173 CASE( 5, 'i', 'd', ifdef);
174 CASE( 5, 'u', 'd', undef);
175
176 CASE( 6, 'a', 's', assert);
177 CASE( 6, 'd', 'f', define);
178 CASE( 6, 'i', 'n', ifndef);
179 CASE( 6, 'i', 'p', import);
180 CASE( 6, 'p', 'a', pragma);
181
182 CASE( 7, 'd', 'f', defined);
183 CASE( 7, 'i', 'c', include);
184 CASE( 7, 'w', 'r', warning);
185
186 CASE( 8, 'u', 'a', unassert);
187 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000189 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000190#undef CASE
191#undef HASH
192 }
193}
Reid Spencer5f016e22007-07-11 17:01:13 +0000194
195//===----------------------------------------------------------------------===//
196// Stats Implementation
197//===----------------------------------------------------------------------===//
198
199/// PrintStats - Print statistics about how well the identifier table is doing
200/// at hashing identifiers.
201void IdentifierTable::PrintStats() const {
202 unsigned NumBuckets = HashTable.getNumBuckets();
203 unsigned NumIdentifiers = HashTable.getNumItems();
204 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
205 unsigned AverageIdentifierSize = 0;
206 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000209 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
211 unsigned IdLen = I->getKeyLength();
212 AverageIdentifierSize += IdLen;
213 if (MaxIdentifierLength < IdLen)
214 MaxIdentifierLength = IdLen;
215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 fprintf(stderr, "\n*** Identifier Table Stats:\n");
218 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
219 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
220 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
221 NumIdentifiers/(double)NumBuckets);
222 fprintf(stderr, "Ave identifier length: %f\n",
223 (AverageIdentifierSize/(double)NumIdentifiers));
224 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 // Compute statistics about the memory allocated for identifiers.
227 HashTable.getAllocator().PrintStats();
228}
Steve Naroff68d331a2007-09-27 14:38:14 +0000229
Steve Naroff29238a02007-10-05 18:42:47 +0000230//===----------------------------------------------------------------------===//
231// SelectorTable Implementation
232//===----------------------------------------------------------------------===//
233
Chris Lattner85994262007-10-05 20:15:24 +0000234unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
235 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
236}
237
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000238namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000239/// MultiKeywordSelector - One of these variable length records is kept for each
240/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000241/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000242/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000243class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000244 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000245 MultiKeywordSelector(unsigned nKeys) {
246 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248public:
Steve Naroff29238a02007-10-05 18:42:47 +0000249 // Constructor for keyword selectors.
250 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
251 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000252 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Steve Naroff29238a02007-10-05 18:42:47 +0000254 // Fill in the trailing keyword array.
255 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
256 for (unsigned i = 0; i != nKeys; ++i)
257 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000258 }
259
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000260 // getName - Derive the full selector name and return it.
261 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000263 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Steve Naroff29238a02007-10-05 18:42:47 +0000265 typedef IdentifierInfo *const *keyword_iterator;
266 keyword_iterator keyword_begin() const {
267 return reinterpret_cast<keyword_iterator>(this+1);
268 }
Mike Stump1eb44332009-09-09 15:08:12 +0000269 keyword_iterator keyword_end() const {
270 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000271 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000272 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000273 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000274 return keyword_begin()[i];
275 }
Mike Stump1eb44332009-09-09 15:08:12 +0000276 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000277 keyword_iterator ArgTys, unsigned NumArgs) {
278 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000279 for (unsigned i = 0; i != NumArgs; ++i)
280 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000281 }
282 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000283 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000284 }
285};
Chris Lattner85994262007-10-05 20:15:24 +0000286} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000287
288unsigned Selector::getNumArgs() const {
289 unsigned IIF = getIdentifierInfoFlag();
290 if (IIF == ZeroArg)
291 return 0;
292 if (IIF == OneArg)
293 return 1;
294 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
295 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000296 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000297}
298
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000299IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000300 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000301 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000302 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000303 }
304 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
305 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
306 return SI->getIdentifierInfoForSlot(argIndex);
307}
308
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000309std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000310 llvm::SmallString<256> Str;
311 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000312 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
313 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000314 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000315 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000318 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000319}
320
Chris Lattner077bf5e2008-11-24 03:33:13 +0000321std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000322 if (InfoPtr == 0)
323 return "<null selector>";
324
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000325 if (InfoPtr & ArgFlags) {
326 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenek150ec292009-03-07 01:22:02 +0000328 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000329 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000330 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000331
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000332 if (!II)
333 return ":";
334
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000335 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000338 // We have a multiple keyword selector (no embedded flags).
339 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000340}
341
342
Chris Lattner5f7d2282009-03-04 05:35:38 +0000343namespace {
344 struct SelectorTableImpl {
345 llvm::FoldingSet<MultiKeywordSelector> Table;
346 llvm::BumpPtrAllocator Allocator;
347 };
348} // end anonymous namespace.
349
350static SelectorTableImpl &getSelectorTableImpl(void *P) {
351 return *static_cast<SelectorTableImpl*>(P);
352}
353
354
Chris Lattnerff384912007-10-07 02:00:24 +0000355Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
356 if (nKeys < 2)
357 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattner5f7d2282009-03-04 05:35:38 +0000359 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Steve Naroff29238a02007-10-05 18:42:47 +0000361 // Unique selector, to guarantee there is one per name.
362 llvm::FoldingSetNodeID ID;
363 MultiKeywordSelector::Profile(ID, IIV, nKeys);
364
365 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000366 if (MultiKeywordSelector *SI =
367 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000368 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Steve Naroff29238a02007-10-05 18:42:47 +0000370 // MultiKeywordSelector objects are not allocated with new because they have a
371 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000372 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
373 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000374 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner5f7d2282009-03-04 05:35:38 +0000375 llvm::alignof<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000376 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000377 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000378 return Selector(SI);
379}
380
Steve Naroff29238a02007-10-05 18:42:47 +0000381SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000382 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000383}
384
385SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000386 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000387}
388
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000389const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
390 switch (Operator) {
391 case OO_None:
392 case NUM_OVERLOADED_OPERATORS:
393 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000394
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000395#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
396 case OO_##Name: return Spelling;
397#include "clang/Basic/OperatorKinds.def"
398 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000399
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000400 return 0;
401}
402