1 //===- DbiModuleList.h - PDB module information list ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_DEBUGINFO_PDB_RAW_DBIMODULELIST_H
11 #define LLVM_DEBUGINFO_PDB_RAW_DBIMODULELIST_H
12 
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/iterator.h"
15 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
16 #include "llvm/Support/BinaryStreamArray.h"
17 #include "llvm/Support/BinaryStreamRef.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Error.h"
20 #include <cstdint>
21 #include <vector>
22 
23 namespace llvm {
24 namespace pdb {
25 
26 class DbiModuleList;
27 struct FileInfoSubstreamHeader;
28 
29 class DbiModuleSourceFilesIterator
30     : public iterator_facade_base<DbiModuleSourceFilesIterator,
31                                   std::random_access_iterator_tag, StringRef> {
32   typedef iterator_facade_base<DbiModuleSourceFilesIterator,
33                                std::random_access_iterator_tag, StringRef>
34       BaseType;
35 
36 public:
37   DbiModuleSourceFilesIterator(const DbiModuleList &Modules, uint32_t Modi,
38                                uint16_t Filei);
39   DbiModuleSourceFilesIterator() = default;
40   DbiModuleSourceFilesIterator &
41   operator=(const DbiModuleSourceFilesIterator &R) = default;
42 
43   bool operator==(const DbiModuleSourceFilesIterator &R) const;
44 
45   const StringRef &operator*() const { return ThisValue; }
46   StringRef &operator*() { return ThisValue; }
47 
48   bool operator<(const DbiModuleSourceFilesIterator &RHS) const;
49   std::ptrdiff_t operator-(const DbiModuleSourceFilesIterator &R) const;
50   DbiModuleSourceFilesIterator &operator+=(std::ptrdiff_t N);
51   DbiModuleSourceFilesIterator &operator-=(std::ptrdiff_t N);
52 
53 private:
54   void setValue();
55 
56   bool isEnd() const;
57   bool isCompatible(const DbiModuleSourceFilesIterator &R) const;
58   bool isUniversalEnd() const;
59 
60   StringRef ThisValue;
61   const DbiModuleList *Modules{nullptr};
62   uint32_t Modi{0};
63   uint16_t Filei{0};
64 };
65 
66 class DbiModuleList {
67   friend DbiModuleSourceFilesIterator;
68 
69 public:
70   Error initialize(BinaryStreamRef ModInfo, BinaryStreamRef FileInfo);
71 
72   Expected<StringRef> getFileName(uint32_t Index) const;
73   uint32_t getModuleCount() const;
74   uint32_t getSourceFileCount() const;
75   uint16_t getSourceFileCount(uint32_t Modi) const;
76 
77   iterator_range<DbiModuleSourceFilesIterator>
78   source_files(uint32_t Modi) const;
79 
80   DbiModuleDescriptor getModuleDescriptor(uint32_t Modi) const;
81 
82 private:
83   Error initializeModInfo(BinaryStreamRef ModInfo);
84   Error initializeFileInfo(BinaryStreamRef FileInfo);
85 
86   VarStreamArray<DbiModuleDescriptor> Descriptors;
87 
88   FixedStreamArray<support::little32_t> FileNameOffsets;
89   FixedStreamArray<support::ulittle16_t> ModFileCountArray;
90 
91   // For each module, there are multiple filenames, which can be obtained by
92   // knowing the index of the file.  Given the index of the file, one can use
93   // that as an offset into the FileNameOffsets array, which contains the
94   // absolute offset of the file name in NamesBuffer.  Thus, for each module
95   // we store the first index in the FileNameOffsets array for this module.
96   // The number of files for the corresponding module is stored in
97   // ModFileCountArray.
98   std::vector<uint32_t> ModuleInitialFileIndex;
99 
100   // In order to provide random access into the Descriptors array, we iterate it
101   // once up front to find the offsets of the individual items and store them in
102   // this array.
103   std::vector<uint32_t> ModuleDescriptorOffsets;
104 
105   const FileInfoSubstreamHeader *FileInfoHeader = nullptr;
106 
107   BinaryStreamRef ModInfoSubstream;
108   BinaryStreamRef FileInfoSubstream;
109   BinaryStreamRef NamesBuffer;
110 };
111 }
112 }
113 
114 #endif // LLVM_DEBUGINFO_PDB_RAW_DBIMODULELIST_H