22#include "llvm/ADT/DenseMapInfo.h"
23#include "llvm/ADT/FoldingSet.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/Support/Allocator.h"
27#include "llvm/Support/raw_ostream.h"
39 "Insufficient ObjCOrBuiltinID Bits");
55 StringRef
Next()
override {
return StringRef(); }
61 return new EmptyLookupIterator();
66 ExternalLookup(ExternalLookup) {}
90 assert((Flag & ~(Flag - 1)) == Flag &&
"Multiple bits set?");
104 if (LangOpts.CPlusPlus11)
108 if (LangOpts.CPlusPlus20)
123 return LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus ?
KS_Enabled
132 if (LangOpts.CPlusPlus)
return KS_Future;
170 llvm_unreachable(
"Unknown KeywordStatus flag");
181 if (LangOpts.MSVCCompat && (Flags &
KEYNOMS18) &&
184 if (LangOpts.ZOSExt && (Flags &
KEYNOZOS))
189 unsigned CurFlag = Flags & ~(Flags - 1);
190 Flags = Flags & ~CurFlag;
191 CurStatus = std::max(
261 if (BTID != tok::not_notable) {
271#define KEYWORD(NAME, FLAGS) \
272 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
273 FLAGS, LangOpts, *this);
274#define ALIAS(NAME, TOK, FLAGS) \
275 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
276 FLAGS, LangOpts, *this);
277#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
278 if (LangOpts.CXXOperatorNames) \
279 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this); \
281 MarkIdentifierAsKeywordInCpp(*this, StringRef(#NAME));
282#define OBJC_AT_KEYWORD(NAME) \
284 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
285#define NOTABLE_IDENTIFIER(NAME) \
286 AddNotableIdentifier(StringRef(#NAME), tok::NAME, *this);
288#define TESTING_KEYWORD(NAME, FLAGS)
289#include "clang/Basic/TokenKinds.def"
291 if (LangOpts.ParseUnknownAnytype)
295 if (LangOpts.DeclSpecKeyword)
298 if (LangOpts.IEEE128)
311#define KEYWORD(NAME, FLAGS) \
312 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
313#include "clang/Basic/TokenKinds.def"
333 if (!LangOpts.CPlusPlus || !
isKeyword(LangOpts))
338 LangOptsNoCPP.CPlusPlus =
false;
339 LangOptsNoCPP.CPlusPlus11 =
false;
340 LangOptsNoCPP.CPlusPlus20 =
false;
350 if (Name.size() <= 1)
354 if (Name[0] ==
'_') {
361 if (
'A' <= Name[1] && Name[1] <=
'Z')
362 return ReservedIdentifierStatus::
363 StartsWithUnderscoreFollowedByCapitalLetter;
371 if (LangOpts.CPlusPlus && Name.contains(
"__"))
387 if (Name.contains(
"__"))
395 if (Name.size() >= 2 && Name.front() ==
'_' &&
396 (Name[1] ==
'_' || (Name[1] >=
'A' && Name[1] <=
'Z')))
397 return Name.ltrim(
'_');
407#define HASH(LEN, FIRST, THIRD) \
408 (LEN << 6) + (((FIRST - 'a') - (THIRD - 'a')) & 63)
409#define CASE(LEN, FIRST, THIRD, NAME) \
410 case HASH(LEN, FIRST, THIRD): \
411 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
414 if (Len < 2)
return tok::pp_not_keyword;
416 switch (
HASH(Len, Name[0], Name[2])) {
417 default:
return tok::pp_not_keyword;
418 CASE( 2,
'i',
'\0',
if);
419 CASE( 4,
'e',
'i', elif);
420 CASE( 4,
'e',
's',
else);
421 CASE( 4,
'l',
'n', line);
422 CASE( 4,
's',
'c', sccs);
423 CASE( 5,
'e',
'b', embed);
424 CASE( 5,
'e',
'd', endif);
425 CASE( 5,
'e',
'r', error);
426 CASE( 5,
'i',
'e', ident);
427 CASE( 5,
'i',
'd', ifdef);
428 CASE( 5,
'u',
'd', undef);
430 CASE( 6,
'a',
's', assert);
431 CASE( 6,
'd',
'f', define);
432 CASE( 6,
'i',
'n', ifndef);
433 CASE( 6,
'i',
'p',
import);
434 CASE( 6,
'p',
'a', pragma);
436 CASE( 7,
'd',
'f', defined);
437 CASE( 7,
'e',
'i', elifdef);
438 CASE( 7,
'i',
'c', include);
439 CASE( 7,
'w',
'r', warning);
441 CASE( 8,
'e',
'i', elifndef);
442 CASE( 8,
'u',
'a', unassert);
443 CASE(12,
'i',
'c', include_next);
445 CASE(14,
'_',
'p', __public_macro);
447 CASE(15,
'_',
'p', __private_macro);
449 CASE(16,
'_',
'i', __include_macros);
462 unsigned NumBuckets = HashTable.getNumBuckets();
463 unsigned NumIdentifiers = HashTable.getNumItems();
464 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
465 unsigned AverageIdentifierSize = 0;
466 unsigned MaxIdentifierLength = 0;
469 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
470 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
471 unsigned IdLen = I->getKeyLength();
472 AverageIdentifierSize += IdLen;
473 if (MaxIdentifierLength < IdLen)
474 MaxIdentifierLength = IdLen;
477 fprintf(
stderr,
"\n*** Identifier Table Stats:\n");
478 fprintf(
stderr,
"# Identifiers: %d\n", NumIdentifiers);
479 fprintf(
stderr,
"# Empty Buckets: %d\n", NumEmptyBuckets);
480 fprintf(
stderr,
"Hash density (#identifiers per bucket): %f\n",
481 NumIdentifiers/(
double)NumBuckets);
482 fprintf(
stderr,
"Ave identifier length: %f\n",
483 (AverageIdentifierSize/(
double)NumIdentifiers));
484 fprintf(
stderr,
"Max identifier length: %d\n", MaxIdentifierLength);
487 HashTable.getAllocator().PrintStats();
499 assert(!Names.empty() &&
"must have >= 1 selector slots");
502 for (
unsigned I = 0, E = Names.size(); I != E; ++I) {
514 unsigned IIF = getIdentifierInfoFlag();
526 if (getIdentifierInfoFlag() < MultiArg) {
527 assert(argIndex == 0 &&
"illegal keyword index");
528 return getAsIdentifierInfo();
538 return II ? II->
getName() : StringRef();
543 llvm::raw_svector_ostream OS(Str);
546 OS << (*I)->getName();
550 return std::string(OS.str());
555 return "<null selector>";
557 if (getIdentifierInfoFlag() < MultiArg) {
561 assert(II &&
"If the number of arguments is 0 then II is guaranteed to "
563 return std::string(II->
getName());
569 return II->
getName().str() +
":";
573 return getMultiKeywordSelector()->getName();
586 if (name.size() < word.size())
return false;
587 return ((name.size() == word.size() || !
isLowercase(name[word.size()])) &&
588 name.starts_with(word));
603 if (name ==
"self")
return OMF_self;
607 if (name ==
"performSelector" || name ==
"performSelectorInBackground" ||
608 name ==
"performSelectorOnMainThread")
615 switch (
name.front()) {
642 StringRef name = first->
getName();
645 switch (name.front()) {
670 StringRef name = first->
getName();
672 switch (name.front()) {
682 if (name ==
"localizedStringWithFormat")
return SFF_NSString;
686 if (name ==
"stringByAppendingFormat" ||
695struct SelectorTableImpl {
696 llvm::FoldingSet<MultiKeywordSelector> Table;
697 llvm::BumpPtrAllocator Allocator;
703 return *
static_cast<SelectorTableImpl*
>(P);
725 assert(Name.starts_with(
"set") &&
"invalid setter name");
726 return (Twine(
toLowercase(Name[3])) + Name.drop_front(4)).str();
731 return SelTabImpl.Allocator.getTotalMemory();
742 llvm::FoldingSetNodeID ID;
745 void *InsertPos =
nullptr;
747 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
757 SelTabImpl.Table.InsertNode(SI, InsertPos);
762 Impl =
new SelectorTableImpl();
775#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
776 case OO_##Name: return Spelling;
777#include "clang/Basic/OperatorKinds.def"
780 llvm_unreachable(
"Invalid OverloadedOperatorKind!");
784 bool isContextSensitive) {
787 return isContextSensitive ?
"nonnull" :
"_Nonnull";
790 return isContextSensitive ?
"nullable" :
"_Nullable";
793 assert(!isContextSensitive &&
794 "_Nullable_result isn't supported as context-sensitive keyword");
795 return "_Nullable_result";
798 return isContextSensitive ?
"null_unspecified" :
"_Null_unspecified";
800 llvm_unreachable(
"Unknown nullability kind.");
807 return OS <<
"NonNull";
809 return OS <<
"Nullable";
811 return OS <<
"NullableResult";
813 return OS <<
"Unspecified";
815 llvm_unreachable(
"Unknown nullability kind.");
823 unsigned Flags = llvm::StringSwitch<unsigned>(II.
getName())
824#define KEYWORD(NAME, FLAGS) .Case(#NAME, FLAGS)
825#include "clang/Basic/TokenKinds.def"
829 if (LangOpts.CPlusPlus) {
831 return diag::warn_cxx11_keyword;
838 return diag::warn_cxx20_keyword;
841 return diag::warn_c99_keyword;
843 return diag::warn_c23_keyword;
847 "Keyword not known to come from a newer Standard or proposed Standard");
static void AddObjCKeyword(StringRef Name, tok::ObjCKeywordKind ObjCID, IdentifierTable &Table)
AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or "property".
static bool IsKeywordInCpp(unsigned Flags)
static void AddCXXOperatorKeyword(StringRef Keyword, tok::TokenKind TokenCode, IdentifierTable &Table)
AddCXXOperatorKeyword - Register a C++ operator keyword alternative representations.
static void AddNotableIdentifier(StringRef Name, tok::NotableIdentifierKind BTID, IdentifierTable &Table)
static void MarkIdentifierAsKeywordInCpp(IdentifierTable &Table, StringRef Name)
static KeywordStatus getKeywordStatusHelper(const LangOptions &LangOpts, TokenKey Flag)
static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts, tok::TokenKind K)
Checks if the specified token kind represents a keyword in the specified language.
static bool startsWithWord(StringRef name, StringRef word)
Interpreting the given string using the normal CamelCase conventions, determine whether the given str...
#define CASE(LEN, FIRST, THIRD, NAME)
static void AddKeyword(StringRef Keyword, tok::TokenKind TokenCode, unsigned Flags, const LangOptions &LangOpts, IdentifierTable &Table)
AddKeyword - This method is used to associate a token ID with specific identifiers because they are l...
static SelectorTableImpl & getSelectorTableImpl(void *P)
#define HASH(LEN, FIRST, THIRD)
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Defines the clang::LangOptions interface.
Defines an enumeration for C++ overloaded operators.
Defines various enumerations that describe declaration and type specifiers.
Enumerates target-specific builtins in their own namespaces within namespace clang.
Defines the clang::TokenKind enum and support functions.
Provides lookups to, and iteration over, IdentiferInfo objects.
virtual ~IdentifierInfoLookup()
virtual IdentifierIterator * getIdentifiers()
Retrieve an iterator into the set of all identifiers known to this identifier lookup source.
One of these records is kept for each identifier that is lexed.
bool isCPlusPlusKeyword(const LangOptions &LangOpts) const
Return true if this token is a C++ keyword in the specified language.
unsigned getLength() const
Efficiently return the length of this identifier info.
void setModulesImport(bool I)
Set whether this identifier is the contextual keyword import.
void setNotableIdentifierID(unsigned ID)
void setIsExtensionToken(bool Val)
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
tok::TokenKind getTokenID() const
If this is a source-language token (e.g.
void setObjCKeywordID(tok::ObjCKeywordKind ID)
void setHandleIdentifierCase(bool Val=true)
void setIsKeywordInCPlusPlus(bool Val=true)
const char * getNameStart() const
Return the beginning of the actual null-terminated string for this identifier.
bool isKeyword(const LangOptions &LangOpts) const
Return true if this token is a keyword in the specified language.
ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const
Determine whether this is a name reserved for the implementation (C99 7.1.3, C++ [lib....
void setIsCPlusPlusOperatorKeyword(bool Val=true)
isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether this identifier is a C++ al...
ReservedLiteralSuffixIdStatus isReservedLiteralSuffixId() const
Determine whether this is a name reserved for future standardization or the implementation (C++ [usrl...
void setIsFutureCompatKeyword(bool Val)
StringRef deuglifiedName() const
If the identifier is an "uglified" reserved name, return a cleaned form.
StringRef getName() const
Return the actual identifier string.
bool isFutureCompatKeyword() const
is/setIsFutureCompatKeyword - Initialize information about whether or not this language token is a ke...
An iterator that walks over all of the known identifiers in the lookup table.
virtual ~IdentifierIterator()
Implements an efficient mapping from strings to IdentifierInfo nodes.
IdentifierTable(IdentifierInfoLookup *ExternalLookup=nullptr)
Create the identifier table.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
void PrintStats() const
Print some statistics to stderr that indicate how well the hashing is doing.
void AddKeywords(const LangOptions &LangOpts)
Populate the identifier table with info about the language keywords for the language specified by Lan...
diag::kind getFutureCompatDiagKind(const IdentifierInfo &II, const LangOptions &LangOpts)
Returns the correct diagnostic to issue for a future-compat diagnostic warning.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
bool isCompatibleWithMSVC() const
One of these variable length records is kept for each selector containing more than one keyword.
keyword_iterator keyword_end() const
const IdentifierInfo *const * keyword_iterator
std::string getName() const
static void Profile(llvm::FoldingSetNodeID &ID, keyword_iterator ArgTys, unsigned NumArgs)
keyword_iterator keyword_begin() const
const IdentifierInfo * getIdentifierInfoForSlot(unsigned i) const
static std::string getPropertyNameFromSetterSelector(Selector Sel)
Return the property name for the given setter selector.
static Selector constructSetterSelector(IdentifierTable &Idents, SelectorTable &SelTable, const IdentifierInfo *Name)
Return the default setter selector for the given identifier.
size_t getTotalMemory() const
Return the total amount of memory allocated for managing selectors.
Selector getSelector(unsigned NumArgs, const IdentifierInfo **IIV)
Can create any sort of selector.
Selector getUnarySelector(const IdentifierInfo *ID)
static SmallString< 64 > constructSetterName(StringRef Name)
Return the default setter name for the given identifier.
Smart pointer class that efficiently represents Objective-C method names.
StringRef getNameForSlot(unsigned argIndex) const
Retrieve the name at a given position in the selector.
const IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
std::string getAsString() const
Derive the full selector name (e.g.
void print(llvm::raw_ostream &OS) const
Prints the full selector name (e.g. "foo:bar:").
void * getAsOpaquePtr() const
bool isKeywordSelector() const
static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel)
bool isUnarySelector() const
bool isNull() const
Determine whether this is the empty selector.
unsigned getNumArgs() const
unsigned kind
All of the diagnostics that can be emitted by the frontend.
NotableIdentifierKind
Provides a namespace for notable identifers such as float_t and double_t.
ObjCKeywordKind
Provides a namespace for Objective-C keywords which start with an '@'.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a '#' at the beginning of the line.
The JSON file list parser is used to communicate input to InstallAPI.
TokenKey
Constants for TokenKinds.def.
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
@ OO_None
Not an overloaded operator.
@ NUM_OVERLOADED_OPERATORS
KeywordStatus getKeywordStatus(const LangOptions &LangOpts, unsigned Flags)
Translates flags as specified in TokenKinds.def into keyword status in the given language standard.
NullabilityKind
Describes the nullability of a particular type.
@ Nullable
Values of this type can be null.
@ Unspecified
Whether values of this type can be null is (explicitly) unspecified.
@ NonNull
Values of this type can never be null.
LLVM_READONLY char toLowercase(char c)
Converts the given ASCII character to its lowercase equivalent.
ObjCMethodFamily
A family of Objective-C methods.
@ OMF_None
No particular method family.
llvm::StringRef getNullabilitySpelling(NullabilityKind kind, bool isContextSensitive=false)
Retrieve the spelling of the given nullability kind.
ObjCInstanceTypeFamily
A family of Objective-C methods.
ReservedLiteralSuffixIdStatus
@ NotStartsWithUnderscore
@ ContainsDoubleUnderscore
LLVM_READONLY bool isLowercase(unsigned char c)
Return true if this character is a lowercase ASCII letter: [a-z].
LLVM_READONLY char toUppercase(char c)
Converts the given ASCII character to its uppercase equivalent.
@ Keyword
The name has been typo-corrected to a keyword.
static constexpr int InterestingIdentifierBits
KeywordStatus
How a keyword is treated in the selected standard.
static constexpr uint64_t LargestBuiltinID
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ConceptReference *C)
Insertion operator for diagnostics.
const char * getOperatorSpelling(OverloadedOperatorKind Operator)
Retrieve the spelling of the given overloaded operator, without the preceding "operator" keyword.
@ StartsWithDoubleUnderscore
@ ContainsDoubleUnderscore
@ StartsWithUnderscoreAtGlobalScope
__LIBC_ATTRS FILE * stderr