]> granicus.if.org Git - llvm/commitdiff
Make TypeTableBuilder inherit from TypeCollection.
authorZachary Turner <zturner@google.com>
Wed, 29 Nov 2017 19:35:21 +0000 (19:35 +0000)
committerZachary Turner <zturner@google.com>
Wed, 29 Nov 2017 19:35:21 +0000 (19:35 +0000)
A couple of places in LLD were passing references to
TypeTableCollections around, which makes it hard to change the
implementation at runtime.  However, these cases only needed to
iterate over the types in the collection, and TypeCollection
already provides a handy abstract interface for this purpose.

By implementing this interface, we can get rid of the need to
pass TypeTableBuilder references around, which should allow us
to swap the implementation at runtime in subsequent patches.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319345 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/DebugInfo/CodeView/TypeCollection.h
include/llvm/DebugInfo/CodeView/TypeTableBuilder.h
lib/DebugInfo/CodeView/TypeTableBuilder.cpp
tools/llvm-pdbutil/llvm-pdbutil.cpp
tools/llvm-readobj/COFFDumper.cpp

index 0f856f57a72751720cfe77aa34d5775eac697b19..e9fc9b0de8efe1a946835514971e2e6b76be07cd 100644 (file)
@@ -31,6 +31,16 @@ public:
   virtual bool contains(TypeIndex Index) = 0;
   virtual uint32_t size() = 0;
   virtual uint32_t capacity() = 0;
+
+  template <typename TFunc> void ForEachRecord(TFunc Func) {
+    Optional<TypeIndex> Next = getFirst();
+
+    while (Next.hasValue()) {
+      TypeIndex N = *Next;
+      Func(N, getType(N));
+      Next = getNext(N);
+    }
+  }
 };
 }
 }
index 6b5cb3a9b5f8af412ab3e38293f3cc829ec9923c..ce018d48f1635134af8dc5d0bcfbcb2bfe20f3df 100644 (file)
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/DebugInfo/CodeView/CodeView.h"
-#include "llvm/DebugInfo/CodeView/RecordSerialization.h"
 #include "llvm/DebugInfo/CodeView/SimpleTypeSerializer.h"
+#include "llvm/DebugInfo/CodeView/TypeCollection.h"
 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
-#include "llvm/DebugInfo/CodeView/TypeRecordMapping.h"
-#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
 #include "llvm/Support/Allocator.h"
-#include "llvm/Support/BinaryByteStream.h"
-#include "llvm/Support/BinaryStreamWriter.h"
 #include "llvm/Support/Error.h"
 #include <cassert>
 #include <cstdint>
@@ -35,7 +31,7 @@ namespace codeview {
 class ContinuationRecordBuilder;
 class TypeHasher;
 
-class TypeTableBuilder {
+class TypeTableBuilder : public TypeCollection {
 
   BumpPtrAllocator &RecordStorage;
   SimpleTypeSerializer SimpleSerializer;
@@ -54,10 +50,17 @@ public:
   explicit TypeTableBuilder(BumpPtrAllocator &Storage, bool Hash = true);
   ~TypeTableBuilder();
 
-  void reset();
-
-  bool empty() const { return SeenRecords.empty(); }
+  // TypeTableCollection overrides
+  Optional<TypeIndex> getFirst() override;
+  Optional<TypeIndex> getNext(TypeIndex Prev) override;
+  CVType getType(TypeIndex Index) override;
+  StringRef getTypeName(TypeIndex Index) override;
+  bool contains(TypeIndex Index) override;
+  uint32_t size() override;
+  uint32_t capacity() override;
 
+  // public interface
+  void reset();
   TypeIndex nextTypeIndex() const;
 
   BumpPtrAllocator &getAllocator() { return RecordStorage; }
@@ -71,15 +74,6 @@ public:
     ArrayRef<uint8_t> Data = SimpleSerializer.serialize(Record);
     return insertRecordBytes(Data);
   }
-
-  template <typename TFunc> void ForEachRecord(TFunc Func) {
-    uint32_t Index = TypeIndex::FirstNonSimpleIndex;
-
-    for (auto Record : SeenRecords) {
-      Func(TypeIndex(Index), Record);
-      ++Index;
-    }
-  }
 };
 
 } // end namespace codeview
index 289bf6ca93c83a2b89c6b21c1d50612ebaab138b..2559b47e804aa6c15a4d5c86556775f98ed8bba1 100644 (file)
@@ -142,6 +142,43 @@ TypeTableBuilder::TypeTableBuilder(BumpPtrAllocator &Storage, bool Hash)
 
 TypeTableBuilder::~TypeTableBuilder() = default;
 
+Optional<TypeIndex> TypeTableBuilder::getFirst() {
+  if (empty())
+    return None;
+
+  return TypeIndex(TypeIndex::FirstNonSimpleIndex);
+}
+
+Optional<TypeIndex> TypeTableBuilder::getNext(TypeIndex Prev) {
+  if (++Prev == nextTypeIndex())
+    return None;
+  return Prev;
+}
+
+CVType TypeTableBuilder::getType(TypeIndex Index) {
+  CVType Type;
+  Type.RecordData = SeenRecords[Index.toArrayIndex()];
+  const RecordPrefix *P =
+      reinterpret_cast<const RecordPrefix *>(Type.RecordData.data());
+  Type.Type = static_cast<TypeLeafKind>(uint16_t(P->RecordKind));
+  return Type;
+}
+
+StringRef TypeTableBuilder::getTypeName(TypeIndex Index) {
+  llvm_unreachable("Method not implemented");
+}
+
+bool TypeTableBuilder::contains(TypeIndex Index) {
+  if (Index.isSimple() || Index.isNoneType())
+    return false;
+
+  return Index.toArrayIndex() < SeenRecords.size();
+}
+
+uint32_t TypeTableBuilder::size() { return SeenRecords.size(); }
+
+uint32_t TypeTableBuilder::capacity() { return SeenRecords.size(); }
+
 ArrayRef<ArrayRef<uint8_t>> TypeTableBuilder::records() const {
   return SeenRecords;
 }
index 7a5cc2c45cf22c9b0684ed04506a2a80b7c769f1..43dad61037cf6686b6402050f66b6c251609db79 100644 (file)
@@ -1020,11 +1020,11 @@ static void mergePdbs() {
 
   auto &DestTpi = Builder.getTpiBuilder();
   auto &DestIpi = Builder.getIpiBuilder();
-  MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, ArrayRef<uint8_t> Data) {
-    DestTpi.addTypeRecord(Data, None);
+  MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, const CVType &Type) {
+    DestTpi.addTypeRecord(Type.RecordData, None);
   });
-  MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, ArrayRef<uint8_t> Data) {
-    DestIpi.addTypeRecord(Data, None);
+  MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, const CVType &Type) {
+    DestIpi.addTypeRecord(Type.RecordData, None);
   });
   Builder.getInfoBuilder().addFeature(PdbRaw_FeatureSig::VC140);
 
index 1ce00610edd8cea096d8f3f5f826d2f45e458163..fa56ed6942ec08c6d196cd016abf62d6eff289f3 100644 (file)
@@ -1808,8 +1808,8 @@ void llvm::dumpCodeViewMergedTypes(ScopedPrinter &Writer,
                                    llvm::codeview::TypeTableBuilder &CVTypes) {
   // Flatten it first, then run our dumper on it.
   SmallString<0> TypeBuf;
-  CVTypes.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Record) {
-    TypeBuf.append(Record.begin(), Record.end());
+  CVTypes.ForEachRecord([&](TypeIndex TI, const CVType &Record) {
+    TypeBuf.append(Record.RecordData.begin(), Record.RecordData.end());
   });
 
   TypeTableCollection TpiTypes(CVTypes.records());