blob: 4ea6cedeb56e93661777f506d48d51c77f605f32 [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
Douglas Gregor95f42922010-10-14 22:11:03 +000047IdentifierIterator::~IdentifierIterator() { }
48
Ted Kremenek72b1b152009-01-15 18:47:46 +000049IdentifierInfoLookup::~IdentifierInfoLookup() {}
50
Douglas Gregor95f42922010-10-14 22:11:03 +000051namespace {
52 /// \brief A simple identifier lookup iterator that represents an
53 /// empty sequence of identifiers.
54 class EmptyLookupIterator : public IdentifierIterator
55 {
56 public:
57 virtual llvm::StringRef Next() { return llvm::StringRef(); }
58 };
59}
60
61IdentifierIterator *IdentifierInfoLookup::getIdentifiers() const {
62 return new EmptyLookupIterator();
63}
64
Douglas Gregor8c5a7602009-04-25 23:30:02 +000065ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
66
Ted Kremenek72b1b152009-01-15 18:47:46 +000067IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
68 IdentifierInfoLookup* externalLookup)
69 : HashTable(8192), // Start with space for 8K identifiers.
70 ExternalLookup(externalLookup) {
Reid Spencer5f016e22007-07-11 17:01:13 +000071
72 // Populate the identifier table with info about keywords for the current
73 // language.
74 AddKeywords(LangOpts);
75}
76
77//===----------------------------------------------------------------------===//
78// Language Keyword Implementation
79//===----------------------------------------------------------------------===//
80
Eli Friedmaneb32fde2009-04-28 03:13:54 +000081// Constants for TokenKinds.def
82namespace {
83 enum {
84 KEYALL = 1,
85 KEYC99 = 2,
86 KEYCXX = 4,
87 KEYCXX0X = 8,
88 KEYGNU = 16,
Nate Begeman72b60e32009-06-25 23:25:15 +000089 KEYMS = 32,
John Thompson82287d12010-02-05 00:12:22 +000090 BOOLSUPPORT = 64,
Chris Lattner34d7c4d2010-05-21 20:22:37 +000091 KEYALTIVEC = 128,
Douglas Gregor3a43d8d2010-10-13 20:00:38 +000092 KEYNOCXX = 256,
Dawn Perchik52fc3142010-09-03 01:29:35 +000093 KEYBORLAND = 512
Eli Friedmaneb32fde2009-04-28 03:13:54 +000094 };
95}
96
Reid Spencer5f016e22007-07-11 17:01:13 +000097/// AddKeyword - This method is used to associate a token ID with specific
98/// identifiers because they are language keywords. This causes the lexer to
99/// automatically map matching identifiers to specialized token codes.
100///
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000101/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000102/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000103/// in the specified language, and set to 0 if disabled in the
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000104/// specified language.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000105static void AddKeyword(llvm::StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000106 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000108 unsigned AddResult = 0;
109 if (Flags & KEYALL) AddResult = 2;
110 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
111 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
112 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000113 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000114 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000115 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000116 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000117 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000118 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000119
120 // Don't add this keyword if disabled in this language.
121 if (AddResult == 0) return;
122
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000123 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000124 Info.setIsExtensionToken(AddResult == 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000125}
126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
128/// representations.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000129static void AddCXXOperatorKeyword(llvm::StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 tok::TokenKind TokenCode,
131 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000132 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000133 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000134}
135
Mike Stump1eb44332009-09-09 15:08:12 +0000136/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000137/// "property".
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000138static void AddObjCKeyword(llvm::StringRef Name,
139 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000141 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000142}
143
144/// AddKeywords - Add all keywords to the symbol table.
145///
146void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 // Add keywords and tokens for the current language.
148#define KEYWORD(NAME, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000149 AddKeyword(llvm::StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000150 FLAGS, LangOpts, *this);
151#define ALIAS(NAME, TOK, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000152 AddKeyword(llvm::StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000153 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000154#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
155 if (LangOpts.CXXOperatorNames) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000156 AddCXXOperatorKeyword(llvm::StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157#define OBJC1_AT_KEYWORD(NAME) \
158 if (LangOpts.ObjC1) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000159 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160#define OBJC2_AT_KEYWORD(NAME) \
161 if (LangOpts.ObjC2) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000162 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163#include "clang/Basic/TokenKinds.def"
164}
165
Chris Lattner387b98d2007-10-07 07:52:34 +0000166tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
167 // We use a perfect hash function here involving the length of the keyword,
168 // the first and third character. For preprocessor ID's there are no
169 // collisions (if there were, the switch below would complain about duplicate
170 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattner387b98d2007-10-07 07:52:34 +0000172#define HASH(LEN, FIRST, THIRD) \
173 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
174#define CASE(LEN, FIRST, THIRD, NAME) \
175 case HASH(LEN, FIRST, THIRD): \
176 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Chris Lattner387b98d2007-10-07 07:52:34 +0000178 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000179 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000180 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000181 switch (HASH(Len, Name[0], Name[2])) {
182 default: return tok::pp_not_keyword;
183 CASE( 2, 'i', '\0', if);
184 CASE( 4, 'e', 'i', elif);
185 CASE( 4, 'e', 's', else);
186 CASE( 4, 'l', 'n', line);
187 CASE( 4, 's', 'c', sccs);
188 CASE( 5, 'e', 'd', endif);
189 CASE( 5, 'e', 'r', error);
190 CASE( 5, 'i', 'e', ident);
191 CASE( 5, 'i', 'd', ifdef);
192 CASE( 5, 'u', 'd', undef);
193
194 CASE( 6, 'a', 's', assert);
195 CASE( 6, 'd', 'f', define);
196 CASE( 6, 'i', 'n', ifndef);
197 CASE( 6, 'i', 'p', import);
198 CASE( 6, 'p', 'a', pragma);
199
200 CASE( 7, 'd', 'f', defined);
201 CASE( 7, 'i', 'c', include);
202 CASE( 7, 'w', 'r', warning);
203
204 CASE( 8, 'u', 'a', unassert);
205 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000207 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000208#undef CASE
209#undef HASH
210 }
211}
Reid Spencer5f016e22007-07-11 17:01:13 +0000212
213//===----------------------------------------------------------------------===//
214// Stats Implementation
215//===----------------------------------------------------------------------===//
216
217/// PrintStats - Print statistics about how well the identifier table is doing
218/// at hashing identifiers.
219void IdentifierTable::PrintStats() const {
220 unsigned NumBuckets = HashTable.getNumBuckets();
221 unsigned NumIdentifiers = HashTable.getNumItems();
222 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
223 unsigned AverageIdentifierSize = 0;
224 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000227 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
229 unsigned IdLen = I->getKeyLength();
230 AverageIdentifierSize += IdLen;
231 if (MaxIdentifierLength < IdLen)
232 MaxIdentifierLength = IdLen;
233 }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 fprintf(stderr, "\n*** Identifier Table Stats:\n");
236 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
237 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
238 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
239 NumIdentifiers/(double)NumBuckets);
240 fprintf(stderr, "Ave identifier length: %f\n",
241 (AverageIdentifierSize/(double)NumIdentifiers));
242 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 // Compute statistics about the memory allocated for identifiers.
245 HashTable.getAllocator().PrintStats();
246}
Steve Naroff68d331a2007-09-27 14:38:14 +0000247
Steve Naroff29238a02007-10-05 18:42:47 +0000248//===----------------------------------------------------------------------===//
249// SelectorTable Implementation
250//===----------------------------------------------------------------------===//
251
Chris Lattner85994262007-10-05 20:15:24 +0000252unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
253 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
254}
255
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000256namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000257/// MultiKeywordSelector - One of these variable length records is kept for each
258/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000259/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000260/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000261class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000262 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000263 MultiKeywordSelector(unsigned nKeys) {
264 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266public:
Steve Naroff29238a02007-10-05 18:42:47 +0000267 // Constructor for keyword selectors.
268 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
269 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000270 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Steve Naroff29238a02007-10-05 18:42:47 +0000272 // Fill in the trailing keyword array.
273 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
274 for (unsigned i = 0; i != nKeys; ++i)
275 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000276 }
277
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000278 // getName - Derive the full selector name and return it.
279 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000281 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Steve Naroff29238a02007-10-05 18:42:47 +0000283 typedef IdentifierInfo *const *keyword_iterator;
284 keyword_iterator keyword_begin() const {
285 return reinterpret_cast<keyword_iterator>(this+1);
286 }
Mike Stump1eb44332009-09-09 15:08:12 +0000287 keyword_iterator keyword_end() const {
288 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000289 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000290 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000291 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000292 return keyword_begin()[i];
293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000295 keyword_iterator ArgTys, unsigned NumArgs) {
296 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000297 for (unsigned i = 0; i != NumArgs; ++i)
298 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000299 }
300 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000301 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000302 }
303};
Chris Lattner85994262007-10-05 20:15:24 +0000304} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000305
306unsigned Selector::getNumArgs() const {
307 unsigned IIF = getIdentifierInfoFlag();
308 if (IIF == ZeroArg)
309 return 0;
310 if (IIF == OneArg)
311 return 1;
312 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
313 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000314 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000315}
316
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000317IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000318 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000319 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000320 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000321 }
322 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
323 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
324 return SI->getIdentifierInfoForSlot(argIndex);
325}
326
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000327std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000328 llvm::SmallString<256> Str;
329 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000330 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
331 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000332 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000333 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000336 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000337}
338
Chris Lattner077bf5e2008-11-24 03:33:13 +0000339std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000340 if (InfoPtr == 0)
341 return "<null selector>";
342
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000343 if (InfoPtr & ArgFlags) {
344 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenek150ec292009-03-07 01:22:02 +0000346 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000347 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000348 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000349
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000350 if (!II)
351 return ":";
352
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000353 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000356 // We have a multiple keyword selector (no embedded flags).
357 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000358}
359
360
Chris Lattner5f7d2282009-03-04 05:35:38 +0000361namespace {
362 struct SelectorTableImpl {
363 llvm::FoldingSet<MultiKeywordSelector> Table;
364 llvm::BumpPtrAllocator Allocator;
365 };
366} // end anonymous namespace.
367
368static SelectorTableImpl &getSelectorTableImpl(void *P) {
369 return *static_cast<SelectorTableImpl*>(P);
370}
371
372
Chris Lattnerff384912007-10-07 02:00:24 +0000373Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
374 if (nKeys < 2)
375 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattner5f7d2282009-03-04 05:35:38 +0000377 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Steve Naroff29238a02007-10-05 18:42:47 +0000379 // Unique selector, to guarantee there is one per name.
380 llvm::FoldingSetNodeID ID;
381 MultiKeywordSelector::Profile(ID, IIV, nKeys);
382
383 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000384 if (MultiKeywordSelector *SI =
385 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000386 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Steve Naroff29238a02007-10-05 18:42:47 +0000388 // MultiKeywordSelector objects are not allocated with new because they have a
389 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000390 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
391 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000392 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner5f7d2282009-03-04 05:35:38 +0000393 llvm::alignof<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000394 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000395 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000396 return Selector(SI);
397}
398
Steve Naroff29238a02007-10-05 18:42:47 +0000399SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000400 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000401}
402
403SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000404 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000405}
406
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000407const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
408 switch (Operator) {
409 case OO_None:
410 case NUM_OVERLOADED_OPERATORS:
411 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000412
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000413#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
414 case OO_##Name: return Spelling;
415#include "clang/Basic/OperatorKinds.def"
416 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000417
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000418 return 0;
419}
420