xref: /llvm-project/llvm/lib/DebugInfo/PDB/PDBSymbolFunc.cpp (revision b52d08d9dd04e6b9a488530d25c1be5f8d8de012)
1 //===- PDBSymbolFunc.cpp - --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
11 
12 #include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
13 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
14 #include "llvm/DebugInfo/PDB/IPDBSession.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
16 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
18 #include "llvm/DebugInfo/PDB/PDBSymDumper.h"
19 #include "llvm/DebugInfo/PDB/PDBTypes.h"
20 
21 #include <unordered_set>
22 #include <utility>
23 #include <vector>
24 
25 using namespace llvm;
26 
27 namespace {
28 class FunctionArgEnumerator : public IPDBEnumChildren<PDBSymbolData> {
29 public:
30   typedef ConcreteSymbolEnumerator<PDBSymbolData> ArgEnumeratorType;
31 
32   FunctionArgEnumerator(const IPDBSession &PDBSession,
33                         const PDBSymbolFunc &PDBFunc)
34       : Session(PDBSession), Func(PDBFunc) {
35     // Arguments can appear multiple times if they have live range
36     // information, so we only take the first occurrence.
37     std::unordered_set<std::string> SeenNames;
38     auto DataChildren = Func.findAllChildren<PDBSymbolData>();
39     while (auto Child = DataChildren->getNext()) {
40       if (Child->getDataKind() == PDB_DataKind::Param) {
41         std::string Name = Child->getName();
42         if (SeenNames.find(Name) != SeenNames.end())
43           continue;
44         Args.push_back(std::move(Child));
45         SeenNames.insert(Name);
46       }
47     }
48     reset();
49   }
50 
51   uint32_t getChildCount() const { return Args.size(); }
52 
53   std::unique_ptr<PDBSymbolData> getChildAtIndex(uint32_t Index) const {
54     if (Index >= Args.size())
55       return nullptr;
56 
57     return Session.getConcreteSymbolById<PDBSymbolData>(
58         Args[Index]->getSymIndexId());
59   }
60 
61   std::unique_ptr<PDBSymbolData> getNext() {
62     if (CurIter == Args.end())
63       return nullptr;
64     const auto &Result = **CurIter;
65     ++CurIter;
66     return Session.getConcreteSymbolById<PDBSymbolData>(Result.getSymIndexId());
67   }
68 
69   void reset() { CurIter = Args.empty() ? Args.end() : Args.begin(); }
70 
71   FunctionArgEnumerator *clone() const {
72     return new FunctionArgEnumerator(Session, Func);
73   }
74 
75 private:
76   typedef std::vector<std::unique_ptr<PDBSymbolData>> ArgListType;
77   const IPDBSession &Session;
78   const PDBSymbolFunc &Func;
79   ArgListType Args;
80   ArgListType::const_iterator CurIter;
81 };
82 }
83 
84 PDBSymbolFunc::PDBSymbolFunc(const IPDBSession &PDBSession,
85                              std::unique_ptr<IPDBRawSymbol> Symbol)
86     : PDBSymbol(PDBSession, std::move(Symbol)) {}
87 
88 std::unique_ptr<PDBSymbolTypeFunctionSig> PDBSymbolFunc::getSignature() const {
89   return Session.getConcreteSymbolById<PDBSymbolTypeFunctionSig>(getTypeId());
90 }
91 
92 std::unique_ptr<IPDBEnumChildren<PDBSymbolData>>
93 PDBSymbolFunc::getArguments() const {
94   return llvm::make_unique<FunctionArgEnumerator>(Session, *this);
95 }
96 
97 std::unique_ptr<PDBSymbolTypeUDT> PDBSymbolFunc::getClassParent() const {
98   return Session.getConcreteSymbolById<PDBSymbolTypeUDT>(getClassParentId());
99 }
100 
101 void PDBSymbolFunc::dump(PDBSymDumper &Dumper) const { Dumper.dump(*this); }
102