178344Sobrien//===- PDBFileBuilder.h - PDB File Creation ---------------------*- C++ -*-===//
278344Sobrien//
398184Sgordon// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
478344Sobrien// See https://llvm.org/LICENSE.txt for license information.
578344Sobrien// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
678344Sobrien//
778344Sobrien//===----------------------------------------------------------------------===//
898184Sgordon
978344Sobrien#ifndef LLVM_DEBUGINFO_PDB_RAW_PDBFILEBUILDER_H
1078344Sobrien#define LLVM_DEBUGINFO_PDB_RAW_PDBFILEBUILDER_H
1178344Sobrien
1278344Sobrien#include "llvm/ADT/ArrayRef.h"
1378344Sobrien#include "llvm/ADT/BitVector.h"
1478344Sobrien#include "llvm/ADT/Optional.h"
1578344Sobrien#include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
1678344Sobrien#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
17121068Sdougb#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
1898184Sgordon#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
1978344Sobrien#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
2078344Sobrien#include "llvm/Support/Allocator.h"
2178344Sobrien#include "llvm/Support/Endian.h"
2278344Sobrien#include "llvm/Support/Error.h"
2378344Sobrien#include "llvm/Support/MemoryBuffer.h"
2478344Sobrien
25197947Sdougb#include <memory>
2678344Sobrien#include <vector>
2798184Sgordon
2878344Sobriennamespace llvm {
2998184Sgordonnamespace msf {
30121068Sdougbclass MSFBuilder;
31179945Smtm}
32121068Sdougbnamespace pdb {
33121068Sdougbclass DbiStreamBuilder;
34121068Sdougbclass InfoStreamBuilder;
35121068Sdougbclass GSIStreamBuilder;
36121068Sdougbclass TpiStreamBuilder;
37121068Sdougb
38121068Sdougbclass PDBFileBuilder {
3998184Sgordonpublic:
4098184Sgordon  explicit PDBFileBuilder(BumpPtrAllocator &Allocator);
4198184Sgordon  ~PDBFileBuilder();
4298184Sgordon  PDBFileBuilder(const PDBFileBuilder &) = delete;
4378344Sobrien  PDBFileBuilder &operator=(const PDBFileBuilder &) = delete;
44121068Sdougb
45197947Sdougb  Error initialize(uint32_t BlockSize);
4678344Sobrien
4778344Sobrien  msf::MSFBuilder &getMsfBuilder();
4878344Sobrien  InfoStreamBuilder &getInfoBuilder();
4978344Sobrien  DbiStreamBuilder &getDbiBuilder();
50  TpiStreamBuilder &getTpiBuilder();
51  TpiStreamBuilder &getIpiBuilder();
52  PDBStringTableBuilder &getStringTableBuilder();
53  GSIStreamBuilder &getGsiBuilder();
54
55  // If HashPDBContentsToGUID is true on the InfoStreamBuilder, Guid is filled
56  // with the computed PDB GUID on return.
57  Error commit(StringRef Filename, codeview::GUID *Guid);
58
59  Expected<uint32_t> getNamedStreamIndex(StringRef Name) const;
60  Error addNamedStream(StringRef Name, StringRef Data);
61  void addInjectedSource(StringRef Name, std::unique_ptr<MemoryBuffer> Buffer);
62
63private:
64  struct InjectedSourceDescriptor {
65    // The full name of the stream that contains the contents of this injected
66    // source.  This is built as a concatenation of the literal "/src/files"
67    // plus the "vname".
68    std::string StreamName;
69
70    // The exact name of the file name as specified by the user.
71    uint32_t NameIndex;
72
73    // The string table index of the "vname" of the file.  As far as we
74    // understand, this is the same as the name, except it is lowercased and
75    // forward slashes are converted to backslashes.
76    uint32_t VNameIndex;
77    std::unique_ptr<MemoryBuffer> Content;
78  };
79
80  Error finalizeMsfLayout();
81  Expected<uint32_t> allocateNamedStream(StringRef Name, uint32_t Size);
82
83  void commitFpm(WritableBinaryStream &MsfBuffer, const msf::MSFLayout &Layout);
84  void commitInjectedSources(WritableBinaryStream &MsfBuffer,
85                             const msf::MSFLayout &Layout);
86  void commitSrcHeaderBlock(WritableBinaryStream &MsfBuffer,
87                            const msf::MSFLayout &Layout);
88
89  BumpPtrAllocator &Allocator;
90
91  std::unique_ptr<msf::MSFBuilder> Msf;
92  std::unique_ptr<InfoStreamBuilder> Info;
93  std::unique_ptr<DbiStreamBuilder> Dbi;
94  std::unique_ptr<GSIStreamBuilder> Gsi;
95  std::unique_ptr<TpiStreamBuilder> Tpi;
96  std::unique_ptr<TpiStreamBuilder> Ipi;
97
98  PDBStringTableBuilder Strings;
99  StringTableHashTraits InjectedSourceHashTraits;
100  HashTable<SrcHeaderBlockEntry> InjectedSourceTable;
101
102  SmallVector<InjectedSourceDescriptor, 2> InjectedSources;
103
104  NamedStreamMap NamedStreams;
105  DenseMap<uint32_t, std::string> NamedStreamData;
106};
107}
108}
109
110#endif
111