1//==- NativeEnumGlobals.cpp - Native Global Enumerator impl ------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/PDB/Native/NativeEnumGlobals.h"
10
11#include "llvm/DebugInfo/CodeView/CVRecord.h"
12#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
13#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
14#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
15#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
16#include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
17#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
18#include "llvm/DebugInfo/PDB/PDBSymbol.h"
19#include "llvm/DebugInfo/PDB/PDBTypes.h"
20
21using namespace llvm;
22using namespace llvm::codeview;
23using namespace llvm::pdb;
24
25NativeEnumGlobals::NativeEnumGlobals(NativeSession &PDBSession,
26                                     std::vector<codeview::SymbolKind> Kinds)
27    : Index(0), Session(PDBSession) {
28  GlobalsStream &GS = cantFail(Session.getPDBFile().getPDBGlobalsStream());
29  SymbolStream &SS = cantFail(Session.getPDBFile().getPDBSymbolStream());
30  for (uint32_t Off : GS.getGlobalsTable()) {
31    CVSymbol S = SS.readRecord(Off);
32    if (!llvm::is_contained(Kinds, S.kind()))
33      continue;
34    MatchOffsets.push_back(Off);
35  }
36}
37
38uint32_t NativeEnumGlobals::getChildCount() const {
39  return static_cast<uint32_t>(MatchOffsets.size());
40}
41
42std::unique_ptr<PDBSymbol>
43NativeEnumGlobals::getChildAtIndex(uint32_t N) const {
44  if (N >= MatchOffsets.size())
45    return nullptr;
46
47  SymIndexId Id =
48      Session.getSymbolCache().getOrCreateGlobalSymbolByOffset(MatchOffsets[N]);
49  return Session.getSymbolCache().getSymbolById(Id);
50}
51
52std::unique_ptr<PDBSymbol> NativeEnumGlobals::getNext() {
53  return getChildAtIndex(Index++);
54}
55
56void NativeEnumGlobals::reset() { Index = 0; }
57