blob: 624d45d62620002f8a6be8e2c0669f17b64688fd [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Greg Clayton3c2e3ae2012-02-06 06:42:51 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "clang/Basic/Builtins.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000025#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanancc427fa2011-07-30 02:42:06 +000027#include "llvm/Support/Casting.h"
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Module.h"
30#include "lldb/Core/PluginManager.h"
31#include "lldb/Core/RegularExpression.h"
32#include "lldb/Core/Scalar.h"
33#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000034#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000035#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Core/Timer.h"
37#include "lldb/Core/Value.h"
38
Greg Clayton20568dd2011-10-13 23:13:20 +000039#include "lldb/Host/Host.h"
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000042#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Symbol/CompileUnit.h"
44#include "lldb/Symbol/LineTable.h"
45#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
47#include "lldb/Symbol/VariableList.h"
48
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000049#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000050#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000051
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "DWARFCompileUnit.h"
53#include "DWARFDebugAbbrev.h"
54#include "DWARFDebugAranges.h"
55#include "DWARFDebugInfo.h"
56#include "DWARFDebugInfoEntry.h"
57#include "DWARFDebugLine.h"
58#include "DWARFDebugPubnames.h"
59#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000060#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFDIECollection.h"
62#include "DWARFFormValue.h"
63#include "DWARFLocationList.h"
64#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000065#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
67#include <map>
68
Greg Clayton62742b12010-11-11 01:09:45 +000069//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000070
71#ifdef ENABLE_DEBUG_PRINTF
72#include <stdio.h>
73#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
74#else
75#define DEBUG_PRINTF(fmt, ...)
76#endif
77
Greg Clayton594e5ed2010-09-27 21:07:38 +000078#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079
80using namespace lldb;
81using namespace lldb_private;
82
Greg Clayton219cf312012-03-30 00:51:13 +000083//static inline bool
84//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
85//{
86// switch (tag)
87// {
88// default:
89// break;
90// case DW_TAG_subprogram:
91// case DW_TAG_inlined_subroutine:
92// case DW_TAG_class_type:
93// case DW_TAG_structure_type:
94// case DW_TAG_union_type:
95// return true;
96// }
97// return false;
98//}
99//
Sean Callananc7fbf732010-08-06 00:32:49 +0000100static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000101DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102{
103 switch (dwarf_accessibility)
104 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000105 case DW_ACCESS_public: return eAccessPublic;
106 case DW_ACCESS_private: return eAccessPrivate;
107 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000108 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000110 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111}
112
Greg Clayton1fba87112012-02-09 20:03:49 +0000113#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
114
115class DIEStack
116{
117public:
118
119 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
120 {
121 m_dies.push_back (DIEInfo(cu, die));
122 }
123
124
125 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
126 {
127 StreamString log_strm;
128 const size_t n = m_dies.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000129 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
Greg Clayton1fba87112012-02-09 20:03:49 +0000130 for (size_t i=0; i<n; i++)
131 {
132 DWARFCompileUnit *cu = m_dies[i].cu;
133 const DWARFDebugInfoEntry *die = m_dies[i].die;
134 std::string qualified_name;
135 die->GetQualifiedName(dwarf, cu, qualified_name);
Daniel Malead01b2952012-11-29 21:49:15 +0000136 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n",
Greg Clayton43e0af02012-09-18 18:04:04 +0000137 (uint64_t)i,
Greg Clayton1fba87112012-02-09 20:03:49 +0000138 die->GetOffset(),
139 DW_TAG_value_to_name(die->Tag()),
140 qualified_name.c_str());
141 }
142 log->PutCString(log_strm.GetData());
143 }
144 void Pop ()
145 {
146 m_dies.pop_back();
147 }
148
149 class ScopedPopper
150 {
151 public:
152 ScopedPopper (DIEStack &die_stack) :
153 m_die_stack (die_stack),
154 m_valid (false)
155 {
156 }
157
158 void
159 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
160 {
161 m_valid = true;
162 m_die_stack.Push (cu, die);
163 }
164
165 ~ScopedPopper ()
166 {
167 if (m_valid)
168 m_die_stack.Pop();
169 }
170
171
172
173 protected:
174 DIEStack &m_die_stack;
175 bool m_valid;
176 };
177
178protected:
179 struct DIEInfo {
180 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
181 cu(c),
182 die(d)
183 {
184 }
185 DWARFCompileUnit *cu;
186 const DWARFDebugInfoEntry *die;
187 };
188 typedef std::vector<DIEInfo> Stack;
189 Stack m_dies;
190};
191#endif
192
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193void
194SymbolFileDWARF::Initialize()
195{
196 LogChannelDWARF::Initialize();
197 PluginManager::RegisterPlugin (GetPluginNameStatic(),
198 GetPluginDescriptionStatic(),
199 CreateInstance);
200}
201
202void
203SymbolFileDWARF::Terminate()
204{
205 PluginManager::UnregisterPlugin (CreateInstance);
206 LogChannelDWARF::Initialize();
207}
208
209
Greg Clayton57abc5d2013-05-10 21:47:16 +0000210lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211SymbolFileDWARF::GetPluginNameStatic()
212{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000213 static ConstString g_name("dwarf");
214 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215}
216
217const char *
218SymbolFileDWARF::GetPluginDescriptionStatic()
219{
220 return "DWARF and DWARF3 debug symbol file reader.";
221}
222
223
224SymbolFile*
225SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
226{
227 return new SymbolFileDWARF(obj_file);
228}
229
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000230TypeList *
231SymbolFileDWARF::GetTypeList ()
232{
Greg Clayton1f746072012-08-29 21:13:06 +0000233 if (GetDebugMapSymfile ())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000234 return m_debug_map_symfile->GetTypeList();
235 return m_obj_file->GetModule()->GetTypeList();
236
237}
Greg Claytonf02500c2013-06-18 22:51:05 +0000238void
239SymbolFileDWARF::GetTypes (DWARFCompileUnit* cu,
240 const DWARFDebugInfoEntry *die,
241 dw_offset_t min_die_offset,
242 dw_offset_t max_die_offset,
243 uint32_t type_mask,
244 TypeSet &type_set)
245{
246 if (cu)
247 {
248 if (die)
249 {
250 const dw_offset_t die_offset = die->GetOffset();
251
252 if (die_offset >= max_die_offset)
253 return;
254
255 if (die_offset >= min_die_offset)
256 {
257 const dw_tag_t tag = die->Tag();
258
259 bool add_type = false;
260
261 switch (tag)
262 {
263 case DW_TAG_array_type: add_type = (type_mask & eTypeClassArray ) != 0; break;
264 case DW_TAG_unspecified_type:
265 case DW_TAG_base_type: add_type = (type_mask & eTypeClassBuiltin ) != 0; break;
266 case DW_TAG_class_type: add_type = (type_mask & eTypeClassClass ) != 0; break;
267 case DW_TAG_structure_type: add_type = (type_mask & eTypeClassStruct ) != 0; break;
268 case DW_TAG_union_type: add_type = (type_mask & eTypeClassUnion ) != 0; break;
269 case DW_TAG_enumeration_type: add_type = (type_mask & eTypeClassEnumeration ) != 0; break;
270 case DW_TAG_subroutine_type:
271 case DW_TAG_subprogram:
272 case DW_TAG_inlined_subroutine: add_type = (type_mask & eTypeClassFunction ) != 0; break;
273 case DW_TAG_pointer_type: add_type = (type_mask & eTypeClassPointer ) != 0; break;
274 case DW_TAG_rvalue_reference_type:
275 case DW_TAG_reference_type: add_type = (type_mask & eTypeClassReference ) != 0; break;
276 case DW_TAG_typedef: add_type = (type_mask & eTypeClassTypedef ) != 0; break;
277 case DW_TAG_ptr_to_member_type: add_type = (type_mask & eTypeClassMemberPointer ) != 0; break;
278 }
279
280 if (add_type)
281 {
282 const bool assert_not_being_parsed = true;
283 Type *type = ResolveTypeUID (cu, die, assert_not_being_parsed);
284 if (type)
285 {
286 if (type_set.find(type) == type_set.end())
287 type_set.insert(type);
288 }
289 }
290 }
291
292 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild();
293 child_die != NULL;
294 child_die = child_die->GetSibling())
295 {
296 GetTypes (cu, child_die, min_die_offset, max_die_offset, type_mask, type_set);
297 }
298 }
299 }
300}
301
302size_t
303SymbolFileDWARF::GetTypes (SymbolContextScope *sc_scope,
304 uint32_t type_mask,
305 TypeList &type_list)
306
307{
308 TypeSet type_set;
309
310 CompileUnit *comp_unit = NULL;
311 DWARFCompileUnit* dwarf_cu = NULL;
312 if (sc_scope)
313 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
314
315 if (comp_unit)
316 {
317 dwarf_cu = GetDWARFCompileUnit(comp_unit);
318 if (dwarf_cu == 0)
319 return 0;
320 GetTypes (dwarf_cu,
321 dwarf_cu->DIE(),
322 dwarf_cu->GetOffset(),
323 dwarf_cu->GetNextCompileUnitOffset(),
324 type_mask,
325 type_set);
326 }
327 else
328 {
329 DWARFDebugInfo* info = DebugInfo();
330 if (info)
331 {
332 const size_t num_cus = info->GetNumCompileUnits();
333 for (size_t cu_idx=0; cu_idx<num_cus; ++cu_idx)
334 {
335 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
336 if (dwarf_cu)
337 {
338 GetTypes (dwarf_cu,
339 dwarf_cu->DIE(),
340 0,
341 UINT32_MAX,
342 type_mask,
343 type_set);
344 }
345 }
346 }
347 }
348// if (m_using_apple_tables)
349// {
350// DWARFMappedHash::MemoryTable *apple_types = m_apple_types_ap.get();
351// if (apple_types)
352// {
353// apple_types->ForEach([this, &type_set, apple_types, type_mask](const DWARFMappedHash::DIEInfoArray &die_info_array) -> bool {
354//
355// for (auto die_info: die_info_array)
356// {
357// bool add_type = TagMatchesTypeMask (type_mask, 0);
358// if (!add_type)
359// {
360// dw_tag_t tag = die_info.tag;
361// if (tag == 0)
362// {
363// const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_info.offset, NULL);
364// tag = die->Tag();
365// }
366// add_type = TagMatchesTypeMask (type_mask, tag);
367// }
368// if (add_type)
369// {
370// Type *type = ResolveTypeUID(die_info.offset);
371//
372// if (type_set.find(type) == type_set.end())
373// type_set.insert(type);
374// }
375// }
376// return true; // Keep iterating
377// });
378// }
379// }
380// else
381// {
382// if (!m_indexed)
383// Index ();
384//
385// m_type_index.ForEach([this, &type_set, type_mask](const char *name, uint32_t die_offset) -> bool {
386//
387// bool add_type = TagMatchesTypeMask (type_mask, 0);
388//
389// if (!add_type)
390// {
391// const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_offset, NULL);
392// if (die)
393// {
394// const dw_tag_t tag = die->Tag();
395// add_type = TagMatchesTypeMask (type_mask, tag);
396// }
397// }
398//
399// if (add_type)
400// {
401// Type *type = ResolveTypeUID(die_offset);
402//
403// if (type_set.find(type) == type_set.end())
404// type_set.insert(type);
405// }
406// return true; // Keep iterating
407// });
408// }
409
410 size_t num_types_added = 0;
411 for (Type *type : type_set)
412 {
413 type_list.Insert (type->shared_from_this());
414 ++num_types_added;
415 }
416 return num_types_added;
417}
418
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000419
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420//----------------------------------------------------------------------
421// Gets the first parent that is a lexical block, function or inlined
422// subroutine, or compile unit.
423//----------------------------------------------------------------------
424static const DWARFDebugInfoEntry *
425GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
426{
427 const DWARFDebugInfoEntry *die;
428 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
429 {
430 dw_tag_t tag = die->Tag();
431
432 switch (tag)
433 {
434 case DW_TAG_compile_unit:
435 case DW_TAG_subprogram:
436 case DW_TAG_inlined_subroutine:
437 case DW_TAG_lexical_block:
438 return die;
439 }
440 }
441 return NULL;
442}
443
444
Greg Clayton450e3f32010-10-12 02:24:53 +0000445SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
446 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000447 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton1f746072012-08-29 21:13:06 +0000448 m_debug_map_module_wp (),
Greg Clayton450e3f32010-10-12 02:24:53 +0000449 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000450 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000452 m_data_debug_abbrev (),
453 m_data_debug_aranges (),
454 m_data_debug_frame (),
455 m_data_debug_info (),
456 m_data_debug_line (),
457 m_data_debug_loc (),
458 m_data_debug_ranges (),
459 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000460 m_data_apple_names (),
461 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000462 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 m_info(),
465 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000466 m_apple_names_ap (),
467 m_apple_types_ap (),
468 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000469 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000470 m_function_basename_index(),
471 m_function_fullname_index(),
472 m_function_method_index(),
473 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000474 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000475 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000476 m_type_index(),
477 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000478 m_indexed (false),
479 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000480 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000481 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000482 m_ranges(),
483 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484{
485}
486
487SymbolFileDWARF::~SymbolFileDWARF()
488{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000489 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000490 {
491 ModuleSP module_sp (m_obj_file->GetModule());
492 if (module_sp)
493 module_sp->GetClangASTContext().RemoveExternalSource ();
494 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000495}
496
497static const ConstString &
498GetDWARFMachOSegmentName ()
499{
500 static ConstString g_dwarf_section_name ("__DWARF");
501 return g_dwarf_section_name;
502}
503
Greg Claytone576ab22011-02-15 00:19:15 +0000504UniqueDWARFASTTypeMap &
505SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
506{
Greg Clayton1f746072012-08-29 21:13:06 +0000507 if (GetDebugMapSymfile ())
Greg Claytone576ab22011-02-15 00:19:15 +0000508 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
509 return m_unique_ast_type_map;
510}
511
Greg Clayton6beaaa62011-01-17 03:46:26 +0000512ClangASTContext &
513SymbolFileDWARF::GetClangASTContext ()
514{
Greg Clayton1f746072012-08-29 21:13:06 +0000515 if (GetDebugMapSymfile ())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000516 return m_debug_map_symfile->GetClangASTContext ();
517
518 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
519 if (!m_is_external_ast_source)
520 {
521 m_is_external_ast_source = true;
522 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
523 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
524 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000525 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000526 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000527 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000528 ast.SetExternalSource (ast_source_ap);
529 }
530 return ast;
531}
532
533void
534SymbolFileDWARF::InitializeObject()
535{
536 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000537 ModuleSP module_sp (m_obj_file->GetModule());
538 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000539 {
540 const SectionList *section_list = m_obj_file->GetSectionList();
541
542 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
543
544 // Memory map the DWARF mach-o segment so we have everything mmap'ed
545 // to keep our heap memory usage down.
546 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000547 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000548 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000549 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000550 if (m_data_apple_names.GetByteSize() > 0)
551 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000552 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
553 if (m_apple_names_ap->IsValid())
554 m_using_apple_tables = true;
555 else
Greg Clayton7f995132011-10-04 22:41:51 +0000556 m_apple_names_ap.reset();
557 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000558 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000559 if (m_data_apple_types.GetByteSize() > 0)
560 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000561 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
562 if (m_apple_types_ap->IsValid())
563 m_using_apple_tables = true;
564 else
Greg Clayton7f995132011-10-04 22:41:51 +0000565 m_apple_types_ap.reset();
566 }
567
568 get_apple_namespaces_data();
569 if (m_data_apple_namespaces.GetByteSize() > 0)
570 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000571 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
572 if (m_apple_namespaces_ap->IsValid())
573 m_using_apple_tables = true;
574 else
Greg Clayton7f995132011-10-04 22:41:51 +0000575 m_apple_namespaces_ap.reset();
576 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000577
Greg Clayton5009f9d2011-10-27 17:55:14 +0000578 get_apple_objc_data();
579 if (m_data_apple_objc.GetByteSize() > 0)
580 {
581 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
582 if (m_apple_objc_ap->IsValid())
583 m_using_apple_tables = true;
584 else
585 m_apple_objc_ap.reset();
586 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
589bool
590SymbolFileDWARF::SupportedVersion(uint16_t version)
591{
Greg Claytonabcbfe52013-04-04 00:00:36 +0000592 return version == 2 || version == 3 || version == 4;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593}
594
595uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000596SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597{
598 uint32_t abilities = 0;
599 if (m_obj_file != NULL)
600 {
601 const Section* section = NULL;
602 const SectionList *section_list = m_obj_file->GetSectionList();
603 if (section_list == NULL)
604 return 0;
605
606 uint64_t debug_abbrev_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 uint64_t debug_info_file_size = 0;
608 uint64_t debug_line_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609
Greg Clayton6beaaa62011-01-17 03:46:26 +0000610 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611
612 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000613 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614
Greg Clayton4ceb9982010-07-21 22:54:26 +0000615 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 if (section != NULL)
617 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000618 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619
Greg Clayton4ceb9982010-07-21 22:54:26 +0000620 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000622 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 else
624 m_flags.Set (flagsGotDebugAbbrevData);
625
Greg Clayton4ceb9982010-07-21 22:54:26 +0000626 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000627 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 m_flags.Set (flagsGotDebugArangesData);
629
Greg Clayton4ceb9982010-07-21 22:54:26 +0000630 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000631 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 m_flags.Set (flagsGotDebugFrameData);
633
Greg Clayton4ceb9982010-07-21 22:54:26 +0000634 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000636 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 else
638 m_flags.Set (flagsGotDebugLineData);
639
Greg Clayton4ceb9982010-07-21 22:54:26 +0000640 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000641 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642 m_flags.Set (flagsGotDebugLocData);
643
Greg Clayton4ceb9982010-07-21 22:54:26 +0000644 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000645 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646 m_flags.Set (flagsGotDebugMacInfoData);
647
Greg Clayton4ceb9982010-07-21 22:54:26 +0000648 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000649 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 m_flags.Set (flagsGotDebugPubNamesData);
651
Greg Clayton4ceb9982010-07-21 22:54:26 +0000652 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000653 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 m_flags.Set (flagsGotDebugPubTypesData);
655
Greg Clayton4ceb9982010-07-21 22:54:26 +0000656 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000657 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 m_flags.Set (flagsGotDebugRangesData);
659
Greg Clayton4ceb9982010-07-21 22:54:26 +0000660 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000661 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 m_flags.Set (flagsGotDebugStrData);
663 }
Greg Clayton6c596612012-05-18 21:47:20 +0000664 else
665 {
666 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString();
667 if (symfile_dir_cstr)
668 {
669 if (strcasestr(symfile_dir_cstr, ".dsym"))
670 {
671 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo)
672 {
673 // We have a dSYM file that didn't have a any debug info.
674 // If the string table has a size of 1, then it was made from
675 // an executable with no debug info, or from an executable that
676 // was stripped.
677 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
678 if (section && section->GetFileSize() == 1)
679 {
680 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info.");
681 }
682 }
683 }
684 }
685 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686
687 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
688 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
689
690 if (debug_line_file_size > 0)
691 abilities |= LineTables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 }
693 return abilities;
694}
695
696const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000697SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698{
699 if (m_flags.IsClear (got_flag))
700 {
701 m_flags.Set (got_flag);
702 const SectionList *section_list = m_obj_file->GetSectionList();
703 if (section_list)
704 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000705 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
706 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 {
708 // See if we memory mapped the DWARF segment?
709 if (m_dwarf_data.GetByteSize())
710 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000711 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 }
713 else
714 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000715 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 data.Clear();
717 }
718 }
719 }
720 }
721 return data;
722}
723
724const DataExtractor&
725SymbolFileDWARF::get_debug_abbrev_data()
726{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000727 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728}
729
730const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000731SymbolFileDWARF::get_debug_aranges_data()
732{
733 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
734}
735
736const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737SymbolFileDWARF::get_debug_frame_data()
738{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000739 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740}
741
742const DataExtractor&
743SymbolFileDWARF::get_debug_info_data()
744{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000745 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746}
747
748const DataExtractor&
749SymbolFileDWARF::get_debug_line_data()
750{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000751 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752}
753
754const DataExtractor&
755SymbolFileDWARF::get_debug_loc_data()
756{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000757 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758}
759
760const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761SymbolFileDWARF::get_debug_ranges_data()
762{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000763 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764}
765
766const DataExtractor&
767SymbolFileDWARF::get_debug_str_data()
768{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000769 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770}
771
Greg Claytonf9eec202011-09-01 23:16:13 +0000772const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000773SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000774{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000775 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000776}
777
778const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000779SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000780{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000781 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000782}
783
Greg Clayton7f995132011-10-04 22:41:51 +0000784const DataExtractor&
785SymbolFileDWARF::get_apple_namespaces_data()
786{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000787 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
788}
789
790const DataExtractor&
791SymbolFileDWARF::get_apple_objc_data()
792{
793 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000794}
795
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796
797DWARFDebugAbbrev*
798SymbolFileDWARF::DebugAbbrev()
799{
800 if (m_abbr.get() == NULL)
801 {
802 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
803 if (debug_abbrev_data.GetByteSize() > 0)
804 {
805 m_abbr.reset(new DWARFDebugAbbrev());
806 if (m_abbr.get())
807 m_abbr->Parse(debug_abbrev_data);
808 }
809 }
810 return m_abbr.get();
811}
812
813const DWARFDebugAbbrev*
814SymbolFileDWARF::DebugAbbrev() const
815{
816 return m_abbr.get();
817}
818
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819
820DWARFDebugInfo*
821SymbolFileDWARF::DebugInfo()
822{
823 if (m_info.get() == NULL)
824 {
825 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
826 if (get_debug_info_data().GetByteSize() > 0)
827 {
828 m_info.reset(new DWARFDebugInfo());
829 if (m_info.get())
830 {
831 m_info->SetDwarfData(this);
832 }
833 }
834 }
835 return m_info.get();
836}
837
838const DWARFDebugInfo*
839SymbolFileDWARF::DebugInfo() const
840{
841 return m_info.get();
842}
843
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844DWARFCompileUnit*
Greg Clayton1f746072012-08-29 21:13:06 +0000845SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846{
847 DWARFDebugInfo* info = DebugInfo();
Greg Clayton1f746072012-08-29 21:13:06 +0000848 if (info)
849 {
850 if (GetDebugMapSymfile ())
851 {
852 // The debug map symbol file made the compile units for this DWARF
853 // file which is .o file with DWARF in it, and we should have
854 // only 1 compile unit which is at offset zero in the DWARF.
855 // TODO: modify to support LTO .o files where each .o file might
856 // have multiple DW_TAG_compile_unit tags.
857 return info->GetCompileUnit(0).get();
858 }
859 else
860 {
861 // Just a normal DWARF file whose user ID for the compile unit is
862 // the DWARF offset itself
863 return info->GetCompileUnit((dw_offset_t)comp_unit->GetID()).get();
864 }
865 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 return NULL;
867}
868
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869
870DWARFDebugRanges*
871SymbolFileDWARF::DebugRanges()
872{
873 if (m_ranges.get() == NULL)
874 {
875 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
876 if (get_debug_ranges_data().GetByteSize() > 0)
877 {
878 m_ranges.reset(new DWARFDebugRanges());
879 if (m_ranges.get())
880 m_ranges->Extract(this);
881 }
882 }
883 return m_ranges.get();
884}
885
886const DWARFDebugRanges*
887SymbolFileDWARF::DebugRanges() const
888{
889 return m_ranges.get();
890}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891
Greg Clayton53eb1c22012-04-02 22:59:12 +0000892lldb::CompUnitSP
893SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000894{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000895 CompUnitSP cu_sp;
896 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000898 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
899 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000901 // We already parsed this compile unit, had out a shared pointer to it
902 cu_sp = comp_unit->shared_from_this();
903 }
904 else
905 {
Greg Clayton1f746072012-08-29 21:13:06 +0000906 if (GetDebugMapSymfile ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000907 {
Greg Clayton1f746072012-08-29 21:13:06 +0000908 // Let the debug map create the compile unit
909 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
910 dwarf_cu->SetUserData(cu_sp.get());
911 }
912 else
913 {
914 ModuleSP module_sp (m_obj_file->GetModule());
915 if (module_sp)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000916 {
Greg Clayton1f746072012-08-29 21:13:06 +0000917 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
918 if (cu_die)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000919 {
Greg Clayton1f746072012-08-29 21:13:06 +0000920 const char * cu_die_name = cu_die->GetName(this, dwarf_cu);
921 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
922 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
923 if (cu_die_name)
924 {
925 std::string ramapped_file;
926 FileSpec cu_file_spec;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000927
Greg Clayton1f746072012-08-29 21:13:06 +0000928 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Greg Clayton53eb1c22012-04-02 22:59:12 +0000929 {
Greg Clayton1f746072012-08-29 21:13:06 +0000930 // If we have a full path to the compile unit, we don't need to resolve
931 // the file. This can be expensive e.g. when the source files are NFS mounted.
932 if (module_sp->RemapSourceFile(cu_die_name, ramapped_file))
933 cu_file_spec.SetFile (ramapped_file.c_str(), false);
934 else
935 cu_file_spec.SetFile (cu_die_name, false);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000936 }
937 else
938 {
Greg Clayton1f746072012-08-29 21:13:06 +0000939 std::string fullpath(cu_comp_dir);
940 if (*fullpath.rbegin() != '/')
941 fullpath += '/';
942 fullpath += cu_die_name;
943 if (module_sp->RemapSourceFile (fullpath.c_str(), ramapped_file))
944 cu_file_spec.SetFile (ramapped_file.c_str(), false);
945 else
946 cu_file_spec.SetFile (fullpath.c_str(), false);
947 }
948
949 cu_sp.reset(new CompileUnit (module_sp,
950 dwarf_cu,
951 cu_file_spec,
952 MakeUserID(dwarf_cu->GetOffset()),
953 cu_language));
954 if (cu_sp)
955 {
956 dwarf_cu->SetUserData(cu_sp.get());
957
Greg Clayton53eb1c22012-04-02 22:59:12 +0000958 // Figure out the compile unit index if we weren't given one
959 if (cu_idx == UINT32_MAX)
960 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
961
962 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
963 }
964 }
965 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966 }
967 }
968 }
969 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000970 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971}
972
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973uint32_t
974SymbolFileDWARF::GetNumCompileUnits()
975{
976 DWARFDebugInfo* info = DebugInfo();
977 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000978 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979 return 0;
980}
981
982CompUnitSP
983SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
984{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000985 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 DWARFDebugInfo* info = DebugInfo();
987 if (info)
988 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000989 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
990 if (dwarf_cu)
991 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000993 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000994}
995
996static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000997AddRangesToBlock (Block& block,
998 DWARFDebugRanges::RangeList& ranges,
999 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001000{
Greg Claytonea3e7d52011-10-08 00:49:15 +00001001 const size_t num_ranges = ranges.GetSize();
1002 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001004 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
1005 const addr_t range_base = range.GetRangeBase();
1006 assert (range_base >= block_base_addr);
1007 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001008 }
Greg Claytonea3e7d52011-10-08 00:49:15 +00001009 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010}
1011
1012
1013Function *
Greg Clayton0fffff52010-09-24 05:15:53 +00001014SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015{
1016 DWARFDebugRanges::RangeList func_ranges;
1017 const char *name = NULL;
1018 const char *mangled = NULL;
1019 int decl_file = 0;
1020 int decl_line = 0;
1021 int decl_column = 0;
1022 int call_file = 0;
1023 int call_line = 0;
1024 int call_column = 0;
1025 DWARFExpression frame_base;
1026
Greg Claytonc93237c2010-10-01 20:48:32 +00001027 assert (die->Tag() == DW_TAG_subprogram);
1028
1029 if (die->Tag() != DW_TAG_subprogram)
1030 return NULL;
1031
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001032 if (die->GetDIENamesAndRanges (this,
1033 dwarf_cu,
1034 name,
1035 mangled,
1036 func_ranges,
1037 decl_file,
1038 decl_line,
1039 decl_column,
1040 call_file,
1041 call_line,
1042 call_column,
1043 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044 {
1045 // Union of all ranges in the function DIE (if the function is discontiguous)
1046 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +00001047 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
1048 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001049 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
1050 {
1051 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
1052 if (func_range.GetBaseAddress().IsValid())
1053 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
1054 }
1055
1056 if (func_range.GetBaseAddress().IsValid())
1057 {
1058 Mangled func_name;
1059 if (mangled)
Greg Clayton037520e2012-07-18 23:18:10 +00001060 func_name.SetValue(ConstString(mangled), true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061 else if (name)
Greg Clayton037520e2012-07-18 23:18:10 +00001062 func_name.SetValue(ConstString(name), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063
1064 FunctionSP func_sp;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001065 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +00001067 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1068 decl_line,
1069 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070
Greg Clayton0bd4e1b2011-09-30 20:52:25 +00001071 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +00001072 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073
1074 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
1075
Greg Clayton9422dd62013-03-04 21:46:16 +00001076 if (FixupAddress (func_range.GetBaseAddress()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001078 const user_id_t func_user_id = MakeUserID(die->GetOffset());
1079 func_sp.reset(new Function (sc.comp_unit,
1080 MakeUserID(func_user_id), // UserID is the DIE offset
1081 MakeUserID(func_user_id),
1082 func_name,
1083 func_type,
1084 func_range)); // first address range
1085
1086 if (func_sp.get() != NULL)
1087 {
1088 if (frame_base.IsValid())
1089 func_sp->GetFrameBaseExpression() = frame_base;
1090 sc.comp_unit->AddFunction(func_sp);
1091 return func_sp.get();
1092 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093 }
1094 }
1095 }
1096 return NULL;
1097}
1098
Greg Clayton9422dd62013-03-04 21:46:16 +00001099bool
1100SymbolFileDWARF::FixupAddress (Address &addr)
1101{
1102 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
1103 if (debug_map_symfile)
1104 {
1105 return debug_map_symfile->LinkOSOAddress(addr);
1106 }
1107 // This is a normal DWARF file, no address fixups need to happen
1108 return true;
1109}
Greg Clayton1f746072012-08-29 21:13:06 +00001110lldb::LanguageType
1111SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
1112{
1113 assert (sc.comp_unit);
1114 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1115 if (dwarf_cu)
1116 {
1117 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly();
Jim Ingham28eb5712012-10-12 17:34:26 +00001118 if (die)
1119 {
1120 const uint32_t language = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
1121 if (language)
1122 return (lldb::LanguageType)language;
1123 }
Greg Clayton1f746072012-08-29 21:13:06 +00001124 }
1125 return eLanguageTypeUnknown;
1126}
1127
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128size_t
1129SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
1130{
1131 assert (sc.comp_unit);
1132 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00001133 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 if (dwarf_cu)
1135 {
1136 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +00001137 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +00001139 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140 {
1141 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +00001142 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001143 {
1144 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
1145 ++functions_added;
1146 }
1147 }
1148 //FixupTypes();
1149 }
1150 return functions_added;
1151}
1152
1153bool
1154SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
1155{
1156 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +00001157 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Claytonda2455b2012-11-01 17:28:37 +00001158 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159 {
Greg Claytonda2455b2012-11-01 17:28:37 +00001160 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001161
Greg Claytonda2455b2012-11-01 17:28:37 +00001162 if (cu_die)
1163 {
1164 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
1165 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166
Greg Claytonda2455b2012-11-01 17:28:37 +00001167 // All file indexes in DWARF are one based and a file of index zero is
1168 // supposed to be the compile unit itself.
1169 support_files.Append (*sc.comp_unit);
1170
1171 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
1172 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173 }
1174 return false;
1175}
1176
1177struct ParseDWARFLineTableCallbackInfo
1178{
1179 LineTable* line_table;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001180 std::unique_ptr<LineSequence> sequence_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181};
1182
1183//----------------------------------------------------------------------
1184// ParseStatementTableCallback
1185//----------------------------------------------------------------------
1186static void
1187ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1188{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001189 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1190 {
1191 // Just started parsing the line table
1192 }
1193 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1194 {
1195 // Done parsing line table, nothing to do for the cleanup
1196 }
1197 else
1198 {
1199 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
Greg Clayton9422dd62013-03-04 21:46:16 +00001200 LineTable* line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201
Greg Clayton9422dd62013-03-04 21:46:16 +00001202 // If this is our first time here, we need to create a
1203 // sequence container.
1204 if (!info->sequence_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001206 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1207 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001209 line_table->AppendLineEntryToSequence (info->sequence_ap.get(),
1210 state.address,
1211 state.line,
1212 state.column,
1213 state.file,
1214 state.is_stmt,
1215 state.basic_block,
1216 state.prologue_end,
1217 state.epilogue_begin,
1218 state.end_sequence);
1219 if (state.end_sequence)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001221 // First, put the current sequence into the line table.
1222 line_table->InsertSequence(info->sequence_ap.get());
1223 // Then, empty it to prepare for the next sequence.
1224 info->sequence_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 }
1227}
1228
1229bool
1230SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1231{
1232 assert (sc.comp_unit);
1233 if (sc.comp_unit->GetLineTable() != NULL)
1234 return true;
1235
Greg Clayton1f746072012-08-29 21:13:06 +00001236 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237 if (dwarf_cu)
1238 {
1239 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001240 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001242 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1243 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001245 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
Greg Clayton129d12c2011-11-28 03:29:03 +00001246 if (line_table_ap.get())
1247 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001248 ParseDWARFLineTableCallbackInfo info;
1249 info.line_table = line_table_ap.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00001250 lldb::offset_t offset = cu_line_offset;
Greg Clayton129d12c2011-11-28 03:29:03 +00001251 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
Greg Clayton9422dd62013-03-04 21:46:16 +00001252 if (m_debug_map_symfile)
1253 {
1254 // We have an object file that has a line table with addresses
1255 // that are not linked. We need to link the line table and convert
1256 // the addresses that are relative to the .o file into addresses
1257 // for the main executable.
1258 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get()));
1259 }
1260 else
1261 {
1262 sc.comp_unit->SetLineTable(line_table_ap.release());
1263 return true;
1264 }
Greg Clayton129d12c2011-11-28 03:29:03 +00001265 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001266 }
1267 }
1268 }
1269 return false;
1270}
1271
1272size_t
1273SymbolFileDWARF::ParseFunctionBlocks
1274(
1275 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001276 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001277 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001278 const DWARFDebugInfoEntry *die,
1279 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001280 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281)
1282{
1283 size_t blocks_added = 0;
1284 while (die != NULL)
1285 {
1286 dw_tag_t tag = die->Tag();
1287
1288 switch (tag)
1289 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001291 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001292 case DW_TAG_lexical_block:
1293 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001294 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001295 if (tag == DW_TAG_subprogram)
1296 {
1297 // Skip any DW_TAG_subprogram DIEs that are inside
1298 // of a normal or inlined functions. These will be
1299 // parsed on their own as separate entities.
1300
1301 if (depth > 0)
1302 break;
1303
1304 block = parent_block;
1305 }
1306 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001307 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001308 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001309 parent_block->AddChild(block_sp);
1310 block = block_sp.get();
1311 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001312 DWARFDebugRanges::RangeList ranges;
1313 const char *name = NULL;
1314 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316 int decl_file = 0;
1317 int decl_line = 0;
1318 int decl_column = 0;
1319 int call_file = 0;
1320 int call_line = 0;
1321 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001322 if (die->GetDIENamesAndRanges (this,
1323 dwarf_cu,
1324 name,
1325 mangled_name,
1326 ranges,
1327 decl_file, decl_line, decl_column,
1328 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001329 {
1330 if (tag == DW_TAG_subprogram)
1331 {
1332 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001333 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001335 else if (tag == DW_TAG_inlined_subroutine)
1336 {
1337 // We get called here for inlined subroutines in two ways.
1338 // The first time is when we are making the Function object
1339 // for this inlined concrete instance. Since we're creating a top level block at
1340 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1341 // adjust the containing address.
1342 // The second time is when we are parsing the blocks inside the function that contains
1343 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1344 // function the offset will be for that function.
1345 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1346 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001347 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001348 }
1349 }
1350
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001351 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001352
1353 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1354 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001355 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001357 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1358 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359
Greg Clayton7b0992d2013-04-18 22:45:39 +00001360 std::unique_ptr<Declaration> call_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001362 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1363 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001365 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001366 }
1367
1368 ++blocks_added;
1369
Greg Claytondd7feaf2011-08-12 17:54:33 +00001370 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001371 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001372 blocks_added += ParseFunctionBlocks (sc,
1373 block,
1374 dwarf_cu,
1375 die->GetFirstChild(),
1376 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001377 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378 }
1379 }
1380 }
1381 break;
1382 default:
1383 break;
1384 }
1385
Greg Claytondd7feaf2011-08-12 17:54:33 +00001386 // Only parse siblings of the block if we are not at depth zero. A depth
1387 // of zero indicates we are currently parsing the top level
1388 // DW_TAG_subprogram DIE
1389
1390 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001392 else
1393 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394 }
1395 return blocks_added;
1396}
1397
Greg Claytonf0705c82011-10-22 03:33:13 +00001398bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001399SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1400 const DWARFDebugInfoEntry *die,
1401 ClangASTContext::TemplateParameterInfos &template_param_infos)
1402{
1403 const dw_tag_t tag = die->Tag();
1404
1405 switch (tag)
1406 {
1407 case DW_TAG_template_type_parameter:
1408 case DW_TAG_template_value_parameter:
1409 {
1410 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1411
1412 DWARFDebugInfoEntry::Attributes attributes;
1413 const size_t num_attributes = die->GetAttributes (this,
1414 dwarf_cu,
1415 fixed_form_sizes,
1416 attributes);
1417 const char *name = NULL;
1418 Type *lldb_type = NULL;
1419 clang_type_t clang_type = NULL;
1420 uint64_t uval64 = 0;
1421 bool uval64_valid = false;
1422 if (num_attributes > 0)
1423 {
1424 DWARFFormValue form_value;
1425 for (size_t i=0; i<num_attributes; ++i)
1426 {
1427 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1428
1429 switch (attr)
1430 {
1431 case DW_AT_name:
1432 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1433 name = form_value.AsCString(&get_debug_str_data());
1434 break;
1435
1436 case DW_AT_type:
1437 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1438 {
1439 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1440 lldb_type = ResolveTypeUID(type_die_offset);
1441 if (lldb_type)
1442 clang_type = lldb_type->GetClangForwardType();
1443 }
1444 break;
1445
1446 case DW_AT_const_value:
1447 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1448 {
1449 uval64_valid = true;
1450 uval64 = form_value.Unsigned();
1451 }
1452 break;
1453 default:
1454 break;
1455 }
1456 }
1457
Greg Clayton283b2652013-04-23 22:38:02 +00001458 clang::ASTContext *ast = GetClangASTContext().getASTContext();
1459 if (!clang_type)
1460 clang_type = ast->VoidTy.getAsOpaquePtr();
1461
1462 if (clang_type)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001463 {
1464 bool is_signed = false;
Greg Clayton283b2652013-04-23 22:38:02 +00001465 if (name && name[0])
1466 template_param_infos.names.push_back(name);
1467 else
1468 template_param_infos.names.push_back(NULL);
1469
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001470 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
Greg Clayton283b2652013-04-23 22:38:02 +00001471 if (tag == DW_TAG_template_value_parameter &&
1472 lldb_type != NULL &&
1473 ClangASTContext::IsIntegerType (clang_type, is_signed) &&
1474 uval64_valid)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001475 {
1476 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
Greg Clayton283b2652013-04-23 22:38:02 +00001477 template_param_infos.args.push_back (clang::TemplateArgument (*ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001478 llvm::APSInt(apint),
1479 clang_qual_type));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001480 }
1481 else
1482 {
1483 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1484 }
1485 }
1486 else
1487 {
1488 return false;
1489 }
1490
1491 }
1492 }
1493 return true;
1494
1495 default:
1496 break;
1497 }
1498 return false;
1499}
1500
1501bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001502SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1503 const DWARFDebugInfoEntry *parent_die,
1504 ClangASTContext::TemplateParameterInfos &template_param_infos)
1505{
1506
1507 if (parent_die == NULL)
Filipe Cabecinhas52008c62012-05-23 16:24:11 +00001508 return false;
Greg Claytonf0705c82011-10-22 03:33:13 +00001509
Greg Claytonf0705c82011-10-22 03:33:13 +00001510 Args template_parameter_names;
1511 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1512 die != NULL;
1513 die = die->GetSibling())
1514 {
1515 const dw_tag_t tag = die->Tag();
1516
1517 switch (tag)
1518 {
1519 case DW_TAG_template_type_parameter:
1520 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001521 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001522 break;
1523
1524 default:
1525 break;
1526 }
1527 }
1528 if (template_param_infos.args.empty())
1529 return false;
1530 return template_param_infos.args.size() == template_param_infos.names.size();
1531}
1532
1533clang::ClassTemplateDecl *
1534SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001535 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001536 const char *parent_name,
1537 int tag_decl_kind,
1538 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1539{
1540 if (template_param_infos.IsValid())
1541 {
1542 std::string template_basename(parent_name);
1543 template_basename.erase (template_basename.find('<'));
1544 ClangASTContext &ast = GetClangASTContext();
1545
1546 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001547 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001548 template_basename.c_str(),
1549 tag_decl_kind,
1550 template_param_infos);
1551 }
1552 return NULL;
1553}
1554
Sean Callanana0b80ab2012-06-04 22:28:05 +00001555class SymbolFileDWARF::DelayedAddObjCClassProperty
1556{
1557public:
1558 DelayedAddObjCClassProperty
1559 (
1560 clang::ASTContext *ast,
1561 lldb::clang_type_t class_opaque_type,
1562 const char *property_name,
1563 lldb::clang_type_t property_opaque_type, // The property type is only required if you don't have an ivar decl
1564 clang::ObjCIvarDecl *ivar_decl,
1565 const char *property_setter_name,
1566 const char *property_getter_name,
1567 uint32_t property_attributes,
Greg Claytonc4ffd662013-03-08 01:37:30 +00001568 const ClangASTMetadata *metadata
Sean Callanana0b80ab2012-06-04 22:28:05 +00001569 ) :
1570 m_ast (ast),
1571 m_class_opaque_type (class_opaque_type),
1572 m_property_name (property_name),
1573 m_property_opaque_type (property_opaque_type),
1574 m_ivar_decl (ivar_decl),
1575 m_property_setter_name (property_setter_name),
1576 m_property_getter_name (property_getter_name),
Jim Ingham379397632012-10-27 02:54:13 +00001577 m_property_attributes (property_attributes)
Sean Callanana0b80ab2012-06-04 22:28:05 +00001578 {
Jim Ingham379397632012-10-27 02:54:13 +00001579 if (metadata != NULL)
1580 {
1581 m_metadata_ap.reset(new ClangASTMetadata());
Greg Claytonc4ffd662013-03-08 01:37:30 +00001582 *m_metadata_ap = *metadata;
Jim Ingham379397632012-10-27 02:54:13 +00001583 }
1584 }
1585
1586 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
1587 {
Daniel Malead4c5be62012-11-12 21:02:14 +00001588 *this = rhs;
1589 }
1590
1591 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs)
1592 {
Jim Ingham379397632012-10-27 02:54:13 +00001593 m_ast = rhs.m_ast;
1594 m_class_opaque_type = rhs.m_class_opaque_type;
1595 m_property_name = rhs.m_property_name;
1596 m_property_opaque_type = rhs.m_property_opaque_type;
1597 m_ivar_decl = rhs.m_ivar_decl;
1598 m_property_setter_name = rhs.m_property_setter_name;
1599 m_property_getter_name = rhs.m_property_getter_name;
1600 m_property_attributes = rhs.m_property_attributes;
1601
1602 if (rhs.m_metadata_ap.get())
1603 {
1604 m_metadata_ap.reset (new ClangASTMetadata());
Greg Claytonc4ffd662013-03-08 01:37:30 +00001605 *m_metadata_ap = *rhs.m_metadata_ap;
Jim Ingham379397632012-10-27 02:54:13 +00001606 }
Daniel Malead4c5be62012-11-12 21:02:14 +00001607 return *this;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001608 }
1609
1610 bool Finalize() const
1611 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001612 return ClangASTContext::AddObjCClassProperty (m_ast,
1613 m_class_opaque_type,
1614 m_property_name,
1615 m_property_opaque_type,
1616 m_ivar_decl,
1617 m_property_setter_name,
1618 m_property_getter_name,
1619 m_property_attributes,
1620 m_metadata_ap.get());
Sean Callanana0b80ab2012-06-04 22:28:05 +00001621 }
1622private:
1623 clang::ASTContext *m_ast;
1624 lldb::clang_type_t m_class_opaque_type;
1625 const char *m_property_name;
1626 lldb::clang_type_t m_property_opaque_type;
1627 clang::ObjCIvarDecl *m_ivar_decl;
1628 const char *m_property_setter_name;
1629 const char *m_property_getter_name;
1630 uint32_t m_property_attributes;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001631 std::unique_ptr<ClangASTMetadata> m_metadata_ap;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001632};
1633
Sean Callananfaa0bb32012-12-05 23:37:14 +00001634struct BitfieldInfo
1635{
1636 uint64_t bit_size;
1637 uint64_t bit_offset;
1638
1639 BitfieldInfo () :
1640 bit_size (LLDB_INVALID_ADDRESS),
1641 bit_offset (LLDB_INVALID_ADDRESS)
1642 {
1643 }
1644
1645 bool IsValid ()
1646 {
1647 return (bit_size != LLDB_INVALID_ADDRESS) &&
1648 (bit_offset != LLDB_INVALID_ADDRESS);
1649 }
1650};
1651
Greg Claytonc4ffd662013-03-08 01:37:30 +00001652
1653bool
1654SymbolFileDWARF::ClassOrStructIsVirtual (DWARFCompileUnit* dwarf_cu,
1655 const DWARFDebugInfoEntry *parent_die)
1656{
1657 if (parent_die)
1658 {
1659 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1660 {
1661 dw_tag_t tag = die->Tag();
1662 bool check_virtuality = false;
1663 switch (tag)
1664 {
1665 case DW_TAG_inheritance:
1666 case DW_TAG_subprogram:
1667 check_virtuality = true;
1668 break;
1669 default:
1670 break;
1671 }
1672 if (check_virtuality)
1673 {
1674 if (die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_virtuality, 0) != 0)
1675 return true;
1676 }
1677 }
1678 }
1679 return false;
1680}
1681
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682size_t
1683SymbolFileDWARF::ParseChildMembers
1684(
1685 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001686 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001688 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001689 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001690 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1691 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001692 DWARFDIECollection& member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00001693 DelayedPropertyList& delayed_properties,
Sean Callananc7fbf732010-08-06 00:32:49 +00001694 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001695 bool &is_a_class,
1696 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001697)
1698{
1699 if (parent_die == NULL)
1700 return 0;
1701
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702 size_t count = 0;
1703 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001704 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001705 uint32_t member_idx = 0;
Sean Callananfaa0bb32012-12-05 23:37:14 +00001706 BitfieldInfo last_field_info;
Greg Claytond88d7592010-09-15 08:33:30 +00001707
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001708 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1709 {
1710 dw_tag_t tag = die->Tag();
1711
1712 switch (tag)
1713 {
1714 case DW_TAG_member:
Sean Callanan3d654b32012-09-24 22:25:51 +00001715 case DW_TAG_APPLE_property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001716 {
1717 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001718 const size_t num_attributes = die->GetAttributes (this,
1719 dwarf_cu,
1720 fixed_form_sizes,
1721 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001722 if (num_attributes > 0)
1723 {
1724 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001725 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001727 const char *prop_name = NULL;
1728 const char *prop_getter_name = NULL;
1729 const char *prop_setter_name = NULL;
Greg Claytone528efd72013-02-26 23:45:18 +00001730 uint32_t prop_attributes = 0;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001731
1732
Greg Clayton24739922010-10-13 03:15:28 +00001733 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001734 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001735 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001736 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001737 size_t byte_size = 0;
1738 size_t bit_offset = 0;
1739 size_t bit_size = 0;
Greg Claytone528efd72013-02-26 23:45:18 +00001740 bool is_external = false; // On DW_TAG_members, this means the member is static
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001741 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001742 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001743 {
1744 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1745 DWARFFormValue form_value;
1746 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1747 {
1748 switch (attr)
1749 {
1750 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1751 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1752 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1753 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1754 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1755 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1756 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1757 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1758 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001759 if (form_value.BlockData())
1760 {
1761 Value initialValue(0);
1762 Value memberOffset(0);
1763 const DataExtractor& debug_info_data = get_debug_info_data();
1764 uint32_t block_length = form_value.Unsigned();
1765 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1766 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1767 NULL, // clang::ASTContext *
1768 NULL, // ClangExpressionVariableList *
1769 NULL, // ClangExpressionDeclMap *
1770 NULL, // RegisterContext *
1771 debug_info_data,
1772 block_offset,
1773 block_length,
1774 eRegisterKindDWARF,
1775 &initialValue,
1776 memberOffset,
1777 NULL))
1778 {
1779 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1780 }
1781 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001782 break;
1783
Greg Clayton8cf05932010-07-22 18:30:50 +00001784 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00001785 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001786 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1787 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1788 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1789 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00001790 case DW_AT_external: is_external = form_value.Boolean(); break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001791
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001792 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001793 case DW_AT_declaration:
1794 case DW_AT_description:
1795 case DW_AT_mutable:
1796 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001797 case DW_AT_sibling:
1798 break;
1799 }
1800 }
1801 }
Sean Callanana6582262012-04-05 00:12:52 +00001802
1803 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00001804 {
Sean Callanana6582262012-04-05 00:12:52 +00001805 ConstString fixed_getter;
1806 ConstString fixed_setter;
1807
1808 // Check if the property getter/setter were provided as full
1809 // names. We want basenames, so we extract them.
1810
1811 if (prop_getter_name && prop_getter_name[0] == '-')
1812 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00001813 ObjCLanguageRuntime::MethodName prop_getter_method(prop_getter_name, true);
1814 prop_getter_name = prop_getter_method.GetSelector().GetCString();
Sean Callanana6582262012-04-05 00:12:52 +00001815 }
1816
1817 if (prop_setter_name && prop_setter_name[0] == '-')
1818 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00001819 ObjCLanguageRuntime::MethodName prop_setter_method(prop_setter_name, true);
1820 prop_setter_name = prop_setter_method.GetSelector().GetCString();
Sean Callanana6582262012-04-05 00:12:52 +00001821 }
1822
1823 // If the names haven't been provided, they need to be
1824 // filled in.
1825
1826 if (!prop_getter_name)
1827 {
1828 prop_getter_name = prop_name;
1829 }
1830 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
1831 {
1832 StreamString ss;
1833
1834 ss.Printf("set%c%s:",
1835 toupper(prop_name[0]),
1836 &prop_name[1]);
1837
1838 fixed_setter.SetCString(ss.GetData());
1839 prop_setter_name = fixed_setter.GetCString();
1840 }
Sean Callanan751aac62012-03-29 19:07:06 +00001841 }
1842
1843 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001844 // represents fields that are references with bad byte size
1845 // and bit size/offset information such as:
1846 //
1847 // DW_AT_byte_size( 0x00 )
1848 // DW_AT_bit_size( 0x40 )
1849 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1850 //
1851 // So check the bit offset to make sure it is sane, and if
1852 // the values are not sane, remove them. If we don't do this
1853 // then we will end up with a crash if we try to use this
1854 // type in an expression when clang becomes unhappy with its
1855 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001856
Greg Clayton44953932011-10-25 01:25:35 +00001857 if (bit_offset > 128)
1858 {
1859 bit_size = 0;
1860 bit_offset = 0;
1861 }
1862
1863 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001864 if (class_language == eLanguageTypeObjC ||
1865 class_language == eLanguageTypeObjC_plus_plus)
1866 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001867
1868 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1869 {
1870 // Not all compilers will mark the vtable pointer
1871 // member as artificial (llvm-gcc). We can't have
1872 // the virtual members in our classes otherwise it
1873 // throws off all child offsets since we end up
1874 // having and extra pointer sized member in our
1875 // class layouts.
1876 is_artificial = true;
1877 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001878
Greg Claytone528efd72013-02-26 23:45:18 +00001879 // Skip static members
1880 if (is_external && member_byte_offset == UINT32_MAX)
Greg Clayton973b6c92013-04-11 16:57:51 +00001881 {
1882 Type *var_type = ResolveTypeUID(encoding_uid);
1883
1884 if (var_type)
1885 {
1886 GetClangASTContext().AddVariableToRecordType (class_clang_type,
1887 name,
1888 var_type->GetClangLayoutType(),
1889 accessibility);
1890 }
Greg Claytone528efd72013-02-26 23:45:18 +00001891 break;
Greg Clayton973b6c92013-04-11 16:57:51 +00001892 }
Greg Claytone528efd72013-02-26 23:45:18 +00001893
Greg Clayton24739922010-10-13 03:15:28 +00001894 if (is_artificial == false)
1895 {
1896 Type *member_type = ResolveTypeUID(encoding_uid);
Sean Callananfa3ab452012-12-14 00:54:13 +00001897
Jim Inghame3ae82a2011-11-12 01:36:43 +00001898 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001899 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001900 {
Sean Callanan751aac62012-03-29 19:07:06 +00001901 if (member_type)
1902 {
1903 if (accessibility == eAccessNone)
1904 accessibility = default_accessibility;
1905 member_accessibilities.push_back(accessibility);
Sean Callananfaa0bb32012-12-05 23:37:14 +00001906
1907 BitfieldInfo this_field_info;
1908
1909 this_field_info.bit_size = bit_size;
1910
1911 if (member_byte_offset != UINT32_MAX || bit_size != 0)
Greg Clayton88bc7f32012-11-06 00:20:41 +00001912 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00001913 /////////////////////////////////////////////////////////////
1914 // How to locate a field given the DWARF debug information
1915 //
1916 // AT_byte_size indicates the size of the word in which the
1917 // bit offset must be interpreted.
1918 //
1919 // AT_data_member_location indicates the byte offset of the
1920 // word from the base address of the structure.
1921 //
1922 // AT_bit_offset indicates how many bits into the word
1923 // (according to the host endianness) the low-order bit of
1924 // the field starts. AT_bit_offset can be negative.
1925 //
1926 // AT_bit_size indicates the size of the field in bits.
1927 /////////////////////////////////////////////////////////////
1928
1929 this_field_info.bit_offset = 0;
1930
1931 this_field_info.bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1932
1933 if (GetObjectFile()->GetByteOrder() == eByteOrderLittle)
1934 {
1935 this_field_info.bit_offset += byte_size * 8;
1936 this_field_info.bit_offset -= (bit_offset + bit_size);
1937 }
1938 else
1939 {
1940 this_field_info.bit_offset += bit_offset;
1941 }
1942 }
1943
1944 // If the member to be emitted did not start on a character boundary and there is
1945 // empty space between the last field and this one, then we need to emit an
1946 // anonymous member filling up the space up to its start. There are three cases
1947 // here:
1948 //
1949 // 1 If the previous member ended on a character boundary, then we can emit an
1950 // anonymous member starting at the most recent character boundary.
1951 //
1952 // 2 If the previous member did not end on a character boundary and the distance
1953 // from the end of the previous member to the current member is less than a
1954 // word width, then we can emit an anonymous member starting right after the
1955 // previous member and right before this member.
1956 //
1957 // 3 If the previous member did not end on a character boundary and the distance
1958 // from the end of the previous member to the current member is greater than
1959 // or equal a word width, then we act as in Case 1.
1960
1961 const uint64_t character_width = 8;
1962 const uint64_t word_width = 32;
1963
1964 if (this_field_info.IsValid())
1965 {
1966 // Objective-C has invalid DW_AT_bit_offset values in older versions
1967 // of clang, so we have to be careful and only insert unnammed bitfields
Greg Clayton37c36e42012-11-27 00:59:26 +00001968 // if we have a new enough clang.
1969 bool detect_unnamed_bitfields = true;
Greg Clayton88bc7f32012-11-06 00:20:41 +00001970
Greg Clayton37c36e42012-11-27 00:59:26 +00001971 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus)
1972 detect_unnamed_bitfields = dwarf_cu->Supports_unnamed_objc_bitfields ();
1973
1974 if (detect_unnamed_bitfields)
Greg Clayton88bc7f32012-11-06 00:20:41 +00001975 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00001976 BitfieldInfo anon_field_info;
1977
1978 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned
Greg Clayton88bc7f32012-11-06 00:20:41 +00001979 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00001980 uint64_t last_field_end = 0;
Greg Clayton88bc7f32012-11-06 00:20:41 +00001981
Sean Callananfaa0bb32012-12-05 23:37:14 +00001982 if (last_field_info.IsValid())
1983 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
Greg Clayton88bc7f32012-11-06 00:20:41 +00001984
Sean Callananfaa0bb32012-12-05 23:37:14 +00001985 if (this_field_info.bit_offset != last_field_end)
1986 {
1987 if (((last_field_end % character_width) == 0) || // case 1
1988 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3
1989 {
1990 anon_field_info.bit_size = this_field_info.bit_offset % character_width;
1991 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size;
1992 }
1993 else // case 2
1994 {
1995 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end;
1996 anon_field_info.bit_offset = last_field_end;
1997 }
Greg Clayton88bc7f32012-11-06 00:20:41 +00001998 }
Greg Clayton88bc7f32012-11-06 00:20:41 +00001999 }
2000
Sean Callananfaa0bb32012-12-05 23:37:14 +00002001 if (anon_field_info.IsValid())
Greg Clayton88bc7f32012-11-06 00:20:41 +00002002 {
Greg Clayton88bc7f32012-11-06 00:20:41 +00002003 clang::FieldDecl *unnamed_bitfield_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
2004 NULL,
Sean Callananfaa0bb32012-12-05 23:37:14 +00002005 GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width),
Greg Clayton88bc7f32012-11-06 00:20:41 +00002006 accessibility,
Sean Callananfaa0bb32012-12-05 23:37:14 +00002007 anon_field_info.bit_size);
Greg Clayton88bc7f32012-11-06 00:20:41 +00002008
Sean Callananfaa0bb32012-12-05 23:37:14 +00002009 layout_info.field_offsets.insert(std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset));
Greg Clayton88bc7f32012-11-06 00:20:41 +00002010 }
2011 }
2012 }
Sean Callananfaa0bb32012-12-05 23:37:14 +00002013
Sean Callananfa3ab452012-12-14 00:54:13 +00002014 clang_type_t member_clang_type = member_type->GetClangLayoutType();
2015
2016 {
2017 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>).
2018 // If the current field is at the end of the structure, then there is definitely no room for extra
2019 // elements and we override the type to array[0].
2020
2021 clang_type_t member_array_element_type;
2022 uint64_t member_array_size;
2023 bool member_array_is_incomplete;
2024
2025 if (GetClangASTContext().IsArrayType(member_clang_type,
2026 &member_array_element_type,
2027 &member_array_size,
2028 &member_array_is_incomplete) &&
2029 !member_array_is_incomplete)
2030 {
2031 uint64_t parent_byte_size = parent_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, UINT64_MAX);
2032
2033 if (member_byte_offset >= parent_byte_size)
2034 {
2035 if (member_array_size != 1)
2036 {
2037 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which extends beyond the bounds of 0x%8.8" PRIx64,
2038 MakeUserID(die->GetOffset()),
2039 name,
2040 encoding_uid,
2041 MakeUserID(parent_die->GetOffset()));
2042 }
2043
Greg Clayton1c8ef472013-04-05 23:27:21 +00002044 member_clang_type = GetClangASTContext().CreateArrayType(member_array_element_type, 0, false);
Sean Callananfa3ab452012-12-14 00:54:13 +00002045 }
2046 }
2047 }
2048
Greg Clayton88bc7f32012-11-06 00:20:41 +00002049 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
Sean Callanan751aac62012-03-29 19:07:06 +00002050 name,
Sean Callananfa3ab452012-12-14 00:54:13 +00002051 member_clang_type,
Sean Callanan751aac62012-03-29 19:07:06 +00002052 accessibility,
2053 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00002054
Greg Claytond0029442013-03-27 01:48:02 +00002055 GetClangASTContext().SetMetadataAsUserID (field_decl, MakeUserID(die->GetOffset()));
Sean Callananfaa0bb32012-12-05 23:37:14 +00002056
2057 if (this_field_info.IsValid())
2058 {
2059 layout_info.field_offsets.insert(std::make_pair(field_decl, this_field_info.bit_offset));
2060 last_field_info = this_field_info;
2061 }
Sean Callanan5b26f272012-02-04 08:49:35 +00002062 }
2063 else
2064 {
Sean Callanan751aac62012-03-29 19:07:06 +00002065 if (name)
Daniel Malead01b2952012-11-29 21:49:15 +00002066 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
Sean Callanan751aac62012-03-29 19:07:06 +00002067 MakeUserID(die->GetOffset()),
2068 name,
2069 encoding_uid);
2070 else
Daniel Malead01b2952012-11-29 21:49:15 +00002071 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
Sean Callanan751aac62012-03-29 19:07:06 +00002072 MakeUserID(die->GetOffset()),
2073 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00002074 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002075 }
Sean Callanan751aac62012-03-29 19:07:06 +00002076
Jim Inghame3ae82a2011-11-12 01:36:43 +00002077 if (prop_name != NULL)
2078 {
Sean Callanan751aac62012-03-29 19:07:06 +00002079 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00002080
Sean Callanan751aac62012-03-29 19:07:06 +00002081 if (field_decl)
2082 {
2083 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
2084 assert (ivar_decl != NULL);
2085 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002086
Jim Ingham379397632012-10-27 02:54:13 +00002087 ClangASTMetadata metadata;
2088 metadata.SetUserID (MakeUserID(die->GetOffset()));
Sean Callanana0b80ab2012-06-04 22:28:05 +00002089 delayed_properties.push_back(DelayedAddObjCClassProperty(GetClangASTContext().getASTContext(),
2090 class_clang_type,
2091 prop_name,
2092 member_type->GetClangLayoutType(),
2093 ivar_decl,
2094 prop_setter_name,
2095 prop_getter_name,
2096 prop_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00002097 &metadata));
Sean Callanan60217122012-04-13 00:10:03 +00002098
Sean Callananad880762012-04-18 01:06:17 +00002099 if (ivar_decl)
Greg Claytond0029442013-03-27 01:48:02 +00002100 GetClangASTContext().SetMetadataAsUserID (ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00002101 }
Greg Clayton24739922010-10-13 03:15:28 +00002102 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002103 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00002104 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002105 }
2106 break;
2107
2108 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00002109 // Let the type parsing code handle this one for us.
2110 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002111 break;
2112
2113 case DW_TAG_inheritance:
2114 {
2115 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00002116 if (default_accessibility == eAccessNone)
2117 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002118 // TODO: implement DW_TAG_inheritance type parsing
2119 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00002120 const size_t num_attributes = die->GetAttributes (this,
2121 dwarf_cu,
2122 fixed_form_sizes,
2123 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002124 if (num_attributes > 0)
2125 {
2126 Declaration decl;
2127 DWARFExpression location;
2128 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00002129 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002130 bool is_virtual = false;
2131 bool is_base_of_class = true;
Greg Clayton2508b9b2012-11-01 23:20:02 +00002132 off_t member_byte_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002133 uint32_t i;
2134 for (i=0; i<num_attributes; ++i)
2135 {
2136 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2137 DWARFFormValue form_value;
2138 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2139 {
2140 switch (attr)
2141 {
2142 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2143 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2144 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2145 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton2508b9b2012-11-01 23:20:02 +00002146 case DW_AT_data_member_location:
2147 if (form_value.BlockData())
2148 {
2149 Value initialValue(0);
2150 Value memberOffset(0);
2151 const DataExtractor& debug_info_data = get_debug_info_data();
2152 uint32_t block_length = form_value.Unsigned();
2153 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
2154 if (DWARFExpression::Evaluate (NULL,
2155 NULL,
2156 NULL,
2157 NULL,
2158 NULL,
2159 debug_info_data,
2160 block_offset,
2161 block_length,
2162 eRegisterKindDWARF,
2163 &initialValue,
2164 memberOffset,
2165 NULL))
2166 {
2167 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
2168 }
2169 }
2170 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002171
2172 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00002173 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002174 break;
2175
Greg Clayton1c8ef472013-04-05 23:27:21 +00002176 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002177 default:
2178 case DW_AT_sibling:
2179 break;
2180 }
2181 }
2182 }
2183
Greg Clayton526e5af2010-11-13 03:52:47 +00002184 Type *base_class_type = ResolveTypeUID(encoding_uid);
2185 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002186
Greg Claytonf4ecaa52011-02-16 23:00:21 +00002187 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
2188 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002189 if (class_language == eLanguageTypeObjC)
2190 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00002191 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002192 }
2193 else
2194 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00002195 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00002196 accessibility,
2197 is_virtual,
2198 is_base_of_class));
Greg Clayton2508b9b2012-11-01 23:20:02 +00002199
2200 if (is_virtual)
2201 {
2202 layout_info.vbase_offsets.insert(std::make_pair(ClangASTType::GetAsCXXRecordDecl(class_clang_type),
2203 clang::CharUnits::fromQuantity(member_byte_offset)));
2204 }
2205 else
2206 {
2207 layout_info.base_offsets.insert(std::make_pair(ClangASTType::GetAsCXXRecordDecl(class_clang_type),
2208 clang::CharUnits::fromQuantity(member_byte_offset)));
2209 }
Greg Clayton9e409562010-07-28 02:04:09 +00002210 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002211 }
2212 }
2213 break;
2214
2215 default:
2216 break;
2217 }
2218 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00002219
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002220 return count;
2221}
2222
2223
2224clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00002225SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002226{
2227 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00002228 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002229 {
2230 DWARFCompileUnitSP cu_sp;
2231 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
2232 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00002233 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00002234 }
2235 return NULL;
2236}
2237
2238clang::DeclContext*
2239SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
2240{
Greg Clayton81c22f62011-10-19 18:09:39 +00002241 if (UserIDMatches(type_uid))
2242 return GetClangDeclContextForDIEOffset (sc, type_uid);
2243 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002244}
2245
2246Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00002247SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002248{
Greg Clayton81c22f62011-10-19 18:09:39 +00002249 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002250 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002251 DWARFDebugInfo* debug_info = DebugInfo();
2252 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00002253 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002254 DWARFCompileUnitSP cu_sp;
2255 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00002256 const bool assert_not_being_parsed = true;
2257 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00002258 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002259 }
2260 return NULL;
2261}
2262
Greg Claytoncab36a32011-12-08 05:16:30 +00002263Type*
2264SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00002265{
Greg Claytoncab36a32011-12-08 05:16:30 +00002266 if (die != NULL)
2267 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002268 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00002269 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002270 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002271 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
2272 die->GetOffset(),
2273 DW_TAG_value_to_name(die->Tag()),
2274 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00002275
Greg Claytoncab36a32011-12-08 05:16:30 +00002276 // We might be coming in in the middle of a type tree (a class
2277 // withing a class, an enum within a class), so parse any needed
2278 // parent DIEs before we get to this one...
2279 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2280 switch (decl_ctx_die->Tag())
2281 {
2282 case DW_TAG_structure_type:
2283 case DW_TAG_union_type:
2284 case DW_TAG_class_type:
2285 {
2286 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00002287 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002288 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002289 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
2290 die->GetOffset(),
2291 DW_TAG_value_to_name(die->Tag()),
2292 die->GetName(this, cu),
2293 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00002294//
2295// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
2296// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
2297// {
2298// if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002299// GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton219cf312012-03-30 00:51:13 +00002300// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
2301// die->GetOffset(),
2302// DW_TAG_value_to_name(die->Tag()),
2303// die->GetName(this, cu),
2304// decl_ctx_die->GetOffset());
2305// // Ask the type to complete itself if it already hasn't since if we
2306// // want a function (method or static) from a class, the class must
2307// // create itself and add it's own methods and class functions.
2308// if (parent_type)
2309// parent_type->GetClangFullType();
2310// }
Greg Claytoncab36a32011-12-08 05:16:30 +00002311 }
2312 break;
2313
2314 default:
2315 break;
2316 }
2317 return ResolveType (cu, die);
2318 }
2319 return NULL;
2320}
2321
Greg Clayton6beaaa62011-01-17 03:46:26 +00002322// This function is used when SymbolFileDWARFDebugMap owns a bunch of
2323// SymbolFileDWARF objects to detect if this DWARF file is the one that
2324// can resolve a clang_type.
2325bool
2326SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
2327{
2328 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2329 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
2330 return die != NULL;
2331}
2332
2333
Greg Clayton1be10fc2010-09-29 01:12:09 +00002334lldb::clang_type_t
2335SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
2336{
2337 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002338 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2339 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002340 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002341 {
2342 // We have already resolved this type...
2343 return clang_type;
2344 }
2345 // Once we start resolving this type, remove it from the forward declaration
2346 // map in case anyone child members or other types require this type to get resolved.
2347 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
2348 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002349 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00002350
Greg Clayton1be10fc2010-09-29 01:12:09 +00002351
Greg Clayton85ae2e12011-10-18 23:36:41 +00002352 // Disable external storage for this type so we don't get anymore
2353 // clang::ExternalASTSource queries for this type.
2354 ClangASTContext::SetHasExternalStorage (clang_type, false);
2355
Greg Clayton450e3f32010-10-12 02:24:53 +00002356 DWARFDebugInfo* debug_info = DebugInfo();
2357
Greg Clayton53eb1c22012-04-02 22:59:12 +00002358 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002359 Type *type = m_die_to_type.lookup (die);
2360
2361 const dw_tag_t tag = die->Tag();
2362
Greg Clayton5160ce52013-03-27 23:08:40 +00002363 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00002364 if (log)
Greg Claytonbaf95da2012-03-30 23:50:54 +00002365 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002366 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
Daniel Malead01b2952012-11-29 21:49:15 +00002367 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
Greg Claytond61c0fc2012-04-23 22:55:20 +00002368 MakeUserID(die->GetOffset()),
2369 DW_TAG_value_to_name(tag),
2370 type->GetName().AsCString());
Greg Claytonbaf95da2012-03-30 23:50:54 +00002371
Greg Claytonbaf95da2012-03-30 23:50:54 +00002372 }
Greg Clayton1be10fc2010-09-29 01:12:09 +00002373 assert (clang_type);
2374 DWARFDebugInfoEntry::Attributes attributes;
2375
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002376 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002377
2378 switch (tag)
2379 {
2380 case DW_TAG_structure_type:
2381 case DW_TAG_union_type:
2382 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002383 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002384 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002385
Greg Clayton1be10fc2010-09-29 01:12:09 +00002386 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002387 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002388 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002389
Sean Callanan77a1fd32012-02-27 20:07:01 +00002390 LanguageType class_language = eLanguageTypeUnknown;
2391 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
2392 if (is_objc_class)
Greg Clayton219cf312012-03-30 00:51:13 +00002393 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002394 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002395 // For objective C we don't start the definition when
2396 // the class is created.
2397 ast.StartTagDeclarationDefinition (clang_type);
2398 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002399
2400 int tag_decl_kind = -1;
2401 AccessType default_accessibility = eAccessNone;
2402 if (tag == DW_TAG_structure_type)
2403 {
2404 tag_decl_kind = clang::TTK_Struct;
2405 default_accessibility = eAccessPublic;
2406 }
2407 else if (tag == DW_TAG_union_type)
2408 {
2409 tag_decl_kind = clang::TTK_Union;
2410 default_accessibility = eAccessPublic;
2411 }
2412 else if (tag == DW_TAG_class_type)
2413 {
2414 tag_decl_kind = clang::TTK_Class;
2415 default_accessibility = eAccessPrivate;
2416 }
2417
Greg Clayton53eb1c22012-04-02 22:59:12 +00002418 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002419 std::vector<clang::CXXBaseSpecifier *> base_classes;
2420 std::vector<int> member_accessibilities;
2421 bool is_a_class = false;
2422 // Parse members and base classes first
2423 DWARFDIECollection member_function_dies;
Sean Callanana0b80ab2012-06-04 22:28:05 +00002424
2425 DelayedPropertyList delayed_properties;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002426 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002427 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002428 die,
2429 clang_type,
2430 class_language,
2431 base_classes,
2432 member_accessibilities,
2433 member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00002434 delayed_properties,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002435 default_accessibility,
2436 is_a_class,
2437 layout_info);
2438
2439 // Now parse any methods if there were any...
2440 size_t num_functions = member_function_dies.Size();
2441 if (num_functions > 0)
2442 {
2443 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002444 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002445 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002446 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002447 }
2448
2449 if (class_language == eLanguageTypeObjC)
2450 {
Greg Clayton84db9102012-03-26 23:03:23 +00002451 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(ast.getASTContext(), clang_type));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002452 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00002453 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002454
Sean Callanan77a1fd32012-02-27 20:07:01 +00002455 DIEArray method_die_offsets;
2456 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002457 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002458 if (m_apple_objc_ap.get())
2459 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
2460 }
2461 else
2462 {
2463 if (!m_indexed)
2464 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002465
Sean Callanan77a1fd32012-02-27 20:07:01 +00002466 ConstString class_name (class_str.c_str());
2467 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2468 }
2469
2470 if (!method_die_offsets.empty())
2471 {
2472 DWARFDebugInfo* debug_info = DebugInfo();
2473
2474 DWARFCompileUnit* method_cu = NULL;
2475 const size_t num_matches = method_die_offsets.size();
2476 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002477 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002478 const dw_offset_t die_offset = method_die_offsets[i];
2479 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2480
2481 if (method_die)
2482 ResolveType (method_cu, method_die);
2483 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002484 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002485 if (m_using_apple_tables)
2486 {
2487 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2488 die_offset, class_str.c_str());
2489 }
2490 }
2491 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002492 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00002493
2494 for (DelayedPropertyList::const_iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
2495 pi != pe;
2496 ++pi)
2497 pi->Finalize();
Greg Clayton450e3f32010-10-12 02:24:53 +00002498 }
2499 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002500
2501 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2502 // need to tell the clang type it is actually a class.
2503 if (class_language != eLanguageTypeObjC)
2504 {
2505 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2506 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2507 }
2508
2509 // Since DW_TAG_structure_type gets used for both classes
2510 // and structures, we may need to set any DW_TAG_member
2511 // fields to have a "private" access if none was specified.
2512 // When we parsed the child members we tracked that actual
2513 // accessibility value for each DW_TAG_member in the
2514 // "member_accessibilities" array. If the value for the
2515 // member is zero, then it was set to the "default_accessibility"
2516 // which for structs was "public". Below we correct this
2517 // by setting any fields to "private" that weren't correctly
2518 // set.
2519 if (is_a_class && !member_accessibilities.empty())
2520 {
2521 // This is a class and all members that didn't have
2522 // their access specified are private.
2523 ast.SetDefaultAccessForRecordFields (clang_type,
2524 eAccessPrivate,
2525 &member_accessibilities.front(),
2526 member_accessibilities.size());
2527 }
2528
2529 if (!base_classes.empty())
2530 {
2531 ast.SetBaseClassesForClassType (clang_type,
2532 &base_classes.front(),
2533 base_classes.size());
2534
2535 // Clang will copy each CXXBaseSpecifier in "base_classes"
2536 // so we have to free them all.
2537 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2538 base_classes.size());
2539 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002540 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002541 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002542
2543 ast.BuildIndirectFields (clang_type);
2544
Sean Callanan77a1fd32012-02-27 20:07:01 +00002545 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002546
Greg Clayton2508b9b2012-11-01 23:20:02 +00002547 if (!layout_info.field_offsets.empty() ||
2548 !layout_info.base_offsets.empty() ||
2549 !layout_info.vbase_offsets.empty() )
Greg Claytoncaab74e2012-01-28 00:48:57 +00002550 {
2551 if (type)
2552 layout_info.bit_size = type->GetByteSize() * 8;
2553 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002554 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Greg Clayton2508b9b2012-11-01 23:20:02 +00002555
2556 clang::CXXRecordDecl *record_decl = ClangASTType::GetAsCXXRecordDecl(clang_type);
2557 if (record_decl)
Greg Claytoncaab74e2012-01-28 00:48:57 +00002558 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002559 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002560 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002561 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00002562 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) caching layout info for record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])",
Greg Claytoncaab74e2012-01-28 00:48:57 +00002563 clang_type,
2564 record_decl,
2565 layout_info.bit_size,
2566 layout_info.alignment,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002567 (uint32_t)layout_info.field_offsets.size(),
2568 (uint32_t)layout_info.base_offsets.size(),
2569 (uint32_t)layout_info.vbase_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002570
Greg Clayton2508b9b2012-11-01 23:20:02 +00002571 uint32_t idx;
2572 {
Greg Clayton821ac6d2012-01-28 02:22:27 +00002573 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
Greg Clayton2508b9b2012-11-01 23:20:02 +00002574 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002575 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002576 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002577 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }",
Greg Clayton821ac6d2012-01-28 02:22:27 +00002578 clang_type,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002579 idx,
Greg Clayton821ac6d2012-01-28 02:22:27 +00002580 (uint32_t)pos->second,
2581 pos->first->getNameAsString().c_str());
2582 }
Greg Clayton2508b9b2012-11-01 23:20:02 +00002583 }
2584
2585 {
2586 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos, base_end = layout_info.base_offsets.end();
2587 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx)
2588 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002589 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002590 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }",
2591 clang_type,
2592 idx,
2593 (uint32_t)base_pos->second.getQuantity(),
2594 base_pos->first->getNameAsString().c_str());
2595 }
2596 }
2597 {
2598 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos, vbase_end = layout_info.vbase_offsets.end();
2599 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx)
2600 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002601 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002602 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }",
2603 clang_type,
2604 idx,
2605 (uint32_t)vbase_pos->second.getQuantity(),
2606 vbase_pos->first->getNameAsString().c_str());
2607 }
2608 }
Greg Clayton821ac6d2012-01-28 02:22:27 +00002609 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002610 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2611 }
2612 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002613 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002614
Greg Claytonc93237c2010-10-01 20:48:32 +00002615 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002616
2617 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002618 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002619 if (die->HasChildren())
2620 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002621 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Greg Clayton3e067532013-03-05 23:54:39 +00002622 bool is_signed = false;
2623 ast.IsIntegerType(clang_type, is_signed);
2624 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002625 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002626 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002627 return clang_type;
2628
2629 default:
2630 assert(false && "not a forward clang type decl!");
2631 break;
2632 }
2633 return NULL;
2634}
2635
Greg Claytonc685f8e2010-09-15 04:15:46 +00002636Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002637SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002638{
2639 if (type_die != NULL)
2640 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002641 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002642
Greg Claytonc685f8e2010-09-15 04:15:46 +00002643 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002644 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002645
Greg Clayton24739922010-10-13 03:15:28 +00002646 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002647 {
2648 if (type != DIE_IS_BEING_PARSED)
2649 return type;
2650
2651 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002652 type_die->GetOffset(),
2653 DW_TAG_value_to_name(type_die->Tag()),
2654 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002655
2656 }
2657 else
2658 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002659 }
2660 return NULL;
2661}
2662
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002663CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002664SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002665{
2666 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002667 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002668 {
2669 // The symbol vendor doesn't know about this compile unit, we
2670 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002671 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002672 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002673 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002674}
2675
2676bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002677SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002678{
Greg Clayton72310352013-02-23 04:12:47 +00002679 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002680 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002681 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002682
Greg Clayton81c22f62011-10-19 18:09:39 +00002683 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002684 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002685 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002686
2687 if (sc.function)
2688 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002689 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002690 return true;
2691 }
2692
2693 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002694}
2695
2696uint32_t
2697SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2698{
2699 Timer scoped_timer(__PRETTY_FUNCTION__,
Daniel Malead01b2952012-11-29 21:49:15 +00002700 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002701 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002702 so_addr.GetOffset(),
2703 resolve_scope);
2704 uint32_t resolved = 0;
2705 if (resolve_scope & ( eSymbolContextCompUnit |
2706 eSymbolContextFunction |
2707 eSymbolContextBlock |
2708 eSymbolContextLineEntry))
2709 {
2710 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2711
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002712 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002713 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002714 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002715 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002716 if (cu_offset != DW_INVALID_OFFSET)
2717 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002718 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002719 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2720 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002721 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002722 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002723 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002724 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002725 resolved |= eSymbolContextCompUnit;
2726
Greg Clayton6ab80132012-12-12 17:30:52 +00002727 bool force_check_line_table = false;
Greg Clayton526a4ae2012-05-16 22:09:01 +00002728 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2729 {
2730 DWARFDebugInfoEntry *function_die = NULL;
2731 DWARFDebugInfoEntry *block_die = NULL;
2732 if (resolve_scope & eSymbolContextBlock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002733 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002734 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
2735 }
2736 else
2737 {
2738 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
2739 }
2740
2741 if (function_die != NULL)
2742 {
2743 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2744 if (sc.function == NULL)
2745 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2746 }
2747 else
2748 {
2749 // We might have had a compile unit that had discontiguous
2750 // address ranges where the gaps are symbols that don't have
2751 // any debug info. Discontiguous compile unit address ranges
2752 // should only happen when there aren't other functions from
2753 // other compile units in these gaps. This helps keep the size
2754 // of the aranges down.
Greg Clayton6ab80132012-12-12 17:30:52 +00002755 force_check_line_table = true;
Greg Clayton526a4ae2012-05-16 22:09:01 +00002756 }
2757
2758 if (sc.function != NULL)
2759 {
2760 resolved |= eSymbolContextFunction;
2761
2762 if (resolve_scope & eSymbolContextBlock)
2763 {
2764 Block& block = sc.function->GetBlock (true);
2765
2766 if (block_die != NULL)
2767 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2768 else
2769 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2770 if (sc.block)
2771 resolved |= eSymbolContextBlock;
2772 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002773 }
2774 }
Greg Clayton6ab80132012-12-12 17:30:52 +00002775
2776 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table)
2777 {
2778 LineTable *line_table = sc.comp_unit->GetLineTable();
2779 if (line_table != NULL)
2780 {
Greg Clayton9422dd62013-03-04 21:46:16 +00002781 // And address that makes it into this function should be in terms
2782 // of this debug file if there is no debug map, or it will be an
2783 // address in the .o file which needs to be fixed up to be in terms
2784 // of the debug map executable. Either way, calling FixupAddress()
2785 // will work for us.
2786 Address exe_so_addr (so_addr);
2787 if (FixupAddress(exe_so_addr))
Greg Clayton6ab80132012-12-12 17:30:52 +00002788 {
Greg Clayton9422dd62013-03-04 21:46:16 +00002789 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry))
Greg Clayton6ab80132012-12-12 17:30:52 +00002790 {
2791 resolved |= eSymbolContextLineEntry;
2792 }
2793 }
Greg Clayton6ab80132012-12-12 17:30:52 +00002794 }
2795 }
2796
2797 if (force_check_line_table && !(resolved & eSymbolContextLineEntry))
2798 {
2799 // We might have had a compile unit that had discontiguous
2800 // address ranges where the gaps are symbols that don't have
2801 // any debug info. Discontiguous compile unit address ranges
2802 // should only happen when there aren't other functions from
2803 // other compile units in these gaps. This helps keep the size
2804 // of the aranges down.
2805 sc.comp_unit = NULL;
2806 resolved &= ~eSymbolContextCompUnit;
2807 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002808 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002809 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002810 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002811 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
2812 cu_offset,
2813 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002814 }
2815 }
2816 }
2817 }
2818 }
2819 return resolved;
2820}
2821
2822
2823
2824uint32_t
2825SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2826{
2827 const uint32_t prev_size = sc_list.GetSize();
2828 if (resolve_scope & eSymbolContextCompUnit)
2829 {
2830 DWARFDebugInfo* debug_info = DebugInfo();
2831 if (debug_info)
2832 {
2833 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002834 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002835
Greg Clayton53eb1c22012-04-02 22:59:12 +00002836 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002837 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002838 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton1f746072012-08-29 21:13:06 +00002839 const bool full_match = file_spec.GetDirectory();
2840 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002841 if (check_inlines || file_spec_matches_cu_file_spec)
2842 {
2843 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002844 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002845 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002846 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002847 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002848
Greg Clayton526a4ae2012-05-16 22:09:01 +00002849 // If we are looking for inline functions only and we don't
2850 // find it in the support files, we are done.
2851 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002852 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002853 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
2854 if (file_idx == UINT32_MAX)
2855 continue;
2856 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002857
Greg Clayton526a4ae2012-05-16 22:09:01 +00002858 if (line != 0)
2859 {
2860 LineTable *line_table = sc.comp_unit->GetLineTable();
2861
2862 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002863 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002864 // We will have already looked up the file index if
2865 // we are searching for inline entries.
2866 if (!check_inlines)
2867 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002868
Greg Clayton526a4ae2012-05-16 22:09:01 +00002869 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002870 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002871 uint32_t found_line;
2872 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2873 found_line = sc.line_entry.line;
2874
2875 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002876 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002877 sc.function = NULL;
2878 sc.block = NULL;
2879 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002880 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002881 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2882 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002883 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002884 DWARFDebugInfoEntry *function_die = NULL;
2885 DWARFDebugInfoEntry *block_die = NULL;
2886 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002887
Greg Clayton526a4ae2012-05-16 22:09:01 +00002888 if (function_die != NULL)
2889 {
2890 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2891 if (sc.function == NULL)
2892 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2893 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002894
Greg Clayton526a4ae2012-05-16 22:09:01 +00002895 if (sc.function != NULL)
2896 {
2897 Block& block = sc.function->GetBlock (true);
2898
2899 if (block_die != NULL)
2900 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2901 else
2902 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2903 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002904 }
2905 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002906
Greg Clayton526a4ae2012-05-16 22:09:01 +00002907 sc_list.Append(sc);
2908 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2909 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002910 }
2911 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002912 else if (file_spec_matches_cu_file_spec && !check_inlines)
2913 {
2914 // only append the context if we aren't looking for inline call sites
2915 // by file and line and if the file spec matches that of the compile unit
2916 sc_list.Append(sc);
2917 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002918 }
2919 else if (file_spec_matches_cu_file_spec && !check_inlines)
2920 {
2921 // only append the context if we aren't looking for inline call sites
2922 // by file and line and if the file spec matches that of the compile unit
2923 sc_list.Append(sc);
2924 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002925
Greg Clayton526a4ae2012-05-16 22:09:01 +00002926 if (!check_inlines)
2927 break;
2928 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002929 }
2930 }
2931 }
2932 }
2933 return sc_list.GetSize() - prev_size;
2934}
2935
2936void
2937SymbolFileDWARF::Index ()
2938{
2939 if (m_indexed)
2940 return;
2941 m_indexed = true;
2942 Timer scoped_timer (__PRETTY_FUNCTION__,
2943 "SymbolFileDWARF::Index (%s)",
2944 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2945
2946 DWARFDebugInfo* debug_info = DebugInfo();
2947 if (debug_info)
2948 {
2949 uint32_t cu_idx = 0;
2950 const uint32_t num_compile_units = GetNumCompileUnits();
2951 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2952 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002953 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002954
Greg Clayton53eb1c22012-04-02 22:59:12 +00002955 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002956
Greg Clayton53eb1c22012-04-02 22:59:12 +00002957 dwarf_cu->Index (cu_idx,
2958 m_function_basename_index,
2959 m_function_fullname_index,
2960 m_function_method_index,
2961 m_function_selector_index,
2962 m_objc_class_selectors_index,
2963 m_global_index,
2964 m_type_index,
2965 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002966
2967 // Keep memory down by clearing DIEs if this generate function
2968 // caused them to be parsed
2969 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002970 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002971 }
2972
Greg Claytond4a2b372011-09-12 23:21:58 +00002973 m_function_basename_index.Finalize();
2974 m_function_fullname_index.Finalize();
2975 m_function_method_index.Finalize();
2976 m_function_selector_index.Finalize();
2977 m_objc_class_selectors_index.Finalize();
2978 m_global_index.Finalize();
2979 m_type_index.Finalize();
2980 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002981
Greg Clayton24739922010-10-13 03:15:28 +00002982#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002983 StreamFile s(stdout, false);
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00002984 s.Printf ("DWARF index for '%s':",
2985 GetObjectFile()->GetFileSpec().GetPath().c_str());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002986 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2987 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2988 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2989 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2990 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2991 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002992 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002993 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002994#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002995 }
2996}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002997
2998bool
2999SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
3000{
3001 if (namespace_decl == NULL)
3002 {
3003 // Invalid namespace decl which means we aren't matching only things
3004 // in this symbol file, so return true to indicate it matches this
3005 // symbol file.
3006 return true;
3007 }
3008
3009 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
3010
3011 if (namespace_ast == NULL)
3012 return true; // No AST in the "namespace_decl", return true since it
3013 // could then match any symbol file, including this one
3014
3015 if (namespace_ast == GetClangASTContext().getASTContext())
3016 return true; // The ASTs match, return true
3017
3018 // The namespace AST was valid, and it does not match...
Greg Clayton5160ce52013-03-27 23:08:40 +00003019 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Sean Callananc41e68b2011-10-13 21:08:11 +00003020
3021 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003022 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00003023
Greg Claytonbfe3dd42011-10-13 00:00:53 +00003024 return false;
3025}
3026
Greg Clayton2506a7a2011-10-12 23:34:26 +00003027bool
3028SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
3029 DWARFCompileUnit* cu,
3030 const DWARFDebugInfoEntry* die)
3031{
3032 // No namespace specified, so the answesr i
3033 if (namespace_decl == NULL)
3034 return true;
Sean Callananebe60672011-10-13 21:50:33 +00003035
Greg Clayton5160ce52013-03-27 23:08:40 +00003036 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00003037
Greg Clayton437a1352012-04-09 22:43:43 +00003038 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
3039 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00003040 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00003041 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00003042 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00003043
Greg Clayton2506a7a2011-10-12 23:34:26 +00003044 if (clang_namespace_decl)
3045 {
3046 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00003047 {
3048 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003049 GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00003050 return false;
Sean Callananebe60672011-10-13 21:50:33 +00003051 }
3052
Greg Clayton437a1352012-04-09 22:43:43 +00003053 if (clang_namespace_decl == die_clang_decl_ctx)
3054 return true;
3055 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00003056 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00003057 }
3058 else
3059 {
3060 // We have a namespace_decl that was not NULL but it contained
3061 // a NULL "clang::NamespaceDecl", so this means the global namespace
3062 // So as long the the contained decl context DIE isn't a namespace
3063 // we should be ok.
3064 if (decl_ctx_die->Tag() != DW_TAG_namespace)
3065 return true;
3066 }
3067 }
Sean Callananebe60672011-10-13 21:50:33 +00003068
3069 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003070 GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00003071
Greg Clayton2506a7a2011-10-12 23:34:26 +00003072 return false;
3073}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003074uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00003075SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003076{
Greg Clayton5160ce52013-03-27 23:08:40 +00003077 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003078
3079 if (log)
3080 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003081 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003082 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
3083 name.GetCString(),
3084 namespace_decl,
3085 append,
3086 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003087 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003088
3089 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3090 return 0;
3091
Greg Claytonc685f8e2010-09-15 04:15:46 +00003092 DWARFDebugInfo* info = DebugInfo();
3093 if (info == NULL)
3094 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003095
3096 // If we aren't appending the results to this list, then clear the list
3097 if (!append)
3098 variables.Clear();
3099
3100 // Remember how many variables are in the list before we search in case
3101 // we are appending the results to a variable list.
3102 const uint32_t original_size = variables.GetSize();
3103
Greg Claytond4a2b372011-09-12 23:21:58 +00003104 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003105
Greg Clayton97fbc342011-10-20 22:30:33 +00003106 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003107 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003108 if (m_apple_names_ap.get())
3109 {
3110 const char *name_cstr = name.GetCString();
3111 const char *base_name_start;
3112 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003113
Greg Clayton97fbc342011-10-20 22:30:33 +00003114 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
3115 base_name_start = name_cstr;
3116
3117 m_apple_names_ap->FindByName (base_name_start, die_offsets);
3118 }
Greg Clayton7f995132011-10-04 22:41:51 +00003119 }
3120 else
3121 {
3122 // Index the DWARF if we haven't already
3123 if (!m_indexed)
3124 Index ();
3125
3126 m_global_index.Find (name, die_offsets);
3127 }
3128
Greg Clayton437a1352012-04-09 22:43:43 +00003129 const size_t num_die_matches = die_offsets.size();
3130 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003131 {
Greg Clayton7f995132011-10-04 22:41:51 +00003132 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00003133 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00003134 assert (sc.module_sp);
3135
Greg Claytond4a2b372011-09-12 23:21:58 +00003136 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00003137 DWARFCompileUnit* dwarf_cu = NULL;
3138 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00003139 bool done = false;
3140 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00003141 {
3142 const dw_offset_t die_offset = die_offsets[i];
3143 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003144
Greg Clayton95d87902011-11-11 03:16:25 +00003145 if (die)
3146 {
Greg Clayton437a1352012-04-09 22:43:43 +00003147 switch (die->Tag())
3148 {
3149 default:
3150 case DW_TAG_subprogram:
3151 case DW_TAG_inlined_subroutine:
3152 case DW_TAG_try_block:
3153 case DW_TAG_catch_block:
3154 break;
3155
3156 case DW_TAG_variable:
3157 {
3158 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Greg Clayton437a1352012-04-09 22:43:43 +00003159
3160 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3161 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003162
Greg Clayton437a1352012-04-09 22:43:43 +00003163 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003164
Greg Clayton437a1352012-04-09 22:43:43 +00003165 if (variables.GetSize() - original_size >= max_matches)
3166 done = true;
3167 }
3168 break;
3169 }
Greg Clayton95d87902011-11-11 03:16:25 +00003170 }
3171 else
3172 {
3173 if (m_using_apple_tables)
3174 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003175 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
3176 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003177 }
3178 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003179 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003180 }
3181
3182 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003183 const uint32_t num_matches = variables.GetSize() - original_size;
3184 if (log && num_matches > 0)
3185 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003186 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003187 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
3188 name.GetCString(),
3189 namespace_decl,
3190 append,
3191 max_matches,
3192 num_matches);
3193 }
3194 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003195}
3196
3197uint32_t
3198SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
3199{
Greg Clayton5160ce52013-03-27 23:08:40 +00003200 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003201
3202 if (log)
3203 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003204 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003205 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
3206 regex.GetText(),
3207 append,
3208 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003209 }
3210
Greg Claytonc685f8e2010-09-15 04:15:46 +00003211 DWARFDebugInfo* info = DebugInfo();
3212 if (info == NULL)
3213 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003214
3215 // If we aren't appending the results to this list, then clear the list
3216 if (!append)
3217 variables.Clear();
3218
3219 // Remember how many variables are in the list before we search in case
3220 // we are appending the results to a variable list.
3221 const uint32_t original_size = variables.GetSize();
3222
Greg Clayton7f995132011-10-04 22:41:51 +00003223 DIEArray die_offsets;
3224
Greg Clayton97fbc342011-10-20 22:30:33 +00003225 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003226 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003227 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00003228 {
3229 DWARFMappedHash::DIEInfoArray hash_data_array;
3230 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
3231 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
3232 }
Greg Clayton7f995132011-10-04 22:41:51 +00003233 }
3234 else
3235 {
3236 // Index the DWARF if we haven't already
3237 if (!m_indexed)
3238 Index ();
3239
3240 m_global_index.Find (regex, die_offsets);
3241 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003242
Greg Claytonc685f8e2010-09-15 04:15:46 +00003243 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00003244 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00003245 assert (sc.module_sp);
3246
Greg Claytond4a2b372011-09-12 23:21:58 +00003247 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00003248 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003249 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003250 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003251 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003252 DWARFDebugInfo* debug_info = DebugInfo();
3253 for (size_t i=0; i<num_matches; ++i)
3254 {
3255 const dw_offset_t die_offset = die_offsets[i];
3256 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00003257
3258 if (die)
3259 {
3260 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003261
Greg Clayton95d87902011-11-11 03:16:25 +00003262 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003263
Greg Clayton95d87902011-11-11 03:16:25 +00003264 if (variables.GetSize() - original_size >= max_matches)
3265 break;
3266 }
3267 else
3268 {
3269 if (m_using_apple_tables)
3270 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003271 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
3272 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00003273 }
3274 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003275 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003276 }
3277
3278 // Return the number of variable that were appended to the list
3279 return variables.GetSize() - original_size;
3280}
3281
Greg Claytonaa044962011-10-13 00:59:38 +00003282
Jim Ingham4cda6e02011-10-07 22:23:45 +00003283bool
3284SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
3285 DWARFCompileUnit *&dwarf_cu,
3286 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00003287{
Greg Claytonaa044962011-10-13 00:59:38 +00003288 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3289 return ResolveFunction (dwarf_cu, die, sc_list);
3290}
3291
3292
3293bool
3294SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
3295 const DWARFDebugInfoEntry *die,
3296 SymbolContextList& sc_list)
3297{
Greg Clayton9e315582011-09-02 04:03:59 +00003298 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00003299
3300 if (die == NULL)
3301 return false;
3302
Jim Ingham4cda6e02011-10-07 22:23:45 +00003303 // If we were passed a die that is not a function, just return false...
3304 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
3305 return false;
3306
3307 const DWARFDebugInfoEntry* inlined_die = NULL;
3308 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00003309 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003310 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00003311
Jim Ingham4cda6e02011-10-07 22:23:45 +00003312 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003313 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003314 if (die->Tag() == DW_TAG_subprogram)
3315 break;
Greg Clayton9e315582011-09-02 04:03:59 +00003316 }
3317 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003318 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00003319 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003320 {
3321 Address addr;
3322 // Parse all blocks if needed
3323 if (inlined_die)
3324 {
Greg Clayton81c22f62011-10-19 18:09:39 +00003325 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00003326 assert (sc.block != NULL);
3327 if (sc.block->GetStartAddress (addr) == false)
3328 addr.Clear();
3329 }
3330 else
3331 {
3332 sc.block = NULL;
3333 addr = sc.function->GetAddressRange().GetBaseAddress();
3334 }
3335
3336 if (addr.IsValid())
3337 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003338 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00003339 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003340 }
3341 }
3342
Greg Claytonaa044962011-10-13 00:59:38 +00003343 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00003344}
3345
Greg Clayton7f995132011-10-04 22:41:51 +00003346void
3347SymbolFileDWARF::FindFunctions (const ConstString &name,
3348 const NameToDIE &name_to_die,
3349 SymbolContextList& sc_list)
3350{
Greg Claytond4a2b372011-09-12 23:21:58 +00003351 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003352 if (name_to_die.Find (name, die_offsets))
3353 {
3354 ParseFunctions (die_offsets, sc_list);
3355 }
3356}
3357
3358
3359void
3360SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3361 const NameToDIE &name_to_die,
3362 SymbolContextList& sc_list)
3363{
3364 DIEArray die_offsets;
3365 if (name_to_die.Find (regex, die_offsets))
3366 {
3367 ParseFunctions (die_offsets, sc_list);
3368 }
3369}
3370
3371
3372void
3373SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3374 const DWARFMappedHash::MemoryTable &memory_table,
3375 SymbolContextList& sc_list)
3376{
3377 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00003378 DWARFMappedHash::DIEInfoArray hash_data_array;
3379 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00003380 {
Greg Claytond1767f02011-12-08 02:13:16 +00003381 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00003382 ParseFunctions (die_offsets, sc_list);
3383 }
3384}
3385
3386void
3387SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
3388 SymbolContextList& sc_list)
3389{
3390 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003391 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00003392 {
Greg Clayton7f995132011-10-04 22:41:51 +00003393 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00003394
3395 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003396 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00003397 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003398 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00003399 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003400 }
3401 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00003402}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003403
Jim Ingham4cda6e02011-10-07 22:23:45 +00003404bool
3405SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
3406 const DWARFCompileUnit *dwarf_cu,
3407 uint32_t name_type_mask,
3408 const char *partial_name,
3409 const char *base_name_start,
3410 const char *base_name_end)
3411{
Greg Claytonfbea0f62012-11-15 18:05:43 +00003412 // If we are looking only for methods, throw away all the ones that are or aren't in C++ classes:
3413 if (name_type_mask == eFunctionNameTypeMethod || name_type_mask == eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003414 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003415 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
3416 if (!containing_decl_ctx)
3417 return false;
3418
3419 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
3420
Greg Claytonfbea0f62012-11-15 18:05:43 +00003421 if (name_type_mask == eFunctionNameTypeMethod)
3422 {
3423 if (is_cxx_method == false)
3424 return false;
3425 }
3426
3427 if (name_type_mask == eFunctionNameTypeBase)
3428 {
3429 if (is_cxx_method == true)
3430 return false;
3431 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003432 }
3433
3434 // Now we need to check whether the name we got back for this type matches the extra specifications
3435 // that were in the name we're looking up:
3436 if (base_name_start != partial_name || *base_name_end != '\0')
3437 {
3438 // First see if the stuff to the left matches the full name. To do that let's see if
3439 // we can pull out the mips linkage name attribute:
3440
3441 Mangled best_name;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003442 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonfbea0f62012-11-15 18:05:43 +00003443 DWARFFormValue form_value;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003444 die->GetAttributes(this, dwarf_cu, NULL, attributes);
3445 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
Greg Clayton71415542012-12-08 00:24:40 +00003446 if (idx == UINT32_MAX)
3447 idx = attributes.FindAttributeIndex(DW_AT_linkage_name);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003448 if (idx != UINT32_MAX)
3449 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003450 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3451 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003452 const char *mangled_name = form_value.AsCString(&get_debug_str_data());
3453 if (mangled_name)
3454 best_name.SetValue (ConstString(mangled_name), true);
3455 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003456 }
Greg Claytonfbea0f62012-11-15 18:05:43 +00003457
3458 if (!best_name)
3459 {
3460 idx = attributes.FindAttributeIndex(DW_AT_name);
3461 if (idx != UINT32_MAX && attributes.ExtractFormValueAtIndex(this, idx, form_value))
3462 {
3463 const char *name = form_value.AsCString(&get_debug_str_data());
3464 best_name.SetValue (ConstString(name), false);
3465 }
3466 }
3467
3468 if (best_name.GetDemangledName())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003469 {
3470 const char *demangled = best_name.GetDemangledName().GetCString();
3471 if (demangled)
3472 {
3473 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003474 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3475 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003476 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003477 else
3478 {
3479 // Sort out the case where our name is something like "Process::Destroy" and the match is
3480 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3481 // namespace boundaries...
3482
3483 if (partial_name[0] == ':' && partial_name[1] == ':')
3484 {
3485 // The partial name was already on a namespace boundary so all matches are good.
3486 return true;
3487 }
3488 else if (partial_in_demangled == demangled)
3489 {
3490 // They both start the same, so this is an good match.
3491 return true;
3492 }
3493 else
3494 {
3495 if (partial_in_demangled - demangled == 1)
3496 {
3497 // Only one character difference, can't be a namespace boundary...
3498 return false;
3499 }
3500 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3501 {
3502 // We are on a namespace boundary, so this is also good.
3503 return true;
3504 }
3505 else
3506 return false;
3507 }
3508 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003509 }
3510 }
3511 }
3512
3513 return true;
3514}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003515
Greg Clayton0c5cd902010-06-28 21:30:43 +00003516uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003517SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003518 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003519 uint32_t name_type_mask,
3520 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003521 bool append,
3522 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003523{
3524 Timer scoped_timer (__PRETTY_FUNCTION__,
3525 "SymbolFileDWARF::FindFunctions (name = '%s')",
3526 name.AsCString());
3527
Greg Clayton43fe2172013-04-03 02:00:15 +00003528 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
3529 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
3530
Greg Clayton5160ce52013-03-27 23:08:40 +00003531 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003532
3533 if (log)
3534 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003535 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003536 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3537 name.GetCString(),
3538 name_type_mask,
3539 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003540 }
3541
Greg Clayton0c5cd902010-06-28 21:30:43 +00003542 // If we aren't appending the results to this list, then clear the list
3543 if (!append)
3544 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003545
3546 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3547 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003548
3549 // If name is empty then we won't find anything.
3550 if (name.IsEmpty())
3551 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003552
3553 // Remember how many sc_list are in the list before we search in case
3554 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003555
Jim Ingham4cda6e02011-10-07 22:23:45 +00003556 const char *name_cstr = name.GetCString();
Greg Clayton43fe2172013-04-03 02:00:15 +00003557
3558 const uint32_t original_size = sc_list.GetSize();
3559
Jim Ingham4cda6e02011-10-07 22:23:45 +00003560 DWARFDebugInfo* info = DebugInfo();
3561 if (info == NULL)
3562 return 0;
3563
Greg Claytonaa044962011-10-13 00:59:38 +00003564 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton43fe2172013-04-03 02:00:15 +00003565 std::set<const DWARFDebugInfoEntry *> resolved_dies;
Greg Clayton97fbc342011-10-20 22:30:33 +00003566 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003567 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003568 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003569 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003570
3571 DIEArray die_offsets;
3572
3573 uint32_t num_matches = 0;
3574
Greg Clayton43fe2172013-04-03 02:00:15 +00003575 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003576 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003577 // If they asked for the full name, match what they typed. At some point we may
3578 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3579 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003580 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003581 for (uint32_t i = 0; i < num_matches; i++)
3582 {
Greg Clayton95d87902011-11-11 03:16:25 +00003583 const dw_offset_t die_offset = die_offsets[i];
3584 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003585 if (die)
3586 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003587 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3588 continue;
3589
Sean Callanan9df05fb2012-02-10 22:52:19 +00003590 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3591 continue;
3592
Greg Clayton43fe2172013-04-03 02:00:15 +00003593 if (resolved_dies.find(die) == resolved_dies.end())
3594 {
3595 if (ResolveFunction (dwarf_cu, die, sc_list))
3596 resolved_dies.insert(die);
3597 }
Greg Claytonaa044962011-10-13 00:59:38 +00003598 }
Greg Clayton95d87902011-11-11 03:16:25 +00003599 else
3600 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003601 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3602 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003603 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003604 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003605 }
Greg Clayton43fe2172013-04-03 02:00:15 +00003606
3607 if (name_type_mask & eFunctionNameTypeSelector)
3608 {
3609 if (namespace_decl && *namespace_decl)
3610 return 0; // no selectors in namespaces
Greg Clayton97fbc342011-10-20 22:30:33 +00003611
Greg Clayton43fe2172013-04-03 02:00:15 +00003612 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3613 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3614 // and if it is an ObjC method name, we're good.
Greg Clayton97fbc342011-10-20 22:30:33 +00003615
Greg Clayton43fe2172013-04-03 02:00:15 +00003616 for (uint32_t i = 0; i < num_matches; i++)
Greg Clayton97fbc342011-10-20 22:30:33 +00003617 {
Greg Clayton43fe2172013-04-03 02:00:15 +00003618 const dw_offset_t die_offset = die_offsets[i];
3619 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3620 if (die)
Greg Clayton97fbc342011-10-20 22:30:33 +00003621 {
Greg Clayton43fe2172013-04-03 02:00:15 +00003622 const char *die_name = die->GetName(this, dwarf_cu);
3623 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Greg Clayton97fbc342011-10-20 22:30:33 +00003624 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003625 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3626 continue;
3627
Greg Clayton43fe2172013-04-03 02:00:15 +00003628 if (resolved_dies.find(die) == resolved_dies.end())
3629 {
3630 if (ResolveFunction (dwarf_cu, die, sc_list))
3631 resolved_dies.insert(die);
3632 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003633 }
3634 }
Greg Clayton43fe2172013-04-03 02:00:15 +00003635 else
3636 {
3637 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3638 die_offset, name_cstr);
3639 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003640 }
Greg Clayton43fe2172013-04-03 02:00:15 +00003641 die_offsets.clear();
3642 }
3643
3644 if (((name_type_mask & eFunctionNameTypeMethod) && !namespace_decl) || name_type_mask & eFunctionNameTypeBase)
3645 {
3646 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3647 // extract the base name, look that up, and if there is any other information in the name we were
3648 // passed in we have to post-filter based on that.
3649
3650 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3651 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3652
3653 for (uint32_t i = 0; i < num_matches; i++)
3654 {
3655 const dw_offset_t die_offset = die_offsets[i];
3656 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3657 if (die)
3658 {
3659 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3660 continue;
3661
3662 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3663 continue;
3664
3665 // If we get to here, the die is good, and we should add it:
3666 if (resolved_dies.find(die) == resolved_dies.end())
3667 if (ResolveFunction (dwarf_cu, die, sc_list))
3668 {
3669 bool keep_die = true;
3670 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod))
3671 {
3672 // We are looking for either basenames or methods, so we need to
3673 // trim out the ones we won't want by looking at the type
3674 SymbolContext sc;
3675 if (sc_list.GetLastContext(sc))
3676 {
3677 if (sc.block)
3678 {
3679 // We have an inlined function
3680 }
3681 else if (sc.function)
3682 {
3683 Type *type = sc.function->GetType();
3684
3685 clang::DeclContext* decl_ctx = GetClangDeclContextContainingTypeUID (type->GetID());
3686 if (decl_ctx->isRecord())
3687 {
3688 if (name_type_mask & eFunctionNameTypeBase)
3689 {
3690 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
3691 keep_die = false;
3692 }
3693 }
3694 else
3695 {
3696 if (name_type_mask & eFunctionNameTypeMethod)
3697 {
3698 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
3699 keep_die = false;
3700 }
3701 }
3702 }
3703 }
3704 }
3705 if (keep_die)
3706 resolved_dies.insert(die);
3707 }
3708 }
3709 else
3710 {
3711 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3712 die_offset, name_cstr);
3713 }
3714 }
3715 die_offsets.clear();
Jim Ingham4cda6e02011-10-07 22:23:45 +00003716 }
3717 }
Greg Clayton7f995132011-10-04 22:41:51 +00003718 }
3719 else
3720 {
3721
3722 // Index the DWARF if we haven't already
3723 if (!m_indexed)
3724 Index ();
3725
Greg Clayton7f995132011-10-04 22:41:51 +00003726 if (name_type_mask & eFunctionNameTypeFull)
Matt Kopecd6089962013-05-10 17:53:48 +00003727 {
Greg Clayton7f995132011-10-04 22:41:51 +00003728 FindFunctions (name, m_function_fullname_index, sc_list);
3729
Matt Kopecd6089962013-05-10 17:53:48 +00003730 // Temporary workaround for global/anonymous namespace functions on linux
3731#if defined (__linux__)
3732 // If we didn't find any functions in the global namespace try
3733 // looking in the basename index but ignore any returned
3734 // functions that have a namespace (ie. mangled names starting with
3735 // '_ZN') but keep functions which have an anonymous namespace
3736 if (sc_list.GetSize() == 0)
3737 {
3738 SymbolContextList temp_sc_list;
3739 FindFunctions (name, m_function_basename_index, temp_sc_list);
3740 if (!namespace_decl)
3741 {
3742 SymbolContext sc;
3743 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++)
3744 {
3745 if (temp_sc_list.GetContextAtIndex(i, sc))
3746 {
Matt Kopeca189d492013-05-10 22:55:24 +00003747 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled);
3748 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Matt Kopec04e5d582013-05-14 19:00:41 +00003749 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
3750 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21))
Matt Kopecd6089962013-05-10 17:53:48 +00003751 {
3752 sc_list.Append(sc);
3753 }
3754 }
3755 }
3756 }
3757 }
3758#endif
3759 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003760 DIEArray die_offsets;
3761 DWARFCompileUnit *dwarf_cu = NULL;
3762
Greg Clayton43fe2172013-04-03 02:00:15 +00003763 if (name_type_mask & eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003764 {
Greg Clayton43fe2172013-04-03 02:00:15 +00003765 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003766 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003767 {
Greg Claytonaa044962011-10-13 00:59:38 +00003768 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3769 if (die)
3770 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003771 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3772 continue;
3773
Sean Callanan213fdb82011-10-13 01:49:10 +00003774 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3775 continue;
3776
Greg Claytonaa044962011-10-13 00:59:38 +00003777 // If we get to here, the die is good, and we should add it:
Greg Clayton43fe2172013-04-03 02:00:15 +00003778 if (resolved_dies.find(die) == resolved_dies.end())
3779 {
3780 if (ResolveFunction (dwarf_cu, die, sc_list))
3781 resolved_dies.insert(die);
3782 }
Greg Claytonaa044962011-10-13 00:59:38 +00003783 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003784 }
3785 die_offsets.clear();
3786 }
3787
Greg Clayton43fe2172013-04-03 02:00:15 +00003788 if (name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003789 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003790 if (namespace_decl && *namespace_decl)
3791 return 0; // no methods in namespaces
3792
Greg Clayton43fe2172013-04-03 02:00:15 +00003793 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003794 {
Greg Claytonaa044962011-10-13 00:59:38 +00003795 for (uint32_t i = 0; i < num_base; i++)
3796 {
3797 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3798 if (die)
3799 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003800 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3801 continue;
3802
Greg Claytonaa044962011-10-13 00:59:38 +00003803 // If we get to here, the die is good, and we should add it:
Greg Clayton43fe2172013-04-03 02:00:15 +00003804 if (resolved_dies.find(die) == resolved_dies.end())
3805 {
3806 if (ResolveFunction (dwarf_cu, die, sc_list))
3807 resolved_dies.insert(die);
3808 }
Greg Claytonaa044962011-10-13 00:59:38 +00003809 }
3810 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003811 }
3812 die_offsets.clear();
3813 }
Greg Clayton7f995132011-10-04 22:41:51 +00003814
Greg Clayton43fe2172013-04-03 02:00:15 +00003815 if ((name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003816 {
Greg Clayton7f995132011-10-04 22:41:51 +00003817 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003818 }
3819
Greg Clayton4d01ace2011-09-29 16:58:15 +00003820 }
3821
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003822 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003823 const uint32_t num_matches = sc_list.GetSize() - original_size;
3824
3825 if (log && num_matches > 0)
3826 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003827 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003828 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u",
3829 name.GetCString(),
3830 name_type_mask,
3831 append,
3832 num_matches);
3833 }
3834 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003835}
3836
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003837uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003838SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003839{
3840 Timer scoped_timer (__PRETTY_FUNCTION__,
3841 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3842 regex.GetText());
3843
Greg Clayton5160ce52013-03-27 23:08:40 +00003844 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003845
3846 if (log)
3847 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003848 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003849 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3850 regex.GetText(),
3851 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003852 }
3853
3854
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003855 // If we aren't appending the results to this list, then clear the list
3856 if (!append)
3857 sc_list.Clear();
3858
3859 // Remember how many sc_list are in the list before we search in case
3860 // we are appending the results to a variable list.
3861 uint32_t original_size = sc_list.GetSize();
3862
Greg Clayton97fbc342011-10-20 22:30:33 +00003863 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003864 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003865 if (m_apple_names_ap.get())
3866 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003867 }
3868 else
3869 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003870 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003871 if (!m_indexed)
3872 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003873
Greg Clayton7f995132011-10-04 22:41:51 +00003874 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003875
Greg Clayton7f995132011-10-04 22:41:51 +00003876 FindFunctions (regex, m_function_fullname_index, sc_list);
3877 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003878
3879 // Return the number of variable that were appended to the list
3880 return sc_list.GetSize() - original_size;
3881}
Jim Ingham318c9f22011-08-26 19:44:13 +00003882
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003883uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003884SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3885 const ConstString &name,
3886 const lldb_private::ClangNamespaceDecl *namespace_decl,
3887 bool append,
3888 uint32_t max_matches,
3889 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003890{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003891 DWARFDebugInfo* info = DebugInfo();
3892 if (info == NULL)
3893 return 0;
3894
Greg Clayton5160ce52013-03-27 23:08:40 +00003895 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003896
3897 if (log)
3898 {
Greg Clayton437a1352012-04-09 22:43:43 +00003899 if (namespace_decl)
3900 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003901 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003902 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
3903 name.GetCString(),
3904 namespace_decl->GetNamespaceDecl(),
3905 namespace_decl->GetQualifiedName().c_str(),
3906 append,
3907 max_matches);
3908 }
3909 else
3910 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003911 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003912 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
3913 name.GetCString(),
3914 append,
3915 max_matches);
3916 }
Greg Clayton21f2a492011-10-06 00:09:08 +00003917 }
3918
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003919 // If we aren't appending the results to this list, then clear the list
3920 if (!append)
3921 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003922
3923 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3924 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003925
Greg Claytond4a2b372011-09-12 23:21:58 +00003926 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003927
Greg Clayton97fbc342011-10-20 22:30:33 +00003928 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003929 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003930 if (m_apple_types_ap.get())
3931 {
3932 const char *name_cstr = name.GetCString();
3933 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3934 }
Greg Clayton7f995132011-10-04 22:41:51 +00003935 }
3936 else
3937 {
3938 if (!m_indexed)
3939 Index ();
3940
3941 m_type_index.Find (name, die_offsets);
3942 }
3943
Greg Clayton437a1352012-04-09 22:43:43 +00003944 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003945
Greg Clayton437a1352012-04-09 22:43:43 +00003946 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003947 {
Greg Clayton7f995132011-10-04 22:41:51 +00003948 const uint32_t initial_types_size = types.GetSize();
3949 DWARFCompileUnit* dwarf_cu = NULL;
3950 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003951 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003952 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003953 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003954 const dw_offset_t die_offset = die_offsets[i];
3955 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3956
Greg Clayton95d87902011-11-11 03:16:25 +00003957 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003958 {
Greg Clayton95d87902011-11-11 03:16:25 +00003959 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3960 continue;
3961
3962 Type *matching_type = ResolveType (dwarf_cu, die);
3963 if (matching_type)
3964 {
3965 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003966 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003967 if (types.GetSize() >= max_matches)
3968 break;
3969 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003970 }
Greg Clayton95d87902011-11-11 03:16:25 +00003971 else
3972 {
3973 if (m_using_apple_tables)
3974 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003975 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3976 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003977 }
3978 }
3979
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003980 }
Greg Clayton437a1352012-04-09 22:43:43 +00003981 const uint32_t num_matches = types.GetSize() - initial_types_size;
3982 if (log && num_matches)
3983 {
3984 if (namespace_decl)
3985 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003986 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003987 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
3988 name.GetCString(),
3989 namespace_decl->GetNamespaceDecl(),
3990 namespace_decl->GetQualifiedName().c_str(),
3991 append,
3992 max_matches,
3993 num_matches);
3994 }
3995 else
3996 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003997 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003998 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
3999 name.GetCString(),
4000 append,
4001 max_matches,
4002 num_matches);
4003 }
4004 }
4005 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004006 }
Greg Clayton7f995132011-10-04 22:41:51 +00004007 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004008}
4009
4010
Greg Clayton526e5af2010-11-13 03:52:47 +00004011ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00004012SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00004013 const ConstString &name,
4014 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004015{
Greg Clayton5160ce52013-03-27 23:08:40 +00004016 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00004017
4018 if (log)
4019 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004020 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00004021 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
4022 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00004023 }
Sean Callanan213fdb82011-10-13 01:49:10 +00004024
4025 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
4026 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00004027
Greg Clayton526e5af2010-11-13 03:52:47 +00004028 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004029 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00004030 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00004031 {
Greg Clayton7f995132011-10-04 22:41:51 +00004032 DIEArray die_offsets;
4033
Greg Clayton526e5af2010-11-13 03:52:47 +00004034 // Index if we already haven't to make sure the compile units
4035 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00004036 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004037 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004038 if (m_apple_namespaces_ap.get())
4039 {
4040 const char *name_cstr = name.GetCString();
4041 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
4042 }
Greg Clayton7f995132011-10-04 22:41:51 +00004043 }
4044 else
4045 {
4046 if (!m_indexed)
4047 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00004048
Greg Clayton7f995132011-10-04 22:41:51 +00004049 m_namespace_index.Find (name, die_offsets);
4050 }
Greg Claytond4a2b372011-09-12 23:21:58 +00004051
4052 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00004053 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00004054 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00004055 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00004056 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004057 DWARFDebugInfo* debug_info = DebugInfo();
4058 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00004059 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004060 const dw_offset_t die_offset = die_offsets[i];
4061 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00004062
Greg Clayton95d87902011-11-11 03:16:25 +00004063 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00004064 {
Greg Clayton95d87902011-11-11 03:16:25 +00004065 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
4066 continue;
4067
4068 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
4069 if (clang_namespace_decl)
4070 {
4071 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
4072 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00004073 break;
Greg Clayton95d87902011-11-11 03:16:25 +00004074 }
Greg Claytond4a2b372011-09-12 23:21:58 +00004075 }
Greg Clayton95d87902011-11-11 03:16:25 +00004076 else
4077 {
4078 if (m_using_apple_tables)
4079 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004080 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
4081 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004082 }
4083 }
4084
Greg Clayton526e5af2010-11-13 03:52:47 +00004085 }
4086 }
Greg Clayton96d7d742010-11-10 23:42:09 +00004087 }
Greg Clayton437a1352012-04-09 22:43:43 +00004088 if (log && namespace_decl.GetNamespaceDecl())
4089 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004090 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004091 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
4092 name.GetCString(),
4093 namespace_decl.GetNamespaceDecl(),
4094 namespace_decl.GetQualifiedName().c_str());
4095 }
4096
Greg Clayton526e5af2010-11-13 03:52:47 +00004097 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004098}
4099
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004100uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004101SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004102{
4103 // Remember how many sc_list are in the list before we search in case
4104 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004105 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004106
4107 const uint32_t num_die_offsets = die_offsets.size();
4108 // Parse all of the types we found from the pubtypes matches
4109 uint32_t i;
4110 uint32_t num_matches = 0;
4111 for (i = 0; i < num_die_offsets; ++i)
4112 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004113 Type *matching_type = ResolveTypeUID (die_offsets[i]);
4114 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004115 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004116 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00004117 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004118 ++num_matches;
4119 if (num_matches >= max_matches)
4120 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004121 }
4122 }
4123
4124 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004125 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004126}
4127
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004128
4129size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00004130SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
4131 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00004132 DWARFCompileUnit* dwarf_cu,
4133 const DWARFDebugInfoEntry *parent_die,
4134 bool skip_artificial,
4135 bool &is_static,
4136 TypeList* type_list,
4137 std::vector<clang_type_t>& function_param_types,
4138 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00004139 unsigned &type_quals,
4140 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004141{
4142 if (parent_die == NULL)
4143 return 0;
4144
Greg Claytond88d7592010-09-15 08:33:30 +00004145 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
4146
Greg Clayton7fedea22010-11-16 02:10:54 +00004147 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004148 const DWARFDebugInfoEntry *die;
4149 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4150 {
4151 dw_tag_t tag = die->Tag();
4152 switch (tag)
4153 {
4154 case DW_TAG_formal_parameter:
4155 {
4156 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004157 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004158 if (num_attributes > 0)
4159 {
4160 const char *name = NULL;
4161 Declaration decl;
4162 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00004163 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004164 // one of None, Auto, Register, Extern, Static, PrivateExtern
4165
Sean Callanane2ef6e32010-09-23 03:01:22 +00004166 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004167 uint32_t i;
4168 for (i=0; i<num_attributes; ++i)
4169 {
4170 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4171 DWARFFormValue form_value;
4172 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4173 {
4174 switch (attr)
4175 {
4176 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4177 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4178 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4179 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
4180 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00004181 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004182 case DW_AT_location:
4183 // if (form_value.BlockData())
4184 // {
4185 // const DataExtractor& debug_info_data = debug_info();
4186 // uint32_t block_length = form_value.Unsigned();
4187 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
4188 // }
4189 // else
4190 // {
4191 // }
4192 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004193 case DW_AT_const_value:
4194 case DW_AT_default_value:
4195 case DW_AT_description:
4196 case DW_AT_endianity:
4197 case DW_AT_is_optional:
4198 case DW_AT_segment:
4199 case DW_AT_variable_parameter:
4200 default:
4201 case DW_AT_abstract_origin:
4202 case DW_AT_sibling:
4203 break;
4204 }
4205 }
4206 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00004207
Greg Clayton0fffff52010-09-24 05:15:53 +00004208 bool skip = false;
4209 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004210 {
Greg Clayton0fffff52010-09-24 05:15:53 +00004211 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00004212 {
4213 // In order to determine if a C++ member function is
4214 // "const" we have to look at the const-ness of "this"...
4215 // Ugly, but that
4216 if (arg_idx == 0)
4217 {
Greg Claytonf0705c82011-10-22 03:33:13 +00004218 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00004219 {
Greg Clayton5113dc82011-08-12 06:47:54 +00004220 // Often times compilers omit the "this" name for the
4221 // specification DIEs, so we can't rely upon the name
4222 // being in the formal parameter DIE...
4223 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00004224 {
Greg Clayton5113dc82011-08-12 06:47:54 +00004225 Type *this_type = ResolveTypeUID (param_type_die_offset);
4226 if (this_type)
4227 {
4228 uint32_t encoding_mask = this_type->GetEncodingMask();
4229 if (encoding_mask & Type::eEncodingIsPointerUID)
4230 {
4231 is_static = false;
4232
4233 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
4234 type_quals |= clang::Qualifiers::Const;
4235 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
4236 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00004237 }
4238 }
4239 }
4240 }
4241 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004242 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00004243 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004244 else
4245 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004246
Greg Clayton0fffff52010-09-24 05:15:53 +00004247 // HACK: Objective C formal parameters "self" and "_cmd"
4248 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00004249 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
4250 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00004251 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004252 switch (comp_unit->GetLanguage())
4253 {
4254 case eLanguageTypeObjC:
4255 case eLanguageTypeObjC_plus_plus:
4256 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
4257 skip = true;
4258 break;
4259 default:
4260 break;
4261 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004262 }
4263 }
4264 }
4265
4266 if (!skip)
4267 {
4268 Type *type = ResolveTypeUID(param_type_die_offset);
4269 if (type)
4270 {
Greg Claytonc93237c2010-10-01 20:48:32 +00004271 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00004272
Greg Clayton42ce2f32011-12-12 21:50:19 +00004273 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
4274 type->GetClangForwardType(),
4275 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00004276 assert(param_var_decl);
4277 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00004278
Greg Claytond0029442013-03-27 01:48:02 +00004279 GetClangASTContext().SetMetadataAsUserID (param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00004280 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004281 }
4282 }
Greg Clayton7fedea22010-11-16 02:10:54 +00004283 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004284 }
4285 break;
4286
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00004287 case DW_TAG_template_type_parameter:
4288 case DW_TAG_template_value_parameter:
4289 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
4290 break;
4291
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004292 default:
4293 break;
4294 }
4295 }
Greg Clayton7fedea22010-11-16 02:10:54 +00004296 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004297}
4298
4299size_t
4300SymbolFileDWARF::ParseChildEnumerators
4301(
4302 const SymbolContext& sc,
Greg Clayton3e067532013-03-05 23:54:39 +00004303 clang_type_t enumerator_clang_type,
4304 bool is_signed,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004305 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00004306 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004307 const DWARFDebugInfoEntry *parent_die
4308)
4309{
4310 if (parent_die == NULL)
4311 return 0;
4312
4313 size_t enumerators_added = 0;
4314 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00004315 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
4316
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004317 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4318 {
4319 const dw_tag_t tag = die->Tag();
4320 if (tag == DW_TAG_enumerator)
4321 {
4322 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004323 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004324 if (num_child_attributes > 0)
4325 {
4326 const char *name = NULL;
4327 bool got_value = false;
4328 int64_t enum_value = 0;
4329 Declaration decl;
4330
4331 uint32_t i;
4332 for (i=0; i<num_child_attributes; ++i)
4333 {
4334 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4335 DWARFFormValue form_value;
4336 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4337 {
4338 switch (attr)
4339 {
4340 case DW_AT_const_value:
4341 got_value = true;
Greg Clayton3e067532013-03-05 23:54:39 +00004342 if (is_signed)
4343 enum_value = form_value.Signed();
4344 else
4345 enum_value = form_value.Unsigned();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004346 break;
4347
4348 case DW_AT_name:
4349 name = form_value.AsCString(&get_debug_str_data());
4350 break;
4351
4352 case DW_AT_description:
4353 default:
4354 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4355 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4356 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4357 case DW_AT_sibling:
4358 break;
4359 }
4360 }
4361 }
4362
4363 if (name && name[0] && got_value)
4364 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004365 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
4366 enumerator_clang_type,
4367 decl,
4368 name,
4369 enum_value,
4370 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004371 ++enumerators_added;
4372 }
4373 }
4374 }
4375 }
4376 return enumerators_added;
4377}
4378
4379void
4380SymbolFileDWARF::ParseChildArrayInfo
4381(
4382 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004383 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004384 const DWARFDebugInfoEntry *parent_die,
4385 int64_t& first_index,
4386 std::vector<uint64_t>& element_orders,
4387 uint32_t& byte_stride,
4388 uint32_t& bit_stride
4389)
4390{
4391 if (parent_die == NULL)
4392 return;
4393
4394 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00004395 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004396 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4397 {
4398 const dw_tag_t tag = die->Tag();
4399 switch (tag)
4400 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004401 case DW_TAG_subrange_type:
4402 {
4403 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004404 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004405 if (num_child_attributes > 0)
4406 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004407 uint64_t num_elements = 0;
4408 uint64_t lower_bound = 0;
4409 uint64_t upper_bound = 0;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004410 bool upper_bound_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004411 uint32_t i;
4412 for (i=0; i<num_child_attributes; ++i)
4413 {
4414 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4415 DWARFFormValue form_value;
4416 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4417 {
4418 switch (attr)
4419 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004420 case DW_AT_name:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004421 break;
4422
4423 case DW_AT_count:
4424 num_elements = form_value.Unsigned();
4425 break;
4426
4427 case DW_AT_bit_stride:
4428 bit_stride = form_value.Unsigned();
4429 break;
4430
4431 case DW_AT_byte_stride:
4432 byte_stride = form_value.Unsigned();
4433 break;
4434
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004435 case DW_AT_lower_bound:
4436 lower_bound = form_value.Unsigned();
4437 break;
4438
4439 case DW_AT_upper_bound:
Greg Clayton4ef877f2012-12-06 02:33:54 +00004440 upper_bound_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004441 upper_bound = form_value.Unsigned();
4442 break;
4443
4444 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004445 case DW_AT_abstract_origin:
4446 case DW_AT_accessibility:
4447 case DW_AT_allocated:
4448 case DW_AT_associated:
4449 case DW_AT_data_location:
4450 case DW_AT_declaration:
4451 case DW_AT_description:
4452 case DW_AT_sibling:
4453 case DW_AT_threads_scaled:
4454 case DW_AT_type:
4455 case DW_AT_visibility:
4456 break;
4457 }
4458 }
4459 }
4460
Greg Claytone6a07792012-12-05 21:59:39 +00004461 if (num_elements == 0)
4462 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004463 if (upper_bound_valid && upper_bound >= lower_bound)
Greg Claytone6a07792012-12-05 21:59:39 +00004464 num_elements = upper_bound - lower_bound + 1;
4465 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004466
Sean Callananec979ba2012-10-22 23:56:48 +00004467 element_orders.push_back (num_elements);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004468 }
4469 }
4470 break;
4471 }
4472 }
4473}
4474
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004475TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004476SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004477{
4478 TypeSP type_sp;
4479 if (die != NULL)
4480 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004481 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004482 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004483 if (type_ptr == NULL)
4484 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004485 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004486 assert (lldb_cu);
4487 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004488 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004489 }
4490 else if (type_ptr != DIE_IS_BEING_PARSED)
4491 {
4492 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004493 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004494 }
4495
4496 }
4497 return type_sp;
4498}
4499
4500clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004501SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004502{
4503 if (die_offset != DW_INVALID_OFFSET)
4504 {
4505 DWARFCompileUnitSP cu_sp;
4506 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004507 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004508 }
4509 return NULL;
4510}
4511
Sean Callanan72e49402011-08-05 23:43:37 +00004512clang::DeclContext *
4513SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4514{
4515 if (die_offset != DW_INVALID_OFFSET)
4516 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004517 DWARFDebugInfo* debug_info = DebugInfo();
4518 if (debug_info)
4519 {
4520 DWARFCompileUnitSP cu_sp;
4521 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4522 if (die)
4523 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4524 }
Sean Callanan72e49402011-08-05 23:43:37 +00004525 }
4526 return NULL;
4527}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004528
Greg Clayton96d7d742010-11-10 23:42:09 +00004529clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004530SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004531{
Greg Clayton030a2042011-10-14 21:34:45 +00004532 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004533 {
Greg Clayton030a2042011-10-14 21:34:45 +00004534 // See if we already parsed this namespace DIE and associated it with a
4535 // uniqued namespace declaration
4536 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4537 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004538 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004539 else
4540 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004541 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4542 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004543 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
Greg Clayton5160ce52013-03-27 23:08:40 +00004544 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton9d3d6882011-10-31 23:51:19 +00004545 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004546 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004547 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004548 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004549 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00004550 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
Greg Claytone38a5ed2012-01-05 03:57:59 +00004551 GetClangASTContext().getASTContext(),
4552 MakeUserID(die->GetOffset()),
4553 namespace_name,
4554 namespace_decl,
4555 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00004556 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004557 else
4558 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004559 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00004560 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
Greg Claytone38a5ed2012-01-05 03:57:59 +00004561 GetClangASTContext().getASTContext(),
4562 MakeUserID(die->GetOffset()),
4563 namespace_decl,
4564 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00004565 }
Greg Clayton030a2042011-10-14 21:34:45 +00004566 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004567
4568 if (namespace_decl)
4569 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4570 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004571 }
4572 }
4573 return NULL;
4574}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004575
4576clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004577SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004578{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004579 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4580 if (clang_decl_ctx)
4581 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004582 // If this DIE has a specification, or an abstract origin, then trace to those.
4583
Greg Claytoncab36a32011-12-08 05:16:30 +00004584 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004585 if (die_offset != DW_INVALID_OFFSET)
4586 return GetClangDeclContextForDIEOffset (sc, die_offset);
4587
Greg Claytoncab36a32011-12-08 05:16:30 +00004588 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004589 if (die_offset != DW_INVALID_OFFSET)
4590 return GetClangDeclContextForDIEOffset (sc, die_offset);
4591
Greg Clayton5160ce52013-03-27 23:08:40 +00004592 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00004593 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00004594 GetObjectFile()->GetModule()->LogMessage(log, "SymbolFileDWARF::GetClangDeclContextForDIE (die = 0x%8.8x) %s '%s'", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), die->GetName(this, cu));
Sean Callanan72e49402011-08-05 23:43:37 +00004595 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004596 bool assert_not_being_parsed = true;
4597 ResolveTypeUID (cu, die, assert_not_being_parsed);
4598
Greg Clayton5cf58b92011-10-05 22:22:08 +00004599 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4600
4601 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004602}
4603
4604clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004605SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004606{
Greg Claytonca512b32011-01-14 04:54:56 +00004607 if (m_clang_tu_decl == NULL)
4608 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004609
Greg Clayton2bc22f82011-09-30 03:20:47 +00004610 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004611
4612 if (decl_ctx_die_copy)
4613 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004614
4615 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004616 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004617
Greg Clayton2bc22f82011-09-30 03:20:47 +00004618 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4619 if (pos != m_die_to_decl_ctx.end())
4620 return pos->second;
4621
4622 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004623 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004624 case DW_TAG_compile_unit:
4625 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004626
Greg Clayton2bc22f82011-09-30 03:20:47 +00004627 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004628 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004629 break;
4630
4631 case DW_TAG_structure_type:
4632 case DW_TAG_union_type:
4633 case DW_TAG_class_type:
4634 {
4635 Type* type = ResolveType (cu, decl_ctx_die);
4636 if (type)
4637 {
4638 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4639 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004640 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004641 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4642 if (decl_ctx)
4643 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004644 }
4645 }
Greg Claytonca512b32011-01-14 04:54:56 +00004646 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004647 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004648
Greg Clayton2bc22f82011-09-30 03:20:47 +00004649 default:
4650 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004651 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004652 }
Greg Clayton7a345282010-11-09 23:46:37 +00004653 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004654}
4655
Greg Clayton2bc22f82011-09-30 03:20:47 +00004656
4657const DWARFDebugInfoEntry *
4658SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4659{
4660 if (cu && die)
4661 {
4662 const DWARFDebugInfoEntry * const decl_die = die;
4663
4664 while (die != NULL)
4665 {
4666 // If this is the original DIE that we are searching for a declaration
4667 // for, then don't look in the cache as we don't want our own decl
4668 // context to be our decl context...
4669 if (decl_die != die)
4670 {
4671 switch (die->Tag())
4672 {
4673 case DW_TAG_compile_unit:
4674 case DW_TAG_namespace:
4675 case DW_TAG_structure_type:
4676 case DW_TAG_union_type:
4677 case DW_TAG_class_type:
4678 return die;
4679
4680 default:
4681 break;
4682 }
4683 }
4684
4685 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4686 if (die_offset != DW_INVALID_OFFSET)
4687 {
4688 DWARFCompileUnit *spec_cu = cu;
4689 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4690 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4691 if (spec_die_decl_ctx_die)
4692 return spec_die_decl_ctx_die;
4693 }
4694
4695 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4696 if (die_offset != DW_INVALID_OFFSET)
4697 {
4698 DWARFCompileUnit *abs_cu = cu;
4699 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4700 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4701 if (abs_die_decl_ctx_die)
4702 return abs_die_decl_ctx_die;
4703 }
4704
4705 die = die->GetParent();
4706 }
4707 }
4708 return NULL;
4709}
4710
4711
Greg Clayton901c5ca2011-12-03 04:40:03 +00004712Symbol *
4713SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4714{
4715 Symbol *objc_class_symbol = NULL;
4716 if (m_obj_file)
4717 {
4718 Symtab *symtab = m_obj_file->GetSymtab();
4719 if (symtab)
4720 {
4721 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4722 eSymbolTypeObjCClass,
4723 Symtab::eDebugNo,
4724 Symtab::eVisibilityAny);
4725 }
4726 }
4727 return objc_class_symbol;
4728}
4729
Greg Claytonc7f03b62012-01-12 04:33:28 +00004730// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4731// then we can end up looking through all class types for a complete type and never find
4732// the full definition. We need to know if this attribute is supported, so we determine
4733// this here and cache th result. We also need to worry about the debug map DWARF file
4734// if we are doing darwin DWARF in .o file debugging.
4735bool
4736SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4737{
4738 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4739 {
4740 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4741 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4742 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4743 else
4744 {
4745 DWARFDebugInfo* debug_info = DebugInfo();
4746 const uint32_t num_compile_units = GetNumCompileUnits();
4747 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4748 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004749 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4750 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004751 {
4752 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4753 break;
4754 }
4755 }
4756 }
Greg Clayton1f746072012-08-29 21:13:06 +00004757 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004758 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4759 }
4760 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4761}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004762
4763// This function can be used when a DIE is found that is a forward declaration
4764// DIE and we want to try and find a type that has the complete definition.
4765TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004766SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4767 const ConstString &type_name,
4768 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004769{
4770
4771 TypeSP type_sp;
4772
Greg Claytonc7f03b62012-01-12 04:33:28 +00004773 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004774 return type_sp;
4775
4776 DIEArray die_offsets;
4777
4778 if (m_using_apple_tables)
4779 {
4780 if (m_apple_types_ap.get())
4781 {
4782 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004783 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004784 }
4785 }
4786 else
4787 {
4788 if (!m_indexed)
4789 Index ();
4790
4791 m_type_index.Find (type_name, die_offsets);
4792 }
4793
Greg Clayton901c5ca2011-12-03 04:40:03 +00004794 const size_t num_matches = die_offsets.size();
4795
Greg Clayton901c5ca2011-12-03 04:40:03 +00004796 DWARFCompileUnit* type_cu = NULL;
4797 const DWARFDebugInfoEntry* type_die = NULL;
4798 if (num_matches)
4799 {
4800 DWARFDebugInfo* debug_info = DebugInfo();
4801 for (size_t i=0; i<num_matches; ++i)
4802 {
4803 const dw_offset_t die_offset = die_offsets[i];
4804 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4805
4806 if (type_die)
4807 {
4808 bool try_resolving_type = false;
4809
4810 // Don't try and resolve the DIE we are looking for with the DIE itself!
4811 if (type_die != die)
4812 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004813 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004814 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004815 case DW_TAG_class_type:
4816 case DW_TAG_structure_type:
4817 try_resolving_type = true;
4818 break;
4819 default:
4820 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004821 }
4822 }
4823
4824 if (try_resolving_type)
4825 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004826 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004827 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004828
4829 if (try_resolving_type)
4830 {
4831 Type *resolved_type = ResolveType (type_cu, type_die, false);
4832 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4833 {
Daniel Malead01b2952012-11-29 21:49:15 +00004834 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ") from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n",
Greg Clayton901c5ca2011-12-03 04:40:03 +00004835 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004836 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton901c5ca2011-12-03 04:40:03 +00004837 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4838 MakeUserID(type_die->GetOffset()),
4839 MakeUserID(type_cu->GetOffset()));
4840
Greg Claytonc7f03b62012-01-12 04:33:28 +00004841 if (die)
4842 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004843 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004844 break;
4845 }
4846 }
4847 }
4848 }
4849 else
4850 {
4851 if (m_using_apple_tables)
4852 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004853 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4854 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004855 }
4856 }
4857
4858 }
4859 }
4860 return type_sp;
4861}
4862
Greg Claytona8022fa2012-04-24 21:22:41 +00004863
Greg Clayton80c26302012-02-05 06:12:47 +00004864//----------------------------------------------------------------------
4865// This function helps to ensure that the declaration contexts match for
4866// two different DIEs. Often times debug information will refer to a
4867// forward declaration of a type (the equivalent of "struct my_struct;".
4868// There will often be a declaration of that type elsewhere that has the
4869// full definition. When we go looking for the full type "my_struct", we
4870// will find one or more matches in the accelerator tables and we will
4871// then need to make sure the type was in the same declaration context
4872// as the original DIE. This function can efficiently compare two DIEs
4873// and will return true when the declaration context matches, and false
4874// when they don't.
4875//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004876bool
4877SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4878 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4879{
Greg Claytona8022fa2012-04-24 21:22:41 +00004880 if (die1 == die2)
4881 return true;
4882
4883#if defined (LLDB_CONFIGURATION_DEBUG)
4884 // You can't and shouldn't call this function with a compile unit from
4885 // two different SymbolFileDWARF instances.
4886 assert (DebugInfo()->ContainsCompileUnit (cu1));
4887 assert (DebugInfo()->ContainsCompileUnit (cu2));
4888#endif
4889
Greg Clayton890ff562012-02-02 05:48:16 +00004890 DWARFDIECollection decl_ctx_1;
4891 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004892 //The declaration DIE stack is a stack of the declaration context
4893 // DIEs all the way back to the compile unit. If a type "T" is
4894 // declared inside a class "B", and class "B" is declared inside
4895 // a class "A" and class "A" is in a namespace "lldb", and the
4896 // namespace is in a compile unit, there will be a stack of DIEs:
4897 //
4898 // [0] DW_TAG_class_type for "B"
4899 // [1] DW_TAG_class_type for "A"
4900 // [2] DW_TAG_namespace for "lldb"
4901 // [3] DW_TAG_compile_unit for the source file.
4902 //
4903 // We grab both contexts and make sure that everything matches
4904 // all the way back to the compiler unit.
4905
4906 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004907 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004908 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004909 // Make sure the context arrays have the same size, otherwise
4910 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004911 const size_t count1 = decl_ctx_1.Size();
4912 const size_t count2 = decl_ctx_2.Size();
4913 if (count1 != count2)
4914 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004915
4916 // Make sure the DW_TAG values match all the way back up the the
4917 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004918 const DWARFDebugInfoEntry *decl_ctx_die1;
4919 const DWARFDebugInfoEntry *decl_ctx_die2;
4920 size_t i;
4921 for (i=0; i<count1; i++)
4922 {
4923 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4924 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4925 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4926 return false;
4927 }
Greg Clayton890ff562012-02-02 05:48:16 +00004928#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004929
4930 // Make sure the top item in the decl context die array is always
4931 // DW_TAG_compile_unit. If it isn't then something went wrong in
4932 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004933 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004934
Greg Clayton890ff562012-02-02 05:48:16 +00004935#endif
4936 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004937 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004938 for (i=0; i<count1 - 1; i++)
4939 {
4940 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4941 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4942 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004943 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004944 // If the string was from a DW_FORM_strp, then the pointer will often
4945 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004946 if (name1 == name2)
4947 continue;
4948
4949 // Name pointers are not equal, so only compare the strings
4950 // if both are not NULL.
4951 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004952 {
Greg Clayton5569e642012-02-06 01:44:54 +00004953 // If the strings don't compare, we are done...
4954 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004955 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004956 }
4957 else
4958 {
4959 // One name was NULL while the other wasn't
4960 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004961 }
4962 }
Greg Clayton80c26302012-02-05 06:12:47 +00004963 // We made it through all of the checks and the declaration contexts
4964 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004965 return true;
4966}
Greg Clayton220a0072011-12-09 08:48:30 +00004967
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004968// This function can be used when a DIE is found that is a forward declaration
4969// DIE and we want to try and find a type that has the complete definition.
Greg Claytona8022fa2012-04-24 21:22:41 +00004970// "cu" and "die" must be from this SymbolFileDWARF
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004971TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004972SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
Greg Clayton7f995132011-10-04 22:41:51 +00004973 const DWARFDebugInfoEntry *die,
4974 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004975{
4976 TypeSP type_sp;
4977
Greg Claytona8022fa2012-04-24 21:22:41 +00004978#if defined (LLDB_CONFIGURATION_DEBUG)
4979 // You can't and shouldn't call this function with a compile unit from
4980 // another SymbolFileDWARF instance.
4981 assert (DebugInfo()->ContainsCompileUnit (cu));
4982#endif
4983
Greg Clayton1a65ae12011-01-25 23:55:37 +00004984 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004985 return type_sp;
4986
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00004987 std::string qualified_name;
4988
Greg Clayton5160ce52013-03-27 23:08:40 +00004989 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004990 if (log)
4991 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004992 die->GetQualifiedName(this, cu, qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00004993 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004994 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x (%s), name='%s')",
4995 die->GetOffset(),
4996 qualified_name.c_str(),
4997 type_name.GetCString());
4998 }
4999
Greg Clayton7f995132011-10-04 22:41:51 +00005000 DIEArray die_offsets;
5001
Greg Clayton97fbc342011-10-20 22:30:33 +00005002 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00005003 {
Greg Clayton97fbc342011-10-20 22:30:33 +00005004 if (m_apple_types_ap.get())
5005 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005006 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
5007 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
5008 if (has_tag && has_qualified_name_hash)
Greg Claytond1767f02011-12-08 02:13:16 +00005009 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005010 if (qualified_name.empty())
5011 die->GetQualifiedName(this, cu, qualified_name);
5012
5013 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name.c_str());
5014 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005015 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()");
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005016 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), die->Tag(), qualified_name_hash, die_offsets);
5017 }
5018 else if (has_tag > 1)
5019 {
5020 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005021 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()");
Greg Claytonae920b62012-01-06 00:17:16 +00005022 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00005023 }
5024 else
5025 {
5026 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
5027 }
Greg Clayton97fbc342011-10-20 22:30:33 +00005028 }
Greg Clayton7f995132011-10-04 22:41:51 +00005029 }
5030 else
5031 {
5032 if (!m_indexed)
5033 Index ();
5034
5035 m_type_index.Find (type_name, die_offsets);
5036 }
Greg Clayton7f995132011-10-04 22:41:51 +00005037
5038 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00005039
Greg Clayton934cb052011-12-03 00:27:05 +00005040 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00005041
5042 DWARFCompileUnit* type_cu = NULL;
5043 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00005044 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005045 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005046 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005047 for (size_t i=0; i<num_matches; ++i)
5048 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005049 const dw_offset_t die_offset = die_offsets[i];
5050 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005051
Greg Clayton95d87902011-11-11 03:16:25 +00005052 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005053 {
Greg Clayton934cb052011-12-03 00:27:05 +00005054 bool try_resolving_type = false;
5055
5056 // Don't try and resolve the DIE we are looking for with the DIE itself!
5057 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005058 {
Greg Clayton934cb052011-12-03 00:27:05 +00005059 const dw_tag_t type_die_tag = type_die->Tag();
5060 // Make sure the tags match
5061 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005062 {
Greg Clayton934cb052011-12-03 00:27:05 +00005063 // The tags match, lets try resolving this type
5064 try_resolving_type = true;
5065 }
5066 else
5067 {
5068 // The tags don't match, but we need to watch our for a
5069 // forward declaration for a struct and ("struct foo")
5070 // ends up being a class ("class foo { ... };") or
5071 // vice versa.
5072 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00005073 {
Greg Clayton934cb052011-12-03 00:27:05 +00005074 case DW_TAG_class_type:
5075 // We had a "class foo", see if we ended up with a "struct foo { ... };"
5076 try_resolving_type = (die_tag == DW_TAG_structure_type);
5077 break;
5078 case DW_TAG_structure_type:
5079 // We had a "struct foo", see if we ended up with a "class foo { ... };"
5080 try_resolving_type = (die_tag == DW_TAG_class_type);
5081 break;
5082 default:
5083 // Tags don't match, don't event try to resolve
5084 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00005085 break;
5086 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005087 }
5088 }
Greg Clayton934cb052011-12-03 00:27:05 +00005089
5090 if (try_resolving_type)
5091 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00005092 if (log)
5093 {
5094 std::string qualified_name;
5095 type_die->GetQualifiedName(this, cu, qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00005096 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton9bbddbf2012-04-20 20:35:47 +00005097 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') trying die=0x%8.8x (%s)",
5098 die->GetOffset(),
5099 type_name.GetCString(),
5100 type_die->GetOffset(),
5101 qualified_name.c_str());
5102 }
5103
Greg Clayton890ff562012-02-02 05:48:16 +00005104 // Make sure the decl contexts match all the way up
5105 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00005106 {
Greg Clayton890ff562012-02-02 05:48:16 +00005107 Type *resolved_type = ResolveType (type_cu, type_die, false);
5108 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
5109 {
Daniel Malead01b2952012-11-29 21:49:15 +00005110 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ") from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n",
Greg Clayton890ff562012-02-02 05:48:16 +00005111 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00005112 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton890ff562012-02-02 05:48:16 +00005113 m_obj_file->GetFileSpec().GetFilename().AsCString(),
5114 MakeUserID(type_die->GetOffset()),
5115 MakeUserID(type_cu->GetOffset()));
5116
5117 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00005118 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00005119 break;
5120 }
Greg Clayton934cb052011-12-03 00:27:05 +00005121 }
5122 }
Greg Clayton9bbddbf2012-04-20 20:35:47 +00005123 else
5124 {
5125 if (log)
5126 {
5127 std::string qualified_name;
5128 type_die->GetQualifiedName(this, cu, qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00005129 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton9bbddbf2012-04-20 20:35:47 +00005130 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') ignoring die=0x%8.8x (%s)",
5131 die->GetOffset(),
5132 type_name.GetCString(),
5133 type_die->GetOffset(),
5134 qualified_name.c_str());
5135 }
5136 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005137 }
Greg Clayton95d87902011-11-11 03:16:25 +00005138 else
5139 {
5140 if (m_using_apple_tables)
5141 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005142 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
5143 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00005144 }
5145 }
5146
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005147 }
5148 }
5149 return type_sp;
5150}
5151
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005152TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00005153SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
5154{
5155 TypeSP type_sp;
5156
5157 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
5158 if (dwarf_decl_ctx_count > 0)
5159 {
5160 const ConstString type_name(dwarf_decl_ctx[0].name);
5161 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
5162
5163 if (type_name)
5164 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005165 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
Greg Claytona8022fa2012-04-24 21:22:41 +00005166 if (log)
5167 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005168 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00005169 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
5170 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5171 dwarf_decl_ctx.GetQualifiedName());
5172 }
5173
5174 DIEArray die_offsets;
5175
5176 if (m_using_apple_tables)
5177 {
5178 if (m_apple_types_ap.get())
5179 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005180 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
5181 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
5182 if (has_tag && has_qualified_name_hash)
Greg Claytona8022fa2012-04-24 21:22:41 +00005183 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005184 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
5185 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name);
5186 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005187 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()");
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005188 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets);
5189 }
5190 else if (has_tag)
5191 {
5192 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005193 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()");
Greg Claytona8022fa2012-04-24 21:22:41 +00005194 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
5195 }
5196 else
5197 {
5198 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
5199 }
5200 }
5201 }
5202 else
5203 {
5204 if (!m_indexed)
5205 Index ();
5206
5207 m_type_index.Find (type_name, die_offsets);
5208 }
5209
5210 const size_t num_matches = die_offsets.size();
5211
5212
5213 DWARFCompileUnit* type_cu = NULL;
5214 const DWARFDebugInfoEntry* type_die = NULL;
5215 if (num_matches)
5216 {
5217 DWARFDebugInfo* debug_info = DebugInfo();
5218 for (size_t i=0; i<num_matches; ++i)
5219 {
5220 const dw_offset_t die_offset = die_offsets[i];
5221 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
5222
5223 if (type_die)
5224 {
5225 bool try_resolving_type = false;
5226
5227 // Don't try and resolve the DIE we are looking for with the DIE itself!
5228 const dw_tag_t type_tag = type_die->Tag();
5229 // Make sure the tags match
5230 if (type_tag == tag)
5231 {
5232 // The tags match, lets try resolving this type
5233 try_resolving_type = true;
5234 }
5235 else
5236 {
5237 // The tags don't match, but we need to watch our for a
5238 // forward declaration for a struct and ("struct foo")
5239 // ends up being a class ("class foo { ... };") or
5240 // vice versa.
5241 switch (type_tag)
5242 {
5243 case DW_TAG_class_type:
5244 // We had a "class foo", see if we ended up with a "struct foo { ... };"
5245 try_resolving_type = (tag == DW_TAG_structure_type);
5246 break;
5247 case DW_TAG_structure_type:
5248 // We had a "struct foo", see if we ended up with a "class foo { ... };"
5249 try_resolving_type = (tag == DW_TAG_class_type);
5250 break;
5251 default:
5252 // Tags don't match, don't event try to resolve
5253 // using this type whose name matches....
5254 break;
5255 }
5256 }
5257
5258 if (try_resolving_type)
5259 {
5260 DWARFDeclContext type_dwarf_decl_ctx;
5261 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
5262
5263 if (log)
5264 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005265 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00005266 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
5267 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5268 dwarf_decl_ctx.GetQualifiedName(),
5269 type_die->GetOffset(),
5270 type_dwarf_decl_ctx.GetQualifiedName());
5271 }
5272
5273 // Make sure the decl contexts match all the way up
5274 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
5275 {
5276 Type *resolved_type = ResolveType (type_cu, type_die, false);
5277 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
5278 {
5279 type_sp = resolved_type->shared_from_this();
5280 break;
5281 }
5282 }
5283 }
5284 else
5285 {
5286 if (log)
5287 {
5288 std::string qualified_name;
5289 type_die->GetQualifiedName(this, type_cu, qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00005290 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00005291 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
5292 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5293 dwarf_decl_ctx.GetQualifiedName(),
5294 type_die->GetOffset(),
5295 qualified_name.c_str());
5296 }
5297 }
5298 }
5299 else
5300 {
5301 if (m_using_apple_tables)
5302 {
5303 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
5304 die_offset, type_name.GetCString());
5305 }
5306 }
5307
5308 }
5309 }
5310 }
5311 }
5312 return type_sp;
5313}
5314
Greg Clayton4116e932012-05-15 02:33:01 +00005315bool
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005316SymbolFileDWARF::CopyUniqueClassMethodTypes (SymbolFileDWARF *src_symfile,
Sean Callanan2367f8a2013-02-26 01:12:25 +00005317 Type *class_type,
Greg Clayton4116e932012-05-15 02:33:01 +00005318 DWARFCompileUnit* src_cu,
5319 const DWARFDebugInfoEntry *src_class_die,
5320 DWARFCompileUnit* dst_cu,
Sean Callanan2367f8a2013-02-26 01:12:25 +00005321 const DWARFDebugInfoEntry *dst_class_die,
5322 llvm::SmallVectorImpl <const DWARFDebugInfoEntry *> &failures)
Greg Clayton4116e932012-05-15 02:33:01 +00005323{
5324 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
5325 return false;
5326 if (src_class_die->Tag() != dst_class_die->Tag())
5327 return false;
5328
5329 // We need to complete the class type so we can get all of the method types
5330 // parsed so we can then unique those types to their equivalent counterparts
5331 // in "dst_cu" and "dst_class_die"
5332 class_type->GetClangFullType();
5333
5334 const DWARFDebugInfoEntry *src_die;
5335 const DWARFDebugInfoEntry *dst_die;
5336 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
5337 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
Greg Clayton65ec1032012-11-12 21:27:20 +00005338 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die_artificial;
5339 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die_artificial;
Greg Clayton4116e932012-05-15 02:33:01 +00005340 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
5341 {
5342 if (src_die->Tag() == DW_TAG_subprogram)
5343 {
Greg Clayton84afacd2012-11-07 23:09:32 +00005344 // Make sure this is a declaration and not a concrete instance by looking
5345 // for DW_AT_declaration set to 1. Sometimes concrete function instances
5346 // are placed inside the class definitions and shouldn't be included in
5347 // the list of things are are tracking here.
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005348 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_declaration, 0) == 1)
Greg Clayton84afacd2012-11-07 23:09:32 +00005349 {
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005350 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
Greg Clayton84afacd2012-11-07 23:09:32 +00005351 if (src_name)
Greg Clayton65ec1032012-11-12 21:27:20 +00005352 {
5353 ConstString src_const_name(src_name);
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005354 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_artificial, 0))
Greg Clayton65ec1032012-11-12 21:27:20 +00005355 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die);
5356 else
5357 src_name_to_die.Append(src_const_name.GetCString(), src_die);
5358 }
Greg Clayton84afacd2012-11-07 23:09:32 +00005359 }
Greg Clayton4116e932012-05-15 02:33:01 +00005360 }
5361 }
5362 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
5363 {
5364 if (dst_die->Tag() == DW_TAG_subprogram)
5365 {
Greg Clayton84afacd2012-11-07 23:09:32 +00005366 // Make sure this is a declaration and not a concrete instance by looking
5367 // for DW_AT_declaration set to 1. Sometimes concrete function instances
5368 // are placed inside the class definitions and shouldn't be included in
5369 // the list of things are are tracking here.
5370 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_declaration, 0) == 1)
5371 {
5372 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
5373 if (dst_name)
Greg Clayton65ec1032012-11-12 21:27:20 +00005374 {
5375 ConstString dst_const_name(dst_name);
5376 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_artificial, 0))
5377 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die);
5378 else
5379 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die);
5380 }
Greg Clayton84afacd2012-11-07 23:09:32 +00005381 }
Greg Clayton4116e932012-05-15 02:33:01 +00005382 }
5383 }
5384 const uint32_t src_size = src_name_to_die.GetSize ();
5385 const uint32_t dst_size = dst_name_to_die.GetSize ();
Greg Clayton5160ce52013-03-27 23:08:40 +00005386 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
Sean Callanan2367f8a2013-02-26 01:12:25 +00005387
5388 // Is everything kosher so we can go through the members at top speed?
5389 bool fast_path = true;
5390
5391 if (src_size != dst_size)
Greg Clayton4116e932012-05-15 02:33:01 +00005392 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00005393 if (src_size != 0 && dst_size != 0)
5394 {
5395 if (log)
5396 log->Printf("warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)",
5397 src_class_die->GetOffset(),
5398 dst_class_die->GetOffset(),
5399 src_size,
5400 dst_size);
5401 }
5402
5403 fast_path = false;
5404 }
5405
5406 uint32_t idx;
5407
5408 if (fast_path)
5409 {
Greg Clayton4116e932012-05-15 02:33:01 +00005410 for (idx = 0; idx < src_size; ++idx)
5411 {
5412 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5413 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
5414
5415 if (src_die->Tag() != dst_die->Tag())
5416 {
5417 if (log)
5418 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
5419 src_class_die->GetOffset(),
5420 dst_class_die->GetOffset(),
5421 src_die->GetOffset(),
5422 DW_TAG_value_to_name(src_die->Tag()),
5423 dst_die->GetOffset(),
5424 DW_TAG_value_to_name(src_die->Tag()));
Sean Callanan2367f8a2013-02-26 01:12:25 +00005425 fast_path = false;
Greg Clayton4116e932012-05-15 02:33:01 +00005426 }
5427
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005428 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
Greg Clayton4116e932012-05-15 02:33:01 +00005429 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
5430
5431 // Make sure the names match
5432 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
5433 continue;
5434
5435 if (log)
5436 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
5437 src_class_die->GetOffset(),
5438 dst_class_die->GetOffset(),
5439 src_die->GetOffset(),
5440 src_name,
5441 dst_die->GetOffset(),
5442 dst_name);
5443
Sean Callanan2367f8a2013-02-26 01:12:25 +00005444 fast_path = false;
Greg Clayton4116e932012-05-15 02:33:01 +00005445 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005446 }
Greg Clayton4116e932012-05-15 02:33:01 +00005447
Sean Callanan2367f8a2013-02-26 01:12:25 +00005448 // Now do the work of linking the DeclContexts and Types.
5449 if (fast_path)
5450 {
5451 // We can do this quickly. Just run across the tables index-for-index since
5452 // we know each node has matching names and tags.
Greg Clayton4116e932012-05-15 02:33:01 +00005453 for (idx = 0; idx < src_size; ++idx)
5454 {
5455 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5456 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
5457
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005458 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
Greg Clayton4116e932012-05-15 02:33:01 +00005459 if (src_decl_ctx)
5460 {
5461 if (log)
Greg Clayton65ec1032012-11-12 21:27:20 +00005462 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005463 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5464 }
5465 else
5466 {
5467 if (log)
Greg Clayton65ec1032012-11-12 21:27:20 +00005468 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005469 }
5470
5471 Type *src_child_type = m_die_to_type[src_die];
5472 if (src_child_type)
5473 {
5474 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00005475 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", src_child_type, src_child_type->GetID(), src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005476 m_die_to_type[dst_die] = src_child_type;
5477 }
5478 else
5479 {
5480 if (log)
Greg Clayton65ec1032012-11-12 21:27:20 +00005481 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5482 }
5483 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005484 }
5485 else
5486 {
5487 // We must do this slowly. For each member of the destination, look
5488 // up a member in the source with the same name, check its tag, and
5489 // unique them if everything matches up. Report failures.
Greg Clayton65ec1032012-11-12 21:27:20 +00005490
Sean Callanan2367f8a2013-02-26 01:12:25 +00005491 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty())
5492 {
5493 src_name_to_die.Sort();
Greg Clayton65ec1032012-11-12 21:27:20 +00005494
Sean Callanan2367f8a2013-02-26 01:12:25 +00005495 for (idx = 0; idx < dst_size; ++idx)
5496 {
5497 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx);
5498 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
5499 src_die = src_name_to_die.Find(dst_name, NULL);
5500
5501 if (src_die && (src_die->Tag() == dst_die->Tag()))
5502 {
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005503 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
Sean Callanan2367f8a2013-02-26 01:12:25 +00005504 if (src_decl_ctx)
5505 {
5506 if (log)
5507 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
5508 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5509 }
5510 else
5511 {
5512 if (log)
5513 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5514 }
5515
5516 Type *src_child_type = m_die_to_type[src_die];
5517 if (src_child_type)
5518 {
5519 if (log)
5520 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", src_child_type, src_child_type->GetID(), src_die->GetOffset(), dst_die->GetOffset());
5521 m_die_to_type[dst_die] = src_child_type;
5522 }
5523 else
5524 {
5525 if (log)
5526 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5527 }
5528 }
5529 else
5530 {
5531 if (log)
5532 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005533
Sean Callanan2367f8a2013-02-26 01:12:25 +00005534 failures.push_back(dst_die);
5535 }
5536 }
5537 }
5538 }
5539
5540 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize ();
5541 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize ();
5542
5543 UniqueCStringMap<const DWARFDebugInfoEntry *> name_to_die_artificial_not_in_src;
5544
5545 if (src_size_artificial && dst_size_artificial)
5546 {
5547 dst_name_to_die_artificial.Sort();
5548
Greg Clayton65ec1032012-11-12 21:27:20 +00005549 for (idx = 0; idx < src_size_artificial; ++idx)
5550 {
5551 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx);
5552 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
5553 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, NULL);
5554
5555 if (dst_die)
5556 {
Greg Clayton65ec1032012-11-12 21:27:20 +00005557 // Both classes have the artificial types, link them
5558 clang::DeclContext *src_decl_ctx = m_die_to_decl_ctx[src_die];
5559 if (src_decl_ctx)
5560 {
5561 if (log)
5562 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
5563 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5564 }
5565 else
5566 {
5567 if (log)
5568 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5569 }
5570
5571 Type *src_child_type = m_die_to_type[src_die];
5572 if (src_child_type)
5573 {
5574 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00005575 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", src_child_type, src_child_type->GetID(), src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005576 m_die_to_type[dst_die] = src_child_type;
5577 }
5578 else
5579 {
5580 if (log)
5581 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5582 }
5583 }
5584 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005585 }
Greg Clayton65ec1032012-11-12 21:27:20 +00005586
Sean Callanan2367f8a2013-02-26 01:12:25 +00005587 if (dst_size_artificial)
Greg Clayton4116e932012-05-15 02:33:01 +00005588 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00005589 for (idx = 0; idx < dst_size_artificial; ++idx)
5590 {
5591 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx);
5592 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
5593 if (log)
5594 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die->GetOffset(), dst_name_artificial);
5595
5596 failures.push_back(dst_die);
5597 }
Greg Clayton4116e932012-05-15 02:33:01 +00005598 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005599
5600 return (failures.size() != 0);
Greg Clayton4116e932012-05-15 02:33:01 +00005601}
Greg Claytona8022fa2012-04-24 21:22:41 +00005602
5603TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00005604SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005605{
5606 TypeSP type_sp;
5607
Greg Clayton1be10fc2010-09-29 01:12:09 +00005608 if (type_is_new_ptr)
5609 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00005610
5611#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5612 static DIEStack g_die_stack;
5613 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
5614#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00005615
Sean Callananc7fbf732010-08-06 00:32:49 +00005616 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005617 if (die != NULL)
5618 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005619 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005620 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00005621 {
5622 const DWARFDebugInfoEntry *context_die;
5623 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
5624
Greg Clayton5160ce52013-03-27 23:08:40 +00005625 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
Sean Callanan5b26f272012-02-04 08:49:35 +00005626 die->GetOffset(),
5627 context,
5628 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00005629 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00005630 die->GetName(this, dwarf_cu));
5631
5632#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5633 scoped_die_logger.Push (dwarf_cu, die);
Greg Clayton5160ce52013-03-27 23:08:40 +00005634 g_die_stack.LogDIEs(log, this);
Greg Clayton1fba87112012-02-09 20:03:49 +00005635#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00005636 }
Greg Clayton3bffb082011-12-10 02:15:28 +00005637//
Greg Clayton5160ce52013-03-27 23:08:40 +00005638// Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005639// if (log && dwarf_cu)
5640// {
5641// StreamString s;
5642// die->DumpLocation (this, dwarf_cu, s);
Greg Clayton5160ce52013-03-27 23:08:40 +00005643// GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00005644//
5645// }
Jim Ingham16746d12011-08-25 23:21:43 +00005646
Greg Clayton594e5ed2010-09-27 21:07:38 +00005647 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005648 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00005649 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005650 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005651 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005652 if (type_is_new_ptr)
5653 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005654
Greg Clayton594e5ed2010-09-27 21:07:38 +00005655 const dw_tag_t tag = die->Tag();
5656
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005657 bool is_forward_declaration = false;
5658 DWARFDebugInfoEntry::Attributes attributes;
5659 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00005660 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00005661 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
Greg Claytonfaac1112013-03-14 18:31:44 +00005662 uint64_t byte_size = 0;
Greg Clayton526e5af2010-11-13 03:52:47 +00005663 Declaration decl;
5664
Greg Clayton4957bf62010-09-30 21:49:03 +00005665 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005666 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005667
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005668 dw_attr_t attr;
5669
5670 switch (tag)
5671 {
5672 case DW_TAG_base_type:
5673 case DW_TAG_pointer_type:
5674 case DW_TAG_reference_type:
Sean Callanance8af862012-05-21 23:31:51 +00005675 case DW_TAG_rvalue_reference_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005676 case DW_TAG_typedef:
5677 case DW_TAG_const_type:
5678 case DW_TAG_restrict_type:
5679 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00005680 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005681 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005682 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005683 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005684
Greg Claytond88d7592010-09-15 08:33:30 +00005685 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005686 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005687 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
5688
5689 if (num_attributes > 0)
5690 {
5691 uint32_t i;
5692 for (i=0; i<num_attributes; ++i)
5693 {
5694 attr = attributes.AttributeAtIndex(i);
5695 DWARFFormValue form_value;
5696 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5697 {
5698 switch (attr)
5699 {
5700 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5701 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5702 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5703 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00005704
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005705 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00005706 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
5707 // include the "&"...
5708 if (tag == DW_TAG_reference_type)
5709 {
5710 if (strchr (type_name_cstr, '&') == NULL)
5711 type_name_cstr = NULL;
5712 }
5713 if (type_name_cstr)
5714 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005715 break;
Greg Clayton23f59502012-07-17 03:23:13 +00005716 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005717 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
5718 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
5719 default:
5720 case DW_AT_sibling:
5721 break;
5722 }
5723 }
5724 }
5725 }
5726
Daniel Malead01b2952012-11-29 21:49:15 +00005727 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8x\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
Greg Claytonc93237c2010-10-01 20:48:32 +00005728
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005729 switch (tag)
5730 {
5731 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00005732 break;
5733
Greg Claytond4436412011-10-28 21:00:00 +00005734 case DW_TAG_unspecified_type:
5735 if (strcmp(type_name_cstr, "nullptr_t") == 0)
5736 {
5737 resolve_state = Type::eResolveStateFull;
5738 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
5739 break;
5740 }
5741 // Fall through to base type below in case we can handle the type there...
5742
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005743 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00005744 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005745 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
5746 encoding,
5747 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005748 break;
5749
Sean Callanance8af862012-05-21 23:31:51 +00005750 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
5751 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
5752 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
5753 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
5754 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
5755 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
5756 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005757 }
5758
Sean Callanan82587052013-01-03 00:05:56 +00005759 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005760 {
Sean Callanan82587052013-01-03 00:05:56 +00005761 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
5762
5763 if (translation_unit_is_objc)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005764 {
Sean Callanan82587052013-01-03 00:05:56 +00005765 if (type_name_cstr != NULL)
Greg Claytonc7f03b62012-01-12 04:33:28 +00005766 {
Sean Callanan82587052013-01-03 00:05:56 +00005767 static ConstString g_objc_type_name_id("id");
5768 static ConstString g_objc_type_name_Class("Class");
5769 static ConstString g_objc_type_name_selector("SEL");
5770
5771 if (type_name_const_str == g_objc_type_name_id)
5772 {
5773 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005774 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
Sean Callanan82587052013-01-03 00:05:56 +00005775 die->GetOffset(),
5776 DW_TAG_value_to_name(die->Tag()),
5777 die->GetName(this, dwarf_cu));
5778 clang_type = ast.GetBuiltInType_objc_id();
5779 encoding_data_type = Type::eEncodingIsUID;
5780 encoding_uid = LLDB_INVALID_UID;
5781 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00005782
Sean Callanan82587052013-01-03 00:05:56 +00005783 }
5784 else if (type_name_const_str == g_objc_type_name_Class)
5785 {
5786 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005787 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
Sean Callanan82587052013-01-03 00:05:56 +00005788 die->GetOffset(),
5789 DW_TAG_value_to_name(die->Tag()),
5790 die->GetName(this, dwarf_cu));
5791 clang_type = ast.GetBuiltInType_objc_Class();
5792 encoding_data_type = Type::eEncodingIsUID;
5793 encoding_uid = LLDB_INVALID_UID;
5794 resolve_state = Type::eResolveStateFull;
5795 }
5796 else if (type_name_const_str == g_objc_type_name_selector)
5797 {
5798 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005799 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
Sean Callanan82587052013-01-03 00:05:56 +00005800 die->GetOffset(),
5801 DW_TAG_value_to_name(die->Tag()),
5802 die->GetName(this, dwarf_cu));
5803 clang_type = ast.GetBuiltInType_objc_selector();
5804 encoding_data_type = Type::eEncodingIsUID;
5805 encoding_uid = LLDB_INVALID_UID;
5806 resolve_state = Type::eResolveStateFull;
5807 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00005808 }
Sean Callanan82587052013-01-03 00:05:56 +00005809 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID)
Greg Claytonc7f03b62012-01-12 04:33:28 +00005810 {
Sean Callanan82587052013-01-03 00:05:56 +00005811 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id".
5812
5813 DWARFDebugInfoEntry* encoding_die = dwarf_cu->GetDIEPtr(encoding_uid);
5814
5815 if (encoding_die && encoding_die->Tag() == DW_TAG_structure_type)
5816 {
5817 if (const char *struct_name = encoding_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL))
5818 {
5819 if (!strcmp(struct_name, "objc_object"))
5820 {
5821 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005822 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.",
Sean Callanan82587052013-01-03 00:05:56 +00005823 die->GetOffset(),
5824 DW_TAG_value_to_name(die->Tag()),
5825 die->GetName(this, dwarf_cu));
5826 clang_type = ast.GetBuiltInType_objc_id();
5827 encoding_data_type = Type::eEncodingIsUID;
5828 encoding_uid = LLDB_INVALID_UID;
5829 resolve_state = Type::eResolveStateFull;
5830 }
5831 }
5832 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00005833 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005834 }
5835 }
5836
Greg Clayton81c22f62011-10-19 18:09:39 +00005837 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005838 this,
5839 type_name_const_str,
5840 byte_size,
5841 NULL,
5842 encoding_uid,
5843 encoding_data_type,
5844 &decl,
5845 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005846 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005847
Greg Clayton594e5ed2010-09-27 21:07:38 +00005848 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005849
5850// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
5851// if (encoding_type != NULL)
5852// {
5853// if (encoding_type != DIE_IS_BEING_PARSED)
5854// type_sp->SetEncodingType(encoding_type);
5855// else
5856// m_indirect_fixups.push_back(type_sp.get());
5857// }
5858 }
5859 break;
5860
5861 case DW_TAG_structure_type:
5862 case DW_TAG_union_type:
5863 case DW_TAG_class_type:
5864 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005865 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005866 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Greg Clayton23f59502012-07-17 03:23:13 +00005867 bool byte_size_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005868
Greg Clayton9e409562010-07-28 02:04:09 +00005869 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00005870 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005871 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00005872 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005873 if (num_attributes > 0)
5874 {
5875 uint32_t i;
5876 for (i=0; i<num_attributes; ++i)
5877 {
5878 attr = attributes.AttributeAtIndex(i);
5879 DWARFFormValue form_value;
5880 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5881 {
5882 switch (attr)
5883 {
Greg Clayton9e409562010-07-28 02:04:09 +00005884 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00005885 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
5886 {
5887 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
5888 // point to the compile unit file, so we clear this invalid value
5889 // so that we can still unique types efficiently.
5890 decl.SetFile(FileSpec ("<invalid>", false));
5891 }
5892 else
5893 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00005894 break;
5895
5896 case DW_AT_decl_line:
5897 decl.SetLine(form_value.Unsigned());
5898 break;
5899
5900 case DW_AT_decl_column:
5901 decl.SetColumn(form_value.Unsigned());
5902 break;
5903
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005904 case DW_AT_name:
5905 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005906 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005907 break;
Greg Clayton9e409562010-07-28 02:04:09 +00005908
5909 case DW_AT_byte_size:
5910 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00005911 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00005912 break;
5913
5914 case DW_AT_accessibility:
5915 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
5916 break;
5917
5918 case DW_AT_declaration:
Greg Clayton1c8ef472013-04-05 23:27:21 +00005919 is_forward_declaration = form_value.Boolean();
Greg Clayton9e409562010-07-28 02:04:09 +00005920 break;
5921
5922 case DW_AT_APPLE_runtime_class:
5923 class_language = (LanguageType)form_value.Signed();
5924 break;
5925
Greg Clayton18774842011-11-29 23:40:34 +00005926 case DW_AT_APPLE_objc_complete_type:
5927 is_complete_objc_class = form_value.Signed();
5928 break;
5929
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005930 case DW_AT_allocated:
5931 case DW_AT_associated:
5932 case DW_AT_data_location:
5933 case DW_AT_description:
5934 case DW_AT_start_scope:
5935 case DW_AT_visibility:
5936 default:
5937 case DW_AT_sibling:
5938 break;
5939 }
5940 }
5941 }
5942 }
5943
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005944 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00005945
Greg Clayton123edca2012-03-02 00:07:15 +00005946 // Only try and unique the type if it has a name.
5947 if (type_name_const_str &&
5948 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00005949 this,
5950 dwarf_cu,
5951 die,
5952 decl,
5953 byte_size_valid ? byte_size : -1,
5954 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005955 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005956 // We have already parsed this type or from another
5957 // compile unit. GCC loves to use the "one definition
5958 // rule" which can result in multiple definitions
5959 // of the same class over and over in each compile
5960 // unit.
5961 type_sp = unique_ast_entry.m_type_sp;
5962 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00005963 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005964 m_die_to_type[die] = type_sp.get();
5965 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005966 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005967 }
5968
Daniel Malead01b2952012-11-29 21:49:15 +00005969 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005970
5971 int tag_decl_kind = -1;
5972 AccessType default_accessibility = eAccessNone;
5973 if (tag == DW_TAG_structure_type)
5974 {
5975 tag_decl_kind = clang::TTK_Struct;
5976 default_accessibility = eAccessPublic;
5977 }
5978 else if (tag == DW_TAG_union_type)
5979 {
5980 tag_decl_kind = clang::TTK_Union;
5981 default_accessibility = eAccessPublic;
5982 }
5983 else if (tag == DW_TAG_class_type)
5984 {
5985 tag_decl_kind = clang::TTK_Class;
5986 default_accessibility = eAccessPrivate;
5987 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00005988
5989 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
5990 die->HasChildren() == false &&
5991 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
5992 {
5993 // Work around an issue with clang at the moment where
5994 // forward declarations for objective C classes are emitted
5995 // as:
5996 // DW_TAG_structure_type [2]
5997 // DW_AT_name( "ForwardObjcClass" )
5998 // DW_AT_byte_size( 0x00 )
5999 // DW_AT_decl_file( "..." )
6000 // DW_AT_decl_line( 1 )
6001 //
6002 // Note that there is no DW_AT_declaration and there are
6003 // no children, and the byte size is zero.
6004 is_forward_declaration = true;
6005 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006006
Sean Callanan7457f8d2012-04-25 01:03:57 +00006007 if (class_language == eLanguageTypeObjC ||
6008 class_language == eLanguageTypeObjC_plus_plus)
Greg Clayton18774842011-11-29 23:40:34 +00006009 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00006010 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00006011 {
6012 // We have a valid eSymbolTypeObjCClass class symbol whose
6013 // name matches the current objective C class that we
6014 // are trying to find and this DIE isn't the complete
6015 // definition (we checked is_complete_objc_class above and
6016 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00006017 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006018
Greg Clayton1f746072012-08-29 21:13:06 +00006019 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton901c5ca2011-12-03 04:40:03 +00006020 {
6021 // We weren't able to find a full declaration in
6022 // this DWARF, see if we have a declaration anywhere
6023 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00006024 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00006025 }
6026
6027 if (type_sp)
6028 {
6029 if (log)
6030 {
Greg Clayton5160ce52013-03-27 23:08:40 +00006031 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00006032 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64,
Greg Claytone38a5ed2012-01-05 03:57:59 +00006033 this,
6034 die->GetOffset(),
6035 DW_TAG_value_to_name(tag),
6036 type_name_cstr,
6037 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00006038 }
6039
6040 // We found a real definition for this type elsewhere
6041 // so lets use it and cache the fact that we found
6042 // a complete type for this die
6043 m_die_to_type[die] = type_sp.get();
6044 return type_sp;
6045 }
6046 }
6047 }
6048
6049
6050 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006051 {
6052 // We have a forward declaration to a type and we need
6053 // to try and find a full declaration. We look in the
6054 // current type index just in case we have a forward
6055 // declaration followed by an actual declarations in the
6056 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00006057 if (log)
6058 {
Greg Clayton5160ce52013-03-27 23:08:40 +00006059 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00006060 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
6061 this,
6062 die->GetOffset(),
6063 DW_TAG_value_to_name(tag),
6064 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00006065 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006066
Greg Claytona8022fa2012-04-24 21:22:41 +00006067 DWARFDeclContext die_decl_ctx;
6068 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx);
6069
6070 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
6071 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006072
Greg Clayton1f746072012-08-29 21:13:06 +00006073 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton4272cc72011-02-02 02:24:04 +00006074 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006075 // We weren't able to find a full declaration in
6076 // this DWARF, see if we have a declaration anywhere
6077 // else...
Greg Claytona8022fa2012-04-24 21:22:41 +00006078 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton4272cc72011-02-02 02:24:04 +00006079 }
6080
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006081 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00006082 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00006083 if (log)
6084 {
Greg Clayton5160ce52013-03-27 23:08:40 +00006085 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00006086 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
Greg Claytone38a5ed2012-01-05 03:57:59 +00006087 this,
6088 die->GetOffset(),
6089 DW_TAG_value_to_name(tag),
6090 type_name_cstr,
6091 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00006092 }
6093
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006094 // We found a real definition for this type elsewhere
6095 // so lets use it and cache the fact that we found
6096 // a complete type for this die
6097 m_die_to_type[die] = type_sp.get();
6098 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00006099 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006100 }
6101 assert (tag_decl_kind != -1);
6102 bool clang_type_was_created = false;
6103 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
6104 if (clang_type == NULL)
6105 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00006106 const DWARFDebugInfoEntry *decl_ctx_die;
6107
6108 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00006109 if (accessibility == eAccessNone && decl_ctx)
6110 {
6111 // Check the decl context that contains this class/struct/union.
6112 // If it is a class we must give it an accessability.
6113 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
6114 if (DeclKindIsCXXClass (containing_decl_kind))
6115 accessibility = default_accessibility;
6116 }
6117
Greg Claytonc4ffd662013-03-08 01:37:30 +00006118 ClangASTMetadata metadata;
6119 metadata.SetUserID(MakeUserID(die->GetOffset()));
6120 metadata.SetIsDynamicCXXType(ClassOrStructIsVirtual (dwarf_cu, die));
6121
Greg Claytonf0705c82011-10-22 03:33:13 +00006122 if (type_name_cstr && strchr (type_name_cstr, '<'))
6123 {
6124 ClangASTContext::TemplateParameterInfos template_param_infos;
6125 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
6126 {
6127 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00006128 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00006129 type_name_cstr,
6130 tag_decl_kind,
6131 template_param_infos);
6132
6133 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
6134 class_template_decl,
6135 tag_decl_kind,
6136 template_param_infos);
6137 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
6138 clang_type_was_created = true;
Sean Callanan60217122012-04-13 00:10:03 +00006139
Greg Claytond0029442013-03-27 01:48:02 +00006140 GetClangASTContext().SetMetadata (class_template_decl, metadata);
6141 GetClangASTContext().SetMetadata (class_specialization_decl, metadata);
Greg Claytonf0705c82011-10-22 03:33:13 +00006142 }
6143 }
6144
6145 if (!clang_type_was_created)
6146 {
6147 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00006148 clang_type = ast.CreateRecordType (decl_ctx,
6149 accessibility,
6150 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00006151 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00006152 class_language,
Jim Ingham379397632012-10-27 02:54:13 +00006153 &metadata);
Greg Claytonf0705c82011-10-22 03:33:13 +00006154 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006155 }
6156
6157 // Store a forward declaration to this class type in case any
6158 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00006159 // types for function prototypes.
6160 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00006161 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006162 this,
6163 type_name_const_str,
6164 byte_size,
6165 NULL,
6166 LLDB_INVALID_UID,
6167 Type::eEncodingIsUID,
6168 &decl,
6169 clang_type,
6170 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00006171
6172 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006173
6174
6175 // Add our type to the unique type map so we don't
6176 // end up creating many copies of the same type over
6177 // and over in the ASTContext for our module
6178 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00006179 unique_ast_entry.m_symfile = this;
6180 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006181 unique_ast_entry.m_die = die;
6182 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00006183 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00006184 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
6185 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006186
Sean Callanan12014a02011-12-08 23:45:45 +00006187 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00006188 {
6189 // Always start the definition for a class type so that
6190 // if the class has child classes or types that require
6191 // the class to be created for use as their decl contexts
6192 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00006193 if (die->HasChildren() == false)
6194 {
6195 // No children for this struct/union/class, lets finish it
6196 ast.StartTagDeclarationDefinition (clang_type);
6197 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan433cd222012-10-19 01:37:25 +00006198
6199 if (tag == DW_TAG_structure_type) // this only applies in C
6200 {
6201 clang::QualType qual_type = clang::QualType::getFromOpaquePtr (clang_type);
6202 const clang::RecordType *record_type = qual_type->getAs<clang::RecordType> ();
6203
6204 if (record_type)
6205 {
6206 clang::RecordDecl *record_decl = record_type->getDecl();
6207
6208 if (record_decl)
6209 {
6210 LayoutInfo layout_info;
6211
6212 layout_info.alignment = 0;
6213 layout_info.bit_size = 0;
6214
6215 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
6216 }
6217 }
6218 }
Sean Callanan12014a02011-12-08 23:45:45 +00006219 }
6220 else if (clang_type_was_created)
6221 {
Greg Clayton219cf312012-03-30 00:51:13 +00006222 // Start the definition if the class is not objective C since
6223 // the underlying decls respond to isCompleteDefinition(). Objective
6224 // C decls dont' respond to isCompleteDefinition() so we can't
6225 // start the declaration definition right away. For C++ classs/union/structs
6226 // we want to start the definition in case the class is needed as the
6227 // declaration context for a contained class or type without the need
6228 // to complete that type..
6229
Sean Callanan7457f8d2012-04-25 01:03:57 +00006230 if (class_language != eLanguageTypeObjC &&
6231 class_language != eLanguageTypeObjC_plus_plus)
Greg Clayton219cf312012-03-30 00:51:13 +00006232 ast.StartTagDeclarationDefinition (clang_type);
6233
Sean Callanan12014a02011-12-08 23:45:45 +00006234 // Leave this as a forward declaration until we need
6235 // to know the details of the type. lldb_private::Type
6236 // will automatically call the SymbolFile virtual function
6237 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
6238 // When the definition needs to be defined.
6239 m_forward_decl_die_to_clang_type[die] = clang_type;
6240 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
6241 ClangASTContext::SetHasExternalStorage (clang_type, true);
6242 }
Greg Claytonc615ce42010-11-09 04:42:43 +00006243 }
Greg Claytoncab36a32011-12-08 05:16:30 +00006244
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006245 }
6246 break;
6247
6248 case DW_TAG_enumeration_type:
6249 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006250 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006251 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006252
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006253 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006254
Greg Claytond88d7592010-09-15 08:33:30 +00006255 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006256 if (num_attributes > 0)
6257 {
6258 uint32_t i;
6259
6260 for (i=0; i<num_attributes; ++i)
6261 {
6262 attr = attributes.AttributeAtIndex(i);
6263 DWARFFormValue form_value;
6264 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6265 {
6266 switch (attr)
6267 {
Greg Clayton7a345282010-11-09 23:46:37 +00006268 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6269 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6270 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006271 case DW_AT_name:
6272 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006273 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006274 break;
Greg Clayton7a345282010-11-09 23:46:37 +00006275 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006276 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
6277 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006278 case DW_AT_declaration: break; //is_forward_declaration = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006279 case DW_AT_allocated:
6280 case DW_AT_associated:
6281 case DW_AT_bit_stride:
6282 case DW_AT_byte_stride:
6283 case DW_AT_data_location:
6284 case DW_AT_description:
6285 case DW_AT_start_scope:
6286 case DW_AT_visibility:
6287 case DW_AT_specification:
6288 case DW_AT_abstract_origin:
6289 case DW_AT_sibling:
6290 break;
6291 }
6292 }
6293 }
6294
Daniel Malead01b2952012-11-29 21:49:15 +00006295 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006296
Greg Clayton1be10fc2010-09-29 01:12:09 +00006297 clang_type_t enumerator_clang_type = NULL;
6298 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
6299 if (clang_type == NULL)
6300 {
Greg Clayton5d14fc02013-03-06 06:23:54 +00006301 if (encoding_uid != DW_INVALID_OFFSET)
6302 {
6303 Type *enumerator_type = ResolveTypeUID(encoding_uid);
6304 if (enumerator_type)
6305 enumerator_clang_type = enumerator_type->GetClangFullType();
6306 }
6307
6308 if (enumerator_clang_type == NULL)
6309 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
6310 DW_ATE_signed,
6311 byte_size * 8);
6312
Greg Claytonca512b32011-01-14 04:54:56 +00006313 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00006314 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00006315 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006316 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00006317 }
6318 else
6319 {
6320 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00006321 }
6322
Greg Claytona2721472011-06-25 00:44:06 +00006323 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
6324
Greg Clayton81c22f62011-10-19 18:09:39 +00006325 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006326 this,
6327 type_name_const_str,
6328 byte_size,
6329 NULL,
6330 encoding_uid,
6331 Type::eEncodingIsUID,
6332 &decl,
6333 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006334 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006335
Greg Clayton6beaaa62011-01-17 03:46:26 +00006336 ast.StartTagDeclarationDefinition (clang_type);
6337 if (die->HasChildren())
6338 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00006339 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Greg Clayton3e067532013-03-05 23:54:39 +00006340 bool is_signed = false;
Greg Clayton5d14fc02013-03-06 06:23:54 +00006341 ast.IsIntegerType(enumerator_clang_type, is_signed);
Greg Clayton3e067532013-03-05 23:54:39 +00006342 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006343 }
6344 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006345 }
6346 }
6347 break;
6348
Jim Inghamb0be4422010-08-12 01:20:14 +00006349 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006350 case DW_TAG_subprogram:
6351 case DW_TAG_subroutine_type:
6352 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006353 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006354 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006355
Greg Clayton23f59502012-07-17 03:23:13 +00006356 //const char *mangled = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006357 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00006358 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006359 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00006360 bool is_static = false;
6361 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00006362 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00006363 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00006364 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
6365 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Jim Ingham379397632012-10-27 02:54:13 +00006366 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00006367
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006368 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00006369 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006370
6371
Greg Claytond88d7592010-09-15 08:33:30 +00006372 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006373 if (num_attributes > 0)
6374 {
6375 uint32_t i;
6376 for (i=0; i<num_attributes; ++i)
6377 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00006378 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006379 DWARFFormValue form_value;
6380 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6381 {
6382 switch (attr)
6383 {
6384 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6385 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6386 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6387 case DW_AT_name:
6388 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006389 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006390 break;
6391
Greg Clayton71415542012-12-08 00:24:40 +00006392 case DW_AT_linkage_name:
Greg Clayton23f59502012-07-17 03:23:13 +00006393 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&get_debug_str_data()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006394 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00006395 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006396 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
6397 case DW_AT_inline: is_inline = form_value.Boolean(); break;
6398 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break;
6399 case DW_AT_explicit: is_explicit = form_value.Boolean(); break;
6400 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Sean Callanandbb58392011-11-02 01:38:59 +00006401
Greg Claytonf51de672010-10-01 02:31:07 +00006402
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006403 case DW_AT_external:
6404 if (form_value.Unsigned())
6405 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00006406 if (storage == clang::SC_None)
6407 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006408 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00006409 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006410 }
6411 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006412
Greg Clayton72da3972011-08-16 18:40:23 +00006413 case DW_AT_specification:
6414 specification_die_offset = form_value.Reference(dwarf_cu);
6415 break;
6416
6417 case DW_AT_abstract_origin:
6418 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
6419 break;
6420
Jim Ingham379397632012-10-27 02:54:13 +00006421 case DW_AT_object_pointer:
6422 object_pointer_die_offset = form_value.Reference(dwarf_cu);
6423 break;
6424
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006425 case DW_AT_allocated:
6426 case DW_AT_associated:
6427 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006428 case DW_AT_calling_convention:
6429 case DW_AT_data_location:
6430 case DW_AT_elemental:
6431 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006432 case DW_AT_frame_base:
6433 case DW_AT_high_pc:
6434 case DW_AT_low_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006435 case DW_AT_prototyped:
6436 case DW_AT_pure:
6437 case DW_AT_ranges:
6438 case DW_AT_recursive:
6439 case DW_AT_return_addr:
6440 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006441 case DW_AT_start_scope:
6442 case DW_AT_static_link:
6443 case DW_AT_trampoline:
6444 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006445 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006446 case DW_AT_description:
6447 case DW_AT_sibling:
6448 break;
6449 }
6450 }
6451 }
Greg Clayton24739922010-10-13 03:15:28 +00006452 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006453
Jim Ingham379397632012-10-27 02:54:13 +00006454 std::string object_pointer_name;
6455 if (object_pointer_die_offset != DW_INVALID_OFFSET)
6456 {
6457 // Get the name from the object pointer die
6458 StreamString s;
6459 if (DWARFDebugInfoEntry::GetName (this, dwarf_cu, object_pointer_die_offset, s))
6460 {
6461 object_pointer_name.assign(s.GetData());
6462 }
6463 }
6464
Daniel Malead01b2952012-11-29 21:49:15 +00006465 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006466
Greg Clayton24739922010-10-13 03:15:28 +00006467 clang_type_t return_clang_type = NULL;
6468 Type *func_type = NULL;
6469
6470 if (type_die_offset != DW_INVALID_OFFSET)
6471 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00006472
Greg Clayton24739922010-10-13 03:15:28 +00006473 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00006474 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00006475 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006476 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006477
Greg Claytonf51de672010-10-01 02:31:07 +00006478
Greg Clayton24739922010-10-13 03:15:28 +00006479 std::vector<clang_type_t> function_param_types;
6480 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006481
Greg Clayton24739922010-10-13 03:15:28 +00006482 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00006483
Greg Claytoncb5860a2011-10-13 23:49:28 +00006484 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
6485 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00006486 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
6487
Greg Claytonf0705c82011-10-22 03:33:13 +00006488 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00006489 // Start off static. This will be set to false in ParseChildParameters(...)
6490 // if we find a "this" paramters as the first parameter
6491 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00006492 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006493 ClangASTContext::TemplateParameterInfos template_param_infos;
6494
Greg Clayton24739922010-10-13 03:15:28 +00006495 if (die->HasChildren())
6496 {
Greg Clayton0fffff52010-09-24 05:15:53 +00006497 bool skip_artificial = true;
Jim Ingham379397632012-10-27 02:54:13 +00006498 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00006499 containing_decl_ctx,
Jim Ingham379397632012-10-27 02:54:13 +00006500 dwarf_cu,
6501 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00006502 skip_artificial,
6503 is_static,
Jim Ingham379397632012-10-27 02:54:13 +00006504 type_list,
6505 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00006506 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006507 type_quals,
6508 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00006509 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006510
Greg Clayton24739922010-10-13 03:15:28 +00006511 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006512 clang_type = ast.CreateFunctionType (return_clang_type,
Greg Clayton9cb25c42012-10-30 17:02:18 +00006513 function_param_types.data(),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006514 function_param_types.size(),
6515 is_variadic,
6516 type_quals);
6517
Sean Callanande9ce872013-05-09 23:13:13 +00006518 bool ignore_containing_context = false;
6519
Greg Clayton24739922010-10-13 03:15:28 +00006520 if (type_name_cstr)
6521 {
6522 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00006523 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006524 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00006525 ObjCLanguageRuntime::MethodName objc_method (type_name_cstr, true);
6526 if (objc_method.IsValid(true))
Greg Clayton0fffff52010-09-24 05:15:53 +00006527 {
Greg Clayton24739922010-10-13 03:15:28 +00006528 SymbolContext empty_sc;
6529 clang_type_t class_opaque_type = NULL;
Greg Clayton1b3815c2013-01-30 00:18:29 +00006530 ConstString class_name(objc_method.GetClassName());
Greg Clayton7c810422012-01-18 23:40:49 +00006531 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00006532 {
Greg Clayton24739922010-10-13 03:15:28 +00006533 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00006534 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00006535
6536 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00006537 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00006538 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
6539 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
6540 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00006541 }
Greg Clayton24739922010-10-13 03:15:28 +00006542 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006543
Greg Clayton24739922010-10-13 03:15:28 +00006544 if (class_opaque_type)
6545 {
6546 // If accessibility isn't set to anything valid, assume public for
6547 // now...
6548 if (accessibility == eAccessNone)
6549 accessibility = eAccessPublic;
6550
Sean Callanan464a5b52012-10-04 22:06:29 +00006551 clang::ObjCMethodDecl *objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
6552 type_name_cstr,
6553 clang_type,
Greg Clayton1bbcc032013-03-08 02:42:06 +00006554 accessibility,
6555 is_artificial);
Greg Clayton24739922010-10-13 03:15:28 +00006556 type_handled = objc_method_decl != NULL;
Sean Callanan464a5b52012-10-04 22:06:29 +00006557 if (type_handled)
6558 {
6559 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Claytond0029442013-03-27 01:48:02 +00006560 GetClangASTContext().SetMetadataAsUserID (objc_method_decl, MakeUserID(die->GetOffset()));
Sean Callanan464a5b52012-10-04 22:06:29 +00006561 }
Sean Callananc3a1d562013-02-06 23:21:59 +00006562 else
6563 {
Jason Molendac1a65832013-03-07 22:44:42 +00006564 GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
Sean Callananc3a1d562013-02-06 23:21:59 +00006565 die->GetOffset(),
6566 tag,
6567 DW_TAG_value_to_name(tag));
6568 }
Greg Clayton24739922010-10-13 03:15:28 +00006569 }
6570 }
Greg Clayton5113dc82011-08-12 06:47:54 +00006571 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00006572 {
6573 // Look at the parent of this DIE and see if is is
6574 // a class or struct and see if this is actually a
6575 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00006576 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00006577 if (class_type)
6578 {
Greg Clayton4116e932012-05-15 02:33:01 +00006579 if (class_type->GetID() != MakeUserID(decl_ctx_die->GetOffset()))
6580 {
6581 // We uniqued the parent class of this function to another class
6582 // so we now need to associate all dies under "decl_ctx_die" to
6583 // DIEs in the DIE for "class_type"...
Greg Clayton5d650c6a2013-02-26 01:31:37 +00006584 SymbolFileDWARF *class_symfile = NULL;
Greg Clayton4116e932012-05-15 02:33:01 +00006585 DWARFCompileUnitSP class_type_cu_sp;
Greg Clayton5d650c6a2013-02-26 01:31:37 +00006586 const DWARFDebugInfoEntry *class_type_die = NULL;
Sean Callanan2367f8a2013-02-26 01:12:25 +00006587
6588 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
6589 if (debug_map_symfile)
6590 {
6591 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID()));
6592 class_type_die = class_symfile->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6593 }
6594 else
6595 {
6596 class_symfile = this;
6597 class_type_die = DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6598 }
Greg Clayton4116e932012-05-15 02:33:01 +00006599 if (class_type_die)
6600 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00006601 llvm::SmallVector<const DWARFDebugInfoEntry *, 0> failures;
6602
6603 CopyUniqueClassMethodTypes (class_symfile,
6604 class_type,
6605 class_type_cu_sp.get(),
6606 class_type_die,
6607 dwarf_cu,
6608 decl_ctx_die,
6609 failures);
6610
6611 // FIXME do something with these failures that's smarter than
6612 // just dropping them on the ground. Unfortunately classes don't
6613 // like having stuff added to them after their definitions are
6614 // complete...
6615
6616 type_ptr = m_die_to_type[die];
6617 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton4116e932012-05-15 02:33:01 +00006618 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00006619 type_sp = type_ptr->shared_from_this();
6620 break;
Greg Clayton4116e932012-05-15 02:33:01 +00006621 }
6622 }
6623 }
6624
Greg Clayton72da3972011-08-16 18:40:23 +00006625 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00006626 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006627 // We have a specification which we are going to base our function
6628 // prototype off of, so we need this type to be completed so that the
6629 // m_die_to_decl_ctx for the method in the specification has a valid
6630 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006631 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00006632 // If we have a specification, then the function type should have been
6633 // made with the specification and not with this die.
6634 DWARFCompileUnitSP spec_cu_sp;
6635 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006636 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006637 if (spec_clang_decl_ctx)
6638 {
6639 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
6640 }
6641 else
Jim Inghamc1663042011-09-29 22:12:35 +00006642 {
Daniel Malead01b2952012-11-29 21:49:15 +00006643 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x) has no decl\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00006644 MakeUserID(die->GetOffset()),
6645 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006646 }
Greg Clayton72da3972011-08-16 18:40:23 +00006647 type_handled = true;
6648 }
6649 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
6650 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006651 // We have a specification which we are going to base our function
6652 // prototype off of, so we need this type to be completed so that the
6653 // m_die_to_decl_ctx for the method in the abstract origin has a valid
6654 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006655 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00006656
Greg Clayton72da3972011-08-16 18:40:23 +00006657 DWARFCompileUnitSP abs_cu_sp;
6658 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006659 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006660 if (abs_clang_decl_ctx)
6661 {
6662 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
6663 }
6664 else
Jim Inghamc1663042011-09-29 22:12:35 +00006665 {
Daniel Malead01b2952012-11-29 21:49:15 +00006666 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x) has no decl\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00006667 MakeUserID(die->GetOffset()),
6668 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006669 }
Greg Clayton72da3972011-08-16 18:40:23 +00006670 type_handled = true;
6671 }
6672 else
6673 {
6674 clang_type_t class_opaque_type = class_type->GetClangForwardType();
6675 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00006676 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006677 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00006678 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006679 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
6680 // in the DWARF for C++ methods... Default to public for now...
6681 if (accessibility == eAccessNone)
6682 accessibility = eAccessPublic;
6683
6684 if (!is_static && !die->HasChildren())
6685 {
6686 // We have a C++ member function with no children (this pointer!)
6687 // and clang will get mad if we try and make a function that isn't
6688 // well formed in the DWARF, so we will just skip it...
6689 type_handled = true;
6690 }
6691 else
6692 {
6693 clang::CXXMethodDecl *cxx_method_decl;
6694 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00006695 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s",
Greg Clayton20568dd2011-10-13 23:13:20 +00006696 type_name_cstr,
6697 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00006698 MakeUserID(die->GetOffset()),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00006699 m_obj_file->GetFileSpec().GetPath().c_str());
Greg Clayton20568dd2011-10-13 23:13:20 +00006700
Sean Callananc1b732d2011-11-01 18:07:13 +00006701 const bool is_attr_used = false;
6702
Greg Clayton20568dd2011-10-13 23:13:20 +00006703 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
6704 type_name_cstr,
6705 clang_type,
6706 accessibility,
6707 is_virtual,
6708 is_static,
6709 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00006710 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00006711 is_attr_used,
6712 is_artificial);
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006713
Greg Clayton20568dd2011-10-13 23:13:20 +00006714 type_handled = cxx_method_decl != NULL;
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006715
6716 if (type_handled)
Jim Ingham379397632012-10-27 02:54:13 +00006717 {
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006718 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
6719
6720 Host::SetCrashDescription (NULL);
6721
6722
6723 ClangASTMetadata metadata;
6724 metadata.SetUserID(MakeUserID(die->GetOffset()));
6725
6726 if (!object_pointer_name.empty())
6727 {
6728 metadata.SetObjectPtrName(object_pointer_name.c_str());
6729 if (log)
Greg Claytond0029442013-03-27 01:48:02 +00006730 log->Printf ("Setting object pointer name: %s on method object %p.\n",
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006731 object_pointer_name.c_str(),
Greg Claytond0029442013-03-27 01:48:02 +00006732 cxx_method_decl);
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006733 }
Greg Claytond0029442013-03-27 01:48:02 +00006734 GetClangASTContext().SetMetadata (cxx_method_decl, metadata);
Jim Ingham379397632012-10-27 02:54:13 +00006735 }
Sean Callananb0640dc2013-04-09 23:22:08 +00006736 else
6737 {
Sean Callanande9ce872013-05-09 23:13:13 +00006738 ignore_containing_context = true;
Sean Callananb0640dc2013-04-09 23:22:08 +00006739 }
Greg Clayton20568dd2011-10-13 23:13:20 +00006740 }
Greg Clayton72da3972011-08-16 18:40:23 +00006741 }
6742 else
6743 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006744 // We were asked to parse the type for a method in a class, yet the
6745 // class hasn't been asked to complete itself through the
6746 // clang::ExternalASTSource protocol, so we need to just have the
6747 // class complete itself and do things the right way, then our
6748 // DIE should then have an entry in the m_die_to_type map. First
6749 // we need to modify the m_die_to_type so it doesn't think we are
6750 // trying to parse this DIE anymore...
6751 m_die_to_type[die] = NULL;
6752
6753 // Now we get the full type to force our class type to complete itself
6754 // using the clang::ExternalASTSource protocol which will parse all
6755 // base classes and all methods (including the method for this DIE).
6756 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006757
Greg Clayton20568dd2011-10-13 23:13:20 +00006758 // The type for this DIE should have been filled in the function call above
6759 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00006760 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton20568dd2011-10-13 23:13:20 +00006761 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006762 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00006763 break;
6764 }
Sean Callanan02eee4d2012-04-12 23:10:00 +00006765
6766 // FIXME This is fixing some even uglier behavior but we really need to
6767 // uniq the methods of each class as well as the class itself.
6768 // <rdar://problem/11240464>
6769 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00006770 }
Greg Clayton931180e2011-01-27 06:44:37 +00006771 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006772 }
6773 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006774 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006775 }
Greg Clayton24739922010-10-13 03:15:28 +00006776
6777 if (!type_handled)
6778 {
6779 // We just have a function that isn't part of a class
Sean Callanande9ce872013-05-09 23:13:13 +00006780 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (ignore_containing_context ? GetClangASTContext().GetTranslationUnitDecl() : containing_decl_ctx,
Greg Clayton147e1fa2011-10-14 22:47:18 +00006781 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006782 clang_type,
6783 storage,
6784 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006785
6786// if (template_param_infos.GetSize() > 0)
6787// {
6788// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
6789// function_decl,
6790// type_name_cstr,
6791// template_param_infos);
6792//
6793// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
6794// func_template_decl,
6795// template_param_infos);
6796// }
Greg Clayton24739922010-10-13 03:15:28 +00006797 // Add the decl to our DIE to decl context map
6798 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00006799 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00006800 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006801 ast.SetFunctionParameters (function_decl,
6802 &function_param_decls.front(),
6803 function_param_decls.size());
Sean Callanan60217122012-04-13 00:10:03 +00006804
Jim Ingham379397632012-10-27 02:54:13 +00006805 ClangASTMetadata metadata;
6806 metadata.SetUserID(MakeUserID(die->GetOffset()));
6807
6808 if (!object_pointer_name.empty())
6809 {
6810 metadata.SetObjectPtrName(object_pointer_name.c_str());
Jim Ingham2cffda3f2012-10-30 23:34:07 +00006811 if (log)
Greg Claytond0029442013-03-27 01:48:02 +00006812 log->Printf ("Setting object pointer name: %s on function object %p.",
Jim Ingham2cffda3f2012-10-30 23:34:07 +00006813 object_pointer_name.c_str(),
Greg Claytond0029442013-03-27 01:48:02 +00006814 function_decl);
Jim Ingham379397632012-10-27 02:54:13 +00006815 }
Greg Claytond0029442013-03-27 01:48:02 +00006816 GetClangASTContext().SetMetadata (function_decl, metadata);
Greg Clayton24739922010-10-13 03:15:28 +00006817 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006818 }
Greg Clayton81c22f62011-10-19 18:09:39 +00006819 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006820 this,
6821 type_name_const_str,
6822 0,
6823 NULL,
6824 LLDB_INVALID_UID,
6825 Type::eEncodingIsUID,
6826 &decl,
6827 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006828 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00006829 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006830 }
6831 break;
6832
6833 case DW_TAG_array_type:
6834 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006835 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006836 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006837
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006838 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006839 int64_t first_index = 0;
6840 uint32_t byte_stride = 0;
6841 uint32_t bit_stride = 0;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006842 bool is_vector = false;
Greg Claytond88d7592010-09-15 08:33:30 +00006843 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006844
6845 if (num_attributes > 0)
6846 {
6847 uint32_t i;
6848 for (i=0; i<num_attributes; ++i)
6849 {
6850 attr = attributes.AttributeAtIndex(i);
6851 DWARFFormValue form_value;
6852 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6853 {
6854 switch (attr)
6855 {
6856 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6857 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6858 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6859 case DW_AT_name:
6860 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006861 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006862 break;
6863
6864 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006865 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006866 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
6867 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006868 case DW_AT_GNU_vector: is_vector = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006869 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006870 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006871 case DW_AT_allocated:
6872 case DW_AT_associated:
6873 case DW_AT_data_location:
6874 case DW_AT_description:
6875 case DW_AT_ordering:
6876 case DW_AT_start_scope:
6877 case DW_AT_visibility:
6878 case DW_AT_specification:
6879 case DW_AT_abstract_origin:
6880 case DW_AT_sibling:
6881 break;
6882 }
6883 }
6884 }
6885
Daniel Malead01b2952012-11-29 21:49:15 +00006886 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006887
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006888 Type *element_type = ResolveTypeUID(type_die_offset);
6889
6890 if (element_type)
6891 {
6892 std::vector<uint64_t> element_orders;
6893 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
6894 if (byte_stride == 0 && bit_stride == 0)
6895 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00006896 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006897 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
6898 uint64_t num_elements = 0;
6899 std::vector<uint64_t>::const_reverse_iterator pos;
6900 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
6901 for (pos = element_orders.rbegin(); pos != end; ++pos)
6902 {
6903 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006904 clang_type = ast.CreateArrayType (array_element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00006905 num_elements,
6906 is_vector);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006907 array_element_type = clang_type;
Greg Clayton4ef877f2012-12-06 02:33:54 +00006908 array_element_bit_stride = num_elements ? array_element_bit_stride * num_elements : array_element_bit_stride;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006909 }
6910 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00006911 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006912 this,
6913 empty_name,
6914 array_element_bit_stride / 8,
6915 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00006916 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006917 Type::eEncodingIsUID,
6918 &decl,
6919 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006920 Type::eResolveStateFull));
6921 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006922 }
6923 }
6924 }
6925 break;
6926
Greg Clayton9b81a312010-06-12 01:20:30 +00006927 case DW_TAG_ptr_to_member_type:
6928 {
6929 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
6930 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
6931
Greg Claytond88d7592010-09-15 08:33:30 +00006932 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00006933
6934 if (num_attributes > 0) {
6935 uint32_t i;
6936 for (i=0; i<num_attributes; ++i)
6937 {
6938 attr = attributes.AttributeAtIndex(i);
6939 DWARFFormValue form_value;
6940 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6941 {
6942 switch (attr)
6943 {
6944 case DW_AT_type:
6945 type_die_offset = form_value.Reference(dwarf_cu); break;
6946 case DW_AT_containing_type:
6947 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
6948 }
6949 }
6950 }
6951
6952 Type *pointee_type = ResolveTypeUID(type_die_offset);
6953 Type *class_type = ResolveTypeUID(containing_type_die_offset);
6954
Greg Clayton526e5af2010-11-13 03:52:47 +00006955 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
6956 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00006957
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006958 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
6959 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00006960
Greg Clayton526e5af2010-11-13 03:52:47 +00006961 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
6962 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00006963
Greg Clayton81c22f62011-10-19 18:09:39 +00006964 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006965 this,
6966 type_name_const_str,
6967 byte_size,
6968 NULL,
6969 LLDB_INVALID_UID,
6970 Type::eEncodingIsUID,
6971 NULL,
6972 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006973 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00006974 }
6975
6976 break;
6977 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006978 default:
Greg Clayton84afacd2012-11-07 23:09:32 +00006979 GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
6980 die->GetOffset(),
6981 tag,
6982 DW_TAG_value_to_name(tag));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006983 break;
6984 }
6985
6986 if (type_sp.get())
6987 {
6988 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6989 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6990
6991 SymbolContextScope * symbol_context_scope = NULL;
6992 if (sc_parent_tag == DW_TAG_compile_unit)
6993 {
6994 symbol_context_scope = sc.comp_unit;
6995 }
6996 else if (sc.function != NULL)
6997 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006998 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006999 if (symbol_context_scope == NULL)
7000 symbol_context_scope = sc.function;
7001 }
7002
7003 if (symbol_context_scope != NULL)
7004 {
7005 type_sp->SetSymbolContextScope(symbol_context_scope);
7006 }
7007
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00007008 // We are ready to put this type into the uniqued list up at the module level
7009 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00007010
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00007011 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007012 }
7013 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00007014 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007015 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00007016 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007017 }
7018 }
7019 return type_sp;
7020}
7021
7022size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00007023SymbolFileDWARF::ParseTypes
7024(
7025 const SymbolContext& sc,
7026 DWARFCompileUnit* dwarf_cu,
7027 const DWARFDebugInfoEntry *die,
7028 bool parse_siblings,
7029 bool parse_children
7030)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007031{
7032 size_t types_added = 0;
7033 while (die != NULL)
7034 {
7035 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00007036 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007037 {
7038 if (type_is_new)
7039 ++types_added;
7040 }
7041
7042 if (parse_children && die->HasChildren())
7043 {
7044 if (die->Tag() == DW_TAG_subprogram)
7045 {
7046 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00007047 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007048 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
7049 }
7050 else
7051 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
7052 }
7053
7054 if (parse_siblings)
7055 die = die->GetSibling();
7056 else
7057 die = NULL;
7058 }
7059 return types_added;
7060}
7061
7062
7063size_t
7064SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
7065{
7066 assert(sc.comp_unit && sc.function);
7067 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00007068 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007069 if (dwarf_cu)
7070 {
7071 dw_offset_t function_die_offset = sc.function->GetID();
7072 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
7073 if (function_die)
7074 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00007075 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007076 }
7077 }
7078
7079 return functions_added;
7080}
7081
7082
7083size_t
7084SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
7085{
7086 // At least a compile unit must be valid
7087 assert(sc.comp_unit);
7088 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00007089 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007090 if (dwarf_cu)
7091 {
7092 if (sc.function)
7093 {
7094 dw_offset_t function_die_offset = sc.function->GetID();
7095 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
7096 if (func_die && func_die->HasChildren())
7097 {
7098 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
7099 }
7100 }
7101 else
7102 {
7103 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
7104 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
7105 {
7106 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
7107 }
7108 }
7109 }
7110
7111 return types_added;
7112}
7113
7114size_t
7115SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
7116{
7117 if (sc.comp_unit != NULL)
7118 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00007119 DWARFDebugInfo* info = DebugInfo();
7120 if (info == NULL)
7121 return 0;
7122
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007123 if (sc.function)
7124 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007125 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitContainingDIE(sc.function->GetID()).get();
7126
7127 if (dwarf_cu == NULL)
7128 return 0;
7129
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007130 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00007131
Greg Claytonc7bece562013-01-25 18:06:21 +00007132 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
7133 if (func_lo_pc != LLDB_INVALID_ADDRESS)
Greg Claytone38a5ed2012-01-05 03:57:59 +00007134 {
7135 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00007136
Greg Claytone38a5ed2012-01-05 03:57:59 +00007137 // Let all blocks know they have parse all their variables
7138 sc.function->GetBlock (false).SetDidParseVariables (true, true);
7139 return num_variables;
7140 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007141 }
7142 else if (sc.comp_unit)
7143 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007144 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID()).get();
7145
7146 if (dwarf_cu == NULL)
7147 return 0;
7148
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007149 uint32_t vars_added = 0;
7150 VariableListSP variables (sc.comp_unit->GetVariableList(false));
7151
7152 if (variables.get() == NULL)
7153 {
7154 variables.reset(new VariableList());
7155 sc.comp_unit->SetVariableList(variables);
7156
Greg Claytond4a2b372011-09-12 23:21:58 +00007157 DWARFCompileUnit* match_dwarf_cu = NULL;
7158 const DWARFDebugInfoEntry* die = NULL;
7159 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00007160 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00007161 {
Greg Clayton97fbc342011-10-20 22:30:33 +00007162 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00007163 {
7164 DWARFMappedHash::DIEInfoArray hash_data_array;
7165 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
7166 dwarf_cu->GetNextCompileUnitOffset(),
7167 hash_data_array))
7168 {
7169 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
7170 }
7171 }
Greg Clayton7f995132011-10-04 22:41:51 +00007172 }
7173 else
7174 {
7175 // Index if we already haven't to make sure the compile units
7176 // get indexed and make their global DIE index list
7177 if (!m_indexed)
7178 Index ();
7179
7180 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
7181 dwarf_cu->GetNextCompileUnitOffset(),
7182 die_offsets);
7183 }
7184
7185 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00007186 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007187 {
Greg Claytond4a2b372011-09-12 23:21:58 +00007188 DWARFDebugInfo* debug_info = DebugInfo();
7189 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007190 {
Greg Claytond4a2b372011-09-12 23:21:58 +00007191 const dw_offset_t die_offset = die_offsets[i];
7192 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00007193 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00007194 {
Greg Clayton95d87902011-11-11 03:16:25 +00007195 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
7196 if (var_sp)
7197 {
7198 variables->AddVariableIfUnique (var_sp);
7199 ++vars_added;
7200 }
Greg Claytond4a2b372011-09-12 23:21:58 +00007201 }
Greg Clayton95d87902011-11-11 03:16:25 +00007202 else
7203 {
7204 if (m_using_apple_tables)
7205 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00007206 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x)\n", die_offset);
Greg Clayton95d87902011-11-11 03:16:25 +00007207 }
7208 }
7209
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007210 }
7211 }
7212 }
7213 return vars_added;
7214 }
7215 }
7216 return 0;
7217}
7218
7219
7220VariableSP
7221SymbolFileDWARF::ParseVariableDIE
7222(
7223 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00007224 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00007225 const DWARFDebugInfoEntry *die,
7226 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007227)
7228{
7229
Greg Clayton83c5cd92010-11-14 22:13:40 +00007230 VariableSP var_sp (m_die_to_variable_sp[die]);
7231 if (var_sp)
7232 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007233
7234 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00007235
7236 if ((tag == DW_TAG_variable) ||
7237 (tag == DW_TAG_constant) ||
7238 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007239 {
Greg Clayton7f995132011-10-04 22:41:51 +00007240 DWARFDebugInfoEntry::Attributes attributes;
7241 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
7242 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007243 {
Greg Clayton7f995132011-10-04 22:41:51 +00007244 const char *name = NULL;
7245 const char *mangled = NULL;
7246 Declaration decl;
7247 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00007248 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00007249 DWARFExpression location;
7250 bool is_external = false;
7251 bool is_artificial = false;
7252 bool location_is_const_value_data = false;
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007253 bool has_explicit_location = false;
Greg Clayton23f59502012-07-17 03:23:13 +00007254 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00007255
7256 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007257 {
Greg Clayton7f995132011-10-04 22:41:51 +00007258 dw_attr_t attr = attributes.AttributeAtIndex(i);
7259 DWARFFormValue form_value;
7260 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007261 {
Greg Clayton7f995132011-10-04 22:41:51 +00007262 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007263 {
Greg Clayton7f995132011-10-04 22:41:51 +00007264 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
7265 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
7266 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
7267 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton71415542012-12-08 00:24:40 +00007268 case DW_AT_linkage_name:
Greg Clayton7f995132011-10-04 22:41:51 +00007269 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00007270 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00007271 case DW_AT_external: is_external = form_value.Boolean(); break;
Greg Clayton7f995132011-10-04 22:41:51 +00007272 case DW_AT_const_value:
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007273 // If we have already found a DW_AT_location attribute, ignore this attribute.
7274 if (!has_explicit_location)
7275 {
7276 location_is_const_value_data = true;
7277 // The constant value will be either a block, a data value or a string.
7278 const DataExtractor& debug_info_data = get_debug_info_data();
7279 if (DWARFFormValue::IsBlockForm(form_value.Form()))
7280 {
7281 // Retrieve the value as a block expression.
7282 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
7283 uint32_t block_length = form_value.Unsigned();
7284 location.CopyOpcodeData(debug_info_data, block_offset, block_length);
7285 }
7286 else if (DWARFFormValue::IsDataForm(form_value.Form()))
7287 {
7288 // Retrieve the value as a data expression.
7289 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
7290 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
7291 uint32_t data_length = fixed_form_sizes[form_value.Form()];
7292 location.CopyOpcodeData(debug_info_data, data_offset, data_length);
7293 }
7294 else
7295 {
7296 // Retrieve the value as a string expression.
7297 if (form_value.Form() == DW_FORM_strp)
7298 {
7299 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
7300 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
7301 uint32_t data_length = fixed_form_sizes[form_value.Form()];
7302 location.CopyOpcodeData(debug_info_data, data_offset, data_length);
7303 }
7304 else
7305 {
7306 const char *str = form_value.AsCString(&debug_info_data);
7307 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart();
7308 uint32_t string_length = strlen(str) + 1;
7309 location.CopyOpcodeData(debug_info_data, string_offset, string_length);
7310 }
7311 }
7312 }
7313 break;
Greg Clayton7f995132011-10-04 22:41:51 +00007314 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007315 {
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007316 location_is_const_value_data = false;
7317 has_explicit_location = true;
Greg Clayton7f995132011-10-04 22:41:51 +00007318 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007319 {
Greg Clayton7f995132011-10-04 22:41:51 +00007320 const DataExtractor& debug_info_data = get_debug_info_data();
7321
7322 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
7323 uint32_t block_length = form_value.Unsigned();
Greg Clayton7f4c7432012-08-11 00:49:14 +00007324 location.CopyOpcodeData(get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00007325 }
7326 else
7327 {
7328 const DataExtractor& debug_loc_data = get_debug_loc_data();
7329 const dw_offset_t debug_loc_offset = form_value.Unsigned();
7330
7331 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
7332 if (loc_list_length > 0)
7333 {
Greg Clayton7f4c7432012-08-11 00:49:14 +00007334 location.CopyOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00007335 assert (func_low_pc != LLDB_INVALID_ADDRESS);
7336 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
7337 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007338 }
7339 }
Greg Clayton7f995132011-10-04 22:41:51 +00007340 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007341
Greg Clayton1c8ef472013-04-05 23:27:21 +00007342 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00007343 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00007344 case DW_AT_declaration:
7345 case DW_AT_description:
7346 case DW_AT_endianity:
7347 case DW_AT_segment:
7348 case DW_AT_start_scope:
7349 case DW_AT_visibility:
7350 default:
7351 case DW_AT_abstract_origin:
7352 case DW_AT_sibling:
7353 case DW_AT_specification:
7354 break;
7355 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007356 }
7357 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007358
Greg Clayton9e9f2192013-05-17 00:55:28 +00007359 ValueType scope = eValueTypeInvalid;
7360
7361 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
7362 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
7363 SymbolContextScope * symbol_context_scope = NULL;
7364
7365 // DWARF doesn't specify if a DW_TAG_variable is a local, global
7366 // or static variable, so we have to do a little digging by
7367 // looking at the location of a varaible to see if it contains
7368 // a DW_OP_addr opcode _somewhere_ in the definition. I say
7369 // somewhere because clang likes to combine small global variables
7370 // into the same symbol and have locations like:
7371 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
7372 // So if we don't have a DW_TAG_formal_parameter, we can look at
7373 // the location to see if it contains a DW_OP_addr opcode, and
7374 // then we can correctly classify our variables.
7375 if (tag == DW_TAG_formal_parameter)
7376 scope = eValueTypeVariableArgument;
7377 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007378 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007379 bool op_error = false;
7380 // Check if the location has a DW_OP_addr with any address value...
7381 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
7382 if (!location_is_const_value_data)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007383 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007384 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error);
7385 if (op_error)
Greg Clayton96c09682012-01-04 22:56:43 +00007386 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007387 StreamString strm;
7388 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
7389 GetObjectFile()->GetModule()->ReportError ("0x%8.8x: %s has an invalid location: %s", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), strm.GetString().c_str());
Greg Clayton96c09682012-01-04 22:56:43 +00007390 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00007391 }
Greg Claytond1767f02011-12-08 02:13:16 +00007392
Greg Clayton9e9f2192013-05-17 00:55:28 +00007393 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
7394 {
7395 if (is_external)
7396 scope = eValueTypeVariableGlobal;
7397 else
7398 scope = eValueTypeVariableStatic;
7399
7400
7401 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile ();
7402
7403 if (debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007404 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007405 // When leaving the DWARF in the .o files on darwin,
7406 // when we have a global variable that wasn't initialized,
7407 // the .o file might not have allocated a virtual
7408 // address for the global variable. In this case it will
7409 // have created a symbol for the global variable
7410 // that is undefined/data and external and the value will
7411 // be the byte size of the variable. When we do the
7412 // address map in SymbolFileDWARFDebugMap we rely on
7413 // having an address, we need to do some magic here
7414 // so we can get the correct address for our global
7415 // variable. The address for all of these entries
7416 // will be zero, and there will be an undefined symbol
7417 // in this object file, and the executable will have
7418 // a matching symbol with a good address. So here we
7419 // dig up the correct address and replace it in the
7420 // location for the variable, and set the variable's
7421 // symbol context scope to be that of the main executable
7422 // so the file address will resolve correctly.
7423 bool linked_oso_file_addr = false;
7424 if (is_external && location_DW_OP_addr == 0)
Greg Clayton9422dd62013-03-04 21:46:16 +00007425 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007426
7427 // we have a possible uninitialized extern global
7428 ConstString const_name(mangled ? mangled : name);
7429 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
7430 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007431 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007432 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
7433 if (debug_map_symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007434 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007435 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
7436 eSymbolTypeData,
7437 Symtab::eDebugYes,
7438 Symtab::eVisibilityExtern);
7439 if (exe_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007440 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007441 if (exe_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007442 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007443 const addr_t exe_file_addr = exe_symbol->GetAddress().GetFileAddress();
7444 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007445 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007446 if (location.Update_DW_OP_addr (exe_file_addr))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007447 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007448 linked_oso_file_addr = true;
7449 symbol_context_scope = exe_symbol;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007450 }
7451 }
7452 }
7453 }
7454 }
7455 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00007456 }
Greg Clayton9422dd62013-03-04 21:46:16 +00007457
Greg Clayton9e9f2192013-05-17 00:55:28 +00007458 if (!linked_oso_file_addr)
7459 {
7460 // The DW_OP_addr is not zero, but it contains a .o file address which
7461 // needs to be linked up correctly.
7462 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
7463 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +00007464 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007465 // Update the file address for this variable
7466 location.Update_DW_OP_addr (exe_file_addr);
7467 }
7468 else
7469 {
7470 // Variable didn't make it into the final executable
7471 return var_sp;
Greg Clayton9422dd62013-03-04 21:46:16 +00007472 }
Greg Claytond1767f02011-12-08 02:13:16 +00007473 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007474 }
Greg Clayton5cf58b92011-10-05 22:22:08 +00007475 }
7476 else
7477 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007478 scope = eValueTypeVariableLocal;
Greg Clayton5cf58b92011-10-05 22:22:08 +00007479 }
Greg Clayton7f995132011-10-04 22:41:51 +00007480 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00007481
7482 if (symbol_context_scope == NULL)
7483 {
7484 switch (parent_tag)
7485 {
7486 case DW_TAG_subprogram:
7487 case DW_TAG_inlined_subroutine:
7488 case DW_TAG_lexical_block:
7489 if (sc.function)
7490 {
7491 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
7492 if (symbol_context_scope == NULL)
7493 symbol_context_scope = sc.function;
7494 }
7495 break;
7496
7497 default:
7498 symbol_context_scope = sc.comp_unit;
7499 break;
7500 }
7501 }
7502
7503 if (symbol_context_scope)
7504 {
7505 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
7506 name,
7507 mangled,
7508 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
7509 scope,
7510 symbol_context_scope,
7511 &decl,
7512 location,
7513 is_external,
7514 is_artificial));
7515
7516 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
7517 }
7518 else
7519 {
7520 // Not ready to parse this variable yet. It might be a global
7521 // or static variable that is in a function scope and the function
7522 // in the symbol context wasn't filled in yet
7523 return var_sp;
7524 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007525 }
Greg Clayton7f995132011-10-04 22:41:51 +00007526 // Cache var_sp even if NULL (the variable was just a specification or
7527 // was missing vital information to be able to be displayed in the debugger
7528 // (missing location due to optimization, etc)) so we don't re-parse
7529 // this DIE over and over later...
7530 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007531 }
7532 return var_sp;
7533}
7534
Greg Claytonc662ec82011-06-17 22:10:16 +00007535
7536const DWARFDebugInfoEntry *
7537SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
7538 dw_offset_t spec_block_die_offset,
7539 DWARFCompileUnit **result_die_cu_handle)
7540{
7541 // Give the concrete function die specified by "func_die_offset", find the
7542 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
7543 // to "spec_block_die_offset"
7544 DWARFDebugInfo* info = DebugInfo();
7545
7546 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
7547 if (die)
7548 {
7549 assert (*result_die_cu_handle);
7550 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
7551 }
7552 return NULL;
7553}
7554
7555
7556const DWARFDebugInfoEntry *
7557SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
7558 const DWARFDebugInfoEntry *die,
7559 dw_offset_t spec_block_die_offset,
7560 DWARFCompileUnit **result_die_cu_handle)
7561{
7562 if (die)
7563 {
7564 switch (die->Tag())
7565 {
7566 case DW_TAG_subprogram:
7567 case DW_TAG_inlined_subroutine:
7568 case DW_TAG_lexical_block:
7569 {
7570 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
7571 {
7572 *result_die_cu_handle = dwarf_cu;
7573 return die;
7574 }
7575
7576 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
7577 {
7578 *result_die_cu_handle = dwarf_cu;
7579 return die;
7580 }
7581 }
7582 break;
7583 }
7584
7585 // Give the concrete function die specified by "func_die_offset", find the
7586 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
7587 // to "spec_block_die_offset"
7588 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
7589 {
7590 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
7591 child_die,
7592 spec_block_die_offset,
7593 result_die_cu_handle);
7594 if (result_die)
7595 return result_die;
7596 }
7597 }
7598
7599 *result_die_cu_handle = NULL;
7600 return NULL;
7601}
7602
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007603size_t
7604SymbolFileDWARF::ParseVariables
7605(
7606 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00007607 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00007608 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007609 const DWARFDebugInfoEntry *orig_die,
7610 bool parse_siblings,
7611 bool parse_children,
7612 VariableList* cc_variable_list
7613)
7614{
7615 if (orig_die == NULL)
7616 return 0;
7617
Greg Claytonc662ec82011-06-17 22:10:16 +00007618 VariableListSP variable_list_sp;
7619
7620 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007621 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00007622 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007623 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007624 dw_tag_t tag = die->Tag();
7625
7626 // Check to see if we have already parsed this variable or constant?
7627 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007628 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007629 if (cc_variable_list)
7630 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007631 }
7632 else
7633 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007634 // We haven't already parsed it, lets do that now.
7635 if ((tag == DW_TAG_variable) ||
7636 (tag == DW_TAG_constant) ||
7637 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007638 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007639 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00007640 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007641 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
7642 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
7643 switch (parent_tag)
7644 {
7645 case DW_TAG_compile_unit:
7646 if (sc.comp_unit != NULL)
7647 {
7648 variable_list_sp = sc.comp_unit->GetVariableList(false);
7649 if (variable_list_sp.get() == NULL)
7650 {
7651 variable_list_sp.reset(new VariableList());
7652 sc.comp_unit->SetVariableList(variable_list_sp);
7653 }
7654 }
7655 else
7656 {
Daniel Malead01b2952012-11-29 21:49:15 +00007657 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8" PRIx64 " %s with no valid compile unit in symbol context for 0x%8.8" PRIx64 " %s.\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00007658 MakeUserID(sc_parent_die->GetOffset()),
7659 DW_TAG_value_to_name (parent_tag),
7660 MakeUserID(orig_die->GetOffset()),
7661 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007662 }
7663 break;
7664
7665 case DW_TAG_subprogram:
7666 case DW_TAG_inlined_subroutine:
7667 case DW_TAG_lexical_block:
7668 if (sc.function != NULL)
7669 {
7670 // Check to see if we already have parsed the variables for the given scope
7671
Greg Clayton81c22f62011-10-19 18:09:39 +00007672 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007673 if (block == NULL)
7674 {
7675 // This must be a specification or abstract origin with
7676 // a concrete block couterpart in the current function. We need
7677 // to find the concrete block so we can correctly add the
7678 // variable to it
7679 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
7680 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
7681 sc_parent_die->GetOffset(),
7682 &concrete_block_die_cu);
7683 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00007684 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007685 }
7686
7687 if (block != NULL)
7688 {
7689 const bool can_create = false;
7690 variable_list_sp = block->GetBlockVariableList (can_create);
7691 if (variable_list_sp.get() == NULL)
7692 {
7693 variable_list_sp.reset(new VariableList());
7694 block->SetVariableList(variable_list_sp);
7695 }
7696 }
7697 }
7698 break;
7699
7700 default:
Daniel Malead01b2952012-11-29 21:49:15 +00007701 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8" PRIx64 " %s.\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00007702 MakeUserID(orig_die->GetOffset()),
7703 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007704 break;
7705 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00007706 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007707
7708 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007709 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00007710 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
7711 if (var_sp)
7712 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007713 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00007714 if (cc_variable_list)
7715 cc_variable_list->AddVariableIfUnique (var_sp);
7716 ++vars_added;
7717 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007718 }
7719 }
7720 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007721
7722 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
7723
7724 if (!skip_children && parse_children && die->HasChildren())
7725 {
7726 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
7727 }
7728
7729 if (parse_siblings)
7730 die = die->GetSibling();
7731 else
7732 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007733 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007734 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007735}
7736
7737//------------------------------------------------------------------
7738// PluginInterface protocol
7739//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00007740ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007741SymbolFileDWARF::GetPluginName()
7742{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007743 return GetPluginNameStatic();
7744}
7745
7746uint32_t
7747SymbolFileDWARF::GetPluginVersion()
7748{
7749 return 1;
7750}
7751
7752void
Greg Clayton6beaaa62011-01-17 03:46:26 +00007753SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
7754{
7755 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7756 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7757 if (clang_type)
7758 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7759}
7760
7761void
7762SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
7763{
7764 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7765 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7766 if (clang_type)
7767 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7768}
7769
Greg Claytona2721472011-06-25 00:44:06 +00007770void
Sean Callanancc427fa2011-07-30 02:42:06 +00007771SymbolFileDWARF::DumpIndexes ()
7772{
7773 StreamFile s(stdout, false);
7774
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00007775 s.Printf ("DWARF index for (%s) '%s':",
Sean Callanancc427fa2011-07-30 02:42:06 +00007776 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00007777 GetObjectFile()->GetFileSpec().GetPath().c_str());
Sean Callanancc427fa2011-07-30 02:42:06 +00007778 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
7779 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
7780 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
7781 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
7782 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
7783 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
7784 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
7785 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
7786}
7787
7788void
7789SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
7790 const char *name,
7791 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00007792{
Sean Callanancc427fa2011-07-30 02:42:06 +00007793 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00007794
7795 if (iter == m_decl_ctx_to_die.end())
7796 return;
7797
Greg Claytoncb5860a2011-10-13 23:49:28 +00007798 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00007799 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007800 const DWARFDebugInfoEntry *context_die = *pos;
7801
7802 if (!results)
7803 return;
7804
7805 DWARFDebugInfo* info = DebugInfo();
7806
7807 DIEArray die_offsets;
7808
7809 DWARFCompileUnit* dwarf_cu = NULL;
7810 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00007811
7812 if (m_using_apple_tables)
7813 {
7814 if (m_apple_types_ap.get())
7815 m_apple_types_ap->FindByName (name, die_offsets);
7816 }
7817 else
7818 {
7819 if (!m_indexed)
7820 Index ();
7821
7822 m_type_index.Find (ConstString(name), die_offsets);
7823 }
7824
Greg Clayton220a0072011-12-09 08:48:30 +00007825 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007826
7827 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00007828 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007829 for (size_t i = 0; i < num_matches; ++i)
7830 {
7831 const dw_offset_t die_offset = die_offsets[i];
7832 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00007833
Greg Claytoncb5860a2011-10-13 23:49:28 +00007834 if (die->GetParent() != context_die)
7835 continue;
7836
7837 Type *matching_type = ResolveType (dwarf_cu, die);
7838
Greg Clayton42ce2f32011-12-12 21:50:19 +00007839 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007840 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
7841
7842 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
7843 {
7844 clang::TagDecl *tag_decl = tag_type->getDecl();
7845 results->push_back(tag_decl);
7846 }
7847 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
7848 {
7849 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
7850 results->push_back(typedef_decl);
7851 }
Greg Claytona2721472011-06-25 00:44:06 +00007852 }
7853 }
7854 }
7855}
7856
7857void
7858SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00007859 const clang::DeclContext *decl_context,
7860 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00007861 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
7862{
Greg Clayton85ae2e12011-10-18 23:36:41 +00007863
7864 switch (decl_context->getDeclKind())
7865 {
7866 case clang::Decl::Namespace:
7867 case clang::Decl::TranslationUnit:
7868 {
7869 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7870 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
7871 }
7872 break;
7873 default:
7874 break;
7875 }
Greg Claytona2721472011-06-25 00:44:06 +00007876}
Greg Claytoncaab74e2012-01-28 00:48:57 +00007877
7878bool
7879SymbolFileDWARF::LayoutRecordType (void *baton,
7880 const clang::RecordDecl *record_decl,
7881 uint64_t &size,
7882 uint64_t &alignment,
7883 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7884 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7885 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7886{
7887 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7888 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
7889}
7890
7891
7892bool
7893SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
7894 uint64_t &bit_size,
7895 uint64_t &alignment,
7896 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7897 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7898 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7899{
Greg Clayton5160ce52013-03-27 23:08:40 +00007900 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Claytoncaab74e2012-01-28 00:48:57 +00007901 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
7902 bool success = false;
7903 base_offsets.clear();
7904 vbase_offsets.clear();
7905 if (pos != m_record_decl_to_layout_map.end())
7906 {
7907 bit_size = pos->second.bit_size;
7908 alignment = pos->second.alignment;
7909 field_offsets.swap(pos->second.field_offsets);
Greg Clayton2508b9b2012-11-01 23:20:02 +00007910 base_offsets.swap (pos->second.base_offsets);
7911 vbase_offsets.swap (pos->second.vbase_offsets);
Greg Claytoncaab74e2012-01-28 00:48:57 +00007912 m_record_decl_to_layout_map.erase(pos);
7913 success = true;
7914 }
7915 else
7916 {
7917 bit_size = 0;
7918 alignment = 0;
7919 field_offsets.clear();
7920 }
7921
7922 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00007923 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00007924 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
Greg Claytoncaab74e2012-01-28 00:48:57 +00007925 record_decl,
7926 bit_size,
7927 alignment,
7928 (uint32_t)field_offsets.size(),
7929 (uint32_t)base_offsets.size(),
7930 (uint32_t)vbase_offsets.size(),
7931 success);
7932 return success;
7933}
7934
7935
Greg Clayton1f746072012-08-29 21:13:06 +00007936SymbolFileDWARFDebugMap *
7937SymbolFileDWARF::GetDebugMapSymfile ()
7938{
7939 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
7940 {
7941 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
7942 if (module_sp)
7943 {
7944 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
7945 if (sym_vendor)
7946 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
7947 }
7948 }
7949 return m_debug_map_symfile;
7950}
7951
Greg Claytoncaab74e2012-01-28 00:48:57 +00007952