IdentifierTable.cpp revision bda0b626e74513950405c27525af87e214e605e2
1//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the IdentifierInfo, IdentifierVisitor, and
11// IdentifierTable interfaces.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Basic/IdentifierTable.h"
16#include "clang/Basic/LangOptions.h"
17#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/Bitcode/Serialize.h"
20#include "llvm/Bitcode/Deserialize.h"
21
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// IdentifierInfo Implementation
26//===----------------------------------------------------------------------===//
27
28IdentifierInfo::IdentifierInfo() {
29  TokenID = tok::identifier;
30  ObjCID = tok::objc_not_keyword;
31  BuiltinID = 0;
32  HasMacro = false;
33  IsExtension = false;
34  IsPoisoned = false;
35  IsCPPOperatorKeyword = false;
36  FETokenInfo = 0;
37}
38
39//===----------------------------------------------------------------------===//
40// IdentifierTable Implementation
41//===----------------------------------------------------------------------===//
42
43IdentifierTable::IdentifierTable(const LangOptions &LangOpts)
44  // Start with space for 8K identifiers.
45  : HashTable(8192) {
46
47  // Populate the identifier table with info about keywords for the current
48  // language.
49  AddKeywords(LangOpts);
50}
51
52// This cstor is intended to be used only for serialization.
53IdentifierTable::IdentifierTable() : HashTable(8192) {}
54
55//===----------------------------------------------------------------------===//
56// Language Keyword Implementation
57//===----------------------------------------------------------------------===//
58
59/// AddKeyword - This method is used to associate a token ID with specific
60/// identifiers because they are language keywords.  This causes the lexer to
61/// automatically map matching identifiers to specialized token codes.
62///
63/// The C90/C99/CPP/CPP0x flags are set to 0 if the token should be
64/// enabled in the specified langauge, set to 1 if it is an extension
65/// in the specified language, and set to 2 if disabled in the
66/// specified language.
67static void AddKeyword(const char *Keyword, unsigned KWLen,
68                       tok::TokenKind TokenCode,
69                       int C90, int C99, int CXX, int CXX0x, int BoolSupport,
70                       const LangOptions &LangOpts, IdentifierTable &Table) {
71  int Flags = 0;
72  if (BoolSupport != 0) {
73    Flags = LangOpts.Boolean ? BoolSupport : 2;
74  } else if (LangOpts.CPlusPlus) {
75    Flags = LangOpts.CPlusPlus0x ? CXX0x : CXX;
76  } else if (LangOpts.C99) {
77    Flags = C99;
78  } else {
79    Flags = C90;
80  }
81
82  // Don't add this keyword if disabled in this language or if an extension
83  // and extensions are disabled.
84  if (Flags + LangOpts.NoExtensions >= 2) return;
85
86  IdentifierInfo &Info = Table.get(Keyword, Keyword+KWLen);
87  Info.setTokenID(TokenCode);
88  Info.setIsExtensionToken(Flags == 1);
89}
90
91static void AddAlias(const char *Keyword, unsigned KWLen,
92                     tok::TokenKind AliaseeID,
93                     const char *AliaseeKeyword, unsigned AliaseeKWLen,
94                     const LangOptions &LangOpts, IdentifierTable &Table) {
95  IdentifierInfo &AliasInfo = Table.get(Keyword, Keyword+KWLen);
96  IdentifierInfo &AliaseeInfo = Table.get(AliaseeKeyword,
97                                          AliaseeKeyword+AliaseeKWLen);
98  AliasInfo.setTokenID(AliaseeID);
99  AliasInfo.setIsExtensionToken(AliaseeInfo.isExtensionToken());
100}
101
102/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
103/// representations.
104static void AddCXXOperatorKeyword(const char *Keyword, unsigned KWLen,
105                                  tok::TokenKind TokenCode,
106                                  IdentifierTable &Table) {
107  IdentifierInfo &Info = Table.get(Keyword, Keyword + KWLen);
108  Info.setTokenID(TokenCode);
109  Info.setIsCPlusPlusOperatorKeyword();
110}
111
112/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
113/// "property".
114static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID,
115                           const char *Name, unsigned NameLen,
116                           IdentifierTable &Table) {
117  Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID);
118}
119
120/// AddKeywords - Add all keywords to the symbol table.
121///
122void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
123  enum {
124    C90Shift = 0,
125    EXTC90   = 1 << C90Shift,
126    NOTC90   = 2 << C90Shift,
127    C99Shift = 2,
128    EXTC99   = 1 << C99Shift,
129    NOTC99   = 2 << C99Shift,
130    CPPShift = 4,
131    EXTCPP   = 1 << CPPShift,
132    NOTCPP   = 2 << CPPShift,
133    CPP0xShift = 6,
134    EXTCPP0x   = 1 << CPP0xShift,
135    NOTCPP0x   = 2 << CPP0xShift,
136    BoolShift = 8,
137    BOOLSUPPORT = 1 << BoolShift,
138    Mask     = 3
139  };
140
141  // Add keywords and tokens for the current language.
142#define KEYWORD(NAME, FLAGS) \
143  AddKeyword(#NAME, strlen(#NAME), tok::kw_ ## NAME,  \
144             ((FLAGS) >> C90Shift) & Mask, \
145             ((FLAGS) >> C99Shift) & Mask, \
146             ((FLAGS) >> CPPShift) & Mask, \
147             ((FLAGS) >> CPP0xShift) & Mask, \
148             ((FLAGS) >> BoolShift) & Mask, LangOpts, *this);
149#define ALIAS(NAME, TOK) \
150  AddAlias(NAME, strlen(NAME), tok::kw_ ## TOK, #TOK, strlen(#TOK),  \
151           LangOpts, *this);
152#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
153  if (LangOpts.CXXOperatorNames)          \
154    AddCXXOperatorKeyword(#NAME, strlen(#NAME), tok::ALIAS, *this);
155#define OBJC1_AT_KEYWORD(NAME) \
156  if (LangOpts.ObjC1)          \
157    AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
158#define OBJC2_AT_KEYWORD(NAME) \
159  if (LangOpts.ObjC2)          \
160    AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
161#include "clang/Basic/TokenKinds.def"
162}
163
164tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
165  // We use a perfect hash function here involving the length of the keyword,
166  // the first and third character.  For preprocessor ID's there are no
167  // collisions (if there were, the switch below would complain about duplicate
168  // case values).  Note that this depends on 'if' being null terminated.
169
170#define HASH(LEN, FIRST, THIRD) \
171  (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
172#define CASE(LEN, FIRST, THIRD, NAME) \
173  case HASH(LEN, FIRST, THIRD): \
174    return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
175
176  unsigned Len = getLength();
177  if (Len < 2) return tok::pp_not_keyword;
178  const char *Name = getName();
179  switch (HASH(Len, Name[0], Name[2])) {
180  default: return tok::pp_not_keyword;
181  CASE( 2, 'i', '\0', if);
182  CASE( 4, 'e', 'i', elif);
183  CASE( 4, 'e', 's', else);
184  CASE( 4, 'l', 'n', line);
185  CASE( 4, 's', 'c', sccs);
186  CASE( 5, 'e', 'd', endif);
187  CASE( 5, 'e', 'r', error);
188  CASE( 5, 'i', 'e', ident);
189  CASE( 5, 'i', 'd', ifdef);
190  CASE( 5, 'u', 'd', undef);
191
192  CASE( 6, 'a', 's', assert);
193  CASE( 6, 'd', 'f', define);
194  CASE( 6, 'i', 'n', ifndef);
195  CASE( 6, 'i', 'p', import);
196  CASE( 6, 'p', 'a', pragma);
197
198  CASE( 7, 'd', 'f', defined);
199  CASE( 7, 'i', 'c', include);
200  CASE( 7, 'w', 'r', warning);
201
202  CASE( 8, 'u', 'a', unassert);
203  CASE(12, 'i', 'c', include_next);
204#undef CASE
205#undef HASH
206  }
207}
208
209//===----------------------------------------------------------------------===//
210// Stats Implementation
211//===----------------------------------------------------------------------===//
212
213/// PrintStats - Print statistics about how well the identifier table is doing
214/// at hashing identifiers.
215void IdentifierTable::PrintStats() const {
216  unsigned NumBuckets = HashTable.getNumBuckets();
217  unsigned NumIdentifiers = HashTable.getNumItems();
218  unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
219  unsigned AverageIdentifierSize = 0;
220  unsigned MaxIdentifierLength = 0;
221
222  // TODO: Figure out maximum times an identifier had to probe for -stats.
223  for (llvm::StringMap<IdentifierInfo, llvm::BumpPtrAllocator>::const_iterator
224       I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
225    unsigned IdLen = I->getKeyLength();
226    AverageIdentifierSize += IdLen;
227    if (MaxIdentifierLength < IdLen)
228      MaxIdentifierLength = IdLen;
229  }
230
231  fprintf(stderr, "\n*** Identifier Table Stats:\n");
232  fprintf(stderr, "# Identifiers:   %d\n", NumIdentifiers);
233  fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
234  fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
235          NumIdentifiers/(double)NumBuckets);
236  fprintf(stderr, "Ave identifier length: %f\n",
237          (AverageIdentifierSize/(double)NumIdentifiers));
238  fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
239
240  // Compute statistics about the memory allocated for identifiers.
241  HashTable.getAllocator().PrintStats();
242}
243
244//===----------------------------------------------------------------------===//
245// SelectorTable Implementation
246//===----------------------------------------------------------------------===//
247
248unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
249  return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
250}
251
252
253/// MultiKeywordSelector - One of these variable length records is kept for each
254/// selector containing more than one keyword. We use a folding set
255/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
256/// this class is provided strictly through Selector.
257namespace clang {
258class MultiKeywordSelector : public llvm::FoldingSetNode {
259  friend SelectorTable* SelectorTable::CreateAndRegister(llvm::Deserializer&);
260  MultiKeywordSelector(unsigned nKeys) : NumArgs(nKeys) {}
261public:
262  unsigned NumArgs;
263
264  // Constructor for keyword selectors.
265  MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
266    assert((nKeys > 1) && "not a multi-keyword selector");
267    NumArgs = nKeys;
268
269    // Fill in the trailing keyword array.
270    IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
271    for (unsigned i = 0; i != nKeys; ++i)
272      KeyInfo[i] = IIV[i];
273  }
274
275  // getName - Derive the full selector name and return it.
276  std::string getName() const;
277
278  unsigned getNumArgs() const { return NumArgs; }
279
280  typedef IdentifierInfo *const *keyword_iterator;
281  keyword_iterator keyword_begin() const {
282    return reinterpret_cast<keyword_iterator>(this+1);
283  }
284  keyword_iterator keyword_end() const {
285    return keyword_begin()+NumArgs;
286  }
287  IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
288    assert(i < NumArgs && "getIdentifierInfoForSlot(): illegal index");
289    return keyword_begin()[i];
290  }
291  static void Profile(llvm::FoldingSetNodeID &ID,
292                      keyword_iterator ArgTys, unsigned NumArgs) {
293    ID.AddInteger(NumArgs);
294    for (unsigned i = 0; i != NumArgs; ++i)
295      ID.AddPointer(ArgTys[i]);
296  }
297  void Profile(llvm::FoldingSetNodeID &ID) {
298    Profile(ID, keyword_begin(), NumArgs);
299  }
300};
301} // end namespace clang.
302
303unsigned Selector::getNumArgs() const {
304  unsigned IIF = getIdentifierInfoFlag();
305  if (IIF == ZeroArg)
306    return 0;
307  if (IIF == OneArg)
308    return 1;
309  // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
310  MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
311  return SI->getNumArgs();
312}
313
314IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
315  if (IdentifierInfo *II = getAsIdentifierInfo()) {
316    assert(argIndex == 0 && "illegal keyword index");
317    return II;
318  }
319  // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
320  MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
321  return SI->getIdentifierInfoForSlot(argIndex);
322}
323
324std::string MultiKeywordSelector::getName() const {
325  std::string Result;
326  unsigned Length = 0;
327  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
328    if (*I)
329      Length += (*I)->getLength();
330    ++Length;  // :
331  }
332
333  Result.reserve(Length);
334
335  for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
336    if (*I)
337      Result.insert(Result.end(), (*I)->getName(),
338                    (*I)->getName()+(*I)->getLength());
339    Result.push_back(':');
340  }
341
342  return Result;
343}
344
345std::string Selector::getName() const {
346  if (IdentifierInfo *II = getAsIdentifierInfo()) {
347    if (getNumArgs() == 0)
348      return II->getName();
349
350    std::string Res = II->getName();
351    Res += ":";
352    return Res;
353  }
354
355  // We have a multiple keyword selector (no embedded flags).
356  return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
357}
358
359
360Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
361  if (nKeys < 2)
362    return Selector(IIV[0], nKeys);
363
364  llvm::FoldingSet<MultiKeywordSelector> *SelTab;
365
366  SelTab = static_cast<llvm::FoldingSet<MultiKeywordSelector> *>(Impl);
367
368  // Unique selector, to guarantee there is one per name.
369  llvm::FoldingSetNodeID ID;
370  MultiKeywordSelector::Profile(ID, IIV, nKeys);
371
372  void *InsertPos = 0;
373  if (MultiKeywordSelector *SI = SelTab->FindNodeOrInsertPos(ID, InsertPos))
374    return Selector(SI);
375
376  // MultiKeywordSelector objects are not allocated with new because they have a
377  // variable size array (for parameter types) at the end of them.
378  MultiKeywordSelector *SI =
379    (MultiKeywordSelector*)malloc(sizeof(MultiKeywordSelector) +
380                                  nKeys*sizeof(IdentifierInfo *));
381  new (SI) MultiKeywordSelector(nKeys, IIV);
382  SelTab->InsertNode(SI, InsertPos);
383  return Selector(SI);
384}
385
386SelectorTable::SelectorTable() {
387  Impl = new llvm::FoldingSet<MultiKeywordSelector>;
388}
389
390SelectorTable::~SelectorTable() {
391  delete static_cast<llvm::FoldingSet<MultiKeywordSelector> *>(Impl);
392}
393
394//===----------------------------------------------------------------------===//
395// Serialization for IdentifierInfo and IdentifierTable.
396//===----------------------------------------------------------------------===//
397
398void IdentifierInfo::Emit(llvm::Serializer& S) const {
399  S.EmitInt(getTokenID());
400  S.EmitInt(getBuiltinID());
401  S.EmitInt(getObjCKeywordID());
402  S.EmitBool(hasMacroDefinition());
403  S.EmitBool(isExtensionToken());
404  S.EmitBool(isPoisoned());
405  S.EmitBool(isCPlusPlusOperatorKeyword());
406  // FIXME: FETokenInfo
407}
408
409void IdentifierInfo::Read(llvm::Deserializer& D) {
410  setTokenID((tok::TokenKind) D.ReadInt());
411  setBuiltinID(D.ReadInt());
412  setObjCKeywordID((tok::ObjCKeywordKind) D.ReadInt());
413  setHasMacroDefinition(D.ReadBool());
414  setIsExtensionToken(D.ReadBool());
415  setIsPoisoned(D.ReadBool());
416  setIsCPlusPlusOperatorKeyword(D.ReadBool());
417  // FIXME: FETokenInfo
418}
419
420void IdentifierTable::Emit(llvm::Serializer& S) const {
421  S.EnterBlock();
422
423  S.EmitPtr(this);
424
425  for (iterator I=begin(), E=end(); I != E; ++I) {
426    const char* Key = I->getKeyData();
427    const IdentifierInfo* Info = &I->getValue();
428
429    bool KeyRegistered = S.isRegistered(Key);
430    bool InfoRegistered = S.isRegistered(Info);
431
432    if (KeyRegistered || InfoRegistered) {
433      // These acrobatics are so that we don't incur the cost of registering
434      // a pointer with the backpatcher during deserialization if nobody
435      // references the object.
436      S.EmitPtr(InfoRegistered ? Info : NULL);
437      S.EmitPtr(KeyRegistered ? Key : NULL);
438      S.EmitCStr(Key);
439      S.Emit(*Info);
440    }
441  }
442
443  S.ExitBlock();
444}
445
446IdentifierTable* IdentifierTable::CreateAndRegister(llvm::Deserializer& D) {
447  llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
448
449  std::vector<char> buff;
450  buff.reserve(200);
451
452  IdentifierTable* t = new IdentifierTable();
453  D.RegisterPtr(t);
454
455  while (!D.FinishedBlock(BLoc)) {
456    llvm::SerializedPtrID InfoPtrID = D.ReadPtrID();
457    llvm::SerializedPtrID KeyPtrID = D.ReadPtrID();
458
459    D.ReadCStr(buff);
460
461    llvm::StringMapEntry<IdentifierInfo>& Entry =
462      t->HashTable.GetOrCreateValue(&buff[0],&buff[0]+buff.size());
463
464    D.Read(Entry.getValue());
465
466    if (InfoPtrID)
467      D.RegisterRef(InfoPtrID,Entry.getValue());
468
469    if (KeyPtrID)
470      D.RegisterPtr(KeyPtrID,Entry.getKeyData());
471  }
472
473  return t;
474}
475
476//===----------------------------------------------------------------------===//
477// Serialization for Selector and SelectorTable.
478//===----------------------------------------------------------------------===//
479
480void Selector::Emit(llvm::Serializer& S) const {
481  S.EmitInt(getIdentifierInfoFlag());
482  S.EmitPtr(reinterpret_cast<void*>(InfoPtr & ~ArgFlags));
483}
484
485Selector Selector::ReadVal(llvm::Deserializer& D) {
486  unsigned flag = D.ReadInt();
487
488  uintptr_t ptr;
489  D.ReadUIntPtr(ptr,false); // No backpatching.
490
491  return Selector(ptr | flag);
492}
493
494void SelectorTable::Emit(llvm::Serializer& S) const {
495  typedef llvm::FoldingSet<MultiKeywordSelector>::iterator iterator;
496  llvm::FoldingSet<MultiKeywordSelector> *SelTab;
497  SelTab = static_cast<llvm::FoldingSet<MultiKeywordSelector> *>(Impl);
498
499  S.EnterBlock();
500
501  S.EmitPtr(this);
502
503  for (iterator I=SelTab->begin(), E=SelTab->end(); I != E; ++I) {
504    if (!S.isRegistered(&*I))
505      continue;
506
507    S.FlushRecord(); // Start a new record.
508
509    S.EmitPtr(&*I);
510    S.EmitInt(I->getNumArgs());
511
512    for (MultiKeywordSelector::keyword_iterator KI = I->keyword_begin(),
513         KE = I->keyword_end(); KI != KE; ++KI)
514      S.EmitPtr(*KI);
515  }
516
517  S.ExitBlock();
518}
519
520SelectorTable* SelectorTable::CreateAndRegister(llvm::Deserializer& D) {
521  llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
522
523  SelectorTable* t = new SelectorTable();
524  D.RegisterPtr(t);
525
526  llvm::FoldingSet<MultiKeywordSelector>& SelTab =
527    *static_cast<llvm::FoldingSet<MultiKeywordSelector>*>(t->Impl);
528
529  while (!D.FinishedBlock(BLoc)) {
530
531    llvm::SerializedPtrID PtrID = D.ReadPtrID();
532    unsigned nKeys = D.ReadInt();
533
534    MultiKeywordSelector *SI =
535      (MultiKeywordSelector*)malloc(sizeof(MultiKeywordSelector) +
536                                    nKeys*sizeof(IdentifierInfo *));
537
538    new (SI) MultiKeywordSelector(nKeys);
539
540    D.RegisterPtr(PtrID,SI);
541
542    IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(SI+1);
543
544    for (unsigned i = 0; i != nKeys; ++i)
545      D.ReadPtr(KeyInfo[i],false);
546
547    SelTab.GetOrInsertNode(SI);
548  }
549
550  return t;
551}
552