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