1 //===- AppendingTypeTableBuilder.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/AppendingTypeTableBuilder.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
12 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
13 #include "llvm/Support/Allocator.h"
14 #include "llvm/Support/ErrorHandling.h"
20 using namespace llvm::codeview
;
22 TypeIndex
AppendingTypeTableBuilder::nextTypeIndex() const {
23 return TypeIndex::fromArrayIndex(SeenRecords
.size());
26 AppendingTypeTableBuilder::AppendingTypeTableBuilder(BumpPtrAllocator
&Storage
)
27 : RecordStorage(Storage
) {}
29 AppendingTypeTableBuilder::~AppendingTypeTableBuilder() = default;
31 std::optional
<TypeIndex
> AppendingTypeTableBuilder::getFirst() {
35 return TypeIndex(TypeIndex::FirstNonSimpleIndex
);
38 std::optional
<TypeIndex
> AppendingTypeTableBuilder::getNext(TypeIndex Prev
) {
39 if (++Prev
== nextTypeIndex())
44 CVType
AppendingTypeTableBuilder::getType(TypeIndex Index
){
45 return CVType(SeenRecords
[Index
.toArrayIndex()]);
48 StringRef
AppendingTypeTableBuilder::getTypeName(TypeIndex Index
) {
49 llvm_unreachable("Method not implemented");
52 bool AppendingTypeTableBuilder::contains(TypeIndex Index
) {
53 if (Index
.isSimple() || Index
.isNoneType())
56 return Index
.toArrayIndex() < SeenRecords
.size();
59 uint32_t AppendingTypeTableBuilder::size() { return SeenRecords
.size(); }
61 uint32_t AppendingTypeTableBuilder::capacity() { return SeenRecords
.size(); }
63 ArrayRef
<ArrayRef
<uint8_t>> AppendingTypeTableBuilder::records() const {
67 void AppendingTypeTableBuilder::reset() { SeenRecords
.clear(); }
69 static ArrayRef
<uint8_t> stabilize(BumpPtrAllocator
&RecordStorage
,
70 ArrayRef
<uint8_t> Record
) {
71 uint8_t *Stable
= RecordStorage
.Allocate
<uint8_t>(Record
.size());
72 memcpy(Stable
, Record
.data(), Record
.size());
73 return ArrayRef
<uint8_t>(Stable
, Record
.size());
77 AppendingTypeTableBuilder::insertRecordBytes(ArrayRef
<uint8_t> &Record
) {
78 TypeIndex NewTI
= nextTypeIndex();
79 Record
= stabilize(RecordStorage
, Record
);
80 SeenRecords
.push_back(Record
);
85 AppendingTypeTableBuilder::insertRecord(ContinuationRecordBuilder
&Builder
) {
87 auto Fragments
= Builder
.end(nextTypeIndex());
88 assert(!Fragments
.empty());
89 for (auto C
: Fragments
)
90 TI
= insertRecordBytes(C
.RecordData
);
94 bool AppendingTypeTableBuilder::replaceType(TypeIndex
&Index
, CVType Data
,
96 assert(Index
.toArrayIndex() < SeenRecords
.size() &&
97 "This function cannot be used to insert records!");
99 ArrayRef
<uint8_t> Record
= Data
.data();
101 Record
= stabilize(RecordStorage
, Record
);
102 SeenRecords
[Index
.toArrayIndex()] = Record
;