[pdb] Use MappedBlockStream to parse the PDB directory.

In order to efficiently write PDBs, we need to be able to make a
StreamWriter class similar to a StreamReader, which can transparently deal
with writing to discontiguous streams, and we need to use this for all
writing, similar to how we use StreamReader for all reading.

Most discontiguous streams are the typical numbered streams that appear in
a PDB file and are described by the directory, but the exception to this,
that until now has been parsed by hand, is the directory itself.
MappedBlockStream works by querying the directory to find out which blocks
a stream occupies and various other things, so naturally the same logic
could not possibly work to describe the blocks that the directory itself
resided on.

To solve this, I've introduced an abstraction IPDBStreamData, which allows
the client to query for the list of blocks occupied by the stream, as well
as the stream length. I provide two implementations of this: one which
queries the directory (for indexed streams), and one which queries the
super block (for the directory stream).

This has the side benefit of vastly simplifying the code to parse the
directory. Whereas before a mini state machine was rolled by hand, now we
simply use FixedStreamArray to read out the stream sizes, then build a
vector of FixedStreamArrays for the stream map, all in just a few lines of
code.

Reviewed By: ruiu
Differential Revision: http://reviews.llvm.org/D21046

llvm-svn: 271982
diff --git a/llvm/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp b/llvm/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp
index 34d1671..03462b3 100644
--- a/llvm/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp
+++ b/llvm/lib/DebugInfo/PDB/Raw/MappedBlockStream.cpp
@@ -8,28 +8,23 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
+#include "llvm/DebugInfo/PDB/Raw/IPDBStreamData.h"
 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
 #include "llvm/DebugInfo/PDB/Raw/RawError.h"
 
 using namespace llvm;
 using namespace llvm::pdb;
 
-MappedBlockStream::MappedBlockStream(uint32_t StreamIdx, const IPDBFile &File)
-    : Pdb(File) {
-  if (StreamIdx >= Pdb.getNumStreams()) {
-    StreamLength = 0;
-  } else {
-    StreamLength = Pdb.getStreamByteSize(StreamIdx);
-    BlockList = Pdb.getStreamBlockList(StreamIdx);
-  }
-}
+MappedBlockStream::MappedBlockStream(std::unique_ptr<IPDBStreamData> Data,
+                                     const IPDBFile &Pdb)
+    : Pdb(Pdb), Data(std::move(Data)) {}
 
 Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
                                    ArrayRef<uint8_t> &Buffer) const {
   // Make sure we aren't trying to read beyond the end of the stream.
-  if (Size > StreamLength)
+  if (Size > Data->getLength())
     return make_error<RawError>(raw_error_code::insufficient_buffer);
-  if (Offset > StreamLength - Size)
+  if (Offset > Data->getLength() - Size)
     return make_error<RawError>(raw_error_code::insufficient_buffer);
 
   if (tryReadContiguously(Offset, Size, Buffer))
@@ -57,6 +52,8 @@
   return Error::success();
 }
 
+uint32_t MappedBlockStream::getLength() const { return Data->getLength(); }
+
 bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
                                             ArrayRef<uint8_t> &Buffer) const {
   // Attempt to fulfill the request with a reference directly into the stream.
@@ -72,6 +69,7 @@
       llvm::alignTo(Size - BytesFromFirstBlock, Pdb.getBlockSize()) /
       Pdb.getBlockSize();
 
+  auto BlockList = Data->getStreamBlocks();
   uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
   uint32_t E = BlockList[BlockNum];
   for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
@@ -93,14 +91,15 @@
   uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
 
   // Make sure we aren't trying to read beyond the end of the stream.
-  if (Buffer.size() > StreamLength)
+  if (Buffer.size() > Data->getLength())
     return make_error<RawError>(raw_error_code::insufficient_buffer);
-  if (Offset > StreamLength - Buffer.size())
+  if (Offset > Data->getLength() - Buffer.size())
     return make_error<RawError>(raw_error_code::insufficient_buffer);
 
   uint32_t BytesLeft = Buffer.size();
   uint32_t BytesWritten = 0;
   uint8_t *WriteBuffer = Buffer.data();
+  auto BlockList = Data->getStreamBlocks();
   while (BytesLeft > 0) {
     uint32_t StreamBlockAddr = BlockList[BlockNum];