llvm-pdbdump: Improve printing of functions and signatures.
[oota-llvm.git] / lib / DebugInfo / PDB / PDBSymbolTypeFunctionSig.cpp
1 //===- PDBSymbolTypeFunctionSig.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/ConcreteSymbolEnumerator.h"
11 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
12 #include "llvm/DebugInfo/PDB/IPDBSession.h"
13 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
14 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
16 #include <utility>
17
18 using namespace llvm;
19
20 namespace {
21 class FunctionArgEnumerator : public IPDBEnumSymbols {
22 public:
23   typedef ConcreteSymbolEnumerator<PDBSymbolTypeFunctionArg> ArgEnumeratorType;
24
25   FunctionArgEnumerator(const IPDBSession &PDBSession,
26                         const PDBSymbolTypeFunctionSig &Sig)
27       : Session(PDBSession),
28         Enumerator(Sig.findAllChildren<PDBSymbolTypeFunctionArg>()) {}
29
30   FunctionArgEnumerator(const IPDBSession &PDBSession,
31                         std::unique_ptr<ArgEnumeratorType> ArgEnumerator)
32       : Session(PDBSession), Enumerator(std::move(ArgEnumerator)) {}
33
34   uint32_t getChildCount() const { return Enumerator->getChildCount(); }
35
36   std::unique_ptr<PDBSymbol> getChildAtIndex(uint32_t Index) const {
37     auto FunctionArgSymbol = Enumerator->getChildAtIndex(Index);
38     if (!FunctionArgSymbol)
39       return nullptr;
40     return Session.getSymbolById(FunctionArgSymbol->getTypeId());
41   }
42
43   std::unique_ptr<PDBSymbol> getNext() {
44     auto FunctionArgSymbol = Enumerator->getNext();
45     if (!FunctionArgSymbol)
46       return nullptr;
47     return Session.getSymbolById(FunctionArgSymbol->getTypeId());
48   }
49
50   void reset() { Enumerator->reset(); }
51
52   MyType *clone() const {
53     std::unique_ptr<ArgEnumeratorType> Clone(Enumerator->clone());
54     return new FunctionArgEnumerator(Session, std::move(Clone));
55   }
56
57 private:
58   const IPDBSession &Session;
59   std::unique_ptr<ArgEnumeratorType> Enumerator;
60 };
61 }
62
63 PDBSymbolTypeFunctionSig::PDBSymbolTypeFunctionSig(
64     const IPDBSession &PDBSession, std::unique_ptr<IPDBRawSymbol> Symbol)
65     : PDBSymbol(PDBSession, std::move(Symbol)) {}
66
67 std::unique_ptr<PDBSymbol> PDBSymbolTypeFunctionSig::getReturnType() const {
68   return Session.getSymbolById(getTypeId());
69 }
70
71 std::unique_ptr<IPDBEnumSymbols>
72 PDBSymbolTypeFunctionSig::getArguments() const {
73   return llvm::make_unique<FunctionArgEnumerator>(Session, *this);
74 }
75
76 std::unique_ptr<PDBSymbol> PDBSymbolTypeFunctionSig::getClassParent() const {
77   uint32_t ClassId = getClassParentId();
78   if (ClassId == 0)
79     return nullptr;
80   return Session.getSymbolById(ClassId);
81 }
82
83 void PDBSymbolTypeFunctionSig::dumpArgList(raw_ostream &OS) const {
84   OS << "(";
85   if (auto ChildEnum = getArguments()) {
86     uint32_t Index = 0;
87     while (auto Arg = ChildEnum->getNext()) {
88       Arg->dump(OS, 0, PDB_DumpLevel::Compact);
89       if (++Index < ChildEnum->getChildCount())
90         OS << ", ";
91     }
92   }
93   OS << ")";
94   if (isConstType())
95     OS << " const";
96   if (isVolatileType())
97     OS << " volatile";
98 }
99
100 void PDBSymbolTypeFunctionSig::dump(raw_ostream &OS, int Indent,
101                                     PDB_DumpLevel Level) const {
102   OS << stream_indent(Indent);
103
104   if (auto ReturnType = getReturnType()) {
105     ReturnType->dump(OS, 0, PDB_DumpLevel::Compact);
106     OS << " ";
107   }
108
109   OS << getCallingConvention() << " ";
110   if (auto ClassParent = getClassParent()) {
111     OS << "(";
112     ClassParent->dump(OS, 0, PDB_DumpLevel::Compact);
113     OS << "::*)";
114   }
115
116   dumpArgList(OS);
117 }