2007-07-11 21:01:13 +04:00
|
|
|
//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the IdentifierInfo, IdentifierVisitor, and
|
|
|
|
// IdentifierTable interfaces.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-07 12:58:51 +04:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2007-10-05 22:42:47 +04:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2007-10-06 00:15:24 +04:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-10-24 23:06:02 +04:00
|
|
|
#include "llvm/Bitcode/Serialize.h"
|
|
|
|
#include "llvm/Bitcode/Deserialize.h"
|
2007-10-24 02:18:37 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IdentifierInfo Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-21 02:28:34 +03:00
|
|
|
IdentifierInfo::IdentifierInfo() {
|
2007-07-11 21:01:13 +04:00
|
|
|
TokenID = tok::identifier;
|
2008-11-06 19:32:23 +03:00
|
|
|
ObjCOrBuiltinID = 0;
|
2007-10-07 11:09:52 +04:00
|
|
|
HasMacro = false;
|
2007-07-11 21:01:13 +04:00
|
|
|
IsExtension = false;
|
|
|
|
IsPoisoned = false;
|
|
|
|
IsCPPOperatorKeyword = false;
|
2009-01-21 10:43:11 +03:00
|
|
|
NeedsHandleIdentifier = false;
|
2007-07-11 21:01:13 +04:00
|
|
|
FETokenInfo = 0;
|
2009-01-21 02:28:34 +03:00
|
|
|
Entry = 0;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IdentifierTable Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-15 21:47:46 +03:00
|
|
|
IdentifierInfoLookup::~IdentifierInfoLookup() {}
|
|
|
|
|
|
|
|
IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
|
|
|
|
IdentifierInfoLookup* externalLookup)
|
|
|
|
: HashTable(8192), // Start with space for 8K identifiers.
|
|
|
|
ExternalLookup(externalLookup) {
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Populate the identifier table with info about keywords for the current
|
|
|
|
// language.
|
|
|
|
AddKeywords(LangOpts);
|
|
|
|
}
|
|
|
|
|
2007-10-24 02:18:37 +04:00
|
|
|
// This cstor is intended to be used only for serialization.
|
2008-11-13 02:21:09 +03:00
|
|
|
IdentifierTable::IdentifierTable()
|
2009-01-15 21:47:46 +03:00
|
|
|
: HashTable(8192), ExternalLookup(0) { }
|
2007-10-24 02:18:37 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Language Keyword Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// AddKeyword - This method is used to associate a token ID with specific
|
|
|
|
/// identifiers because they are language keywords. This causes the lexer to
|
|
|
|
/// automatically map matching identifiers to specialized token codes.
|
|
|
|
///
|
2007-07-16 08:18:29 +04:00
|
|
|
/// The C90/C99/CPP/CPP0x flags are set to 0 if the token should be
|
|
|
|
/// enabled in the specified langauge, set to 1 if it is an extension
|
|
|
|
/// in the specified language, and set to 2 if disabled in the
|
|
|
|
/// specified language.
|
2007-07-11 21:01:13 +04:00
|
|
|
static void AddKeyword(const char *Keyword, unsigned KWLen,
|
|
|
|
tok::TokenKind TokenCode,
|
2007-11-15 10:30:50 +03:00
|
|
|
int C90, int C99, int CXX, int CXX0x, int BoolSupport,
|
2007-07-11 21:01:13 +04:00
|
|
|
const LangOptions &LangOpts, IdentifierTable &Table) {
|
2007-11-15 10:30:50 +03:00
|
|
|
int Flags = 0;
|
|
|
|
if (BoolSupport != 0) {
|
2008-09-11 16:06:59 +04:00
|
|
|
Flags = LangOpts.CPlusPlus? 0 : LangOpts.Boolean ? BoolSupport : 2;
|
2007-11-15 10:30:50 +03:00
|
|
|
} else if (LangOpts.CPlusPlus) {
|
|
|
|
Flags = LangOpts.CPlusPlus0x ? CXX0x : CXX;
|
|
|
|
} else if (LangOpts.C99) {
|
|
|
|
Flags = C99;
|
|
|
|
} else {
|
|
|
|
Flags = C90;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Don't add this keyword if disabled in this language or if an extension
|
|
|
|
// and extensions are disabled.
|
|
|
|
if (Flags + LangOpts.NoExtensions >= 2) return;
|
|
|
|
|
|
|
|
IdentifierInfo &Info = Table.get(Keyword, Keyword+KWLen);
|
|
|
|
Info.setTokenID(TokenCode);
|
|
|
|
Info.setIsExtensionToken(Flags == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddAlias(const char *Keyword, unsigned KWLen,
|
2008-02-19 09:46:10 +03:00
|
|
|
tok::TokenKind AliaseeID,
|
2007-07-11 21:01:13 +04:00
|
|
|
const char *AliaseeKeyword, unsigned AliaseeKWLen,
|
|
|
|
const LangOptions &LangOpts, IdentifierTable &Table) {
|
|
|
|
IdentifierInfo &AliasInfo = Table.get(Keyword, Keyword+KWLen);
|
|
|
|
IdentifierInfo &AliaseeInfo = Table.get(AliaseeKeyword,
|
|
|
|
AliaseeKeyword+AliaseeKWLen);
|
2008-02-19 09:46:10 +03:00
|
|
|
AliasInfo.setTokenID(AliaseeID);
|
2007-07-11 21:01:13 +04:00
|
|
|
AliasInfo.setIsExtensionToken(AliaseeInfo.isExtensionToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
|
|
|
|
/// representations.
|
|
|
|
static void AddCXXOperatorKeyword(const char *Keyword, unsigned KWLen,
|
|
|
|
tok::TokenKind TokenCode,
|
|
|
|
IdentifierTable &Table) {
|
|
|
|
IdentifierInfo &Info = Table.get(Keyword, Keyword + KWLen);
|
|
|
|
Info.setTokenID(TokenCode);
|
2007-10-24 02:18:37 +04:00
|
|
|
Info.setIsCPlusPlusOperatorKeyword();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
|
|
|
|
/// "property".
|
|
|
|
static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID,
|
|
|
|
const char *Name, unsigned NameLen,
|
|
|
|
IdentifierTable &Table) {
|
|
|
|
Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AddKeywords - Add all keywords to the symbol table.
|
|
|
|
///
|
|
|
|
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
|
|
|
|
enum {
|
|
|
|
C90Shift = 0,
|
|
|
|
EXTC90 = 1 << C90Shift,
|
|
|
|
NOTC90 = 2 << C90Shift,
|
|
|
|
C99Shift = 2,
|
|
|
|
EXTC99 = 1 << C99Shift,
|
|
|
|
NOTC99 = 2 << C99Shift,
|
|
|
|
CPPShift = 4,
|
|
|
|
EXTCPP = 1 << CPPShift,
|
|
|
|
NOTCPP = 2 << CPPShift,
|
2007-07-16 08:18:29 +04:00
|
|
|
CPP0xShift = 6,
|
|
|
|
EXTCPP0x = 1 << CPP0xShift,
|
|
|
|
NOTCPP0x = 2 << CPP0xShift,
|
2007-11-15 10:30:50 +03:00
|
|
|
BoolShift = 8,
|
|
|
|
BOOLSUPPORT = 1 << BoolShift,
|
2007-07-11 21:01:13 +04:00
|
|
|
Mask = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add keywords and tokens for the current language.
|
|
|
|
#define KEYWORD(NAME, FLAGS) \
|
|
|
|
AddKeyword(#NAME, strlen(#NAME), tok::kw_ ## NAME, \
|
|
|
|
((FLAGS) >> C90Shift) & Mask, \
|
|
|
|
((FLAGS) >> C99Shift) & Mask, \
|
2007-07-16 08:18:29 +04:00
|
|
|
((FLAGS) >> CPPShift) & Mask, \
|
2007-11-15 10:30:50 +03:00
|
|
|
((FLAGS) >> CPP0xShift) & Mask, \
|
|
|
|
((FLAGS) >> BoolShift) & Mask, LangOpts, *this);
|
2007-07-11 21:01:13 +04:00
|
|
|
#define ALIAS(NAME, TOK) \
|
2008-02-19 09:46:10 +03:00
|
|
|
AddAlias(NAME, strlen(NAME), tok::kw_ ## TOK, #TOK, strlen(#TOK), \
|
|
|
|
LangOpts, *this);
|
2007-07-11 21:01:13 +04:00
|
|
|
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
|
|
|
|
if (LangOpts.CXXOperatorNames) \
|
|
|
|
AddCXXOperatorKeyword(#NAME, strlen(#NAME), tok::ALIAS, *this);
|
|
|
|
#define OBJC1_AT_KEYWORD(NAME) \
|
|
|
|
if (LangOpts.ObjC1) \
|
|
|
|
AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
|
|
|
|
#define OBJC2_AT_KEYWORD(NAME) \
|
|
|
|
if (LangOpts.ObjC2) \
|
|
|
|
AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
|
|
|
|
#include "clang/Basic/TokenKinds.def"
|
|
|
|
}
|
|
|
|
|
2007-10-07 11:52:34 +04:00
|
|
|
tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
|
|
|
|
// We use a perfect hash function here involving the length of the keyword,
|
|
|
|
// the first and third character. For preprocessor ID's there are no
|
|
|
|
// collisions (if there were, the switch below would complain about duplicate
|
|
|
|
// case values). Note that this depends on 'if' being null terminated.
|
|
|
|
|
|
|
|
#define HASH(LEN, FIRST, THIRD) \
|
|
|
|
(LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
|
|
|
|
#define CASE(LEN, FIRST, THIRD, NAME) \
|
|
|
|
case HASH(LEN, FIRST, THIRD): \
|
|
|
|
return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
|
|
|
|
|
|
|
|
unsigned Len = getLength();
|
2007-10-11 00:59:57 +04:00
|
|
|
if (Len < 2) return tok::pp_not_keyword;
|
2007-10-07 11:52:34 +04:00
|
|
|
const char *Name = getName();
|
|
|
|
switch (HASH(Len, Name[0], Name[2])) {
|
|
|
|
default: return tok::pp_not_keyword;
|
|
|
|
CASE( 2, 'i', '\0', if);
|
|
|
|
CASE( 4, 'e', 'i', elif);
|
|
|
|
CASE( 4, 'e', 's', else);
|
|
|
|
CASE( 4, 'l', 'n', line);
|
|
|
|
CASE( 4, 's', 'c', sccs);
|
|
|
|
CASE( 5, 'e', 'd', endif);
|
|
|
|
CASE( 5, 'e', 'r', error);
|
|
|
|
CASE( 5, 'i', 'e', ident);
|
|
|
|
CASE( 5, 'i', 'd', ifdef);
|
|
|
|
CASE( 5, 'u', 'd', undef);
|
|
|
|
|
|
|
|
CASE( 6, 'a', 's', assert);
|
|
|
|
CASE( 6, 'd', 'f', define);
|
|
|
|
CASE( 6, 'i', 'n', ifndef);
|
|
|
|
CASE( 6, 'i', 'p', import);
|
|
|
|
CASE( 6, 'p', 'a', pragma);
|
|
|
|
|
|
|
|
CASE( 7, 'd', 'f', defined);
|
|
|
|
CASE( 7, 'i', 'c', include);
|
|
|
|
CASE( 7, 'w', 'r', warning);
|
|
|
|
|
|
|
|
CASE( 8, 'u', 'a', unassert);
|
|
|
|
CASE(12, 'i', 'c', include_next);
|
|
|
|
#undef CASE
|
|
|
|
#undef HASH
|
|
|
|
}
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Stats Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// PrintStats - Print statistics about how well the identifier table is doing
|
|
|
|
/// at hashing identifiers.
|
|
|
|
void IdentifierTable::PrintStats() const {
|
|
|
|
unsigned NumBuckets = HashTable.getNumBuckets();
|
|
|
|
unsigned NumIdentifiers = HashTable.getNumItems();
|
|
|
|
unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
|
|
|
|
unsigned AverageIdentifierSize = 0;
|
|
|
|
unsigned MaxIdentifierLength = 0;
|
|
|
|
|
|
|
|
// TODO: Figure out maximum times an identifier had to probe for -stats.
|
2009-01-21 02:28:34 +03:00
|
|
|
for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
|
2007-07-11 21:01:13 +04:00
|
|
|
I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
|
|
|
|
unsigned IdLen = I->getKeyLength();
|
|
|
|
AverageIdentifierSize += IdLen;
|
|
|
|
if (MaxIdentifierLength < IdLen)
|
|
|
|
MaxIdentifierLength = IdLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "\n*** Identifier Table Stats:\n");
|
|
|
|
fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
|
|
|
|
fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
|
|
|
|
fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
|
|
|
|
NumIdentifiers/(double)NumBuckets);
|
|
|
|
fprintf(stderr, "Ave identifier length: %f\n",
|
|
|
|
(AverageIdentifierSize/(double)NumIdentifiers));
|
|
|
|
fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
|
|
|
|
|
|
|
|
// Compute statistics about the memory allocated for identifiers.
|
|
|
|
HashTable.getAllocator().PrintStats();
|
|
|
|
}
|
Add SelectorInfo (similar in spirit to IdentifierInfo). The key difference is SelectorInfo is not string-oriented, it is a unique aggregate of IdentifierInfo's (using a folding set). SelectorInfo also has a richer API that simplifies the parser/action interface. 3 noteworthy benefits:
#1: It is cleaner. I never "liked" storing keyword selectors (i.e. foo:bar:baz) in the IdentifierTable.
#2: It is more space efficient. Since Cocoa keyword selectors can be quite long, this technique is space saving. For Cocoa.h, pulling the keyword selectors out saves ~180k. The cost of the SelectorInfo data is ~100k. Saves ~80k, or 43%.
#3: It results in many API simplifications. Here are some highlights:
- Removed 3 actions (ActOnKeywordMessage, ActOnUnaryMessage, & one flavor of ObjcBuildMethodDeclaration that was specific to unary messages).
- Removed 3 funky structs from DeclSpec.h (ObjcKeywordMessage, ObjcKeywordDecl, and ObjcKeywordInfo).
- Removed 2 ivars and 2 constructors from ObjCMessageExpr (fyi, this space savings has not been measured).
I am happy with the way it turned out (though it took a bit more hacking than I expected). Given the central role of selectors in ObjC, making sure this is "right" will pay dividends later.
Thanks to Chris for talking this through with me and suggesting this approach.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42395 91177308-0d34-0410-b5e6-96231b3b80d8
2007-09-27 18:38:14 +04:00
|
|
|
|
2007-10-05 22:42:47 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SelectorTable Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-06 00:15:24 +04:00
|
|
|
unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
|
|
|
|
return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
|
|
|
|
}
|
|
|
|
|
2008-11-17 17:58:09 +03:00
|
|
|
namespace clang {
|
2007-10-05 22:42:47 +04:00
|
|
|
/// MultiKeywordSelector - One of these variable length records is kept for each
|
|
|
|
/// selector containing more than one keyword. We use a folding set
|
|
|
|
/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
|
|
|
|
/// this class is provided strictly through Selector.
|
2008-11-17 17:58:09 +03:00
|
|
|
class MultiKeywordSelector
|
|
|
|
: public DeclarationNameExtra, public llvm::FoldingSetNode {
|
2007-12-01 01:46:56 +03:00
|
|
|
friend SelectorTable* SelectorTable::CreateAndRegister(llvm::Deserializer&);
|
2008-11-17 17:58:09 +03:00
|
|
|
MultiKeywordSelector(unsigned nKeys) {
|
|
|
|
ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
|
|
|
|
}
|
2007-10-05 22:42:47 +04:00
|
|
|
public:
|
|
|
|
// Constructor for keyword selectors.
|
|
|
|
MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
|
|
|
|
assert((nKeys > 1) && "not a multi-keyword selector");
|
2008-11-17 17:58:09 +03:00
|
|
|
ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
|
2007-12-01 01:46:56 +03:00
|
|
|
|
2007-10-05 22:42:47 +04:00
|
|
|
// Fill in the trailing keyword array.
|
|
|
|
IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
|
|
|
|
for (unsigned i = 0; i != nKeys; ++i)
|
|
|
|
KeyInfo[i] = IIV[i];
|
2007-12-01 01:46:56 +03:00
|
|
|
}
|
|
|
|
|
2007-10-07 05:33:16 +04:00
|
|
|
// getName - Derive the full selector name and return it.
|
|
|
|
std::string getName() const;
|
|
|
|
|
2008-11-17 17:58:09 +03:00
|
|
|
unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
|
2007-10-05 22:42:47 +04:00
|
|
|
|
|
|
|
typedef IdentifierInfo *const *keyword_iterator;
|
|
|
|
keyword_iterator keyword_begin() const {
|
|
|
|
return reinterpret_cast<keyword_iterator>(this+1);
|
|
|
|
}
|
|
|
|
keyword_iterator keyword_end() const {
|
2008-11-17 17:58:09 +03:00
|
|
|
return keyword_begin()+getNumArgs();
|
2007-10-05 22:42:47 +04:00
|
|
|
}
|
2007-10-07 05:33:16 +04:00
|
|
|
IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
|
2008-11-17 17:58:09 +03:00
|
|
|
assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
|
2007-10-05 22:42:47 +04:00
|
|
|
return keyword_begin()[i];
|
|
|
|
}
|
|
|
|
static void Profile(llvm::FoldingSetNodeID &ID,
|
|
|
|
keyword_iterator ArgTys, unsigned NumArgs) {
|
|
|
|
ID.AddInteger(NumArgs);
|
2007-10-07 05:33:16 +04:00
|
|
|
for (unsigned i = 0; i != NumArgs; ++i)
|
|
|
|
ID.AddPointer(ArgTys[i]);
|
2007-10-05 22:42:47 +04:00
|
|
|
}
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) {
|
2008-11-17 17:58:09 +03:00
|
|
|
Profile(ID, keyword_begin(), getNumArgs());
|
2007-10-05 22:42:47 +04:00
|
|
|
}
|
|
|
|
};
|
2007-10-06 00:15:24 +04:00
|
|
|
} // end namespace clang.
|
2007-10-05 22:42:47 +04:00
|
|
|
|
|
|
|
unsigned Selector::getNumArgs() const {
|
|
|
|
unsigned IIF = getIdentifierInfoFlag();
|
|
|
|
if (IIF == ZeroArg)
|
|
|
|
return 0;
|
|
|
|
if (IIF == OneArg)
|
|
|
|
return 1;
|
|
|
|
// We point to a MultiKeywordSelector (pointer doesn't contain any flags).
|
|
|
|
MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
|
|
|
|
return SI->getNumArgs();
|
|
|
|
}
|
|
|
|
|
2007-10-07 05:33:16 +04:00
|
|
|
IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
|
|
|
|
if (IdentifierInfo *II = getAsIdentifierInfo()) {
|
|
|
|
assert(argIndex == 0 && "illegal keyword index");
|
2007-10-05 22:42:47 +04:00
|
|
|
return II;
|
|
|
|
}
|
|
|
|
// We point to a MultiKeywordSelector (pointer doesn't contain any flags).
|
|
|
|
MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
|
|
|
|
return SI->getIdentifierInfoForSlot(argIndex);
|
|
|
|
}
|
|
|
|
|
2007-10-07 05:33:16 +04:00
|
|
|
std::string MultiKeywordSelector::getName() const {
|
|
|
|
std::string Result;
|
|
|
|
unsigned Length = 0;
|
|
|
|
for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
|
|
|
|
if (*I)
|
|
|
|
Length += (*I)->getLength();
|
|
|
|
++Length; // :
|
|
|
|
}
|
|
|
|
|
|
|
|
Result.reserve(Length);
|
|
|
|
|
|
|
|
for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
|
|
|
|
if (*I)
|
|
|
|
Result.insert(Result.end(), (*I)->getName(),
|
|
|
|
(*I)->getName()+(*I)->getLength());
|
|
|
|
Result.push_back(':');
|
2007-10-05 22:42:47 +04:00
|
|
|
}
|
2007-10-07 05:33:16 +04:00
|
|
|
|
|
|
|
return Result;
|
2007-10-05 22:42:47 +04:00
|
|
|
}
|
|
|
|
|
2008-11-24 06:33:13 +03:00
|
|
|
std::string Selector::getAsString() const {
|
2007-10-07 05:33:16 +04:00
|
|
|
if (IdentifierInfo *II = getAsIdentifierInfo()) {
|
|
|
|
if (getNumArgs() == 0)
|
|
|
|
return II->getName();
|
|
|
|
|
|
|
|
std::string Res = II->getName();
|
|
|
|
Res += ":";
|
|
|
|
return Res;
|
2007-10-05 22:42:47 +04:00
|
|
|
}
|
2007-10-07 05:33:16 +04:00
|
|
|
|
|
|
|
// We have a multiple keyword selector (no embedded flags).
|
|
|
|
return reinterpret_cast<MultiKeywordSelector *>(InfoPtr)->getName();
|
2007-10-05 22:42:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-07 06:00:24 +04:00
|
|
|
Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
|
|
|
|
if (nKeys < 2)
|
|
|
|
return Selector(IIV[0], nKeys);
|
2007-10-07 05:33:16 +04:00
|
|
|
|
2007-10-05 22:42:47 +04:00
|
|
|
llvm::FoldingSet<MultiKeywordSelector> *SelTab;
|
|
|
|
|
|
|
|
SelTab = static_cast<llvm::FoldingSet<MultiKeywordSelector> *>(Impl);
|
|
|
|
|
|
|
|
// Unique selector, to guarantee there is one per name.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
MultiKeywordSelector::Profile(ID, IIV, nKeys);
|
|
|
|
|
|
|
|
void *InsertPos = 0;
|
2007-10-07 05:33:16 +04:00
|
|
|
if (MultiKeywordSelector *SI = SelTab->FindNodeOrInsertPos(ID, InsertPos))
|
2007-10-05 22:42:47 +04:00
|
|
|
return Selector(SI);
|
2007-10-07 05:33:16 +04:00
|
|
|
|
2007-10-05 22:42:47 +04:00
|
|
|
// MultiKeywordSelector objects are not allocated with new because they have a
|
|
|
|
// variable size array (for parameter types) at the end of them.
|
|
|
|
MultiKeywordSelector *SI =
|
|
|
|
(MultiKeywordSelector*)malloc(sizeof(MultiKeywordSelector) +
|
|
|
|
nKeys*sizeof(IdentifierInfo *));
|
|
|
|
new (SI) MultiKeywordSelector(nKeys, IIV);
|
|
|
|
SelTab->InsertNode(SI, InsertPos);
|
|
|
|
return Selector(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectorTable::SelectorTable() {
|
|
|
|
Impl = new llvm::FoldingSet<MultiKeywordSelector>;
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectorTable::~SelectorTable() {
|
|
|
|
delete static_cast<llvm::FoldingSet<MultiKeywordSelector> *>(Impl);
|
|
|
|
}
|
|
|
|
|
2007-10-24 02:18:37 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Serialization for IdentifierInfo and IdentifierTable.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-11-08 22:52:41 +03:00
|
|
|
void IdentifierInfo::Emit(llvm::Serializer& S) const {
|
|
|
|
S.EmitInt(getTokenID());
|
|
|
|
S.EmitInt(getBuiltinID());
|
|
|
|
S.EmitInt(getObjCKeywordID());
|
|
|
|
S.EmitBool(hasMacroDefinition());
|
|
|
|
S.EmitBool(isExtensionToken());
|
|
|
|
S.EmitBool(isPoisoned());
|
|
|
|
S.EmitBool(isCPlusPlusOperatorKeyword());
|
|
|
|
// FIXME: FETokenInfo
|
2007-10-24 02:18:37 +04:00
|
|
|
}
|
|
|
|
|
2007-11-08 22:52:41 +03:00
|
|
|
void IdentifierInfo::Read(llvm::Deserializer& D) {
|
|
|
|
setTokenID((tok::TokenKind) D.ReadInt());
|
|
|
|
setBuiltinID(D.ReadInt());
|
|
|
|
setObjCKeywordID((tok::ObjCKeywordKind) D.ReadInt());
|
|
|
|
setHasMacroDefinition(D.ReadBool());
|
|
|
|
setIsExtensionToken(D.ReadBool());
|
|
|
|
setIsPoisoned(D.ReadBool());
|
|
|
|
setIsCPlusPlusOperatorKeyword(D.ReadBool());
|
|
|
|
// FIXME: FETokenInfo
|
2007-10-24 02:18:37 +04:00
|
|
|
}
|
|
|
|
|
2007-11-08 22:52:41 +03:00
|
|
|
void IdentifierTable::Emit(llvm::Serializer& S) const {
|
|
|
|
S.EnterBlock();
|
2007-10-24 02:18:37 +04:00
|
|
|
|
2007-12-01 01:46:56 +03:00
|
|
|
S.EmitPtr(this);
|
|
|
|
|
2007-11-08 22:52:41 +03:00
|
|
|
for (iterator I=begin(), E=end(); I != E; ++I) {
|
|
|
|
const char* Key = I->getKeyData();
|
2009-01-21 02:28:34 +03:00
|
|
|
const IdentifierInfo* Info = I->getValue();
|
2007-11-08 22:52:41 +03:00
|
|
|
|
2007-11-10 05:11:55 +03:00
|
|
|
bool KeyRegistered = S.isRegistered(Key);
|
|
|
|
bool InfoRegistered = S.isRegistered(Info);
|
2007-11-08 22:52:41 +03:00
|
|
|
|
|
|
|
if (KeyRegistered || InfoRegistered) {
|
|
|
|
// These acrobatics are so that we don't incur the cost of registering
|
|
|
|
// a pointer with the backpatcher during deserialization if nobody
|
|
|
|
// references the object.
|
|
|
|
S.EmitPtr(InfoRegistered ? Info : NULL);
|
|
|
|
S.EmitPtr(KeyRegistered ? Key : NULL);
|
|
|
|
S.EmitCStr(Key);
|
|
|
|
S.Emit(*Info);
|
|
|
|
}
|
2007-10-24 02:18:37 +04:00
|
|
|
}
|
2007-11-08 22:52:41 +03:00
|
|
|
|
|
|
|
S.ExitBlock();
|
2007-10-24 02:18:37 +04:00
|
|
|
}
|
|
|
|
|
2007-12-01 01:46:56 +03:00
|
|
|
IdentifierTable* IdentifierTable::CreateAndRegister(llvm::Deserializer& D) {
|
2007-11-09 03:43:55 +03:00
|
|
|
llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
|
2007-11-08 22:52:41 +03:00
|
|
|
|
2007-10-24 02:18:37 +04:00
|
|
|
std::vector<char> buff;
|
|
|
|
buff.reserve(200);
|
2007-11-08 22:52:41 +03:00
|
|
|
|
|
|
|
IdentifierTable* t = new IdentifierTable();
|
2007-12-01 01:46:56 +03:00
|
|
|
D.RegisterPtr(t);
|
2007-10-24 02:18:37 +04:00
|
|
|
|
2007-11-08 22:52:41 +03:00
|
|
|
while (!D.FinishedBlock(BLoc)) {
|
|
|
|
llvm::SerializedPtrID InfoPtrID = D.ReadPtrID();
|
|
|
|
llvm::SerializedPtrID KeyPtrID = D.ReadPtrID();
|
|
|
|
|
2007-10-24 23:06:02 +04:00
|
|
|
D.ReadCStr(buff);
|
2009-01-21 02:28:34 +03:00
|
|
|
IdentifierInfo *II = &t->get(&buff[0], &buff[0] + buff.size());
|
|
|
|
II->Read(D);
|
2007-11-08 22:52:41 +03:00
|
|
|
|
2009-01-21 02:28:34 +03:00
|
|
|
if (InfoPtrID) D.RegisterRef(InfoPtrID, II);
|
|
|
|
if (KeyPtrID) D.RegisterPtr(KeyPtrID, II->getName());
|
2007-10-24 02:18:37 +04:00
|
|
|
}
|
2007-11-08 22:52:41 +03:00
|
|
|
|
2007-10-24 23:06:02 +04:00
|
|
|
return t;
|
|
|
|
}
|
2007-12-01 01:46:56 +03:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Serialization for Selector and SelectorTable.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void Selector::Emit(llvm::Serializer& S) const {
|
|
|
|
S.EmitInt(getIdentifierInfoFlag());
|
|
|
|
S.EmitPtr(reinterpret_cast<void*>(InfoPtr & ~ArgFlags));
|
|
|
|
}
|
|
|
|
|
|
|
|
Selector Selector::ReadVal(llvm::Deserializer& D) {
|
|
|
|
unsigned flag = D.ReadInt();
|
|
|
|
|
|
|
|
uintptr_t ptr;
|
|
|
|
D.ReadUIntPtr(ptr,false); // No backpatching.
|
|
|
|
|
|
|
|
return Selector(ptr | flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectorTable::Emit(llvm::Serializer& S) const {
|
|
|
|
typedef llvm::FoldingSet<MultiKeywordSelector>::iterator iterator;
|
|
|
|
llvm::FoldingSet<MultiKeywordSelector> *SelTab;
|
|
|
|
SelTab = static_cast<llvm::FoldingSet<MultiKeywordSelector> *>(Impl);
|
|
|
|
|
|
|
|
S.EnterBlock();
|
|
|
|
|
|
|
|
S.EmitPtr(this);
|
|
|
|
|
|
|
|
for (iterator I=SelTab->begin(), E=SelTab->end(); I != E; ++I) {
|
2007-12-01 07:43:17 +03:00
|
|
|
if (!S.isRegistered(&*I))
|
|
|
|
continue;
|
|
|
|
|
2007-12-01 01:46:56 +03:00
|
|
|
S.FlushRecord(); // Start a new record.
|
2007-12-01 07:43:17 +03:00
|
|
|
|
|
|
|
S.EmitPtr(&*I);
|
2007-12-01 01:46:56 +03:00
|
|
|
S.EmitInt(I->getNumArgs());
|
|
|
|
|
|
|
|
for (MultiKeywordSelector::keyword_iterator KI = I->keyword_begin(),
|
|
|
|
KE = I->keyword_end(); KI != KE; ++KI)
|
|
|
|
S.EmitPtr(*KI);
|
|
|
|
}
|
|
|
|
|
|
|
|
S.ExitBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectorTable* SelectorTable::CreateAndRegister(llvm::Deserializer& D) {
|
|
|
|
llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
|
|
|
|
|
|
|
|
SelectorTable* t = new SelectorTable();
|
|
|
|
D.RegisterPtr(t);
|
|
|
|
|
|
|
|
llvm::FoldingSet<MultiKeywordSelector>& SelTab =
|
|
|
|
*static_cast<llvm::FoldingSet<MultiKeywordSelector>*>(t->Impl);
|
|
|
|
|
|
|
|
while (!D.FinishedBlock(BLoc)) {
|
2007-12-01 07:43:17 +03:00
|
|
|
|
|
|
|
llvm::SerializedPtrID PtrID = D.ReadPtrID();
|
2007-12-01 01:46:56 +03:00
|
|
|
unsigned nKeys = D.ReadInt();
|
|
|
|
|
|
|
|
MultiKeywordSelector *SI =
|
|
|
|
(MultiKeywordSelector*)malloc(sizeof(MultiKeywordSelector) +
|
|
|
|
nKeys*sizeof(IdentifierInfo *));
|
|
|
|
|
|
|
|
new (SI) MultiKeywordSelector(nKeys);
|
2007-12-01 07:43:17 +03:00
|
|
|
|
|
|
|
D.RegisterPtr(PtrID,SI);
|
2007-12-01 01:46:56 +03:00
|
|
|
|
|
|
|
IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(SI+1);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != nKeys; ++i)
|
|
|
|
D.ReadPtr(KeyInfo[i],false);
|
|
|
|
|
|
|
|
SelTab.GetOrInsertNode(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|