1 //===- NativeTypeEnum.cpp - info about enum type ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/PDB/Native/NativeTypeEnum.h"
12 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
13 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
14 #include "llvm/DebugInfo/PDB/Native/NativeEnumTypes.h"
15 #include "llvm/DebugInfo/PDB/Native/NativeSymbolEnumerator.h"
16 #include "llvm/DebugInfo/PDB/Native/NativeTypeBuiltin.h"
17 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
18 #include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
19 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
22 #include "llvm/Support/FormatVariadic.h"
27 using namespace llvm::codeview;
28 using namespace llvm::pdb;
31 // Yea, this is a pretty terrible class name. But if we have an enum:
38 // then A and B are the "enumerators" of the "enum" Foo. And we need
40 class NativeEnumEnumEnumerators : public IPDBEnumSymbols, TypeVisitorCallbacks {
42 NativeEnumEnumEnumerators(NativeSession &Session,
43 const NativeTypeEnum &ClassParent);
45 uint32_t getChildCount() const override;
46 std::unique_ptr<PDBSymbol> getChildAtIndex(uint32_t Index) const override;
47 std::unique_ptr<PDBSymbol> getNext() override;
48 void reset() override;
51 Error visitKnownMember(CVMemberRecord &CVM,
52 EnumeratorRecord &Record) override;
53 Error visitKnownMember(CVMemberRecord &CVM,
54 ListContinuationRecord &Record) override;
56 NativeSession &Session;
57 const NativeTypeEnum &ClassParent;
58 std::vector<EnumeratorRecord> Enumerators;
59 Optional<TypeIndex> ContinuationIndex;
64 NativeEnumEnumEnumerators::NativeEnumEnumEnumerators(
65 NativeSession &Session, const NativeTypeEnum &ClassParent)
66 : Session(Session), ClassParent(ClassParent) {
67 TpiStream &Tpi = cantFail(Session.getPDBFile().getPDBTpiStream());
68 LazyRandomTypeCollection &Types = Tpi.typeCollection();
70 ContinuationIndex = ClassParent.getEnumRecord().FieldList;
71 while (ContinuationIndex) {
72 CVType FieldList = Types.getType(*ContinuationIndex);
73 assert(FieldList.kind() == LF_FIELDLIST);
74 ContinuationIndex.reset();
75 cantFail(visitMemberRecordStream(FieldList.data(), *this));
79 Error NativeEnumEnumEnumerators::visitKnownMember(CVMemberRecord &CVM,
80 EnumeratorRecord &Record) {
81 Enumerators.push_back(Record);
82 return Error::success();
85 Error NativeEnumEnumEnumerators::visitKnownMember(
86 CVMemberRecord &CVM, ListContinuationRecord &Record) {
87 ContinuationIndex = Record.ContinuationIndex;
88 return Error::success();
91 uint32_t NativeEnumEnumEnumerators::getChildCount() const {
92 return Enumerators.size();
95 std::unique_ptr<PDBSymbol>
96 NativeEnumEnumEnumerators::getChildAtIndex(uint32_t Index) const {
97 if (Index >= getChildCount())
100 SymIndexId Id = Session.getSymbolCache()
101 .getOrCreateFieldListMember<NativeSymbolEnumerator>(
102 ClassParent.getEnumRecord().FieldList, Index,
103 ClassParent, Enumerators[Index]);
104 return Session.getSymbolCache().getSymbolById(Id);
107 std::unique_ptr<PDBSymbol> NativeEnumEnumEnumerators::getNext() {
108 if (Index >= getChildCount())
111 return getChildAtIndex(Index++);
114 void NativeEnumEnumEnumerators::reset() { Index = 0; }
116 NativeTypeEnum::NativeTypeEnum(NativeSession &Session, SymIndexId Id,
117 TypeIndex Index, EnumRecord Record)
118 : NativeRawSymbol(Session, PDB_SymType::Enum, Id), Index(Index),
119 Record(std::move(Record)) {}
121 NativeTypeEnum::NativeTypeEnum(NativeSession &Session, SymIndexId Id,
122 NativeTypeEnum &UnmodifiedType,
123 codeview::ModifierRecord Modifier)
124 : NativeRawSymbol(Session, PDB_SymType::Enum, Id),
125 UnmodifiedType(&UnmodifiedType), Modifiers(std::move(Modifier)) {}
127 NativeTypeEnum::~NativeTypeEnum() {}
129 void NativeTypeEnum::dump(raw_ostream &OS, int Indent,
130 PdbSymbolIdField ShowIdFields,
131 PdbSymbolIdField RecurseIdFields) const {
132 NativeRawSymbol::dump(OS, Indent, ShowIdFields, RecurseIdFields);
134 dumpSymbolField(OS, "baseType", static_cast<uint32_t>(getBuiltinType()),
136 dumpSymbolIdField(OS, "lexicalParentId", 0, Indent, Session,
137 PdbSymbolIdField::LexicalParent, ShowIdFields,
139 dumpSymbolField(OS, "name", getName(), Indent);
140 dumpSymbolIdField(OS, "typeId", getTypeId(), Indent, Session,
141 PdbSymbolIdField::Type, ShowIdFields, RecurseIdFields);
142 if (Modifiers.hasValue())
143 dumpSymbolIdField(OS, "unmodifiedTypeId", getUnmodifiedTypeId(), Indent,
144 Session, PdbSymbolIdField::UnmodifiedType, ShowIdFields,
146 dumpSymbolField(OS, "length", getLength(), Indent);
147 dumpSymbolField(OS, "constructor", hasConstructor(), Indent);
148 dumpSymbolField(OS, "constType", isConstType(), Indent);
149 dumpSymbolField(OS, "hasAssignmentOperator", hasAssignmentOperator(), Indent);
150 dumpSymbolField(OS, "hasCastOperator", hasCastOperator(), Indent);
151 dumpSymbolField(OS, "hasNestedTypes", hasNestedTypes(), Indent);
152 dumpSymbolField(OS, "overloadedOperator", hasOverloadedOperator(), Indent);
153 dumpSymbolField(OS, "isInterfaceUdt", isInterfaceUdt(), Indent);
154 dumpSymbolField(OS, "intrinsic", isIntrinsic(), Indent);
155 dumpSymbolField(OS, "nested", isNested(), Indent);
156 dumpSymbolField(OS, "packed", isPacked(), Indent);
157 dumpSymbolField(OS, "isRefUdt", isRefUdt(), Indent);
158 dumpSymbolField(OS, "scoped", isScoped(), Indent);
159 dumpSymbolField(OS, "unalignedType", isUnalignedType(), Indent);
160 dumpSymbolField(OS, "isValueUdt", isValueUdt(), Indent);
161 dumpSymbolField(OS, "volatileType", isVolatileType(), Indent);
164 std::unique_ptr<IPDBEnumSymbols>
165 NativeTypeEnum::findChildren(PDB_SymType Type) const {
166 if (Type != PDB_SymType::Data)
167 return llvm::make_unique<NullEnumerator<PDBSymbol>>();
169 const NativeTypeEnum *ClassParent = nullptr;
173 ClassParent = UnmodifiedType;
174 return llvm::make_unique<NativeEnumEnumEnumerators>(Session, *ClassParent);
177 PDB_SymType NativeTypeEnum::getSymTag() const { return PDB_SymType::Enum; }
179 PDB_BuiltinType NativeTypeEnum::getBuiltinType() const {
181 return UnmodifiedType->getBuiltinType();
183 Session.getSymbolCache().findSymbolByTypeIndex(Record->getUnderlyingType());
185 codeview::TypeIndex Underlying = Record->getUnderlyingType();
187 // This indicates a corrupt record.
188 if (!Underlying.isSimple() ||
189 Underlying.getSimpleMode() != SimpleTypeMode::Direct) {
190 return PDB_BuiltinType::None;
193 switch (Underlying.getSimpleKind()) {
194 case SimpleTypeKind::Boolean128:
195 case SimpleTypeKind::Boolean64:
196 case SimpleTypeKind::Boolean32:
197 case SimpleTypeKind::Boolean16:
198 case SimpleTypeKind::Boolean8:
199 return PDB_BuiltinType::Bool;
200 case SimpleTypeKind::NarrowCharacter:
201 case SimpleTypeKind::UnsignedCharacter:
202 case SimpleTypeKind::SignedCharacter:
203 return PDB_BuiltinType::Char;
204 case SimpleTypeKind::WideCharacter:
205 return PDB_BuiltinType::WCharT;
206 case SimpleTypeKind::Character16:
207 return PDB_BuiltinType::Char16;
208 case SimpleTypeKind::Character32:
209 return PDB_BuiltinType::Char32;
210 case SimpleTypeKind::Int128:
211 case SimpleTypeKind::Int128Oct:
212 case SimpleTypeKind::Int16:
213 case SimpleTypeKind::Int16Short:
214 case SimpleTypeKind::Int32:
215 case SimpleTypeKind::Int32Long:
216 case SimpleTypeKind::Int64:
217 case SimpleTypeKind::Int64Quad:
218 return PDB_BuiltinType::Int;
219 case SimpleTypeKind::UInt128:
220 case SimpleTypeKind::UInt128Oct:
221 case SimpleTypeKind::UInt16:
222 case SimpleTypeKind::UInt16Short:
223 case SimpleTypeKind::UInt32:
224 case SimpleTypeKind::UInt32Long:
225 case SimpleTypeKind::UInt64:
226 case SimpleTypeKind::UInt64Quad:
227 return PDB_BuiltinType::UInt;
228 case SimpleTypeKind::HResult:
229 return PDB_BuiltinType::HResult;
230 case SimpleTypeKind::Complex16:
231 case SimpleTypeKind::Complex32:
232 case SimpleTypeKind::Complex32PartialPrecision:
233 case SimpleTypeKind::Complex64:
234 case SimpleTypeKind::Complex80:
235 case SimpleTypeKind::Complex128:
236 return PDB_BuiltinType::Complex;
237 case SimpleTypeKind::Float16:
238 case SimpleTypeKind::Float32:
239 case SimpleTypeKind::Float32PartialPrecision:
240 case SimpleTypeKind::Float48:
241 case SimpleTypeKind::Float64:
242 case SimpleTypeKind::Float80:
243 case SimpleTypeKind::Float128:
244 return PDB_BuiltinType::Float;
246 return PDB_BuiltinType::None;
248 llvm_unreachable("Unreachable");
251 SymIndexId NativeTypeEnum::getUnmodifiedTypeId() const {
252 return UnmodifiedType ? UnmodifiedType->getSymIndexId() : 0;
255 bool NativeTypeEnum::hasConstructor() const {
257 return UnmodifiedType->hasConstructor();
259 return bool(Record->getOptions() &
260 codeview::ClassOptions::HasConstructorOrDestructor);
263 bool NativeTypeEnum::hasAssignmentOperator() const {
265 return UnmodifiedType->hasAssignmentOperator();
267 return bool(Record->getOptions() &
268 codeview::ClassOptions::HasOverloadedAssignmentOperator);
271 bool NativeTypeEnum::hasNestedTypes() const {
273 return UnmodifiedType->hasNestedTypes();
275 return bool(Record->getOptions() &
276 codeview::ClassOptions::ContainsNestedClass);
279 bool NativeTypeEnum::isIntrinsic() const {
281 return UnmodifiedType->isIntrinsic();
283 return bool(Record->getOptions() & codeview::ClassOptions::Intrinsic);
286 bool NativeTypeEnum::hasCastOperator() const {
288 return UnmodifiedType->hasCastOperator();
290 return bool(Record->getOptions() &
291 codeview::ClassOptions::HasConversionOperator);
294 uint64_t NativeTypeEnum::getLength() const {
296 return UnmodifiedType->getLength();
298 const auto Id = Session.getSymbolCache().findSymbolByTypeIndex(
299 Record->getUnderlyingType());
300 const auto UnderlyingType =
301 Session.getConcreteSymbolById<PDBSymbolTypeBuiltin>(Id);
302 return UnderlyingType ? UnderlyingType->getLength() : 0;
305 std::string NativeTypeEnum::getName() const {
307 return UnmodifiedType->getName();
309 return Record->getName();
312 bool NativeTypeEnum::isNested() const {
314 return UnmodifiedType->isNested();
316 return bool(Record->getOptions() & codeview::ClassOptions::Nested);
319 bool NativeTypeEnum::hasOverloadedOperator() const {
321 return UnmodifiedType->hasOverloadedOperator();
323 return bool(Record->getOptions() &
324 codeview::ClassOptions::HasOverloadedOperator);
327 bool NativeTypeEnum::isPacked() const {
329 return UnmodifiedType->isPacked();
331 return bool(Record->getOptions() & codeview::ClassOptions::Packed);
334 bool NativeTypeEnum::isScoped() const {
336 return UnmodifiedType->isScoped();
338 return bool(Record->getOptions() & codeview::ClassOptions::Scoped);
341 SymIndexId NativeTypeEnum::getTypeId() const {
343 return UnmodifiedType->getTypeId();
345 return Session.getSymbolCache().findSymbolByTypeIndex(
346 Record->getUnderlyingType());
349 bool NativeTypeEnum::isRefUdt() const { return false; }
351 bool NativeTypeEnum::isValueUdt() const { return false; }
353 bool NativeTypeEnum::isInterfaceUdt() const { return false; }
355 bool NativeTypeEnum::isConstType() const {
358 return ((Modifiers->getModifiers() & ModifierOptions::Const) !=
359 ModifierOptions::None);
362 bool NativeTypeEnum::isVolatileType() const {
365 return ((Modifiers->getModifiers() & ModifierOptions::Volatile) !=
366 ModifierOptions::None);
369 bool NativeTypeEnum::isUnalignedType() const {
372 return ((Modifiers->getModifiers() & ModifierOptions::Unaligned) !=
373 ModifierOptions::None);
376 const NativeTypeBuiltin &NativeTypeEnum::getUnderlyingBuiltinType() const {
378 return UnmodifiedType->getUnderlyingBuiltinType();
380 return Session.getSymbolCache().getNativeSymbolById<NativeTypeBuiltin>(