IdentifierTable.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Chris Lattner and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the IdentifierInfo, IdentifierVisitor, and
  11. // IdentifierTable interfaces.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Lex/IdentifierTable.h"
  15. #include "clang/Lex/MacroInfo.h"
  16. #include "clang/Basic/LangOptions.h"
  17. using namespace clang;
  18. //===----------------------------------------------------------------------===//
  19. // IdentifierInfo Implementation
  20. //===----------------------------------------------------------------------===//
  21. IdentifierInfo::IdentifierInfo() {
  22. Macro = 0;
  23. TokenID = tok::identifier;
  24. PPID = tok::pp_not_keyword;
  25. ObjCID = tok::objc_not_keyword;
  26. BuiltinID = 0;
  27. IsExtension = false;
  28. IsPoisoned = false;
  29. IsOtherTargetMacro = false;
  30. IsCPPOperatorKeyword = false;
  31. FETokenInfo = 0;
  32. }
  33. IdentifierInfo::~IdentifierInfo() {
  34. delete Macro;
  35. }
  36. //===----------------------------------------------------------------------===//
  37. // IdentifierTable Implementation
  38. //===----------------------------------------------------------------------===//
  39. IdentifierTable::IdentifierTable(const LangOptions &LangOpts)
  40. // Start with space for 8K identifiers.
  41. : HashTable(8192) {
  42. // Populate the identifier table with info about keywords for the current
  43. // language.
  44. AddKeywords(LangOpts);
  45. }
  46. //===----------------------------------------------------------------------===//
  47. // Language Keyword Implementation
  48. //===----------------------------------------------------------------------===//
  49. /// AddKeyword - This method is used to associate a token ID with specific
  50. /// identifiers because they are language keywords. This causes the lexer to
  51. /// automatically map matching identifiers to specialized token codes.
  52. ///
  53. /// The C90/C99/CPP flags are set to 0 if the token should be enabled in the
  54. /// specified langauge, set to 1 if it is an extension in the specified
  55. /// language, and set to 2 if disabled in the specified language.
  56. static void AddKeyword(const char *Keyword, unsigned KWLen,
  57. tok::TokenKind TokenCode,
  58. int C90, int C99, int CXX,
  59. const LangOptions &LangOpts, IdentifierTable &Table) {
  60. int Flags = LangOpts.CPlusPlus ? CXX : (LangOpts.C99 ? C99 : C90);
  61. // Don't add this keyword if disabled in this language or if an extension
  62. // and extensions are disabled.
  63. if (Flags + LangOpts.NoExtensions >= 2) return;
  64. IdentifierInfo &Info = Table.get(Keyword, Keyword+KWLen);
  65. Info.setTokenID(TokenCode);
  66. Info.setIsExtensionToken(Flags == 1);
  67. }
  68. static void AddAlias(const char *Keyword, unsigned KWLen,
  69. const char *AliaseeKeyword, unsigned AliaseeKWLen,
  70. const LangOptions &LangOpts, IdentifierTable &Table) {
  71. IdentifierInfo &AliasInfo = Table.get(Keyword, Keyword+KWLen);
  72. IdentifierInfo &AliaseeInfo = Table.get(AliaseeKeyword,
  73. AliaseeKeyword+AliaseeKWLen);
  74. AliasInfo.setTokenID(AliaseeInfo.getTokenID());
  75. AliasInfo.setIsExtensionToken(AliaseeInfo.isExtensionToken());
  76. }
  77. /// AddPPKeyword - Register a preprocessor keyword like "define" "undef" or
  78. /// "elif".
  79. static void AddPPKeyword(tok::PPKeywordKind PPID,
  80. const char *Name, unsigned NameLen,
  81. IdentifierTable &Table) {
  82. Table.get(Name, Name+NameLen).setPPKeywordID(PPID);
  83. }
  84. /// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
  85. /// representations.
  86. static void AddCXXOperatorKeyword(const char *Keyword, unsigned KWLen,
  87. tok::TokenKind TokenCode,
  88. IdentifierTable &Table) {
  89. IdentifierInfo &Info = Table.get(Keyword, Keyword + KWLen);
  90. Info.setTokenID(TokenCode);
  91. Info.setIsCPlusplusOperatorKeyword();
  92. }
  93. /// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
  94. /// "property".
  95. static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID,
  96. const char *Name, unsigned NameLen,
  97. IdentifierTable &Table) {
  98. Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID);
  99. }
  100. /// AddKeywords - Add all keywords to the symbol table.
  101. ///
  102. void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
  103. enum {
  104. C90Shift = 0,
  105. EXTC90 = 1 << C90Shift,
  106. NOTC90 = 2 << C90Shift,
  107. C99Shift = 2,
  108. EXTC99 = 1 << C99Shift,
  109. NOTC99 = 2 << C99Shift,
  110. CPPShift = 4,
  111. EXTCPP = 1 << CPPShift,
  112. NOTCPP = 2 << CPPShift,
  113. Mask = 3
  114. };
  115. // Add keywords and tokens for the current language.
  116. #define KEYWORD(NAME, FLAGS) \
  117. AddKeyword(#NAME, strlen(#NAME), tok::kw_ ## NAME, \
  118. ((FLAGS) >> C90Shift) & Mask, \
  119. ((FLAGS) >> C99Shift) & Mask, \
  120. ((FLAGS) >> CPPShift) & Mask, LangOpts, *this);
  121. #define ALIAS(NAME, TOK) \
  122. AddAlias(NAME, strlen(NAME), #TOK, strlen(#TOK), LangOpts, *this);
  123. #define PPKEYWORD(NAME) \
  124. AddPPKeyword(tok::pp_##NAME, #NAME, strlen(#NAME), *this);
  125. #define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
  126. if (LangOpts.CXXOperatorNames) \
  127. AddCXXOperatorKeyword(#NAME, strlen(#NAME), tok::ALIAS, *this);
  128. #define OBJC1_AT_KEYWORD(NAME) \
  129. if (LangOpts.ObjC1) \
  130. AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
  131. #define OBJC2_AT_KEYWORD(NAME) \
  132. if (LangOpts.ObjC2) \
  133. AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
  134. #include "clang/Basic/TokenKinds.def"
  135. }
  136. //===----------------------------------------------------------------------===//
  137. // Stats Implementation
  138. //===----------------------------------------------------------------------===//
  139. /// PrintStats - Print statistics about how well the identifier table is doing
  140. /// at hashing identifiers.
  141. void IdentifierTable::PrintStats() const {
  142. unsigned NumBuckets = HashTable.getNumBuckets();
  143. unsigned NumIdentifiers = HashTable.getNumItems();
  144. unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
  145. unsigned AverageIdentifierSize = 0;
  146. unsigned MaxIdentifierLength = 0;
  147. // TODO: Figure out maximum times an identifier had to probe for -stats.
  148. for (llvm::StringMap<IdentifierInfo, llvm::BumpPtrAllocator>::const_iterator
  149. I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
  150. unsigned IdLen = I->getKeyLength();
  151. AverageIdentifierSize += IdLen;
  152. if (MaxIdentifierLength < IdLen)
  153. MaxIdentifierLength = IdLen;
  154. }
  155. fprintf(stderr, "\n*** Identifier Table Stats:\n");
  156. fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
  157. fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
  158. fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
  159. NumIdentifiers/(double)NumBuckets);
  160. fprintf(stderr, "Ave identifier length: %f\n",
  161. (AverageIdentifierSize/(double)NumIdentifiers));
  162. fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
  163. // Compute statistics about the memory allocated for identifiers.
  164. HashTable.getAllocator().PrintStats();
  165. }