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)
314#define KEYWORD(NAME, FLAGS) \
315 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
316#include "clang/Basic/TokenKinds.def"
336 if (!LangOpts.CPlusPlus || !
isKeyword(LangOpts))
341 LangOptsNoCPP.CPlusPlus =
false;
342 LangOptsNoCPP.CPlusPlus11 =
false;
343 LangOptsNoCPP.CPlusPlus20 =
false;
353 if (Name.size() <= 1)
357 if (Name[0] ==
'_') {
364 if (
'A' <= Name[1] && Name[1] <=
'Z')
365 return ReservedIdentifierStatus::
366 StartsWithUnderscoreFollowedByCapitalLetter;
374 if (LangOpts.CPlusPlus && Name.contains(
"__"))
390 if (Name.contains(
"__"))
398 if (Name.size() >= 2 && Name.front() ==
'_' &&
399 (Name[1] ==
'_' || (Name[1] >=
'A' && Name[1] <=
'Z')))
400 return Name.ltrim(
'_');
410#define HASH(LEN, FIRST, THIRD) \
411 (LEN << 6) + (((FIRST - 'a') - (THIRD - 'a')) & 63)
412#define CASE(LEN, FIRST, THIRD, NAME) \
413 case HASH(LEN, FIRST, THIRD): \
414 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
417 if (Len < 2)
return tok::pp_not_keyword;
421 return tok::pp___preprocessed_import;
423 return tok::pp___preprocessed_module;
426 switch (
HASH(Len, Name[0], Name[2])) {
427 default:
return tok::pp_not_keyword;
428 CASE( 2,
'i',
'\0',
if);
429 CASE( 4,
'e',
'i', elif);
430 CASE( 4,
'e',
's',
else);
431 CASE( 4,
'l',
'n', line);
432 CASE( 4,
's',
'c', sccs);
433 CASE( 5,
'e',
'b', embed);
434 CASE( 5,
'e',
'd', endif);
435 CASE( 5,
'e',
'r', error);
436 CASE( 5,
'i',
'e', ident);
437 CASE( 5,
'i',
'd', ifdef);
438 CASE( 5,
'u',
'd', undef);
440 CASE( 6,
'a',
's', assert);
441 CASE( 6,
'd',
'f', define);
442 CASE( 6,
'i',
'n', ifndef);
443 CASE( 6,
'i',
'p',
import);
444 CASE( 6,
'm',
'd', module);
445 CASE( 6,
'p',
'a', pragma);
447 CASE( 7,
'd',
'f', defined);
448 CASE( 7,
'e',
'i', elifdef);
449 CASE( 7,
'i',
'c', include);
450 CASE( 7,
'w',
'r', warning);
452 CASE( 8,
'e',
'i', elifndef);
453 CASE( 8,
'u',
'a', unassert);
454 CASE(12,
'i',
'c', include_next);
456 CASE(14,
'_',
'p', __public_macro);
458 CASE(15,
'_',
'p', __private_macro);
460 CASE(16,
'_',
'i', __include_macros);
474 unsigned NumBuckets = HashTable.getNumBuckets();
475 unsigned NumIdentifiers = HashTable.getNumItems();
476 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
477 unsigned AverageIdentifierSize = 0;
478 unsigned MaxIdentifierLength = 0;
481 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
482 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
483 unsigned IdLen = I->getKeyLength();
484 AverageIdentifierSize += IdLen;
485 if (MaxIdentifierLength < IdLen)
486 MaxIdentifierLength = IdLen;
489 fprintf(
stderr,
"\n*** Identifier Table Stats:\n");
490 fprintf(
stderr,
"# Identifiers: %d\n", NumIdentifiers);
491 fprintf(
stderr,
"# Empty Buckets: %d\n", NumEmptyBuckets);
492 fprintf(
stderr,
"Hash density (#identifiers per bucket): %f\n",
493 NumIdentifiers/(
double)NumBuckets);
494 fprintf(
stderr,
"Ave identifier length: %f\n",
495 (AverageIdentifierSize/(
double)NumIdentifiers));
496 fprintf(
stderr,
"Max identifier length: %d\n", MaxIdentifierLength);
499 HashTable.getAllocator().PrintStats();
511 assert(!Names.empty() &&
"must have >= 1 selector slots");
514 for (
unsigned I = 0, E = Names.size(); I != E; ++I) {
526 unsigned IIF = getIdentifierInfoFlag();
538 if (getIdentifierInfoFlag() < MultiArg) {
539 assert(argIndex == 0 &&
"illegal keyword index");
540 return getAsIdentifierInfo();
550 return II ? II->
getName() : StringRef();
555 llvm::raw_svector_ostream OS(Str);
558 OS << (*I)->getName();
562 return std::string(OS.str());
567 return "<null selector>";
569 if (getIdentifierInfoFlag() < MultiArg) {
573 assert(II &&
"If the number of arguments is 0 then II is guaranteed to "
575 return std::string(II->
getName());
581 return II->
getName().str() +
":";
585 return getMultiKeywordSelector()->getName();
598 if (name.size() < word.size())
return false;
599 return ((name.size() == word.size() || !
isLowercase(name[word.size()])) &&
600 name.starts_with(word));
615 if (name ==
"self")
return OMF_self;
619 if (name ==
"performSelector" || name ==
"performSelectorInBackground" ||
620 name ==
"performSelectorOnMainThread")
627 switch (
name.front()) {
654 StringRef name = first->
getName();
657 switch (name.front()) {
682 StringRef name = first->
getName();
684 switch (name.front()) {
694 if (name ==
"localizedStringWithFormat")
return SFF_NSString;
698 if (name ==
"stringByAppendingFormat" ||
707struct SelectorTableImpl {
708 llvm::FoldingSet<MultiKeywordSelector> Table;
709 llvm::BumpPtrAllocator Allocator;
715 return *
static_cast<SelectorTableImpl*
>(P);
737 assert(Name.starts_with(
"set") &&
"invalid setter name");
738 return (Twine(
toLowercase(Name[3])) + Name.drop_front(4)).str();
743 return SelTabImpl.Allocator.getTotalMemory();
754 llvm::FoldingSetNodeID ID;
757 void *InsertPos =
nullptr;
759 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
769 SelTabImpl.Table.InsertNode(SI, InsertPos);
774 Impl =
new SelectorTableImpl();
787#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
788 case OO_##Name: return Spelling;
789#include "clang/Basic/OperatorKinds.def"
792 llvm_unreachable(
"Invalid OverloadedOperatorKind!");
796 bool isContextSensitive) {
799 return isContextSensitive ?
"nonnull" :
"_Nonnull";
802 return isContextSensitive ?
"nullable" :
"_Nullable";
805 assert(!isContextSensitive &&
806 "_Nullable_result isn't supported as context-sensitive keyword");
807 return "_Nullable_result";
810 return isContextSensitive ?
"null_unspecified" :
"_Null_unspecified";
812 llvm_unreachable(
"Unknown nullability kind.");
819 return OS <<
"NonNull";
821 return OS <<
"Nullable";
823 return OS <<
"NullableResult";
825 return OS <<
"Unspecified";
827 llvm_unreachable(
"Unknown nullability kind.");
835 unsigned Flags = llvm::StringSwitch<unsigned>(II.
getName())
836#define KEYWORD(NAME, FLAGS) .Case(#NAME, FLAGS)
837#include "clang/Basic/TokenKinds.def"
841 if (LangOpts.CPlusPlus) {
843 return diag::warn_cxx11_keyword;
850 return diag::warn_cxx20_keyword;
853 return diag::warn_c99_keyword;
855 return diag::warn_c23_keyword;
859 "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.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
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.
bool isModuleKeyword() const
Determine whether this is the contextual keyword module.
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 setModuleKeyword(bool Val)
Set whether this identifier is the contextual keyword module.
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.
bool isImportKeyword() const
Determine whether this is the contextual keyword import.
ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const
Determine whether this is a name reserved for the implementation (C99 7.1.3, C++ [lib....
void setKeywordImport(bool Val)
Set whether this identifier is the contextual keyword import.
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