[llvm-pdbdump] Rewrite dumper using visitor pattern.
[oota-llvm.git] / tools / llvm-pdbdump / TypedefDumper.cpp
1 //===- TypedefDumper.cpp - PDBSymDumper impl for typedefs -------- * 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 "TypedefDumper.h"
11
12 #include "FunctionDumper.h"
13 #include "llvm-pdbdump.h"
14
15 #include "llvm/DebugInfo/PDB/IPDBSession.h"
16 #include "llvm/DebugInfo/PDB/PDBExtras.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
23
24 using namespace llvm;
25
26 TypedefDumper::TypedefDumper() : PDBSymDumper(true) {}
27
28 void TypedefDumper::start(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
29                           int Indent) {
30   OS << "typedef:" << Symbol.getName() << " -> ";
31   uint32_t TargetId = Symbol.getTypeId();
32   if (auto TypeSymbol = Symbol.getSession().getSymbolById(TargetId))
33     TypeSymbol->dump(OS, 0, *this);
34 }
35
36 void TypedefDumper::dump(const PDBSymbolTypeArray &Symbol, raw_ostream &OS,
37                          int Indent) {}
38
39 void TypedefDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
40                          int Indent) {
41   PDB_BuiltinType Type = Symbol.getBuiltinType();
42   OS << Type;
43   if (Type == PDB_BuiltinType::UInt || Type == PDB_BuiltinType::Int)
44     OS << (8 * Symbol.getLength()) << "_t";
45 }
46
47 void TypedefDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
48                          int Indent) {
49   OS << "enum " << Symbol.getName();
50 }
51
52 void TypedefDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
53                          int Indent) {
54   if (Symbol.isConstType())
55     OS << "const ";
56   if (Symbol.isVolatileType())
57     OS << "volatile ";
58   uint32_t PointeeId = Symbol.getTypeId();
59   auto PointeeType = Symbol.getSession().getSymbolById(PointeeId);
60   if (!PointeeType)
61     return;
62   if (auto FuncSig = dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
63     FunctionDumper::PointerType Pointer = FunctionDumper::PointerType::Pointer;
64     if (Symbol.isReference())
65       Pointer = FunctionDumper::PointerType::Reference;
66     FunctionDumper NestedDumper;
67     NestedDumper.start(*FuncSig, Pointer, OS);
68     OS.flush();
69   } else {
70     PointeeType->dump(OS, Indent, *this);
71     OS << ((Symbol.isReference()) ? "&" : "*");
72   }
73 }
74
75 void TypedefDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
76                          raw_ostream &OS, int Indent) {
77   FunctionDumper Dumper;
78   Dumper.start(Symbol, FunctionDumper::PointerType::None, OS);
79 }
80
81 void TypedefDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
82                          int Indent) {
83   OS << "class " << Symbol.getName();
84 }