blob: bd30c68da2b71734cbfebeaca9f8e686f0b31dad [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 Redl3c7f4132010-08-18 23:57:06 +000037 IsFromAST = false;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +000038 RevertedTokenID = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 FETokenInfo = 0;
Ted Kremenekea9c26b2009-01-20 23:28:34 +000040 Entry = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000041}
42
Reid Spencer5f016e22007-07-11 17:01:13 +000043//===----------------------------------------------------------------------===//
44// IdentifierTable Implementation
45//===----------------------------------------------------------------------===//
46
Ted Kremenek72b1b152009-01-15 18:47:46 +000047IdentifierInfoLookup::~IdentifierInfoLookup() {}
48
Douglas Gregor8c5a7602009-04-25 23:30:02 +000049ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
50
Ted Kremenek72b1b152009-01-15 18:47:46 +000051IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
52 IdentifierInfoLookup* externalLookup)
53 : HashTable(8192), // Start with space for 8K identifiers.
54 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000055
56 // Populate the identifier table with info about keywords for the current
57 // language.
58 AddKeywords(LangOpts);
59}
60
61//===----------------------------------------------------------------------===//
62// Language Keyword Implementation
63//===----------------------------------------------------------------------===//
64
Eli Friedmaneb32fde2009-04-28 03:13:54 +000065// Constants for TokenKinds.def
66namespace {
67 enum {
68 KEYALL = 1,
69 KEYC99 = 2,
70 KEYCXX = 4,
71 KEYCXX0X = 8,
72 KEYGNU = 16,
Nate Begeman72b60e32009-06-25 23:25:15 +000073 KEYMS = 32,
John Thompson82287d12010-02-05 00:12:22 +000074 BOOLSUPPORT = 64,
Chris Lattner34d7c4d2010-05-21 20:22:37 +000075 KEYALTIVEC = 128,
Douglas Gregor3a43d8d2010-10-13 20:00:38 +000076 KEYNOCXX = 256,
Dawn Perchik52fc3142010-09-03 01:29:35 +000077 KEYBORLAND = 512
Eli Friedmaneb32fde2009-04-28 03:13:54 +000078 };
79}
80
Reid Spencer5f016e22007-07-11 17:01:13 +000081/// AddKeyword - This method is used to associate a token ID with specific
82/// identifiers because they are language keywords. This causes the lexer to
83/// automatically map matching identifiers to specialized token codes.
84///
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000085/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattnerd4b80f12007-07-16 04:18:29 +000086/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000087/// in the specified language, and set to 0 if disabled in the
Chris Lattnerd4b80f12007-07-16 04:18:29 +000088/// specified language.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +000089static void AddKeyword(llvm::StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +000090 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +000091 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +000092 unsigned AddResult = 0;
93 if (Flags & KEYALL) AddResult = 2;
94 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
95 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
96 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000097 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedmaneb32fde2009-04-28 03:13:54 +000098 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +000099 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000100 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000101 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000102 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000103
104 // Don't add this keyword if disabled in this language.
105 if (AddResult == 0) return;
106
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000107 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000108 Info.setIsExtensionToken(AddResult == 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000109}
110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
112/// representations.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000113static void AddCXXOperatorKeyword(llvm::StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 tok::TokenKind TokenCode,
115 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000116 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000117 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000118}
119
Mike Stump1eb44332009-09-09 15:08:12 +0000120/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000121/// "property".
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000122static void AddObjCKeyword(llvm::StringRef Name,
123 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000125 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000126}
127
128/// AddKeywords - Add all keywords to the symbol table.
129///
130void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 // Add keywords and tokens for the current language.
132#define KEYWORD(NAME, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000133 AddKeyword(llvm::StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000134 FLAGS, LangOpts, *this);
135#define ALIAS(NAME, TOK, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000136 AddKeyword(llvm::StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000137 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000138#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
139 if (LangOpts.CXXOperatorNames) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000140 AddCXXOperatorKeyword(llvm::StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000141#define OBJC1_AT_KEYWORD(NAME) \
142 if (LangOpts.ObjC1) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000143 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144#define OBJC2_AT_KEYWORD(NAME) \
145 if (LangOpts.ObjC2) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000146 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147#include "clang/Basic/TokenKinds.def"
148}
149
Chris Lattner387b98d2007-10-07 07:52:34 +0000150tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
151 // We use a perfect hash function here involving the length of the keyword,
152 // the first and third character. For preprocessor ID's there are no
153 // collisions (if there were, the switch below would complain about duplicate
154 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner387b98d2007-10-07 07:52:34 +0000156#define HASH(LEN, FIRST, THIRD) \
157 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
158#define CASE(LEN, FIRST, THIRD, NAME) \
159 case HASH(LEN, FIRST, THIRD): \
160 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Chris Lattner387b98d2007-10-07 07:52:34 +0000162 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000163 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000164 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000165 switch (HASH(Len, Name[0], Name[2])) {
166 default: return tok::pp_not_keyword;
167 CASE( 2, 'i', '\0', if);
168 CASE( 4, 'e', 'i', elif);
169 CASE( 4, 'e', 's', else);
170 CASE( 4, 'l', 'n', line);
171 CASE( 4, 's', 'c', sccs);
172 CASE( 5, 'e', 'd', endif);
173 CASE( 5, 'e', 'r', error);
174 CASE( 5, 'i', 'e', ident);
175 CASE( 5, 'i', 'd', ifdef);
176 CASE( 5, 'u', 'd', undef);
177
178 CASE( 6, 'a', 's', assert);
179 CASE( 6, 'd', 'f', define);
180 CASE( 6, 'i', 'n', ifndef);
181 CASE( 6, 'i', 'p', import);
182 CASE( 6, 'p', 'a', pragma);
183
184 CASE( 7, 'd', 'f', defined);
185 CASE( 7, 'i', 'c', include);
186 CASE( 7, 'w', 'r', warning);
187
188 CASE( 8, 'u', 'a', unassert);
189 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000191 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000192#undef CASE
193#undef HASH
194 }
195}
Reid Spencer5f016e22007-07-11 17:01:13 +0000196
197//===----------------------------------------------------------------------===//
198// Stats Implementation
199//===----------------------------------------------------------------------===//
200
201/// PrintStats - Print statistics about how well the identifier table is doing
202/// at hashing identifiers.
203void IdentifierTable::PrintStats() const {
204 unsigned NumBuckets = HashTable.getNumBuckets();
205 unsigned NumIdentifiers = HashTable.getNumItems();
206 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
207 unsigned AverageIdentifierSize = 0;
208 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000211 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
213 unsigned IdLen = I->getKeyLength();
214 AverageIdentifierSize += IdLen;
215 if (MaxIdentifierLength < IdLen)
216 MaxIdentifierLength = IdLen;
217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 fprintf(stderr, "\n*** Identifier Table Stats:\n");
220 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
221 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
222 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
223 NumIdentifiers/(double)NumBuckets);
224 fprintf(stderr, "Ave identifier length: %f\n",
225 (AverageIdentifierSize/(double)NumIdentifiers));
226 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 // Compute statistics about the memory allocated for identifiers.
229 HashTable.getAllocator().PrintStats();
230}
Steve Naroff68d331a2007-09-27 14:38:14 +0000231
Steve Naroff29238a02007-10-05 18:42:47 +0000232//===----------------------------------------------------------------------===//
233// SelectorTable Implementation
234//===----------------------------------------------------------------------===//
235
Chris Lattner85994262007-10-05 20:15:24 +0000236unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
237 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
238}
239
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000240namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000241/// MultiKeywordSelector - One of these variable length records is kept for each
242/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000243/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000244/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000245class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000246 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000247 MultiKeywordSelector(unsigned nKeys) {
248 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250public:
Steve Naroff29238a02007-10-05 18:42:47 +0000251 // Constructor for keyword selectors.
252 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
253 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000254 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Steve Naroff29238a02007-10-05 18:42:47 +0000256 // Fill in the trailing keyword array.
257 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
258 for (unsigned i = 0; i != nKeys; ++i)
259 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000260 }
261
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000262 // getName - Derive the full selector name and return it.
263 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000265 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Steve Naroff29238a02007-10-05 18:42:47 +0000267 typedef IdentifierInfo *const *keyword_iterator;
268 keyword_iterator keyword_begin() const {
269 return reinterpret_cast<keyword_iterator>(this+1);
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271 keyword_iterator keyword_end() const {
272 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000273 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000274 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000275 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000276 return keyword_begin()[i];
277 }
Mike Stump1eb44332009-09-09 15:08:12 +0000278 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000279 keyword_iterator ArgTys, unsigned NumArgs) {
280 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000281 for (unsigned i = 0; i != NumArgs; ++i)
282 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000283 }
284 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000285 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000286 }
287};
Chris Lattner85994262007-10-05 20:15:24 +0000288} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000289
290unsigned Selector::getNumArgs() const {
291 unsigned IIF = getIdentifierInfoFlag();
292 if (IIF == ZeroArg)
293 return 0;
294 if (IIF == OneArg)
295 return 1;
296 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
297 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000298 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000299}
300
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000301IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000302 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000303 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000304 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000305 }
306 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
307 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
308 return SI->getIdentifierInfoForSlot(argIndex);
309}
310
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000311std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000312 llvm::SmallString<256> Str;
313 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000314 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
315 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000316 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000317 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000318 }
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000320 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000321}
322
Chris Lattner077bf5e2008-11-24 03:33:13 +0000323std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000324 if (InfoPtr == 0)
325 return "<null selector>";
326
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000327 if (InfoPtr & ArgFlags) {
328 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Ted Kremenek150ec292009-03-07 01:22:02 +0000330 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000331 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000332 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000333
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000334 if (!II)
335 return ":";
336
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000337 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000340 // We have a multiple keyword selector (no embedded flags).
341 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000342}
343
344
Chris Lattner5f7d2282009-03-04 05:35:38 +0000345namespace {
346 struct SelectorTableImpl {
347 llvm::FoldingSet<MultiKeywordSelector> Table;
348 llvm::BumpPtrAllocator Allocator;
349 };
350} // end anonymous namespace.
351
352static SelectorTableImpl &getSelectorTableImpl(void *P) {
353 return *static_cast<SelectorTableImpl*>(P);
354}
355
356
Chris Lattnerff384912007-10-07 02:00:24 +0000357Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
358 if (nKeys < 2)
359 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner5f7d2282009-03-04 05:35:38 +0000361 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Steve Naroff29238a02007-10-05 18:42:47 +0000363 // Unique selector, to guarantee there is one per name.
364 llvm::FoldingSetNodeID ID;
365 MultiKeywordSelector::Profile(ID, IIV, nKeys);
366
367 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000368 if (MultiKeywordSelector *SI =
369 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000370 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Steve Naroff29238a02007-10-05 18:42:47 +0000372 // MultiKeywordSelector objects are not allocated with new because they have a
373 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000374 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
375 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000376 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner5f7d2282009-03-04 05:35:38 +0000377 llvm::alignof<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000378 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000379 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000380 return Selector(SI);
381}
382
Steve Naroff29238a02007-10-05 18:42:47 +0000383SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000384 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000385}
386
387SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000388 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000389}
390
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000391const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
392 switch (Operator) {
393 case OO_None:
394 case NUM_OVERLOADED_OPERATORS:
395 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000396
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000397#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
398 case OO_##Name: return Spelling;
399#include "clang/Basic/OperatorKinds.def"
400 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000401
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000402 return 0;
403}
404