1 //===-- DWARFASTParserClang.cpp -------------------------------------------===//
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 //===----------------------------------------------------------------------===//
11 #include "DWARFASTParserClang.h"
12 #include "DWARFDebugInfo.h"
13 #include "DWARFDeclContext.h"
14 #include "DWARFDefines.h"
15 #include "SymbolFileDWARF.h"
16 #include "SymbolFileDWARFDebugMap.h"
17 #include "SymbolFileDWARFDwo.h"
18 #include "UniqueDWARFASTType.h"
20 #include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
21 #include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
22 #include "Plugins/ExpressionParser/Clang/ClangUtil.h"
23 #include "Plugins/Language/ObjC/ObjCLanguage.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Core/Value.h"
26 #include "lldb/Host/Host.h"
27 #include "lldb/Symbol/CompileUnit.h"
28 #include "lldb/Symbol/Function.h"
29 #include "lldb/Symbol/ObjectFile.h"
30 #include "lldb/Symbol/SymbolFile.h"
31 #include "lldb/Symbol/TypeList.h"
32 #include "lldb/Symbol/TypeMap.h"
33 #include "lldb/Target/Language.h"
34 #include "lldb/Utility/LLDBAssert.h"
35 #include "lldb/Utility/Log.h"
36 #include "lldb/Utility/StreamString.h"
38 #include "llvm/Demangle/Demangle.h"
40 #include "clang/AST/CXXInheritance.h"
41 #include "clang/AST/DeclCXX.h"
42 #include "clang/AST/DeclObjC.h"
43 #include "clang/AST/DeclTemplate.h"
49 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
51 #ifdef ENABLE_DEBUG_PRINTF
53 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
55 #define DEBUG_PRINTF(fmt, ...)
59 using namespace lldb_private;
60 DWARFASTParserClang::DWARFASTParserClang(TypeSystemClang &ast)
61 : m_ast(ast), m_die_to_decl_ctx(), m_decl_ctx_to_die() {}
63 DWARFASTParserClang::~DWARFASTParserClang() {}
65 static AccessType DW_ACCESS_to_AccessType(uint32_t dwarf_accessibility) {
66 switch (dwarf_accessibility) {
67 case DW_ACCESS_public:
69 case DW_ACCESS_private:
70 return eAccessPrivate;
71 case DW_ACCESS_protected:
72 return eAccessProtected;
79 static bool DeclKindIsCXXClass(clang::Decl::Kind decl_kind) {
81 case clang::Decl::CXXRecord:
82 case clang::Decl::ClassTemplateSpecialization:
91 ClangASTImporter &DWARFASTParserClang::GetClangASTImporter() {
92 if (!m_clang_ast_importer_up) {
93 m_clang_ast_importer_up = std::make_unique<ClangASTImporter>();
95 return *m_clang_ast_importer_up;
98 /// Detect a forward declaration that is nested in a DW_TAG_module.
99 static bool IsClangModuleFwdDecl(const DWARFDIE &Die) {
100 if (!Die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0))
102 auto Parent = Die.GetParent();
103 while (Parent.IsValid()) {
104 if (Parent.Tag() == DW_TAG_module)
106 Parent = Parent.GetParent();
111 static DWARFDIE GetContainingClangModuleDIE(const DWARFDIE &die) {
113 DWARFDIE top_module_die;
114 // Now make sure this DIE is scoped in a DW_TAG_module tag and return true
116 for (DWARFDIE parent = die.GetParent(); parent.IsValid();
117 parent = parent.GetParent()) {
118 const dw_tag_t tag = parent.Tag();
119 if (tag == DW_TAG_module)
120 top_module_die = parent;
121 else if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
125 return top_module_die;
130 static lldb::ModuleSP GetContainingClangModule(const DWARFDIE &die) {
132 DWARFDIE clang_module_die = GetContainingClangModuleDIE(die);
134 if (clang_module_die) {
135 const char *module_name = clang_module_die.GetName();
137 return die.GetDWARF()->GetExternalModule(
138 lldb_private::ConstString(module_name));
141 return lldb::ModuleSP();
144 TypeSP DWARFASTParserClang::ParseTypeFromClangModule(const SymbolContext &sc,
147 ModuleSP clang_module_sp = GetContainingClangModule(die);
148 if (!clang_module_sp)
151 // If this type comes from a Clang module, recursively look in the
152 // DWARF section of the .pcm file in the module cache. Clang
153 // generates DWO skeleton units as breadcrumbs to find them.
154 llvm::SmallVector<CompilerContext, 4> decl_context;
155 die.GetDeclContext(decl_context);
158 // The type in the Clang module must have the same language as the current CU.
159 LanguageSet languages;
160 languages.Insert(SymbolFileDWARF::GetLanguage(*die.GetCU()));
161 llvm::DenseSet<SymbolFile *> searched_symbol_files;
162 clang_module_sp->GetSymbolFile()->FindTypes(decl_context, languages,
163 searched_symbol_files, pcm_types);
164 if (pcm_types.Empty()) {
165 // Since this type is defined in one of the Clang modules imported
166 // by this symbol file, search all of them. Instead of calling
167 // sym_file->FindTypes(), which would return this again, go straight
168 // to the imported modules.
169 auto &sym_file = die.GetCU()->GetSymbolFileDWARF();
171 // Well-formed clang modules never form cycles; guard against corrupted
172 // ones by inserting the current file.
173 searched_symbol_files.insert(&sym_file);
174 sym_file.ForEachExternalModule(
175 *sc.comp_unit, searched_symbol_files, [&](Module &module) {
176 module.GetSymbolFile()->FindTypes(decl_context, languages,
177 searched_symbol_files, pcm_types);
178 return pcm_types.GetSize();
182 if (!pcm_types.GetSize())
185 // We found a real definition for this type in the Clang module, so lets use
186 // it and cache the fact that we found a complete type for this die.
187 TypeSP pcm_type_sp = pcm_types.GetTypeAtIndex(0);
191 lldb_private::CompilerType pcm_type = pcm_type_sp->GetForwardCompilerType();
192 lldb_private::CompilerType type =
193 GetClangASTImporter().CopyType(m_ast, pcm_type);
198 // Under normal operation pcm_type is a shallow forward declaration
199 // that gets completed later. This is necessary to support cyclic
200 // data structures. If, however, pcm_type is already complete (for
201 // example, because it was loaded for a different target before),
202 // the definition needs to be imported right away, too.
203 // Type::ResolveClangType() effectively ignores the ResolveState
204 // inside type_sp and only looks at IsDefined(), so it never calls
205 // ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(),
206 // which does extra work for Objective-C classes. This would result
207 // in only the forward declaration to be visible.
208 if (pcm_type.IsDefined())
209 GetClangASTImporter().RequireCompleteType(ClangUtil::GetQualType(type));
211 SymbolFileDWARF *dwarf = die.GetDWARF();
212 TypeSP type_sp(new Type(
213 die.GetID(), dwarf, pcm_type_sp->GetName(), pcm_type_sp->GetByteSize(),
214 nullptr, LLDB_INVALID_UID, Type::eEncodingInvalid,
215 &pcm_type_sp->GetDeclaration(), type, Type::ResolveState::Forward,
216 TypePayloadClang(GetOwningClangModule(die))));
218 dwarf->GetTypeList().Insert(type_sp);
219 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
220 clang::TagDecl *tag_decl = TypeSystemClang::GetAsTagDecl(type);
222 LinkDeclContextToDIE(tag_decl, die);
224 clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(die);
226 LinkDeclContextToDIE(defn_decl_ctx, die);
232 static void CompleteExternalTagDeclType(TypeSystemClang &ast,
233 ClangASTImporter &ast_importer,
234 clang::DeclContext *decl_ctx,
236 const char *type_name_cstr) {
237 auto *tag_decl_ctx = clang::dyn_cast<clang::TagDecl>(decl_ctx);
241 // If this type was not imported from an external AST, there's nothing to do.
242 CompilerType type = ast.GetTypeForDecl(tag_decl_ctx);
243 if (!type || !ast_importer.CanImport(type))
246 auto qual_type = ClangUtil::GetQualType(type);
247 if (!ast_importer.RequireCompleteType(qual_type)) {
248 die.GetDWARF()->GetObjectFile()->GetModule()->ReportError(
249 "Unable to complete the Decl context for DIE '%s' at offset "
250 "0x%8.8x.\nPlease file a bug report.",
251 type_name_cstr ? type_name_cstr : "", die.GetOffset());
252 // We need to make the type look complete otherwise, we might crash in
253 // Clang when adding children.
254 if (TypeSystemClang::StartTagDeclarationDefinition(type))
255 TypeSystemClang::CompleteTagDeclarationDefinition(type);
259 ParsedDWARFTypeAttributes::ParsedDWARFTypeAttributes(const DWARFDIE &die) {
260 DWARFAttributes attributes;
261 size_t num_attributes = die.GetAttributes(attributes);
262 for (size_t i = 0; i < num_attributes; ++i) {
263 dw_attr_t attr = attributes.AttributeAtIndex(i);
264 DWARFFormValue form_value;
265 if (!attributes.ExtractFormValueAtIndex(i, form_value))
268 case DW_AT_abstract_origin:
269 abstract_origin = form_value;
272 case DW_AT_accessibility:
273 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
276 case DW_AT_artificial:
277 is_artificial = form_value.Boolean();
280 case DW_AT_bit_stride:
281 bit_stride = form_value.Unsigned();
284 case DW_AT_byte_size:
285 byte_size = form_value.Unsigned();
288 case DW_AT_byte_stride:
289 byte_stride = form_value.Unsigned();
292 case DW_AT_calling_convention:
293 calling_convention = form_value.Unsigned();
296 case DW_AT_containing_type:
297 containing_type = form_value;
300 case DW_AT_decl_file:
301 decl.SetFile(die.GetCU()->GetFile(form_value.Unsigned()));
303 case DW_AT_decl_line:
304 decl.SetLine(form_value.Unsigned());
306 case DW_AT_decl_column:
307 decl.SetColumn(form_value.Unsigned());
310 case DW_AT_declaration:
311 is_forward_declaration = form_value.Boolean();
315 encoding = form_value.Unsigned();
318 case DW_AT_enum_class:
319 is_scoped_enum = form_value.Boolean();
323 is_explicit = form_value.Boolean();
327 if (form_value.Unsigned())
328 storage = clang::SC_Extern;
332 is_inline = form_value.Boolean();
335 case DW_AT_linkage_name:
336 case DW_AT_MIPS_linkage_name:
337 mangled_name = form_value.AsCString();
341 name.SetCString(form_value.AsCString());
344 case DW_AT_object_pointer:
345 object_pointer = form_value.Reference();
348 case DW_AT_signature:
349 signature = form_value;
352 case DW_AT_specification:
353 specification = form_value;
360 case DW_AT_virtuality:
361 is_virtual = form_value.Boolean();
364 case DW_AT_APPLE_objc_complete_type:
365 is_complete_objc_class = form_value.Signed();
368 case DW_AT_APPLE_objc_direct:
369 is_objc_direct_call = true;
372 case DW_AT_APPLE_runtime_class:
373 class_language = (LanguageType)form_value.Signed();
376 case DW_AT_GNU_vector:
377 is_vector = form_value.Boolean();
379 case DW_AT_export_symbols:
380 exports_symbols = form_value.Boolean();
386 static std::string GetUnitName(const DWARFDIE &die) {
387 if (DWARFUnit *unit = die.GetCU())
388 return unit->GetAbsolutePath().GetPath();
389 return "<missing DWARF unit path>";
392 TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
394 bool *type_is_new_ptr) {
396 *type_is_new_ptr = false;
401 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
404 SymbolFileDWARF *dwarf = die.GetDWARF();
406 DWARFDIE context_die;
407 clang::DeclContext *context =
408 GetClangDeclContextContainingDIE(die, &context_die);
410 dwarf->GetObjectFile()->GetModule()->LogMessage(
412 "DWARFASTParserClang::ParseTypeFromDWARF "
413 "(die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
414 die.GetOffset(), static_cast<void *>(context), context_die.GetOffset(),
415 die.GetTagAsCString(), die.GetName());
418 Type *type_ptr = dwarf->GetDIEToType().lookup(die.GetDIE());
419 if (type_ptr == DIE_IS_BEING_PARSED)
422 return type_ptr->shared_from_this();
423 // Set a bit that lets us know that we are currently parsing this
424 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
426 ParsedDWARFTypeAttributes attrs(die);
428 if (DWARFDIE signature_die = attrs.signature.Reference()) {
430 ParseTypeFromDWARF(sc, signature_die, type_is_new_ptr)) {
431 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
432 if (clang::DeclContext *decl_ctx =
433 GetCachedClangDeclContextForDIE(signature_die))
434 LinkDeclContextToDIE(decl_ctx, die);
441 *type_is_new_ptr = true;
443 const dw_tag_t tag = die.Tag();
449 case DW_TAG_base_type:
450 case DW_TAG_pointer_type:
451 case DW_TAG_reference_type:
452 case DW_TAG_rvalue_reference_type:
453 case DW_TAG_const_type:
454 case DW_TAG_restrict_type:
455 case DW_TAG_volatile_type:
456 case DW_TAG_atomic_type:
457 case DW_TAG_unspecified_type: {
458 type_sp = ParseTypeModifier(sc, die, attrs);
462 case DW_TAG_structure_type:
463 case DW_TAG_union_type:
464 case DW_TAG_class_type: {
465 type_sp = ParseStructureLikeDIE(sc, die, attrs);
469 case DW_TAG_enumeration_type: {
470 type_sp = ParseEnum(sc, die, attrs);
474 case DW_TAG_inlined_subroutine:
475 case DW_TAG_subprogram:
476 case DW_TAG_subroutine_type: {
477 type_sp = ParseSubroutine(die, attrs);
480 case DW_TAG_array_type: {
481 type_sp = ParseArrayType(die, attrs);
484 case DW_TAG_ptr_to_member_type: {
485 type_sp = ParsePointerToMemberType(die, attrs);
489 dwarf->GetObjectFile()->GetModule()->ReportError(
490 "{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and "
491 "attach the file at the start of this error message",
492 die.GetOffset(), tag, DW_TAG_value_to_name(tag));
496 // TODO: We should consider making the switch above exhaustive to simplify
497 // control flow in ParseTypeFromDWARF. Then, we could simply replace this
498 // return statement with a call to llvm_unreachable.
499 return UpdateSymbolContextScopeForType(sc, die, type_sp);
503 DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
505 ParsedDWARFTypeAttributes &attrs) {
506 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
508 SymbolFileDWARF *dwarf = die.GetDWARF();
509 const dw_tag_t tag = die.Tag();
510 LanguageType cu_language = SymbolFileDWARF::GetLanguage(*die.GetCU());
511 Type::ResolveState resolve_state = Type::ResolveState::Unresolved;
512 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
514 CompilerType clang_type;
516 if (tag == DW_TAG_typedef && attrs.type.IsValid()) {
517 // Try to parse a typedef from the (DWARF embedded in the) Clang
518 // module file first as modules can contain typedef'ed
519 // structures that have no names like:
521 // typedef struct { int a; } Foo;
523 // In this case we will have a structure with no name and a
524 // typedef named "Foo" that points to this unnamed
525 // structure. The name in the typedef is the only identifier for
526 // the struct, so always try to get typedefs from Clang modules
529 // The type_sp returned will be empty if the typedef doesn't
530 // exist in a module file, so it is cheap to call this function
533 // If we don't do this we end up creating a TypeSP that says
534 // this is a typedef to type 0x123 (the DW_AT_type value would
535 // be 0x123 in the DW_TAG_typedef), and this is the unnamed
536 // structure type. We will have a hard time tracking down an
537 // unnammed structure type in the module debug info, so we make
538 // sure we don't get into this situation by always resolving
539 // typedefs from the module.
540 const DWARFDIE encoding_die = attrs.type.Reference();
542 // First make sure that the die that this is typedef'ed to _is_
543 // just a declaration (DW_AT_declaration == 1), not a full
544 // definition since template types can't be represented in
545 // modules since only concrete instances of templates are ever
546 // emitted and modules won't contain those
548 encoding_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
549 type_sp = ParseTypeFromClangModule(sc, die, log);
555 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", die.GetID(),
556 DW_TAG_value_to_name(tag), type_name_cstr,
557 encoding_uid.Reference());
563 case DW_TAG_unspecified_type:
564 if (attrs.name == "nullptr_t" || attrs.name == "decltype(nullptr)") {
565 resolve_state = Type::ResolveState::Full;
566 clang_type = m_ast.GetBasicType(eBasicTypeNullPtr);
569 // Fall through to base type below in case we can handle the type
573 case DW_TAG_base_type:
574 resolve_state = Type::ResolveState::Full;
575 clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
576 attrs.name.GetStringRef(), attrs.encoding,
577 attrs.byte_size.getValueOr(0) * 8);
580 case DW_TAG_pointer_type:
581 encoding_data_type = Type::eEncodingIsPointerUID;
583 case DW_TAG_reference_type:
584 encoding_data_type = Type::eEncodingIsLValueReferenceUID;
586 case DW_TAG_rvalue_reference_type:
587 encoding_data_type = Type::eEncodingIsRValueReferenceUID;
590 encoding_data_type = Type::eEncodingIsTypedefUID;
592 case DW_TAG_const_type:
593 encoding_data_type = Type::eEncodingIsConstUID;
595 case DW_TAG_restrict_type:
596 encoding_data_type = Type::eEncodingIsRestrictUID;
598 case DW_TAG_volatile_type:
599 encoding_data_type = Type::eEncodingIsVolatileUID;
601 case DW_TAG_atomic_type:
602 encoding_data_type = Type::eEncodingIsAtomicUID;
606 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID ||
607 encoding_data_type == Type::eEncodingIsTypedefUID)) {
608 if (tag == DW_TAG_pointer_type) {
609 DWARFDIE target_die = die.GetReferencedDIE(DW_AT_type);
611 if (target_die.GetAttributeValueAsUnsigned(DW_AT_APPLE_block, 0)) {
612 // Blocks have a __FuncPtr inside them which is a pointer to a
613 // function of the proper type.
615 for (DWARFDIE child_die = target_die.GetFirstChild();
616 child_die.IsValid(); child_die = child_die.GetSibling()) {
617 if (!strcmp(child_die.GetAttributeValueAsString(DW_AT_name, ""),
619 DWARFDIE function_pointer_type =
620 child_die.GetReferencedDIE(DW_AT_type);
622 if (function_pointer_type) {
623 DWARFDIE function_type =
624 function_pointer_type.GetReferencedDIE(DW_AT_type);
626 bool function_type_is_new_pointer;
627 TypeSP lldb_function_type_sp = ParseTypeFromDWARF(
628 sc, function_type, &function_type_is_new_pointer);
630 if (lldb_function_type_sp) {
631 clang_type = m_ast.CreateBlockPointerType(
632 lldb_function_type_sp->GetForwardCompilerType());
633 encoding_data_type = Type::eEncodingIsUID;
635 resolve_state = Type::ResolveState::Full;
645 if (cu_language == eLanguageTypeObjC ||
646 cu_language == eLanguageTypeObjC_plus_plus) {
648 if (attrs.name == "id") {
650 dwarf->GetObjectFile()->GetModule()->LogMessage(
652 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
653 "is Objective-C 'id' built-in type.",
654 die.GetOffset(), die.GetTagAsCString(), die.GetName());
655 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
656 encoding_data_type = Type::eEncodingIsUID;
658 resolve_state = Type::ResolveState::Full;
659 } else if (attrs.name == "Class") {
661 dwarf->GetObjectFile()->GetModule()->LogMessage(
663 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
664 "is Objective-C 'Class' built-in type.",
665 die.GetOffset(), die.GetTagAsCString(), die.GetName());
666 clang_type = m_ast.GetBasicType(eBasicTypeObjCClass);
667 encoding_data_type = Type::eEncodingIsUID;
669 resolve_state = Type::ResolveState::Full;
670 } else if (attrs.name == "SEL") {
672 dwarf->GetObjectFile()->GetModule()->LogMessage(
674 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
675 "is Objective-C 'selector' built-in type.",
676 die.GetOffset(), die.GetTagAsCString(), die.GetName());
677 clang_type = m_ast.GetBasicType(eBasicTypeObjCSel);
678 encoding_data_type = Type::eEncodingIsUID;
680 resolve_state = Type::ResolveState::Full;
682 } else if (encoding_data_type == Type::eEncodingIsPointerUID &&
683 attrs.type.IsValid()) {
684 // Clang sometimes erroneously emits id as objc_object*. In that
685 // case we fix up the type to "id".
687 const DWARFDIE encoding_die = attrs.type.Reference();
689 if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type) {
690 llvm::StringRef struct_name = encoding_die.GetName();
691 if (struct_name == "objc_object") {
693 dwarf->GetObjectFile()->GetModule()->LogMessage(
695 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s "
696 "'%s' is 'objc_object*', which we overrode to "
698 die.GetOffset(), die.GetTagAsCString(), die.GetName());
699 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
700 encoding_data_type = Type::eEncodingIsUID;
702 resolve_state = Type::ResolveState::Full;
709 type_sp = std::make_shared<Type>(
710 die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
711 dwarf->GetUID(attrs.type.Reference()), encoding_data_type, &attrs.decl,
712 clang_type, resolve_state, TypePayloadClang(GetOwningClangModule(die)));
714 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
718 TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
720 ParsedDWARFTypeAttributes &attrs) {
721 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
723 SymbolFileDWARF *dwarf = die.GetDWARF();
724 const dw_tag_t tag = die.Tag();
727 if (attrs.is_forward_declaration) {
728 type_sp = ParseTypeFromClangModule(sc, die, log);
732 DWARFDeclContext die_decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
734 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
737 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
738 if (debug_map_symfile) {
739 // We weren't able to find a full declaration in this DWARF,
740 // see if we have a declaration anywhere else...
741 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
748 dwarf->GetObjectFile()->GetModule()->LogMessage(
750 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
751 "forward declaration, complete type is 0x%8.8" PRIx64,
752 static_cast<void *>(this), die.GetOffset(),
753 DW_TAG_value_to_name(tag), attrs.name.GetCString(),
757 // We found a real definition for this type elsewhere so lets use
758 // it and cache the fact that we found a complete type for this
760 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
761 clang::DeclContext *defn_decl_ctx =
762 GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
764 LinkDeclContextToDIE(defn_decl_ctx, die);
768 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
769 DW_TAG_value_to_name(tag), type_name_cstr);
771 CompilerType enumerator_clang_type;
772 CompilerType clang_type;
773 clang_type.SetCompilerType(
774 &m_ast, dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
776 if (attrs.type.IsValid()) {
777 Type *enumerator_type =
778 dwarf->ResolveTypeUID(attrs.type.Reference(), true);
780 enumerator_clang_type = enumerator_type->GetFullCompilerType();
783 if (!enumerator_clang_type) {
784 if (attrs.byte_size) {
785 enumerator_clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
786 "", DW_ATE_signed, *attrs.byte_size * 8);
788 enumerator_clang_type = m_ast.GetBasicType(eBasicTypeInt);
792 clang_type = m_ast.CreateEnumerationType(
793 attrs.name.GetCString(), GetClangDeclContextContainingDIE(die, nullptr),
794 GetOwningClangModule(die), attrs.decl, enumerator_clang_type,
795 attrs.is_scoped_enum);
797 enumerator_clang_type = m_ast.GetEnumerationIntegerType(clang_type);
800 LinkDeclContextToDIE(TypeSystemClang::GetDeclContextForType(clang_type), die);
802 type_sp = std::make_shared<Type>(
803 die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
804 dwarf->GetUID(attrs.type.Reference()), Type::eEncodingIsUID, &attrs.decl,
805 clang_type, Type::ResolveState::Forward,
806 TypePayloadClang(GetOwningClangModule(die)));
808 if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) {
809 if (die.HasChildren()) {
810 bool is_signed = false;
811 enumerator_clang_type.IsIntegerType(is_signed);
812 ParseChildEnumerators(clang_type, is_signed,
813 type_sp->GetByteSize().getValueOr(0), die);
815 TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
817 dwarf->GetObjectFile()->GetModule()->ReportError(
818 "DWARF DIE at 0x%8.8x named \"%s\" was not able to start its "
819 "definition.\nPlease file a bug and attach the file at the "
820 "start of this error message",
821 die.GetOffset(), attrs.name.GetCString());
826 TypeSP DWARFASTParserClang::ParseSubroutine(const DWARFDIE &die,
827 ParsedDWARFTypeAttributes &attrs) {
828 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
831 SymbolFileDWARF *dwarf = die.GetDWARF();
832 const dw_tag_t tag = die.Tag();
834 bool is_variadic = false;
835 bool is_static = false;
836 bool has_template_params = false;
838 unsigned type_quals = 0;
840 std::string object_pointer_name;
841 if (attrs.object_pointer) {
842 const char *object_pointer_name_cstr = attrs.object_pointer.GetName();
843 if (object_pointer_name_cstr)
844 object_pointer_name = object_pointer_name_cstr;
847 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
848 DW_TAG_value_to_name(tag), type_name_cstr);
850 CompilerType return_clang_type;
851 Type *func_type = NULL;
853 if (attrs.type.IsValid())
854 func_type = dwarf->ResolveTypeUID(attrs.type.Reference(), true);
857 return_clang_type = func_type->GetForwardCompilerType();
859 return_clang_type = m_ast.GetBasicType(eBasicTypeVoid);
861 std::vector<CompilerType> function_param_types;
862 std::vector<clang::ParmVarDecl *> function_param_decls;
864 // Parse the function children for the parameters
866 DWARFDIE decl_ctx_die;
867 clang::DeclContext *containing_decl_ctx =
868 GetClangDeclContextContainingDIE(die, &decl_ctx_die);
869 const clang::Decl::Kind containing_decl_kind =
870 containing_decl_ctx->getDeclKind();
872 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_kind);
873 // Start off static. This will be set to false in
874 // ParseChildParameters(...) if we find a "this" parameters as the
880 if (die.HasChildren()) {
881 bool skip_artificial = true;
882 ParseChildParameters(containing_decl_ctx, die, skip_artificial, is_static,
883 is_variadic, has_template_params,
884 function_param_types, function_param_decls,
888 bool ignore_containing_context = false;
889 // Check for templatized class member functions. If we had any
890 // DW_TAG_template_type_parameter or DW_TAG_template_value_parameter
891 // the DW_TAG_subprogram DIE, then we can't let this become a method in
892 // a class. Why? Because templatized functions are only emitted if one
893 // of the templatized methods is used in the current compile unit and
894 // we will end up with classes that may or may not include these member
895 // functions and this means one class won't match another class
896 // definition and it affects our ability to use a class in the clang
897 // expression parser. So for the greater good, we currently must not
898 // allow any template member functions in a class definition.
899 if (is_cxx_method && has_template_params) {
900 ignore_containing_context = true;
901 is_cxx_method = false;
904 // clang_type will get the function prototype clang type after this
906 CompilerType clang_type = m_ast.CreateFunctionType(
907 return_clang_type, function_param_types.data(),
908 function_param_types.size(), is_variadic, type_quals);
911 bool type_handled = false;
912 if (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) {
913 ObjCLanguage::MethodName objc_method(attrs.name.GetStringRef(), true);
914 if (objc_method.IsValid(true)) {
915 CompilerType class_opaque_type;
916 ConstString class_name(objc_method.GetClassName());
918 TypeSP complete_objc_class_type_sp(
919 dwarf->FindCompleteObjCDefinitionTypeForDIE(DWARFDIE(),
922 if (complete_objc_class_type_sp) {
923 CompilerType type_clang_forward_type =
924 complete_objc_class_type_sp->GetForwardCompilerType();
925 if (TypeSystemClang::IsObjCObjectOrInterfaceType(
926 type_clang_forward_type))
927 class_opaque_type = type_clang_forward_type;
931 if (class_opaque_type) {
932 // If accessibility isn't set to anything valid, assume public
934 if (attrs.accessibility == eAccessNone)
935 attrs.accessibility = eAccessPublic;
937 clang::ObjCMethodDecl *objc_method_decl =
938 m_ast.AddMethodToObjCObjectType(
939 class_opaque_type, attrs.name.GetCString(), clang_type,
940 attrs.accessibility, attrs.is_artificial, is_variadic,
941 attrs.is_objc_direct_call);
942 type_handled = objc_method_decl != NULL;
944 LinkDeclContextToDIE(objc_method_decl, die);
945 m_ast.SetMetadataAsUserID(objc_method_decl, die.GetID());
947 dwarf->GetObjectFile()->GetModule()->ReportError(
948 "{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), "
949 "please file a bug and attach the file at the start of "
950 "this error message",
951 die.GetOffset(), tag, DW_TAG_value_to_name(tag));
954 } else if (is_cxx_method) {
955 // Look at the parent of this DIE and see if is is a class or
956 // struct and see if this is actually a C++ method
957 Type *class_type = dwarf->ResolveType(decl_ctx_die);
959 bool alternate_defn = false;
960 if (class_type->GetID() != decl_ctx_die.GetID() ||
961 IsClangModuleFwdDecl(decl_ctx_die)) {
962 alternate_defn = true;
964 // We uniqued the parent class of this function to another
965 // class so we now need to associate all dies under
966 // "decl_ctx_die" to DIEs in the DIE for "class_type"...
967 DWARFDIE class_type_die = dwarf->GetDIE(class_type->GetID());
969 if (class_type_die) {
970 std::vector<DWARFDIE> failures;
972 CopyUniqueClassMethodTypes(decl_ctx_die, class_type_die,
973 class_type, failures);
975 // FIXME do something with these failures that's
976 // smarter than just dropping them on the ground.
977 // Unfortunately classes don't like having stuff added
978 // to them after their definitions are complete...
980 Type *type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
981 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
982 return type_ptr->shared_from_this();
987 if (attrs.specification.IsValid()) {
988 // We have a specification which we are going to base our
989 // function prototype off of, so we need this type to be
990 // completed so that the m_die_to_decl_ctx for the method in
991 // the specification has a valid clang decl context.
992 class_type->GetForwardCompilerType();
993 // If we have a specification, then the function type should
994 // have been made with the specification and not with this
996 DWARFDIE spec_die = attrs.specification.Reference();
997 clang::DeclContext *spec_clang_decl_ctx =
998 GetClangDeclContextForDIE(spec_die);
999 if (spec_clang_decl_ctx) {
1000 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
1002 dwarf->GetObjectFile()->GetModule()->ReportWarning(
1003 "0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x"
1005 die.GetID(), spec_die.GetOffset());
1007 type_handled = true;
1008 } else if (attrs.abstract_origin.IsValid()) {
1009 // We have a specification which we are going to base our
1010 // function prototype off of, so we need this type to be
1011 // completed so that the m_die_to_decl_ctx for the method in
1012 // the abstract origin has a valid clang decl context.
1013 class_type->GetForwardCompilerType();
1015 DWARFDIE abs_die = attrs.abstract_origin.Reference();
1016 clang::DeclContext *abs_clang_decl_ctx =
1017 GetClangDeclContextForDIE(abs_die);
1018 if (abs_clang_decl_ctx) {
1019 LinkDeclContextToDIE(abs_clang_decl_ctx, die);
1021 dwarf->GetObjectFile()->GetModule()->ReportWarning(
1022 "0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x"
1024 die.GetID(), abs_die.GetOffset());
1026 type_handled = true;
1028 CompilerType class_opaque_type =
1029 class_type->GetForwardCompilerType();
1030 if (TypeSystemClang::IsCXXClassType(class_opaque_type)) {
1031 if (class_opaque_type.IsBeingDefined() || alternate_defn) {
1032 if (!is_static && !die.HasChildren()) {
1033 // We have a C++ member function with no children (this
1034 // pointer!) and clang will get mad if we try and make
1035 // a function that isn't well formed in the DWARF, so
1036 // we will just skip it...
1037 type_handled = true;
1039 bool add_method = true;
1040 if (alternate_defn) {
1041 // If an alternate definition for the class exists,
1042 // then add the method only if an equivalent is not
1044 clang::CXXRecordDecl *record_decl =
1045 m_ast.GetAsCXXRecordDecl(
1046 class_opaque_type.GetOpaqueQualType());
1048 for (auto method_iter = record_decl->method_begin();
1049 method_iter != record_decl->method_end();
1051 clang::CXXMethodDecl *method_decl = *method_iter;
1052 if (method_decl->getNameInfo().getAsString() ==
1053 attrs.name.GetStringRef()) {
1054 if (method_decl->getType() ==
1055 ClangUtil::GetQualType(clang_type)) {
1057 LinkDeclContextToDIE(method_decl, die);
1058 type_handled = true;
1068 llvm::PrettyStackTraceFormat stack_trace(
1069 "SymbolFileDWARF::ParseType() is adding a method "
1070 "%s to class %s in DIE 0x%8.8" PRIx64 " from %s",
1071 attrs.name.GetCString(),
1072 class_type->GetName().GetCString(), die.GetID(),
1073 dwarf->GetObjectFile()
1078 const bool is_attr_used = false;
1079 // Neither GCC 4.2 nor clang++ currently set a valid
1080 // accessibility in the DWARF for C++ methods...
1081 // Default to public for now...
1082 if (attrs.accessibility == eAccessNone)
1083 attrs.accessibility = eAccessPublic;
1085 clang::CXXMethodDecl *cxx_method_decl =
1086 m_ast.AddMethodToCXXRecordType(
1087 class_opaque_type.GetOpaqueQualType(),
1088 attrs.name.GetCString(), attrs.mangled_name,
1089 clang_type, attrs.accessibility, attrs.is_virtual,
1090 is_static, attrs.is_inline, attrs.is_explicit,
1091 is_attr_used, attrs.is_artificial);
1093 type_handled = cxx_method_decl != NULL;
1094 // Artificial methods are always handled even when we
1095 // don't create a new declaration for them.
1096 type_handled |= attrs.is_artificial;
1098 if (cxx_method_decl) {
1099 LinkDeclContextToDIE(cxx_method_decl, die);
1101 ClangASTMetadata metadata;
1102 metadata.SetUserID(die.GetID());
1104 if (!object_pointer_name.empty()) {
1105 metadata.SetObjectPtrName(
1106 object_pointer_name.c_str());
1108 "Setting object pointer name: %s on method "
1110 object_pointer_name.c_str(),
1111 static_cast<void *>(cxx_method_decl));
1113 m_ast.SetMetadata(cxx_method_decl, metadata);
1115 ignore_containing_context = true;
1120 // We were asked to parse the type for a method in a
1121 // class, yet the class hasn't been asked to complete
1122 // itself through the clang::ExternalASTSource protocol,
1123 // so we need to just have the class complete itself and
1124 // do things the right way, then our
1125 // DIE should then have an entry in the
1126 // dwarf->GetDIEToType() map. First
1127 // we need to modify the dwarf->GetDIEToType() so it
1128 // doesn't think we are trying to parse this DIE
1130 dwarf->GetDIEToType()[die.GetDIE()] = NULL;
1132 // Now we get the full type to force our class type to
1133 // complete itself using the clang::ExternalASTSource
1134 // protocol which will parse all base classes and all
1135 // methods (including the method for this DIE).
1136 class_type->GetFullCompilerType();
1138 // The type for this DIE should have been filled in the
1139 // function call above
1140 Type *type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1141 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
1142 return type_ptr->shared_from_this();
1145 // FIXME This is fixing some even uglier behavior but we
1147 // uniq the methods of each class as well as the class
1148 // itself. <rdar://problem/11240464>
1149 type_handled = true;
1157 if (!type_handled) {
1158 clang::FunctionDecl *function_decl = nullptr;
1159 clang::FunctionDecl *template_function_decl = nullptr;
1161 if (attrs.abstract_origin.IsValid()) {
1162 DWARFDIE abs_die = attrs.abstract_origin.Reference();
1164 if (dwarf->ResolveType(abs_die)) {
1165 function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(
1166 GetCachedClangDeclContextForDIE(abs_die));
1168 if (function_decl) {
1169 LinkDeclContextToDIE(function_decl, die);
1174 if (!function_decl) {
1175 const char *name = attrs.name.GetCString();
1177 // We currently generate function templates with template parameters in
1178 // their name. In order to get closer to the AST that clang generates
1179 // we want to strip these from the name when creating the AST.
1180 if (attrs.mangled_name) {
1181 llvm::ItaniumPartialDemangler D;
1182 if (!D.partialDemangle(attrs.mangled_name))
1183 name = D.getFunctionBaseName(nullptr, nullptr);
1186 // We just have a function that isn't part of a class
1187 function_decl = m_ast.CreateFunctionDeclaration(
1188 ignore_containing_context ? m_ast.GetTranslationUnitDecl()
1189 : containing_decl_ctx,
1190 GetOwningClangModule(die), name, clang_type, attrs.storage,
1193 if (has_template_params) {
1194 TypeSystemClang::TemplateParameterInfos template_param_infos;
1195 ParseTemplateParameterInfos(die, template_param_infos);
1196 template_function_decl = m_ast.CreateFunctionDeclaration(
1197 ignore_containing_context ? m_ast.GetTranslationUnitDecl()
1198 : containing_decl_ctx,
1199 GetOwningClangModule(die), attrs.name.GetCString(), clang_type,
1200 attrs.storage, attrs.is_inline);
1201 clang::FunctionTemplateDecl *func_template_decl =
1202 m_ast.CreateFunctionTemplateDecl(
1203 containing_decl_ctx, GetOwningClangModule(die),
1204 template_function_decl, name, template_param_infos);
1205 m_ast.CreateFunctionTemplateSpecializationInfo(
1206 template_function_decl, func_template_decl, template_param_infos);
1209 lldbassert(function_decl);
1211 if (function_decl) {
1212 LinkDeclContextToDIE(function_decl, die);
1214 if (!function_param_decls.empty()) {
1215 m_ast.SetFunctionParameters(function_decl,
1216 &function_param_decls.front(),
1217 function_param_decls.size());
1218 if (template_function_decl)
1219 m_ast.SetFunctionParameters(template_function_decl,
1220 &function_param_decls.front(),
1221 function_param_decls.size());
1224 ClangASTMetadata metadata;
1225 metadata.SetUserID(die.GetID());
1227 if (!object_pointer_name.empty()) {
1228 metadata.SetObjectPtrName(object_pointer_name.c_str());
1230 "Setting object pointer name: %s on function "
1232 object_pointer_name.c_str(),
1233 static_cast<void *>(function_decl));
1235 m_ast.SetMetadata(function_decl, metadata);
1240 return std::make_shared<Type>(
1241 die.GetID(), dwarf, attrs.name, llvm::None, nullptr, LLDB_INVALID_UID,
1242 Type::eEncodingIsUID, &attrs.decl, clang_type, Type::ResolveState::Full);
1245 TypeSP DWARFASTParserClang::ParseArrayType(const DWARFDIE &die,
1246 ParsedDWARFTypeAttributes &attrs) {
1247 SymbolFileDWARF *dwarf = die.GetDWARF();
1249 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1250 DW_TAG_value_to_name(tag), type_name_cstr);
1252 DWARFDIE type_die = attrs.type.Reference();
1253 Type *element_type = dwarf->ResolveTypeUID(type_die, true);
1258 llvm::Optional<SymbolFile::ArrayInfo> array_info = ParseChildArrayInfo(die);
1260 attrs.byte_stride = array_info->byte_stride;
1261 attrs.bit_stride = array_info->bit_stride;
1263 if (attrs.byte_stride == 0 && attrs.bit_stride == 0)
1264 attrs.byte_stride = element_type->GetByteSize().getValueOr(0);
1265 CompilerType array_element_type = element_type->GetForwardCompilerType();
1266 CompleteType(array_element_type);
1268 uint64_t array_element_bit_stride =
1269 attrs.byte_stride * 8 + attrs.bit_stride;
1270 CompilerType clang_type;
1271 if (array_info && array_info->element_orders.size() > 0) {
1272 uint64_t num_elements = 0;
1273 auto end = array_info->element_orders.rend();
1274 for (auto pos = array_info->element_orders.rbegin(); pos != end; ++pos) {
1275 num_elements = *pos;
1276 clang_type = m_ast.CreateArrayType(array_element_type, num_elements,
1278 array_element_type = clang_type;
1279 array_element_bit_stride = num_elements
1280 ? array_element_bit_stride * num_elements
1281 : array_element_bit_stride;
1285 m_ast.CreateArrayType(array_element_type, 0, attrs.is_vector);
1287 ConstString empty_name;
1288 TypeSP type_sp = std::make_shared<Type>(
1289 die.GetID(), dwarf, empty_name, array_element_bit_stride / 8, nullptr,
1290 dwarf->GetUID(type_die), Type::eEncodingIsUID, &attrs.decl, clang_type,
1291 Type::ResolveState::Full);
1292 type_sp->SetEncodingType(element_type);
1293 const clang::Type *type = ClangUtil::GetQualType(clang_type).getTypePtr();
1294 m_ast.SetMetadataAsUserID(type, die.GetID());
1298 TypeSP DWARFASTParserClang::ParsePointerToMemberType(
1299 const DWARFDIE &die, const ParsedDWARFTypeAttributes &attrs) {
1300 SymbolFileDWARF *dwarf = die.GetDWARF();
1301 Type *pointee_type = dwarf->ResolveTypeUID(attrs.type.Reference(), true);
1303 dwarf->ResolveTypeUID(attrs.containing_type.Reference(), true);
1305 CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType();
1306 CompilerType class_clang_type = class_type->GetLayoutCompilerType();
1308 CompilerType clang_type = TypeSystemClang::CreateMemberPointerType(
1309 class_clang_type, pointee_clang_type);
1311 if (llvm::Optional<uint64_t> clang_type_size =
1312 clang_type.GetByteSize(nullptr)) {
1313 return std::make_shared<Type>(die.GetID(), dwarf, attrs.name,
1314 *clang_type_size, nullptr, LLDB_INVALID_UID,
1315 Type::eEncodingIsUID, nullptr, clang_type,
1316 Type::ResolveState::Forward);
1321 void DWARFASTParserClang::CompleteType(CompilerType type) {
1322 // Technically, enums can be incomplete too, but we don't handle those as they
1323 // are emitted even under -flimit-debug-info.
1324 if (!TypeSystemClang::IsCXXClassType(type))
1327 if (type.GetCompleteType())
1330 // No complete definition in this module. Mark the class as complete to
1331 // satisfy local ast invariants, but make a note of the fact that
1332 // it is not _really_ complete so we can later search for a definition in a
1333 // different module.
1334 // Since we provide layout assistance, layouts of types containing this class
1335 // will be correct even if we are not able to find the definition elsewhere.
1336 bool started = TypeSystemClang::StartTagDeclarationDefinition(type);
1337 lldbassert(started && "Unable to start a class type definition.");
1338 TypeSystemClang::CompleteTagDeclarationDefinition(type);
1339 const clang::TagDecl *td = ClangUtil::GetAsTagDecl(type);
1340 m_ast.GetMetadata(td)->SetIsForcefullyCompleted();
1343 TypeSP DWARFASTParserClang::UpdateSymbolContextScopeForType(
1344 const SymbolContext &sc, const DWARFDIE &die, TypeSP type_sp) {
1348 SymbolFileDWARF *dwarf = die.GetDWARF();
1349 TypeList &type_list = dwarf->GetTypeList();
1350 DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
1351 dw_tag_t sc_parent_tag = sc_parent_die.Tag();
1353 SymbolContextScope *symbol_context_scope = nullptr;
1354 if (sc_parent_tag == DW_TAG_compile_unit ||
1355 sc_parent_tag == DW_TAG_partial_unit) {
1356 symbol_context_scope = sc.comp_unit;
1357 } else if (sc.function != nullptr && sc_parent_die) {
1358 symbol_context_scope =
1359 sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
1360 if (symbol_context_scope == nullptr)
1361 symbol_context_scope = sc.function;
1363 symbol_context_scope = sc.module_sp.get();
1366 if (symbol_context_scope != nullptr)
1367 type_sp->SetSymbolContextScope(symbol_context_scope);
1369 // We are ready to put this type into the uniqued list up at the module
1371 type_list.Insert(type_sp);
1373 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1378 DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
1379 const DWARFDIE &die,
1380 ParsedDWARFTypeAttributes &attrs) {
1382 CompilerType clang_type;
1383 const dw_tag_t tag = die.Tag();
1384 SymbolFileDWARF *dwarf = die.GetDWARF();
1385 LanguageType cu_language = SymbolFileDWARF::GetLanguage(*die.GetCU());
1386 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_TYPE_COMPLETION |
1389 // UniqueDWARFASTType is large, so don't create a local variables on the
1390 // stack, put it on the heap. This function is often called recursively and
1391 // clang isn't good at sharing the stack space for variables in different
1393 auto unique_ast_entry_up = std::make_unique<UniqueDWARFASTType>();
1395 ConstString unique_typename(attrs.name);
1396 Declaration unique_decl(attrs.decl);
1399 if (Language::LanguageIsCPlusPlus(cu_language)) {
1400 // For C++, we rely solely upon the one definition rule that says
1401 // only one thing can exist at a given decl context. We ignore the
1402 // file and line that things are declared on.
1403 std::string qualified_name;
1404 if (die.GetQualifiedName(qualified_name))
1405 unique_typename = ConstString(qualified_name);
1406 unique_decl.Clear();
1409 if (dwarf->GetUniqueDWARFASTTypeMap().Find(
1410 unique_typename, die, unique_decl, attrs.byte_size.getValueOr(-1),
1411 *unique_ast_entry_up)) {
1412 type_sp = unique_ast_entry_up->m_type_sp;
1414 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1415 LinkDeclContextToDIE(
1416 GetCachedClangDeclContextForDIE(unique_ast_entry_up->m_die), die);
1422 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1423 DW_TAG_value_to_name(tag), type_name_cstr);
1425 int tag_decl_kind = -1;
1426 AccessType default_accessibility = eAccessNone;
1427 if (tag == DW_TAG_structure_type) {
1428 tag_decl_kind = clang::TTK_Struct;
1429 default_accessibility = eAccessPublic;
1430 } else if (tag == DW_TAG_union_type) {
1431 tag_decl_kind = clang::TTK_Union;
1432 default_accessibility = eAccessPublic;
1433 } else if (tag == DW_TAG_class_type) {
1434 tag_decl_kind = clang::TTK_Class;
1435 default_accessibility = eAccessPrivate;
1438 if (attrs.byte_size && *attrs.byte_size == 0 && attrs.name &&
1439 !die.HasChildren() && cu_language == eLanguageTypeObjC) {
1440 // Work around an issue with clang at the moment where forward
1441 // declarations for objective C classes are emitted as:
1442 // DW_TAG_structure_type [2]
1443 // DW_AT_name( "ForwardObjcClass" )
1444 // DW_AT_byte_size( 0x00 )
1445 // DW_AT_decl_file( "..." )
1446 // DW_AT_decl_line( 1 )
1448 // Note that there is no DW_AT_declaration and there are no children,
1449 // and the byte size is zero.
1450 attrs.is_forward_declaration = true;
1453 if (attrs.class_language == eLanguageTypeObjC ||
1454 attrs.class_language == eLanguageTypeObjC_plus_plus) {
1455 if (!attrs.is_complete_objc_class &&
1456 die.Supports_DW_AT_APPLE_objc_complete_type()) {
1457 // We have a valid eSymbolTypeObjCClass class symbol whose name
1458 // matches the current objective C class that we are trying to find
1459 // and this DIE isn't the complete definition (we checked
1460 // is_complete_objc_class above and know it is false), so the real
1461 // definition is in here somewhere
1463 dwarf->FindCompleteObjCDefinitionTypeForDIE(die, attrs.name, true);
1466 SymbolFileDWARFDebugMap *debug_map_symfile =
1467 dwarf->GetDebugMapSymfile();
1468 if (debug_map_symfile) {
1469 // We weren't able to find a full declaration in this DWARF,
1470 // see if we have a declaration anywhere else...
1471 type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE(
1472 die, attrs.name, true);
1478 dwarf->GetObjectFile()->GetModule()->LogMessage(
1480 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an "
1481 "incomplete objc type, complete type is 0x%8.8" PRIx64,
1482 static_cast<void *>(this), die.GetOffset(),
1483 DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1487 // We found a real definition for this type elsewhere so lets use
1488 // it and cache the fact that we found a complete type for this
1490 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1496 if (attrs.is_forward_declaration) {
1497 // We have a forward declaration to a type and we need to try and
1498 // find a full declaration. We look in the current type index just in
1499 // case we have a forward declaration followed by an actual
1500 // declarations in the DWARF. If this fails, we need to look
1503 dwarf->GetObjectFile()->GetModule()->LogMessage(
1505 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
1506 "forward declaration, trying to find complete type",
1507 static_cast<void *>(this), die.GetOffset(), DW_TAG_value_to_name(tag),
1508 attrs.name.GetCString());
1511 // See if the type comes from a Clang module and if so, track down
1513 type_sp = ParseTypeFromClangModule(sc, die, log);
1517 DWARFDeclContext die_decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
1519 // type_sp = FindDefinitionTypeForDIE (dwarf_cu, die,
1520 // type_name_const_str);
1521 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
1524 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
1525 if (debug_map_symfile) {
1526 // We weren't able to find a full declaration in this DWARF, see
1527 // if we have a declaration anywhere else...
1528 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
1535 dwarf->GetObjectFile()->GetModule()->LogMessage(
1537 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
1538 "forward declaration, complete type is 0x%8.8" PRIx64,
1539 static_cast<void *>(this), die.GetOffset(),
1540 DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1544 // We found a real definition for this type elsewhere so lets use
1545 // it and cache the fact that we found a complete type for this die
1546 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1547 clang::DeclContext *defn_decl_ctx =
1548 GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
1550 LinkDeclContextToDIE(defn_decl_ctx, die);
1554 assert(tag_decl_kind != -1);
1555 bool clang_type_was_created = false;
1556 clang_type.SetCompilerType(
1557 &m_ast, dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
1559 clang::DeclContext *decl_ctx =
1560 GetClangDeclContextContainingDIE(die, nullptr);
1562 // If your decl context is a record that was imported from another
1563 // AST context (in the gmodules case), we need to make sure the type
1564 // backing the Decl is complete before adding children to it. This is
1565 // not an issue in the non-gmodules case because the debug info will
1566 // always contain a full definition of parent types in that case.
1567 CompleteExternalTagDeclType(m_ast, GetClangASTImporter(), decl_ctx, die,
1568 attrs.name.GetCString());
1570 if (attrs.accessibility == eAccessNone && decl_ctx) {
1571 // Check the decl context that contains this class/struct/union. If
1572 // it is a class we must give it an accessibility.
1573 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
1574 if (DeclKindIsCXXClass(containing_decl_kind))
1575 attrs.accessibility = default_accessibility;
1578 ClangASTMetadata metadata;
1579 metadata.SetUserID(die.GetID());
1580 metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual(die));
1582 if (attrs.name.GetStringRef().contains('<')) {
1583 TypeSystemClang::TemplateParameterInfos template_param_infos;
1584 if (ParseTemplateParameterInfos(die, template_param_infos)) {
1585 clang::ClassTemplateDecl *class_template_decl =
1586 m_ast.ParseClassTemplateDecl(
1587 decl_ctx, GetOwningClangModule(die), attrs.accessibility,
1588 attrs.name.GetCString(), tag_decl_kind, template_param_infos);
1589 if (!class_template_decl) {
1591 dwarf->GetObjectFile()->GetModule()->LogMessage(
1593 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" "
1594 "clang::ClassTemplateDecl failed to return a decl.",
1595 static_cast<void *>(this), die.GetOffset(),
1596 DW_TAG_value_to_name(tag), attrs.name.GetCString());
1601 clang::ClassTemplateSpecializationDecl *class_specialization_decl =
1602 m_ast.CreateClassTemplateSpecializationDecl(
1603 decl_ctx, GetOwningClangModule(die), class_template_decl,
1604 tag_decl_kind, template_param_infos);
1605 clang_type = m_ast.CreateClassTemplateSpecializationType(
1606 class_specialization_decl);
1607 clang_type_was_created = true;
1609 m_ast.SetMetadata(class_template_decl, metadata);
1610 m_ast.SetMetadata(class_specialization_decl, metadata);
1614 if (!clang_type_was_created) {
1615 clang_type_was_created = true;
1616 clang_type = m_ast.CreateRecordType(
1617 decl_ctx, GetOwningClangModule(die), attrs.accessibility,
1618 attrs.name.GetCString(), tag_decl_kind, attrs.class_language,
1619 &metadata, attrs.exports_symbols);
1623 // Store a forward declaration to this class type in case any
1624 // parameters in any class methods need it for the clang types for
1625 // function prototypes.
1626 LinkDeclContextToDIE(m_ast.GetDeclContextForType(clang_type), die);
1627 type_sp = std::make_shared<Type>(
1628 die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
1629 LLDB_INVALID_UID, Type::eEncodingIsUID, &attrs.decl, clang_type,
1630 Type::ResolveState::Forward,
1631 TypePayloadClang(OptionalClangModuleID(), attrs.is_complete_objc_class));
1633 // Add our type to the unique type map so we don't end up creating many
1634 // copies of the same type over and over in the ASTContext for our
1636 unique_ast_entry_up->m_type_sp = type_sp;
1637 unique_ast_entry_up->m_die = die;
1638 unique_ast_entry_up->m_declaration = unique_decl;
1639 unique_ast_entry_up->m_byte_size = attrs.byte_size.getValueOr(0);
1640 dwarf->GetUniqueDWARFASTTypeMap().Insert(unique_typename,
1641 *unique_ast_entry_up);
1643 if (attrs.is_forward_declaration && die.HasChildren()) {
1644 // Check to see if the DIE actually has a definition, some version of
1646 // emit DIEs with DW_AT_declaration set to true, but yet still have
1647 // subprogram, members, or inheritance, so we can't trust it
1648 DWARFDIE child_die = die.GetFirstChild();
1650 switch (child_die.Tag()) {
1651 case DW_TAG_inheritance:
1652 case DW_TAG_subprogram:
1654 case DW_TAG_APPLE_property:
1655 case DW_TAG_class_type:
1656 case DW_TAG_structure_type:
1657 case DW_TAG_enumeration_type:
1658 case DW_TAG_typedef:
1659 case DW_TAG_union_type:
1661 attrs.is_forward_declaration = false;
1664 child_die = child_die.GetSibling();
1670 if (!attrs.is_forward_declaration) {
1671 // Always start the definition for a class type so that if the class
1672 // has child classes or types that require the class to be created
1673 // for use as their decl contexts the class will be ready to accept
1674 // these child definitions.
1675 if (!die.HasChildren()) {
1676 // No children for this struct/union/class, lets finish it
1677 if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) {
1678 TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
1680 dwarf->GetObjectFile()->GetModule()->ReportError(
1681 "DWARF DIE at 0x%8.8x named \"%s\" was not able to start its "
1682 "definition.\nPlease file a bug and attach the file at the "
1683 "start of this error message",
1684 die.GetOffset(), attrs.name.GetCString());
1687 if (tag == DW_TAG_structure_type) // this only applies in C
1689 clang::RecordDecl *record_decl =
1690 TypeSystemClang::GetAsRecordDecl(clang_type);
1693 GetClangASTImporter().SetRecordLayout(
1694 record_decl, ClangASTImporter::LayoutInfo());
1697 } else if (clang_type_was_created) {
1698 // Start the definition if the class is not objective C since the
1699 // underlying decls respond to isCompleteDefinition(). Objective
1700 // C decls don't respond to isCompleteDefinition() so we can't
1701 // start the declaration definition right away. For C++
1702 // class/union/structs we want to start the definition in case the
1703 // class is needed as the declaration context for a contained class
1704 // or type without the need to complete that type..
1706 if (attrs.class_language != eLanguageTypeObjC &&
1707 attrs.class_language != eLanguageTypeObjC_plus_plus)
1708 TypeSystemClang::StartTagDeclarationDefinition(clang_type);
1710 // Leave this as a forward declaration until we need to know the
1711 // details of the type. lldb_private::Type will automatically call
1712 // the SymbolFile virtual function
1713 // "SymbolFileDWARF::CompleteType(Type *)" When the definition
1714 // needs to be defined.
1715 assert(!dwarf->GetForwardDeclClangTypeToDie().count(
1716 ClangUtil::RemoveFastQualifiers(clang_type)
1717 .GetOpaqueQualType()) &&
1718 "Type already in the forward declaration map!");
1719 // Can't assume m_ast.GetSymbolFile() is actually a
1720 // SymbolFileDWARF, it can be a SymbolFileDWARFDebugMap for Apple
1722 dwarf->GetForwardDeclDieToClangType()[die.GetDIE()] =
1723 clang_type.GetOpaqueQualType();
1724 dwarf->GetForwardDeclClangTypeToDie().try_emplace(
1725 ClangUtil::RemoveFastQualifiers(clang_type).GetOpaqueQualType(),
1727 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
1731 // If we made a clang type, set the trivial abi if applicable: We only
1732 // do this for pass by value - which implies the Trivial ABI. There
1733 // isn't a way to assert that something that would normally be pass by
1734 // value is pass by reference, so we ignore that attribute if set.
1735 if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_value) {
1736 clang::CXXRecordDecl *record_decl =
1737 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
1738 if (record_decl && record_decl->getDefinition()) {
1739 record_decl->setHasTrivialSpecialMemberForCall();
1743 if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_reference) {
1744 clang::CXXRecordDecl *record_decl =
1745 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
1747 record_decl->setArgPassingRestrictions(
1748 clang::RecordDecl::APK_CannotPassInRegs);
1753 // DWARF parsing functions
1755 class DWARFASTParserClang::DelayedAddObjCClassProperty {
1757 DelayedAddObjCClassProperty(
1758 const CompilerType &class_opaque_type, const char *property_name,
1759 const CompilerType &property_opaque_type, // The property type is only
1760 // required if you don't have an
1762 clang::ObjCIvarDecl *ivar_decl, const char *property_setter_name,
1763 const char *property_getter_name, uint32_t property_attributes,
1764 const ClangASTMetadata *metadata)
1765 : m_class_opaque_type(class_opaque_type), m_property_name(property_name),
1766 m_property_opaque_type(property_opaque_type), m_ivar_decl(ivar_decl),
1767 m_property_setter_name(property_setter_name),
1768 m_property_getter_name(property_getter_name),
1769 m_property_attributes(property_attributes) {
1770 if (metadata != nullptr) {
1771 m_metadata_up = std::make_unique<ClangASTMetadata>();
1772 *m_metadata_up = *metadata;
1776 DelayedAddObjCClassProperty(const DelayedAddObjCClassProperty &rhs) {
1780 DelayedAddObjCClassProperty &
1781 operator=(const DelayedAddObjCClassProperty &rhs) {
1782 m_class_opaque_type = rhs.m_class_opaque_type;
1783 m_property_name = rhs.m_property_name;
1784 m_property_opaque_type = rhs.m_property_opaque_type;
1785 m_ivar_decl = rhs.m_ivar_decl;
1786 m_property_setter_name = rhs.m_property_setter_name;
1787 m_property_getter_name = rhs.m_property_getter_name;
1788 m_property_attributes = rhs.m_property_attributes;
1790 if (rhs.m_metadata_up) {
1791 m_metadata_up = std::make_unique<ClangASTMetadata>();
1792 *m_metadata_up = *rhs.m_metadata_up;
1798 return TypeSystemClang::AddObjCClassProperty(
1799 m_class_opaque_type, m_property_name, m_property_opaque_type,
1800 m_ivar_decl, m_property_setter_name, m_property_getter_name,
1801 m_property_attributes, m_metadata_up.get());
1805 CompilerType m_class_opaque_type;
1806 const char *m_property_name;
1807 CompilerType m_property_opaque_type;
1808 clang::ObjCIvarDecl *m_ivar_decl;
1809 const char *m_property_setter_name;
1810 const char *m_property_getter_name;
1811 uint32_t m_property_attributes;
1812 std::unique_ptr<ClangASTMetadata> m_metadata_up;
1815 bool DWARFASTParserClang::ParseTemplateDIE(
1816 const DWARFDIE &die,
1817 TypeSystemClang::TemplateParameterInfos &template_param_infos) {
1818 const dw_tag_t tag = die.Tag();
1819 bool is_template_template_argument = false;
1822 case DW_TAG_GNU_template_parameter_pack: {
1823 template_param_infos.packed_args =
1824 std::make_unique<TypeSystemClang::TemplateParameterInfos>();
1825 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
1826 child_die = child_die.GetSibling()) {
1827 if (!ParseTemplateDIE(child_die, *template_param_infos.packed_args))
1830 if (const char *name = die.GetName()) {
1831 template_param_infos.pack_name = name;
1835 case DW_TAG_GNU_template_template_param:
1836 is_template_template_argument = true;
1838 case DW_TAG_template_type_parameter:
1839 case DW_TAG_template_value_parameter: {
1840 DWARFAttributes attributes;
1841 const size_t num_attributes = die.GetAttributes(attributes);
1842 const char *name = nullptr;
1843 const char *template_name = nullptr;
1844 CompilerType clang_type;
1845 uint64_t uval64 = 0;
1846 bool uval64_valid = false;
1847 if (num_attributes > 0) {
1848 DWARFFormValue form_value;
1849 for (size_t i = 0; i < num_attributes; ++i) {
1850 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1854 if (attributes.ExtractFormValueAtIndex(i, form_value))
1855 name = form_value.AsCString();
1858 case DW_AT_GNU_template_name:
1859 if (attributes.ExtractFormValueAtIndex(i, form_value))
1860 template_name = form_value.AsCString();
1864 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1865 Type *lldb_type = die.ResolveTypeUID(form_value.Reference());
1867 clang_type = lldb_type->GetForwardCompilerType();
1871 case DW_AT_const_value:
1872 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1873 uval64_valid = true;
1874 uval64 = form_value.Unsigned();
1882 clang::ASTContext &ast = m_ast.getASTContext();
1884 clang_type = m_ast.GetBasicType(eBasicTypeVoid);
1886 if (!is_template_template_argument) {
1887 bool is_signed = false;
1888 if (name && name[0])
1889 template_param_infos.names.push_back(name);
1891 template_param_infos.names.push_back(NULL);
1893 // Get the signed value for any integer or enumeration if available
1894 clang_type.IsIntegerOrEnumerationType(is_signed);
1896 if (tag == DW_TAG_template_value_parameter && uval64_valid) {
1897 llvm::Optional<uint64_t> size = clang_type.GetBitSize(nullptr);
1900 llvm::APInt apint(*size, uval64, is_signed);
1901 template_param_infos.args.push_back(
1902 clang::TemplateArgument(ast, llvm::APSInt(apint, !is_signed),
1903 ClangUtil::GetQualType(clang_type)));
1905 template_param_infos.args.push_back(
1906 clang::TemplateArgument(ClangUtil::GetQualType(clang_type)));
1909 auto *tplt_type = m_ast.CreateTemplateTemplateParmDecl(template_name);
1910 template_param_infos.names.push_back(name);
1911 template_param_infos.args.push_back(
1912 clang::TemplateArgument(clang::TemplateName(tplt_type)));
1924 bool DWARFASTParserClang::ParseTemplateParameterInfos(
1925 const DWARFDIE &parent_die,
1926 TypeSystemClang::TemplateParameterInfos &template_param_infos) {
1931 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
1932 die = die.GetSibling()) {
1933 const dw_tag_t tag = die.Tag();
1936 case DW_TAG_template_type_parameter:
1937 case DW_TAG_template_value_parameter:
1938 case DW_TAG_GNU_template_parameter_pack:
1939 case DW_TAG_GNU_template_template_param:
1940 ParseTemplateDIE(die, template_param_infos);
1947 if (template_param_infos.args.empty())
1949 return template_param_infos.args.size() == template_param_infos.names.size();
1952 bool DWARFASTParserClang::CompleteRecordType(const DWARFDIE &die,
1953 lldb_private::Type *type,
1954 CompilerType &clang_type) {
1955 const dw_tag_t tag = die.Tag();
1956 SymbolFileDWARF *dwarf = die.GetDWARF();
1958 ClangASTImporter::LayoutInfo layout_info;
1960 if (die.HasChildren()) {
1961 const bool type_is_objc_object_or_interface =
1962 TypeSystemClang::IsObjCObjectOrInterfaceType(clang_type);
1963 if (type_is_objc_object_or_interface) {
1964 // For objective C we don't start the definition when the class is
1966 TypeSystemClang::StartTagDeclarationDefinition(clang_type);
1969 int tag_decl_kind = -1;
1970 AccessType default_accessibility = eAccessNone;
1971 if (tag == DW_TAG_structure_type) {
1972 tag_decl_kind = clang::TTK_Struct;
1973 default_accessibility = eAccessPublic;
1974 } else if (tag == DW_TAG_union_type) {
1975 tag_decl_kind = clang::TTK_Union;
1976 default_accessibility = eAccessPublic;
1977 } else if (tag == DW_TAG_class_type) {
1978 tag_decl_kind = clang::TTK_Class;
1979 default_accessibility = eAccessPrivate;
1982 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases;
1983 std::vector<int> member_accessibilities;
1984 bool is_a_class = false;
1985 // Parse members and base classes first
1986 std::vector<DWARFDIE> member_function_dies;
1988 DelayedPropertyList delayed_properties;
1989 ParseChildMembers(die, clang_type, bases, member_accessibilities,
1990 member_function_dies, delayed_properties,
1991 default_accessibility, is_a_class, layout_info);
1993 // Now parse any methods if there were any...
1994 for (const DWARFDIE &die : member_function_dies)
1995 dwarf->ResolveType(die);
1997 if (type_is_objc_object_or_interface) {
1998 ConstString class_name(clang_type.GetTypeName());
2000 dwarf->GetObjCMethods(class_name, [&](DWARFDIE method_die) {
2001 method_die.ResolveType();
2005 for (DelayedPropertyList::iterator pi = delayed_properties.begin(),
2006 pe = delayed_properties.end();
2012 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2013 // need to tell the clang type it is actually a class.
2014 if (!type_is_objc_object_or_interface) {
2015 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2016 m_ast.SetTagTypeKind(ClangUtil::GetQualType(clang_type),
2020 // Since DW_TAG_structure_type gets used for both classes and
2021 // structures, we may need to set any DW_TAG_member fields to have a
2022 // "private" access if none was specified. When we parsed the child
2023 // members we tracked that actual accessibility value for each
2024 // DW_TAG_member in the "member_accessibilities" array. If the value
2025 // for the member is zero, then it was set to the
2026 // "default_accessibility" which for structs was "public". Below we
2027 // correct this by setting any fields to "private" that weren't
2029 if (is_a_class && !member_accessibilities.empty()) {
2030 // This is a class and all members that didn't have their access
2031 // specified are private.
2032 m_ast.SetDefaultAccessForRecordFields(
2033 m_ast.GetAsRecordDecl(clang_type), eAccessPrivate,
2034 &member_accessibilities.front(), member_accessibilities.size());
2037 if (!bases.empty()) {
2038 // Make sure all base classes refer to complete types and not forward
2039 // declarations. If we don't do this, clang will crash with an
2040 // assertion in the call to clang_type.TransferBaseClasses()
2041 for (const auto &base_class : bases) {
2042 clang::TypeSourceInfo *type_source_info =
2043 base_class->getTypeSourceInfo();
2044 if (type_source_info)
2045 CompleteType(m_ast.GetType(type_source_info->getType()));
2048 m_ast.TransferBaseClasses(clang_type.GetOpaqueQualType(),
2053 m_ast.AddMethodOverridesForCXXRecordType(clang_type.GetOpaqueQualType());
2054 TypeSystemClang::BuildIndirectFields(clang_type);
2055 TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
2057 if (!layout_info.field_offsets.empty() || !layout_info.base_offsets.empty() ||
2058 !layout_info.vbase_offsets.empty()) {
2060 layout_info.bit_size = type->GetByteSize().getValueOr(0) * 8;
2061 if (layout_info.bit_size == 0)
2062 layout_info.bit_size =
2063 die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2065 clang::CXXRecordDecl *record_decl =
2066 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
2068 GetClangASTImporter().SetRecordLayout(record_decl, layout_info);
2071 return (bool)clang_type;
2074 bool DWARFASTParserClang::CompleteEnumType(const DWARFDIE &die,
2075 lldb_private::Type *type,
2076 CompilerType &clang_type) {
2077 if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) {
2078 if (die.HasChildren()) {
2079 bool is_signed = false;
2080 clang_type.IsIntegerType(is_signed);
2081 ParseChildEnumerators(clang_type, is_signed,
2082 type->GetByteSize().getValueOr(0), die);
2084 TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
2086 return (bool)clang_type;
2089 bool DWARFASTParserClang::CompleteTypeFromDWARF(const DWARFDIE &die,
2090 lldb_private::Type *type,
2091 CompilerType &clang_type) {
2092 SymbolFileDWARF *dwarf = die.GetDWARF();
2094 std::lock_guard<std::recursive_mutex> guard(
2095 dwarf->GetObjectFile()->GetModule()->GetMutex());
2097 // Disable external storage for this type so we don't get anymore
2098 // clang::ExternalASTSource queries for this type.
2099 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), false);
2104 const dw_tag_t tag = die.Tag();
2107 nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
2109 dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
2110 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
2111 die.GetID(), die.GetTagAsCString(), type->GetName().AsCString());
2113 DWARFAttributes attributes;
2115 case DW_TAG_structure_type:
2116 case DW_TAG_union_type:
2117 case DW_TAG_class_type:
2118 return CompleteRecordType(die, type, clang_type);
2119 case DW_TAG_enumeration_type:
2120 return CompleteEnumType(die, type, clang_type);
2122 assert(false && "not a forward clang type decl!");
2129 void DWARFASTParserClang::EnsureAllDIEsInDeclContextHaveBeenParsed(
2130 lldb_private::CompilerDeclContext decl_context) {
2131 auto opaque_decl_ctx =
2132 (clang::DeclContext *)decl_context.GetOpaqueDeclContext();
2133 for (auto it = m_decl_ctx_to_die.find(opaque_decl_ctx);
2134 it != m_decl_ctx_to_die.end() && it->first == opaque_decl_ctx;
2135 it = m_decl_ctx_to_die.erase(it))
2136 for (DWARFDIE decl = it->second.GetFirstChild(); decl;
2137 decl = decl.GetSibling())
2138 GetClangDeclForDIE(decl);
2141 CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) {
2142 clang::Decl *clang_decl = GetClangDeclForDIE(die);
2143 if (clang_decl != nullptr)
2144 return m_ast.GetCompilerDecl(clang_decl);
2145 return CompilerDecl();
2149 DWARFASTParserClang::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) {
2150 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die);
2152 return m_ast.CreateDeclContext(clang_decl_ctx);
2153 return CompilerDeclContext();
2157 DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) {
2158 clang::DeclContext *clang_decl_ctx =
2159 GetClangDeclContextContainingDIE(die, nullptr);
2161 return m_ast.CreateDeclContext(clang_decl_ctx);
2162 return CompilerDeclContext();
2165 size_t DWARFASTParserClang::ParseChildEnumerators(
2166 lldb_private::CompilerType &clang_type, bool is_signed,
2167 uint32_t enumerator_byte_size, const DWARFDIE &parent_die) {
2171 size_t enumerators_added = 0;
2173 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
2174 die = die.GetSibling()) {
2175 const dw_tag_t tag = die.Tag();
2176 if (tag == DW_TAG_enumerator) {
2177 DWARFAttributes attributes;
2178 const size_t num_child_attributes = die.GetAttributes(attributes);
2179 if (num_child_attributes > 0) {
2180 const char *name = nullptr;
2181 bool got_value = false;
2182 int64_t enum_value = 0;
2186 for (i = 0; i < num_child_attributes; ++i) {
2187 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2188 DWARFFormValue form_value;
2189 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2191 case DW_AT_const_value:
2194 enum_value = form_value.Signed();
2196 enum_value = form_value.Unsigned();
2200 name = form_value.AsCString();
2203 case DW_AT_description:
2205 case DW_AT_decl_file:
2206 decl.SetFile(die.GetCU()->GetFile(form_value.Unsigned()));
2208 case DW_AT_decl_line:
2209 decl.SetLine(form_value.Unsigned());
2211 case DW_AT_decl_column:
2212 decl.SetColumn(form_value.Unsigned());
2220 if (name && name[0] && got_value) {
2221 m_ast.AddEnumerationValueToEnumerationType(
2222 clang_type, decl, name, enum_value, enumerator_byte_size * 8);
2223 ++enumerators_added;
2228 return enumerators_added;
2231 Function *DWARFASTParserClang::ParseFunctionFromDWARF(CompileUnit &comp_unit,
2232 const DWARFDIE &die) {
2233 DWARFRangeList func_ranges;
2234 const char *name = nullptr;
2235 const char *mangled = nullptr;
2238 int decl_column = 0;
2241 int call_column = 0;
2242 DWARFExpression frame_base;
2244 const dw_tag_t tag = die.Tag();
2246 if (tag != DW_TAG_subprogram)
2249 if (die.GetDIENamesAndRanges(name, mangled, func_ranges, decl_file, decl_line,
2250 decl_column, call_file, call_line, call_column,
2253 // Union of all ranges in the function DIE (if the function is
2255 AddressRange func_range;
2256 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase(0);
2257 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd(0);
2258 if (lowest_func_addr != LLDB_INVALID_ADDRESS &&
2259 lowest_func_addr <= highest_func_addr) {
2260 ModuleSP module_sp(die.GetModule());
2261 func_range.GetBaseAddress().ResolveAddressUsingFileSections(
2262 lowest_func_addr, module_sp->GetSectionList());
2263 if (func_range.GetBaseAddress().IsValid())
2264 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
2267 if (func_range.GetBaseAddress().IsValid()) {
2270 func_name.SetValue(ConstString(mangled), true);
2271 else if ((die.GetParent().Tag() == DW_TAG_compile_unit ||
2272 die.GetParent().Tag() == DW_TAG_partial_unit) &&
2273 Language::LanguageIsCPlusPlus(
2274 SymbolFileDWARF::GetLanguage(*die.GetCU())) &&
2275 !Language::LanguageIsObjC(
2276 SymbolFileDWARF::GetLanguage(*die.GetCU())) &&
2277 name && strcmp(name, "main") != 0) {
2278 // If the mangled name is not present in the DWARF, generate the
2279 // demangled name using the decl context. We skip if the function is
2280 // "main" as its name is never mangled.
2281 bool is_static = false;
2282 bool is_variadic = false;
2283 bool has_template_params = false;
2284 unsigned type_quals = 0;
2285 std::vector<CompilerType> param_types;
2286 std::vector<clang::ParmVarDecl *> param_decls;
2289 DWARFDeclContext decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
2290 sstr << decl_ctx.GetQualifiedName();
2292 clang::DeclContext *containing_decl_ctx =
2293 GetClangDeclContextContainingDIE(die, nullptr);
2294 ParseChildParameters(containing_decl_ctx, die, true, is_static,
2295 is_variadic, has_template_params, param_types,
2296 param_decls, type_quals);
2298 for (size_t i = 0; i < param_types.size(); i++) {
2301 sstr << param_types[i].GetTypeName();
2306 if (type_quals & clang::Qualifiers::Const)
2309 func_name.SetValue(ConstString(sstr.GetString()), false);
2311 func_name.SetValue(ConstString(name), false);
2314 std::unique_ptr<Declaration> decl_up;
2315 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
2316 decl_up = std::make_unique<Declaration>(die.GetCU()->GetFile(decl_file),
2317 decl_line, decl_column);
2319 SymbolFileDWARF *dwarf = die.GetDWARF();
2320 // Supply the type _only_ if it has already been parsed
2321 Type *func_type = dwarf->GetDIEToType().lookup(die.GetDIE());
2323 assert(func_type == nullptr || func_type != DIE_IS_BEING_PARSED);
2325 if (dwarf->FixupAddress(func_range.GetBaseAddress())) {
2326 const user_id_t func_user_id = die.GetID();
2328 std::make_shared<Function>(&comp_unit,
2329 func_user_id, // UserID is the DIE offset
2330 func_user_id, func_name, func_type,
2331 func_range); // first address range
2333 if (func_sp.get() != nullptr) {
2334 if (frame_base.IsValid())
2335 func_sp->GetFrameBaseExpression() = frame_base;
2336 comp_unit.AddFunction(func_sp);
2337 return func_sp.get();
2345 void DWARFASTParserClang::ParseSingleMember(
2346 const DWARFDIE &die, const DWARFDIE &parent_die,
2347 const lldb_private::CompilerType &class_clang_type,
2348 std::vector<int> &member_accessibilities,
2349 lldb::AccessType default_accessibility,
2350 DelayedPropertyList &delayed_properties,
2351 lldb_private::ClangASTImporter::LayoutInfo &layout_info,
2352 FieldInfo &last_field_info) {
2353 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2354 const dw_tag_t tag = die.Tag();
2355 // Get the parent byte size so we can verify any members will fit
2356 const uint64_t parent_byte_size =
2357 parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
2358 const uint64_t parent_bit_size =
2359 parent_byte_size == UINT64_MAX ? UINT64_MAX : parent_byte_size * 8;
2361 DWARFAttributes attributes;
2362 const size_t num_attributes = die.GetAttributes(attributes);
2363 if (num_attributes == 0)
2366 const char *name = nullptr;
2367 const char *prop_name = nullptr;
2368 const char *prop_getter_name = nullptr;
2369 const char *prop_setter_name = nullptr;
2370 uint32_t prop_attributes = 0;
2372 bool is_artificial = false;
2373 DWARFFormValue encoding_form;
2374 AccessType accessibility = eAccessNone;
2375 uint32_t member_byte_offset =
2376 (parent_die.Tag() == DW_TAG_union_type) ? 0 : UINT32_MAX;
2377 llvm::Optional<uint64_t> byte_size;
2378 int64_t bit_offset = 0;
2379 uint64_t data_bit_offset = UINT64_MAX;
2380 size_t bit_size = 0;
2382 false; // On DW_TAG_members, this means the member is static
2384 for (i = 0; i < num_attributes && !is_artificial; ++i) {
2385 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2386 DWARFFormValue form_value;
2387 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2388 // DW_AT_data_member_location indicates the byte offset of the
2389 // word from the base address of the structure.
2391 // DW_AT_bit_offset indicates how many bits into the word
2392 // (according to the host endianness) the low-order bit of the
2393 // field starts. AT_bit_offset can be negative.
2395 // DW_AT_bit_size indicates the size of the field in bits.
2398 name = form_value.AsCString();
2401 encoding_form = form_value;
2403 case DW_AT_bit_offset:
2404 bit_offset = form_value.Signed();
2406 case DW_AT_bit_size:
2407 bit_size = form_value.Unsigned();
2409 case DW_AT_byte_size:
2410 byte_size = form_value.Unsigned();
2412 case DW_AT_data_bit_offset:
2413 data_bit_offset = form_value.Unsigned();
2415 case DW_AT_data_member_location:
2416 if (form_value.BlockData()) {
2417 Value initialValue(0);
2418 Value memberOffset(0);
2419 const DWARFDataExtractor &debug_info_data = die.GetData();
2420 uint32_t block_length = form_value.Unsigned();
2421 uint32_t block_offset =
2422 form_value.BlockData() - debug_info_data.GetDataStart();
2423 if (DWARFExpression::Evaluate(
2424 nullptr, // ExecutionContext *
2425 nullptr, // RegisterContext *
2427 DataExtractor(debug_info_data, block_offset, block_length),
2428 die.GetCU(), eRegisterKindDWARF, &initialValue, nullptr,
2429 memberOffset, nullptr)) {
2430 member_byte_offset = memberOffset.ResolveValue(nullptr).UInt();
2433 // With DWARF 3 and later, if the value is an integer constant,
2434 // this form value is the offset in bytes from the beginning of
2435 // the containing entity.
2436 member_byte_offset = form_value.Unsigned();
2440 case DW_AT_accessibility:
2441 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
2443 case DW_AT_artificial:
2444 is_artificial = form_value.Boolean();
2446 case DW_AT_APPLE_property_name:
2447 prop_name = form_value.AsCString();
2449 case DW_AT_APPLE_property_getter:
2450 prop_getter_name = form_value.AsCString();
2452 case DW_AT_APPLE_property_setter:
2453 prop_setter_name = form_value.AsCString();
2455 case DW_AT_APPLE_property_attribute:
2456 prop_attributes = form_value.Unsigned();
2458 case DW_AT_external:
2459 is_external = form_value.Boolean();
2463 case DW_AT_declaration:
2464 case DW_AT_description:
2466 case DW_AT_visibility:
2474 ConstString fixed_setter;
2476 // Check if the property getter/setter were provided as full names.
2477 // We want basenames, so we extract them.
2479 if (prop_getter_name && prop_getter_name[0] == '-') {
2480 ObjCLanguage::MethodName prop_getter_method(prop_getter_name, true);
2481 prop_getter_name = prop_getter_method.GetSelector().GetCString();
2484 if (prop_setter_name && prop_setter_name[0] == '-') {
2485 ObjCLanguage::MethodName prop_setter_method(prop_setter_name, true);
2486 prop_setter_name = prop_setter_method.GetSelector().GetCString();
2489 // If the names haven't been provided, they need to be filled in.
2491 if (!prop_getter_name) {
2492 prop_getter_name = prop_name;
2494 if (!prop_setter_name && prop_name[0] &&
2495 !(prop_attributes & DW_APPLE_PROPERTY_readonly)) {
2498 ss.Printf("set%c%s:", toupper(prop_name[0]), &prop_name[1]);
2500 fixed_setter.SetString(ss.GetString());
2501 prop_setter_name = fixed_setter.GetCString();
2505 // Clang has a DWARF generation bug where sometimes it represents
2506 // fields that are references with bad byte size and bit size/offset
2507 // information such as:
2509 // DW_AT_byte_size( 0x00 )
2510 // DW_AT_bit_size( 0x40 )
2511 // DW_AT_bit_offset( 0xffffffffffffffc0 )
2513 // So check the bit offset to make sure it is sane, and if the values
2514 // are not sane, remove them. If we don't do this then we will end up
2515 // with a crash if we try to use this type in an expression when clang
2516 // becomes unhappy with its recycled debug info.
2518 if (byte_size.getValueOr(0) == 0 && bit_offset < 0) {
2523 const bool class_is_objc_object_or_interface =
2524 TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type);
2526 // FIXME: Make Clang ignore Objective-C accessibility for expressions
2527 if (class_is_objc_object_or_interface)
2528 accessibility = eAccessNone;
2530 // Handle static members
2531 if (is_external && member_byte_offset == UINT32_MAX) {
2532 Type *var_type = die.ResolveTypeUID(encoding_form.Reference());
2535 if (accessibility == eAccessNone)
2536 accessibility = eAccessPublic;
2537 TypeSystemClang::AddVariableToRecordType(
2538 class_clang_type, name, var_type->GetLayoutCompilerType(),
2544 if (!is_artificial) {
2545 Type *member_type = die.ResolveTypeUID(encoding_form.Reference());
2547 clang::FieldDecl *field_decl = nullptr;
2548 const uint64_t character_width = 8;
2549 const uint64_t word_width = 32;
2550 if (tag == DW_TAG_member) {
2552 CompilerType member_clang_type = member_type->GetLayoutCompilerType();
2554 if (accessibility == eAccessNone)
2555 accessibility = default_accessibility;
2556 member_accessibilities.push_back(accessibility);
2558 uint64_t field_bit_offset =
2559 (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
2562 FieldInfo this_field_info;
2563 this_field_info.bit_offset = field_bit_offset;
2564 this_field_info.bit_size = bit_size;
2566 if (data_bit_offset != UINT64_MAX) {
2567 this_field_info.bit_offset = data_bit_offset;
2570 byte_size = member_type->GetByteSize();
2572 ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2573 if (objfile->GetByteOrder() == eByteOrderLittle) {
2574 this_field_info.bit_offset += byte_size.getValueOr(0) * 8;
2575 this_field_info.bit_offset -= (bit_offset + bit_size);
2577 this_field_info.bit_offset += bit_offset;
2581 if ((this_field_info.bit_offset >= parent_bit_size) ||
2582 (last_field_info.IsBitfield() &&
2583 !last_field_info.NextBitfieldOffsetIsValid(
2584 this_field_info.bit_offset))) {
2585 ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2586 objfile->GetModule()->ReportWarning(
2587 "0x%8.8" PRIx64 ": %s bitfield named \"%s\" has invalid "
2588 "bit offset (0x%8.8" PRIx64
2589 ") member will be ignored. Please file a bug against the "
2590 "compiler and include the preprocessed output for %s\n",
2591 die.GetID(), DW_TAG_value_to_name(tag), name,
2592 this_field_info.bit_offset, GetUnitName(parent_die).c_str());
2596 // Update the field bit offset we will report for layout
2597 field_bit_offset = this_field_info.bit_offset;
2599 // Objective-C has invalid DW_AT_bit_offset values in older
2600 // versions of clang, so we have to be careful and only insert
2601 // unnamed bitfields if we have a new enough clang.
2602 bool detect_unnamed_bitfields = true;
2604 if (class_is_objc_object_or_interface)
2605 detect_unnamed_bitfields =
2606 die.GetCU()->Supports_unnamed_objc_bitfields();
2608 if (detect_unnamed_bitfields) {
2609 clang::Optional<FieldInfo> unnamed_field_info;
2610 uint64_t last_field_end = 0;
2613 last_field_info.bit_offset + last_field_info.bit_size;
2615 if (!last_field_info.IsBitfield()) {
2616 // The last field was not a bit-field...
2617 // but if it did take up the entire word then we need to extend
2618 // last_field_end so the bit-field does not step into the last
2620 if (last_field_end != 0 && ((last_field_end % word_width) != 0))
2621 last_field_end += word_width - (last_field_end % word_width);
2624 // If we have a gap between the last_field_end and the current
2625 // field we have an unnamed bit-field.
2626 // If we have a base class, we assume there is no unnamed
2627 // bit-field if this is the first field since the gap can be
2628 // attributed to the members from the base class. This assumption
2629 // is not correct if the first field of the derived class is
2630 // indeed an unnamed bit-field. We currently do not have the
2631 // machinary to track the offset of the last field of classes we
2632 // have seen before, so we are not handling this case.
2633 if (this_field_info.bit_offset != last_field_end &&
2634 this_field_info.bit_offset > last_field_end &&
2635 !(last_field_info.bit_offset == 0 &&
2636 last_field_info.bit_size == 0 &&
2637 layout_info.base_offsets.size() != 0)) {
2638 unnamed_field_info = FieldInfo{};
2639 unnamed_field_info->bit_size =
2640 this_field_info.bit_offset - last_field_end;
2641 unnamed_field_info->bit_offset = last_field_end;
2644 if (unnamed_field_info) {
2645 clang::FieldDecl *unnamed_bitfield_decl =
2646 TypeSystemClang::AddFieldToRecordType(
2647 class_clang_type, llvm::StringRef(),
2648 m_ast.GetBuiltinTypeForEncodingAndBitSize(eEncodingSint,
2650 accessibility, unnamed_field_info->bit_size);
2652 layout_info.field_offsets.insert(std::make_pair(
2653 unnamed_bitfield_decl, unnamed_field_info->bit_offset));
2657 last_field_info = this_field_info;
2658 last_field_info.SetIsBitfield(true);
2660 last_field_info.bit_offset = field_bit_offset;
2662 if (llvm::Optional<uint64_t> clang_type_size =
2663 member_clang_type.GetByteSize(nullptr)) {
2664 last_field_info.bit_size = *clang_type_size * character_width;
2667 last_field_info.SetIsBitfield(false);
2670 if (!member_clang_type.IsCompleteType())
2671 member_clang_type.GetCompleteType();
2674 // Older versions of clang emit array[0] and array[1] in the
2675 // same way (<rdar://problem/12566646>). If the current field
2676 // is at the end of the structure, then there is definitely no
2677 // room for extra elements and we override the type to
2680 CompilerType member_array_element_type;
2681 uint64_t member_array_size;
2682 bool member_array_is_incomplete;
2684 if (member_clang_type.IsArrayType(&member_array_element_type,
2686 &member_array_is_incomplete) &&
2687 !member_array_is_incomplete) {
2688 uint64_t parent_byte_size =
2689 parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size,
2692 if (member_byte_offset >= parent_byte_size) {
2693 if (member_array_size != 1 &&
2694 (member_array_size != 0 ||
2695 member_byte_offset > parent_byte_size)) {
2696 module_sp->ReportError(
2698 ": DW_TAG_member '%s' refers to type 0x%8.8x"
2699 " which extends beyond the bounds of 0x%8.8" PRIx64,
2700 die.GetID(), name, encoding_form.Reference().GetOffset(),
2701 parent_die.GetID());
2705 m_ast.CreateArrayType(member_array_element_type, 0, false);
2710 CompleteType(member_clang_type);
2712 field_decl = TypeSystemClang::AddFieldToRecordType(
2713 class_clang_type, name, member_clang_type, accessibility,
2716 m_ast.SetMetadataAsUserID(field_decl, die.GetID());
2718 layout_info.field_offsets.insert(
2719 std::make_pair(field_decl, field_bit_offset));
2722 module_sp->ReportError(
2723 "0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8x"
2724 " which was unable to be parsed",
2725 die.GetID(), name, encoding_form.Reference().GetOffset());
2727 module_sp->ReportError(
2728 "0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8x"
2729 " which was unable to be parsed",
2730 die.GetID(), encoding_form.Reference().GetOffset());
2734 if (prop_name != nullptr && member_type) {
2735 clang::ObjCIvarDecl *ivar_decl = nullptr;
2738 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
2739 assert(ivar_decl != nullptr);
2742 ClangASTMetadata metadata;
2743 metadata.SetUserID(die.GetID());
2744 delayed_properties.push_back(DelayedAddObjCClassProperty(
2745 class_clang_type, prop_name, member_type->GetLayoutCompilerType(),
2746 ivar_decl, prop_setter_name, prop_getter_name, prop_attributes,
2750 m_ast.SetMetadataAsUserID(ivar_decl, die.GetID());
2755 bool DWARFASTParserClang::ParseChildMembers(
2756 const DWARFDIE &parent_die, CompilerType &class_clang_type,
2757 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
2758 std::vector<int> &member_accessibilities,
2759 std::vector<DWARFDIE> &member_function_dies,
2760 DelayedPropertyList &delayed_properties, AccessType &default_accessibility,
2761 bool &is_a_class, ClangASTImporter::LayoutInfo &layout_info) {
2765 FieldInfo last_field_info;
2767 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2768 TypeSystemClang *ast =
2769 llvm::dyn_cast_or_null<TypeSystemClang>(class_clang_type.GetTypeSystem());
2773 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
2774 die = die.GetSibling()) {
2775 dw_tag_t tag = die.Tag();
2779 case DW_TAG_APPLE_property:
2780 ParseSingleMember(die, parent_die, class_clang_type,
2781 member_accessibilities, default_accessibility,
2782 delayed_properties, layout_info, last_field_info);
2785 case DW_TAG_subprogram:
2786 // Let the type parsing code handle this one for us.
2787 member_function_dies.push_back(die);
2790 case DW_TAG_inheritance: {
2792 if (default_accessibility == eAccessNone)
2793 default_accessibility = eAccessPrivate;
2794 // TODO: implement DW_TAG_inheritance type parsing
2795 DWARFAttributes attributes;
2796 const size_t num_attributes = die.GetAttributes(attributes);
2797 if (num_attributes > 0) {
2798 DWARFFormValue encoding_form;
2799 AccessType accessibility = default_accessibility;
2800 bool is_virtual = false;
2801 bool is_base_of_class = true;
2802 off_t member_byte_offset = 0;
2804 for (i = 0; i < num_attributes; ++i) {
2805 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2806 DWARFFormValue form_value;
2807 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2810 encoding_form = form_value;
2812 case DW_AT_data_member_location:
2813 if (form_value.BlockData()) {
2814 Value initialValue(0);
2815 Value memberOffset(0);
2816 const DWARFDataExtractor &debug_info_data = die.GetData();
2817 uint32_t block_length = form_value.Unsigned();
2818 uint32_t block_offset =
2819 form_value.BlockData() - debug_info_data.GetDataStart();
2820 if (DWARFExpression::Evaluate(
2821 nullptr, nullptr, module_sp,
2822 DataExtractor(debug_info_data, block_offset,
2824 die.GetCU(), eRegisterKindDWARF, &initialValue, nullptr,
2825 memberOffset, nullptr)) {
2826 member_byte_offset =
2827 memberOffset.ResolveValue(nullptr).UInt();
2830 // With DWARF 3 and later, if the value is an integer constant,
2831 // this form value is the offset in bytes from the beginning of
2832 // the containing entity.
2833 member_byte_offset = form_value.Unsigned();
2837 case DW_AT_accessibility:
2838 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
2841 case DW_AT_virtuality:
2842 is_virtual = form_value.Boolean();
2854 Type *base_class_type = die.ResolveTypeUID(encoding_form.Reference());
2855 if (base_class_type == nullptr) {
2856 module_sp->ReportError("0x%8.8x: DW_TAG_inheritance failed to "
2857 "resolve the base class at 0x%8.8x"
2858 " from enclosing type 0x%8.8x. \nPlease file "
2859 "a bug and attach the file at the start of "
2860 "this error message",
2862 encoding_form.Reference().GetOffset(),
2863 parent_die.GetOffset());
2867 CompilerType base_class_clang_type =
2868 base_class_type->GetFullCompilerType();
2869 assert(base_class_clang_type);
2870 if (TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type)) {
2871 ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
2873 std::unique_ptr<clang::CXXBaseSpecifier> result =
2874 ast->CreateBaseClassSpecifier(
2875 base_class_clang_type.GetOpaqueQualType(), accessibility,
2876 is_virtual, is_base_of_class);
2880 base_classes.push_back(std::move(result));
2883 // Do not specify any offset for virtual inheritance. The DWARF
2884 // produced by clang doesn't give us a constant offset, but gives
2885 // us a DWARF expressions that requires an actual object in memory.
2886 // the DW_AT_data_member_location for a virtual base class looks
2888 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref,
2889 // DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref,
2891 // Given this, there is really no valid response we can give to
2892 // clang for virtual base class offsets, and this should eventually
2893 // be removed from LayoutRecordType() in the external
2894 // AST source in clang.
2896 layout_info.base_offsets.insert(std::make_pair(
2897 ast->GetAsCXXRecordDecl(
2898 base_class_clang_type.GetOpaqueQualType()),
2899 clang::CharUnits::fromQuantity(member_byte_offset)));
2913 size_t DWARFASTParserClang::ParseChildParameters(
2914 clang::DeclContext *containing_decl_ctx, const DWARFDIE &parent_die,
2915 bool skip_artificial, bool &is_static, bool &is_variadic,
2916 bool &has_template_params, std::vector<CompilerType> &function_param_types,
2917 std::vector<clang::ParmVarDecl *> &function_param_decls,
2918 unsigned &type_quals) {
2923 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
2924 die = die.GetSibling()) {
2925 const dw_tag_t tag = die.Tag();
2927 case DW_TAG_formal_parameter: {
2928 DWARFAttributes attributes;
2929 const size_t num_attributes = die.GetAttributes(attributes);
2930 if (num_attributes > 0) {
2931 const char *name = nullptr;
2932 DWARFFormValue param_type_die_form;
2933 bool is_artificial = false;
2934 // one of None, Auto, Register, Extern, Static, PrivateExtern
2936 clang::StorageClass storage = clang::SC_None;
2938 for (i = 0; i < num_attributes; ++i) {
2939 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2940 DWARFFormValue form_value;
2941 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2944 name = form_value.AsCString();
2947 param_type_die_form = form_value;
2949 case DW_AT_artificial:
2950 is_artificial = form_value.Boolean();
2952 case DW_AT_location:
2953 case DW_AT_const_value:
2954 case DW_AT_default_value:
2955 case DW_AT_description:
2956 case DW_AT_endianity:
2957 case DW_AT_is_optional:
2959 case DW_AT_variable_parameter:
2961 case DW_AT_abstract_origin:
2969 if (skip_artificial && is_artificial) {
2970 // In order to determine if a C++ member function is "const" we
2971 // have to look at the const-ness of "this"...
2973 DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()) &&
2974 // Often times compilers omit the "this" name for the
2975 // specification DIEs, so we can't rely upon the name being in
2976 // the formal parameter DIE...
2977 (name == nullptr || ::strcmp(name, "this") == 0)) {
2979 die.ResolveTypeUID(param_type_die_form.Reference());
2981 uint32_t encoding_mask = this_type->GetEncodingMask();
2982 if (encoding_mask & Type::eEncodingIsPointerUID) {
2985 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
2986 type_quals |= clang::Qualifiers::Const;
2987 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
2988 type_quals |= clang::Qualifiers::Volatile;
2996 Type *type = die.ResolveTypeUID(param_type_die_form.Reference());
2998 function_param_types.push_back(type->GetForwardCompilerType());
3000 clang::ParmVarDecl *param_var_decl =
3001 m_ast.CreateParameterDeclaration(
3002 containing_decl_ctx, GetOwningClangModule(die), name,
3003 type->GetForwardCompilerType(), storage);
3004 assert(param_var_decl);
3005 function_param_decls.push_back(param_var_decl);
3007 m_ast.SetMetadataAsUserID(param_var_decl, die.GetID());
3014 case DW_TAG_unspecified_parameters:
3018 case DW_TAG_template_type_parameter:
3019 case DW_TAG_template_value_parameter:
3020 case DW_TAG_GNU_template_parameter_pack:
3021 // The one caller of this was never using the template_param_infos, and
3022 // the local variable was taking up a large amount of stack space in
3023 // SymbolFileDWARF::ParseType() so this was removed. If we ever need the
3024 // template params back, we can add them back.
3025 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
3026 has_template_params = true;
3036 llvm::Optional<SymbolFile::ArrayInfo>
3037 DWARFASTParser::ParseChildArrayInfo(const DWARFDIE &parent_die,
3038 const ExecutionContext *exe_ctx) {
3039 SymbolFile::ArrayInfo array_info;
3043 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
3044 die = die.GetSibling()) {
3045 const dw_tag_t tag = die.Tag();
3047 case DW_TAG_subrange_type: {
3048 DWARFAttributes attributes;
3049 const size_t num_child_attributes = die.GetAttributes(attributes);
3050 if (num_child_attributes > 0) {
3051 uint64_t num_elements = 0;
3052 uint64_t lower_bound = 0;
3053 uint64_t upper_bound = 0;
3054 bool upper_bound_valid = false;
3056 for (i = 0; i < num_child_attributes; ++i) {
3057 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3058 DWARFFormValue form_value;
3059 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3065 if (DWARFDIE var_die = die.GetReferencedDIE(DW_AT_count)) {
3066 if (var_die.Tag() == DW_TAG_variable)
3068 if (auto frame = exe_ctx->GetFrameSP()) {
3070 lldb::VariableSP var_sp;
3071 auto valobj_sp = frame->GetValueForVariableExpressionPath(
3072 var_die.GetName(), eNoDynamicValues, 0, var_sp,
3075 num_elements = valobj_sp->GetValueAsUnsigned(0);
3081 num_elements = form_value.Unsigned();
3084 case DW_AT_bit_stride:
3085 array_info.bit_stride = form_value.Unsigned();
3088 case DW_AT_byte_stride:
3089 array_info.byte_stride = form_value.Unsigned();
3092 case DW_AT_lower_bound:
3093 lower_bound = form_value.Unsigned();
3096 case DW_AT_upper_bound:
3097 upper_bound_valid = true;
3098 upper_bound = form_value.Unsigned();
3102 case DW_AT_abstract_origin:
3103 case DW_AT_accessibility:
3104 case DW_AT_allocated:
3105 case DW_AT_associated:
3106 case DW_AT_data_location:
3107 case DW_AT_declaration:
3108 case DW_AT_description:
3110 case DW_AT_threads_scaled:
3112 case DW_AT_visibility:
3118 if (num_elements == 0) {
3119 if (upper_bound_valid && upper_bound >= lower_bound)
3120 num_elements = upper_bound - lower_bound + 1;
3123 array_info.element_orders.push_back(num_elements);
3133 Type *DWARFASTParserClang::GetTypeForDIE(const DWARFDIE &die) {
3135 SymbolFileDWARF *dwarf = die.GetDWARF();
3136 DWARFAttributes attributes;
3137 const size_t num_attributes = die.GetAttributes(attributes);
3138 if (num_attributes > 0) {
3139 DWARFFormValue type_die_form;
3140 for (size_t i = 0; i < num_attributes; ++i) {
3141 dw_attr_t attr = attributes.AttributeAtIndex(i);
3142 DWARFFormValue form_value;
3144 if (attr == DW_AT_type &&
3145 attributes.ExtractFormValueAtIndex(i, form_value))
3146 return dwarf->ResolveTypeUID(form_value.Reference(), true);
3154 clang::Decl *DWARFASTParserClang::GetClangDeclForDIE(const DWARFDIE &die) {
3158 switch (die.Tag()) {
3159 case DW_TAG_variable:
3160 case DW_TAG_constant:
3161 case DW_TAG_formal_parameter:
3162 case DW_TAG_imported_declaration:
3163 case DW_TAG_imported_module:
3169 DIEToDeclMap::iterator cache_pos = m_die_to_decl.find(die.GetDIE());
3170 if (cache_pos != m_die_to_decl.end())
3171 return cache_pos->second;
3173 if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification)) {
3174 clang::Decl *decl = GetClangDeclForDIE(spec_die);
3175 m_die_to_decl[die.GetDIE()] = decl;
3176 m_decl_to_die[decl].insert(die.GetDIE());
3180 if (DWARFDIE abstract_origin_die =
3181 die.GetReferencedDIE(DW_AT_abstract_origin)) {
3182 clang::Decl *decl = GetClangDeclForDIE(abstract_origin_die);
3183 m_die_to_decl[die.GetDIE()] = decl;
3184 m_decl_to_die[decl].insert(die.GetDIE());
3188 clang::Decl *decl = nullptr;
3189 switch (die.Tag()) {
3190 case DW_TAG_variable:
3191 case DW_TAG_constant:
3192 case DW_TAG_formal_parameter: {
3193 SymbolFileDWARF *dwarf = die.GetDWARF();
3194 Type *type = GetTypeForDIE(die);
3195 if (dwarf && type) {
3196 const char *name = die.GetName();
3197 clang::DeclContext *decl_context =
3198 TypeSystemClang::DeclContextGetAsDeclContext(
3199 dwarf->GetDeclContextContainingUID(die.GetID()));
3200 decl = m_ast.CreateVariableDeclaration(
3201 decl_context, GetOwningClangModule(die), name,
3202 ClangUtil::GetQualType(type->GetForwardCompilerType()));
3206 case DW_TAG_imported_declaration: {
3207 SymbolFileDWARF *dwarf = die.GetDWARF();
3208 DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3210 CompilerDecl imported_decl = SymbolFileDWARF::GetDecl(imported_uid);
3211 if (imported_decl) {
3212 clang::DeclContext *decl_context =
3213 TypeSystemClang::DeclContextGetAsDeclContext(
3214 dwarf->GetDeclContextContainingUID(die.GetID()));
3215 if (clang::NamedDecl *clang_imported_decl =
3216 llvm::dyn_cast<clang::NamedDecl>(
3217 (clang::Decl *)imported_decl.GetOpaqueDecl()))
3218 decl = m_ast.CreateUsingDeclaration(
3219 decl_context, OptionalClangModuleID(), clang_imported_decl);
3224 case DW_TAG_imported_module: {
3225 SymbolFileDWARF *dwarf = die.GetDWARF();
3226 DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3229 CompilerDeclContext imported_decl_ctx =
3230 SymbolFileDWARF::GetDeclContext(imported_uid);
3231 if (imported_decl_ctx) {
3232 clang::DeclContext *decl_context =
3233 TypeSystemClang::DeclContextGetAsDeclContext(
3234 dwarf->GetDeclContextContainingUID(die.GetID()));
3235 if (clang::NamespaceDecl *ns_decl =
3236 TypeSystemClang::DeclContextGetAsNamespaceDecl(
3238 decl = m_ast.CreateUsingDirectiveDeclaration(
3239 decl_context, OptionalClangModuleID(), ns_decl);
3248 m_die_to_decl[die.GetDIE()] = decl;
3249 m_decl_to_die[decl].insert(die.GetDIE());
3254 clang::DeclContext *
3255 DWARFASTParserClang::GetClangDeclContextForDIE(const DWARFDIE &die) {
3257 clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE(die);
3261 bool try_parsing_type = true;
3262 switch (die.Tag()) {
3263 case DW_TAG_compile_unit:
3264 case DW_TAG_partial_unit:
3265 decl_ctx = m_ast.GetTranslationUnitDecl();
3266 try_parsing_type = false;
3269 case DW_TAG_namespace:
3270 decl_ctx = ResolveNamespaceDIE(die);
3271 try_parsing_type = false;
3274 case DW_TAG_lexical_block:
3275 decl_ctx = GetDeclContextForBlock(die);
3276 try_parsing_type = false;
3283 if (decl_ctx == nullptr && try_parsing_type) {
3284 Type *type = die.GetDWARF()->ResolveType(die);
3286 decl_ctx = GetCachedClangDeclContextForDIE(die);
3290 LinkDeclContextToDIE(decl_ctx, die);
3297 OptionalClangModuleID
3298 DWARFASTParserClang::GetOwningClangModule(const DWARFDIE &die) {
3302 for (DWARFDIE parent = die.GetParent(); parent.IsValid();
3303 parent = parent.GetParent()) {
3304 const dw_tag_t tag = parent.Tag();
3305 if (tag == DW_TAG_module) {
3306 DWARFDIE module_die = parent;
3307 auto it = m_die_to_module.find(module_die.GetDIE());
3308 if (it != m_die_to_module.end())
3310 const char *name = module_die.GetAttributeValueAsString(DW_AT_name, 0);
3314 OptionalClangModuleID id =
3315 m_ast.GetOrCreateClangModule(name, GetOwningClangModule(module_die));
3316 m_die_to_module.insert({module_die.GetDIE(), id});
3323 static bool IsSubroutine(const DWARFDIE &die) {
3324 switch (die.Tag()) {
3325 case DW_TAG_subprogram:
3326 case DW_TAG_inlined_subroutine:
3333 static DWARFDIE GetContainingFunctionWithAbstractOrigin(const DWARFDIE &die) {
3334 for (DWARFDIE candidate = die; candidate; candidate = candidate.GetParent()) {
3335 if (IsSubroutine(candidate)) {
3336 if (candidate.GetReferencedDIE(DW_AT_abstract_origin)) {
3343 assert(0 && "Shouldn't call GetContainingFunctionWithAbstractOrigin on "
3344 "something not in a function");
3348 static DWARFDIE FindAnyChildWithAbstractOrigin(const DWARFDIE &context) {
3349 for (DWARFDIE candidate = context.GetFirstChild(); candidate.IsValid();
3350 candidate = candidate.GetSibling()) {
3351 if (candidate.GetReferencedDIE(DW_AT_abstract_origin)) {
3358 static DWARFDIE FindFirstChildWithAbstractOrigin(const DWARFDIE &block,
3359 const DWARFDIE &function) {
3360 assert(IsSubroutine(function));
3361 for (DWARFDIE context = block; context != function.GetParent();
3362 context = context.GetParent()) {
3363 assert(!IsSubroutine(context) || context == function);
3364 if (DWARFDIE child = FindAnyChildWithAbstractOrigin(context)) {
3371 clang::DeclContext *
3372 DWARFASTParserClang::GetDeclContextForBlock(const DWARFDIE &die) {
3373 assert(die.Tag() == DW_TAG_lexical_block);
3374 DWARFDIE containing_function_with_abstract_origin =
3375 GetContainingFunctionWithAbstractOrigin(die);
3376 if (!containing_function_with_abstract_origin) {
3377 return (clang::DeclContext *)ResolveBlockDIE(die);
3379 DWARFDIE child = FindFirstChildWithAbstractOrigin(
3380 die, containing_function_with_abstract_origin);
3381 CompilerDeclContext decl_context =
3382 GetDeclContextContainingUIDFromDWARF(child);
3383 return (clang::DeclContext *)decl_context.GetOpaqueDeclContext();
3386 clang::BlockDecl *DWARFASTParserClang::ResolveBlockDIE(const DWARFDIE &die) {
3387 if (die && die.Tag() == DW_TAG_lexical_block) {
3388 clang::BlockDecl *decl =
3389 llvm::cast_or_null<clang::BlockDecl>(m_die_to_decl_ctx[die.GetDIE()]);
3392 DWARFDIE decl_context_die;
3393 clang::DeclContext *decl_context =
3394 GetClangDeclContextContainingDIE(die, &decl_context_die);
3396 m_ast.CreateBlockDeclaration(decl_context, GetOwningClangModule(die));
3399 LinkDeclContextToDIE((clang::DeclContext *)decl, die);
3407 clang::NamespaceDecl *
3408 DWARFASTParserClang::ResolveNamespaceDIE(const DWARFDIE &die) {
3409 if (die && die.Tag() == DW_TAG_namespace) {
3410 // See if we already parsed this namespace DIE and associated it with a
3411 // uniqued namespace declaration
3412 clang::NamespaceDecl *namespace_decl =
3413 static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die.GetDIE()]);
3415 return namespace_decl;
3417 const char *namespace_name = die.GetName();
3418 clang::DeclContext *containing_decl_ctx =
3419 GetClangDeclContextContainingDIE(die, nullptr);
3421 die.GetAttributeValueAsUnsigned(DW_AT_export_symbols, 0) != 0;
3423 namespace_decl = m_ast.GetUniqueNamespaceDeclaration(
3424 namespace_name, containing_decl_ctx, GetOwningClangModule(die),
3427 nullptr; // (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3429 SymbolFileDWARF *dwarf = die.GetDWARF();
3430 if (namespace_name) {
3431 dwarf->GetObjectFile()->GetModule()->LogMessage(
3433 "ASTContext => %p: 0x%8.8" PRIx64
3434 ": DW_TAG_namespace with DW_AT_name(\"%s\") => "
3435 "clang::NamespaceDecl *%p (original = %p)",
3436 static_cast<void *>(&m_ast.getASTContext()), die.GetID(),
3437 namespace_name, static_cast<void *>(namespace_decl),
3438 static_cast<void *>(namespace_decl->getOriginalNamespace()));
3440 dwarf->GetObjectFile()->GetModule()->LogMessage(
3442 "ASTContext => %p: 0x%8.8" PRIx64
3443 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p "
3445 static_cast<void *>(&m_ast.getASTContext()), die.GetID(),
3446 static_cast<void *>(namespace_decl),
3447 static_cast<void *>(namespace_decl->getOriginalNamespace()));
3452 LinkDeclContextToDIE((clang::DeclContext *)namespace_decl, die);
3453 return namespace_decl;
3459 clang::DeclContext *DWARFASTParserClang::GetClangDeclContextContainingDIE(
3460 const DWARFDIE &die, DWARFDIE *decl_ctx_die_copy) {
3461 SymbolFileDWARF *dwarf = die.GetDWARF();
3463 DWARFDIE decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE(die);
3465 if (decl_ctx_die_copy)
3466 *decl_ctx_die_copy = decl_ctx_die;
3469 clang::DeclContext *clang_decl_ctx =
3470 GetClangDeclContextForDIE(decl_ctx_die);
3472 return clang_decl_ctx;
3474 return m_ast.GetTranslationUnitDecl();
3477 clang::DeclContext *
3478 DWARFASTParserClang::GetCachedClangDeclContextForDIE(const DWARFDIE &die) {
3480 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die.GetDIE());
3481 if (pos != m_die_to_decl_ctx.end())
3487 void DWARFASTParserClang::LinkDeclContextToDIE(clang::DeclContext *decl_ctx,
3488 const DWARFDIE &die) {
3489 m_die_to_decl_ctx[die.GetDIE()] = decl_ctx;
3490 // There can be many DIEs for a single decl context
3491 // m_decl_ctx_to_die[decl_ctx].insert(die.GetDIE());
3492 m_decl_ctx_to_die.insert(std::make_pair(decl_ctx, die));
3495 bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
3496 const DWARFDIE &src_class_die, const DWARFDIE &dst_class_die,
3497 lldb_private::Type *class_type, std::vector<DWARFDIE> &failures) {
3498 if (!class_type || !src_class_die || !dst_class_die)
3500 if (src_class_die.Tag() != dst_class_die.Tag())
3503 // We need to complete the class type so we can get all of the method types
3504 // parsed so we can then unique those types to their equivalent counterparts
3505 // in "dst_cu" and "dst_class_die"
3506 class_type->GetFullCompilerType();
3510 UniqueCStringMap<DWARFDIE> src_name_to_die;
3511 UniqueCStringMap<DWARFDIE> dst_name_to_die;
3512 UniqueCStringMap<DWARFDIE> src_name_to_die_artificial;
3513 UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial;
3514 for (src_die = src_class_die.GetFirstChild(); src_die.IsValid();
3515 src_die = src_die.GetSibling()) {
3516 if (src_die.Tag() == DW_TAG_subprogram) {
3517 // Make sure this is a declaration and not a concrete instance by looking
3518 // for DW_AT_declaration set to 1. Sometimes concrete function instances
3519 // are placed inside the class definitions and shouldn't be included in
3520 // the list of things are are tracking here.
3521 if (src_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3522 const char *src_name = src_die.GetMangledName();
3524 ConstString src_const_name(src_name);
3525 if (src_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3526 src_name_to_die_artificial.Append(src_const_name, src_die);
3528 src_name_to_die.Append(src_const_name, src_die);
3533 for (dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid();
3534 dst_die = dst_die.GetSibling()) {
3535 if (dst_die.Tag() == DW_TAG_subprogram) {
3536 // Make sure this is a declaration and not a concrete instance by looking
3537 // for DW_AT_declaration set to 1. Sometimes concrete function instances
3538 // are placed inside the class definitions and shouldn't be included in
3539 // the list of things are are tracking here.
3540 if (dst_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3541 const char *dst_name = dst_die.GetMangledName();
3543 ConstString dst_const_name(dst_name);
3544 if (dst_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3545 dst_name_to_die_artificial.Append(dst_const_name, dst_die);
3547 dst_name_to_die.Append(dst_const_name, dst_die);
3552 const uint32_t src_size = src_name_to_die.GetSize();
3553 const uint32_t dst_size = dst_name_to_die.GetSize();
3554 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
3555 // DWARF_LOG_TYPE_COMPLETION));
3557 // Is everything kosher so we can go through the members at top speed?
3558 bool fast_path = true;
3560 if (src_size != dst_size) {
3561 if (src_size != 0 && dst_size != 0) {
3563 "warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, "
3564 "but they didn't have the same size (src=%d, dst=%d)",
3565 src_class_die.GetOffset(), dst_class_die.GetOffset(), src_size,
3575 for (idx = 0; idx < src_size; ++idx) {
3576 src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3577 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3579 if (src_die.Tag() != dst_die.Tag()) {
3581 "warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, "
3582 "but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
3583 src_class_die.GetOffset(), dst_class_die.GetOffset(),
3584 src_die.GetOffset(), src_die.GetTagAsCString(),
3585 dst_die.GetOffset(), dst_die.GetTagAsCString());
3589 const char *src_name = src_die.GetMangledName();
3590 const char *dst_name = dst_die.GetMangledName();
3592 // Make sure the names match
3593 if (src_name == dst_name || (strcmp(src_name, dst_name) == 0))
3597 "warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, "
3598 "but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
3599 src_class_die.GetOffset(), dst_class_die.GetOffset(),
3600 src_die.GetOffset(), src_name, dst_die.GetOffset(), dst_name);
3606 DWARFASTParserClang *src_dwarf_ast_parser =
3607 static_cast<DWARFASTParserClang *>(
3608 SymbolFileDWARF::GetDWARFParser(*src_die.GetCU()));
3609 DWARFASTParserClang *dst_dwarf_ast_parser =
3610 static_cast<DWARFASTParserClang *>(
3611 SymbolFileDWARF::GetDWARFParser(*dst_die.GetCU()));
3613 // Now do the work of linking the DeclContexts and Types.
3615 // We can do this quickly. Just run across the tables index-for-index
3616 // since we know each node has matching names and tags.
3617 for (idx = 0; idx < src_size; ++idx) {
3618 src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3619 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3621 clang::DeclContext *src_decl_ctx =
3622 src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3624 LLDB_LOGF(log, "uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3625 static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3626 dst_die.GetOffset());
3627 dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3630 "warning: tried to unique decl context from 0x%8.8x for "
3631 "0x%8.8x, but none was found",
3632 src_die.GetOffset(), dst_die.GetOffset());
3635 Type *src_child_type =
3636 dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3637 if (src_child_type) {
3639 "uniquing type %p (uid=0x%" PRIx64
3640 ") from 0x%8.8x for 0x%8.8x",
3641 static_cast<void *>(src_child_type), src_child_type->GetID(),
3642 src_die.GetOffset(), dst_die.GetOffset());
3643 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3646 "warning: tried to unique lldb_private::Type from "
3647 "0x%8.8x for 0x%8.8x, but none was found",
3648 src_die.GetOffset(), dst_die.GetOffset());
3652 // We must do this slowly. For each member of the destination, look up a
3653 // member in the source with the same name, check its tag, and unique them
3654 // if everything matches up. Report failures.
3656 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty()) {
3657 src_name_to_die.Sort();
3659 for (idx = 0; idx < dst_size; ++idx) {
3660 ConstString dst_name = dst_name_to_die.GetCStringAtIndex(idx);
3661 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3662 src_die = src_name_to_die.Find(dst_name, DWARFDIE());
3664 if (src_die && (src_die.Tag() == dst_die.Tag())) {
3665 clang::DeclContext *src_decl_ctx =
3666 src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3668 LLDB_LOGF(log, "uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3669 static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3670 dst_die.GetOffset());
3671 dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3674 "warning: tried to unique decl context from 0x%8.8x "
3675 "for 0x%8.8x, but none was found",
3676 src_die.GetOffset(), dst_die.GetOffset());
3679 Type *src_child_type =
3680 dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3681 if (src_child_type) {
3684 "uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3685 static_cast<void *>(src_child_type), src_child_type->GetID(),
3686 src_die.GetOffset(), dst_die.GetOffset());
3687 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] =
3691 "warning: tried to unique lldb_private::Type from "
3692 "0x%8.8x for 0x%8.8x, but none was found",
3693 src_die.GetOffset(), dst_die.GetOffset());
3696 LLDB_LOGF(log, "warning: couldn't find a match for 0x%8.8x",
3697 dst_die.GetOffset());
3699 failures.push_back(dst_die);
3705 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize();
3706 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize();
3708 if (src_size_artificial && dst_size_artificial) {
3709 dst_name_to_die_artificial.Sort();
3711 for (idx = 0; idx < src_size_artificial; ++idx) {
3712 ConstString src_name_artificial =
3713 src_name_to_die_artificial.GetCStringAtIndex(idx);
3714 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3716 dst_name_to_die_artificial.Find(src_name_artificial, DWARFDIE());
3719 // Both classes have the artificial types, link them
3720 clang::DeclContext *src_decl_ctx =
3721 src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3723 LLDB_LOGF(log, "uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3724 static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3725 dst_die.GetOffset());
3726 dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3729 "warning: tried to unique decl context from 0x%8.8x "
3730 "for 0x%8.8x, but none was found",
3731 src_die.GetOffset(), dst_die.GetOffset());
3734 Type *src_child_type =
3735 dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3736 if (src_child_type) {
3739 "uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3740 static_cast<void *>(src_child_type), src_child_type->GetID(),
3741 src_die.GetOffset(), dst_die.GetOffset());
3742 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3745 "warning: tried to unique lldb_private::Type from "
3746 "0x%8.8x for 0x%8.8x, but none was found",
3747 src_die.GetOffset(), dst_die.GetOffset());
3753 if (dst_size_artificial) {
3754 for (idx = 0; idx < dst_size_artificial; ++idx) {
3755 ConstString dst_name_artificial =
3756 dst_name_to_die_artificial.GetCStringAtIndex(idx);
3757 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3759 "warning: need to create artificial method for 0x%8.8x for "
3761 dst_die.GetOffset(), dst_name_artificial.GetCString());
3763 failures.push_back(dst_die);
3767 return !failures.empty();