[cfe-commits] r68715 - in /cfe/trunk: include/clang/AST/DeclBase.h lib/AST/DeclBase.cpp

Douglas Gregor dgregor at apple.com
Thu Apr 9 10:29:08 PDT 2009


Author: dgregor
Date: Thu Apr  9 12:29:08 2009
New Revision: 68715

URL: http://llvm.org/viewvc/llvm-project?rev=68715&view=rev
Log:
Simple DeclContext's internal representation by always storing a
StoredDeclsMap, instead of using the it's-an-array-or-its-a-map
trick. I'll verify that performance isn't impacted later; for now, I
need the common representation.


Modified:
    cfe/trunk/include/clang/AST/DeclBase.h
    cfe/trunk/lib/AST/DeclBase.cpp

Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=68715&r1=68714&r2=68715&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Thu Apr  9 12:29:08 2009
@@ -349,21 +349,10 @@
   /// 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.
@@ -377,7 +366,7 @@
 
 protected:
    DeclContext(Decl::Kind K) 
-     : DeclKind(K), LookupPtr(), FirstDecl(0), LastDecl(0) { }
+     : DeclKind(K), LookupPtr(0), FirstDecl(0), LastDecl(0) { }
 
   void DestroyDecls(ASTContext &C);
 
@@ -759,12 +748,8 @@
 
   // 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; }

Modified: cfe/trunk/lib/AST/DeclBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=68715&r1=68714&r2=68715&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Thu Apr  9 12:29:08 2009
@@ -365,10 +365,7 @@
 }
 
 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) {
@@ -488,29 +485,18 @@
   /// 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 
@@ -550,7 +536,7 @@
   // 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,
@@ -570,86 +556,11 @@
   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);
@@ -659,7 +570,7 @@
   // 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.





More information about the cfe-commits mailing list