blob: 4d1015668c4b1d49d1c32acaf1eebeb54f7b539b [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
Matthew Gardinere81df3b2014-08-26 06:57:23 +000069#include <ctype.h>
70#include <string.h>
71
Greg Clayton62742b12010-11-11 01:09:45 +000072//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000073
74#ifdef ENABLE_DEBUG_PRINTF
75#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000076#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000077#else
78#define DEBUG_PRINTF(fmt, ...)
79#endif
80
Greg Clayton594e5ed2010-09-27 21:07:38 +000081#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082
83using namespace lldb;
84using namespace lldb_private;
85
Greg Clayton219cf312012-03-30 00:51:13 +000086//static inline bool
87//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
88//{
89// switch (tag)
90// {
91// default:
92// break;
93// case DW_TAG_subprogram:
94// case DW_TAG_inlined_subroutine:
95// case DW_TAG_class_type:
96// case DW_TAG_structure_type:
97// case DW_TAG_union_type:
98// return true;
99// }
100// return false;
101//}
102//
Sean Callananc7fbf732010-08-06 00:32:49 +0000103static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000104DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105{
106 switch (dwarf_accessibility)
107 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000108 case DW_ACCESS_public: return eAccessPublic;
109 case DW_ACCESS_private: return eAccessPrivate;
110 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000111 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000113 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114}
115
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000116static const char*
117removeHostnameFromPathname(const char* path_from_dwarf)
118{
119 if (!path_from_dwarf || !path_from_dwarf[0])
120 {
121 return path_from_dwarf;
122 }
123
124 const char *colon_pos = strchr(path_from_dwarf, ':');
125 if (!colon_pos)
126 {
127 return path_from_dwarf;
128 }
129
130 // check whether we have a windows path, and so the first character
131 // is a drive-letter not a hostname.
132 if (
133 colon_pos == path_from_dwarf + 1 &&
134 isalpha(*path_from_dwarf) &&
135 strlen(path_from_dwarf) > 2 &&
136 '\\' == path_from_dwarf[2])
137 {
138 return path_from_dwarf;
139 }
140
141 return colon_pos + 1;
142}
143
Todd Fiala0a70a842014-05-28 16:43:26 +0000144#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
Greg Clayton1fba87112012-02-09 20:03:49 +0000145
146class DIEStack
147{
148public:
149
150 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
151 {
152 m_dies.push_back (DIEInfo(cu, die));
153 }
154
155
156 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
157 {
158 StreamString log_strm;
159 const size_t n = m_dies.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000160 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
Greg Clayton1fba87112012-02-09 20:03:49 +0000161 for (size_t i=0; i<n; i++)
162 {
163 DWARFCompileUnit *cu = m_dies[i].cu;
164 const DWARFDebugInfoEntry *die = m_dies[i].die;
165 std::string qualified_name;
166 die->GetQualifiedName(dwarf, cu, qualified_name);
Daniel Malead01b2952012-11-29 21:49:15 +0000167 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n",
Greg Clayton43e0af02012-09-18 18:04:04 +0000168 (uint64_t)i,
Greg Clayton1fba87112012-02-09 20:03:49 +0000169 die->GetOffset(),
170 DW_TAG_value_to_name(die->Tag()),
171 qualified_name.c_str());
172 }
173 log->PutCString(log_strm.GetData());
174 }
175 void Pop ()
176 {
177 m_dies.pop_back();
178 }
179
180 class ScopedPopper
181 {
182 public:
183 ScopedPopper (DIEStack &die_stack) :
184 m_die_stack (die_stack),
185 m_valid (false)
186 {
187 }
188
189 void
190 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
191 {
192 m_valid = true;
193 m_die_stack.Push (cu, die);
194 }
195
196 ~ScopedPopper ()
197 {
198 if (m_valid)
199 m_die_stack.Pop();
200 }
201
202
203
204 protected:
205 DIEStack &m_die_stack;
206 bool m_valid;
207 };
208
209protected:
210 struct DIEInfo {
211 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
212 cu(c),
213 die(d)
214 {
215 }
216 DWARFCompileUnit *cu;
217 const DWARFDebugInfoEntry *die;
218 };
219 typedef std::vector<DIEInfo> Stack;
220 Stack m_dies;
221};
222#endif
223
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224void
225SymbolFileDWARF::Initialize()
226{
227 LogChannelDWARF::Initialize();
228 PluginManager::RegisterPlugin (GetPluginNameStatic(),
229 GetPluginDescriptionStatic(),
230 CreateInstance);
231}
232
233void
234SymbolFileDWARF::Terminate()
235{
236 PluginManager::UnregisterPlugin (CreateInstance);
237 LogChannelDWARF::Initialize();
238}
239
240
Greg Clayton57abc5d2013-05-10 21:47:16 +0000241lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242SymbolFileDWARF::GetPluginNameStatic()
243{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000244 static ConstString g_name("dwarf");
245 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
248const char *
249SymbolFileDWARF::GetPluginDescriptionStatic()
250{
251 return "DWARF and DWARF3 debug symbol file reader.";
252}
253
254
255SymbolFile*
256SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
257{
258 return new SymbolFileDWARF(obj_file);
259}
260
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000261TypeList *
262SymbolFileDWARF::GetTypeList ()
263{
Greg Clayton1f746072012-08-29 21:13:06 +0000264 if (GetDebugMapSymfile ())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000265 return m_debug_map_symfile->GetTypeList();
266 return m_obj_file->GetModule()->GetTypeList();
267
268}
Greg Claytonf02500c2013-06-18 22:51:05 +0000269void
270SymbolFileDWARF::GetTypes (DWARFCompileUnit* cu,
271 const DWARFDebugInfoEntry *die,
272 dw_offset_t min_die_offset,
273 dw_offset_t max_die_offset,
274 uint32_t type_mask,
275 TypeSet &type_set)
276{
277 if (cu)
278 {
279 if (die)
280 {
281 const dw_offset_t die_offset = die->GetOffset();
282
283 if (die_offset >= max_die_offset)
284 return;
285
286 if (die_offset >= min_die_offset)
287 {
288 const dw_tag_t tag = die->Tag();
289
290 bool add_type = false;
291
292 switch (tag)
293 {
294 case DW_TAG_array_type: add_type = (type_mask & eTypeClassArray ) != 0; break;
295 case DW_TAG_unspecified_type:
296 case DW_TAG_base_type: add_type = (type_mask & eTypeClassBuiltin ) != 0; break;
297 case DW_TAG_class_type: add_type = (type_mask & eTypeClassClass ) != 0; break;
298 case DW_TAG_structure_type: add_type = (type_mask & eTypeClassStruct ) != 0; break;
299 case DW_TAG_union_type: add_type = (type_mask & eTypeClassUnion ) != 0; break;
300 case DW_TAG_enumeration_type: add_type = (type_mask & eTypeClassEnumeration ) != 0; break;
301 case DW_TAG_subroutine_type:
302 case DW_TAG_subprogram:
303 case DW_TAG_inlined_subroutine: add_type = (type_mask & eTypeClassFunction ) != 0; break;
304 case DW_TAG_pointer_type: add_type = (type_mask & eTypeClassPointer ) != 0; break;
305 case DW_TAG_rvalue_reference_type:
306 case DW_TAG_reference_type: add_type = (type_mask & eTypeClassReference ) != 0; break;
307 case DW_TAG_typedef: add_type = (type_mask & eTypeClassTypedef ) != 0; break;
308 case DW_TAG_ptr_to_member_type: add_type = (type_mask & eTypeClassMemberPointer ) != 0; break;
309 }
310
311 if (add_type)
312 {
313 const bool assert_not_being_parsed = true;
314 Type *type = ResolveTypeUID (cu, die, assert_not_being_parsed);
315 if (type)
316 {
317 if (type_set.find(type) == type_set.end())
318 type_set.insert(type);
319 }
320 }
321 }
322
323 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild();
324 child_die != NULL;
325 child_die = child_die->GetSibling())
326 {
327 GetTypes (cu, child_die, min_die_offset, max_die_offset, type_mask, type_set);
328 }
329 }
330 }
331}
332
333size_t
334SymbolFileDWARF::GetTypes (SymbolContextScope *sc_scope,
335 uint32_t type_mask,
336 TypeList &type_list)
337
338{
339 TypeSet type_set;
340
341 CompileUnit *comp_unit = NULL;
342 DWARFCompileUnit* dwarf_cu = NULL;
343 if (sc_scope)
344 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
345
346 if (comp_unit)
347 {
348 dwarf_cu = GetDWARFCompileUnit(comp_unit);
349 if (dwarf_cu == 0)
350 return 0;
351 GetTypes (dwarf_cu,
352 dwarf_cu->DIE(),
353 dwarf_cu->GetOffset(),
354 dwarf_cu->GetNextCompileUnitOffset(),
355 type_mask,
356 type_set);
357 }
358 else
359 {
360 DWARFDebugInfo* info = DebugInfo();
361 if (info)
362 {
363 const size_t num_cus = info->GetNumCompileUnits();
364 for (size_t cu_idx=0; cu_idx<num_cus; ++cu_idx)
365 {
366 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
367 if (dwarf_cu)
368 {
369 GetTypes (dwarf_cu,
370 dwarf_cu->DIE(),
371 0,
372 UINT32_MAX,
373 type_mask,
374 type_set);
375 }
376 }
377 }
378 }
379// if (m_using_apple_tables)
380// {
381// DWARFMappedHash::MemoryTable *apple_types = m_apple_types_ap.get();
382// if (apple_types)
383// {
384// apple_types->ForEach([this, &type_set, apple_types, type_mask](const DWARFMappedHash::DIEInfoArray &die_info_array) -> bool {
385//
386// for (auto die_info: die_info_array)
387// {
388// bool add_type = TagMatchesTypeMask (type_mask, 0);
389// if (!add_type)
390// {
391// dw_tag_t tag = die_info.tag;
392// if (tag == 0)
393// {
394// const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_info.offset, NULL);
395// tag = die->Tag();
396// }
397// add_type = TagMatchesTypeMask (type_mask, tag);
398// }
399// if (add_type)
400// {
401// Type *type = ResolveTypeUID(die_info.offset);
402//
403// if (type_set.find(type) == type_set.end())
404// type_set.insert(type);
405// }
406// }
407// return true; // Keep iterating
408// });
409// }
410// }
411// else
412// {
413// if (!m_indexed)
414// Index ();
415//
416// m_type_index.ForEach([this, &type_set, type_mask](const char *name, uint32_t die_offset) -> bool {
417//
418// bool add_type = TagMatchesTypeMask (type_mask, 0);
419//
420// if (!add_type)
421// {
422// const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_offset, NULL);
423// if (die)
424// {
425// const dw_tag_t tag = die->Tag();
426// add_type = TagMatchesTypeMask (type_mask, tag);
427// }
428// }
429//
430// if (add_type)
431// {
432// Type *type = ResolveTypeUID(die_offset);
433//
434// if (type_set.find(type) == type_set.end())
435// type_set.insert(type);
436// }
437// return true; // Keep iterating
438// });
439// }
440
Greg Clayton57ee3062013-07-11 22:46:58 +0000441 std::set<ClangASTType> clang_type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000442 size_t num_types_added = 0;
443 for (Type *type : type_set)
444 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000445 ClangASTType clang_type = type->GetClangForwardType();
Greg Clayton0fc4f312013-06-20 01:23:18 +0000446 if (clang_type_set.find(clang_type) == clang_type_set.end())
447 {
448 clang_type_set.insert(clang_type);
449 type_list.Insert (type->shared_from_this());
450 ++num_types_added;
451 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000452 }
453 return num_types_added;
454}
455
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000456
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457//----------------------------------------------------------------------
458// Gets the first parent that is a lexical block, function or inlined
459// subroutine, or compile unit.
460//----------------------------------------------------------------------
461static const DWARFDebugInfoEntry *
462GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
463{
464 const DWARFDebugInfoEntry *die;
465 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
466 {
467 dw_tag_t tag = die->Tag();
468
469 switch (tag)
470 {
471 case DW_TAG_compile_unit:
472 case DW_TAG_subprogram:
473 case DW_TAG_inlined_subroutine:
474 case DW_TAG_lexical_block:
475 return die;
476 }
477 }
478 return NULL;
479}
480
481
Greg Clayton450e3f32010-10-12 02:24:53 +0000482SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
483 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000484 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 +0000485 m_debug_map_module_wp (),
Greg Clayton450e3f32010-10-12 02:24:53 +0000486 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000487 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000489 m_data_debug_abbrev (),
490 m_data_debug_aranges (),
491 m_data_debug_frame (),
492 m_data_debug_info (),
493 m_data_debug_line (),
494 m_data_debug_loc (),
495 m_data_debug_ranges (),
496 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000497 m_data_apple_names (),
498 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000499 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501 m_info(),
502 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000503 m_apple_names_ap (),
504 m_apple_types_ap (),
505 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000506 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000507 m_function_basename_index(),
508 m_function_fullname_index(),
509 m_function_method_index(),
510 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000511 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000512 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000513 m_type_index(),
514 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000515 m_indexed (false),
516 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000517 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000518 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000519 m_ranges(),
520 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521{
522}
523
524SymbolFileDWARF::~SymbolFileDWARF()
525{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000526 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000527 {
528 ModuleSP module_sp (m_obj_file->GetModule());
529 if (module_sp)
530 module_sp->GetClangASTContext().RemoveExternalSource ();
531 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000532}
533
534static const ConstString &
535GetDWARFMachOSegmentName ()
536{
537 static ConstString g_dwarf_section_name ("__DWARF");
538 return g_dwarf_section_name;
539}
540
Greg Claytone576ab22011-02-15 00:19:15 +0000541UniqueDWARFASTTypeMap &
542SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
543{
Greg Clayton1f746072012-08-29 21:13:06 +0000544 if (GetDebugMapSymfile ())
Greg Claytone576ab22011-02-15 00:19:15 +0000545 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
546 return m_unique_ast_type_map;
547}
548
Greg Clayton6beaaa62011-01-17 03:46:26 +0000549ClangASTContext &
550SymbolFileDWARF::GetClangASTContext ()
551{
Greg Clayton1f746072012-08-29 21:13:06 +0000552 if (GetDebugMapSymfile ())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000553 return m_debug_map_symfile->GetClangASTContext ();
554
555 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
556 if (!m_is_external_ast_source)
557 {
558 m_is_external_ast_source = true;
Todd Fiala955fe6f2014-02-27 17:18:23 +0000559 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (
Greg Clayton6beaaa62011-01-17 03:46:26 +0000560 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
561 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000562 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000563 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000564 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000565 ast.SetExternalSource (ast_source_ap);
566 }
567 return ast;
568}
569
570void
571SymbolFileDWARF::InitializeObject()
572{
573 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000574 ModuleSP module_sp (m_obj_file->GetModule());
575 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000576 {
Greg Clayton3046e662013-07-10 01:23:25 +0000577 const SectionList *section_list = module_sp->GetSectionList();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000578
579 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
580
581 // Memory map the DWARF mach-o segment so we have everything mmap'ed
582 // to keep our heap memory usage down.
583 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000584 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000585 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000586 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000587 if (m_data_apple_names.GetByteSize() > 0)
588 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000589 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
590 if (m_apple_names_ap->IsValid())
591 m_using_apple_tables = true;
592 else
Greg Clayton7f995132011-10-04 22:41:51 +0000593 m_apple_names_ap.reset();
594 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000595 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000596 if (m_data_apple_types.GetByteSize() > 0)
597 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000598 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
599 if (m_apple_types_ap->IsValid())
600 m_using_apple_tables = true;
601 else
Greg Clayton7f995132011-10-04 22:41:51 +0000602 m_apple_types_ap.reset();
603 }
604
605 get_apple_namespaces_data();
606 if (m_data_apple_namespaces.GetByteSize() > 0)
607 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000608 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
609 if (m_apple_namespaces_ap->IsValid())
610 m_using_apple_tables = true;
611 else
Greg Clayton7f995132011-10-04 22:41:51 +0000612 m_apple_namespaces_ap.reset();
613 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000614
Greg Clayton5009f9d2011-10-27 17:55:14 +0000615 get_apple_objc_data();
616 if (m_data_apple_objc.GetByteSize() > 0)
617 {
618 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
619 if (m_apple_objc_ap->IsValid())
620 m_using_apple_tables = true;
621 else
622 m_apple_objc_ap.reset();
623 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624}
625
626bool
627SymbolFileDWARF::SupportedVersion(uint16_t version)
628{
Greg Claytonabcbfe52013-04-04 00:00:36 +0000629 return version == 2 || version == 3 || version == 4;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630}
631
632uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000633SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634{
635 uint32_t abilities = 0;
636 if (m_obj_file != NULL)
637 {
638 const Section* section = NULL;
639 const SectionList *section_list = m_obj_file->GetSectionList();
640 if (section_list == NULL)
641 return 0;
642
643 uint64_t debug_abbrev_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644 uint64_t debug_info_file_size = 0;
645 uint64_t debug_line_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646
Greg Clayton6beaaa62011-01-17 03:46:26 +0000647 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648
649 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000650 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651
Greg Clayton4ceb9982010-07-21 22:54:26 +0000652 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653 if (section != NULL)
654 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000655 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656
Greg Clayton4ceb9982010-07-21 22:54:26 +0000657 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000659 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660 else
661 m_flags.Set (flagsGotDebugAbbrevData);
662
Greg Clayton4ceb9982010-07-21 22:54:26 +0000663 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000664 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 m_flags.Set (flagsGotDebugArangesData);
666
Greg Clayton4ceb9982010-07-21 22:54:26 +0000667 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000668 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 m_flags.Set (flagsGotDebugFrameData);
670
Greg Clayton4ceb9982010-07-21 22:54:26 +0000671 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000673 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 else
675 m_flags.Set (flagsGotDebugLineData);
676
Greg Clayton4ceb9982010-07-21 22:54:26 +0000677 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000678 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679 m_flags.Set (flagsGotDebugLocData);
680
Greg Clayton4ceb9982010-07-21 22:54:26 +0000681 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000682 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 m_flags.Set (flagsGotDebugMacInfoData);
684
Greg Clayton4ceb9982010-07-21 22:54:26 +0000685 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000686 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 m_flags.Set (flagsGotDebugPubNamesData);
688
Greg Clayton4ceb9982010-07-21 22:54:26 +0000689 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000690 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691 m_flags.Set (flagsGotDebugPubTypesData);
692
Greg Clayton4ceb9982010-07-21 22:54:26 +0000693 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000694 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 m_flags.Set (flagsGotDebugRangesData);
696
Greg Clayton4ceb9982010-07-21 22:54:26 +0000697 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000698 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 m_flags.Set (flagsGotDebugStrData);
700 }
Greg Clayton6c596612012-05-18 21:47:20 +0000701 else
702 {
703 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString();
704 if (symfile_dir_cstr)
705 {
706 if (strcasestr(symfile_dir_cstr, ".dsym"))
707 {
708 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo)
709 {
710 // We have a dSYM file that didn't have a any debug info.
711 // If the string table has a size of 1, then it was made from
712 // an executable with no debug info, or from an executable that
713 // was stripped.
714 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
715 if (section && section->GetFileSize() == 1)
716 {
717 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info.");
718 }
719 }
720 }
721 }
722 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723
724 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
725 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
726
727 if (debug_line_file_size > 0)
728 abilities |= LineTables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 }
730 return abilities;
731}
732
Ed Masteeeae7212013-10-24 20:43:47 +0000733const DWARFDataExtractor&
734SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DWARFDataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735{
736 if (m_flags.IsClear (got_flag))
737 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000738 ModuleSP module_sp (m_obj_file->GetModule());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739 m_flags.Set (got_flag);
Greg Clayton3046e662013-07-10 01:23:25 +0000740 const SectionList *section_list = module_sp->GetSectionList();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741 if (section_list)
742 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000743 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
744 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 {
746 // See if we memory mapped the DWARF segment?
747 if (m_dwarf_data.GetByteSize())
748 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000749 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750 }
751 else
752 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000753 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754 data.Clear();
755 }
756 }
757 }
758 }
759 return data;
760}
761
Ed Masteeeae7212013-10-24 20:43:47 +0000762const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763SymbolFileDWARF::get_debug_abbrev_data()
764{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000765 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766}
767
Ed Masteeeae7212013-10-24 20:43:47 +0000768const DWARFDataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000769SymbolFileDWARF::get_debug_aranges_data()
770{
771 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
772}
773
Ed Masteeeae7212013-10-24 20:43:47 +0000774const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775SymbolFileDWARF::get_debug_frame_data()
776{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000777 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000778}
779
Ed Masteeeae7212013-10-24 20:43:47 +0000780const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781SymbolFileDWARF::get_debug_info_data()
782{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000783 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784}
785
Ed Masteeeae7212013-10-24 20:43:47 +0000786const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000787SymbolFileDWARF::get_debug_line_data()
788{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000789 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790}
791
Ed Masteeeae7212013-10-24 20:43:47 +0000792const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793SymbolFileDWARF::get_debug_loc_data()
794{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000795 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796}
797
Ed Masteeeae7212013-10-24 20:43:47 +0000798const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799SymbolFileDWARF::get_debug_ranges_data()
800{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000801 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802}
803
Ed Masteeeae7212013-10-24 20:43:47 +0000804const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805SymbolFileDWARF::get_debug_str_data()
806{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000807 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808}
809
Ed Masteeeae7212013-10-24 20:43:47 +0000810const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000811SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000812{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000813 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000814}
815
Ed Masteeeae7212013-10-24 20:43:47 +0000816const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000817SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000818{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000819 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000820}
821
Ed Masteeeae7212013-10-24 20:43:47 +0000822const DWARFDataExtractor&
Greg Clayton7f995132011-10-04 22:41:51 +0000823SymbolFileDWARF::get_apple_namespaces_data()
824{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000825 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
826}
827
Ed Masteeeae7212013-10-24 20:43:47 +0000828const DWARFDataExtractor&
Greg Clayton5009f9d2011-10-27 17:55:14 +0000829SymbolFileDWARF::get_apple_objc_data()
830{
831 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000832}
833
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834
835DWARFDebugAbbrev*
836SymbolFileDWARF::DebugAbbrev()
837{
838 if (m_abbr.get() == NULL)
839 {
Ed Masteeeae7212013-10-24 20:43:47 +0000840 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 if (debug_abbrev_data.GetByteSize() > 0)
842 {
843 m_abbr.reset(new DWARFDebugAbbrev());
844 if (m_abbr.get())
845 m_abbr->Parse(debug_abbrev_data);
846 }
847 }
848 return m_abbr.get();
849}
850
851const DWARFDebugAbbrev*
852SymbolFileDWARF::DebugAbbrev() const
853{
854 return m_abbr.get();
855}
856
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857
858DWARFDebugInfo*
859SymbolFileDWARF::DebugInfo()
860{
861 if (m_info.get() == NULL)
862 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000863 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
864 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 if (get_debug_info_data().GetByteSize() > 0)
866 {
867 m_info.reset(new DWARFDebugInfo());
868 if (m_info.get())
869 {
870 m_info->SetDwarfData(this);
871 }
872 }
873 }
874 return m_info.get();
875}
876
877const DWARFDebugInfo*
878SymbolFileDWARF::DebugInfo() const
879{
880 return m_info.get();
881}
882
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883DWARFCompileUnit*
Greg Clayton1f746072012-08-29 21:13:06 +0000884SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885{
886 DWARFDebugInfo* info = DebugInfo();
Greg Clayton1f746072012-08-29 21:13:06 +0000887 if (info)
888 {
889 if (GetDebugMapSymfile ())
890 {
891 // The debug map symbol file made the compile units for this DWARF
892 // file which is .o file with DWARF in it, and we should have
893 // only 1 compile unit which is at offset zero in the DWARF.
894 // TODO: modify to support LTO .o files where each .o file might
895 // have multiple DW_TAG_compile_unit tags.
896 return info->GetCompileUnit(0).get();
897 }
898 else
899 {
900 // Just a normal DWARF file whose user ID for the compile unit is
901 // the DWARF offset itself
902 return info->GetCompileUnit((dw_offset_t)comp_unit->GetID()).get();
903 }
904 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000905 return NULL;
906}
907
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908
909DWARFDebugRanges*
910SymbolFileDWARF::DebugRanges()
911{
912 if (m_ranges.get() == NULL)
913 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000914 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
915 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 if (get_debug_ranges_data().GetByteSize() > 0)
917 {
918 m_ranges.reset(new DWARFDebugRanges());
919 if (m_ranges.get())
920 m_ranges->Extract(this);
921 }
922 }
923 return m_ranges.get();
924}
925
926const DWARFDebugRanges*
927SymbolFileDWARF::DebugRanges() const
928{
929 return m_ranges.get();
930}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931
Greg Clayton53eb1c22012-04-02 22:59:12 +0000932lldb::CompUnitSP
933SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000935 CompUnitSP cu_sp;
936 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000937 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000938 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
939 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000941 // We already parsed this compile unit, had out a shared pointer to it
942 cu_sp = comp_unit->shared_from_this();
943 }
944 else
945 {
Greg Clayton1f746072012-08-29 21:13:06 +0000946 if (GetDebugMapSymfile ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947 {
Greg Clayton1f746072012-08-29 21:13:06 +0000948 // Let the debug map create the compile unit
949 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
950 dwarf_cu->SetUserData(cu_sp.get());
951 }
952 else
953 {
954 ModuleSP module_sp (m_obj_file->GetModule());
955 if (module_sp)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000956 {
Greg Clayton1f746072012-08-29 21:13:06 +0000957 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
958 if (cu_die)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000959 {
Greg Clayton1f746072012-08-29 21:13:06 +0000960 const char * cu_die_name = cu_die->GetName(this, dwarf_cu);
961 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
962 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
963 if (cu_die_name)
964 {
965 std::string ramapped_file;
966 FileSpec cu_file_spec;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000967
Greg Clayton1f746072012-08-29 21:13:06 +0000968 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Greg Clayton53eb1c22012-04-02 22:59:12 +0000969 {
Greg Clayton1f746072012-08-29 21:13:06 +0000970 // If we have a full path to the compile unit, we don't need to resolve
971 // the file. This can be expensive e.g. when the source files are NFS mounted.
972 if (module_sp->RemapSourceFile(cu_die_name, ramapped_file))
973 cu_file_spec.SetFile (ramapped_file.c_str(), false);
974 else
975 cu_file_spec.SetFile (cu_die_name, false);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000976 }
977 else
978 {
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000979 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
980 // Remove the host part if present.
981 cu_comp_dir = removeHostnameFromPathname(cu_comp_dir);
Greg Clayton1f746072012-08-29 21:13:06 +0000982 std::string fullpath(cu_comp_dir);
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000983
Greg Clayton1f746072012-08-29 21:13:06 +0000984 if (*fullpath.rbegin() != '/')
985 fullpath += '/';
986 fullpath += cu_die_name;
987 if (module_sp->RemapSourceFile (fullpath.c_str(), ramapped_file))
988 cu_file_spec.SetFile (ramapped_file.c_str(), false);
989 else
990 cu_file_spec.SetFile (fullpath.c_str(), false);
991 }
992
993 cu_sp.reset(new CompileUnit (module_sp,
994 dwarf_cu,
995 cu_file_spec,
996 MakeUserID(dwarf_cu->GetOffset()),
997 cu_language));
998 if (cu_sp)
999 {
1000 dwarf_cu->SetUserData(cu_sp.get());
1001
Greg Clayton53eb1c22012-04-02 22:59:12 +00001002 // Figure out the compile unit index if we weren't given one
1003 if (cu_idx == UINT32_MAX)
1004 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
1005
1006 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
1007 }
1008 }
1009 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010 }
1011 }
1012 }
1013 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001014 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015}
1016
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017uint32_t
1018SymbolFileDWARF::GetNumCompileUnits()
1019{
1020 DWARFDebugInfo* info = DebugInfo();
1021 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023 return 0;
1024}
1025
1026CompUnitSP
1027SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
1028{
Greg Clayton53eb1c22012-04-02 22:59:12 +00001029 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030 DWARFDebugInfo* info = DebugInfo();
1031 if (info)
1032 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001033 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
1034 if (dwarf_cu)
1035 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001036 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001037 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038}
1039
1040static void
Greg Claytonea3e7d52011-10-08 00:49:15 +00001041AddRangesToBlock (Block& block,
1042 DWARFDebugRanges::RangeList& ranges,
1043 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044{
Greg Claytonea3e7d52011-10-08 00:49:15 +00001045 const size_t num_ranges = ranges.GetSize();
1046 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001048 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
1049 const addr_t range_base = range.GetRangeBase();
1050 assert (range_base >= block_base_addr);
1051 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 }
Greg Claytonea3e7d52011-10-08 00:49:15 +00001053 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054}
1055
1056
1057Function *
Greg Clayton0fffff52010-09-24 05:15:53 +00001058SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059{
1060 DWARFDebugRanges::RangeList func_ranges;
1061 const char *name = NULL;
1062 const char *mangled = NULL;
1063 int decl_file = 0;
1064 int decl_line = 0;
1065 int decl_column = 0;
1066 int call_file = 0;
1067 int call_line = 0;
1068 int call_column = 0;
1069 DWARFExpression frame_base;
1070
Greg Claytonc93237c2010-10-01 20:48:32 +00001071 assert (die->Tag() == DW_TAG_subprogram);
1072
1073 if (die->Tag() != DW_TAG_subprogram)
1074 return NULL;
1075
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001076 if (die->GetDIENamesAndRanges (this,
1077 dwarf_cu,
1078 name,
1079 mangled,
1080 func_ranges,
1081 decl_file,
1082 decl_line,
1083 decl_column,
1084 call_file,
1085 call_line,
1086 call_column,
1087 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001088 {
1089 // Union of all ranges in the function DIE (if the function is discontiguous)
1090 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +00001091 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
1092 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
1094 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001095 ModuleSP module_sp (m_obj_file->GetModule());
Greg Clayton3046e662013-07-10 01:23:25 +00001096 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, module_sp->GetSectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001097 if (func_range.GetBaseAddress().IsValid())
1098 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
1099 }
1100
1101 if (func_range.GetBaseAddress().IsValid())
1102 {
1103 Mangled func_name;
1104 if (mangled)
Greg Clayton037520e2012-07-18 23:18:10 +00001105 func_name.SetValue(ConstString(mangled), true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106 else if (name)
Greg Clayton037520e2012-07-18 23:18:10 +00001107 func_name.SetValue(ConstString(name), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108
1109 FunctionSP func_sp;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001110 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +00001112 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1113 decl_line,
1114 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115
Greg Clayton0bd4e1b2011-09-30 20:52:25 +00001116 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +00001117 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118
1119 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
1120
Greg Clayton9422dd62013-03-04 21:46:16 +00001121 if (FixupAddress (func_range.GetBaseAddress()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001123 const user_id_t func_user_id = MakeUserID(die->GetOffset());
1124 func_sp.reset(new Function (sc.comp_unit,
1125 MakeUserID(func_user_id), // UserID is the DIE offset
1126 MakeUserID(func_user_id),
1127 func_name,
1128 func_type,
1129 func_range)); // first address range
1130
1131 if (func_sp.get() != NULL)
1132 {
1133 if (frame_base.IsValid())
1134 func_sp->GetFrameBaseExpression() = frame_base;
1135 sc.comp_unit->AddFunction(func_sp);
1136 return func_sp.get();
1137 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 }
1139 }
1140 }
1141 return NULL;
1142}
1143
Greg Clayton9422dd62013-03-04 21:46:16 +00001144bool
1145SymbolFileDWARF::FixupAddress (Address &addr)
1146{
1147 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
1148 if (debug_map_symfile)
1149 {
1150 return debug_map_symfile->LinkOSOAddress(addr);
1151 }
1152 // This is a normal DWARF file, no address fixups need to happen
1153 return true;
1154}
Greg Clayton1f746072012-08-29 21:13:06 +00001155lldb::LanguageType
1156SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
1157{
1158 assert (sc.comp_unit);
1159 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1160 if (dwarf_cu)
1161 {
1162 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly();
Jim Ingham28eb5712012-10-12 17:34:26 +00001163 if (die)
1164 {
1165 const uint32_t language = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
1166 if (language)
1167 return (lldb::LanguageType)language;
1168 }
Greg Clayton1f746072012-08-29 21:13:06 +00001169 }
1170 return eLanguageTypeUnknown;
1171}
1172
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173size_t
1174SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
1175{
1176 assert (sc.comp_unit);
1177 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00001178 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179 if (dwarf_cu)
1180 {
1181 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +00001182 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +00001184 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001185 {
1186 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +00001187 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 {
1189 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
1190 ++functions_added;
1191 }
1192 }
1193 //FixupTypes();
1194 }
1195 return functions_added;
1196}
1197
1198bool
1199SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
1200{
1201 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +00001202 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Claytonda2455b2012-11-01 17:28:37 +00001203 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204 {
Greg Claytonda2455b2012-11-01 17:28:37 +00001205 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206
Greg Claytonda2455b2012-11-01 17:28:37 +00001207 if (cu_die)
1208 {
1209 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
Matthew Gardinere81df3b2014-08-26 06:57:23 +00001210
1211 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
1212 // Remove the host part if present.
1213 cu_comp_dir = removeHostnameFromPathname(cu_comp_dir);
1214
Greg Claytonda2455b2012-11-01 17:28:37 +00001215 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 +00001216
Greg Claytonda2455b2012-11-01 17:28:37 +00001217 // All file indexes in DWARF are one based and a file of index zero is
1218 // supposed to be the compile unit itself.
1219 support_files.Append (*sc.comp_unit);
1220
1221 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
1222 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001223 }
1224 return false;
1225}
1226
1227struct ParseDWARFLineTableCallbackInfo
1228{
1229 LineTable* line_table;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001230 std::unique_ptr<LineSequence> sequence_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231};
1232
1233//----------------------------------------------------------------------
1234// ParseStatementTableCallback
1235//----------------------------------------------------------------------
1236static void
1237ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1238{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1240 {
1241 // Just started parsing the line table
1242 }
1243 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1244 {
1245 // Done parsing line table, nothing to do for the cleanup
1246 }
1247 else
1248 {
1249 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
Greg Clayton9422dd62013-03-04 21:46:16 +00001250 LineTable* line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251
Greg Clayton9422dd62013-03-04 21:46:16 +00001252 // If this is our first time here, we need to create a
1253 // sequence container.
1254 if (!info->sequence_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001256 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1257 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001259 line_table->AppendLineEntryToSequence (info->sequence_ap.get(),
1260 state.address,
1261 state.line,
1262 state.column,
1263 state.file,
1264 state.is_stmt,
1265 state.basic_block,
1266 state.prologue_end,
1267 state.epilogue_begin,
1268 state.end_sequence);
1269 if (state.end_sequence)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001271 // First, put the current sequence into the line table.
1272 line_table->InsertSequence(info->sequence_ap.get());
1273 // Then, empty it to prepare for the next sequence.
1274 info->sequence_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001276 }
1277}
1278
1279bool
1280SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1281{
1282 assert (sc.comp_unit);
1283 if (sc.comp_unit->GetLineTable() != NULL)
1284 return true;
1285
Greg Clayton1f746072012-08-29 21:13:06 +00001286 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287 if (dwarf_cu)
1288 {
1289 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001290 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001292 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1293 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001295 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
Greg Clayton129d12c2011-11-28 03:29:03 +00001296 if (line_table_ap.get())
1297 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001298 ParseDWARFLineTableCallbackInfo info;
1299 info.line_table = line_table_ap.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00001300 lldb::offset_t offset = cu_line_offset;
Greg Clayton129d12c2011-11-28 03:29:03 +00001301 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
Greg Clayton9422dd62013-03-04 21:46:16 +00001302 if (m_debug_map_symfile)
1303 {
1304 // We have an object file that has a line table with addresses
1305 // that are not linked. We need to link the line table and convert
1306 // the addresses that are relative to the .o file into addresses
1307 // for the main executable.
1308 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get()));
1309 }
1310 else
1311 {
1312 sc.comp_unit->SetLineTable(line_table_ap.release());
1313 return true;
1314 }
Greg Clayton129d12c2011-11-28 03:29:03 +00001315 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316 }
1317 }
1318 }
1319 return false;
1320}
1321
1322size_t
1323SymbolFileDWARF::ParseFunctionBlocks
1324(
1325 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001326 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001327 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328 const DWARFDebugInfoEntry *die,
1329 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001330 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331)
1332{
1333 size_t blocks_added = 0;
1334 while (die != NULL)
1335 {
1336 dw_tag_t tag = die->Tag();
1337
1338 switch (tag)
1339 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001341 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342 case DW_TAG_lexical_block:
1343 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001344 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001345 if (tag == DW_TAG_subprogram)
1346 {
1347 // Skip any DW_TAG_subprogram DIEs that are inside
1348 // of a normal or inlined functions. These will be
1349 // parsed on their own as separate entities.
1350
1351 if (depth > 0)
1352 break;
1353
1354 block = parent_block;
1355 }
1356 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001357 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001358 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001359 parent_block->AddChild(block_sp);
1360 block = block_sp.get();
1361 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001362 DWARFDebugRanges::RangeList ranges;
1363 const char *name = NULL;
1364 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001366 int decl_file = 0;
1367 int decl_line = 0;
1368 int decl_column = 0;
1369 int call_file = 0;
1370 int call_line = 0;
1371 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001372 if (die->GetDIENamesAndRanges (this,
1373 dwarf_cu,
1374 name,
1375 mangled_name,
1376 ranges,
1377 decl_file, decl_line, decl_column,
1378 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379 {
1380 if (tag == DW_TAG_subprogram)
1381 {
1382 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001383 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001385 else if (tag == DW_TAG_inlined_subroutine)
1386 {
1387 // We get called here for inlined subroutines in two ways.
1388 // The first time is when we are making the Function object
1389 // for this inlined concrete instance. Since we're creating a top level block at
1390 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1391 // adjust the containing address.
1392 // The second time is when we are parsing the blocks inside the function that contains
1393 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1394 // function the offset will be for that function.
1395 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1396 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001397 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001398 }
1399 }
1400
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001401 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402
1403 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1404 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001405 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001407 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1408 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409
Greg Clayton7b0992d2013-04-18 22:45:39 +00001410 std::unique_ptr<Declaration> call_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001412 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1413 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001415 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001416 }
1417
1418 ++blocks_added;
1419
Greg Claytondd7feaf2011-08-12 17:54:33 +00001420 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001422 blocks_added += ParseFunctionBlocks (sc,
1423 block,
1424 dwarf_cu,
1425 die->GetFirstChild(),
1426 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001427 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428 }
1429 }
1430 }
1431 break;
1432 default:
1433 break;
1434 }
1435
Greg Claytondd7feaf2011-08-12 17:54:33 +00001436 // Only parse siblings of the block if we are not at depth zero. A depth
1437 // of zero indicates we are currently parsing the top level
1438 // DW_TAG_subprogram DIE
1439
1440 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001441 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001442 else
1443 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001444 }
1445 return blocks_added;
1446}
1447
Greg Claytonf0705c82011-10-22 03:33:13 +00001448bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001449SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1450 const DWARFDebugInfoEntry *die,
1451 ClangASTContext::TemplateParameterInfos &template_param_infos)
1452{
1453 const dw_tag_t tag = die->Tag();
1454
1455 switch (tag)
1456 {
1457 case DW_TAG_template_type_parameter:
1458 case DW_TAG_template_value_parameter:
1459 {
Todd Fialaee8bfc62014-09-11 17:29:12 +00001460 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001461
1462 DWARFDebugInfoEntry::Attributes attributes;
1463 const size_t num_attributes = die->GetAttributes (this,
1464 dwarf_cu,
1465 fixed_form_sizes,
1466 attributes);
1467 const char *name = NULL;
1468 Type *lldb_type = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001469 ClangASTType clang_type;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001470 uint64_t uval64 = 0;
1471 bool uval64_valid = false;
1472 if (num_attributes > 0)
1473 {
1474 DWARFFormValue form_value;
1475 for (size_t i=0; i<num_attributes; ++i)
1476 {
1477 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1478
1479 switch (attr)
1480 {
1481 case DW_AT_name:
1482 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1483 name = form_value.AsCString(&get_debug_str_data());
1484 break;
1485
1486 case DW_AT_type:
1487 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1488 {
Greg Clayton54166af2014-11-22 01:58:59 +00001489 const dw_offset_t type_die_offset = form_value.Reference();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001490 lldb_type = ResolveTypeUID(type_die_offset);
1491 if (lldb_type)
1492 clang_type = lldb_type->GetClangForwardType();
1493 }
1494 break;
1495
1496 case DW_AT_const_value:
1497 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1498 {
1499 uval64_valid = true;
1500 uval64 = form_value.Unsigned();
1501 }
1502 break;
1503 default:
1504 break;
1505 }
1506 }
1507
Greg Clayton283b2652013-04-23 22:38:02 +00001508 clang::ASTContext *ast = GetClangASTContext().getASTContext();
1509 if (!clang_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00001510 clang_type = GetClangASTContext().GetBasicType(eBasicTypeVoid);
Greg Clayton283b2652013-04-23 22:38:02 +00001511
1512 if (clang_type)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001513 {
1514 bool is_signed = false;
Greg Clayton283b2652013-04-23 22:38:02 +00001515 if (name && name[0])
1516 template_param_infos.names.push_back(name);
1517 else
1518 template_param_infos.names.push_back(NULL);
1519
Greg Clayton283b2652013-04-23 22:38:02 +00001520 if (tag == DW_TAG_template_value_parameter &&
1521 lldb_type != NULL &&
Greg Clayton57ee3062013-07-11 22:46:58 +00001522 clang_type.IsIntegerType (is_signed) &&
Greg Clayton283b2652013-04-23 22:38:02 +00001523 uval64_valid)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001524 {
1525 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
Greg Clayton283b2652013-04-23 22:38:02 +00001526 template_param_infos.args.push_back (clang::TemplateArgument (*ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001527 llvm::APSInt(apint),
Greg Clayton57ee3062013-07-11 22:46:58 +00001528 clang_type.GetQualType()));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001529 }
1530 else
1531 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001532 template_param_infos.args.push_back (clang::TemplateArgument (clang_type.GetQualType()));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001533 }
1534 }
1535 else
1536 {
1537 return false;
1538 }
1539
1540 }
1541 }
1542 return true;
1543
1544 default:
1545 break;
1546 }
1547 return false;
1548}
1549
1550bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001551SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1552 const DWARFDebugInfoEntry *parent_die,
1553 ClangASTContext::TemplateParameterInfos &template_param_infos)
1554{
1555
1556 if (parent_die == NULL)
Filipe Cabecinhas52008c62012-05-23 16:24:11 +00001557 return false;
Greg Claytonf0705c82011-10-22 03:33:13 +00001558
Greg Claytonf0705c82011-10-22 03:33:13 +00001559 Args template_parameter_names;
1560 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1561 die != NULL;
1562 die = die->GetSibling())
1563 {
1564 const dw_tag_t tag = die->Tag();
1565
1566 switch (tag)
1567 {
1568 case DW_TAG_template_type_parameter:
1569 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001570 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001571 break;
1572
1573 default:
1574 break;
1575 }
1576 }
1577 if (template_param_infos.args.empty())
1578 return false;
1579 return template_param_infos.args.size() == template_param_infos.names.size();
1580}
1581
1582clang::ClassTemplateDecl *
1583SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001584 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001585 const char *parent_name,
1586 int tag_decl_kind,
1587 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1588{
1589 if (template_param_infos.IsValid())
1590 {
1591 std::string template_basename(parent_name);
1592 template_basename.erase (template_basename.find('<'));
1593 ClangASTContext &ast = GetClangASTContext();
1594
1595 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001596 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001597 template_basename.c_str(),
1598 tag_decl_kind,
1599 template_param_infos);
1600 }
1601 return NULL;
1602}
1603
Sean Callanana0b80ab2012-06-04 22:28:05 +00001604class SymbolFileDWARF::DelayedAddObjCClassProperty
1605{
1606public:
1607 DelayedAddObjCClassProperty
1608 (
Greg Clayton57ee3062013-07-11 22:46:58 +00001609 const ClangASTType &class_opaque_type,
Sean Callanana0b80ab2012-06-04 22:28:05 +00001610 const char *property_name,
Greg Clayton57ee3062013-07-11 22:46:58 +00001611 const ClangASTType &property_opaque_type, // The property type is only required if you don't have an ivar decl
Sean Callanana0b80ab2012-06-04 22:28:05 +00001612 clang::ObjCIvarDecl *ivar_decl,
1613 const char *property_setter_name,
1614 const char *property_getter_name,
1615 uint32_t property_attributes,
Greg Claytonc4ffd662013-03-08 01:37:30 +00001616 const ClangASTMetadata *metadata
Sean Callanana0b80ab2012-06-04 22:28:05 +00001617 ) :
Sean Callanana0b80ab2012-06-04 22:28:05 +00001618 m_class_opaque_type (class_opaque_type),
1619 m_property_name (property_name),
1620 m_property_opaque_type (property_opaque_type),
1621 m_ivar_decl (ivar_decl),
1622 m_property_setter_name (property_setter_name),
1623 m_property_getter_name (property_getter_name),
Jim Ingham379397632012-10-27 02:54:13 +00001624 m_property_attributes (property_attributes)
Sean Callanana0b80ab2012-06-04 22:28:05 +00001625 {
Jim Ingham379397632012-10-27 02:54:13 +00001626 if (metadata != NULL)
1627 {
1628 m_metadata_ap.reset(new ClangASTMetadata());
Greg Claytonc4ffd662013-03-08 01:37:30 +00001629 *m_metadata_ap = *metadata;
Jim Ingham379397632012-10-27 02:54:13 +00001630 }
1631 }
1632
1633 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
1634 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001635 *this = rhs;
Daniel Malead4c5be62012-11-12 21:02:14 +00001636 }
1637
1638 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs)
1639 {
Jim Ingham379397632012-10-27 02:54:13 +00001640 m_class_opaque_type = rhs.m_class_opaque_type;
1641 m_property_name = rhs.m_property_name;
1642 m_property_opaque_type = rhs.m_property_opaque_type;
1643 m_ivar_decl = rhs.m_ivar_decl;
1644 m_property_setter_name = rhs.m_property_setter_name;
1645 m_property_getter_name = rhs.m_property_getter_name;
1646 m_property_attributes = rhs.m_property_attributes;
1647
1648 if (rhs.m_metadata_ap.get())
1649 {
1650 m_metadata_ap.reset (new ClangASTMetadata());
Greg Claytonc4ffd662013-03-08 01:37:30 +00001651 *m_metadata_ap = *rhs.m_metadata_ap;
Jim Ingham379397632012-10-27 02:54:13 +00001652 }
Daniel Malead4c5be62012-11-12 21:02:14 +00001653 return *this;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001654 }
1655
Greg Clayton57ee3062013-07-11 22:46:58 +00001656 bool
1657 Finalize()
Sean Callanana0b80ab2012-06-04 22:28:05 +00001658 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001659 return m_class_opaque_type.AddObjCClassProperty (m_property_name,
1660 m_property_opaque_type,
1661 m_ivar_decl,
1662 m_property_setter_name,
1663 m_property_getter_name,
1664 m_property_attributes,
1665 m_metadata_ap.get());
Sean Callanana0b80ab2012-06-04 22:28:05 +00001666 }
1667private:
Greg Clayton57ee3062013-07-11 22:46:58 +00001668 ClangASTType m_class_opaque_type;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001669 const char *m_property_name;
Greg Clayton57ee3062013-07-11 22:46:58 +00001670 ClangASTType m_property_opaque_type;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001671 clang::ObjCIvarDecl *m_ivar_decl;
1672 const char *m_property_setter_name;
1673 const char *m_property_getter_name;
1674 uint32_t m_property_attributes;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001675 std::unique_ptr<ClangASTMetadata> m_metadata_ap;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001676};
1677
Sean Callananfaa0bb32012-12-05 23:37:14 +00001678struct BitfieldInfo
1679{
1680 uint64_t bit_size;
1681 uint64_t bit_offset;
1682
1683 BitfieldInfo () :
1684 bit_size (LLDB_INVALID_ADDRESS),
1685 bit_offset (LLDB_INVALID_ADDRESS)
1686 {
1687 }
1688
Greg Clayton78f4d952013-12-11 23:10:39 +00001689 void
1690 Clear()
1691 {
1692 bit_size = LLDB_INVALID_ADDRESS;
1693 bit_offset = LLDB_INVALID_ADDRESS;
1694 }
1695
Sean Callananfaa0bb32012-12-05 23:37:14 +00001696 bool IsValid ()
1697 {
1698 return (bit_size != LLDB_INVALID_ADDRESS) &&
1699 (bit_offset != LLDB_INVALID_ADDRESS);
1700 }
1701};
1702
Greg Claytonc4ffd662013-03-08 01:37:30 +00001703
1704bool
1705SymbolFileDWARF::ClassOrStructIsVirtual (DWARFCompileUnit* dwarf_cu,
1706 const DWARFDebugInfoEntry *parent_die)
1707{
1708 if (parent_die)
1709 {
1710 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1711 {
1712 dw_tag_t tag = die->Tag();
1713 bool check_virtuality = false;
1714 switch (tag)
1715 {
1716 case DW_TAG_inheritance:
1717 case DW_TAG_subprogram:
1718 check_virtuality = true;
1719 break;
1720 default:
1721 break;
1722 }
1723 if (check_virtuality)
1724 {
1725 if (die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_virtuality, 0) != 0)
1726 return true;
1727 }
1728 }
1729 }
1730 return false;
1731}
1732
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733size_t
1734SymbolFileDWARF::ParseChildMembers
1735(
1736 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001737 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001738 const DWARFDebugInfoEntry *parent_die,
Greg Clayton57ee3062013-07-11 22:46:58 +00001739 ClangASTType &class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001740 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001741 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1742 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001743 DWARFDIECollection& member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00001744 DelayedPropertyList& delayed_properties,
Sean Callananc7fbf732010-08-06 00:32:49 +00001745 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001746 bool &is_a_class,
1747 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748)
1749{
1750 if (parent_die == NULL)
1751 return 0;
1752
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001753 size_t count = 0;
1754 const DWARFDebugInfoEntry *die;
Todd Fialaee8bfc62014-09-11 17:29:12 +00001755 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001756 uint32_t member_idx = 0;
Sean Callananfaa0bb32012-12-05 23:37:14 +00001757 BitfieldInfo last_field_info;
Richard Mitton0a558352013-10-17 21:14:00 +00001758 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytond88d7592010-09-15 08:33:30 +00001759
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001760 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1761 {
1762 dw_tag_t tag = die->Tag();
1763
1764 switch (tag)
1765 {
1766 case DW_TAG_member:
Sean Callanan3d654b32012-09-24 22:25:51 +00001767 case DW_TAG_APPLE_property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768 {
1769 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001770 const size_t num_attributes = die->GetAttributes (this,
1771 dwarf_cu,
1772 fixed_form_sizes,
1773 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001774 if (num_attributes > 0)
1775 {
1776 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001777 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001778 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001779 const char *prop_name = NULL;
1780 const char *prop_getter_name = NULL;
1781 const char *prop_setter_name = NULL;
Greg Claytone528efd72013-02-26 23:45:18 +00001782 uint32_t prop_attributes = 0;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001783
1784
Greg Clayton24739922010-10-13 03:15:28 +00001785 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001787 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001788 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789 size_t byte_size = 0;
1790 size_t bit_offset = 0;
1791 size_t bit_size = 0;
Greg Claytone528efd72013-02-26 23:45:18 +00001792 bool is_external = false; // On DW_TAG_members, this means the member is static
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001793 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001794 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001795 {
1796 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1797 DWARFFormValue form_value;
1798 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1799 {
1800 switch (attr)
1801 {
1802 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1803 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1804 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1805 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00001806 case DW_AT_type: encoding_uid = form_value.Reference(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001807 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1808 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1809 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1810 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001811 if (form_value.BlockData())
1812 {
1813 Value initialValue(0);
1814 Value memberOffset(0);
Ed Masteeeae7212013-10-24 20:43:47 +00001815 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Claytoncaab74e2012-01-28 00:48:57 +00001816 uint32_t block_length = form_value.Unsigned();
1817 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1818 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
Greg Claytoncaab74e2012-01-28 00:48:57 +00001819 NULL, // ClangExpressionVariableList *
1820 NULL, // ClangExpressionDeclMap *
1821 NULL, // RegisterContext *
Richard Mitton0a558352013-10-17 21:14:00 +00001822 module,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001823 debug_info_data,
1824 block_offset,
1825 block_length,
1826 eRegisterKindDWARF,
1827 &initialValue,
1828 memberOffset,
1829 NULL))
1830 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001831 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
Greg Claytoncaab74e2012-01-28 00:48:57 +00001832 }
1833 }
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00001834 else
1835 {
1836 // With DWARF 3 and later, if the value is an integer constant,
1837 // this form value is the offset in bytes from the beginning
1838 // of the containing entity.
1839 member_byte_offset = form_value.Unsigned();
1840 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841 break;
1842
Greg Clayton8cf05932010-07-22 18:30:50 +00001843 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00001844 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001845 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1846 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1847 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1848 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00001849 case DW_AT_external: is_external = form_value.Boolean(); break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001850
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001851 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001852 case DW_AT_declaration:
1853 case DW_AT_description:
1854 case DW_AT_mutable:
1855 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001856 case DW_AT_sibling:
1857 break;
1858 }
1859 }
1860 }
Sean Callanana6582262012-04-05 00:12:52 +00001861
1862 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00001863 {
Sean Callanana6582262012-04-05 00:12:52 +00001864 ConstString fixed_getter;
1865 ConstString fixed_setter;
1866
1867 // Check if the property getter/setter were provided as full
1868 // names. We want basenames, so we extract them.
1869
1870 if (prop_getter_name && prop_getter_name[0] == '-')
1871 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00001872 ObjCLanguageRuntime::MethodName prop_getter_method(prop_getter_name, true);
1873 prop_getter_name = prop_getter_method.GetSelector().GetCString();
Sean Callanana6582262012-04-05 00:12:52 +00001874 }
1875
1876 if (prop_setter_name && prop_setter_name[0] == '-')
1877 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00001878 ObjCLanguageRuntime::MethodName prop_setter_method(prop_setter_name, true);
1879 prop_setter_name = prop_setter_method.GetSelector().GetCString();
Sean Callanana6582262012-04-05 00:12:52 +00001880 }
1881
1882 // If the names haven't been provided, they need to be
1883 // filled in.
1884
1885 if (!prop_getter_name)
1886 {
1887 prop_getter_name = prop_name;
1888 }
1889 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
1890 {
1891 StreamString ss;
1892
1893 ss.Printf("set%c%s:",
1894 toupper(prop_name[0]),
1895 &prop_name[1]);
1896
1897 fixed_setter.SetCString(ss.GetData());
1898 prop_setter_name = fixed_setter.GetCString();
1899 }
Sean Callanan751aac62012-03-29 19:07:06 +00001900 }
1901
1902 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001903 // represents fields that are references with bad byte size
1904 // and bit size/offset information such as:
1905 //
1906 // DW_AT_byte_size( 0x00 )
1907 // DW_AT_bit_size( 0x40 )
1908 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1909 //
1910 // So check the bit offset to make sure it is sane, and if
1911 // the values are not sane, remove them. If we don't do this
1912 // then we will end up with a crash if we try to use this
1913 // type in an expression when clang becomes unhappy with its
1914 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001915
Greg Clayton44953932011-10-25 01:25:35 +00001916 if (bit_offset > 128)
1917 {
1918 bit_size = 0;
1919 bit_offset = 0;
1920 }
1921
1922 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001923 if (class_language == eLanguageTypeObjC ||
1924 class_language == eLanguageTypeObjC_plus_plus)
1925 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001926
1927 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1928 {
1929 // Not all compilers will mark the vtable pointer
1930 // member as artificial (llvm-gcc). We can't have
1931 // the virtual members in our classes otherwise it
1932 // throws off all child offsets since we end up
1933 // having and extra pointer sized member in our
1934 // class layouts.
1935 is_artificial = true;
1936 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001937
Greg Clayton29659712013-07-12 20:08:35 +00001938 // Handle static members
Greg Claytone528efd72013-02-26 23:45:18 +00001939 if (is_external && member_byte_offset == UINT32_MAX)
Greg Clayton973b6c92013-04-11 16:57:51 +00001940 {
1941 Type *var_type = ResolveTypeUID(encoding_uid);
1942
1943 if (var_type)
1944 {
Greg Clayton29659712013-07-12 20:08:35 +00001945 if (accessibility == eAccessNone)
1946 accessibility = eAccessPublic;
Greg Clayton57ee3062013-07-11 22:46:58 +00001947 class_clang_type.AddVariableToRecordType (name,
1948 var_type->GetClangLayoutType(),
1949 accessibility);
Greg Clayton973b6c92013-04-11 16:57:51 +00001950 }
Greg Claytone528efd72013-02-26 23:45:18 +00001951 break;
Greg Clayton973b6c92013-04-11 16:57:51 +00001952 }
Greg Claytone528efd72013-02-26 23:45:18 +00001953
Greg Clayton24739922010-10-13 03:15:28 +00001954 if (is_artificial == false)
1955 {
1956 Type *member_type = ResolveTypeUID(encoding_uid);
Sean Callananfa3ab452012-12-14 00:54:13 +00001957
Jim Inghame3ae82a2011-11-12 01:36:43 +00001958 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001959 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001960 {
Sean Callanan751aac62012-03-29 19:07:06 +00001961 if (member_type)
1962 {
1963 if (accessibility == eAccessNone)
1964 accessibility = default_accessibility;
1965 member_accessibilities.push_back(accessibility);
Sean Callananfaa0bb32012-12-05 23:37:14 +00001966
Greg Clayton78f4d952013-12-11 23:10:39 +00001967 uint64_t field_bit_offset = (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1968 if (bit_size > 0)
Greg Clayton88bc7f32012-11-06 00:20:41 +00001969 {
Greg Clayton78f4d952013-12-11 23:10:39 +00001970
1971 BitfieldInfo this_field_info;
1972 this_field_info.bit_offset = field_bit_offset;
1973 this_field_info.bit_size = bit_size;
1974
Sean Callananfaa0bb32012-12-05 23:37:14 +00001975 /////////////////////////////////////////////////////////////
1976 // How to locate a field given the DWARF debug information
1977 //
1978 // AT_byte_size indicates the size of the word in which the
1979 // bit offset must be interpreted.
1980 //
1981 // AT_data_member_location indicates the byte offset of the
1982 // word from the base address of the structure.
1983 //
1984 // AT_bit_offset indicates how many bits into the word
1985 // (according to the host endianness) the low-order bit of
1986 // the field starts. AT_bit_offset can be negative.
1987 //
1988 // AT_bit_size indicates the size of the field in bits.
1989 /////////////////////////////////////////////////////////////
1990
Greg Clayton78f4d952013-12-11 23:10:39 +00001991 if (byte_size == 0)
1992 byte_size = member_type->GetByteSize();
1993
Sean Callananfaa0bb32012-12-05 23:37:14 +00001994 if (GetObjectFile()->GetByteOrder() == eByteOrderLittle)
1995 {
1996 this_field_info.bit_offset += byte_size * 8;
1997 this_field_info.bit_offset -= (bit_offset + bit_size);
1998 }
1999 else
2000 {
2001 this_field_info.bit_offset += bit_offset;
2002 }
Greg Clayton78f4d952013-12-11 23:10:39 +00002003
2004 // Update the field bit offset we will report for layout
2005 field_bit_offset = this_field_info.bit_offset;
Sean Callananfaa0bb32012-12-05 23:37:14 +00002006
Greg Clayton78f4d952013-12-11 23:10:39 +00002007 // If the member to be emitted did not start on a character boundary and there is
2008 // empty space between the last field and this one, then we need to emit an
2009 // anonymous member filling up the space up to its start. There are three cases
2010 // here:
2011 //
2012 // 1 If the previous member ended on a character boundary, then we can emit an
2013 // anonymous member starting at the most recent character boundary.
2014 //
2015 // 2 If the previous member did not end on a character boundary and the distance
2016 // from the end of the previous member to the current member is less than a
2017 // word width, then we can emit an anonymous member starting right after the
2018 // previous member and right before this member.
2019 //
2020 // 3 If the previous member did not end on a character boundary and the distance
2021 // from the end of the previous member to the current member is greater than
2022 // or equal a word width, then we act as in Case 1.
2023
2024 const uint64_t character_width = 8;
2025 const uint64_t word_width = 32;
2026
Sean Callananfaa0bb32012-12-05 23:37:14 +00002027 // Objective-C has invalid DW_AT_bit_offset values in older versions
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00002028 // of clang, so we have to be careful and only insert unnamed bitfields
Greg Clayton37c36e42012-11-27 00:59:26 +00002029 // if we have a new enough clang.
2030 bool detect_unnamed_bitfields = true;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002031
Greg Clayton37c36e42012-11-27 00:59:26 +00002032 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus)
2033 detect_unnamed_bitfields = dwarf_cu->Supports_unnamed_objc_bitfields ();
2034
2035 if (detect_unnamed_bitfields)
Greg Clayton88bc7f32012-11-06 00:20:41 +00002036 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00002037 BitfieldInfo anon_field_info;
2038
2039 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned
Greg Clayton88bc7f32012-11-06 00:20:41 +00002040 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00002041 uint64_t last_field_end = 0;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002042
Sean Callananfaa0bb32012-12-05 23:37:14 +00002043 if (last_field_info.IsValid())
2044 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002045
Sean Callananfaa0bb32012-12-05 23:37:14 +00002046 if (this_field_info.bit_offset != last_field_end)
2047 {
2048 if (((last_field_end % character_width) == 0) || // case 1
2049 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3
2050 {
2051 anon_field_info.bit_size = this_field_info.bit_offset % character_width;
2052 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size;
2053 }
2054 else // case 2
2055 {
2056 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end;
2057 anon_field_info.bit_offset = last_field_end;
2058 }
Greg Clayton88bc7f32012-11-06 00:20:41 +00002059 }
Greg Clayton88bc7f32012-11-06 00:20:41 +00002060 }
2061
Sean Callananfaa0bb32012-12-05 23:37:14 +00002062 if (anon_field_info.IsValid())
Greg Clayton88bc7f32012-11-06 00:20:41 +00002063 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002064 clang::FieldDecl *unnamed_bitfield_decl = class_clang_type.AddFieldToRecordType (NULL,
2065 GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width),
2066 accessibility,
2067 anon_field_info.bit_size);
Greg Clayton88bc7f32012-11-06 00:20:41 +00002068
Sean Callananfaa0bb32012-12-05 23:37:14 +00002069 layout_info.field_offsets.insert(std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset));
Greg Clayton88bc7f32012-11-06 00:20:41 +00002070 }
2071 }
Greg Clayton78f4d952013-12-11 23:10:39 +00002072 last_field_info = this_field_info;
2073 }
2074 else
2075 {
2076 last_field_info.Clear();
Greg Clayton88bc7f32012-11-06 00:20:41 +00002077 }
Sean Callananfaa0bb32012-12-05 23:37:14 +00002078
Greg Clayton57ee3062013-07-11 22:46:58 +00002079 ClangASTType member_clang_type = member_type->GetClangLayoutType();
Sean Callananfa3ab452012-12-14 00:54:13 +00002080
2081 {
2082 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>).
2083 // If the current field is at the end of the structure, then there is definitely no room for extra
2084 // elements and we override the type to array[0].
2085
Greg Clayton57ee3062013-07-11 22:46:58 +00002086 ClangASTType member_array_element_type;
Sean Callananfa3ab452012-12-14 00:54:13 +00002087 uint64_t member_array_size;
2088 bool member_array_is_incomplete;
2089
Greg Clayton57ee3062013-07-11 22:46:58 +00002090 if (member_clang_type.IsArrayType(&member_array_element_type,
2091 &member_array_size,
2092 &member_array_is_incomplete) &&
Sean Callananfa3ab452012-12-14 00:54:13 +00002093 !member_array_is_incomplete)
2094 {
2095 uint64_t parent_byte_size = parent_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, UINT64_MAX);
2096
2097 if (member_byte_offset >= parent_byte_size)
2098 {
2099 if (member_array_size != 1)
2100 {
2101 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,
2102 MakeUserID(die->GetOffset()),
2103 name,
2104 encoding_uid,
2105 MakeUserID(parent_die->GetOffset()));
2106 }
2107
Greg Clayton1c8ef472013-04-05 23:27:21 +00002108 member_clang_type = GetClangASTContext().CreateArrayType(member_array_element_type, 0, false);
Sean Callananfa3ab452012-12-14 00:54:13 +00002109 }
2110 }
2111 }
2112
Greg Clayton57ee3062013-07-11 22:46:58 +00002113 field_decl = class_clang_type.AddFieldToRecordType (name,
2114 member_clang_type,
2115 accessibility,
2116 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00002117
Greg Claytond0029442013-03-27 01:48:02 +00002118 GetClangASTContext().SetMetadataAsUserID (field_decl, MakeUserID(die->GetOffset()));
Sean Callananfaa0bb32012-12-05 23:37:14 +00002119
Greg Clayton78f4d952013-12-11 23:10:39 +00002120 layout_info.field_offsets.insert(std::make_pair(field_decl, field_bit_offset));
2121
Sean Callanan5b26f272012-02-04 08:49:35 +00002122 }
2123 else
2124 {
Sean Callanan751aac62012-03-29 19:07:06 +00002125 if (name)
Daniel Malead01b2952012-11-29 21:49:15 +00002126 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 +00002127 MakeUserID(die->GetOffset()),
2128 name,
2129 encoding_uid);
2130 else
Daniel Malead01b2952012-11-29 21:49:15 +00002131 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 +00002132 MakeUserID(die->GetOffset()),
2133 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00002134 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002135 }
Sean Callanan751aac62012-03-29 19:07:06 +00002136
Jim Inghame3ae82a2011-11-12 01:36:43 +00002137 if (prop_name != NULL)
2138 {
Sean Callanan751aac62012-03-29 19:07:06 +00002139 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00002140
Sean Callanan751aac62012-03-29 19:07:06 +00002141 if (field_decl)
2142 {
2143 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
2144 assert (ivar_decl != NULL);
2145 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002146
Jim Ingham379397632012-10-27 02:54:13 +00002147 ClangASTMetadata metadata;
2148 metadata.SetUserID (MakeUserID(die->GetOffset()));
Greg Clayton57ee3062013-07-11 22:46:58 +00002149 delayed_properties.push_back(DelayedAddObjCClassProperty(class_clang_type,
Sean Callanana0b80ab2012-06-04 22:28:05 +00002150 prop_name,
2151 member_type->GetClangLayoutType(),
2152 ivar_decl,
2153 prop_setter_name,
2154 prop_getter_name,
2155 prop_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00002156 &metadata));
Sean Callanan60217122012-04-13 00:10:03 +00002157
Sean Callananad880762012-04-18 01:06:17 +00002158 if (ivar_decl)
Greg Claytond0029442013-03-27 01:48:02 +00002159 GetClangASTContext().SetMetadataAsUserID (ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00002160 }
Greg Clayton24739922010-10-13 03:15:28 +00002161 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002162 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00002163 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002164 }
2165 break;
2166
2167 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00002168 // Let the type parsing code handle this one for us.
2169 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002170 break;
2171
2172 case DW_TAG_inheritance:
2173 {
2174 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00002175 if (default_accessibility == eAccessNone)
2176 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002177 // TODO: implement DW_TAG_inheritance type parsing
2178 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00002179 const size_t num_attributes = die->GetAttributes (this,
2180 dwarf_cu,
2181 fixed_form_sizes,
2182 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002183 if (num_attributes > 0)
2184 {
2185 Declaration decl;
2186 DWARFExpression location;
2187 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00002188 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002189 bool is_virtual = false;
2190 bool is_base_of_class = true;
Greg Clayton2508b9b2012-11-01 23:20:02 +00002191 off_t member_byte_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002192 uint32_t i;
2193 for (i=0; i<num_attributes; ++i)
2194 {
2195 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2196 DWARFFormValue form_value;
2197 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2198 {
2199 switch (attr)
2200 {
2201 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2202 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2203 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00002204 case DW_AT_type: encoding_uid = form_value.Reference(); break;
Greg Clayton2508b9b2012-11-01 23:20:02 +00002205 case DW_AT_data_member_location:
2206 if (form_value.BlockData())
2207 {
2208 Value initialValue(0);
2209 Value memberOffset(0);
Ed Masteeeae7212013-10-24 20:43:47 +00002210 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Clayton2508b9b2012-11-01 23:20:02 +00002211 uint32_t block_length = form_value.Unsigned();
2212 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
2213 if (DWARFExpression::Evaluate (NULL,
2214 NULL,
2215 NULL,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002216 NULL,
Richard Mitton0a558352013-10-17 21:14:00 +00002217 module,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002218 debug_info_data,
2219 block_offset,
2220 block_length,
2221 eRegisterKindDWARF,
2222 &initialValue,
2223 memberOffset,
2224 NULL))
2225 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002226 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
Greg Clayton2508b9b2012-11-01 23:20:02 +00002227 }
2228 }
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00002229 else
2230 {
2231 // With DWARF 3 and later, if the value is an integer constant,
2232 // this form value is the offset in bytes from the beginning
2233 // of the containing entity.
2234 member_byte_offset = form_value.Unsigned();
2235 }
Greg Clayton2508b9b2012-11-01 23:20:02 +00002236 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002237
2238 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00002239 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002240 break;
2241
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00002242 case DW_AT_virtuality:
2243 is_virtual = form_value.Boolean();
2244 break;
2245
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002246 case DW_AT_sibling:
2247 break;
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00002248
2249 default:
2250 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002251 }
2252 }
2253 }
2254
Greg Clayton526e5af2010-11-13 03:52:47 +00002255 Type *base_class_type = ResolveTypeUID(encoding_uid);
2256 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002257
Greg Clayton57ee3062013-07-11 22:46:58 +00002258 ClangASTType base_class_clang_type = base_class_type->GetClangFullType();
Greg Claytonf4ecaa52011-02-16 23:00:21 +00002259 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002260 if (class_language == eLanguageTypeObjC)
2261 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002262 class_clang_type.SetObjCSuperClass(base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002263 }
2264 else
2265 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002266 base_classes.push_back (base_class_clang_type.CreateBaseClassSpecifier (accessibility,
Greg Claytonba2d22d2010-11-13 22:57:37 +00002267 is_virtual,
2268 is_base_of_class));
Greg Clayton2508b9b2012-11-01 23:20:02 +00002269
2270 if (is_virtual)
2271 {
Greg Clayton52694e32013-09-16 21:57:07 +00002272 // Do not specify any offset for virtual inheritance. The DWARF produced by clang doesn't
2273 // give us a constant offset, but gives us a DWARF expressions that requires an actual object
2274 // in memory. the DW_AT_data_member_location for a virtual base class looks like:
2275 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, DW_OP_plus )
2276 // Given this, there is really no valid response we can give to clang for virtual base
2277 // class offsets, and this should eventually be removed from LayoutRecordType() in the external
2278 // AST source in clang.
Greg Clayton2508b9b2012-11-01 23:20:02 +00002279 }
2280 else
2281 {
Greg Clayton57648732013-09-12 17:22:32 +00002282 layout_info.base_offsets.insert(std::make_pair(base_class_clang_type.GetAsCXXRecordDecl(),
Greg Clayton2508b9b2012-11-01 23:20:02 +00002283 clang::CharUnits::fromQuantity(member_byte_offset)));
2284 }
Greg Clayton9e409562010-07-28 02:04:09 +00002285 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002286 }
2287 }
2288 break;
2289
2290 default:
2291 break;
2292 }
2293 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00002294
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002295 return count;
2296}
2297
2298
2299clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00002300SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002301{
2302 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00002303 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002304 {
2305 DWARFCompileUnitSP cu_sp;
2306 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
2307 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00002308 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00002309 }
2310 return NULL;
2311}
2312
2313clang::DeclContext*
2314SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
2315{
Greg Clayton81c22f62011-10-19 18:09:39 +00002316 if (UserIDMatches(type_uid))
2317 return GetClangDeclContextForDIEOffset (sc, type_uid);
2318 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002319}
2320
2321Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00002322SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002323{
Greg Clayton81c22f62011-10-19 18:09:39 +00002324 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002325 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002326 DWARFDebugInfo* debug_info = DebugInfo();
2327 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00002328 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002329 DWARFCompileUnitSP cu_sp;
2330 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00002331 const bool assert_not_being_parsed = true;
2332 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00002333 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002334 }
2335 return NULL;
2336}
2337
Greg Claytoncab36a32011-12-08 05:16:30 +00002338Type*
2339SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00002340{
Greg Claytoncab36a32011-12-08 05:16:30 +00002341 if (die != NULL)
2342 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002343 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00002344 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002345 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002346 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
2347 die->GetOffset(),
2348 DW_TAG_value_to_name(die->Tag()),
2349 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00002350
Greg Claytoncab36a32011-12-08 05:16:30 +00002351 // We might be coming in in the middle of a type tree (a class
2352 // withing a class, an enum within a class), so parse any needed
2353 // parent DIEs before we get to this one...
2354 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2355 switch (decl_ctx_die->Tag())
2356 {
2357 case DW_TAG_structure_type:
2358 case DW_TAG_union_type:
2359 case DW_TAG_class_type:
2360 {
2361 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00002362 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002363 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002364 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
2365 die->GetOffset(),
2366 DW_TAG_value_to_name(die->Tag()),
2367 die->GetName(this, cu),
2368 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00002369//
2370// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
2371// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
2372// {
2373// if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002374// GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton219cf312012-03-30 00:51:13 +00002375// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
2376// die->GetOffset(),
2377// DW_TAG_value_to_name(die->Tag()),
2378// die->GetName(this, cu),
2379// decl_ctx_die->GetOffset());
2380// // Ask the type to complete itself if it already hasn't since if we
2381// // want a function (method or static) from a class, the class must
2382// // create itself and add it's own methods and class functions.
2383// if (parent_type)
2384// parent_type->GetClangFullType();
2385// }
Greg Claytoncab36a32011-12-08 05:16:30 +00002386 }
2387 break;
2388
2389 default:
2390 break;
2391 }
2392 return ResolveType (cu, die);
2393 }
2394 return NULL;
2395}
2396
Greg Clayton6beaaa62011-01-17 03:46:26 +00002397// This function is used when SymbolFileDWARFDebugMap owns a bunch of
2398// SymbolFileDWARF objects to detect if this DWARF file is the one that
2399// can resolve a clang_type.
2400bool
Greg Clayton57ee3062013-07-11 22:46:58 +00002401SymbolFileDWARF::HasForwardDeclForClangType (const ClangASTType &clang_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002402{
Greg Clayton57ee3062013-07-11 22:46:58 +00002403 ClangASTType clang_type_no_qualifiers = clang_type.RemoveFastQualifiers();
2404 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002405 return die != NULL;
2406}
2407
2408
Greg Clayton57ee3062013-07-11 22:46:58 +00002409bool
2410SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (ClangASTType &clang_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00002411{
2412 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton57ee3062013-07-11 22:46:58 +00002413 ClangASTType clang_type_no_qualifiers = clang_type.RemoveFastQualifiers();
2414 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton1be10fc2010-09-29 01:12:09 +00002415 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002416 {
2417 // We have already resolved this type...
Greg Clayton57ee3062013-07-11 22:46:58 +00002418 return true;
Greg Clayton73b472d2010-10-27 03:32:59 +00002419 }
2420 // Once we start resolving this type, remove it from the forward declaration
2421 // map in case anyone child members or other types require this type to get resolved.
2422 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
2423 // are done.
Greg Clayton57ee3062013-07-11 22:46:58 +00002424 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton1be10fc2010-09-29 01:12:09 +00002425
Greg Clayton85ae2e12011-10-18 23:36:41 +00002426 // Disable external storage for this type so we don't get anymore
2427 // clang::ExternalASTSource queries for this type.
Greg Clayton57ee3062013-07-11 22:46:58 +00002428 clang_type.SetHasExternalStorage (false);
Greg Clayton85ae2e12011-10-18 23:36:41 +00002429
Greg Clayton450e3f32010-10-12 02:24:53 +00002430 DWARFDebugInfo* debug_info = DebugInfo();
2431
Greg Clayton53eb1c22012-04-02 22:59:12 +00002432 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002433 Type *type = m_die_to_type.lookup (die);
2434
2435 const dw_tag_t tag = die->Tag();
2436
Greg Clayton5160ce52013-03-27 23:08:40 +00002437 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00002438 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002439 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
Daniel Malead01b2952012-11-29 21:49:15 +00002440 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
Greg Claytond61c0fc2012-04-23 22:55:20 +00002441 MakeUserID(die->GetOffset()),
2442 DW_TAG_value_to_name(tag),
2443 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00002444 assert (clang_type);
2445 DWARFDebugInfoEntry::Attributes attributes;
2446
Greg Clayton1be10fc2010-09-29 01:12:09 +00002447 switch (tag)
2448 {
2449 case DW_TAG_structure_type:
2450 case DW_TAG_union_type:
2451 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002452 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002453 LayoutInfo layout_info;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002454
Greg Clayton1be10fc2010-09-29 01:12:09 +00002455 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002456 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002457 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002458 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton57ee3062013-07-11 22:46:58 +00002459 if (clang_type.IsObjCObjectOrInterfaceType())
Greg Clayton219cf312012-03-30 00:51:13 +00002460 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002461 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002462 // For objective C we don't start the definition when
2463 // the class is created.
Greg Clayton57ee3062013-07-11 22:46:58 +00002464 clang_type.StartTagDeclarationDefinition ();
Greg Clayton219cf312012-03-30 00:51:13 +00002465 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002466
Sean Callanan77a1fd32012-02-27 20:07:01 +00002467 int tag_decl_kind = -1;
2468 AccessType default_accessibility = eAccessNone;
2469 if (tag == DW_TAG_structure_type)
2470 {
2471 tag_decl_kind = clang::TTK_Struct;
2472 default_accessibility = eAccessPublic;
2473 }
2474 else if (tag == DW_TAG_union_type)
2475 {
2476 tag_decl_kind = clang::TTK_Union;
2477 default_accessibility = eAccessPublic;
2478 }
2479 else if (tag == DW_TAG_class_type)
2480 {
2481 tag_decl_kind = clang::TTK_Class;
2482 default_accessibility = eAccessPrivate;
2483 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002484
Greg Clayton53eb1c22012-04-02 22:59:12 +00002485 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002486 std::vector<clang::CXXBaseSpecifier *> base_classes;
2487 std::vector<int> member_accessibilities;
2488 bool is_a_class = false;
2489 // Parse members and base classes first
2490 DWARFDIECollection member_function_dies;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002491
Sean Callanana0b80ab2012-06-04 22:28:05 +00002492 DelayedPropertyList delayed_properties;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002493 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002494 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002495 die,
2496 clang_type,
2497 class_language,
2498 base_classes,
2499 member_accessibilities,
2500 member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00002501 delayed_properties,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002502 default_accessibility,
2503 is_a_class,
2504 layout_info);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002505
Sean Callanan77a1fd32012-02-27 20:07:01 +00002506 // Now parse any methods if there were any...
2507 size_t num_functions = member_function_dies.Size();
2508 if (num_functions > 0)
2509 {
2510 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002511 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002512 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002513 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002514 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002515
Sean Callanan77a1fd32012-02-27 20:07:01 +00002516 if (class_language == eLanguageTypeObjC)
2517 {
Jim Inghamfb6fc0d2013-09-27 20:59:37 +00002518 ConstString class_name (clang_type.GetTypeName());
2519 if (class_name)
Greg Claytoncaab74e2012-01-28 00:48:57 +00002520 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002521 DIEArray method_die_offsets;
2522 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002523 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002524 if (m_apple_objc_ap.get())
Jim Inghamfb6fc0d2013-09-27 20:59:37 +00002525 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
Sean Callanan77a1fd32012-02-27 20:07:01 +00002526 }
2527 else
2528 {
2529 if (!m_indexed)
2530 Index ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002531
Sean Callanan77a1fd32012-02-27 20:07:01 +00002532 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2533 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002534
Sean Callanan77a1fd32012-02-27 20:07:01 +00002535 if (!method_die_offsets.empty())
2536 {
2537 DWARFDebugInfo* debug_info = DebugInfo();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002538
Sean Callanan77a1fd32012-02-27 20:07:01 +00002539 DWARFCompileUnit* method_cu = NULL;
2540 const size_t num_matches = method_die_offsets.size();
2541 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002542 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002543 const dw_offset_t die_offset = method_die_offsets[i];
2544 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002545
Sean Callanan77a1fd32012-02-27 20:07:01 +00002546 if (method_die)
2547 ResolveType (method_cu, method_die);
2548 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002549 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002550 if (m_using_apple_tables)
2551 {
2552 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
Jim Inghamfb6fc0d2013-09-27 20:59:37 +00002553 die_offset, class_name.GetCString());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002554 }
2555 }
2556 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002557 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002558
Greg Clayton57ee3062013-07-11 22:46:58 +00002559 for (DelayedPropertyList::iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
Sean Callanana0b80ab2012-06-04 22:28:05 +00002560 pi != pe;
2561 ++pi)
2562 pi->Finalize();
Greg Clayton450e3f32010-10-12 02:24:53 +00002563 }
2564 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002565
Sean Callanan77a1fd32012-02-27 20:07:01 +00002566 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2567 // need to tell the clang type it is actually a class.
2568 if (class_language != eLanguageTypeObjC)
2569 {
2570 if (is_a_class && tag_decl_kind != clang::TTK_Class)
Greg Clayton57ee3062013-07-11 22:46:58 +00002571 clang_type.SetTagTypeKind (clang::TTK_Class);
Sean Callanan77a1fd32012-02-27 20:07:01 +00002572 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002573
Sean Callanan77a1fd32012-02-27 20:07:01 +00002574 // Since DW_TAG_structure_type gets used for both classes
2575 // and structures, we may need to set any DW_TAG_member
2576 // fields to have a "private" access if none was specified.
2577 // When we parsed the child members we tracked that actual
2578 // accessibility value for each DW_TAG_member in the
2579 // "member_accessibilities" array. If the value for the
2580 // member is zero, then it was set to the "default_accessibility"
2581 // which for structs was "public". Below we correct this
2582 // by setting any fields to "private" that weren't correctly
2583 // set.
2584 if (is_a_class && !member_accessibilities.empty())
2585 {
2586 // This is a class and all members that didn't have
2587 // their access specified are private.
Greg Clayton57ee3062013-07-11 22:46:58 +00002588 clang_type.SetDefaultAccessForRecordFields (eAccessPrivate,
2589 &member_accessibilities.front(),
2590 member_accessibilities.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002591 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002592
Sean Callanan77a1fd32012-02-27 20:07:01 +00002593 if (!base_classes.empty())
2594 {
Greg Clayton4705f8d2013-12-12 01:54:04 +00002595 // Make sure all base classes refer to complete types and not
2596 // forward declarations. If we don't do this, clang will crash
2597 // with an assertion in the call to clang_type.SetBaseClassesForClassType()
2598 bool base_class_error = false;
2599 for (auto &base_class : base_classes)
2600 {
2601 clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo();
2602 if (type_source_info)
2603 {
2604 ClangASTType base_class_type (GetClangASTContext().getASTContext(), type_source_info->getType());
2605 if (base_class_type.GetCompleteType() == false)
2606 {
2607 if (!base_class_error)
2608 {
2609 GetObjectFile()->GetModule()->ReportError ("DWARF DIE at 0x%8.8x for class '%s' has a base class '%s' that is a forward declaration, not a complete definition.\nPlease file a bug against the compiler and include the preprocessed output for %s",
2610 die->GetOffset(),
2611 die->GetName(this, dwarf_cu),
2612 base_class_type.GetTypeName().GetCString(),
2613 sc.comp_unit ? sc.comp_unit->GetPath().c_str() : "the source file");
2614 }
2615 // We have no choice other than to pretend that the base class
2616 // is complete. If we don't do this, clang will crash when we
2617 // call setBases() inside of "clang_type.SetBaseClassesForClassType()"
2618 // below. Since we provide layout assistance, all ivars in this
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002619 // class and other classes will be fine, this is the best we can do
Greg Clayton4705f8d2013-12-12 01:54:04 +00002620 // short of crashing.
2621 base_class_type.StartTagDeclarationDefinition ();
2622 base_class_type.CompleteTagDeclarationDefinition ();
2623 }
2624 }
2625 }
Greg Clayton57ee3062013-07-11 22:46:58 +00002626 clang_type.SetBaseClassesForClassType (&base_classes.front(),
2627 base_classes.size());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002628
Sean Callanan77a1fd32012-02-27 20:07:01 +00002629 // Clang will copy each CXXBaseSpecifier in "base_classes"
2630 // so we have to free them all.
Greg Clayton57ee3062013-07-11 22:46:58 +00002631 ClangASTType::DeleteBaseClassSpecifiers (&base_classes.front(),
2632 base_classes.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002633 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002634 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002635 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002636
Greg Clayton57ee3062013-07-11 22:46:58 +00002637 clang_type.BuildIndirectFields ();
2638 clang_type.CompleteTagDeclarationDefinition ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002639
Greg Clayton2508b9b2012-11-01 23:20:02 +00002640 if (!layout_info.field_offsets.empty() ||
2641 !layout_info.base_offsets.empty() ||
2642 !layout_info.vbase_offsets.empty() )
Greg Claytoncaab74e2012-01-28 00:48:57 +00002643 {
2644 if (type)
2645 layout_info.bit_size = type->GetByteSize() * 8;
2646 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002647 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002648
Greg Clayton57ee3062013-07-11 22:46:58 +00002649 clang::CXXRecordDecl *record_decl = clang_type.GetAsCXXRecordDecl();
Greg Clayton2508b9b2012-11-01 23:20:02 +00002650 if (record_decl)
Greg Claytoncaab74e2012-01-28 00:48:57 +00002651 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002652 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002653 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002654 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00002655 "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])",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002656 static_cast<void*>(clang_type.GetOpaqueQualType()),
2657 static_cast<void*>(record_decl),
Greg Claytoncaab74e2012-01-28 00:48:57 +00002658 layout_info.bit_size,
2659 layout_info.alignment,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002660 static_cast<uint32_t>(layout_info.field_offsets.size()),
2661 static_cast<uint32_t>(layout_info.base_offsets.size()),
2662 static_cast<uint32_t>(layout_info.vbase_offsets.size()));
2663
Greg Clayton2508b9b2012-11-01 23:20:02 +00002664 uint32_t idx;
2665 {
Greg Clayton821ac6d2012-01-28 02:22:27 +00002666 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
Greg Clayton2508b9b2012-11-01 23:20:02 +00002667 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002668 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002669 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002670 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002671 static_cast<void*>(clang_type.GetOpaqueQualType()),
Greg Clayton2508b9b2012-11-01 23:20:02 +00002672 idx,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002673 static_cast<uint32_t>(pos->second),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002674 pos->first->getNameAsString().c_str());
2675 }
Greg Clayton2508b9b2012-11-01 23:20:02 +00002676 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002677
Greg Clayton2508b9b2012-11-01 23:20:02 +00002678 {
2679 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos, base_end = layout_info.base_offsets.end();
2680 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx)
2681 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002682 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002683 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }",
Greg Clayton57ee3062013-07-11 22:46:58 +00002684 clang_type.GetOpaqueQualType(),
Greg Clayton2508b9b2012-11-01 23:20:02 +00002685 idx,
2686 (uint32_t)base_pos->second.getQuantity(),
2687 base_pos->first->getNameAsString().c_str());
2688 }
2689 }
2690 {
2691 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos, vbase_end = layout_info.vbase_offsets.end();
2692 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx)
2693 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002694 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002695 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002696 static_cast<void*>(clang_type.GetOpaqueQualType()),
Greg Clayton2508b9b2012-11-01 23:20:02 +00002697 idx,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002698 static_cast<uint32_t>(vbase_pos->second.getQuantity()),
Greg Clayton2508b9b2012-11-01 23:20:02 +00002699 vbase_pos->first->getNameAsString().c_str());
2700 }
2701 }
Greg Clayton821ac6d2012-01-28 02:22:27 +00002702 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002703 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2704 }
2705 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002706 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002707
Sean Callanan9076c0f2013-10-04 21:35:29 +00002708 return (bool)clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002709
2710 case DW_TAG_enumeration_type:
Greg Clayton57ee3062013-07-11 22:46:58 +00002711 clang_type.StartTagDeclarationDefinition ();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002712 if (die->HasChildren())
2713 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002714 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Greg Clayton3e067532013-03-05 23:54:39 +00002715 bool is_signed = false;
Greg Clayton57ee3062013-07-11 22:46:58 +00002716 clang_type.IsIntegerType(is_signed);
Greg Clayton3e067532013-03-05 23:54:39 +00002717 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002718 }
Greg Clayton57ee3062013-07-11 22:46:58 +00002719 clang_type.CompleteTagDeclarationDefinition ();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002720 return (bool)clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002721
2722 default:
2723 assert(false && "not a forward clang type decl!");
2724 break;
2725 }
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00002726 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002727}
2728
Greg Claytonc685f8e2010-09-15 04:15:46 +00002729Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002730SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002731{
2732 if (type_die != NULL)
2733 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002734 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002735
Greg Claytonc685f8e2010-09-15 04:15:46 +00002736 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002737 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002738
Greg Clayton24739922010-10-13 03:15:28 +00002739 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002740 {
2741 if (type != DIE_IS_BEING_PARSED)
2742 return type;
2743
2744 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002745 type_die->GetOffset(),
2746 DW_TAG_value_to_name(type_die->Tag()),
2747 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002748
2749 }
2750 else
2751 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002752 }
2753 return NULL;
2754}
2755
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002756CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002757SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002758{
2759 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002760 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002761 {
2762 // The symbol vendor doesn't know about this compile unit, we
2763 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002764 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002765 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002766 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002767}
2768
2769bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002770SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002771{
Greg Clayton72310352013-02-23 04:12:47 +00002772 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002773 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002774 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002775
Greg Clayton81c22f62011-10-19 18:09:39 +00002776 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002777 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002778 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002779
2780 if (sc.function)
2781 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002782 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002783 return true;
2784 }
2785
2786 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002787}
2788
Greg Clayton2501e5e2015-01-15 02:59:20 +00002789
2790
2791SymbolFileDWARF::GlobalVariableMap &
2792SymbolFileDWARF::GetGlobalAranges()
2793{
2794 if (!m_global_aranges_ap)
2795 {
2796 m_global_aranges_ap.reset (new GlobalVariableMap());
2797
2798 ModuleSP module_sp = GetObjectFile()->GetModule();
2799 if (module_sp)
2800 {
2801 const size_t num_cus = module_sp->GetNumCompileUnits();
2802 for (size_t i = 0; i < num_cus; ++i)
2803 {
2804 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
2805 if (cu_sp)
2806 {
2807 VariableListSP globals_sp = cu_sp->GetVariableList(true);
2808 if (globals_sp)
2809 {
2810 const size_t num_globals = globals_sp->GetSize();
2811 for (size_t g = 0; g < num_globals; ++g)
2812 {
2813 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
2814 if (var_sp && !var_sp->GetLocationIsConstantValueData())
2815 {
2816 const DWARFExpression &location = var_sp->LocationExpression();
2817 Value location_result;
2818 Error error;
2819 if (location.Evaluate(NULL, NULL, NULL, LLDB_INVALID_ADDRESS, NULL, location_result, &error))
2820 {
2821 if (location_result.GetValueType() == Value::eValueTypeFileAddress)
2822 {
2823 lldb::addr_t file_addr = location_result.GetScalar().ULongLong();
2824 lldb::addr_t byte_size = 1;
2825 if (var_sp->GetType())
2826 byte_size = var_sp->GetType()->GetByteSize();
2827 m_global_aranges_ap->Append(GlobalVariableMap::Entry(file_addr, byte_size, var_sp.get()));
2828 }
2829 }
2830 }
2831 }
2832 }
2833 }
2834 }
2835 }
2836 m_global_aranges_ap->Sort();
2837 }
2838 return *m_global_aranges_ap;
2839}
2840
2841
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002842uint32_t
2843SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2844{
2845 Timer scoped_timer(__PRETTY_FUNCTION__,
Daniel Malead01b2952012-11-29 21:49:15 +00002846 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002847 static_cast<void*>(so_addr.GetSection().get()),
2848 so_addr.GetOffset(), resolve_scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002849 uint32_t resolved = 0;
Greg Clayton2501e5e2015-01-15 02:59:20 +00002850 if (resolve_scope & ( eSymbolContextCompUnit |
2851 eSymbolContextFunction |
2852 eSymbolContextBlock |
2853 eSymbolContextLineEntry |
2854 eSymbolContextVariable ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002855 {
2856 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2857
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002858 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002859 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002860 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002861 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Greg Clayton2501e5e2015-01-15 02:59:20 +00002862 if (cu_offset == DW_INVALID_OFFSET)
2863 {
2864 // Global variables are not in the compile unit address ranges. The only way to
2865 // currently find global variables is to iterate over the .debug_pubnames or the
2866 // __apple_names table and find all items in there that point to DW_TAG_variable
2867 // DIEs and then find the address that matches.
2868 if (resolve_scope & eSymbolContextVariable)
2869 {
2870 GlobalVariableMap &map = GetGlobalAranges();
2871 const GlobalVariableMap::Entry *entry = map.FindEntryThatContains(file_vm_addr);
2872 if (entry && entry->data)
2873 {
2874 Variable *variable = entry->data;
2875 SymbolContextScope *scc = variable->GetSymbolContextScope();
2876 if (scc)
2877 {
2878 scc->CalculateSymbolContext(&sc);
2879 sc.variable = variable;
2880 }
2881 return sc.GetResolvedMask();
2882 }
2883 }
2884 }
2885 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002886 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002887 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002888 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2889 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002890 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002891 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002892 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002893 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002894 resolved |= eSymbolContextCompUnit;
2895
Greg Clayton6ab80132012-12-12 17:30:52 +00002896 bool force_check_line_table = false;
Greg Clayton526a4ae2012-05-16 22:09:01 +00002897 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2898 {
2899 DWARFDebugInfoEntry *function_die = NULL;
2900 DWARFDebugInfoEntry *block_die = NULL;
2901 if (resolve_scope & eSymbolContextBlock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002902 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002903 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
2904 }
2905 else
2906 {
2907 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
2908 }
2909
2910 if (function_die != NULL)
2911 {
2912 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2913 if (sc.function == NULL)
2914 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2915 }
2916 else
2917 {
2918 // We might have had a compile unit that had discontiguous
2919 // address ranges where the gaps are symbols that don't have
2920 // any debug info. Discontiguous compile unit address ranges
2921 // should only happen when there aren't other functions from
2922 // other compile units in these gaps. This helps keep the size
2923 // of the aranges down.
Greg Clayton6ab80132012-12-12 17:30:52 +00002924 force_check_line_table = true;
Greg Clayton526a4ae2012-05-16 22:09:01 +00002925 }
2926
2927 if (sc.function != NULL)
2928 {
2929 resolved |= eSymbolContextFunction;
2930
2931 if (resolve_scope & eSymbolContextBlock)
2932 {
2933 Block& block = sc.function->GetBlock (true);
2934
2935 if (block_die != NULL)
2936 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2937 else
2938 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2939 if (sc.block)
2940 resolved |= eSymbolContextBlock;
2941 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002942 }
2943 }
Greg Clayton6ab80132012-12-12 17:30:52 +00002944
2945 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table)
2946 {
2947 LineTable *line_table = sc.comp_unit->GetLineTable();
2948 if (line_table != NULL)
2949 {
Greg Clayton9422dd62013-03-04 21:46:16 +00002950 // And address that makes it into this function should be in terms
2951 // of this debug file if there is no debug map, or it will be an
2952 // address in the .o file which needs to be fixed up to be in terms
2953 // of the debug map executable. Either way, calling FixupAddress()
2954 // will work for us.
2955 Address exe_so_addr (so_addr);
2956 if (FixupAddress(exe_so_addr))
Greg Clayton6ab80132012-12-12 17:30:52 +00002957 {
Greg Clayton9422dd62013-03-04 21:46:16 +00002958 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry))
Greg Clayton6ab80132012-12-12 17:30:52 +00002959 {
2960 resolved |= eSymbolContextLineEntry;
2961 }
2962 }
Greg Clayton6ab80132012-12-12 17:30:52 +00002963 }
2964 }
2965
2966 if (force_check_line_table && !(resolved & eSymbolContextLineEntry))
2967 {
2968 // We might have had a compile unit that had discontiguous
2969 // address ranges where the gaps are symbols that don't have
2970 // any debug info. Discontiguous compile unit address ranges
2971 // should only happen when there aren't other functions from
2972 // other compile units in these gaps. This helps keep the size
2973 // of the aranges down.
2974 sc.comp_unit = NULL;
2975 resolved &= ~eSymbolContextCompUnit;
2976 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002977 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002978 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002979 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002980 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
2981 cu_offset,
2982 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002983 }
2984 }
2985 }
2986 }
2987 }
2988 return resolved;
2989}
2990
2991
2992
2993uint32_t
2994SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2995{
2996 const uint32_t prev_size = sc_list.GetSize();
2997 if (resolve_scope & eSymbolContextCompUnit)
2998 {
2999 DWARFDebugInfo* debug_info = DebugInfo();
3000 if (debug_info)
3001 {
3002 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00003003 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003004
Greg Clayton53eb1c22012-04-02 22:59:12 +00003005 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003006 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003007 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Sean Callananddd7a2a2013-10-03 22:27:29 +00003008 const bool full_match = (bool)file_spec.GetDirectory();
Greg Clayton1f746072012-08-29 21:13:06 +00003009 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 +00003010 if (check_inlines || file_spec_matches_cu_file_spec)
3011 {
3012 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00003013 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00003014 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003015 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003016 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003017
Greg Clayton526a4ae2012-05-16 22:09:01 +00003018 // If we are looking for inline functions only and we don't
3019 // find it in the support files, we are done.
3020 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003021 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003022 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
3023 if (file_idx == UINT32_MAX)
3024 continue;
3025 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003026
Greg Clayton526a4ae2012-05-16 22:09:01 +00003027 if (line != 0)
3028 {
3029 LineTable *line_table = sc.comp_unit->GetLineTable();
3030
3031 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003032 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003033 // We will have already looked up the file index if
3034 // we are searching for inline entries.
3035 if (!check_inlines)
3036 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003037
Greg Clayton526a4ae2012-05-16 22:09:01 +00003038 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003039 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003040 uint32_t found_line;
3041 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
3042 found_line = sc.line_entry.line;
3043
3044 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003045 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003046 sc.function = NULL;
3047 sc.block = NULL;
3048 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003049 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003050 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
3051 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003052 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003053 DWARFDebugInfoEntry *function_die = NULL;
3054 DWARFDebugInfoEntry *block_die = NULL;
3055 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003056
Greg Clayton526a4ae2012-05-16 22:09:01 +00003057 if (function_die != NULL)
3058 {
3059 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
3060 if (sc.function == NULL)
3061 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
3062 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003063
Greg Clayton526a4ae2012-05-16 22:09:01 +00003064 if (sc.function != NULL)
3065 {
3066 Block& block = sc.function->GetBlock (true);
3067
3068 if (block_die != NULL)
3069 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Jason Molenda60db6e42014-10-16 01:40:16 +00003070 else if (function_die != NULL)
Greg Clayton526a4ae2012-05-16 22:09:01 +00003071 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
3072 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003073 }
3074 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003075
Greg Clayton526a4ae2012-05-16 22:09:01 +00003076 sc_list.Append(sc);
3077 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
3078 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003079 }
3080 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00003081 else if (file_spec_matches_cu_file_spec && !check_inlines)
3082 {
3083 // only append the context if we aren't looking for inline call sites
3084 // by file and line and if the file spec matches that of the compile unit
3085 sc_list.Append(sc);
3086 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003087 }
3088 else if (file_spec_matches_cu_file_spec && !check_inlines)
3089 {
3090 // only append the context if we aren't looking for inline call sites
3091 // by file and line and if the file spec matches that of the compile unit
3092 sc_list.Append(sc);
3093 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003094
Greg Clayton526a4ae2012-05-16 22:09:01 +00003095 if (!check_inlines)
3096 break;
3097 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003098 }
3099 }
3100 }
3101 }
3102 return sc_list.GetSize() - prev_size;
3103}
3104
3105void
3106SymbolFileDWARF::Index ()
3107{
3108 if (m_indexed)
3109 return;
3110 m_indexed = true;
3111 Timer scoped_timer (__PRETTY_FUNCTION__,
3112 "SymbolFileDWARF::Index (%s)",
Jim Ingham4af59612014-12-19 19:20:44 +00003113 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003114
3115 DWARFDebugInfo* debug_info = DebugInfo();
3116 if (debug_info)
3117 {
3118 uint32_t cu_idx = 0;
3119 const uint32_t num_compile_units = GetNumCompileUnits();
3120 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
3121 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003122 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003123
Greg Clayton53eb1c22012-04-02 22:59:12 +00003124 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003125
Greg Clayton53eb1c22012-04-02 22:59:12 +00003126 dwarf_cu->Index (cu_idx,
3127 m_function_basename_index,
3128 m_function_fullname_index,
3129 m_function_method_index,
3130 m_function_selector_index,
3131 m_objc_class_selectors_index,
3132 m_global_index,
3133 m_type_index,
3134 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003135
3136 // Keep memory down by clearing DIEs if this generate function
3137 // caused them to be parsed
3138 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00003139 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003140 }
3141
Greg Claytond4a2b372011-09-12 23:21:58 +00003142 m_function_basename_index.Finalize();
3143 m_function_fullname_index.Finalize();
3144 m_function_method_index.Finalize();
3145 m_function_selector_index.Finalize();
3146 m_objc_class_selectors_index.Finalize();
3147 m_global_index.Finalize();
3148 m_type_index.Finalize();
3149 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00003150
Greg Clayton24739922010-10-13 03:15:28 +00003151#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00003152 StreamFile s(stdout, false);
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00003153 s.Printf ("DWARF index for '%s':",
3154 GetObjectFile()->GetFileSpec().GetPath().c_str());
Greg Claytonba2d22d2010-11-13 22:57:37 +00003155 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
3156 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
3157 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
3158 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
3159 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
3160 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00003161 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00003162 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00003163#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003164 }
3165}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00003166
3167bool
3168SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
3169{
3170 if (namespace_decl == NULL)
3171 {
3172 // Invalid namespace decl which means we aren't matching only things
3173 // in this symbol file, so return true to indicate it matches this
3174 // symbol file.
3175 return true;
3176 }
3177
3178 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
3179
3180 if (namespace_ast == NULL)
3181 return true; // No AST in the "namespace_decl", return true since it
3182 // could then match any symbol file, including this one
3183
3184 if (namespace_ast == GetClangASTContext().getASTContext())
3185 return true; // The ASTs match, return true
3186
3187 // The namespace AST was valid, and it does not match...
Greg Clayton5160ce52013-03-27 23:08:40 +00003188 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Sean Callananc41e68b2011-10-13 21:08:11 +00003189
3190 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003191 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00003192
Greg Claytonbfe3dd42011-10-13 00:00:53 +00003193 return false;
3194}
3195
Greg Clayton2506a7a2011-10-12 23:34:26 +00003196bool
3197SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
3198 DWARFCompileUnit* cu,
3199 const DWARFDebugInfoEntry* die)
3200{
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00003201 // No namespace specified, so the answer is
Greg Clayton2506a7a2011-10-12 23:34:26 +00003202 if (namespace_decl == NULL)
3203 return true;
Sean Callananebe60672011-10-13 21:50:33 +00003204
Greg Clayton5160ce52013-03-27 23:08:40 +00003205 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00003206
Greg Clayton437a1352012-04-09 22:43:43 +00003207 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
3208 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00003209 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00003210 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00003211 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00003212
Greg Clayton2506a7a2011-10-12 23:34:26 +00003213 if (clang_namespace_decl)
3214 {
3215 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00003216 {
3217 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003218 GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00003219 return false;
Sean Callananebe60672011-10-13 21:50:33 +00003220 }
3221
Greg Clayton437a1352012-04-09 22:43:43 +00003222 if (clang_namespace_decl == die_clang_decl_ctx)
3223 return true;
3224 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00003225 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00003226 }
3227 else
3228 {
3229 // We have a namespace_decl that was not NULL but it contained
3230 // a NULL "clang::NamespaceDecl", so this means the global namespace
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003231 // So as long the contained decl context DIE isn't a namespace
Greg Clayton2506a7a2011-10-12 23:34:26 +00003232 // we should be ok.
3233 if (decl_ctx_die->Tag() != DW_TAG_namespace)
3234 return true;
3235 }
3236 }
Sean Callananebe60672011-10-13 21:50:33 +00003237
3238 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003239 GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00003240
Greg Clayton2506a7a2011-10-12 23:34:26 +00003241 return false;
3242}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003243uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00003244SymbolFileDWARF::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 +00003245{
Greg Clayton5160ce52013-03-27 23:08:40 +00003246 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003247
3248 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003249 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003250 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
3251 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003252 static_cast<const void*>(namespace_decl),
3253 append, max_matches);
3254
Sean Callanan213fdb82011-10-13 01:49:10 +00003255 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00003256 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003257
Greg Claytonc685f8e2010-09-15 04:15:46 +00003258 DWARFDebugInfo* info = DebugInfo();
3259 if (info == NULL)
3260 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003261
3262 // If we aren't appending the results to this list, then clear the list
3263 if (!append)
3264 variables.Clear();
3265
3266 // Remember how many variables are in the list before we search in case
3267 // we are appending the results to a variable list.
3268 const uint32_t original_size = variables.GetSize();
3269
Greg Claytond4a2b372011-09-12 23:21:58 +00003270 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003271
Greg Clayton97fbc342011-10-20 22:30:33 +00003272 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003273 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003274 if (m_apple_names_ap.get())
3275 {
3276 const char *name_cstr = name.GetCString();
Jim Inghamfa39bb42014-10-25 00:33:55 +00003277 llvm::StringRef basename;
3278 llvm::StringRef context;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003279
Jim Inghamfa39bb42014-10-25 00:33:55 +00003280 if (!CPPLanguageRuntime::ExtractContextAndIdentifier(name_cstr, context, basename))
3281 basename = name_cstr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003282
Jim Inghamfa39bb42014-10-25 00:33:55 +00003283 m_apple_names_ap->FindByName (basename.data(), die_offsets);
Greg Clayton97fbc342011-10-20 22:30:33 +00003284 }
Greg Clayton7f995132011-10-04 22:41:51 +00003285 }
3286 else
3287 {
3288 // Index the DWARF if we haven't already
3289 if (!m_indexed)
3290 Index ();
3291
3292 m_global_index.Find (name, die_offsets);
3293 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003294
Greg Clayton437a1352012-04-09 22:43:43 +00003295 const size_t num_die_matches = die_offsets.size();
3296 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003297 {
Greg Clayton7f995132011-10-04 22:41:51 +00003298 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00003299 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00003300 assert (sc.module_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003301
Greg Claytond4a2b372011-09-12 23:21:58 +00003302 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00003303 DWARFCompileUnit* dwarf_cu = NULL;
3304 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00003305 bool done = false;
3306 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00003307 {
3308 const dw_offset_t die_offset = die_offsets[i];
3309 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003310
Greg Clayton95d87902011-11-11 03:16:25 +00003311 if (die)
3312 {
Greg Clayton437a1352012-04-09 22:43:43 +00003313 switch (die->Tag())
3314 {
3315 default:
3316 case DW_TAG_subprogram:
3317 case DW_TAG_inlined_subroutine:
3318 case DW_TAG_try_block:
3319 case DW_TAG_catch_block:
3320 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003321
Greg Clayton437a1352012-04-09 22:43:43 +00003322 case DW_TAG_variable:
3323 {
3324 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003325
Greg Clayton437a1352012-04-09 22:43:43 +00003326 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3327 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003328
Greg Clayton437a1352012-04-09 22:43:43 +00003329 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003330
Greg Clayton437a1352012-04-09 22:43:43 +00003331 if (variables.GetSize() - original_size >= max_matches)
3332 done = true;
3333 }
3334 break;
3335 }
Greg Clayton95d87902011-11-11 03:16:25 +00003336 }
3337 else
3338 {
3339 if (m_using_apple_tables)
3340 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003341 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
3342 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003343 }
3344 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003345 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003346 }
3347
3348 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003349 const uint32_t num_matches = variables.GetSize() - original_size;
3350 if (log && num_matches > 0)
3351 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003352 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003353 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003354 name.GetCString(),
3355 static_cast<const void*>(namespace_decl),
3356 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00003357 num_matches);
3358 }
3359 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003360}
3361
3362uint32_t
3363SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
3364{
Greg Clayton5160ce52013-03-27 23:08:40 +00003365 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003366
Greg Clayton21f2a492011-10-06 00:09:08 +00003367 if (log)
3368 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003369 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003370 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003371 regex.GetText(), append,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003372 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003373 }
3374
Greg Claytonc685f8e2010-09-15 04:15:46 +00003375 DWARFDebugInfo* info = DebugInfo();
3376 if (info == NULL)
3377 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003378
3379 // If we aren't appending the results to this list, then clear the list
3380 if (!append)
3381 variables.Clear();
3382
3383 // Remember how many variables are in the list before we search in case
3384 // we are appending the results to a variable list.
3385 const uint32_t original_size = variables.GetSize();
3386
Greg Clayton7f995132011-10-04 22:41:51 +00003387 DIEArray die_offsets;
3388
Greg Clayton97fbc342011-10-20 22:30:33 +00003389 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003390 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003391 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00003392 {
3393 DWARFMappedHash::DIEInfoArray hash_data_array;
3394 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
3395 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
3396 }
Greg Clayton7f995132011-10-04 22:41:51 +00003397 }
3398 else
3399 {
3400 // Index the DWARF if we haven't already
3401 if (!m_indexed)
3402 Index ();
3403
3404 m_global_index.Find (regex, die_offsets);
3405 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003406
Greg Claytonc685f8e2010-09-15 04:15:46 +00003407 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00003408 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00003409 assert (sc.module_sp);
3410
Greg Claytond4a2b372011-09-12 23:21:58 +00003411 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00003412 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003413 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003414 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003415 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003416 DWARFDebugInfo* debug_info = DebugInfo();
3417 for (size_t i=0; i<num_matches; ++i)
3418 {
3419 const dw_offset_t die_offset = die_offsets[i];
3420 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00003421
3422 if (die)
3423 {
3424 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003425
Greg Clayton95d87902011-11-11 03:16:25 +00003426 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003427
Greg Clayton95d87902011-11-11 03:16:25 +00003428 if (variables.GetSize() - original_size >= max_matches)
3429 break;
3430 }
3431 else
3432 {
3433 if (m_using_apple_tables)
3434 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003435 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
3436 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00003437 }
3438 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003439 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003440 }
3441
3442 // Return the number of variable that were appended to the list
3443 return variables.GetSize() - original_size;
3444}
3445
Greg Claytonaa044962011-10-13 00:59:38 +00003446
Jim Ingham4cda6e02011-10-07 22:23:45 +00003447bool
3448SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
3449 DWARFCompileUnit *&dwarf_cu,
3450 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00003451{
Greg Claytonaa044962011-10-13 00:59:38 +00003452 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3453 return ResolveFunction (dwarf_cu, die, sc_list);
3454}
3455
3456
3457bool
3458SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
3459 const DWARFDebugInfoEntry *die,
3460 SymbolContextList& sc_list)
3461{
Greg Clayton9e315582011-09-02 04:03:59 +00003462 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00003463
3464 if (die == NULL)
3465 return false;
3466
Jim Ingham4cda6e02011-10-07 22:23:45 +00003467 // If we were passed a die that is not a function, just return false...
3468 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
3469 return false;
3470
3471 const DWARFDebugInfoEntry* inlined_die = NULL;
3472 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00003473 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003474 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00003475
Jim Ingham4cda6e02011-10-07 22:23:45 +00003476 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003477 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003478 if (die->Tag() == DW_TAG_subprogram)
3479 break;
Greg Clayton9e315582011-09-02 04:03:59 +00003480 }
3481 }
Jason Molenda60db6e42014-10-16 01:40:16 +00003482 assert (die && die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00003483 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003484 {
3485 Address addr;
3486 // Parse all blocks if needed
3487 if (inlined_die)
3488 {
Greg Clayton81c22f62011-10-19 18:09:39 +00003489 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00003490 assert (sc.block != NULL);
3491 if (sc.block->GetStartAddress (addr) == false)
3492 addr.Clear();
3493 }
3494 else
3495 {
3496 sc.block = NULL;
3497 addr = sc.function->GetAddressRange().GetBaseAddress();
3498 }
3499
3500 if (addr.IsValid())
3501 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003502 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00003503 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003504 }
3505 }
3506
Greg Claytonaa044962011-10-13 00:59:38 +00003507 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00003508}
3509
Greg Clayton7f995132011-10-04 22:41:51 +00003510void
3511SymbolFileDWARF::FindFunctions (const ConstString &name,
3512 const NameToDIE &name_to_die,
3513 SymbolContextList& sc_list)
3514{
Greg Claytond4a2b372011-09-12 23:21:58 +00003515 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003516 if (name_to_die.Find (name, die_offsets))
3517 {
3518 ParseFunctions (die_offsets, sc_list);
3519 }
3520}
3521
3522
3523void
3524SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3525 const NameToDIE &name_to_die,
3526 SymbolContextList& sc_list)
3527{
3528 DIEArray die_offsets;
3529 if (name_to_die.Find (regex, die_offsets))
3530 {
3531 ParseFunctions (die_offsets, sc_list);
3532 }
3533}
3534
3535
3536void
3537SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3538 const DWARFMappedHash::MemoryTable &memory_table,
3539 SymbolContextList& sc_list)
3540{
3541 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00003542 DWARFMappedHash::DIEInfoArray hash_data_array;
3543 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00003544 {
Greg Claytond1767f02011-12-08 02:13:16 +00003545 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00003546 ParseFunctions (die_offsets, sc_list);
3547 }
3548}
3549
3550void
3551SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
3552 SymbolContextList& sc_list)
3553{
3554 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003555 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00003556 {
Greg Clayton7f995132011-10-04 22:41:51 +00003557 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00003558
3559 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003560 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00003561 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003562 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00003563 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003564 }
3565 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00003566}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003567
Jim Ingham4cda6e02011-10-07 22:23:45 +00003568bool
3569SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
3570 const DWARFCompileUnit *dwarf_cu,
3571 uint32_t name_type_mask,
3572 const char *partial_name,
3573 const char *base_name_start,
3574 const char *base_name_end)
3575{
Greg Claytonfbea0f62012-11-15 18:05:43 +00003576 // If we are looking only for methods, throw away all the ones that are or aren't in C++ classes:
3577 if (name_type_mask == eFunctionNameTypeMethod || name_type_mask == eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003578 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003579 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
3580 if (!containing_decl_ctx)
3581 return false;
3582
3583 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
3584
Greg Claytonfbea0f62012-11-15 18:05:43 +00003585 if (name_type_mask == eFunctionNameTypeMethod)
3586 {
3587 if (is_cxx_method == false)
3588 return false;
3589 }
3590
3591 if (name_type_mask == eFunctionNameTypeBase)
3592 {
3593 if (is_cxx_method == true)
3594 return false;
3595 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003596 }
3597
3598 // Now we need to check whether the name we got back for this type matches the extra specifications
3599 // that were in the name we're looking up:
3600 if (base_name_start != partial_name || *base_name_end != '\0')
3601 {
3602 // First see if the stuff to the left matches the full name. To do that let's see if
3603 // we can pull out the mips linkage name attribute:
3604
3605 Mangled best_name;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003606 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonfbea0f62012-11-15 18:05:43 +00003607 DWARFFormValue form_value;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003608 die->GetAttributes(this, dwarf_cu, NULL, attributes);
3609 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
Greg Clayton71415542012-12-08 00:24:40 +00003610 if (idx == UINT32_MAX)
3611 idx = attributes.FindAttributeIndex(DW_AT_linkage_name);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003612 if (idx != UINT32_MAX)
3613 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003614 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3615 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003616 const char *mangled_name = form_value.AsCString(&get_debug_str_data());
3617 if (mangled_name)
3618 best_name.SetValue (ConstString(mangled_name), true);
3619 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003620 }
Greg Claytonfbea0f62012-11-15 18:05:43 +00003621
3622 if (!best_name)
3623 {
3624 idx = attributes.FindAttributeIndex(DW_AT_name);
3625 if (idx != UINT32_MAX && attributes.ExtractFormValueAtIndex(this, idx, form_value))
3626 {
3627 const char *name = form_value.AsCString(&get_debug_str_data());
3628 best_name.SetValue (ConstString(name), false);
3629 }
3630 }
3631
3632 if (best_name.GetDemangledName())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003633 {
3634 const char *demangled = best_name.GetDemangledName().GetCString();
3635 if (demangled)
3636 {
3637 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003638 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3639 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003640 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003641 else
3642 {
3643 // Sort out the case where our name is something like "Process::Destroy" and the match is
3644 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3645 // namespace boundaries...
3646
3647 if (partial_name[0] == ':' && partial_name[1] == ':')
3648 {
3649 // The partial name was already on a namespace boundary so all matches are good.
3650 return true;
3651 }
3652 else if (partial_in_demangled == demangled)
3653 {
3654 // They both start the same, so this is an good match.
3655 return true;
3656 }
3657 else
3658 {
3659 if (partial_in_demangled - demangled == 1)
3660 {
3661 // Only one character difference, can't be a namespace boundary...
3662 return false;
3663 }
3664 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3665 {
3666 // We are on a namespace boundary, so this is also good.
3667 return true;
3668 }
3669 else
3670 return false;
3671 }
3672 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003673 }
3674 }
3675 }
3676
3677 return true;
3678}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003679
Greg Clayton0c5cd902010-06-28 21:30:43 +00003680uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003681SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003682 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003683 uint32_t name_type_mask,
3684 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003685 bool append,
3686 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003687{
3688 Timer scoped_timer (__PRETTY_FUNCTION__,
3689 "SymbolFileDWARF::FindFunctions (name = '%s')",
3690 name.AsCString());
3691
Greg Clayton43fe2172013-04-03 02:00:15 +00003692 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
3693 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
3694
Greg Clayton5160ce52013-03-27 23:08:40 +00003695 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003696
3697 if (log)
3698 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003699 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003700 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3701 name.GetCString(),
3702 name_type_mask,
3703 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003704 }
3705
Greg Clayton0c5cd902010-06-28 21:30:43 +00003706 // If we aren't appending the results to this list, then clear the list
3707 if (!append)
3708 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003709
3710 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00003711 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003712
3713 // If name is empty then we won't find anything.
3714 if (name.IsEmpty())
3715 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003716
3717 // Remember how many sc_list are in the list before we search in case
3718 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003719
Jim Ingham4cda6e02011-10-07 22:23:45 +00003720 const char *name_cstr = name.GetCString();
Greg Clayton43fe2172013-04-03 02:00:15 +00003721
3722 const uint32_t original_size = sc_list.GetSize();
3723
Jim Ingham4cda6e02011-10-07 22:23:45 +00003724 DWARFDebugInfo* info = DebugInfo();
3725 if (info == NULL)
3726 return 0;
3727
Greg Claytonaa044962011-10-13 00:59:38 +00003728 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton43fe2172013-04-03 02:00:15 +00003729 std::set<const DWARFDebugInfoEntry *> resolved_dies;
Greg Clayton97fbc342011-10-20 22:30:33 +00003730 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003731 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003732 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003733 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003734
3735 DIEArray die_offsets;
3736
3737 uint32_t num_matches = 0;
3738
Greg Clayton43fe2172013-04-03 02:00:15 +00003739 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003740 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003741 // If they asked for the full name, match what they typed. At some point we may
3742 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3743 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003744 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003745 for (uint32_t i = 0; i < num_matches; i++)
3746 {
Greg Clayton95d87902011-11-11 03:16:25 +00003747 const dw_offset_t die_offset = die_offsets[i];
3748 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003749 if (die)
3750 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003751 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3752 continue;
3753
Sean Callanan9df05fb2012-02-10 22:52:19 +00003754 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3755 continue;
3756
Greg Clayton43fe2172013-04-03 02:00:15 +00003757 if (resolved_dies.find(die) == resolved_dies.end())
3758 {
3759 if (ResolveFunction (dwarf_cu, die, sc_list))
3760 resolved_dies.insert(die);
3761 }
Greg Claytonaa044962011-10-13 00:59:38 +00003762 }
Greg Clayton95d87902011-11-11 03:16:25 +00003763 else
3764 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003765 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3766 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003767 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003768 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003769 }
Greg Clayton43fe2172013-04-03 02:00:15 +00003770
3771 if (name_type_mask & eFunctionNameTypeSelector)
3772 {
3773 if (namespace_decl && *namespace_decl)
3774 return 0; // no selectors in namespaces
Greg Clayton97fbc342011-10-20 22:30:33 +00003775
Greg Clayton43fe2172013-04-03 02:00:15 +00003776 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3777 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3778 // and if it is an ObjC method name, we're good.
Greg Clayton97fbc342011-10-20 22:30:33 +00003779
Greg Clayton43fe2172013-04-03 02:00:15 +00003780 for (uint32_t i = 0; i < num_matches; i++)
Greg Clayton97fbc342011-10-20 22:30:33 +00003781 {
Greg Clayton43fe2172013-04-03 02:00:15 +00003782 const dw_offset_t die_offset = die_offsets[i];
3783 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3784 if (die)
Greg Clayton97fbc342011-10-20 22:30:33 +00003785 {
Greg Clayton43fe2172013-04-03 02:00:15 +00003786 const char *die_name = die->GetName(this, dwarf_cu);
3787 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Greg Clayton97fbc342011-10-20 22:30:33 +00003788 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003789 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3790 continue;
3791
Greg Clayton43fe2172013-04-03 02:00:15 +00003792 if (resolved_dies.find(die) == resolved_dies.end())
3793 {
3794 if (ResolveFunction (dwarf_cu, die, sc_list))
3795 resolved_dies.insert(die);
3796 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003797 }
3798 }
Greg Clayton43fe2172013-04-03 02:00:15 +00003799 else
3800 {
3801 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3802 die_offset, name_cstr);
3803 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003804 }
Greg Clayton43fe2172013-04-03 02:00:15 +00003805 die_offsets.clear();
3806 }
3807
3808 if (((name_type_mask & eFunctionNameTypeMethod) && !namespace_decl) || name_type_mask & eFunctionNameTypeBase)
3809 {
3810 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3811 // extract the base name, look that up, and if there is any other information in the name we were
3812 // passed in we have to post-filter based on that.
3813
3814 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3815 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3816
3817 for (uint32_t i = 0; i < num_matches; i++)
3818 {
3819 const dw_offset_t die_offset = die_offsets[i];
3820 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3821 if (die)
3822 {
3823 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3824 continue;
3825
3826 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3827 continue;
3828
3829 // If we get to here, the die is good, and we should add it:
3830 if (resolved_dies.find(die) == resolved_dies.end())
3831 if (ResolveFunction (dwarf_cu, die, sc_list))
3832 {
3833 bool keep_die = true;
3834 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod))
3835 {
3836 // We are looking for either basenames or methods, so we need to
3837 // trim out the ones we won't want by looking at the type
3838 SymbolContext sc;
3839 if (sc_list.GetLastContext(sc))
3840 {
3841 if (sc.block)
3842 {
3843 // We have an inlined function
3844 }
3845 else if (sc.function)
3846 {
3847 Type *type = sc.function->GetType();
3848
Sean Callananc370a8a2013-09-18 22:59:55 +00003849 if (type)
Greg Clayton43fe2172013-04-03 02:00:15 +00003850 {
Sean Callananc370a8a2013-09-18 22:59:55 +00003851 clang::DeclContext* decl_ctx = GetClangDeclContextContainingTypeUID (type->GetID());
3852 if (decl_ctx->isRecord())
Greg Clayton43fe2172013-04-03 02:00:15 +00003853 {
Sean Callananc370a8a2013-09-18 22:59:55 +00003854 if (name_type_mask & eFunctionNameTypeBase)
3855 {
3856 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
3857 keep_die = false;
3858 }
3859 }
3860 else
3861 {
3862 if (name_type_mask & eFunctionNameTypeMethod)
3863 {
3864 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
3865 keep_die = false;
3866 }
Greg Clayton43fe2172013-04-03 02:00:15 +00003867 }
3868 }
3869 else
3870 {
Sean Callananc370a8a2013-09-18 22:59:55 +00003871 GetObjectFile()->GetModule()->ReportWarning ("function at die offset 0x%8.8x had no function type",
3872 die_offset);
Greg Clayton43fe2172013-04-03 02:00:15 +00003873 }
3874 }
3875 }
3876 }
3877 if (keep_die)
3878 resolved_dies.insert(die);
3879 }
3880 }
3881 else
3882 {
3883 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3884 die_offset, name_cstr);
3885 }
3886 }
3887 die_offsets.clear();
Jim Ingham4cda6e02011-10-07 22:23:45 +00003888 }
3889 }
Greg Clayton7f995132011-10-04 22:41:51 +00003890 }
3891 else
3892 {
3893
3894 // Index the DWARF if we haven't already
3895 if (!m_indexed)
3896 Index ();
3897
Greg Clayton7f995132011-10-04 22:41:51 +00003898 if (name_type_mask & eFunctionNameTypeFull)
Matt Kopecd6089962013-05-10 17:53:48 +00003899 {
Greg Clayton7f995132011-10-04 22:41:51 +00003900 FindFunctions (name, m_function_fullname_index, sc_list);
3901
Ed Mastefc7baa02013-09-09 18:00:45 +00003902 // FIXME Temporary workaround for global/anonymous namespace
3903 // functions on FreeBSD and Linux
3904#if defined (__FreeBSD__) || defined (__linux__)
Matt Kopecd6089962013-05-10 17:53:48 +00003905 // If we didn't find any functions in the global namespace try
3906 // looking in the basename index but ignore any returned
3907 // functions that have a namespace (ie. mangled names starting with
3908 // '_ZN') but keep functions which have an anonymous namespace
3909 if (sc_list.GetSize() == 0)
3910 {
3911 SymbolContextList temp_sc_list;
3912 FindFunctions (name, m_function_basename_index, temp_sc_list);
3913 if (!namespace_decl)
3914 {
3915 SymbolContext sc;
3916 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++)
3917 {
3918 if (temp_sc_list.GetContextAtIndex(i, sc))
3919 {
Matt Kopeca189d492013-05-10 22:55:24 +00003920 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled);
3921 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Matt Kopec04e5d582013-05-14 19:00:41 +00003922 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
3923 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21))
Matt Kopecd6089962013-05-10 17:53:48 +00003924 {
3925 sc_list.Append(sc);
3926 }
3927 }
3928 }
3929 }
3930 }
3931#endif
3932 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003933 DIEArray die_offsets;
3934 DWARFCompileUnit *dwarf_cu = NULL;
3935
Greg Clayton43fe2172013-04-03 02:00:15 +00003936 if (name_type_mask & eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003937 {
Greg Clayton43fe2172013-04-03 02:00:15 +00003938 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003939 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003940 {
Greg Claytonaa044962011-10-13 00:59:38 +00003941 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3942 if (die)
3943 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003944 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3945 continue;
3946
Sean Callanan213fdb82011-10-13 01:49:10 +00003947 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3948 continue;
3949
Greg Claytonaa044962011-10-13 00:59:38 +00003950 // If we get to here, the die is good, and we should add it:
Greg Clayton43fe2172013-04-03 02:00:15 +00003951 if (resolved_dies.find(die) == resolved_dies.end())
3952 {
3953 if (ResolveFunction (dwarf_cu, die, sc_list))
3954 resolved_dies.insert(die);
3955 }
Greg Claytonaa044962011-10-13 00:59:38 +00003956 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003957 }
3958 die_offsets.clear();
3959 }
3960
Greg Clayton43fe2172013-04-03 02:00:15 +00003961 if (name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003962 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003963 if (namespace_decl && *namespace_decl)
3964 return 0; // no methods in namespaces
3965
Greg Clayton43fe2172013-04-03 02:00:15 +00003966 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003967 {
Greg Claytonaa044962011-10-13 00:59:38 +00003968 for (uint32_t i = 0; i < num_base; i++)
3969 {
3970 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3971 if (die)
3972 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003973 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3974 continue;
3975
Greg Claytonaa044962011-10-13 00:59:38 +00003976 // If we get to here, the die is good, and we should add it:
Greg Clayton43fe2172013-04-03 02:00:15 +00003977 if (resolved_dies.find(die) == resolved_dies.end())
3978 {
3979 if (ResolveFunction (dwarf_cu, die, sc_list))
3980 resolved_dies.insert(die);
3981 }
Greg Claytonaa044962011-10-13 00:59:38 +00003982 }
3983 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003984 }
3985 die_offsets.clear();
3986 }
Greg Clayton7f995132011-10-04 22:41:51 +00003987
Greg Clayton43fe2172013-04-03 02:00:15 +00003988 if ((name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003989 {
Greg Clayton7f995132011-10-04 22:41:51 +00003990 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003991 }
3992
Greg Clayton4d01ace2011-09-29 16:58:15 +00003993 }
3994
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003995 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003996 const uint32_t num_matches = sc_list.GetSize() - original_size;
3997
3998 if (log && num_matches > 0)
3999 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004000 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004001 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u",
4002 name.GetCString(),
4003 name_type_mask,
4004 append,
4005 num_matches);
4006 }
4007 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004008}
4009
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004010uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00004011SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004012{
4013 Timer scoped_timer (__PRETTY_FUNCTION__,
4014 "SymbolFileDWARF::FindFunctions (regex = '%s')",
4015 regex.GetText());
4016
Greg Clayton5160ce52013-03-27 23:08:40 +00004017 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00004018
4019 if (log)
4020 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004021 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00004022 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
4023 regex.GetText(),
4024 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00004025 }
4026
4027
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004028 // If we aren't appending the results to this list, then clear the list
4029 if (!append)
4030 sc_list.Clear();
4031
4032 // Remember how many sc_list are in the list before we search in case
4033 // we are appending the results to a variable list.
4034 uint32_t original_size = sc_list.GetSize();
4035
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_names_ap.get())
4039 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00004040 }
4041 else
4042 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00004043 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00004044 if (!m_indexed)
4045 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004046
Greg Clayton7f995132011-10-04 22:41:51 +00004047 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004048
Greg Clayton7f995132011-10-04 22:41:51 +00004049 FindFunctions (regex, m_function_fullname_index, sc_list);
4050 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004051
4052 // Return the number of variable that were appended to the list
4053 return sc_list.GetSize() - original_size;
4054}
Jim Ingham318c9f22011-08-26 19:44:13 +00004055
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004056uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00004057SymbolFileDWARF::FindTypes (const SymbolContext& sc,
4058 const ConstString &name,
4059 const lldb_private::ClangNamespaceDecl *namespace_decl,
4060 bool append,
4061 uint32_t max_matches,
4062 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004063{
Greg Claytonc685f8e2010-09-15 04:15:46 +00004064 DWARFDebugInfo* info = DebugInfo();
4065 if (info == NULL)
4066 return 0;
4067
Greg Clayton5160ce52013-03-27 23:08:40 +00004068 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004069
Greg Clayton21f2a492011-10-06 00:09:08 +00004070 if (log)
4071 {
Greg Clayton437a1352012-04-09 22:43:43 +00004072 if (namespace_decl)
Greg Clayton5160ce52013-03-27 23:08:40 +00004073 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004074 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
4075 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004076 static_cast<void*>(namespace_decl->GetNamespaceDecl()),
Greg Clayton437a1352012-04-09 22:43:43 +00004077 namespace_decl->GetQualifiedName().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004078 append, max_matches);
Greg Clayton437a1352012-04-09 22:43:43 +00004079 else
Greg Clayton5160ce52013-03-27 23:08:40 +00004080 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004081 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004082 name.GetCString(), append,
Greg Clayton437a1352012-04-09 22:43:43 +00004083 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00004084 }
4085
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004086 // If we aren't appending the results to this list, then clear the list
4087 if (!append)
4088 types.Clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004089
Sean Callanan213fdb82011-10-13 01:49:10 +00004090 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00004091 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004092
Greg Claytond4a2b372011-09-12 23:21:58 +00004093 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004094
Greg Clayton97fbc342011-10-20 22:30:33 +00004095 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004096 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004097 if (m_apple_types_ap.get())
4098 {
4099 const char *name_cstr = name.GetCString();
4100 m_apple_types_ap->FindByName (name_cstr, die_offsets);
4101 }
Greg Clayton7f995132011-10-04 22:41:51 +00004102 }
4103 else
4104 {
4105 if (!m_indexed)
4106 Index ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004107
Greg Clayton7f995132011-10-04 22:41:51 +00004108 m_type_index.Find (name, die_offsets);
4109 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004110
Greg Clayton437a1352012-04-09 22:43:43 +00004111 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00004112
Greg Clayton437a1352012-04-09 22:43:43 +00004113 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004114 {
Greg Clayton7f995132011-10-04 22:41:51 +00004115 const uint32_t initial_types_size = types.GetSize();
4116 DWARFCompileUnit* dwarf_cu = NULL;
4117 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004118 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00004119 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004120 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004121 const dw_offset_t die_offset = die_offsets[i];
4122 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
4123
Greg Clayton95d87902011-11-11 03:16:25 +00004124 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00004125 {
Greg Clayton95d87902011-11-11 03:16:25 +00004126 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
4127 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004128
Greg Clayton95d87902011-11-11 03:16:25 +00004129 Type *matching_type = ResolveType (dwarf_cu, die);
4130 if (matching_type)
4131 {
4132 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00004133 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00004134 if (types.GetSize() >= max_matches)
4135 break;
4136 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00004137 }
Greg Clayton95d87902011-11-11 03:16:25 +00004138 else
4139 {
4140 if (m_using_apple_tables)
4141 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004142 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4143 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004144 }
4145 }
4146
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004147 }
Greg Clayton437a1352012-04-09 22:43:43 +00004148 const uint32_t num_matches = types.GetSize() - initial_types_size;
4149 if (log && num_matches)
4150 {
4151 if (namespace_decl)
4152 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004153 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004154 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
4155 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004156 static_cast<void*>(namespace_decl->GetNamespaceDecl()),
Greg Clayton437a1352012-04-09 22:43:43 +00004157 namespace_decl->GetQualifiedName().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004158 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00004159 num_matches);
4160 }
4161 else
4162 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004163 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004164 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
4165 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004166 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00004167 num_matches);
4168 }
4169 }
4170 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004171 }
Greg Clayton7f995132011-10-04 22:41:51 +00004172 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004173}
4174
4175
Greg Clayton526e5af2010-11-13 03:52:47 +00004176ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00004177SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00004178 const ConstString &name,
4179 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004180{
Greg Clayton5160ce52013-03-27 23:08:40 +00004181 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00004182
4183 if (log)
4184 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004185 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00004186 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
4187 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00004188 }
Sean Callanan213fdb82011-10-13 01:49:10 +00004189
4190 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00004191 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00004192
Greg Clayton526e5af2010-11-13 03:52:47 +00004193 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004194 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00004195 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00004196 {
Greg Clayton7f995132011-10-04 22:41:51 +00004197 DIEArray die_offsets;
4198
Greg Clayton526e5af2010-11-13 03:52:47 +00004199 // Index if we already haven't to make sure the compile units
4200 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00004201 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004202 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004203 if (m_apple_namespaces_ap.get())
4204 {
4205 const char *name_cstr = name.GetCString();
4206 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
4207 }
Greg Clayton7f995132011-10-04 22:41:51 +00004208 }
4209 else
4210 {
4211 if (!m_indexed)
4212 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00004213
Greg Clayton7f995132011-10-04 22:41:51 +00004214 m_namespace_index.Find (name, die_offsets);
4215 }
Greg Claytond4a2b372011-09-12 23:21:58 +00004216
4217 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00004218 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00004219 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00004220 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00004221 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004222 DWARFDebugInfo* debug_info = DebugInfo();
4223 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00004224 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004225 const dw_offset_t die_offset = die_offsets[i];
4226 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00004227
Greg Clayton95d87902011-11-11 03:16:25 +00004228 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00004229 {
Greg Clayton95d87902011-11-11 03:16:25 +00004230 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
4231 continue;
4232
4233 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
4234 if (clang_namespace_decl)
4235 {
4236 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
4237 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00004238 break;
Greg Clayton95d87902011-11-11 03:16:25 +00004239 }
Greg Claytond4a2b372011-09-12 23:21:58 +00004240 }
Greg Clayton95d87902011-11-11 03:16:25 +00004241 else
4242 {
4243 if (m_using_apple_tables)
4244 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004245 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
4246 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004247 }
4248 }
4249
Greg Clayton526e5af2010-11-13 03:52:47 +00004250 }
4251 }
Greg Clayton96d7d742010-11-10 23:42:09 +00004252 }
Greg Clayton437a1352012-04-09 22:43:43 +00004253 if (log && namespace_decl.GetNamespaceDecl())
4254 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004255 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004256 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
4257 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004258 static_cast<const void*>(namespace_decl.GetNamespaceDecl()),
Greg Clayton437a1352012-04-09 22:43:43 +00004259 namespace_decl.GetQualifiedName().c_str());
4260 }
4261
Greg Clayton526e5af2010-11-13 03:52:47 +00004262 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004263}
4264
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004265uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004266SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004267{
4268 // Remember how many sc_list are in the list before we search in case
4269 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004270 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004271
4272 const uint32_t num_die_offsets = die_offsets.size();
4273 // Parse all of the types we found from the pubtypes matches
4274 uint32_t i;
4275 uint32_t num_matches = 0;
4276 for (i = 0; i < num_die_offsets; ++i)
4277 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004278 Type *matching_type = ResolveTypeUID (die_offsets[i]);
4279 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004280 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004281 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00004282 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004283 ++num_matches;
4284 if (num_matches >= max_matches)
4285 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004286 }
4287 }
4288
4289 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004290 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004291}
4292
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004293
4294size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00004295SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
4296 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00004297 DWARFCompileUnit* dwarf_cu,
4298 const DWARFDebugInfoEntry *parent_die,
4299 bool skip_artificial,
4300 bool &is_static,
Greg Clayton03969752014-02-24 18:53:11 +00004301 bool &is_variadic,
Greg Clayton5113dc82011-08-12 06:47:54 +00004302 TypeList* type_list,
Greg Clayton57ee3062013-07-11 22:46:58 +00004303 std::vector<ClangASTType>& function_param_types,
Greg Clayton5113dc82011-08-12 06:47:54 +00004304 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Claytond20deac2014-04-04 18:15:18 +00004305 unsigned &type_quals) // ,
4306 // ClangASTContext::TemplateParameterInfos &template_param_infos))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004307{
4308 if (parent_die == NULL)
4309 return 0;
4310
Todd Fialaee8bfc62014-09-11 17:29:12 +00004311 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Greg Claytond88d7592010-09-15 08:33:30 +00004312
Greg Clayton7fedea22010-11-16 02:10:54 +00004313 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004314 const DWARFDebugInfoEntry *die;
4315 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4316 {
4317 dw_tag_t tag = die->Tag();
4318 switch (tag)
4319 {
4320 case DW_TAG_formal_parameter:
4321 {
4322 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004323 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004324 if (num_attributes > 0)
4325 {
4326 const char *name = NULL;
4327 Declaration decl;
4328 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00004329 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004330 // one of None, Auto, Register, Extern, Static, PrivateExtern
4331
Sean Callanane2ef6e32010-09-23 03:01:22 +00004332 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004333 uint32_t i;
4334 for (i=0; i<num_attributes; ++i)
4335 {
4336 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4337 DWARFFormValue form_value;
4338 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4339 {
4340 switch (attr)
4341 {
4342 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4343 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4344 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4345 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00004346 case DW_AT_type: param_type_die_offset = form_value.Reference(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00004347 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004348 case DW_AT_location:
4349 // if (form_value.BlockData())
4350 // {
Ed Masteeeae7212013-10-24 20:43:47 +00004351 // const DWARFDataExtractor& debug_info_data = debug_info();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004352 // uint32_t block_length = form_value.Unsigned();
Ed Masteeeae7212013-10-24 20:43:47 +00004353 // DWARFDataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004354 // }
4355 // else
4356 // {
4357 // }
4358 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004359 case DW_AT_const_value:
4360 case DW_AT_default_value:
4361 case DW_AT_description:
4362 case DW_AT_endianity:
4363 case DW_AT_is_optional:
4364 case DW_AT_segment:
4365 case DW_AT_variable_parameter:
4366 default:
4367 case DW_AT_abstract_origin:
4368 case DW_AT_sibling:
4369 break;
4370 }
4371 }
4372 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00004373
Greg Clayton0fffff52010-09-24 05:15:53 +00004374 bool skip = false;
4375 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004376 {
Greg Clayton0fffff52010-09-24 05:15:53 +00004377 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00004378 {
4379 // In order to determine if a C++ member function is
4380 // "const" we have to look at the const-ness of "this"...
4381 // Ugly, but that
4382 if (arg_idx == 0)
4383 {
Greg Claytonf0705c82011-10-22 03:33:13 +00004384 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00004385 {
Greg Clayton5113dc82011-08-12 06:47:54 +00004386 // Often times compilers omit the "this" name for the
4387 // specification DIEs, so we can't rely upon the name
4388 // being in the formal parameter DIE...
4389 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00004390 {
Greg Clayton5113dc82011-08-12 06:47:54 +00004391 Type *this_type = ResolveTypeUID (param_type_die_offset);
4392 if (this_type)
4393 {
4394 uint32_t encoding_mask = this_type->GetEncodingMask();
4395 if (encoding_mask & Type::eEncodingIsPointerUID)
4396 {
4397 is_static = false;
4398
4399 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
4400 type_quals |= clang::Qualifiers::Const;
4401 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
4402 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00004403 }
4404 }
4405 }
4406 }
4407 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004408 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00004409 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004410 else
4411 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004412
Greg Clayton0fffff52010-09-24 05:15:53 +00004413 // HACK: Objective C formal parameters "self" and "_cmd"
4414 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00004415 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
4416 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00004417 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004418 switch (comp_unit->GetLanguage())
4419 {
4420 case eLanguageTypeObjC:
4421 case eLanguageTypeObjC_plus_plus:
4422 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
4423 skip = true;
4424 break;
4425 default:
4426 break;
4427 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004428 }
4429 }
4430 }
4431
4432 if (!skip)
4433 {
4434 Type *type = ResolveTypeUID(param_type_die_offset);
4435 if (type)
4436 {
Greg Claytonc93237c2010-10-01 20:48:32 +00004437 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00004438
Greg Clayton42ce2f32011-12-12 21:50:19 +00004439 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
4440 type->GetClangForwardType(),
4441 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00004442 assert(param_var_decl);
4443 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00004444
Greg Claytond0029442013-03-27 01:48:02 +00004445 GetClangASTContext().SetMetadataAsUserID (param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00004446 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004447 }
4448 }
Greg Clayton7fedea22010-11-16 02:10:54 +00004449 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004450 }
4451 break;
4452
Greg Clayton03969752014-02-24 18:53:11 +00004453 case DW_TAG_unspecified_parameters:
4454 is_variadic = true;
4455 break;
4456
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00004457 case DW_TAG_template_type_parameter:
4458 case DW_TAG_template_value_parameter:
Greg Claytond20deac2014-04-04 18:15:18 +00004459 // The one caller of this was never using the template_param_infos,
4460 // and the local variable was taking up a large amount of stack space
4461 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need
4462 // the template params back, we can add them back.
4463 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00004464 break;
4465
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004466 default:
4467 break;
4468 }
4469 }
Greg Clayton7fedea22010-11-16 02:10:54 +00004470 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004471}
4472
4473size_t
4474SymbolFileDWARF::ParseChildEnumerators
4475(
4476 const SymbolContext& sc,
Greg Clayton57ee3062013-07-11 22:46:58 +00004477 lldb_private::ClangASTType &clang_type,
Greg Clayton3e067532013-03-05 23:54:39 +00004478 bool is_signed,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004479 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00004480 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004481 const DWARFDebugInfoEntry *parent_die
4482)
4483{
4484 if (parent_die == NULL)
4485 return 0;
4486
4487 size_t enumerators_added = 0;
4488 const DWARFDebugInfoEntry *die;
Todd Fialaee8bfc62014-09-11 17:29:12 +00004489 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Greg Claytond88d7592010-09-15 08:33:30 +00004490
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004491 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4492 {
4493 const dw_tag_t tag = die->Tag();
4494 if (tag == DW_TAG_enumerator)
4495 {
4496 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004497 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004498 if (num_child_attributes > 0)
4499 {
4500 const char *name = NULL;
4501 bool got_value = false;
4502 int64_t enum_value = 0;
4503 Declaration decl;
4504
4505 uint32_t i;
4506 for (i=0; i<num_child_attributes; ++i)
4507 {
4508 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4509 DWARFFormValue form_value;
4510 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4511 {
4512 switch (attr)
4513 {
4514 case DW_AT_const_value:
4515 got_value = true;
Greg Clayton3e067532013-03-05 23:54:39 +00004516 if (is_signed)
4517 enum_value = form_value.Signed();
4518 else
4519 enum_value = form_value.Unsigned();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004520 break;
4521
4522 case DW_AT_name:
4523 name = form_value.AsCString(&get_debug_str_data());
4524 break;
4525
4526 case DW_AT_description:
4527 default:
4528 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4529 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4530 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4531 case DW_AT_sibling:
4532 break;
4533 }
4534 }
4535 }
4536
4537 if (name && name[0] && got_value)
4538 {
Greg Clayton57ee3062013-07-11 22:46:58 +00004539 clang_type.AddEnumerationValueToEnumerationType (clang_type.GetEnumerationIntegerType(),
4540 decl,
4541 name,
4542 enum_value,
4543 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004544 ++enumerators_added;
4545 }
4546 }
4547 }
4548 }
4549 return enumerators_added;
4550}
4551
4552void
4553SymbolFileDWARF::ParseChildArrayInfo
4554(
4555 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004556 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004557 const DWARFDebugInfoEntry *parent_die,
4558 int64_t& first_index,
4559 std::vector<uint64_t>& element_orders,
4560 uint32_t& byte_stride,
4561 uint32_t& bit_stride
4562)
4563{
4564 if (parent_die == NULL)
4565 return;
4566
4567 const DWARFDebugInfoEntry *die;
Todd Fialaee8bfc62014-09-11 17:29:12 +00004568 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004569 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4570 {
4571 const dw_tag_t tag = die->Tag();
4572 switch (tag)
4573 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004574 case DW_TAG_subrange_type:
4575 {
4576 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004577 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004578 if (num_child_attributes > 0)
4579 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004580 uint64_t num_elements = 0;
4581 uint64_t lower_bound = 0;
4582 uint64_t upper_bound = 0;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004583 bool upper_bound_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004584 uint32_t i;
4585 for (i=0; i<num_child_attributes; ++i)
4586 {
4587 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4588 DWARFFormValue form_value;
4589 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4590 {
4591 switch (attr)
4592 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004593 case DW_AT_name:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004594 break;
4595
4596 case DW_AT_count:
4597 num_elements = form_value.Unsigned();
4598 break;
4599
4600 case DW_AT_bit_stride:
4601 bit_stride = form_value.Unsigned();
4602 break;
4603
4604 case DW_AT_byte_stride:
4605 byte_stride = form_value.Unsigned();
4606 break;
4607
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004608 case DW_AT_lower_bound:
4609 lower_bound = form_value.Unsigned();
4610 break;
4611
4612 case DW_AT_upper_bound:
Greg Clayton4ef877f2012-12-06 02:33:54 +00004613 upper_bound_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004614 upper_bound = form_value.Unsigned();
4615 break;
4616
4617 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004618 case DW_AT_abstract_origin:
4619 case DW_AT_accessibility:
4620 case DW_AT_allocated:
4621 case DW_AT_associated:
4622 case DW_AT_data_location:
4623 case DW_AT_declaration:
4624 case DW_AT_description:
4625 case DW_AT_sibling:
4626 case DW_AT_threads_scaled:
4627 case DW_AT_type:
4628 case DW_AT_visibility:
4629 break;
4630 }
4631 }
4632 }
4633
Greg Claytone6a07792012-12-05 21:59:39 +00004634 if (num_elements == 0)
4635 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004636 if (upper_bound_valid && upper_bound >= lower_bound)
Greg Claytone6a07792012-12-05 21:59:39 +00004637 num_elements = upper_bound - lower_bound + 1;
4638 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004639
Sean Callananec979ba2012-10-22 23:56:48 +00004640 element_orders.push_back (num_elements);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004641 }
4642 }
4643 break;
4644 }
4645 }
4646}
4647
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004648TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004649SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004650{
4651 TypeSP type_sp;
4652 if (die != NULL)
4653 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004654 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004655 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004656 if (type_ptr == NULL)
4657 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004658 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004659 assert (lldb_cu);
4660 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004661 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004662 }
4663 else if (type_ptr != DIE_IS_BEING_PARSED)
4664 {
4665 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004666 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004667 }
4668
4669 }
4670 return type_sp;
4671}
4672
4673clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004674SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004675{
4676 if (die_offset != DW_INVALID_OFFSET)
4677 {
4678 DWARFCompileUnitSP cu_sp;
4679 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004680 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004681 }
4682 return NULL;
4683}
4684
Sean Callanan72e49402011-08-05 23:43:37 +00004685clang::DeclContext *
4686SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4687{
4688 if (die_offset != DW_INVALID_OFFSET)
4689 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004690 DWARFDebugInfo* debug_info = DebugInfo();
4691 if (debug_info)
4692 {
4693 DWARFCompileUnitSP cu_sp;
4694 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4695 if (die)
4696 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4697 }
Sean Callanan72e49402011-08-05 23:43:37 +00004698 }
4699 return NULL;
4700}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004701
Greg Clayton96d7d742010-11-10 23:42:09 +00004702clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004703SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004704{
Greg Clayton030a2042011-10-14 21:34:45 +00004705 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004706 {
Greg Clayton030a2042011-10-14 21:34:45 +00004707 // See if we already parsed this namespace DIE and associated it with a
4708 // uniqued namespace declaration
4709 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4710 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004711 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004712 else
4713 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004714 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4715 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004716 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
Greg Clayton5160ce52013-03-27 23:08:40 +00004717 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton9d3d6882011-10-31 23:51:19 +00004718 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004719 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004720 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004721 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004722 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00004723 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004724 static_cast<void*>(GetClangASTContext().getASTContext()),
Greg Claytone38a5ed2012-01-05 03:57:59 +00004725 MakeUserID(die->GetOffset()),
4726 namespace_name,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004727 static_cast<void*>(namespace_decl),
4728 static_cast<void*>(namespace_decl->getOriginalNamespace()));
Greg Clayton030a2042011-10-14 21:34:45 +00004729 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004730 else
4731 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004732 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00004733 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004734 static_cast<void*>(GetClangASTContext().getASTContext()),
Greg Claytone38a5ed2012-01-05 03:57:59 +00004735 MakeUserID(die->GetOffset()),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004736 static_cast<void*>(namespace_decl),
4737 static_cast<void*>(namespace_decl->getOriginalNamespace()));
Greg Clayton9d3d6882011-10-31 23:51:19 +00004738 }
Greg Clayton030a2042011-10-14 21:34:45 +00004739 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004740
4741 if (namespace_decl)
4742 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4743 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004744 }
4745 }
4746 return NULL;
4747}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004748
4749clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004750SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004751{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004752 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4753 if (clang_decl_ctx)
4754 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004755 // If this DIE has a specification, or an abstract origin, then trace to those.
4756
Greg Claytoncab36a32011-12-08 05:16:30 +00004757 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004758 if (die_offset != DW_INVALID_OFFSET)
4759 return GetClangDeclContextForDIEOffset (sc, die_offset);
4760
Greg Claytoncab36a32011-12-08 05:16:30 +00004761 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004762 if (die_offset != DW_INVALID_OFFSET)
4763 return GetClangDeclContextForDIEOffset (sc, die_offset);
4764
Greg Clayton5160ce52013-03-27 23:08:40 +00004765 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00004766 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00004767 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 +00004768 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004769 bool assert_not_being_parsed = true;
4770 ResolveTypeUID (cu, die, assert_not_being_parsed);
4771
Greg Clayton5cf58b92011-10-05 22:22:08 +00004772 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4773
4774 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004775}
4776
4777clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004778SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004779{
Greg Claytonca512b32011-01-14 04:54:56 +00004780 if (m_clang_tu_decl == NULL)
4781 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004782
Greg Clayton2bc22f82011-09-30 03:20:47 +00004783 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004784
4785 if (decl_ctx_die_copy)
4786 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004787
4788 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004789 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004790
Greg Clayton2bc22f82011-09-30 03:20:47 +00004791 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4792 if (pos != m_die_to_decl_ctx.end())
4793 return pos->second;
4794
4795 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004796 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004797 case DW_TAG_compile_unit:
4798 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004799
Greg Clayton2bc22f82011-09-30 03:20:47 +00004800 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004801 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004802 break;
4803
4804 case DW_TAG_structure_type:
4805 case DW_TAG_union_type:
4806 case DW_TAG_class_type:
4807 {
4808 Type* type = ResolveType (cu, decl_ctx_die);
4809 if (type)
4810 {
Greg Clayton57ee3062013-07-11 22:46:58 +00004811 clang::DeclContext *decl_ctx = type->GetClangForwardType().GetDeclContextForType ();
Greg Clayton2bc22f82011-09-30 03:20:47 +00004812 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004813 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004814 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4815 if (decl_ctx)
4816 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004817 }
4818 }
Greg Claytonca512b32011-01-14 04:54:56 +00004819 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004820 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004821
Greg Clayton2bc22f82011-09-30 03:20:47 +00004822 default:
4823 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004824 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004825 }
Greg Clayton7a345282010-11-09 23:46:37 +00004826 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004827}
4828
Greg Clayton2bc22f82011-09-30 03:20:47 +00004829
4830const DWARFDebugInfoEntry *
4831SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4832{
4833 if (cu && die)
4834 {
4835 const DWARFDebugInfoEntry * const decl_die = die;
4836
4837 while (die != NULL)
4838 {
4839 // If this is the original DIE that we are searching for a declaration
4840 // for, then don't look in the cache as we don't want our own decl
4841 // context to be our decl context...
4842 if (decl_die != die)
4843 {
4844 switch (die->Tag())
4845 {
4846 case DW_TAG_compile_unit:
4847 case DW_TAG_namespace:
4848 case DW_TAG_structure_type:
4849 case DW_TAG_union_type:
4850 case DW_TAG_class_type:
4851 return die;
4852
4853 default:
4854 break;
4855 }
4856 }
4857
4858 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4859 if (die_offset != DW_INVALID_OFFSET)
4860 {
4861 DWARFCompileUnit *spec_cu = cu;
4862 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4863 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4864 if (spec_die_decl_ctx_die)
4865 return spec_die_decl_ctx_die;
4866 }
4867
4868 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4869 if (die_offset != DW_INVALID_OFFSET)
4870 {
4871 DWARFCompileUnit *abs_cu = cu;
4872 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4873 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4874 if (abs_die_decl_ctx_die)
4875 return abs_die_decl_ctx_die;
4876 }
4877
4878 die = die->GetParent();
4879 }
4880 }
4881 return NULL;
4882}
4883
4884
Greg Clayton901c5ca2011-12-03 04:40:03 +00004885Symbol *
4886SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4887{
4888 Symbol *objc_class_symbol = NULL;
4889 if (m_obj_file)
4890 {
Greg Clayton3046e662013-07-10 01:23:25 +00004891 Symtab *symtab = m_obj_file->GetSymtab ();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004892 if (symtab)
4893 {
4894 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4895 eSymbolTypeObjCClass,
4896 Symtab::eDebugNo,
4897 Symtab::eVisibilityAny);
4898 }
4899 }
4900 return objc_class_symbol;
4901}
4902
Greg Claytonc7f03b62012-01-12 04:33:28 +00004903// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4904// then we can end up looking through all class types for a complete type and never find
4905// the full definition. We need to know if this attribute is supported, so we determine
4906// this here and cache th result. We also need to worry about the debug map DWARF file
4907// if we are doing darwin DWARF in .o file debugging.
4908bool
4909SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4910{
4911 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4912 {
4913 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4914 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4915 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4916 else
4917 {
4918 DWARFDebugInfo* debug_info = DebugInfo();
4919 const uint32_t num_compile_units = GetNumCompileUnits();
4920 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4921 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004922 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4923 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004924 {
4925 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4926 break;
4927 }
4928 }
4929 }
Greg Clayton1f746072012-08-29 21:13:06 +00004930 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004931 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4932 }
4933 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4934}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004935
4936// This function can be used when a DIE is found that is a forward declaration
4937// DIE and we want to try and find a type that has the complete definition.
4938TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004939SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4940 const ConstString &type_name,
4941 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004942{
4943
4944 TypeSP type_sp;
4945
Greg Claytonc7f03b62012-01-12 04:33:28 +00004946 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004947 return type_sp;
4948
4949 DIEArray die_offsets;
4950
4951 if (m_using_apple_tables)
4952 {
4953 if (m_apple_types_ap.get())
4954 {
4955 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004956 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004957 }
4958 }
4959 else
4960 {
4961 if (!m_indexed)
4962 Index ();
4963
4964 m_type_index.Find (type_name, die_offsets);
4965 }
4966
Greg Clayton901c5ca2011-12-03 04:40:03 +00004967 const size_t num_matches = die_offsets.size();
4968
Greg Clayton901c5ca2011-12-03 04:40:03 +00004969 DWARFCompileUnit* type_cu = NULL;
4970 const DWARFDebugInfoEntry* type_die = NULL;
4971 if (num_matches)
4972 {
4973 DWARFDebugInfo* debug_info = DebugInfo();
4974 for (size_t i=0; i<num_matches; ++i)
4975 {
4976 const dw_offset_t die_offset = die_offsets[i];
4977 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4978
4979 if (type_die)
4980 {
4981 bool try_resolving_type = false;
4982
4983 // Don't try and resolve the DIE we are looking for with the DIE itself!
4984 if (type_die != die)
4985 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004986 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004987 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004988 case DW_TAG_class_type:
4989 case DW_TAG_structure_type:
4990 try_resolving_type = true;
4991 break;
4992 default:
4993 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004994 }
4995 }
4996
4997 if (try_resolving_type)
4998 {
Ed Maste4c24b122013-10-17 20:13:14 +00004999 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
5000 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005001
5002 if (try_resolving_type)
5003 {
5004 Type *resolved_type = ResolveType (type_cu, type_die, false);
5005 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
5006 {
Ed Mastea0191d12013-10-17 20:42:56 +00005007 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n",
Greg Clayton901c5ca2011-12-03 04:40:03 +00005008 MakeUserID(die->GetOffset()),
Jim Ingham4af59612014-12-19 19:20:44 +00005009 m_obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Greg Clayton901c5ca2011-12-03 04:40:03 +00005010 MakeUserID(type_die->GetOffset()),
5011 MakeUserID(type_cu->GetOffset()));
5012
Greg Claytonc7f03b62012-01-12 04:33:28 +00005013 if (die)
5014 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00005015 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00005016 break;
5017 }
5018 }
5019 }
5020 }
5021 else
5022 {
5023 if (m_using_apple_tables)
5024 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005025 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
5026 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00005027 }
5028 }
5029
5030 }
5031 }
5032 return type_sp;
5033}
5034
Greg Claytona8022fa2012-04-24 21:22:41 +00005035
Greg Clayton80c26302012-02-05 06:12:47 +00005036//----------------------------------------------------------------------
5037// This function helps to ensure that the declaration contexts match for
5038// two different DIEs. Often times debug information will refer to a
5039// forward declaration of a type (the equivalent of "struct my_struct;".
5040// There will often be a declaration of that type elsewhere that has the
5041// full definition. When we go looking for the full type "my_struct", we
5042// will find one or more matches in the accelerator tables and we will
5043// then need to make sure the type was in the same declaration context
5044// as the original DIE. This function can efficiently compare two DIEs
5045// and will return true when the declaration context matches, and false
5046// when they don't.
5047//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00005048bool
5049SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
5050 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
5051{
Greg Claytona8022fa2012-04-24 21:22:41 +00005052 if (die1 == die2)
5053 return true;
5054
5055#if defined (LLDB_CONFIGURATION_DEBUG)
5056 // You can't and shouldn't call this function with a compile unit from
5057 // two different SymbolFileDWARF instances.
5058 assert (DebugInfo()->ContainsCompileUnit (cu1));
5059 assert (DebugInfo()->ContainsCompileUnit (cu2));
5060#endif
5061
Greg Clayton890ff562012-02-02 05:48:16 +00005062 DWARFDIECollection decl_ctx_1;
5063 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00005064 //The declaration DIE stack is a stack of the declaration context
5065 // DIEs all the way back to the compile unit. If a type "T" is
5066 // declared inside a class "B", and class "B" is declared inside
5067 // a class "A" and class "A" is in a namespace "lldb", and the
5068 // namespace is in a compile unit, there will be a stack of DIEs:
5069 //
5070 // [0] DW_TAG_class_type for "B"
5071 // [1] DW_TAG_class_type for "A"
5072 // [2] DW_TAG_namespace for "lldb"
5073 // [3] DW_TAG_compile_unit for the source file.
5074 //
5075 // We grab both contexts and make sure that everything matches
5076 // all the way back to the compiler unit.
5077
5078 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00005079 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00005080 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00005081 // Make sure the context arrays have the same size, otherwise
5082 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00005083 const size_t count1 = decl_ctx_1.Size();
5084 const size_t count2 = decl_ctx_2.Size();
5085 if (count1 != count2)
5086 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00005087
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00005088 // Make sure the DW_TAG values match all the way back up the
Greg Clayton80c26302012-02-05 06:12:47 +00005089 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00005090 const DWARFDebugInfoEntry *decl_ctx_die1;
5091 const DWARFDebugInfoEntry *decl_ctx_die2;
5092 size_t i;
5093 for (i=0; i<count1; i++)
5094 {
5095 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
5096 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
5097 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
5098 return false;
5099 }
Greg Clayton890ff562012-02-02 05:48:16 +00005100#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00005101
5102 // Make sure the top item in the decl context die array is always
5103 // DW_TAG_compile_unit. If it isn't then something went wrong in
5104 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00005105 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00005106
Greg Clayton890ff562012-02-02 05:48:16 +00005107#endif
5108 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00005109 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00005110 for (i=0; i<count1 - 1; i++)
5111 {
5112 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
5113 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
5114 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00005115 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00005116 // If the string was from a DW_FORM_strp, then the pointer will often
5117 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00005118 if (name1 == name2)
5119 continue;
5120
5121 // Name pointers are not equal, so only compare the strings
5122 // if both are not NULL.
5123 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00005124 {
Greg Clayton5569e642012-02-06 01:44:54 +00005125 // If the strings don't compare, we are done...
5126 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00005127 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00005128 }
5129 else
5130 {
5131 // One name was NULL while the other wasn't
5132 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00005133 }
5134 }
Greg Clayton80c26302012-02-05 06:12:47 +00005135 // We made it through all of the checks and the declaration contexts
5136 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00005137 return true;
5138}
Greg Clayton220a0072011-12-09 08:48:30 +00005139
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005140
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005141TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00005142SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
5143{
5144 TypeSP type_sp;
5145
5146 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
5147 if (dwarf_decl_ctx_count > 0)
5148 {
5149 const ConstString type_name(dwarf_decl_ctx[0].name);
5150 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
5151
5152 if (type_name)
5153 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005154 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
Greg Claytona8022fa2012-04-24 21:22:41 +00005155 if (log)
5156 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005157 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00005158 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
5159 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5160 dwarf_decl_ctx.GetQualifiedName());
5161 }
5162
5163 DIEArray die_offsets;
5164
5165 if (m_using_apple_tables)
5166 {
5167 if (m_apple_types_ap.get())
5168 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005169 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
5170 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
5171 if (has_tag && has_qualified_name_hash)
Greg Claytona8022fa2012-04-24 21:22:41 +00005172 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005173 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
5174 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name);
5175 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005176 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()");
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005177 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets);
5178 }
5179 else if (has_tag)
5180 {
5181 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005182 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()");
Greg Claytona8022fa2012-04-24 21:22:41 +00005183 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
5184 }
5185 else
5186 {
5187 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
5188 }
5189 }
5190 }
5191 else
5192 {
5193 if (!m_indexed)
5194 Index ();
5195
5196 m_type_index.Find (type_name, die_offsets);
5197 }
5198
5199 const size_t num_matches = die_offsets.size();
5200
5201
5202 DWARFCompileUnit* type_cu = NULL;
5203 const DWARFDebugInfoEntry* type_die = NULL;
5204 if (num_matches)
5205 {
5206 DWARFDebugInfo* debug_info = DebugInfo();
5207 for (size_t i=0; i<num_matches; ++i)
5208 {
5209 const dw_offset_t die_offset = die_offsets[i];
5210 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
5211
5212 if (type_die)
5213 {
5214 bool try_resolving_type = false;
5215
5216 // Don't try and resolve the DIE we are looking for with the DIE itself!
5217 const dw_tag_t type_tag = type_die->Tag();
5218 // Make sure the tags match
5219 if (type_tag == tag)
5220 {
5221 // The tags match, lets try resolving this type
5222 try_resolving_type = true;
5223 }
5224 else
5225 {
5226 // The tags don't match, but we need to watch our for a
5227 // forward declaration for a struct and ("struct foo")
5228 // ends up being a class ("class foo { ... };") or
5229 // vice versa.
5230 switch (type_tag)
5231 {
5232 case DW_TAG_class_type:
5233 // We had a "class foo", see if we ended up with a "struct foo { ... };"
5234 try_resolving_type = (tag == DW_TAG_structure_type);
5235 break;
5236 case DW_TAG_structure_type:
5237 // We had a "struct foo", see if we ended up with a "class foo { ... };"
5238 try_resolving_type = (tag == DW_TAG_class_type);
5239 break;
5240 default:
5241 // Tags don't match, don't event try to resolve
5242 // using this type whose name matches....
5243 break;
5244 }
5245 }
5246
5247 if (try_resolving_type)
5248 {
5249 DWARFDeclContext type_dwarf_decl_ctx;
5250 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
5251
5252 if (log)
5253 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005254 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00005255 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
5256 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5257 dwarf_decl_ctx.GetQualifiedName(),
5258 type_die->GetOffset(),
5259 type_dwarf_decl_ctx.GetQualifiedName());
5260 }
5261
5262 // Make sure the decl contexts match all the way up
5263 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
5264 {
5265 Type *resolved_type = ResolveType (type_cu, type_die, false);
5266 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
5267 {
5268 type_sp = resolved_type->shared_from_this();
5269 break;
5270 }
5271 }
5272 }
5273 else
5274 {
5275 if (log)
5276 {
5277 std::string qualified_name;
5278 type_die->GetQualifiedName(this, type_cu, qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00005279 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00005280 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
5281 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5282 dwarf_decl_ctx.GetQualifiedName(),
5283 type_die->GetOffset(),
5284 qualified_name.c_str());
5285 }
5286 }
5287 }
5288 else
5289 {
5290 if (m_using_apple_tables)
5291 {
5292 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
5293 die_offset, type_name.GetCString());
5294 }
5295 }
5296
5297 }
5298 }
5299 }
5300 }
5301 return type_sp;
5302}
5303
Greg Clayton4116e932012-05-15 02:33:01 +00005304bool
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005305SymbolFileDWARF::CopyUniqueClassMethodTypes (SymbolFileDWARF *src_symfile,
Sean Callanan2367f8a2013-02-26 01:12:25 +00005306 Type *class_type,
Greg Clayton4116e932012-05-15 02:33:01 +00005307 DWARFCompileUnit* src_cu,
5308 const DWARFDebugInfoEntry *src_class_die,
5309 DWARFCompileUnit* dst_cu,
Sean Callanan2367f8a2013-02-26 01:12:25 +00005310 const DWARFDebugInfoEntry *dst_class_die,
Greg Claytond20deac2014-04-04 18:15:18 +00005311 DWARFDIECollection &failures)
Greg Clayton4116e932012-05-15 02:33:01 +00005312{
5313 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
5314 return false;
5315 if (src_class_die->Tag() != dst_class_die->Tag())
5316 return false;
5317
5318 // We need to complete the class type so we can get all of the method types
5319 // parsed so we can then unique those types to their equivalent counterparts
5320 // in "dst_cu" and "dst_class_die"
5321 class_type->GetClangFullType();
5322
5323 const DWARFDebugInfoEntry *src_die;
5324 const DWARFDebugInfoEntry *dst_die;
5325 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
5326 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
Greg Clayton65ec1032012-11-12 21:27:20 +00005327 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die_artificial;
5328 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die_artificial;
Greg Clayton4116e932012-05-15 02:33:01 +00005329 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
5330 {
5331 if (src_die->Tag() == DW_TAG_subprogram)
5332 {
Greg Clayton84afacd2012-11-07 23:09:32 +00005333 // Make sure this is a declaration and not a concrete instance by looking
5334 // for DW_AT_declaration set to 1. Sometimes concrete function instances
5335 // are placed inside the class definitions and shouldn't be included in
5336 // the list of things are are tracking here.
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005337 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_declaration, 0) == 1)
Greg Clayton84afacd2012-11-07 23:09:32 +00005338 {
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005339 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
Greg Clayton84afacd2012-11-07 23:09:32 +00005340 if (src_name)
Greg Clayton65ec1032012-11-12 21:27:20 +00005341 {
5342 ConstString src_const_name(src_name);
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005343 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_artificial, 0))
Greg Clayton65ec1032012-11-12 21:27:20 +00005344 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die);
5345 else
5346 src_name_to_die.Append(src_const_name.GetCString(), src_die);
5347 }
Greg Clayton84afacd2012-11-07 23:09:32 +00005348 }
Greg Clayton4116e932012-05-15 02:33:01 +00005349 }
5350 }
5351 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
5352 {
5353 if (dst_die->Tag() == DW_TAG_subprogram)
5354 {
Greg Clayton84afacd2012-11-07 23:09:32 +00005355 // Make sure this is a declaration and not a concrete instance by looking
5356 // for DW_AT_declaration set to 1. Sometimes concrete function instances
5357 // are placed inside the class definitions and shouldn't be included in
5358 // the list of things are are tracking here.
5359 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_declaration, 0) == 1)
5360 {
5361 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
5362 if (dst_name)
Greg Clayton65ec1032012-11-12 21:27:20 +00005363 {
5364 ConstString dst_const_name(dst_name);
5365 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_artificial, 0))
5366 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die);
5367 else
5368 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die);
5369 }
Greg Clayton84afacd2012-11-07 23:09:32 +00005370 }
Greg Clayton4116e932012-05-15 02:33:01 +00005371 }
5372 }
5373 const uint32_t src_size = src_name_to_die.GetSize ();
5374 const uint32_t dst_size = dst_name_to_die.GetSize ();
Greg Clayton5160ce52013-03-27 23:08:40 +00005375 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
Sean Callanan2367f8a2013-02-26 01:12:25 +00005376
5377 // Is everything kosher so we can go through the members at top speed?
5378 bool fast_path = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005379
Sean Callanan2367f8a2013-02-26 01:12:25 +00005380 if (src_size != dst_size)
Greg Clayton4116e932012-05-15 02:33:01 +00005381 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00005382 if (src_size != 0 && dst_size != 0)
5383 {
5384 if (log)
5385 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)",
5386 src_class_die->GetOffset(),
5387 dst_class_die->GetOffset(),
5388 src_size,
5389 dst_size);
5390 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005391
Sean Callanan2367f8a2013-02-26 01:12:25 +00005392 fast_path = false;
5393 }
5394
5395 uint32_t idx;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005396
Sean Callanan2367f8a2013-02-26 01:12:25 +00005397 if (fast_path)
5398 {
Greg Clayton4116e932012-05-15 02:33:01 +00005399 for (idx = 0; idx < src_size; ++idx)
5400 {
5401 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5402 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
5403
5404 if (src_die->Tag() != dst_die->Tag())
5405 {
5406 if (log)
5407 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)",
5408 src_class_die->GetOffset(),
5409 dst_class_die->GetOffset(),
5410 src_die->GetOffset(),
5411 DW_TAG_value_to_name(src_die->Tag()),
5412 dst_die->GetOffset(),
5413 DW_TAG_value_to_name(src_die->Tag()));
Sean Callanan2367f8a2013-02-26 01:12:25 +00005414 fast_path = false;
Greg Clayton4116e932012-05-15 02:33:01 +00005415 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005416
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005417 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
Greg Clayton4116e932012-05-15 02:33:01 +00005418 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005419
Greg Clayton4116e932012-05-15 02:33:01 +00005420 // Make sure the names match
5421 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
5422 continue;
5423
5424 if (log)
5425 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)",
5426 src_class_die->GetOffset(),
5427 dst_class_die->GetOffset(),
5428 src_die->GetOffset(),
5429 src_name,
5430 dst_die->GetOffset(),
5431 dst_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005432
Sean Callanan2367f8a2013-02-26 01:12:25 +00005433 fast_path = false;
Greg Clayton4116e932012-05-15 02:33:01 +00005434 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005435 }
Greg Clayton4116e932012-05-15 02:33:01 +00005436
Sean Callanan2367f8a2013-02-26 01:12:25 +00005437 // Now do the work of linking the DeclContexts and Types.
5438 if (fast_path)
5439 {
5440 // We can do this quickly. Just run across the tables index-for-index since
5441 // we know each node has matching names and tags.
Greg Clayton4116e932012-05-15 02:33:01 +00005442 for (idx = 0; idx < src_size; ++idx)
5443 {
5444 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5445 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005446
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005447 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
Greg Clayton4116e932012-05-15 02:33:01 +00005448 if (src_decl_ctx)
5449 {
5450 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005451 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
5452 static_cast<void*>(src_decl_ctx),
5453 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005454 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5455 }
5456 else
5457 {
5458 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005459 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found",
5460 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005461 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005462
Greg Clayton4116e932012-05-15 02:33:01 +00005463 Type *src_child_type = m_die_to_type[src_die];
5464 if (src_child_type)
5465 {
5466 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005467 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
5468 static_cast<void*>(src_child_type),
5469 src_child_type->GetID(),
5470 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005471 m_die_to_type[dst_die] = src_child_type;
5472 }
5473 else
5474 {
5475 if (log)
Greg Clayton65ec1032012-11-12 21:27:20 +00005476 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());
5477 }
5478 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005479 }
5480 else
5481 {
5482 // We must do this slowly. For each member of the destination, look
5483 // up a member in the source with the same name, check its tag, and
5484 // unique them if everything matches up. Report failures.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005485
Sean Callanan2367f8a2013-02-26 01:12:25 +00005486 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty())
5487 {
5488 src_name_to_die.Sort();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005489
Sean Callanan2367f8a2013-02-26 01:12:25 +00005490 for (idx = 0; idx < dst_size; ++idx)
5491 {
5492 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx);
5493 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
5494 src_die = src_name_to_die.Find(dst_name, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005495
Sean Callanan2367f8a2013-02-26 01:12:25 +00005496 if (src_die && (src_die->Tag() == dst_die->Tag()))
5497 {
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005498 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
Sean Callanan2367f8a2013-02-26 01:12:25 +00005499 if (src_decl_ctx)
5500 {
5501 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005502 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
5503 static_cast<void*>(src_decl_ctx),
5504 src_die->GetOffset(),
5505 dst_die->GetOffset());
Sean Callanan2367f8a2013-02-26 01:12:25 +00005506 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5507 }
5508 else
5509 {
5510 if (log)
5511 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());
5512 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005513
Sean Callanan2367f8a2013-02-26 01:12:25 +00005514 Type *src_child_type = m_die_to_type[src_die];
5515 if (src_child_type)
5516 {
5517 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005518 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
5519 static_cast<void*>(src_child_type),
5520 src_child_type->GetID(),
5521 src_die->GetOffset(),
5522 dst_die->GetOffset());
Sean Callanan2367f8a2013-02-26 01:12:25 +00005523 m_die_to_type[dst_die] = src_child_type;
5524 }
5525 else
5526 {
5527 if (log)
5528 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());
5529 }
5530 }
5531 else
5532 {
5533 if (log)
5534 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005535
Greg Claytond20deac2014-04-04 18:15:18 +00005536 failures.Append(dst_die);
Sean Callanan2367f8a2013-02-26 01:12:25 +00005537 }
5538 }
5539 }
5540 }
5541
5542 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize ();
5543 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize ();
5544
5545 UniqueCStringMap<const DWARFDebugInfoEntry *> name_to_die_artificial_not_in_src;
5546
5547 if (src_size_artificial && dst_size_artificial)
5548 {
5549 dst_name_to_die_artificial.Sort();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005550
Greg Clayton65ec1032012-11-12 21:27:20 +00005551 for (idx = 0; idx < src_size_artificial; ++idx)
5552 {
5553 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx);
5554 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
5555 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005556
Greg Clayton65ec1032012-11-12 21:27:20 +00005557 if (dst_die)
5558 {
Greg Clayton65ec1032012-11-12 21:27:20 +00005559 // Both classes have the artificial types, link them
5560 clang::DeclContext *src_decl_ctx = m_die_to_decl_ctx[src_die];
5561 if (src_decl_ctx)
5562 {
5563 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005564 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
5565 static_cast<void*>(src_decl_ctx),
5566 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005567 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5568 }
5569 else
5570 {
5571 if (log)
5572 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());
5573 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005574
Greg Clayton65ec1032012-11-12 21:27:20 +00005575 Type *src_child_type = m_die_to_type[src_die];
5576 if (src_child_type)
5577 {
5578 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005579 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
5580 static_cast<void*>(src_child_type),
5581 src_child_type->GetID(),
5582 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005583 m_die_to_type[dst_die] = src_child_type;
5584 }
5585 else
5586 {
5587 if (log)
5588 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());
5589 }
5590 }
5591 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005592 }
Greg Clayton65ec1032012-11-12 21:27:20 +00005593
Sean Callanan2367f8a2013-02-26 01:12:25 +00005594 if (dst_size_artificial)
Greg Clayton4116e932012-05-15 02:33:01 +00005595 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00005596 for (idx = 0; idx < dst_size_artificial; ++idx)
5597 {
5598 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx);
5599 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
5600 if (log)
5601 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die->GetOffset(), dst_name_artificial);
5602
Greg Claytond20deac2014-04-04 18:15:18 +00005603 failures.Append(dst_die);
Sean Callanan2367f8a2013-02-26 01:12:25 +00005604 }
Greg Clayton4116e932012-05-15 02:33:01 +00005605 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005606
Greg Claytond20deac2014-04-04 18:15:18 +00005607 return (failures.Size() != 0);
Greg Clayton4116e932012-05-15 02:33:01 +00005608}
Greg Claytona8022fa2012-04-24 21:22:41 +00005609
5610TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00005611SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005612{
5613 TypeSP type_sp;
5614
Greg Clayton1be10fc2010-09-29 01:12:09 +00005615 if (type_is_new_ptr)
5616 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00005617
Todd Fiala0a70a842014-05-28 16:43:26 +00005618#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
Greg Clayton1fba87112012-02-09 20:03:49 +00005619 static DIEStack g_die_stack;
5620 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
5621#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00005622
Sean Callananc7fbf732010-08-06 00:32:49 +00005623 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005624 if (die != NULL)
5625 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005626 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005627 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00005628 {
5629 const DWARFDebugInfoEntry *context_die;
5630 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005631
Greg Clayton5160ce52013-03-27 23:08:40 +00005632 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005633 die->GetOffset(),
5634 static_cast<void*>(context),
5635 context_die->GetOffset(),
5636 DW_TAG_value_to_name(die->Tag()),
5637 die->GetName(this, dwarf_cu));
5638
Todd Fiala0a70a842014-05-28 16:43:26 +00005639#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
Greg Clayton1fba87112012-02-09 20:03:49 +00005640 scoped_die_logger.Push (dwarf_cu, die);
Greg Clayton5160ce52013-03-27 23:08:40 +00005641 g_die_stack.LogDIEs(log, this);
Greg Clayton1fba87112012-02-09 20:03:49 +00005642#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00005643 }
Greg Clayton3bffb082011-12-10 02:15:28 +00005644//
Greg Clayton5160ce52013-03-27 23:08:40 +00005645// Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005646// if (log && dwarf_cu)
5647// {
5648// StreamString s;
5649// die->DumpLocation (this, dwarf_cu, s);
Greg Clayton5160ce52013-03-27 23:08:40 +00005650// GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00005651//
5652// }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005653
Greg Clayton594e5ed2010-09-27 21:07:38 +00005654 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005655 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00005656 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005657 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005658 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005659 if (type_is_new_ptr)
5660 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005661
Greg Clayton594e5ed2010-09-27 21:07:38 +00005662 const dw_tag_t tag = die->Tag();
5663
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005664 bool is_forward_declaration = false;
5665 DWARFDebugInfoEntry::Attributes attributes;
5666 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00005667 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00005668 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
Greg Claytonfaac1112013-03-14 18:31:44 +00005669 uint64_t byte_size = 0;
Greg Clayton526e5af2010-11-13 03:52:47 +00005670 Declaration decl;
5671
Greg Clayton4957bf62010-09-30 21:49:03 +00005672 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton57ee3062013-07-11 22:46:58 +00005673 ClangASTType clang_type;
Greg Claytond20deac2014-04-04 18:15:18 +00005674 DWARFFormValue form_value;
5675
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005676 dw_attr_t attr;
5677
5678 switch (tag)
5679 {
5680 case DW_TAG_base_type:
5681 case DW_TAG_pointer_type:
5682 case DW_TAG_reference_type:
Sean Callanance8af862012-05-21 23:31:51 +00005683 case DW_TAG_rvalue_reference_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005684 case DW_TAG_typedef:
5685 case DW_TAG_const_type:
5686 case DW_TAG_restrict_type:
5687 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00005688 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005689 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005690 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005691 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005692
Greg Claytond88d7592010-09-15 08:33:30 +00005693 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005694 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005695 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
5696
5697 if (num_attributes > 0)
5698 {
5699 uint32_t i;
5700 for (i=0; i<num_attributes; ++i)
5701 {
5702 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005703 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5704 {
5705 switch (attr)
5706 {
5707 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5708 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5709 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5710 case DW_AT_name:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005711
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005712 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00005713 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
5714 // include the "&"...
5715 if (tag == DW_TAG_reference_type)
5716 {
5717 if (strchr (type_name_cstr, '&') == NULL)
5718 type_name_cstr = NULL;
5719 }
5720 if (type_name_cstr)
5721 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005722 break;
Greg Clayton23f59502012-07-17 03:23:13 +00005723 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005724 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
Greg Clayton54166af2014-11-22 01:58:59 +00005725 case DW_AT_type: encoding_uid = form_value.Reference(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005726 default:
5727 case DW_AT_sibling:
5728 break;
5729 }
5730 }
5731 }
5732 }
5733
Ed Mastea0191d12013-10-17 20:42:56 +00005734 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
Greg Claytonc93237c2010-10-01 20:48:32 +00005735
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005736 switch (tag)
5737 {
5738 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00005739 break;
5740
Greg Claytond4436412011-10-28 21:00:00 +00005741 case DW_TAG_unspecified_type:
Greg Clayton7fba2632013-07-01 21:01:52 +00005742 if (strcmp(type_name_cstr, "nullptr_t") == 0 ||
5743 strcmp(type_name_cstr, "decltype(nullptr)") == 0 )
Greg Claytond4436412011-10-28 21:00:00 +00005744 {
5745 resolve_state = Type::eResolveStateFull;
Greg Clayton57ee3062013-07-11 22:46:58 +00005746 clang_type = ast.GetBasicType(eBasicTypeNullPtr);
Greg Claytond4436412011-10-28 21:00:00 +00005747 break;
5748 }
5749 // Fall through to base type below in case we can handle the type there...
5750
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005751 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00005752 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005753 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
5754 encoding,
5755 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005756 break;
5757
Sean Callanance8af862012-05-21 23:31:51 +00005758 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
5759 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
5760 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
5761 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
5762 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
5763 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
5764 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005765 }
5766
Greg Clayton57ee3062013-07-11 22:46:58 +00005767 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005768 {
Sean Callanan82587052013-01-03 00:05:56 +00005769 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005770
Sean Callanan82587052013-01-03 00:05:56 +00005771 if (translation_unit_is_objc)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005772 {
Sean Callanan82587052013-01-03 00:05:56 +00005773 if (type_name_cstr != NULL)
Greg Claytonc7f03b62012-01-12 04:33:28 +00005774 {
Sean Callanan82587052013-01-03 00:05:56 +00005775 static ConstString g_objc_type_name_id("id");
5776 static ConstString g_objc_type_name_Class("Class");
5777 static ConstString g_objc_type_name_selector("SEL");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005778
Sean Callanan82587052013-01-03 00:05:56 +00005779 if (type_name_const_str == g_objc_type_name_id)
5780 {
5781 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005782 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 +00005783 die->GetOffset(),
5784 DW_TAG_value_to_name(die->Tag()),
5785 die->GetName(this, dwarf_cu));
Greg Clayton57ee3062013-07-11 22:46:58 +00005786 clang_type = ast.GetBasicType(eBasicTypeObjCID);
Sean Callanan82587052013-01-03 00:05:56 +00005787 encoding_data_type = Type::eEncodingIsUID;
5788 encoding_uid = LLDB_INVALID_UID;
5789 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00005790
Sean Callanan82587052013-01-03 00:05:56 +00005791 }
5792 else if (type_name_const_str == g_objc_type_name_Class)
5793 {
5794 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005795 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 +00005796 die->GetOffset(),
5797 DW_TAG_value_to_name(die->Tag()),
5798 die->GetName(this, dwarf_cu));
Greg Clayton57ee3062013-07-11 22:46:58 +00005799 clang_type = ast.GetBasicType(eBasicTypeObjCClass);
Sean Callanan82587052013-01-03 00:05:56 +00005800 encoding_data_type = Type::eEncodingIsUID;
5801 encoding_uid = LLDB_INVALID_UID;
5802 resolve_state = Type::eResolveStateFull;
5803 }
5804 else if (type_name_const_str == g_objc_type_name_selector)
5805 {
5806 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005807 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 +00005808 die->GetOffset(),
5809 DW_TAG_value_to_name(die->Tag()),
5810 die->GetName(this, dwarf_cu));
Greg Clayton57ee3062013-07-11 22:46:58 +00005811 clang_type = ast.GetBasicType(eBasicTypeObjCSel);
Sean Callanan82587052013-01-03 00:05:56 +00005812 encoding_data_type = Type::eEncodingIsUID;
5813 encoding_uid = LLDB_INVALID_UID;
5814 resolve_state = Type::eResolveStateFull;
5815 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00005816 }
Sean Callanan82587052013-01-03 00:05:56 +00005817 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID)
Greg Claytonc7f03b62012-01-12 04:33:28 +00005818 {
Sean Callanan82587052013-01-03 00:05:56 +00005819 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id".
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005820
Sean Callanan82587052013-01-03 00:05:56 +00005821 DWARFDebugInfoEntry* encoding_die = dwarf_cu->GetDIEPtr(encoding_uid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005822
Sean Callanan82587052013-01-03 00:05:56 +00005823 if (encoding_die && encoding_die->Tag() == DW_TAG_structure_type)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005824 {
Sean Callanan82587052013-01-03 00:05:56 +00005825 if (const char *struct_name = encoding_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL))
5826 {
5827 if (!strcmp(struct_name, "objc_object"))
5828 {
5829 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005830 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 +00005831 die->GetOffset(),
5832 DW_TAG_value_to_name(die->Tag()),
5833 die->GetName(this, dwarf_cu));
Greg Clayton57ee3062013-07-11 22:46:58 +00005834 clang_type = ast.GetBasicType(eBasicTypeObjCID);
Sean Callanan82587052013-01-03 00:05:56 +00005835 encoding_data_type = Type::eEncodingIsUID;
5836 encoding_uid = LLDB_INVALID_UID;
5837 resolve_state = Type::eResolveStateFull;
5838 }
5839 }
5840 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00005841 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005842 }
5843 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005844
Greg Clayton81c22f62011-10-19 18:09:39 +00005845 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005846 this,
5847 type_name_const_str,
5848 byte_size,
5849 NULL,
5850 encoding_uid,
5851 encoding_data_type,
5852 &decl,
5853 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005854 resolve_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005855
Greg Clayton594e5ed2010-09-27 21:07:38 +00005856 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005857
5858// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
5859// if (encoding_type != NULL)
5860// {
5861// if (encoding_type != DIE_IS_BEING_PARSED)
5862// type_sp->SetEncodingType(encoding_type);
5863// else
5864// m_indirect_fixups.push_back(type_sp.get());
5865// }
5866 }
5867 break;
5868
5869 case DW_TAG_structure_type:
5870 case DW_TAG_union_type:
5871 case DW_TAG_class_type:
5872 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005873 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005874 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Greg Clayton23f59502012-07-17 03:23:13 +00005875 bool byte_size_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005876
Greg Clayton9e409562010-07-28 02:04:09 +00005877 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00005878 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005879 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00005880 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005881 if (num_attributes > 0)
5882 {
5883 uint32_t i;
5884 for (i=0; i<num_attributes; ++i)
5885 {
5886 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005887 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5888 {
5889 switch (attr)
5890 {
Greg Clayton9e409562010-07-28 02:04:09 +00005891 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00005892 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
Ed Maste4c24b122013-10-17 20:13:14 +00005893 {
5894 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
5895 // point to the compile unit file, so we clear this invalid value
5896 // so that we can still unique types efficiently.
Greg Claytonc7f03b62012-01-12 04:33:28 +00005897 decl.SetFile(FileSpec ("<invalid>", false));
Ed Maste4c24b122013-10-17 20:13:14 +00005898 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00005899 else
5900 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00005901 break;
5902
5903 case DW_AT_decl_line:
5904 decl.SetLine(form_value.Unsigned());
5905 break;
5906
5907 case DW_AT_decl_column:
5908 decl.SetColumn(form_value.Unsigned());
5909 break;
5910
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005911 case DW_AT_name:
5912 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005913 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005914 break;
Greg Clayton9e409562010-07-28 02:04:09 +00005915
5916 case DW_AT_byte_size:
5917 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00005918 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00005919 break;
5920
5921 case DW_AT_accessibility:
5922 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
5923 break;
5924
5925 case DW_AT_declaration:
Greg Clayton1c8ef472013-04-05 23:27:21 +00005926 is_forward_declaration = form_value.Boolean();
Greg Clayton9e409562010-07-28 02:04:09 +00005927 break;
5928
5929 case DW_AT_APPLE_runtime_class:
5930 class_language = (LanguageType)form_value.Signed();
5931 break;
5932
Greg Clayton18774842011-11-29 23:40:34 +00005933 case DW_AT_APPLE_objc_complete_type:
5934 is_complete_objc_class = form_value.Signed();
5935 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005936
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005937 case DW_AT_allocated:
5938 case DW_AT_associated:
5939 case DW_AT_data_location:
5940 case DW_AT_description:
5941 case DW_AT_start_scope:
5942 case DW_AT_visibility:
5943 default:
5944 case DW_AT_sibling:
5945 break;
5946 }
5947 }
5948 }
5949 }
Greg Claytond20deac2014-04-04 18:15:18 +00005950
5951 // UniqueDWARFASTType is large, so don't create a local variables on the
5952 // stack, put it on the heap. This function is often called recursively
5953 // and clang isn't good and sharing the stack space for variables in different blocks.
5954 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType());
Sean Callanan8e8072d2012-02-07 21:13:38 +00005955
Greg Clayton123edca2012-03-02 00:07:15 +00005956 // Only try and unique the type if it has a name.
5957 if (type_name_const_str &&
5958 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00005959 this,
5960 dwarf_cu,
5961 die,
5962 decl,
5963 byte_size_valid ? byte_size : -1,
Greg Claytond20deac2014-04-04 18:15:18 +00005964 *unique_ast_entry_ap))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005965 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005966 // We have already parsed this type or from another
5967 // compile unit. GCC loves to use the "one definition
5968 // rule" which can result in multiple definitions
5969 // of the same class over and over in each compile
5970 // unit.
Greg Claytond20deac2014-04-04 18:15:18 +00005971 type_sp = unique_ast_entry_ap->m_type_sp;
Sean Callanan8e8072d2012-02-07 21:13:38 +00005972 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00005973 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005974 m_die_to_type[die] = type_sp.get();
5975 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005976 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005977 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005978
Daniel Malead01b2952012-11-29 21:49:15 +00005979 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 +00005980
5981 int tag_decl_kind = -1;
5982 AccessType default_accessibility = eAccessNone;
5983 if (tag == DW_TAG_structure_type)
5984 {
5985 tag_decl_kind = clang::TTK_Struct;
5986 default_accessibility = eAccessPublic;
5987 }
5988 else if (tag == DW_TAG_union_type)
5989 {
5990 tag_decl_kind = clang::TTK_Union;
5991 default_accessibility = eAccessPublic;
5992 }
5993 else if (tag == DW_TAG_class_type)
5994 {
5995 tag_decl_kind = clang::TTK_Class;
5996 default_accessibility = eAccessPrivate;
5997 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005998
Greg Clayton3a5f29a2011-11-30 02:48:28 +00005999 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
6000 die->HasChildren() == false &&
6001 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
6002 {
6003 // Work around an issue with clang at the moment where
6004 // forward declarations for objective C classes are emitted
6005 // as:
6006 // DW_TAG_structure_type [2]
6007 // DW_AT_name( "ForwardObjcClass" )
6008 // DW_AT_byte_size( 0x00 )
6009 // DW_AT_decl_file( "..." )
6010 // DW_AT_decl_line( 1 )
6011 //
6012 // Note that there is no DW_AT_declaration and there are
6013 // no children, and the byte size is zero.
6014 is_forward_declaration = true;
6015 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006016
Sean Callanan7457f8d2012-04-25 01:03:57 +00006017 if (class_language == eLanguageTypeObjC ||
6018 class_language == eLanguageTypeObjC_plus_plus)
Greg Clayton18774842011-11-29 23:40:34 +00006019 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00006020 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00006021 {
6022 // We have a valid eSymbolTypeObjCClass class symbol whose
6023 // name matches the current objective C class that we
6024 // are trying to find and this DIE isn't the complete
6025 // definition (we checked is_complete_objc_class above and
6026 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00006027 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006028
Greg Clayton1f746072012-08-29 21:13:06 +00006029 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton901c5ca2011-12-03 04:40:03 +00006030 {
6031 // We weren't able to find a full declaration in
6032 // this DWARF, see if we have a declaration anywhere
6033 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00006034 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00006035 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006036
Greg Clayton901c5ca2011-12-03 04:40:03 +00006037 if (type_sp)
6038 {
6039 if (log)
6040 {
Greg Clayton5160ce52013-03-27 23:08:40 +00006041 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00006042 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006043 static_cast<void*>(this),
Greg Claytone38a5ed2012-01-05 03:57:59 +00006044 die->GetOffset(),
6045 DW_TAG_value_to_name(tag),
6046 type_name_cstr,
6047 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00006048 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006049
Greg Clayton901c5ca2011-12-03 04:40:03 +00006050 // We found a real definition for this type elsewhere
6051 // so lets use it and cache the fact that we found
6052 // a complete type for this die
6053 m_die_to_type[die] = type_sp.get();
6054 return type_sp;
6055 }
6056 }
6057 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006058
Greg Clayton901c5ca2011-12-03 04:40:03 +00006059
6060 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006061 {
6062 // We have a forward declaration to a type and we need
6063 // to try and find a full declaration. We look in the
6064 // current type index just in case we have a forward
6065 // declaration followed by an actual declarations in the
6066 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00006067 if (log)
6068 {
Greg Clayton5160ce52013-03-27 23:08:40 +00006069 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00006070 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006071 static_cast<void*>(this),
Greg Claytone38a5ed2012-01-05 03:57:59 +00006072 die->GetOffset(),
6073 DW_TAG_value_to_name(tag),
6074 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00006075 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006076
Greg Claytona8022fa2012-04-24 21:22:41 +00006077 DWARFDeclContext die_decl_ctx;
6078 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx);
6079
6080 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
6081 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006082
Greg Clayton1f746072012-08-29 21:13:06 +00006083 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton4272cc72011-02-02 02:24:04 +00006084 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006085 // We weren't able to find a full declaration in
6086 // this DWARF, see if we have a declaration anywhere
6087 // else...
Greg Claytona8022fa2012-04-24 21:22:41 +00006088 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton4272cc72011-02-02 02:24:04 +00006089 }
6090
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006091 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00006092 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00006093 if (log)
6094 {
Greg Clayton5160ce52013-03-27 23:08:40 +00006095 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00006096 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006097 static_cast<void*>(this),
Greg Claytone38a5ed2012-01-05 03:57:59 +00006098 die->GetOffset(),
6099 DW_TAG_value_to_name(tag),
6100 type_name_cstr,
6101 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00006102 }
6103
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006104 // We found a real definition for this type elsewhere
6105 // so lets use it and cache the fact that we found
6106 // a complete type for this die
6107 m_die_to_type[die] = type_sp.get();
6108 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00006109 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006110 }
6111 assert (tag_decl_kind != -1);
6112 bool clang_type_was_created = false;
Greg Clayton57ee3062013-07-11 22:46:58 +00006113 clang_type.SetClangType(ast.getASTContext(), m_forward_decl_die_to_clang_type.lookup (die));
6114 if (!clang_type)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006115 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00006116 const DWARFDebugInfoEntry *decl_ctx_die;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006117
Sean Callanan4d04c6a2012-02-09 22:54:11 +00006118 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00006119 if (accessibility == eAccessNone && decl_ctx)
6120 {
6121 // Check the decl context that contains this class/struct/union.
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00006122 // If it is a class we must give it an accessibility.
Greg Clayton55561e92011-10-26 03:31:36 +00006123 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
6124 if (DeclKindIsCXXClass (containing_decl_kind))
6125 accessibility = default_accessibility;
6126 }
6127
Greg Claytonc4ffd662013-03-08 01:37:30 +00006128 ClangASTMetadata metadata;
6129 metadata.SetUserID(MakeUserID(die->GetOffset()));
6130 metadata.SetIsDynamicCXXType(ClassOrStructIsVirtual (dwarf_cu, die));
6131
Greg Claytonf0705c82011-10-22 03:33:13 +00006132 if (type_name_cstr && strchr (type_name_cstr, '<'))
6133 {
6134 ClangASTContext::TemplateParameterInfos template_param_infos;
6135 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
6136 {
6137 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00006138 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00006139 type_name_cstr,
6140 tag_decl_kind,
6141 template_param_infos);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006142
Greg Claytonf0705c82011-10-22 03:33:13 +00006143 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
6144 class_template_decl,
6145 tag_decl_kind,
6146 template_param_infos);
6147 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
6148 clang_type_was_created = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006149
Greg Claytond0029442013-03-27 01:48:02 +00006150 GetClangASTContext().SetMetadata (class_template_decl, metadata);
6151 GetClangASTContext().SetMetadata (class_specialization_decl, metadata);
Greg Claytonf0705c82011-10-22 03:33:13 +00006152 }
6153 }
6154
6155 if (!clang_type_was_created)
6156 {
6157 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00006158 clang_type = ast.CreateRecordType (decl_ctx,
6159 accessibility,
6160 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00006161 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00006162 class_language,
Jim Ingham379397632012-10-27 02:54:13 +00006163 &metadata);
Greg Claytonf0705c82011-10-22 03:33:13 +00006164 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006165 }
6166
6167 // Store a forward declaration to this class type in case any
6168 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00006169 // types for function prototypes.
Greg Clayton57ee3062013-07-11 22:46:58 +00006170 LinkDeclContextToDIE(clang_type.GetDeclContextForType(), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00006171 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006172 this,
6173 type_name_const_str,
6174 byte_size,
6175 NULL,
6176 LLDB_INVALID_UID,
6177 Type::eEncodingIsUID,
6178 &decl,
6179 clang_type,
6180 Type::eResolveStateForward));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006181
Sean Callanan72772842012-02-22 23:57:45 +00006182 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006183
6184
6185 // Add our type to the unique type map so we don't
6186 // end up creating many copies of the same type over
6187 // and over in the ASTContext for our module
Greg Claytond20deac2014-04-04 18:15:18 +00006188 unique_ast_entry_ap->m_type_sp = type_sp;
6189 unique_ast_entry_ap->m_symfile = this;
6190 unique_ast_entry_ap->m_cu = dwarf_cu;
6191 unique_ast_entry_ap->m_die = die;
6192 unique_ast_entry_ap->m_declaration = decl;
6193 unique_ast_entry_ap->m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00006194 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
Greg Claytond20deac2014-04-04 18:15:18 +00006195 *unique_ast_entry_ap);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006196
Greg Clayton0ec71a02013-08-10 00:09:35 +00006197 if (is_forward_declaration && die->HasChildren())
6198 {
6199 // Check to see if the DIE actually has a definition, some version of GCC will
6200 // emit DIEs with DW_AT_declaration set to true, but yet still have subprogram,
6201 // members, or inheritance, so we can't trust it
6202 const DWARFDebugInfoEntry *child_die = die->GetFirstChild();
6203 while (child_die)
6204 {
6205 switch (child_die->Tag())
6206 {
6207 case DW_TAG_inheritance:
6208 case DW_TAG_subprogram:
6209 case DW_TAG_member:
6210 case DW_TAG_APPLE_property:
Greg Clayton4c484ec2014-02-07 19:21:56 +00006211 case DW_TAG_class_type:
6212 case DW_TAG_structure_type:
6213 case DW_TAG_enumeration_type:
6214 case DW_TAG_typedef:
6215 case DW_TAG_union_type:
Greg Clayton0ec71a02013-08-10 00:09:35 +00006216 child_die = NULL;
6217 is_forward_declaration = false;
6218 break;
6219 default:
6220 child_die = child_die->GetSibling();
6221 break;
6222 }
6223 }
6224 }
6225
Sean Callanan12014a02011-12-08 23:45:45 +00006226 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00006227 {
6228 // Always start the definition for a class type so that
6229 // if the class has child classes or types that require
6230 // the class to be created for use as their decl contexts
6231 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00006232 if (die->HasChildren() == false)
6233 {
6234 // No children for this struct/union/class, lets finish it
Greg Clayton57ee3062013-07-11 22:46:58 +00006235 clang_type.StartTagDeclarationDefinition ();
6236 clang_type.CompleteTagDeclarationDefinition ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006237
Sean Callanan433cd222012-10-19 01:37:25 +00006238 if (tag == DW_TAG_structure_type) // this only applies in C
6239 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006240 clang::RecordDecl *record_decl = clang_type.GetAsRecordDecl();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006241
Greg Clayton57ee3062013-07-11 22:46:58 +00006242 if (record_decl)
Greg Claytond20deac2014-04-04 18:15:18 +00006243 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, LayoutInfo()));
Sean Callanan433cd222012-10-19 01:37:25 +00006244 }
Sean Callanan12014a02011-12-08 23:45:45 +00006245 }
6246 else if (clang_type_was_created)
6247 {
Greg Clayton219cf312012-03-30 00:51:13 +00006248 // Start the definition if the class is not objective C since
6249 // the underlying decls respond to isCompleteDefinition(). Objective
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00006250 // C decls don't respond to isCompleteDefinition() so we can't
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00006251 // start the declaration definition right away. For C++ class/union/structs
Greg Clayton219cf312012-03-30 00:51:13 +00006252 // we want to start the definition in case the class is needed as the
6253 // declaration context for a contained class or type without the need
6254 // to complete that type..
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006255
Sean Callanan7457f8d2012-04-25 01:03:57 +00006256 if (class_language != eLanguageTypeObjC &&
6257 class_language != eLanguageTypeObjC_plus_plus)
Greg Clayton57ee3062013-07-11 22:46:58 +00006258 clang_type.StartTagDeclarationDefinition ();
Greg Clayton219cf312012-03-30 00:51:13 +00006259
Sean Callanan12014a02011-12-08 23:45:45 +00006260 // Leave this as a forward declaration until we need
6261 // to know the details of the type. lldb_private::Type
6262 // will automatically call the SymbolFile virtual function
6263 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
6264 // When the definition needs to be defined.
Greg Clayton57ee3062013-07-11 22:46:58 +00006265 m_forward_decl_die_to_clang_type[die] = clang_type.GetOpaqueQualType();
6266 m_forward_decl_clang_type_to_die[clang_type.RemoveFastQualifiers().GetOpaqueQualType()] = die;
6267 clang_type.SetHasExternalStorage (true);
Sean Callanan12014a02011-12-08 23:45:45 +00006268 }
Greg Claytonc615ce42010-11-09 04:42:43 +00006269 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006270
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006271 }
6272 break;
6273
6274 case DW_TAG_enumeration_type:
6275 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006276 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006277 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006278
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006279 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006280
Greg Claytond88d7592010-09-15 08:33:30 +00006281 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006282 if (num_attributes > 0)
6283 {
6284 uint32_t i;
6285
6286 for (i=0; i<num_attributes; ++i)
6287 {
6288 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006289 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6290 {
6291 switch (attr)
6292 {
Greg Clayton7a345282010-11-09 23:46:37 +00006293 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6294 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6295 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006296 case DW_AT_name:
6297 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006298 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006299 break;
Greg Clayton54166af2014-11-22 01:58:59 +00006300 case DW_AT_type: encoding_uid = form_value.Reference(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006301 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
6302 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006303 case DW_AT_declaration: break; //is_forward_declaration = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006304 case DW_AT_allocated:
6305 case DW_AT_associated:
6306 case DW_AT_bit_stride:
6307 case DW_AT_byte_stride:
6308 case DW_AT_data_location:
6309 case DW_AT_description:
6310 case DW_AT_start_scope:
6311 case DW_AT_visibility:
6312 case DW_AT_specification:
6313 case DW_AT_abstract_origin:
6314 case DW_AT_sibling:
6315 break;
6316 }
6317 }
6318 }
6319
Daniel Malead01b2952012-11-29 21:49:15 +00006320 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 +00006321
Greg Clayton57ee3062013-07-11 22:46:58 +00006322 ClangASTType enumerator_clang_type;
6323 clang_type.SetClangType (ast.getASTContext(), m_forward_decl_die_to_clang_type.lookup (die));
6324 if (!clang_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00006325 {
Greg Clayton5d14fc02013-03-06 06:23:54 +00006326 if (encoding_uid != DW_INVALID_OFFSET)
6327 {
6328 Type *enumerator_type = ResolveTypeUID(encoding_uid);
6329 if (enumerator_type)
6330 enumerator_clang_type = enumerator_type->GetClangFullType();
6331 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006332
Greg Clayton57ee3062013-07-11 22:46:58 +00006333 if (!enumerator_clang_type)
Greg Clayton5d14fc02013-03-06 06:23:54 +00006334 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
6335 DW_ATE_signed,
6336 byte_size * 8);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006337
Greg Claytonca512b32011-01-14 04:54:56 +00006338 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00006339 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00006340 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006341 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00006342 }
6343 else
6344 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006345 enumerator_clang_type = clang_type.GetEnumerationIntegerType ();
Greg Clayton1be10fc2010-09-29 01:12:09 +00006346 }
6347
Greg Clayton57ee3062013-07-11 22:46:58 +00006348 LinkDeclContextToDIE(clang_type.GetDeclContextForType(), die);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006349
Greg Clayton81c22f62011-10-19 18:09:39 +00006350 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006351 this,
6352 type_name_const_str,
6353 byte_size,
6354 NULL,
6355 encoding_uid,
6356 Type::eEncodingIsUID,
6357 &decl,
6358 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006359 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006360
Greg Clayton57ee3062013-07-11 22:46:58 +00006361 clang_type.StartTagDeclarationDefinition ();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006362 if (die->HasChildren())
6363 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00006364 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Greg Clayton3e067532013-03-05 23:54:39 +00006365 bool is_signed = false;
Greg Clayton57ee3062013-07-11 22:46:58 +00006366 enumerator_clang_type.IsIntegerType(is_signed);
Greg Clayton3e067532013-03-05 23:54:39 +00006367 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006368 }
Greg Clayton57ee3062013-07-11 22:46:58 +00006369 clang_type.CompleteTagDeclarationDefinition ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006370 }
6371 }
6372 break;
6373
Jim Inghamb0be4422010-08-12 01:20:14 +00006374 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006375 case DW_TAG_subprogram:
6376 case DW_TAG_subroutine_type:
6377 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006378 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006379 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006380
Greg Clayton23f59502012-07-17 03:23:13 +00006381 //const char *mangled = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006382 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00006383 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006384 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00006385 bool is_static = false;
6386 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00006387 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00006388 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00006389 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
6390 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Jim Ingham379397632012-10-27 02:54:13 +00006391 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00006392
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006393 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00006394 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006395
6396
Greg Claytond88d7592010-09-15 08:33:30 +00006397 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006398 if (num_attributes > 0)
6399 {
6400 uint32_t i;
6401 for (i=0; i<num_attributes; ++i)
6402 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00006403 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006404 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6405 {
6406 switch (attr)
6407 {
6408 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6409 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6410 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6411 case DW_AT_name:
6412 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006413 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006414 break;
6415
Greg Clayton71415542012-12-08 00:24:40 +00006416 case DW_AT_linkage_name:
Greg Clayton23f59502012-07-17 03:23:13 +00006417 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00006418 case DW_AT_type: type_die_offset = form_value.Reference(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00006419 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006420 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
6421 case DW_AT_inline: is_inline = form_value.Boolean(); break;
6422 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break;
6423 case DW_AT_explicit: is_explicit = form_value.Boolean(); break;
6424 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006425
Greg Claytonf51de672010-10-01 02:31:07 +00006426
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006427 case DW_AT_external:
6428 if (form_value.Unsigned())
6429 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00006430 if (storage == clang::SC_None)
6431 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006432 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00006433 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006434 }
6435 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006436
Greg Clayton72da3972011-08-16 18:40:23 +00006437 case DW_AT_specification:
Greg Clayton54166af2014-11-22 01:58:59 +00006438 specification_die_offset = form_value.Reference();
Greg Clayton72da3972011-08-16 18:40:23 +00006439 break;
6440
6441 case DW_AT_abstract_origin:
Greg Clayton54166af2014-11-22 01:58:59 +00006442 abstract_origin_die_offset = form_value.Reference();
Greg Clayton72da3972011-08-16 18:40:23 +00006443 break;
6444
Jim Ingham379397632012-10-27 02:54:13 +00006445 case DW_AT_object_pointer:
Greg Clayton54166af2014-11-22 01:58:59 +00006446 object_pointer_die_offset = form_value.Reference();
Jim Ingham379397632012-10-27 02:54:13 +00006447 break;
6448
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006449 case DW_AT_allocated:
6450 case DW_AT_associated:
6451 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006452 case DW_AT_calling_convention:
6453 case DW_AT_data_location:
6454 case DW_AT_elemental:
6455 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006456 case DW_AT_frame_base:
6457 case DW_AT_high_pc:
6458 case DW_AT_low_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006459 case DW_AT_prototyped:
6460 case DW_AT_pure:
6461 case DW_AT_ranges:
6462 case DW_AT_recursive:
6463 case DW_AT_return_addr:
6464 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006465 case DW_AT_start_scope:
6466 case DW_AT_static_link:
6467 case DW_AT_trampoline:
6468 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006469 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006470 case DW_AT_description:
6471 case DW_AT_sibling:
6472 break;
6473 }
6474 }
6475 }
Greg Clayton24739922010-10-13 03:15:28 +00006476 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006477
Jim Ingham379397632012-10-27 02:54:13 +00006478 std::string object_pointer_name;
6479 if (object_pointer_die_offset != DW_INVALID_OFFSET)
6480 {
6481 // Get the name from the object pointer die
6482 StreamString s;
6483 if (DWARFDebugInfoEntry::GetName (this, dwarf_cu, object_pointer_die_offset, s))
6484 {
6485 object_pointer_name.assign(s.GetData());
6486 }
6487 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006488
Daniel Malead01b2952012-11-29 21:49:15 +00006489 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 +00006490
Greg Clayton57ee3062013-07-11 22:46:58 +00006491 ClangASTType return_clang_type;
Greg Clayton24739922010-10-13 03:15:28 +00006492 Type *func_type = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006493
Greg Clayton24739922010-10-13 03:15:28 +00006494 if (type_die_offset != DW_INVALID_OFFSET)
6495 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00006496
Greg Clayton24739922010-10-13 03:15:28 +00006497 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00006498 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00006499 else
Greg Clayton57ee3062013-07-11 22:46:58 +00006500 return_clang_type = ast.GetBasicType(eBasicTypeVoid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006501
Greg Claytonf51de672010-10-01 02:31:07 +00006502
Greg Clayton57ee3062013-07-11 22:46:58 +00006503 std::vector<ClangASTType> function_param_types;
Greg Clayton24739922010-10-13 03:15:28 +00006504 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006505
Greg Clayton24739922010-10-13 03:15:28 +00006506 // Parse the function children for the parameters
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006507
Greg Claytoncb5860a2011-10-13 23:49:28 +00006508 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
6509 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00006510 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
6511
Greg Claytonf0705c82011-10-22 03:33:13 +00006512 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00006513 // Start off static. This will be set to false in ParseChildParameters(...)
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00006514 // if we find a "this" parameters as the first parameter
Greg Clayton5113dc82011-08-12 06:47:54 +00006515 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00006516 is_static = true;
Greg Claytond20deac2014-04-04 18:15:18 +00006517
Greg Clayton24739922010-10-13 03:15:28 +00006518 if (die->HasChildren())
6519 {
Greg Clayton0fffff52010-09-24 05:15:53 +00006520 bool skip_artificial = true;
Jim Ingham379397632012-10-27 02:54:13 +00006521 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00006522 containing_decl_ctx,
Jim Ingham379397632012-10-27 02:54:13 +00006523 dwarf_cu,
6524 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00006525 skip_artificial,
6526 is_static,
Greg Clayton03969752014-02-24 18:53:11 +00006527 is_variadic,
Jim Ingham379397632012-10-27 02:54:13 +00006528 type_list,
6529 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00006530 function_param_decls,
Greg Claytond20deac2014-04-04 18:15:18 +00006531 type_quals);
Greg Clayton24739922010-10-13 03:15:28 +00006532 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006533
Greg Clayton24739922010-10-13 03:15:28 +00006534 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006535 clang_type = ast.CreateFunctionType (return_clang_type,
Greg Clayton9cb25c42012-10-30 17:02:18 +00006536 function_param_types.data(),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006537 function_param_types.size(),
6538 is_variadic,
6539 type_quals);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006540
Sean Callanande9ce872013-05-09 23:13:13 +00006541 bool ignore_containing_context = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006542
Greg Clayton24739922010-10-13 03:15:28 +00006543 if (type_name_cstr)
6544 {
6545 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00006546 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006547 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00006548 ObjCLanguageRuntime::MethodName objc_method (type_name_cstr, true);
6549 if (objc_method.IsValid(true))
Greg Clayton0fffff52010-09-24 05:15:53 +00006550 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006551 ClangASTType class_opaque_type;
Greg Clayton1b3815c2013-01-30 00:18:29 +00006552 ConstString class_name(objc_method.GetClassName());
Greg Clayton7c810422012-01-18 23:40:49 +00006553 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00006554 {
Greg Clayton278a16b2012-01-19 00:52:59 +00006555 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00006556
6557 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00006558 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006559 ClangASTType type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
6560 if (type_clang_forward_type.IsObjCObjectOrInterfaceType ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00006561 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00006562 }
Greg Clayton24739922010-10-13 03:15:28 +00006563 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006564
Greg Clayton24739922010-10-13 03:15:28 +00006565 if (class_opaque_type)
6566 {
6567 // If accessibility isn't set to anything valid, assume public for
6568 // now...
6569 if (accessibility == eAccessNone)
6570 accessibility = eAccessPublic;
6571
Greg Clayton57ee3062013-07-11 22:46:58 +00006572 clang::ObjCMethodDecl *objc_method_decl = class_opaque_type.AddMethodToObjCObjectType (type_name_cstr,
6573 clang_type,
6574 accessibility,
6575 is_artificial);
Greg Clayton24739922010-10-13 03:15:28 +00006576 type_handled = objc_method_decl != NULL;
Sean Callanan464a5b52012-10-04 22:06:29 +00006577 if (type_handled)
6578 {
6579 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Claytond0029442013-03-27 01:48:02 +00006580 GetClangASTContext().SetMetadataAsUserID (objc_method_decl, MakeUserID(die->GetOffset()));
Sean Callanan464a5b52012-10-04 22:06:29 +00006581 }
Sean Callananc3a1d562013-02-06 23:21:59 +00006582 else
6583 {
Jason Molendac1a65832013-03-07 22:44:42 +00006584 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 +00006585 die->GetOffset(),
6586 tag,
6587 DW_TAG_value_to_name(tag));
6588 }
Greg Clayton24739922010-10-13 03:15:28 +00006589 }
6590 }
Greg Clayton5113dc82011-08-12 06:47:54 +00006591 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00006592 {
6593 // Look at the parent of this DIE and see if is is
6594 // a class or struct and see if this is actually a
6595 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00006596 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00006597 if (class_type)
6598 {
Greg Clayton4116e932012-05-15 02:33:01 +00006599 if (class_type->GetID() != MakeUserID(decl_ctx_die->GetOffset()))
6600 {
6601 // We uniqued the parent class of this function to another class
6602 // so we now need to associate all dies under "decl_ctx_die" to
6603 // DIEs in the DIE for "class_type"...
Greg Clayton5d650c6a2013-02-26 01:31:37 +00006604 SymbolFileDWARF *class_symfile = NULL;
Greg Clayton4116e932012-05-15 02:33:01 +00006605 DWARFCompileUnitSP class_type_cu_sp;
Greg Clayton5d650c6a2013-02-26 01:31:37 +00006606 const DWARFDebugInfoEntry *class_type_die = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006607
Sean Callanan2367f8a2013-02-26 01:12:25 +00006608 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
6609 if (debug_map_symfile)
6610 {
6611 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID()));
6612 class_type_die = class_symfile->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6613 }
6614 else
6615 {
6616 class_symfile = this;
6617 class_type_die = DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6618 }
Greg Clayton4116e932012-05-15 02:33:01 +00006619 if (class_type_die)
6620 {
Greg Claytond20deac2014-04-04 18:15:18 +00006621 DWARFDIECollection failures;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006622
Sean Callanan2367f8a2013-02-26 01:12:25 +00006623 CopyUniqueClassMethodTypes (class_symfile,
6624 class_type,
6625 class_type_cu_sp.get(),
6626 class_type_die,
6627 dwarf_cu,
6628 decl_ctx_die,
6629 failures);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006630
Sean Callanan2367f8a2013-02-26 01:12:25 +00006631 // FIXME do something with these failures that's smarter than
6632 // just dropping them on the ground. Unfortunately classes don't
6633 // like having stuff added to them after their definitions are
6634 // complete...
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006635
Sean Callanan2367f8a2013-02-26 01:12:25 +00006636 type_ptr = m_die_to_type[die];
6637 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton4116e932012-05-15 02:33:01 +00006638 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00006639 type_sp = type_ptr->shared_from_this();
6640 break;
Greg Clayton4116e932012-05-15 02:33:01 +00006641 }
6642 }
6643 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006644
Greg Clayton72da3972011-08-16 18:40:23 +00006645 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00006646 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006647 // We have a specification which we are going to base our function
6648 // prototype off of, so we need this type to be completed so that the
6649 // m_die_to_decl_ctx for the method in the specification has a valid
6650 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006651 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00006652 // If we have a specification, then the function type should have been
6653 // made with the specification and not with this die.
6654 DWARFCompileUnitSP spec_cu_sp;
6655 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006656 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006657 if (spec_clang_decl_ctx)
6658 {
6659 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
6660 }
6661 else
Jim Inghamc1663042011-09-29 22:12:35 +00006662 {
Daniel Malead01b2952012-11-29 21:49:15 +00006663 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x) has no decl\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00006664 MakeUserID(die->GetOffset()),
6665 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006666 }
Greg Clayton72da3972011-08-16 18:40:23 +00006667 type_handled = true;
6668 }
6669 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
6670 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006671 // We have a specification which we are going to base our function
6672 // prototype off of, so we need this type to be completed so that the
6673 // m_die_to_decl_ctx for the method in the abstract origin has a valid
6674 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006675 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00006676
Greg Clayton72da3972011-08-16 18:40:23 +00006677 DWARFCompileUnitSP abs_cu_sp;
6678 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006679 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006680 if (abs_clang_decl_ctx)
6681 {
6682 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
6683 }
6684 else
Jim Inghamc1663042011-09-29 22:12:35 +00006685 {
Daniel Malead01b2952012-11-29 21:49:15 +00006686 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 +00006687 MakeUserID(die->GetOffset()),
6688 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006689 }
Greg Clayton72da3972011-08-16 18:40:23 +00006690 type_handled = true;
6691 }
6692 else
6693 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006694 ClangASTType class_opaque_type = class_type->GetClangForwardType();
6695 if (class_opaque_type.IsCXXClassType ())
Greg Clayton931180e2011-01-27 06:44:37 +00006696 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006697 if (class_opaque_type.IsBeingDefined ())
Greg Clayton72da3972011-08-16 18:40:23 +00006698 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006699 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
6700 // in the DWARF for C++ methods... Default to public for now...
6701 if (accessibility == eAccessNone)
6702 accessibility = eAccessPublic;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006703
Greg Clayton20568dd2011-10-13 23:13:20 +00006704 if (!is_static && !die->HasChildren())
6705 {
6706 // We have a C++ member function with no children (this pointer!)
6707 // and clang will get mad if we try and make a function that isn't
6708 // well formed in the DWARF, so we will just skip it...
6709 type_handled = true;
6710 }
6711 else
6712 {
6713 clang::CXXMethodDecl *cxx_method_decl;
6714 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00006715 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 +00006716 type_name_cstr,
6717 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00006718 MakeUserID(die->GetOffset()),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00006719 m_obj_file->GetFileSpec().GetPath().c_str());
Greg Clayton20568dd2011-10-13 23:13:20 +00006720
Sean Callananc1b732d2011-11-01 18:07:13 +00006721 const bool is_attr_used = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006722
Greg Clayton57ee3062013-07-11 22:46:58 +00006723 cxx_method_decl = class_opaque_type.AddMethodToCXXRecordType (type_name_cstr,
6724 clang_type,
6725 accessibility,
6726 is_virtual,
6727 is_static,
6728 is_inline,
6729 is_explicit,
6730 is_attr_used,
6731 is_artificial);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006732
Greg Clayton20568dd2011-10-13 23:13:20 +00006733 type_handled = cxx_method_decl != NULL;
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006734
6735 if (type_handled)
Jim Ingham379397632012-10-27 02:54:13 +00006736 {
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006737 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
6738
6739 Host::SetCrashDescription (NULL);
6740
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006741
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006742 ClangASTMetadata metadata;
6743 metadata.SetUserID(MakeUserID(die->GetOffset()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006744
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006745 if (!object_pointer_name.empty())
6746 {
6747 metadata.SetObjectPtrName(object_pointer_name.c_str());
6748 if (log)
Greg Claytond0029442013-03-27 01:48:02 +00006749 log->Printf ("Setting object pointer name: %s on method object %p.\n",
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006750 object_pointer_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006751 static_cast<void*>(cxx_method_decl));
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006752 }
Greg Claytond0029442013-03-27 01:48:02 +00006753 GetClangASTContext().SetMetadata (cxx_method_decl, metadata);
Jim Ingham379397632012-10-27 02:54:13 +00006754 }
Sean Callananb0640dc2013-04-09 23:22:08 +00006755 else
6756 {
Sean Callanande9ce872013-05-09 23:13:13 +00006757 ignore_containing_context = true;
Sean Callananb0640dc2013-04-09 23:22:08 +00006758 }
Greg Clayton20568dd2011-10-13 23:13:20 +00006759 }
Greg Clayton72da3972011-08-16 18:40:23 +00006760 }
6761 else
6762 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006763 // We were asked to parse the type for a method in a class, yet the
6764 // class hasn't been asked to complete itself through the
6765 // clang::ExternalASTSource protocol, so we need to just have the
6766 // class complete itself and do things the right way, then our
6767 // DIE should then have an entry in the m_die_to_type map. First
6768 // we need to modify the m_die_to_type so it doesn't think we are
6769 // trying to parse this DIE anymore...
6770 m_die_to_type[die] = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006771
Greg Clayton20568dd2011-10-13 23:13:20 +00006772 // Now we get the full type to force our class type to complete itself
6773 // using the clang::ExternalASTSource protocol which will parse all
6774 // base classes and all methods (including the method for this DIE).
6775 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006776
Greg Clayton20568dd2011-10-13 23:13:20 +00006777 // The type for this DIE should have been filled in the function call above
6778 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00006779 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton20568dd2011-10-13 23:13:20 +00006780 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006781 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00006782 break;
6783 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006784
Sean Callanan02eee4d2012-04-12 23:10:00 +00006785 // FIXME This is fixing some even uglier behavior but we really need to
6786 // uniq the methods of each class as well as the class itself.
6787 // <rdar://problem/11240464>
6788 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00006789 }
Greg Clayton931180e2011-01-27 06:44:37 +00006790 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006791 }
6792 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006793 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006794 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006795
Greg Clayton24739922010-10-13 03:15:28 +00006796 if (!type_handled)
6797 {
6798 // We just have a function that isn't part of a class
Sean Callanande9ce872013-05-09 23:13:13 +00006799 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (ignore_containing_context ? GetClangASTContext().GetTranslationUnitDecl() : containing_decl_ctx,
Greg Clayton147e1fa2011-10-14 22:47:18 +00006800 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006801 clang_type,
6802 storage,
6803 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006804
6805// if (template_param_infos.GetSize() > 0)
6806// {
6807// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
6808// function_decl,
6809// type_name_cstr,
6810// template_param_infos);
6811//
6812// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
6813// func_template_decl,
6814// template_param_infos);
6815// }
Greg Clayton24739922010-10-13 03:15:28 +00006816 // Add the decl to our DIE to decl context map
6817 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00006818 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00006819 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006820 ast.SetFunctionParameters (function_decl,
6821 &function_param_decls.front(),
6822 function_param_decls.size());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006823
Jim Ingham379397632012-10-27 02:54:13 +00006824 ClangASTMetadata metadata;
6825 metadata.SetUserID(MakeUserID(die->GetOffset()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006826
Jim Ingham379397632012-10-27 02:54:13 +00006827 if (!object_pointer_name.empty())
6828 {
6829 metadata.SetObjectPtrName(object_pointer_name.c_str());
Jim Ingham2cffda3f2012-10-30 23:34:07 +00006830 if (log)
Greg Claytond0029442013-03-27 01:48:02 +00006831 log->Printf ("Setting object pointer name: %s on function object %p.",
Jim Ingham2cffda3f2012-10-30 23:34:07 +00006832 object_pointer_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006833 static_cast<void*>(function_decl));
Jim Ingham379397632012-10-27 02:54:13 +00006834 }
Greg Claytond0029442013-03-27 01:48:02 +00006835 GetClangASTContext().SetMetadata (function_decl, metadata);
Greg Clayton24739922010-10-13 03:15:28 +00006836 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006837 }
Greg Clayton81c22f62011-10-19 18:09:39 +00006838 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006839 this,
6840 type_name_const_str,
6841 0,
6842 NULL,
6843 LLDB_INVALID_UID,
6844 Type::eEncodingIsUID,
6845 &decl,
6846 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006847 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00006848 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006849 }
6850 break;
6851
6852 case DW_TAG_array_type:
6853 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006854 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006855 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006856
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006857 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006858 int64_t first_index = 0;
6859 uint32_t byte_stride = 0;
6860 uint32_t bit_stride = 0;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006861 bool is_vector = false;
Greg Claytond88d7592010-09-15 08:33:30 +00006862 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006863
6864 if (num_attributes > 0)
6865 {
6866 uint32_t i;
6867 for (i=0; i<num_attributes; ++i)
6868 {
6869 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006870 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6871 {
6872 switch (attr)
6873 {
6874 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6875 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6876 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6877 case DW_AT_name:
6878 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006879 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006880 break;
6881
Greg Clayton54166af2014-11-22 01:58:59 +00006882 case DW_AT_type: type_die_offset = form_value.Reference(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006883 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006884 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
6885 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006886 case DW_AT_GNU_vector: is_vector = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006887 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006888 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006889 case DW_AT_allocated:
6890 case DW_AT_associated:
6891 case DW_AT_data_location:
6892 case DW_AT_description:
6893 case DW_AT_ordering:
6894 case DW_AT_start_scope:
6895 case DW_AT_visibility:
6896 case DW_AT_specification:
6897 case DW_AT_abstract_origin:
6898 case DW_AT_sibling:
6899 break;
6900 }
6901 }
6902 }
6903
Daniel Malead01b2952012-11-29 21:49:15 +00006904 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 +00006905
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006906 Type *element_type = ResolveTypeUID(type_die_offset);
6907
6908 if (element_type)
6909 {
6910 std::vector<uint64_t> element_orders;
6911 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
6912 if (byte_stride == 0 && bit_stride == 0)
6913 byte_stride = element_type->GetByteSize();
Greg Clayton57ee3062013-07-11 22:46:58 +00006914 ClangASTType array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006915 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
Siva Chandra89ce9552015-01-05 23:06:14 +00006916 if (element_orders.size() > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006917 {
Siva Chandra89ce9552015-01-05 23:06:14 +00006918 uint64_t num_elements = 0;
6919 std::vector<uint64_t>::const_reverse_iterator pos;
6920 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
6921 for (pos = element_orders.rbegin(); pos != end; ++pos)
6922 {
6923 num_elements = *pos;
6924 clang_type = ast.CreateArrayType (array_element_type,
6925 num_elements,
6926 is_vector);
6927 array_element_type = clang_type;
6928 array_element_bit_stride = num_elements ?
6929 array_element_bit_stride * num_elements :
6930 array_element_bit_stride;
6931 }
6932 }
6933 else
6934 {
6935 clang_type = ast.CreateArrayType (array_element_type, 0, is_vector);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006936 }
6937 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00006938 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006939 this,
6940 empty_name,
6941 array_element_bit_stride / 8,
6942 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00006943 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006944 Type::eEncodingIsUID,
6945 &decl,
6946 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006947 Type::eResolveStateFull));
6948 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006949 }
6950 }
6951 }
6952 break;
6953
Greg Clayton9b81a312010-06-12 01:20:30 +00006954 case DW_TAG_ptr_to_member_type:
6955 {
6956 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
6957 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
6958
Greg Claytond88d7592010-09-15 08:33:30 +00006959 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006960
Greg Clayton9b81a312010-06-12 01:20:30 +00006961 if (num_attributes > 0) {
6962 uint32_t i;
6963 for (i=0; i<num_attributes; ++i)
6964 {
6965 attr = attributes.AttributeAtIndex(i);
Greg Clayton9b81a312010-06-12 01:20:30 +00006966 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6967 {
6968 switch (attr)
6969 {
6970 case DW_AT_type:
Greg Clayton54166af2014-11-22 01:58:59 +00006971 type_die_offset = form_value.Reference(); break;
Greg Clayton9b81a312010-06-12 01:20:30 +00006972 case DW_AT_containing_type:
Greg Clayton54166af2014-11-22 01:58:59 +00006973 containing_type_die_offset = form_value.Reference(); break;
Greg Clayton9b81a312010-06-12 01:20:30 +00006974 }
6975 }
6976 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006977
Greg Clayton9b81a312010-06-12 01:20:30 +00006978 Type *pointee_type = ResolveTypeUID(type_die_offset);
6979 Type *class_type = ResolveTypeUID(containing_type_die_offset);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006980
Greg Clayton57ee3062013-07-11 22:46:58 +00006981 ClangASTType pointee_clang_type = pointee_type->GetClangForwardType();
6982 ClangASTType class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00006983
Greg Clayton57ee3062013-07-11 22:46:58 +00006984 clang_type = pointee_clang_type.CreateMemberPointerType(class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00006985
Greg Clayton57ee3062013-07-11 22:46:58 +00006986 byte_size = clang_type.GetByteSize();
Greg Clayton9b81a312010-06-12 01:20:30 +00006987
Greg Clayton81c22f62011-10-19 18:09:39 +00006988 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006989 this,
6990 type_name_const_str,
6991 byte_size,
6992 NULL,
6993 LLDB_INVALID_UID,
6994 Type::eEncodingIsUID,
6995 NULL,
6996 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006997 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00006998 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006999
Greg Clayton9b81a312010-06-12 01:20:30 +00007000 break;
7001 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007002 default:
Greg Clayton84afacd2012-11-07 23:09:32 +00007003 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",
7004 die->GetOffset(),
7005 tag,
7006 DW_TAG_value_to_name(tag));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007007 break;
7008 }
7009
7010 if (type_sp.get())
7011 {
7012 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
7013 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
7014
7015 SymbolContextScope * symbol_context_scope = NULL;
7016 if (sc_parent_tag == DW_TAG_compile_unit)
7017 {
7018 symbol_context_scope = sc.comp_unit;
7019 }
Jason Molenda60db6e42014-10-16 01:40:16 +00007020 else if (sc.function != NULL && sc_parent_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007021 {
Greg Clayton81c22f62011-10-19 18:09:39 +00007022 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007023 if (symbol_context_scope == NULL)
7024 symbol_context_scope = sc.function;
7025 }
7026
7027 if (symbol_context_scope != NULL)
7028 {
7029 type_sp->SetSymbolContextScope(symbol_context_scope);
7030 }
7031
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00007032 // We are ready to put this type into the uniqued list up at the module level
7033 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00007034
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00007035 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007036 }
7037 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00007038 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007039 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00007040 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007041 }
7042 }
7043 return type_sp;
7044}
7045
7046size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00007047SymbolFileDWARF::ParseTypes
7048(
7049 const SymbolContext& sc,
7050 DWARFCompileUnit* dwarf_cu,
7051 const DWARFDebugInfoEntry *die,
7052 bool parse_siblings,
7053 bool parse_children
7054)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007055{
7056 size_t types_added = 0;
7057 while (die != NULL)
7058 {
7059 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00007060 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007061 {
7062 if (type_is_new)
7063 ++types_added;
7064 }
7065
7066 if (parse_children && die->HasChildren())
7067 {
7068 if (die->Tag() == DW_TAG_subprogram)
7069 {
7070 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00007071 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007072 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
7073 }
7074 else
7075 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
7076 }
7077
7078 if (parse_siblings)
7079 die = die->GetSibling();
7080 else
7081 die = NULL;
7082 }
7083 return types_added;
7084}
7085
7086
7087size_t
7088SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
7089{
7090 assert(sc.comp_unit && sc.function);
7091 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00007092 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007093 if (dwarf_cu)
7094 {
7095 dw_offset_t function_die_offset = sc.function->GetID();
7096 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
7097 if (function_die)
7098 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00007099 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007100 }
7101 }
7102
7103 return functions_added;
7104}
7105
7106
7107size_t
7108SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
7109{
7110 // At least a compile unit must be valid
7111 assert(sc.comp_unit);
7112 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00007113 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007114 if (dwarf_cu)
7115 {
7116 if (sc.function)
7117 {
7118 dw_offset_t function_die_offset = sc.function->GetID();
7119 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
7120 if (func_die && func_die->HasChildren())
7121 {
7122 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
7123 }
7124 }
7125 else
7126 {
7127 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
7128 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
7129 {
7130 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
7131 }
7132 }
7133 }
7134
7135 return types_added;
7136}
7137
7138size_t
7139SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
7140{
7141 if (sc.comp_unit != NULL)
7142 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00007143 DWARFDebugInfo* info = DebugInfo();
7144 if (info == NULL)
7145 return 0;
7146
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007147 if (sc.function)
7148 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007149 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitContainingDIE(sc.function->GetID()).get();
7150
7151 if (dwarf_cu == NULL)
7152 return 0;
7153
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007154 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00007155
Greg Claytonc7bece562013-01-25 18:06:21 +00007156 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
7157 if (func_lo_pc != LLDB_INVALID_ADDRESS)
Greg Claytone38a5ed2012-01-05 03:57:59 +00007158 {
7159 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00007160
Greg Claytone38a5ed2012-01-05 03:57:59 +00007161 // Let all blocks know they have parse all their variables
7162 sc.function->GetBlock (false).SetDidParseVariables (true, true);
7163 return num_variables;
7164 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007165 }
7166 else if (sc.comp_unit)
7167 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007168 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID()).get();
7169
7170 if (dwarf_cu == NULL)
7171 return 0;
7172
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007173 uint32_t vars_added = 0;
7174 VariableListSP variables (sc.comp_unit->GetVariableList(false));
7175
7176 if (variables.get() == NULL)
7177 {
7178 variables.reset(new VariableList());
7179 sc.comp_unit->SetVariableList(variables);
7180
Greg Claytond4a2b372011-09-12 23:21:58 +00007181 DWARFCompileUnit* match_dwarf_cu = NULL;
7182 const DWARFDebugInfoEntry* die = NULL;
7183 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00007184 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00007185 {
Greg Clayton97fbc342011-10-20 22:30:33 +00007186 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00007187 {
7188 DWARFMappedHash::DIEInfoArray hash_data_array;
7189 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
7190 dwarf_cu->GetNextCompileUnitOffset(),
7191 hash_data_array))
7192 {
7193 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
7194 }
7195 }
Greg Clayton7f995132011-10-04 22:41:51 +00007196 }
7197 else
7198 {
7199 // Index if we already haven't to make sure the compile units
7200 // get indexed and make their global DIE index list
7201 if (!m_indexed)
7202 Index ();
7203
7204 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
7205 dwarf_cu->GetNextCompileUnitOffset(),
7206 die_offsets);
7207 }
7208
7209 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00007210 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007211 {
Greg Claytond4a2b372011-09-12 23:21:58 +00007212 DWARFDebugInfo* debug_info = DebugInfo();
7213 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007214 {
Greg Claytond4a2b372011-09-12 23:21:58 +00007215 const dw_offset_t die_offset = die_offsets[i];
7216 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00007217 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00007218 {
Greg Clayton95d87902011-11-11 03:16:25 +00007219 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
7220 if (var_sp)
7221 {
7222 variables->AddVariableIfUnique (var_sp);
7223 ++vars_added;
7224 }
Greg Claytond4a2b372011-09-12 23:21:58 +00007225 }
Greg Clayton95d87902011-11-11 03:16:25 +00007226 else
7227 {
7228 if (m_using_apple_tables)
7229 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00007230 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 +00007231 }
7232 }
7233
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007234 }
7235 }
7236 }
7237 return vars_added;
7238 }
7239 }
7240 return 0;
7241}
7242
7243
7244VariableSP
7245SymbolFileDWARF::ParseVariableDIE
7246(
7247 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00007248 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00007249 const DWARFDebugInfoEntry *die,
7250 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007251)
7252{
Greg Clayton83c5cd92010-11-14 22:13:40 +00007253 VariableSP var_sp (m_die_to_variable_sp[die]);
7254 if (var_sp)
7255 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007256
7257 const dw_tag_t tag = die->Tag();
Richard Mitton0a558352013-10-17 21:14:00 +00007258 ModuleSP module = GetObjectFile()->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00007259
7260 if ((tag == DW_TAG_variable) ||
7261 (tag == DW_TAG_constant) ||
7262 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007263 {
Greg Clayton7f995132011-10-04 22:41:51 +00007264 DWARFDebugInfoEntry::Attributes attributes;
7265 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
7266 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007267 {
Greg Clayton7f995132011-10-04 22:41:51 +00007268 const char *name = NULL;
7269 const char *mangled = NULL;
7270 Declaration decl;
7271 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00007272 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00007273 DWARFExpression location;
7274 bool is_external = false;
7275 bool is_artificial = false;
7276 bool location_is_const_value_data = false;
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007277 bool has_explicit_location = false;
Enrico Granata4ec130d2014-08-11 19:16:35 +00007278 DWARFFormValue const_value;
Greg Clayton23f59502012-07-17 03:23:13 +00007279 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00007280
7281 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007282 {
Greg Clayton7f995132011-10-04 22:41:51 +00007283 dw_attr_t attr = attributes.AttributeAtIndex(i);
7284 DWARFFormValue form_value;
Greg Clayton54166af2014-11-22 01:58:59 +00007285
Greg Clayton7f995132011-10-04 22:41:51 +00007286 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007287 {
Greg Clayton7f995132011-10-04 22:41:51 +00007288 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007289 {
Greg Clayton7f995132011-10-04 22:41:51 +00007290 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
7291 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
7292 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
7293 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton71415542012-12-08 00:24:40 +00007294 case DW_AT_linkage_name:
Greg Clayton7f995132011-10-04 22:41:51 +00007295 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00007296 case DW_AT_type: type_uid = form_value.Reference(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00007297 case DW_AT_external: is_external = form_value.Boolean(); break;
Greg Clayton7f995132011-10-04 22:41:51 +00007298 case DW_AT_const_value:
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007299 // If we have already found a DW_AT_location attribute, ignore this attribute.
7300 if (!has_explicit_location)
7301 {
7302 location_is_const_value_data = true;
7303 // The constant value will be either a block, a data value or a string.
Ed Masteeeae7212013-10-24 20:43:47 +00007304 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007305 if (DWARFFormValue::IsBlockForm(form_value.Form()))
7306 {
7307 // Retrieve the value as a block expression.
7308 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
7309 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00007310 location.CopyOpcodeData(module, debug_info_data, block_offset, block_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007311 }
7312 else if (DWARFFormValue::IsDataForm(form_value.Form()))
7313 {
7314 // Retrieve the value as a data expression.
Greg Clayton54166af2014-11-22 01:58:59 +00007315 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007316 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
7317 uint32_t data_length = fixed_form_sizes[form_value.Form()];
Enrico Granata4ec130d2014-08-11 19:16:35 +00007318 if (data_length == 0)
7319 {
7320 const uint8_t *data_pointer = form_value.BlockData();
7321 if (data_pointer)
7322 {
Jason Molenda18f5fd32014-10-16 07:52:17 +00007323 form_value.Unsigned();
Enrico Granata4ec130d2014-08-11 19:16:35 +00007324 }
7325 else if (DWARFFormValue::IsDataForm(form_value.Form()))
7326 {
7327 // we need to get the byte size of the type later after we create the variable
7328 const_value = form_value;
7329 }
7330 }
7331 else
7332 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007333 }
7334 else
7335 {
7336 // Retrieve the value as a string expression.
7337 if (form_value.Form() == DW_FORM_strp)
7338 {
Greg Clayton54166af2014-11-22 01:58:59 +00007339 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007340 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
7341 uint32_t data_length = fixed_form_sizes[form_value.Form()];
Richard Mitton0a558352013-10-17 21:14:00 +00007342 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007343 }
7344 else
7345 {
7346 const char *str = form_value.AsCString(&debug_info_data);
7347 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart();
7348 uint32_t string_length = strlen(str) + 1;
Richard Mitton0a558352013-10-17 21:14:00 +00007349 location.CopyOpcodeData(module, debug_info_data, string_offset, string_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007350 }
7351 }
7352 }
7353 break;
Greg Clayton7f995132011-10-04 22:41:51 +00007354 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007355 {
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007356 location_is_const_value_data = false;
7357 has_explicit_location = true;
Greg Clayton7f995132011-10-04 22:41:51 +00007358 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007359 {
Ed Masteeeae7212013-10-24 20:43:47 +00007360 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Clayton7f995132011-10-04 22:41:51 +00007361
7362 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
7363 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00007364 location.CopyOpcodeData(module, get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00007365 }
7366 else
7367 {
Ed Masteeeae7212013-10-24 20:43:47 +00007368 const DWARFDataExtractor& debug_loc_data = get_debug_loc_data();
Greg Clayton7f995132011-10-04 22:41:51 +00007369 const dw_offset_t debug_loc_offset = form_value.Unsigned();
7370
7371 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
7372 if (loc_list_length > 0)
7373 {
Richard Mitton0a558352013-10-17 21:14:00 +00007374 location.CopyOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00007375 assert (func_low_pc != LLDB_INVALID_ADDRESS);
Greg Clayton54166af2014-11-22 01:58:59 +00007376 location.SetLocationListSlide (func_low_pc - attributes.CompileUnitAtIndex(i)->GetBaseAddress());
Greg Clayton7f995132011-10-04 22:41:51 +00007377 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007378 }
7379 }
Greg Clayton7f995132011-10-04 22:41:51 +00007380 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007381
Greg Clayton1c8ef472013-04-05 23:27:21 +00007382 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00007383 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00007384 case DW_AT_declaration:
7385 case DW_AT_description:
7386 case DW_AT_endianity:
7387 case DW_AT_segment:
7388 case DW_AT_start_scope:
7389 case DW_AT_visibility:
7390 default:
7391 case DW_AT_abstract_origin:
7392 case DW_AT_sibling:
7393 case DW_AT_specification:
7394 break;
7395 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007396 }
7397 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007398
Greg Clayton9e9f2192013-05-17 00:55:28 +00007399 ValueType scope = eValueTypeInvalid;
7400
7401 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
7402 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
7403 SymbolContextScope * symbol_context_scope = NULL;
7404
7405 // DWARF doesn't specify if a DW_TAG_variable is a local, global
7406 // or static variable, so we have to do a little digging by
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00007407 // looking at the location of a variable to see if it contains
Greg Clayton9e9f2192013-05-17 00:55:28 +00007408 // a DW_OP_addr opcode _somewhere_ in the definition. I say
7409 // somewhere because clang likes to combine small global variables
7410 // into the same symbol and have locations like:
7411 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
7412 // So if we don't have a DW_TAG_formal_parameter, we can look at
7413 // the location to see if it contains a DW_OP_addr opcode, and
7414 // then we can correctly classify our variables.
7415 if (tag == DW_TAG_formal_parameter)
7416 scope = eValueTypeVariableArgument;
7417 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007418 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007419 bool op_error = false;
7420 // Check if the location has a DW_OP_addr with any address value...
7421 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
7422 if (!location_is_const_value_data)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007423 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007424 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error);
7425 if (op_error)
Greg Clayton96c09682012-01-04 22:56:43 +00007426 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007427 StreamString strm;
7428 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
7429 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 +00007430 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00007431 }
Greg Claytond1767f02011-12-08 02:13:16 +00007432
Greg Clayton9e9f2192013-05-17 00:55:28 +00007433 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
7434 {
7435 if (is_external)
7436 scope = eValueTypeVariableGlobal;
7437 else
7438 scope = eValueTypeVariableStatic;
7439
7440
7441 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile ();
7442
7443 if (debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007444 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007445 // When leaving the DWARF in the .o files on darwin,
7446 // when we have a global variable that wasn't initialized,
7447 // the .o file might not have allocated a virtual
7448 // address for the global variable. In this case it will
7449 // have created a symbol for the global variable
7450 // that is undefined/data and external and the value will
7451 // be the byte size of the variable. When we do the
7452 // address map in SymbolFileDWARFDebugMap we rely on
7453 // having an address, we need to do some magic here
7454 // so we can get the correct address for our global
7455 // variable. The address for all of these entries
7456 // will be zero, and there will be an undefined symbol
7457 // in this object file, and the executable will have
7458 // a matching symbol with a good address. So here we
7459 // dig up the correct address and replace it in the
7460 // location for the variable, and set the variable's
7461 // symbol context scope to be that of the main executable
7462 // so the file address will resolve correctly.
7463 bool linked_oso_file_addr = false;
7464 if (is_external && location_DW_OP_addr == 0)
Greg Clayton9422dd62013-03-04 21:46:16 +00007465 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007466 // we have a possible uninitialized extern global
7467 ConstString const_name(mangled ? mangled : name);
7468 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
7469 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007470 {
Greg Clayton3046e662013-07-10 01:23:25 +00007471 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
Greg Clayton9e9f2192013-05-17 00:55:28 +00007472 if (debug_map_symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007473 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007474 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
7475 eSymbolTypeData,
7476 Symtab::eDebugYes,
7477 Symtab::eVisibilityExtern);
7478 if (exe_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007479 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007480 if (exe_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007481 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007482 const addr_t exe_file_addr = exe_symbol->GetAddress().GetFileAddress();
7483 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007484 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007485 if (location.Update_DW_OP_addr (exe_file_addr))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007486 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007487 linked_oso_file_addr = true;
7488 symbol_context_scope = exe_symbol;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007489 }
7490 }
7491 }
7492 }
7493 }
7494 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00007495 }
Greg Clayton9422dd62013-03-04 21:46:16 +00007496
Greg Clayton9e9f2192013-05-17 00:55:28 +00007497 if (!linked_oso_file_addr)
7498 {
7499 // The DW_OP_addr is not zero, but it contains a .o file address which
7500 // needs to be linked up correctly.
7501 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
7502 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +00007503 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007504 // Update the file address for this variable
7505 location.Update_DW_OP_addr (exe_file_addr);
7506 }
7507 else
7508 {
7509 // Variable didn't make it into the final executable
7510 return var_sp;
Greg Clayton9422dd62013-03-04 21:46:16 +00007511 }
Greg Claytond1767f02011-12-08 02:13:16 +00007512 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007513 }
Greg Clayton5cf58b92011-10-05 22:22:08 +00007514 }
7515 else
7516 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007517 scope = eValueTypeVariableLocal;
Greg Clayton5cf58b92011-10-05 22:22:08 +00007518 }
Greg Clayton7f995132011-10-04 22:41:51 +00007519 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00007520
7521 if (symbol_context_scope == NULL)
7522 {
7523 switch (parent_tag)
7524 {
7525 case DW_TAG_subprogram:
7526 case DW_TAG_inlined_subroutine:
7527 case DW_TAG_lexical_block:
7528 if (sc.function)
7529 {
7530 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
7531 if (symbol_context_scope == NULL)
7532 symbol_context_scope = sc.function;
7533 }
7534 break;
7535
7536 default:
7537 symbol_context_scope = sc.comp_unit;
7538 break;
7539 }
7540 }
7541
7542 if (symbol_context_scope)
7543 {
Enrico Granata4ec130d2014-08-11 19:16:35 +00007544 SymbolFileTypeSP type_sp(new SymbolFileType(*this, type_uid));
7545
7546 if (const_value.Form() && type_sp && type_sp->GetType())
7547 location.CopyOpcodeData(const_value.Unsigned(), type_sp->GetType()->GetByteSize(), dwarf_cu->GetAddressByteSize());
7548
Greg Clayton9e9f2192013-05-17 00:55:28 +00007549 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
7550 name,
7551 mangled,
Enrico Granata4ec130d2014-08-11 19:16:35 +00007552 type_sp,
Greg Clayton9e9f2192013-05-17 00:55:28 +00007553 scope,
7554 symbol_context_scope,
7555 &decl,
7556 location,
7557 is_external,
7558 is_artificial));
7559
7560 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
7561 }
7562 else
7563 {
7564 // Not ready to parse this variable yet. It might be a global
7565 // or static variable that is in a function scope and the function
7566 // in the symbol context wasn't filled in yet
7567 return var_sp;
7568 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007569 }
Greg Clayton7f995132011-10-04 22:41:51 +00007570 // Cache var_sp even if NULL (the variable was just a specification or
7571 // was missing vital information to be able to be displayed in the debugger
7572 // (missing location due to optimization, etc)) so we don't re-parse
7573 // this DIE over and over later...
7574 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007575 }
7576 return var_sp;
7577}
7578
Greg Claytonc662ec82011-06-17 22:10:16 +00007579
7580const DWARFDebugInfoEntry *
7581SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
7582 dw_offset_t spec_block_die_offset,
7583 DWARFCompileUnit **result_die_cu_handle)
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 DWARFDebugInfo* info = DebugInfo();
7589
7590 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
7591 if (die)
7592 {
7593 assert (*result_die_cu_handle);
7594 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
7595 }
7596 return NULL;
7597}
7598
7599
7600const DWARFDebugInfoEntry *
7601SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
7602 const DWARFDebugInfoEntry *die,
7603 dw_offset_t spec_block_die_offset,
7604 DWARFCompileUnit **result_die_cu_handle)
7605{
7606 if (die)
7607 {
7608 switch (die->Tag())
7609 {
7610 case DW_TAG_subprogram:
7611 case DW_TAG_inlined_subroutine:
7612 case DW_TAG_lexical_block:
7613 {
7614 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
7615 {
7616 *result_die_cu_handle = dwarf_cu;
7617 return die;
7618 }
7619
7620 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
7621 {
7622 *result_die_cu_handle = dwarf_cu;
7623 return die;
7624 }
7625 }
7626 break;
7627 }
7628
7629 // Give the concrete function die specified by "func_die_offset", find the
7630 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
7631 // to "spec_block_die_offset"
7632 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
7633 {
7634 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
7635 child_die,
7636 spec_block_die_offset,
7637 result_die_cu_handle);
7638 if (result_die)
7639 return result_die;
7640 }
7641 }
7642
7643 *result_die_cu_handle = NULL;
7644 return NULL;
7645}
7646
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007647size_t
7648SymbolFileDWARF::ParseVariables
7649(
7650 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00007651 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00007652 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007653 const DWARFDebugInfoEntry *orig_die,
7654 bool parse_siblings,
7655 bool parse_children,
7656 VariableList* cc_variable_list
7657)
7658{
7659 if (orig_die == NULL)
7660 return 0;
7661
Greg Claytonc662ec82011-06-17 22:10:16 +00007662 VariableListSP variable_list_sp;
7663
7664 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007665 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00007666 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007667 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007668 dw_tag_t tag = die->Tag();
7669
7670 // Check to see if we have already parsed this variable or constant?
7671 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007672 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007673 if (cc_variable_list)
7674 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007675 }
7676 else
7677 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007678 // We haven't already parsed it, lets do that now.
7679 if ((tag == DW_TAG_variable) ||
7680 (tag == DW_TAG_constant) ||
7681 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007682 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007683 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00007684 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007685 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
7686 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
7687 switch (parent_tag)
7688 {
7689 case DW_TAG_compile_unit:
7690 if (sc.comp_unit != NULL)
7691 {
7692 variable_list_sp = sc.comp_unit->GetVariableList(false);
7693 if (variable_list_sp.get() == NULL)
7694 {
7695 variable_list_sp.reset(new VariableList());
7696 sc.comp_unit->SetVariableList(variable_list_sp);
7697 }
7698 }
7699 else
7700 {
Daniel Malead01b2952012-11-29 21:49:15 +00007701 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 +00007702 MakeUserID(sc_parent_die->GetOffset()),
7703 DW_TAG_value_to_name (parent_tag),
7704 MakeUserID(orig_die->GetOffset()),
7705 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007706 }
7707 break;
7708
7709 case DW_TAG_subprogram:
7710 case DW_TAG_inlined_subroutine:
7711 case DW_TAG_lexical_block:
7712 if (sc.function != NULL)
7713 {
7714 // Check to see if we already have parsed the variables for the given scope
7715
Greg Clayton81c22f62011-10-19 18:09:39 +00007716 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007717 if (block == NULL)
7718 {
7719 // This must be a specification or abstract origin with
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00007720 // a concrete block counterpart in the current function. We need
Greg Claytonc662ec82011-06-17 22:10:16 +00007721 // to find the concrete block so we can correctly add the
7722 // variable to it
7723 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
7724 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
7725 sc_parent_die->GetOffset(),
7726 &concrete_block_die_cu);
7727 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00007728 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007729 }
7730
7731 if (block != NULL)
7732 {
7733 const bool can_create = false;
7734 variable_list_sp = block->GetBlockVariableList (can_create);
7735 if (variable_list_sp.get() == NULL)
7736 {
7737 variable_list_sp.reset(new VariableList());
7738 block->SetVariableList(variable_list_sp);
7739 }
7740 }
7741 }
7742 break;
7743
7744 default:
Daniel Malead01b2952012-11-29 21:49:15 +00007745 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 +00007746 MakeUserID(orig_die->GetOffset()),
7747 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007748 break;
7749 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00007750 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007751
7752 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007753 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00007754 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
7755 if (var_sp)
7756 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007757 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00007758 if (cc_variable_list)
7759 cc_variable_list->AddVariableIfUnique (var_sp);
7760 ++vars_added;
7761 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007762 }
7763 }
7764 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007765
7766 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
7767
7768 if (!skip_children && parse_children && die->HasChildren())
7769 {
7770 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
7771 }
7772
7773 if (parse_siblings)
7774 die = die->GetSibling();
7775 else
7776 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007777 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007778 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007779}
7780
7781//------------------------------------------------------------------
7782// PluginInterface protocol
7783//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00007784ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007785SymbolFileDWARF::GetPluginName()
7786{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007787 return GetPluginNameStatic();
7788}
7789
7790uint32_t
7791SymbolFileDWARF::GetPluginVersion()
7792{
7793 return 1;
7794}
7795
7796void
Greg Clayton6beaaa62011-01-17 03:46:26 +00007797SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
7798{
7799 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
Greg Clayton57ee3062013-07-11 22:46:58 +00007800 ClangASTType clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00007801 if (clang_type)
7802 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7803}
7804
7805void
7806SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
7807{
7808 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
Greg Clayton57ee3062013-07-11 22:46:58 +00007809 ClangASTType clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00007810 if (clang_type)
7811 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7812}
7813
Greg Claytona2721472011-06-25 00:44:06 +00007814void
Sean Callanancc427fa2011-07-30 02:42:06 +00007815SymbolFileDWARF::DumpIndexes ()
7816{
7817 StreamFile s(stdout, false);
7818
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00007819 s.Printf ("DWARF index for (%s) '%s':",
Sean Callanancc427fa2011-07-30 02:42:06 +00007820 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00007821 GetObjectFile()->GetFileSpec().GetPath().c_str());
Sean Callanancc427fa2011-07-30 02:42:06 +00007822 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
7823 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
7824 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
7825 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
7826 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
7827 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
7828 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
7829 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
7830}
7831
7832void
7833SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
7834 const char *name,
7835 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00007836{
Sean Callanancc427fa2011-07-30 02:42:06 +00007837 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00007838
7839 if (iter == m_decl_ctx_to_die.end())
7840 return;
7841
Greg Claytoncb5860a2011-10-13 23:49:28 +00007842 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00007843 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007844 const DWARFDebugInfoEntry *context_die = *pos;
7845
7846 if (!results)
7847 return;
7848
7849 DWARFDebugInfo* info = DebugInfo();
7850
7851 DIEArray die_offsets;
7852
7853 DWARFCompileUnit* dwarf_cu = NULL;
7854 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00007855
7856 if (m_using_apple_tables)
7857 {
7858 if (m_apple_types_ap.get())
7859 m_apple_types_ap->FindByName (name, die_offsets);
7860 }
7861 else
7862 {
7863 if (!m_indexed)
7864 Index ();
7865
7866 m_type_index.Find (ConstString(name), die_offsets);
7867 }
7868
Greg Clayton220a0072011-12-09 08:48:30 +00007869 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007870
7871 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00007872 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007873 for (size_t i = 0; i < num_matches; ++i)
7874 {
7875 const dw_offset_t die_offset = die_offsets[i];
7876 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00007877
Greg Claytoncb5860a2011-10-13 23:49:28 +00007878 if (die->GetParent() != context_die)
7879 continue;
7880
7881 Type *matching_type = ResolveType (dwarf_cu, die);
7882
Greg Clayton57ee3062013-07-11 22:46:58 +00007883 clang::QualType qual_type = matching_type->GetClangForwardType().GetQualType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007884
7885 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
7886 {
7887 clang::TagDecl *tag_decl = tag_type->getDecl();
7888 results->push_back(tag_decl);
7889 }
7890 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
7891 {
7892 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
7893 results->push_back(typedef_decl);
7894 }
Greg Claytona2721472011-06-25 00:44:06 +00007895 }
7896 }
7897 }
7898}
7899
7900void
7901SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00007902 const clang::DeclContext *decl_context,
7903 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00007904 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
7905{
Greg Clayton85ae2e12011-10-18 23:36:41 +00007906
7907 switch (decl_context->getDeclKind())
7908 {
7909 case clang::Decl::Namespace:
7910 case clang::Decl::TranslationUnit:
7911 {
7912 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7913 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
7914 }
7915 break;
7916 default:
7917 break;
7918 }
Greg Claytona2721472011-06-25 00:44:06 +00007919}
Greg Claytoncaab74e2012-01-28 00:48:57 +00007920
7921bool
7922SymbolFileDWARF::LayoutRecordType (void *baton,
7923 const clang::RecordDecl *record_decl,
7924 uint64_t &size,
7925 uint64_t &alignment,
7926 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7927 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7928 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7929{
7930 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7931 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
7932}
7933
7934
7935bool
7936SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
7937 uint64_t &bit_size,
7938 uint64_t &alignment,
7939 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7940 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7941 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7942{
Greg Clayton5160ce52013-03-27 23:08:40 +00007943 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Claytoncaab74e2012-01-28 00:48:57 +00007944 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
7945 bool success = false;
7946 base_offsets.clear();
7947 vbase_offsets.clear();
7948 if (pos != m_record_decl_to_layout_map.end())
7949 {
7950 bit_size = pos->second.bit_size;
7951 alignment = pos->second.alignment;
7952 field_offsets.swap(pos->second.field_offsets);
Greg Clayton2508b9b2012-11-01 23:20:02 +00007953 base_offsets.swap (pos->second.base_offsets);
7954 vbase_offsets.swap (pos->second.vbase_offsets);
Greg Claytoncaab74e2012-01-28 00:48:57 +00007955 m_record_decl_to_layout_map.erase(pos);
7956 success = true;
7957 }
7958 else
7959 {
7960 bit_size = 0;
7961 alignment = 0;
7962 field_offsets.clear();
7963 }
7964
7965 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00007966 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00007967 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007968 static_cast<const void*>(record_decl),
7969 bit_size, alignment,
7970 static_cast<uint32_t>(field_offsets.size()),
7971 static_cast<uint32_t>(base_offsets.size()),
7972 static_cast<uint32_t>(vbase_offsets.size()),
Greg Claytoncaab74e2012-01-28 00:48:57 +00007973 success);
7974 return success;
7975}
7976
7977
Greg Clayton1f746072012-08-29 21:13:06 +00007978SymbolFileDWARFDebugMap *
7979SymbolFileDWARF::GetDebugMapSymfile ()
7980{
7981 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
7982 {
7983 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
7984 if (module_sp)
7985 {
7986 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
7987 if (sym_vendor)
7988 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
7989 }
7990 }
7991 return m_debug_map_symfile;
7992}
7993
Greg Claytoncaab74e2012-01-28 00:48:57 +00007994