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;
168 llvm_unreachable(
"Unknown KeywordStatus flag");
179 if (LangOpts.MSVCCompat && (Flags &
KEYNOMS18) &&
182 if (LangOpts.ZOSExt && (Flags &
KEYNOZOS))
187 unsigned CurFlag = Flags & ~(Flags - 1);
188 Flags = Flags & ~CurFlag;
189 CurStatus = std::max(
259 if (BTID != tok::not_notable) {
269#define KEYWORD(NAME, FLAGS) \
270 AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
271 FLAGS, LangOpts, *this);
272#define ALIAS(NAME, TOK, FLAGS) \
273 AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
274 FLAGS, LangOpts, *this);
275#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
276 if (LangOpts.CXXOperatorNames) \
277 AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this); \
279 MarkIdentifierAsKeywordInCpp(*this, StringRef(#NAME));
280#define OBJC_AT_KEYWORD(NAME) \
282 AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
283#define NOTABLE_IDENTIFIER(NAME) \
284 AddNotableIdentifier(StringRef(#NAME), tok::NAME, *this);
286#define TESTING_KEYWORD(NAME, FLAGS)
287#include "clang/Basic/TokenKinds.def"
289 if (LangOpts.ParseUnknownAnytype)
293 if (LangOpts.DeclSpecKeyword)
296 if (LangOpts.IEEE128)
309#define KEYWORD(NAME, FLAGS) \
310 case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
311#include "clang/Basic/TokenKinds.def"
331 if (!LangOpts.CPlusPlus || !
isKeyword(LangOpts))
336 LangOptsNoCPP.CPlusPlus =
false;
337 LangOptsNoCPP.CPlusPlus11 =
false;
338 LangOptsNoCPP.CPlusPlus20 =
false;
348 if (Name.size() <= 1)
352 if (Name[0] ==
'_') {
359 if (
'A' <= Name[1] && Name[1] <=
'Z')
360 return ReservedIdentifierStatus::
361 StartsWithUnderscoreFollowedByCapitalLetter;
369 if (LangOpts.CPlusPlus && Name.contains(
"__"))
385 if (Name.contains(
"__"))
393 if (Name.size() >= 2 && Name.front() ==
'_' &&
394 (Name[1] ==
'_' || (Name[1] >=
'A' && Name[1] <=
'Z')))
395 return Name.ltrim(
'_');
405#define HASH(LEN, FIRST, THIRD) \
406 (LEN << 6) + (((FIRST - 'a') - (THIRD - 'a')) & 63)
407#define CASE(LEN, FIRST, THIRD, NAME) \
408 case HASH(LEN, FIRST, THIRD): \
409 return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
412 if (Len < 2)
return tok::pp_not_keyword;
414 switch (
HASH(Len, Name[0], Name[2])) {
415 default:
return tok::pp_not_keyword;
416 CASE( 2,
'i',
'\0',
if);
417 CASE( 4,
'e',
'i', elif);
418 CASE( 4,
'e',
's',
else);
419 CASE( 4,
'l',
'n', line);
420 CASE( 4,
's',
'c', sccs);
421 CASE( 5,
'e',
'b', embed);
422 CASE( 5,
'e',
'd', endif);
423 CASE( 5,
'e',
'r', error);
424 CASE( 5,
'i',
'e', ident);
425 CASE( 5,
'i',
'd', ifdef);
426 CASE( 5,
'u',
'd', undef);
428 CASE( 6,
'a',
's', assert);
429 CASE( 6,
'd',
'f', define);
430 CASE( 6,
'i',
'n', ifndef);
431 CASE( 6,
'i',
'p',
import);
432 CASE( 6,
'p',
'a', pragma);
434 CASE( 7,
'd',
'f', defined);
435 CASE( 7,
'e',
'i', elifdef);
436 CASE( 7,
'i',
'c', include);
437 CASE( 7,
'w',
'r', warning);
439 CASE( 8,
'e',
'i', elifndef);
440 CASE( 8,
'u',
'a', unassert);
441 CASE(12,
'i',
'c', include_next);
443 CASE(14,
'_',
'p', __public_macro);
445 CASE(15,
'_',
'p', __private_macro);
447 CASE(16,
'_',
'i', __include_macros);
460 unsigned NumBuckets = HashTable.getNumBuckets();
461 unsigned NumIdentifiers = HashTable.getNumItems();
462 unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
463 unsigned AverageIdentifierSize = 0;
464 unsigned MaxIdentifierLength = 0;
467 for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
468 I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
469 unsigned IdLen = I->getKeyLength();
470 AverageIdentifierSize += IdLen;
471 if (MaxIdentifierLength < IdLen)
472 MaxIdentifierLength = IdLen;
475 fprintf(stderr,
"\n*** Identifier Table Stats:\n");
476 fprintf(stderr,
"# Identifiers: %d\n", NumIdentifiers);
477 fprintf(stderr,
"# Empty Buckets: %d\n", NumEmptyBuckets);
478 fprintf(stderr,
"Hash density (#identifiers per bucket): %f\n",
479 NumIdentifiers/(
double)NumBuckets);
480 fprintf(stderr,
"Ave identifier length: %f\n",
481 (AverageIdentifierSize/(
double)NumIdentifiers));
482 fprintf(stderr,
"Max identifier length: %d\n", MaxIdentifierLength);
485 HashTable.getAllocator().PrintStats();
497 assert(!Names.empty() &&
"must have >= 1 selector slots");
500 for (
unsigned I = 0, E = Names.size(); I != E; ++I) {
512 unsigned IIF = getIdentifierInfoFlag();
524 if (getIdentifierInfoFlag() < MultiArg) {
525 assert(argIndex == 0 &&
"illegal keyword index");
526 return getAsIdentifierInfo();
536 return II ? II->
getName() : StringRef();
541 llvm::raw_svector_ostream OS(Str);
544 OS << (*I)->getName();
548 return std::string(OS.str());
553 return "<null selector>";
555 if (getIdentifierInfoFlag() < MultiArg) {
559 assert(II &&
"If the number of arguments is 0 then II is guaranteed to "
561 return std::string(II->
getName());
567 return II->
getName().str() +
":";
571 return getMultiKeywordSelector()->getName();
584 if (name.size() < word.size())
return false;
585 return ((name.size() == word.size() || !
isLowercase(name[word.size()])) &&
586 name.starts_with(word));
601 if (name ==
"self")
return OMF_self;
605 if (name ==
"performSelector" || name ==
"performSelectorInBackground" ||
606 name ==
"performSelectorOnMainThread")
613 switch (
name.front()) {
640 StringRef name = first->
getName();
643 switch (name.front()) {
668 StringRef name = first->
getName();
670 switch (name.front()) {
680 if (name ==
"localizedStringWithFormat")
return SFF_NSString;
684 if (name ==
"stringByAppendingFormat" ||
693struct SelectorTableImpl {
694 llvm::FoldingSet<MultiKeywordSelector> Table;
695 llvm::BumpPtrAllocator Allocator;
701 return *
static_cast<SelectorTableImpl*
>(P);
723 assert(Name.starts_with(
"set") &&
"invalid setter name");
724 return (Twine(
toLowercase(Name[3])) + Name.drop_front(4)).str();
729 return SelTabImpl.Allocator.getTotalMemory();
740 llvm::FoldingSetNodeID ID;
743 void *InsertPos =
nullptr;
745 SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
755 SelTabImpl.Table.InsertNode(SI, InsertPos);
760 Impl =
new SelectorTableImpl();
773#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
774 case OO_##Name: return Spelling;
775#include "clang/Basic/OperatorKinds.def"
778 llvm_unreachable(
"Invalid OverloadedOperatorKind!");
782 bool isContextSensitive) {
785 return isContextSensitive ?
"nonnull" :
"_Nonnull";
788 return isContextSensitive ?
"nullable" :
"_Nullable";
791 assert(!isContextSensitive &&
792 "_Nullable_result isn't supported as context-sensitive keyword");
793 return "_Nullable_result";
796 return isContextSensitive ?
"null_unspecified" :
"_Null_unspecified";
798 llvm_unreachable(
"Unknown nullability kind.");
805 return OS <<
"NonNull";
807 return OS <<
"Nullable";
809 return OS <<
"NullableResult";
811 return OS <<
"Unspecified";
813 llvm_unreachable(
"Unknown nullability kind.");
821 unsigned Flags = llvm::StringSwitch<unsigned>(II.
getName())
822#define KEYWORD(NAME, FLAGS) .Case(#NAME, FLAGS)
823#include "clang/Basic/TokenKinds.def"
827 if (LangOpts.CPlusPlus) {
829 return diag::warn_cxx11_keyword;
836 return diag::warn_cxx20_keyword;
839 return diag::warn_c99_keyword;
841 return diag::warn_c23_keyword;
845 "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(MSVCMajorVersion MajorVersion) 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