1 //===- TpiStreamBuilder.h - PDB Tpi Stream Creation -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_DEBUGINFO_PDB_NATIVE_TPISTREAMBUILDER_H
10 #define LLVM_DEBUGINFO_PDB_NATIVE_TPISTREAMBUILDER_H
11 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/DebugInfo/CodeView/CVRecord.h"
14 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
15 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
16 #include "llvm/Support/Allocator.h"
17 #include "llvm/Support/BinaryStreamRef.h"
18 #include "llvm/Support/Error.h"
19 
20 #include <vector>
21 
22 namespace llvm {
23 class BinaryByteStream;
24 template <typename T> struct BinaryItemTraits;
25 
26 template <> struct BinaryItemTraits<llvm::codeview::CVType> {
27   static size_t length(const codeview::CVType &Item) { return Item.length(); }
28   static ArrayRef<uint8_t> bytes(const codeview::CVType &Item) {
29     return Item.data();
30   }
31 };
32 
33 namespace msf {
34 class MSFBuilder;
35 struct MSFLayout;
36 }
37 namespace pdb {
38 struct TpiStreamHeader;
39 
40 class TpiStreamBuilder {
41 public:
42   explicit TpiStreamBuilder(msf::MSFBuilder &Msf, uint32_t StreamIdx);
43   ~TpiStreamBuilder();
44 
45   TpiStreamBuilder(const TpiStreamBuilder &) = delete;
46   TpiStreamBuilder &operator=(const TpiStreamBuilder &) = delete;
47 
48   void setVersionHeader(PdbRaw_TpiVer Version);
49   void addTypeRecord(ArrayRef<uint8_t> Type, Optional<uint32_t> Hash);
50   void addTypeRecords(ArrayRef<uint8_t> Types, ArrayRef<uint16_t> Sizes,
51                       ArrayRef<uint32_t> Hashes);
52 
53   Error finalizeMsfLayout();
54 
55   uint32_t getRecordCount() const { return TypeRecordCount; }
56 
57   Error commit(const msf::MSFLayout &Layout, WritableBinaryStreamRef Buffer);
58 
59   uint32_t calculateSerializedLength();
60 
61 private:
62   void updateTypeIndexOffsets(ArrayRef<uint16_t> Sizes);
63 
64   uint32_t calculateHashBufferSize() const;
65   uint32_t calculateIndexOffsetSize() const;
66   Error finalize();
67 
68   msf::MSFBuilder &Msf;
69   BumpPtrAllocator &Allocator;
70 
71   uint32_t TypeRecordCount = 0;
72   size_t TypeRecordBytes = 0;
73 
74   PdbRaw_TpiVer VerHeader = PdbRaw_TpiVer::PdbTpiV80;
75   std::vector<ArrayRef<uint8_t>> TypeRecBuffers;
76   std::vector<uint32_t> TypeHashes;
77   std::vector<codeview::TypeIndexOffset> TypeIndexOffsets;
78   uint32_t HashStreamIndex = kInvalidStreamIndex;
79   std::unique_ptr<BinaryByteStream> HashValueStream;
80 
81   const TpiStreamHeader *Header;
82   uint32_t Idx;
83 };
84 } // namespace pdb
85 }
86 
87 #endif
88