GlobalTypeTableBuilder.h revision 363496
1//===- GlobalTypeTableBuilder.h ----------------------------------*- 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_CODEVIEW_GLOBALTYPETABLEBUILDER_H 10#define LLVM_DEBUGINFO_CODEVIEW_GLOBALTYPETABLEBUILDER_H 11 12#include "llvm/ADT/ArrayRef.h" 13#include "llvm/ADT/DenseSet.h" 14#include "llvm/ADT/SmallVector.h" 15#include "llvm/DebugInfo/CodeView/CodeView.h" 16#include "llvm/DebugInfo/CodeView/SimpleTypeSerializer.h" 17#include "llvm/DebugInfo/CodeView/TypeCollection.h" 18#include "llvm/DebugInfo/CodeView/TypeHashing.h" 19#include "llvm/DebugInfo/CodeView/TypeIndex.h" 20#include "llvm/Support/Allocator.h" 21#include <cassert> 22#include <cstdint> 23#include <memory> 24#include <vector> 25 26namespace llvm { 27namespace codeview { 28 29class ContinuationRecordBuilder; 30 31class GlobalTypeTableBuilder : public TypeCollection { 32 /// Storage for records. These need to outlive the TypeTableBuilder. 33 BumpPtrAllocator &RecordStorage; 34 35 /// A serializer that can write non-continuation leaf types. Only used as 36 /// a convenience function so that we can provide an interface method to 37 /// write an unserialized record. 38 SimpleTypeSerializer SimpleSerializer; 39 40 /// Hash table. 41 DenseMap<GloballyHashedType, TypeIndex> HashedRecords; 42 43 /// Contains a list of all records indexed by TypeIndex.toArrayIndex(). 44 SmallVector<ArrayRef<uint8_t>, 2> SeenRecords; 45 46 /// Contains a list of all hash values indexed by TypeIndex.toArrayIndex(). 47 SmallVector<GloballyHashedType, 2> SeenHashes; 48 49public: 50 explicit GlobalTypeTableBuilder(BumpPtrAllocator &Storage); 51 ~GlobalTypeTableBuilder(); 52 53 // TypeTableCollection overrides 54 Optional<TypeIndex> getFirst() override; 55 Optional<TypeIndex> getNext(TypeIndex Prev) override; 56 CVType getType(TypeIndex Index) override; 57 StringRef getTypeName(TypeIndex Index) override; 58 bool contains(TypeIndex Index) override; 59 uint32_t size() override; 60 uint32_t capacity() override; 61 62 // public interface 63 void reset(); 64 TypeIndex nextTypeIndex() const; 65 66 BumpPtrAllocator &getAllocator() { return RecordStorage; } 67 68 ArrayRef<ArrayRef<uint8_t>> records() const; 69 ArrayRef<GloballyHashedType> hashes() const; 70 71 template <typename CreateFunc> 72 TypeIndex insertRecordAs(GloballyHashedType Hash, size_t RecordSize, 73 CreateFunc Create) { 74 assert(RecordSize < UINT32_MAX && "Record too big"); 75 assert(RecordSize % 4 == 0 && 76 "RecordSize is not a multiple of 4 bytes which will cause " 77 "misalignment in the output TPI stream!"); 78 79 auto Result = HashedRecords.try_emplace(Hash, nextTypeIndex()); 80 81 if (LLVM_UNLIKELY(Result.second /*inserted*/ || 82 Result.first->second.isSimple())) { 83 uint8_t *Stable = RecordStorage.Allocate<uint8_t>(RecordSize); 84 MutableArrayRef<uint8_t> Data(Stable, RecordSize); 85 ArrayRef<uint8_t> StableRecord = Create(Data); 86 if (StableRecord.empty()) { 87 // Records with forward references into the Type stream will be deferred 88 // for insertion at a later time, on the second pass. 89 Result.first->getSecond() = TypeIndex(SimpleTypeKind::NotTranslated); 90 return TypeIndex(SimpleTypeKind::NotTranslated); 91 } 92 if (Result.first->second.isSimple()) { 93 assert(Result.first->second.getIndex() == 94 (uint32_t)SimpleTypeKind::NotTranslated); 95 // On the second pass, update with index to remapped record. The 96 // (initially misbehaved) record will now come *after* other records 97 // resolved in the first pass, with proper *back* references in the 98 // stream. 99 Result.first->second = nextTypeIndex(); 100 } 101 SeenRecords.push_back(StableRecord); 102 SeenHashes.push_back(Hash); 103 } 104 105 return Result.first->second; 106 } 107 108 TypeIndex insertRecordBytes(ArrayRef<uint8_t> Data); 109 TypeIndex insertRecord(ContinuationRecordBuilder &Builder); 110 111 template <typename T> TypeIndex writeLeafType(T &Record) { 112 ArrayRef<uint8_t> Data = SimpleSerializer.serialize(Record); 113 return insertRecordBytes(Data); 114 } 115}; 116 117} // end namespace codeview 118} // end namespace llvm 119 120#endif // LLVM_DEBUGINFO_CODEVIEW_MERGINGTYPETABLEBUILDER_H 121