#ifndef LLVM_DEBUGINFO_CODEVIEW_TYPEHASHING_H
#define LLVM_DEBUGINFO_CODEVIEW_TYPEHASHING_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/DebugInfo/CodeView/CVRecord.h"
#include "llvm/DebugInfo/CodeView/TypeCollection.h"
#include "llvm/DebugInfo/CodeView/TypeIndex.h"
#include "llvm/Support/FormatProviders.h"
#include <type_traits>
namespace llvm {
class raw_ostream;
namespace codeview {
struct LocallyHashedType {
hash_code Hash;
ArrayRef<uint8_t> RecordData;
static LocallyHashedType hashType(ArrayRef<uint8_t> RecordData);
template <typename Range>
static std::vector<LocallyHashedType> hashTypes(Range &&Records) {
std::vector<LocallyHashedType> Hashes;
Hashes.reserve(std::distance(std::begin(Records), std::end(Records)));
for (const auto &R : Records)
Hashes.push_back(hashType(R));
return Hashes;
}
static std::vector<LocallyHashedType>
hashTypeCollection(TypeCollection &Types) {
std::vector<LocallyHashedType> Hashes;
Types.ForEachRecord([&Hashes](TypeIndex TI, const CVType &Type) {
Hashes.push_back(hashType(Type.RecordData));
});
return Hashes;
}
};
enum class GlobalTypeHashAlg : uint16_t {
SHA1 = 0, SHA1_8 };
struct GloballyHashedType {
GloballyHashedType() = default;
GloballyHashedType(StringRef H)
: GloballyHashedType(ArrayRef<uint8_t>(H.bytes_begin(), H.bytes_end())) {}
GloballyHashedType(ArrayRef<uint8_t> H) {
assert(H.size() == 8);
::memcpy(Hash.data(), H.data(), 8);
}
std::array<uint8_t, 8> Hash;
bool empty() const { return *(const uint64_t*)Hash.data() == 0; }
friend inline bool operator==(const GloballyHashedType &L,
const GloballyHashedType &R) {
return L.Hash == R.Hash;
}
friend inline bool operator!=(const GloballyHashedType &L,
const GloballyHashedType &R) {
return !(L.Hash == R.Hash);
}
static GloballyHashedType hashType(ArrayRef<uint8_t> RecordData,
ArrayRef<GloballyHashedType> PreviousTypes,
ArrayRef<GloballyHashedType> PreviousIds);
static GloballyHashedType hashType(CVType Type,
ArrayRef<GloballyHashedType> PreviousTypes,
ArrayRef<GloballyHashedType> PreviousIds) {
return hashType(Type.RecordData, PreviousTypes, PreviousIds);
}
template <typename Range>
static std::vector<GloballyHashedType> hashTypes(Range &&Records) {
std::vector<GloballyHashedType> Hashes;
bool UnresolvedRecords = false;
for (const auto &R : Records) {
GloballyHashedType H = hashType(R, Hashes, Hashes);
if (H.empty())
UnresolvedRecords = true;
Hashes.push_back(H);
}
while (UnresolvedRecords) {
UnresolvedRecords = false;
auto HashIt = Hashes.begin();
for (const auto &R : Records) {
if (HashIt->empty()) {
GloballyHashedType H = hashType(R, Hashes, Hashes);
if (H.empty())
UnresolvedRecords = true;
else
*HashIt = H;
}
++HashIt;
}
}
return Hashes;
}
template <typename Range>
static std::vector<GloballyHashedType>
hashIds(Range &&Records, ArrayRef<GloballyHashedType> TypeHashes) {
std::vector<GloballyHashedType> IdHashes;
for (const auto &R : Records)
IdHashes.push_back(hashType(R, TypeHashes, IdHashes));
return IdHashes;
}
static std::vector<GloballyHashedType>
hashTypeCollection(TypeCollection &Types) {
std::vector<GloballyHashedType> Hashes;
Types.ForEachRecord([&Hashes](TypeIndex TI, const CVType &Type) {
Hashes.push_back(hashType(Type.RecordData, Hashes, Hashes));
});
return Hashes;
}
};
static_assert(std::is_trivially_copyable<GloballyHashedType>::value,
"GloballyHashedType must be trivially copyable so that we can "
"reinterpret_cast arrays of hash data to arrays of "
"GloballyHashedType");
}
template <> struct DenseMapInfo<codeview::LocallyHashedType> {
static codeview::LocallyHashedType Empty;
static codeview::LocallyHashedType Tombstone;
static codeview::LocallyHashedType getEmptyKey() { return Empty; }
static codeview::LocallyHashedType getTombstoneKey() { return Tombstone; }
static unsigned getHashValue(codeview::LocallyHashedType Val) {
return Val.Hash;
}
static bool isEqual(codeview::LocallyHashedType LHS,
codeview::LocallyHashedType RHS) {
if (LHS.Hash != RHS.Hash)
return false;
return LHS.RecordData == RHS.RecordData;
}
};
template <> struct DenseMapInfo<codeview::GloballyHashedType> {
static codeview::GloballyHashedType Empty;
static codeview::GloballyHashedType Tombstone;
static codeview::GloballyHashedType getEmptyKey() { return Empty; }
static codeview::GloballyHashedType getTombstoneKey() { return Tombstone; }
static unsigned getHashValue(codeview::GloballyHashedType Val) {
return *reinterpret_cast<const unsigned *>(Val.Hash.data());
}
static bool isEqual(codeview::GloballyHashedType LHS,
codeview::GloballyHashedType RHS) {
return LHS == RHS;
}
};
template <> struct format_provider<codeview::LocallyHashedType> {
public:
static void format(const codeview::LocallyHashedType &V,
llvm::raw_ostream &Stream, StringRef Style) {
write_hex(Stream, V.Hash, HexPrintStyle::Upper, 8);
}
};
template <> struct format_provider<codeview::GloballyHashedType> {
public:
static void format(const codeview::GloballyHashedType &V,
llvm::raw_ostream &Stream, StringRef Style) {
for (uint8_t B : V.Hash) {
write_hex(Stream, B, HexPrintStyle::Upper, 2);
}
}
};
}
#endif