blob: 4ea6cedeb56e93661777f506d48d51c77f605f32 [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"
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +000019#include "llvm/ADT/StringRef.h"
Daniel Dunbar1c0761d2009-10-17 18:13:02 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000021#include <cstdio>
Ted Kremenekf25f4a32007-10-23 22:18:37 +000022
Chris Lattner22eb9722006-06-18 05:43:12 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000026// IdentifierInfo Implementation
Chris Lattner22eb9722006-06-18 05:43:12 +000027//===----------------------------------------------------------------------===//
28
Ted Kremenek52f73ca2009-01-20 23:28:34 +000029IdentifierInfo::IdentifierInfo() {
Chris Lattner3bc804e2006-10-28 23:46:24 +000030 TokenID = tok::identifier;
Douglas Gregor2ad7ee92008-11-06 16:32:23 +000031 ObjCOrBuiltinID = 0;
Chris Lattnera441ca62007-10-07 07:09:52 +000032 HasMacro = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000033 IsExtension = false;
34 IsPoisoned = false;
Chris Lattner5b9f4892006-11-21 17:23:33 +000035 IsCPPOperatorKeyword = false;
Chris Lattnerad89ec02009-01-21 07:43:11 +000036 NeedsHandleIdentifier = false;
Sebastian Redld44cd6a2010-08-18 23:57:06 +000037 IsFromAST = false;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +000038 RevertedTokenID = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000039 FETokenInfo = 0;
Ted Kremenek52f73ca2009-01-20 23:28:34 +000040 Entry = 0;
Chris Lattner3bc804e2006-10-28 23:46:24 +000041}
42
Chris Lattner91cbf112006-07-03 04:28:52 +000043//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +000044// IdentifierTable Implementation
45//===----------------------------------------------------------------------===//
46
Douglas Gregor57756ea2010-10-14 22:11:03 +000047IdentifierIterator::~IdentifierIterator() { }
48
Ted Kremeneka705b042009-01-15 18:47:46 +000049IdentifierInfoLookup::~IdentifierInfoLookup() {}
50
Douglas Gregor57756ea2010-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 Gregor99734e72009-04-25 23:30:02 +000065ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
66
Ted Kremeneka705b042009-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) {
Chris Lattnerda933aa2006-10-29 23:49:15 +000071
Chris Lattnerf2e3ac32006-10-27 03:59:10 +000072 // Populate the identifier table with info about keywords for the current
73 // language.
Chris Lattner25e0d542006-10-18 06:07:05 +000074 AddKeywords(LangOpts);
Chris Lattner91cbf112006-07-03 04:28:52 +000075}
Chris Lattner22eb9722006-06-18 05:43:12 +000076
Chris Lattner25e0d542006-10-18 06:07:05 +000077//===----------------------------------------------------------------------===//
78// Language Keyword Implementation
79//===----------------------------------------------------------------------===//
80
Eli Friedman2b680b42009-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 Begeman8b68d6f2009-06-25 23:25:15 +000089 KEYMS = 32,
John Thompson22334602010-02-05 00:12:22 +000090 BOOLSUPPORT = 64,
Chris Lattnerc425fe42010-05-21 20:22:37 +000091 KEYALTIVEC = 128,
Douglas Gregorf10c97f2010-10-13 20:00:38 +000092 KEYNOCXX = 256,
Dawn Perchik335e16b2010-09-03 01:29:35 +000093 KEYBORLAND = 512
Eli Friedman2b680b42009-04-28 03:13:54 +000094 };
95}
96
Chris Lattner25e0d542006-10-18 06:07:05 +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 Carruthe03aa552010-04-17 20:17:31 +0000101/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattner539007a2007-07-16 04:18:29 +0000102/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carruthe03aa552010-04-17 20:17:31 +0000103/// in the specified language, and set to 0 if disabled in the
Chris Lattner539007a2007-07-16 04:18:29 +0000104/// specified language.
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000105static void AddKeyword(llvm::StringRef Keyword,
Eli Friedman2b680b42009-04-28 03:13:54 +0000106 tok::TokenKind TokenCode, unsigned Flags,
Chris Lattner25e0d542006-10-18 06:07:05 +0000107 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedman2b680b42009-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 Carruthe03aa552010-04-17 20:17:31 +0000113 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedman2b680b42009-04-28 03:13:54 +0000114 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000115 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnerc61089a2009-06-30 01:26:17 +0000116 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson22334602010-02-05 00:12:22 +0000117 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Douglas Gregorf10c97f2010-10-13 20:00:38 +0000118 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Eli Friedman2b680b42009-04-28 03:13:54 +0000119
120 // Don't add this keyword if disabled in this language.
121 if (AddResult == 0) return;
122
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000123 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Eli Friedman2b680b42009-04-28 03:13:54 +0000124 Info.setIsExtensionToken(AddResult == 1);
Chris Lattner25e0d542006-10-18 06:07:05 +0000125}
126
Chris Lattner5b9f4892006-11-21 17:23:33 +0000127/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
128/// representations.
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000129static void AddCXXOperatorKeyword(llvm::StringRef Keyword,
Chris Lattner5b9f4892006-11-21 17:23:33 +0000130 tok::TokenKind TokenCode,
131 IdentifierTable &Table) {
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000132 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekf25f4a32007-10-23 22:18:37 +0000133 Info.setIsCPlusPlusOperatorKeyword();
Chris Lattner5b9f4892006-11-21 17:23:33 +0000134}
135
Mike Stump11289f42009-09-09 15:08:12 +0000136/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Chris Lattner25e0d542006-10-18 06:07:05 +0000137/// "property".
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000138static void AddObjCKeyword(llvm::StringRef Name,
139 tok::ObjCKeywordKind ObjCID,
Chris Lattner25e0d542006-10-18 06:07:05 +0000140 IdentifierTable &Table) {
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000141 Table.get(Name).setObjCKeywordID(ObjCID);
Chris Lattner25e0d542006-10-18 06:07:05 +0000142}
143
144/// AddKeywords - Add all keywords to the symbol table.
145///
146void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Chris Lattner25e0d542006-10-18 06:07:05 +0000147 // Add keywords and tokens for the current language.
148#define KEYWORD(NAME, FLAGS) \
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000149 AddKeyword(llvm::StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000150 FLAGS, LangOpts, *this);
151#define ALIAS(NAME, TOK, FLAGS) \
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000152 AddKeyword(llvm::StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000153 FLAGS, LangOpts, *this);
Chris Lattner5b9f4892006-11-21 17:23:33 +0000154#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
Chris Lattner3e7592e2006-12-04 07:48:37 +0000155 if (LangOpts.CXXOperatorNames) \
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000156 AddCXXOperatorKeyword(llvm::StringRef(#NAME), tok::ALIAS, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000157#define OBJC1_AT_KEYWORD(NAME) \
158 if (LangOpts.ObjC1) \
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000159 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000160#define OBJC2_AT_KEYWORD(NAME) \
161 if (LangOpts.ObjC2) \
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000162 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000163#include "clang/Basic/TokenKinds.def"
164}
165
Chris Lattnerff067ce2007-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 Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattnerff067ce2007-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 Stump11289f42009-09-09 15:08:12 +0000177
Chris Lattnerff067ce2007-10-07 07:52:34 +0000178 unsigned Len = getLength();
Chris Lattnerd2b8ce42007-10-10 20:59:57 +0000179 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000180 const char *Name = getNameStart();
Chris Lattnerff067ce2007-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 Stump11289f42009-09-09 15:08:12 +0000206
Chris Lattner14a7f392009-04-08 18:24:34 +0000207 CASE(16, '_', 'i', __include_macros);
Chris Lattnerff067ce2007-10-07 07:52:34 +0000208#undef CASE
209#undef HASH
210 }
211}
Chris Lattner25e0d542006-10-18 06:07:05 +0000212
213//===----------------------------------------------------------------------===//
214// Stats Implementation
215//===----------------------------------------------------------------------===//
216
Chris Lattner22eb9722006-06-18 05:43:12 +0000217/// PrintStats - Print statistics about how well the identifier table is doing
218/// at hashing identifiers.
219void IdentifierTable::PrintStats() const {
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000220 unsigned NumBuckets = HashTable.getNumBuckets();
221 unsigned NumIdentifiers = HashTable.getNumItems();
222 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
Chris Lattner22eb9722006-06-18 05:43:12 +0000223 unsigned AverageIdentifierSize = 0;
224 unsigned MaxIdentifierLength = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000225
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000226 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000227 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Chris Lattnerb055f2d2007-02-11 08:19:57 +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 Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattner23b7eb62007-06-15 23:05:46 +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 Stump11289f42009-09-09 15:08:12 +0000243
Chris Lattner22eb9722006-06-18 05:43:12 +0000244 // Compute statistics about the memory allocated for identifiers.
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000245 HashTable.getAllocator().PrintStats();
Chris Lattner22eb9722006-06-18 05:43:12 +0000246}
Steve Narofff73590d2007-09-27 14:38:14 +0000247
Steve Naroffe61bfa82007-10-05 18:42:47 +0000248//===----------------------------------------------------------------------===//
249// SelectorTable Implementation
250//===----------------------------------------------------------------------===//
251
Chris Lattnerdadc7622007-10-05 20:15:24 +0000252unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
253 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
254}
255
Douglas Gregor77324f32008-11-17 14:58:09 +0000256namespace clang {
Steve Naroffe61bfa82007-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 Stump11289f42009-09-09 15:08:12 +0000259/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroffe61bfa82007-10-05 18:42:47 +0000260/// this class is provided strictly through Selector.
Mike Stump11289f42009-09-09 15:08:12 +0000261class MultiKeywordSelector
Douglas Gregor77324f32008-11-17 14:58:09 +0000262 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor77324f32008-11-17 14:58:09 +0000263 MultiKeywordSelector(unsigned nKeys) {
264 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
265 }
Mike Stump11289f42009-09-09 15:08:12 +0000266public:
Steve Naroffe61bfa82007-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 Gregor77324f32008-11-17 14:58:09 +0000270 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump11289f42009-09-09 15:08:12 +0000271
Steve Naroffe61bfa82007-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 Stump11289f42009-09-09 15:08:12 +0000276 }
277
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000278 // getName - Derive the full selector name and return it.
279 std::string getName() const;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Douglas Gregor77324f32008-11-17 14:58:09 +0000281 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump11289f42009-09-09 15:08:12 +0000282
Steve Naroffe61bfa82007-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 Stump11289f42009-09-09 15:08:12 +0000287 keyword_iterator keyword_end() const {
288 return keyword_begin()+getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000289 }
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000290 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor77324f32008-11-17 14:58:09 +0000291 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroffe61bfa82007-10-05 18:42:47 +0000292 return keyword_begin()[i];
293 }
Mike Stump11289f42009-09-09 15:08:12 +0000294 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroffe61bfa82007-10-05 18:42:47 +0000295 keyword_iterator ArgTys, unsigned NumArgs) {
296 ID.AddInteger(NumArgs);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000297 for (unsigned i = 0; i != NumArgs; ++i)
298 ID.AddPointer(ArgTys[i]);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000299 }
300 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000301 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000302 }
303};
Chris Lattnerdadc7622007-10-05 20:15:24 +0000304} // end namespace clang.
Steve Naroffe61bfa82007-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 Stump11289f42009-09-09 15:08:12 +0000314 return SI->getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000315}
316
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000317IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor70091b82009-04-26 22:20:50 +0000318 if (getIdentifierInfoFlag()) {
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000319 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor70091b82009-04-26 22:20:50 +0000320 return getAsIdentifierInfo();
Steve Naroffe61bfa82007-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 Lattnerf7f34d02007-10-07 01:33:16 +0000327std::string MultiKeywordSelector::getName() const {
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000328 llvm::SmallString<256> Str;
329 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000330 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
331 if (*I)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000332 OS << (*I)->getName();
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000333 OS << ':';
Steve Naroffe61bfa82007-10-05 18:42:47 +0000334 }
Mike Stump11289f42009-09-09 15:08:12 +0000335
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000336 return OS.str();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000337}
338
Chris Lattnere4b95692008-11-24 03:33:13 +0000339std::string Selector::getAsString() const {
Douglas Gregor70091b82009-04-26 22:20:50 +0000340 if (InfoPtr == 0)
341 return "<null selector>";
342
Ted Kremenek5f080b42009-03-06 23:36:28 +0000343 if (InfoPtr & ArgFlags) {
344 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000345
Ted Kremenek0666a6c2009-03-07 01:22:02 +0000346 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenek5f080b42009-03-06 23:36:28 +0000347 if (getNumArgs() == 0)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000348 return II->getName();
Ted Kremenek5f080b42009-03-06 23:36:28 +0000349
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000350 if (!II)
351 return ":";
352
Daniel Dunbar07d07852009-10-18 21:17:35 +0000353 return II->getName().str() + ":";
Steve Naroffe61bfa82007-10-05 18:42:47 +0000354 }
Mike Stump11289f42009-09-09 15:08:12 +0000355
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000356 // We have a multiple keyword selector (no embedded flags).
357 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000358}
359
360
Chris Lattner1a849942009-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 Lattner5700fab2007-10-07 02:00:24 +0000373Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
374 if (nKeys < 2)
375 return Selector(IIV[0], nKeys);
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattner1a849942009-03-04 05:35:38 +0000377 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump11289f42009-09-09 15:08:12 +0000378
Steve Naroffe61bfa82007-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 Lattner1a849942009-03-04 05:35:38 +0000384 if (MultiKeywordSelector *SI =
385 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe61bfa82007-10-05 18:42:47 +0000386 return Selector(SI);
Mike Stump11289f42009-09-09 15:08:12 +0000387
Steve Naroffe61bfa82007-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 Lattner1a849942009-03-04 05:35:38 +0000390 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
391 MultiKeywordSelector *SI =
Mike Stump11289f42009-09-09 15:08:12 +0000392 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner1a849942009-03-04 05:35:38 +0000393 llvm::alignof<MultiKeywordSelector>());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000394 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner1a849942009-03-04 05:35:38 +0000395 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000396 return Selector(SI);
397}
398
Steve Naroffe61bfa82007-10-05 18:42:47 +0000399SelectorTable::SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000400 Impl = new SelectorTableImpl();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000401}
402
403SelectorTable::~SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000404 delete &getSelectorTableImpl(Impl);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000405}
406
Douglas Gregor71395fa2009-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 Rajaratnam00682a42010-03-12 11:27:37 +0000412
Douglas Gregor71395fa2009-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 Rajaratnam00682a42010-03-12 11:27:37 +0000417
Douglas Gregor71395fa2009-11-04 00:56:37 +0000418 return 0;
419}
420