1 //===-- DWARFASTParserGo.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 //===----------------------------------------------------------------------===//
10 #include "DWARFASTParserGo.h"
12 #include "DWARFASTParserGo.h"
13 #include "DWARFCompileUnit.h"
14 #include "DWARFDebugInfo.h"
15 #include "DWARFDeclContext.h"
16 #include "DWARFDefines.h"
18 #include "DWARFDIECollection.h"
19 #include "SymbolFileDWARF.h"
20 #include "SymbolFileDWARFDebugMap.h"
21 #include "UniqueDWARFASTType.h"
23 #include "clang/Basic/Specifiers.h"
25 #include "lldb/Core/Module.h"
26 #include "lldb/Core/Value.h"
27 #include "lldb/Symbol/CompileUnit.h"
28 #include "lldb/Symbol/Function.h"
29 #include "lldb/Symbol/ObjectFile.h"
30 #include "lldb/Symbol/TypeList.h"
32 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
34 #ifdef ENABLE_DEBUG_PRINTF
36 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
38 #define DEBUG_PRINTF(fmt, ...)
41 #define DW_AT_go_kind 0x2900
42 #define DW_AT_go_key 0x2901
43 #define DW_AT_go_elem 0x2902
46 using namespace lldb_private;
47 DWARFASTParserGo::DWARFASTParserGo(GoASTContext &ast)
52 DWARFASTParserGo::~DWARFASTParserGo()
57 DWARFASTParserGo::ParseTypeFromDWARF(const lldb_private::SymbolContext &sc, const DWARFDIE &die, lldb_private::Log *log,
58 bool *type_is_new_ptr)
63 *type_is_new_ptr = false;
67 SymbolFileDWARF *dwarf = die.GetDWARF();
70 dwarf->GetObjectFile()->GetModule()->LogMessage(
71 log, "DWARFASTParserGo::ParseTypeFromDWARF (die = 0x%8.8x) %s name = '%s')", die.GetOffset(),
72 DW_TAG_value_to_name(die.Tag()), die.GetName());
75 Type *type_ptr = dwarf->m_die_to_type.lookup(die.GetDIE());
76 TypeList *type_list = dwarf->GetTypeList();
80 *type_is_new_ptr = true;
82 const dw_tag_t tag = die.Tag();
84 bool is_forward_declaration = false;
85 DWARFAttributes attributes;
86 const char *type_name_cstr = NULL;
87 ConstString type_name_const_str;
88 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
89 uint64_t byte_size = 0;
93 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
94 CompilerType compiler_type;
95 DWARFFormValue form_value;
101 case DW_TAG_base_type:
102 case DW_TAG_pointer_type:
104 case DW_TAG_unspecified_type:
106 // Set a bit that lets us know that we are currently parsing this
107 dwarf->m_die_to_type[die.GetDIE()] = DIE_IS_BEING_PARSED;
109 const size_t num_attributes = die.GetAttributes(attributes);
110 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
112 if (num_attributes > 0)
115 for (i = 0; i < num_attributes; ++i)
117 attr = attributes.AttributeAtIndex(i);
118 if (attributes.ExtractFormValueAtIndex(i, form_value))
123 type_name_cstr = form_value.AsCString();
125 type_name_const_str.SetCString(type_name_cstr);
127 case DW_AT_byte_size:
128 byte_size = form_value.Unsigned();
131 // = form_value.Unsigned();
134 encoding_uid = form_value.Reference();
137 go_kind = form_value.Unsigned();
140 // Do we care about DW_AT_go_key or DW_AT_go_elem?
147 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", die.GetID(),
148 DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
155 case DW_TAG_unspecified_type:
156 resolve_state = Type::eResolveStateFull;
157 compiler_type = m_ast.CreateVoidType(type_name_const_str);
160 case DW_TAG_base_type:
161 resolve_state = Type::eResolveStateFull;
162 compiler_type = m_ast.CreateBaseType(go_kind, type_name_const_str, byte_size);
165 case DW_TAG_pointer_type:
166 encoding_data_type = Type::eEncodingIsPointerUID;
169 encoding_data_type = Type::eEncodingIsTypedefUID;
171 Type *type = dwarf->ResolveTypeUID(encoding_uid);
174 if (go_kind == 0 && type->GetName() == type_name_const_str)
176 // Go emits extra typedefs as a forward declaration. Ignore these.
177 dwarf->m_die_to_type[die.GetDIE()] = type;
178 return type->shared_from_this();
180 impl = type->GetForwardCompilerType();
181 compiler_type = m_ast.CreateTypedefType (go_kind, type_name_const_str, impl);
186 type_sp.reset(new Type(die.GetID(), dwarf, type_name_const_str, byte_size,
187 NULL, encoding_uid, encoding_data_type, &decl, compiler_type, resolve_state));
189 dwarf->m_die_to_type[die.GetDIE()] = type_sp.get();
193 case DW_TAG_structure_type:
195 // Set a bit that lets us know that we are currently parsing this
196 dwarf->m_die_to_type[die.GetDIE()] = DIE_IS_BEING_PARSED;
197 bool byte_size_valid = false;
199 const size_t num_attributes = die.GetAttributes(attributes);
200 if (num_attributes > 0)
203 for (i = 0; i < num_attributes; ++i)
205 attr = attributes.AttributeAtIndex(i);
206 if (attributes.ExtractFormValueAtIndex(i, form_value))
211 type_name_cstr = form_value.AsCString();
212 type_name_const_str.SetCString(type_name_cstr);
215 case DW_AT_byte_size:
216 byte_size = form_value.Unsigned();
217 byte_size_valid = true;
221 go_kind = form_value.Unsigned();
224 // TODO: Should we use SLICETYPE's DW_AT_go_elem?
232 // TODO(ribrdb): Do we need this?
234 // UniqueDWARFASTType is large, so don't create a local variables on the
235 // stack, put it on the heap. This function is often called recursively
236 // and clang isn't good and sharing the stack space for variables in different blocks.
237 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType());
239 // Only try and unique the type if it has a name.
240 if (type_name_const_str &&
241 dwarf->GetUniqueDWARFASTTypeMap().Find(type_name_const_str, die, decl,
242 byte_size_valid ? byte_size : -1, *unique_ast_entry_ap))
244 // We have already parsed this type or from another
245 // compile unit. GCC loves to use the "one definition
246 // rule" which can result in multiple definitions
247 // of the same class over and over in each compile
249 type_sp = unique_ast_entry_ap->m_type_sp;
252 dwarf->m_die_to_type[die.GetDIE()] = type_sp.get();
257 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
258 DW_TAG_value_to_name(tag), type_name_cstr);
260 bool compiler_type_was_created = false;
261 compiler_type.SetCompilerType(&m_ast, dwarf->m_forward_decl_die_to_clang_type.lookup(die.GetDIE()));
264 compiler_type_was_created = true;
265 compiler_type = m_ast.CreateStructType(go_kind, type_name_const_str, byte_size);
268 type_sp.reset(new Type(die.GetID(), dwarf, type_name_const_str, byte_size,
269 NULL, LLDB_INVALID_UID, Type::eEncodingIsUID, &decl, compiler_type,
270 Type::eResolveStateForward));
272 // Add our type to the unique type map so we don't
273 // end up creating many copies of the same type over
274 // and over in the ASTContext for our module
275 unique_ast_entry_ap->m_type_sp = type_sp;
276 unique_ast_entry_ap->m_die = die;
277 unique_ast_entry_ap->m_declaration = decl;
278 unique_ast_entry_ap->m_byte_size = byte_size;
279 dwarf->GetUniqueDWARFASTTypeMap().Insert(type_name_const_str, *unique_ast_entry_ap);
281 if (!is_forward_declaration)
283 // Always start the definition for a class type so that
284 // if the class has child classes or types that require
285 // the class to be created for use as their decl contexts
286 // the class will be ready to accept these child definitions.
287 if (die.HasChildren() == false)
289 // No children for this struct/union/class, lets finish it
290 m_ast.CompleteStructType(compiler_type);
292 else if (compiler_type_was_created)
294 // Leave this as a forward declaration until we need
295 // to know the details of the type. lldb_private::Type
296 // will automatically call the SymbolFile virtual function
297 // "SymbolFileDWARF::CompleteType(Type *)"
298 // When the definition needs to be defined.
299 dwarf->m_forward_decl_die_to_clang_type[die.GetDIE()] = compiler_type.GetOpaqueQualType();
300 dwarf->m_forward_decl_clang_type_to_die[compiler_type.GetOpaqueQualType()] = die.GetDIERef();
301 // SetHasExternalStorage (compiler_type.GetOpaqueQualType(), true);
307 case DW_TAG_subprogram:
308 case DW_TAG_subroutine_type:
310 // Set a bit that lets us know that we are currently parsing this
311 dwarf->m_die_to_type[die.GetDIE()] = DIE_IS_BEING_PARSED;
313 bool is_variadic = false;
314 clang::StorageClass storage = clang::SC_None; //, Extern, Static, PrivateExtern
316 const size_t num_attributes = die.GetAttributes(attributes);
317 if (num_attributes > 0)
320 for (i = 0; i < num_attributes; ++i)
322 attr = attributes.AttributeAtIndex(i);
323 if (attributes.ExtractFormValueAtIndex(i, form_value))
328 type_name_cstr = form_value.AsCString();
329 type_name_const_str.SetCString(type_name_cstr);
333 if (form_value.Unsigned())
335 if (storage == clang::SC_None)
336 storage = clang::SC_Extern;
338 storage = clang::SC_PrivateExtern;
350 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
351 DW_TAG_value_to_name(tag), type_name_cstr);
353 std::vector<CompilerType> function_param_types;
355 // Parse the function children for the parameters
357 if (die.HasChildren())
359 ParseChildParameters(sc, die, is_variadic, function_param_types);
362 // compiler_type will get the function prototype clang type after this call
363 compiler_type = m_ast.CreateFunctionType(type_name_const_str, function_param_types.data(),
364 function_param_types.size(), is_variadic);
366 type_sp.reset(new Type(die.GetID(), dwarf, type_name_const_str, 0, NULL,
367 LLDB_INVALID_UID, Type::eEncodingIsUID, &decl, compiler_type,
368 Type::eResolveStateFull));
369 assert(type_sp.get());
373 case DW_TAG_array_type:
375 // Set a bit that lets us know that we are currently parsing this
376 dwarf->m_die_to_type[die.GetDIE()] = DIE_IS_BEING_PARSED;
378 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
379 int64_t first_index = 0;
380 uint32_t byte_stride = 0;
381 uint32_t bit_stride = 0;
382 const size_t num_attributes = die.GetAttributes(attributes);
384 if (num_attributes > 0)
387 for (i = 0; i < num_attributes; ++i)
389 attr = attributes.AttributeAtIndex(i);
390 if (attributes.ExtractFormValueAtIndex(i, form_value))
395 type_name_cstr = form_value.AsCString();
396 type_name_const_str.SetCString(type_name_cstr);
400 type_die_offset = form_value.Reference();
402 case DW_AT_byte_size:
403 break; // byte_size = form_value.Unsigned(); break;
405 go_kind = form_value.Unsigned();
413 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
414 DW_TAG_value_to_name(tag), type_name_cstr);
416 Type *element_type = dwarf->ResolveTypeUID(type_die_offset);
420 std::vector<uint64_t> element_orders;
421 ParseChildArrayInfo(sc, die, first_index, element_orders, byte_stride, bit_stride);
422 if (byte_stride == 0)
423 byte_stride = element_type->GetByteSize();
424 CompilerType array_element_type = element_type->GetForwardCompilerType();
425 if (element_orders.size() > 0)
427 if (element_orders.size() > 1)
428 printf("golang: unsupported multi-dimensional array %s\n", type_name_cstr);
430 m_ast.CreateArrayType(type_name_const_str, array_element_type, element_orders[0]);
434 compiler_type = m_ast.CreateArrayType(type_name_const_str, array_element_type, 0);
436 type_sp.reset(new Type(die.GetID(), dwarf, type_name_const_str,
437 byte_stride, NULL, type_die_offset, Type::eEncodingIsUID, &decl,
438 compiler_type, Type::eResolveStateFull));
439 type_sp->SetEncodingType(element_type);
446 dwarf->GetObjectFile()->GetModule()->ReportError("{0x%8.8x}: unhandled type tag 0x%4.4x (%s), "
447 "please file a bug and attach the file at the "
448 "start of this error message",
449 die.GetOffset(), tag, DW_TAG_value_to_name(tag));
455 DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
456 dw_tag_t sc_parent_tag = sc_parent_die.Tag();
458 SymbolContextScope *symbol_context_scope = NULL;
459 if (sc_parent_tag == DW_TAG_compile_unit)
461 symbol_context_scope = sc.comp_unit;
463 else if (sc.function != NULL && sc_parent_die)
465 symbol_context_scope =
466 sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
467 if (symbol_context_scope == NULL)
468 symbol_context_scope = sc.function;
471 if (symbol_context_scope != NULL)
473 type_sp->SetSymbolContextScope(symbol_context_scope);
476 // We are ready to put this type into the uniqued list up at the module level
477 type_list->Insert(type_sp);
479 dwarf->m_die_to_type[die.GetDIE()] = type_sp.get();
482 else if (type_ptr != DIE_IS_BEING_PARSED)
484 type_sp = type_ptr->shared_from_this();
491 DWARFASTParserGo::ParseChildParameters(const SymbolContext &sc,
493 const DWARFDIE &parent_die, bool &is_variadic,
494 std::vector<CompilerType> &function_param_types)
500 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
503 dw_tag_t tag = die.Tag();
506 case DW_TAG_formal_parameter:
508 DWARFAttributes attributes;
509 const size_t num_attributes = die.GetAttributes(attributes);
510 if (num_attributes > 0)
513 DWARFFormValue param_type_die_offset;
516 for (i = 0; i < num_attributes; ++i)
518 const dw_attr_t attr = attributes.AttributeAtIndex(i);
519 DWARFFormValue form_value;
520 if (attributes.ExtractFormValueAtIndex(i, form_value))
525 // = form_value.AsCString();
528 param_type_die_offset = form_value;
531 // if (form_value.BlockData())
533 // const DWARFDataExtractor& debug_info_data =
535 // uint32_t block_length = form_value.Unsigned();
536 // DWARFDataExtractor location(debug_info_data,
537 // form_value.BlockData() - debug_info_data.GetDataStart(),
550 Type *type = parent_die.ResolveTypeUID(DIERef(param_type_die_offset));
553 function_param_types.push_back(type->GetForwardCompilerType());
560 case DW_TAG_unspecified_parameters:
572 DWARFASTParserGo::ParseChildArrayInfo(const SymbolContext &sc, const DWARFDIE &parent_die, int64_t &first_index,
573 std::vector<uint64_t> &element_orders, uint32_t &byte_stride,
574 uint32_t &bit_stride)
579 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
581 const dw_tag_t tag = die.Tag();
584 case DW_TAG_subrange_type:
586 DWARFAttributes attributes;
587 const size_t num_child_attributes = die.GetAttributes(attributes);
588 if (num_child_attributes > 0)
590 uint64_t num_elements = 0;
592 for (i = 0; i < num_child_attributes; ++i)
594 const dw_attr_t attr = attributes.AttributeAtIndex(i);
595 DWARFFormValue form_value;
596 if (attributes.ExtractFormValueAtIndex(i, form_value))
601 num_elements = form_value.Unsigned();
611 element_orders.push_back(num_elements);
620 DWARFASTParserGo::CompleteTypeFromDWARF(const DWARFDIE &die, lldb_private::Type *type, CompilerType &compiler_type)
625 const dw_tag_t tag = die.Tag();
627 SymbolFileDWARF *dwarf = die.GetDWARF();
628 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
630 dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
631 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...", die.GetID(),
632 DW_TAG_value_to_name(tag), type->GetName().AsCString());
633 assert(compiler_type);
634 DWARFAttributes attributes;
638 case DW_TAG_structure_type:
641 if (die.HasChildren())
643 SymbolContext sc(die.GetLLDBCompileUnit());
645 ParseChildMembers(sc, die, compiler_type);
648 m_ast.CompleteStructType(compiler_type);
649 return (bool)compiler_type;
653 assert(false && "not a forward go type decl!");
661 DWARFASTParserGo::ParseChildMembers(const SymbolContext &sc, const DWARFDIE &parent_die, CompilerType &class_compiler_type)
664 uint32_t member_idx = 0;
666 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
667 GoASTContext *ast = llvm::dyn_cast_or_null<GoASTContext>(class_compiler_type.GetTypeSystem());
671 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
673 dw_tag_t tag = die.Tag();
679 DWARFAttributes attributes;
680 const size_t num_attributes = die.GetAttributes(attributes);
681 if (num_attributes > 0)
684 const char *name = NULL;
686 DWARFFormValue encoding_uid;
687 uint32_t member_byte_offset = UINT32_MAX;
689 for (i = 0; i < num_attributes; ++i)
691 const dw_attr_t attr = attributes.AttributeAtIndex(i);
692 DWARFFormValue form_value;
693 if (attributes.ExtractFormValueAtIndex(i, form_value))
698 name = form_value.AsCString();
701 encoding_uid = form_value;
703 case DW_AT_data_member_location:
704 if (form_value.BlockData())
706 Value initialValue(0);
707 Value memberOffset(0);
708 const DWARFDataExtractor &debug_info_data =
709 die.GetDWARF()->get_debug_info_data();
710 uint32_t block_length = form_value.Unsigned();
711 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
712 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
713 NULL, // ClangExpressionVariableList *
714 NULL, // ClangExpressionDeclMap *
715 NULL, // RegisterContext *
716 module_sp, debug_info_data, die.GetCU(),
717 block_offset, block_length, eRegisterKindDWARF,
718 &initialValue, NULL, memberOffset, NULL))
720 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
725 // With DWARF 3 and later, if the value is an integer constant,
726 // this form value is the offset in bytes from the beginning
727 // of the containing entity.
728 member_byte_offset = form_value.Unsigned();
738 Type *member_type = die.ResolveTypeUID(DIERef(encoding_uid));
741 CompilerType member_go_type = member_type->GetFullCompilerType();
742 ConstString name_const_str(name);
743 m_ast.AddFieldToStruct(class_compiler_type, name_const_str, member_go_type, member_byte_offset);
759 DWARFASTParserGo::ParseFunctionFromDWARF(const SymbolContext &sc, const DWARFDIE &die)
761 DWARFRangeList func_ranges;
762 const char *name = NULL;
763 const char *mangled = NULL;
770 DWARFExpression frame_base(die.GetCU());
772 assert(die.Tag() == DW_TAG_subprogram);
774 if (die.Tag() != DW_TAG_subprogram)
777 if (die.GetDIENamesAndRanges(name, mangled, func_ranges, decl_file, decl_line, decl_column, call_file, call_line,
778 call_column, &frame_base))
780 // Union of all ranges in the function DIE (if the function is discontiguous)
781 AddressRange func_range;
782 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase(0);
783 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd(0);
784 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
786 ModuleSP module_sp(die.GetModule());
787 func_range.GetBaseAddress().ResolveAddressUsingFileSections(lowest_func_addr, module_sp->GetSectionList());
788 if (func_range.GetBaseAddress().IsValid())
789 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
792 if (func_range.GetBaseAddress().IsValid())
795 func_name.SetValue(ConstString(name), false);
798 std::unique_ptr<Declaration> decl_ap;
799 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
800 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), decl_line,
803 SymbolFileDWARF *dwarf = die.GetDWARF();
804 // Supply the type _only_ if it has already been parsed
805 Type *func_type = dwarf->m_die_to_type.lookup(die.GetDIE());
807 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
809 if (dwarf->FixupAddress(func_range.GetBaseAddress()))
811 const user_id_t func_user_id = die.GetID();
812 func_sp.reset(new Function(sc.comp_unit,
813 func_user_id, // UserID is the DIE offset
817 func_range)); // first address range
819 if (func_sp.get() != NULL)
821 if (frame_base.IsValid())
822 func_sp->GetFrameBaseExpression() = frame_base;
823 sc.comp_unit->AddFunction(func_sp);
824 return func_sp.get();