1 //===- TypeDeserializer.h ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_DEBUGINFO_CODEVIEW_TYPEDESERIALIZER_H
11 #define LLVM_DEBUGINFO_CODEVIEW_TYPEDESERIALIZER_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/DebugInfo/CodeView/CodeView.h"
16 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
17 #include "llvm/DebugInfo/CodeView/TypeRecordMapping.h"
18 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
19 #include "llvm/Support/BinaryByteStream.h"
20 #include "llvm/Support/BinaryStreamReader.h"
21 #include "llvm/Support/Error.h"
29 class TypeDeserializer : public TypeVisitorCallbacks {
31 explicit MappingInfo(ArrayRef<uint8_t> RecordData)
32 : Stream(RecordData, llvm::support::little), Reader(Stream),
35 BinaryByteStream Stream;
36 BinaryStreamReader Reader;
37 TypeRecordMapping Mapping;
41 TypeDeserializer() = default;
43 Error visitTypeBegin(CVType &Record) override {
44 assert(!Mapping && "Already in a type mapping!");
45 Mapping = llvm::make_unique<MappingInfo>(Record.content());
46 return Mapping->Mapping.visitTypeBegin(Record);
49 Error visitTypeBegin(CVType &Record, TypeIndex Index) override {
50 return visitTypeBegin(Record);
53 Error visitTypeEnd(CVType &Record) override {
54 assert(Mapping && "Not in a type mapping!");
55 auto EC = Mapping->Mapping.visitTypeEnd(Record);
60 #define TYPE_RECORD(EnumName, EnumVal, Name) \
61 Error visitKnownRecord(CVType &CVR, Name##Record &Record) override { \
62 return visitKnownRecordImpl<Name##Record>(CVR, Record); \
64 #define MEMBER_RECORD(EnumName, EnumVal, Name)
65 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
66 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
67 #include "TypeRecords.def"
70 template <typename RecordType>
71 Error visitKnownRecordImpl(CVType &CVR, RecordType &Record) {
72 return Mapping->Mapping.visitKnownRecord(CVR, Record);
75 std::unique_ptr<MappingInfo> Mapping;
78 class FieldListDeserializer : public TypeVisitorCallbacks {
80 explicit MappingInfo(BinaryStreamReader &R)
81 : Reader(R), Mapping(Reader), StartOffset(0) {}
83 BinaryStreamReader &Reader;
84 TypeRecordMapping Mapping;
89 explicit FieldListDeserializer(BinaryStreamReader &Reader) : Mapping(Reader) {
91 FieldList.Type = TypeLeafKind::LF_FIELDLIST;
92 consumeError(Mapping.Mapping.visitTypeBegin(FieldList));
95 ~FieldListDeserializer() override {
97 FieldList.Type = TypeLeafKind::LF_FIELDLIST;
98 consumeError(Mapping.Mapping.visitTypeEnd(FieldList));
101 Error visitMemberBegin(CVMemberRecord &Record) override {
102 Mapping.StartOffset = Mapping.Reader.getOffset();
103 return Mapping.Mapping.visitMemberBegin(Record);
106 Error visitMemberEnd(CVMemberRecord &Record) override {
107 if (auto EC = Mapping.Mapping.visitMemberEnd(Record))
109 return Error::success();
112 #define TYPE_RECORD(EnumName, EnumVal, Name)
113 #define MEMBER_RECORD(EnumName, EnumVal, Name) \
114 Error visitKnownMember(CVMemberRecord &CVR, Name##Record &Record) override { \
115 return visitKnownMemberImpl<Name##Record>(CVR, Record); \
117 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
118 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
119 #include "TypeRecords.def"
122 template <typename RecordType>
123 Error visitKnownMemberImpl(CVMemberRecord &CVR, RecordType &Record) {
124 if (auto EC = Mapping.Mapping.visitKnownMember(CVR, Record))
127 uint32_t EndOffset = Mapping.Reader.getOffset();
128 uint32_t RecordLength = EndOffset - Mapping.StartOffset;
129 Mapping.Reader.setOffset(Mapping.StartOffset);
130 if (auto EC = Mapping.Reader.readBytes(CVR.Data, RecordLength))
132 assert(Mapping.Reader.getOffset() == EndOffset);
133 return Error::success();
138 } // end namespace codeview
139 } // end namespace llvm
141 #endif // LLVM_DEBUGINFO_CODEVIEW_TYPEDESERIALIZER_H