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/DebugInfo/MSF/ByteStream.h"
20 #include "llvm/DebugInfo/MSF/StreamReader.h"
21 #include "llvm/Support/Error.h"
29 class TypeDeserializer : public TypeVisitorCallbacks {
31 explicit MappingInfo(ArrayRef<uint8_t> RecordData)
32 : Stream(RecordData), Reader(Stream), Mapping(Reader) {}
34 msf::ByteStream Stream;
35 msf::StreamReader Reader;
36 TypeRecordMapping Mapping;
40 TypeDeserializer() = default;
42 Error visitTypeBegin(CVType &Record) override {
43 assert(!Mapping && "Already in a type mapping!");
44 Mapping = llvm::make_unique<MappingInfo>(Record.content());
45 return Mapping->Mapping.visitTypeBegin(Record);
48 Error visitTypeEnd(CVType &Record) override {
49 assert(Mapping && "Not in a type mapping!");
50 auto EC = Mapping->Mapping.visitTypeEnd(Record);
55 #define TYPE_RECORD(EnumName, EnumVal, Name) \
56 Error visitKnownRecord(CVType &CVR, Name##Record &Record) override { \
57 return visitKnownRecordImpl<Name##Record>(CVR, Record); \
59 #define MEMBER_RECORD(EnumName, EnumVal, Name)
60 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
61 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
62 #include "TypeRecords.def"
65 template <typename RecordType>
66 Error visitKnownRecordImpl(CVType &CVR, RecordType &Record) {
67 return Mapping->Mapping.visitKnownRecord(CVR, Record);
70 std::unique_ptr<MappingInfo> Mapping;
73 class FieldListDeserializer : public TypeVisitorCallbacks {
75 explicit MappingInfo(msf::StreamReader &R)
76 : Reader(R), Mapping(Reader), StartOffset(0) {}
78 msf::StreamReader &Reader;
79 TypeRecordMapping Mapping;
84 explicit FieldListDeserializer(msf::StreamReader &Reader) : Mapping(Reader) {
86 FieldList.Type = TypeLeafKind::LF_FIELDLIST;
87 consumeError(Mapping.Mapping.visitTypeBegin(FieldList));
90 ~FieldListDeserializer() override {
92 FieldList.Type = TypeLeafKind::LF_FIELDLIST;
93 consumeError(Mapping.Mapping.visitTypeEnd(FieldList));
96 Error visitMemberBegin(CVMemberRecord &Record) override {
97 Mapping.StartOffset = Mapping.Reader.getOffset();
98 return Mapping.Mapping.visitMemberBegin(Record);
101 Error visitMemberEnd(CVMemberRecord &Record) override {
102 if (auto EC = Mapping.Mapping.visitMemberEnd(Record))
104 return Error::success();
107 #define TYPE_RECORD(EnumName, EnumVal, Name)
108 #define MEMBER_RECORD(EnumName, EnumVal, Name) \
109 Error visitKnownMember(CVMemberRecord &CVR, Name##Record &Record) override { \
110 return visitKnownMemberImpl<Name##Record>(CVR, Record); \
112 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
113 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
114 #include "TypeRecords.def"
117 template <typename RecordType>
118 Error visitKnownMemberImpl(CVMemberRecord &CVR, RecordType &Record) {
119 if (auto EC = Mapping.Mapping.visitKnownMember(CVR, Record))
122 uint32_t EndOffset = Mapping.Reader.getOffset();
123 uint32_t RecordLength = EndOffset - Mapping.StartOffset;
124 Mapping.Reader.setOffset(Mapping.StartOffset);
125 if (auto EC = Mapping.Reader.readBytes(CVR.Data, RecordLength))
127 assert(Mapping.Reader.getOffset() == EndOffset);
128 return Error::success();
133 } // end namespace codeview
134 } // end namespace llvm
136 #endif // LLVM_DEBUGINFO_CODEVIEW_TYPEDESERIALIZER_H