Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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" |
Steve Naroff | f9e80db | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/FoldingSet.h" |
Chris Lattner | 48ed6f8 | 2007-10-05 20:15:24 +0000 | [diff] [blame^] | 19 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
Steve Naroff | 87c329f | 2007-08-23 18:16:40 +0000 | [diff] [blame] | 23 | // Token Implementation |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. |
| 27 | bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const { |
| 28 | return getKind() == tok::identifier && |
| 29 | getIdentifierInfo()->getObjCKeywordID() == objcKey; |
| 30 | } |
| 31 | |
| 32 | /// getObjCKeywordID - Return the ObjC keyword kind. |
| 33 | tok::ObjCKeywordKind Token::getObjCKeywordID() const { |
| 34 | IdentifierInfo *specId = getIdentifierInfo(); |
| 35 | return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; |
| 36 | } |
| 37 | |
| 38 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 39 | // IdentifierInfo Implementation |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| 42 | IdentifierInfo::IdentifierInfo() { |
| 43 | Macro = 0; |
| 44 | TokenID = tok::identifier; |
| 45 | PPID = tok::pp_not_keyword; |
| 46 | ObjCID = tok::objc_not_keyword; |
| 47 | BuiltinID = 0; |
| 48 | IsExtension = false; |
| 49 | IsPoisoned = false; |
| 50 | IsOtherTargetMacro = false; |
| 51 | IsCPPOperatorKeyword = false; |
| 52 | IsNonPortableBuiltin = false; |
| 53 | FETokenInfo = 0; |
| 54 | } |
| 55 | |
| 56 | IdentifierInfo::~IdentifierInfo() { |
| 57 | delete Macro; |
| 58 | } |
| 59 | |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | // IdentifierTable Implementation |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
| 64 | IdentifierTable::IdentifierTable(const LangOptions &LangOpts) |
| 65 | // Start with space for 8K identifiers. |
| 66 | : HashTable(8192) { |
| 67 | |
| 68 | // Populate the identifier table with info about keywords for the current |
| 69 | // language. |
| 70 | AddKeywords(LangOpts); |
| 71 | } |
| 72 | |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | // Language Keyword Implementation |
| 75 | //===----------------------------------------------------------------------===// |
| 76 | |
| 77 | /// AddKeyword - This method is used to associate a token ID with specific |
| 78 | /// identifiers because they are language keywords. This causes the lexer to |
| 79 | /// automatically map matching identifiers to specialized token codes. |
| 80 | /// |
| 81 | /// The C90/C99/CPP/CPP0x flags are set to 0 if the token should be |
| 82 | /// enabled in the specified langauge, set to 1 if it is an extension |
| 83 | /// in the specified language, and set to 2 if disabled in the |
| 84 | /// specified language. |
| 85 | static void AddKeyword(const char *Keyword, unsigned KWLen, |
| 86 | tok::TokenKind TokenCode, |
| 87 | int C90, int C99, int CXX, int CXX0x, |
| 88 | const LangOptions &LangOpts, IdentifierTable &Table) { |
| 89 | int Flags = LangOpts.CPlusPlus ? (LangOpts.CPlusPlus0x? CXX0x : CXX) |
| 90 | : (LangOpts.C99 ? C99 : C90); |
| 91 | |
| 92 | // Don't add this keyword if disabled in this language or if an extension |
| 93 | // and extensions are disabled. |
| 94 | if (Flags + LangOpts.NoExtensions >= 2) return; |
| 95 | |
| 96 | IdentifierInfo &Info = Table.get(Keyword, Keyword+KWLen); |
| 97 | Info.setTokenID(TokenCode); |
| 98 | Info.setIsExtensionToken(Flags == 1); |
| 99 | } |
| 100 | |
| 101 | static void AddAlias(const char *Keyword, unsigned KWLen, |
| 102 | const char *AliaseeKeyword, unsigned AliaseeKWLen, |
| 103 | const LangOptions &LangOpts, IdentifierTable &Table) { |
| 104 | IdentifierInfo &AliasInfo = Table.get(Keyword, Keyword+KWLen); |
| 105 | IdentifierInfo &AliaseeInfo = Table.get(AliaseeKeyword, |
| 106 | AliaseeKeyword+AliaseeKWLen); |
| 107 | AliasInfo.setTokenID(AliaseeInfo.getTokenID()); |
| 108 | AliasInfo.setIsExtensionToken(AliaseeInfo.isExtensionToken()); |
| 109 | } |
| 110 | |
| 111 | /// AddPPKeyword - Register a preprocessor keyword like "define" "undef" or |
| 112 | /// "elif". |
| 113 | static void AddPPKeyword(tok::PPKeywordKind PPID, |
| 114 | const char *Name, unsigned NameLen, |
| 115 | IdentifierTable &Table) { |
| 116 | Table.get(Name, Name+NameLen).setPPKeywordID(PPID); |
| 117 | } |
| 118 | |
| 119 | /// AddCXXOperatorKeyword - Register a C++ operator keyword alternative |
| 120 | /// representations. |
| 121 | static void AddCXXOperatorKeyword(const char *Keyword, unsigned KWLen, |
| 122 | tok::TokenKind TokenCode, |
| 123 | IdentifierTable &Table) { |
| 124 | IdentifierInfo &Info = Table.get(Keyword, Keyword + KWLen); |
| 125 | Info.setTokenID(TokenCode); |
| 126 | Info.setIsCPlusplusOperatorKeyword(); |
| 127 | } |
| 128 | |
| 129 | /// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or |
| 130 | /// "property". |
| 131 | static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID, |
| 132 | const char *Name, unsigned NameLen, |
| 133 | IdentifierTable &Table) { |
| 134 | Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID); |
| 135 | } |
| 136 | |
| 137 | /// AddKeywords - Add all keywords to the symbol table. |
| 138 | /// |
| 139 | void IdentifierTable::AddKeywords(const LangOptions &LangOpts) { |
| 140 | enum { |
| 141 | C90Shift = 0, |
| 142 | EXTC90 = 1 << C90Shift, |
| 143 | NOTC90 = 2 << C90Shift, |
| 144 | C99Shift = 2, |
| 145 | EXTC99 = 1 << C99Shift, |
| 146 | NOTC99 = 2 << C99Shift, |
| 147 | CPPShift = 4, |
| 148 | EXTCPP = 1 << CPPShift, |
| 149 | NOTCPP = 2 << CPPShift, |
| 150 | CPP0xShift = 6, |
| 151 | EXTCPP0x = 1 << CPP0xShift, |
| 152 | NOTCPP0x = 2 << CPP0xShift, |
| 153 | Mask = 3 |
| 154 | }; |
| 155 | |
| 156 | // Add keywords and tokens for the current language. |
| 157 | #define KEYWORD(NAME, FLAGS) \ |
| 158 | AddKeyword(#NAME, strlen(#NAME), tok::kw_ ## NAME, \ |
| 159 | ((FLAGS) >> C90Shift) & Mask, \ |
| 160 | ((FLAGS) >> C99Shift) & Mask, \ |
| 161 | ((FLAGS) >> CPPShift) & Mask, \ |
| 162 | ((FLAGS) >> CPP0xShift) & Mask, LangOpts, *this); |
| 163 | #define ALIAS(NAME, TOK) \ |
| 164 | AddAlias(NAME, strlen(NAME), #TOK, strlen(#TOK), LangOpts, *this); |
| 165 | #define PPKEYWORD(NAME) \ |
| 166 | AddPPKeyword(tok::pp_##NAME, #NAME, strlen(#NAME), *this); |
| 167 | #define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \ |
| 168 | if (LangOpts.CXXOperatorNames) \ |
| 169 | AddCXXOperatorKeyword(#NAME, strlen(#NAME), tok::ALIAS, *this); |
| 170 | #define OBJC1_AT_KEYWORD(NAME) \ |
| 171 | if (LangOpts.ObjC1) \ |
| 172 | AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this); |
| 173 | #define OBJC2_AT_KEYWORD(NAME) \ |
| 174 | if (LangOpts.ObjC2) \ |
| 175 | AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this); |
| 176 | #include "clang/Basic/TokenKinds.def" |
| 177 | } |
| 178 | |
| 179 | |
| 180 | //===----------------------------------------------------------------------===// |
| 181 | // Stats Implementation |
| 182 | //===----------------------------------------------------------------------===// |
| 183 | |
| 184 | /// PrintStats - Print statistics about how well the identifier table is doing |
| 185 | /// at hashing identifiers. |
| 186 | void IdentifierTable::PrintStats() const { |
| 187 | unsigned NumBuckets = HashTable.getNumBuckets(); |
| 188 | unsigned NumIdentifiers = HashTable.getNumItems(); |
| 189 | unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers; |
| 190 | unsigned AverageIdentifierSize = 0; |
| 191 | unsigned MaxIdentifierLength = 0; |
| 192 | |
| 193 | // TODO: Figure out maximum times an identifier had to probe for -stats. |
| 194 | for (llvm::StringMap<IdentifierInfo, llvm::BumpPtrAllocator>::const_iterator |
| 195 | I = HashTable.begin(), E = HashTable.end(); I != E; ++I) { |
| 196 | unsigned IdLen = I->getKeyLength(); |
| 197 | AverageIdentifierSize += IdLen; |
| 198 | if (MaxIdentifierLength < IdLen) |
| 199 | MaxIdentifierLength = IdLen; |
| 200 | } |
| 201 | |
| 202 | fprintf(stderr, "\n*** Identifier Table Stats:\n"); |
| 203 | fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers); |
| 204 | fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets); |
| 205 | fprintf(stderr, "Hash density (#identifiers per bucket): %f\n", |
| 206 | NumIdentifiers/(double)NumBuckets); |
| 207 | fprintf(stderr, "Ave identifier length: %f\n", |
| 208 | (AverageIdentifierSize/(double)NumIdentifiers)); |
| 209 | fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength); |
| 210 | |
| 211 | // Compute statistics about the memory allocated for identifiers. |
| 212 | HashTable.getAllocator().PrintStats(); |
| 213 | } |
Steve Naroff | 4ed9d66 | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 214 | |
Steve Naroff | f9e80db | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
| 216 | // SelectorTable Implementation |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | |
Chris Lattner | 48ed6f8 | 2007-10-05 20:15:24 +0000 | [diff] [blame^] | 219 | unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) { |
| 220 | return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr()); |
| 221 | } |
| 222 | |
| 223 | |
Steve Naroff | f9e80db | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 224 | /// MultiKeywordSelector - One of these variable length records is kept for each |
| 225 | /// selector containing more than one keyword. We use a folding set |
| 226 | /// to unique aggregate names (keyword selectors in ObjC parlance). Access to |
| 227 | /// this class is provided strictly through Selector. |
Chris Lattner | 48ed6f8 | 2007-10-05 20:15:24 +0000 | [diff] [blame^] | 228 | namespace clang { |
Steve Naroff | f9e80db | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 229 | class MultiKeywordSelector : public llvm::FoldingSetNode { |
| 230 | public: |
| 231 | unsigned NumArgs; |
| 232 | |
| 233 | // Constructor for keyword selectors. |
| 234 | MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) { |
| 235 | assert((nKeys > 1) && "not a multi-keyword selector"); |
| 236 | NumArgs = nKeys; |
| 237 | // Fill in the trailing keyword array. |
| 238 | IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1); |
| 239 | for (unsigned i = 0; i != nKeys; ++i) |
| 240 | KeyInfo[i] = IIV[i]; |
| 241 | } |
| 242 | // Derive the full selector name, placing the result into methodBuffer. |
| 243 | // As a convenience, a pointer to the first character is returned. |
| 244 | // Example usage: llvm::SmallString<128> mbuf; Selector->getName(mbuf); |
| 245 | char *getName(llvm::SmallVectorImpl<char> &methodBuffer); |
| 246 | |
| 247 | unsigned getNumArgs() const { return NumArgs; } |
| 248 | |
| 249 | typedef IdentifierInfo *const *keyword_iterator; |
| 250 | keyword_iterator keyword_begin() const { |
| 251 | return reinterpret_cast<keyword_iterator>(this+1); |
| 252 | } |
| 253 | keyword_iterator keyword_end() const { |
| 254 | return keyword_begin()+NumArgs; |
| 255 | } |
| 256 | IdentifierInfo *getIdentifierInfoForSlot(unsigned i) { |
| 257 | assert((i < NumArgs) && "getIdentifierInfoForSlot(): illegal index"); |
| 258 | return keyword_begin()[i]; |
| 259 | } |
| 260 | static void Profile(llvm::FoldingSetNodeID &ID, |
| 261 | keyword_iterator ArgTys, unsigned NumArgs) { |
| 262 | ID.AddInteger(NumArgs); |
| 263 | if (NumArgs) { // handle keyword selector. |
| 264 | for (unsigned i = 0; i != NumArgs; ++i) |
| 265 | ID.AddPointer(ArgTys[i]); |
| 266 | } else // handle unary selector. |
| 267 | ID.AddPointer(ArgTys[0]); |
| 268 | } |
| 269 | void Profile(llvm::FoldingSetNodeID &ID) { |
| 270 | Profile(ID, keyword_begin(), NumArgs); |
| 271 | } |
| 272 | }; |
Chris Lattner | 48ed6f8 | 2007-10-05 20:15:24 +0000 | [diff] [blame^] | 273 | } // end namespace clang. |
Steve Naroff | f9e80db | 2007-10-05 18:42:47 +0000 | [diff] [blame] | 274 | |
| 275 | unsigned Selector::getNumArgs() const { |
| 276 | unsigned IIF = getIdentifierInfoFlag(); |
| 277 | if (IIF == ZeroArg) |
| 278 | return 0; |
| 279 | if (IIF == OneArg) |
| 280 | return 1; |
| 281 | // We point to a MultiKeywordSelector (pointer doesn't contain any flags). |
| 282 | MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr); |
| 283 | return SI->getNumArgs(); |
| 284 | } |
| 285 | |
| 286 | IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) { |
| 287 | IdentifierInfo *II = getAsIdentifierInfo(); |
| 288 | if (II) { |
| 289 | assert(((argIndex == 0) || (argIndex == 1)) && "illegal keyword index"); |
| 290 | return II; |
| 291 | } |
| 292 | // We point to a MultiKeywordSelector (pointer doesn't contain any flags). |
| 293 | MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr); |
| 294 | return SI->getIdentifierInfoForSlot(argIndex); |
| 295 | } |
| 296 | |
| 297 | char *MultiKeywordSelector::getName(llvm::SmallVectorImpl<char> &methodName) { |
| 298 | methodName[0] = '\0'; |
| 299 | keyword_iterator KeyIter = keyword_begin(); |
| 300 | for (unsigned int i = 0; i < NumArgs; i++) { |
| 301 | if (KeyIter[i]) { |
| 302 | unsigned KeyLen = KeyIter[i]->getLength(); |
| 303 | methodName.append(KeyIter[i]->getName(), KeyIter[i]->getName()+KeyLen); |
| 304 | } |
| 305 | methodName.push_back(':'); |
| 306 | } |
| 307 | methodName.push_back('\0'); |
| 308 | return &methodName[0]; |
| 309 | } |
| 310 | |
| 311 | char *Selector::getName(llvm::SmallVectorImpl<char> &methodName) { |
| 312 | methodName[0] = '\0'; |
| 313 | IdentifierInfo *II = getAsIdentifierInfo(); |
| 314 | if (II) { |
| 315 | unsigned NameLen = II->getLength(); |
| 316 | methodName.append(II->getName(), II->getName()+NameLen); |
| 317 | if (getNumArgs() == 1) |
| 318 | methodName.push_back(':'); |
| 319 | methodName.push_back('\0'); |
| 320 | } else { // We have a multiple keyword selector (no embedded flags). |
| 321 | MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr); |
| 322 | SI->getName(methodName); |
| 323 | } |
| 324 | return &methodName[0]; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | Selector SelectorTable::getKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) |
| 329 | { |
| 330 | llvm::FoldingSet<MultiKeywordSelector> *SelTab; |
| 331 | |
| 332 | SelTab = static_cast<llvm::FoldingSet<MultiKeywordSelector> *>(Impl); |
| 333 | |
| 334 | // Unique selector, to guarantee there is one per name. |
| 335 | llvm::FoldingSetNodeID ID; |
| 336 | MultiKeywordSelector::Profile(ID, IIV, nKeys); |
| 337 | |
| 338 | void *InsertPos = 0; |
| 339 | if (MultiKeywordSelector *SI = SelTab->FindNodeOrInsertPos(ID, InsertPos)) { |
| 340 | return Selector(SI); |
| 341 | } |
| 342 | // MultiKeywordSelector objects are not allocated with new because they have a |
| 343 | // variable size array (for parameter types) at the end of them. |
| 344 | MultiKeywordSelector *SI = |
| 345 | (MultiKeywordSelector*)malloc(sizeof(MultiKeywordSelector) + |
| 346 | nKeys*sizeof(IdentifierInfo *)); |
| 347 | new (SI) MultiKeywordSelector(nKeys, IIV); |
| 348 | SelTab->InsertNode(SI, InsertPos); |
| 349 | return Selector(SI); |
| 350 | } |
| 351 | |
| 352 | Selector SelectorTable::getUnarySelector(IdentifierInfo *ID) { |
| 353 | return Selector(ID, 1); |
| 354 | } |
| 355 | |
| 356 | Selector SelectorTable::getNullarySelector(IdentifierInfo *ID) { |
| 357 | return Selector(ID, 0); |
| 358 | } |
| 359 | |
| 360 | SelectorTable::SelectorTable() { |
| 361 | Impl = new llvm::FoldingSet<MultiKeywordSelector>; |
| 362 | } |
| 363 | |
| 364 | SelectorTable::~SelectorTable() { |
| 365 | delete static_cast<llvm::FoldingSet<MultiKeywordSelector> *>(Impl); |
| 366 | } |
| 367 | |
| 368 | |