1 //===-- DWARFASTParserClang.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "DWARFASTParserClang.h"
13 #include "DWARFCompileUnit.h"
15 #include "DWARFDIECollection.h"
16 #include "DWARFDebugInfo.h"
17 #include "DWARFDeclContext.h"
18 #include "DWARFDefines.h"
19 #include "SymbolFileDWARF.h"
20 #include "SymbolFileDWARFDebugMap.h"
21 #include "UniqueDWARFASTType.h"
23 #include "Plugins/Language/ObjC/ObjCLanguage.h"
24 #include "lldb/Core/Log.h"
25 #include "lldb/Core/Module.h"
26 #include "lldb/Core/StreamString.h"
27 #include "lldb/Core/Value.h"
28 #include "lldb/Host/Host.h"
29 #include "lldb/Interpreter/Args.h"
30 #include "lldb/Symbol/ClangASTImporter.h"
31 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
32 #include "lldb/Symbol/ClangUtil.h"
33 #include "lldb/Symbol/CompileUnit.h"
34 #include "lldb/Symbol/Function.h"
35 #include "lldb/Symbol/ObjectFile.h"
36 #include "lldb/Symbol/SymbolVendor.h"
37 #include "lldb/Symbol/TypeList.h"
38 #include "lldb/Symbol/TypeMap.h"
39 #include "lldb/Target/Language.h"
40 #include "lldb/Utility/LLDBAssert.h"
42 #include "clang/AST/DeclCXX.h"
43 #include "clang/AST/DeclObjC.h"
48 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
50 #ifdef ENABLE_DEBUG_PRINTF
52 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
54 #define DEBUG_PRINTF(fmt, ...)
58 using namespace lldb_private;
59 DWARFASTParserClang::DWARFASTParserClang(ClangASTContext &ast)
60 : m_ast(ast), m_die_to_decl_ctx(), m_decl_ctx_to_die() {}
62 DWARFASTParserClang::~DWARFASTParserClang() {}
64 static AccessType DW_ACCESS_to_AccessType(uint32_t dwarf_accessibility) {
65 switch (dwarf_accessibility) {
66 case DW_ACCESS_public:
68 case DW_ACCESS_private:
69 return eAccessPrivate;
70 case DW_ACCESS_protected:
71 return eAccessProtected;
78 static bool DeclKindIsCXXClass(clang::Decl::Kind decl_kind) {
80 case clang::Decl::CXXRecord:
81 case clang::Decl::ClassTemplateSpecialization:
94 : bit_size(LLDB_INVALID_ADDRESS), bit_offset(LLDB_INVALID_ADDRESS) {}
97 bit_size = LLDB_INVALID_ADDRESS;
98 bit_offset = LLDB_INVALID_ADDRESS;
101 bool IsValid() const {
102 return (bit_size != LLDB_INVALID_ADDRESS) &&
103 (bit_offset != LLDB_INVALID_ADDRESS);
106 bool NextBitfieldOffsetIsValid(const uint64_t next_bit_offset) const {
108 // This bitfield info is valid, so any subsequent bitfields
109 // must not overlap and must be at a higher bit offset than
110 // any previous bitfield + size.
111 return (bit_size + bit_offset) <= next_bit_offset;
113 // If the this BitfieldInfo is not valid, then any offset isOK
119 ClangASTImporter &DWARFASTParserClang::GetClangASTImporter() {
120 if (!m_clang_ast_importer_ap) {
121 m_clang_ast_importer_ap.reset(new ClangASTImporter);
123 return *m_clang_ast_importer_ap;
126 TypeSP DWARFASTParserClang::ParseTypeFromDWO(const DWARFDIE &die, Log *log) {
127 ModuleSP dwo_module_sp = die.GetContainingDWOModule();
129 // This type comes from an external DWO module
130 std::vector<CompilerContext> dwo_context;
131 die.GetDWOContext(dwo_context);
133 if (dwo_module_sp->GetSymbolVendor()->FindTypes(dwo_context, true,
135 const size_t num_dwo_types = dwo_types.GetSize();
136 if (num_dwo_types == 1) {
137 // We found a real definition for this type elsewhere
138 // so lets use it and cache the fact that we found
139 // a complete type for this die
140 TypeSP dwo_type_sp = dwo_types.GetTypeAtIndex(0);
142 lldb_private::CompilerType dwo_type =
143 dwo_type_sp->GetForwardCompilerType();
145 lldb_private::CompilerType type =
146 GetClangASTImporter().CopyType(m_ast, dwo_type);
148 // printf ("copied_qual_type: ast = %p, clang_type = %p, name =
149 // '%s'\n", m_ast, copied_qual_type.getAsOpaquePtr(),
150 // external_type->GetName().GetCString());
152 SymbolFileDWARF *dwarf = die.GetDWARF();
153 TypeSP type_sp(new Type(die.GetID(), dwarf, dwo_type_sp->GetName(),
154 dwo_type_sp->GetByteSize(), NULL,
155 LLDB_INVALID_UID, Type::eEncodingInvalid,
156 &dwo_type_sp->GetDeclaration(), type,
157 Type::eResolveStateForward));
159 dwarf->GetTypeList()->Insert(type_sp);
160 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
161 clang::TagDecl *tag_decl = ClangASTContext::GetAsTagDecl(type);
163 LinkDeclContextToDIE(tag_decl, die);
165 clang::DeclContext *defn_decl_ctx =
166 GetCachedClangDeclContextForDIE(die);
168 LinkDeclContextToDIE(defn_decl_ctx, die);
179 TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
180 const DWARFDIE &die, Log *log,
181 bool *type_is_new_ptr) {
185 *type_is_new_ptr = false;
187 AccessType accessibility = eAccessNone;
189 SymbolFileDWARF *dwarf = die.GetDWARF();
191 DWARFDIE context_die;
192 clang::DeclContext *context =
193 GetClangDeclContextContainingDIE(die, &context_die);
195 dwarf->GetObjectFile()->GetModule()->LogMessage(
196 log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die "
197 "0x%8.8x)) %s name = '%s')",
198 die.GetOffset(), static_cast<void *>(context),
199 context_die.GetOffset(), die.GetTagAsCString(), die.GetName());
202 // Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
203 // if (log && dwarf_cu)
206 // die->DumpLocation (this, dwarf_cu, s);
207 // dwarf->GetObjectFile()->GetModule()->LogMessage (log,
208 // "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
212 Type *type_ptr = dwarf->GetDIEToType().lookup(die.GetDIE());
213 TypeList *type_list = dwarf->GetTypeList();
214 if (type_ptr == NULL) {
216 *type_is_new_ptr = true;
218 const dw_tag_t tag = die.Tag();
220 bool is_forward_declaration = false;
221 DWARFAttributes attributes;
222 const char *type_name_cstr = NULL;
223 ConstString type_name_const_str;
224 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
225 uint64_t byte_size = 0;
228 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
229 CompilerType clang_type;
230 DWARFFormValue form_value;
236 case DW_TAG_base_type:
237 case DW_TAG_pointer_type:
238 case DW_TAG_reference_type:
239 case DW_TAG_rvalue_reference_type:
240 case DW_TAG_const_type:
241 case DW_TAG_restrict_type:
242 case DW_TAG_volatile_type:
243 case DW_TAG_unspecified_type: {
244 // Set a bit that lets us know that we are currently parsing this
245 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
247 const size_t num_attributes = die.GetAttributes(attributes);
248 uint32_t encoding = 0;
249 DWARFFormValue encoding_uid;
251 if (num_attributes > 0) {
253 for (i = 0; i < num_attributes; ++i) {
254 attr = attributes.AttributeAtIndex(i);
255 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
257 case DW_AT_decl_file:
258 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
259 form_value.Unsigned()));
261 case DW_AT_decl_line:
262 decl.SetLine(form_value.Unsigned());
264 case DW_AT_decl_column:
265 decl.SetColumn(form_value.Unsigned());
269 type_name_cstr = form_value.AsCString();
270 // Work around a bug in llvm-gcc where they give a name to a
271 // reference type which doesn't
272 // include the "&"...
273 if (tag == DW_TAG_reference_type) {
274 if (strchr(type_name_cstr, '&') == NULL)
275 type_name_cstr = NULL;
278 type_name_const_str.SetCString(type_name_cstr);
280 case DW_AT_byte_size:
281 byte_size = form_value.Unsigned();
284 encoding = form_value.Unsigned();
287 encoding_uid = form_value;
297 if (tag == DW_TAG_typedef && encoding_uid.IsValid()) {
298 // Try to parse a typedef from the DWO file first as modules
299 // can contain typedef'ed structures that have no names like:
301 // typedef struct { int a; } Foo;
303 // In this case we will have a structure with no name and a
304 // typedef named "Foo" that points to this unnamed structure.
305 // The name in the typedef is the only identifier for the struct,
306 // so always try to get typedefs from DWO files if possible.
308 // The type_sp returned will be empty if the typedef doesn't exist
309 // in a DWO file, so it is cheap to call this function just to check.
311 // If we don't do this we end up creating a TypeSP that says this
312 // is a typedef to type 0x123 (the DW_AT_type value would be 0x123
313 // in the DW_TAG_typedef), and this is the unnamed structure type.
314 // We will have a hard time tracking down an unnammed structure
315 // type in the module DWO file, so we make sure we don't get into
316 // this situation by always resolving typedefs from the DWO file.
317 const DWARFDIE encoding_die = dwarf->GetDIE(DIERef(encoding_uid));
319 // First make sure that the die that this is typedef'ed to _is_
320 // just a declaration (DW_AT_declaration == 1), not a full definition
321 // since template types can't be represented in modules since only
322 // concrete instances of templates are ever emitted and modules
323 // won't contain those
325 encoding_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) ==
327 type_sp = ParseTypeFromDWO(die, log);
333 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n",
334 die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr,
335 encoding_uid.Reference());
341 case DW_TAG_unspecified_type:
342 if (strcmp(type_name_cstr, "nullptr_t") == 0 ||
343 strcmp(type_name_cstr, "decltype(nullptr)") == 0) {
344 resolve_state = Type::eResolveStateFull;
345 clang_type = m_ast.GetBasicType(eBasicTypeNullPtr);
348 // Fall through to base type below in case we can handle the type
352 case DW_TAG_base_type:
353 resolve_state = Type::eResolveStateFull;
354 clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
355 type_name_cstr, encoding, byte_size * 8);
358 case DW_TAG_pointer_type:
359 encoding_data_type = Type::eEncodingIsPointerUID;
361 case DW_TAG_reference_type:
362 encoding_data_type = Type::eEncodingIsLValueReferenceUID;
364 case DW_TAG_rvalue_reference_type:
365 encoding_data_type = Type::eEncodingIsRValueReferenceUID;
368 encoding_data_type = Type::eEncodingIsTypedefUID;
370 case DW_TAG_const_type:
371 encoding_data_type = Type::eEncodingIsConstUID;
373 case DW_TAG_restrict_type:
374 encoding_data_type = Type::eEncodingIsRestrictUID;
376 case DW_TAG_volatile_type:
377 encoding_data_type = Type::eEncodingIsVolatileUID;
382 (encoding_data_type == Type::eEncodingIsPointerUID ||
383 encoding_data_type == Type::eEncodingIsTypedefUID) &&
384 sc.comp_unit != NULL) {
385 if (tag == DW_TAG_pointer_type) {
386 DWARFDIE target_die = die.GetReferencedDIE(DW_AT_type);
388 if (target_die.GetAttributeValueAsUnsigned(DW_AT_APPLE_block, 0)) {
389 // Blocks have a __FuncPtr inside them which is a pointer to a
390 // function of the proper type.
392 for (DWARFDIE child_die = target_die.GetFirstChild();
393 child_die.IsValid(); child_die = child_die.GetSibling()) {
394 if (!strcmp(child_die.GetAttributeValueAsString(DW_AT_name, ""),
396 DWARFDIE function_pointer_type =
397 child_die.GetReferencedDIE(DW_AT_type);
399 if (function_pointer_type) {
400 DWARFDIE function_type =
401 function_pointer_type.GetReferencedDIE(DW_AT_type);
403 bool function_type_is_new_pointer;
404 TypeSP lldb_function_type_sp = ParseTypeFromDWARF(
405 sc, function_type, log, &function_type_is_new_pointer);
407 if (lldb_function_type_sp) {
408 clang_type = m_ast.CreateBlockPointerType(
409 lldb_function_type_sp->GetForwardCompilerType());
410 encoding_data_type = Type::eEncodingIsUID;
411 encoding_uid.Clear();
412 resolve_state = Type::eResolveStateFull;
422 bool translation_unit_is_objc =
423 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC ||
424 sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
426 if (translation_unit_is_objc) {
427 if (type_name_cstr != NULL) {
428 static ConstString g_objc_type_name_id("id");
429 static ConstString g_objc_type_name_Class("Class");
430 static ConstString g_objc_type_name_selector("SEL");
432 if (type_name_const_str == g_objc_type_name_id) {
434 dwarf->GetObjectFile()->GetModule()->LogMessage(
435 log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
436 "is Objective C 'id' built-in type.",
437 die.GetOffset(), die.GetTagAsCString(), die.GetName());
438 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
439 encoding_data_type = Type::eEncodingIsUID;
440 encoding_uid.Clear();
441 resolve_state = Type::eResolveStateFull;
443 } else if (type_name_const_str == g_objc_type_name_Class) {
445 dwarf->GetObjectFile()->GetModule()->LogMessage(
446 log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
447 "is Objective C 'Class' built-in type.",
448 die.GetOffset(), die.GetTagAsCString(), die.GetName());
449 clang_type = m_ast.GetBasicType(eBasicTypeObjCClass);
450 encoding_data_type = Type::eEncodingIsUID;
451 encoding_uid.Clear();
452 resolve_state = Type::eResolveStateFull;
453 } else if (type_name_const_str == g_objc_type_name_selector) {
455 dwarf->GetObjectFile()->GetModule()->LogMessage(
456 log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
457 "is Objective C 'selector' built-in type.",
458 die.GetOffset(), die.GetTagAsCString(), die.GetName());
459 clang_type = m_ast.GetBasicType(eBasicTypeObjCSel);
460 encoding_data_type = Type::eEncodingIsUID;
461 encoding_uid.Clear();
462 resolve_state = Type::eResolveStateFull;
464 } else if (encoding_data_type == Type::eEncodingIsPointerUID &&
465 encoding_uid.IsValid()) {
466 // Clang sometimes erroneously emits id as objc_object*. In that
467 // case we fix up the type to "id".
469 const DWARFDIE encoding_die = dwarf->GetDIE(DIERef(encoding_uid));
471 if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type) {
472 if (const char *struct_name = encoding_die.GetName()) {
473 if (!strcmp(struct_name, "objc_object")) {
475 dwarf->GetObjectFile()->GetModule()->LogMessage(
476 log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s "
477 "'%s' is 'objc_object*', which we overrode to "
479 die.GetOffset(), die.GetTagAsCString(),
481 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
482 encoding_data_type = Type::eEncodingIsUID;
483 encoding_uid.Clear();
484 resolve_state = Type::eResolveStateFull;
493 new Type(die.GetID(), dwarf, type_name_const_str, byte_size, NULL,
494 DIERef(encoding_uid).GetUID(dwarf), encoding_data_type,
495 &decl, clang_type, resolve_state));
497 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
499 // Type* encoding_type =
500 // GetUniquedTypeForDIEOffset(encoding_uid, type_sp,
501 // NULL, 0, 0, false);
502 // if (encoding_type != NULL)
504 // if (encoding_type != DIE_IS_BEING_PARSED)
505 // type_sp->SetEncodingType(encoding_type);
507 // m_indirect_fixups.push_back(type_sp.get());
511 case DW_TAG_structure_type:
512 case DW_TAG_union_type:
513 case DW_TAG_class_type: {
514 // Set a bit that lets us know that we are currently parsing this
515 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
516 bool byte_size_valid = false;
518 LanguageType class_language = eLanguageTypeUnknown;
519 bool is_complete_objc_class = false;
520 // bool struct_is_class = false;
521 const size_t num_attributes = die.GetAttributes(attributes);
522 if (num_attributes > 0) {
524 for (i = 0; i < num_attributes; ++i) {
525 attr = attributes.AttributeAtIndex(i);
526 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
528 case DW_AT_decl_file:
529 if (die.GetCU()->DW_AT_decl_file_attributes_are_invalid()) {
530 // llvm-gcc outputs invalid DW_AT_decl_file attributes that
532 // point to the compile unit file, so we clear this invalid
534 // so that we can still unique types efficiently.
535 decl.SetFile(FileSpec("<invalid>", false));
538 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
539 form_value.Unsigned()));
542 case DW_AT_decl_line:
543 decl.SetLine(form_value.Unsigned());
546 case DW_AT_decl_column:
547 decl.SetColumn(form_value.Unsigned());
551 type_name_cstr = form_value.AsCString();
552 type_name_const_str.SetCString(type_name_cstr);
555 case DW_AT_byte_size:
556 byte_size = form_value.Unsigned();
557 byte_size_valid = true;
560 case DW_AT_accessibility:
561 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
564 case DW_AT_declaration:
565 is_forward_declaration = form_value.Boolean();
568 case DW_AT_APPLE_runtime_class:
569 class_language = (LanguageType)form_value.Signed();
572 case DW_AT_APPLE_objc_complete_type:
573 is_complete_objc_class = form_value.Signed();
576 case DW_AT_allocated:
577 case DW_AT_associated:
578 case DW_AT_data_location:
579 case DW_AT_description:
580 case DW_AT_start_scope:
581 case DW_AT_visibility:
590 // UniqueDWARFASTType is large, so don't create a local variables on the
591 // stack, put it on the heap. This function is often called recursively
592 // and clang isn't good and sharing the stack space for variables in
594 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(
595 new UniqueDWARFASTType());
597 ConstString unique_typename(type_name_const_str);
598 Declaration unique_decl(decl);
600 if (type_name_const_str) {
601 LanguageType die_language = die.GetLanguage();
602 if (Language::LanguageIsCPlusPlus(die_language)) {
603 // For C++, we rely solely upon the one definition rule that says
605 // one thing can exist at a given decl context. We ignore the file
607 // line that things are declared on.
608 std::string qualified_name;
609 if (die.GetQualifiedName(qualified_name))
610 unique_typename = ConstString(qualified_name);
614 if (dwarf->GetUniqueDWARFASTTypeMap().Find(
615 unique_typename, die, unique_decl,
616 byte_size_valid ? byte_size : -1, *unique_ast_entry_ap)) {
617 type_sp = unique_ast_entry_ap->m_type_sp;
619 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
625 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
626 DW_TAG_value_to_name(tag), type_name_cstr);
628 int tag_decl_kind = -1;
629 AccessType default_accessibility = eAccessNone;
630 if (tag == DW_TAG_structure_type) {
631 tag_decl_kind = clang::TTK_Struct;
632 default_accessibility = eAccessPublic;
633 } else if (tag == DW_TAG_union_type) {
634 tag_decl_kind = clang::TTK_Union;
635 default_accessibility = eAccessPublic;
636 } else if (tag == DW_TAG_class_type) {
637 tag_decl_kind = clang::TTK_Class;
638 default_accessibility = eAccessPrivate;
641 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
642 die.HasChildren() == false &&
643 sc.comp_unit->GetLanguage() == eLanguageTypeObjC) {
644 // Work around an issue with clang at the moment where
645 // forward declarations for objective C classes are emitted
647 // DW_TAG_structure_type [2]
648 // DW_AT_name( "ForwardObjcClass" )
649 // DW_AT_byte_size( 0x00 )
650 // DW_AT_decl_file( "..." )
651 // DW_AT_decl_line( 1 )
653 // Note that there is no DW_AT_declaration and there are
654 // no children, and the byte size is zero.
655 is_forward_declaration = true;
658 if (class_language == eLanguageTypeObjC ||
659 class_language == eLanguageTypeObjC_plus_plus) {
660 if (!is_complete_objc_class &&
661 die.Supports_DW_AT_APPLE_objc_complete_type()) {
662 // We have a valid eSymbolTypeObjCClass class symbol whose
663 // name matches the current objective C class that we
664 // are trying to find and this DIE isn't the complete
665 // definition (we checked is_complete_objc_class above and
666 // know it is false), so the real definition is in here somewhere
667 type_sp = dwarf->FindCompleteObjCDefinitionTypeForDIE(
668 die, type_name_const_str, true);
671 SymbolFileDWARFDebugMap *debug_map_symfile =
672 dwarf->GetDebugMapSymfile();
673 if (debug_map_symfile) {
674 // We weren't able to find a full declaration in
675 // this DWARF, see if we have a declaration anywhere
678 debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE(
679 die, type_name_const_str, true);
685 dwarf->GetObjectFile()->GetModule()->LogMessage(
686 log, "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an "
687 "incomplete objc type, complete type is 0x%8.8" PRIx64,
688 static_cast<void *>(this), die.GetOffset(),
689 DW_TAG_value_to_name(tag), type_name_cstr,
693 // We found a real definition for this type elsewhere
694 // so lets use it and cache the fact that we found
695 // a complete type for this die
696 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
702 if (is_forward_declaration) {
703 // We have a forward declaration to a type and we need
704 // to try and find a full declaration. We look in the
705 // current type index just in case we have a forward
706 // declaration followed by an actual declarations in the
707 // DWARF. If this fails, we need to look elsewhere...
709 dwarf->GetObjectFile()->GetModule()->LogMessage(
710 log, "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
711 "forward declaration, trying to find complete type",
712 static_cast<void *>(this), die.GetOffset(),
713 DW_TAG_value_to_name(tag), type_name_cstr);
716 // See if the type comes from a DWO module and if so, track down that
718 type_sp = ParseTypeFromDWO(die, log);
722 DWARFDeclContext die_decl_ctx;
723 die.GetDWARFDeclContext(die_decl_ctx);
725 // type_sp = FindDefinitionTypeForDIE (dwarf_cu, die,
726 // type_name_const_str);
727 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
730 SymbolFileDWARFDebugMap *debug_map_symfile =
731 dwarf->GetDebugMapSymfile();
732 if (debug_map_symfile) {
733 // We weren't able to find a full declaration in
734 // this DWARF, see if we have a declaration anywhere
737 debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
744 dwarf->GetObjectFile()->GetModule()->LogMessage(
745 log, "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
746 "forward declaration, complete type is 0x%8.8" PRIx64,
747 static_cast<void *>(this), die.GetOffset(),
748 DW_TAG_value_to_name(tag), type_name_cstr, type_sp->GetID());
751 // We found a real definition for this type elsewhere
752 // so lets use it and cache the fact that we found
753 // a complete type for this die
754 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
755 clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(
756 dwarf->DebugInfo()->GetDIE(DIERef(type_sp->GetID(), dwarf)));
758 LinkDeclContextToDIE(defn_decl_ctx, die);
762 assert(tag_decl_kind != -1);
763 bool clang_type_was_created = false;
764 clang_type.SetCompilerType(
765 &m_ast, dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
767 clang::DeclContext *decl_ctx =
768 GetClangDeclContextContainingDIE(die, nullptr);
769 if (accessibility == eAccessNone && decl_ctx) {
770 // Check the decl context that contains this class/struct/union.
771 // If it is a class we must give it an accessibility.
772 const clang::Decl::Kind containing_decl_kind =
773 decl_ctx->getDeclKind();
774 if (DeclKindIsCXXClass(containing_decl_kind))
775 accessibility = default_accessibility;
778 ClangASTMetadata metadata;
779 metadata.SetUserID(die.GetID());
780 metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual(die));
782 if (type_name_cstr && strchr(type_name_cstr, '<')) {
783 ClangASTContext::TemplateParameterInfos template_param_infos;
784 if (ParseTemplateParameterInfos(die, template_param_infos)) {
785 clang::ClassTemplateDecl *class_template_decl =
786 m_ast.ParseClassTemplateDecl(decl_ctx, accessibility,
787 type_name_cstr, tag_decl_kind,
788 template_param_infos);
790 clang::ClassTemplateSpecializationDecl
791 *class_specialization_decl =
792 m_ast.CreateClassTemplateSpecializationDecl(
793 decl_ctx, class_template_decl, tag_decl_kind,
794 template_param_infos);
795 clang_type = m_ast.CreateClassTemplateSpecializationType(
796 class_specialization_decl);
797 clang_type_was_created = true;
799 m_ast.SetMetadata(class_template_decl, metadata);
800 m_ast.SetMetadata(class_specialization_decl, metadata);
804 if (!clang_type_was_created) {
805 clang_type_was_created = true;
806 clang_type = m_ast.CreateRecordType(decl_ctx, accessibility,
807 type_name_cstr, tag_decl_kind,
808 class_language, &metadata);
812 // Store a forward declaration to this class type in case any
813 // parameters in any class methods need it for the clang
814 // types for function prototypes.
815 LinkDeclContextToDIE(m_ast.GetDeclContextForType(clang_type), die);
816 type_sp.reset(new Type(die.GetID(), dwarf, type_name_const_str,
817 byte_size, NULL, LLDB_INVALID_UID,
818 Type::eEncodingIsUID, &decl, clang_type,
819 Type::eResolveStateForward));
821 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
823 // Add our type to the unique type map so we don't
824 // end up creating many copies of the same type over
825 // and over in the ASTContext for our module
826 unique_ast_entry_ap->m_type_sp = type_sp;
827 unique_ast_entry_ap->m_die = die;
828 unique_ast_entry_ap->m_declaration = unique_decl;
829 unique_ast_entry_ap->m_byte_size = byte_size;
830 dwarf->GetUniqueDWARFASTTypeMap().Insert(unique_typename,
831 *unique_ast_entry_ap);
833 if (is_forward_declaration && die.HasChildren()) {
834 // Check to see if the DIE actually has a definition, some version of
836 // emit DIEs with DW_AT_declaration set to true, but yet still have
838 // members, or inheritance, so we can't trust it
839 DWARFDIE child_die = die.GetFirstChild();
841 switch (child_die.Tag()) {
842 case DW_TAG_inheritance:
843 case DW_TAG_subprogram:
845 case DW_TAG_APPLE_property:
846 case DW_TAG_class_type:
847 case DW_TAG_structure_type:
848 case DW_TAG_enumeration_type:
850 case DW_TAG_union_type:
852 is_forward_declaration = false;
855 child_die = child_die.GetSibling();
861 if (!is_forward_declaration) {
862 // Always start the definition for a class type so that
863 // if the class has child classes or types that require
864 // the class to be created for use as their decl contexts
865 // the class will be ready to accept these child definitions.
866 if (die.HasChildren() == false) {
867 // No children for this struct/union/class, lets finish it
868 if (ClangASTContext::StartTagDeclarationDefinition(clang_type)) {
869 ClangASTContext::CompleteTagDeclarationDefinition(clang_type);
871 dwarf->GetObjectFile()->GetModule()->ReportError(
872 "DWARF DIE at 0x%8.8x named \"%s\" was not able to start its "
873 "definition.\nPlease file a bug and attach the file at the "
874 "start of this error message",
875 die.GetOffset(), type_name_cstr);
878 if (tag == DW_TAG_structure_type) // this only applies in C
880 clang::RecordDecl *record_decl =
881 ClangASTContext::GetAsRecordDecl(clang_type);
884 GetClangASTImporter().InsertRecordDecl(
885 record_decl, ClangASTImporter::LayoutInfo());
888 } else if (clang_type_was_created) {
889 // Start the definition if the class is not objective C since
890 // the underlying decls respond to isCompleteDefinition(). Objective
891 // C decls don't respond to isCompleteDefinition() so we can't
892 // start the declaration definition right away. For C++
893 // class/union/structs
894 // we want to start the definition in case the class is needed as
896 // declaration context for a contained class or type without the
898 // to complete that type..
900 if (class_language != eLanguageTypeObjC &&
901 class_language != eLanguageTypeObjC_plus_plus)
902 ClangASTContext::StartTagDeclarationDefinition(clang_type);
904 // Leave this as a forward declaration until we need
905 // to know the details of the type. lldb_private::Type
906 // will automatically call the SymbolFile virtual function
907 // "SymbolFileDWARF::CompleteType(Type *)"
908 // When the definition needs to be defined.
909 assert(!dwarf->GetForwardDeclClangTypeToDie().count(
910 ClangUtil::RemoveFastQualifiers(clang_type)
911 .GetOpaqueQualType()) &&
912 "Type already in the forward declaration map!");
913 // Can't assume m_ast.GetSymbolFile() is actually a SymbolFileDWARF,
915 // SymbolFileDWARFDebugMap for Apple binaries.
916 dwarf->GetForwardDeclDieToClangType()[die.GetDIE()] =
917 clang_type.GetOpaqueQualType();
918 dwarf->GetForwardDeclClangTypeToDie()
919 [ClangUtil::RemoveFastQualifiers(clang_type)
920 .GetOpaqueQualType()] = die.GetDIERef();
921 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
926 case DW_TAG_enumeration_type: {
927 // Set a bit that lets us know that we are currently parsing this
928 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
930 DWARFFormValue encoding_form;
932 const size_t num_attributes = die.GetAttributes(attributes);
933 if (num_attributes > 0) {
936 for (i = 0; i < num_attributes; ++i) {
937 attr = attributes.AttributeAtIndex(i);
938 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
940 case DW_AT_decl_file:
941 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
942 form_value.Unsigned()));
944 case DW_AT_decl_line:
945 decl.SetLine(form_value.Unsigned());
947 case DW_AT_decl_column:
948 decl.SetColumn(form_value.Unsigned());
951 type_name_cstr = form_value.AsCString();
952 type_name_const_str.SetCString(type_name_cstr);
955 encoding_form = form_value;
957 case DW_AT_byte_size:
958 byte_size = form_value.Unsigned();
960 case DW_AT_accessibility:
961 break; // accessibility =
962 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
963 case DW_AT_declaration:
964 is_forward_declaration = form_value.Boolean();
966 case DW_AT_allocated:
967 case DW_AT_associated:
968 case DW_AT_bit_stride:
969 case DW_AT_byte_stride:
970 case DW_AT_data_location:
971 case DW_AT_description:
972 case DW_AT_start_scope:
973 case DW_AT_visibility:
974 case DW_AT_specification:
975 case DW_AT_abstract_origin:
982 if (is_forward_declaration) {
983 type_sp = ParseTypeFromDWO(die, log);
987 DWARFDeclContext die_decl_ctx;
988 die.GetDWARFDeclContext(die_decl_ctx);
991 dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
994 SymbolFileDWARFDebugMap *debug_map_symfile =
995 dwarf->GetDebugMapSymfile();
996 if (debug_map_symfile) {
997 // We weren't able to find a full declaration in
998 // this DWARF, see if we have a declaration anywhere
1001 debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
1008 dwarf->GetObjectFile()->GetModule()->LogMessage(
1009 log, "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
1010 "forward declaration, complete type is 0x%8.8" PRIx64,
1011 static_cast<void *>(this), die.GetOffset(),
1012 DW_TAG_value_to_name(tag), type_name_cstr,
1016 // We found a real definition for this type elsewhere
1017 // so lets use it and cache the fact that we found
1018 // a complete type for this die
1019 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1020 clang::DeclContext *defn_decl_ctx =
1021 GetCachedClangDeclContextForDIE(dwarf->DebugInfo()->GetDIE(
1022 DIERef(type_sp->GetID(), dwarf)));
1024 LinkDeclContextToDIE(defn_decl_ctx, die);
1028 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1029 DW_TAG_value_to_name(tag), type_name_cstr);
1031 CompilerType enumerator_clang_type;
1032 clang_type.SetCompilerType(
1034 dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
1036 if (encoding_form.IsValid()) {
1037 Type *enumerator_type =
1038 dwarf->ResolveTypeUID(DIERef(encoding_form));
1039 if (enumerator_type)
1040 enumerator_clang_type = enumerator_type->GetFullCompilerType();
1043 if (!enumerator_clang_type) {
1044 if (byte_size > 0) {
1045 enumerator_clang_type =
1046 m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
1047 NULL, DW_ATE_signed, byte_size * 8);
1049 enumerator_clang_type = m_ast.GetBasicType(eBasicTypeInt);
1053 clang_type = m_ast.CreateEnumerationType(
1054 type_name_cstr, GetClangDeclContextContainingDIE(die, nullptr),
1055 decl, enumerator_clang_type);
1057 enumerator_clang_type =
1058 m_ast.GetEnumerationIntegerType(clang_type.GetOpaqueQualType());
1061 LinkDeclContextToDIE(
1062 ClangASTContext::GetDeclContextForType(clang_type), die);
1064 type_sp.reset(new Type(
1065 die.GetID(), dwarf, type_name_const_str, byte_size, NULL,
1066 DIERef(encoding_form).GetUID(dwarf), Type::eEncodingIsUID, &decl,
1067 clang_type, Type::eResolveStateForward));
1069 if (ClangASTContext::StartTagDeclarationDefinition(clang_type)) {
1070 if (die.HasChildren()) {
1071 SymbolContext cu_sc(die.GetLLDBCompileUnit());
1072 bool is_signed = false;
1073 enumerator_clang_type.IsIntegerType(is_signed);
1074 ParseChildEnumerators(cu_sc, clang_type, is_signed,
1075 type_sp->GetByteSize(), die);
1077 ClangASTContext::CompleteTagDeclarationDefinition(clang_type);
1079 dwarf->GetObjectFile()->GetModule()->ReportError(
1080 "DWARF DIE at 0x%8.8x named \"%s\" was not able to start its "
1081 "definition.\nPlease file a bug and attach the file at the "
1082 "start of this error message",
1083 die.GetOffset(), type_name_cstr);
1088 case DW_TAG_inlined_subroutine:
1089 case DW_TAG_subprogram:
1090 case DW_TAG_subroutine_type: {
1091 // Set a bit that lets us know that we are currently parsing this
1092 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
1094 DWARFFormValue type_die_form;
1095 bool is_variadic = false;
1096 bool is_inline = false;
1097 bool is_static = false;
1098 bool is_virtual = false;
1099 bool is_explicit = false;
1100 bool is_artificial = false;
1101 bool has_template_params = false;
1102 DWARFFormValue specification_die_form;
1103 DWARFFormValue abstract_origin_die_form;
1104 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
1106 unsigned type_quals = 0;
1107 clang::StorageClass storage =
1108 clang::SC_None; //, Extern, Static, PrivateExtern
1110 const size_t num_attributes = die.GetAttributes(attributes);
1111 if (num_attributes > 0) {
1113 for (i = 0; i < num_attributes; ++i) {
1114 attr = attributes.AttributeAtIndex(i);
1115 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1117 case DW_AT_decl_file:
1118 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
1119 form_value.Unsigned()));
1121 case DW_AT_decl_line:
1122 decl.SetLine(form_value.Unsigned());
1124 case DW_AT_decl_column:
1125 decl.SetColumn(form_value.Unsigned());
1128 type_name_cstr = form_value.AsCString();
1129 type_name_const_str.SetCString(type_name_cstr);
1132 case DW_AT_linkage_name:
1133 case DW_AT_MIPS_linkage_name:
1135 // form_value.AsCString(&dwarf->get_debug_str_data());
1138 type_die_form = form_value;
1140 case DW_AT_accessibility:
1141 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
1143 case DW_AT_declaration:
1144 break; // is_forward_declaration = form_value.Boolean(); break;
1146 is_inline = form_value.Boolean();
1148 case DW_AT_virtuality:
1149 is_virtual = form_value.Boolean();
1151 case DW_AT_explicit:
1152 is_explicit = form_value.Boolean();
1154 case DW_AT_artificial:
1155 is_artificial = form_value.Boolean();
1158 case DW_AT_external:
1159 if (form_value.Unsigned()) {
1160 if (storage == clang::SC_None)
1161 storage = clang::SC_Extern;
1163 storage = clang::SC_PrivateExtern;
1167 case DW_AT_specification:
1168 specification_die_form = form_value;
1171 case DW_AT_abstract_origin:
1172 abstract_origin_die_form = form_value;
1175 case DW_AT_object_pointer:
1176 object_pointer_die_offset = form_value.Reference();
1179 case DW_AT_allocated:
1180 case DW_AT_associated:
1181 case DW_AT_address_class:
1182 case DW_AT_calling_convention:
1183 case DW_AT_data_location:
1184 case DW_AT_elemental:
1185 case DW_AT_entry_pc:
1186 case DW_AT_frame_base:
1189 case DW_AT_prototyped:
1192 case DW_AT_recursive:
1193 case DW_AT_return_addr:
1195 case DW_AT_start_scope:
1196 case DW_AT_static_link:
1197 case DW_AT_trampoline:
1198 case DW_AT_visibility:
1199 case DW_AT_vtable_elem_location:
1200 case DW_AT_description:
1208 std::string object_pointer_name;
1209 if (object_pointer_die_offset != DW_INVALID_OFFSET) {
1210 DWARFDIE object_pointer_die = die.GetDIE(object_pointer_die_offset);
1211 if (object_pointer_die) {
1212 const char *object_pointer_name_cstr = object_pointer_die.GetName();
1213 if (object_pointer_name_cstr)
1214 object_pointer_name = object_pointer_name_cstr;
1218 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1219 DW_TAG_value_to_name(tag), type_name_cstr);
1221 CompilerType return_clang_type;
1222 Type *func_type = NULL;
1224 if (type_die_form.IsValid())
1225 func_type = dwarf->ResolveTypeUID(DIERef(type_die_form));
1228 return_clang_type = func_type->GetForwardCompilerType();
1230 return_clang_type = m_ast.GetBasicType(eBasicTypeVoid);
1232 std::vector<CompilerType> function_param_types;
1233 std::vector<clang::ParmVarDecl *> function_param_decls;
1235 // Parse the function children for the parameters
1237 DWARFDIE decl_ctx_die;
1238 clang::DeclContext *containing_decl_ctx =
1239 GetClangDeclContextContainingDIE(die, &decl_ctx_die);
1240 const clang::Decl::Kind containing_decl_kind =
1241 containing_decl_ctx->getDeclKind();
1243 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_kind);
1244 // Start off static. This will be set to false in
1245 // ParseChildParameters(...)
1246 // if we find a "this" parameters as the first parameter
1247 if (is_cxx_method) {
1251 if (die.HasChildren()) {
1252 bool skip_artificial = true;
1253 ParseChildParameters(sc, containing_decl_ctx, die, skip_artificial,
1254 is_static, is_variadic, has_template_params,
1255 function_param_types, function_param_decls,
1259 bool ignore_containing_context = false;
1260 // Check for templatized class member functions. If we had any
1261 // DW_TAG_template_type_parameter
1262 // or DW_TAG_template_value_parameter the DW_TAG_subprogram DIE, then we
1263 // can't let this become
1264 // a method in a class. Why? Because templatized functions are only
1265 // emitted if one of the
1266 // templatized methods is used in the current compile unit and we will
1267 // end up with classes
1268 // that may or may not include these member functions and this means one
1269 // class won't match another
1270 // class definition and it affects our ability to use a class in the
1271 // clang expression parser. So
1272 // for the greater good, we currently must not allow any template member
1273 // functions in a class definition.
1274 if (is_cxx_method && has_template_params) {
1275 ignore_containing_context = true;
1276 is_cxx_method = false;
1279 // clang_type will get the function prototype clang type after this call
1280 clang_type = m_ast.CreateFunctionType(
1281 return_clang_type, function_param_types.data(),
1282 function_param_types.size(), is_variadic, type_quals);
1284 if (type_name_cstr) {
1285 bool type_handled = false;
1286 if (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) {
1287 ObjCLanguage::MethodName objc_method(type_name_cstr, true);
1288 if (objc_method.IsValid(true)) {
1289 CompilerType class_opaque_type;
1290 ConstString class_name(objc_method.GetClassName());
1292 TypeSP complete_objc_class_type_sp(
1293 dwarf->FindCompleteObjCDefinitionTypeForDIE(
1294 DWARFDIE(), class_name, false));
1296 if (complete_objc_class_type_sp) {
1297 CompilerType type_clang_forward_type =
1298 complete_objc_class_type_sp->GetForwardCompilerType();
1299 if (ClangASTContext::IsObjCObjectOrInterfaceType(
1300 type_clang_forward_type))
1301 class_opaque_type = type_clang_forward_type;
1305 if (class_opaque_type) {
1306 // If accessibility isn't set to anything valid, assume public
1309 if (accessibility == eAccessNone)
1310 accessibility = eAccessPublic;
1312 clang::ObjCMethodDecl *objc_method_decl =
1313 m_ast.AddMethodToObjCObjectType(
1314 class_opaque_type, type_name_cstr, clang_type,
1315 accessibility, is_artificial, is_variadic);
1316 type_handled = objc_method_decl != NULL;
1318 LinkDeclContextToDIE(
1319 ClangASTContext::GetAsDeclContext(objc_method_decl), die);
1320 m_ast.SetMetadataAsUserID(objc_method_decl, die.GetID());
1322 dwarf->GetObjectFile()->GetModule()->ReportError(
1323 "{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), "
1324 "please file a bug and attach the file at the start of "
1325 "this error message",
1326 die.GetOffset(), tag, DW_TAG_value_to_name(tag));
1329 } else if (is_cxx_method) {
1330 // Look at the parent of this DIE and see if is is
1331 // a class or struct and see if this is actually a
1333 Type *class_type = dwarf->ResolveType(decl_ctx_die);
1335 bool alternate_defn = false;
1336 if (class_type->GetID() != decl_ctx_die.GetID() ||
1337 decl_ctx_die.GetContainingDWOModuleDIE()) {
1338 alternate_defn = true;
1340 // We uniqued the parent class of this function to another
1342 // so we now need to associate all dies under "decl_ctx_die"
1344 // DIEs in the DIE for "class_type"...
1345 SymbolFileDWARF *class_symfile = NULL;
1346 DWARFDIE class_type_die;
1348 SymbolFileDWARFDebugMap *debug_map_symfile =
1349 dwarf->GetDebugMapSymfile();
1350 if (debug_map_symfile) {
1351 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(
1352 SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(
1353 class_type->GetID()));
1354 class_type_die = class_symfile->DebugInfo()->GetDIE(
1355 DIERef(class_type->GetID(), dwarf));
1357 class_symfile = dwarf;
1358 class_type_die = dwarf->DebugInfo()->GetDIE(
1359 DIERef(class_type->GetID(), dwarf));
1361 if (class_type_die) {
1362 DWARFDIECollection failures;
1364 CopyUniqueClassMethodTypes(decl_ctx_die, class_type_die,
1365 class_type, failures);
1367 // FIXME do something with these failures that's smarter
1369 // just dropping them on the ground. Unfortunately classes
1371 // like having stuff added to them after their definitions
1375 type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1376 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
1377 type_sp = type_ptr->shared_from_this();
1383 if (specification_die_form.IsValid()) {
1384 // We have a specification which we are going to base our
1386 // prototype off of, so we need this type to be completed so
1388 // m_die_to_decl_ctx for the method in the specification has a
1390 // clang decl context.
1391 class_type->GetForwardCompilerType();
1392 // If we have a specification, then the function type should
1394 // made with the specification and not with this die.
1395 DWARFDIE spec_die = dwarf->DebugInfo()->GetDIE(
1396 DIERef(specification_die_form));
1397 clang::DeclContext *spec_clang_decl_ctx =
1398 GetClangDeclContextForDIE(spec_die);
1399 if (spec_clang_decl_ctx) {
1400 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
1402 dwarf->GetObjectFile()->GetModule()->ReportWarning(
1403 "0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8" PRIx64
1405 die.GetID(), specification_die_form.Reference());
1407 type_handled = true;
1408 } else if (abstract_origin_die_form.IsValid()) {
1409 // We have a specification which we are going to base our
1411 // prototype off of, so we need this type to be completed so
1413 // m_die_to_decl_ctx for the method in the abstract origin has
1415 // clang decl context.
1416 class_type->GetForwardCompilerType();
1418 DWARFDIE abs_die = dwarf->DebugInfo()->GetDIE(
1419 DIERef(abstract_origin_die_form));
1420 clang::DeclContext *abs_clang_decl_ctx =
1421 GetClangDeclContextForDIE(abs_die);
1422 if (abs_clang_decl_ctx) {
1423 LinkDeclContextToDIE(abs_clang_decl_ctx, die);
1425 dwarf->GetObjectFile()->GetModule()->ReportWarning(
1426 "0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8" PRIx64
1428 die.GetID(), abstract_origin_die_form.Reference());
1430 type_handled = true;
1432 CompilerType class_opaque_type =
1433 class_type->GetForwardCompilerType();
1434 if (ClangASTContext::IsCXXClassType(class_opaque_type)) {
1435 if (class_opaque_type.IsBeingDefined() || alternate_defn) {
1436 if (!is_static && !die.HasChildren()) {
1437 // We have a C++ member function with no children (this
1439 // and clang will get mad if we try and make a function
1441 // well formed in the DWARF, so we will just skip it...
1442 type_handled = true;
1444 bool add_method = true;
1445 if (alternate_defn) {
1446 // If an alternate definition for the class exists,
1447 // then add the method only if an
1448 // equivalent is not already present.
1449 clang::CXXRecordDecl *record_decl =
1450 m_ast.GetAsCXXRecordDecl(
1451 class_opaque_type.GetOpaqueQualType());
1453 for (auto method_iter = record_decl->method_begin();
1454 method_iter != record_decl->method_end();
1456 clang::CXXMethodDecl *method_decl = *method_iter;
1457 if (method_decl->getNameInfo().getAsString() ==
1458 std::string(type_name_cstr)) {
1459 if (method_decl->getType() ==
1460 ClangUtil::GetQualType(clang_type)) {
1462 LinkDeclContextToDIE(
1463 ClangASTContext::GetAsDeclContext(
1466 type_handled = true;
1476 llvm::PrettyStackTraceFormat stack_trace(
1477 "SymbolFileDWARF::ParseType() is adding a method "
1478 "%s to class %s in DIE 0x%8.8" PRIx64 " from %s",
1480 class_type->GetName().GetCString(), die.GetID(),
1481 dwarf->GetObjectFile()
1486 const bool is_attr_used = false;
1487 // Neither GCC 4.2 nor clang++ currently set a valid
1489 // in the DWARF for C++ methods... Default to public
1491 if (accessibility == eAccessNone)
1492 accessibility = eAccessPublic;
1494 clang::CXXMethodDecl *cxx_method_decl =
1495 m_ast.AddMethodToCXXRecordType(
1496 class_opaque_type.GetOpaqueQualType(),
1497 type_name_cstr, clang_type, accessibility,
1498 is_virtual, is_static, is_inline, is_explicit,
1499 is_attr_used, is_artificial);
1501 type_handled = cxx_method_decl != NULL;
1504 LinkDeclContextToDIE(
1505 ClangASTContext::GetAsDeclContext(
1509 ClangASTMetadata metadata;
1510 metadata.SetUserID(die.GetID());
1512 if (!object_pointer_name.empty()) {
1513 metadata.SetObjectPtrName(
1514 object_pointer_name.c_str());
1517 "Setting object pointer name: %s on method "
1519 object_pointer_name.c_str(),
1520 static_cast<void *>(cxx_method_decl));
1522 m_ast.SetMetadata(cxx_method_decl, metadata);
1524 ignore_containing_context = true;
1529 // We were asked to parse the type for a method in a
1531 // class hasn't been asked to complete itself through the
1532 // clang::ExternalASTSource protocol, so we need to just
1534 // class complete itself and do things the right way, then
1536 // DIE should then have an entry in the
1537 // dwarf->GetDIEToType() map. First
1538 // we need to modify the dwarf->GetDIEToType() so it
1539 // doesn't think we are
1540 // trying to parse this DIE anymore...
1541 dwarf->GetDIEToType()[die.GetDIE()] = NULL;
1543 // Now we get the full type to force our class type to
1545 // using the clang::ExternalASTSource protocol which will
1547 // base classes and all methods (including the method for
1549 class_type->GetFullCompilerType();
1551 // The type for this DIE should have been filled in the
1552 // function call above
1553 type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1554 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
1555 type_sp = type_ptr->shared_from_this();
1559 // FIXME This is fixing some even uglier behavior but we
1561 // uniq the methods of each class as well as the class
1563 // <rdar://problem/11240464>
1564 type_handled = true;
1572 if (!type_handled) {
1573 clang::FunctionDecl *function_decl = nullptr;
1575 if (abstract_origin_die_form.IsValid()) {
1577 dwarf->DebugInfo()->GetDIE(DIERef(abstract_origin_die_form));
1581 if (dwarf->ResolveType(abs_die)) {
1582 function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(
1583 GetCachedClangDeclContextForDIE(abs_die));
1585 if (function_decl) {
1586 LinkDeclContextToDIE(function_decl, die);
1591 if (!function_decl) {
1592 // We just have a function that isn't part of a class
1593 function_decl = m_ast.CreateFunctionDeclaration(
1594 ignore_containing_context ? m_ast.GetTranslationUnitDecl()
1595 : containing_decl_ctx,
1596 type_name_cstr, clang_type, storage, is_inline);
1598 // if (template_param_infos.GetSize() >
1601 // clang::FunctionTemplateDecl
1602 // *func_template_decl =
1603 // CreateFunctionTemplateDecl
1604 // (containing_decl_ctx,
1607 // template_param_infos);
1609 // CreateFunctionTemplateSpecializationInfo
1611 // func_template_decl,
1612 // template_param_infos);
1614 // Add the decl to our DIE to decl context map
1616 lldbassert(function_decl);
1618 if (function_decl) {
1619 LinkDeclContextToDIE(function_decl, die);
1621 if (!function_param_decls.empty())
1622 m_ast.SetFunctionParameters(function_decl,
1623 &function_param_decls.front(),
1624 function_param_decls.size());
1626 ClangASTMetadata metadata;
1627 metadata.SetUserID(die.GetID());
1629 if (!object_pointer_name.empty()) {
1630 metadata.SetObjectPtrName(object_pointer_name.c_str());
1632 log->Printf("Setting object pointer name: %s on function "
1634 object_pointer_name.c_str(),
1635 static_cast<void *>(function_decl));
1637 m_ast.SetMetadata(function_decl, metadata);
1642 type_sp.reset(new Type(die.GetID(), dwarf, type_name_const_str, 0, NULL,
1643 LLDB_INVALID_UID, Type::eEncodingIsUID, &decl,
1644 clang_type, Type::eResolveStateFull));
1645 assert(type_sp.get());
1648 case DW_TAG_array_type: {
1649 // Set a bit that lets us know that we are currently parsing this
1650 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
1652 DWARFFormValue type_die_form;
1653 int64_t first_index = 0;
1654 uint32_t byte_stride = 0;
1655 uint32_t bit_stride = 0;
1656 bool is_vector = false;
1657 const size_t num_attributes = die.GetAttributes(attributes);
1659 if (num_attributes > 0) {
1661 for (i = 0; i < num_attributes; ++i) {
1662 attr = attributes.AttributeAtIndex(i);
1663 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1665 case DW_AT_decl_file:
1666 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
1667 form_value.Unsigned()));
1669 case DW_AT_decl_line:
1670 decl.SetLine(form_value.Unsigned());
1672 case DW_AT_decl_column:
1673 decl.SetColumn(form_value.Unsigned());
1676 type_name_cstr = form_value.AsCString();
1677 type_name_const_str.SetCString(type_name_cstr);
1681 type_die_form = form_value;
1683 case DW_AT_byte_size:
1684 break; // byte_size = form_value.Unsigned(); break;
1685 case DW_AT_byte_stride:
1686 byte_stride = form_value.Unsigned();
1688 case DW_AT_bit_stride:
1689 bit_stride = form_value.Unsigned();
1691 case DW_AT_GNU_vector:
1692 is_vector = form_value.Boolean();
1694 case DW_AT_accessibility:
1695 break; // accessibility =
1696 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
1697 case DW_AT_declaration:
1698 break; // is_forward_declaration = form_value.Boolean(); break;
1699 case DW_AT_allocated:
1700 case DW_AT_associated:
1701 case DW_AT_data_location:
1702 case DW_AT_description:
1703 case DW_AT_ordering:
1704 case DW_AT_start_scope:
1705 case DW_AT_visibility:
1706 case DW_AT_specification:
1707 case DW_AT_abstract_origin:
1714 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1715 DW_TAG_value_to_name(tag), type_name_cstr);
1717 DIERef type_die_ref(type_die_form);
1718 Type *element_type = dwarf->ResolveTypeUID(type_die_ref);
1721 std::vector<uint64_t> element_orders;
1722 ParseChildArrayInfo(sc, die, first_index, element_orders,
1723 byte_stride, bit_stride);
1724 if (byte_stride == 0 && bit_stride == 0)
1725 byte_stride = element_type->GetByteSize();
1726 CompilerType array_element_type =
1727 element_type->GetForwardCompilerType();
1729 if (ClangASTContext::IsCXXClassType(array_element_type) &&
1730 array_element_type.GetCompleteType() == false) {
1731 ModuleSP module_sp = die.GetModule();
1733 if (die.GetCU()->GetProducer() ==
1734 DWARFCompileUnit::eProducerClang)
1735 module_sp->ReportError(
1736 "DWARF DW_TAG_array_type DIE at 0x%8.8x has a "
1737 "class/union/struct element type DIE 0x%8.8x that is a "
1738 "forward declaration, not a complete definition.\nTry "
1739 "compiling the source file with -fno-limit-debug-info or "
1741 die.GetOffset(), type_die_ref.die_offset);
1743 module_sp->ReportError(
1744 "DWARF DW_TAG_array_type DIE at 0x%8.8x has a "
1745 "class/union/struct element type DIE 0x%8.8x that is a "
1746 "forward declaration, not a complete definition.\nPlease "
1747 "file a bug against the compiler and include the "
1748 "preprocessed output for %s",
1749 die.GetOffset(), type_die_ref.die_offset,
1750 die.GetLLDBCompileUnit()
1751 ? die.GetLLDBCompileUnit()->GetPath().c_str()
1752 : "the source file");
1755 // We have no choice other than to pretend that the element class
1757 // is complete. If we don't do this, clang will crash when trying
1758 // to layout the class. Since we provide layout assistance, all
1759 // ivars in this class and other classes will be fine, this is
1760 // the best we can do short of crashing.
1761 if (ClangASTContext::StartTagDeclarationDefinition(
1762 array_element_type)) {
1763 ClangASTContext::CompleteTagDeclarationDefinition(
1764 array_element_type);
1766 module_sp->ReportError("DWARF DIE at 0x%8.8x was not able to "
1767 "start its definition.\nPlease file a "
1768 "bug and attach the file at the start "
1769 "of this error message",
1770 type_die_ref.die_offset);
1774 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
1775 if (element_orders.size() > 0) {
1776 uint64_t num_elements = 0;
1777 std::vector<uint64_t>::const_reverse_iterator pos;
1778 std::vector<uint64_t>::const_reverse_iterator end =
1779 element_orders.rend();
1780 for (pos = element_orders.rbegin(); pos != end; ++pos) {
1781 num_elements = *pos;
1782 clang_type = m_ast.CreateArrayType(array_element_type,
1783 num_elements, is_vector);
1784 array_element_type = clang_type;
1785 array_element_bit_stride =
1786 num_elements ? array_element_bit_stride * num_elements
1787 : array_element_bit_stride;
1791 m_ast.CreateArrayType(array_element_type, 0, is_vector);
1793 ConstString empty_name;
1794 type_sp.reset(new Type(
1795 die.GetID(), dwarf, empty_name, array_element_bit_stride / 8,
1796 NULL, DIERef(type_die_form).GetUID(dwarf), Type::eEncodingIsUID,
1797 &decl, clang_type, Type::eResolveStateFull));
1798 type_sp->SetEncodingType(element_type);
1803 case DW_TAG_ptr_to_member_type: {
1804 DWARFFormValue type_die_form;
1805 DWARFFormValue containing_type_die_form;
1807 const size_t num_attributes = die.GetAttributes(attributes);
1809 if (num_attributes > 0) {
1811 for (i = 0; i < num_attributes; ++i) {
1812 attr = attributes.AttributeAtIndex(i);
1813 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1816 type_die_form = form_value;
1818 case DW_AT_containing_type:
1819 containing_type_die_form = form_value;
1825 Type *pointee_type = dwarf->ResolveTypeUID(DIERef(type_die_form));
1827 dwarf->ResolveTypeUID(DIERef(containing_type_die_form));
1829 CompilerType pointee_clang_type =
1830 pointee_type->GetForwardCompilerType();
1831 CompilerType class_clang_type = class_type->GetLayoutCompilerType();
1833 clang_type = ClangASTContext::CreateMemberPointerType(
1834 class_clang_type, pointee_clang_type);
1836 byte_size = clang_type.GetByteSize(nullptr);
1838 type_sp.reset(new Type(die.GetID(), dwarf, type_name_const_str,
1839 byte_size, NULL, LLDB_INVALID_UID,
1840 Type::eEncodingIsUID, NULL, clang_type,
1841 Type::eResolveStateForward));
1847 dwarf->GetObjectFile()->GetModule()->ReportError(
1848 "{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and "
1849 "attach the file at the start of this error message",
1850 die.GetOffset(), tag, DW_TAG_value_to_name(tag));
1854 if (type_sp.get()) {
1855 DWARFDIE sc_parent_die =
1856 SymbolFileDWARF::GetParentSymbolContextDIE(die);
1857 dw_tag_t sc_parent_tag = sc_parent_die.Tag();
1859 SymbolContextScope *symbol_context_scope = NULL;
1860 if (sc_parent_tag == DW_TAG_compile_unit) {
1861 symbol_context_scope = sc.comp_unit;
1862 } else if (sc.function != NULL && sc_parent_die) {
1863 symbol_context_scope =
1864 sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
1865 if (symbol_context_scope == NULL)
1866 symbol_context_scope = sc.function;
1869 if (symbol_context_scope != NULL) {
1870 type_sp->SetSymbolContextScope(symbol_context_scope);
1873 // We are ready to put this type into the uniqued list up at the module
1875 type_list->Insert(type_sp);
1877 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1879 } else if (type_ptr != DIE_IS_BEING_PARSED) {
1880 type_sp = type_ptr->shared_from_this();
1886 // DWARF parsing functions
1888 class DWARFASTParserClang::DelayedAddObjCClassProperty {
1890 DelayedAddObjCClassProperty(
1891 const CompilerType &class_opaque_type, const char *property_name,
1892 const CompilerType &property_opaque_type, // The property type is only
1893 // required if you don't have an
1895 clang::ObjCIvarDecl *ivar_decl, const char *property_setter_name,
1896 const char *property_getter_name, uint32_t property_attributes,
1897 const ClangASTMetadata *metadata)
1898 : m_class_opaque_type(class_opaque_type), m_property_name(property_name),
1899 m_property_opaque_type(property_opaque_type), m_ivar_decl(ivar_decl),
1900 m_property_setter_name(property_setter_name),
1901 m_property_getter_name(property_getter_name),
1902 m_property_attributes(property_attributes) {
1903 if (metadata != NULL) {
1904 m_metadata_ap.reset(new ClangASTMetadata());
1905 *m_metadata_ap = *metadata;
1909 DelayedAddObjCClassProperty(const DelayedAddObjCClassProperty &rhs) {
1913 DelayedAddObjCClassProperty &
1914 operator=(const DelayedAddObjCClassProperty &rhs) {
1915 m_class_opaque_type = rhs.m_class_opaque_type;
1916 m_property_name = rhs.m_property_name;
1917 m_property_opaque_type = rhs.m_property_opaque_type;
1918 m_ivar_decl = rhs.m_ivar_decl;
1919 m_property_setter_name = rhs.m_property_setter_name;
1920 m_property_getter_name = rhs.m_property_getter_name;
1921 m_property_attributes = rhs.m_property_attributes;
1923 if (rhs.m_metadata_ap.get()) {
1924 m_metadata_ap.reset(new ClangASTMetadata());
1925 *m_metadata_ap = *rhs.m_metadata_ap;
1931 return ClangASTContext::AddObjCClassProperty(
1932 m_class_opaque_type, m_property_name, m_property_opaque_type,
1933 m_ivar_decl, m_property_setter_name, m_property_getter_name,
1934 m_property_attributes, m_metadata_ap.get());
1938 CompilerType m_class_opaque_type;
1939 const char *m_property_name;
1940 CompilerType m_property_opaque_type;
1941 clang::ObjCIvarDecl *m_ivar_decl;
1942 const char *m_property_setter_name;
1943 const char *m_property_getter_name;
1944 uint32_t m_property_attributes;
1945 std::unique_ptr<ClangASTMetadata> m_metadata_ap;
1948 bool DWARFASTParserClang::ParseTemplateDIE(
1949 const DWARFDIE &die,
1950 ClangASTContext::TemplateParameterInfos &template_param_infos) {
1951 const dw_tag_t tag = die.Tag();
1954 case DW_TAG_template_type_parameter:
1955 case DW_TAG_template_value_parameter: {
1956 DWARFAttributes attributes;
1957 const size_t num_attributes = die.GetAttributes(attributes);
1958 const char *name = nullptr;
1959 CompilerType clang_type;
1960 uint64_t uval64 = 0;
1961 bool uval64_valid = false;
1962 if (num_attributes > 0) {
1963 DWARFFormValue form_value;
1964 for (size_t i = 0; i < num_attributes; ++i) {
1965 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1969 if (attributes.ExtractFormValueAtIndex(i, form_value))
1970 name = form_value.AsCString();
1974 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1975 Type *lldb_type = die.ResolveTypeUID(DIERef(form_value));
1977 clang_type = lldb_type->GetForwardCompilerType();
1981 case DW_AT_const_value:
1982 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1983 uval64_valid = true;
1984 uval64 = form_value.Unsigned();
1992 clang::ASTContext *ast = m_ast.getASTContext();
1994 clang_type = m_ast.GetBasicType(eBasicTypeVoid);
1997 bool is_signed = false;
1998 if (name && name[0])
1999 template_param_infos.names.push_back(name);
2001 template_param_infos.names.push_back(NULL);
2003 // Get the signed value for any integer or enumeration if available
2004 clang_type.IsIntegerOrEnumerationType(is_signed);
2006 if (tag == DW_TAG_template_value_parameter && uval64_valid) {
2007 llvm::APInt apint(clang_type.GetBitSize(nullptr), uval64, is_signed);
2008 template_param_infos.args.push_back(
2009 clang::TemplateArgument(*ast, llvm::APSInt(apint, !is_signed),
2010 ClangUtil::GetQualType(clang_type)));
2012 template_param_infos.args.push_back(
2013 clang::TemplateArgument(ClangUtil::GetQualType(clang_type)));
2028 bool DWARFASTParserClang::ParseTemplateParameterInfos(
2029 const DWARFDIE &parent_die,
2030 ClangASTContext::TemplateParameterInfos &template_param_infos) {
2035 Args template_parameter_names;
2036 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
2037 die = die.GetSibling()) {
2038 const dw_tag_t tag = die.Tag();
2041 case DW_TAG_template_type_parameter:
2042 case DW_TAG_template_value_parameter:
2043 ParseTemplateDIE(die, template_param_infos);
2050 if (template_param_infos.args.empty())
2052 return template_param_infos.args.size() == template_param_infos.names.size();
2055 bool DWARFASTParserClang::CompleteTypeFromDWARF(const DWARFDIE &die,
2056 lldb_private::Type *type,
2057 CompilerType &clang_type) {
2058 SymbolFileDWARF *dwarf = die.GetDWARF();
2060 std::lock_guard<std::recursive_mutex> guard(
2061 dwarf->GetObjectFile()->GetModule()->GetMutex());
2063 // Disable external storage for this type so we don't get anymore
2064 // clang::ExternalASTSource queries for this type.
2065 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), false);
2070 #if defined LLDB_CONFIGURATION_DEBUG
2071 //----------------------------------------------------------------------
2072 // For debugging purposes, the LLDB_DWARF_DONT_COMPLETE_TYPENAMES
2073 // environment variable can be set with one or more typenames separated
2074 // by ';' characters. This will cause this function to not complete any
2075 // types whose names match.
2077 // Examples of setting this environment variable:
2079 // LLDB_DWARF_DONT_COMPLETE_TYPENAMES=Foo
2080 // LLDB_DWARF_DONT_COMPLETE_TYPENAMES=Foo;Bar;Baz
2081 //----------------------------------------------------------------------
2082 const char *dont_complete_typenames_cstr =
2083 getenv("LLDB_DWARF_DONT_COMPLETE_TYPENAMES");
2084 if (dont_complete_typenames_cstr && dont_complete_typenames_cstr[0]) {
2085 const char *die_name = die.GetName();
2086 if (die_name && die_name[0]) {
2087 const char *match = strstr(dont_complete_typenames_cstr, die_name);
2089 size_t die_name_length = strlen(die_name);
2091 const char separator_char = ';';
2092 const char next_char = match[die_name_length];
2093 if (next_char == '\0' || next_char == separator_char) {
2094 if (match == dont_complete_typenames_cstr ||
2095 match[-1] == separator_char)
2098 match = strstr(match + 1, die_name);
2105 const dw_tag_t tag = die.Tag();
2108 nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
2110 dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
2111 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
2112 die.GetID(), die.GetTagAsCString(), type->GetName().AsCString());
2114 DWARFAttributes attributes;
2116 case DW_TAG_structure_type:
2117 case DW_TAG_union_type:
2118 case DW_TAG_class_type: {
2119 ClangASTImporter::LayoutInfo layout_info;
2122 if (die.HasChildren()) {
2123 LanguageType class_language = eLanguageTypeUnknown;
2124 if (ClangASTContext::IsObjCObjectOrInterfaceType(clang_type)) {
2125 class_language = eLanguageTypeObjC;
2126 // For objective C we don't start the definition when
2127 // the class is created.
2128 ClangASTContext::StartTagDeclarationDefinition(clang_type);
2131 int tag_decl_kind = -1;
2132 AccessType default_accessibility = eAccessNone;
2133 if (tag == DW_TAG_structure_type) {
2134 tag_decl_kind = clang::TTK_Struct;
2135 default_accessibility = eAccessPublic;
2136 } else if (tag == DW_TAG_union_type) {
2137 tag_decl_kind = clang::TTK_Union;
2138 default_accessibility = eAccessPublic;
2139 } else if (tag == DW_TAG_class_type) {
2140 tag_decl_kind = clang::TTK_Class;
2141 default_accessibility = eAccessPrivate;
2144 SymbolContext sc(die.GetLLDBCompileUnit());
2145 std::vector<clang::CXXBaseSpecifier *> base_classes;
2146 std::vector<int> member_accessibilities;
2147 bool is_a_class = false;
2148 // Parse members and base classes first
2149 DWARFDIECollection member_function_dies;
2151 DelayedPropertyList delayed_properties;
2152 ParseChildMembers(sc, die, clang_type, class_language, base_classes,
2153 member_accessibilities, member_function_dies,
2154 delayed_properties, default_accessibility, is_a_class,
2157 // Now parse any methods if there were any...
2158 size_t num_functions = member_function_dies.Size();
2159 if (num_functions > 0) {
2160 for (size_t i = 0; i < num_functions; ++i) {
2161 dwarf->ResolveType(member_function_dies.GetDIEAtIndex(i));
2165 if (class_language == eLanguageTypeObjC) {
2166 ConstString class_name(clang_type.GetTypeName());
2168 DIEArray method_die_offsets;
2169 dwarf->GetObjCMethodDIEOffsets(class_name, method_die_offsets);
2171 if (!method_die_offsets.empty()) {
2172 DWARFDebugInfo *debug_info = dwarf->DebugInfo();
2174 const size_t num_matches = method_die_offsets.size();
2175 for (size_t i = 0; i < num_matches; ++i) {
2176 const DIERef &die_ref = method_die_offsets[i];
2177 DWARFDIE method_die = debug_info->GetDIE(die_ref);
2180 method_die.ResolveType();
2184 for (DelayedPropertyList::iterator pi = delayed_properties.begin(),
2185 pe = delayed_properties.end();
2191 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2192 // need to tell the clang type it is actually a class.
2193 if (class_language != eLanguageTypeObjC) {
2194 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2195 m_ast.SetTagTypeKind(ClangUtil::GetQualType(clang_type),
2199 // Since DW_TAG_structure_type gets used for both classes
2200 // and structures, we may need to set any DW_TAG_member
2201 // fields to have a "private" access if none was specified.
2202 // When we parsed the child members we tracked that actual
2203 // accessibility value for each DW_TAG_member in the
2204 // "member_accessibilities" array. If the value for the
2205 // member is zero, then it was set to the "default_accessibility"
2206 // which for structs was "public". Below we correct this
2207 // by setting any fields to "private" that weren't correctly
2209 if (is_a_class && !member_accessibilities.empty()) {
2210 // This is a class and all members that didn't have
2211 // their access specified are private.
2212 m_ast.SetDefaultAccessForRecordFields(
2213 m_ast.GetAsRecordDecl(clang_type), eAccessPrivate,
2214 &member_accessibilities.front(), member_accessibilities.size());
2217 if (!base_classes.empty()) {
2218 // Make sure all base classes refer to complete types and not
2219 // forward declarations. If we don't do this, clang will crash
2220 // with an assertion in the call to
2221 // clang_type.SetBaseClassesForClassType()
2222 for (auto &base_class : base_classes) {
2223 clang::TypeSourceInfo *type_source_info =
2224 base_class->getTypeSourceInfo();
2225 if (type_source_info) {
2226 CompilerType base_class_type(
2227 &m_ast, type_source_info->getType().getAsOpaquePtr());
2228 if (base_class_type.GetCompleteType() == false) {
2229 auto module = dwarf->GetObjectFile()->GetModule();
2230 module->ReportError(":: Class '%s' has a base class '%s' which "
2231 "does not have a complete definition.",
2233 base_class_type.GetTypeName().GetCString());
2234 if (die.GetCU()->GetProducer() ==
2235 DWARFCompileUnit::eProducerClang)
2236 module->ReportError(":: Try compiling the source file with "
2237 "-fno-limit-debug-info.");
2239 // We have no choice other than to pretend that the base class
2240 // is complete. If we don't do this, clang will crash when we
2241 // call setBases() inside of
2242 // "clang_type.SetBaseClassesForClassType()"
2243 // below. Since we provide layout assistance, all ivars in this
2244 // class and other classes will be fine, this is the best we can
2246 // short of crashing.
2247 if (ClangASTContext::StartTagDeclarationDefinition(
2249 ClangASTContext::CompleteTagDeclarationDefinition(
2255 m_ast.SetBaseClassesForClassType(clang_type.GetOpaqueQualType(),
2256 &base_classes.front(),
2257 base_classes.size());
2259 // Clang will copy each CXXBaseSpecifier in "base_classes"
2260 // so we have to free them all.
2261 ClangASTContext::DeleteBaseClassSpecifiers(&base_classes.front(),
2262 base_classes.size());
2267 ClangASTContext::BuildIndirectFields(clang_type);
2268 ClangASTContext::CompleteTagDeclarationDefinition(clang_type);
2270 if (!layout_info.field_offsets.empty() ||
2271 !layout_info.base_offsets.empty() ||
2272 !layout_info.vbase_offsets.empty()) {
2274 layout_info.bit_size = type->GetByteSize() * 8;
2275 if (layout_info.bit_size == 0)
2276 layout_info.bit_size =
2277 die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2279 clang::CXXRecordDecl *record_decl =
2280 m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
2283 ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
2286 module_sp->LogMessage(
2288 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) "
2289 "caching layout info for record_decl = %p, bit_size = %" PRIu64
2290 ", alignment = %" PRIu64
2291 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])",
2292 static_cast<void *>(clang_type.GetOpaqueQualType()),
2293 static_cast<void *>(record_decl), layout_info.bit_size,
2294 layout_info.alignment,
2295 static_cast<uint32_t>(layout_info.field_offsets.size()),
2296 static_cast<uint32_t>(layout_info.base_offsets.size()),
2297 static_cast<uint32_t>(layout_info.vbase_offsets.size()));
2301 llvm::DenseMap<const clang::FieldDecl *, uint64_t>::const_iterator
2303 end = layout_info.field_offsets.end();
2304 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end;
2306 module_sp->LogMessage(
2307 log, "ClangASTContext::CompleteTypeFromDWARF (clang_type = "
2308 "%p) field[%u] = { bit_offset=%u, name='%s' }",
2309 static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
2310 static_cast<uint32_t>(pos->second),
2311 pos->first->getNameAsString().c_str());
2316 llvm::DenseMap<const clang::CXXRecordDecl *,
2317 clang::CharUnits>::const_iterator base_pos,
2318 base_end = layout_info.base_offsets.end();
2319 for (idx = 0, base_pos = layout_info.base_offsets.begin();
2320 base_pos != base_end; ++base_pos, ++idx) {
2321 module_sp->LogMessage(
2322 log, "ClangASTContext::CompleteTypeFromDWARF (clang_type = "
2323 "%p) base[%u] = { byte_offset=%u, name='%s' }",
2324 clang_type.GetOpaqueQualType(), idx,
2325 (uint32_t)base_pos->second.getQuantity(),
2326 base_pos->first->getNameAsString().c_str());
2330 llvm::DenseMap<const clang::CXXRecordDecl *,
2331 clang::CharUnits>::const_iterator vbase_pos,
2332 vbase_end = layout_info.vbase_offsets.end();
2333 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin();
2334 vbase_pos != vbase_end; ++vbase_pos, ++idx) {
2335 module_sp->LogMessage(
2336 log, "ClangASTContext::CompleteTypeFromDWARF (clang_type = "
2337 "%p) vbase[%u] = { byte_offset=%u, name='%s' }",
2338 static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
2339 static_cast<uint32_t>(vbase_pos->second.getQuantity()),
2340 vbase_pos->first->getNameAsString().c_str());
2345 GetClangASTImporter().InsertRecordDecl(record_decl, layout_info);
2350 return (bool)clang_type;
2352 case DW_TAG_enumeration_type:
2353 if (ClangASTContext::StartTagDeclarationDefinition(clang_type)) {
2354 if (die.HasChildren()) {
2355 SymbolContext sc(die.GetLLDBCompileUnit());
2356 bool is_signed = false;
2357 clang_type.IsIntegerType(is_signed);
2358 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(),
2361 ClangASTContext::CompleteTagDeclarationDefinition(clang_type);
2363 return (bool)clang_type;
2366 assert(false && "not a forward clang type decl!");
2373 std::vector<DWARFDIE> DWARFASTParserClang::GetDIEForDeclContext(
2374 lldb_private::CompilerDeclContext decl_context) {
2375 std::vector<DWARFDIE> result;
2376 for (auto it = m_decl_ctx_to_die.find(
2377 (clang::DeclContext *)decl_context.GetOpaqueDeclContext());
2378 it != m_decl_ctx_to_die.end(); it++)
2379 result.push_back(it->second);
2383 CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) {
2384 clang::Decl *clang_decl = GetClangDeclForDIE(die);
2385 if (clang_decl != nullptr)
2386 return CompilerDecl(&m_ast, clang_decl);
2387 return CompilerDecl();
2391 DWARFASTParserClang::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) {
2392 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die);
2394 return CompilerDeclContext(&m_ast, clang_decl_ctx);
2395 return CompilerDeclContext();
2399 DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) {
2400 clang::DeclContext *clang_decl_ctx =
2401 GetClangDeclContextContainingDIE(die, nullptr);
2403 return CompilerDeclContext(&m_ast, clang_decl_ctx);
2404 return CompilerDeclContext();
2407 size_t DWARFASTParserClang::ParseChildEnumerators(
2408 const SymbolContext &sc, lldb_private::CompilerType &clang_type,
2409 bool is_signed, uint32_t enumerator_byte_size, const DWARFDIE &parent_die) {
2413 size_t enumerators_added = 0;
2415 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
2416 die = die.GetSibling()) {
2417 const dw_tag_t tag = die.Tag();
2418 if (tag == DW_TAG_enumerator) {
2419 DWARFAttributes attributes;
2420 const size_t num_child_attributes = die.GetAttributes(attributes);
2421 if (num_child_attributes > 0) {
2422 const char *name = NULL;
2423 bool got_value = false;
2424 int64_t enum_value = 0;
2428 for (i = 0; i < num_child_attributes; ++i) {
2429 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2430 DWARFFormValue form_value;
2431 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2433 case DW_AT_const_value:
2436 enum_value = form_value.Signed();
2438 enum_value = form_value.Unsigned();
2442 name = form_value.AsCString();
2445 case DW_AT_description:
2447 case DW_AT_decl_file:
2448 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
2449 form_value.Unsigned()));
2451 case DW_AT_decl_line:
2452 decl.SetLine(form_value.Unsigned());
2454 case DW_AT_decl_column:
2455 decl.SetColumn(form_value.Unsigned());
2463 if (name && name[0] && got_value) {
2464 m_ast.AddEnumerationValueToEnumerationType(
2465 clang_type.GetOpaqueQualType(),
2466 m_ast.GetEnumerationIntegerType(clang_type.GetOpaqueQualType()),
2467 decl, name, enum_value, enumerator_byte_size * 8);
2468 ++enumerators_added;
2473 return enumerators_added;
2476 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
2480 void Push(const DWARFDIE &die) { m_dies.push_back(die); }
2482 void LogDIEs(Log *log) {
2483 StreamString log_strm;
2484 const size_t n = m_dies.size();
2485 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
2486 for (size_t i = 0; i < n; i++) {
2487 std::string qualified_name;
2488 const DWARFDIE &die = m_dies[i];
2489 die.GetQualifiedName(qualified_name);
2490 log_strm.Printf("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n", (uint64_t)i,
2491 die.GetOffset(), die.GetTagAsCString(),
2492 qualified_name.c_str());
2494 log->PutCString(log_strm.GetData());
2496 void Pop() { m_dies.pop_back(); }
2498 class ScopedPopper {
2500 ScopedPopper(DIEStack &die_stack)
2501 : m_die_stack(die_stack), m_valid(false) {}
2503 void Push(const DWARFDIE &die) {
2505 m_die_stack.Push(die);
2514 DIEStack &m_die_stack;
2519 typedef std::vector<DWARFDIE> Stack;
2524 Function *DWARFASTParserClang::ParseFunctionFromDWARF(const SymbolContext &sc,
2525 const DWARFDIE &die) {
2526 DWARFRangeList func_ranges;
2527 const char *name = NULL;
2528 const char *mangled = NULL;
2531 int decl_column = 0;
2534 int call_column = 0;
2535 DWARFExpression frame_base(die.GetCU());
2537 const dw_tag_t tag = die.Tag();
2539 if (tag != DW_TAG_subprogram)
2542 if (die.GetDIENamesAndRanges(name, mangled, func_ranges, decl_file, decl_line,
2543 decl_column, call_file, call_line, call_column,
2546 // Union of all ranges in the function DIE (if the function is
2548 AddressRange func_range;
2549 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase(0);
2550 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd(0);
2551 if (lowest_func_addr != LLDB_INVALID_ADDRESS &&
2552 lowest_func_addr <= highest_func_addr) {
2553 ModuleSP module_sp(die.GetModule());
2554 func_range.GetBaseAddress().ResolveAddressUsingFileSections(
2555 lowest_func_addr, module_sp->GetSectionList());
2556 if (func_range.GetBaseAddress().IsValid())
2557 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
2560 if (func_range.GetBaseAddress().IsValid()) {
2563 func_name.SetValue(ConstString(mangled), true);
2564 else if (die.GetParent().Tag() == DW_TAG_compile_unit &&
2565 Language::LanguageIsCPlusPlus(die.GetLanguage()) && name &&
2566 strcmp(name, "main") != 0) {
2567 // If the mangled name is not present in the DWARF, generate the
2569 // using the decl context. We skip if the function is "main" as its name
2572 bool is_static = false;
2573 bool is_variadic = false;
2574 bool has_template_params = false;
2575 unsigned type_quals = 0;
2576 std::vector<CompilerType> param_types;
2577 std::vector<clang::ParmVarDecl *> param_decls;
2578 DWARFDeclContext decl_ctx;
2581 die.GetDWARFDeclContext(decl_ctx);
2582 sstr << decl_ctx.GetQualifiedName();
2584 clang::DeclContext *containing_decl_ctx =
2585 GetClangDeclContextContainingDIE(die, nullptr);
2586 ParseChildParameters(sc, containing_decl_ctx, die, true, is_static,
2587 is_variadic, has_template_params, param_types,
2588 param_decls, type_quals);
2590 for (size_t i = 0; i < param_types.size(); i++) {
2593 sstr << param_types[i].GetTypeName();
2598 if (type_quals & clang::Qualifiers::Const)
2601 func_name.SetValue(ConstString(sstr.GetString()), false);
2603 func_name.SetValue(ConstString(name), false);
2606 std::unique_ptr<Declaration> decl_ap;
2607 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
2608 decl_ap.reset(new Declaration(
2609 sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
2610 decl_line, decl_column));
2612 SymbolFileDWARF *dwarf = die.GetDWARF();
2613 // Supply the type _only_ if it has already been parsed
2614 Type *func_type = dwarf->GetDIEToType().lookup(die.GetDIE());
2616 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
2618 if (dwarf->FixupAddress(func_range.GetBaseAddress())) {
2619 const user_id_t func_user_id = die.GetID();
2620 func_sp.reset(new Function(sc.comp_unit,
2621 func_user_id, // UserID is the DIE offset
2622 func_user_id, func_name, func_type,
2623 func_range)); // first address range
2625 if (func_sp.get() != NULL) {
2626 if (frame_base.IsValid())
2627 func_sp->GetFrameBaseExpression() = frame_base;
2628 sc.comp_unit->AddFunction(func_sp);
2629 return func_sp.get();
2637 bool DWARFASTParserClang::ParseChildMembers(
2638 const SymbolContext &sc, const DWARFDIE &parent_die,
2639 CompilerType &class_clang_type, const LanguageType class_language,
2640 std::vector<clang::CXXBaseSpecifier *> &base_classes,
2641 std::vector<int> &member_accessibilities,
2642 DWARFDIECollection &member_function_dies,
2643 DelayedPropertyList &delayed_properties, AccessType &default_accessibility,
2644 bool &is_a_class, ClangASTImporter::LayoutInfo &layout_info) {
2648 // Get the parent byte size so we can verify any members will fit
2649 const uint64_t parent_byte_size =
2650 parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
2651 const uint64_t parent_bit_size =
2652 parent_byte_size == UINT64_MAX ? UINT64_MAX : parent_byte_size * 8;
2654 uint32_t member_idx = 0;
2655 BitfieldInfo last_field_info;
2657 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2658 ClangASTContext *ast =
2659 llvm::dyn_cast_or_null<ClangASTContext>(class_clang_type.GetTypeSystem());
2663 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
2664 die = die.GetSibling()) {
2665 dw_tag_t tag = die.Tag();
2669 case DW_TAG_APPLE_property: {
2670 DWARFAttributes attributes;
2671 const size_t num_attributes = die.GetAttributes(attributes);
2672 if (num_attributes > 0) {
2674 // DWARFExpression location;
2675 const char *name = NULL;
2676 const char *prop_name = NULL;
2677 const char *prop_getter_name = NULL;
2678 const char *prop_setter_name = NULL;
2679 uint32_t prop_attributes = 0;
2681 bool is_artificial = false;
2682 DWARFFormValue encoding_form;
2683 AccessType accessibility = eAccessNone;
2684 uint32_t member_byte_offset =
2685 (parent_die.Tag() == DW_TAG_union_type) ? 0 : UINT32_MAX;
2686 size_t byte_size = 0;
2687 int64_t bit_offset = 0;
2688 uint64_t data_bit_offset = UINT64_MAX;
2689 size_t bit_size = 0;
2691 false; // On DW_TAG_members, this means the member is static
2693 for (i = 0; i < num_attributes && !is_artificial; ++i) {
2694 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2695 DWARFFormValue form_value;
2696 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2698 case DW_AT_decl_file:
2699 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
2700 form_value.Unsigned()));
2702 case DW_AT_decl_line:
2703 decl.SetLine(form_value.Unsigned());
2705 case DW_AT_decl_column:
2706 decl.SetColumn(form_value.Unsigned());
2709 name = form_value.AsCString();
2712 encoding_form = form_value;
2714 case DW_AT_bit_offset:
2715 bit_offset = form_value.Signed();
2717 case DW_AT_bit_size:
2718 bit_size = form_value.Unsigned();
2720 case DW_AT_byte_size:
2721 byte_size = form_value.Unsigned();
2723 case DW_AT_data_bit_offset:
2724 data_bit_offset = form_value.Unsigned();
2726 case DW_AT_data_member_location:
2727 if (form_value.BlockData()) {
2728 Value initialValue(0);
2729 Value memberOffset(0);
2730 const DWARFDataExtractor &debug_info_data =
2731 die.GetDWARF()->get_debug_info_data();
2732 uint32_t block_length = form_value.Unsigned();
2733 uint32_t block_offset =
2734 form_value.BlockData() - debug_info_data.GetDataStart();
2735 if (DWARFExpression::Evaluate(
2736 nullptr, // ExecutionContext *
2737 nullptr, // ClangExpressionVariableList *
2738 nullptr, // ClangExpressionDeclMap *
2739 nullptr, // RegisterContext *
2740 module_sp, debug_info_data, die.GetCU(), block_offset,
2741 block_length, eRegisterKindDWARF, &initialValue,
2742 nullptr, memberOffset, nullptr)) {
2743 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
2746 // With DWARF 3 and later, if the value is an integer constant,
2747 // this form value is the offset in bytes from the beginning
2748 // of the containing entity.
2749 member_byte_offset = form_value.Unsigned();
2753 case DW_AT_accessibility:
2754 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
2756 case DW_AT_artificial:
2757 is_artificial = form_value.Boolean();
2759 case DW_AT_APPLE_property_name:
2760 prop_name = form_value.AsCString();
2762 case DW_AT_APPLE_property_getter:
2763 prop_getter_name = form_value.AsCString();
2765 case DW_AT_APPLE_property_setter:
2766 prop_setter_name = form_value.AsCString();
2768 case DW_AT_APPLE_property_attribute:
2769 prop_attributes = form_value.Unsigned();
2771 case DW_AT_external:
2772 is_external = form_value.Boolean();
2776 case DW_AT_declaration:
2777 case DW_AT_description:
2779 case DW_AT_visibility:
2787 ConstString fixed_getter;
2788 ConstString fixed_setter;
2790 // Check if the property getter/setter were provided as full
2791 // names. We want basenames, so we extract them.
2793 if (prop_getter_name && prop_getter_name[0] == '-') {
2794 ObjCLanguage::MethodName prop_getter_method(prop_getter_name, true);
2795 prop_getter_name = prop_getter_method.GetSelector().GetCString();
2798 if (prop_setter_name && prop_setter_name[0] == '-') {
2799 ObjCLanguage::MethodName prop_setter_method(prop_setter_name, true);
2800 prop_setter_name = prop_setter_method.GetSelector().GetCString();
2803 // If the names haven't been provided, they need to be
2806 if (!prop_getter_name) {
2807 prop_getter_name = prop_name;
2809 if (!prop_setter_name && prop_name[0] &&
2810 !(prop_attributes & DW_APPLE_PROPERTY_readonly)) {
2813 ss.Printf("set%c%s:", toupper(prop_name[0]), &prop_name[1]);
2815 fixed_setter.SetString(ss.GetString());
2816 prop_setter_name = fixed_setter.GetCString();
2820 // Clang has a DWARF generation bug where sometimes it
2821 // represents fields that are references with bad byte size
2822 // and bit size/offset information such as:
2824 // DW_AT_byte_size( 0x00 )
2825 // DW_AT_bit_size( 0x40 )
2826 // DW_AT_bit_offset( 0xffffffffffffffc0 )
2828 // So check the bit offset to make sure it is sane, and if
2829 // the values are not sane, remove them. If we don't do this
2830 // then we will end up with a crash if we try to use this
2831 // type in an expression when clang becomes unhappy with its
2832 // recycled debug info.
2834 if (byte_size == 0 && bit_offset < 0) {
2839 // FIXME: Make Clang ignore Objective-C accessibility for expressions
2840 if (class_language == eLanguageTypeObjC ||
2841 class_language == eLanguageTypeObjC_plus_plus)
2842 accessibility = eAccessNone;
2844 if (member_idx == 0 && !is_artificial && name &&
2845 (strstr(name, "_vptr$") == name)) {
2846 // Not all compilers will mark the vtable pointer
2847 // member as artificial (llvm-gcc). We can't have
2848 // the virtual members in our classes otherwise it
2849 // throws off all child offsets since we end up
2850 // having and extra pointer sized member in our
2852 is_artificial = true;
2855 // Handle static members
2856 if (is_external && member_byte_offset == UINT32_MAX) {
2857 Type *var_type = die.ResolveTypeUID(DIERef(encoding_form));
2860 if (accessibility == eAccessNone)
2861 accessibility = eAccessPublic;
2862 ClangASTContext::AddVariableToRecordType(
2863 class_clang_type, name, var_type->GetLayoutCompilerType(),
2869 if (is_artificial == false) {
2870 Type *member_type = die.ResolveTypeUID(DIERef(encoding_form));
2872 clang::FieldDecl *field_decl = NULL;
2873 if (tag == DW_TAG_member) {
2875 if (accessibility == eAccessNone)
2876 accessibility = default_accessibility;
2877 member_accessibilities.push_back(accessibility);
2879 uint64_t field_bit_offset =
2880 (member_byte_offset == UINT32_MAX ? 0
2881 : (member_byte_offset * 8));
2884 BitfieldInfo this_field_info;
2885 this_field_info.bit_offset = field_bit_offset;
2886 this_field_info.bit_size = bit_size;
2888 /////////////////////////////////////////////////////////////
2889 // How to locate a field given the DWARF debug information
2891 // AT_byte_size indicates the size of the word in which the
2892 // bit offset must be interpreted.
2894 // AT_data_member_location indicates the byte offset of the
2895 // word from the base address of the structure.
2897 // AT_bit_offset indicates how many bits into the word
2898 // (according to the host endianness) the low-order bit of
2899 // the field starts. AT_bit_offset can be negative.
2901 // AT_bit_size indicates the size of the field in bits.
2902 /////////////////////////////////////////////////////////////
2904 if (data_bit_offset != UINT64_MAX) {
2905 this_field_info.bit_offset = data_bit_offset;
2908 byte_size = member_type->GetByteSize();
2910 ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2911 if (objfile->GetByteOrder() == eByteOrderLittle) {
2912 this_field_info.bit_offset += byte_size * 8;
2913 this_field_info.bit_offset -= (bit_offset + bit_size);
2915 this_field_info.bit_offset += bit_offset;
2919 if ((this_field_info.bit_offset >= parent_bit_size) ||
2920 !last_field_info.NextBitfieldOffsetIsValid(
2921 this_field_info.bit_offset)) {
2922 ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2923 objfile->GetModule()->ReportWarning(
2924 "0x%8.8" PRIx64 ": %s bitfield named \"%s\" has invalid "
2925 "bit offset (0x%8.8" PRIx64
2926 ") member will be ignored. Please file a bug against the "
2927 "compiler and include the preprocessed output for %s\n",
2928 die.GetID(), DW_TAG_value_to_name(tag), name,
2929 this_field_info.bit_offset,
2930 sc.comp_unit ? sc.comp_unit->GetPath().c_str()
2931 : "the source file");
2932 this_field_info.Clear();
2936 // Update the field bit offset we will report for layout
2937 field_bit_offset = this_field_info.bit_offset;
2939 // If the member to be emitted did not start on a character
2940 // boundary and there is
2941 // empty space between the last field and this one, then we need
2943 // anonymous member filling up the space up to its start. There
2947 // 1 If the previous member ended on a character boundary, then
2949 // anonymous member starting at the most recent character
2952 // 2 If the previous member did not end on a character boundary
2954 // from the end of the previous member to the current member
2956 // word width, then we can emit an anonymous member starting
2958 // previous member and right before this member.
2960 // 3 If the previous member did not end on a character boundary
2962 // from the end of the previous member to the current member
2964 // or equal a word width, then we act as in Case 1.
2966 const uint64_t character_width = 8;
2967 const uint64_t word_width = 32;
2969 // Objective-C has invalid DW_AT_bit_offset values in older
2971 // of clang, so we have to be careful and only insert unnamed
2973 // if we have a new enough clang.
2974 bool detect_unnamed_bitfields = true;
2976 if (class_language == eLanguageTypeObjC ||
2977 class_language == eLanguageTypeObjC_plus_plus)
2978 detect_unnamed_bitfields =
2979 die.GetCU()->Supports_unnamed_objc_bitfields();
2981 if (detect_unnamed_bitfields) {
2982 BitfieldInfo anon_field_info;
2984 if ((this_field_info.bit_offset % character_width) !=
2985 0) // not char aligned
2987 uint64_t last_field_end = 0;
2989 if (last_field_info.IsValid())
2991 last_field_info.bit_offset + last_field_info.bit_size;
2993 if (this_field_info.bit_offset != last_field_end) {
2994 if (((last_field_end % character_width) == 0) || // case 1
2995 (this_field_info.bit_offset - last_field_end >=
2996 word_width)) // case 3
2998 anon_field_info.bit_size =
2999 this_field_info.bit_offset % character_width;
3000 anon_field_info.bit_offset =
3001 this_field_info.bit_offset -
3002 anon_field_info.bit_size;
3005 anon_field_info.bit_size =
3006 this_field_info.bit_offset - last_field_end;
3007 anon_field_info.bit_offset = last_field_end;
3012 if (anon_field_info.IsValid()) {
3013 clang::FieldDecl *unnamed_bitfield_decl =
3014 ClangASTContext::AddFieldToRecordType(
3015 class_clang_type, NULL,
3016 m_ast.GetBuiltinTypeForEncodingAndBitSize(
3017 eEncodingSint, word_width),
3018 accessibility, anon_field_info.bit_size);
3020 layout_info.field_offsets.insert(std::make_pair(
3021 unnamed_bitfield_decl, anon_field_info.bit_offset));
3024 last_field_info = this_field_info;
3026 last_field_info.Clear();
3029 CompilerType member_clang_type =
3030 member_type->GetLayoutCompilerType();
3031 if (!member_clang_type.IsCompleteType())
3032 member_clang_type.GetCompleteType();
3035 // Older versions of clang emit array[0] and array[1] in the
3036 // same way (<rdar://problem/12566646>).
3037 // If the current field is at the end of the structure, then
3038 // there is definitely no room for extra
3039 // elements and we override the type to array[0].
3041 CompilerType member_array_element_type;
3042 uint64_t member_array_size;
3043 bool member_array_is_incomplete;
3045 if (member_clang_type.IsArrayType(
3046 &member_array_element_type, &member_array_size,
3047 &member_array_is_incomplete) &&
3048 !member_array_is_incomplete) {
3049 uint64_t parent_byte_size =
3050 parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size,
3053 if (member_byte_offset >= parent_byte_size) {
3054 if (member_array_size != 1 &&
3055 (member_array_size != 0 ||
3056 member_byte_offset > parent_byte_size)) {
3057 module_sp->ReportError(
3059 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64
3060 " which extends beyond the bounds of 0x%8.8" PRIx64,
3061 die.GetID(), name, encoding_form.Reference(),
3062 parent_die.GetID());
3065 member_clang_type = m_ast.CreateArrayType(
3066 member_array_element_type, 0, false);
3071 if (ClangASTContext::IsCXXClassType(member_clang_type) &&
3072 member_clang_type.GetCompleteType() == false) {
3073 if (die.GetCU()->GetProducer() ==
3074 DWARFCompileUnit::eProducerClang)
3075 module_sp->ReportError(
3076 "DWARF DIE at 0x%8.8x (class %s) has a member variable "
3077 "0x%8.8x (%s) whose type is a forward declaration, not a "
3078 "complete definition.\nTry compiling the source file "
3079 "with -fno-limit-debug-info",
3080 parent_die.GetOffset(), parent_die.GetName(),
3081 die.GetOffset(), name);
3083 module_sp->ReportError(
3084 "DWARF DIE at 0x%8.8x (class %s) has a member variable "
3085 "0x%8.8x (%s) whose type is a forward declaration, not a "
3086 "complete definition.\nPlease file a bug against the "
3087 "compiler and include the preprocessed output for %s",
3088 parent_die.GetOffset(), parent_die.GetName(),
3089 die.GetOffset(), name,
3090 sc.comp_unit ? sc.comp_unit->GetPath().c_str()
3091 : "the source file");
3092 // We have no choice other than to pretend that the member class
3093 // is complete. If we don't do this, clang will crash when
3095 // to layout the class. Since we provide layout assistance, all
3096 // ivars in this class and other classes will be fine, this is
3097 // the best we can do short of crashing.
3098 if (ClangASTContext::StartTagDeclarationDefinition(
3099 member_clang_type)) {
3100 ClangASTContext::CompleteTagDeclarationDefinition(
3103 module_sp->ReportError(
3104 "DWARF DIE at 0x%8.8x (class %s) has a member variable "
3105 "0x%8.8x (%s) whose type claims to be a C++ class but we "
3106 "were not able to start its definition.\nPlease file a "
3107 "bug and attach the file at the start of this error "
3109 parent_die.GetOffset(), parent_die.GetName(),
3110 die.GetOffset(), name);
3114 field_decl = ClangASTContext::AddFieldToRecordType(
3115 class_clang_type, name, member_clang_type, accessibility,
3118 m_ast.SetMetadataAsUserID(field_decl, die.GetID());
3120 layout_info.field_offsets.insert(
3121 std::make_pair(field_decl, field_bit_offset));
3124 module_sp->ReportError(
3126 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64
3127 " which was unable to be parsed",
3128 die.GetID(), name, encoding_form.Reference());
3130 module_sp->ReportError(
3132 ": DW_TAG_member refers to type 0x%8.8" PRIx64
3133 " which was unable to be parsed",
3134 die.GetID(), encoding_form.Reference());
3138 if (prop_name != NULL && member_type) {
3139 clang::ObjCIvarDecl *ivar_decl = NULL;
3142 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
3143 assert(ivar_decl != NULL);
3146 ClangASTMetadata metadata;
3147 metadata.SetUserID(die.GetID());
3148 delayed_properties.push_back(DelayedAddObjCClassProperty(
3149 class_clang_type, prop_name,
3150 member_type->GetLayoutCompilerType(), ivar_decl,
3151 prop_setter_name, prop_getter_name, prop_attributes,
3155 m_ast.SetMetadataAsUserID(ivar_decl, die.GetID());
3162 case DW_TAG_subprogram:
3163 // Let the type parsing code handle this one for us.
3164 member_function_dies.Append(die);
3167 case DW_TAG_inheritance: {
3169 if (default_accessibility == eAccessNone)
3170 default_accessibility = eAccessPrivate;
3171 // TODO: implement DW_TAG_inheritance type parsing
3172 DWARFAttributes attributes;
3173 const size_t num_attributes = die.GetAttributes(attributes);
3174 if (num_attributes > 0) {
3176 DWARFExpression location(die.GetCU());
3177 DWARFFormValue encoding_form;
3178 AccessType accessibility = default_accessibility;
3179 bool is_virtual = false;
3180 bool is_base_of_class = true;
3181 off_t member_byte_offset = 0;
3183 for (i = 0; i < num_attributes; ++i) {
3184 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3185 DWARFFormValue form_value;
3186 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3188 case DW_AT_decl_file:
3189 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3190 form_value.Unsigned()));
3192 case DW_AT_decl_line:
3193 decl.SetLine(form_value.Unsigned());
3195 case DW_AT_decl_column:
3196 decl.SetColumn(form_value.Unsigned());
3199 encoding_form = form_value;
3201 case DW_AT_data_member_location:
3202 if (form_value.BlockData()) {
3203 Value initialValue(0);
3204 Value memberOffset(0);
3205 const DWARFDataExtractor &debug_info_data =
3206 die.GetDWARF()->get_debug_info_data();
3207 uint32_t block_length = form_value.Unsigned();
3208 uint32_t block_offset =
3209 form_value.BlockData() - debug_info_data.GetDataStart();
3210 if (DWARFExpression::Evaluate(
3211 nullptr, nullptr, nullptr, nullptr, module_sp,
3212 debug_info_data, die.GetCU(), block_offset,
3213 block_length, eRegisterKindDWARF, &initialValue,
3214 nullptr, memberOffset, nullptr)) {
3215 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
3218 // With DWARF 3 and later, if the value is an integer constant,
3219 // this form value is the offset in bytes from the beginning
3220 // of the containing entity.
3221 member_byte_offset = form_value.Unsigned();
3225 case DW_AT_accessibility:
3226 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
3229 case DW_AT_virtuality:
3230 is_virtual = form_value.Boolean();
3242 Type *base_class_type = die.ResolveTypeUID(DIERef(encoding_form));
3243 if (base_class_type == NULL) {
3244 module_sp->ReportError("0x%8.8x: DW_TAG_inheritance failed to "
3245 "resolve the base class at 0x%8.8" PRIx64
3246 " from enclosing type 0x%8.8x. \nPlease file "
3247 "a bug and attach the file at the start of "
3248 "this error message",
3249 die.GetOffset(), encoding_form.Reference(),
3250 parent_die.GetOffset());
3254 CompilerType base_class_clang_type =
3255 base_class_type->GetFullCompilerType();
3256 assert(base_class_clang_type);
3257 if (class_language == eLanguageTypeObjC) {
3258 ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
3260 base_classes.push_back(ast->CreateBaseClassSpecifier(
3261 base_class_clang_type.GetOpaqueQualType(), accessibility,
3262 is_virtual, is_base_of_class));
3265 // Do not specify any offset for virtual inheritance. The DWARF
3266 // produced by clang doesn't
3267 // give us a constant offset, but gives us a DWARF expressions that
3268 // requires an actual object
3269 // in memory. the DW_AT_data_member_location for a virtual base
3270 // class looks like:
3271 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref,
3272 // DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref,
3274 // Given this, there is really no valid response we can give to
3275 // clang for virtual base
3276 // class offsets, and this should eventually be removed from
3277 // LayoutRecordType() in the external
3278 // AST source in clang.
3280 layout_info.base_offsets.insert(std::make_pair(
3281 ast->GetAsCXXRecordDecl(
3282 base_class_clang_type.GetOpaqueQualType()),
3283 clang::CharUnits::fromQuantity(member_byte_offset)));
3297 size_t DWARFASTParserClang::ParseChildParameters(
3298 const SymbolContext &sc, clang::DeclContext *containing_decl_ctx,
3299 const DWARFDIE &parent_die, bool skip_artificial, bool &is_static,
3300 bool &is_variadic, bool &has_template_params,
3301 std::vector<CompilerType> &function_param_types,
3302 std::vector<clang::ParmVarDecl *> &function_param_decls,
3303 unsigned &type_quals) {
3308 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
3309 die = die.GetSibling()) {
3310 const dw_tag_t tag = die.Tag();
3312 case DW_TAG_formal_parameter: {
3313 DWARFAttributes attributes;
3314 const size_t num_attributes = die.GetAttributes(attributes);
3315 if (num_attributes > 0) {
3316 const char *name = NULL;
3318 DWARFFormValue param_type_die_form;
3319 bool is_artificial = false;
3320 // one of None, Auto, Register, Extern, Static, PrivateExtern
3322 clang::StorageClass storage = clang::SC_None;
3324 for (i = 0; i < num_attributes; ++i) {
3325 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3326 DWARFFormValue form_value;
3327 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3329 case DW_AT_decl_file:
3330 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
3331 form_value.Unsigned()));
3333 case DW_AT_decl_line:
3334 decl.SetLine(form_value.Unsigned());
3336 case DW_AT_decl_column:
3337 decl.SetColumn(form_value.Unsigned());
3340 name = form_value.AsCString();
3343 param_type_die_form = form_value;
3345 case DW_AT_artificial:
3346 is_artificial = form_value.Boolean();
3348 case DW_AT_location:
3349 // if (form_value.BlockData())
3351 // const DWARFDataExtractor&
3352 // debug_info_data = debug_info();
3353 // uint32_t block_length =
3354 // form_value.Unsigned();
3355 // DWARFDataExtractor
3356 // location(debug_info_data,
3357 // form_value.BlockData() -
3358 // debug_info_data.GetDataStart(),
3365 case DW_AT_const_value:
3366 case DW_AT_default_value:
3367 case DW_AT_description:
3368 case DW_AT_endianity:
3369 case DW_AT_is_optional:
3371 case DW_AT_variable_parameter:
3373 case DW_AT_abstract_origin:
3381 if (skip_artificial) {
3382 if (is_artificial) {
3383 // In order to determine if a C++ member function is
3384 // "const" we have to look at the const-ness of "this"...
3387 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind())) {
3388 // Often times compilers omit the "this" name for the
3389 // specification DIEs, so we can't rely upon the name
3390 // being in the formal parameter DIE...
3391 if (name == NULL || ::strcmp(name, "this") == 0) {
3393 die.ResolveTypeUID(DIERef(param_type_die_form));
3395 uint32_t encoding_mask = this_type->GetEncodingMask();
3396 if (encoding_mask & Type::eEncodingIsPointerUID) {
3399 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3400 type_quals |= clang::Qualifiers::Const;
3401 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3402 type_quals |= clang::Qualifiers::Volatile;
3411 // HACK: Objective C formal parameters "self" and "_cmd"
3412 // are not marked as artificial in the DWARF...
3413 CompileUnit *comp_unit = die.GetLLDBCompileUnit();
3415 switch (comp_unit->GetLanguage()) {
3416 case eLanguageTypeObjC:
3417 case eLanguageTypeObjC_plus_plus:
3418 if (name && name[0] &&
3419 (strcmp(name, "self") == 0 || strcmp(name, "_cmd") == 0))
3430 Type *type = die.ResolveTypeUID(DIERef(param_type_die_form));
3432 function_param_types.push_back(type->GetForwardCompilerType());
3434 clang::ParmVarDecl *param_var_decl =
3435 m_ast.CreateParameterDeclaration(
3436 name, type->GetForwardCompilerType(), storage);
3437 assert(param_var_decl);
3438 function_param_decls.push_back(param_var_decl);
3440 m_ast.SetMetadataAsUserID(param_var_decl, die.GetID());
3447 case DW_TAG_unspecified_parameters:
3451 case DW_TAG_template_type_parameter:
3452 case DW_TAG_template_value_parameter:
3453 // The one caller of this was never using the template_param_infos,
3454 // and the local variable was taking up a large amount of stack space
3455 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need
3456 // the template params back, we can add them back.
3457 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
3458 has_template_params = true;
3468 void DWARFASTParserClang::ParseChildArrayInfo(
3469 const SymbolContext &sc, const DWARFDIE &parent_die, int64_t &first_index,
3470 std::vector<uint64_t> &element_orders, uint32_t &byte_stride,
3471 uint32_t &bit_stride) {
3475 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
3476 die = die.GetSibling()) {
3477 const dw_tag_t tag = die.Tag();
3479 case DW_TAG_subrange_type: {
3480 DWARFAttributes attributes;
3481 const size_t num_child_attributes = die.GetAttributes(attributes);
3482 if (num_child_attributes > 0) {
3483 uint64_t num_elements = 0;
3484 uint64_t lower_bound = 0;
3485 uint64_t upper_bound = 0;
3486 bool upper_bound_valid = false;
3488 for (i = 0; i < num_child_attributes; ++i) {
3489 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3490 DWARFFormValue form_value;
3491 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3497 num_elements = form_value.Unsigned();
3500 case DW_AT_bit_stride:
3501 bit_stride = form_value.Unsigned();
3504 case DW_AT_byte_stride:
3505 byte_stride = form_value.Unsigned();
3508 case DW_AT_lower_bound:
3509 lower_bound = form_value.Unsigned();
3512 case DW_AT_upper_bound:
3513 upper_bound_valid = true;
3514 upper_bound = form_value.Unsigned();
3518 case DW_AT_abstract_origin:
3519 case DW_AT_accessibility:
3520 case DW_AT_allocated:
3521 case DW_AT_associated:
3522 case DW_AT_data_location:
3523 case DW_AT_declaration:
3524 case DW_AT_description:
3526 case DW_AT_threads_scaled:
3528 case DW_AT_visibility:
3534 if (num_elements == 0) {
3535 if (upper_bound_valid && upper_bound >= lower_bound)
3536 num_elements = upper_bound - lower_bound + 1;
3539 element_orders.push_back(num_elements);
3546 Type *DWARFASTParserClang::GetTypeForDIE(const DWARFDIE &die) {
3548 SymbolFileDWARF *dwarf = die.GetDWARF();
3549 DWARFAttributes attributes;
3550 const size_t num_attributes = die.GetAttributes(attributes);
3551 if (num_attributes > 0) {
3552 DWARFFormValue type_die_form;
3553 for (size_t i = 0; i < num_attributes; ++i) {
3554 dw_attr_t attr = attributes.AttributeAtIndex(i);
3555 DWARFFormValue form_value;
3557 if (attr == DW_AT_type &&
3558 attributes.ExtractFormValueAtIndex(i, form_value))
3559 return dwarf->ResolveTypeUID(dwarf->GetDIE(DIERef(form_value)), true);
3567 clang::Decl *DWARFASTParserClang::GetClangDeclForDIE(const DWARFDIE &die) {
3571 switch (die.Tag()) {
3572 case DW_TAG_variable:
3573 case DW_TAG_constant:
3574 case DW_TAG_formal_parameter:
3575 case DW_TAG_imported_declaration:
3576 case DW_TAG_imported_module:
3582 DIEToDeclMap::iterator cache_pos = m_die_to_decl.find(die.GetDIE());
3583 if (cache_pos != m_die_to_decl.end())
3584 return cache_pos->second;
3586 if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification)) {
3587 clang::Decl *decl = GetClangDeclForDIE(spec_die);
3588 m_die_to_decl[die.GetDIE()] = decl;
3589 m_decl_to_die[decl].insert(die.GetDIE());
3593 if (DWARFDIE abstract_origin_die =
3594 die.GetReferencedDIE(DW_AT_abstract_origin)) {
3595 clang::Decl *decl = GetClangDeclForDIE(abstract_origin_die);
3596 m_die_to_decl[die.GetDIE()] = decl;
3597 m_decl_to_die[decl].insert(die.GetDIE());
3601 clang::Decl *decl = nullptr;
3602 switch (die.Tag()) {
3603 case DW_TAG_variable:
3604 case DW_TAG_constant:
3605 case DW_TAG_formal_parameter: {
3606 SymbolFileDWARF *dwarf = die.GetDWARF();
3607 Type *type = GetTypeForDIE(die);
3608 if (dwarf && type) {
3609 const char *name = die.GetName();
3610 clang::DeclContext *decl_context =
3611 ClangASTContext::DeclContextGetAsDeclContext(
3612 dwarf->GetDeclContextContainingUID(die.GetID()));
3613 decl = m_ast.CreateVariableDeclaration(
3615 ClangUtil::GetQualType(type->GetForwardCompilerType()));
3619 case DW_TAG_imported_declaration: {
3620 SymbolFileDWARF *dwarf = die.GetDWARF();
3621 DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3623 CompilerDecl imported_decl = imported_uid.GetDecl();
3624 if (imported_decl) {
3625 clang::DeclContext *decl_context =
3626 ClangASTContext::DeclContextGetAsDeclContext(
3627 dwarf->GetDeclContextContainingUID(die.GetID()));
3628 if (clang::NamedDecl *clang_imported_decl =
3629 llvm::dyn_cast<clang::NamedDecl>(
3630 (clang::Decl *)imported_decl.GetOpaqueDecl()))
3632 m_ast.CreateUsingDeclaration(decl_context, clang_imported_decl);
3637 case DW_TAG_imported_module: {
3638 SymbolFileDWARF *dwarf = die.GetDWARF();
3639 DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3642 CompilerDeclContext imported_decl_ctx = imported_uid.GetDeclContext();
3643 if (imported_decl_ctx) {
3644 clang::DeclContext *decl_context =
3645 ClangASTContext::DeclContextGetAsDeclContext(
3646 dwarf->GetDeclContextContainingUID(die.GetID()));
3647 if (clang::NamespaceDecl *ns_decl =
3648 ClangASTContext::DeclContextGetAsNamespaceDecl(
3650 decl = m_ast.CreateUsingDirectiveDeclaration(decl_context, ns_decl);
3659 m_die_to_decl[die.GetDIE()] = decl;
3660 m_decl_to_die[decl].insert(die.GetDIE());
3665 clang::DeclContext *
3666 DWARFASTParserClang::GetClangDeclContextForDIE(const DWARFDIE &die) {
3668 clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE(die);
3672 bool try_parsing_type = true;
3673 switch (die.Tag()) {
3674 case DW_TAG_compile_unit:
3675 decl_ctx = m_ast.GetTranslationUnitDecl();
3676 try_parsing_type = false;
3679 case DW_TAG_namespace:
3680 decl_ctx = ResolveNamespaceDIE(die);
3681 try_parsing_type = false;
3684 case DW_TAG_lexical_block:
3685 decl_ctx = (clang::DeclContext *)ResolveBlockDIE(die);
3686 try_parsing_type = false;
3693 if (decl_ctx == nullptr && try_parsing_type) {
3694 Type *type = die.GetDWARF()->ResolveType(die);
3696 decl_ctx = GetCachedClangDeclContextForDIE(die);
3700 LinkDeclContextToDIE(decl_ctx, die);
3707 clang::BlockDecl *DWARFASTParserClang::ResolveBlockDIE(const DWARFDIE &die) {
3708 if (die && die.Tag() == DW_TAG_lexical_block) {
3709 clang::BlockDecl *decl =
3710 llvm::cast_or_null<clang::BlockDecl>(m_die_to_decl_ctx[die.GetDIE()]);
3713 DWARFDIE decl_context_die;
3714 clang::DeclContext *decl_context =
3715 GetClangDeclContextContainingDIE(die, &decl_context_die);
3716 decl = m_ast.CreateBlockDeclaration(decl_context);
3719 LinkDeclContextToDIE((clang::DeclContext *)decl, die);
3727 clang::NamespaceDecl *
3728 DWARFASTParserClang::ResolveNamespaceDIE(const DWARFDIE &die) {
3729 if (die && die.Tag() == DW_TAG_namespace) {
3730 // See if we already parsed this namespace DIE and associated it with a
3731 // uniqued namespace declaration
3732 clang::NamespaceDecl *namespace_decl =
3733 static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die.GetDIE()]);
3735 return namespace_decl;
3737 const char *namespace_name = die.GetName();
3738 clang::DeclContext *containing_decl_ctx =
3739 GetClangDeclContextContainingDIE(die, nullptr);
3740 namespace_decl = m_ast.GetUniqueNamespaceDeclaration(namespace_name,
3741 containing_decl_ctx);
3743 nullptr; // (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3745 SymbolFileDWARF *dwarf = die.GetDWARF();
3746 if (namespace_name) {
3747 dwarf->GetObjectFile()->GetModule()->LogMessage(
3748 log, "ASTContext => %p: 0x%8.8" PRIx64
3749 ": DW_TAG_namespace with DW_AT_name(\"%s\") => "
3750 "clang::NamespaceDecl *%p (original = %p)",
3751 static_cast<void *>(m_ast.getASTContext()), die.GetID(),
3752 namespace_name, static_cast<void *>(namespace_decl),
3753 static_cast<void *>(namespace_decl->getOriginalNamespace()));
3755 dwarf->GetObjectFile()->GetModule()->LogMessage(
3756 log, "ASTContext => %p: 0x%8.8" PRIx64
3757 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p "
3759 static_cast<void *>(m_ast.getASTContext()), die.GetID(),
3760 static_cast<void *>(namespace_decl),
3761 static_cast<void *>(namespace_decl->getOriginalNamespace()));
3766 LinkDeclContextToDIE((clang::DeclContext *)namespace_decl, die);
3767 return namespace_decl;
3773 clang::DeclContext *DWARFASTParserClang::GetClangDeclContextContainingDIE(
3774 const DWARFDIE &die, DWARFDIE *decl_ctx_die_copy) {
3775 SymbolFileDWARF *dwarf = die.GetDWARF();
3777 DWARFDIE decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE(die);
3779 if (decl_ctx_die_copy)
3780 *decl_ctx_die_copy = decl_ctx_die;
3783 clang::DeclContext *clang_decl_ctx =
3784 GetClangDeclContextForDIE(decl_ctx_die);
3786 return clang_decl_ctx;
3788 return m_ast.GetTranslationUnitDecl();
3791 clang::DeclContext *
3792 DWARFASTParserClang::GetCachedClangDeclContextForDIE(const DWARFDIE &die) {
3794 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die.GetDIE());
3795 if (pos != m_die_to_decl_ctx.end())
3801 void DWARFASTParserClang::LinkDeclContextToDIE(clang::DeclContext *decl_ctx,
3802 const DWARFDIE &die) {
3803 m_die_to_decl_ctx[die.GetDIE()] = decl_ctx;
3804 // There can be many DIEs for a single decl context
3805 // m_decl_ctx_to_die[decl_ctx].insert(die.GetDIE());
3806 m_decl_ctx_to_die.insert(std::make_pair(decl_ctx, die));
3809 bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
3810 const DWARFDIE &src_class_die, const DWARFDIE &dst_class_die,
3811 lldb_private::Type *class_type, DWARFDIECollection &failures) {
3812 if (!class_type || !src_class_die || !dst_class_die)
3814 if (src_class_die.Tag() != dst_class_die.Tag())
3817 // We need to complete the class type so we can get all of the method types
3818 // parsed so we can then unique those types to their equivalent counterparts
3819 // in "dst_cu" and "dst_class_die"
3820 class_type->GetFullCompilerType();
3824 UniqueCStringMap<DWARFDIE> src_name_to_die;
3825 UniqueCStringMap<DWARFDIE> dst_name_to_die;
3826 UniqueCStringMap<DWARFDIE> src_name_to_die_artificial;
3827 UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial;
3828 for (src_die = src_class_die.GetFirstChild(); src_die.IsValid();
3829 src_die = src_die.GetSibling()) {
3830 if (src_die.Tag() == DW_TAG_subprogram) {
3831 // Make sure this is a declaration and not a concrete instance by looking
3832 // for DW_AT_declaration set to 1. Sometimes concrete function instances
3833 // are placed inside the class definitions and shouldn't be included in
3834 // the list of things are are tracking here.
3835 if (src_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3836 const char *src_name = src_die.GetMangledName();
3838 ConstString src_const_name(src_name);
3839 if (src_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3840 src_name_to_die_artificial.Append(src_const_name.GetStringRef(),
3843 src_name_to_die.Append(src_const_name.GetStringRef(), src_die);
3848 for (dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid();
3849 dst_die = dst_die.GetSibling()) {
3850 if (dst_die.Tag() == DW_TAG_subprogram) {
3851 // Make sure this is a declaration and not a concrete instance by looking
3852 // for DW_AT_declaration set to 1. Sometimes concrete function instances
3853 // are placed inside the class definitions and shouldn't be included in
3854 // the list of things are are tracking here.
3855 if (dst_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3856 const char *dst_name = dst_die.GetMangledName();
3858 ConstString dst_const_name(dst_name);
3859 if (dst_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3860 dst_name_to_die_artificial.Append(dst_const_name.GetStringRef(),
3863 dst_name_to_die.Append(dst_const_name.GetStringRef(), dst_die);
3868 const uint32_t src_size = src_name_to_die.GetSize();
3869 const uint32_t dst_size = dst_name_to_die.GetSize();
3870 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
3871 // DWARF_LOG_TYPE_COMPLETION));
3873 // Is everything kosher so we can go through the members at top speed?
3874 bool fast_path = true;
3876 if (src_size != dst_size) {
3877 if (src_size != 0 && dst_size != 0) {
3879 log->Printf("warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, "
3880 "but they didn't have the same size (src=%d, dst=%d)",
3881 src_class_die.GetOffset(), dst_class_die.GetOffset(),
3882 src_size, dst_size);
3891 for (idx = 0; idx < src_size; ++idx) {
3892 src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3893 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3895 if (src_die.Tag() != dst_die.Tag()) {
3897 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, "
3898 "but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
3899 src_class_die.GetOffset(), dst_class_die.GetOffset(),
3900 src_die.GetOffset(), src_die.GetTagAsCString(),
3901 dst_die.GetOffset(), dst_die.GetTagAsCString());
3905 const char *src_name = src_die.GetMangledName();
3906 const char *dst_name = dst_die.GetMangledName();
3908 // Make sure the names match
3909 if (src_name == dst_name || (strcmp(src_name, dst_name) == 0))
3913 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, "
3914 "but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
3915 src_class_die.GetOffset(), dst_class_die.GetOffset(),
3916 src_die.GetOffset(), src_name, dst_die.GetOffset(),
3923 DWARFASTParserClang *src_dwarf_ast_parser =
3924 (DWARFASTParserClang *)src_die.GetDWARFParser();
3925 DWARFASTParserClang *dst_dwarf_ast_parser =
3926 (DWARFASTParserClang *)dst_die.GetDWARFParser();
3928 // Now do the work of linking the DeclContexts and Types.
3930 // We can do this quickly. Just run across the tables index-for-index since
3931 // we know each node has matching names and tags.
3932 for (idx = 0; idx < src_size; ++idx) {
3933 src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3934 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3936 clang::DeclContext *src_decl_ctx =
3937 src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3940 log->Printf("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3941 static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3942 dst_die.GetOffset());
3943 dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3946 log->Printf("warning: tried to unique decl context from 0x%8.8x for "
3947 "0x%8.8x, but none was found",
3948 src_die.GetOffset(), dst_die.GetOffset());
3951 Type *src_child_type =
3952 dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3953 if (src_child_type) {
3956 "uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3957 static_cast<void *>(src_child_type), src_child_type->GetID(),
3958 src_die.GetOffset(), dst_die.GetOffset());
3959 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3962 log->Printf("warning: tried to unique lldb_private::Type from "
3963 "0x%8.8x for 0x%8.8x, but none was found",
3964 src_die.GetOffset(), dst_die.GetOffset());
3968 // We must do this slowly. For each member of the destination, look
3969 // up a member in the source with the same name, check its tag, and
3970 // unique them if everything matches up. Report failures.
3972 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty()) {
3973 src_name_to_die.Sort();
3975 for (idx = 0; idx < dst_size; ++idx) {
3976 llvm::StringRef dst_name = dst_name_to_die.GetCStringAtIndex(idx);
3977 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3978 src_die = src_name_to_die.Find(dst_name, DWARFDIE());
3980 if (src_die && (src_die.Tag() == dst_die.Tag())) {
3981 clang::DeclContext *src_decl_ctx =
3982 src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3985 log->Printf("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3986 static_cast<void *>(src_decl_ctx),
3987 src_die.GetOffset(), dst_die.GetOffset());
3988 dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3991 log->Printf("warning: tried to unique decl context from 0x%8.8x "
3992 "for 0x%8.8x, but none was found",
3993 src_die.GetOffset(), dst_die.GetOffset());
3996 Type *src_child_type =
3997 dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3998 if (src_child_type) {
4000 log->Printf("uniquing type %p (uid=0x%" PRIx64
4001 ") from 0x%8.8x for 0x%8.8x",
4002 static_cast<void *>(src_child_type),
4003 src_child_type->GetID(), src_die.GetOffset(),
4004 dst_die.GetOffset());
4005 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] =
4009 log->Printf("warning: tried to unique lldb_private::Type from "
4010 "0x%8.8x for 0x%8.8x, but none was found",
4011 src_die.GetOffset(), dst_die.GetOffset());
4015 log->Printf("warning: couldn't find a match for 0x%8.8x",
4016 dst_die.GetOffset());
4018 failures.Append(dst_die);
4024 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize();
4025 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize();
4027 if (src_size_artificial && dst_size_artificial) {
4028 dst_name_to_die_artificial.Sort();
4030 for (idx = 0; idx < src_size_artificial; ++idx) {
4031 llvm::StringRef src_name_artificial =
4032 src_name_to_die_artificial.GetCStringAtIndex(idx);
4033 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
4035 dst_name_to_die_artificial.Find(src_name_artificial, DWARFDIE());
4038 // Both classes have the artificial types, link them
4039 clang::DeclContext *src_decl_ctx =
4040 src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
4043 log->Printf("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
4044 static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
4045 dst_die.GetOffset());
4046 dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
4049 log->Printf("warning: tried to unique decl context from 0x%8.8x "
4050 "for 0x%8.8x, but none was found",
4051 src_die.GetOffset(), dst_die.GetOffset());
4054 Type *src_child_type =
4055 dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
4056 if (src_child_type) {
4059 "uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
4060 static_cast<void *>(src_child_type), src_child_type->GetID(),
4061 src_die.GetOffset(), dst_die.GetOffset());
4062 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
4065 log->Printf("warning: tried to unique lldb_private::Type from "
4066 "0x%8.8x for 0x%8.8x, but none was found",
4067 src_die.GetOffset(), dst_die.GetOffset());
4073 if (dst_size_artificial) {
4074 for (idx = 0; idx < dst_size_artificial; ++idx) {
4075 llvm::StringRef dst_name_artificial =
4076 dst_name_to_die_artificial.GetCStringAtIndex(idx);
4077 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
4079 log->Printf("warning: need to create artificial method for 0x%8.8x for "
4081 dst_die.GetOffset(), dst_name_artificial.str().c_str());
4083 failures.Append(dst_die);
4087 return (failures.Size() != 0);