blob: ed0de8c4af09e3f72ad053566f3e0cc9e1383cf4 [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,
73 KEYALTIVEC = 128
Eli Friedmaneb32fde2009-04-28 03:13:54 +000074 };
75}
76
Reid Spencer5f016e22007-07-11 17:01:13 +000077/// AddKeyword - This method is used to associate a token ID with specific
78/// identifiers because they are language keywords. This causes the lexer to
79/// automatically map matching identifiers to specialized token codes.
80///
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000081/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattnerd4b80f12007-07-16 04:18:29 +000082/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000083/// in the specified language, and set to 0 if disabled in the
Chris Lattnerd4b80f12007-07-16 04:18:29 +000084/// specified language.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +000085static void AddKeyword(llvm::StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +000086 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +000087 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +000088 unsigned AddResult = 0;
89 if (Flags & KEYALL) AddResult = 2;
90 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
91 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
92 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000093 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedmaneb32fde2009-04-28 03:13:54 +000094 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +000095 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +000096 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +000097
98 // Don't add this keyword if disabled in this language.
99 if (AddResult == 0) return;
100
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000101 IdentifierInfo &Info = Table.get(Keyword);
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 Info.setTokenID(TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000103 Info.setIsExtensionToken(AddResult == 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000104}
105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
107/// representations.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000108static void AddCXXOperatorKeyword(llvm::StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 tok::TokenKind TokenCode,
110 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000111 IdentifierInfo &Info = Table.get(Keyword);
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 Info.setTokenID(TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000113 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000114}
115
Mike Stump1eb44332009-09-09 15:08:12 +0000116/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000117/// "property".
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000118static void AddObjCKeyword(llvm::StringRef Name,
119 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000121 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000122}
123
124/// AddKeywords - Add all keywords to the symbol table.
125///
126void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // Add keywords and tokens for the current language.
128#define KEYWORD(NAME, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000129 AddKeyword(llvm::StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000130 FLAGS, LangOpts, *this);
131#define ALIAS(NAME, TOK, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000132 AddKeyword(llvm::StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000133 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000134#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
135 if (LangOpts.CXXOperatorNames) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000136 AddCXXOperatorKeyword(llvm::StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000137#define OBJC1_AT_KEYWORD(NAME) \
138 if (LangOpts.ObjC1) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000139 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000140#define OBJC2_AT_KEYWORD(NAME) \
141 if (LangOpts.ObjC2) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000142 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143#include "clang/Basic/TokenKinds.def"
144}
145
Chris Lattner387b98d2007-10-07 07:52:34 +0000146tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
147 // We use a perfect hash function here involving the length of the keyword,
148 // the first and third character. For preprocessor ID's there are no
149 // collisions (if there were, the switch below would complain about duplicate
150 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Chris Lattner387b98d2007-10-07 07:52:34 +0000152#define HASH(LEN, FIRST, THIRD) \
153 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
154#define CASE(LEN, FIRST, THIRD, NAME) \
155 case HASH(LEN, FIRST, THIRD): \
156 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattner387b98d2007-10-07 07:52:34 +0000158 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000159 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000160 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000161 switch (HASH(Len, Name[0], Name[2])) {
162 default: return tok::pp_not_keyword;
163 CASE( 2, 'i', '\0', if);
164 CASE( 4, 'e', 'i', elif);
165 CASE( 4, 'e', 's', else);
166 CASE( 4, 'l', 'n', line);
167 CASE( 4, 's', 'c', sccs);
168 CASE( 5, 'e', 'd', endif);
169 CASE( 5, 'e', 'r', error);
170 CASE( 5, 'i', 'e', ident);
171 CASE( 5, 'i', 'd', ifdef);
172 CASE( 5, 'u', 'd', undef);
173
174 CASE( 6, 'a', 's', assert);
175 CASE( 6, 'd', 'f', define);
176 CASE( 6, 'i', 'n', ifndef);
177 CASE( 6, 'i', 'p', import);
178 CASE( 6, 'p', 'a', pragma);
179
180 CASE( 7, 'd', 'f', defined);
181 CASE( 7, 'i', 'c', include);
182 CASE( 7, 'w', 'r', warning);
183
184 CASE( 8, 'u', 'a', unassert);
185 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000187 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000188#undef CASE
189#undef HASH
190 }
191}
Reid Spencer5f016e22007-07-11 17:01:13 +0000192
193//===----------------------------------------------------------------------===//
194// Stats Implementation
195//===----------------------------------------------------------------------===//
196
197/// PrintStats - Print statistics about how well the identifier table is doing
198/// at hashing identifiers.
199void IdentifierTable::PrintStats() const {
200 unsigned NumBuckets = HashTable.getNumBuckets();
201 unsigned NumIdentifiers = HashTable.getNumItems();
202 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
203 unsigned AverageIdentifierSize = 0;
204 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000207 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
209 unsigned IdLen = I->getKeyLength();
210 AverageIdentifierSize += IdLen;
211 if (MaxIdentifierLength < IdLen)
212 MaxIdentifierLength = IdLen;
213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 fprintf(stderr, "\n*** Identifier Table Stats:\n");
216 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
217 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
218 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
219 NumIdentifiers/(double)NumBuckets);
220 fprintf(stderr, "Ave identifier length: %f\n",
221 (AverageIdentifierSize/(double)NumIdentifiers));
222 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // Compute statistics about the memory allocated for identifiers.
225 HashTable.getAllocator().PrintStats();
226}
Steve Naroff68d331a2007-09-27 14:38:14 +0000227
Steve Naroff29238a02007-10-05 18:42:47 +0000228//===----------------------------------------------------------------------===//
229// SelectorTable Implementation
230//===----------------------------------------------------------------------===//
231
Chris Lattner85994262007-10-05 20:15:24 +0000232unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
233 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
234}
235
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000236namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000237/// MultiKeywordSelector - One of these variable length records is kept for each
238/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000239/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000240/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000241class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000242 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000243 MultiKeywordSelector(unsigned nKeys) {
244 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246public:
Steve Naroff29238a02007-10-05 18:42:47 +0000247 // Constructor for keyword selectors.
248 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
249 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000250 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Steve Naroff29238a02007-10-05 18:42:47 +0000252 // Fill in the trailing keyword array.
253 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
254 for (unsigned i = 0; i != nKeys; ++i)
255 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000256 }
257
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000258 // getName - Derive the full selector name and return it.
259 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000261 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Steve Naroff29238a02007-10-05 18:42:47 +0000263 typedef IdentifierInfo *const *keyword_iterator;
264 keyword_iterator keyword_begin() const {
265 return reinterpret_cast<keyword_iterator>(this+1);
266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267 keyword_iterator keyword_end() const {
268 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000269 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000270 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000271 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000272 return keyword_begin()[i];
273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000275 keyword_iterator ArgTys, unsigned NumArgs) {
276 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000277 for (unsigned i = 0; i != NumArgs; ++i)
278 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000279 }
280 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000281 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000282 }
283};
Chris Lattner85994262007-10-05 20:15:24 +0000284} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000285
286unsigned Selector::getNumArgs() const {
287 unsigned IIF = getIdentifierInfoFlag();
288 if (IIF == ZeroArg)
289 return 0;
290 if (IIF == OneArg)
291 return 1;
292 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
293 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000294 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000295}
296
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000297IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000298 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000299 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000300 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000301 }
302 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
303 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
304 return SI->getIdentifierInfoForSlot(argIndex);
305}
306
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000307std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000308 llvm::SmallString<256> Str;
309 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000310 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
311 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000312 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000313 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000316 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000317}
318
Chris Lattner077bf5e2008-11-24 03:33:13 +0000319std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000320 if (InfoPtr == 0)
321 return "<null selector>";
322
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000323 if (InfoPtr & ArgFlags) {
324 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremenek150ec292009-03-07 01:22:02 +0000326 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000327 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000328 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000329
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000330 if (!II)
331 return ":";
332
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000333 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000336 // We have a multiple keyword selector (no embedded flags).
337 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000338}
339
340
Chris Lattner5f7d2282009-03-04 05:35:38 +0000341namespace {
342 struct SelectorTableImpl {
343 llvm::FoldingSet<MultiKeywordSelector> Table;
344 llvm::BumpPtrAllocator Allocator;
345 };
346} // end anonymous namespace.
347
348static SelectorTableImpl &getSelectorTableImpl(void *P) {
349 return *static_cast<SelectorTableImpl*>(P);
350}
351
352
Chris Lattnerff384912007-10-07 02:00:24 +0000353Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
354 if (nKeys < 2)
355 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner5f7d2282009-03-04 05:35:38 +0000357 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Steve Naroff29238a02007-10-05 18:42:47 +0000359 // Unique selector, to guarantee there is one per name.
360 llvm::FoldingSetNodeID ID;
361 MultiKeywordSelector::Profile(ID, IIV, nKeys);
362
363 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000364 if (MultiKeywordSelector *SI =
365 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000366 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Steve Naroff29238a02007-10-05 18:42:47 +0000368 // MultiKeywordSelector objects are not allocated with new because they have a
369 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000370 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
371 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000372 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner5f7d2282009-03-04 05:35:38 +0000373 llvm::alignof<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000374 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000375 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000376 return Selector(SI);
377}
378
Steve Naroff29238a02007-10-05 18:42:47 +0000379SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000380 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000381}
382
383SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000384 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000385}
386
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000387const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
388 switch (Operator) {
389 case OO_None:
390 case NUM_OVERLOADED_OPERATORS:
391 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000392
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000393#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
394 case OO_##Name: return Spelling;
395#include "clang/Basic/OperatorKinds.def"
396 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000397
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000398 return 0;
399}
400