blob: 401e6cba06928ad293612803acd3904c4266531a [file] [log] [blame]
Chris Lattnerec659fc2006-10-29 22:09:44 +00001//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000010// This file implements the IdentifierInfo, IdentifierVisitor, and
Chris Lattner91cbf112006-07-03 04:28:52 +000011// IdentifierTable interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattneref6b1362007-10-07 08:58:51 +000015#include "clang/Basic/IdentifierTable.h"
Chris Lattner25e0d542006-10-18 06:07:05 +000016#include "clang/Basic/LangOptions.h"
Steve Naroffe61bfa82007-10-05 18:42:47 +000017#include "llvm/ADT/FoldingSet.h"
Chris Lattnerdadc7622007-10-05 20:15:24 +000018#include "llvm/ADT/DenseMap.h"
Daniel Dunbar1c0761d2009-10-17 18:13:02 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000020#include <cstdio>
Ted Kremenekf25f4a32007-10-23 22:18:37 +000021
Chris Lattner22eb9722006-06-18 05:43:12 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000025// IdentifierInfo Implementation
Chris Lattner22eb9722006-06-18 05:43:12 +000026//===----------------------------------------------------------------------===//
27
Ted Kremenek52f73ca2009-01-20 23:28:34 +000028IdentifierInfo::IdentifierInfo() {
Chris Lattner3bc804e2006-10-28 23:46:24 +000029 TokenID = tok::identifier;
Douglas Gregor2ad7ee92008-11-06 16:32:23 +000030 ObjCOrBuiltinID = 0;
Chris Lattnera441ca62007-10-07 07:09:52 +000031 HasMacro = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000032 IsExtension = false;
33 IsPoisoned = false;
Chris Lattner5b9f4892006-11-21 17:23:33 +000034 IsCPPOperatorKeyword = false;
Chris Lattnerad89ec02009-01-21 07:43:11 +000035 NeedsHandleIdentifier = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000036 FETokenInfo = 0;
Ted Kremenek52f73ca2009-01-20 23:28:34 +000037 Entry = 0;
Chris Lattner3bc804e2006-10-28 23:46:24 +000038}
39
Chris Lattner91cbf112006-07-03 04:28:52 +000040//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +000041// IdentifierTable Implementation
42//===----------------------------------------------------------------------===//
43
Ted Kremeneka705b042009-01-15 18:47:46 +000044IdentifierInfoLookup::~IdentifierInfoLookup() {}
45
Douglas Gregor99734e72009-04-25 23:30:02 +000046ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
47
Ted Kremeneka705b042009-01-15 18:47:46 +000048IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
49 IdentifierInfoLookup* externalLookup)
50 : HashTable(8192), // Start with space for 8K identifiers.
51 ExternalLookup(externalLookup) {
Chris Lattnerda933aa2006-10-29 23:49:15 +000052
Chris Lattnerf2e3ac32006-10-27 03:59:10 +000053 // Populate the identifier table with info about keywords for the current
54 // language.
Chris Lattner25e0d542006-10-18 06:07:05 +000055 AddKeywords(LangOpts);
Chris Lattner91cbf112006-07-03 04:28:52 +000056}
Chris Lattner22eb9722006-06-18 05:43:12 +000057
Chris Lattner25e0d542006-10-18 06:07:05 +000058//===----------------------------------------------------------------------===//
59// Language Keyword Implementation
60//===----------------------------------------------------------------------===//
61
Eli Friedman2b680b42009-04-28 03:13:54 +000062// Constants for TokenKinds.def
63namespace {
64 enum {
65 KEYALL = 1,
66 KEYC99 = 2,
67 KEYCXX = 4,
68 KEYCXX0X = 8,
69 KEYGNU = 16,
Nate Begeman8b68d6f2009-06-25 23:25:15 +000070 KEYMS = 32,
71 BOOLSUPPORT = 64
Eli Friedman2b680b42009-04-28 03:13:54 +000072 };
73}
74
Chris Lattner25e0d542006-10-18 06:07:05 +000075/// AddKeyword - This method is used to associate a token ID with specific
76/// identifiers because they are language keywords. This causes the lexer to
77/// automatically map matching identifiers to specialized token codes.
78///
Chris Lattner539007a2007-07-16 04:18:29 +000079/// The C90/C99/CPP/CPP0x flags are set to 0 if the token should be
80/// enabled in the specified langauge, set to 1 if it is an extension
81/// in the specified language, and set to 2 if disabled in the
82/// specified language.
Chris Lattner2b9e19b2006-10-29 23:43:13 +000083static void AddKeyword(const char *Keyword, unsigned KWLen,
Eli Friedman2b680b42009-04-28 03:13:54 +000084 tok::TokenKind TokenCode, unsigned Flags,
Chris Lattner25e0d542006-10-18 06:07:05 +000085 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedman2b680b42009-04-28 03:13:54 +000086 unsigned AddResult = 0;
87 if (Flags & KEYALL) AddResult = 2;
88 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
89 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
90 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
91 else if (LangOpts.GNUMode && (Flags & KEYGNU)) AddResult = 1;
92 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Chris Lattnerc61089a2009-06-30 01:26:17 +000093 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
Eli Friedman2b680b42009-04-28 03:13:54 +000094
95 // Don't add this keyword if disabled in this language.
96 if (AddResult == 0) return;
97
Chris Lattner2b9e19b2006-10-29 23:43:13 +000098 IdentifierInfo &Info = Table.get(Keyword, Keyword+KWLen);
Chris Lattner25e0d542006-10-18 06:07:05 +000099 Info.setTokenID(TokenCode);
Eli Friedman2b680b42009-04-28 03:13:54 +0000100 Info.setIsExtensionToken(AddResult == 1);
Chris Lattner25e0d542006-10-18 06:07:05 +0000101}
102
Chris Lattner5b9f4892006-11-21 17:23:33 +0000103/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
104/// representations.
105static void AddCXXOperatorKeyword(const char *Keyword, unsigned KWLen,
106 tok::TokenKind TokenCode,
107 IdentifierTable &Table) {
108 IdentifierInfo &Info = Table.get(Keyword, Keyword + KWLen);
109 Info.setTokenID(TokenCode);
Ted Kremenekf25f4a32007-10-23 22:18:37 +0000110 Info.setIsCPlusPlusOperatorKeyword();
Chris Lattner5b9f4892006-11-21 17:23:33 +0000111}
112
Mike Stump11289f42009-09-09 15:08:12 +0000113/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Chris Lattner25e0d542006-10-18 06:07:05 +0000114/// "property".
Mike Stump11289f42009-09-09 15:08:12 +0000115static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID,
Chris Lattner25e0d542006-10-18 06:07:05 +0000116 const char *Name, unsigned NameLen,
117 IdentifierTable &Table) {
118 Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID);
119}
120
121/// AddKeywords - Add all keywords to the symbol table.
122///
123void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Chris Lattner25e0d542006-10-18 06:07:05 +0000124 // Add keywords and tokens for the current language.
125#define KEYWORD(NAME, FLAGS) \
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000126 AddKeyword(#NAME, strlen(#NAME), tok::kw_ ## NAME, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000127 FLAGS, LangOpts, *this);
128#define ALIAS(NAME, TOK, FLAGS) \
129 AddKeyword(NAME, strlen(NAME), tok::kw_ ## TOK, \
130 FLAGS, LangOpts, *this);
Chris Lattner5b9f4892006-11-21 17:23:33 +0000131#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
Chris Lattner3e7592e2006-12-04 07:48:37 +0000132 if (LangOpts.CXXOperatorNames) \
Chris Lattner5b9f4892006-11-21 17:23:33 +0000133 AddCXXOperatorKeyword(#NAME, strlen(#NAME), tok::ALIAS, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000134#define OBJC1_AT_KEYWORD(NAME) \
135 if (LangOpts.ObjC1) \
136 AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
137#define OBJC2_AT_KEYWORD(NAME) \
138 if (LangOpts.ObjC2) \
139 AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
140#include "clang/Basic/TokenKinds.def"
141}
142
Chris Lattnerff067ce2007-10-07 07:52:34 +0000143tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
144 // We use a perfect hash function here involving the length of the keyword,
145 // the first and third character. For preprocessor ID's there are no
146 // collisions (if there were, the switch below would complain about duplicate
147 // case values). Note that this depends on 'if' being null terminated.
Mike Stump11289f42009-09-09 15:08:12 +0000148
Chris Lattnerff067ce2007-10-07 07:52:34 +0000149#define HASH(LEN, FIRST, THIRD) \
150 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
151#define CASE(LEN, FIRST, THIRD, NAME) \
152 case HASH(LEN, FIRST, THIRD): \
153 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chris Lattnerff067ce2007-10-07 07:52:34 +0000155 unsigned Len = getLength();
Chris Lattnerd2b8ce42007-10-10 20:59:57 +0000156 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000157 const char *Name = getNameStart();
Chris Lattnerff067ce2007-10-07 07:52:34 +0000158 switch (HASH(Len, Name[0], Name[2])) {
159 default: return tok::pp_not_keyword;
160 CASE( 2, 'i', '\0', if);
161 CASE( 4, 'e', 'i', elif);
162 CASE( 4, 'e', 's', else);
163 CASE( 4, 'l', 'n', line);
164 CASE( 4, 's', 'c', sccs);
165 CASE( 5, 'e', 'd', endif);
166 CASE( 5, 'e', 'r', error);
167 CASE( 5, 'i', 'e', ident);
168 CASE( 5, 'i', 'd', ifdef);
169 CASE( 5, 'u', 'd', undef);
170
171 CASE( 6, 'a', 's', assert);
172 CASE( 6, 'd', 'f', define);
173 CASE( 6, 'i', 'n', ifndef);
174 CASE( 6, 'i', 'p', import);
175 CASE( 6, 'p', 'a', pragma);
176
177 CASE( 7, 'd', 'f', defined);
178 CASE( 7, 'i', 'c', include);
179 CASE( 7, 'w', 'r', warning);
180
181 CASE( 8, 'u', 'a', unassert);
182 CASE(12, 'i', 'c', include_next);
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattner14a7f392009-04-08 18:24:34 +0000184 CASE(16, '_', 'i', __include_macros);
Chris Lattnerff067ce2007-10-07 07:52:34 +0000185#undef CASE
186#undef HASH
187 }
188}
Chris Lattner25e0d542006-10-18 06:07:05 +0000189
190//===----------------------------------------------------------------------===//
191// Stats Implementation
192//===----------------------------------------------------------------------===//
193
Chris Lattner22eb9722006-06-18 05:43:12 +0000194/// PrintStats - Print statistics about how well the identifier table is doing
195/// at hashing identifiers.
196void IdentifierTable::PrintStats() const {
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000197 unsigned NumBuckets = HashTable.getNumBuckets();
198 unsigned NumIdentifiers = HashTable.getNumItems();
199 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
Chris Lattner22eb9722006-06-18 05:43:12 +0000200 unsigned AverageIdentifierSize = 0;
201 unsigned MaxIdentifierLength = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000203 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000204 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000205 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
206 unsigned IdLen = I->getKeyLength();
207 AverageIdentifierSize += IdLen;
208 if (MaxIdentifierLength < IdLen)
209 MaxIdentifierLength = IdLen;
210 }
Mike Stump11289f42009-09-09 15:08:12 +0000211
Chris Lattner23b7eb62007-06-15 23:05:46 +0000212 fprintf(stderr, "\n*** Identifier Table Stats:\n");
213 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
214 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
215 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
216 NumIdentifiers/(double)NumBuckets);
217 fprintf(stderr, "Ave identifier length: %f\n",
218 (AverageIdentifierSize/(double)NumIdentifiers));
219 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump11289f42009-09-09 15:08:12 +0000220
Chris Lattner22eb9722006-06-18 05:43:12 +0000221 // Compute statistics about the memory allocated for identifiers.
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000222 HashTable.getAllocator().PrintStats();
Chris Lattner22eb9722006-06-18 05:43:12 +0000223}
Steve Narofff73590d2007-09-27 14:38:14 +0000224
Steve Naroffe61bfa82007-10-05 18:42:47 +0000225//===----------------------------------------------------------------------===//
226// SelectorTable Implementation
227//===----------------------------------------------------------------------===//
228
Chris Lattnerdadc7622007-10-05 20:15:24 +0000229unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
230 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
231}
232
Douglas Gregor77324f32008-11-17 14:58:09 +0000233namespace clang {
Steve Naroffe61bfa82007-10-05 18:42:47 +0000234/// MultiKeywordSelector - One of these variable length records is kept for each
235/// selector containing more than one keyword. We use a folding set
Mike Stump11289f42009-09-09 15:08:12 +0000236/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroffe61bfa82007-10-05 18:42:47 +0000237/// this class is provided strictly through Selector.
Mike Stump11289f42009-09-09 15:08:12 +0000238class MultiKeywordSelector
Douglas Gregor77324f32008-11-17 14:58:09 +0000239 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor77324f32008-11-17 14:58:09 +0000240 MultiKeywordSelector(unsigned nKeys) {
241 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
242 }
Mike Stump11289f42009-09-09 15:08:12 +0000243public:
Steve Naroffe61bfa82007-10-05 18:42:47 +0000244 // Constructor for keyword selectors.
245 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
246 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor77324f32008-11-17 14:58:09 +0000247 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump11289f42009-09-09 15:08:12 +0000248
Steve Naroffe61bfa82007-10-05 18:42:47 +0000249 // Fill in the trailing keyword array.
250 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
251 for (unsigned i = 0; i != nKeys; ++i)
252 KeyInfo[i] = IIV[i];
Mike Stump11289f42009-09-09 15:08:12 +0000253 }
254
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000255 // getName - Derive the full selector name and return it.
256 std::string getName() const;
Mike Stump11289f42009-09-09 15:08:12 +0000257
Douglas Gregor77324f32008-11-17 14:58:09 +0000258 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump11289f42009-09-09 15:08:12 +0000259
Steve Naroffe61bfa82007-10-05 18:42:47 +0000260 typedef IdentifierInfo *const *keyword_iterator;
261 keyword_iterator keyword_begin() const {
262 return reinterpret_cast<keyword_iterator>(this+1);
263 }
Mike Stump11289f42009-09-09 15:08:12 +0000264 keyword_iterator keyword_end() const {
265 return keyword_begin()+getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000266 }
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000267 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor77324f32008-11-17 14:58:09 +0000268 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroffe61bfa82007-10-05 18:42:47 +0000269 return keyword_begin()[i];
270 }
Mike Stump11289f42009-09-09 15:08:12 +0000271 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroffe61bfa82007-10-05 18:42:47 +0000272 keyword_iterator ArgTys, unsigned NumArgs) {
273 ID.AddInteger(NumArgs);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000274 for (unsigned i = 0; i != NumArgs; ++i)
275 ID.AddPointer(ArgTys[i]);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000276 }
277 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000278 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000279 }
280};
Chris Lattnerdadc7622007-10-05 20:15:24 +0000281} // end namespace clang.
Steve Naroffe61bfa82007-10-05 18:42:47 +0000282
283unsigned Selector::getNumArgs() const {
284 unsigned IIF = getIdentifierInfoFlag();
285 if (IIF == ZeroArg)
286 return 0;
287 if (IIF == OneArg)
288 return 1;
289 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
290 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump11289f42009-09-09 15:08:12 +0000291 return SI->getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000292}
293
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000294IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor70091b82009-04-26 22:20:50 +0000295 if (getIdentifierInfoFlag()) {
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000296 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor70091b82009-04-26 22:20:50 +0000297 return getAsIdentifierInfo();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000298 }
299 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
300 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
301 return SI->getIdentifierInfoForSlot(argIndex);
302}
303
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000304std::string MultiKeywordSelector::getName() const {
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000305 llvm::SmallString<256> Str;
306 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000307 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
308 if (*I)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000309 OS << (*I)->getName();
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000310 OS << ':';
Steve Naroffe61bfa82007-10-05 18:42:47 +0000311 }
Mike Stump11289f42009-09-09 15:08:12 +0000312
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000313 return OS.str();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000314}
315
Chris Lattnere4b95692008-11-24 03:33:13 +0000316std::string Selector::getAsString() const {
Douglas Gregor70091b82009-04-26 22:20:50 +0000317 if (InfoPtr == 0)
318 return "<null selector>";
319
Ted Kremenek5f080b42009-03-06 23:36:28 +0000320 if (InfoPtr & ArgFlags) {
321 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000322
Ted Kremenek0666a6c2009-03-07 01:22:02 +0000323 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenek5f080b42009-03-06 23:36:28 +0000324 if (getNumArgs() == 0)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000325 return II->getName();
Ted Kremenek5f080b42009-03-06 23:36:28 +0000326
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000327 if (!II)
328 return ":";
329
Daniel Dunbar07d07852009-10-18 21:17:35 +0000330 return II->getName().str() + ":";
Steve Naroffe61bfa82007-10-05 18:42:47 +0000331 }
Mike Stump11289f42009-09-09 15:08:12 +0000332
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000333 // We have a multiple keyword selector (no embedded flags).
334 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000335}
336
337
Chris Lattner1a849942009-03-04 05:35:38 +0000338namespace {
339 struct SelectorTableImpl {
340 llvm::FoldingSet<MultiKeywordSelector> Table;
341 llvm::BumpPtrAllocator Allocator;
342 };
343} // end anonymous namespace.
344
345static SelectorTableImpl &getSelectorTableImpl(void *P) {
346 return *static_cast<SelectorTableImpl*>(P);
347}
348
349
Chris Lattner5700fab2007-10-07 02:00:24 +0000350Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
351 if (nKeys < 2)
352 return Selector(IIV[0], nKeys);
Mike Stump11289f42009-09-09 15:08:12 +0000353
Chris Lattner1a849942009-03-04 05:35:38 +0000354 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump11289f42009-09-09 15:08:12 +0000355
Steve Naroffe61bfa82007-10-05 18:42:47 +0000356 // Unique selector, to guarantee there is one per name.
357 llvm::FoldingSetNodeID ID;
358 MultiKeywordSelector::Profile(ID, IIV, nKeys);
359
360 void *InsertPos = 0;
Chris Lattner1a849942009-03-04 05:35:38 +0000361 if (MultiKeywordSelector *SI =
362 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe61bfa82007-10-05 18:42:47 +0000363 return Selector(SI);
Mike Stump11289f42009-09-09 15:08:12 +0000364
Steve Naroffe61bfa82007-10-05 18:42:47 +0000365 // MultiKeywordSelector objects are not allocated with new because they have a
366 // variable size array (for parameter types) at the end of them.
Chris Lattner1a849942009-03-04 05:35:38 +0000367 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
368 MultiKeywordSelector *SI =
Mike Stump11289f42009-09-09 15:08:12 +0000369 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner1a849942009-03-04 05:35:38 +0000370 llvm::alignof<MultiKeywordSelector>());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000371 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner1a849942009-03-04 05:35:38 +0000372 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000373 return Selector(SI);
374}
375
Steve Naroffe61bfa82007-10-05 18:42:47 +0000376SelectorTable::SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000377 Impl = new SelectorTableImpl();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000378}
379
380SelectorTable::~SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000381 delete &getSelectorTableImpl(Impl);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000382}
383
Douglas Gregor71395fa2009-11-04 00:56:37 +0000384const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
385 switch (Operator) {
386 case OO_None:
387 case NUM_OVERLOADED_OPERATORS:
388 return 0;
389
390#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
391 case OO_##Name: return Spelling;
392#include "clang/Basic/OperatorKinds.def"
393 }
394
395 return 0;
396}
397