1//===- AppendingTypeTableBuilder.cpp --------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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"
15#include <cassert>
16#include <cstdint>
17#include <cstring>
18
19using namespace llvm;
20using namespace llvm::codeview;
21
22TypeIndex AppendingTypeTableBuilder::nextTypeIndex() const {
23 return TypeIndex::fromArrayIndex(Index: SeenRecords.size());
24}
25
26AppendingTypeTableBuilder::AppendingTypeTableBuilder(BumpPtrAllocator &Storage)
27 : RecordStorage(Storage) {}
28
29AppendingTypeTableBuilder::~AppendingTypeTableBuilder() = default;
30
31std::optional<TypeIndex> AppendingTypeTableBuilder::getFirst() {
32 if (empty())
33 return std::nullopt;
34
35 return TypeIndex(TypeIndex::FirstNonSimpleIndex);
36}
37
38std::optional<TypeIndex> AppendingTypeTableBuilder::getNext(TypeIndex Prev) {
39 if (++Prev == nextTypeIndex())
40 return std::nullopt;
41 return Prev;
42}
43
44CVType AppendingTypeTableBuilder::getType(TypeIndex Index){
45 return CVType(SeenRecords[Index.toArrayIndex()]);
46}
47
48StringRef AppendingTypeTableBuilder::getTypeName(TypeIndex Index) {
49 llvm_unreachable("Method not implemented");
50}
51
52bool AppendingTypeTableBuilder::contains(TypeIndex Index) {
53 if (Index.isSimple() || Index.isNoneType())
54 return false;
55
56 return Index.toArrayIndex() < SeenRecords.size();
57}
58
59uint32_t AppendingTypeTableBuilder::size() { return SeenRecords.size(); }
60
61uint32_t AppendingTypeTableBuilder::capacity() { return SeenRecords.size(); }
62
63ArrayRef<ArrayRef<uint8_t>> AppendingTypeTableBuilder::records() const {
64 return SeenRecords;
65}
66
67void AppendingTypeTableBuilder::reset() { SeenRecords.clear(); }
68
69static ArrayRef<uint8_t> stabilize(BumpPtrAllocator &RecordStorage,
70 ArrayRef<uint8_t> Record) {
71 uint8_t *Stable = RecordStorage.Allocate<uint8_t>(Num: Record.size());
72 memcpy(dest: Stable, src: Record.data(), n: Record.size());
73 return ArrayRef<uint8_t>(Stable, Record.size());
74}
75
76TypeIndex
77AppendingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) {
78 TypeIndex NewTI = nextTypeIndex();
79 Record = stabilize(RecordStorage, Record);
80 SeenRecords.push_back(Elt: Record);
81 return NewTI;
82}
83
84TypeIndex
85AppendingTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
86 TypeIndex TI;
87 auto Fragments = Builder.end(Index: nextTypeIndex());
88 assert(!Fragments.empty());
89 for (auto C : Fragments)
90 TI = insertRecordBytes(Record&: C.RecordData);
91 return TI;
92}
93
94bool AppendingTypeTableBuilder::replaceType(TypeIndex &Index, CVType Data,
95 bool Stabilize) {
96 assert(Index.toArrayIndex() < SeenRecords.size() &&
97 "This function cannot be used to insert records!");
98
99 ArrayRef<uint8_t> Record = Data.data();
100 if (Stabilize)
101 Record = stabilize(RecordStorage, Record);
102 SeenRecords[Index.toArrayIndex()] = Record;
103 return true;
104}
105