blob: 1f0c52bdc19898f1073b51c3fd3a6f886b5bd1fb [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;
Sebastian Redlffaab3e2010-07-30 00:29:29 +000037 IsFromPCH = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000038 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000039 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000040}
41
Reid Spencer5f016e22007-07-11 17:01:13 +000042//===----------------------------------------------------------------------===//
43// IdentifierTable Implementation
44//===----------------------------------------------------------------------===//
45
Ted Kremenek72b1b152009-01-15 18:47:46 +000046IdentifierInfoLookup::~IdentifierInfoLookup() {}
47
Douglas Gregor8c5a7602009-04-25 23:30:02 +000048ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
49
Ted Kremenek72b1b152009-01-15 18:47:46 +000050IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
51 IdentifierInfoLookup* externalLookup)
52 : HashTable(8192), // Start with space for 8K identifiers.
53 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000054
55 // Populate the identifier table with info about keywords for the current
56 // language.
57 AddKeywords(LangOpts);
58}
59
60//===----------------------------------------------------------------------===//
61// Language Keyword Implementation
62//===----------------------------------------------------------------------===//
63
Eli Friedmaneb32fde2009-04-28 03:13:54 +000064// Constants for TokenKinds.def
65namespace {
66 enum {
67 KEYALL = 1,
68 KEYC99 = 2,
69 KEYCXX = 4,
70 KEYCXX0X = 8,
71 KEYGNU = 16,
Nate Begeman72b60e32009-06-25 23:25:15 +000072 KEYMS = 32,
John Thompson82287d12010-02-05 00:12:22 +000073 BOOLSUPPORT = 64,
Chris Lattner34d7c4d2010-05-21 20:22:37 +000074 KEYALTIVEC = 128,
75 KEYNOMS = 256
Eli Friedmaneb32fde2009-04-28 03:13:54 +000076 };
77}
78
Reid Spencer5f016e22007-07-11 17:01:13 +000079/// AddKeyword - This method is used to associate a token ID with specific
80/// identifiers because they are language keywords. This causes the lexer to
81/// automatically map matching identifiers to specialized token codes.
82///
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000083/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattnerd4b80f12007-07-16 04:18:29 +000084/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000085/// in the specified language, and set to 0 if disabled in the
Chris Lattnerd4b80f12007-07-16 04:18:29 +000086/// specified language.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +000087static void AddKeyword(llvm::StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +000088 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +000089 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +000090 unsigned AddResult = 0;
91 if (Flags & KEYALL) AddResult = 2;
92 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
93 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
94 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000095 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedmaneb32fde2009-04-28 03:13:54 +000096 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +000097 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +000098 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Chris Lattner34d7c4d2010-05-21 20:22:37 +000099 else if (!LangOpts.Microsoft && (Flags & KEYNOMS)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000100
101 // Don't add this keyword if disabled in this language.
102 if (AddResult == 0) return;
103
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000104 IdentifierInfo &Info = Table.get(Keyword);
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 Info.setTokenID(TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000106 Info.setIsExtensionToken(AddResult == 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000107}
108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
110/// representations.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000111static void AddCXXOperatorKeyword(llvm::StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 tok::TokenKind TokenCode,
113 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000114 IdentifierInfo &Info = Table.get(Keyword);
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 Info.setTokenID(TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000116 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000117}
118
Mike Stump1eb44332009-09-09 15:08:12 +0000119/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000120/// "property".
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000121static void AddObjCKeyword(llvm::StringRef Name,
122 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000124 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000125}
126
127/// AddKeywords - Add all keywords to the symbol table.
128///
129void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 // Add keywords and tokens for the current language.
131#define KEYWORD(NAME, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000132 AddKeyword(llvm::StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000133 FLAGS, LangOpts, *this);
134#define ALIAS(NAME, TOK, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000135 AddKeyword(llvm::StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000136 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000137#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
138 if (LangOpts.CXXOperatorNames) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000139 AddCXXOperatorKeyword(llvm::StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000140#define OBJC1_AT_KEYWORD(NAME) \
141 if (LangOpts.ObjC1) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000142 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143#define OBJC2_AT_KEYWORD(NAME) \
144 if (LangOpts.ObjC2) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000145 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000146#include "clang/Basic/TokenKinds.def"
147}
148
Chris Lattner387b98d2007-10-07 07:52:34 +0000149tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
150 // We use a perfect hash function here involving the length of the keyword,
151 // the first and third character. For preprocessor ID's there are no
152 // collisions (if there were, the switch below would complain about duplicate
153 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Chris Lattner387b98d2007-10-07 07:52:34 +0000155#define HASH(LEN, FIRST, THIRD) \
156 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
157#define CASE(LEN, FIRST, THIRD, NAME) \
158 case HASH(LEN, FIRST, THIRD): \
159 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Chris Lattner387b98d2007-10-07 07:52:34 +0000161 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000162 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000163 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000164 switch (HASH(Len, Name[0], Name[2])) {
165 default: return tok::pp_not_keyword;
166 CASE( 2, 'i', '\0', if);
167 CASE( 4, 'e', 'i', elif);
168 CASE( 4, 'e', 's', else);
169 CASE( 4, 'l', 'n', line);
170 CASE( 4, 's', 'c', sccs);
171 CASE( 5, 'e', 'd', endif);
172 CASE( 5, 'e', 'r', error);
173 CASE( 5, 'i', 'e', ident);
174 CASE( 5, 'i', 'd', ifdef);
175 CASE( 5, 'u', 'd', undef);
176
177 CASE( 6, 'a', 's', assert);
178 CASE( 6, 'd', 'f', define);
179 CASE( 6, 'i', 'n', ifndef);
180 CASE( 6, 'i', 'p', import);
181 CASE( 6, 'p', 'a', pragma);
182
183 CASE( 7, 'd', 'f', defined);
184 CASE( 7, 'i', 'c', include);
185 CASE( 7, 'w', 'r', warning);
186
187 CASE( 8, 'u', 'a', unassert);
188 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000190 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000191#undef CASE
192#undef HASH
193 }
194}
Reid Spencer5f016e22007-07-11 17:01:13 +0000195
196//===----------------------------------------------------------------------===//
197// Stats Implementation
198//===----------------------------------------------------------------------===//
199
200/// PrintStats - Print statistics about how well the identifier table is doing
201/// at hashing identifiers.
202void IdentifierTable::PrintStats() const {
203 unsigned NumBuckets = HashTable.getNumBuckets();
204 unsigned NumIdentifiers = HashTable.getNumItems();
205 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
206 unsigned AverageIdentifierSize = 0;
207 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000210 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
212 unsigned IdLen = I->getKeyLength();
213 AverageIdentifierSize += IdLen;
214 if (MaxIdentifierLength < IdLen)
215 MaxIdentifierLength = IdLen;
216 }
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 fprintf(stderr, "\n*** Identifier Table Stats:\n");
219 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
220 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
221 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
222 NumIdentifiers/(double)NumBuckets);
223 fprintf(stderr, "Ave identifier length: %f\n",
224 (AverageIdentifierSize/(double)NumIdentifiers));
225 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 // Compute statistics about the memory allocated for identifiers.
228 HashTable.getAllocator().PrintStats();
229}
Steve Naroff68d331a2007-09-27 14:38:14 +0000230
Steve Naroff29238a02007-10-05 18:42:47 +0000231//===----------------------------------------------------------------------===//
232// SelectorTable Implementation
233//===----------------------------------------------------------------------===//
234
Chris Lattner85994262007-10-05 20:15:24 +0000235unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
236 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
237}
238
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000239namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000240/// MultiKeywordSelector - One of these variable length records is kept for each
241/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000242/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000243/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000244class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000245 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000246 MultiKeywordSelector(unsigned nKeys) {
247 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249public:
Steve Naroff29238a02007-10-05 18:42:47 +0000250 // Constructor for keyword selectors.
251 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
252 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000253 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Steve Naroff29238a02007-10-05 18:42:47 +0000255 // Fill in the trailing keyword array.
256 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
257 for (unsigned i = 0; i != nKeys; ++i)
258 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000259 }
260
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000261 // getName - Derive the full selector name and return it.
262 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000264 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Steve Naroff29238a02007-10-05 18:42:47 +0000266 typedef IdentifierInfo *const *keyword_iterator;
267 keyword_iterator keyword_begin() const {
268 return reinterpret_cast<keyword_iterator>(this+1);
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270 keyword_iterator keyword_end() const {
271 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000272 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000273 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000274 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000275 return keyword_begin()[i];
276 }
Mike Stump1eb44332009-09-09 15:08:12 +0000277 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000278 keyword_iterator ArgTys, unsigned NumArgs) {
279 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000280 for (unsigned i = 0; i != NumArgs; ++i)
281 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000282 }
283 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000284 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000285 }
286};
Chris Lattner85994262007-10-05 20:15:24 +0000287} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000288
289unsigned Selector::getNumArgs() const {
290 unsigned IIF = getIdentifierInfoFlag();
291 if (IIF == ZeroArg)
292 return 0;
293 if (IIF == OneArg)
294 return 1;
295 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
296 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000297 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000298}
299
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000300IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000301 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000302 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000303 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000304 }
305 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
306 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
307 return SI->getIdentifierInfoForSlot(argIndex);
308}
309
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000310std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000311 llvm::SmallString<256> Str;
312 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000313 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
314 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000315 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000316 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000317 }
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000319 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000320}
321
Chris Lattner077bf5e2008-11-24 03:33:13 +0000322std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000323 if (InfoPtr == 0)
324 return "<null selector>";
325
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000326 if (InfoPtr & ArgFlags) {
327 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek150ec292009-03-07 01:22:02 +0000329 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000330 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000331 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000332
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000333 if (!II)
334 return ":";
335
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000336 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000337 }
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000339 // We have a multiple keyword selector (no embedded flags).
340 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000341}
342
343
Chris Lattner5f7d2282009-03-04 05:35:38 +0000344namespace {
345 struct SelectorTableImpl {
346 llvm::FoldingSet<MultiKeywordSelector> Table;
347 llvm::BumpPtrAllocator Allocator;
348 };
349} // end anonymous namespace.
350
351static SelectorTableImpl &getSelectorTableImpl(void *P) {
352 return *static_cast<SelectorTableImpl*>(P);
353}
354
355
Chris Lattnerff384912007-10-07 02:00:24 +0000356Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
357 if (nKeys < 2)
358 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattner5f7d2282009-03-04 05:35:38 +0000360 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Steve Naroff29238a02007-10-05 18:42:47 +0000362 // Unique selector, to guarantee there is one per name.
363 llvm::FoldingSetNodeID ID;
364 MultiKeywordSelector::Profile(ID, IIV, nKeys);
365
366 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000367 if (MultiKeywordSelector *SI =
368 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000369 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Steve Naroff29238a02007-10-05 18:42:47 +0000371 // MultiKeywordSelector objects are not allocated with new because they have a
372 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000373 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
374 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000375 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner5f7d2282009-03-04 05:35:38 +0000376 llvm::alignof<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000377 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000378 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000379 return Selector(SI);
380}
381
Steve Naroff29238a02007-10-05 18:42:47 +0000382SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000383 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000384}
385
386SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000387 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000388}
389
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000390const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
391 switch (Operator) {
392 case OO_None:
393 case NUM_OVERLOADED_OPERATORS:
394 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000395
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000396#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
397 case OO_##Name: return Spelling;
398#include "clang/Basic/OperatorKinds.def"
399 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000400
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000401 return 0;
402}
403