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 /// \brief 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 KEYOPENCL = 0x200, 112 KEYC11 = 0x400, 113 KEYARC = 0x800, 114 KEYNOMS18 = 0x01000, 115 KEYNOOPENCL = 0x02000, 116 WCHARSUPPORT = 0x04000, 117 HALFSUPPORT = 0x08000, 118 CHAR8SUPPORT = 0x10000, 119 KEYCONCEPTS = 0x20000, 120 KEYOBJC2 = 0x40000, 121 KEYZVECTOR = 0x80000, 122 KEYCOROUTINES = 0x100000, 123 KEYMODULES = 0x200000, 124 KEYCXX2A = 0x400000, 125 KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX2A, 126 KEYALL = (0x7fffff & ~KEYNOMS18 & 127 ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude. 128 }; 129 130 /// \brief 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 /// \brief 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.OpenCL && (Flags & KEYOPENCL)) return KS_Enabled; 158 if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled; 159 if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled; 160 // We treat bridge casts as objective-C keywords so we can warn on them 161 // in non-arc mode. 162 if (LangOpts.ObjC2 && (Flags & KEYARC)) return KS_Enabled; 163 if (LangOpts.ObjC2 && (Flags & KEYOBJC2)) return KS_Enabled; 164 if (LangOpts.ConceptsTS && (Flags & KEYCONCEPTS)) return KS_Enabled; 165 if (LangOpts.CoroutinesTS && (Flags & KEYCOROUTINES)) return KS_Enabled; 166 if (LangOpts.ModulesTS && (Flags & KEYMODULES)) return KS_Enabled; 167 if (LangOpts.CPlusPlus && (Flags & KEYALLCXX)) return KS_Future; 168 return KS_Disabled; 169 } 170 171 /// AddKeyword - This method is used to associate a token ID with specific 172 /// identifiers because they are language keywords. This causes the lexer to 173 /// automatically map matching identifiers to specialized token codes. 174 static void AddKeyword(StringRef Keyword, 175 tok::TokenKind TokenCode, unsigned Flags, 176 const LangOptions &LangOpts, IdentifierTable &Table) { 177 KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags); 178 179 // Don't add this keyword under MSVCCompat. 180 if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) && 181 !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)) 182 return; 183 184 // Don't add this keyword under OpenCL. 185 if (LangOpts.OpenCL && (Flags & KEYNOOPENCL)) 186 return; 187 188 // Don't add this keyword if disabled in this language. 189 if (AddResult == KS_Disabled) return; 190 191 IdentifierInfo &Info = 192 Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode); 193 Info.setIsExtensionToken(AddResult == KS_Extension); 194 Info.setIsFutureCompatKeyword(AddResult == KS_Future); 195 } 196 197 /// AddCXXOperatorKeyword - Register a C++ operator keyword alternative 198 /// representations. 199 static void AddCXXOperatorKeyword(StringRef Keyword, 200 tok::TokenKind TokenCode, 201 IdentifierTable &Table) { 202 IdentifierInfo &Info = Table.get(Keyword, TokenCode); 203 Info.setIsCPlusPlusOperatorKeyword(); 204 } 205 206 /// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector" 207 /// or "property". 208 static void AddObjCKeyword(StringRef Name, 209 tok::ObjCKeywordKind ObjCID, 210 IdentifierTable &Table) { 211 Table.get(Name).setObjCKeywordID(ObjCID); 212 } 213 214 /// AddKeywords - Add all keywords to the symbol table. 215 /// 216 void IdentifierTable::AddKeywords(const LangOptions &LangOpts) { 217 // Add keywords and tokens for the current language. 218 #define KEYWORD(NAME, FLAGS) \ 219 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \ 220 FLAGS, LangOpts, *this); 221 #define ALIAS(NAME, TOK, FLAGS) \ 222 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \ 223 FLAGS, LangOpts, *this); 224 #define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \ 225 if (LangOpts.CXXOperatorNames) \ 226 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this); 227 #define OBJC1_AT_KEYWORD(NAME) \ 228 if (LangOpts.ObjC1) \ 229 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this); 230 #define OBJC2_AT_KEYWORD(NAME) \ 231 if (LangOpts.ObjC2) \ 232 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this); 233 #define TESTING_KEYWORD(NAME, FLAGS) 234 #include "clang/Basic/TokenKinds.def" 235 236 if (LangOpts.ParseUnknownAnytype) 237 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL, 238 LangOpts, *this); 239 240 if (LangOpts.DeclSpecKeyword) 241 AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this); 242 243 // Add the '_experimental_modules_import' contextual keyword. 244 get("import").setModulesImport(true); 245 } 246 247 /// \brief Checks if the specified token kind represents a keyword in the 248 /// specified language. 249 /// \returns Status of the keyword in the language. 250 static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts, 251 tok::TokenKind K) { 252 switch (K) { 253 #define KEYWORD(NAME, FLAGS) \ 254 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS); 255 #include "clang/Basic/TokenKinds.def" 256 default: return KS_Disabled; 257 } 258 } 259 260 /// \brief Returns true if the identifier represents a keyword in the 261 /// specified language. 262 bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const { 263 switch (getTokenKwStatus(LangOpts, getTokenID())) { 264 case KS_Enabled: 265 case KS_Extension: 266 return true; 267 default: 268 return false; 269 } 270 } 271 272 /// \brief Returns true if the identifier represents a C++ keyword in the 273 /// specified language. 274 bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const { 275 if (!LangOpts.CPlusPlus || !isKeyword(LangOpts)) 276 return false; 277 // This is a C++ keyword if this identifier is not a keyword when checked 278 // using LangOptions without C++ support. 279 LangOptions LangOptsNoCPP = LangOpts; 280 LangOptsNoCPP.CPlusPlus = false; 281 LangOptsNoCPP.CPlusPlus11 = false; 282 LangOptsNoCPP.CPlusPlus2a = false; 283 return !isKeyword(LangOptsNoCPP); 284 } 285 286 tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const { 287 // We use a perfect hash function here involving the length of the keyword, 288 // the first and third character. For preprocessor ID's there are no 289 // collisions (if there were, the switch below would complain about duplicate 290 // case values). Note that this depends on 'if' being null terminated. 291 292 #define HASH(LEN, FIRST, THIRD) \ 293 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31) 294 #define CASE(LEN, FIRST, THIRD, NAME) \ 295 case HASH(LEN, FIRST, THIRD): \ 296 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME 297 298 unsigned Len = getLength(); 299 if (Len < 2) return tok::pp_not_keyword; 300 const char *Name = getNameStart(); 301 switch (HASH(Len, Name[0], Name[2])) { 302 default: return tok::pp_not_keyword; 303 CASE( 2, 'i', '\0', if); 304 CASE( 4, 'e', 'i', elif); 305 CASE( 4, 'e', 's', else); 306 CASE( 4, 'l', 'n', line); 307 CASE( 4, 's', 'c', sccs); 308 CASE( 5, 'e', 'd', endif); 309 CASE( 5, 'e', 'r', error); 310 CASE( 5, 'i', 'e', ident); 311 CASE( 5, 'i', 'd', ifdef); 312 CASE( 5, 'u', 'd', undef); 313 314 CASE( 6, 'a', 's', assert); 315 CASE( 6, 'd', 'f', define); 316 CASE( 6, 'i', 'n', ifndef); 317 CASE( 6, 'i', 'p', import); 318 CASE( 6, 'p', 'a', pragma); 319 320 CASE( 7, 'd', 'f', defined); 321 CASE( 7, 'i', 'c', include); 322 CASE( 7, 'w', 'r', warning); 323 324 CASE( 8, 'u', 'a', unassert); 325 CASE(12, 'i', 'c', include_next); 326 327 CASE(14, '_', 'p', __public_macro); 328 329 CASE(15, '_', 'p', __private_macro); 330 331 CASE(16, '_', 'i', __include_macros); 332 #undef CASE 333 #undef HASH 334 } 335 } 336 337 //===----------------------------------------------------------------------===// 338 // Stats Implementation 339 //===----------------------------------------------------------------------===// 340 341 /// PrintStats - Print statistics about how well the identifier table is doing 342 /// at hashing identifiers. 343 void IdentifierTable::PrintStats() const { 344 unsigned NumBuckets = HashTable.getNumBuckets(); 345 unsigned NumIdentifiers = HashTable.getNumItems(); 346 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers; 347 unsigned AverageIdentifierSize = 0; 348 unsigned MaxIdentifierLength = 0; 349 350 // TODO: Figure out maximum times an identifier had to probe for -stats. 351 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator 352 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) { 353 unsigned IdLen = I->getKeyLength(); 354 AverageIdentifierSize += IdLen; 355 if (MaxIdentifierLength < IdLen) 356 MaxIdentifierLength = IdLen; 357 } 358 359 fprintf(stderr, "\n*** Identifier Table Stats:\n"); 360 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers); 361 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets); 362 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n", 363 NumIdentifiers/(double)NumBuckets); 364 fprintf(stderr, "Ave identifier length: %f\n", 365 (AverageIdentifierSize/(double)NumIdentifiers)); 366 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength); 367 368 // Compute statistics about the memory allocated for identifiers. 369 HashTable.getAllocator().PrintStats(); 370 } 371 372 //===----------------------------------------------------------------------===// 373 // SelectorTable Implementation 374 //===----------------------------------------------------------------------===// 375 376 unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) { 377 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr()); 378 } 379 380 namespace clang { 381 382 /// MultiKeywordSelector - One of these variable length records is kept for each 383 /// selector containing more than one keyword. We use a folding set 384 /// to unique aggregate names (keyword selectors in ObjC parlance). Access to 385 /// this class is provided strictly through Selector. 386 class MultiKeywordSelector 387 : public DeclarationNameExtra, public llvm::FoldingSetNode { 388 MultiKeywordSelector(unsigned nKeys) { 389 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys; 390 } 391 392 public: 393 // Constructor for keyword selectors. 394 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) { 395 assert((nKeys > 1) && "not a multi-keyword selector"); 396 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys; 397 398 // Fill in the trailing keyword array. 399 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1); 400 for (unsigned i = 0; i != nKeys; ++i) 401 KeyInfo[i] = IIV[i]; 402 } 403 404 // getName - Derive the full selector name and return it. 405 std::string getName() const; 406 407 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; } 408 409 using keyword_iterator = IdentifierInfo *const *; 410 411 keyword_iterator keyword_begin() const { 412 return reinterpret_cast<keyword_iterator>(this+1); 413 } 414 415 keyword_iterator keyword_end() const { 416 return keyword_begin()+getNumArgs(); 417 } 418 419 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const { 420 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index"); 421 return keyword_begin()[i]; 422 } 423 424 static void Profile(llvm::FoldingSetNodeID &ID, 425 keyword_iterator ArgTys, unsigned NumArgs) { 426 ID.AddInteger(NumArgs); 427 for (unsigned i = 0; i != NumArgs; ++i) 428 ID.AddPointer(ArgTys[i]); 429 } 430 431 void Profile(llvm::FoldingSetNodeID &ID) { 432 Profile(ID, keyword_begin(), getNumArgs()); 433 } 434 }; 435 436 } // namespace clang. 437 438 unsigned Selector::getNumArgs() const { 439 unsigned IIF = getIdentifierInfoFlag(); 440 if (IIF <= ZeroArg) 441 return 0; 442 if (IIF == OneArg) 443 return 1; 444 // We point to a MultiKeywordSelector. 445 MultiKeywordSelector *SI = getMultiKeywordSelector(); 446 return SI->getNumArgs(); 447 } 448 449 IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const { 450 if (getIdentifierInfoFlag() < MultiArg) { 451 assert(argIndex == 0 && "illegal keyword index"); 452 return getAsIdentifierInfo(); 453 } 454 455 // We point to a MultiKeywordSelector. 456 MultiKeywordSelector *SI = getMultiKeywordSelector(); 457 return SI->getIdentifierInfoForSlot(argIndex); 458 } 459 460 StringRef Selector::getNameForSlot(unsigned int argIndex) const { 461 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex); 462 return II? II->getName() : StringRef(); 463 } 464 465 std::string MultiKeywordSelector::getName() const { 466 SmallString<256> Str; 467 llvm::raw_svector_ostream OS(Str); 468 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) { 469 if (*I) 470 OS << (*I)->getName(); 471 OS << ':'; 472 } 473 474 return OS.str(); 475 } 476 477 std::string Selector::getAsString() const { 478 if (InfoPtr == 0) 479 return "<null selector>"; 480 481 if (getIdentifierInfoFlag() < MultiArg) { 482 IdentifierInfo *II = getAsIdentifierInfo(); 483 484 if (getNumArgs() == 0) { 485 assert(II && "If the number of arguments is 0 then II is guaranteed to " 486 "not be null."); 487 return II->getName(); 488 } 489 490 if (!II) 491 return ":"; 492 493 return II->getName().str() + ":"; 494 } 495 496 // We have a multiple keyword selector. 497 return getMultiKeywordSelector()->getName(); 498 } 499 500 void Selector::print(llvm::raw_ostream &OS) const { 501 OS << getAsString(); 502 } 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 size_t SelectorTable::getTotalMemory() const { 646 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl); 647 return SelTabImpl.Allocator.getTotalMemory(); 648 } 649 650 Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) { 651 if (nKeys < 2) 652 return Selector(IIV[0], nKeys); 653 654 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl); 655 656 // Unique selector, to guarantee there is one per name. 657 llvm::FoldingSetNodeID ID; 658 MultiKeywordSelector::Profile(ID, IIV, nKeys); 659 660 void *InsertPos = nullptr; 661 if (MultiKeywordSelector *SI = 662 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos)) 663 return Selector(SI); 664 665 // MultiKeywordSelector objects are not allocated with new because they have a 666 // variable size array (for parameter types) at the end of them. 667 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *); 668 MultiKeywordSelector *SI = 669 (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate( 670 Size, alignof(MultiKeywordSelector)); 671 new (SI) MultiKeywordSelector(nKeys, IIV); 672 SelTabImpl.Table.InsertNode(SI, InsertPos); 673 return Selector(SI); 674 } 675 676 SelectorTable::SelectorTable() { 677 Impl = new SelectorTableImpl(); 678 } 679 680 SelectorTable::~SelectorTable() { 681 delete &getSelectorTableImpl(Impl); 682 } 683 684 const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) { 685 switch (Operator) { 686 case OO_None: 687 case NUM_OVERLOADED_OPERATORS: 688 return nullptr; 689 690 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 691 case OO_##Name: return Spelling; 692 #include "clang/Basic/OperatorKinds.def" 693 } 694 695 llvm_unreachable("Invalid OverloadedOperatorKind!"); 696 } 697 698 StringRef clang::getNullabilitySpelling(NullabilityKind kind, 699 bool isContextSensitive) { 700 switch (kind) { 701 case NullabilityKind::NonNull: 702 return isContextSensitive ? "nonnull" : "_Nonnull"; 703 704 case NullabilityKind::Nullable: 705 return isContextSensitive ? "nullable" : "_Nullable"; 706 707 case NullabilityKind::Unspecified: 708 return isContextSensitive ? "null_unspecified" : "_Null_unspecified"; 709 } 710 llvm_unreachable("Unknown nullability kind."); 711 } 712