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/CodeView.h"
12 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
13 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/ErrorHandling.h"
21 using namespace llvm::codeview
;
23 TypeIndex
AppendingTypeTableBuilder::nextTypeIndex() const {
24 return TypeIndex::fromArrayIndex(SeenRecords
.size());
27 AppendingTypeTableBuilder::AppendingTypeTableBuilder(BumpPtrAllocator
&Storage
)
28 : RecordStorage(Storage
) {}
30 AppendingTypeTableBuilder::~AppendingTypeTableBuilder() = default;
32 std::optional
<TypeIndex
> AppendingTypeTableBuilder::getFirst() {
36 return TypeIndex(TypeIndex::FirstNonSimpleIndex
);
39 std::optional
<TypeIndex
> AppendingTypeTableBuilder::getNext(TypeIndex Prev
) {
40 if (++Prev
== nextTypeIndex())
45 CVType
AppendingTypeTableBuilder::getType(TypeIndex Index
){
46 return CVType(SeenRecords
[Index
.toArrayIndex()]);
49 StringRef
AppendingTypeTableBuilder::getTypeName(TypeIndex Index
) {
50 llvm_unreachable("Method not implemented");
53 bool AppendingTypeTableBuilder::contains(TypeIndex Index
) {
54 if (Index
.isSimple() || Index
.isNoneType())
57 return Index
.toArrayIndex() < SeenRecords
.size();
60 uint32_t AppendingTypeTableBuilder::size() { return SeenRecords
.size(); }
62 uint32_t AppendingTypeTableBuilder::capacity() { return SeenRecords
.size(); }
64 ArrayRef
<ArrayRef
<uint8_t>> AppendingTypeTableBuilder::records() const {
68 void AppendingTypeTableBuilder::reset() { SeenRecords
.clear(); }
70 static ArrayRef
<uint8_t> stabilize(BumpPtrAllocator
&RecordStorage
,
71 ArrayRef
<uint8_t> Record
) {
72 uint8_t *Stable
= RecordStorage
.Allocate
<uint8_t>(Record
.size());
73 memcpy(Stable
, Record
.data(), Record
.size());
74 return ArrayRef
<uint8_t>(Stable
, Record
.size());
78 AppendingTypeTableBuilder::insertRecordBytes(ArrayRef
<uint8_t> &Record
) {
79 TypeIndex NewTI
= nextTypeIndex();
80 Record
= stabilize(RecordStorage
, Record
);
81 SeenRecords
.push_back(Record
);
86 AppendingTypeTableBuilder::insertRecord(ContinuationRecordBuilder
&Builder
) {
88 auto Fragments
= Builder
.end(nextTypeIndex());
89 assert(!Fragments
.empty());
90 for (auto C
: Fragments
)
91 TI
= insertRecordBytes(C
.RecordData
);
95 bool AppendingTypeTableBuilder::replaceType(TypeIndex
&Index
, CVType Data
,
97 assert(Index
.toArrayIndex() < SeenRecords
.size() &&
98 "This function cannot be used to insert records!");
100 ArrayRef
<uint8_t> Record
= Data
.data();
102 Record
= stabilize(RecordStorage
, Record
);
103 SeenRecords
[Index
.toArrayIndex()] = Record
;