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