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