2008-04-11 15:06:57 +08:00
|
|
|
//===- IdentifierResolver.cpp - Lexical Scope Name lookup -------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-04-12 09:50:47 +08:00
|
|
|
// This file implements the IdentifierResolver class, which is used for lexical
|
2008-11-18 04:34:05 +08:00
|
|
|
// scoped lookup, based on declaration names.
|
2008-04-11 15:06:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "IdentifierResolver.h"
|
2008-09-10 05:57:58 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2008-04-11 15:06:57 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-09-10 03:28:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IdDeclInfoMap class
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2008-11-18 04:34:05 +08:00
|
|
|
/// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
|
2008-04-12 09:50:47 +08:00
|
|
|
/// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
|
2008-04-11 15:06:57 +08:00
|
|
|
/// individual IdDeclInfo to heap.
|
2008-05-10 07:39:43 +08:00
|
|
|
class IdentifierResolver::IdDeclInfoMap {
|
2010-02-16 03:38:00 +08:00
|
|
|
static const unsigned int POOL_SIZE = 512;
|
|
|
|
|
|
|
|
/// We use our own linked-list implementation because it is sadly
|
|
|
|
/// impossible to add something to a pre-C++0x STL container without
|
|
|
|
/// a completely unnecessary copy.
|
|
|
|
struct IdDeclInfoPool {
|
|
|
|
IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
|
|
|
|
|
|
|
|
IdDeclInfoPool *Next;
|
|
|
|
IdDeclInfo Pool[POOL_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
IdDeclInfoPool *CurPool;
|
2008-04-11 15:06:57 +08:00
|
|
|
unsigned int CurIndex;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-04-11 15:06:57 +08:00
|
|
|
public:
|
2010-02-16 03:38:00 +08:00
|
|
|
IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE) {}
|
|
|
|
|
|
|
|
~IdDeclInfoMap() {
|
|
|
|
IdDeclInfoPool *Cur = CurPool;
|
|
|
|
while (IdDeclInfoPool *P = Cur) {
|
|
|
|
Cur = Cur->Next;
|
|
|
|
delete P;
|
|
|
|
}
|
|
|
|
}
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2008-11-18 04:34:05 +08:00
|
|
|
/// Returns the IdDeclInfo associated to the DeclarationName.
|
2008-04-11 15:06:57 +08:00
|
|
|
/// It creates a new IdDeclInfo if one was not created before for this id.
|
2008-11-18 04:34:05 +08:00
|
|
|
IdDeclInfo &operator[](DeclarationName Name);
|
2008-04-11 15:06:57 +08:00
|
|
|
};
|
|
|
|
|
2008-04-14 08:09:21 +08:00
|
|
|
|
2008-09-10 03:28:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IdDeclInfo Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// RemoveDecl - Remove the decl from the scope chain.
|
|
|
|
/// The decl must already be part of the decl chain.
|
|
|
|
void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
|
|
|
|
for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
|
|
|
|
if (D == *(I-1)) {
|
|
|
|
Decls.erase(I-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0 && "Didn't find this decl on its identifier's chain!");
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool
|
2009-03-02 08:19:53 +08:00
|
|
|
IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
|
|
|
|
for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
|
|
|
|
if (Old == *(I-1)) {
|
|
|
|
*(I - 1) = New;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-10 03:28:27 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IdentifierResolver Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-09-10 05:32:02 +08:00
|
|
|
IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
|
|
|
|
: LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
|
|
|
|
}
|
2008-04-14 08:09:21 +08:00
|
|
|
IdentifierResolver::~IdentifierResolver() {
|
2008-05-10 07:39:43 +08:00
|
|
|
delete IdDeclInfos;
|
2008-04-14 08:09:21 +08:00
|
|
|
}
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2008-09-10 03:28:27 +08:00
|
|
|
/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
|
|
|
|
/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
|
|
|
|
/// true if 'D' belongs to the given declaration context.
|
2008-09-10 05:57:58 +08:00
|
|
|
bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
|
2008-12-12 00:49:14 +08:00
|
|
|
ASTContext &Context, Scope *S) const {
|
2009-01-07 07:51:29 +08:00
|
|
|
Ctx = Ctx->getLookupContext();
|
2009-01-06 03:45:36 +08:00
|
|
|
|
2008-09-10 05:57:58 +08:00
|
|
|
if (Ctx->isFunctionOrMethod()) {
|
2009-01-06 03:45:36 +08:00
|
|
|
// Ignore the scopes associated within transparent declaration contexts.
|
2009-09-09 23:08:12 +08:00
|
|
|
while (S->getEntity() &&
|
2009-01-06 03:45:36 +08:00
|
|
|
((DeclContext *)S->getEntity())->isTransparentContext())
|
|
|
|
S = S->getParent();
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
if (S->isDeclScope(Action::DeclPtrTy::make(D)))
|
2008-09-10 05:57:58 +08:00
|
|
|
return true;
|
|
|
|
if (LangOpt.CPlusPlus) {
|
2008-12-22 00:41:36 +08:00
|
|
|
// C++ 3.3.2p3:
|
|
|
|
// The name declared in a catch exception-declaration is local to the
|
|
|
|
// handler and shall not be redeclared in the outermost block of the
|
|
|
|
// handler.
|
2008-09-10 05:57:58 +08:00
|
|
|
// C++ 3.3.2p4:
|
|
|
|
// Names declared in the for-init-statement, and in the condition of if,
|
|
|
|
// while, for, and switch statements are local to the if, while, for, or
|
|
|
|
// switch statement (including the controlled statement), and shall not be
|
|
|
|
// redeclared in a subsequent condition of that statement nor in the
|
|
|
|
// outermost block (or, for the if statement, any of the outermost blocks)
|
|
|
|
// of the controlled statement.
|
|
|
|
//
|
|
|
|
assert(S->getParent() && "No TUScope?");
|
|
|
|
if (S->getParent()->getFlags() & Scope::ControlScope)
|
2009-03-29 03:18:32 +08:00
|
|
|
return S->getParent()->isDeclScope(Action::DeclPtrTy::make(D));
|
2008-09-10 05:57:58 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-10 03:28:27 +08:00
|
|
|
|
2009-09-02 01:22:34 +08:00
|
|
|
return D->getDeclContext()->getLookupContext()->Equals(Ctx);
|
2008-09-10 03:28:27 +08:00
|
|
|
}
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
/// AddDecl - Link the decl to its shadowed decl chain.
|
2008-05-10 07:39:43 +08:00
|
|
|
void IdentifierResolver::AddDecl(NamedDecl *D) {
|
2008-11-18 04:34:05 +08:00
|
|
|
DeclarationName Name = D->getDeclName();
|
|
|
|
void *Ptr = Name.getFETokenInfo<void>();
|
2008-04-11 15:06:57 +08:00
|
|
|
|
|
|
|
if (!Ptr) {
|
2008-11-18 04:34:05 +08:00
|
|
|
Name.setFETokenInfo(D);
|
2008-04-11 15:06:57 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdDeclInfo *IDI;
|
|
|
|
|
|
|
|
if (isDeclPtr(Ptr)) {
|
2008-11-18 04:34:05 +08:00
|
|
|
Name.setFETokenInfo(NULL);
|
|
|
|
IDI = &(*IdDeclInfos)[Name];
|
2008-05-10 07:39:43 +08:00
|
|
|
NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
|
|
|
|
IDI->AddDecl(PrevD);
|
2008-04-11 15:06:57 +08:00
|
|
|
} else
|
|
|
|
IDI = toIdDeclInfo(Ptr);
|
|
|
|
|
2008-05-10 07:39:43 +08:00
|
|
|
IDI->AddDecl(D);
|
2008-04-11 15:06:57 +08:00
|
|
|
}
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
/// RemoveDecl - Unlink the decl from its shadowed decl chain.
|
2008-04-11 15:06:57 +08:00
|
|
|
/// The decl must already be part of the decl chain.
|
|
|
|
void IdentifierResolver::RemoveDecl(NamedDecl *D) {
|
|
|
|
assert(D && "null param passed");
|
2008-11-18 04:34:05 +08:00
|
|
|
DeclarationName Name = D->getDeclName();
|
|
|
|
void *Ptr = Name.getFETokenInfo<void>();
|
2008-04-11 15:06:57 +08:00
|
|
|
|
|
|
|
assert(Ptr && "Didn't find this decl on its identifier's chain!");
|
|
|
|
|
|
|
|
if (isDeclPtr(Ptr)) {
|
|
|
|
assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
|
2008-11-18 04:34:05 +08:00
|
|
|
Name.setFETokenInfo(NULL);
|
2008-04-11 15:06:57 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-10 07:39:43 +08:00
|
|
|
return toIdDeclInfo(Ptr)->RemoveDecl(D);
|
2008-04-11 15:06:57 +08:00
|
|
|
}
|
|
|
|
|
2009-03-02 08:19:53 +08:00
|
|
|
bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(Old->getDeclName() == New->getDeclName() &&
|
2009-03-02 08:19:53 +08:00
|
|
|
"Cannot replace a decl with another decl of a different name");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-02 08:19:53 +08:00
|
|
|
DeclarationName Name = Old->getDeclName();
|
|
|
|
void *Ptr = Name.getFETokenInfo<void>();
|
|
|
|
|
|
|
|
if (!Ptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (isDeclPtr(Ptr)) {
|
|
|
|
if (Ptr == Old) {
|
|
|
|
Name.setFETokenInfo(New);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
|
2009-03-02 08:19:53 +08:00
|
|
|
}
|
|
|
|
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
/// begin - Returns an iterator for decls with name 'Name'.
|
2008-05-10 07:39:43 +08:00
|
|
|
IdentifierResolver::iterator
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
IdentifierResolver::begin(DeclarationName Name) {
|
2008-11-18 04:34:05 +08:00
|
|
|
void *Ptr = Name.getFETokenInfo<void>();
|
2008-07-18 01:49:50 +08:00
|
|
|
if (!Ptr) return end();
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2009-03-04 14:34:08 +08:00
|
|
|
if (isDeclPtr(Ptr))
|
|
|
|
return iterator(static_cast<NamedDecl*>(Ptr));
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2008-07-18 01:49:50 +08:00
|
|
|
IdDeclInfo *IDI = toIdDeclInfo(Ptr);
|
2008-05-10 07:39:43 +08:00
|
|
|
|
2009-01-06 03:45:36 +08:00
|
|
|
IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
|
2008-07-18 01:49:50 +08:00
|
|
|
if (I != IDI->decls_begin())
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
return iterator(I-1);
|
2009-03-04 14:34:08 +08:00
|
|
|
// No decls found.
|
|
|
|
return end();
|
2008-07-18 01:49:50 +08:00
|
|
|
}
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void IdentifierResolver::AddDeclToIdentifierChain(IdentifierInfo *II,
|
Lazy deserialization of the declaration chains associated with
identifiers from a precompiled header.
This patch changes the primary name lookup method for entities within
a precompiled header. Previously, we would load all of the names of
declarations at translation unit scope into a large DenseMap (inside
the TranslationUnitDecl's DeclContext), and then perform a special
"last resort" lookup into this DeclContext when we knew there was a
PCH file (see Sema::LookupName). Now, when we see an identifier named
for the first time, we load all of the declarations with that name
that are visible from the translation unit into the IdentifierInfo's
chain of declarations. Thus, the explicit "look into the translation
unit's DeclContext" code is gone, and Sema effectively uses the same
IdentifierInfo-based name lookup mechanism whether we are using a PCH
file or not.
This approach should help PCH scale with the size of the input program
rather than the size of the PCH file. The "Hello, World!" application
with Carbon.h as a PCH file now loads 20% of the identifiers in the
PCH file rather than 85% of the identifiers.
90% of the 20% of identifiers loaded are actually loaded when we
deserialize the preprocessor state. The next step is to make the
preprocessor load macros lazily, which should drastically reduce the
number of types, declarations, and identifiers loaded for "Hello,
World".
llvm-svn: 69737
2009-04-22 06:25:48 +08:00
|
|
|
NamedDecl *D) {
|
|
|
|
void *Ptr = II->getFETokenInfo<void>();
|
|
|
|
|
|
|
|
if (!Ptr) {
|
|
|
|
II->setFETokenInfo(D);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdDeclInfo *IDI;
|
|
|
|
|
|
|
|
if (isDeclPtr(Ptr)) {
|
|
|
|
II->setFETokenInfo(NULL);
|
|
|
|
IDI = &(*IdDeclInfos)[II];
|
|
|
|
NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
|
|
|
|
IDI->AddDecl(PrevD);
|
|
|
|
} else
|
|
|
|
IDI = toIdDeclInfo(Ptr);
|
|
|
|
|
|
|
|
IDI->AddDecl(D);
|
|
|
|
}
|
|
|
|
|
2008-09-10 03:28:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IdDeclInfoMap Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-11-18 04:34:05 +08:00
|
|
|
/// Returns the IdDeclInfo associated to the DeclarationName.
|
2008-04-11 15:06:57 +08:00
|
|
|
/// It creates a new IdDeclInfo if one was not created before for this id.
|
2008-05-10 07:39:43 +08:00
|
|
|
IdentifierResolver::IdDeclInfo &
|
2008-11-18 04:34:05 +08:00
|
|
|
IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
|
|
|
|
void *Ptr = Name.getFETokenInfo<void>();
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2008-05-10 07:39:43 +08:00
|
|
|
if (Ptr) return *toIdDeclInfo(Ptr);
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2010-02-16 03:38:00 +08:00
|
|
|
if (CurIndex == POOL_SIZE) {
|
|
|
|
CurPool = new IdDeclInfoPool(CurPool);
|
2008-04-11 15:06:57 +08:00
|
|
|
CurIndex = 0;
|
|
|
|
}
|
2010-02-16 03:38:00 +08:00
|
|
|
IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
|
2008-11-18 04:34:05 +08:00
|
|
|
Name.setFETokenInfo(reinterpret_cast<void*>(
|
2008-04-11 15:06:57 +08:00
|
|
|
reinterpret_cast<uintptr_t>(IDI) | 0x1)
|
|
|
|
);
|
|
|
|
++CurIndex;
|
|
|
|
return *IDI;
|
|
|
|
}
|