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