From: Zachary Turner Date: Wed, 29 Nov 2017 19:35:21 +0000 (+0000) Subject: Make TypeTableBuilder inherit from TypeCollection. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7fe0c00835994ebd08ccf0f2b962fb602c9d3e9e;p=llvm Make TypeTableBuilder inherit from TypeCollection. 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 --- diff --git a/include/llvm/DebugInfo/CodeView/TypeCollection.h b/include/llvm/DebugInfo/CodeView/TypeCollection.h index 0f856f57a72..e9fc9b0de8e 100644 --- a/include/llvm/DebugInfo/CodeView/TypeCollection.h +++ b/include/llvm/DebugInfo/CodeView/TypeCollection.h @@ -31,6 +31,16 @@ public: virtual bool contains(TypeIndex Index) = 0; virtual uint32_t size() = 0; virtual uint32_t capacity() = 0; + + template void ForEachRecord(TFunc Func) { + Optional Next = getFirst(); + + while (Next.hasValue()) { + TypeIndex N = *Next; + Func(N, getType(N)); + Next = getNext(N); + } + } }; } } diff --git a/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h b/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h index 6b5cb3a9b5f..ce018d48f16 100644 --- a/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h +++ b/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h @@ -14,15 +14,11 @@ #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 #include @@ -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 getFirst() override; + Optional 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 Data = SimpleSerializer.serialize(Record); return insertRecordBytes(Data); } - - template void ForEachRecord(TFunc Func) { - uint32_t Index = TypeIndex::FirstNonSimpleIndex; - - for (auto Record : SeenRecords) { - Func(TypeIndex(Index), Record); - ++Index; - } - } }; } // end namespace codeview diff --git a/lib/DebugInfo/CodeView/TypeTableBuilder.cpp b/lib/DebugInfo/CodeView/TypeTableBuilder.cpp index 289bf6ca93c..2559b47e804 100644 --- a/lib/DebugInfo/CodeView/TypeTableBuilder.cpp +++ b/lib/DebugInfo/CodeView/TypeTableBuilder.cpp @@ -142,6 +142,43 @@ TypeTableBuilder::TypeTableBuilder(BumpPtrAllocator &Storage, bool Hash) TypeTableBuilder::~TypeTableBuilder() = default; +Optional TypeTableBuilder::getFirst() { + if (empty()) + return None; + + return TypeIndex(TypeIndex::FirstNonSimpleIndex); +} + +Optional 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(Type.RecordData.data()); + Type.Type = static_cast(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> TypeTableBuilder::records() const { return SeenRecords; } diff --git a/tools/llvm-pdbutil/llvm-pdbutil.cpp b/tools/llvm-pdbutil/llvm-pdbutil.cpp index 7a5cc2c45cf..43dad61037c 100644 --- a/tools/llvm-pdbutil/llvm-pdbutil.cpp +++ b/tools/llvm-pdbutil/llvm-pdbutil.cpp @@ -1020,11 +1020,11 @@ static void mergePdbs() { auto &DestTpi = Builder.getTpiBuilder(); auto &DestIpi = Builder.getIpiBuilder(); - MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, ArrayRef Data) { - DestTpi.addTypeRecord(Data, None); + MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, const CVType &Type) { + DestTpi.addTypeRecord(Type.RecordData, None); }); - MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, ArrayRef Data) { - DestIpi.addTypeRecord(Data, None); + MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, const CVType &Type) { + DestIpi.addTypeRecord(Type.RecordData, None); }); Builder.getInfoBuilder().addFeature(PdbRaw_FeatureSig::VC140); diff --git a/tools/llvm-readobj/COFFDumper.cpp b/tools/llvm-readobj/COFFDumper.cpp index 1ce00610edd..fa56ed6942e 100644 --- a/tools/llvm-readobj/COFFDumper.cpp +++ b/tools/llvm-readobj/COFFDumper.cpp @@ -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 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());