1deb39130SZachary 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"
15deb39130SZachary 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*3226fe95SZachary Turner #include "llvm/DebugInfo/CodeView/DebugSymbolRVASubsection.h"
21deb39130SZachary Turner #include "llvm/DebugInfo/CodeView/DebugSymbolsSubsection.h"
228c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugUnknownSubsection.h"
238c099fe0SZachary Turner #include "llvm/Support/BinaryStreamReader.h"
248c099fe0SZachary Turner #include "llvm/Support/BinaryStreamRef.h"
258c099fe0SZachary Turner 
268c099fe0SZachary Turner using namespace llvm;
278c099fe0SZachary Turner using namespace llvm::codeview;
288c099fe0SZachary Turner 
291bf77620SZachary Turner DebugSubsectionState::DebugSubsectionState() {}
301bf77620SZachary Turner 
311bf77620SZachary Turner DebugSubsectionState::DebugSubsectionState(
321bf77620SZachary Turner     const DebugStringTableSubsectionRef &Strings)
331bf77620SZachary Turner     : Strings(&Strings) {}
341bf77620SZachary Turner 
351bf77620SZachary Turner DebugSubsectionState::DebugSubsectionState(
361bf77620SZachary Turner     const DebugStringTableSubsectionRef &Strings,
371bf77620SZachary Turner     const DebugChecksumsSubsectionRef &Checksums)
381bf77620SZachary Turner     : Strings(&Strings), Checksums(&Checksums) {}
391bf77620SZachary Turner 
401bf77620SZachary Turner void DebugSubsectionState::initializeStrings(const DebugSubsectionRecord &SR) {
411bf77620SZachary Turner   assert(SR.kind() == DebugSubsectionKind::StringTable);
421bf77620SZachary Turner   assert(!Strings && "Found a string table even though we already have one!");
431bf77620SZachary Turner 
441bf77620SZachary Turner   OwnedStrings = llvm::make_unique<DebugStringTableSubsectionRef>();
451bf77620SZachary Turner   consumeError(OwnedStrings->initialize(SR.getRecordData()));
461bf77620SZachary Turner   Strings = OwnedStrings.get();
471bf77620SZachary Turner }
481bf77620SZachary Turner 
491bf77620SZachary Turner void DebugSubsectionState::initializeChecksums(
501bf77620SZachary Turner     const DebugSubsectionRecord &FCR) {
511bf77620SZachary Turner   assert(FCR.kind() == DebugSubsectionKind::FileChecksums);
521bf77620SZachary Turner   if (Checksums)
531bf77620SZachary Turner     return;
541bf77620SZachary Turner 
551bf77620SZachary Turner   OwnedChecksums = llvm::make_unique<DebugChecksumsSubsectionRef>();
561bf77620SZachary Turner   consumeError(OwnedChecksums->initialize(FCR.getRecordData()));
571bf77620SZachary Turner   Checksums = OwnedChecksums.get();
581bf77620SZachary Turner }
591bf77620SZachary Turner 
608c099fe0SZachary Turner Error llvm::codeview::visitDebugSubsection(const DebugSubsectionRecord &R,
611bf77620SZachary Turner                                            DebugSubsectionVisitor &V,
621bf77620SZachary Turner                                            const DebugSubsectionState &State) {
638c099fe0SZachary Turner   BinaryStreamReader Reader(R.getRecordData());
648c099fe0SZachary Turner   switch (R.kind()) {
658c099fe0SZachary Turner   case DebugSubsectionKind::Lines: {
668c099fe0SZachary Turner     DebugLinesSubsectionRef Fragment;
678c099fe0SZachary Turner     if (auto EC = Fragment.initialize(Reader))
688c099fe0SZachary Turner       return EC;
698c099fe0SZachary Turner 
701bf77620SZachary Turner     return V.visitLines(Fragment, State);
718c099fe0SZachary Turner   }
728c099fe0SZachary Turner   case DebugSubsectionKind::FileChecksums: {
738c099fe0SZachary Turner     DebugChecksumsSubsectionRef Fragment;
748c099fe0SZachary Turner     if (auto EC = Fragment.initialize(Reader))
758c099fe0SZachary Turner       return EC;
768c099fe0SZachary Turner 
771bf77620SZachary Turner     return V.visitFileChecksums(Fragment, State);
788c099fe0SZachary Turner   }
798c099fe0SZachary Turner   case DebugSubsectionKind::InlineeLines: {
808c099fe0SZachary Turner     DebugInlineeLinesSubsectionRef Fragment;
818c099fe0SZachary Turner     if (auto EC = Fragment.initialize(Reader))
828c099fe0SZachary Turner       return EC;
831bf77620SZachary Turner     return V.visitInlineeLines(Fragment, State);
848c099fe0SZachary Turner   }
85349c18f8SZachary Turner   case DebugSubsectionKind::CrossScopeExports: {
86349c18f8SZachary Turner     DebugCrossModuleExportsSubsectionRef Section;
87349c18f8SZachary Turner     if (auto EC = Section.initialize(Reader))
88349c18f8SZachary Turner       return EC;
891bf77620SZachary Turner     return V.visitCrossModuleExports(Section, State);
90349c18f8SZachary Turner   }
91349c18f8SZachary Turner   case DebugSubsectionKind::CrossScopeImports: {
92349c18f8SZachary Turner     DebugCrossModuleImportsSubsectionRef Section;
93349c18f8SZachary Turner     if (auto EC = Section.initialize(Reader))
94349c18f8SZachary Turner       return EC;
951bf77620SZachary Turner     return V.visitCrossModuleImports(Section, State);
96349c18f8SZachary Turner   }
97deb39130SZachary Turner   case DebugSubsectionKind::Symbols: {
98deb39130SZachary Turner     DebugSymbolsSubsectionRef Section;
99deb39130SZachary Turner     if (auto EC = Section.initialize(Reader))
100deb39130SZachary Turner       return EC;
101deb39130SZachary Turner     return V.visitSymbols(Section, State);
102deb39130SZachary Turner   }
103deb39130SZachary Turner   case DebugSubsectionKind::StringTable: {
104deb39130SZachary Turner     DebugStringTableSubsectionRef Section;
105deb39130SZachary Turner     if (auto EC = Section.initialize(Reader))
106deb39130SZachary Turner       return EC;
107deb39130SZachary Turner     return V.visitStringTable(Section, State);
108deb39130SZachary Turner   }
109deb39130SZachary Turner   case DebugSubsectionKind::FrameData: {
110deb39130SZachary Turner     DebugFrameDataSubsectionRef Section;
111deb39130SZachary Turner     if (auto EC = Section.initialize(Reader))
112deb39130SZachary Turner       return EC;
113deb39130SZachary Turner     return V.visitFrameData(Section, State);
114deb39130SZachary Turner   }
115*3226fe95SZachary Turner   case DebugSubsectionKind::CoffSymbolRVA: {
116*3226fe95SZachary Turner     DebugSymbolRVASubsectionRef Section;
117*3226fe95SZachary Turner     if (auto EC = Section.initialize(Reader))
118*3226fe95SZachary Turner       return EC;
119*3226fe95SZachary Turner     return V.visitCOFFSymbolRVAs(Section, State);
120*3226fe95SZachary Turner   }
1218c099fe0SZachary Turner   default: {
1228c099fe0SZachary Turner     DebugUnknownSubsectionRef Fragment(R.kind(), R.getRecordData());
1238c099fe0SZachary Turner     return V.visitUnknown(Fragment);
1248c099fe0SZachary Turner   }
1258c099fe0SZachary Turner   }
1268c099fe0SZachary Turner }
127