1 //===- LazyRandomTypeCollection.cpp ---------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/DebugInfo/CodeView/CodeViewError.h"
15 #include "llvm/DebugInfo/CodeView/RecordName.h"
16 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
17 #include "llvm/Support/BinaryStreamReader.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/Error.h"
26 using namespace llvm::codeview
;
28 static void error(Error
&&EC
) {
29 assert(!static_cast<bool>(EC
));
31 consumeError(std::move(EC
));
34 LazyRandomTypeCollection::LazyRandomTypeCollection(uint32_t RecordCountHint
)
35 : LazyRandomTypeCollection(CVTypeArray(), RecordCountHint
,
36 PartialOffsetArray()) {}
38 LazyRandomTypeCollection::LazyRandomTypeCollection(
39 const CVTypeArray
&Types
, uint32_t RecordCountHint
,
40 PartialOffsetArray PartialOffsets
)
41 : NameStorage(Allocator
), Types(Types
), PartialOffsets(PartialOffsets
) {
42 Records
.resize(RecordCountHint
);
45 LazyRandomTypeCollection::LazyRandomTypeCollection(ArrayRef
<uint8_t> Data
,
46 uint32_t RecordCountHint
)
47 : LazyRandomTypeCollection(RecordCountHint
) {
50 LazyRandomTypeCollection::LazyRandomTypeCollection(StringRef Data
,
51 uint32_t RecordCountHint
)
52 : LazyRandomTypeCollection(
53 makeArrayRef(Data
.bytes_begin(), Data
.bytes_end()), RecordCountHint
) {
56 LazyRandomTypeCollection::LazyRandomTypeCollection(const CVTypeArray
&Types
,
58 : LazyRandomTypeCollection(Types
, NumRecords
, PartialOffsetArray()) {}
60 void LazyRandomTypeCollection::reset(BinaryStreamReader
&Reader
,
61 uint32_t RecordCountHint
) {
63 PartialOffsets
= PartialOffsetArray();
65 error(Reader
.readArray(Types
, Reader
.bytesRemaining()));
67 // Clear and then resize, to make sure existing data gets destroyed.
69 Records
.resize(RecordCountHint
);
72 void LazyRandomTypeCollection::reset(StringRef Data
, uint32_t RecordCountHint
) {
73 BinaryStreamReader
Reader(Data
, support::little
);
74 reset(Reader
, RecordCountHint
);
77 void LazyRandomTypeCollection::reset(ArrayRef
<uint8_t> Data
,
78 uint32_t RecordCountHint
) {
79 BinaryStreamReader
Reader(Data
, support::little
);
80 reset(Reader
, RecordCountHint
);
83 uint32_t LazyRandomTypeCollection::getOffsetOfType(TypeIndex Index
) {
84 error(ensureTypeExists(Index
));
85 assert(contains(Index
));
87 return Records
[Index
.toArrayIndex()].Offset
;
90 CVType
LazyRandomTypeCollection::getType(TypeIndex Index
) {
91 assert(!Index
.isSimple());
93 auto EC
= ensureTypeExists(Index
);
95 assert(contains(Index
));
97 return Records
[Index
.toArrayIndex()].Type
;
100 Optional
<CVType
> LazyRandomTypeCollection::tryGetType(TypeIndex Index
) {
101 if (Index
.isSimple())
104 if (auto EC
= ensureTypeExists(Index
)) {
105 consumeError(std::move(EC
));
109 assert(contains(Index
));
110 return Records
[Index
.toArrayIndex()].Type
;
113 StringRef
LazyRandomTypeCollection::getTypeName(TypeIndex Index
) {
114 if (Index
.isNoneType() || Index
.isSimple())
115 return TypeIndex::simpleTypeName(Index
);
117 // Try to make sure the type exists. Even if it doesn't though, it may be
118 // because we're dumping a symbol stream with no corresponding type stream
119 // present, in which case we still want to be able to print <unknown UDT>
120 // for the type names.
121 if (auto EC
= ensureTypeExists(Index
)) {
122 consumeError(std::move(EC
));
123 return "<unknown UDT>";
126 uint32_t I
= Index
.toArrayIndex();
127 ensureCapacityFor(Index
);
128 if (Records
[I
].Name
.data() == nullptr) {
129 StringRef Result
= NameStorage
.save(computeTypeName(*this, Index
));
130 Records
[I
].Name
= Result
;
132 return Records
[I
].Name
;
135 bool LazyRandomTypeCollection::contains(TypeIndex Index
) {
136 if (Index
.isSimple() || Index
.isNoneType())
139 if (Records
.size() <= Index
.toArrayIndex())
141 if (!Records
[Index
.toArrayIndex()].Type
.valid())
146 uint32_t LazyRandomTypeCollection::size() { return Count
; }
148 uint32_t LazyRandomTypeCollection::capacity() { return Records
.size(); }
150 Error
LazyRandomTypeCollection::ensureTypeExists(TypeIndex TI
) {
152 return Error::success();
154 return visitRangeForType(TI
);
157 void LazyRandomTypeCollection::ensureCapacityFor(TypeIndex Index
) {
158 assert(!Index
.isSimple());
159 uint32_t MinSize
= Index
.toArrayIndex() + 1;
161 if (MinSize
<= capacity())
164 uint32_t NewCapacity
= MinSize
* 3 / 2;
166 assert(NewCapacity
> capacity());
167 Records
.resize(NewCapacity
);
170 Error
LazyRandomTypeCollection::visitRangeForType(TypeIndex TI
) {
171 assert(!TI
.isSimple());
172 if (PartialOffsets
.empty())
173 return fullScanForType(TI
);
175 auto Next
= std::upper_bound(PartialOffsets
.begin(), PartialOffsets
.end(), TI
,
176 [](TypeIndex Value
, const TypeIndexOffset
&IO
) {
177 return Value
< IO
.Type
;
180 assert(Next
!= PartialOffsets
.begin());
181 auto Prev
= std::prev(Next
);
183 TypeIndex TIB
= Prev
->Type
;
185 // They've asked us to fetch a type index, but the entry we found in the
186 // partial offsets array has already been visited. Since we visit an entire
187 // block every time, that means this record should have been previously
188 // discovered. Ultimately, this means this is a request for a non-existant
190 return make_error
<CodeViewError
>("Invalid type index");
194 if (Next
== PartialOffsets
.end()) {
195 TIE
= TypeIndex::fromArrayIndex(capacity());
200 visitRange(TIB
, Prev
->Offset
, TIE
);
201 return Error::success();
204 Optional
<TypeIndex
> LazyRandomTypeCollection::getFirst() {
205 TypeIndex TI
= TypeIndex::fromArrayIndex(0);
206 if (auto EC
= ensureTypeExists(TI
)) {
207 consumeError(std::move(EC
));
213 Optional
<TypeIndex
> LazyRandomTypeCollection::getNext(TypeIndex Prev
) {
214 // We can't be sure how long this type stream is, given that the initial count
215 // given to the constructor is just a hint. So just try to make sure the next
216 // record exists, and if anything goes wrong, we must be at the end.
217 if (auto EC
= ensureTypeExists(Prev
+ 1)) {
218 consumeError(std::move(EC
));
225 Error
LazyRandomTypeCollection::fullScanForType(TypeIndex TI
) {
226 assert(!TI
.isSimple());
227 assert(PartialOffsets
.empty());
229 TypeIndex CurrentTI
= TypeIndex::fromArrayIndex(0);
230 auto Begin
= Types
.begin();
233 // In the case of type streams which we don't know the number of records of,
234 // it's possible to search for a type index triggering a full scan, but then
235 // later additional records are added since we didn't know how many there
236 // would be until we did a full visitation, then you try to access the new
237 // type triggering another full scan. To avoid this, we assume that if the
238 // database has some records, this must be what's going on. We can also
239 // assume that this index must be larger than the largest type index we've
240 // visited, so we start from there and scan forward.
241 uint32_t Offset
= Records
[LargestTypeIndex
.toArrayIndex()].Offset
;
242 CurrentTI
= LargestTypeIndex
+ 1;
243 Begin
= Types
.at(Offset
);
247 auto End
= Types
.end();
248 while (Begin
!= End
) {
249 ensureCapacityFor(CurrentTI
);
250 LargestTypeIndex
= std::max(LargestTypeIndex
, CurrentTI
);
251 auto Idx
= CurrentTI
.toArrayIndex();
252 Records
[Idx
].Type
= *Begin
;
253 Records
[Idx
].Offset
= Begin
.offset();
258 if (CurrentTI
<= TI
) {
259 return make_error
<CodeViewError
>("Type Index does not exist!");
261 return Error::success();
264 void LazyRandomTypeCollection::visitRange(TypeIndex Begin
, uint32_t BeginOffset
,
266 auto RI
= Types
.at(BeginOffset
);
267 assert(RI
!= Types
.end());
269 ensureCapacityFor(End
);
270 while (Begin
!= End
) {
271 LargestTypeIndex
= std::max(LargestTypeIndex
, Begin
);
272 auto Idx
= Begin
.toArrayIndex();
273 Records
[Idx
].Type
= *RI
;
274 Records
[Idx
].Offset
= RI
.offset();