1 //===- IdentifierTable.cpp - Hash table for identifier lookup -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the IdentifierInfo, IdentifierVisitor, and 10 // IdentifierTable interfaces. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Basic/IdentifierTable.h" 15 #include "clang/Basic/CharInfo.h" 16 #include "clang/Basic/LangOptions.h" 17 #include "clang/Basic/OperatorKinds.h" 18 #include "clang/Basic/Specifiers.h" 19 #include "clang/Basic/TokenKinds.h" 20 #include "llvm/ADT/DenseMapInfo.h" 21 #include "llvm/ADT/FoldingSet.h" 22 #include "llvm/ADT/SmallString.h" 23 #include "llvm/ADT/StringMap.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/Support/Allocator.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include <cassert> 29 #include <cstdio> 30 #include <cstring> 31 #include <string> 32 33 using namespace clang; 34 35 //===----------------------------------------------------------------------===// 36 // IdentifierTable Implementation 37 //===----------------------------------------------------------------------===// 38 39 IdentifierIterator::~IdentifierIterator() = default; 40 41 IdentifierInfoLookup::~IdentifierInfoLookup() = default; 42 43 namespace { 44 45 /// A simple identifier lookup iterator that represents an 46 /// empty sequence of identifiers. 47 class EmptyLookupIterator : public IdentifierIterator 48 { 49 public: 50 StringRef Next() override { return StringRef(); } 51 }; 52 53 } // namespace 54 55 IdentifierIterator *IdentifierInfoLookup::getIdentifiers() { 56 return new EmptyLookupIterator(); 57 } 58 59 IdentifierTable::IdentifierTable(IdentifierInfoLookup *ExternalLookup) 60 : HashTable(8192), // Start with space for 8K identifiers. 61 ExternalLookup(ExternalLookup) {} 62 63 IdentifierTable::IdentifierTable(const LangOptions &LangOpts, 64 IdentifierInfoLookup *ExternalLookup) 65 : IdentifierTable(ExternalLookup) { 66 // Populate the identifier table with info about keywords for the current 67 // language. 68 AddKeywords(LangOpts); 69 } 70 71 //===----------------------------------------------------------------------===// 72 // Language Keyword Implementation 73 //===----------------------------------------------------------------------===// 74 75 // Constants for TokenKinds.def 76 namespace { 77 78 enum { 79 KEYC99 = 0x1, 80 KEYCXX = 0x2, 81 KEYCXX11 = 0x4, 82 KEYGNU = 0x8, 83 KEYMS = 0x10, 84 BOOLSUPPORT = 0x20, 85 KEYALTIVEC = 0x40, 86 KEYNOCXX = 0x80, 87 KEYBORLAND = 0x100, 88 KEYOPENCLC = 0x200, 89 KEYC11 = 0x400, 90 KEYNOMS18 = 0x800, 91 KEYNOOPENCL = 0x1000, 92 WCHARSUPPORT = 0x2000, 93 HALFSUPPORT = 0x4000, 94 CHAR8SUPPORT = 0x8000, 95 KEYCONCEPTS = 0x10000, 96 KEYOBJC = 0x20000, 97 KEYZVECTOR = 0x40000, 98 KEYCOROUTINES = 0x80000, 99 KEYMODULES = 0x100000, 100 KEYCXX2A = 0x200000, 101 KEYOPENCLCXX = 0x400000, 102 KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX2A, 103 KEYALL = (0xffffff & ~KEYNOMS18 & 104 ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude. 105 }; 106 107 /// How a keyword is treated in the selected standard. 108 enum KeywordStatus { 109 KS_Disabled, // Disabled 110 KS_Extension, // Is an extension 111 KS_Enabled, // Enabled 112 KS_Future // Is a keyword in future standard 113 }; 114 115 } // namespace 116 117 /// Translates flags as specified in TokenKinds.def into keyword status 118 /// in the given language standard. 119 static KeywordStatus getKeywordStatus(const LangOptions &LangOpts, 120 unsigned Flags) { 121 if (Flags == KEYALL) return KS_Enabled; 122 if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled; 123 if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled; 124 if (LangOpts.CPlusPlus2a && (Flags & KEYCXX2A)) return KS_Enabled; 125 if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled; 126 if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension; 127 if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension; 128 if (LangOpts.Borland && (Flags & KEYBORLAND)) return KS_Extension; 129 if (LangOpts.Bool && (Flags & BOOLSUPPORT)) return KS_Enabled; 130 if (LangOpts.Half && (Flags & HALFSUPPORT)) return KS_Enabled; 131 if (LangOpts.WChar && (Flags & WCHARSUPPORT)) return KS_Enabled; 132 if (LangOpts.Char8 && (Flags & CHAR8SUPPORT)) return KS_Enabled; 133 if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) return KS_Enabled; 134 if (LangOpts.ZVector && (Flags & KEYZVECTOR)) return KS_Enabled; 135 if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLC)) 136 return KS_Enabled; 137 if (LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLCXX)) return KS_Enabled; 138 if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled; 139 if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled; 140 // We treat bridge casts as objective-C keywords so we can warn on them 141 // in non-arc mode. 142 if (LangOpts.ObjC && (Flags & KEYOBJC)) return KS_Enabled; 143 if (LangOpts.ConceptsTS && (Flags & KEYCONCEPTS)) return KS_Enabled; 144 if (LangOpts.CoroutinesTS && (Flags & KEYCOROUTINES)) return KS_Enabled; 145 if (LangOpts.ModulesTS && (Flags & KEYMODULES)) return KS_Enabled; 146 if (LangOpts.CPlusPlus && (Flags & KEYALLCXX)) return KS_Future; 147 return KS_Disabled; 148 } 149 150 /// AddKeyword - This method is used to associate a token ID with specific 151 /// identifiers because they are language keywords. This causes the lexer to 152 /// automatically map matching identifiers to specialized token codes. 153 static void AddKeyword(StringRef Keyword, 154 tok::TokenKind TokenCode, unsigned Flags, 155 const LangOptions &LangOpts, IdentifierTable &Table) { 156 KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags); 157 158 // Don't add this keyword under MSVCCompat. 159 if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) && 160 !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)) 161 return; 162 163 // Don't add this keyword under OpenCL. 164 if (LangOpts.OpenCL && (Flags & KEYNOOPENCL)) 165 return; 166 167 // Don't add this keyword if disabled in this language. 168 if (AddResult == KS_Disabled) return; 169 170 IdentifierInfo &Info = 171 Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode); 172 Info.setIsExtensionToken(AddResult == KS_Extension); 173 Info.setIsFutureCompatKeyword(AddResult == KS_Future); 174 } 175 176 /// AddCXXOperatorKeyword - Register a C++ operator keyword alternative 177 /// representations. 178 static void AddCXXOperatorKeyword(StringRef Keyword, 179 tok::TokenKind TokenCode, 180 IdentifierTable &Table) { 181 IdentifierInfo &Info = Table.get(Keyword, TokenCode); 182 Info.setIsCPlusPlusOperatorKeyword(); 183 } 184 185 /// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector" 186 /// or "property". 187 static void AddObjCKeyword(StringRef Name, 188 tok::ObjCKeywordKind ObjCID, 189 IdentifierTable &Table) { 190 Table.get(Name).setObjCKeywordID(ObjCID); 191 } 192 193 /// AddKeywords - Add all keywords to the symbol table. 194 /// 195 void IdentifierTable::AddKeywords(const LangOptions &LangOpts) { 196 // Add keywords and tokens for the current language. 197 #define KEYWORD(NAME, FLAGS) \ 198 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \ 199 FLAGS, LangOpts, *this); 200 #define ALIAS(NAME, TOK, FLAGS) \ 201 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \ 202 FLAGS, LangOpts, *this); 203 #define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \ 204 if (LangOpts.CXXOperatorNames) \ 205 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this); 206 #define OBJC_AT_KEYWORD(NAME) \ 207 if (LangOpts.ObjC) \ 208 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this); 209 #define TESTING_KEYWORD(NAME, FLAGS) 210 #include "clang/Basic/TokenKinds.def" 211 212 if (LangOpts.ParseUnknownAnytype) 213 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL, 214 LangOpts, *this); 215 216 if (LangOpts.DeclSpecKeyword) 217 AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this); 218 219 // Add the '_experimental_modules_import' contextual keyword. 220 get("import").setModulesImport(true); 221 } 222 223 /// Checks if the specified token kind represents a keyword in the 224 /// specified language. 225 /// \returns Status of the keyword in the language. 226 static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts, 227 tok::TokenKind K) { 228 switch (K) { 229 #define KEYWORD(NAME, FLAGS) \ 230 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS); 231 #include "clang/Basic/TokenKinds.def" 232 default: return KS_Disabled; 233 } 234 } 235 236 /// Returns true if the identifier represents a keyword in the 237 /// specified language. 238 bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const { 239 switch (getTokenKwStatus(LangOpts, getTokenID())) { 240 case KS_Enabled: 241 case KS_Extension: 242 return true; 243 default: 244 return false; 245 } 246 } 247 248 /// Returns true if the identifier represents a C++ keyword in the 249 /// specified language. 250 bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const { 251 if (!LangOpts.CPlusPlus || !isKeyword(LangOpts)) 252 return false; 253 // This is a C++ keyword if this identifier is not a keyword when checked 254 // using LangOptions without C++ support. 255 LangOptions LangOptsNoCPP = LangOpts; 256 LangOptsNoCPP.CPlusPlus = false; 257 LangOptsNoCPP.CPlusPlus11 = false; 258 LangOptsNoCPP.CPlusPlus2a = false; 259 return !isKeyword(LangOptsNoCPP); 260 } 261 262 tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const { 263 // We use a perfect hash function here involving the length of the keyword, 264 // the first and third character. For preprocessor ID's there are no 265 // collisions (if there were, the switch below would complain about duplicate 266 // case values). Note that this depends on 'if' being null terminated. 267 268 #define HASH(LEN, FIRST, THIRD) \ 269 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31) 270 #define CASE(LEN, FIRST, THIRD, NAME) \ 271 case HASH(LEN, FIRST, THIRD): \ 272 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME 273 274 unsigned Len = getLength(); 275 if (Len < 2) return tok::pp_not_keyword; 276 const char *Name = getNameStart(); 277 switch (HASH(Len, Name[0], Name[2])) { 278 default: return tok::pp_not_keyword; 279 CASE( 2, 'i', '\0', if); 280 CASE( 4, 'e', 'i', elif); 281 CASE( 4, 'e', 's', else); 282 CASE( 4, 'l', 'n', line); 283 CASE( 4, 's', 'c', sccs); 284 CASE( 5, 'e', 'd', endif); 285 CASE( 5, 'e', 'r', error); 286 CASE( 5, 'i', 'e', ident); 287 CASE( 5, 'i', 'd', ifdef); 288 CASE( 5, 'u', 'd', undef); 289 290 CASE( 6, 'a', 's', assert); 291 CASE( 6, 'd', 'f', define); 292 CASE( 6, 'i', 'n', ifndef); 293 CASE( 6, 'i', 'p', import); 294 CASE( 6, 'p', 'a', pragma); 295 296 CASE( 7, 'd', 'f', defined); 297 CASE( 7, 'i', 'c', include); 298 CASE( 7, 'w', 'r', warning); 299 300 CASE( 8, 'u', 'a', unassert); 301 CASE(12, 'i', 'c', include_next); 302 303 CASE(14, '_', 'p', __public_macro); 304 305 CASE(15, '_', 'p', __private_macro); 306 307 CASE(16, '_', 'i', __include_macros); 308 #undef CASE 309 #undef HASH 310 } 311 } 312 313 //===----------------------------------------------------------------------===// 314 // Stats Implementation 315 //===----------------------------------------------------------------------===// 316 317 /// PrintStats - Print statistics about how well the identifier table is doing 318 /// at hashing identifiers. 319 void IdentifierTable::PrintStats() const { 320 unsigned NumBuckets = HashTable.getNumBuckets(); 321 unsigned NumIdentifiers = HashTable.getNumItems(); 322 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers; 323 unsigned AverageIdentifierSize = 0; 324 unsigned MaxIdentifierLength = 0; 325 326 // TODO: Figure out maximum times an identifier had to probe for -stats. 327 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator 328 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) { 329 unsigned IdLen = I->getKeyLength(); 330 AverageIdentifierSize += IdLen; 331 if (MaxIdentifierLength < IdLen) 332 MaxIdentifierLength = IdLen; 333 } 334 335 fprintf(stderr, "\n*** Identifier Table Stats:\n"); 336 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers); 337 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets); 338 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n", 339 NumIdentifiers/(double)NumBuckets); 340 fprintf(stderr, "Ave identifier length: %f\n", 341 (AverageIdentifierSize/(double)NumIdentifiers)); 342 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength); 343 344 // Compute statistics about the memory allocated for identifiers. 345 HashTable.getAllocator().PrintStats(); 346 } 347 348 //===----------------------------------------------------------------------===// 349 // SelectorTable Implementation 350 //===----------------------------------------------------------------------===// 351 352 unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) { 353 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr()); 354 } 355 356 namespace clang { 357 358 /// One of these variable length records is kept for each 359 /// selector containing more than one keyword. We use a folding set 360 /// to unique aggregate names (keyword selectors in ObjC parlance). Access to 361 /// this class is provided strictly through Selector. 362 class alignas(IdentifierInfoAlignment) MultiKeywordSelector 363 : public detail::DeclarationNameExtra, 364 public llvm::FoldingSetNode { 365 MultiKeywordSelector(unsigned nKeys) : DeclarationNameExtra(nKeys) {} 366 367 public: 368 // Constructor for keyword selectors. 369 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) 370 : DeclarationNameExtra(nKeys) { 371 assert((nKeys > 1) && "not a multi-keyword selector"); 372 373 // Fill in the trailing keyword array. 374 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this + 1); 375 for (unsigned i = 0; i != nKeys; ++i) 376 KeyInfo[i] = IIV[i]; 377 } 378 379 // getName - Derive the full selector name and return it. 380 std::string getName() const; 381 382 using DeclarationNameExtra::getNumArgs; 383 384 using keyword_iterator = IdentifierInfo *const *; 385 386 keyword_iterator keyword_begin() const { 387 return reinterpret_cast<keyword_iterator>(this + 1); 388 } 389 390 keyword_iterator keyword_end() const { 391 return keyword_begin() + getNumArgs(); 392 } 393 394 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const { 395 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index"); 396 return keyword_begin()[i]; 397 } 398 399 static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys, 400 unsigned NumArgs) { 401 ID.AddInteger(NumArgs); 402 for (unsigned i = 0; i != NumArgs; ++i) 403 ID.AddPointer(ArgTys[i]); 404 } 405 406 void Profile(llvm::FoldingSetNodeID &ID) { 407 Profile(ID, keyword_begin(), getNumArgs()); 408 } 409 }; 410 411 } // namespace clang. 412 413 unsigned Selector::getNumArgs() const { 414 unsigned IIF = getIdentifierInfoFlag(); 415 if (IIF <= ZeroArg) 416 return 0; 417 if (IIF == OneArg) 418 return 1; 419 // We point to a MultiKeywordSelector. 420 MultiKeywordSelector *SI = getMultiKeywordSelector(); 421 return SI->getNumArgs(); 422 } 423 424 IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const { 425 if (getIdentifierInfoFlag() < MultiArg) { 426 assert(argIndex == 0 && "illegal keyword index"); 427 return getAsIdentifierInfo(); 428 } 429 430 // We point to a MultiKeywordSelector. 431 MultiKeywordSelector *SI = getMultiKeywordSelector(); 432 return SI->getIdentifierInfoForSlot(argIndex); 433 } 434 435 StringRef Selector::getNameForSlot(unsigned int argIndex) const { 436 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex); 437 return II ? II->getName() : StringRef(); 438 } 439 440 std::string MultiKeywordSelector::getName() const { 441 SmallString<256> Str; 442 llvm::raw_svector_ostream OS(Str); 443 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) { 444 if (*I) 445 OS << (*I)->getName(); 446 OS << ':'; 447 } 448 449 return OS.str(); 450 } 451 452 std::string Selector::getAsString() const { 453 if (InfoPtr == 0) 454 return "<null selector>"; 455 456 if (getIdentifierInfoFlag() < MultiArg) { 457 IdentifierInfo *II = getAsIdentifierInfo(); 458 459 if (getNumArgs() == 0) { 460 assert(II && "If the number of arguments is 0 then II is guaranteed to " 461 "not be null."); 462 return II->getName(); 463 } 464 465 if (!II) 466 return ":"; 467 468 return II->getName().str() + ":"; 469 } 470 471 // We have a multiple keyword selector. 472 return getMultiKeywordSelector()->getName(); 473 } 474 475 void Selector::print(llvm::raw_ostream &OS) const { 476 OS << getAsString(); 477 } 478 479 LLVM_DUMP_METHOD void Selector::dump() const { print(llvm::errs()); } 480 481 /// Interpreting the given string using the normal CamelCase 482 /// conventions, determine whether the given string starts with the 483 /// given "word", which is assumed to end in a lowercase letter. 484 static bool startsWithWord(StringRef name, StringRef word) { 485 if (name.size() < word.size()) return false; 486 return ((name.size() == word.size() || !isLowercase(name[word.size()])) && 487 name.startswith(word)); 488 } 489 490 ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) { 491 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0); 492 if (!first) return OMF_None; 493 494 StringRef name = first->getName(); 495 if (sel.isUnarySelector()) { 496 if (name == "autorelease") return OMF_autorelease; 497 if (name == "dealloc") return OMF_dealloc; 498 if (name == "finalize") return OMF_finalize; 499 if (name == "release") return OMF_release; 500 if (name == "retain") return OMF_retain; 501 if (name == "retainCount") return OMF_retainCount; 502 if (name == "self") return OMF_self; 503 if (name == "initialize") return OMF_initialize; 504 } 505 506 if (name == "performSelector" || name == "performSelectorInBackground" || 507 name == "performSelectorOnMainThread") 508 return OMF_performSelector; 509 510 // The other method families may begin with a prefix of underscores. 511 while (!name.empty() && name.front() == '_') 512 name = name.substr(1); 513 514 if (name.empty()) return OMF_None; 515 switch (name.front()) { 516 case 'a': 517 if (startsWithWord(name, "alloc")) return OMF_alloc; 518 break; 519 case 'c': 520 if (startsWithWord(name, "copy")) return OMF_copy; 521 break; 522 case 'i': 523 if (startsWithWord(name, "init")) return OMF_init; 524 break; 525 case 'm': 526 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy; 527 break; 528 case 'n': 529 if (startsWithWord(name, "new")) return OMF_new; 530 break; 531 default: 532 break; 533 } 534 535 return OMF_None; 536 } 537 538 ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) { 539 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0); 540 if (!first) return OIT_None; 541 542 StringRef name = first->getName(); 543 544 if (name.empty()) return OIT_None; 545 switch (name.front()) { 546 case 'a': 547 if (startsWithWord(name, "array")) return OIT_Array; 548 break; 549 case 'd': 550 if (startsWithWord(name, "default")) return OIT_ReturnsSelf; 551 if (startsWithWord(name, "dictionary")) return OIT_Dictionary; 552 break; 553 case 's': 554 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf; 555 if (startsWithWord(name, "standard")) return OIT_Singleton; 556 break; 557 case 'i': 558 if (startsWithWord(name, "init")) return OIT_Init; 559 break; 560 default: 561 break; 562 } 563 return OIT_None; 564 } 565 566 ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) { 567 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0); 568 if (!first) return SFF_None; 569 570 StringRef name = first->getName(); 571 572 switch (name.front()) { 573 case 'a': 574 if (name == "appendFormat") return SFF_NSString; 575 break; 576 577 case 'i': 578 if (name == "initWithFormat") return SFF_NSString; 579 break; 580 581 case 'l': 582 if (name == "localizedStringWithFormat") return SFF_NSString; 583 break; 584 585 case 's': 586 if (name == "stringByAppendingFormat" || 587 name == "stringWithFormat") return SFF_NSString; 588 break; 589 } 590 return SFF_None; 591 } 592 593 namespace { 594 595 struct SelectorTableImpl { 596 llvm::FoldingSet<MultiKeywordSelector> Table; 597 llvm::BumpPtrAllocator Allocator; 598 }; 599 600 } // namespace 601 602 static SelectorTableImpl &getSelectorTableImpl(void *P) { 603 return *static_cast<SelectorTableImpl*>(P); 604 } 605 606 SmallString<64> 607 SelectorTable::constructSetterName(StringRef Name) { 608 SmallString<64> SetterName("set"); 609 SetterName += Name; 610 SetterName[3] = toUppercase(SetterName[3]); 611 return SetterName; 612 } 613 614 Selector 615 SelectorTable::constructSetterSelector(IdentifierTable &Idents, 616 SelectorTable &SelTable, 617 const IdentifierInfo *Name) { 618 IdentifierInfo *SetterName = 619 &Idents.get(constructSetterName(Name->getName())); 620 return SelTable.getUnarySelector(SetterName); 621 } 622 623 std::string SelectorTable::getPropertyNameFromSetterSelector(Selector Sel) { 624 StringRef Name = Sel.getNameForSlot(0); 625 assert(Name.startswith("set") && "invalid setter name"); 626 return (Twine(toLowercase(Name[3])) + Name.drop_front(4)).str(); 627 } 628 629 size_t SelectorTable::getTotalMemory() const { 630 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl); 631 return SelTabImpl.Allocator.getTotalMemory(); 632 } 633 634 Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) { 635 if (nKeys < 2) 636 return Selector(IIV[0], nKeys); 637 638 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl); 639 640 // Unique selector, to guarantee there is one per name. 641 llvm::FoldingSetNodeID ID; 642 MultiKeywordSelector::Profile(ID, IIV, nKeys); 643 644 void *InsertPos = nullptr; 645 if (MultiKeywordSelector *SI = 646 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos)) 647 return Selector(SI); 648 649 // MultiKeywordSelector objects are not allocated with new because they have a 650 // variable size array (for parameter types) at the end of them. 651 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *); 652 MultiKeywordSelector *SI = 653 (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate( 654 Size, alignof(MultiKeywordSelector)); 655 new (SI) MultiKeywordSelector(nKeys, IIV); 656 SelTabImpl.Table.InsertNode(SI, InsertPos); 657 return Selector(SI); 658 } 659 660 SelectorTable::SelectorTable() { 661 Impl = new SelectorTableImpl(); 662 } 663 664 SelectorTable::~SelectorTable() { 665 delete &getSelectorTableImpl(Impl); 666 } 667 668 const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) { 669 switch (Operator) { 670 case OO_None: 671 case NUM_OVERLOADED_OPERATORS: 672 return nullptr; 673 674 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 675 case OO_##Name: return Spelling; 676 #include "clang/Basic/OperatorKinds.def" 677 } 678 679 llvm_unreachable("Invalid OverloadedOperatorKind!"); 680 } 681 682 StringRef clang::getNullabilitySpelling(NullabilityKind kind, 683 bool isContextSensitive) { 684 switch (kind) { 685 case NullabilityKind::NonNull: 686 return isContextSensitive ? "nonnull" : "_Nonnull"; 687 688 case NullabilityKind::Nullable: 689 return isContextSensitive ? "nullable" : "_Nullable"; 690 691 case NullabilityKind::Unspecified: 692 return isContextSensitive ? "null_unspecified" : "_Null_unspecified"; 693 } 694 llvm_unreachable("Unknown nullability kind."); 695 } 696