1*deb39130SZachary 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"
15*deb39130SZachary Turner #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
168c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
178c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
181bf77620SZachary Turner #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
198c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
20*deb39130SZachary Turner #include "llvm/DebugInfo/CodeView/DebugSymbolsSubsection.h"
218c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
228c099fe0SZachary Turner #include "llvm/Support/BinaryStreamReader.h"
238c099fe0SZachary Turner #include "llvm/Support/BinaryStreamRef.h"
248c099fe0SZachary Turner 
258c099fe0SZachary Turner using namespace llvm;
268c099fe0SZachary Turner using namespace llvm::codeview;
278c099fe0SZachary Turner 
281bf77620SZachary Turner DebugSubsectionState::DebugSubsectionState() {}
291bf77620SZachary Turner 
301bf77620SZachary Turner DebugSubsectionState::DebugSubsectionState(
311bf77620SZachary Turner     const DebugStringTableSubsectionRef &Strings)
321bf77620SZachary Turner     : Strings(&Strings) {}
331bf77620SZachary Turner 
341bf77620SZachary Turner DebugSubsectionState::DebugSubsectionState(
351bf77620SZachary Turner     const DebugStringTableSubsectionRef &Strings,
361bf77620SZachary Turner     const DebugChecksumsSubsectionRef &Checksums)
371bf77620SZachary Turner     : Strings(&Strings), Checksums(&Checksums) {}
381bf77620SZachary Turner 
391bf77620SZachary Turner void DebugSubsectionState::initializeStrings(const DebugSubsectionRecord &SR) {
401bf77620SZachary Turner   assert(SR.kind() == DebugSubsectionKind::StringTable);
411bf77620SZachary Turner   assert(!Strings && "Found a string table even though we already have one!");
421bf77620SZachary Turner 
431bf77620SZachary Turner   OwnedStrings = llvm::make_unique<DebugStringTableSubsectionRef>();
441bf77620SZachary Turner   consumeError(OwnedStrings->initialize(SR.getRecordData()));
451bf77620SZachary Turner   Strings = OwnedStrings.get();
461bf77620SZachary Turner }
471bf77620SZachary Turner 
481bf77620SZachary Turner void DebugSubsectionState::initializeChecksums(
491bf77620SZachary Turner     const DebugSubsectionRecord &FCR) {
501bf77620SZachary Turner   assert(FCR.kind() == DebugSubsectionKind::FileChecksums);
511bf77620SZachary Turner   if (Checksums)
521bf77620SZachary Turner     return;
531bf77620SZachary Turner 
541bf77620SZachary Turner   OwnedChecksums = llvm::make_unique<DebugChecksumsSubsectionRef>();
551bf77620SZachary Turner   consumeError(OwnedChecksums->initialize(FCR.getRecordData()));
561bf77620SZachary Turner   Checksums = OwnedChecksums.get();
571bf77620SZachary Turner }
581bf77620SZachary Turner 
598c099fe0SZachary Turner Error llvm::codeview::visitDebugSubsection(const DebugSubsectionRecord &R,
601bf77620SZachary Turner                                            DebugSubsectionVisitor &V,
611bf77620SZachary Turner                                            const DebugSubsectionState &State) {
628c099fe0SZachary Turner   BinaryStreamReader Reader(R.getRecordData());
638c099fe0SZachary Turner   switch (R.kind()) {
648c099fe0SZachary Turner   case DebugSubsectionKind::Lines: {
658c099fe0SZachary Turner     DebugLinesSubsectionRef Fragment;
668c099fe0SZachary Turner     if (auto EC = Fragment.initialize(Reader))
678c099fe0SZachary Turner       return EC;
688c099fe0SZachary Turner 
691bf77620SZachary Turner     return V.visitLines(Fragment, State);
708c099fe0SZachary Turner   }
718c099fe0SZachary Turner   case DebugSubsectionKind::FileChecksums: {
728c099fe0SZachary Turner     DebugChecksumsSubsectionRef Fragment;
738c099fe0SZachary Turner     if (auto EC = Fragment.initialize(Reader))
748c099fe0SZachary Turner       return EC;
758c099fe0SZachary Turner 
761bf77620SZachary Turner     return V.visitFileChecksums(Fragment, State);
778c099fe0SZachary Turner   }
788c099fe0SZachary Turner   case DebugSubsectionKind::InlineeLines: {
798c099fe0SZachary Turner     DebugInlineeLinesSubsectionRef Fragment;
808c099fe0SZachary Turner     if (auto EC = Fragment.initialize(Reader))
818c099fe0SZachary Turner       return EC;
821bf77620SZachary Turner     return V.visitInlineeLines(Fragment, State);
838c099fe0SZachary Turner   }
84349c18f8SZachary Turner   case DebugSubsectionKind::CrossScopeExports: {
85349c18f8SZachary Turner     DebugCrossModuleExportsSubsectionRef Section;
86349c18f8SZachary Turner     if (auto EC = Section.initialize(Reader))
87349c18f8SZachary Turner       return EC;
881bf77620SZachary Turner     return V.visitCrossModuleExports(Section, State);
89349c18f8SZachary Turner   }
90349c18f8SZachary Turner   case DebugSubsectionKind::CrossScopeImports: {
91349c18f8SZachary Turner     DebugCrossModuleImportsSubsectionRef Section;
92349c18f8SZachary Turner     if (auto EC = Section.initialize(Reader))
93349c18f8SZachary Turner       return EC;
941bf77620SZachary Turner     return V.visitCrossModuleImports(Section, State);
95349c18f8SZachary Turner   }
96*deb39130SZachary Turner   case DebugSubsectionKind::Symbols: {
97*deb39130SZachary Turner     DebugSymbolsSubsectionRef Section;
98*deb39130SZachary Turner     if (auto EC = Section.initialize(Reader))
99*deb39130SZachary Turner       return EC;
100*deb39130SZachary Turner     return V.visitSymbols(Section, State);
101*deb39130SZachary Turner   }
102*deb39130SZachary Turner   case DebugSubsectionKind::StringTable: {
103*deb39130SZachary Turner     DebugStringTableSubsectionRef Section;
104*deb39130SZachary Turner     if (auto EC = Section.initialize(Reader))
105*deb39130SZachary Turner       return EC;
106*deb39130SZachary Turner     return V.visitStringTable(Section, State);
107*deb39130SZachary Turner   }
108*deb39130SZachary Turner   case DebugSubsectionKind::FrameData: {
109*deb39130SZachary Turner     DebugFrameDataSubsectionRef Section;
110*deb39130SZachary Turner     if (auto EC = Section.initialize(Reader))
111*deb39130SZachary Turner       return EC;
112*deb39130SZachary Turner     return V.visitFrameData(Section, State);
113*deb39130SZachary Turner   }
1148c099fe0SZachary Turner   default: {
1158c099fe0SZachary Turner     DebugUnknownSubsectionRef Fragment(R.kind(), R.getRecordData());
1168c099fe0SZachary Turner     return V.visitUnknown(Fragment);
1178c099fe0SZachary Turner   }
1188c099fe0SZachary Turner   }
1198c099fe0SZachary Turner }
120