aboutsummaryrefslogtreecommitdiff
path: root/lib/DebugInfo/CodeView/TypeDatabase.cpp
blob: 08f848b36a9d5aeda4d73e31785c2f792b10407e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//===- TypeDatabase.cpp --------------------------------------- *- C++ --*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/DebugInfo/CodeView/TypeDatabase.h"

using namespace llvm;
using namespace llvm::codeview;

TypeDatabase::TypeDatabase(uint32_t Capacity) : TypeNameStorage(Allocator) {
  CVUDTNames.resize(Capacity);
  TypeRecords.resize(Capacity);
  ValidRecords.resize(Capacity);
}

TypeIndex TypeDatabase::appendType(StringRef Name, const CVType &Data) {
  LargestTypeIndex = getAppendIndex();
  if (LargestTypeIndex.toArrayIndex() >= capacity())
    grow();
  recordType(Name, LargestTypeIndex, Data);
  return LargestTypeIndex;
}

void TypeDatabase::recordType(StringRef Name, TypeIndex Index,
                              const CVType &Data) {
  LargestTypeIndex = empty() ? Index : std::max(Index, LargestTypeIndex);

  if (LargestTypeIndex.toArrayIndex() >= capacity())
    grow(Index);

  uint32_t AI = Index.toArrayIndex();

  assert(!contains(Index));
  assert(AI < capacity());

  CVUDTNames[AI] = Name;
  TypeRecords[AI] = Data;
  ValidRecords.set(AI);
  ++Count;
}

/// Saves the name in a StringSet and creates a stable StringRef.
StringRef TypeDatabase::saveTypeName(StringRef TypeName) {
  return TypeNameStorage.save(TypeName);
}

StringRef TypeDatabase::getTypeName(TypeIndex Index) const {
  if (Index.isNoneType() || Index.isSimple())
    return TypeIndex::simpleTypeName(Index);

  if (contains(Index))
    return CVUDTNames[Index.toArrayIndex()];

  return "<unknown UDT>";
}

const CVType &TypeDatabase::getTypeRecord(TypeIndex Index) const {
  assert(contains(Index));
  return TypeRecords[Index.toArrayIndex()];
}

CVType &TypeDatabase::getTypeRecord(TypeIndex Index) {
  assert(contains(Index));
  return TypeRecords[Index.toArrayIndex()];
}

bool TypeDatabase::contains(TypeIndex Index) const {
  uint32_t AI = Index.toArrayIndex();
  if (AI >= capacity())
    return false;

  return ValidRecords.test(AI);
}

uint32_t TypeDatabase::size() const { return Count; }

uint32_t TypeDatabase::capacity() const { return TypeRecords.size(); }

CVType TypeDatabase::getType(TypeIndex Index) { return getTypeRecord(Index); }

StringRef TypeDatabase::getTypeName(TypeIndex Index) {
  return static_cast<const TypeDatabase *>(this)->getTypeName(Index);
}

bool TypeDatabase::contains(TypeIndex Index) {
  return static_cast<const TypeDatabase *>(this)->contains(Index);
}

uint32_t TypeDatabase::size() {
  return static_cast<const TypeDatabase *>(this)->size();
}

uint32_t TypeDatabase::capacity() {
  return static_cast<const TypeDatabase *>(this)->capacity();
}

void TypeDatabase::grow() { grow(LargestTypeIndex + 1); }

void TypeDatabase::grow(TypeIndex NewIndex) {
  uint32_t NewSize = NewIndex.toArrayIndex() + 1;

  if (NewSize <= capacity())
    return;

  uint32_t NewCapacity = NewSize * 3 / 2;

  TypeRecords.resize(NewCapacity);
  CVUDTNames.resize(NewCapacity);
  ValidRecords.resize(NewCapacity);
}

bool TypeDatabase::empty() const { return size() == 0; }

Optional<TypeIndex> TypeDatabase::largestTypeIndexLessThan(TypeIndex TI) const {
  uint32_t AI = TI.toArrayIndex();
  int N = ValidRecords.find_prev(AI);
  if (N == -1)
    return None;
  return TypeIndex::fromArrayIndex(N);
}

TypeIndex TypeDatabase::getAppendIndex() const {
  if (empty())
    return TypeIndex::fromArrayIndex(0);

  return LargestTypeIndex + 1;
}

Optional<TypeIndex> TypeDatabase::getFirst() {
  int N = ValidRecords.find_first();
  if (N == -1)
    return None;
  return TypeIndex::fromArrayIndex(N);
}

Optional<TypeIndex> TypeDatabase::getNext(TypeIndex Prev) {
  int N = ValidRecords.find_next(Prev.toArrayIndex());
  if (N == -1)
    return None;
  return TypeIndex::fromArrayIndex(N);
}