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