LLVM  5.0.0
TypeName.cpp
Go to the documentation of this file.
1 //===- TypeName.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 
11 
12 #include "llvm/ADT/SmallString.h"
16 
17 using namespace llvm;
18 using namespace llvm::codeview;
19 
20 namespace {
21 class TypeNameComputer : public TypeVisitorCallbacks {
22  /// The type collection. Used to calculate names of nested types.
23  TypeCollection &Types;
24  TypeIndex CurrentTypeIndex = TypeIndex::None();
25 
26  /// Name of the current type. Only valid before visitTypeEnd.
28 
29 public:
30  explicit TypeNameComputer(TypeCollection &Types) : Types(Types) {}
31 
32  StringRef name() const { return Name; }
33 
34  /// Paired begin/end actions for all types. Receives all record data,
35  /// including the fixed-length record prefix.
36  Error visitTypeBegin(CVType &Record) override;
37  Error visitTypeBegin(CVType &Record, TypeIndex Index) override;
38  Error visitTypeEnd(CVType &Record) override;
39 
40 #define TYPE_RECORD(EnumName, EnumVal, Name) \
41  Error visitKnownRecord(CVType &CVR, Name##Record &Record) override;
42 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
43 #define MEMBER_RECORD(EnumName, EnumVal, Name)
44 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
45 };
46 } // namespace
47 
48 Error TypeNameComputer::visitTypeBegin(CVType &Record) {
49  llvm_unreachable("Must call visitTypeBegin with a TypeIndex!");
50  return Error::success();
51 }
52 
53 Error TypeNameComputer::visitTypeBegin(CVType &Record, TypeIndex Index) {
54  // Reset Name to the empty string. If the visitor sets it, we know it.
55  Name = "";
56  CurrentTypeIndex = Index;
57  return Error::success();
58 }
59 
60 Error TypeNameComputer::visitTypeEnd(CVType &CVR) { return Error::success(); }
61 
63  FieldListRecord &FieldList) {
64  Name = "<field list>";
65  return Error::success();
66 }
67 
70  Name = String.getString();
71  return Error::success();
72 }
73 
75  auto Indices = Args.getIndices();
76  uint32_t Size = Indices.size();
77  Name = "(";
78  for (uint32_t I = 0; I < Size; ++I) {
79  assert(Indices[I] < CurrentTypeIndex);
80 
81  Name.append(Types.getTypeName(Indices[I]));
82  if (I + 1 != Size)
83  Name.append(", ");
84  }
85  Name.push_back(')');
86  return Error::success();
87 }
88 
90  StringListRecord &Strings) {
91  auto Indices = Strings.getIndices();
92  uint32_t Size = Indices.size();
93  Name = "\"";
94  for (uint32_t I = 0; I < Size; ++I) {
95  Name.append(Types.getTypeName(Indices[I]));
96  if (I + 1 != Size)
97  Name.append("\" \"");
98  }
99  Name.push_back('\"');
100  return Error::success();
101 }
102 
104  Name = Class.getName();
105  return Error::success();
106 }
107 
109  Name = Union.getName();
110  return Error::success();
111 }
112 
114  Name = Enum.getName();
115  return Error::success();
116 }
117 
119  Name = AT.getName();
120  return Error::success();
121 }
122 
124  Name = VFT.getName();
125  return Error::success();
126 }
127 
129  Name = Id.getName();
130  return Error::success();
131 }
132 
134  StringRef Ret = Types.getTypeName(Proc.getReturnType());
135  StringRef Params = Types.getTypeName(Proc.getArgumentList());
136  Name = formatv("{0} {1}", Ret, Params).sstr<256>();
137  return Error::success();
138 }
139 
141  MemberFunctionRecord &MF) {
142  StringRef Ret = Types.getTypeName(MF.getReturnType());
143  StringRef Class = Types.getTypeName(MF.getClassType());
144  StringRef Params = Types.getTypeName(MF.getArgumentList());
145  Name = formatv("{0} {1}::{2}", Ret, Class, Params).sstr<256>();
146  return Error::success();
147 }
148 
150  Name = Func.getName();
151  return Error::success();
152 }
153 
155  Name = TS.getName();
156  return Error::success();
157 }
158 
160 
161  if (Ptr.isPointerToMember()) {
162  const MemberPointerInfo &MI = Ptr.getMemberInfo();
163 
164  StringRef Pointee = Types.getTypeName(Ptr.getReferentType());
165  StringRef Class = Types.getTypeName(MI.getContainingType());
166  Name = formatv("{0} {1}::*", Pointee, Class);
167  } else {
168  if (Ptr.isConst())
169  Name.append("const ");
170  if (Ptr.isVolatile())
171  Name.append("volatile ");
172  if (Ptr.isUnaligned())
173  Name.append("__unaligned ");
174 
175  Name.append(Types.getTypeName(Ptr.getReferentType()));
176 
178  Name.append("&");
179  else if (Ptr.getMode() == PointerMode::RValueReference)
180  Name.append("&&");
181  else if (Ptr.getMode() == PointerMode::Pointer)
182  Name.append("*");
183  }
184  return Error::success();
185 }
186 
188  uint16_t Mods = static_cast<uint16_t>(Mod.getModifiers());
189 
191  if (Mods & uint16_t(ModifierOptions::Const))
192  Name.append("const ");
193  if (Mods & uint16_t(ModifierOptions::Volatile))
194  Name.append("volatile ");
195  if (Mods & uint16_t(ModifierOptions::Unaligned))
196  Name.append("__unaligned ");
197  Name.append(Types.getTypeName(Mod.getModifiedType()));
198  return Error::success();
199 }
200 
202  VFTableShapeRecord &Shape) {
203  Name = formatv("<vftable {0} methods>", Shape.getEntryCount());
204  return Error::success();
205 }
206 
208  CVType &CVR, UdtModSourceLineRecord &ModSourceLine) {
209  return Error::success();
210 }
211 
213  UdtSourceLineRecord &SourceLine) {
214  return Error::success();
215 }
216 
218  return Error::success();
219 }
220 
222  MethodOverloadListRecord &Overloads) {
223  return Error::success();
224 }
225 
227  return Error::success();
228 }
229 
231  return Error::success();
232 }
233 
235  TypeIndex Index) {
236  TypeNameComputer Computer(Types);
237  CVType Record = Types.getType(Index);
238  if (auto EC = visitTypeRecord(Record, Index, Computer)) {
239  consumeError(std::move(EC));
240  return "<unknown UDT>";
241  }
242  return Computer.name();
243 }
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
TypeIndex getArgumentList() const
Definition: TypeRecord.h:162
virtual CVType getType(TypeIndex Index)=0
ArrayRef< TypeIndex > getIndices() const
Definition: TypeRecord.h:259
std::string computeTypeName(TypeCollection &Types, TypeIndex Index)
Definition: TypeName.cpp:234
StringRef getString() const
Definition: TypeRecord.h:567
bool isPointerToMember() const
Definition: TypeRecord.h:317
StringRef getName() const
Definition: TypeRecord.h:388
TypeIndex getContainingType() const
Definition: TypeRecord.h:110
static Error visitKnownRecord(CVSymbol &Record, SymbolVisitorCallbacks &Callbacks)
A 32-bit type reference.
Definition: TypeIndex.h:96
TypeIndex getReturnType() const
Definition: TypeRecord.h:158
const char * Name
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
StringRef getName() const
Definition: TypeRecord.h:661
Error visitTypeRecord(CVType &Record, TypeIndex Index, TypeVisitorCallbacks &Callbacks, VisitorDataSource Source=VDS_BytesPresent)
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:593
ModifierOptions getModifiers() const
Definition: TypeRecord.h:140
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
PointerMode getMode() const
Definition: TypeRecord.h:302
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
TypeIndex getModifiedType() const
Definition: TypeRecord.h:139
ArrayRef< TypeIndex > getIndices() const
Definition: TypeRecord.h:245
TypeIndex getArgumentList() const
Definition: TypeRecord.h:193
StringRef getName() const
Definition: TypeRecord.h:418
#define I(x, y, z)
Definition: MD5.cpp:58
MemberPointerInfo getMemberInfo() const
Definition: TypeRecord.h:315
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
int * Ptr
static TypeIndex None()
Definition: TypeIndex.h:135
TypeIndex getReferentType() const
Definition: TypeRecord.h:295
StringRef getName() const
Definition: TypeRecord.h:584