blob: 48a5f49914b2a536b1bdd6eab550250c07c9e198 [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,
Peter Collingbournef315fa82011-02-14 01:42:53 +000093 KEYBORLAND = 512,
94 KEYOPENCL = 1024
Eli Friedmaneb32fde2009-04-28 03:13:54 +000095 };
96}
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// AddKeyword - This method is used to associate a token ID with specific
99/// identifiers because they are language keywords. This causes the lexer to
100/// automatically map matching identifiers to specialized token codes.
101///
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000102/// The C90/C99/CPP/CPP0x flags are set to 2 if the token should be
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000103/// enabled in the specified langauge, set to 1 if it is an extension
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000104/// in the specified language, and set to 0 if disabled in the
Chris Lattnerd4b80f12007-07-16 04:18:29 +0000105/// specified language.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000106static void AddKeyword(llvm::StringRef Keyword,
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000107 tok::TokenKind TokenCode, unsigned Flags,
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 const LangOptions &LangOpts, IdentifierTable &Table) {
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000109 unsigned AddResult = 0;
110 if (Flags & KEYALL) AddResult = 2;
111 else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
112 else if (LangOpts.CPlusPlus0x && (Flags & KEYCXX0X)) AddResult = 2;
113 else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000114 else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000115 else if (LangOpts.Microsoft && (Flags & KEYMS)) AddResult = 1;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000116 else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
Chris Lattnere4f21422009-06-30 01:26:17 +0000117 else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
John Thompson82287d12010-02-05 00:12:22 +0000118 else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
Peter Collingbournef315fa82011-02-14 01:42:53 +0000119 else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
Douglas Gregor3a43d8d2010-10-13 20:00:38 +0000120 else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000121
122 // Don't add this keyword if disabled in this language.
123 if (AddResult == 0) return;
124
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000125 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000126 Info.setIsExtensionToken(AddResult == 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000127}
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
130/// representations.
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000131static void AddCXXOperatorKeyword(llvm::StringRef Keyword,
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 tok::TokenKind TokenCode,
133 IdentifierTable &Table) {
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000134 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekc637e6b2007-10-23 22:18:37 +0000135 Info.setIsCPlusPlusOperatorKeyword();
Reid Spencer5f016e22007-07-11 17:01:13 +0000136}
137
Mike Stump1eb44332009-09-09 15:08:12 +0000138/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
Reid Spencer5f016e22007-07-11 17:01:13 +0000139/// "property".
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000140static void AddObjCKeyword(llvm::StringRef Name,
141 tok::ObjCKeywordKind ObjCID,
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 IdentifierTable &Table) {
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000143 Table.get(Name).setObjCKeywordID(ObjCID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
146/// AddKeywords - Add all keywords to the symbol table.
147///
148void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 // Add keywords and tokens for the current language.
150#define KEYWORD(NAME, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000151 AddKeyword(llvm::StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000152 FLAGS, LangOpts, *this);
153#define ALIAS(NAME, TOK, FLAGS) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000154 AddKeyword(llvm::StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedmaneb32fde2009-04-28 03:13:54 +0000155 FLAGS, LangOpts, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
157 if (LangOpts.CXXOperatorNames) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000158 AddCXXOperatorKeyword(llvm::StringRef(#NAME), tok::ALIAS, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159#define OBJC1_AT_KEYWORD(NAME) \
160 if (LangOpts.ObjC1) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000161 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000162#define OBJC2_AT_KEYWORD(NAME) \
163 if (LangOpts.ObjC2) \
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000164 AddObjCKeyword(llvm::StringRef(#NAME), tok::objc_##NAME, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165#include "clang/Basic/TokenKinds.def"
166}
167
Chris Lattner387b98d2007-10-07 07:52:34 +0000168tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
169 // We use a perfect hash function here involving the length of the keyword,
170 // the first and third character. For preprocessor ID's there are no
171 // collisions (if there were, the switch below would complain about duplicate
172 // case values). Note that this depends on 'if' being null terminated.
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Chris Lattner387b98d2007-10-07 07:52:34 +0000174#define HASH(LEN, FIRST, THIRD) \
175 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
176#define CASE(LEN, FIRST, THIRD, NAME) \
177 case HASH(LEN, FIRST, THIRD): \
178 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner387b98d2007-10-07 07:52:34 +0000180 unsigned Len = getLength();
Chris Lattnera31f0302007-10-10 20:59:57 +0000181 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000182 const char *Name = getNameStart();
Chris Lattner387b98d2007-10-07 07:52:34 +0000183 switch (HASH(Len, Name[0], Name[2])) {
184 default: return tok::pp_not_keyword;
185 CASE( 2, 'i', '\0', if);
186 CASE( 4, 'e', 'i', elif);
187 CASE( 4, 'e', 's', else);
188 CASE( 4, 'l', 'n', line);
189 CASE( 4, 's', 'c', sccs);
190 CASE( 5, 'e', 'd', endif);
191 CASE( 5, 'e', 'r', error);
192 CASE( 5, 'i', 'e', ident);
193 CASE( 5, 'i', 'd', ifdef);
194 CASE( 5, 'u', 'd', undef);
195
196 CASE( 6, 'a', 's', assert);
197 CASE( 6, 'd', 'f', define);
198 CASE( 6, 'i', 'n', ifndef);
199 CASE( 6, 'i', 'p', import);
200 CASE( 6, 'p', 'a', pragma);
201
202 CASE( 7, 'd', 'f', defined);
203 CASE( 7, 'i', 'c', include);
204 CASE( 7, 'w', 'r', warning);
205
206 CASE( 8, 'u', 'a', unassert);
207 CASE(12, 'i', 'c', include_next);
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattnerb8e240e2009-04-08 18:24:34 +0000209 CASE(16, '_', 'i', __include_macros);
Chris Lattner387b98d2007-10-07 07:52:34 +0000210#undef CASE
211#undef HASH
212 }
213}
Reid Spencer5f016e22007-07-11 17:01:13 +0000214
215//===----------------------------------------------------------------------===//
216// Stats Implementation
217//===----------------------------------------------------------------------===//
218
219/// PrintStats - Print statistics about how well the identifier table is doing
220/// at hashing identifiers.
221void IdentifierTable::PrintStats() const {
222 unsigned NumBuckets = HashTable.getNumBuckets();
223 unsigned NumIdentifiers = HashTable.getNumItems();
224 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
225 unsigned AverageIdentifierSize = 0;
226 unsigned MaxIdentifierLength = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenekea9c26b2009-01-20 23:28:34 +0000229 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
231 unsigned IdLen = I->getKeyLength();
232 AverageIdentifierSize += IdLen;
233 if (MaxIdentifierLength < IdLen)
234 MaxIdentifierLength = IdLen;
235 }
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 fprintf(stderr, "\n*** Identifier Table Stats:\n");
238 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
239 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
240 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
241 NumIdentifiers/(double)NumBuckets);
242 fprintf(stderr, "Ave identifier length: %f\n",
243 (AverageIdentifierSize/(double)NumIdentifiers));
244 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // Compute statistics about the memory allocated for identifiers.
247 HashTable.getAllocator().PrintStats();
248}
Steve Naroff68d331a2007-09-27 14:38:14 +0000249
Steve Naroff29238a02007-10-05 18:42:47 +0000250//===----------------------------------------------------------------------===//
251// SelectorTable Implementation
252//===----------------------------------------------------------------------===//
253
Chris Lattner85994262007-10-05 20:15:24 +0000254unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
255 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
256}
257
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000258namespace clang {
Steve Naroff29238a02007-10-05 18:42:47 +0000259/// MultiKeywordSelector - One of these variable length records is kept for each
260/// selector containing more than one keyword. We use a folding set
Mike Stump1eb44332009-09-09 15:08:12 +0000261/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroff29238a02007-10-05 18:42:47 +0000262/// this class is provided strictly through Selector.
Mike Stump1eb44332009-09-09 15:08:12 +0000263class MultiKeywordSelector
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000264 : public DeclarationNameExtra, public llvm::FoldingSetNode {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000265 MultiKeywordSelector(unsigned nKeys) {
266 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
267 }
Mike Stump1eb44332009-09-09 15:08:12 +0000268public:
Steve Naroff29238a02007-10-05 18:42:47 +0000269 // Constructor for keyword selectors.
270 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
271 assert((nKeys > 1) && "not a multi-keyword selector");
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000272 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Steve Naroff29238a02007-10-05 18:42:47 +0000274 // Fill in the trailing keyword array.
275 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
276 for (unsigned i = 0; i != nKeys; ++i)
277 KeyInfo[i] = IIV[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000278 }
279
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000280 // getName - Derive the full selector name and return it.
281 std::string getName() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000283 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Steve Naroff29238a02007-10-05 18:42:47 +0000285 typedef IdentifierInfo *const *keyword_iterator;
286 keyword_iterator keyword_begin() const {
287 return reinterpret_cast<keyword_iterator>(this+1);
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289 keyword_iterator keyword_end() const {
290 return keyword_begin()+getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000291 }
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000292 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000293 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroff29238a02007-10-05 18:42:47 +0000294 return keyword_begin()[i];
295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296 static void Profile(llvm::FoldingSetNodeID &ID,
Steve Naroff29238a02007-10-05 18:42:47 +0000297 keyword_iterator ArgTys, unsigned NumArgs) {
298 ID.AddInteger(NumArgs);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000299 for (unsigned i = 0; i != NumArgs; ++i)
300 ID.AddPointer(ArgTys[i]);
Steve Naroff29238a02007-10-05 18:42:47 +0000301 }
302 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000303 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroff29238a02007-10-05 18:42:47 +0000304 }
305};
Chris Lattner85994262007-10-05 20:15:24 +0000306} // end namespace clang.
Steve Naroff29238a02007-10-05 18:42:47 +0000307
308unsigned Selector::getNumArgs() const {
309 unsigned IIF = getIdentifierInfoFlag();
310 if (IIF == ZeroArg)
311 return 0;
312 if (IIF == OneArg)
313 return 1;
314 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
315 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000316 return SI->getNumArgs();
Steve Naroff29238a02007-10-05 18:42:47 +0000317}
318
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000319IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000320 if (getIdentifierInfoFlag()) {
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000321 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor405bad02009-04-26 22:20:50 +0000322 return getAsIdentifierInfo();
Steve Naroff29238a02007-10-05 18:42:47 +0000323 }
324 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
325 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
326 return SI->getIdentifierInfoForSlot(argIndex);
327}
328
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000329std::string MultiKeywordSelector::getName() const {
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000330 llvm::SmallString<256> Str;
331 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000332 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
333 if (*I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000334 OS << (*I)->getName();
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000335 OS << ':';
Steve Naroff29238a02007-10-05 18:42:47 +0000336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000338 return OS.str();
Steve Naroff29238a02007-10-05 18:42:47 +0000339}
340
Chris Lattner077bf5e2008-11-24 03:33:13 +0000341std::string Selector::getAsString() const {
Douglas Gregor405bad02009-04-26 22:20:50 +0000342 if (InfoPtr == 0)
343 return "<null selector>";
344
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000345 if (InfoPtr & ArgFlags) {
346 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek150ec292009-03-07 01:22:02 +0000348 // If the number of arguments is 0 then II is guaranteed to not be null.
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000349 if (getNumArgs() == 0)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000350 return II->getName();
Ted Kremenekf5ed3962009-03-06 23:36:28 +0000351
Daniel Dunbar76b61cc2009-10-17 18:13:02 +0000352 if (!II)
353 return ":";
354
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000355 return II->getName().str() + ":";
Steve Naroff29238a02007-10-05 18:42:47 +0000356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattnerf836e3f2007-10-07 01:33:16 +0000358 // We have a multiple keyword selector (no embedded flags).
359 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
Steve Naroff29238a02007-10-05 18:42:47 +0000360}
361
362
Chris Lattner5f7d2282009-03-04 05:35:38 +0000363namespace {
364 struct SelectorTableImpl {
365 llvm::FoldingSet<MultiKeywordSelector> Table;
366 llvm::BumpPtrAllocator Allocator;
367 };
368} // end anonymous namespace.
369
370static SelectorTableImpl &getSelectorTableImpl(void *P) {
371 return *static_cast<SelectorTableImpl*>(P);
372}
373
374
Chris Lattnerff384912007-10-07 02:00:24 +0000375Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
376 if (nKeys < 2)
377 return Selector(IIV[0], nKeys);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattner5f7d2282009-03-04 05:35:38 +0000379 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Steve Naroff29238a02007-10-05 18:42:47 +0000381 // Unique selector, to guarantee there is one per name.
382 llvm::FoldingSetNodeID ID;
383 MultiKeywordSelector::Profile(ID, IIV, nKeys);
384
385 void *InsertPos = 0;
Chris Lattner5f7d2282009-03-04 05:35:38 +0000386 if (MultiKeywordSelector *SI =
387 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroff29238a02007-10-05 18:42:47 +0000388 return Selector(SI);
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Steve Naroff29238a02007-10-05 18:42:47 +0000390 // MultiKeywordSelector objects are not allocated with new because they have a
391 // variable size array (for parameter types) at the end of them.
Chris Lattner5f7d2282009-03-04 05:35:38 +0000392 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
393 MultiKeywordSelector *SI =
Mike Stump1eb44332009-09-09 15:08:12 +0000394 (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
Chris Lattner32488542010-10-30 05:14:06 +0000395 llvm::alignOf<MultiKeywordSelector>());
Steve Naroff29238a02007-10-05 18:42:47 +0000396 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner5f7d2282009-03-04 05:35:38 +0000397 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroff29238a02007-10-05 18:42:47 +0000398 return Selector(SI);
399}
400
Steve Naroff29238a02007-10-05 18:42:47 +0000401SelectorTable::SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000402 Impl = new SelectorTableImpl();
Steve Naroff29238a02007-10-05 18:42:47 +0000403}
404
405SelectorTable::~SelectorTable() {
Chris Lattner5f7d2282009-03-04 05:35:38 +0000406 delete &getSelectorTableImpl(Impl);
Steve Naroff29238a02007-10-05 18:42:47 +0000407}
408
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000409const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
410 switch (Operator) {
411 case OO_None:
412 case NUM_OVERLOADED_OPERATORS:
413 return 0;
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000414
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000415#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
416 case OO_##Name: return Spelling;
417#include "clang/Basic/OperatorKinds.def"
418 }
Kovarththanan Rajaratnam50acf242010-03-12 11:27:37 +0000419
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000420 return 0;
421}
422