//===- TypeSerializer.h -----------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef LLVM_DEBUGINFO_CODEVIEW_TYPESERIALIZER_H #define LLVM_DEBUGINFO_CODEVIEW_TYPESERIALIZER_H #include "llvm/DebugInfo/CodeView/TypeRecordMapping.h" #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h" #include "llvm/Support/BinaryByteStream.h" #include "llvm/Support/BinaryStreamWriter.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Error.h" namespace llvm { namespace codeview { class TypeHasher; class TypeSerializer : public TypeVisitorCallbacks { struct SubRecord { SubRecord(TypeLeafKind K, uint32_t S) : Kind(K), Size(S) {} TypeLeafKind Kind; uint32_t Size = 0; }; struct RecordSegment { SmallVector SubRecords; uint32_t length() const { uint32_t L = sizeof(RecordPrefix); for (const auto &R : SubRecords) { L += R.Size; } return L; } }; typedef SmallVector, 2> MutableRecordList; static constexpr uint8_t ContinuationLength = 8; BumpPtrAllocator &RecordStorage; RecordSegment CurrentSegment; MutableRecordList FieldListSegments; Optional TypeKind; Optional MemberKind; std::vector RecordBuffer; MutableBinaryByteStream Stream; BinaryStreamWriter Writer; TypeRecordMapping Mapping; /// Private type record hashing implementation details are handled here. std::unique_ptr Hasher; /// Contains a list of all records indexed by TypeIndex.toArrayIndex(). SmallVector, 2> SeenRecords; /// Temporary storage that we use to copy a record's data while re-writing /// its type indices. SmallVector RemapStorage; TypeIndex nextTypeIndex() const; bool isInFieldList() const; MutableArrayRef getCurrentSubRecordData(); MutableArrayRef getCurrentRecordData(); Error writeRecordPrefix(TypeLeafKind Kind); Expected> addPadding(MutableArrayRef Record); public: explicit TypeSerializer(BumpPtrAllocator &Storage, bool Hash = true); ~TypeSerializer(); void reset(); BumpPtrAllocator &getAllocator() { return RecordStorage; } ArrayRef> records() const; TypeIndex insertRecordBytes(ArrayRef &Record); TypeIndex insertRecord(const RemappedType &Record); Expected visitTypeEndGetIndex(CVType &Record); Error visitTypeBegin(CVType &Record) override; Error visitTypeEnd(CVType &Record) override; Error visitMemberBegin(CVMemberRecord &Record) override; Error visitMemberEnd(CVMemberRecord &Record) override; #define TYPE_RECORD(EnumName, EnumVal, Name) \ virtual Error visitKnownRecord(CVType &CVR, Name##Record &Record) override { \ return visitKnownRecordImpl(CVR, Record); \ } #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) #define MEMBER_RECORD(EnumName, EnumVal, Name) \ Error visitKnownMember(CVMemberRecord &CVR, Name##Record &Record) override { \ return visitKnownMemberImpl(CVR, Record); \ } #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) #include "llvm/DebugInfo/CodeView/CodeViewTypes.def" private: template Error visitKnownRecordImpl(CVType &CVR, RecordKind &Record) { return Mapping.visitKnownRecord(CVR, Record); } template Error visitKnownMemberImpl(CVMemberRecord &CVR, RecordType &Record) { assert(CVR.Kind == static_cast(Record.getKind())); if (auto EC = Writer.writeEnum(CVR.Kind)) return EC; if (auto EC = Mapping.visitKnownMember(CVR, Record)) return EC; // Get all the data that was just written and is yet to be committed to // the current segment. Then pad it to 4 bytes. MutableArrayRef ThisRecord = getCurrentSubRecordData(); auto ExpectedRecord = addPadding(ThisRecord); if (!ExpectedRecord) return ExpectedRecord.takeError(); ThisRecord = *ExpectedRecord; CurrentSegment.SubRecords.emplace_back(CVR.Kind, ThisRecord.size()); CVR.Data = ThisRecord; // Both the last subrecord and the total length of this segment should be // multiples of 4. assert(ThisRecord.size() % 4 == 0); assert(CurrentSegment.length() % 4 == 0); return Error::success(); } }; } } #endif