19#include "llvm/ADT/STLExtras.h"
20#include "llvm/DebugInfo/CodeView/Formatters.h"
21#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
22#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
23#include "llvm/DebugInfo/CodeView/TypeIndex.h"
24#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
25#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
26#include "llvm/DebugInfo/PDB/PDBTypes.h"
40 llvm::DenseMap<clang::Decl *, DeclStatus> &decl_to_status)
46 case CVTagRecord::Enum:
48 case CVTagRecord::Union:
49 m_layout.bit_size = m_cv_tag_record.asUnion().getSize() * 8;
50 m_record.record.kind = Member::Union;
52 case CVTagRecord::Class:
53 case CVTagRecord::Struct:
54 m_layout.bit_size = m_cv_tag_record.asClass().getSize() * 8;
55 m_record.record.kind = Member::Struct;
63 return Error::success();
67 llvm::codeview::TypeIndex ti, llvm::codeview::MemberAccess access,
68 std::optional<uint64_t> vtable_idx) {
70 clang::QualType qt =
m_ast_builder.GetOrCreateClangType(type_id);
72 CVType udt_cvt =
m_index.tpi().getType(ti);
74 std::unique_ptr<clang::CXXBaseSpecifier> base_spec =
77 vtable_idx.has_value(), udt_cvt.kind() == LF_CLASS);
82 std::make_pair(vtable_idx.value_or(0), std::move(base_spec)));
88 MethodOptions options,
89 MemberAttributes attrs) {
90 clang::QualType method_qt =
92 if (method_qt.isNull())
96 bool is_artificial = (options & MethodOptions::CompilerGenerated) ==
97 MethodOptions::CompilerGenerated;
99 m_ast_builder.clang().GetSymbolFile()->GetBackingSymbolFile());
100 std::string asm_label =
pdb->GetMethodCallLabel(
103 asm_label, method_ct, attrs.isVirtual(),
104 attrs.isStatic(), is_artificial);
107Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
108 BaseClassRecord &base) {
109 clang::QualType base_qt =
112 if (base_qt.isNull())
113 return llvm::Error::success();
119 return Error::success();
122 auto offset = clang::CharUnits::fromQuantity(base.getBaseOffset());
125 return llvm::Error::success();
128Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
129 VirtualBaseClassRecord &base) {
134 if (cvr.Kind == LF_VBCLASS)
137 return Error::success();
140Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
141 ListContinuationRecord &cont) {
142 return Error::success();
145Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
146 VFPtrRecord &vfptr) {
147 return Error::success();
150Error UdtRecordCompleter::visitKnownMember(
151 CVMemberRecord &cvr, StaticDataMemberRecord &static_data_member) {
152 clang::QualType member_type =
153 m_ast_builder.GetOrCreateClangType(PdbTypeSymId(static_data_member.Type));
154 if (member_type.isNull())
155 return llvm::Error::success();
157 CompilerType member_ct =
m_ast_builder.ToCompilerType(member_type);
168 std::string qual_name =
172 m_index.globals().findRecordsByName(qual_name,
m_index.symrecords());
174 for (
const auto &result : results) {
175 if (result.second.kind() == SymbolKind::S_CONSTANT) {
176 ConstantSym constant(SymbolRecordKind::ConstantSym);
177 cantFail(SymbolDeserializer::deserializeAs<ConstantSym>(result.second,
180 clang::QualType qual_type = decl->getType();
181 unsigned type_width = decl->getASTContext().getIntWidth(qual_type);
186 unsigned min_constant_width = 0;
187 if (qual_type->isUnsignedIntegerOrEnumerationType())
188 min_constant_width = constant.Value.getActiveBits();
190 min_constant_width = constant.Value.getSignificantBits();
192 if (qual_type->isIntegralOrEnumerationType()) {
193 if (type_width >= min_constant_width) {
195 decl, constant.Value.extOrTrunc(type_width));
198 "Class '{0}' has a member '{1}' of type '{2}' ({3} bits) "
199 "which resolves to a wider constant value ({4} bits). "
200 "Ignoring constant.",
202 member_ct.
GetTypeName(), type_width, min_constant_width);
206 switch (basic_type_enum) {
210 if (type_width == min_constant_width) {
213 ? llvm::APFloat(constant.Value.bitsToFloat())
214 : llvm::APFloat(constant.Value.bitsToDouble()));
215 decl->setConstexpr(
true);
219 "Class '{0}' has a member '{1}' of type '{2}' ({3} bits) "
220 "which resolves to a constant value of mismatched width "
221 "({4} bits). Ignoring constant.",
223 member_ct.
GetTypeName(), type_width, min_constant_width);
237 return Error::success();
240Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
241 NestedTypeRecord &nested) {
244 return Error::success();
247 m_ast_builder.GetOrCreateClangType(PdbTypeSymId(nested.Type,
false));
249 return Error::success();
254 auto *pdb =
static_cast<SymbolFileNativePDB *
>(
255 m_ast_builder.clang().GetSymbolFile()->GetBackingSymbolFile());
256 std::optional<TypeIndex> parent = pdb->GetParentType(nested.Type);
257 if (parent && *parent ==
m_id.index && ct.
GetTypeName(
true) == nested.Name)
258 return Error::success();
260 clang::DeclContext *decl_ctx =
263 return Error::success();
265 std::string name = nested.Name.str();
268 return Error::success();
271Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
272 DataMemberRecord &data_member) {
274 uint64_t offset = data_member.FieldOffset * 8;
275 uint32_t bitfield_width = 0;
277 TypeIndex ti(data_member.Type);
278 if (!ti.isSimple()) {
279 CVType cvt =
m_index.tpi().getType(ti);
280 if (cvt.kind() == LF_BITFIELD) {
282 llvm::cantFail(TypeDeserializer::deserializeAs<BitFieldRecord>(cvt, bfr));
283 offset += bfr.BitOffset;
284 bitfield_width = bfr.BitSize;
289 clang::QualType member_qt =
291 if (member_qt.isNull())
292 return Error::success();
298 return Error::success();
299 m_record.CollectMember(data_member.Name, offset, field_size, member_qt,
301 return Error::success();
304Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
305 OneMethodRecord &one_method) {
306 AddMethod(one_method.Name, one_method.Type, one_method.getOptions(),
309 return Error::success();
312Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
313 OverloadedMethodRecord &overloaded) {
314 TypeIndex method_list_idx = overloaded.MethodList;
316 CVType method_list_type =
m_index.tpi().getType(method_list_idx);
317 assert(method_list_type.kind() == LF_METHODLIST);
319 MethodOverloadListRecord method_list;
320 llvm::cantFail(TypeDeserializer::deserializeAs<MethodOverloadListRecord>(
321 method_list_type, method_list));
323 for (
const OneMethodRecord &method : method_list.Methods)
324 AddMethod(overloaded.Name, method.Type, method.getOptions(), method.Attrs);
326 return Error::success();
329Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
330 EnumeratorRecord &enumerator) {
336 return Error::success();
338 llvm::APSInt val = enumerator.Value;
339 clang::QualType int_ty = enum_decl->getIntegerType();
340 uint64_t n_bits =
m_ast_builder.clang().getASTContext().getTypeSize(int_ty);
342 return Error::success();
347 val = val.extOrTrunc(n_bits);
348 val.setIsSigned(int_ty->isSignedIntegerType());
352 return Error::success();
357 llvm::stable_sort(
m_bases, llvm::less_first());
359 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases;
362 bases.push_back(std::move(ib.second));
368 for (
const auto &base_class : bases) {
369 clang::TypeSourceInfo *type_source_info =
370 base_class->getTypeSourceInfo();
371 if (type_source_info) {
373 clang.GetType(type_source_info->getType()));
384 if (
auto *record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(&
m_tag_decl)) {
393 clang::DeclContext *parent_decl_ctx) {
395 clang.GetSymbolFile()->GetBackingSymbolFile());
396 clang::FieldDecl *field_decl =
nullptr;
397 uint64_t bit_size = 0;
398 switch (field->
kind) {
409 ? clang::TagTypeKind::Struct
410 : clang::TagTypeKind::Union;
419 clang::DeclContext *decl_ctx =
clang.GetDeclContextForType(record_ct);
420 for (
const auto &member : field->
fields) {
424 uint64_t member_bit_size =
AddMember(
clang, member.get(), member_offset,
425 record_ct, layout, decl_ctx);
427 bit_size = std::max(bit_size, member_offset + member_bit_size);
429 bit_size = std::max(bit_size, member_bit_size);
433 clang::RecordDecl *record_decl =
clang.GetAsRecordDecl(record_ct);
434 m_ast_builder.GetClangASTImporter().SetRecordLayout(record_decl, layout);
440 status.
uid =
pdb->anonymous_id--;
447 parent_layout.
field_offsets.insert({field_decl, bit_offset});
453 clang::DeclContext *decl_ctx =
458 for (
const auto &field :
m_record.record.fields) {
466 return std::tie(lhs->original_index, lhs->bit_offset) <
467 std::tie(rhs->original_index, rhs->bit_offset);
471 field->RestoreOriginalOrder();
475 llvm::StringRef name, uint64_t offset, uint64_t field_size,
477 uint32_t member_index) {
478 fields_map[offset].push_back(std::make_unique<Member>(
479 name, offset, field_size, qt, access, bitfield_width, member_index));
503 std::map<uint64_t, std::vector<Member *>> end_offset_map;
504 auto is_last_end_offset = [&](
auto it) {
505 return it != end_offset_map.end() && ++it == end_offset_map.end();
509 uint64_t offset = pair.first;
510 auto &fields = pair.second;
517 assert(!end_offset_map.empty());
518 auto iter = end_offset_map.lower_bound(offset);
519 if (iter == end_offset_map.end())
521 else if (iter->first > offset) {
522 if (iter == end_offset_map.begin())
526 if (iter->second.empty())
531 if (iter->first <= offset && is_last_end_offset(iter)) {
536 "Current record must be a union");
537 assert(!
record.fields.empty());
539 parent =
record.fields.back().get();
542 parent = iter->second.back();
543 iter->second.pop_back();
551 if (fields.size() == 1) {
552 uint64_t end_offset = offset + fields.back()->bit_size;
553 parent->
fields.push_back(std::move(fields.back()));
555 end_offset_map[end_offset].push_back(parent);
557 assert(parent == &
record &&
558 "If parent is union, it must be the top level record.");
559 end_offset_map[end_offset].push_back(parent->
fields.back().get());
564 parent = parent->
fields.back().get();
568 assert(parent == &
record &&
569 "If parent is union, it must be the top level record.");
571 for (
auto &field : fields) {
572 int64_t bit_size = field->bit_size;
576 parent->
fields.push_back(std::move(field));
577 end_offset_map[offset + bit_size].push_back(
578 parent->
fields.back().get());
583 record.RestoreOriginalOrder();
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Generic representation of a type in a programming language.
lldb::BasicType GetBasicTypeEnumeration() const
bool IsCompleteType() const
ConstString GetTypeName(bool BaseOnly=false) const
CompilerType CreateTypedef(const char *name, const CompilerDeclContext &decl_ctx, uint32_t payload) const
Create a typedef to this type using "name" as the name of the typedef this type is valid and the type...
A TypeSystem implementation based on Clang.
static void SetFloatingInitializerForVariable(clang::VarDecl *var, const llvm::APFloat &init_value)
Initializes a variable with a floating point value.
static void SetIntegerInitializerForVariable(clang::VarDecl *var, const llvm::APInt &init_value)
Initializes a variable with an integer value.
static clang::VarDecl * AddVariableToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &var_type)
static void BuildIndirectFields(const CompilerType &type)
static clang::CXXRecordDecl * GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type)
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
static bool StartTagDeclarationDefinition(const CompilerType &type)
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, uint32_t bitfield_bit_size)
static clang::EnumDecl * GetAsEnumDecl(const CompilerType &type)
static void RequireCompleteType(CompilerType type)
Complete a type from debug info, or mark it as forcefully completed if there is no definition of the ...
TypeSystemClang & clang()
PdbIndex - Lazy access to the important parts of a PDB file.
std::vector< IndexedBase > m_bases
CVTagRecord m_cv_tag_record
llvm::Error visitMemberEnd(llvm::codeview::CVMemberRecord &Record) override
ClangASTImporter::LayoutInfo m_layout
UdtRecordCompleter(PdbTypeSymId id, CompilerType &derived_ct, clang::TagDecl &tag_decl, PdbAstBuilderClang &ast_builder, PdbIndex &index, llvm::DenseMap< clang::Decl *, DeclStatus > &decl_to_status)
clang::TagDecl & m_tag_decl
CompilerType & m_derived_ct
llvm::DenseMap< clang::Decl *, DeclStatus > & m_decl_to_status
uint64_t AddMember(TypeSystemClang &clang, Member *field, uint64_t bit_offset, CompilerType parent_ct, ClangASTImporter::LayoutInfo &parent_layout, clang::DeclContext *decl_ctx)
PdbAstBuilderClang & m_ast_builder
clang::QualType AddBaseClassForTypeIndex(llvm::codeview::TypeIndex ti, llvm::codeview::MemberAccess access, std::optional< uint64_t > vtable_idx=std::optional< uint64_t >())
uint32_t m_member_index
Index of the current member.
void AddMethod(llvm::StringRef name, llvm::codeview::TypeIndex type_idx, llvm::codeview::MethodOptions options, llvm::codeview::MemberAttributes attrs)
std::unique_ptr< Member > MemberUP
llvm::StringRef DropNameScope(llvm::StringRef name)
lldb::AccessType TranslateMemberAccess(llvm::codeview::MemberAccess access)
size_t GetSizeOfType(PdbTypeSymId id, llvm::pdb::TpiStream &tpi)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
BasicType
Basic types enumeration for the public API SBType::GetBasicType().
@ eLanguageTypeC_plus_plus
ISO C++:1998.
llvm::DenseMap< const clang::FieldDecl *, uint64_t > field_offsets
llvm::codeview::TypeIndex index
llvm::SmallVector< MemberUP, 1 > fields
enum lldb_private::npdb::UdtRecordCompleter::Member::Kind kind
void RestoreOriginalOrder()
uint32_t original_index
Index of the member inside the LF_FIELDLIST.
std::map< uint64_t, llvm::SmallVector< MemberUP, 1 > > fields_map
void CollectMember(llvm::StringRef name, uint64_t offset, uint64_t field_size, clang::QualType qt, lldb::AccessType access, uint64_t bitfield_width, uint32_t member_index)