/// DeclKind - This indicates which class this is.
Decl::Kind DeclKind : 8;
- /// LookupPtrKind - Describes what kind of pointer LookupPtr
- /// actually is.
- enum LookupPtrKind {
- /// LookupIsMap - Indicates that LookupPtr is actually a map.
- LookupIsMap = 7
- };
-
- /// LookupPtr - Pointer to a data structure used to lookup
- /// declarations within this context. If the context contains fewer
- /// than seven declarations, the number of declarations is provided
- /// in the 3 lowest-order bits and the upper bits are treated as a
- /// pointer to an array of NamedDecl pointers. If the context
- /// contains seven or more declarations, the upper bits are treated
- /// as a pointer to a DenseMap<DeclarationName, StoredDeclsList>.
- llvm::PointerIntPair<void*, 3> LookupPtr;
+ /// \brief Pointer to the data structure used to lookup declarations
+ /// within this context, which is a DenseMap<DeclarationName,
+ /// StoredDeclsList>.
+ void* LookupPtr;
/// FirstDecl - The first declaration stored within this declaration
/// context.
protected:
DeclContext(Decl::Kind K)
- : DeclKind(K), LookupPtr(), FirstDecl(0), LastDecl(0) { }
+ : DeclKind(K), LookupPtr(0), FirstDecl(0), LastDecl(0) { }
void DestroyDecls(ASTContext &C);
// Low-level accessors
- /// \brief Determine if the lookup structure is a
- /// DenseMap. Othewise, it is an array.
- bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; }
-
/// \brief Retrieve the internal representation of the lookup structure.
- llvm::PointerIntPair<void*, 3> getLookupPtr() const { return LookupPtr; }
+ void* getLookupPtr() const { return LookupPtr; }
static bool classof(const Decl *D);
static bool classof(const DeclContext *D) { return true; }
}
DeclContext::~DeclContext() {
- if (isLookupMap())
- delete static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
- else
- delete [] static_cast<NamedDecl**>(LookupPtr.getPointer());
+ delete static_cast<StoredDeclsMap*>(LookupPtr);
}
void DeclContext::DestroyDecls(ASTContext &C) {
/// If there is no lookup data structure, build one now by walking
/// all of the linked DeclContexts (in declaration order!) and
/// inserting their values.
- if (LookupPtr.getPointer() == 0)
+ if (!LookupPtr) {
buildLookup(this);
- if (isLookupMap()) {
- StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
- StoredDeclsMap::iterator Pos = Map->find(Name);
- if (Pos == Map->end())
+ if (!LookupPtr)
return lookup_result(0, 0);
- return Pos->second.getLookupResult();
- }
-
- // We have a small array. Look into it.
- unsigned Size = LookupPtr.getInt();
- NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
- for (unsigned Idx = 0; Idx != Size; ++Idx)
- if (Array[Idx]->getDeclName() == Name) {
- unsigned Last = Idx + 1;
- while (Last != Size && Array[Last]->getDeclName() == Name)
- ++Last;
- return lookup_result(&Array[Idx], &Array[Last]);
- }
+ }
- return lookup_result(0, 0);
+ StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
+ StoredDeclsMap::iterator Pos = Map->find(Name);
+ if (Pos == Map->end())
+ return lookup_result(0, 0);
+ return Pos->second.getLookupResult();
}
DeclContext::lookup_const_result
// If we already have a lookup data structure, perform the insertion
// into it. Otherwise, be lazy and don't build that structure until
// someone asks for it.
- if (LookupPtr.getPointer())
+ if (LookupPtr)
makeDeclVisibleInContextImpl(D);
// If we are a transparent context, insert into our parent context,
if (isa<ClassTemplateSpecializationDecl>(D))
return;
- bool MayBeRedeclaration = true;
-
- if (!isLookupMap()) {
- unsigned Size = LookupPtr.getInt();
-
- // The lookup data is stored as an array. Search through the array
- // to find the insertion location.
- NamedDecl **Array;
- if (Size == 0) {
- Array = new NamedDecl*[LookupIsMap - 1];
- LookupPtr.setPointer(Array);
- } else {
- Array = static_cast<NamedDecl **>(LookupPtr.getPointer());
- }
-
- // We always keep declarations of the same name next to each other
- // in the array, so that it is easy to return multiple results
- // from lookup().
- unsigned FirstMatch;
- for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
- if (Array[FirstMatch]->getDeclName() == D->getDeclName())
- break;
-
- unsigned InsertPos = FirstMatch;
- if (FirstMatch != Size) {
- // We found another declaration with the same name. First
- // determine whether this is a redeclaration of an existing
- // declaration in this scope, in which case we will replace the
- // existing declaration.
- unsigned LastMatch = FirstMatch;
- for (; LastMatch != Size; ++LastMatch) {
- if (Array[LastMatch]->getDeclName() != D->getDeclName())
- break;
-
- if (D->declarationReplaces(Array[LastMatch])) {
- // D is a redeclaration of an existing element in the
- // array. Replace that element with D.
- Array[LastMatch] = D;
- return;
- }
- }
-
- // [FirstMatch, LastMatch) contains the set of declarations that
- // have the same name as this declaration. Determine where the
- // declaration D will be inserted into this range.
- if (D->getKind() == Decl::UsingDirective ||
- D->getIdentifierNamespace() == Decl::IDNS_Tag)
- InsertPos = LastMatch;
- else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
- InsertPos = LastMatch - 1;
- else
- InsertPos = LastMatch;
- }
-
- if (Size < LookupIsMap - 1) {
- // The new declaration will fit in the array. Insert the new
- // declaration at the position Match in the array.
- for (unsigned Idx = Size; Idx > InsertPos; --Idx)
- Array[Idx] = Array[Idx-1];
-
- Array[InsertPos] = D;
- LookupPtr.setInt(Size + 1);
- return;
- }
-
- // We've reached capacity in this array. Create a map and copy in
- // all of the declarations that were stored in the array.
- StoredDeclsMap *Map = new StoredDeclsMap(16);
- LookupPtr.setPointer(Map);
- LookupPtr.setInt(LookupIsMap);
- for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
- makeDeclVisibleInContextImpl(Array[Idx]);
- delete [] Array;
-
- // Fall through to perform insertion into the map.
- MayBeRedeclaration = false;
- }
+ if (!LookupPtr)
+ LookupPtr = new StoredDeclsMap;
// Insert this declaration into the map.
- StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
+ StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
if (DeclNameEntries.isNull()) {
DeclNameEntries.setOnlyValue(D);
// If it is possible that this is a redeclaration, check to see if there is
// already a decl for which declarationReplaces returns true. If there is
// one, just replace it and return.
- if (MayBeRedeclaration && DeclNameEntries.HandleRedeclaration(D))
+ if (DeclNameEntries.HandleRedeclaration(D))
return;
// Put this declaration into the appropriate slot.