1ff0cc061SDimitry Andric //===- DIADataStream.cpp - DIA implementation of IPDBDataStream -*- C++ -*-===//
2ff0cc061SDimitry Andric //
3ff0cc061SDimitry Andric //                     The LLVM Compiler Infrastructure
4ff0cc061SDimitry Andric //
5ff0cc061SDimitry Andric // This file is distributed under the University of Illinois Open Source
6ff0cc061SDimitry Andric // License. See LICENSE.TXT for details.
7ff0cc061SDimitry Andric //
8ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
9ff0cc061SDimitry Andric 
10ff0cc061SDimitry Andric #include "llvm/DebugInfo/PDB/DIA/DIADataStream.h"
11*b5893f02SDimitry Andric #include "llvm/DebugInfo/PDB/DIA/DIAUtils.h"
12ff0cc061SDimitry Andric 
13ff0cc061SDimitry Andric using namespace llvm;
143ca95b02SDimitry Andric using namespace llvm::pdb;
15ff0cc061SDimitry Andric 
DIADataStream(CComPtr<IDiaEnumDebugStreamData> DiaStreamData)16ff0cc061SDimitry Andric DIADataStream::DIADataStream(CComPtr<IDiaEnumDebugStreamData> DiaStreamData)
17ff0cc061SDimitry Andric     : StreamData(DiaStreamData) {}
18ff0cc061SDimitry Andric 
getRecordCount() const19ff0cc061SDimitry Andric uint32_t DIADataStream::getRecordCount() const {
20ff0cc061SDimitry Andric   LONG Count = 0;
21ff0cc061SDimitry Andric   return (S_OK == StreamData->get_Count(&Count)) ? Count : 0;
22ff0cc061SDimitry Andric }
23ff0cc061SDimitry Andric 
getName() const24ff0cc061SDimitry Andric std::string DIADataStream::getName() const {
25*b5893f02SDimitry Andric   return invokeBstrMethod(*StreamData, &IDiaEnumDebugStreamData::get_name);
26ff0cc061SDimitry Andric }
27ff0cc061SDimitry Andric 
28ff0cc061SDimitry Andric llvm::Optional<DIADataStream::RecordType>
getItemAtIndex(uint32_t Index) const29ff0cc061SDimitry Andric DIADataStream::getItemAtIndex(uint32_t Index) const {
30ff0cc061SDimitry Andric   RecordType Record;
31ff0cc061SDimitry Andric   DWORD RecordSize = 0;
32ff0cc061SDimitry Andric   StreamData->Item(Index, 0, &RecordSize, nullptr);
33ff0cc061SDimitry Andric   if (RecordSize == 0)
34ff0cc061SDimitry Andric     return llvm::Optional<RecordType>();
35ff0cc061SDimitry Andric 
36ff0cc061SDimitry Andric   Record.resize(RecordSize);
37ff0cc061SDimitry Andric   if (S_OK != StreamData->Item(Index, RecordSize, &RecordSize, &Record[0]))
38ff0cc061SDimitry Andric     return llvm::Optional<RecordType>();
39ff0cc061SDimitry Andric   return Record;
40ff0cc061SDimitry Andric }
41ff0cc061SDimitry Andric 
getNext(RecordType & Record)42ff0cc061SDimitry Andric bool DIADataStream::getNext(RecordType &Record) {
43ff0cc061SDimitry Andric   Record.clear();
44ff0cc061SDimitry Andric   DWORD RecordSize = 0;
45ff0cc061SDimitry Andric   ULONG CountFetched = 0;
46ff0cc061SDimitry Andric   StreamData->Next(1, 0, &RecordSize, nullptr, &CountFetched);
47ff0cc061SDimitry Andric   if (RecordSize == 0)
48ff0cc061SDimitry Andric     return false;
49ff0cc061SDimitry Andric 
50ff0cc061SDimitry Andric   Record.resize(RecordSize);
51ff0cc061SDimitry Andric   if (S_OK ==
52ff0cc061SDimitry Andric       StreamData->Next(1, RecordSize, &RecordSize, &Record[0], &CountFetched))
53ff0cc061SDimitry Andric     return false;
54ff0cc061SDimitry Andric   return true;
55ff0cc061SDimitry Andric }
56ff0cc061SDimitry Andric 
reset()57ff0cc061SDimitry Andric void DIADataStream::reset() { StreamData->Reset(); }
58