20#include "llvm/ADT/STLExtras.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,
42 llvm::SmallSet<std::pair<llvm::StringRef, CompilerType>, 8>>
49 case CVTagRecord::Enum:
51 case CVTagRecord::Union:
52 m_layout.bit_size = m_cv_tag_record.asUnion().getSize() * 8;
53 m_record.record.kind = Member::Union;
55 case CVTagRecord::Class:
56 case CVTagRecord::Struct:
57 m_layout.bit_size = m_cv_tag_record.asClass().getSize() * 8;
58 m_record.record.kind = Member::Struct;
64 llvm::codeview::TypeIndex ti, llvm::codeview::MemberAccess access,
65 std::optional<uint64_t> vtable_idx) {
67 clang::QualType qt =
m_ast_builder.GetOrCreateClangType(type_id);
69 CVType udt_cvt =
m_index.tpi().getType(ti);
71 std::unique_ptr<clang::CXXBaseSpecifier> base_spec =
74 vtable_idx.has_value(), udt_cvt.kind() == LF_CLASS);
79 std::make_pair(vtable_idx.value_or(0), std::move(base_spec)));
85 MethodOptions options,
86 MemberAttributes attrs) {
87 clang::QualType method_qt =
89 if (method_qt.isNull())
97 if (iter->getSecond().contains({name, method_ct})) {
102 bool is_artificial = (options & MethodOptions::CompilerGenerated) ==
103 MethodOptions::CompilerGenerated;
105 derived_opaque_ty, name.data(), {}, method_ct,
106 attrs.isVirtual(), attrs.isStatic(),
false,
false,
false, is_artificial);
111Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
112 BaseClassRecord &base) {
113 clang::QualType base_qt =
116 if (base_qt.isNull())
117 return llvm::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, access,
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) {
465 llvm::StringRef name, uint64_t offset, uint64_t field_size,
467 fields_map[offset].push_back(std::make_unique<Member>(
468 name, offset, field_size, qt, access, bitfield_width));
492 std::map<uint64_t, std::vector<Member *>> end_offset_map;
493 auto is_last_end_offset = [&](
auto it) {
494 return it != end_offset_map.end() && ++it == end_offset_map.end();
498 uint64_t offset = pair.first;
499 auto &fields = pair.second;
507 auto iter = end_offset_map.lower_bound(offset);
508 if (iter == end_offset_map.end())
510 else if (iter->first > offset) {
511 if (iter == end_offset_map.begin())
515 if (iter->second.empty())
520 if (iter->first <= offset && is_last_end_offset(iter)) {
525 "Current record must be a union");
526 assert(!
record.fields.empty());
528 parent =
record.fields.back().get();
531 parent = iter->second.back();
532 iter->second.pop_back();
540 if (fields.size() == 1) {
541 uint64_t end_offset = offset + fields.back()->bit_size;
542 parent->
fields.push_back(std::move(fields.back()));
544 end_offset_map[end_offset].push_back(parent);
547 "If parent is union, it must be the top level record.");
548 end_offset_map[end_offset].push_back(parent->
fields.back().get());
553 parent = parent->
fields.back().get();
557 "If parent is union, it must be the top level record.");
559 for (
auto &field : fields) {
560 int64_t bit_size = field->bit_size;
561 parent->
fields.push_back(std::move(field));
562 end_offset_map[offset + bit_size].push_back(
563 parent->
fields.back().get());
#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::DenseMap< lldb::opaque_compiler_type_t, llvm::SmallSet< std::pair< llvm::StringRef, CompilerType >, 8 > > & m_cxx_record_map
ClangASTImporter::LayoutInfo m_layout
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)
UdtRecordCompleter(PdbTypeSymId id, CompilerType &derived_ct, clang::TagDecl &tag_decl, PdbAstBuilderClang &ast_builder, PdbIndex &index, llvm::DenseMap< clang::Decl *, DeclStatus > &decl_to_status, llvm::DenseMap< lldb::opaque_compiler_type_t, llvm::SmallSet< std::pair< llvm::StringRef, CompilerType >, 8 > > &cxx_record_map)
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 >())
void AddMethod(llvm::StringRef name, llvm::codeview::TypeIndex type_idx, llvm::codeview::MethodOptions options, llvm::codeview::MemberAttributes attrs)
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.
void * opaque_compiler_type_t
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::SmallVector< MemberUP, 1 > fields
enum lldb_private::npdb::UdtRecordCompleter::Member::Kind kind
void CollectMember(llvm::StringRef name, uint64_t offset, uint64_t field_size, clang::QualType qt, lldb::AccessType access, uint64_t bitfield_width)
std::map< uint64_t, llvm::SmallVector< MemberUP, 1 > > fields_map