18c099fe0SZachary Turner //===- DebugSubsectionVisitor.cpp ---------------------------*- C++ -*-===//
28c099fe0SZachary Turner //
38c099fe0SZachary Turner //                     The LLVM Compiler Infrastructure
48c099fe0SZachary Turner //
58c099fe0SZachary Turner // This file is distributed under the University of Illinois Open Source
68c099fe0SZachary Turner // License. See LICENSE.TXT for details.
78c099fe0SZachary Turner //
88c099fe0SZachary Turner //===----------------------------------------------------------------------===//
98c099fe0SZachary Turner 
108c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h"
118c099fe0SZachary Turner 
128c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
13349c18f8SZachary Turner #include "llvm/DebugInfo/CodeView/DebugCrossExSubsection.h"
14349c18f8SZachary Turner #include "llvm/DebugInfo/CodeView/DebugCrossImpSubsection.h"
158c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
168c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
17*1bf77620SZachary Turner #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
188c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
198c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
208c099fe0SZachary Turner #include "llvm/Support/BinaryStreamReader.h"
218c099fe0SZachary Turner #include "llvm/Support/BinaryStreamRef.h"
228c099fe0SZachary Turner 
238c099fe0SZachary Turner using namespace llvm;
248c099fe0SZachary Turner using namespace llvm::codeview;
258c099fe0SZachary Turner 
26*1bf77620SZachary Turner DebugSubsectionState::DebugSubsectionState() {}
27*1bf77620SZachary Turner 
28*1bf77620SZachary Turner DebugSubsectionState::DebugSubsectionState(
29*1bf77620SZachary Turner     const DebugStringTableSubsectionRef &Strings)
30*1bf77620SZachary Turner     : Strings(&Strings) {}
31*1bf77620SZachary Turner 
32*1bf77620SZachary Turner DebugSubsectionState::DebugSubsectionState(
33*1bf77620SZachary Turner     const DebugStringTableSubsectionRef &Strings,
34*1bf77620SZachary Turner     const DebugChecksumsSubsectionRef &Checksums)
35*1bf77620SZachary Turner     : Strings(&Strings), Checksums(&Checksums) {}
36*1bf77620SZachary Turner 
37*1bf77620SZachary Turner void DebugSubsectionState::initializeStrings(const DebugSubsectionRecord &SR) {
38*1bf77620SZachary Turner   assert(SR.kind() == DebugSubsectionKind::StringTable);
39*1bf77620SZachary Turner   assert(!Strings && "Found a string table even though we already have one!");
40*1bf77620SZachary Turner 
41*1bf77620SZachary Turner   OwnedStrings = llvm::make_unique<DebugStringTableSubsectionRef>();
42*1bf77620SZachary Turner   consumeError(OwnedStrings->initialize(SR.getRecordData()));
43*1bf77620SZachary Turner   Strings = OwnedStrings.get();
44*1bf77620SZachary Turner }
45*1bf77620SZachary Turner 
46*1bf77620SZachary Turner void DebugSubsectionState::initializeChecksums(
47*1bf77620SZachary Turner     const DebugSubsectionRecord &FCR) {
48*1bf77620SZachary Turner   assert(FCR.kind() == DebugSubsectionKind::FileChecksums);
49*1bf77620SZachary Turner   if (Checksums)
50*1bf77620SZachary Turner     return;
51*1bf77620SZachary Turner 
52*1bf77620SZachary Turner   OwnedChecksums = llvm::make_unique<DebugChecksumsSubsectionRef>();
53*1bf77620SZachary Turner   consumeError(OwnedChecksums->initialize(FCR.getRecordData()));
54*1bf77620SZachary Turner   Checksums = OwnedChecksums.get();
55*1bf77620SZachary Turner }
56*1bf77620SZachary Turner 
578c099fe0SZachary Turner Error llvm::codeview::visitDebugSubsection(const DebugSubsectionRecord &R,
58*1bf77620SZachary Turner                                            DebugSubsectionVisitor &V,
59*1bf77620SZachary Turner                                            const DebugSubsectionState &State) {
608c099fe0SZachary Turner   BinaryStreamReader Reader(R.getRecordData());
618c099fe0SZachary Turner   switch (R.kind()) {
628c099fe0SZachary Turner   case DebugSubsectionKind::Lines: {
638c099fe0SZachary Turner     DebugLinesSubsectionRef Fragment;
648c099fe0SZachary Turner     if (auto EC = Fragment.initialize(Reader))
658c099fe0SZachary Turner       return EC;
668c099fe0SZachary Turner 
67*1bf77620SZachary Turner     return V.visitLines(Fragment, State);
688c099fe0SZachary Turner   }
698c099fe0SZachary Turner   case DebugSubsectionKind::FileChecksums: {
708c099fe0SZachary Turner     DebugChecksumsSubsectionRef Fragment;
718c099fe0SZachary Turner     if (auto EC = Fragment.initialize(Reader))
728c099fe0SZachary Turner       return EC;
738c099fe0SZachary Turner 
74*1bf77620SZachary Turner     return V.visitFileChecksums(Fragment, State);
758c099fe0SZachary Turner   }
768c099fe0SZachary Turner   case DebugSubsectionKind::InlineeLines: {
778c099fe0SZachary Turner     DebugInlineeLinesSubsectionRef Fragment;
788c099fe0SZachary Turner     if (auto EC = Fragment.initialize(Reader))
798c099fe0SZachary Turner       return EC;
80*1bf77620SZachary Turner     return V.visitInlineeLines(Fragment, State);
818c099fe0SZachary Turner   }
82349c18f8SZachary Turner   case DebugSubsectionKind::CrossScopeExports: {
83349c18f8SZachary Turner     DebugCrossModuleExportsSubsectionRef Section;
84349c18f8SZachary Turner     if (auto EC = Section.initialize(Reader))
85349c18f8SZachary Turner       return EC;
86*1bf77620SZachary Turner     return V.visitCrossModuleExports(Section, State);
87349c18f8SZachary Turner   }
88349c18f8SZachary Turner   case DebugSubsectionKind::CrossScopeImports: {
89349c18f8SZachary Turner     DebugCrossModuleImportsSubsectionRef Section;
90349c18f8SZachary Turner     if (auto EC = Section.initialize(Reader))
91349c18f8SZachary Turner       return EC;
92*1bf77620SZachary Turner     return V.visitCrossModuleImports(Section, State);
93349c18f8SZachary Turner   }
948c099fe0SZachary Turner   default: {
958c099fe0SZachary Turner     DebugUnknownSubsectionRef Fragment(R.kind(), R.getRecordData());
968c099fe0SZachary Turner     return V.visitUnknown(Fragment);
978c099fe0SZachary Turner   }
988c099fe0SZachary Turner   }
998c099fe0SZachary Turner }
100