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