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 KEYCONCEPTS = 0x10000, 119 KEYOBJC2 = 0x20000, 120 KEYZVECTOR = 0x40000, 121 KEYCOROUTINES = 0x80000, 122 KEYMODULES = 0x100000, 123 KEYCXX2A = 0x200000, 124 KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX2A, 125 KEYALL = (0x3fffff & ~KEYNOMS18 & 126 ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude. 127 }; 128 129 /// \brief How a keyword is treated in the selected standard. 130 enum KeywordStatus { 131 KS_Disabled, // Disabled 132 KS_Extension, // Is an extension 133 KS_Enabled, // Enabled 134 KS_Future // Is a keyword in future standard 135 }; 136 137 } // namespace 138 139 /// \brief Translates flags as specified in TokenKinds.def into keyword status 140 /// in the given language standard. 141 static KeywordStatus getKeywordStatus(const LangOptions &LangOpts, 142 unsigned Flags) { 143 if (Flags == KEYALL) return KS_Enabled; 144 if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled; 145 if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled; 146 if (LangOpts.CPlusPlus2a && (Flags & KEYCXX2A)) return KS_Enabled; 147 if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled; 148 if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension; 149 if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension; 150 if (LangOpts.Borland && (Flags & KEYBORLAND)) return KS_Extension; 151 if (LangOpts.Bool && (Flags & BOOLSUPPORT)) return KS_Enabled; 152 if (LangOpts.Half && (Flags & HALFSUPPORT)) return KS_Enabled; 153 if (LangOpts.WChar && (Flags & WCHARSUPPORT)) return KS_Enabled; 154 if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) return KS_Enabled; 155 if (LangOpts.OpenCL && (Flags & KEYOPENCL)) return KS_Enabled; 156 if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled; 157 if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled; 158 // We treat bridge casts as objective-C keywords so we can warn on them 159 // in non-arc mode. 160 if (LangOpts.ObjC2 && (Flags & KEYARC)) return KS_Enabled; 161 if (LangOpts.ObjC2 && (Flags & KEYOBJC2)) return KS_Enabled; 162 if (LangOpts.ConceptsTS && (Flags & KEYCONCEPTS)) return KS_Enabled; 163 if (LangOpts.CoroutinesTS && (Flags & KEYCOROUTINES)) return KS_Enabled; 164 if (LangOpts.ModulesTS && (Flags & KEYMODULES)) return KS_Enabled; 165 if (LangOpts.CPlusPlus && (Flags & KEYALLCXX)) return KS_Future; 166 return KS_Disabled; 167 } 168 169 /// AddKeyword - This method is used to associate a token ID with specific 170 /// identifiers because they are language keywords. This causes the lexer to 171 /// automatically map matching identifiers to specialized token codes. 172 static void AddKeyword(StringRef Keyword, 173 tok::TokenKind TokenCode, unsigned Flags, 174 const LangOptions &LangOpts, IdentifierTable &Table) { 175 KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags); 176 177 // Don't add this keyword under MSVCCompat. 178 if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) && 179 !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)) 180 return; 181 182 // Don't add this keyword under OpenCL. 183 if (LangOpts.OpenCL && (Flags & KEYNOOPENCL)) 184 return; 185 186 // Don't add this keyword if disabled in this language. 187 if (AddResult == KS_Disabled) return; 188 189 IdentifierInfo &Info = 190 Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode); 191 Info.setIsExtensionToken(AddResult == KS_Extension); 192 Info.setIsFutureCompatKeyword(AddResult == KS_Future); 193 } 194 195 /// AddCXXOperatorKeyword - Register a C++ operator keyword alternative 196 /// representations. 197 static void AddCXXOperatorKeyword(StringRef Keyword, 198 tok::TokenKind TokenCode, 199 IdentifierTable &Table) { 200 IdentifierInfo &Info = Table.get(Keyword, TokenCode); 201 Info.setIsCPlusPlusOperatorKeyword(); 202 } 203 204 /// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector" 205 /// or "property". 206 static void AddObjCKeyword(StringRef Name, 207 tok::ObjCKeywordKind ObjCID, 208 IdentifierTable &Table) { 209 Table.get(Name).setObjCKeywordID(ObjCID); 210 } 211 212 /// AddKeywords - Add all keywords to the symbol table. 213 /// 214 void IdentifierTable::AddKeywords(const LangOptions &LangOpts) { 215 // Add keywords and tokens for the current language. 216 #define KEYWORD(NAME, FLAGS) \ 217 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \ 218 FLAGS, LangOpts, *this); 219 #define ALIAS(NAME, TOK, FLAGS) \ 220 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \ 221 FLAGS, LangOpts, *this); 222 #define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \ 223 if (LangOpts.CXXOperatorNames) \ 224 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this); 225 #define OBJC1_AT_KEYWORD(NAME) \ 226 if (LangOpts.ObjC1) \ 227 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this); 228 #define OBJC2_AT_KEYWORD(NAME) \ 229 if (LangOpts.ObjC2) \ 230 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this); 231 #define TESTING_KEYWORD(NAME, FLAGS) 232 #include "clang/Basic/TokenKinds.def" 233 234 if (LangOpts.ParseUnknownAnytype) 235 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL, 236 LangOpts, *this); 237 238 if (LangOpts.DeclSpecKeyword) 239 AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this); 240 241 // Add the '_experimental_modules_import' contextual keyword. 242 get("import").setModulesImport(true); 243 } 244 245 /// \brief Checks if the specified token kind represents a keyword in the 246 /// specified language. 247 /// \returns Status of the keyword in the language. 248 static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts, 249 tok::TokenKind K) { 250 switch (K) { 251 #define KEYWORD(NAME, FLAGS) \ 252 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS); 253 #include "clang/Basic/TokenKinds.def" 254 default: return KS_Disabled; 255 } 256 } 257 258 /// \brief Returns true if the identifier represents a keyword in the 259 /// specified language. 260 bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const { 261 switch (getTokenKwStatus(LangOpts, getTokenID())) { 262 case KS_Enabled: 263 case KS_Extension: 264 return true; 265 default: 266 return false; 267 } 268 } 269 270 /// \brief Returns true if the identifier represents a C++ keyword in the 271 /// specified language. 272 bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const { 273 if (!LangOpts.CPlusPlus || !isKeyword(LangOpts)) 274 return false; 275 // This is a C++ keyword if this identifier is not a keyword when checked 276 // using LangOptions without C++ support. 277 LangOptions LangOptsNoCPP = LangOpts; 278 LangOptsNoCPP.CPlusPlus = false; 279 LangOptsNoCPP.CPlusPlus11 = false; 280 LangOptsNoCPP.CPlusPlus2a = false; 281 return !isKeyword(LangOptsNoCPP); 282 } 283 284 tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const { 285 // We use a perfect hash function here involving the length of the keyword, 286 // the first and third character. For preprocessor ID's there are no 287 // collisions (if there were, the switch below would complain about duplicate 288 // case values). Note that this depends on 'if' being null terminated. 289 290 #define HASH(LEN, FIRST, THIRD) \ 291 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31) 292 #define CASE(LEN, FIRST, THIRD, NAME) \ 293 case HASH(LEN, FIRST, THIRD): \ 294 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME 295 296 unsigned Len = getLength(); 297 if (Len < 2) return tok::pp_not_keyword; 298 const char *Name = getNameStart(); 299 switch (HASH(Len, Name[0], Name[2])) { 300 default: return tok::pp_not_keyword; 301 CASE( 2, 'i', '\0', if); 302 CASE( 4, 'e', 'i', elif); 303 CASE( 4, 'e', 's', else); 304 CASE( 4, 'l', 'n', line); 305 CASE( 4, 's', 'c', sccs); 306 CASE( 5, 'e', 'd', endif); 307 CASE( 5, 'e', 'r', error); 308 CASE( 5, 'i', 'e', ident); 309 CASE( 5, 'i', 'd', ifdef); 310 CASE( 5, 'u', 'd', undef); 311 312 CASE( 6, 'a', 's', assert); 313 CASE( 6, 'd', 'f', define); 314 CASE( 6, 'i', 'n', ifndef); 315 CASE( 6, 'i', 'p', import); 316 CASE( 6, 'p', 'a', pragma); 317 318 CASE( 7, 'd', 'f', defined); 319 CASE( 7, 'i', 'c', include); 320 CASE( 7, 'w', 'r', warning); 321 322 CASE( 8, 'u', 'a', unassert); 323 CASE(12, 'i', 'c', include_next); 324 325 CASE(14, '_', 'p', __public_macro); 326 327 CASE(15, '_', 'p', __private_macro); 328 329 CASE(16, '_', 'i', __include_macros); 330 #undef CASE 331 #undef HASH 332 } 333 } 334 335 //===----------------------------------------------------------------------===// 336 // Stats Implementation 337 //===----------------------------------------------------------------------===// 338 339 /// PrintStats - Print statistics about how well the identifier table is doing 340 /// at hashing identifiers. 341 void IdentifierTable::PrintStats() const { 342 unsigned NumBuckets = HashTable.getNumBuckets(); 343 unsigned NumIdentifiers = HashTable.getNumItems(); 344 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers; 345 unsigned AverageIdentifierSize = 0; 346 unsigned MaxIdentifierLength = 0; 347 348 // TODO: Figure out maximum times an identifier had to probe for -stats. 349 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator 350 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) { 351 unsigned IdLen = I->getKeyLength(); 352 AverageIdentifierSize += IdLen; 353 if (MaxIdentifierLength < IdLen) 354 MaxIdentifierLength = IdLen; 355 } 356 357 fprintf(stderr, "\n*** Identifier Table Stats:\n"); 358 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers); 359 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets); 360 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n", 361 NumIdentifiers/(double)NumBuckets); 362 fprintf(stderr, "Ave identifier length: %f\n", 363 (AverageIdentifierSize/(double)NumIdentifiers)); 364 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength); 365 366 // Compute statistics about the memory allocated for identifiers. 367 HashTable.getAllocator().PrintStats(); 368 } 369 370 //===----------------------------------------------------------------------===// 371 // SelectorTable Implementation 372 //===----------------------------------------------------------------------===// 373 374 unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) { 375 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr()); 376 } 377 378 namespace clang { 379 380 /// MultiKeywordSelector - One of these variable length records is kept for each 381 /// selector containing more than one keyword. We use a folding set 382 /// to unique aggregate names (keyword selectors in ObjC parlance). Access to 383 /// this class is provided strictly through Selector. 384 class MultiKeywordSelector 385 : public DeclarationNameExtra, public llvm::FoldingSetNode { 386 MultiKeywordSelector(unsigned nKeys) { 387 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys; 388 } 389 390 public: 391 // Constructor for keyword selectors. 392 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) { 393 assert((nKeys > 1) && "not a multi-keyword selector"); 394 ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys; 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 unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; } 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, 423 keyword_iterator ArgTys, 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 /// Interpreting the given string using the normal CamelCase 503 /// conventions, determine whether the given string starts with the 504 /// given "word", which is assumed to end in a lowercase letter. 505 static bool startsWithWord(StringRef name, StringRef word) { 506 if (name.size() < word.size()) return false; 507 return ((name.size() == word.size() || !isLowercase(name[word.size()])) && 508 name.startswith(word)); 509 } 510 511 ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) { 512 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0); 513 if (!first) return OMF_None; 514 515 StringRef name = first->getName(); 516 if (sel.isUnarySelector()) { 517 if (name == "autorelease") return OMF_autorelease; 518 if (name == "dealloc") return OMF_dealloc; 519 if (name == "finalize") return OMF_finalize; 520 if (name == "release") return OMF_release; 521 if (name == "retain") return OMF_retain; 522 if (name == "retainCount") return OMF_retainCount; 523 if (name == "self") return OMF_self; 524 if (name == "initialize") return OMF_initialize; 525 } 526 527 if (name == "performSelector" || name == "performSelectorInBackground" || 528 name == "performSelectorOnMainThread") 529 return OMF_performSelector; 530 531 // The other method families may begin with a prefix of underscores. 532 while (!name.empty() && name.front() == '_') 533 name = name.substr(1); 534 535 if (name.empty()) return OMF_None; 536 switch (name.front()) { 537 case 'a': 538 if (startsWithWord(name, "alloc")) return OMF_alloc; 539 break; 540 case 'c': 541 if (startsWithWord(name, "copy")) return OMF_copy; 542 break; 543 case 'i': 544 if (startsWithWord(name, "init")) return OMF_init; 545 break; 546 case 'm': 547 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy; 548 break; 549 case 'n': 550 if (startsWithWord(name, "new")) return OMF_new; 551 break; 552 default: 553 break; 554 } 555 556 return OMF_None; 557 } 558 559 ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) { 560 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0); 561 if (!first) return OIT_None; 562 563 StringRef name = first->getName(); 564 565 if (name.empty()) return OIT_None; 566 switch (name.front()) { 567 case 'a': 568 if (startsWithWord(name, "array")) return OIT_Array; 569 break; 570 case 'd': 571 if (startsWithWord(name, "default")) return OIT_ReturnsSelf; 572 if (startsWithWord(name, "dictionary")) return OIT_Dictionary; 573 break; 574 case 's': 575 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf; 576 if (startsWithWord(name, "standard")) return OIT_Singleton; 577 break; 578 case 'i': 579 if (startsWithWord(name, "init")) return OIT_Init; 580 default: 581 break; 582 } 583 return OIT_None; 584 } 585 586 ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) { 587 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0); 588 if (!first) return SFF_None; 589 590 StringRef name = first->getName(); 591 592 switch (name.front()) { 593 case 'a': 594 if (name == "appendFormat") return SFF_NSString; 595 break; 596 597 case 'i': 598 if (name == "initWithFormat") return SFF_NSString; 599 break; 600 601 case 'l': 602 if (name == "localizedStringWithFormat") return SFF_NSString; 603 break; 604 605 case 's': 606 if (name == "stringByAppendingFormat" || 607 name == "stringWithFormat") return SFF_NSString; 608 break; 609 } 610 return SFF_None; 611 } 612 613 namespace { 614 615 struct SelectorTableImpl { 616 llvm::FoldingSet<MultiKeywordSelector> Table; 617 llvm::BumpPtrAllocator Allocator; 618 }; 619 620 } // namespace 621 622 static SelectorTableImpl &getSelectorTableImpl(void *P) { 623 return *static_cast<SelectorTableImpl*>(P); 624 } 625 626 SmallString<64> 627 SelectorTable::constructSetterName(StringRef Name) { 628 SmallString<64> SetterName("set"); 629 SetterName += Name; 630 SetterName[3] = toUppercase(SetterName[3]); 631 return SetterName; 632 } 633 634 Selector 635 SelectorTable::constructSetterSelector(IdentifierTable &Idents, 636 SelectorTable &SelTable, 637 const IdentifierInfo *Name) { 638 IdentifierInfo *SetterName = 639 &Idents.get(constructSetterName(Name->getName())); 640 return SelTable.getUnarySelector(SetterName); 641 } 642 643 size_t SelectorTable::getTotalMemory() const { 644 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl); 645 return SelTabImpl.Allocator.getTotalMemory(); 646 } 647 648 Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) { 649 if (nKeys < 2) 650 return Selector(IIV[0], nKeys); 651 652 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl); 653 654 // Unique selector, to guarantee there is one per name. 655 llvm::FoldingSetNodeID ID; 656 MultiKeywordSelector::Profile(ID, IIV, nKeys); 657 658 void *InsertPos = nullptr; 659 if (MultiKeywordSelector *SI = 660 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos)) 661 return Selector(SI); 662 663 // MultiKeywordSelector objects are not allocated with new because they have a 664 // variable size array (for parameter types) at the end of them. 665 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *); 666 MultiKeywordSelector *SI = 667 (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate( 668 Size, alignof(MultiKeywordSelector)); 669 new (SI) MultiKeywordSelector(nKeys, IIV); 670 SelTabImpl.Table.InsertNode(SI, InsertPos); 671 return Selector(SI); 672 } 673 674 SelectorTable::SelectorTable() { 675 Impl = new SelectorTableImpl(); 676 } 677 678 SelectorTable::~SelectorTable() { 679 delete &getSelectorTableImpl(Impl); 680 } 681 682 const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) { 683 switch (Operator) { 684 case OO_None: 685 case NUM_OVERLOADED_OPERATORS: 686 return nullptr; 687 688 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 689 case OO_##Name: return Spelling; 690 #include "clang/Basic/OperatorKinds.def" 691 } 692 693 llvm_unreachable("Invalid OverloadedOperatorKind!"); 694 } 695 696 StringRef clang::getNullabilitySpelling(NullabilityKind kind, 697 bool isContextSensitive) { 698 switch (kind) { 699 case NullabilityKind::NonNull: 700 return isContextSensitive ? "nonnull" : "_Nonnull"; 701 702 case NullabilityKind::Nullable: 703 return isContextSensitive ? "nullable" : "_Nullable"; 704 705 case NullabilityKind::Unspecified: 706 return isContextSensitive ? "null_unspecified" : "_Null_unspecified"; 707 } 708 llvm_unreachable("Unknown nullability kind."); 709 } 710