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-04-11 15:06:57 +08:00
|
|
|
// scoped lookup, based on identifier.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "IdentifierResolver.h"
|
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2008-04-14 05:07:44 +08:00
|
|
|
#include "clang/Parse/Scope.h"
|
2008-04-11 15:06:57 +08:00
|
|
|
#include <list>
|
2008-04-12 09:50:47 +08:00
|
|
|
#include <vector>
|
2008-04-11 15:06:57 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
namespace {
|
|
|
|
|
2008-04-11 15:06:57 +08:00
|
|
|
class IdDeclInfo;
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
/// Identifier's FETokenInfo contains a Decl pointer if lower bit == 0.
|
2008-04-11 15:06:57 +08:00
|
|
|
static inline bool isDeclPtr(void *Ptr) {
|
|
|
|
return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
|
|
|
|
}
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
/// Identifier's FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
|
2008-04-11 15:06:57 +08:00
|
|
|
static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
|
|
|
|
return reinterpret_cast<IdDeclInfo*>(
|
|
|
|
reinterpret_cast<uintptr_t>(Ptr) & ~0x1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
/// IdDeclInfo - Keeps track of information about decls associated to a
|
|
|
|
/// particular identifier. IdDeclInfos are lazily constructed and assigned
|
|
|
|
/// to an identifier the first time a decl with that identifier is shadowed
|
|
|
|
/// in some scope.
|
2008-04-11 15:06:57 +08:00
|
|
|
class IdDeclInfo {
|
|
|
|
typedef llvm::SmallVector<NamedDecl *, 2> ShadowedTy;
|
|
|
|
ShadowedTy ShadowedDecls;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef ShadowedTy::iterator ShadowedIter;
|
|
|
|
|
|
|
|
inline ShadowedIter shadowed_begin() { return ShadowedDecls.begin(); }
|
|
|
|
inline ShadowedIter shadowed_end() { return ShadowedDecls.end(); }
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
/// Add a decl in the scope chain.
|
2008-04-11 15:06:57 +08:00
|
|
|
void PushShadowed(NamedDecl *D) {
|
|
|
|
assert(D && "Decl null");
|
|
|
|
ShadowedDecls.push_back(D);
|
|
|
|
}
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
/// Add the decl at the top of scope chain.
|
2008-04-11 15:06:57 +08:00
|
|
|
void PushGlobalShadowed(NamedDecl *D) {
|
|
|
|
assert(D && "Decl null");
|
|
|
|
ShadowedDecls.insert(ShadowedDecls.begin(), D);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RemoveShadowed - Remove the decl from the scope chain.
|
|
|
|
/// The decl must already be part of the decl chain.
|
|
|
|
void RemoveShadowed(NamedDecl *D);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// IdDeclInfoMap - Associates IdDeclInfos with Identifiers.
|
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-04-14 08:09:21 +08:00
|
|
|
class IdDeclInfoMap {
|
2008-04-12 09:50:47 +08:00
|
|
|
static const unsigned int VECTOR_SIZE = 512;
|
|
|
|
// Holds vectors of IdDeclInfos that serve as 'pools'.
|
|
|
|
// New vectors are added when the current one is full.
|
|
|
|
std::list< std::vector<IdDeclInfo> > IDIVecs;
|
2008-04-11 15:06:57 +08:00
|
|
|
unsigned int CurIndex;
|
|
|
|
|
|
|
|
public:
|
2008-04-12 09:50:47 +08:00
|
|
|
IdDeclInfoMap() : CurIndex(VECTOR_SIZE) {}
|
2008-04-11 15:06:57 +08:00
|
|
|
|
|
|
|
/// Returns the IdDeclInfo associated to the IdentifierInfo.
|
|
|
|
/// It creates a new IdDeclInfo if one was not created before for this id.
|
|
|
|
IdDeclInfo &operator[](IdentifierInfo *II);
|
|
|
|
};
|
|
|
|
|
2008-04-14 08:09:21 +08:00
|
|
|
} // end anonymous namespace
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2008-04-14 08:09:21 +08:00
|
|
|
|
|
|
|
IdentifierResolver::IdentifierResolver() : IdDeclInfos(new IdDeclInfoMap) {}
|
|
|
|
IdentifierResolver::~IdentifierResolver() {
|
|
|
|
delete static_cast<IdDeclInfoMap*>(IdDeclInfos);
|
|
|
|
}
|
2008-04-11 15:06:57 +08:00
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
/// AddDecl - Link the decl to its shadowed decl chain.
|
2008-04-11 15:06:57 +08:00
|
|
|
void IdentifierResolver::AddDecl(NamedDecl *D, Scope *S) {
|
|
|
|
assert(D && S && "null param passed");
|
|
|
|
IdentifierInfo *II = D->getIdentifier();
|
|
|
|
void *Ptr = II->getFETokenInfo<void>();
|
|
|
|
|
|
|
|
if (!Ptr) {
|
|
|
|
II->setFETokenInfo(D);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdDeclInfo *IDI;
|
|
|
|
|
|
|
|
if (isDeclPtr(Ptr)) {
|
|
|
|
II->setFETokenInfo(NULL);
|
2008-04-14 08:09:21 +08:00
|
|
|
IdDeclInfoMap &Map = *static_cast<IdDeclInfoMap*>(IdDeclInfos);
|
|
|
|
IDI = &Map[II];
|
2008-04-11 15:06:57 +08:00
|
|
|
IDI->PushShadowed(static_cast<NamedDecl*>(Ptr));
|
|
|
|
} else
|
|
|
|
IDI = toIdDeclInfo(Ptr);
|
|
|
|
|
2008-04-14 05:07:44 +08:00
|
|
|
// C++ [basic.scope]p4:
|
|
|
|
// -- exactly one declaration shall declare a class name or
|
|
|
|
// enumeration name that is not a typedef name and the other
|
|
|
|
// declarations shall all refer to the same object or
|
|
|
|
// enumerator, or all refer to functions and function templates;
|
|
|
|
// in this case the class name or enumeration name is hidden.
|
2008-04-14 08:26:07 +08:00
|
|
|
if (isa<TagDecl>(D) && IDI->shadowed_end() != IDI->shadowed_begin()) {
|
2008-04-14 05:07:44 +08:00
|
|
|
// We are pushing the name of a tag (enum or class).
|
|
|
|
IdDeclInfo::ShadowedIter TopIter = IDI->shadowed_end() - 1;
|
|
|
|
if (S->isDeclScope(*TopIter)) {
|
|
|
|
// There is already a declaration with the same name in the same
|
|
|
|
// scope. It must be found before we find the new declaration,
|
|
|
|
// so swap the order on the shadowed declaration stack.
|
|
|
|
NamedDecl *Temp = *TopIter;
|
|
|
|
*TopIter = D;
|
|
|
|
D = Temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-11 15:06:57 +08:00
|
|
|
IDI->PushShadowed(D);
|
|
|
|
}
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
/// AddGlobalDecl - Link the decl at the top of the shadowed decl chain.
|
2008-04-11 15:06:57 +08:00
|
|
|
void IdentifierResolver::AddGlobalDecl(NamedDecl *D) {
|
|
|
|
assert(D && "null param passed");
|
|
|
|
IdentifierInfo *II = D->getIdentifier();
|
|
|
|
void *Ptr = II->getFETokenInfo<void>();
|
|
|
|
|
|
|
|
if (!Ptr) {
|
|
|
|
II->setFETokenInfo(D);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdDeclInfo *IDI;
|
|
|
|
|
|
|
|
if (isDeclPtr(Ptr)) {
|
|
|
|
II->setFETokenInfo(NULL);
|
2008-04-14 08:09:21 +08:00
|
|
|
IdDeclInfoMap &Map = *static_cast<IdDeclInfoMap*>(IdDeclInfos);
|
|
|
|
IDI = &Map[II];
|
2008-04-11 15:06:57 +08:00
|
|
|
IDI->PushShadowed(static_cast<NamedDecl*>(Ptr));
|
|
|
|
} else
|
|
|
|
IDI = toIdDeclInfo(Ptr);
|
|
|
|
|
|
|
|
IDI->PushGlobalShadowed(D);
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
IdentifierInfo *II = D->getIdentifier();
|
|
|
|
void *Ptr = II->getFETokenInfo<void>();
|
|
|
|
|
|
|
|
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!");
|
|
|
|
II->setFETokenInfo(NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return toIdDeclInfo(Ptr)->RemoveShadowed(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Lookup - Find the non-shadowed decl that belongs to a particular
|
|
|
|
/// Decl::IdentifierNamespace.
|
2008-04-14 05:07:44 +08:00
|
|
|
NamedDecl *IdentifierResolver::Lookup(const IdentifierInfo *II, unsigned NS) {
|
2008-04-11 15:06:57 +08:00
|
|
|
assert(II && "null param passed");
|
|
|
|
void *Ptr = II->getFETokenInfo<void>();
|
|
|
|
|
|
|
|
if (!Ptr) return NULL;
|
|
|
|
|
|
|
|
if (isDeclPtr(Ptr)) {
|
|
|
|
NamedDecl *D = static_cast<NamedDecl*>(Ptr);
|
2008-04-14 05:07:44 +08:00
|
|
|
return (D->getIdentifierNamespace() & NS) ? D : NULL;
|
2008-04-11 15:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IdDeclInfo *IDI = toIdDeclInfo(Ptr);
|
|
|
|
|
|
|
|
// ShadowedDecls are ordered from most shadowed to less shadowed.
|
|
|
|
// So we do a reverse iteration from end to begin.
|
|
|
|
for (IdDeclInfo::ShadowedIter SI = IDI->shadowed_end();
|
|
|
|
SI != IDI->shadowed_begin(); --SI) {
|
|
|
|
NamedDecl *D = *(SI-1);
|
2008-04-14 05:07:44 +08:00
|
|
|
if (D->getIdentifierNamespace() & NS)
|
2008-04-11 15:06:57 +08:00
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we didn't find the decl.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// RemoveShadowed - Remove the decl from the scope chain.
|
|
|
|
/// The decl must already be part of the decl chain.
|
|
|
|
void IdDeclInfo::RemoveShadowed(NamedDecl *D) {
|
|
|
|
assert(D && "null decl passed");
|
2008-04-12 20:38:58 +08:00
|
|
|
assert(!ShadowedDecls.empty() &&
|
2008-04-11 15:06:57 +08:00
|
|
|
"Didn't find this decl on its identifier's chain!");
|
|
|
|
|
|
|
|
// common case
|
|
|
|
if (D == ShadowedDecls.back()) {
|
|
|
|
ShadowedDecls.pop_back();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ShadowedIter SI = ShadowedDecls.end()-1;
|
|
|
|
SI != ShadowedDecls.begin(); --SI) {
|
|
|
|
if (*(SI-1) == D) {
|
|
|
|
ShadowedDecls.erase(SI-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false && "Didn't find this decl on its identifier's chain!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the IdDeclInfo associated to the IdentifierInfo.
|
|
|
|
/// It creates a new IdDeclInfo if one was not created before for this id.
|
2008-04-14 08:09:21 +08:00
|
|
|
IdDeclInfo &IdDeclInfoMap::operator[](IdentifierInfo *II) {
|
2008-04-11 15:06:57 +08:00
|
|
|
assert (II && "null IdentifierInfo passed");
|
|
|
|
void *Ptr = II->getFETokenInfo<void>();
|
|
|
|
|
|
|
|
if (Ptr) {
|
|
|
|
assert(!isDeclPtr(Ptr) && "didn't clear decl for FEToken");
|
|
|
|
return *toIdDeclInfo(Ptr);
|
|
|
|
}
|
|
|
|
|
2008-04-12 09:50:47 +08:00
|
|
|
if (CurIndex == VECTOR_SIZE) {
|
|
|
|
// Add a IdDeclInfo vector 'pool'
|
2008-04-12 20:38:58 +08:00
|
|
|
IDIVecs.push_back(std::vector<IdDeclInfo>());
|
2008-04-12 09:50:47 +08:00
|
|
|
// Fill the vector
|
|
|
|
IDIVecs.back().resize(VECTOR_SIZE);
|
2008-04-11 15:06:57 +08:00
|
|
|
CurIndex = 0;
|
|
|
|
}
|
2008-04-12 09:50:47 +08:00
|
|
|
IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
|
2008-04-11 15:06:57 +08:00
|
|
|
II->setFETokenInfo(reinterpret_cast<void*>(
|
|
|
|
reinterpret_cast<uintptr_t>(IDI) | 0x1)
|
|
|
|
);
|
|
|
|
++CurIndex;
|
|
|
|
return *IDI;
|
|
|
|
}
|