Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | |
| 15 | #include "clang/Lex/IdentifierTable.h" |
| 16 | #include "clang/Lex/MacroInfo.h" |
| 17 | #include "clang/Basic/LangOptions.h" |
| 18 | using namespace clang; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // IdentifierInfo Implementation |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | IdentifierInfo::IdentifierInfo() { |
| 25 | Macro = 0; |
| 26 | TokenID = tok::identifier; |
| 27 | PPID = tok::pp_not_keyword; |
| 28 | ObjCID = tok::objc_not_keyword; |
| 29 | BuiltinID = 0; |
| 30 | IsExtension = false; |
| 31 | IsPoisoned = false; |
| 32 | IsOtherTargetMacro = false; |
| 33 | IsCPPOperatorKeyword = false; |
Chris Lattner | 938867c | 2007-07-19 00:11:19 +0000 | [diff] [blame^] | 34 | IsNonPortableBuiltin = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | FETokenInfo = 0; |
| 36 | } |
| 37 | |
| 38 | IdentifierInfo::~IdentifierInfo() { |
| 39 | delete Macro; |
| 40 | } |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | // IdentifierTable Implementation |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
| 46 | IdentifierTable::IdentifierTable(const LangOptions &LangOpts) |
| 47 | // Start with space for 8K identifiers. |
| 48 | : HashTable(8192) { |
| 49 | |
| 50 | // Populate the identifier table with info about keywords for the current |
| 51 | // language. |
| 52 | AddKeywords(LangOpts); |
| 53 | } |
| 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 | /// |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | static void AddKeyword(const char *Keyword, unsigned KWLen, |
| 68 | tok::TokenKind TokenCode, |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 69 | int C90, int C99, int CXX, int CXX0x, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | const LangOptions &LangOpts, IdentifierTable &Table) { |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 71 | int Flags = LangOpts.CPlusPlus ? (LangOpts.CPlusPlus0x? CXX0x : CXX) |
| 72 | : (LangOpts.C99 ? C99 : C90); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | |
| 74 | // Don't add this keyword if disabled in this language or if an extension |
| 75 | // and extensions are disabled. |
| 76 | if (Flags + LangOpts.NoExtensions >= 2) return; |
| 77 | |
| 78 | IdentifierInfo &Info = Table.get(Keyword, Keyword+KWLen); |
| 79 | Info.setTokenID(TokenCode); |
| 80 | Info.setIsExtensionToken(Flags == 1); |
| 81 | } |
| 82 | |
| 83 | static void AddAlias(const char *Keyword, unsigned KWLen, |
| 84 | const char *AliaseeKeyword, unsigned AliaseeKWLen, |
| 85 | const LangOptions &LangOpts, IdentifierTable &Table) { |
| 86 | IdentifierInfo &AliasInfo = Table.get(Keyword, Keyword+KWLen); |
| 87 | IdentifierInfo &AliaseeInfo = Table.get(AliaseeKeyword, |
| 88 | AliaseeKeyword+AliaseeKWLen); |
| 89 | AliasInfo.setTokenID(AliaseeInfo.getTokenID()); |
| 90 | AliasInfo.setIsExtensionToken(AliaseeInfo.isExtensionToken()); |
| 91 | } |
| 92 | |
| 93 | /// AddPPKeyword - Register a preprocessor keyword like "define" "undef" or |
| 94 | /// "elif". |
| 95 | static void AddPPKeyword(tok::PPKeywordKind PPID, |
| 96 | const char *Name, unsigned NameLen, |
| 97 | IdentifierTable &Table) { |
| 98 | Table.get(Name, Name+NameLen).setPPKeywordID(PPID); |
| 99 | } |
| 100 | |
| 101 | /// AddCXXOperatorKeyword - Register a C++ operator keyword alternative |
| 102 | /// representations. |
| 103 | static void AddCXXOperatorKeyword(const char *Keyword, unsigned KWLen, |
| 104 | tok::TokenKind TokenCode, |
| 105 | IdentifierTable &Table) { |
| 106 | IdentifierInfo &Info = Table.get(Keyword, Keyword + KWLen); |
| 107 | Info.setTokenID(TokenCode); |
| 108 | Info.setIsCPlusplusOperatorKeyword(); |
| 109 | } |
| 110 | |
| 111 | /// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or |
| 112 | /// "property". |
| 113 | static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID, |
| 114 | const char *Name, unsigned NameLen, |
| 115 | IdentifierTable &Table) { |
| 116 | Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID); |
| 117 | } |
| 118 | |
| 119 | /// AddKeywords - Add all keywords to the symbol table. |
| 120 | /// |
| 121 | void IdentifierTable::AddKeywords(const LangOptions &LangOpts) { |
| 122 | enum { |
| 123 | C90Shift = 0, |
| 124 | EXTC90 = 1 << C90Shift, |
| 125 | NOTC90 = 2 << C90Shift, |
| 126 | C99Shift = 2, |
| 127 | EXTC99 = 1 << C99Shift, |
| 128 | NOTC99 = 2 << C99Shift, |
| 129 | CPPShift = 4, |
| 130 | EXTCPP = 1 << CPPShift, |
| 131 | NOTCPP = 2 << CPPShift, |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 132 | CPP0xShift = 6, |
| 133 | EXTCPP0x = 1 << CPP0xShift, |
| 134 | NOTCPP0x = 2 << CPP0xShift, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | Mask = 3 |
| 136 | }; |
| 137 | |
| 138 | // Add keywords and tokens for the current language. |
| 139 | #define KEYWORD(NAME, FLAGS) \ |
| 140 | AddKeyword(#NAME, strlen(#NAME), tok::kw_ ## NAME, \ |
| 141 | ((FLAGS) >> C90Shift) & Mask, \ |
| 142 | ((FLAGS) >> C99Shift) & Mask, \ |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 143 | ((FLAGS) >> CPPShift) & Mask, \ |
| 144 | ((FLAGS) >> CPP0xShift) & Mask, LangOpts, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | #define ALIAS(NAME, TOK) \ |
| 146 | AddAlias(NAME, strlen(NAME), #TOK, strlen(#TOK), LangOpts, *this); |
| 147 | #define PPKEYWORD(NAME) \ |
| 148 | AddPPKeyword(tok::pp_##NAME, #NAME, strlen(#NAME), *this); |
| 149 | #define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \ |
| 150 | if (LangOpts.CXXOperatorNames) \ |
| 151 | AddCXXOperatorKeyword(#NAME, strlen(#NAME), tok::ALIAS, *this); |
| 152 | #define OBJC1_AT_KEYWORD(NAME) \ |
| 153 | if (LangOpts.ObjC1) \ |
| 154 | AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this); |
| 155 | #define OBJC2_AT_KEYWORD(NAME) \ |
| 156 | if (LangOpts.ObjC2) \ |
| 157 | AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this); |
| 158 | #include "clang/Basic/TokenKinds.def" |
| 159 | } |
| 160 | |
| 161 | |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | // Stats Implementation |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | |
| 166 | /// PrintStats - Print statistics about how well the identifier table is doing |
| 167 | /// at hashing identifiers. |
| 168 | void IdentifierTable::PrintStats() const { |
| 169 | unsigned NumBuckets = HashTable.getNumBuckets(); |
| 170 | unsigned NumIdentifiers = HashTable.getNumItems(); |
| 171 | unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers; |
| 172 | unsigned AverageIdentifierSize = 0; |
| 173 | unsigned MaxIdentifierLength = 0; |
| 174 | |
| 175 | // TODO: Figure out maximum times an identifier had to probe for -stats. |
| 176 | for (llvm::StringMap<IdentifierInfo, llvm::BumpPtrAllocator>::const_iterator |
| 177 | I = HashTable.begin(), E = HashTable.end(); I != E; ++I) { |
| 178 | unsigned IdLen = I->getKeyLength(); |
| 179 | AverageIdentifierSize += IdLen; |
| 180 | if (MaxIdentifierLength < IdLen) |
| 181 | MaxIdentifierLength = IdLen; |
| 182 | } |
| 183 | |
| 184 | fprintf(stderr, "\n*** Identifier Table Stats:\n"); |
| 185 | fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers); |
| 186 | fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets); |
| 187 | fprintf(stderr, "Hash density (#identifiers per bucket): %f\n", |
| 188 | NumIdentifiers/(double)NumBuckets); |
| 189 | fprintf(stderr, "Ave identifier length: %f\n", |
| 190 | (AverageIdentifierSize/(double)NumIdentifiers)); |
| 191 | fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength); |
| 192 | |
| 193 | // Compute statistics about the memory allocated for identifiers. |
| 194 | HashTable.getAllocator().PrintStats(); |
| 195 | } |