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