2006-10-30 06:09:44 +08:00
|
|
|
//===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-07-05 01:53:21 +08:00
|
|
|
// This file implements the IdentifierInfo, IdentifierVisitor, and
|
2006-07-03 12:28:52 +08:00
|
|
|
// IdentifierTable interfaces.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-07 16:58:51 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2006-10-18 14:07:05 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2013-06-08 06:29:12 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2007-10-06 04:15:24 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2012-01-17 14:56:22 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-03-03 06:20:04 +08:00
|
|
|
#include <cstdio>
|
2007-10-24 06:18:37 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-07-05 01:53:21 +08:00
|
|
|
// IdentifierInfo Implementation
|
2006-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-21 07:28:34 +08:00
|
|
|
IdentifierInfo::IdentifierInfo() {
|
2006-10-29 07:46:24 +08:00
|
|
|
TokenID = tok::identifier;
|
2008-11-07 00:32:23 +08:00
|
|
|
ObjCOrBuiltinID = 0;
|
2007-10-07 15:09:52 +08:00
|
|
|
HasMacro = false;
|
2012-09-26 01:18:14 +08:00
|
|
|
HadMacro = false;
|
2006-10-29 07:46:24 +08:00
|
|
|
IsExtension = false;
|
2011-10-12 03:57:52 +08:00
|
|
|
IsCXX11CompatKeyword = false;
|
2006-10-29 07:46:24 +08:00
|
|
|
IsPoisoned = false;
|
2006-11-22 01:23:33 +08:00
|
|
|
IsCPPOperatorKeyword = false;
|
2009-01-21 15:43:11 +08:00
|
|
|
NeedsHandleIdentifier = false;
|
2010-08-19 07:57:06 +08:00
|
|
|
IsFromAST = false;
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
ChangedAfterLoad = false;
|
2010-08-12 06:55:12 +08:00
|
|
|
RevertedTokenID = false;
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
OutOfDate = false;
|
2012-03-02 06:07:04 +08:00
|
|
|
IsModulesImport = false;
|
2006-10-29 07:46:24 +08:00
|
|
|
FETokenInfo = 0;
|
2009-01-21 07:28:34 +08:00
|
|
|
Entry = 0;
|
2006-10-29 07:46:24 +08:00
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IdentifierTable Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-10-15 06:11:03 +08:00
|
|
|
IdentifierIterator::~IdentifierIterator() { }
|
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
IdentifierInfoLookup::~IdentifierInfoLookup() {}
|
|
|
|
|
2010-10-15 06:11:03 +08:00
|
|
|
namespace {
|
|
|
|
/// \brief A simple identifier lookup iterator that represents an
|
|
|
|
/// empty sequence of identifiers.
|
|
|
|
class EmptyLookupIterator : public IdentifierIterator
|
|
|
|
{
|
|
|
|
public:
|
2011-07-23 18:55:15 +08:00
|
|
|
virtual StringRef Next() { return StringRef(); }
|
2010-10-15 06:11:03 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-04-18 06:10:55 +08:00
|
|
|
IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
|
2010-10-15 06:11:03 +08:00
|
|
|
return new EmptyLookupIterator();
|
|
|
|
}
|
|
|
|
|
2009-04-26 07:30:02 +08:00
|
|
|
ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
|
|
|
|
|
2009-01-16 02:47:46 +08:00
|
|
|
IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
|
|
|
|
IdentifierInfoLookup* externalLookup)
|
|
|
|
: HashTable(8192), // Start with space for 8K identifiers.
|
|
|
|
ExternalLookup(externalLookup) {
|
2006-10-30 07:49:15 +08:00
|
|
|
|
2006-10-27 11:59:10 +08:00
|
|
|
// Populate the identifier table with info about keywords for the current
|
|
|
|
// language.
|
2006-10-18 14:07:05 +08:00
|
|
|
AddKeywords(LangOpts);
|
2012-03-02 06:53:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Add the '_experimental_modules_import' contextual keyword.
|
2012-12-12 06:11:52 +08:00
|
|
|
get("import").setModulesImport(true);
|
2006-07-03 12:28:52 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-10-18 14:07:05 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Language Keyword Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-04-28 11:13:54 +08:00
|
|
|
// Constants for TokenKinds.def
|
|
|
|
namespace {
|
|
|
|
enum {
|
2011-04-09 21:34:05 +08:00
|
|
|
KEYC99 = 0x1,
|
|
|
|
KEYCXX = 0x2,
|
2013-01-02 20:01:23 +08:00
|
|
|
KEYCXX11 = 0x4,
|
2011-04-09 21:34:05 +08:00
|
|
|
KEYGNU = 0x8,
|
|
|
|
KEYMS = 0x10,
|
|
|
|
BOOLSUPPORT = 0x20,
|
|
|
|
KEYALTIVEC = 0x40,
|
|
|
|
KEYNOCXX = 0x80,
|
|
|
|
KEYBORLAND = 0x100,
|
|
|
|
KEYOPENCL = 0x200,
|
2011-12-24 01:00:35 +08:00
|
|
|
KEYC11 = 0x400,
|
2011-06-16 07:02:42 +08:00
|
|
|
KEYARC = 0x800,
|
2012-07-24 14:17:24 +08:00
|
|
|
KEYNOMS = 0x01000,
|
2012-09-06 01:30:57 +08:00
|
|
|
WCHARSUPPORT = 0x02000,
|
2012-07-24 14:17:24 +08:00
|
|
|
KEYALL = (0xffff & ~KEYNOMS) // Because KEYNOMS is used to exclude.
|
2009-04-28 11:13:54 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2006-10-18 14:07:05 +08:00
|
|
|
/// 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.
|
|
|
|
///
|
2011-10-12 03:57:52 +08:00
|
|
|
/// The C90/C99/CPP/CPP0x flags are set to 3 if the token is a keyword in a
|
|
|
|
/// future language standard, set to 2 if the token should be enabled in the
|
2012-03-12 07:14:21 +08:00
|
|
|
/// specified language, set to 1 if it is an extension in the specified
|
2011-10-12 03:57:52 +08:00
|
|
|
/// language, and set to 0 if disabled in the specified language.
|
2011-07-23 18:55:15 +08:00
|
|
|
static void AddKeyword(StringRef Keyword,
|
2009-04-28 11:13:54 +08:00
|
|
|
tok::TokenKind TokenCode, unsigned Flags,
|
2006-10-18 14:07:05 +08:00
|
|
|
const LangOptions &LangOpts, IdentifierTable &Table) {
|
2009-04-28 11:13:54 +08:00
|
|
|
unsigned AddResult = 0;
|
2011-04-09 21:34:05 +08:00
|
|
|
if (Flags == KEYALL) AddResult = 2;
|
2009-04-28 11:13:54 +08:00
|
|
|
else if (LangOpts.CPlusPlus && (Flags & KEYCXX)) AddResult = 2;
|
2013-01-02 20:01:23 +08:00
|
|
|
else if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) AddResult = 2;
|
2009-04-28 11:13:54 +08:00
|
|
|
else if (LangOpts.C99 && (Flags & KEYC99)) AddResult = 2;
|
2010-04-18 04:17:31 +08:00
|
|
|
else if (LangOpts.GNUKeywords && (Flags & KEYGNU)) AddResult = 1;
|
2011-09-18 01:15:52 +08:00
|
|
|
else if (LangOpts.MicrosoftExt && (Flags & KEYMS)) AddResult = 1;
|
2010-09-03 09:29:35 +08:00
|
|
|
else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
|
2009-06-30 09:26:17 +08:00
|
|
|
else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
|
2012-09-06 01:30:57 +08:00
|
|
|
else if (LangOpts.WChar && (Flags & WCHARSUPPORT)) AddResult = 2;
|
2010-02-05 08:12:22 +08:00
|
|
|
else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
|
2011-02-14 09:42:53 +08:00
|
|
|
else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
|
2010-10-14 04:00:38 +08:00
|
|
|
else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
|
2011-12-24 01:00:35 +08:00
|
|
|
else if (LangOpts.C11 && (Flags & KEYC11)) AddResult = 2;
|
2011-12-20 05:06:15 +08:00
|
|
|
// We treat bridge casts as objective-C keywords so we can warn on them
|
|
|
|
// in non-arc mode.
|
|
|
|
else if (LangOpts.ObjC2 && (Flags & KEYARC)) AddResult = 2;
|
2013-01-02 20:01:23 +08:00
|
|
|
else if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) AddResult = 3;
|
2011-10-12 03:57:52 +08:00
|
|
|
|
2012-07-22 19:32:41 +08:00
|
|
|
// Don't add this keyword under MicrosoftMode.
|
|
|
|
if (LangOpts.MicrosoftMode && (Flags & KEYNOMS))
|
|
|
|
return;
|
2009-04-28 11:13:54 +08:00
|
|
|
// Don't add this keyword if disabled in this language.
|
|
|
|
if (AddResult == 0) return;
|
|
|
|
|
2011-10-12 03:57:52 +08:00
|
|
|
IdentifierInfo &Info =
|
|
|
|
Table.get(Keyword, AddResult == 3 ? tok::identifier : TokenCode);
|
2009-04-28 11:13:54 +08:00
|
|
|
Info.setIsExtensionToken(AddResult == 1);
|
2011-10-12 03:57:52 +08:00
|
|
|
Info.setIsCXX11CompatKeyword(AddResult == 3);
|
2006-10-18 14:07:05 +08:00
|
|
|
}
|
|
|
|
|
2006-11-22 01:23:33 +08:00
|
|
|
/// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
|
|
|
|
/// representations.
|
2011-07-23 18:55:15 +08:00
|
|
|
static void AddCXXOperatorKeyword(StringRef Keyword,
|
2006-11-22 01:23:33 +08:00
|
|
|
tok::TokenKind TokenCode,
|
|
|
|
IdentifierTable &Table) {
|
2010-08-12 06:55:12 +08:00
|
|
|
IdentifierInfo &Info = Table.get(Keyword, TokenCode);
|
2007-10-24 06:18:37 +08:00
|
|
|
Info.setIsCPlusPlusOperatorKeyword();
|
2006-11-22 01:23:33 +08:00
|
|
|
}
|
|
|
|
|
2012-06-16 05:27:44 +08:00
|
|
|
/// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
|
|
|
|
/// or "property".
|
2011-07-23 18:55:15 +08:00
|
|
|
static void AddObjCKeyword(StringRef Name,
|
2010-03-12 19:27:37 +08:00
|
|
|
tok::ObjCKeywordKind ObjCID,
|
2006-10-18 14:07:05 +08:00
|
|
|
IdentifierTable &Table) {
|
2010-03-12 19:27:37 +08:00
|
|
|
Table.get(Name).setObjCKeywordID(ObjCID);
|
2006-10-18 14:07:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// AddKeywords - Add all keywords to the symbol table.
|
|
|
|
///
|
|
|
|
void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
|
|
|
|
// Add keywords and tokens for the current language.
|
|
|
|
#define KEYWORD(NAME, FLAGS) \
|
2011-07-23 18:55:15 +08:00
|
|
|
AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
|
2009-04-28 11:13:54 +08:00
|
|
|
FLAGS, LangOpts, *this);
|
|
|
|
#define ALIAS(NAME, TOK, FLAGS) \
|
2011-07-23 18:55:15 +08:00
|
|
|
AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
|
2009-04-28 11:13:54 +08:00
|
|
|
FLAGS, LangOpts, *this);
|
2006-11-22 01:23:33 +08:00
|
|
|
#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
|
2006-12-04 15:48:37 +08:00
|
|
|
if (LangOpts.CXXOperatorNames) \
|
2011-07-23 18:55:15 +08:00
|
|
|
AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
|
2006-10-18 14:07:05 +08:00
|
|
|
#define OBJC1_AT_KEYWORD(NAME) \
|
|
|
|
if (LangOpts.ObjC1) \
|
2011-07-23 18:55:15 +08:00
|
|
|
AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
|
2006-10-18 14:07:05 +08:00
|
|
|
#define OBJC2_AT_KEYWORD(NAME) \
|
|
|
|
if (LangOpts.ObjC2) \
|
2011-07-23 18:55:15 +08:00
|
|
|
AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
|
2011-04-10 06:50:59 +08:00
|
|
|
#define TESTING_KEYWORD(NAME, FLAGS)
|
2006-10-18 14:07:05 +08:00
|
|
|
#include "clang/Basic/TokenKinds.def"
|
2011-04-10 06:50:59 +08:00
|
|
|
|
|
|
|
if (LangOpts.ParseUnknownAnytype)
|
|
|
|
AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
|
|
|
|
LangOpts, *this);
|
2006-10-18 14:07:05 +08:00
|
|
|
}
|
|
|
|
|
2007-10-07 15:52:34 +08: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.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-07 15:52:34 +08:00
|
|
|
#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
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-07 15:52:34 +08:00
|
|
|
unsigned Len = getLength();
|
2007-10-11 04:59:57 +08:00
|
|
|
if (Len < 2) return tok::pp_not_keyword;
|
2009-10-18 02:13:02 +08:00
|
|
|
const char *Name = getNameStart();
|
2007-10-07 15:52:34 +08:00
|
|
|
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);
|
2012-01-04 02:24:14 +08:00
|
|
|
|
2007-10-07 15:52:34 +08:00
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-04 03:48:16 +08:00
|
|
|
CASE(14, '_', 'p', __public_macro);
|
|
|
|
|
|
|
|
CASE(15, '_', 'p', __private_macro);
|
|
|
|
|
2009-04-09 02:24:34 +08:00
|
|
|
CASE(16, '_', 'i', __include_macros);
|
2007-10-07 15:52:34 +08:00
|
|
|
#undef CASE
|
|
|
|
#undef HASH
|
|
|
|
}
|
|
|
|
}
|
2006-10-18 14:07:05 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Stats Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// PrintStats - Print statistics about how well the identifier table is doing
|
|
|
|
/// at hashing identifiers.
|
|
|
|
void IdentifierTable::PrintStats() const {
|
2006-10-30 07:43:13 +08:00
|
|
|
unsigned NumBuckets = HashTable.getNumBuckets();
|
|
|
|
unsigned NumIdentifiers = HashTable.getNumItems();
|
|
|
|
unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
|
2006-06-18 13:43:12 +08:00
|
|
|
unsigned AverageIdentifierSize = 0;
|
|
|
|
unsigned MaxIdentifierLength = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-30 07:43:13 +08:00
|
|
|
// TODO: Figure out maximum times an identifier had to probe for -stats.
|
2009-01-21 07:28:34 +08:00
|
|
|
for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
|
2007-02-11 16:19:57 +08:00
|
|
|
I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
|
|
|
|
unsigned IdLen = I->getKeyLength();
|
|
|
|
AverageIdentifierSize += IdLen;
|
|
|
|
if (MaxIdentifierLength < IdLen)
|
|
|
|
MaxIdentifierLength = IdLen;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Compute statistics about the memory allocated for identifiers.
|
2006-10-30 07:43:13 +08:00
|
|
|
HashTable.getAllocator().PrintStats();
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
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.
llvm-svn: 42395
2007-09-27 22:38:14 +08:00
|
|
|
|
2007-10-06 02:42:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SelectorTable Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-10-06 04:15:24 +08:00
|
|
|
unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
|
|
|
|
return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
|
|
|
|
}
|
|
|
|
|
2008-11-17 22:58:09 +08:00
|
|
|
namespace clang {
|
2007-10-06 02:42:47 +08:00
|
|
|
/// MultiKeywordSelector - One of these variable length records is kept for each
|
|
|
|
/// selector containing more than one keyword. We use a folding set
|
2009-09-09 23:08:12 +08:00
|
|
|
/// to unique aggregate names (keyword selectors in ObjC parlance). Access to
|
2007-10-06 02:42:47 +08:00
|
|
|
/// this class is provided strictly through Selector.
|
2009-09-09 23:08:12 +08:00
|
|
|
class MultiKeywordSelector
|
2008-11-17 22:58:09 +08:00
|
|
|
: public DeclarationNameExtra, public llvm::FoldingSetNode {
|
|
|
|
MultiKeywordSelector(unsigned nKeys) {
|
|
|
|
ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
public:
|
2007-10-06 02:42:47 +08:00
|
|
|
// Constructor for keyword selectors.
|
|
|
|
MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
|
|
|
|
assert((nKeys > 1) && "not a multi-keyword selector");
|
2008-11-17 22:58:09 +08:00
|
|
|
ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-06 02:42:47 +08: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];
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
|
|
|
|
2007-10-07 09:33:16 +08:00
|
|
|
// getName - Derive the full selector name and return it.
|
|
|
|
std::string getName() const;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-17 22:58:09 +08:00
|
|
|
unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-06 02:42:47 +08:00
|
|
|
typedef IdentifierInfo *const *keyword_iterator;
|
|
|
|
keyword_iterator keyword_begin() const {
|
|
|
|
return reinterpret_cast<keyword_iterator>(this+1);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
keyword_iterator keyword_end() const {
|
|
|
|
return keyword_begin()+getNumArgs();
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
2007-10-07 09:33:16 +08:00
|
|
|
IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
|
2008-11-17 22:58:09 +08:00
|
|
|
assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
|
2007-10-06 02:42:47 +08:00
|
|
|
return keyword_begin()[i];
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
static void Profile(llvm::FoldingSetNodeID &ID,
|
2007-10-06 02:42:47 +08:00
|
|
|
keyword_iterator ArgTys, unsigned NumArgs) {
|
|
|
|
ID.AddInteger(NumArgs);
|
2007-10-07 09:33:16 +08:00
|
|
|
for (unsigned i = 0; i != NumArgs; ++i)
|
|
|
|
ID.AddPointer(ArgTys[i]);
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
|
|
|
void Profile(llvm::FoldingSetNodeID &ID) {
|
2008-11-17 22:58:09 +08:00
|
|
|
Profile(ID, keyword_begin(), getNumArgs());
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
|
|
|
};
|
2007-10-06 04:15:24 +08:00
|
|
|
} // end namespace clang.
|
2007-10-06 02:42:47 +08:00
|
|
|
|
|
|
|
unsigned Selector::getNumArgs() const {
|
|
|
|
unsigned IIF = getIdentifierInfoFlag();
|
2012-05-05 02:24:37 +08:00
|
|
|
if (IIF <= ZeroArg)
|
2007-10-06 02:42:47 +08:00
|
|
|
return 0;
|
|
|
|
if (IIF == OneArg)
|
|
|
|
return 1;
|
2012-05-05 02:24:37 +08:00
|
|
|
// We point to a MultiKeywordSelector.
|
|
|
|
MultiKeywordSelector *SI = getMultiKeywordSelector();
|
2009-09-09 23:08:12 +08:00
|
|
|
return SI->getNumArgs();
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
|
|
|
|
2007-10-07 09:33:16 +08:00
|
|
|
IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
|
2012-05-05 02:24:37 +08:00
|
|
|
if (getIdentifierInfoFlag() < MultiArg) {
|
2007-10-07 09:33:16 +08:00
|
|
|
assert(argIndex == 0 && "illegal keyword index");
|
2009-04-27 06:20:50 +08:00
|
|
|
return getAsIdentifierInfo();
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
2012-05-05 02:24:37 +08:00
|
|
|
// We point to a MultiKeywordSelector.
|
|
|
|
MultiKeywordSelector *SI = getMultiKeywordSelector();
|
2007-10-06 02:42:47 +08:00
|
|
|
return SI->getIdentifierInfoForSlot(argIndex);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Selector::getNameForSlot(unsigned int argIndex) const {
|
2011-02-19 06:29:55 +08:00
|
|
|
IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
|
2011-07-23 18:55:15 +08:00
|
|
|
return II? II->getName() : StringRef();
|
2011-02-19 06:29:55 +08:00
|
|
|
}
|
|
|
|
|
2007-10-07 09:33:16 +08:00
|
|
|
std::string MultiKeywordSelector::getName() const {
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> Str;
|
2009-10-18 02:13:02 +08:00
|
|
|
llvm::raw_svector_ostream OS(Str);
|
2007-10-07 09:33:16 +08:00
|
|
|
for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
|
|
|
|
if (*I)
|
2009-10-19 05:17:35 +08:00
|
|
|
OS << (*I)->getName();
|
2009-10-18 02:13:02 +08:00
|
|
|
OS << ':';
|
2007-10-07 09:33:16 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-18 02:13:02 +08:00
|
|
|
return OS.str();
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
|
|
|
|
2008-11-24 11:33:13 +08:00
|
|
|
std::string Selector::getAsString() const {
|
2009-04-27 06:20:50 +08:00
|
|
|
if (InfoPtr == 0)
|
|
|
|
return "<null selector>";
|
|
|
|
|
2012-05-05 02:24:37 +08:00
|
|
|
if (getIdentifierInfoFlag() < MultiArg) {
|
2009-03-07 07:36:28 +08:00
|
|
|
IdentifierInfo *II = getAsIdentifierInfo();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-07 09:22:02 +08:00
|
|
|
// If the number of arguments is 0 then II is guaranteed to not be null.
|
2009-03-07 07:36:28 +08:00
|
|
|
if (getNumArgs() == 0)
|
2009-10-19 05:17:35 +08:00
|
|
|
return II->getName();
|
2009-10-18 02:13:02 +08:00
|
|
|
|
|
|
|
if (!II)
|
|
|
|
return ":";
|
2009-03-07 07:36:28 +08:00
|
|
|
|
2009-10-19 05:17:35 +08:00
|
|
|
return II->getName().str() + ":";
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-05-05 02:24:37 +08:00
|
|
|
// We have a multiple keyword selector.
|
|
|
|
return getMultiKeywordSelector()->getName();
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
|
|
|
|
2011-03-02 09:50:55 +08:00
|
|
|
/// Interpreting the given string using the normal CamelCase
|
|
|
|
/// conventions, determine whether the given string starts with the
|
|
|
|
/// given "word", which is assumed to end in a lowercase letter.
|
2011-07-23 18:55:15 +08:00
|
|
|
static bool startsWithWord(StringRef name, StringRef word) {
|
2011-03-02 09:50:55 +08:00
|
|
|
if (name.size() < word.size()) return false;
|
2013-02-09 06:30:41 +08:00
|
|
|
return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
|
|
|
|
name.startswith(word));
|
2011-03-02 09:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
|
|
|
|
IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
|
|
|
|
if (!first) return OMF_None;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef name = first->getName();
|
2011-03-02 09:50:55 +08:00
|
|
|
if (sel.isUnarySelector()) {
|
|
|
|
if (name == "autorelease") return OMF_autorelease;
|
|
|
|
if (name == "dealloc") return OMF_dealloc;
|
2011-08-29 06:35:17 +08:00
|
|
|
if (name == "finalize") return OMF_finalize;
|
2011-03-02 09:50:55 +08:00
|
|
|
if (name == "release") return OMF_release;
|
|
|
|
if (name == "retain") return OMF_retain;
|
|
|
|
if (name == "retainCount") return OMF_retainCount;
|
2011-06-11 09:09:30 +08:00
|
|
|
if (name == "self") return OMF_self;
|
2011-03-02 09:50:55 +08:00
|
|
|
}
|
2011-07-06 06:38:59 +08:00
|
|
|
|
|
|
|
if (name == "performSelector") return OMF_performSelector;
|
2011-03-02 09:50:55 +08:00
|
|
|
|
|
|
|
// The other method families may begin with a prefix of underscores.
|
|
|
|
while (!name.empty() && name.front() == '_')
|
|
|
|
name = name.substr(1);
|
|
|
|
|
|
|
|
if (name.empty()) return OMF_None;
|
|
|
|
switch (name.front()) {
|
|
|
|
case 'a':
|
|
|
|
if (startsWithWord(name, "alloc")) return OMF_alloc;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (startsWithWord(name, "copy")) return OMF_copy;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
if (startsWithWord(name, "init")) return OMF_init;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
if (startsWithWord(name, "new")) return OMF_new;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OMF_None;
|
|
|
|
}
|
2007-10-06 02:42:47 +08:00
|
|
|
|
2013-07-24 03:31:17 +08:00
|
|
|
ObjCInstanceTypeFamily Selector::getInstTypeMethodFamilyImpl(Selector sel) {
|
|
|
|
IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
|
|
|
|
if (!first) return OIT_None;
|
|
|
|
|
|
|
|
StringRef name = first->getName();
|
|
|
|
|
|
|
|
if (name.empty()) return OIT_None;
|
|
|
|
switch (name.front()) {
|
|
|
|
case 'a':
|
|
|
|
if (startsWithWord(name, "alloc")) return OIT_MemManage;
|
|
|
|
else
|
|
|
|
if (startsWithWord(name, "array")) return OIT_Array;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
if (startsWithWord(name, "init")) return OIT_MemManage;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (startsWithWord(name, "string")) return OIT_NSString;
|
|
|
|
else
|
|
|
|
if (startsWithWord(name, "set")) return OIT_NSSet;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
if (startsWithWord(name, "URL")) return OIT_NSURL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return OIT_None;
|
|
|
|
}
|
|
|
|
|
2009-03-04 13:35:38 +08:00
|
|
|
namespace {
|
|
|
|
struct SelectorTableImpl {
|
|
|
|
llvm::FoldingSet<MultiKeywordSelector> Table;
|
|
|
|
llvm::BumpPtrAllocator Allocator;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
|
|
|
static SelectorTableImpl &getSelectorTableImpl(void *P) {
|
|
|
|
return *static_cast<SelectorTableImpl*>(P);
|
|
|
|
}
|
|
|
|
|
2013-06-11 05:36:55 +08:00
|
|
|
SmallString<64>
|
2013-06-08 06:29:12 +08:00
|
|
|
SelectorTable::constructSetterName(StringRef Name) {
|
2013-06-11 05:36:55 +08:00
|
|
|
SmallString<64> SetterName("set");
|
|
|
|
SetterName += Name;
|
|
|
|
SetterName[3] = toUppercase(SetterName[3]);
|
|
|
|
return SetterName;
|
2013-06-08 06:29:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-11 05:36:55 +08:00
|
|
|
Selector
|
2013-06-08 06:29:12 +08:00
|
|
|
SelectorTable::constructSetterSelector(IdentifierTable &Idents,
|
|
|
|
SelectorTable &SelTable,
|
|
|
|
const IdentifierInfo *Name) {
|
|
|
|
IdentifierInfo *SetterName =
|
|
|
|
&Idents.get(constructSetterName(Name->getName()));
|
2012-02-04 21:45:25 +08:00
|
|
|
return SelTable.getUnarySelector(SetterName);
|
|
|
|
}
|
|
|
|
|
2011-04-19 06:47:04 +08:00
|
|
|
size_t SelectorTable::getTotalMemory() const {
|
|
|
|
SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
|
|
|
|
return SelTabImpl.Allocator.getTotalMemory();
|
|
|
|
}
|
2009-03-04 13:35:38 +08:00
|
|
|
|
2007-10-07 10:00:24 +08:00
|
|
|
Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
|
|
|
|
if (nKeys < 2)
|
|
|
|
return Selector(IIV[0], nKeys);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 13:35:38 +08:00
|
|
|
SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-06 02:42:47 +08:00
|
|
|
// Unique selector, to guarantee there is one per name.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
MultiKeywordSelector::Profile(ID, IIV, nKeys);
|
|
|
|
|
|
|
|
void *InsertPos = 0;
|
2009-03-04 13:35:38 +08:00
|
|
|
if (MultiKeywordSelector *SI =
|
|
|
|
SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
|
2007-10-06 02:42:47 +08:00
|
|
|
return Selector(SI);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-06 02:42:47 +08:00
|
|
|
// MultiKeywordSelector objects are not allocated with new because they have a
|
|
|
|
// variable size array (for parameter types) at the end of them.
|
2009-03-04 13:35:38 +08:00
|
|
|
unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
|
|
|
|
MultiKeywordSelector *SI =
|
2009-09-09 23:08:12 +08:00
|
|
|
(MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
|
2010-10-30 13:14:06 +08:00
|
|
|
llvm::alignOf<MultiKeywordSelector>());
|
2007-10-06 02:42:47 +08:00
|
|
|
new (SI) MultiKeywordSelector(nKeys, IIV);
|
2009-03-04 13:35:38 +08:00
|
|
|
SelTabImpl.Table.InsertNode(SI, InsertPos);
|
2007-10-06 02:42:47 +08:00
|
|
|
return Selector(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectorTable::SelectorTable() {
|
2009-03-04 13:35:38 +08:00
|
|
|
Impl = new SelectorTableImpl();
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SelectorTable::~SelectorTable() {
|
2009-03-04 13:35:38 +08:00
|
|
|
delete &getSelectorTableImpl(Impl);
|
2007-10-06 02:42:47 +08:00
|
|
|
}
|
|
|
|
|
2009-11-04 08:56:37 +08:00
|
|
|
const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
|
|
|
|
switch (Operator) {
|
|
|
|
case OO_None:
|
|
|
|
case NUM_OVERLOADED_OPERATORS:
|
|
|
|
return 0;
|
2010-03-12 19:27:37 +08:00
|
|
|
|
2009-11-04 08:56:37 +08:00
|
|
|
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
|
|
|
|
case OO_##Name: return Spelling;
|
|
|
|
#include "clang/Basic/OperatorKinds.def"
|
|
|
|
}
|
2010-03-12 19:27:37 +08:00
|
|
|
|
2012-01-17 14:56:22 +08:00
|
|
|
llvm_unreachable("Invalid OverloadedOperatorKind!");
|
2009-11-04 08:56:37 +08:00
|
|
|
}
|