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