1 //===- NativeTypeEnum.cpp - info about enum type ----------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/Native/NativeTypeEnum.h"
11 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
12 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
13 #include "llvm/DebugInfo/PDB/Native/NativeEnumTypes.h"
14 #include "llvm/DebugInfo/PDB/Native/NativeSymbolEnumerator.h"
15 #include "llvm/DebugInfo/PDB/Native/NativeTypeBuiltin.h"
16 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
17 #include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
18 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
21 #include "llvm/Support/FormatVariadic.h"
26 using namespace llvm::codeview;
27 using namespace llvm::pdb;
30 // Yea, this is a pretty terrible class name. But if we have an enum:
37 // then A and B are the "enumerators" of the "enum" Foo. And we need
39 class NativeEnumEnumEnumerators : public IPDBEnumSymbols, TypeVisitorCallbacks {
41 NativeEnumEnumEnumerators(NativeSession &Session,
42 const NativeTypeEnum &ClassParent);
44 uint32_t getChildCount() const override;
45 std::unique_ptr<PDBSymbol> getChildAtIndex(uint32_t Index) const override;
46 std::unique_ptr<PDBSymbol> getNext() override;
47 void reset() override;
50 Error visitKnownMember(CVMemberRecord &CVM,
51 EnumeratorRecord &Record) override;
52 Error visitKnownMember(CVMemberRecord &CVM,
53 ListContinuationRecord &Record) override;
55 NativeSession &Session;
56 const NativeTypeEnum &ClassParent;
57 std::vector<EnumeratorRecord> Enumerators;
58 Optional<TypeIndex> ContinuationIndex;
63 NativeEnumEnumEnumerators::NativeEnumEnumEnumerators(
64 NativeSession &Session, const NativeTypeEnum &ClassParent)
65 : Session(Session), ClassParent(ClassParent) {
66 TpiStream &Tpi = cantFail(Session.getPDBFile().getPDBTpiStream());
67 LazyRandomTypeCollection &Types = Tpi.typeCollection();
69 ContinuationIndex = ClassParent.getEnumRecord().FieldList;
70 while (ContinuationIndex) {
71 CVType FieldList = Types.getType(*ContinuationIndex);
72 assert(FieldList.kind() == LF_FIELDLIST);
73 ContinuationIndex.reset();
74 cantFail(visitMemberRecordStream(FieldList.data(), *this));
78 Error NativeEnumEnumEnumerators::visitKnownMember(CVMemberRecord &CVM,
79 EnumeratorRecord &Record) {
80 Enumerators.push_back(Record);
81 return Error::success();
84 Error NativeEnumEnumEnumerators::visitKnownMember(
85 CVMemberRecord &CVM, ListContinuationRecord &Record) {
86 ContinuationIndex = Record.ContinuationIndex;
87 return Error::success();
90 uint32_t NativeEnumEnumEnumerators::getChildCount() const {
91 return Enumerators.size();
94 std::unique_ptr<PDBSymbol>
95 NativeEnumEnumEnumerators::getChildAtIndex(uint32_t Index) const {
96 if (Index >= getChildCount())
99 SymIndexId Id = Session.getSymbolCache()
100 .getOrCreateFieldListMember<NativeSymbolEnumerator>(
101 ClassParent.getEnumRecord().FieldList, Index,
102 ClassParent, Enumerators[Index]);
103 return Session.getSymbolCache().getSymbolById(Id);
106 std::unique_ptr<PDBSymbol> NativeEnumEnumEnumerators::getNext() {
107 if (Index >= getChildCount())
110 return getChildAtIndex(Index++);
113 void NativeEnumEnumEnumerators::reset() { Index = 0; }
115 NativeTypeEnum::NativeTypeEnum(NativeSession &Session, SymIndexId Id,
116 TypeIndex Index, EnumRecord Record)
117 : NativeRawSymbol(Session, PDB_SymType::Enum, Id), Index(Index),
118 Record(std::move(Record)) {}
120 NativeTypeEnum::NativeTypeEnum(NativeSession &Session, SymIndexId Id,
121 NativeTypeEnum &UnmodifiedType,
122 codeview::ModifierRecord Modifier)
123 : NativeRawSymbol(Session, PDB_SymType::Enum, Id),
124 UnmodifiedType(&UnmodifiedType), Modifiers(std::move(Modifier)) {}
126 NativeTypeEnum::~NativeTypeEnum() {}
128 void NativeTypeEnum::dump(raw_ostream &OS, int Indent,
129 PdbSymbolIdField ShowIdFields,
130 PdbSymbolIdField RecurseIdFields) const {
131 NativeRawSymbol::dump(OS, Indent, ShowIdFields, RecurseIdFields);
133 dumpSymbolField(OS, "baseType", static_cast<uint32_t>(getBuiltinType()),
135 dumpSymbolIdField(OS, "lexicalParentId", 0, Indent, Session,
136 PdbSymbolIdField::LexicalParent, ShowIdFields,
138 dumpSymbolField(OS, "name", getName(), Indent);
139 dumpSymbolIdField(OS, "typeId", getTypeId(), Indent, Session,
140 PdbSymbolIdField::Type, ShowIdFields, RecurseIdFields);
141 if (Modifiers.hasValue())
142 dumpSymbolIdField(OS, "unmodifiedTypeId", getUnmodifiedTypeId(), Indent,
143 Session, PdbSymbolIdField::UnmodifiedType, ShowIdFields,
145 dumpSymbolField(OS, "length", getLength(), Indent);
146 dumpSymbolField(OS, "constructor", hasConstructor(), Indent);
147 dumpSymbolField(OS, "constType", isConstType(), Indent);
148 dumpSymbolField(OS, "hasAssignmentOperator", hasAssignmentOperator(), Indent);
149 dumpSymbolField(OS, "hasCastOperator", hasCastOperator(), Indent);
150 dumpSymbolField(OS, "hasNestedTypes", hasNestedTypes(), Indent);
151 dumpSymbolField(OS, "overloadedOperator", hasOverloadedOperator(), Indent);
152 dumpSymbolField(OS, "isInterfaceUdt", isInterfaceUdt(), Indent);
153 dumpSymbolField(OS, "intrinsic", isIntrinsic(), Indent);
154 dumpSymbolField(OS, "nested", isNested(), Indent);
155 dumpSymbolField(OS, "packed", isPacked(), Indent);
156 dumpSymbolField(OS, "isRefUdt", isRefUdt(), Indent);
157 dumpSymbolField(OS, "scoped", isScoped(), Indent);
158 dumpSymbolField(OS, "unalignedType", isUnalignedType(), Indent);
159 dumpSymbolField(OS, "isValueUdt", isValueUdt(), Indent);
160 dumpSymbolField(OS, "volatileType", isVolatileType(), Indent);
163 std::unique_ptr<IPDBEnumSymbols>
164 NativeTypeEnum::findChildren(PDB_SymType Type) const {
165 if (Type != PDB_SymType::Data)
166 return llvm::make_unique<NullEnumerator<PDBSymbol>>();
168 const NativeTypeEnum *ClassParent = nullptr;
172 ClassParent = UnmodifiedType;
173 return llvm::make_unique<NativeEnumEnumEnumerators>(Session, *ClassParent);
176 PDB_SymType NativeTypeEnum::getSymTag() const { return PDB_SymType::Enum; }
178 PDB_BuiltinType NativeTypeEnum::getBuiltinType() const {
180 return UnmodifiedType->getBuiltinType();
182 Session.getSymbolCache().findSymbolByTypeIndex(Record->getUnderlyingType());
184 codeview::TypeIndex Underlying = Record->getUnderlyingType();
186 // This indicates a corrupt record.
187 if (!Underlying.isSimple() ||
188 Underlying.getSimpleMode() != SimpleTypeMode::Direct) {
189 return PDB_BuiltinType::None;
192 switch (Underlying.getSimpleKind()) {
193 case SimpleTypeKind::Boolean128:
194 case SimpleTypeKind::Boolean64:
195 case SimpleTypeKind::Boolean32:
196 case SimpleTypeKind::Boolean16:
197 case SimpleTypeKind::Boolean8:
198 return PDB_BuiltinType::Bool;
199 case SimpleTypeKind::NarrowCharacter:
200 case SimpleTypeKind::UnsignedCharacter:
201 case SimpleTypeKind::SignedCharacter:
202 return PDB_BuiltinType::Char;
203 case SimpleTypeKind::WideCharacter:
204 return PDB_BuiltinType::WCharT;
205 case SimpleTypeKind::Character16:
206 return PDB_BuiltinType::Char16;
207 case SimpleTypeKind::Character32:
208 return PDB_BuiltinType::Char32;
209 case SimpleTypeKind::Int128:
210 case SimpleTypeKind::Int128Oct:
211 case SimpleTypeKind::Int16:
212 case SimpleTypeKind::Int16Short:
213 case SimpleTypeKind::Int32:
214 case SimpleTypeKind::Int32Long:
215 case SimpleTypeKind::Int64:
216 case SimpleTypeKind::Int64Quad:
217 return PDB_BuiltinType::Int;
218 case SimpleTypeKind::UInt128:
219 case SimpleTypeKind::UInt128Oct:
220 case SimpleTypeKind::UInt16:
221 case SimpleTypeKind::UInt16Short:
222 case SimpleTypeKind::UInt32:
223 case SimpleTypeKind::UInt32Long:
224 case SimpleTypeKind::UInt64:
225 case SimpleTypeKind::UInt64Quad:
226 return PDB_BuiltinType::UInt;
227 case SimpleTypeKind::HResult:
228 return PDB_BuiltinType::HResult;
229 case SimpleTypeKind::Complex16:
230 case SimpleTypeKind::Complex32:
231 case SimpleTypeKind::Complex32PartialPrecision:
232 case SimpleTypeKind::Complex64:
233 case SimpleTypeKind::Complex80:
234 case SimpleTypeKind::Complex128:
235 return PDB_BuiltinType::Complex;
236 case SimpleTypeKind::Float16:
237 case SimpleTypeKind::Float32:
238 case SimpleTypeKind::Float32PartialPrecision:
239 case SimpleTypeKind::Float48:
240 case SimpleTypeKind::Float64:
241 case SimpleTypeKind::Float80:
242 case SimpleTypeKind::Float128:
243 return PDB_BuiltinType::Float;
245 return PDB_BuiltinType::None;
247 llvm_unreachable("Unreachable");
250 SymIndexId NativeTypeEnum::getUnmodifiedTypeId() const {
251 return UnmodifiedType ? UnmodifiedType->getSymIndexId() : 0;
254 bool NativeTypeEnum::hasConstructor() const {
256 return UnmodifiedType->hasConstructor();
258 return bool(Record->getOptions() &
259 codeview::ClassOptions::HasConstructorOrDestructor);
262 bool NativeTypeEnum::hasAssignmentOperator() const {
264 return UnmodifiedType->hasAssignmentOperator();
266 return bool(Record->getOptions() &
267 codeview::ClassOptions::HasOverloadedAssignmentOperator);
270 bool NativeTypeEnum::hasNestedTypes() const {
272 return UnmodifiedType->hasNestedTypes();
274 return bool(Record->getOptions() &
275 codeview::ClassOptions::ContainsNestedClass);
278 bool NativeTypeEnum::isIntrinsic() const {
280 return UnmodifiedType->isIntrinsic();
282 return bool(Record->getOptions() & codeview::ClassOptions::Intrinsic);
285 bool NativeTypeEnum::hasCastOperator() const {
287 return UnmodifiedType->hasCastOperator();
289 return bool(Record->getOptions() &
290 codeview::ClassOptions::HasConversionOperator);
293 uint64_t NativeTypeEnum::getLength() const {
295 return UnmodifiedType->getLength();
297 const auto Id = Session.getSymbolCache().findSymbolByTypeIndex(
298 Record->getUnderlyingType());
299 const auto UnderlyingType =
300 Session.getConcreteSymbolById<PDBSymbolTypeBuiltin>(Id);
301 return UnderlyingType ? UnderlyingType->getLength() : 0;
304 std::string NativeTypeEnum::getName() const {
306 return UnmodifiedType->getName();
308 return Record->getName();
311 bool NativeTypeEnum::isNested() const {
313 return UnmodifiedType->isNested();
315 return bool(Record->getOptions() & codeview::ClassOptions::Nested);
318 bool NativeTypeEnum::hasOverloadedOperator() const {
320 return UnmodifiedType->hasOverloadedOperator();
322 return bool(Record->getOptions() &
323 codeview::ClassOptions::HasOverloadedOperator);
326 bool NativeTypeEnum::isPacked() const {
328 return UnmodifiedType->isPacked();
330 return bool(Record->getOptions() & codeview::ClassOptions::Packed);
333 bool NativeTypeEnum::isScoped() const {
335 return UnmodifiedType->isScoped();
337 return bool(Record->getOptions() & codeview::ClassOptions::Scoped);
340 SymIndexId NativeTypeEnum::getTypeId() const {
342 return UnmodifiedType->getTypeId();
344 return Session.getSymbolCache().findSymbolByTypeIndex(
345 Record->getUnderlyingType());
348 bool NativeTypeEnum::isRefUdt() const { return false; }
350 bool NativeTypeEnum::isValueUdt() const { return false; }
352 bool NativeTypeEnum::isInterfaceUdt() const { return false; }
354 bool NativeTypeEnum::isConstType() const {
357 return ((Modifiers->getModifiers() & ModifierOptions::Const) !=
358 ModifierOptions::None);
361 bool NativeTypeEnum::isVolatileType() const {
364 return ((Modifiers->getModifiers() & ModifierOptions::Volatile) !=
365 ModifierOptions::None);
368 bool NativeTypeEnum::isUnalignedType() const {
371 return ((Modifiers->getModifiers() & ModifierOptions::Unaligned) !=
372 ModifierOptions::None);
375 const NativeTypeBuiltin &NativeTypeEnum::getUnderlyingBuiltinType() const {
377 return UnmodifiedType->getUnderlyingBuiltinType();
379 return Session.getSymbolCache().getNativeSymbolById<NativeTypeBuiltin>(