LLVM  10.0.0svn
SymbolCache.h
Go to the documentation of this file.
1 //==- SymbolCache.h - Cache of native symbols and ids ------------*- C++ -*-==//
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 #ifndef LLVM_DEBUGINFO_PDB_NATIVE_SYMBOLCACHE_H
10 #define LLVM_DEBUGINFO_PDB_NATIVE_SYMBOLCACHE_H
11 
12 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/Support/Allocator.h"
18 
19 #include <memory>
20 #include <vector>
21 
22 namespace llvm {
23 namespace pdb {
24 class DbiStream;
25 class PDBFile;
26 
27 class SymbolCache {
28  NativeSession &Session;
29  DbiStream *Dbi = nullptr;
30 
31  /// Cache of all stable symbols, indexed by SymIndexId. Just because a
32  /// symbol has been parsed does not imply that it will be stable and have
33  /// an Id. Id allocation is an implementation, with the only guarantee
34  /// being that once an Id is allocated, the symbol can be assumed to be
35  /// cached.
36  std::vector<std::unique_ptr<NativeRawSymbol>> Cache;
37 
38  /// For type records from the TPI stream which have been paresd and cached,
39  /// stores a mapping to SymIndexId of the cached symbol.
41 
42  /// For field list members which have been parsed and cached, stores a mapping
43  /// from (IndexOfClass, MemberIndex) to the corresponding SymIndexId of the
44  /// cached symbol.
46  FieldListMembersToSymbolId;
47 
48  /// List of SymIndexIds for each compiland, indexed by compiland index as they
49  /// appear in the PDB file.
50  std::vector<SymIndexId> Compilands;
51 
52  /// Map from global symbol offset to SymIndexId.
53  DenseMap<uint32_t, SymIndexId> GlobalOffsetToSymbolId;
54 
55  SymIndexId createSymbolPlaceholder() {
56  SymIndexId Id = Cache.size();
57  Cache.push_back(nullptr);
58  return Id;
59  }
60 
61  template <typename ConcreteSymbolT, typename CVRecordT, typename... Args>
62  SymIndexId createSymbolForType(codeview::TypeIndex TI, codeview::CVType CVT,
63  Args &&... ConstructorArgs) {
64  CVRecordT Record;
65  if (auto EC =
66  codeview::TypeDeserializer::deserializeAs<CVRecordT>(CVT, Record)) {
67  consumeError(std::move(EC));
68  return 0;
69  }
70 
71  return createSymbol<ConcreteSymbolT>(
72  TI, std::move(Record), std::forward<Args>(ConstructorArgs)...);
73  }
74 
75  SymIndexId createSymbolForModifiedType(codeview::TypeIndex ModifierTI,
76  codeview::CVType CVT);
77 
78  SymIndexId createSimpleType(codeview::TypeIndex TI,
80 
81 public:
82  SymbolCache(NativeSession &Session, DbiStream *Dbi);
83 
84  template <typename ConcreteSymbolT, typename... Args>
85  SymIndexId createSymbol(Args &&... ConstructorArgs) {
86  SymIndexId Id = Cache.size();
87 
88  // Initial construction must not access the cache, since it must be done
89  // atomically.
90  auto Result = std::make_unique<ConcreteSymbolT>(
91  Session, Id, std::forward<Args>(ConstructorArgs)...);
92  Result->SymbolId = Id;
93 
94  NativeRawSymbol *NRS = static_cast<NativeRawSymbol *>(Result.get());
95  Cache.push_back(std::move(Result));
96 
97  // After the item is in the cache, we can do further initialization which
98  // is then allowed to access the cache.
99  NRS->initialize();
100  return Id;
101  }
102 
103  std::unique_ptr<IPDBEnumSymbols>
105 
106  std::unique_ptr<IPDBEnumSymbols>
107  createTypeEnumerator(std::vector<codeview::TypeLeafKind> Kinds);
108 
109  std::unique_ptr<IPDBEnumSymbols>
111 
113 
114  template <typename ConcreteSymbolT, typename... Args>
116  uint32_t Index,
117  Args &&... ConstructorArgs) {
118  SymIndexId SymId = Cache.size();
119  std::pair<codeview::TypeIndex, uint32_t> Key{FieldListTI, Index};
120  auto Result = FieldListMembersToSymbolId.try_emplace(Key, SymId);
121  if (Result.second)
122  SymId =
123  createSymbol<ConcreteSymbolT>(std::forward<Args>(ConstructorArgs)...);
124  else
125  SymId = Result.first->second;
126  return SymId;
127  }
128 
130 
131  std::unique_ptr<PDBSymbolCompiland> getOrCreateCompiland(uint32_t Index);
132  uint32_t getNumCompilands() const;
133 
134  std::unique_ptr<PDBSymbol> getSymbolById(SymIndexId SymbolId) const;
135 
137 
138  template <typename ConcreteT>
139  ConcreteT &getNativeSymbolById(SymIndexId SymbolId) const {
140  return static_cast<ConcreteT &>(getNativeSymbolById(SymbolId));
141  }
142 };
143 
144 } // namespace pdb
145 } // namespace llvm
146 
147 #endif
std::unique_ptr< IPDBEnumSymbols > createGlobalsEnumerator(codeview::SymbolKind Kind)
Definition: SymbolCache.cpp:91
This class represents lattice values for constants.
Definition: AllocatorList.h:23
TypeLeafKind
Duplicate copy of the above enum, but using the official CV names.
Definition: CodeView.h:33
std::unique_ptr< IPDBEnumSymbols > createTypeEnumerator(codeview::TypeLeafKind Kind)
Definition: SymbolCache.cpp:74
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
std::unique_ptr< PDBSymbol > getSymbolById(SymIndexId SymbolId) const
Key
PAL metadata keys.
std::unique_ptr< PDBSymbolCompiland > getOrCreateCompiland(uint32_t Index)
SymbolCache(NativeSession &Session, DbiStream *Dbi)
Definition: SymbolCache.cpp:64
ModifierOptions
Equivalent to CV_modifier_t.
Definition: CodeView.h:299
A 32-bit type reference.
Definition: TypeIndex.h:95
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1001
SymIndexId getOrCreateGlobalSymbolByOffset(uint32_t Offset)
SymIndexId createSymbol(Args &&... ConstructorArgs)
Definition: SymbolCache.h:85
NativeRawSymbol & getNativeSymbolById(SymIndexId SymbolId) const
ConcreteT & getNativeSymbolById(SymIndexId SymbolId) const
Definition: SymbolCache.h:139
SymIndexId findSymbolByTypeIndex(codeview::TypeIndex TI)
uint32_t getNumCompilands() const
SymbolKind
Duplicate copy of the above enum, but using the official CV names.
Definition: CodeView.h:47
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
SymIndexId getOrCreateFieldListMember(codeview::TypeIndex FieldListTI, uint32_t Index, Args &&... ConstructorArgs)
Definition: SymbolCache.h:115