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