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() = default;
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::GetLanguageFamily(*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(die.GetID(), dwarf, pcm_type_sp->GetName(),
213 pcm_type_sp->GetByteSize(nullptr), nullptr,
214 LLDB_INVALID_UID, Type::eEncodingInvalid,
215 &pcm_type_sp->GetDeclaration(), type,
216 Type::ResolveState::Forward,
217 TypePayloadClang(GetOwningClangModule(die))));
219 dwarf->GetTypeList().Insert(type_sp);
220 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
221 clang::TagDecl *tag_decl = TypeSystemClang::GetAsTagDecl(type);
223 LinkDeclContextToDIE(tag_decl, die);
225 clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(die);
227 LinkDeclContextToDIE(defn_decl_ctx, die);
233 static void ForcefullyCompleteType(CompilerType type) {
234 bool started = TypeSystemClang::StartTagDeclarationDefinition(type);
235 lldbassert(started && "Unable to start a class type definition.");
236 TypeSystemClang::CompleteTagDeclarationDefinition(type);
237 const clang::TagDecl *td = ClangUtil::GetAsTagDecl(type);
238 auto &ts = llvm::cast<TypeSystemClang>(*type.GetTypeSystem());
239 ts.GetMetadata(td)->SetIsForcefullyCompleted();
242 /// Complete a type from debug info, or mark it as forcefully completed if
243 /// there is no definition of the type in the current Module. Call this function
244 /// in contexts where the usual C++ rules require a type to be complete (base
245 /// class, member, etc.).
246 static void RequireCompleteType(CompilerType type) {
247 // Technically, enums can be incomplete too, but we don't handle those as they
248 // are emitted even under -flimit-debug-info.
249 if (!TypeSystemClang::IsCXXClassType(type))
252 if (type.GetCompleteType())
255 // No complete definition in this module. Mark the class as complete to
256 // satisfy local ast invariants, but make a note of the fact that
257 // it is not _really_ complete so we can later search for a definition in a
259 // Since we provide layout assistance, layouts of types containing this class
260 // will be correct even if we are not able to find the definition elsewhere.
261 ForcefullyCompleteType(type);
264 /// This function serves a similar purpose as RequireCompleteType above, but it
265 /// avoids completing the type if it is not immediately necessary. It only
266 /// ensures we _can_ complete the type later.
267 static void PrepareContextToReceiveMembers(TypeSystemClang &ast,
268 ClangASTImporter &ast_importer,
269 clang::DeclContext *decl_ctx,
271 const char *type_name_cstr) {
272 auto *tag_decl_ctx = clang::dyn_cast<clang::TagDecl>(decl_ctx);
274 return; // Non-tag context are always ready.
276 // We have already completed the type, or we have found its definition and are
277 // ready to complete it later (cf. ParseStructureLikeDIE).
278 if (tag_decl_ctx->isCompleteDefinition() || tag_decl_ctx->isBeingDefined())
281 // We reach this point of the tag was present in the debug info as a
282 // declaration only. If it was imported from another AST context (in the
283 // gmodules case), we can complete the type by doing a full import.
285 // If this type was not imported from an external AST, there's nothing to do.
286 CompilerType type = ast.GetTypeForDecl(tag_decl_ctx);
287 if (type && ast_importer.CanImport(type)) {
288 auto qual_type = ClangUtil::GetQualType(type);
289 if (ast_importer.RequireCompleteType(qual_type))
291 die.GetDWARF()->GetObjectFile()->GetModule()->ReportError(
292 "Unable to complete the Decl context for DIE '%s' at offset "
293 "0x%8.8x.\nPlease file a bug report.",
294 type_name_cstr ? type_name_cstr : "", die.GetOffset());
297 // We don't have a type definition and/or the import failed. We must
298 // forcefully complete the type to avoid crashes.
299 ForcefullyCompleteType(type);
302 ParsedDWARFTypeAttributes::ParsedDWARFTypeAttributes(const DWARFDIE &die) {
303 DWARFAttributes attributes;
304 size_t num_attributes = die.GetAttributes(attributes);
305 for (size_t i = 0; i < num_attributes; ++i) {
306 dw_attr_t attr = attributes.AttributeAtIndex(i);
307 DWARFFormValue form_value;
308 if (!attributes.ExtractFormValueAtIndex(i, form_value))
311 case DW_AT_abstract_origin:
312 abstract_origin = form_value;
315 case DW_AT_accessibility:
316 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
319 case DW_AT_artificial:
320 is_artificial = form_value.Boolean();
323 case DW_AT_bit_stride:
324 bit_stride = form_value.Unsigned();
327 case DW_AT_byte_size:
328 byte_size = form_value.Unsigned();
331 case DW_AT_byte_stride:
332 byte_stride = form_value.Unsigned();
335 case DW_AT_calling_convention:
336 calling_convention = form_value.Unsigned();
339 case DW_AT_containing_type:
340 containing_type = form_value;
343 case DW_AT_decl_file:
344 // die.GetCU() can differ if DW_AT_specification uses DW_FORM_ref_addr.
346 attributes.CompileUnitAtIndex(i)->GetFile(form_value.Unsigned()));
348 case DW_AT_decl_line:
349 decl.SetLine(form_value.Unsigned());
351 case DW_AT_decl_column:
352 decl.SetColumn(form_value.Unsigned());
355 case DW_AT_declaration:
356 is_forward_declaration = form_value.Boolean();
360 encoding = form_value.Unsigned();
363 case DW_AT_enum_class:
364 is_scoped_enum = form_value.Boolean();
368 is_explicit = form_value.Boolean();
372 if (form_value.Unsigned())
373 storage = clang::SC_Extern;
377 is_inline = form_value.Boolean();
380 case DW_AT_linkage_name:
381 case DW_AT_MIPS_linkage_name:
382 mangled_name = form_value.AsCString();
386 name.SetCString(form_value.AsCString());
389 case DW_AT_object_pointer:
390 object_pointer = form_value.Reference();
393 case DW_AT_signature:
394 signature = form_value;
397 case DW_AT_specification:
398 specification = form_value;
405 case DW_AT_virtuality:
406 is_virtual = form_value.Boolean();
409 case DW_AT_APPLE_objc_complete_type:
410 is_complete_objc_class = form_value.Signed();
413 case DW_AT_APPLE_objc_direct:
414 is_objc_direct_call = true;
417 case DW_AT_APPLE_runtime_class:
418 class_language = (LanguageType)form_value.Signed();
421 case DW_AT_GNU_vector:
422 is_vector = form_value.Boolean();
424 case DW_AT_export_symbols:
425 exports_symbols = form_value.Boolean();
431 static std::string GetUnitName(const DWARFDIE &die) {
432 if (DWARFUnit *unit = die.GetCU())
433 return unit->GetAbsolutePath().GetPath();
434 return "<missing DWARF unit path>";
437 TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
439 bool *type_is_new_ptr) {
441 *type_is_new_ptr = false;
446 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
449 SymbolFileDWARF *dwarf = die.GetDWARF();
451 DWARFDIE context_die;
452 clang::DeclContext *context =
453 GetClangDeclContextContainingDIE(die, &context_die);
455 dwarf->GetObjectFile()->GetModule()->LogMessage(
457 "DWARFASTParserClang::ParseTypeFromDWARF "
458 "(die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
459 die.GetOffset(), static_cast<void *>(context), context_die.GetOffset(),
460 die.GetTagAsCString(), die.GetName());
463 Type *type_ptr = dwarf->GetDIEToType().lookup(die.GetDIE());
464 if (type_ptr == DIE_IS_BEING_PARSED)
467 return type_ptr->shared_from_this();
468 // Set a bit that lets us know that we are currently parsing this
469 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
471 ParsedDWARFTypeAttributes attrs(die);
473 if (DWARFDIE signature_die = attrs.signature.Reference()) {
475 ParseTypeFromDWARF(sc, signature_die, type_is_new_ptr)) {
476 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
477 if (clang::DeclContext *decl_ctx =
478 GetCachedClangDeclContextForDIE(signature_die))
479 LinkDeclContextToDIE(decl_ctx, die);
486 *type_is_new_ptr = true;
488 const dw_tag_t tag = die.Tag();
494 case DW_TAG_base_type:
495 case DW_TAG_pointer_type:
496 case DW_TAG_reference_type:
497 case DW_TAG_rvalue_reference_type:
498 case DW_TAG_const_type:
499 case DW_TAG_restrict_type:
500 case DW_TAG_volatile_type:
501 case DW_TAG_atomic_type:
502 case DW_TAG_unspecified_type: {
503 type_sp = ParseTypeModifier(sc, die, attrs);
507 case DW_TAG_structure_type:
508 case DW_TAG_union_type:
509 case DW_TAG_class_type: {
510 type_sp = ParseStructureLikeDIE(sc, die, attrs);
514 case DW_TAG_enumeration_type: {
515 type_sp = ParseEnum(sc, die, attrs);
519 case DW_TAG_inlined_subroutine:
520 case DW_TAG_subprogram:
521 case DW_TAG_subroutine_type: {
522 type_sp = ParseSubroutine(die, attrs);
525 case DW_TAG_array_type: {
526 type_sp = ParseArrayType(die, attrs);
529 case DW_TAG_ptr_to_member_type: {
530 type_sp = ParsePointerToMemberType(die, attrs);
534 dwarf->GetObjectFile()->GetModule()->ReportError(
535 "{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and "
536 "attach the file at the start of this error message",
537 die.GetOffset(), tag, DW_TAG_value_to_name(tag));
541 // TODO: We should consider making the switch above exhaustive to simplify
542 // control flow in ParseTypeFromDWARF. Then, we could simply replace this
543 // return statement with a call to llvm_unreachable.
544 return UpdateSymbolContextScopeForType(sc, die, type_sp);
548 DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
550 ParsedDWARFTypeAttributes &attrs) {
551 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
553 SymbolFileDWARF *dwarf = die.GetDWARF();
554 const dw_tag_t tag = die.Tag();
555 LanguageType cu_language = SymbolFileDWARF::GetLanguage(*die.GetCU());
556 Type::ResolveState resolve_state = Type::ResolveState::Unresolved;
557 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
559 CompilerType clang_type;
561 if (tag == DW_TAG_typedef) {
562 // DeclContext will be populated when the clang type is materialized in
563 // Type::ResolveCompilerType.
564 PrepareContextToReceiveMembers(
565 m_ast, GetClangASTImporter(),
566 GetClangDeclContextContainingDIE(die, nullptr), die,
567 attrs.name.GetCString());
569 if (attrs.type.IsValid()) {
570 // Try to parse a typedef from the (DWARF embedded in the) Clang
571 // module file first as modules can contain typedef'ed
572 // structures that have no names like:
574 // typedef struct { int a; } Foo;
576 // In this case we will have a structure with no name and a
577 // typedef named "Foo" that points to this unnamed
578 // structure. The name in the typedef is the only identifier for
579 // the struct, so always try to get typedefs from Clang modules
582 // The type_sp returned will be empty if the typedef doesn't
583 // exist in a module file, so it is cheap to call this function
586 // If we don't do this we end up creating a TypeSP that says
587 // this is a typedef to type 0x123 (the DW_AT_type value would
588 // be 0x123 in the DW_TAG_typedef), and this is the unnamed
589 // structure type. We will have a hard time tracking down an
590 // unnammed structure type in the module debug info, so we make
591 // sure we don't get into this situation by always resolving
592 // typedefs from the module.
593 const DWARFDIE encoding_die = attrs.type.Reference();
595 // First make sure that the die that this is typedef'ed to _is_
596 // just a declaration (DW_AT_declaration == 1), not a full
597 // definition since template types can't be represented in
598 // modules since only concrete instances of templates are ever
599 // emitted and modules won't contain those
601 encoding_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
602 type_sp = ParseTypeFromClangModule(sc, die, log);
609 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", die.GetID(),
610 DW_TAG_value_to_name(tag), type_name_cstr,
611 encoding_uid.Reference());
617 case DW_TAG_unspecified_type:
618 if (attrs.name == "nullptr_t" || attrs.name == "decltype(nullptr)") {
619 resolve_state = Type::ResolveState::Full;
620 clang_type = m_ast.GetBasicType(eBasicTypeNullPtr);
623 // Fall through to base type below in case we can handle the type
627 case DW_TAG_base_type:
628 resolve_state = Type::ResolveState::Full;
629 clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
630 attrs.name.GetStringRef(), attrs.encoding,
631 attrs.byte_size.getValueOr(0) * 8);
634 case DW_TAG_pointer_type:
635 encoding_data_type = Type::eEncodingIsPointerUID;
637 case DW_TAG_reference_type:
638 encoding_data_type = Type::eEncodingIsLValueReferenceUID;
640 case DW_TAG_rvalue_reference_type:
641 encoding_data_type = Type::eEncodingIsRValueReferenceUID;
644 encoding_data_type = Type::eEncodingIsTypedefUID;
646 case DW_TAG_const_type:
647 encoding_data_type = Type::eEncodingIsConstUID;
649 case DW_TAG_restrict_type:
650 encoding_data_type = Type::eEncodingIsRestrictUID;
652 case DW_TAG_volatile_type:
653 encoding_data_type = Type::eEncodingIsVolatileUID;
655 case DW_TAG_atomic_type:
656 encoding_data_type = Type::eEncodingIsAtomicUID;
660 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID ||
661 encoding_data_type == Type::eEncodingIsTypedefUID)) {
662 if (tag == DW_TAG_pointer_type) {
663 DWARFDIE target_die = die.GetReferencedDIE(DW_AT_type);
665 if (target_die.GetAttributeValueAsUnsigned(DW_AT_APPLE_block, 0)) {
666 // Blocks have a __FuncPtr inside them which is a pointer to a
667 // function of the proper type.
669 for (DWARFDIE child_die : target_die.children()) {
670 if (!strcmp(child_die.GetAttributeValueAsString(DW_AT_name, ""),
672 DWARFDIE function_pointer_type =
673 child_die.GetReferencedDIE(DW_AT_type);
675 if (function_pointer_type) {
676 DWARFDIE function_type =
677 function_pointer_type.GetReferencedDIE(DW_AT_type);
679 bool function_type_is_new_pointer;
680 TypeSP lldb_function_type_sp = ParseTypeFromDWARF(
681 sc, function_type, &function_type_is_new_pointer);
683 if (lldb_function_type_sp) {
684 clang_type = m_ast.CreateBlockPointerType(
685 lldb_function_type_sp->GetForwardCompilerType());
686 encoding_data_type = Type::eEncodingIsUID;
688 resolve_state = Type::ResolveState::Full;
698 if (cu_language == eLanguageTypeObjC ||
699 cu_language == eLanguageTypeObjC_plus_plus) {
701 if (attrs.name == "id") {
703 dwarf->GetObjectFile()->GetModule()->LogMessage(
705 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
706 "is Objective-C 'id' built-in type.",
707 die.GetOffset(), die.GetTagAsCString(), die.GetName());
708 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
709 encoding_data_type = Type::eEncodingIsUID;
711 resolve_state = Type::ResolveState::Full;
712 } else if (attrs.name == "Class") {
714 dwarf->GetObjectFile()->GetModule()->LogMessage(
716 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
717 "is Objective-C 'Class' built-in type.",
718 die.GetOffset(), die.GetTagAsCString(), die.GetName());
719 clang_type = m_ast.GetBasicType(eBasicTypeObjCClass);
720 encoding_data_type = Type::eEncodingIsUID;
722 resolve_state = Type::ResolveState::Full;
723 } else if (attrs.name == "SEL") {
725 dwarf->GetObjectFile()->GetModule()->LogMessage(
727 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
728 "is Objective-C 'selector' built-in type.",
729 die.GetOffset(), die.GetTagAsCString(), die.GetName());
730 clang_type = m_ast.GetBasicType(eBasicTypeObjCSel);
731 encoding_data_type = Type::eEncodingIsUID;
733 resolve_state = Type::ResolveState::Full;
735 } else if (encoding_data_type == Type::eEncodingIsPointerUID &&
736 attrs.type.IsValid()) {
737 // Clang sometimes erroneously emits id as objc_object*. In that
738 // case we fix up the type to "id".
740 const DWARFDIE encoding_die = attrs.type.Reference();
742 if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type) {
743 llvm::StringRef struct_name = encoding_die.GetName();
744 if (struct_name == "objc_object") {
746 dwarf->GetObjectFile()->GetModule()->LogMessage(
748 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s "
749 "'%s' is 'objc_object*', which we overrode to "
751 die.GetOffset(), die.GetTagAsCString(), die.GetName());
752 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
753 encoding_data_type = Type::eEncodingIsUID;
755 resolve_state = Type::ResolveState::Full;
762 type_sp = std::make_shared<Type>(
763 die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
764 dwarf->GetUID(attrs.type.Reference()), encoding_data_type, &attrs.decl,
765 clang_type, resolve_state, TypePayloadClang(GetOwningClangModule(die)));
767 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
771 TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
773 ParsedDWARFTypeAttributes &attrs) {
774 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
776 SymbolFileDWARF *dwarf = die.GetDWARF();
777 const dw_tag_t tag = die.Tag();
780 if (attrs.is_forward_declaration) {
781 type_sp = ParseTypeFromClangModule(sc, die, log);
785 DWARFDeclContext die_decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
787 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
790 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
791 if (debug_map_symfile) {
792 // We weren't able to find a full declaration in this DWARF,
793 // see if we have a declaration anywhere else...
794 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
801 dwarf->GetObjectFile()->GetModule()->LogMessage(
803 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
804 "forward declaration, complete type is 0x%8.8" PRIx64,
805 static_cast<void *>(this), die.GetOffset(),
806 DW_TAG_value_to_name(tag), attrs.name.GetCString(),
810 // We found a real definition for this type elsewhere so lets use
811 // it and cache the fact that we found a complete type for this
813 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
814 clang::DeclContext *defn_decl_ctx =
815 GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
817 LinkDeclContextToDIE(defn_decl_ctx, die);
821 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
822 DW_TAG_value_to_name(tag), type_name_cstr);
824 CompilerType enumerator_clang_type;
825 CompilerType clang_type;
826 clang_type.SetCompilerType(
827 &m_ast, dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
829 if (attrs.type.IsValid()) {
830 Type *enumerator_type =
831 dwarf->ResolveTypeUID(attrs.type.Reference(), true);
833 enumerator_clang_type = enumerator_type->GetFullCompilerType();
836 if (!enumerator_clang_type) {
837 if (attrs.byte_size) {
838 enumerator_clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
839 "", DW_ATE_signed, *attrs.byte_size * 8);
841 enumerator_clang_type = m_ast.GetBasicType(eBasicTypeInt);
845 clang_type = m_ast.CreateEnumerationType(
846 attrs.name.GetCString(), GetClangDeclContextContainingDIE(die, nullptr),
847 GetOwningClangModule(die), attrs.decl, enumerator_clang_type,
848 attrs.is_scoped_enum);
850 enumerator_clang_type = m_ast.GetEnumerationIntegerType(clang_type);
853 LinkDeclContextToDIE(TypeSystemClang::GetDeclContextForType(clang_type), die);
855 type_sp = std::make_shared<Type>(
856 die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
857 dwarf->GetUID(attrs.type.Reference()), Type::eEncodingIsUID, &attrs.decl,
858 clang_type, Type::ResolveState::Forward,
859 TypePayloadClang(GetOwningClangModule(die)));
861 if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) {
862 if (die.HasChildren()) {
863 bool is_signed = false;
864 enumerator_clang_type.IsIntegerType(is_signed);
865 ParseChildEnumerators(clang_type, is_signed,
866 type_sp->GetByteSize(nullptr).getValueOr(0), die);
868 TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
870 dwarf->GetObjectFile()->GetModule()->ReportError(
871 "DWARF DIE at 0x%8.8x named \"%s\" was not able to start its "
872 "definition.\nPlease file a bug and attach the file at the "
873 "start of this error message",
874 die.GetOffset(), attrs.name.GetCString());
879 TypeSP DWARFASTParserClang::ParseSubroutine(const DWARFDIE &die,
880 ParsedDWARFTypeAttributes &attrs) {
881 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
884 SymbolFileDWARF *dwarf = die.GetDWARF();
885 const dw_tag_t tag = die.Tag();
887 bool is_variadic = false;
888 bool is_static = false;
889 bool has_template_params = false;
891 unsigned type_quals = 0;
893 std::string object_pointer_name;
894 if (attrs.object_pointer) {
895 const char *object_pointer_name_cstr = attrs.object_pointer.GetName();
896 if (object_pointer_name_cstr)
897 object_pointer_name = object_pointer_name_cstr;
900 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
901 DW_TAG_value_to_name(tag), type_name_cstr);
903 CompilerType return_clang_type;
904 Type *func_type = NULL;
906 if (attrs.type.IsValid())
907 func_type = dwarf->ResolveTypeUID(attrs.type.Reference(), true);
910 return_clang_type = func_type->GetForwardCompilerType();
912 return_clang_type = m_ast.GetBasicType(eBasicTypeVoid);
914 std::vector<CompilerType> function_param_types;
915 std::vector<clang::ParmVarDecl *> function_param_decls;
917 // Parse the function children for the parameters
919 DWARFDIE decl_ctx_die;
920 clang::DeclContext *containing_decl_ctx =
921 GetClangDeclContextContainingDIE(die, &decl_ctx_die);
922 const clang::Decl::Kind containing_decl_kind =
923 containing_decl_ctx->getDeclKind();
925 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_kind);
926 // Start off static. This will be set to false in
927 // ParseChildParameters(...) if we find a "this" parameters as the
933 if (die.HasChildren()) {
934 bool skip_artificial = true;
935 ParseChildParameters(containing_decl_ctx, die, skip_artificial, is_static,
936 is_variadic, has_template_params,
937 function_param_types, function_param_decls,
941 bool ignore_containing_context = false;
942 // Check for templatized class member functions. If we had any
943 // DW_TAG_template_type_parameter or DW_TAG_template_value_parameter
944 // the DW_TAG_subprogram DIE, then we can't let this become a method in
945 // a class. Why? Because templatized functions are only emitted if one
946 // of the templatized methods is used in the current compile unit and
947 // we will end up with classes that may or may not include these member
948 // functions and this means one class won't match another class
949 // definition and it affects our ability to use a class in the clang
950 // expression parser. So for the greater good, we currently must not
951 // allow any template member functions in a class definition.
952 if (is_cxx_method && has_template_params) {
953 ignore_containing_context = true;
954 is_cxx_method = false;
957 // clang_type will get the function prototype clang type after this
959 CompilerType clang_type = m_ast.CreateFunctionType(
960 return_clang_type, function_param_types.data(),
961 function_param_types.size(), is_variadic, type_quals);
964 bool type_handled = false;
965 if (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) {
966 ObjCLanguage::MethodName objc_method(attrs.name.GetStringRef(), true);
967 if (objc_method.IsValid(true)) {
968 CompilerType class_opaque_type;
969 ConstString class_name(objc_method.GetClassName());
971 TypeSP complete_objc_class_type_sp(
972 dwarf->FindCompleteObjCDefinitionTypeForDIE(DWARFDIE(),
975 if (complete_objc_class_type_sp) {
976 CompilerType type_clang_forward_type =
977 complete_objc_class_type_sp->GetForwardCompilerType();
978 if (TypeSystemClang::IsObjCObjectOrInterfaceType(
979 type_clang_forward_type))
980 class_opaque_type = type_clang_forward_type;
984 if (class_opaque_type) {
985 // If accessibility isn't set to anything valid, assume public
987 if (attrs.accessibility == eAccessNone)
988 attrs.accessibility = eAccessPublic;
990 clang::ObjCMethodDecl *objc_method_decl =
991 m_ast.AddMethodToObjCObjectType(
992 class_opaque_type, attrs.name.GetCString(), clang_type,
993 attrs.accessibility, attrs.is_artificial, is_variadic,
994 attrs.is_objc_direct_call);
995 type_handled = objc_method_decl != NULL;
997 LinkDeclContextToDIE(objc_method_decl, die);
998 m_ast.SetMetadataAsUserID(objc_method_decl, die.GetID());
1000 dwarf->GetObjectFile()->GetModule()->ReportError(
1001 "{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), "
1002 "please file a bug and attach the file at the start of "
1003 "this error message",
1004 die.GetOffset(), tag, DW_TAG_value_to_name(tag));
1007 } else if (is_cxx_method) {
1008 // Look at the parent of this DIE and see if is is a class or
1009 // struct and see if this is actually a C++ method
1010 Type *class_type = dwarf->ResolveType(decl_ctx_die);
1012 bool alternate_defn = false;
1013 if (class_type->GetID() != decl_ctx_die.GetID() ||
1014 IsClangModuleFwdDecl(decl_ctx_die)) {
1015 alternate_defn = true;
1017 // We uniqued the parent class of this function to another
1018 // class so we now need to associate all dies under
1019 // "decl_ctx_die" to DIEs in the DIE for "class_type"...
1020 DWARFDIE class_type_die = dwarf->GetDIE(class_type->GetID());
1022 if (class_type_die) {
1023 std::vector<DWARFDIE> failures;
1025 CopyUniqueClassMethodTypes(decl_ctx_die, class_type_die,
1026 class_type, failures);
1028 // FIXME do something with these failures that's
1029 // smarter than just dropping them on the ground.
1030 // Unfortunately classes don't like having stuff added
1031 // to them after their definitions are complete...
1033 Type *type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1034 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
1035 return type_ptr->shared_from_this();
1040 if (attrs.specification.IsValid()) {
1041 // We have a specification which we are going to base our
1042 // function prototype off of, so we need this type to be
1043 // completed so that the m_die_to_decl_ctx for the method in
1044 // the specification has a valid clang decl context.
1045 class_type->GetForwardCompilerType();
1046 // If we have a specification, then the function type should
1047 // have been made with the specification and not with this
1049 DWARFDIE spec_die = attrs.specification.Reference();
1050 clang::DeclContext *spec_clang_decl_ctx =
1051 GetClangDeclContextForDIE(spec_die);
1052 if (spec_clang_decl_ctx) {
1053 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
1055 dwarf->GetObjectFile()->GetModule()->ReportWarning(
1056 "0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x"
1058 die.GetID(), spec_die.GetOffset());
1060 type_handled = true;
1061 } else if (attrs.abstract_origin.IsValid()) {
1062 // We have a specification which we are going to base our
1063 // function prototype off of, so we need this type to be
1064 // completed so that the m_die_to_decl_ctx for the method in
1065 // the abstract origin has a valid clang decl context.
1066 class_type->GetForwardCompilerType();
1068 DWARFDIE abs_die = attrs.abstract_origin.Reference();
1069 clang::DeclContext *abs_clang_decl_ctx =
1070 GetClangDeclContextForDIE(abs_die);
1071 if (abs_clang_decl_ctx) {
1072 LinkDeclContextToDIE(abs_clang_decl_ctx, die);
1074 dwarf->GetObjectFile()->GetModule()->ReportWarning(
1075 "0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x"
1077 die.GetID(), abs_die.GetOffset());
1079 type_handled = true;
1081 CompilerType class_opaque_type =
1082 class_type->GetForwardCompilerType();
1083 if (TypeSystemClang::IsCXXClassType(class_opaque_type)) {
1084 if (class_opaque_type.IsBeingDefined() || alternate_defn) {
1085 if (!is_static && !die.HasChildren()) {
1086 // We have a C++ member function with no children (this
1087 // pointer!) and clang will get mad if we try and make
1088 // a function that isn't well formed in the DWARF, so
1089 // we will just skip it...
1090 type_handled = true;
1092 bool add_method = true;
1093 if (alternate_defn) {
1094 // If an alternate definition for the class exists,
1095 // then add the method only if an equivalent is not
1097 clang::CXXRecordDecl *record_decl =
1098 m_ast.GetAsCXXRecordDecl(
1099 class_opaque_type.GetOpaqueQualType());
1101 for (auto method_iter = record_decl->method_begin();
1102 method_iter != record_decl->method_end();
1104 clang::CXXMethodDecl *method_decl = *method_iter;
1105 if (method_decl->getNameInfo().getAsString() ==
1106 attrs.name.GetStringRef()) {
1107 if (method_decl->getType() ==
1108 ClangUtil::GetQualType(clang_type)) {
1110 LinkDeclContextToDIE(method_decl, die);
1111 type_handled = true;
1121 llvm::PrettyStackTraceFormat stack_trace(
1122 "SymbolFileDWARF::ParseType() is adding a method "
1123 "%s to class %s in DIE 0x%8.8" PRIx64 " from %s",
1124 attrs.name.GetCString(),
1125 class_type->GetName().GetCString(), die.GetID(),
1126 dwarf->GetObjectFile()
1131 const bool is_attr_used = false;
1132 // Neither GCC 4.2 nor clang++ currently set a valid
1133 // accessibility in the DWARF for C++ methods...
1134 // Default to public for now...
1135 if (attrs.accessibility == eAccessNone)
1136 attrs.accessibility = eAccessPublic;
1138 clang::CXXMethodDecl *cxx_method_decl =
1139 m_ast.AddMethodToCXXRecordType(
1140 class_opaque_type.GetOpaqueQualType(),
1141 attrs.name.GetCString(), attrs.mangled_name,
1142 clang_type, attrs.accessibility, attrs.is_virtual,
1143 is_static, attrs.is_inline, attrs.is_explicit,
1144 is_attr_used, attrs.is_artificial);
1146 type_handled = cxx_method_decl != NULL;
1147 // Artificial methods are always handled even when we
1148 // don't create a new declaration for them.
1149 type_handled |= attrs.is_artificial;
1151 if (cxx_method_decl) {
1152 LinkDeclContextToDIE(cxx_method_decl, die);
1154 ClangASTMetadata metadata;
1155 metadata.SetUserID(die.GetID());
1157 if (!object_pointer_name.empty()) {
1158 metadata.SetObjectPtrName(
1159 object_pointer_name.c_str());
1161 "Setting object pointer name: %s on method "
1163 object_pointer_name.c_str(),
1164 static_cast<void *>(cxx_method_decl));
1166 m_ast.SetMetadata(cxx_method_decl, metadata);
1168 ignore_containing_context = true;
1173 // We were asked to parse the type for a method in a
1174 // class, yet the class hasn't been asked to complete
1175 // itself through the clang::ExternalASTSource protocol,
1176 // so we need to just have the class complete itself and
1177 // do things the right way, then our
1178 // DIE should then have an entry in the
1179 // dwarf->GetDIEToType() map. First
1180 // we need to modify the dwarf->GetDIEToType() so it
1181 // doesn't think we are trying to parse this DIE
1183 dwarf->GetDIEToType()[die.GetDIE()] = NULL;
1185 // Now we get the full type to force our class type to
1186 // complete itself using the clang::ExternalASTSource
1187 // protocol which will parse all base classes and all
1188 // methods (including the method for this DIE).
1189 class_type->GetFullCompilerType();
1191 // The type for this DIE should have been filled in the
1192 // function call above
1193 Type *type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1194 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
1195 return type_ptr->shared_from_this();
1198 // FIXME This is fixing some even uglier behavior but we
1200 // uniq the methods of each class as well as the class
1201 // itself. <rdar://problem/11240464>
1202 type_handled = true;
1210 if (!type_handled) {
1211 clang::FunctionDecl *function_decl = nullptr;
1212 clang::FunctionDecl *template_function_decl = nullptr;
1214 if (attrs.abstract_origin.IsValid()) {
1215 DWARFDIE abs_die = attrs.abstract_origin.Reference();
1217 if (dwarf->ResolveType(abs_die)) {
1218 function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(
1219 GetCachedClangDeclContextForDIE(abs_die));
1221 if (function_decl) {
1222 LinkDeclContextToDIE(function_decl, die);
1227 if (!function_decl) {
1228 char *name_buf = nullptr;
1229 llvm::StringRef name = attrs.name.GetStringRef();
1231 // We currently generate function templates with template parameters in
1232 // their name. In order to get closer to the AST that clang generates
1233 // we want to strip these from the name when creating the AST.
1234 if (attrs.mangled_name) {
1235 llvm::ItaniumPartialDemangler D;
1236 if (!D.partialDemangle(attrs.mangled_name)) {
1237 name_buf = D.getFunctionBaseName(nullptr, nullptr);
1242 // We just have a function that isn't part of a class
1243 function_decl = m_ast.CreateFunctionDeclaration(
1244 ignore_containing_context ? m_ast.GetTranslationUnitDecl()
1245 : containing_decl_ctx,
1246 GetOwningClangModule(die), name, clang_type, attrs.storage,
1248 std::free(name_buf);
1250 if (has_template_params) {
1251 TypeSystemClang::TemplateParameterInfos template_param_infos;
1252 ParseTemplateParameterInfos(die, template_param_infos);
1253 template_function_decl = m_ast.CreateFunctionDeclaration(
1254 ignore_containing_context ? m_ast.GetTranslationUnitDecl()
1255 : containing_decl_ctx,
1256 GetOwningClangModule(die), attrs.name.GetStringRef(), clang_type,
1257 attrs.storage, attrs.is_inline);
1258 clang::FunctionTemplateDecl *func_template_decl =
1259 m_ast.CreateFunctionTemplateDecl(
1260 containing_decl_ctx, GetOwningClangModule(die),
1261 template_function_decl, template_param_infos);
1262 m_ast.CreateFunctionTemplateSpecializationInfo(
1263 template_function_decl, func_template_decl, template_param_infos);
1266 lldbassert(function_decl);
1268 if (function_decl) {
1269 LinkDeclContextToDIE(function_decl, die);
1271 if (!function_param_decls.empty()) {
1272 m_ast.SetFunctionParameters(function_decl, function_param_decls);
1273 if (template_function_decl)
1274 m_ast.SetFunctionParameters(template_function_decl,
1275 function_param_decls);
1278 ClangASTMetadata metadata;
1279 metadata.SetUserID(die.GetID());
1281 if (!object_pointer_name.empty()) {
1282 metadata.SetObjectPtrName(object_pointer_name.c_str());
1284 "Setting object pointer name: %s on function "
1286 object_pointer_name.c_str(),
1287 static_cast<void *>(function_decl));
1289 m_ast.SetMetadata(function_decl, metadata);
1294 return std::make_shared<Type>(
1295 die.GetID(), dwarf, attrs.name, llvm::None, nullptr, LLDB_INVALID_UID,
1296 Type::eEncodingIsUID, &attrs.decl, clang_type, Type::ResolveState::Full);
1299 TypeSP DWARFASTParserClang::ParseArrayType(const DWARFDIE &die,
1300 ParsedDWARFTypeAttributes &attrs) {
1301 SymbolFileDWARF *dwarf = die.GetDWARF();
1303 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1304 DW_TAG_value_to_name(tag), type_name_cstr);
1306 DWARFDIE type_die = attrs.type.Reference();
1307 Type *element_type = dwarf->ResolveTypeUID(type_die, true);
1312 llvm::Optional<SymbolFile::ArrayInfo> array_info = ParseChildArrayInfo(die);
1314 attrs.byte_stride = array_info->byte_stride;
1315 attrs.bit_stride = array_info->bit_stride;
1317 if (attrs.byte_stride == 0 && attrs.bit_stride == 0)
1318 attrs.byte_stride = element_type->GetByteSize(nullptr).getValueOr(0);
1319 CompilerType array_element_type = element_type->GetForwardCompilerType();
1320 RequireCompleteType(array_element_type);
1322 uint64_t array_element_bit_stride =
1323 attrs.byte_stride * 8 + attrs.bit_stride;
1324 CompilerType clang_type;
1325 if (array_info && array_info->element_orders.size() > 0) {
1326 uint64_t num_elements = 0;
1327 auto end = array_info->element_orders.rend();
1328 for (auto pos = array_info->element_orders.rbegin(); pos != end; ++pos) {
1329 num_elements = *pos;
1330 clang_type = m_ast.CreateArrayType(array_element_type, num_elements,
1332 array_element_type = clang_type;
1333 array_element_bit_stride = num_elements
1334 ? array_element_bit_stride * num_elements
1335 : array_element_bit_stride;
1339 m_ast.CreateArrayType(array_element_type, 0, attrs.is_vector);
1341 ConstString empty_name;
1342 TypeSP type_sp = std::make_shared<Type>(
1343 die.GetID(), dwarf, empty_name, array_element_bit_stride / 8, nullptr,
1344 dwarf->GetUID(type_die), Type::eEncodingIsUID, &attrs.decl, clang_type,
1345 Type::ResolveState::Full);
1346 type_sp->SetEncodingType(element_type);
1347 const clang::Type *type = ClangUtil::GetQualType(clang_type).getTypePtr();
1348 m_ast.SetMetadataAsUserID(type, die.GetID());
1352 TypeSP DWARFASTParserClang::ParsePointerToMemberType(
1353 const DWARFDIE &die, const ParsedDWARFTypeAttributes &attrs) {
1354 SymbolFileDWARF *dwarf = die.GetDWARF();
1355 Type *pointee_type = dwarf->ResolveTypeUID(attrs.type.Reference(), true);
1357 dwarf->ResolveTypeUID(attrs.containing_type.Reference(), true);
1359 CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType();
1360 CompilerType class_clang_type = class_type->GetForwardCompilerType();
1362 CompilerType clang_type = TypeSystemClang::CreateMemberPointerType(
1363 class_clang_type, pointee_clang_type);
1365 if (llvm::Optional<uint64_t> clang_type_size =
1366 clang_type.GetByteSize(nullptr)) {
1367 return std::make_shared<Type>(die.GetID(), dwarf, attrs.name,
1368 *clang_type_size, nullptr, LLDB_INVALID_UID,
1369 Type::eEncodingIsUID, nullptr, clang_type,
1370 Type::ResolveState::Forward);
1375 TypeSP DWARFASTParserClang::UpdateSymbolContextScopeForType(
1376 const SymbolContext &sc, const DWARFDIE &die, TypeSP type_sp) {
1380 SymbolFileDWARF *dwarf = die.GetDWARF();
1381 TypeList &type_list = dwarf->GetTypeList();
1382 DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
1383 dw_tag_t sc_parent_tag = sc_parent_die.Tag();
1385 SymbolContextScope *symbol_context_scope = nullptr;
1386 if (sc_parent_tag == DW_TAG_compile_unit ||
1387 sc_parent_tag == DW_TAG_partial_unit) {
1388 symbol_context_scope = sc.comp_unit;
1389 } else if (sc.function != nullptr && sc_parent_die) {
1390 symbol_context_scope =
1391 sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
1392 if (symbol_context_scope == nullptr)
1393 symbol_context_scope = sc.function;
1395 symbol_context_scope = sc.module_sp.get();
1398 if (symbol_context_scope != nullptr)
1399 type_sp->SetSymbolContextScope(symbol_context_scope);
1401 // We are ready to put this type into the uniqued list up at the module
1403 type_list.Insert(type_sp);
1405 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1410 DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
1411 const DWARFDIE &die,
1412 ParsedDWARFTypeAttributes &attrs) {
1414 CompilerType clang_type;
1415 const dw_tag_t tag = die.Tag();
1416 SymbolFileDWARF *dwarf = die.GetDWARF();
1417 LanguageType cu_language = SymbolFileDWARF::GetLanguage(*die.GetCU());
1418 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_TYPE_COMPLETION |
1421 // UniqueDWARFASTType is large, so don't create a local variables on the
1422 // stack, put it on the heap. This function is often called recursively and
1423 // clang isn't good at sharing the stack space for variables in different
1425 auto unique_ast_entry_up = std::make_unique<UniqueDWARFASTType>();
1427 ConstString unique_typename(attrs.name);
1428 Declaration unique_decl(attrs.decl);
1431 if (Language::LanguageIsCPlusPlus(cu_language)) {
1432 // For C++, we rely solely upon the one definition rule that says
1433 // only one thing can exist at a given decl context. We ignore the
1434 // file and line that things are declared on.
1435 std::string qualified_name;
1436 if (die.GetQualifiedName(qualified_name))
1437 unique_typename = ConstString(qualified_name);
1438 unique_decl.Clear();
1441 if (dwarf->GetUniqueDWARFASTTypeMap().Find(
1442 unique_typename, die, unique_decl, attrs.byte_size.getValueOr(-1),
1443 *unique_ast_entry_up)) {
1444 type_sp = unique_ast_entry_up->m_type_sp;
1446 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1447 LinkDeclContextToDIE(
1448 GetCachedClangDeclContextForDIE(unique_ast_entry_up->m_die), die);
1454 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1455 DW_TAG_value_to_name(tag), type_name_cstr);
1457 int tag_decl_kind = -1;
1458 AccessType default_accessibility = eAccessNone;
1459 if (tag == DW_TAG_structure_type) {
1460 tag_decl_kind = clang::TTK_Struct;
1461 default_accessibility = eAccessPublic;
1462 } else if (tag == DW_TAG_union_type) {
1463 tag_decl_kind = clang::TTK_Union;
1464 default_accessibility = eAccessPublic;
1465 } else if (tag == DW_TAG_class_type) {
1466 tag_decl_kind = clang::TTK_Class;
1467 default_accessibility = eAccessPrivate;
1470 if (attrs.byte_size && *attrs.byte_size == 0 && attrs.name &&
1471 !die.HasChildren() && cu_language == eLanguageTypeObjC) {
1472 // Work around an issue with clang at the moment where forward
1473 // declarations for objective C classes are emitted as:
1474 // DW_TAG_structure_type [2]
1475 // DW_AT_name( "ForwardObjcClass" )
1476 // DW_AT_byte_size( 0x00 )
1477 // DW_AT_decl_file( "..." )
1478 // DW_AT_decl_line( 1 )
1480 // Note that there is no DW_AT_declaration and there are no children,
1481 // and the byte size is zero.
1482 attrs.is_forward_declaration = true;
1485 if (attrs.class_language == eLanguageTypeObjC ||
1486 attrs.class_language == eLanguageTypeObjC_plus_plus) {
1487 if (!attrs.is_complete_objc_class &&
1488 die.Supports_DW_AT_APPLE_objc_complete_type()) {
1489 // We have a valid eSymbolTypeObjCClass class symbol whose name
1490 // matches the current objective C class that we are trying to find
1491 // and this DIE isn't the complete definition (we checked
1492 // is_complete_objc_class above and know it is false), so the real
1493 // definition is in here somewhere
1495 dwarf->FindCompleteObjCDefinitionTypeForDIE(die, attrs.name, true);
1498 SymbolFileDWARFDebugMap *debug_map_symfile =
1499 dwarf->GetDebugMapSymfile();
1500 if (debug_map_symfile) {
1501 // We weren't able to find a full declaration in this DWARF,
1502 // see if we have a declaration anywhere else...
1503 type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE(
1504 die, attrs.name, true);
1510 dwarf->GetObjectFile()->GetModule()->LogMessage(
1512 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an "
1513 "incomplete objc type, complete type is 0x%8.8" PRIx64,
1514 static_cast<void *>(this), die.GetOffset(),
1515 DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1519 // We found a real definition for this type elsewhere so lets use
1520 // it and cache the fact that we found a complete type for this
1522 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1528 if (attrs.is_forward_declaration) {
1529 // We have a forward declaration to a type and we need to try and
1530 // find a full declaration. We look in the current type index just in
1531 // case we have a forward declaration followed by an actual
1532 // declarations in the DWARF. If this fails, we need to look
1535 dwarf->GetObjectFile()->GetModule()->LogMessage(
1537 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
1538 "forward declaration, trying to find complete type",
1539 static_cast<void *>(this), die.GetOffset(), DW_TAG_value_to_name(tag),
1540 attrs.name.GetCString());
1543 // See if the type comes from a Clang module and if so, track down
1545 type_sp = ParseTypeFromClangModule(sc, die, log);
1549 DWARFDeclContext die_decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
1551 // type_sp = FindDefinitionTypeForDIE (dwarf_cu, die,
1552 // type_name_const_str);
1553 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
1556 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
1557 if (debug_map_symfile) {
1558 // We weren't able to find a full declaration in this DWARF, see
1559 // if we have a declaration anywhere else...
1560 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
1567 dwarf->GetObjectFile()->GetModule()->LogMessage(
1569 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
1570 "forward declaration, complete type is 0x%8.8" PRIx64,
1571 static_cast<void *>(this), die.GetOffset(),
1572 DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1576 // We found a real definition for this type elsewhere so lets use
1577 // it and cache the fact that we found a complete type for this die
1578 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1579 clang::DeclContext *defn_decl_ctx =
1580 GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
1582 LinkDeclContextToDIE(defn_decl_ctx, die);
1586 assert(tag_decl_kind != -1);
1587 bool clang_type_was_created = false;
1588 clang_type.SetCompilerType(
1589 &m_ast, dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
1591 clang::DeclContext *decl_ctx =
1592 GetClangDeclContextContainingDIE(die, nullptr);
1594 PrepareContextToReceiveMembers(m_ast, GetClangASTImporter(), decl_ctx, die,
1595 attrs.name.GetCString());
1597 if (attrs.accessibility == eAccessNone && decl_ctx) {
1598 // Check the decl context that contains this class/struct/union. If
1599 // it is a class we must give it an accessibility.
1600 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
1601 if (DeclKindIsCXXClass(containing_decl_kind))
1602 attrs.accessibility = default_accessibility;
1605 ClangASTMetadata metadata;
1606 metadata.SetUserID(die.GetID());
1607 metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual(die));
1609 if (attrs.name.GetStringRef().contains('<')) {
1610 TypeSystemClang::TemplateParameterInfos template_param_infos;
1611 if (ParseTemplateParameterInfos(die, template_param_infos)) {
1612 clang::ClassTemplateDecl *class_template_decl =
1613 m_ast.ParseClassTemplateDecl(
1614 decl_ctx, GetOwningClangModule(die), attrs.accessibility,
1615 attrs.name.GetCString(), tag_decl_kind, template_param_infos);
1616 if (!class_template_decl) {
1618 dwarf->GetObjectFile()->GetModule()->LogMessage(
1620 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" "
1621 "clang::ClassTemplateDecl failed to return a decl.",
1622 static_cast<void *>(this), die.GetOffset(),
1623 DW_TAG_value_to_name(tag), attrs.name.GetCString());
1628 clang::ClassTemplateSpecializationDecl *class_specialization_decl =
1629 m_ast.CreateClassTemplateSpecializationDecl(
1630 decl_ctx, GetOwningClangModule(die), class_template_decl,
1631 tag_decl_kind, template_param_infos);
1632 clang_type = m_ast.CreateClassTemplateSpecializationType(
1633 class_specialization_decl);
1634 clang_type_was_created = true;
1636 m_ast.SetMetadata(class_template_decl, metadata);
1637 m_ast.SetMetadata(class_specialization_decl, metadata);
1641 if (!clang_type_was_created) {
1642 clang_type_was_created = true;
1643 clang_type = m_ast.CreateRecordType(
1644 decl_ctx, GetOwningClangModule(die), attrs.accessibility,
1645 attrs.name.GetCString(), tag_decl_kind, attrs.class_language,
1646 &metadata, attrs.exports_symbols);
1650 // Store a forward declaration to this class type in case any
1651 // parameters in any class methods need it for the clang types for
1652 // function prototypes.
1653 LinkDeclContextToDIE(m_ast.GetDeclContextForType(clang_type), die);
1654 type_sp = std::make_shared<Type>(
1655 die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
1656 LLDB_INVALID_UID, Type::eEncodingIsUID, &attrs.decl, clang_type,
1657 Type::ResolveState::Forward,
1658 TypePayloadClang(OptionalClangModuleID(), attrs.is_complete_objc_class));
1660 // Add our type to the unique type map so we don't end up creating many
1661 // copies of the same type over and over in the ASTContext for our
1663 unique_ast_entry_up->m_type_sp = type_sp;
1664 unique_ast_entry_up->m_die = die;
1665 unique_ast_entry_up->m_declaration = unique_decl;
1666 unique_ast_entry_up->m_byte_size = attrs.byte_size.getValueOr(0);
1667 dwarf->GetUniqueDWARFASTTypeMap().Insert(unique_typename,
1668 *unique_ast_entry_up);
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 the byte size of the record is specified then overwrite the size
1688 // that would be computed by Clang. This is only needed as LLDB's
1689 // TypeSystemClang is always in C++ mode, but some compilers such as
1690 // GCC and Clang give empty structs a size of 0 in C mode (in contrast to
1691 // the size of 1 for empty structs that would be computed in C++ mode).
1692 if (attrs.byte_size) {
1693 clang::RecordDecl *record_decl =
1694 TypeSystemClang::GetAsRecordDecl(clang_type);
1696 ClangASTImporter::LayoutInfo layout;
1697 layout.bit_size = *attrs.byte_size * 8;
1698 GetClangASTImporter().SetRecordLayout(record_decl, layout);
1701 } else if (clang_type_was_created) {
1702 // Start the definition if the class is not objective C since the
1703 // underlying decls respond to isCompleteDefinition(). Objective
1704 // C decls don't respond to isCompleteDefinition() so we can't
1705 // start the declaration definition right away. For C++
1706 // class/union/structs we want to start the definition in case the
1707 // class is needed as the declaration context for a contained class
1708 // or type without the need to complete that type..
1710 if (attrs.class_language != eLanguageTypeObjC &&
1711 attrs.class_language != eLanguageTypeObjC_plus_plus)
1712 TypeSystemClang::StartTagDeclarationDefinition(clang_type);
1714 // Leave this as a forward declaration until we need to know the
1715 // details of the type. lldb_private::Type will automatically call
1716 // the SymbolFile virtual function
1717 // "SymbolFileDWARF::CompleteType(Type *)" When the definition
1718 // needs to be defined.
1719 assert(!dwarf->GetForwardDeclClangTypeToDie().count(
1720 ClangUtil::RemoveFastQualifiers(clang_type)
1721 .GetOpaqueQualType()) &&
1722 "Type already in the forward declaration map!");
1723 // Can't assume m_ast.GetSymbolFile() is actually a
1724 // SymbolFileDWARF, it can be a SymbolFileDWARFDebugMap for Apple
1726 dwarf->GetForwardDeclDieToClangType()[die.GetDIE()] =
1727 clang_type.GetOpaqueQualType();
1728 dwarf->GetForwardDeclClangTypeToDie().try_emplace(
1729 ClangUtil::RemoveFastQualifiers(clang_type).GetOpaqueQualType(),
1731 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
1735 // If we made a clang type, set the trivial abi if applicable: We only
1736 // do this for pass by value - which implies the Trivial ABI. There
1737 // isn't a way to assert that something that would normally be pass by
1738 // value is pass by reference, so we ignore that attribute if set.
1739 if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_value) {
1740 clang::CXXRecordDecl *record_decl =
1741 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
1742 if (record_decl && record_decl->getDefinition()) {
1743 record_decl->setHasTrivialSpecialMemberForCall();
1747 if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_reference) {
1748 clang::CXXRecordDecl *record_decl =
1749 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
1751 record_decl->setArgPassingRestrictions(
1752 clang::RecordDecl::APK_CannotPassInRegs);
1757 // DWARF parsing functions
1759 class DWARFASTParserClang::DelayedAddObjCClassProperty {
1761 DelayedAddObjCClassProperty(
1762 const CompilerType &class_opaque_type, const char *property_name,
1763 const CompilerType &property_opaque_type, // The property type is only
1764 // required if you don't have an
1766 clang::ObjCIvarDecl *ivar_decl, const char *property_setter_name,
1767 const char *property_getter_name, uint32_t property_attributes,
1768 const ClangASTMetadata *metadata)
1769 : m_class_opaque_type(class_opaque_type), m_property_name(property_name),
1770 m_property_opaque_type(property_opaque_type), m_ivar_decl(ivar_decl),
1771 m_property_setter_name(property_setter_name),
1772 m_property_getter_name(property_getter_name),
1773 m_property_attributes(property_attributes) {
1774 if (metadata != nullptr) {
1775 m_metadata_up = std::make_unique<ClangASTMetadata>();
1776 *m_metadata_up = *metadata;
1780 DelayedAddObjCClassProperty(const DelayedAddObjCClassProperty &rhs) {
1784 DelayedAddObjCClassProperty &
1785 operator=(const DelayedAddObjCClassProperty &rhs) {
1786 m_class_opaque_type = rhs.m_class_opaque_type;
1787 m_property_name = rhs.m_property_name;
1788 m_property_opaque_type = rhs.m_property_opaque_type;
1789 m_ivar_decl = rhs.m_ivar_decl;
1790 m_property_setter_name = rhs.m_property_setter_name;
1791 m_property_getter_name = rhs.m_property_getter_name;
1792 m_property_attributes = rhs.m_property_attributes;
1794 if (rhs.m_metadata_up) {
1795 m_metadata_up = std::make_unique<ClangASTMetadata>();
1796 *m_metadata_up = *rhs.m_metadata_up;
1802 return TypeSystemClang::AddObjCClassProperty(
1803 m_class_opaque_type, m_property_name, m_property_opaque_type,
1804 m_ivar_decl, m_property_setter_name, m_property_getter_name,
1805 m_property_attributes, m_metadata_up.get());
1809 CompilerType m_class_opaque_type;
1810 const char *m_property_name;
1811 CompilerType m_property_opaque_type;
1812 clang::ObjCIvarDecl *m_ivar_decl;
1813 const char *m_property_setter_name;
1814 const char *m_property_getter_name;
1815 uint32_t m_property_attributes;
1816 std::unique_ptr<ClangASTMetadata> m_metadata_up;
1819 bool DWARFASTParserClang::ParseTemplateDIE(
1820 const DWARFDIE &die,
1821 TypeSystemClang::TemplateParameterInfos &template_param_infos) {
1822 const dw_tag_t tag = die.Tag();
1823 bool is_template_template_argument = false;
1826 case DW_TAG_GNU_template_parameter_pack: {
1827 template_param_infos.packed_args =
1828 std::make_unique<TypeSystemClang::TemplateParameterInfos>();
1829 for (DWARFDIE child_die : die.children()) {
1830 if (!ParseTemplateDIE(child_die, *template_param_infos.packed_args))
1833 if (const char *name = die.GetName()) {
1834 template_param_infos.pack_name = name;
1838 case DW_TAG_GNU_template_template_param:
1839 is_template_template_argument = true;
1841 case DW_TAG_template_type_parameter:
1842 case DW_TAG_template_value_parameter: {
1843 DWARFAttributes attributes;
1844 const size_t num_attributes = die.GetAttributes(attributes);
1845 const char *name = nullptr;
1846 const char *template_name = nullptr;
1847 CompilerType clang_type;
1848 uint64_t uval64 = 0;
1849 bool uval64_valid = false;
1850 if (num_attributes > 0) {
1851 DWARFFormValue form_value;
1852 for (size_t i = 0; i < num_attributes; ++i) {
1853 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1857 if (attributes.ExtractFormValueAtIndex(i, form_value))
1858 name = form_value.AsCString();
1861 case DW_AT_GNU_template_name:
1862 if (attributes.ExtractFormValueAtIndex(i, form_value))
1863 template_name = form_value.AsCString();
1867 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1868 Type *lldb_type = die.ResolveTypeUID(form_value.Reference());
1870 clang_type = lldb_type->GetForwardCompilerType();
1874 case DW_AT_const_value:
1875 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1876 uval64_valid = true;
1877 uval64 = form_value.Unsigned();
1885 clang::ASTContext &ast = m_ast.getASTContext();
1887 clang_type = m_ast.GetBasicType(eBasicTypeVoid);
1889 if (!is_template_template_argument) {
1890 bool is_signed = false;
1891 if (name && name[0])
1892 template_param_infos.names.push_back(name);
1894 template_param_infos.names.push_back(NULL);
1896 // Get the signed value for any integer or enumeration if available
1897 clang_type.IsIntegerOrEnumerationType(is_signed);
1899 if (tag == DW_TAG_template_value_parameter && uval64_valid) {
1900 llvm::Optional<uint64_t> size = clang_type.GetBitSize(nullptr);
1903 llvm::APInt apint(*size, uval64, is_signed);
1904 template_param_infos.args.push_back(
1905 clang::TemplateArgument(ast, llvm::APSInt(apint, !is_signed),
1906 ClangUtil::GetQualType(clang_type)));
1908 template_param_infos.args.push_back(
1909 clang::TemplateArgument(ClangUtil::GetQualType(clang_type)));
1912 auto *tplt_type = m_ast.CreateTemplateTemplateParmDecl(template_name);
1913 template_param_infos.names.push_back(name);
1914 template_param_infos.args.push_back(
1915 clang::TemplateArgument(clang::TemplateName(tplt_type)));
1927 bool DWARFASTParserClang::ParseTemplateParameterInfos(
1928 const DWARFDIE &parent_die,
1929 TypeSystemClang::TemplateParameterInfos &template_param_infos) {
1934 for (DWARFDIE die : parent_die.children()) {
1935 const dw_tag_t tag = die.Tag();
1938 case DW_TAG_template_type_parameter:
1939 case DW_TAG_template_value_parameter:
1940 case DW_TAG_GNU_template_parameter_pack:
1941 case DW_TAG_GNU_template_template_param:
1942 ParseTemplateDIE(die, template_param_infos);
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 // Parse members and base classes first
1984 std::vector<DWARFDIE> member_function_dies;
1986 DelayedPropertyList delayed_properties;
1987 ParseChildMembers(die, clang_type, bases, member_function_dies,
1988 delayed_properties, default_accessibility, layout_info);
1990 // Now parse any methods if there were any...
1991 for (const DWARFDIE &die : member_function_dies)
1992 dwarf->ResolveType(die);
1994 if (type_is_objc_object_or_interface) {
1995 ConstString class_name(clang_type.GetTypeName());
1997 dwarf->GetObjCMethods(class_name, [&](DWARFDIE method_die) {
1998 method_die.ResolveType();
2002 for (DelayedPropertyList::iterator pi = delayed_properties.begin(),
2003 pe = delayed_properties.end();
2009 if (!bases.empty()) {
2010 // Make sure all base classes refer to complete types and not forward
2011 // declarations. If we don't do this, clang will crash with an
2012 // assertion in the call to clang_type.TransferBaseClasses()
2013 for (const auto &base_class : bases) {
2014 clang::TypeSourceInfo *type_source_info =
2015 base_class->getTypeSourceInfo();
2016 if (type_source_info)
2017 RequireCompleteType(m_ast.GetType(type_source_info->getType()));
2020 m_ast.TransferBaseClasses(clang_type.GetOpaqueQualType(),
2025 m_ast.AddMethodOverridesForCXXRecordType(clang_type.GetOpaqueQualType());
2026 TypeSystemClang::BuildIndirectFields(clang_type);
2027 TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
2029 if (!layout_info.field_offsets.empty() || !layout_info.base_offsets.empty() ||
2030 !layout_info.vbase_offsets.empty()) {
2032 layout_info.bit_size = type->GetByteSize(nullptr).getValueOr(0) * 8;
2033 if (layout_info.bit_size == 0)
2034 layout_info.bit_size =
2035 die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2037 clang::CXXRecordDecl *record_decl =
2038 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
2040 GetClangASTImporter().SetRecordLayout(record_decl, layout_info);
2043 return (bool)clang_type;
2046 bool DWARFASTParserClang::CompleteEnumType(const DWARFDIE &die,
2047 lldb_private::Type *type,
2048 CompilerType &clang_type) {
2049 if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) {
2050 if (die.HasChildren()) {
2051 bool is_signed = false;
2052 clang_type.IsIntegerType(is_signed);
2053 ParseChildEnumerators(clang_type, is_signed,
2054 type->GetByteSize(nullptr).getValueOr(0), die);
2056 TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
2058 return (bool)clang_type;
2061 bool DWARFASTParserClang::CompleteTypeFromDWARF(const DWARFDIE &die,
2062 lldb_private::Type *type,
2063 CompilerType &clang_type) {
2064 SymbolFileDWARF *dwarf = die.GetDWARF();
2066 std::lock_guard<std::recursive_mutex> guard(
2067 dwarf->GetObjectFile()->GetModule()->GetMutex());
2069 // Disable external storage for this type so we don't get anymore
2070 // clang::ExternalASTSource queries for this type.
2071 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), false);
2076 const dw_tag_t tag = die.Tag();
2079 nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
2081 dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
2082 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
2083 die.GetID(), die.GetTagAsCString(), type->GetName().AsCString());
2085 DWARFAttributes attributes;
2087 case DW_TAG_structure_type:
2088 case DW_TAG_union_type:
2089 case DW_TAG_class_type:
2090 return CompleteRecordType(die, type, clang_type);
2091 case DW_TAG_enumeration_type:
2092 return CompleteEnumType(die, type, clang_type);
2094 assert(false && "not a forward clang type decl!");
2101 void DWARFASTParserClang::EnsureAllDIEsInDeclContextHaveBeenParsed(
2102 lldb_private::CompilerDeclContext decl_context) {
2103 auto opaque_decl_ctx =
2104 (clang::DeclContext *)decl_context.GetOpaqueDeclContext();
2105 for (auto it = m_decl_ctx_to_die.find(opaque_decl_ctx);
2106 it != m_decl_ctx_to_die.end() && it->first == opaque_decl_ctx;
2107 it = m_decl_ctx_to_die.erase(it))
2108 for (DWARFDIE decl : it->second.children())
2109 GetClangDeclForDIE(decl);
2112 CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) {
2113 clang::Decl *clang_decl = GetClangDeclForDIE(die);
2114 if (clang_decl != nullptr)
2115 return m_ast.GetCompilerDecl(clang_decl);
2116 return CompilerDecl();
2120 DWARFASTParserClang::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) {
2121 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die);
2123 return m_ast.CreateDeclContext(clang_decl_ctx);
2124 return CompilerDeclContext();
2128 DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) {
2129 clang::DeclContext *clang_decl_ctx =
2130 GetClangDeclContextContainingDIE(die, nullptr);
2132 return m_ast.CreateDeclContext(clang_decl_ctx);
2133 return CompilerDeclContext();
2136 size_t DWARFASTParserClang::ParseChildEnumerators(
2137 lldb_private::CompilerType &clang_type, bool is_signed,
2138 uint32_t enumerator_byte_size, const DWARFDIE &parent_die) {
2142 size_t enumerators_added = 0;
2144 for (DWARFDIE die : parent_die.children()) {
2145 const dw_tag_t tag = die.Tag();
2146 if (tag == DW_TAG_enumerator) {
2147 DWARFAttributes attributes;
2148 const size_t num_child_attributes = die.GetAttributes(attributes);
2149 if (num_child_attributes > 0) {
2150 const char *name = nullptr;
2151 bool got_value = false;
2152 int64_t enum_value = 0;
2156 for (i = 0; i < num_child_attributes; ++i) {
2157 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2158 DWARFFormValue form_value;
2159 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2161 case DW_AT_const_value:
2164 enum_value = form_value.Signed();
2166 enum_value = form_value.Unsigned();
2170 name = form_value.AsCString();
2173 case DW_AT_description:
2175 case DW_AT_decl_file:
2176 decl.SetFile(attributes.CompileUnitAtIndex(i)->GetFile(
2177 form_value.Unsigned()));
2179 case DW_AT_decl_line:
2180 decl.SetLine(form_value.Unsigned());
2182 case DW_AT_decl_column:
2183 decl.SetColumn(form_value.Unsigned());
2191 if (name && name[0] && got_value) {
2192 m_ast.AddEnumerationValueToEnumerationType(
2193 clang_type, decl, name, enum_value, enumerator_byte_size * 8);
2194 ++enumerators_added;
2199 return enumerators_added;
2202 Function *DWARFASTParserClang::ParseFunctionFromDWARF(CompileUnit &comp_unit,
2203 const DWARFDIE &die) {
2204 DWARFRangeList func_ranges;
2205 const char *name = nullptr;
2206 const char *mangled = nullptr;
2209 int decl_column = 0;
2212 int call_column = 0;
2213 DWARFExpression frame_base;
2215 const dw_tag_t tag = die.Tag();
2217 if (tag != DW_TAG_subprogram)
2220 if (die.GetDIENamesAndRanges(name, mangled, func_ranges, decl_file, decl_line,
2221 decl_column, call_file, call_line, call_column,
2224 // Union of all ranges in the function DIE (if the function is
2226 AddressRange func_range;
2227 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase(0);
2228 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd(0);
2229 if (lowest_func_addr != LLDB_INVALID_ADDRESS &&
2230 lowest_func_addr <= highest_func_addr) {
2231 ModuleSP module_sp(die.GetModule());
2232 func_range.GetBaseAddress().ResolveAddressUsingFileSections(
2233 lowest_func_addr, module_sp->GetSectionList());
2234 if (func_range.GetBaseAddress().IsValid())
2235 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
2238 if (func_range.GetBaseAddress().IsValid()) {
2241 func_name.SetValue(ConstString(mangled), true);
2242 else if ((die.GetParent().Tag() == DW_TAG_compile_unit ||
2243 die.GetParent().Tag() == DW_TAG_partial_unit) &&
2244 Language::LanguageIsCPlusPlus(
2245 SymbolFileDWARF::GetLanguage(*die.GetCU())) &&
2246 !Language::LanguageIsObjC(
2247 SymbolFileDWARF::GetLanguage(*die.GetCU())) &&
2248 name && strcmp(name, "main") != 0) {
2249 // If the mangled name is not present in the DWARF, generate the
2250 // demangled name using the decl context. We skip if the function is
2251 // "main" as its name is never mangled.
2252 bool is_static = false;
2253 bool is_variadic = false;
2254 bool has_template_params = false;
2255 unsigned type_quals = 0;
2256 std::vector<CompilerType> param_types;
2257 std::vector<clang::ParmVarDecl *> param_decls;
2260 DWARFDeclContext decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
2261 sstr << decl_ctx.GetQualifiedName();
2263 clang::DeclContext *containing_decl_ctx =
2264 GetClangDeclContextContainingDIE(die, nullptr);
2265 ParseChildParameters(containing_decl_ctx, die, true, is_static,
2266 is_variadic, has_template_params, param_types,
2267 param_decls, type_quals);
2269 for (size_t i = 0; i < param_types.size(); i++) {
2272 sstr << param_types[i].GetTypeName();
2277 if (type_quals & clang::Qualifiers::Const)
2280 func_name.SetValue(ConstString(sstr.GetString()), false);
2282 func_name.SetValue(ConstString(name), false);
2285 std::unique_ptr<Declaration> decl_up;
2286 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
2287 decl_up = std::make_unique<Declaration>(die.GetCU()->GetFile(decl_file),
2288 decl_line, decl_column);
2290 SymbolFileDWARF *dwarf = die.GetDWARF();
2291 // Supply the type _only_ if it has already been parsed
2292 Type *func_type = dwarf->GetDIEToType().lookup(die.GetDIE());
2294 assert(func_type == nullptr || func_type != DIE_IS_BEING_PARSED);
2296 if (dwarf->FixupAddress(func_range.GetBaseAddress())) {
2297 const user_id_t func_user_id = die.GetID();
2299 std::make_shared<Function>(&comp_unit,
2300 func_user_id, // UserID is the DIE offset
2301 func_user_id, func_name, func_type,
2302 func_range); // first address range
2304 if (func_sp.get() != nullptr) {
2305 if (frame_base.IsValid())
2306 func_sp->GetFrameBaseExpression() = frame_base;
2307 comp_unit.AddFunction(func_sp);
2308 return func_sp.get();
2316 void DWARFASTParserClang::ParseSingleMember(
2317 const DWARFDIE &die, const DWARFDIE &parent_die,
2318 const lldb_private::CompilerType &class_clang_type,
2319 lldb::AccessType default_accessibility,
2320 DelayedPropertyList &delayed_properties,
2321 lldb_private::ClangASTImporter::LayoutInfo &layout_info,
2322 FieldInfo &last_field_info) {
2323 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2324 const dw_tag_t tag = die.Tag();
2325 // Get the parent byte size so we can verify any members will fit
2326 const uint64_t parent_byte_size =
2327 parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
2328 const uint64_t parent_bit_size =
2329 parent_byte_size == UINT64_MAX ? UINT64_MAX : parent_byte_size * 8;
2331 DWARFAttributes attributes;
2332 const size_t num_attributes = die.GetAttributes(attributes);
2333 if (num_attributes == 0)
2336 const char *name = nullptr;
2337 const char *prop_name = nullptr;
2338 const char *prop_getter_name = nullptr;
2339 const char *prop_setter_name = nullptr;
2340 uint32_t prop_attributes = 0;
2342 bool is_artificial = false;
2343 DWARFFormValue encoding_form;
2344 AccessType accessibility = eAccessNone;
2345 uint32_t member_byte_offset =
2346 (parent_die.Tag() == DW_TAG_union_type) ? 0 : UINT32_MAX;
2347 llvm::Optional<uint64_t> byte_size;
2348 int64_t bit_offset = 0;
2349 uint64_t data_bit_offset = UINT64_MAX;
2350 size_t bit_size = 0;
2352 false; // On DW_TAG_members, this means the member is static
2354 for (i = 0; i < num_attributes && !is_artificial; ++i) {
2355 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2356 DWARFFormValue form_value;
2357 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2358 // DW_AT_data_member_location indicates the byte offset of the
2359 // word from the base address of the structure.
2361 // DW_AT_bit_offset indicates how many bits into the word
2362 // (according to the host endianness) the low-order bit of the
2363 // field starts. AT_bit_offset can be negative.
2365 // DW_AT_bit_size indicates the size of the field in bits.
2368 name = form_value.AsCString();
2371 encoding_form = form_value;
2373 case DW_AT_bit_offset:
2374 bit_offset = form_value.Signed();
2376 case DW_AT_bit_size:
2377 bit_size = form_value.Unsigned();
2379 case DW_AT_byte_size:
2380 byte_size = form_value.Unsigned();
2382 case DW_AT_data_bit_offset:
2383 data_bit_offset = form_value.Unsigned();
2385 case DW_AT_data_member_location:
2386 if (form_value.BlockData()) {
2387 Value initialValue(0);
2388 Value memberOffset(0);
2389 const DWARFDataExtractor &debug_info_data = die.GetData();
2390 uint32_t block_length = form_value.Unsigned();
2391 uint32_t block_offset =
2392 form_value.BlockData() - debug_info_data.GetDataStart();
2393 if (DWARFExpression::Evaluate(
2394 nullptr, // ExecutionContext *
2395 nullptr, // RegisterContext *
2397 DataExtractor(debug_info_data, block_offset, block_length),
2398 die.GetCU(), eRegisterKindDWARF, &initialValue, nullptr,
2399 memberOffset, nullptr)) {
2400 member_byte_offset = memberOffset.ResolveValue(nullptr).UInt();
2403 // With DWARF 3 and later, if the value is an integer constant,
2404 // this form value is the offset in bytes from the beginning of
2405 // the containing entity.
2406 member_byte_offset = form_value.Unsigned();
2410 case DW_AT_accessibility:
2411 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
2413 case DW_AT_artificial:
2414 is_artificial = form_value.Boolean();
2416 case DW_AT_APPLE_property_name:
2417 prop_name = form_value.AsCString();
2419 case DW_AT_APPLE_property_getter:
2420 prop_getter_name = form_value.AsCString();
2422 case DW_AT_APPLE_property_setter:
2423 prop_setter_name = form_value.AsCString();
2425 case DW_AT_APPLE_property_attribute:
2426 prop_attributes = form_value.Unsigned();
2428 case DW_AT_external:
2429 is_external = form_value.Boolean();
2433 case DW_AT_declaration:
2434 case DW_AT_description:
2436 case DW_AT_visibility:
2444 ConstString fixed_setter;
2446 // Check if the property getter/setter were provided as full names.
2447 // We want basenames, so we extract them.
2449 if (prop_getter_name && prop_getter_name[0] == '-') {
2450 ObjCLanguage::MethodName prop_getter_method(prop_getter_name, true);
2451 prop_getter_name = prop_getter_method.GetSelector().GetCString();
2454 if (prop_setter_name && prop_setter_name[0] == '-') {
2455 ObjCLanguage::MethodName prop_setter_method(prop_setter_name, true);
2456 prop_setter_name = prop_setter_method.GetSelector().GetCString();
2459 // If the names haven't been provided, they need to be filled in.
2461 if (!prop_getter_name) {
2462 prop_getter_name = prop_name;
2464 if (!prop_setter_name && prop_name[0] &&
2465 !(prop_attributes & DW_APPLE_PROPERTY_readonly)) {
2468 ss.Printf("set%c%s:", toupper(prop_name[0]), &prop_name[1]);
2470 fixed_setter.SetString(ss.GetString());
2471 prop_setter_name = fixed_setter.GetCString();
2475 // Clang has a DWARF generation bug where sometimes it represents
2476 // fields that are references with bad byte size and bit size/offset
2477 // information such as:
2479 // DW_AT_byte_size( 0x00 )
2480 // DW_AT_bit_size( 0x40 )
2481 // DW_AT_bit_offset( 0xffffffffffffffc0 )
2483 // So check the bit offset to make sure it is sane, and if the values
2484 // are not sane, remove them. If we don't do this then we will end up
2485 // with a crash if we try to use this type in an expression when clang
2486 // becomes unhappy with its recycled debug info.
2488 if (byte_size.getValueOr(0) == 0 && bit_offset < 0) {
2493 const bool class_is_objc_object_or_interface =
2494 TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type);
2496 // FIXME: Make Clang ignore Objective-C accessibility for expressions
2497 if (class_is_objc_object_or_interface)
2498 accessibility = eAccessNone;
2500 // Handle static members
2501 if (is_external && member_byte_offset == UINT32_MAX) {
2502 Type *var_type = die.ResolveTypeUID(encoding_form.Reference());
2505 if (accessibility == eAccessNone)
2506 accessibility = eAccessPublic;
2507 TypeSystemClang::AddVariableToRecordType(
2508 class_clang_type, name, var_type->GetForwardCompilerType(),
2514 if (!is_artificial) {
2515 Type *member_type = die.ResolveTypeUID(encoding_form.Reference());
2517 clang::FieldDecl *field_decl = nullptr;
2518 const uint64_t character_width = 8;
2519 const uint64_t word_width = 32;
2520 if (tag == DW_TAG_member) {
2522 CompilerType member_clang_type = member_type->GetLayoutCompilerType();
2524 if (accessibility == eAccessNone)
2525 accessibility = default_accessibility;
2527 uint64_t field_bit_offset =
2528 (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
2531 FieldInfo this_field_info;
2532 this_field_info.bit_offset = field_bit_offset;
2533 this_field_info.bit_size = bit_size;
2535 if (data_bit_offset != UINT64_MAX) {
2536 this_field_info.bit_offset = data_bit_offset;
2539 byte_size = member_type->GetByteSize(nullptr);
2541 ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2542 if (objfile->GetByteOrder() == eByteOrderLittle) {
2543 this_field_info.bit_offset += byte_size.getValueOr(0) * 8;
2544 this_field_info.bit_offset -= (bit_offset + bit_size);
2546 this_field_info.bit_offset += bit_offset;
2550 // The ObjC runtime knows the byte offset but we still need to provide
2551 // the bit-offset in the layout. It just means something different then
2552 // what it does in C and C++. So we skip this check for ObjC types.
2554 // We also skip this for fields of a union since they will all have a
2556 if (!TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type) &&
2557 !(parent_die.Tag() == DW_TAG_union_type && this_field_info.bit_offset == 0) &&
2558 ((this_field_info.bit_offset >= parent_bit_size) ||
2559 (last_field_info.IsBitfield() &&
2560 !last_field_info.NextBitfieldOffsetIsValid(
2561 this_field_info.bit_offset)))) {
2562 ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2563 objfile->GetModule()->ReportWarning(
2564 "0x%8.8" PRIx64 ": %s bitfield named \"%s\" has invalid "
2565 "bit offset (0x%8.8" PRIx64
2566 ") member will be ignored. Please file a bug against the "
2567 "compiler and include the preprocessed output for %s\n",
2568 die.GetID(), DW_TAG_value_to_name(tag), name,
2569 this_field_info.bit_offset, GetUnitName(parent_die).c_str());
2573 // Update the field bit offset we will report for layout
2574 field_bit_offset = this_field_info.bit_offset;
2576 // Objective-C has invalid DW_AT_bit_offset values in older
2577 // versions of clang, so we have to be careful and only insert
2578 // unnamed bitfields if we have a new enough clang.
2579 bool detect_unnamed_bitfields = true;
2581 if (class_is_objc_object_or_interface)
2582 detect_unnamed_bitfields =
2583 die.GetCU()->Supports_unnamed_objc_bitfields();
2585 if (detect_unnamed_bitfields) {
2586 clang::Optional<FieldInfo> unnamed_field_info;
2587 uint64_t last_field_end = 0;
2590 last_field_info.bit_offset + last_field_info.bit_size;
2592 if (!last_field_info.IsBitfield()) {
2593 // The last field was not a bit-field...
2594 // but if it did take up the entire word then we need to extend
2595 // last_field_end so the bit-field does not step into the last
2597 if (last_field_end != 0 && ((last_field_end % word_width) != 0))
2598 last_field_end += word_width - (last_field_end % word_width);
2601 // If we have a gap between the last_field_end and the current
2602 // field we have an unnamed bit-field.
2603 // If we have a base class, we assume there is no unnamed
2604 // bit-field if this is the first field since the gap can be
2605 // attributed to the members from the base class. This assumption
2606 // is not correct if the first field of the derived class is
2607 // indeed an unnamed bit-field. We currently do not have the
2608 // machinary to track the offset of the last field of classes we
2609 // have seen before, so we are not handling this case.
2610 if (this_field_info.bit_offset != last_field_end &&
2611 this_field_info.bit_offset > last_field_end &&
2612 !(last_field_info.bit_offset == 0 &&
2613 last_field_info.bit_size == 0 &&
2614 layout_info.base_offsets.size() != 0)) {
2615 unnamed_field_info = FieldInfo{};
2616 unnamed_field_info->bit_size =
2617 this_field_info.bit_offset - last_field_end;
2618 unnamed_field_info->bit_offset = last_field_end;
2621 if (unnamed_field_info) {
2622 clang::FieldDecl *unnamed_bitfield_decl =
2623 TypeSystemClang::AddFieldToRecordType(
2624 class_clang_type, llvm::StringRef(),
2625 m_ast.GetBuiltinTypeForEncodingAndBitSize(eEncodingSint,
2627 accessibility, unnamed_field_info->bit_size);
2629 layout_info.field_offsets.insert(std::make_pair(
2630 unnamed_bitfield_decl, unnamed_field_info->bit_offset));
2634 last_field_info = this_field_info;
2635 last_field_info.SetIsBitfield(true);
2637 last_field_info.bit_offset = field_bit_offset;
2639 if (llvm::Optional<uint64_t> clang_type_size =
2640 member_type->GetByteSize(nullptr)) {
2641 last_field_info.bit_size = *clang_type_size * character_width;
2644 last_field_info.SetIsBitfield(false);
2647 if (!member_clang_type.IsCompleteType())
2648 member_clang_type.GetCompleteType();
2651 // Older versions of clang emit array[0] and array[1] in the
2652 // same way (<rdar://problem/12566646>). If the current field
2653 // is at the end of the structure, then there is definitely no
2654 // room for extra elements and we override the type to
2657 CompilerType member_array_element_type;
2658 uint64_t member_array_size;
2659 bool member_array_is_incomplete;
2661 if (member_clang_type.IsArrayType(&member_array_element_type,
2663 &member_array_is_incomplete) &&
2664 !member_array_is_incomplete) {
2665 uint64_t parent_byte_size =
2666 parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size,
2669 if (member_byte_offset >= parent_byte_size) {
2670 if (member_array_size != 1 &&
2671 (member_array_size != 0 ||
2672 member_byte_offset > parent_byte_size)) {
2673 module_sp->ReportError(
2675 ": DW_TAG_member '%s' refers to type 0x%8.8x"
2676 " which extends beyond the bounds of 0x%8.8" PRIx64,
2677 die.GetID(), name, encoding_form.Reference().GetOffset(),
2678 parent_die.GetID());
2682 m_ast.CreateArrayType(member_array_element_type, 0, false);
2687 RequireCompleteType(member_clang_type);
2689 field_decl = TypeSystemClang::AddFieldToRecordType(
2690 class_clang_type, name, member_clang_type, accessibility,
2693 m_ast.SetMetadataAsUserID(field_decl, die.GetID());
2695 layout_info.field_offsets.insert(
2696 std::make_pair(field_decl, field_bit_offset));
2699 module_sp->ReportError(
2700 "0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8x"
2701 " which was unable to be parsed",
2702 die.GetID(), name, encoding_form.Reference().GetOffset());
2704 module_sp->ReportError(
2705 "0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8x"
2706 " which was unable to be parsed",
2707 die.GetID(), encoding_form.Reference().GetOffset());
2711 if (prop_name != nullptr && member_type) {
2712 clang::ObjCIvarDecl *ivar_decl = nullptr;
2715 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
2716 assert(ivar_decl != nullptr);
2719 ClangASTMetadata metadata;
2720 metadata.SetUserID(die.GetID());
2721 delayed_properties.push_back(DelayedAddObjCClassProperty(
2722 class_clang_type, prop_name, member_type->GetLayoutCompilerType(),
2723 ivar_decl, prop_setter_name, prop_getter_name, prop_attributes,
2727 m_ast.SetMetadataAsUserID(ivar_decl, die.GetID());
2732 bool DWARFASTParserClang::ParseChildMembers(
2733 const DWARFDIE &parent_die, CompilerType &class_clang_type,
2734 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
2735 std::vector<DWARFDIE> &member_function_dies,
2736 DelayedPropertyList &delayed_properties, AccessType &default_accessibility,
2737 ClangASTImporter::LayoutInfo &layout_info) {
2741 FieldInfo last_field_info;
2743 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2744 TypeSystemClang *ast =
2745 llvm::dyn_cast_or_null<TypeSystemClang>(class_clang_type.GetTypeSystem());
2749 for (DWARFDIE die : parent_die.children()) {
2750 dw_tag_t tag = die.Tag();
2754 case DW_TAG_APPLE_property:
2755 ParseSingleMember(die, parent_die, class_clang_type,
2756 default_accessibility, delayed_properties, layout_info,
2760 case DW_TAG_subprogram:
2761 // Let the type parsing code handle this one for us.
2762 member_function_dies.push_back(die);
2765 case DW_TAG_inheritance: {
2766 // TODO: implement DW_TAG_inheritance type parsing
2767 DWARFAttributes attributes;
2768 const size_t num_attributes = die.GetAttributes(attributes);
2769 if (num_attributes > 0) {
2770 DWARFFormValue encoding_form;
2771 AccessType accessibility = default_accessibility;
2772 bool is_virtual = false;
2773 bool is_base_of_class = true;
2774 off_t member_byte_offset = 0;
2776 for (i = 0; i < num_attributes; ++i) {
2777 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2778 DWARFFormValue form_value;
2779 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2782 encoding_form = form_value;
2784 case DW_AT_data_member_location:
2785 if (form_value.BlockData()) {
2786 Value initialValue(0);
2787 Value memberOffset(0);
2788 const DWARFDataExtractor &debug_info_data = die.GetData();
2789 uint32_t block_length = form_value.Unsigned();
2790 uint32_t block_offset =
2791 form_value.BlockData() - debug_info_data.GetDataStart();
2792 if (DWARFExpression::Evaluate(
2793 nullptr, nullptr, module_sp,
2794 DataExtractor(debug_info_data, block_offset,
2796 die.GetCU(), eRegisterKindDWARF, &initialValue, nullptr,
2797 memberOffset, nullptr)) {
2798 member_byte_offset =
2799 memberOffset.ResolveValue(nullptr).UInt();
2802 // With DWARF 3 and later, if the value is an integer constant,
2803 // this form value is the offset in bytes from the beginning of
2804 // the containing entity.
2805 member_byte_offset = form_value.Unsigned();
2809 case DW_AT_accessibility:
2810 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
2813 case DW_AT_virtuality:
2814 is_virtual = form_value.Boolean();
2826 Type *base_class_type = die.ResolveTypeUID(encoding_form.Reference());
2827 if (base_class_type == nullptr) {
2828 module_sp->ReportError("0x%8.8x: DW_TAG_inheritance failed to "
2829 "resolve the base class at 0x%8.8x"
2830 " from enclosing type 0x%8.8x. \nPlease file "
2831 "a bug and attach the file at the start of "
2832 "this error message",
2834 encoding_form.Reference().GetOffset(),
2835 parent_die.GetOffset());
2839 CompilerType base_class_clang_type =
2840 base_class_type->GetFullCompilerType();
2841 assert(base_class_clang_type);
2842 if (TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type)) {
2843 ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
2845 std::unique_ptr<clang::CXXBaseSpecifier> result =
2846 ast->CreateBaseClassSpecifier(
2847 base_class_clang_type.GetOpaqueQualType(), accessibility,
2848 is_virtual, is_base_of_class);
2852 base_classes.push_back(std::move(result));
2855 // Do not specify any offset for virtual inheritance. The DWARF
2856 // produced by clang doesn't give us a constant offset, but gives
2857 // us a DWARF expressions that requires an actual object in memory.
2858 // the DW_AT_data_member_location for a virtual base class looks
2860 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref,
2861 // DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref,
2863 // Given this, there is really no valid response we can give to
2864 // clang for virtual base class offsets, and this should eventually
2865 // be removed from LayoutRecordType() in the external
2866 // AST source in clang.
2868 layout_info.base_offsets.insert(std::make_pair(
2869 ast->GetAsCXXRecordDecl(
2870 base_class_clang_type.GetOpaqueQualType()),
2871 clang::CharUnits::fromQuantity(member_byte_offset)));
2885 size_t DWARFASTParserClang::ParseChildParameters(
2886 clang::DeclContext *containing_decl_ctx, const DWARFDIE &parent_die,
2887 bool skip_artificial, bool &is_static, bool &is_variadic,
2888 bool &has_template_params, std::vector<CompilerType> &function_param_types,
2889 std::vector<clang::ParmVarDecl *> &function_param_decls,
2890 unsigned &type_quals) {
2895 for (DWARFDIE die : parent_die.children()) {
2896 const dw_tag_t tag = die.Tag();
2898 case DW_TAG_formal_parameter: {
2899 DWARFAttributes attributes;
2900 const size_t num_attributes = die.GetAttributes(attributes);
2901 if (num_attributes > 0) {
2902 const char *name = nullptr;
2903 DWARFFormValue param_type_die_form;
2904 bool is_artificial = false;
2905 // one of None, Auto, Register, Extern, Static, PrivateExtern
2907 clang::StorageClass storage = clang::SC_None;
2909 for (i = 0; i < num_attributes; ++i) {
2910 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2911 DWARFFormValue form_value;
2912 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2915 name = form_value.AsCString();
2918 param_type_die_form = form_value;
2920 case DW_AT_artificial:
2921 is_artificial = form_value.Boolean();
2923 case DW_AT_location:
2924 case DW_AT_const_value:
2925 case DW_AT_default_value:
2926 case DW_AT_description:
2927 case DW_AT_endianity:
2928 case DW_AT_is_optional:
2930 case DW_AT_variable_parameter:
2932 case DW_AT_abstract_origin:
2940 if (skip_artificial && is_artificial) {
2941 // In order to determine if a C++ member function is "const" we
2942 // have to look at the const-ness of "this"...
2944 DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()) &&
2945 // Often times compilers omit the "this" name for the
2946 // specification DIEs, so we can't rely upon the name being in
2947 // the formal parameter DIE...
2948 (name == nullptr || ::strcmp(name, "this") == 0)) {
2950 die.ResolveTypeUID(param_type_die_form.Reference());
2952 uint32_t encoding_mask = this_type->GetEncodingMask();
2953 if (encoding_mask & Type::eEncodingIsPointerUID) {
2956 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
2957 type_quals |= clang::Qualifiers::Const;
2958 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
2959 type_quals |= clang::Qualifiers::Volatile;
2967 Type *type = die.ResolveTypeUID(param_type_die_form.Reference());
2969 function_param_types.push_back(type->GetForwardCompilerType());
2971 clang::ParmVarDecl *param_var_decl =
2972 m_ast.CreateParameterDeclaration(
2973 containing_decl_ctx, GetOwningClangModule(die), name,
2974 type->GetForwardCompilerType(), storage);
2975 assert(param_var_decl);
2976 function_param_decls.push_back(param_var_decl);
2978 m_ast.SetMetadataAsUserID(param_var_decl, die.GetID());
2985 case DW_TAG_unspecified_parameters:
2989 case DW_TAG_template_type_parameter:
2990 case DW_TAG_template_value_parameter:
2991 case DW_TAG_GNU_template_parameter_pack:
2992 // The one caller of this was never using the template_param_infos, and
2993 // the local variable was taking up a large amount of stack space in
2994 // SymbolFileDWARF::ParseType() so this was removed. If we ever need the
2995 // template params back, we can add them back.
2996 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
2997 has_template_params = true;
3007 llvm::Optional<SymbolFile::ArrayInfo>
3008 DWARFASTParser::ParseChildArrayInfo(const DWARFDIE &parent_die,
3009 const ExecutionContext *exe_ctx) {
3010 SymbolFile::ArrayInfo array_info;
3014 for (DWARFDIE die : parent_die.children()) {
3015 const dw_tag_t tag = die.Tag();
3016 if (tag != DW_TAG_subrange_type)
3019 DWARFAttributes attributes;
3020 const size_t num_child_attributes = die.GetAttributes(attributes);
3021 if (num_child_attributes > 0) {
3022 uint64_t num_elements = 0;
3023 uint64_t lower_bound = 0;
3024 uint64_t upper_bound = 0;
3025 bool upper_bound_valid = false;
3027 for (i = 0; i < num_child_attributes; ++i) {
3028 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3029 DWARFFormValue form_value;
3030 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3036 if (DWARFDIE var_die = die.GetReferencedDIE(DW_AT_count)) {
3037 if (var_die.Tag() == DW_TAG_variable)
3039 if (auto frame = exe_ctx->GetFrameSP()) {
3041 lldb::VariableSP var_sp;
3042 auto valobj_sp = frame->GetValueForVariableExpressionPath(
3043 var_die.GetName(), eNoDynamicValues, 0, var_sp,
3046 num_elements = valobj_sp->GetValueAsUnsigned(0);
3052 num_elements = form_value.Unsigned();
3055 case DW_AT_bit_stride:
3056 array_info.bit_stride = form_value.Unsigned();
3059 case DW_AT_byte_stride:
3060 array_info.byte_stride = form_value.Unsigned();
3063 case DW_AT_lower_bound:
3064 lower_bound = form_value.Unsigned();
3067 case DW_AT_upper_bound:
3068 upper_bound_valid = true;
3069 upper_bound = form_value.Unsigned();
3073 case DW_AT_abstract_origin:
3074 case DW_AT_accessibility:
3075 case DW_AT_allocated:
3076 case DW_AT_associated:
3077 case DW_AT_data_location:
3078 case DW_AT_declaration:
3079 case DW_AT_description:
3081 case DW_AT_threads_scaled:
3083 case DW_AT_visibility:
3089 if (num_elements == 0) {
3090 if (upper_bound_valid && upper_bound >= lower_bound)
3091 num_elements = upper_bound - lower_bound + 1;
3094 array_info.element_orders.push_back(num_elements);
3100 Type *DWARFASTParserClang::GetTypeForDIE(const DWARFDIE &die) {
3102 SymbolFileDWARF *dwarf = die.GetDWARF();
3103 DWARFAttributes attributes;
3104 const size_t num_attributes = die.GetAttributes(attributes);
3105 if (num_attributes > 0) {
3106 DWARFFormValue type_die_form;
3107 for (size_t i = 0; i < num_attributes; ++i) {
3108 dw_attr_t attr = attributes.AttributeAtIndex(i);
3109 DWARFFormValue form_value;
3111 if (attr == DW_AT_type &&
3112 attributes.ExtractFormValueAtIndex(i, form_value))
3113 return dwarf->ResolveTypeUID(form_value.Reference(), true);
3121 clang::Decl *DWARFASTParserClang::GetClangDeclForDIE(const DWARFDIE &die) {
3125 switch (die.Tag()) {
3126 case DW_TAG_variable:
3127 case DW_TAG_constant:
3128 case DW_TAG_formal_parameter:
3129 case DW_TAG_imported_declaration:
3130 case DW_TAG_imported_module:
3136 DIEToDeclMap::iterator cache_pos = m_die_to_decl.find(die.GetDIE());
3137 if (cache_pos != m_die_to_decl.end())
3138 return cache_pos->second;
3140 if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification)) {
3141 clang::Decl *decl = GetClangDeclForDIE(spec_die);
3142 m_die_to_decl[die.GetDIE()] = decl;
3143 m_decl_to_die[decl].insert(die.GetDIE());
3147 if (DWARFDIE abstract_origin_die =
3148 die.GetReferencedDIE(DW_AT_abstract_origin)) {
3149 clang::Decl *decl = GetClangDeclForDIE(abstract_origin_die);
3150 m_die_to_decl[die.GetDIE()] = decl;
3151 m_decl_to_die[decl].insert(die.GetDIE());
3155 clang::Decl *decl = nullptr;
3156 switch (die.Tag()) {
3157 case DW_TAG_variable:
3158 case DW_TAG_constant:
3159 case DW_TAG_formal_parameter: {
3160 SymbolFileDWARF *dwarf = die.GetDWARF();
3161 Type *type = GetTypeForDIE(die);
3162 if (dwarf && type) {
3163 const char *name = die.GetName();
3164 clang::DeclContext *decl_context =
3165 TypeSystemClang::DeclContextGetAsDeclContext(
3166 dwarf->GetDeclContextContainingUID(die.GetID()));
3167 decl = m_ast.CreateVariableDeclaration(
3168 decl_context, GetOwningClangModule(die), name,
3169 ClangUtil::GetQualType(type->GetForwardCompilerType()));
3173 case DW_TAG_imported_declaration: {
3174 SymbolFileDWARF *dwarf = die.GetDWARF();
3175 DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3177 CompilerDecl imported_decl = SymbolFileDWARF::GetDecl(imported_uid);
3178 if (imported_decl) {
3179 clang::DeclContext *decl_context =
3180 TypeSystemClang::DeclContextGetAsDeclContext(
3181 dwarf->GetDeclContextContainingUID(die.GetID()));
3182 if (clang::NamedDecl *clang_imported_decl =
3183 llvm::dyn_cast<clang::NamedDecl>(
3184 (clang::Decl *)imported_decl.GetOpaqueDecl()))
3185 decl = m_ast.CreateUsingDeclaration(
3186 decl_context, OptionalClangModuleID(), clang_imported_decl);
3191 case DW_TAG_imported_module: {
3192 SymbolFileDWARF *dwarf = die.GetDWARF();
3193 DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3196 CompilerDeclContext imported_decl_ctx =
3197 SymbolFileDWARF::GetDeclContext(imported_uid);
3198 if (imported_decl_ctx) {
3199 clang::DeclContext *decl_context =
3200 TypeSystemClang::DeclContextGetAsDeclContext(
3201 dwarf->GetDeclContextContainingUID(die.GetID()));
3202 if (clang::NamespaceDecl *ns_decl =
3203 TypeSystemClang::DeclContextGetAsNamespaceDecl(
3205 decl = m_ast.CreateUsingDirectiveDeclaration(
3206 decl_context, OptionalClangModuleID(), ns_decl);
3215 m_die_to_decl[die.GetDIE()] = decl;
3216 m_decl_to_die[decl].insert(die.GetDIE());
3221 clang::DeclContext *
3222 DWARFASTParserClang::GetClangDeclContextForDIE(const DWARFDIE &die) {
3224 clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE(die);
3228 bool try_parsing_type = true;
3229 switch (die.Tag()) {
3230 case DW_TAG_compile_unit:
3231 case DW_TAG_partial_unit:
3232 decl_ctx = m_ast.GetTranslationUnitDecl();
3233 try_parsing_type = false;
3236 case DW_TAG_namespace:
3237 decl_ctx = ResolveNamespaceDIE(die);
3238 try_parsing_type = false;
3241 case DW_TAG_lexical_block:
3242 decl_ctx = GetDeclContextForBlock(die);
3243 try_parsing_type = false;
3250 if (decl_ctx == nullptr && try_parsing_type) {
3251 Type *type = die.GetDWARF()->ResolveType(die);
3253 decl_ctx = GetCachedClangDeclContextForDIE(die);
3257 LinkDeclContextToDIE(decl_ctx, die);
3264 OptionalClangModuleID
3265 DWARFASTParserClang::GetOwningClangModule(const DWARFDIE &die) {
3269 for (DWARFDIE parent = die.GetParent(); parent.IsValid();
3270 parent = parent.GetParent()) {
3271 const dw_tag_t tag = parent.Tag();
3272 if (tag == DW_TAG_module) {
3273 DWARFDIE module_die = parent;
3274 auto it = m_die_to_module.find(module_die.GetDIE());
3275 if (it != m_die_to_module.end())
3277 const char *name = module_die.GetAttributeValueAsString(DW_AT_name, 0);
3281 OptionalClangModuleID id =
3282 m_ast.GetOrCreateClangModule(name, GetOwningClangModule(module_die));
3283 m_die_to_module.insert({module_die.GetDIE(), id});
3290 static bool IsSubroutine(const DWARFDIE &die) {
3291 switch (die.Tag()) {
3292 case DW_TAG_subprogram:
3293 case DW_TAG_inlined_subroutine:
3300 static DWARFDIE GetContainingFunctionWithAbstractOrigin(const DWARFDIE &die) {
3301 for (DWARFDIE candidate = die; candidate; candidate = candidate.GetParent()) {
3302 if (IsSubroutine(candidate)) {
3303 if (candidate.GetReferencedDIE(DW_AT_abstract_origin)) {
3310 assert(0 && "Shouldn't call GetContainingFunctionWithAbstractOrigin on "
3311 "something not in a function");
3315 static DWARFDIE FindAnyChildWithAbstractOrigin(const DWARFDIE &context) {
3316 for (DWARFDIE candidate : context.children()) {
3317 if (candidate.GetReferencedDIE(DW_AT_abstract_origin)) {
3324 static DWARFDIE FindFirstChildWithAbstractOrigin(const DWARFDIE &block,
3325 const DWARFDIE &function) {
3326 assert(IsSubroutine(function));
3327 for (DWARFDIE context = block; context != function.GetParent();
3328 context = context.GetParent()) {
3329 assert(!IsSubroutine(context) || context == function);
3330 if (DWARFDIE child = FindAnyChildWithAbstractOrigin(context)) {
3337 clang::DeclContext *
3338 DWARFASTParserClang::GetDeclContextForBlock(const DWARFDIE &die) {
3339 assert(die.Tag() == DW_TAG_lexical_block);
3340 DWARFDIE containing_function_with_abstract_origin =
3341 GetContainingFunctionWithAbstractOrigin(die);
3342 if (!containing_function_with_abstract_origin) {
3343 return (clang::DeclContext *)ResolveBlockDIE(die);
3345 DWARFDIE child = FindFirstChildWithAbstractOrigin(
3346 die, containing_function_with_abstract_origin);
3347 CompilerDeclContext decl_context =
3348 GetDeclContextContainingUIDFromDWARF(child);
3349 return (clang::DeclContext *)decl_context.GetOpaqueDeclContext();
3352 clang::BlockDecl *DWARFASTParserClang::ResolveBlockDIE(const DWARFDIE &die) {
3353 if (die && die.Tag() == DW_TAG_lexical_block) {
3354 clang::BlockDecl *decl =
3355 llvm::cast_or_null<clang::BlockDecl>(m_die_to_decl_ctx[die.GetDIE()]);
3358 DWARFDIE decl_context_die;
3359 clang::DeclContext *decl_context =
3360 GetClangDeclContextContainingDIE(die, &decl_context_die);
3362 m_ast.CreateBlockDeclaration(decl_context, GetOwningClangModule(die));
3365 LinkDeclContextToDIE((clang::DeclContext *)decl, die);
3373 clang::NamespaceDecl *
3374 DWARFASTParserClang::ResolveNamespaceDIE(const DWARFDIE &die) {
3375 if (die && die.Tag() == DW_TAG_namespace) {
3376 // See if we already parsed this namespace DIE and associated it with a
3377 // uniqued namespace declaration
3378 clang::NamespaceDecl *namespace_decl =
3379 static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die.GetDIE()]);
3381 return namespace_decl;
3383 const char *namespace_name = die.GetName();
3384 clang::DeclContext *containing_decl_ctx =
3385 GetClangDeclContextContainingDIE(die, nullptr);
3387 die.GetAttributeValueAsUnsigned(DW_AT_export_symbols, 0) != 0;
3389 namespace_decl = m_ast.GetUniqueNamespaceDeclaration(
3390 namespace_name, containing_decl_ctx, GetOwningClangModule(die),
3393 nullptr; // (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3395 SymbolFileDWARF *dwarf = die.GetDWARF();
3396 if (namespace_name) {
3397 dwarf->GetObjectFile()->GetModule()->LogMessage(
3399 "ASTContext => %p: 0x%8.8" PRIx64
3400 ": DW_TAG_namespace with DW_AT_name(\"%s\") => "
3401 "clang::NamespaceDecl *%p (original = %p)",
3402 static_cast<void *>(&m_ast.getASTContext()), die.GetID(),
3403 namespace_name, static_cast<void *>(namespace_decl),
3404 static_cast<void *>(namespace_decl->getOriginalNamespace()));
3406 dwarf->GetObjectFile()->GetModule()->LogMessage(
3408 "ASTContext => %p: 0x%8.8" PRIx64
3409 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p "
3411 static_cast<void *>(&m_ast.getASTContext()), die.GetID(),
3412 static_cast<void *>(namespace_decl),
3413 static_cast<void *>(namespace_decl->getOriginalNamespace()));
3418 LinkDeclContextToDIE((clang::DeclContext *)namespace_decl, die);
3419 return namespace_decl;
3425 clang::DeclContext *DWARFASTParserClang::GetClangDeclContextContainingDIE(
3426 const DWARFDIE &die, DWARFDIE *decl_ctx_die_copy) {
3427 SymbolFileDWARF *dwarf = die.GetDWARF();
3429 DWARFDIE decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE(die);
3431 if (decl_ctx_die_copy)
3432 *decl_ctx_die_copy = decl_ctx_die;
3435 clang::DeclContext *clang_decl_ctx =
3436 GetClangDeclContextForDIE(decl_ctx_die);
3438 return clang_decl_ctx;
3440 return m_ast.GetTranslationUnitDecl();
3443 clang::DeclContext *
3444 DWARFASTParserClang::GetCachedClangDeclContextForDIE(const DWARFDIE &die) {
3446 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die.GetDIE());
3447 if (pos != m_die_to_decl_ctx.end())
3453 void DWARFASTParserClang::LinkDeclContextToDIE(clang::DeclContext *decl_ctx,
3454 const DWARFDIE &die) {
3455 m_die_to_decl_ctx[die.GetDIE()] = decl_ctx;
3456 // There can be many DIEs for a single decl context
3457 // m_decl_ctx_to_die[decl_ctx].insert(die.GetDIE());
3458 m_decl_ctx_to_die.insert(std::make_pair(decl_ctx, die));
3461 bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
3462 const DWARFDIE &src_class_die, const DWARFDIE &dst_class_die,
3463 lldb_private::Type *class_type, std::vector<DWARFDIE> &failures) {
3464 if (!class_type || !src_class_die || !dst_class_die)
3466 if (src_class_die.Tag() != dst_class_die.Tag())
3469 // We need to complete the class type so we can get all of the method types
3470 // parsed so we can then unique those types to their equivalent counterparts
3471 // in "dst_cu" and "dst_class_die"
3472 class_type->GetFullCompilerType();
3476 UniqueCStringMap<DWARFDIE> src_name_to_die;
3477 UniqueCStringMap<DWARFDIE> dst_name_to_die;
3478 UniqueCStringMap<DWARFDIE> src_name_to_die_artificial;
3479 UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial;
3480 for (DWARFDIE src_die : src_class_die.children()) {
3481 if (src_die.Tag() == DW_TAG_subprogram) {
3482 // Make sure this is a declaration and not a concrete instance by looking
3483 // for DW_AT_declaration set to 1. Sometimes concrete function instances
3484 // are placed inside the class definitions and shouldn't be included in
3485 // the list of things are are tracking here.
3486 if (src_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3487 const char *src_name = src_die.GetMangledName();
3489 ConstString src_const_name(src_name);
3490 if (src_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3491 src_name_to_die_artificial.Append(src_const_name, src_die);
3493 src_name_to_die.Append(src_const_name, src_die);
3498 for (DWARFDIE dst_die : dst_class_die.children()) {
3499 if (dst_die.Tag() == DW_TAG_subprogram) {
3500 // Make sure this is a declaration and not a concrete instance by looking
3501 // for DW_AT_declaration set to 1. Sometimes concrete function instances
3502 // are placed inside the class definitions and shouldn't be included in
3503 // the list of things are are tracking here.
3504 if (dst_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3505 const char *dst_name = dst_die.GetMangledName();
3507 ConstString dst_const_name(dst_name);
3508 if (dst_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3509 dst_name_to_die_artificial.Append(dst_const_name, dst_die);
3511 dst_name_to_die.Append(dst_const_name, dst_die);
3516 const uint32_t src_size = src_name_to_die.GetSize();
3517 const uint32_t dst_size = dst_name_to_die.GetSize();
3518 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
3519 // DWARF_LOG_TYPE_COMPLETION));
3521 // Is everything kosher so we can go through the members at top speed?
3522 bool fast_path = true;
3524 if (src_size != dst_size) {
3525 if (src_size != 0 && dst_size != 0) {
3527 "warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, "
3528 "but they didn't have the same size (src=%d, dst=%d)",
3529 src_class_die.GetOffset(), dst_class_die.GetOffset(), src_size,
3539 for (idx = 0; idx < src_size; ++idx) {
3540 src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3541 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3543 if (src_die.Tag() != dst_die.Tag()) {
3545 "warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, "
3546 "but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
3547 src_class_die.GetOffset(), dst_class_die.GetOffset(),
3548 src_die.GetOffset(), src_die.GetTagAsCString(),
3549 dst_die.GetOffset(), dst_die.GetTagAsCString());
3553 const char *src_name = src_die.GetMangledName();
3554 const char *dst_name = dst_die.GetMangledName();
3556 // Make sure the names match
3557 if (src_name == dst_name || (strcmp(src_name, dst_name) == 0))
3561 "warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, "
3562 "but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
3563 src_class_die.GetOffset(), dst_class_die.GetOffset(),
3564 src_die.GetOffset(), src_name, dst_die.GetOffset(), dst_name);
3570 DWARFASTParserClang *src_dwarf_ast_parser =
3571 static_cast<DWARFASTParserClang *>(
3572 SymbolFileDWARF::GetDWARFParser(*src_die.GetCU()));
3573 DWARFASTParserClang *dst_dwarf_ast_parser =
3574 static_cast<DWARFASTParserClang *>(
3575 SymbolFileDWARF::GetDWARFParser(*dst_die.GetCU()));
3577 // Now do the work of linking the DeclContexts and Types.
3579 // We can do this quickly. Just run across the tables index-for-index
3580 // since we know each node has matching names and tags.
3581 for (idx = 0; idx < src_size; ++idx) {
3582 src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3583 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3585 clang::DeclContext *src_decl_ctx =
3586 src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3588 LLDB_LOGF(log, "uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3589 static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3590 dst_die.GetOffset());
3591 dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3594 "warning: tried to unique decl context from 0x%8.8x for "
3595 "0x%8.8x, but none was found",
3596 src_die.GetOffset(), dst_die.GetOffset());
3599 Type *src_child_type =
3600 dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3601 if (src_child_type) {
3603 "uniquing type %p (uid=0x%" PRIx64
3604 ") from 0x%8.8x for 0x%8.8x",
3605 static_cast<void *>(src_child_type), src_child_type->GetID(),
3606 src_die.GetOffset(), dst_die.GetOffset());
3607 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3610 "warning: tried to unique lldb_private::Type from "
3611 "0x%8.8x for 0x%8.8x, but none was found",
3612 src_die.GetOffset(), dst_die.GetOffset());
3616 // We must do this slowly. For each member of the destination, look up a
3617 // member in the source with the same name, check its tag, and unique them
3618 // if everything matches up. Report failures.
3620 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty()) {
3621 src_name_to_die.Sort();
3623 for (idx = 0; idx < dst_size; ++idx) {
3624 ConstString dst_name = dst_name_to_die.GetCStringAtIndex(idx);
3625 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3626 src_die = src_name_to_die.Find(dst_name, DWARFDIE());
3628 if (src_die && (src_die.Tag() == dst_die.Tag())) {
3629 clang::DeclContext *src_decl_ctx =
3630 src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3632 LLDB_LOGF(log, "uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3633 static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3634 dst_die.GetOffset());
3635 dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3638 "warning: tried to unique decl context from 0x%8.8x "
3639 "for 0x%8.8x, but none was found",
3640 src_die.GetOffset(), dst_die.GetOffset());
3643 Type *src_child_type =
3644 dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3645 if (src_child_type) {
3648 "uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3649 static_cast<void *>(src_child_type), src_child_type->GetID(),
3650 src_die.GetOffset(), dst_die.GetOffset());
3651 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] =
3655 "warning: tried to unique lldb_private::Type from "
3656 "0x%8.8x for 0x%8.8x, but none was found",
3657 src_die.GetOffset(), dst_die.GetOffset());
3660 LLDB_LOGF(log, "warning: couldn't find a match for 0x%8.8x",
3661 dst_die.GetOffset());
3663 failures.push_back(dst_die);
3669 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize();
3670 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize();
3672 if (src_size_artificial && dst_size_artificial) {
3673 dst_name_to_die_artificial.Sort();
3675 for (idx = 0; idx < src_size_artificial; ++idx) {
3676 ConstString src_name_artificial =
3677 src_name_to_die_artificial.GetCStringAtIndex(idx);
3678 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3680 dst_name_to_die_artificial.Find(src_name_artificial, DWARFDIE());
3683 // Both classes have the artificial types, link them
3684 clang::DeclContext *src_decl_ctx =
3685 src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3687 LLDB_LOGF(log, "uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3688 static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3689 dst_die.GetOffset());
3690 dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3693 "warning: tried to unique decl context from 0x%8.8x "
3694 "for 0x%8.8x, but none was found",
3695 src_die.GetOffset(), dst_die.GetOffset());
3698 Type *src_child_type =
3699 dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3700 if (src_child_type) {
3703 "uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3704 static_cast<void *>(src_child_type), src_child_type->GetID(),
3705 src_die.GetOffset(), dst_die.GetOffset());
3706 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3709 "warning: tried to unique lldb_private::Type from "
3710 "0x%8.8x for 0x%8.8x, but none was found",
3711 src_die.GetOffset(), dst_die.GetOffset());
3717 if (dst_size_artificial) {
3718 for (idx = 0; idx < dst_size_artificial; ++idx) {
3719 ConstString dst_name_artificial =
3720 dst_name_to_die_artificial.GetCStringAtIndex(idx);
3721 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3723 "warning: need to create artificial method for 0x%8.8x for "
3725 dst_die.GetOffset(), dst_name_artificial.GetCString());
3727 failures.push_back(dst_die);
3731 return !failures.empty();