blob: 147f3e04751ab08afd7f71fda56bc20166b48169 [file] [log] [blame]
Eugene Zelenko918e0ca2017-11-03 22:35:27 +00001//===- IdentifierTable.cpp - Hash table for identifier lookup -------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000010// This file implements the IdentifierInfo, IdentifierVisitor, and
Chris Lattner91cbf112006-07-03 04:28:52 +000011// IdentifierTable interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000015#include "clang/Basic/IdentifierTable.h"
Eugene Zelenko918e0ca2017-11-03 22:35:27 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000017#include "clang/Basic/LangOptions.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000018#include "clang/Basic/OperatorKinds.h"
Douglas Gregor813a0662015-06-19 18:14:38 +000019#include "clang/Basic/Specifiers.h"
Eugene Zelenko918e0ca2017-11-03 22:35:27 +000020#include "clang/Basic/TokenKinds.h"
21#include "llvm/ADT/DenseMapInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/ADT/FoldingSet.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000023#include "llvm/ADT/SmallString.h"
Eugene Zelenko918e0ca2017-11-03 22:35:27 +000024#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/Support/Allocator.h"
David Blaikie8a40f702012-01-17 06:56:22 +000027#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "llvm/Support/raw_ostream.h"
Eugene Zelenko918e0ca2017-11-03 22:35:27 +000029#include <cassert>
Chris Lattnerc25d8a72009-03-02 22:20:04 +000030#include <cstdio>
Eugene Zelenko918e0ca2017-11-03 22:35:27 +000031#include <cstring>
32#include <string>
Ted Kremenekf25f4a32007-10-23 22:18:37 +000033
Chris Lattner22eb9722006-06-18 05:43:12 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000037// IdentifierInfo Implementation
Chris Lattner22eb9722006-06-18 05:43:12 +000038//===----------------------------------------------------------------------===//
39
Ted Kremenek52f73ca2009-01-20 23:28:34 +000040IdentifierInfo::IdentifierInfo() {
Chris Lattner3bc804e2006-10-28 23:46:24 +000041 TokenID = tok::identifier;
Douglas Gregor2ad7ee92008-11-06 16:32:23 +000042 ObjCOrBuiltinID = 0;
Chris Lattnera441ca62007-10-07 07:09:52 +000043 HasMacro = false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +000044 HadMacro = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000045 IsExtension = false;
Richard Smith31d51842015-05-14 04:00:59 +000046 IsFutureCompatKeyword = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000047 IsPoisoned = false;
Chris Lattner5b9f4892006-11-21 17:23:33 +000048 IsCPPOperatorKeyword = false;
Chris Lattnerad89ec02009-01-21 07:43:11 +000049 NeedsHandleIdentifier = false;
Sebastian Redld44cd6a2010-08-18 23:57:06 +000050 IsFromAST = false;
Douglas Gregor935bc7a22011-10-27 09:33:13 +000051 ChangedAfterLoad = false;
Richard Smithd79514e2016-02-05 19:03:40 +000052 FEChangedAfterLoad = false;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +000053 RevertedTokenID = false;
Douglas Gregor935bc7a22011-10-27 09:33:13 +000054 OutOfDate = false;
Ted Kremenekc1e4dd02012-03-01 22:07:04 +000055 IsModulesImport = false;
Chris Lattner3bc804e2006-10-28 23:46:24 +000056}
57
Chris Lattner91cbf112006-07-03 04:28:52 +000058//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +000059// IdentifierTable Implementation
60//===----------------------------------------------------------------------===//
61
Eugene Zelenko918e0ca2017-11-03 22:35:27 +000062IdentifierIterator::~IdentifierIterator() = default;
Douglas Gregor57756ea2010-10-14 22:11:03 +000063
Eugene Zelenko918e0ca2017-11-03 22:35:27 +000064IdentifierInfoLookup::~IdentifierInfoLookup() = default;
Ted Kremeneka705b042009-01-15 18:47:46 +000065
Douglas Gregor57756ea2010-10-14 22:11:03 +000066namespace {
Eugene Zelenko918e0ca2017-11-03 22:35:27 +000067
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000068/// A simple identifier lookup iterator that represents an
Eugene Zelenko918e0ca2017-11-03 22:35:27 +000069/// empty sequence of identifiers.
70class EmptyLookupIterator : public IdentifierIterator
71{
72public:
73 StringRef Next() override { return StringRef(); }
74};
75
76} // namespace
Douglas Gregor57756ea2010-10-14 22:11:03 +000077
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +000078IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
Douglas Gregor57756ea2010-10-14 22:11:03 +000079 return new EmptyLookupIterator();
80}
81
Aaron Ballmand742dc22018-04-16 21:07:08 +000082IdentifierTable::IdentifierTable(IdentifierInfoLookup *ExternalLookup)
83 : HashTable(8192), // Start with space for 8K identifiers.
84 ExternalLookup(ExternalLookup) {}
85
Ted Kremeneka705b042009-01-15 18:47:46 +000086IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
Aaron Ballmand742dc22018-04-16 21:07:08 +000087 IdentifierInfoLookup *ExternalLookup)
88 : IdentifierTable(ExternalLookup) {
Chris Lattnerf2e3ac32006-10-27 03:59:10 +000089 // Populate the identifier table with info about keywords for the current
90 // language.
Chris Lattner25e0d542006-10-18 06:07:05 +000091 AddKeywords(LangOpts);
Chris Lattner91cbf112006-07-03 04:28:52 +000092}
Chris Lattner22eb9722006-06-18 05:43:12 +000093
Chris Lattner25e0d542006-10-18 06:07:05 +000094//===----------------------------------------------------------------------===//
95// Language Keyword Implementation
96//===----------------------------------------------------------------------===//
97
Eli Friedman2b680b42009-04-28 03:13:54 +000098// Constants for TokenKinds.def
99namespace {
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000100
Eli Friedman2b680b42009-04-28 03:13:54 +0000101 enum {
Dylan Noblesmith92c07c22011-04-09 13:34:05 +0000102 KEYC99 = 0x1,
103 KEYCXX = 0x2,
Richard Smith89645bc2013-01-02 12:01:23 +0000104 KEYCXX11 = 0x4,
Dylan Noblesmith92c07c22011-04-09 13:34:05 +0000105 KEYGNU = 0x8,
106 KEYMS = 0x10,
107 BOOLSUPPORT = 0x20,
108 KEYALTIVEC = 0x40,
109 KEYNOCXX = 0x80,
110 KEYBORLAND = 0x100,
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +0000111 KEYOPENCLC = 0x200,
Benjamin Kramere56f3932011-12-23 17:00:35 +0000112 KEYC11 = 0x400,
John McCall31168b02011-06-15 23:02:42 +0000113 KEYARC = 0x800,
David Majnemer28aae9c2015-03-18 04:15:23 +0000114 KEYNOMS18 = 0x01000,
Anastasia Stulovab1152f12015-03-18 12:55:29 +0000115 KEYNOOPENCL = 0x02000,
116 WCHARSUPPORT = 0x04000,
117 HALFSUPPORT = 0x08000,
Richard Smith3a8244d2018-05-01 05:02:45 +0000118 CHAR8SUPPORT = 0x10000,
119 KEYCONCEPTS = 0x20000,
120 KEYOBJC2 = 0x40000,
121 KEYZVECTOR = 0x80000,
122 KEYCOROUTINES = 0x100000,
123 KEYMODULES = 0x200000,
124 KEYCXX2A = 0x400000,
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +0000125 KEYOPENCLCXX = 0x800000,
Richard Smith6c74e322017-08-13 21:32:33 +0000126 KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX2A,
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +0000127 KEYALL = (0xffffff & ~KEYNOMS18 &
Anastasia Stulovab1152f12015-03-18 12:55:29 +0000128 ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
Eli Friedman2b680b42009-04-28 03:13:54 +0000129 };
Serge Pavlov77af3812014-10-29 10:59:18 +0000130
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000131 /// How a keyword is treated in the selected standard.
Serge Pavlov77af3812014-10-29 10:59:18 +0000132 enum KeywordStatus {
133 KS_Disabled, // Disabled
134 KS_Extension, // Is an extension
135 KS_Enabled, // Enabled
136 KS_Future // Is a keyword in future standard
137 };
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000138
139} // namespace
Serge Pavlov77af3812014-10-29 10:59:18 +0000140
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000141/// Translates flags as specified in TokenKinds.def into keyword status
Serge Pavlov77af3812014-10-29 10:59:18 +0000142/// in the given language standard.
Serge Pavlov83cf0782014-12-11 12:18:08 +0000143static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
Serge Pavlov77af3812014-10-29 10:59:18 +0000144 unsigned Flags) {
145 if (Flags == KEYALL) return KS_Enabled;
146 if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled;
147 if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled;
Richard Smith6c74e322017-08-13 21:32:33 +0000148 if (LangOpts.CPlusPlus2a && (Flags & KEYCXX2A)) return KS_Enabled;
Serge Pavlov77af3812014-10-29 10:59:18 +0000149 if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled;
150 if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension;
151 if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension;
152 if (LangOpts.Borland && (Flags & KEYBORLAND)) return KS_Extension;
153 if (LangOpts.Bool && (Flags & BOOLSUPPORT)) return KS_Enabled;
154 if (LangOpts.Half && (Flags & HALFSUPPORT)) return KS_Enabled;
155 if (LangOpts.WChar && (Flags & WCHARSUPPORT)) return KS_Enabled;
Richard Smith3a8244d2018-05-01 05:02:45 +0000156 if (LangOpts.Char8 && (Flags & CHAR8SUPPORT)) return KS_Enabled;
Serge Pavlov77af3812014-10-29 10:59:18 +0000157 if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) return KS_Enabled;
Sven van Haastregt2ca6ba12018-05-09 13:16:17 +0000158 if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLC))
159 return KS_Enabled;
160 if (LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLCXX)) return KS_Enabled;
Serge Pavlov77af3812014-10-29 10:59:18 +0000161 if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled;
162 if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled;
163 // We treat bridge casts as objective-C keywords so we can warn on them
164 // in non-arc mode.
165 if (LangOpts.ObjC2 && (Flags & KEYARC)) return KS_Enabled;
Douglas Gregorab209d82015-07-07 03:58:42 +0000166 if (LangOpts.ObjC2 && (Flags & KEYOBJC2)) return KS_Enabled;
Richard Smithc7bf3802016-07-23 02:32:21 +0000167 if (LangOpts.ConceptsTS && (Flags & KEYCONCEPTS)) return KS_Enabled;
Gor Nishanov4ffb4342016-10-02 03:31:58 +0000168 if (LangOpts.CoroutinesTS && (Flags & KEYCOROUTINES)) return KS_Enabled;
Richard Smithc7bf3802016-07-23 02:32:21 +0000169 if (LangOpts.ModulesTS && (Flags & KEYMODULES)) return KS_Enabled;
Richard Smith6c74e322017-08-13 21:32:33 +0000170 if (LangOpts.CPlusPlus && (Flags & KEYALLCXX)) return KS_Future;
Serge Pavlov77af3812014-10-29 10:59:18 +0000171 return KS_Disabled;
Eli Friedman2b680b42009-04-28 03:13:54 +0000172}
173
Chris Lattner25e0d542006-10-18 06:07:05 +0000174/// AddKeyword - This method is used to associate a token ID with specific
175/// identifiers because they are language keywords. This causes the lexer to
176/// automatically map matching identifiers to specialized token codes.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000177static void AddKeyword(StringRef Keyword,
Eli Friedman2b680b42009-04-28 03:13:54 +0000178 tok::TokenKind TokenCode, unsigned Flags,
Chris Lattner25e0d542006-10-18 06:07:05 +0000179 const LangOptions &LangOpts, IdentifierTable &Table) {
Serge Pavlov83cf0782014-12-11 12:18:08 +0000180 KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
Richard Smith4dd85d62011-10-11 19:57:52 +0000181
Alp Tokerbfa39342014-01-14 12:51:41 +0000182 // Don't add this keyword under MSVCCompat.
David Majnemer28aae9c2015-03-18 04:15:23 +0000183 if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) &&
David Majnemerb710a932015-05-11 03:57:49 +0000184 !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
Anastasia Stulovab1152f12015-03-18 12:55:29 +0000185 return;
186
187 // Don't add this keyword under OpenCL.
188 if (LangOpts.OpenCL && (Flags & KEYNOOPENCL))
189 return;
190
Eli Friedman2b680b42009-04-28 03:13:54 +0000191 // Don't add this keyword if disabled in this language.
Serge Pavlov77af3812014-10-29 10:59:18 +0000192 if (AddResult == KS_Disabled) return;
Eli Friedman2b680b42009-04-28 03:13:54 +0000193
Richard Smith4dd85d62011-10-11 19:57:52 +0000194 IdentifierInfo &Info =
Serge Pavlov77af3812014-10-29 10:59:18 +0000195 Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode);
196 Info.setIsExtensionToken(AddResult == KS_Extension);
Richard Smith31d51842015-05-14 04:00:59 +0000197 Info.setIsFutureCompatKeyword(AddResult == KS_Future);
Chris Lattner25e0d542006-10-18 06:07:05 +0000198}
199
Chris Lattner5b9f4892006-11-21 17:23:33 +0000200/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
201/// representations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000202static void AddCXXOperatorKeyword(StringRef Keyword,
Chris Lattner5b9f4892006-11-21 17:23:33 +0000203 tok::TokenKind TokenCode,
204 IdentifierTable &Table) {
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000205 IdentifierInfo &Info = Table.get(Keyword, TokenCode);
Ted Kremenekf25f4a32007-10-23 22:18:37 +0000206 Info.setIsCPlusPlusOperatorKeyword();
Chris Lattner5b9f4892006-11-21 17:23:33 +0000207}
208
James Dennett0d8a3f82012-06-15 21:27:44 +0000209/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
210/// or "property".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000211static void AddObjCKeyword(StringRef Name,
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000212 tok::ObjCKeywordKind ObjCID,
Chris Lattner25e0d542006-10-18 06:07:05 +0000213 IdentifierTable &Table) {
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000214 Table.get(Name).setObjCKeywordID(ObjCID);
Chris Lattner25e0d542006-10-18 06:07:05 +0000215}
216
217/// AddKeywords - Add all keywords to the symbol table.
218///
219void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
Chris Lattner25e0d542006-10-18 06:07:05 +0000220 // Add keywords and tokens for the current language.
221#define KEYWORD(NAME, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000222 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000223 FLAGS, LangOpts, *this);
224#define ALIAS(NAME, TOK, FLAGS) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000225 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
Eli Friedman2b680b42009-04-28 03:13:54 +0000226 FLAGS, LangOpts, *this);
Chris Lattner5b9f4892006-11-21 17:23:33 +0000227#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
Chris Lattner3e7592e2006-12-04 07:48:37 +0000228 if (LangOpts.CXXOperatorNames) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000229 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000230#define OBJC1_AT_KEYWORD(NAME) \
231 if (LangOpts.ObjC1) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000232 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
Chris Lattner25e0d542006-10-18 06:07:05 +0000233#define OBJC2_AT_KEYWORD(NAME) \
234 if (LangOpts.ObjC2) \
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000235 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
John McCall39439732011-04-09 22:50:59 +0000236#define TESTING_KEYWORD(NAME, FLAGS)
Chris Lattner25e0d542006-10-18 06:07:05 +0000237#include "clang/Basic/TokenKinds.def"
John McCall39439732011-04-09 22:50:59 +0000238
239 if (LangOpts.ParseUnknownAnytype)
240 AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
241 LangOpts, *this);
Aaron Ballman674cf262015-05-26 19:44:52 +0000242
Saleem Abdulrasoold170c4b2015-10-04 17:51:05 +0000243 if (LangOpts.DeclSpecKeyword)
Aaron Ballman674cf262015-05-26 19:44:52 +0000244 AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
Aaron Ballmand742dc22018-04-16 21:07:08 +0000245
246 // Add the '_experimental_modules_import' contextual keyword.
247 get("import").setModulesImport(true);
Chris Lattner25e0d542006-10-18 06:07:05 +0000248}
249
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000250/// Checks if the specified token kind represents a keyword in the
Serge Pavlov83cf0782014-12-11 12:18:08 +0000251/// specified language.
252/// \returns Status of the keyword in the language.
253static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
254 tok::TokenKind K) {
255 switch (K) {
256#define KEYWORD(NAME, FLAGS) \
257 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
258#include "clang/Basic/TokenKinds.def"
259 default: return KS_Disabled;
260 }
261}
262
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000263/// Returns true if the identifier represents a keyword in the
Serge Pavlov83cf0782014-12-11 12:18:08 +0000264/// specified language.
Alex Lorenzf1278212017-04-11 15:01:53 +0000265bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) const {
Serge Pavlov83cf0782014-12-11 12:18:08 +0000266 switch (getTokenKwStatus(LangOpts, getTokenID())) {
267 case KS_Enabled:
268 case KS_Extension:
269 return true;
270 default:
271 return false;
272 }
273}
274
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000275/// Returns true if the identifier represents a C++ keyword in the
Alex Lorenzf1278212017-04-11 15:01:53 +0000276/// specified language.
277bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const {
278 if (!LangOpts.CPlusPlus || !isKeyword(LangOpts))
279 return false;
280 // This is a C++ keyword if this identifier is not a keyword when checked
281 // using LangOptions without C++ support.
282 LangOptions LangOptsNoCPP = LangOpts;
283 LangOptsNoCPP.CPlusPlus = false;
284 LangOptsNoCPP.CPlusPlus11 = false;
Richard Smith6c74e322017-08-13 21:32:33 +0000285 LangOptsNoCPP.CPlusPlus2a = false;
Alex Lorenzf1278212017-04-11 15:01:53 +0000286 return !isKeyword(LangOptsNoCPP);
287}
288
Chris Lattnerff067ce2007-10-07 07:52:34 +0000289tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
290 // We use a perfect hash function here involving the length of the keyword,
291 // the first and third character. For preprocessor ID's there are no
292 // collisions (if there were, the switch below would complain about duplicate
293 // case values). Note that this depends on 'if' being null terminated.
Mike Stump11289f42009-09-09 15:08:12 +0000294
Chris Lattnerff067ce2007-10-07 07:52:34 +0000295#define HASH(LEN, FIRST, THIRD) \
296 (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
297#define CASE(LEN, FIRST, THIRD, NAME) \
298 case HASH(LEN, FIRST, THIRD): \
299 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattnerff067ce2007-10-07 07:52:34 +0000301 unsigned Len = getLength();
Chris Lattnerd2b8ce42007-10-10 20:59:57 +0000302 if (Len < 2) return tok::pp_not_keyword;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000303 const char *Name = getNameStart();
Chris Lattnerff067ce2007-10-07 07:52:34 +0000304 switch (HASH(Len, Name[0], Name[2])) {
305 default: return tok::pp_not_keyword;
306 CASE( 2, 'i', '\0', if);
307 CASE( 4, 'e', 'i', elif);
308 CASE( 4, 'e', 's', else);
309 CASE( 4, 'l', 'n', line);
310 CASE( 4, 's', 'c', sccs);
311 CASE( 5, 'e', 'd', endif);
312 CASE( 5, 'e', 'r', error);
313 CASE( 5, 'i', 'e', ident);
314 CASE( 5, 'i', 'd', ifdef);
315 CASE( 5, 'u', 'd', undef);
316
317 CASE( 6, 'a', 's', assert);
318 CASE( 6, 'd', 'f', define);
319 CASE( 6, 'i', 'n', ifndef);
320 CASE( 6, 'i', 'p', import);
321 CASE( 6, 'p', 'a', pragma);
Fangrui Song6907ce22018-07-30 19:24:48 +0000322
Chris Lattnerff067ce2007-10-07 07:52:34 +0000323 CASE( 7, 'd', 'f', defined);
324 CASE( 7, 'i', 'c', include);
325 CASE( 7, 'w', 'r', warning);
326
327 CASE( 8, 'u', 'a', unassert);
328 CASE(12, 'i', 'c', include_next);
Mike Stump11289f42009-09-09 15:08:12 +0000329
Douglas Gregor663b48f2012-01-03 19:48:16 +0000330 CASE(14, '_', 'p', __public_macro);
Fangrui Song6907ce22018-07-30 19:24:48 +0000331
Douglas Gregor663b48f2012-01-03 19:48:16 +0000332 CASE(15, '_', 'p', __private_macro);
333
Chris Lattner14a7f392009-04-08 18:24:34 +0000334 CASE(16, '_', 'i', __include_macros);
Chris Lattnerff067ce2007-10-07 07:52:34 +0000335#undef CASE
336#undef HASH
337 }
338}
Chris Lattner25e0d542006-10-18 06:07:05 +0000339
340//===----------------------------------------------------------------------===//
341// Stats Implementation
342//===----------------------------------------------------------------------===//
343
Chris Lattner22eb9722006-06-18 05:43:12 +0000344/// PrintStats - Print statistics about how well the identifier table is doing
345/// at hashing identifiers.
346void IdentifierTable::PrintStats() const {
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000347 unsigned NumBuckets = HashTable.getNumBuckets();
348 unsigned NumIdentifiers = HashTable.getNumItems();
349 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
Chris Lattner22eb9722006-06-18 05:43:12 +0000350 unsigned AverageIdentifierSize = 0;
351 unsigned MaxIdentifierLength = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000353 // TODO: Figure out maximum times an identifier had to probe for -stats.
Ted Kremenek52f73ca2009-01-20 23:28:34 +0000354 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
Chris Lattnerb055f2d2007-02-11 08:19:57 +0000355 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
356 unsigned IdLen = I->getKeyLength();
357 AverageIdentifierSize += IdLen;
358 if (MaxIdentifierLength < IdLen)
359 MaxIdentifierLength = IdLen;
360 }
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattner23b7eb62007-06-15 23:05:46 +0000362 fprintf(stderr, "\n*** Identifier Table Stats:\n");
363 fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
364 fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
365 fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
366 NumIdentifiers/(double)NumBuckets);
367 fprintf(stderr, "Ave identifier length: %f\n",
368 (AverageIdentifierSize/(double)NumIdentifiers));
369 fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
Mike Stump11289f42009-09-09 15:08:12 +0000370
Chris Lattner22eb9722006-06-18 05:43:12 +0000371 // Compute statistics about the memory allocated for identifiers.
Chris Lattner2b9e19b2006-10-29 23:43:13 +0000372 HashTable.getAllocator().PrintStats();
Chris Lattner22eb9722006-06-18 05:43:12 +0000373}
Steve Narofff73590d2007-09-27 14:38:14 +0000374
Steve Naroffe61bfa82007-10-05 18:42:47 +0000375//===----------------------------------------------------------------------===//
376// SelectorTable Implementation
377//===----------------------------------------------------------------------===//
378
Chris Lattnerdadc7622007-10-05 20:15:24 +0000379unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
380 return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
381}
382
Douglas Gregor77324f32008-11-17 14:58:09 +0000383namespace clang {
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000384
Bruno Ricci366ba732018-09-21 12:53:22 +0000385/// One of these variable length records is kept for each
Steve Naroffe61bfa82007-10-05 18:42:47 +0000386/// selector containing more than one keyword. We use a folding set
Mike Stump11289f42009-09-09 15:08:12 +0000387/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
Steve Naroffe61bfa82007-10-05 18:42:47 +0000388/// this class is provided strictly through Selector.
Bruno Ricci366ba732018-09-21 12:53:22 +0000389class alignas(IdentifierInfoAlignment) MultiKeywordSelector
390 : public detail::DeclarationNameExtra,
391 public llvm::FoldingSetNode {
392 MultiKeywordSelector(unsigned nKeys) : DeclarationNameExtra(nKeys) {}
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000393
Mike Stump11289f42009-09-09 15:08:12 +0000394public:
Steve Naroffe61bfa82007-10-05 18:42:47 +0000395 // Constructor for keyword selectors.
Bruno Ricci366ba732018-09-21 12:53:22 +0000396 MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV)
397 : DeclarationNameExtra(nKeys) {
Steve Naroffe61bfa82007-10-05 18:42:47 +0000398 assert((nKeys > 1) && "not a multi-keyword selector");
Mike Stump11289f42009-09-09 15:08:12 +0000399
Steve Naroffe61bfa82007-10-05 18:42:47 +0000400 // Fill in the trailing keyword array.
Bruno Ricci366ba732018-09-21 12:53:22 +0000401 IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this + 1);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000402 for (unsigned i = 0; i != nKeys; ++i)
403 KeyInfo[i] = IIV[i];
Mike Stump11289f42009-09-09 15:08:12 +0000404 }
405
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000406 // getName - Derive the full selector name and return it.
407 std::string getName() const;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Bruno Ricci366ba732018-09-21 12:53:22 +0000409 using DeclarationNameExtra::getNumArgs;
Mike Stump11289f42009-09-09 15:08:12 +0000410
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000411 using keyword_iterator = IdentifierInfo *const *;
412
Steve Naroffe61bfa82007-10-05 18:42:47 +0000413 keyword_iterator keyword_begin() const {
Bruno Ricci366ba732018-09-21 12:53:22 +0000414 return reinterpret_cast<keyword_iterator>(this + 1);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000415 }
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000416
Mike Stump11289f42009-09-09 15:08:12 +0000417 keyword_iterator keyword_end() const {
Bruno Ricci366ba732018-09-21 12:53:22 +0000418 return keyword_begin() + getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000419 }
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000420
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000421 IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
Douglas Gregor77324f32008-11-17 14:58:09 +0000422 assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
Steve Naroffe61bfa82007-10-05 18:42:47 +0000423 return keyword_begin()[i];
424 }
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000425
Bruno Ricci366ba732018-09-21 12:53:22 +0000426 static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys,
427 unsigned NumArgs) {
Steve Naroffe61bfa82007-10-05 18:42:47 +0000428 ID.AddInteger(NumArgs);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000429 for (unsigned i = 0; i != NumArgs; ++i)
430 ID.AddPointer(ArgTys[i]);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000431 }
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000432
Steve Naroffe61bfa82007-10-05 18:42:47 +0000433 void Profile(llvm::FoldingSetNodeID &ID) {
Douglas Gregor77324f32008-11-17 14:58:09 +0000434 Profile(ID, keyword_begin(), getNumArgs());
Steve Naroffe61bfa82007-10-05 18:42:47 +0000435 }
436};
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000437
438} // namespace clang.
Steve Naroffe61bfa82007-10-05 18:42:47 +0000439
440unsigned Selector::getNumArgs() const {
441 unsigned IIF = getIdentifierInfoFlag();
Douglas Gregor93a586f2012-05-04 18:24:37 +0000442 if (IIF <= ZeroArg)
Steve Naroffe61bfa82007-10-05 18:42:47 +0000443 return 0;
444 if (IIF == OneArg)
445 return 1;
Douglas Gregor93a586f2012-05-04 18:24:37 +0000446 // We point to a MultiKeywordSelector.
447 MultiKeywordSelector *SI = getMultiKeywordSelector();
Mike Stump11289f42009-09-09 15:08:12 +0000448 return SI->getNumArgs();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000449}
450
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000451IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
Douglas Gregor93a586f2012-05-04 18:24:37 +0000452 if (getIdentifierInfoFlag() < MultiArg) {
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000453 assert(argIndex == 0 && "illegal keyword index");
Douglas Gregor70091b82009-04-26 22:20:50 +0000454 return getAsIdentifierInfo();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000455 }
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000456
Douglas Gregor93a586f2012-05-04 18:24:37 +0000457 // We point to a MultiKeywordSelector.
458 MultiKeywordSelector *SI = getMultiKeywordSelector();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000459 return SI->getIdentifierInfoForSlot(argIndex);
460}
461
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000462StringRef Selector::getNameForSlot(unsigned int argIndex) const {
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +0000463 IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
Bruno Ricci366ba732018-09-21 12:53:22 +0000464 return II ? II->getName() : StringRef();
Douglas Gregoraf2a6ae2011-02-18 22:29:55 +0000465}
466
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000467std::string MultiKeywordSelector::getName() const {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000468 SmallString<256> Str;
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000469 llvm::raw_svector_ostream OS(Str);
Chris Lattnerf7f34d02007-10-07 01:33:16 +0000470 for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
471 if (*I)
Daniel Dunbar07d07852009-10-18 21:17:35 +0000472 OS << (*I)->getName();
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000473 OS << ':';
Steve Naroffe61bfa82007-10-05 18:42:47 +0000474 }
Mike Stump11289f42009-09-09 15:08:12 +0000475
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000476 return OS.str();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000477}
478
Chris Lattnere4b95692008-11-24 03:33:13 +0000479std::string Selector::getAsString() const {
Douglas Gregor70091b82009-04-26 22:20:50 +0000480 if (InfoPtr == 0)
481 return "<null selector>";
482
Douglas Gregor93a586f2012-05-04 18:24:37 +0000483 if (getIdentifierInfoFlag() < MultiArg) {
Ted Kremenek5f080b42009-03-06 23:36:28 +0000484 IdentifierInfo *II = getAsIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000485
Chandler Carruth59666772016-11-04 06:32:57 +0000486 if (getNumArgs() == 0) {
487 assert(II && "If the number of arguments is 0 then II is guaranteed to "
488 "not be null.");
Daniel Dunbar07d07852009-10-18 21:17:35 +0000489 return II->getName();
Chandler Carruth59666772016-11-04 06:32:57 +0000490 }
Ted Kremenek5f080b42009-03-06 23:36:28 +0000491
Daniel Dunbar1c0761d2009-10-17 18:13:02 +0000492 if (!II)
493 return ":";
494
Daniel Dunbar07d07852009-10-18 21:17:35 +0000495 return II->getName().str() + ":";
Steve Naroffe61bfa82007-10-05 18:42:47 +0000496 }
Mike Stump11289f42009-09-09 15:08:12 +0000497
Douglas Gregor93a586f2012-05-04 18:24:37 +0000498 // We have a multiple keyword selector.
499 return getMultiKeywordSelector()->getName();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000500}
501
Aaron Ballmanb190f972014-01-03 17:59:55 +0000502void Selector::print(llvm::raw_ostream &OS) const {
503 OS << getAsString();
504}
505
Aditya Kumar69958212018-05-31 14:45:32 +0000506LLVM_DUMP_METHOD void Selector::dump() const { print(llvm::errs()); }
507
John McCallb4526252011-03-02 01:50:55 +0000508/// Interpreting the given string using the normal CamelCase
509/// conventions, determine whether the given string starts with the
510/// given "word", which is assumed to end in a lowercase letter.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000511static bool startsWithWord(StringRef name, StringRef word) {
John McCallb4526252011-03-02 01:50:55 +0000512 if (name.size() < word.size()) return false;
Jordan Rosea7d03842013-02-08 22:30:41 +0000513 return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
514 name.startswith(word));
John McCallb4526252011-03-02 01:50:55 +0000515}
516
517ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
518 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
519 if (!first) return OMF_None;
520
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000521 StringRef name = first->getName();
John McCallb4526252011-03-02 01:50:55 +0000522 if (sel.isUnarySelector()) {
523 if (name == "autorelease") return OMF_autorelease;
524 if (name == "dealloc") return OMF_dealloc;
Nico Weber1fb82662011-08-28 22:35:17 +0000525 if (name == "finalize") return OMF_finalize;
John McCallb4526252011-03-02 01:50:55 +0000526 if (name == "release") return OMF_release;
527 if (name == "retain") return OMF_retain;
528 if (name == "retainCount") return OMF_retainCount;
Douglas Gregor33823722011-06-11 01:09:30 +0000529 if (name == "self") return OMF_self;
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000530 if (name == "initialize") return OMF_initialize;
John McCallb4526252011-03-02 01:50:55 +0000531 }
Alex Lorenz0e23c612017-03-06 15:58:34 +0000532
533 if (name == "performSelector" || name == "performSelectorInBackground" ||
534 name == "performSelectorOnMainThread")
535 return OMF_performSelector;
John McCallb4526252011-03-02 01:50:55 +0000536
537 // The other method families may begin with a prefix of underscores.
538 while (!name.empty() && name.front() == '_')
539 name = name.substr(1);
540
541 if (name.empty()) return OMF_None;
542 switch (name.front()) {
543 case 'a':
544 if (startsWithWord(name, "alloc")) return OMF_alloc;
545 break;
546 case 'c':
547 if (startsWithWord(name, "copy")) return OMF_copy;
548 break;
549 case 'i':
550 if (startsWithWord(name, "init")) return OMF_init;
551 break;
552 case 'm':
553 if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
554 break;
555 case 'n':
556 if (startsWithWord(name, "new")) return OMF_new;
557 break;
558 default:
559 break;
560 }
561
562 return OMF_None;
563}
Steve Naroffe61bfa82007-10-05 18:42:47 +0000564
Fariborz Jahanian71221352013-07-23 22:42:28 +0000565ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000566 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
567 if (!first) return OIT_None;
Fangrui Song6907ce22018-07-30 19:24:48 +0000568
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000569 StringRef name = first->getName();
Fangrui Song6907ce22018-07-30 19:24:48 +0000570
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000571 if (name.empty()) return OIT_None;
572 switch (name.front()) {
573 case 'a':
Fariborz Jahanian4ccdc732013-08-29 16:22:26 +0000574 if (startsWithWord(name, "array")) return OIT_Array;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000575 break;
576 case 'd':
Fariborz Jahanian7c87b432013-10-10 18:23:13 +0000577 if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000578 if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
579 break;
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000580 case 's':
Fariborz Jahanian7c87b432013-10-10 18:23:13 +0000581 if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
582 if (startsWithWord(name, "standard")) return OIT_Singleton;
Galina Kistanovaddcd2812017-06-03 06:40:10 +0000583 break;
Fariborz Jahanian1c900bc2013-09-18 20:35:47 +0000584 case 'i':
585 if (startsWithWord(name, "init")) return OIT_Init;
Fariborz Jahanian4f3a64f2013-07-23 19:31:17 +0000586 default:
587 break;
588 }
589 return OIT_None;
590}
591
Fariborz Jahanian6485fe42014-09-09 23:10:54 +0000592ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
593 IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
594 if (!first) return SFF_None;
Fangrui Song6907ce22018-07-30 19:24:48 +0000595
Fariborz Jahanian6485fe42014-09-09 23:10:54 +0000596 StringRef name = first->getName();
Fangrui Song6907ce22018-07-30 19:24:48 +0000597
Fariborz Jahanian6485fe42014-09-09 23:10:54 +0000598 switch (name.front()) {
599 case 'a':
600 if (name == "appendFormat") return SFF_NSString;
601 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000602
Fariborz Jahanian6485fe42014-09-09 23:10:54 +0000603 case 'i':
604 if (name == "initWithFormat") return SFF_NSString;
605 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000606
Fariborz Jahanian6485fe42014-09-09 23:10:54 +0000607 case 'l':
608 if (name == "localizedStringWithFormat") return SFF_NSString;
609 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000610
Fariborz Jahanian6485fe42014-09-09 23:10:54 +0000611 case 's':
612 if (name == "stringByAppendingFormat" ||
613 name == "stringWithFormat") return SFF_NSString;
614 break;
615 }
616 return SFF_None;
617}
618
Chris Lattner1a849942009-03-04 05:35:38 +0000619namespace {
Eugene Zelenko918e0ca2017-11-03 22:35:27 +0000620
621struct SelectorTableImpl {
622 llvm::FoldingSet<MultiKeywordSelector> Table;
623 llvm::BumpPtrAllocator Allocator;
624};
625
626} // namespace
Chris Lattner1a849942009-03-04 05:35:38 +0000627
628static SelectorTableImpl &getSelectorTableImpl(void *P) {
629 return *static_cast<SelectorTableImpl*>(P);
630}
631
Adrian Prantl6e77c962013-06-10 21:36:55 +0000632SmallString<64>
Adrian Prantla4ce9062013-06-07 22:29:12 +0000633SelectorTable::constructSetterName(StringRef Name) {
Adrian Prantl6e77c962013-06-10 21:36:55 +0000634 SmallString<64> SetterName("set");
635 SetterName += Name;
636 SetterName[3] = toUppercase(SetterName[3]);
637 return SetterName;
Adrian Prantla4ce9062013-06-07 22:29:12 +0000638}
639
Adrian Prantl6e77c962013-06-10 21:36:55 +0000640Selector
Adrian Prantla4ce9062013-06-07 22:29:12 +0000641SelectorTable::constructSetterSelector(IdentifierTable &Idents,
642 SelectorTable &SelTable,
643 const IdentifierInfo *Name) {
644 IdentifierInfo *SetterName =
645 &Idents.get(constructSetterName(Name->getName()));
Benjamin Kramer49038022012-02-04 13:45:25 +0000646 return SelTable.getUnarySelector(SetterName);
647}
648
Alex Lorenz5a5a6542018-05-23 00:52:20 +0000649std::string SelectorTable::getPropertyNameFromSetterSelector(Selector Sel) {
650 StringRef Name = Sel.getNameForSlot(0);
651 assert(Name.startswith("set") && "invalid setter name");
652 return (Twine(toLowercase(Name[3])) + Name.drop_front(4)).str();
653}
654
Ted Kremenek1c2239e2011-04-18 22:47:04 +0000655size_t SelectorTable::getTotalMemory() const {
656 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
657 return SelTabImpl.Allocator.getTotalMemory();
658}
Chris Lattner1a849942009-03-04 05:35:38 +0000659
Chris Lattner5700fab2007-10-07 02:00:24 +0000660Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
661 if (nKeys < 2)
662 return Selector(IIV[0], nKeys);
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattner1a849942009-03-04 05:35:38 +0000664 SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
Mike Stump11289f42009-09-09 15:08:12 +0000665
Steve Naroffe61bfa82007-10-05 18:42:47 +0000666 // Unique selector, to guarantee there is one per name.
667 llvm::FoldingSetNodeID ID;
668 MultiKeywordSelector::Profile(ID, IIV, nKeys);
669
Craig Topperf1186c52014-05-08 06:41:40 +0000670 void *InsertPos = nullptr;
Chris Lattner1a849942009-03-04 05:35:38 +0000671 if (MultiKeywordSelector *SI =
672 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe61bfa82007-10-05 18:42:47 +0000673 return Selector(SI);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Steve Naroffe61bfa82007-10-05 18:42:47 +0000675 // MultiKeywordSelector objects are not allocated with new because they have a
676 // variable size array (for parameter types) at the end of them.
Chris Lattner1a849942009-03-04 05:35:38 +0000677 unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
678 MultiKeywordSelector *SI =
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000679 (MultiKeywordSelector *)SelTabImpl.Allocator.Allocate(
680 Size, alignof(MultiKeywordSelector));
Steve Naroffe61bfa82007-10-05 18:42:47 +0000681 new (SI) MultiKeywordSelector(nKeys, IIV);
Chris Lattner1a849942009-03-04 05:35:38 +0000682 SelTabImpl.Table.InsertNode(SI, InsertPos);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000683 return Selector(SI);
684}
685
Steve Naroffe61bfa82007-10-05 18:42:47 +0000686SelectorTable::SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000687 Impl = new SelectorTableImpl();
Steve Naroffe61bfa82007-10-05 18:42:47 +0000688}
689
690SelectorTable::~SelectorTable() {
Chris Lattner1a849942009-03-04 05:35:38 +0000691 delete &getSelectorTableImpl(Impl);
Steve Naroffe61bfa82007-10-05 18:42:47 +0000692}
693
Douglas Gregor71395fa2009-11-04 00:56:37 +0000694const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
695 switch (Operator) {
696 case OO_None:
697 case NUM_OVERLOADED_OPERATORS:
Craig Topperf1186c52014-05-08 06:41:40 +0000698 return nullptr;
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000699
Douglas Gregor71395fa2009-11-04 00:56:37 +0000700#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
701 case OO_##Name: return Spelling;
702#include "clang/Basic/OperatorKinds.def"
703 }
Kovarththanan Rajaratnam00682a42010-03-12 11:27:37 +0000704
David Blaikie8a40f702012-01-17 06:56:22 +0000705 llvm_unreachable("Invalid OverloadedOperatorKind!");
Douglas Gregor71395fa2009-11-04 00:56:37 +0000706}
Douglas Gregor813a0662015-06-19 18:14:38 +0000707
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000708StringRef clang::getNullabilitySpelling(NullabilityKind kind,
709 bool isContextSensitive) {
Douglas Gregor813a0662015-06-19 18:14:38 +0000710 switch (kind) {
711 case NullabilityKind::NonNull:
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000712 return isContextSensitive ? "nonnull" : "_Nonnull";
Douglas Gregor813a0662015-06-19 18:14:38 +0000713
714 case NullabilityKind::Nullable:
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000715 return isContextSensitive ? "nullable" : "_Nullable";
Douglas Gregor813a0662015-06-19 18:14:38 +0000716
717 case NullabilityKind::Unspecified:
Douglas Gregoraea7afd2015-06-24 22:02:08 +0000718 return isContextSensitive ? "null_unspecified" : "_Null_unspecified";
Douglas Gregor813a0662015-06-19 18:14:38 +0000719 }
Nico Weber708aabd2015-06-20 00:06:30 +0000720 llvm_unreachable("Unknown nullability kind.");
Douglas Gregor813a0662015-06-19 18:14:38 +0000721}