blob: 7ccd6f24d86ca2a436d1e1a4e16a6859b1926445 [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"
60#include "DWARFDIECollection.h"
61#include "DWARFFormValue.h"
62#include "DWARFLocationList.h"
63#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000064#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065
66#include <map>
67
Greg Clayton62742b12010-11-11 01:09:45 +000068//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000069
70#ifdef ENABLE_DEBUG_PRINTF
71#include <stdio.h>
72#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
73#else
74#define DEBUG_PRINTF(fmt, ...)
75#endif
76
Greg Clayton594e5ed2010-09-27 21:07:38 +000077#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
79using namespace lldb;
80using namespace lldb_private;
81
Greg Clayton3bffb082011-12-10 02:15:28 +000082static inline bool
Sean Callanan5b26f272012-02-04 08:49:35 +000083child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton3bffb082011-12-10 02:15:28 +000084{
Sean Callanan5b26f272012-02-04 08:49:35 +000085 switch (tag)
86 {
87 default:
88 break;
89 case DW_TAG_subprogram:
90 case DW_TAG_inlined_subroutine:
91 case DW_TAG_class_type:
92 case DW_TAG_structure_type:
93 case DW_TAG_union_type:
94 return true;
95 }
96 return false;
Greg Clayton3bffb082011-12-10 02:15:28 +000097}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098
Sean Callananc7fbf732010-08-06 00:32:49 +000099static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000100DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101{
102 switch (dwarf_accessibility)
103 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000104 case DW_ACCESS_public: return eAccessPublic;
105 case DW_ACCESS_private: return eAccessPrivate;
106 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000107 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000109 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
Greg Clayton1fba87112012-02-09 20:03:49 +0000112#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
113
114class DIEStack
115{
116public:
117
118 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
119 {
120 m_dies.push_back (DIEInfo(cu, die));
121 }
122
123
124 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
125 {
126 StreamString log_strm;
127 const size_t n = m_dies.size();
128 log_strm.Printf("DIEStack[%zu]:\n", n);
129 for (size_t i=0; i<n; i++)
130 {
131 DWARFCompileUnit *cu = m_dies[i].cu;
132 const DWARFDebugInfoEntry *die = m_dies[i].die;
133 std::string qualified_name;
134 die->GetQualifiedName(dwarf, cu, qualified_name);
135 log_strm.Printf ("[%zu] 0x%8.8x: %s name='%s'\n",
136 i,
137 die->GetOffset(),
138 DW_TAG_value_to_name(die->Tag()),
139 qualified_name.c_str());
140 }
141 log->PutCString(log_strm.GetData());
142 }
143 void Pop ()
144 {
145 m_dies.pop_back();
146 }
147
148 class ScopedPopper
149 {
150 public:
151 ScopedPopper (DIEStack &die_stack) :
152 m_die_stack (die_stack),
153 m_valid (false)
154 {
155 }
156
157 void
158 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
159 {
160 m_valid = true;
161 m_die_stack.Push (cu, die);
162 }
163
164 ~ScopedPopper ()
165 {
166 if (m_valid)
167 m_die_stack.Pop();
168 }
169
170
171
172 protected:
173 DIEStack &m_die_stack;
174 bool m_valid;
175 };
176
177protected:
178 struct DIEInfo {
179 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
180 cu(c),
181 die(d)
182 {
183 }
184 DWARFCompileUnit *cu;
185 const DWARFDebugInfoEntry *die;
186 };
187 typedef std::vector<DIEInfo> Stack;
188 Stack m_dies;
189};
190#endif
191
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192void
193SymbolFileDWARF::Initialize()
194{
195 LogChannelDWARF::Initialize();
196 PluginManager::RegisterPlugin (GetPluginNameStatic(),
197 GetPluginDescriptionStatic(),
198 CreateInstance);
199}
200
201void
202SymbolFileDWARF::Terminate()
203{
204 PluginManager::UnregisterPlugin (CreateInstance);
205 LogChannelDWARF::Initialize();
206}
207
208
209const char *
210SymbolFileDWARF::GetPluginNameStatic()
211{
Greg Claytond4a2b372011-09-12 23:21:58 +0000212 return "dwarf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213}
214
215const char *
216SymbolFileDWARF::GetPluginDescriptionStatic()
217{
218 return "DWARF and DWARF3 debug symbol file reader.";
219}
220
221
222SymbolFile*
223SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
224{
225 return new SymbolFileDWARF(obj_file);
226}
227
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000228TypeList *
229SymbolFileDWARF::GetTypeList ()
230{
231 if (m_debug_map_symfile)
232 return m_debug_map_symfile->GetTypeList();
233 return m_obj_file->GetModule()->GetTypeList();
234
235}
236
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237//----------------------------------------------------------------------
238// Gets the first parent that is a lexical block, function or inlined
239// subroutine, or compile unit.
240//----------------------------------------------------------------------
241static const DWARFDebugInfoEntry *
242GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
243{
244 const DWARFDebugInfoEntry *die;
245 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
246 {
247 dw_tag_t tag = die->Tag();
248
249 switch (tag)
250 {
251 case DW_TAG_compile_unit:
252 case DW_TAG_subprogram:
253 case DW_TAG_inlined_subroutine:
254 case DW_TAG_lexical_block:
255 return die;
256 }
257 }
258 return NULL;
259}
260
261
Greg Clayton450e3f32010-10-12 02:24:53 +0000262SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
263 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000264 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton450e3f32010-10-12 02:24:53 +0000265 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000266 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000268 m_data_debug_abbrev (),
269 m_data_debug_aranges (),
270 m_data_debug_frame (),
271 m_data_debug_info (),
272 m_data_debug_line (),
273 m_data_debug_loc (),
274 m_data_debug_ranges (),
275 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000276 m_data_apple_names (),
277 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000278 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 m_info(),
281 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000282 m_apple_names_ap (),
283 m_apple_types_ap (),
284 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000285 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000286 m_function_basename_index(),
287 m_function_fullname_index(),
288 m_function_method_index(),
289 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000290 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000291 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000292 m_type_index(),
293 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000294 m_indexed (false),
295 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000296 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000297 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000298 m_ranges(),
299 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300{
301}
302
303SymbolFileDWARF::~SymbolFileDWARF()
304{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000305 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000306 {
307 ModuleSP module_sp (m_obj_file->GetModule());
308 if (module_sp)
309 module_sp->GetClangASTContext().RemoveExternalSource ();
310 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000311}
312
313static const ConstString &
314GetDWARFMachOSegmentName ()
315{
316 static ConstString g_dwarf_section_name ("__DWARF");
317 return g_dwarf_section_name;
318}
319
Greg Claytone576ab22011-02-15 00:19:15 +0000320UniqueDWARFASTTypeMap &
321SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
322{
323 if (m_debug_map_symfile)
324 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
325 return m_unique_ast_type_map;
326}
327
Greg Clayton6beaaa62011-01-17 03:46:26 +0000328ClangASTContext &
329SymbolFileDWARF::GetClangASTContext ()
330{
331 if (m_debug_map_symfile)
332 return m_debug_map_symfile->GetClangASTContext ();
333
334 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
335 if (!m_is_external_ast_source)
336 {
337 m_is_external_ast_source = true;
338 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
339 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
340 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000341 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000342 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000343 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000344 ast.SetExternalSource (ast_source_ap);
345 }
346 return ast;
347}
348
349void
350SymbolFileDWARF::InitializeObject()
351{
352 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000353 ModuleSP module_sp (m_obj_file->GetModule());
354 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000355 {
356 const SectionList *section_list = m_obj_file->GetSectionList();
357
358 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
359
360 // Memory map the DWARF mach-o segment so we have everything mmap'ed
361 // to keep our heap memory usage down.
362 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000363 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000364 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000365 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000366 if (m_data_apple_names.GetByteSize() > 0)
367 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000368 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
369 if (m_apple_names_ap->IsValid())
370 m_using_apple_tables = true;
371 else
Greg Clayton7f995132011-10-04 22:41:51 +0000372 m_apple_names_ap.reset();
373 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000374 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000375 if (m_data_apple_types.GetByteSize() > 0)
376 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000377 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
378 if (m_apple_types_ap->IsValid())
379 m_using_apple_tables = true;
380 else
Greg Clayton7f995132011-10-04 22:41:51 +0000381 m_apple_types_ap.reset();
382 }
383
384 get_apple_namespaces_data();
385 if (m_data_apple_namespaces.GetByteSize() > 0)
386 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000387 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
388 if (m_apple_namespaces_ap->IsValid())
389 m_using_apple_tables = true;
390 else
Greg Clayton7f995132011-10-04 22:41:51 +0000391 m_apple_namespaces_ap.reset();
392 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000393
Greg Clayton5009f9d2011-10-27 17:55:14 +0000394 get_apple_objc_data();
395 if (m_data_apple_objc.GetByteSize() > 0)
396 {
397 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
398 if (m_apple_objc_ap->IsValid())
399 m_using_apple_tables = true;
400 else
401 m_apple_objc_ap.reset();
402 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
405bool
406SymbolFileDWARF::SupportedVersion(uint16_t version)
407{
408 return version == 2 || version == 3;
409}
410
411uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000412SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413{
414 uint32_t abilities = 0;
415 if (m_obj_file != NULL)
416 {
417 const Section* section = NULL;
418 const SectionList *section_list = m_obj_file->GetSectionList();
419 if (section_list == NULL)
420 return 0;
421
422 uint64_t debug_abbrev_file_size = 0;
423 uint64_t debug_aranges_file_size = 0;
424 uint64_t debug_frame_file_size = 0;
425 uint64_t debug_info_file_size = 0;
426 uint64_t debug_line_file_size = 0;
427 uint64_t debug_loc_file_size = 0;
428 uint64_t debug_macinfo_file_size = 0;
429 uint64_t debug_pubnames_file_size = 0;
430 uint64_t debug_pubtypes_file_size = 0;
431 uint64_t debug_ranges_file_size = 0;
432 uint64_t debug_str_file_size = 0;
433
Greg Clayton6beaaa62011-01-17 03:46:26 +0000434 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435
436 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000437 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438
Greg Clayton4ceb9982010-07-21 22:54:26 +0000439 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 if (section != NULL)
441 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000442 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443
Greg Clayton4ceb9982010-07-21 22:54:26 +0000444 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000446 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 else
448 m_flags.Set (flagsGotDebugAbbrevData);
449
Greg Clayton4ceb9982010-07-21 22:54:26 +0000450 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000452 debug_aranges_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 else
454 m_flags.Set (flagsGotDebugArangesData);
455
Greg Clayton4ceb9982010-07-21 22:54:26 +0000456 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000458 debug_frame_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 else
460 m_flags.Set (flagsGotDebugFrameData);
461
Greg Clayton4ceb9982010-07-21 22:54:26 +0000462 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000464 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 else
466 m_flags.Set (flagsGotDebugLineData);
467
Greg Clayton4ceb9982010-07-21 22:54:26 +0000468 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000470 debug_loc_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 else
472 m_flags.Set (flagsGotDebugLocData);
473
Greg Clayton4ceb9982010-07-21 22:54:26 +0000474 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000476 debug_macinfo_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 else
478 m_flags.Set (flagsGotDebugMacInfoData);
479
Greg Clayton4ceb9982010-07-21 22:54:26 +0000480 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000482 debug_pubnames_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 else
484 m_flags.Set (flagsGotDebugPubNamesData);
485
Greg Clayton4ceb9982010-07-21 22:54:26 +0000486 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000488 debug_pubtypes_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 else
490 m_flags.Set (flagsGotDebugPubTypesData);
491
Greg Clayton4ceb9982010-07-21 22:54:26 +0000492 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000494 debug_ranges_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495 else
496 m_flags.Set (flagsGotDebugRangesData);
497
Greg Clayton4ceb9982010-07-21 22:54:26 +0000498 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000500 debug_str_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501 else
502 m_flags.Set (flagsGotDebugStrData);
503 }
504
505 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
506 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
507
508 if (debug_line_file_size > 0)
509 abilities |= LineTables;
510
511 if (debug_aranges_file_size > 0)
512 abilities |= AddressAcceleratorTable;
513
514 if (debug_pubnames_file_size > 0)
515 abilities |= FunctionAcceleratorTable;
516
517 if (debug_pubtypes_file_size > 0)
518 abilities |= TypeAcceleratorTable;
519
520 if (debug_macinfo_file_size > 0)
521 abilities |= MacroInformation;
522
523 if (debug_frame_file_size > 0)
524 abilities |= CallFrameInformation;
525 }
526 return abilities;
527}
528
529const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000530SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531{
532 if (m_flags.IsClear (got_flag))
533 {
534 m_flags.Set (got_flag);
535 const SectionList *section_list = m_obj_file->GetSectionList();
536 if (section_list)
537 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000538 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
539 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 {
541 // See if we memory mapped the DWARF segment?
542 if (m_dwarf_data.GetByteSize())
543 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000544 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 }
546 else
547 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000548 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 data.Clear();
550 }
551 }
552 }
553 }
554 return data;
555}
556
557const DataExtractor&
558SymbolFileDWARF::get_debug_abbrev_data()
559{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000560 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561}
562
563const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000564SymbolFileDWARF::get_debug_aranges_data()
565{
566 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
567}
568
569const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570SymbolFileDWARF::get_debug_frame_data()
571{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000572 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573}
574
575const DataExtractor&
576SymbolFileDWARF::get_debug_info_data()
577{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000578 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579}
580
581const DataExtractor&
582SymbolFileDWARF::get_debug_line_data()
583{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000584 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
587const DataExtractor&
588SymbolFileDWARF::get_debug_loc_data()
589{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000590 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591}
592
593const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594SymbolFileDWARF::get_debug_ranges_data()
595{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000596 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597}
598
599const DataExtractor&
600SymbolFileDWARF::get_debug_str_data()
601{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000602 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603}
604
Greg Claytonf9eec202011-09-01 23:16:13 +0000605const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000606SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000607{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000608 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000609}
610
611const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000612SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000613{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000614 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000615}
616
Greg Clayton7f995132011-10-04 22:41:51 +0000617const DataExtractor&
618SymbolFileDWARF::get_apple_namespaces_data()
619{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000620 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
621}
622
623const DataExtractor&
624SymbolFileDWARF::get_apple_objc_data()
625{
626 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000627}
628
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629
630DWARFDebugAbbrev*
631SymbolFileDWARF::DebugAbbrev()
632{
633 if (m_abbr.get() == NULL)
634 {
635 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
636 if (debug_abbrev_data.GetByteSize() > 0)
637 {
638 m_abbr.reset(new DWARFDebugAbbrev());
639 if (m_abbr.get())
640 m_abbr->Parse(debug_abbrev_data);
641 }
642 }
643 return m_abbr.get();
644}
645
646const DWARFDebugAbbrev*
647SymbolFileDWARF::DebugAbbrev() const
648{
649 return m_abbr.get();
650}
651
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652
653DWARFDebugInfo*
654SymbolFileDWARF::DebugInfo()
655{
656 if (m_info.get() == NULL)
657 {
658 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
659 if (get_debug_info_data().GetByteSize() > 0)
660 {
661 m_info.reset(new DWARFDebugInfo());
662 if (m_info.get())
663 {
664 m_info->SetDwarfData(this);
665 }
666 }
667 }
668 return m_info.get();
669}
670
671const DWARFDebugInfo*
672SymbolFileDWARF::DebugInfo() const
673{
674 return m_info.get();
675}
676
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677DWARFCompileUnit*
678SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
679{
680 DWARFDebugInfo* info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +0000681 if (info && UserIDMatches(cu_uid))
682 return info->GetCompileUnit((dw_offset_t)cu_uid).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 return NULL;
684}
685
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686
687DWARFDebugRanges*
688SymbolFileDWARF::DebugRanges()
689{
690 if (m_ranges.get() == NULL)
691 {
692 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
693 if (get_debug_ranges_data().GetByteSize() > 0)
694 {
695 m_ranges.reset(new DWARFDebugRanges());
696 if (m_ranges.get())
697 m_ranges->Extract(this);
698 }
699 }
700 return m_ranges.get();
701}
702
703const DWARFDebugRanges*
704SymbolFileDWARF::DebugRanges() const
705{
706 return m_ranges.get();
707}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708
709bool
Greg Clayton96d7d742010-11-10 23:42:09 +0000710SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711{
Greg Clayton96d7d742010-11-10 23:42:09 +0000712 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 {
Greg Claytond804d282012-03-15 21:01:31 +0000714 ModuleSP module_sp (m_obj_file->GetModule());
715 if (!module_sp)
716 return false;
717
Greg Clayton96d7d742010-11-10 23:42:09 +0000718 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 if (cu_die)
720 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000721 const char * cu_die_name = cu_die->GetName(this, curr_cu);
722 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
Jim Ingham0f35ac22011-08-24 23:34:20 +0000723 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 if (cu_die_name)
725 {
Greg Claytonf9be6932012-03-19 22:22:41 +0000726 std::string ramapped_file;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000727 FileSpec cu_file_spec;
728
Greg Clayton7bd65b92011-02-09 23:39:34 +0000729 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000731 // If we have a full path to the compile unit, we don't need to resolve
732 // the file. This can be expensive e.g. when the source files are NFS mounted.
Greg Claytonf9be6932012-03-19 22:22:41 +0000733 if (module_sp->RemapSourceFile(cu_die_name, ramapped_file))
734 cu_file_spec.SetFile (ramapped_file.c_str(), false);
735 else
736 cu_file_spec.SetFile (cu_die_name, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 }
738 else
739 {
740 std::string fullpath(cu_comp_dir);
741 if (*fullpath.rbegin() != '/')
742 fullpath += '/';
743 fullpath += cu_die_name;
Greg Claytonf9be6932012-03-19 22:22:41 +0000744 if (module_sp->RemapSourceFile (fullpath.c_str(), ramapped_file))
745 cu_file_spec.SetFile (ramapped_file.c_str(), false);
746 else
747 cu_file_spec.SetFile (fullpath.c_str(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 }
749
Greg Claytond804d282012-03-15 21:01:31 +0000750 compile_unit_sp.reset(new CompileUnit (module_sp,
751 curr_cu,
Greg Clayton81c22f62011-10-19 18:09:39 +0000752 cu_file_spec,
753 MakeUserID(curr_cu->GetOffset()),
754 cu_language));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755 if (compile_unit_sp.get())
756 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000757 curr_cu->SetUserData(compile_unit_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758 return true;
759 }
760 }
761 }
762 }
763 return false;
764}
765
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766uint32_t
767SymbolFileDWARF::GetNumCompileUnits()
768{
769 DWARFDebugInfo* info = DebugInfo();
770 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772 return 0;
773}
774
775CompUnitSP
776SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
777{
778 CompUnitSP comp_unit;
779 DWARFDebugInfo* info = DebugInfo();
780 if (info)
781 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000782 DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx);
783 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784 {
785 // Our symbol vendor shouldn't be asking us to add a compile unit that
786 // has already been added to it, which this DWARF plug-in knows as it
787 // stores the lldb compile unit (CompileUnit) pointer in each
788 // DWARFCompileUnit object when it gets added.
Greg Clayton96d7d742010-11-10 23:42:09 +0000789 assert(curr_cu->GetUserData() == NULL);
790 ParseCompileUnit(curr_cu, comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 }
792 }
793 return comp_unit;
794}
795
796static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000797AddRangesToBlock (Block& block,
798 DWARFDebugRanges::RangeList& ranges,
799 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000801 const size_t num_ranges = ranges.GetSize();
802 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000804 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
805 const addr_t range_base = range.GetRangeBase();
806 assert (range_base >= block_base_addr);
807 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000809 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810}
811
812
813Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000814SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815{
816 DWARFDebugRanges::RangeList func_ranges;
817 const char *name = NULL;
818 const char *mangled = NULL;
819 int decl_file = 0;
820 int decl_line = 0;
821 int decl_column = 0;
822 int call_file = 0;
823 int call_line = 0;
824 int call_column = 0;
825 DWARFExpression frame_base;
826
Greg Claytonc93237c2010-10-01 20:48:32 +0000827 assert (die->Tag() == DW_TAG_subprogram);
828
829 if (die->Tag() != DW_TAG_subprogram)
830 return NULL;
831
Greg Clayton3c2e3ae2012-02-06 06:42:51 +0000832 if (die->GetDIENamesAndRanges (this,
833 dwarf_cu,
834 name,
835 mangled,
836 func_ranges,
837 decl_file,
838 decl_line,
839 decl_column,
840 call_file,
841 call_line,
842 call_column,
843 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844 {
845 // Union of all ranges in the function DIE (if the function is discontiguous)
846 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000847 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
848 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
850 {
851 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
852 if (func_range.GetBaseAddress().IsValid())
853 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
854 }
855
856 if (func_range.GetBaseAddress().IsValid())
857 {
858 Mangled func_name;
859 if (mangled)
860 func_name.SetValue(mangled, true);
861 else if (name)
862 func_name.SetValue(name, false);
863
864 FunctionSP func_sp;
865 std::auto_ptr<Declaration> decl_ap;
866 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000867 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
868 decl_line,
869 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000871 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000872 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873
874 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
875
876 func_range.GetBaseAddress().ResolveLinkedAddress();
877
Greg Clayton81c22f62011-10-19 18:09:39 +0000878 const user_id_t func_user_id = MakeUserID(die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879 func_sp.reset(new Function (sc.comp_unit,
Greg Clayton81c22f62011-10-19 18:09:39 +0000880 func_user_id, // UserID is the DIE offset
881 func_user_id,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882 func_name,
883 func_type,
884 func_range)); // first address range
885
886 if (func_sp.get() != NULL)
887 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000888 if (frame_base.IsValid())
889 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890 sc.comp_unit->AddFunction(func_sp);
891 return func_sp.get();
892 }
893 }
894 }
895 return NULL;
896}
897
898size_t
899SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
900{
901 assert (sc.comp_unit);
902 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000903 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904 if (dwarf_cu)
905 {
906 DWARFDIECollection function_dies;
907 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
908 size_t func_idx;
909 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
910 {
911 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000912 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913 {
914 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
915 ++functions_added;
916 }
917 }
918 //FixupTypes();
919 }
920 return functions_added;
921}
922
923bool
924SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
925{
926 assert (sc.comp_unit);
Greg Clayton96d7d742010-11-10 23:42:09 +0000927 DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
928 assert (curr_cu);
929 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930
931 if (cu_die)
932 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000933 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
934 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000935
936 // All file indexes in DWARF are one based and a file of index zero is
937 // supposed to be the compile unit itself.
938 support_files.Append (*sc.comp_unit);
939
Greg Claytond804d282012-03-15 21:01:31 +0000940 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 }
942 return false;
943}
944
945struct ParseDWARFLineTableCallbackInfo
946{
947 LineTable* line_table;
948 const SectionList *section_list;
949 lldb::addr_t prev_sect_file_base_addr;
950 lldb::addr_t curr_sect_file_base_addr;
951 bool is_oso_for_debug_map;
952 bool prev_in_final_executable;
953 DWARFDebugLine::Row prev_row;
954 SectionSP prev_section_sp;
955 SectionSP curr_section_sp;
956};
957
958//----------------------------------------------------------------------
959// ParseStatementTableCallback
960//----------------------------------------------------------------------
961static void
962ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
963{
964 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
965 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
966 {
967 // Just started parsing the line table
968 }
969 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
970 {
971 // Done parsing line table, nothing to do for the cleanup
972 }
973 else
974 {
975 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
976 // We have a new row, lets append it
977
978 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
979 {
980 info->prev_section_sp = info->curr_section_sp;
981 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
982 // If this is an end sequence entry, then we subtract one from the
983 // address to make sure we get an address that is not the end of
984 // a section.
985 if (state.end_sequence && state.address != 0)
986 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
987 else
988 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
989
990 if (info->curr_section_sp.get())
991 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
992 else
993 info->curr_sect_file_base_addr = 0;
994 }
995 if (info->curr_section_sp.get())
996 {
997 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
998 // Check for the fancy section magic to determine if we
999
1000 if (info->is_oso_for_debug_map)
1001 {
1002 // When this is a debug map object file that contains DWARF
1003 // (referenced from an N_OSO debug map nlist entry) we will have
1004 // a file address in the file range for our section from the
1005 // original .o file, and a load address in the executable that
1006 // contains the debug map.
1007 //
1008 // If the sections for the file range and load range are
1009 // different, we have a remapped section for the function and
1010 // this address is resolved. If they are the same, then the
1011 // function for this address didn't make it into the final
1012 // executable.
1013 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
1014
1015 // If we are doing DWARF with debug map, then we need to carefully
1016 // add each line table entry as there may be gaps as functions
1017 // get moved around or removed.
1018 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
1019 {
1020 if (info->prev_in_final_executable)
1021 {
1022 bool terminate_previous_entry = false;
1023 if (!curr_in_final_executable)
1024 {
1025 // Check for the case where the previous line entry
1026 // in a function made it into the final executable,
1027 // yet the current line entry falls in a function
1028 // that didn't. The line table used to be contiguous
1029 // through this address range but now it isn't. We
1030 // need to terminate the previous line entry so
1031 // that we can reconstruct the line range correctly
1032 // for it and to keep the line table correct.
1033 terminate_previous_entry = true;
1034 }
1035 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
1036 {
1037 // Check for cases where the line entries used to be
1038 // contiguous address ranges, but now they aren't.
1039 // This can happen when order files specify the
1040 // ordering of the functions.
1041 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
1042 Section *curr_sect = info->curr_section_sp.get();
1043 Section *prev_sect = info->prev_section_sp.get();
1044 assert (curr_sect->GetLinkedSection());
1045 assert (prev_sect->GetLinkedSection());
1046 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
1047 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
1048 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
1049 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
1050 if (object_file_addr_delta != linked_file_addr_delta)
1051 terminate_previous_entry = true;
1052 }
1053
1054 if (terminate_previous_entry)
1055 {
1056 line_table->InsertLineEntry (info->prev_section_sp,
1057 state.address - info->prev_sect_file_base_addr,
1058 info->prev_row.line,
1059 info->prev_row.column,
1060 info->prev_row.file,
1061 false, // is_stmt
1062 false, // basic_block
1063 false, // state.prologue_end
1064 false, // state.epilogue_begin
1065 true); // end_sequence);
1066 }
1067 }
1068 }
1069
1070 if (curr_in_final_executable)
1071 {
1072 line_table->InsertLineEntry (info->curr_section_sp,
1073 curr_line_section_offset,
1074 state.line,
1075 state.column,
1076 state.file,
1077 state.is_stmt,
1078 state.basic_block,
1079 state.prologue_end,
1080 state.epilogue_begin,
1081 state.end_sequence);
1082 info->prev_section_sp = info->curr_section_sp;
1083 }
1084 else
1085 {
1086 // If the current address didn't make it into the final
1087 // executable, the current section will be the __text
1088 // segment in the .o file, so we need to clear this so
1089 // we can catch the next function that did make it into
1090 // the final executable.
1091 info->prev_section_sp.reset();
1092 info->curr_section_sp.reset();
1093 }
1094
1095 info->prev_in_final_executable = curr_in_final_executable;
1096 }
1097 else
1098 {
1099 // We are not in an object file that contains DWARF for an
1100 // N_OSO, this is just a normal DWARF file. The DWARF spec
1101 // guarantees that the addresses will be in increasing order
1102 // so, since we store line tables in file address order, we
1103 // can always just append the line entry without needing to
1104 // search for the correct insertion point (we don't need to
1105 // use LineEntry::InsertLineEntry()).
1106 line_table->AppendLineEntry (info->curr_section_sp,
1107 curr_line_section_offset,
1108 state.line,
1109 state.column,
1110 state.file,
1111 state.is_stmt,
1112 state.basic_block,
1113 state.prologue_end,
1114 state.epilogue_begin,
1115 state.end_sequence);
1116 }
1117 }
1118
1119 info->prev_row = state;
1120 }
1121}
1122
1123bool
1124SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1125{
1126 assert (sc.comp_unit);
1127 if (sc.comp_unit->GetLineTable() != NULL)
1128 return true;
1129
1130 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
1131 if (dwarf_cu)
1132 {
1133 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001134 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001136 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1137 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001139 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1140 if (line_table_ap.get())
1141 {
1142 ParseDWARFLineTableCallbackInfo info = {
1143 line_table_ap.get(),
1144 m_obj_file->GetSectionList(),
1145 0,
1146 0,
1147 m_debug_map_symfile != NULL,
1148 false,
1149 DWARFDebugLine::Row(),
1150 SectionSP(),
1151 SectionSP()
1152 };
1153 uint32_t offset = cu_line_offset;
1154 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1155 sc.comp_unit->SetLineTable(line_table_ap.release());
1156 return true;
1157 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 }
1159 }
1160 }
1161 return false;
1162}
1163
1164size_t
1165SymbolFileDWARF::ParseFunctionBlocks
1166(
1167 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001168 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001169 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 const DWARFDebugInfoEntry *die,
1171 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001172 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173)
1174{
1175 size_t blocks_added = 0;
1176 while (die != NULL)
1177 {
1178 dw_tag_t tag = die->Tag();
1179
1180 switch (tag)
1181 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001183 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184 case DW_TAG_lexical_block:
1185 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001186 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001187 if (tag == DW_TAG_subprogram)
1188 {
1189 // Skip any DW_TAG_subprogram DIEs that are inside
1190 // of a normal or inlined functions. These will be
1191 // parsed on their own as separate entities.
1192
1193 if (depth > 0)
1194 break;
1195
1196 block = parent_block;
1197 }
1198 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001199 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001200 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001201 parent_block->AddChild(block_sp);
1202 block = block_sp.get();
1203 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001204 DWARFDebugRanges::RangeList ranges;
1205 const char *name = NULL;
1206 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 int decl_file = 0;
1209 int decl_line = 0;
1210 int decl_column = 0;
1211 int call_file = 0;
1212 int call_line = 0;
1213 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001214 if (die->GetDIENamesAndRanges (this,
1215 dwarf_cu,
1216 name,
1217 mangled_name,
1218 ranges,
1219 decl_file, decl_line, decl_column,
1220 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221 {
1222 if (tag == DW_TAG_subprogram)
1223 {
1224 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001225 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001227 else if (tag == DW_TAG_inlined_subroutine)
1228 {
1229 // We get called here for inlined subroutines in two ways.
1230 // The first time is when we are making the Function object
1231 // for this inlined concrete instance. Since we're creating a top level block at
1232 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1233 // adjust the containing address.
1234 // The second time is when we are parsing the blocks inside the function that contains
1235 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1236 // function the offset will be for that function.
1237 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1238 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001239 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001240 }
1241 }
1242
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001243 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244
1245 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1246 {
1247 std::auto_ptr<Declaration> decl_ap;
1248 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001249 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1250 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251
1252 std::auto_ptr<Declaration> call_ap;
1253 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001254 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1255 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001257 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 }
1259
1260 ++blocks_added;
1261
Greg Claytondd7feaf2011-08-12 17:54:33 +00001262 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001264 blocks_added += ParseFunctionBlocks (sc,
1265 block,
1266 dwarf_cu,
1267 die->GetFirstChild(),
1268 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001269 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 }
1271 }
1272 }
1273 break;
1274 default:
1275 break;
1276 }
1277
Greg Claytondd7feaf2011-08-12 17:54:33 +00001278 // Only parse siblings of the block if we are not at depth zero. A depth
1279 // of zero indicates we are currently parsing the top level
1280 // DW_TAG_subprogram DIE
1281
1282 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001284 else
1285 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286 }
1287 return blocks_added;
1288}
1289
Greg Claytonf0705c82011-10-22 03:33:13 +00001290bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001291SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1292 const DWARFDebugInfoEntry *die,
1293 ClangASTContext::TemplateParameterInfos &template_param_infos)
1294{
1295 const dw_tag_t tag = die->Tag();
1296
1297 switch (tag)
1298 {
1299 case DW_TAG_template_type_parameter:
1300 case DW_TAG_template_value_parameter:
1301 {
1302 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1303
1304 DWARFDebugInfoEntry::Attributes attributes;
1305 const size_t num_attributes = die->GetAttributes (this,
1306 dwarf_cu,
1307 fixed_form_sizes,
1308 attributes);
1309 const char *name = NULL;
1310 Type *lldb_type = NULL;
1311 clang_type_t clang_type = NULL;
1312 uint64_t uval64 = 0;
1313 bool uval64_valid = false;
1314 if (num_attributes > 0)
1315 {
1316 DWARFFormValue form_value;
1317 for (size_t i=0; i<num_attributes; ++i)
1318 {
1319 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1320
1321 switch (attr)
1322 {
1323 case DW_AT_name:
1324 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1325 name = form_value.AsCString(&get_debug_str_data());
1326 break;
1327
1328 case DW_AT_type:
1329 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1330 {
1331 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1332 lldb_type = ResolveTypeUID(type_die_offset);
1333 if (lldb_type)
1334 clang_type = lldb_type->GetClangForwardType();
1335 }
1336 break;
1337
1338 case DW_AT_const_value:
1339 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1340 {
1341 uval64_valid = true;
1342 uval64 = form_value.Unsigned();
1343 }
1344 break;
1345 default:
1346 break;
1347 }
1348 }
1349
1350 if (name && lldb_type && clang_type)
1351 {
1352 bool is_signed = false;
1353 template_param_infos.names.push_back(name);
1354 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1355 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1356 {
1357 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1358 template_param_infos.args.push_back (clang::TemplateArgument (llvm::APSInt(apint), clang_qual_type));
1359 }
1360 else
1361 {
1362 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1363 }
1364 }
1365 else
1366 {
1367 return false;
1368 }
1369
1370 }
1371 }
1372 return true;
1373
1374 default:
1375 break;
1376 }
1377 return false;
1378}
1379
1380bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001381SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1382 const DWARFDebugInfoEntry *parent_die,
1383 ClangASTContext::TemplateParameterInfos &template_param_infos)
1384{
1385
1386 if (parent_die == NULL)
1387 return NULL;
1388
Greg Claytonf0705c82011-10-22 03:33:13 +00001389 Args template_parameter_names;
1390 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1391 die != NULL;
1392 die = die->GetSibling())
1393 {
1394 const dw_tag_t tag = die->Tag();
1395
1396 switch (tag)
1397 {
1398 case DW_TAG_template_type_parameter:
1399 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001400 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001401 break;
1402
1403 default:
1404 break;
1405 }
1406 }
1407 if (template_param_infos.args.empty())
1408 return false;
1409 return template_param_infos.args.size() == template_param_infos.names.size();
1410}
1411
1412clang::ClassTemplateDecl *
1413SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001414 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001415 const char *parent_name,
1416 int tag_decl_kind,
1417 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1418{
1419 if (template_param_infos.IsValid())
1420 {
1421 std::string template_basename(parent_name);
1422 template_basename.erase (template_basename.find('<'));
1423 ClangASTContext &ast = GetClangASTContext();
1424
1425 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001426 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001427 template_basename.c_str(),
1428 tag_decl_kind,
1429 template_param_infos);
1430 }
1431 return NULL;
1432}
1433
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001434size_t
1435SymbolFileDWARF::ParseChildMembers
1436(
1437 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001438 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001440 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001441 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1443 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001444 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001445 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001446 bool &is_a_class,
1447 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001448)
1449{
1450 if (parent_die == NULL)
1451 return 0;
1452
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001453 size_t count = 0;
1454 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001455 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001456 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001457
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1459 {
1460 dw_tag_t tag = die->Tag();
1461
1462 switch (tag)
1463 {
1464 case DW_TAG_member:
Sean Callanan751aac62012-03-29 19:07:06 +00001465 case DW_TAG_APPLE_Property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466 {
1467 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001468 const size_t num_attributes = die->GetAttributes (this,
1469 dwarf_cu,
1470 fixed_form_sizes,
1471 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472 if (num_attributes > 0)
1473 {
1474 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001475 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001477 const char *prop_name = NULL;
1478 const char *prop_getter_name = NULL;
1479 const char *prop_setter_name = NULL;
1480 uint32_t prop_attributes = 0;
1481
1482
Greg Clayton24739922010-10-13 03:15:28 +00001483 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001485 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001486 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487 size_t byte_size = 0;
1488 size_t bit_offset = 0;
1489 size_t bit_size = 0;
1490 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001491 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492 {
1493 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1494 DWARFFormValue form_value;
1495 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1496 {
1497 switch (attr)
1498 {
1499 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1500 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1501 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1502 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1503 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1504 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1505 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1506 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1507 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001508 if (form_value.BlockData())
1509 {
1510 Value initialValue(0);
1511 Value memberOffset(0);
1512 const DataExtractor& debug_info_data = get_debug_info_data();
1513 uint32_t block_length = form_value.Unsigned();
1514 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1515 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1516 NULL, // clang::ASTContext *
1517 NULL, // ClangExpressionVariableList *
1518 NULL, // ClangExpressionDeclMap *
1519 NULL, // RegisterContext *
1520 debug_info_data,
1521 block_offset,
1522 block_length,
1523 eRegisterKindDWARF,
1524 &initialValue,
1525 memberOffset,
1526 NULL))
1527 {
1528 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1529 }
1530 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531 break;
1532
Greg Clayton8cf05932010-07-22 18:30:50 +00001533 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1b02c172012-03-14 21:00:47 +00001534 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001535 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1536 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1537 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1538 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1539
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001540 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001541 case DW_AT_declaration:
1542 case DW_AT_description:
1543 case DW_AT_mutable:
1544 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001545 case DW_AT_sibling:
1546 break;
1547 }
1548 }
1549 }
Sean Callanan5a477cf2010-10-30 01:56:10 +00001550
Sean Callanan751aac62012-03-29 19:07:06 +00001551 ConstString fixed_getter;
1552 ConstString fixed_setter;
1553
1554 if (prop_getter_name && prop_getter_name[0] == '-')
1555 {
1556 ObjCLanguageRuntime::ParseMethodName (prop_getter_name,
1557 NULL,
1558 &fixed_getter,
1559 NULL,
1560 NULL);
1561 prop_getter_name = fixed_getter.GetCString();
1562 }
1563
1564 if (prop_setter_name && prop_setter_name[0] == '-')
1565 {
1566 ObjCLanguageRuntime::ParseMethodName (prop_setter_name,
1567 NULL,
1568 &fixed_setter,
1569 NULL,
1570 NULL);
1571 prop_setter_name = fixed_setter.GetCString();
1572 }
1573
1574 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001575 // represents fields that are references with bad byte size
1576 // and bit size/offset information such as:
1577 //
1578 // DW_AT_byte_size( 0x00 )
1579 // DW_AT_bit_size( 0x40 )
1580 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1581 //
1582 // So check the bit offset to make sure it is sane, and if
1583 // the values are not sane, remove them. If we don't do this
1584 // then we will end up with a crash if we try to use this
1585 // type in an expression when clang becomes unhappy with its
1586 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001587
Greg Clayton44953932011-10-25 01:25:35 +00001588 if (bit_offset > 128)
1589 {
1590 bit_size = 0;
1591 bit_offset = 0;
1592 }
1593
1594 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001595 if (class_language == eLanguageTypeObjC ||
1596 class_language == eLanguageTypeObjC_plus_plus)
1597 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001598
1599 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1600 {
1601 // Not all compilers will mark the vtable pointer
1602 // member as artificial (llvm-gcc). We can't have
1603 // the virtual members in our classes otherwise it
1604 // throws off all child offsets since we end up
1605 // having and extra pointer sized member in our
1606 // class layouts.
1607 is_artificial = true;
1608 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001609
Greg Clayton24739922010-10-13 03:15:28 +00001610 if (is_artificial == false)
1611 {
1612 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001613 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001614 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001615 {
Sean Callanan751aac62012-03-29 19:07:06 +00001616 if (member_type)
1617 {
1618 if (accessibility == eAccessNone)
1619 accessibility = default_accessibility;
1620 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001621
Sean Callanan751aac62012-03-29 19:07:06 +00001622 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
1623 name,
1624 member_type->GetClangLayoutType(),
1625 accessibility,
1626 bit_size);
Sean Callanan5b26f272012-02-04 08:49:35 +00001627 }
1628 else
1629 {
Sean Callanan751aac62012-03-29 19:07:06 +00001630 if (name)
1631 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1632 MakeUserID(die->GetOffset()),
1633 name,
1634 encoding_uid);
1635 else
1636 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1637 MakeUserID(die->GetOffset()),
1638 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00001639 }
Sean Callanan751aac62012-03-29 19:07:06 +00001640
1641 if (member_byte_offset != UINT32_MAX || bit_size != 0)
1642 {
1643 /////////////////////////////////////////////////////////////
1644 // How to locate a field given the DWARF debug information
1645 //
1646 // AT_byte_size indicates the size of the word in which the
1647 // bit offset must be interpreted.
1648 //
1649 // AT_data_member_location indicates the byte offset of the
1650 // word from the base address of the structure.
1651 //
1652 // AT_bit_offset indicates how many bits into the word
1653 // (according to the host endianness) the low-order bit of
1654 // the field starts. AT_bit_offset can be negative.
1655 //
1656 // AT_bit_size indicates the size of the field in bits.
1657 /////////////////////////////////////////////////////////////
Sean Callanan5b26f272012-02-04 08:49:35 +00001658
Sean Callanan751aac62012-03-29 19:07:06 +00001659 ByteOrder object_endian = GetObjectFile()->GetModule()->GetArchitecture().GetDefaultEndian();
1660
1661 uint64_t total_bit_offset = 0;
1662
1663 total_bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1664
1665 if (object_endian == eByteOrderLittle)
1666 {
1667 total_bit_offset += byte_size * 8;
1668 total_bit_offset -= (bit_offset + bit_size);
1669 }
1670 else
1671 {
1672 total_bit_offset += bit_offset;
1673 }
1674
1675 layout_info.field_offsets.insert(std::make_pair(field_decl, total_bit_offset));
1676 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001677 }
Sean Callanan751aac62012-03-29 19:07:06 +00001678
Jim Inghame3ae82a2011-11-12 01:36:43 +00001679 if (prop_name != NULL)
1680 {
Sean Callanan751aac62012-03-29 19:07:06 +00001681 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001682
Sean Callanan751aac62012-03-29 19:07:06 +00001683 if (field_decl)
1684 {
1685 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1686 assert (ivar_decl != NULL);
1687 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001688
Jim Inghame3ae82a2011-11-12 01:36:43 +00001689 GetClangASTContext().AddObjCClassProperty (class_clang_type,
1690 prop_name,
Sean Callanan751aac62012-03-29 19:07:06 +00001691 member_type->GetClangLayoutType(),
Jim Inghame3ae82a2011-11-12 01:36:43 +00001692 ivar_decl,
1693 prop_setter_name,
1694 prop_getter_name,
1695 prop_attributes);
1696 }
Greg Clayton24739922010-10-13 03:15:28 +00001697 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001698 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001699 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700 }
1701 break;
1702
1703 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001704 // Let the type parsing code handle this one for us.
1705 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001706 break;
1707
1708 case DW_TAG_inheritance:
1709 {
1710 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001711 if (default_accessibility == eAccessNone)
1712 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713 // TODO: implement DW_TAG_inheritance type parsing
1714 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001715 const size_t num_attributes = die->GetAttributes (this,
1716 dwarf_cu,
1717 fixed_form_sizes,
1718 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001719 if (num_attributes > 0)
1720 {
1721 Declaration decl;
1722 DWARFExpression location;
1723 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001724 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001725 bool is_virtual = false;
1726 bool is_base_of_class = true;
1727 off_t member_offset = 0;
1728 uint32_t i;
1729 for (i=0; i<num_attributes; ++i)
1730 {
1731 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1732 DWARFFormValue form_value;
1733 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1734 {
1735 switch (attr)
1736 {
1737 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1738 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1739 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1740 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1741 case DW_AT_data_member_location:
1742 if (form_value.BlockData())
1743 {
1744 Value initialValue(0);
1745 Value memberOffset(0);
1746 const DataExtractor& debug_info_data = get_debug_info_data();
1747 uint32_t block_length = form_value.Unsigned();
1748 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001749 if (DWARFExpression::Evaluate (NULL,
1750 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001751 NULL,
1752 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001753 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001754 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001755 block_offset,
1756 block_length,
1757 eRegisterKindDWARF,
1758 &initialValue,
1759 memberOffset,
1760 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761 {
1762 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1763 }
1764 }
1765 break;
1766
1767 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001768 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769 break;
1770
1771 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1772 default:
1773 case DW_AT_sibling:
1774 break;
1775 }
1776 }
1777 }
1778
Greg Clayton526e5af2010-11-13 03:52:47 +00001779 Type *base_class_type = ResolveTypeUID(encoding_uid);
1780 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001781
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001782 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1783 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001784 if (class_language == eLanguageTypeObjC)
1785 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001786 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001787 }
1788 else
1789 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001790 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001791 accessibility,
1792 is_virtual,
1793 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001794 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001795 }
1796 }
1797 break;
1798
1799 default:
1800 break;
1801 }
1802 }
1803 return count;
1804}
1805
1806
1807clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001808SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001809{
1810 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001811 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001812 {
1813 DWARFCompileUnitSP cu_sp;
1814 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1815 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001816 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001817 }
1818 return NULL;
1819}
1820
1821clang::DeclContext*
1822SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1823{
Greg Clayton81c22f62011-10-19 18:09:39 +00001824 if (UserIDMatches(type_uid))
1825 return GetClangDeclContextForDIEOffset (sc, type_uid);
1826 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001827}
1828
1829Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001830SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001831{
Greg Clayton81c22f62011-10-19 18:09:39 +00001832 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001833 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001834 DWARFDebugInfo* debug_info = DebugInfo();
1835 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001836 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001837 DWARFCompileUnitSP cu_sp;
1838 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001839 const bool assert_not_being_parsed = true;
1840 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001841 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001842 }
1843 return NULL;
1844}
1845
Greg Claytoncab36a32011-12-08 05:16:30 +00001846Type*
1847SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001848{
Greg Claytoncab36a32011-12-08 05:16:30 +00001849 if (die != NULL)
1850 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001851 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1852 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001853 GetObjectFile()->GetModule()->LogMessage (log.get(),
1854 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1855 die->GetOffset(),
1856 DW_TAG_value_to_name(die->Tag()),
1857 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001858
Greg Claytoncab36a32011-12-08 05:16:30 +00001859 // We might be coming in in the middle of a type tree (a class
1860 // withing a class, an enum within a class), so parse any needed
1861 // parent DIEs before we get to this one...
1862 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1863 switch (decl_ctx_die->Tag())
1864 {
1865 case DW_TAG_structure_type:
1866 case DW_TAG_union_type:
1867 case DW_TAG_class_type:
1868 {
1869 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001870 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001871 GetObjectFile()->GetModule()->LogMessage (log.get(),
1872 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1873 die->GetOffset(),
1874 DW_TAG_value_to_name(die->Tag()),
1875 die->GetName(this, cu),
1876 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001877
Greg Claytoncab36a32011-12-08 05:16:30 +00001878 Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
Sean Callanan5b26f272012-02-04 08:49:35 +00001879 if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
Greg Clayton3bffb082011-12-10 02:15:28 +00001880 {
1881 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001882 GetObjectFile()->GetModule()->LogMessage (log.get(),
1883 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
1884 die->GetOffset(),
1885 DW_TAG_value_to_name(die->Tag()),
1886 die->GetName(this, cu),
1887 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001888 // Ask the type to complete itself if it already hasn't since if we
1889 // want a function (method or static) from a class, the class must
1890 // create itself and add it's own methods and class functions.
1891 if (parent_type)
1892 parent_type->GetClangFullType();
1893 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001894 }
1895 break;
1896
1897 default:
1898 break;
1899 }
1900 return ResolveType (cu, die);
1901 }
1902 return NULL;
1903}
1904
Greg Clayton6beaaa62011-01-17 03:46:26 +00001905// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1906// SymbolFileDWARF objects to detect if this DWARF file is the one that
1907// can resolve a clang_type.
1908bool
1909SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1910{
1911 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1912 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1913 return die != NULL;
1914}
1915
1916
Greg Clayton1be10fc2010-09-29 01:12:09 +00001917lldb::clang_type_t
1918SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1919{
1920 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001921 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1922 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001923 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001924 {
1925 // We have already resolved this type...
1926 return clang_type;
1927 }
1928 // Once we start resolving this type, remove it from the forward declaration
1929 // map in case anyone child members or other types require this type to get resolved.
1930 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1931 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001932 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001933
Greg Clayton1be10fc2010-09-29 01:12:09 +00001934
Greg Clayton85ae2e12011-10-18 23:36:41 +00001935 // Disable external storage for this type so we don't get anymore
1936 // clang::ExternalASTSource queries for this type.
1937 ClangASTContext::SetHasExternalStorage (clang_type, false);
1938
Greg Clayton450e3f32010-10-12 02:24:53 +00001939 DWARFDebugInfo* debug_info = DebugInfo();
1940
Greg Clayton96d7d742010-11-10 23:42:09 +00001941 DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001942 Type *type = m_die_to_type.lookup (die);
1943
1944 const dw_tag_t tag = die->Tag();
1945
Greg Clayton3bffb082011-12-10 02:15:28 +00001946 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1947 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001948 GetObjectFile()->GetModule()->LogMessage (log.get(),
1949 "0x%8.8llx: %s '%s' resolving forward declaration...\n",
1950 MakeUserID(die->GetOffset()),
1951 DW_TAG_value_to_name(tag),
1952 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001953 assert (clang_type);
1954 DWARFDebugInfoEntry::Attributes attributes;
1955
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001956 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001957
1958 switch (tag)
1959 {
1960 case DW_TAG_structure_type:
1961 case DW_TAG_union_type:
1962 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00001963 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001964 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00001965
1966 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001967 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00001968 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00001969 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001970
Sean Callanan77a1fd32012-02-27 20:07:01 +00001971 LanguageType class_language = eLanguageTypeUnknown;
1972 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
1973 if (is_objc_class)
1974 class_language = eLanguageTypeObjC;
1975
1976 int tag_decl_kind = -1;
1977 AccessType default_accessibility = eAccessNone;
1978 if (tag == DW_TAG_structure_type)
1979 {
1980 tag_decl_kind = clang::TTK_Struct;
1981 default_accessibility = eAccessPublic;
1982 }
1983 else if (tag == DW_TAG_union_type)
1984 {
1985 tag_decl_kind = clang::TTK_Union;
1986 default_accessibility = eAccessPublic;
1987 }
1988 else if (tag == DW_TAG_class_type)
1989 {
1990 tag_decl_kind = clang::TTK_Class;
1991 default_accessibility = eAccessPrivate;
1992 }
1993
1994 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1995 std::vector<clang::CXXBaseSpecifier *> base_classes;
1996 std::vector<int> member_accessibilities;
1997 bool is_a_class = false;
1998 // Parse members and base classes first
1999 DWARFDIECollection member_function_dies;
2000
2001 ParseChildMembers (sc,
2002 curr_cu,
2003 die,
2004 clang_type,
2005 class_language,
2006 base_classes,
2007 member_accessibilities,
2008 member_function_dies,
2009 default_accessibility,
2010 is_a_class,
2011 layout_info);
2012
2013 // Now parse any methods if there were any...
2014 size_t num_functions = member_function_dies.Size();
2015 if (num_functions > 0)
2016 {
2017 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002018 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002019 ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002020 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002021 }
2022
2023 if (class_language == eLanguageTypeObjC)
2024 {
Greg Clayton84db9102012-03-26 23:03:23 +00002025 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(ast.getASTContext(), clang_type));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002026 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00002027 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002028
Sean Callanan77a1fd32012-02-27 20:07:01 +00002029 DIEArray method_die_offsets;
2030 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002031 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002032 if (m_apple_objc_ap.get())
2033 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
2034 }
2035 else
2036 {
2037 if (!m_indexed)
2038 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002039
Sean Callanan77a1fd32012-02-27 20:07:01 +00002040 ConstString class_name (class_str.c_str());
2041 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2042 }
2043
2044 if (!method_die_offsets.empty())
2045 {
2046 DWARFDebugInfo* debug_info = DebugInfo();
2047
2048 DWARFCompileUnit* method_cu = NULL;
2049 const size_t num_matches = method_die_offsets.size();
2050 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002051 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002052 const dw_offset_t die_offset = method_die_offsets[i];
2053 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2054
2055 if (method_die)
2056 ResolveType (method_cu, method_die);
2057 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002058 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002059 if (m_using_apple_tables)
2060 {
2061 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2062 die_offset, class_str.c_str());
2063 }
2064 }
2065 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002066 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002067 }
2068 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002069
2070 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2071 // need to tell the clang type it is actually a class.
2072 if (class_language != eLanguageTypeObjC)
2073 {
2074 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2075 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2076 }
2077
2078 // Since DW_TAG_structure_type gets used for both classes
2079 // and structures, we may need to set any DW_TAG_member
2080 // fields to have a "private" access if none was specified.
2081 // When we parsed the child members we tracked that actual
2082 // accessibility value for each DW_TAG_member in the
2083 // "member_accessibilities" array. If the value for the
2084 // member is zero, then it was set to the "default_accessibility"
2085 // which for structs was "public". Below we correct this
2086 // by setting any fields to "private" that weren't correctly
2087 // set.
2088 if (is_a_class && !member_accessibilities.empty())
2089 {
2090 // This is a class and all members that didn't have
2091 // their access specified are private.
2092 ast.SetDefaultAccessForRecordFields (clang_type,
2093 eAccessPrivate,
2094 &member_accessibilities.front(),
2095 member_accessibilities.size());
2096 }
2097
2098 if (!base_classes.empty())
2099 {
2100 ast.SetBaseClassesForClassType (clang_type,
2101 &base_classes.front(),
2102 base_classes.size());
2103
2104 // Clang will copy each CXXBaseSpecifier in "base_classes"
2105 // so we have to free them all.
2106 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2107 base_classes.size());
2108 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002109 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002110 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002111
2112 ast.BuildIndirectFields (clang_type);
2113
Sean Callanan77a1fd32012-02-27 20:07:01 +00002114 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002115
Greg Claytoncaab74e2012-01-28 00:48:57 +00002116 if (!layout_info.field_offsets.empty())
2117 {
2118 if (type)
2119 layout_info.bit_size = type->GetByteSize() * 8;
2120 if (layout_info.bit_size == 0)
2121 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_byte_size, 0) * 8;
2122 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
2123 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
2124 if (record_type)
2125 {
2126 const clang::RecordDecl *record_decl = record_type->getDecl();
2127
2128 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002129 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002130 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002131 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) caching layout info for record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u], base_offsets[0], vbase_offsets[0])",
Greg Claytoncaab74e2012-01-28 00:48:57 +00002132 clang_type,
2133 record_decl,
2134 layout_info.bit_size,
2135 layout_info.alignment,
2136 (uint32_t)layout_info.field_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002137
Greg Clayton821ac6d2012-01-28 02:22:27 +00002138 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
2139 for (pos = layout_info.field_offsets.begin(); pos != end; ++pos)
2140 {
2141 GetObjectFile()->GetModule()->LogMessage (log.get(),
2142 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field = { bit_offset=%u, name='%s' }",
2143 clang_type,
2144 (uint32_t)pos->second,
2145 pos->first->getNameAsString().c_str());
2146 }
2147 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002148 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2149 }
2150 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002151 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002152
Greg Claytonc93237c2010-10-01 20:48:32 +00002153 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002154
2155 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002156 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002157 if (die->HasChildren())
2158 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002159 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
2160 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002161 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002162 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002163 return clang_type;
2164
2165 default:
2166 assert(false && "not a forward clang type decl!");
2167 break;
2168 }
2169 return NULL;
2170}
2171
Greg Claytonc685f8e2010-09-15 04:15:46 +00002172Type*
Greg Clayton96d7d742010-11-10 23:42:09 +00002173SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002174{
2175 if (type_die != NULL)
2176 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002177 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002178
Greg Claytonc685f8e2010-09-15 04:15:46 +00002179 if (type == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002180 type = GetTypeForDIE (curr_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002181
Greg Clayton24739922010-10-13 03:15:28 +00002182 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002183 {
2184 if (type != DIE_IS_BEING_PARSED)
2185 return type;
2186
2187 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
2188 type_die->GetOffset(),
2189 DW_TAG_value_to_name(type_die->Tag()),
2190 type_die->GetName(this, curr_cu));
2191
2192 }
2193 else
2194 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002195 }
2196 return NULL;
2197}
2198
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002199CompileUnit*
Greg Clayton96d7d742010-11-10 23:42:09 +00002200SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002201{
2202 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00002203 if (curr_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002204 {
2205 // The symbol vendor doesn't know about this compile unit, we
2206 // need to parse and add it to the symbol vendor object.
2207 CompUnitSP dc_cu;
Greg Clayton96d7d742010-11-10 23:42:09 +00002208 ParseCompileUnit(curr_cu, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002209 if (dc_cu.get())
2210 {
2211 // Figure out the compile unit index if we weren't given one
Greg Clayton016a95e2010-09-14 02:20:48 +00002212 if (cu_idx == UINT32_MAX)
Greg Clayton96d7d742010-11-10 23:42:09 +00002213 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002214
2215 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx);
Greg Clayton450e3f32010-10-12 02:24:53 +00002216
2217 if (m_debug_map_symfile)
2218 m_debug_map_symfile->SetCompileUnit(this, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002219 }
2220 }
Greg Clayton96d7d742010-11-10 23:42:09 +00002221 return (CompileUnit*)curr_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002222}
2223
2224bool
Greg Clayton96d7d742010-11-10 23:42:09 +00002225SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002226{
2227 sc.Clear();
2228 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00002229 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002230
Greg Clayton81c22f62011-10-19 18:09:39 +00002231 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002232 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002233 sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002234
2235 if (sc.function)
2236 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002237 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002238 return true;
2239 }
2240
2241 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002242}
2243
2244uint32_t
2245SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2246{
2247 Timer scoped_timer(__PRETTY_FUNCTION__,
2248 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002249 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002250 so_addr.GetOffset(),
2251 resolve_scope);
2252 uint32_t resolved = 0;
2253 if (resolve_scope & ( eSymbolContextCompUnit |
2254 eSymbolContextFunction |
2255 eSymbolContextBlock |
2256 eSymbolContextLineEntry))
2257 {
2258 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2259
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002261 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002262 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002263 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002264 if (cu_offset != DW_INVALID_OFFSET)
2265 {
2266 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00002267 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2268 if (curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002269 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002270 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002271 assert(sc.comp_unit != NULL);
2272 resolved |= eSymbolContextCompUnit;
2273
2274 if (resolve_scope & eSymbolContextLineEntry)
2275 {
2276 LineTable *line_table = sc.comp_unit->GetLineTable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002277 if (line_table != NULL)
2278 {
2279 if (so_addr.IsLinkedAddress())
2280 {
2281 Address linked_addr (so_addr);
2282 linked_addr.ResolveLinkedAddress();
2283 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2284 {
2285 resolved |= eSymbolContextLineEntry;
2286 }
2287 }
2288 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
2289 {
2290 resolved |= eSymbolContextLineEntry;
2291 }
2292 }
2293 }
2294
2295 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2296 {
2297 DWARFDebugInfoEntry *function_die = NULL;
2298 DWARFDebugInfoEntry *block_die = NULL;
2299 if (resolve_scope & eSymbolContextBlock)
2300 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002301 curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002302 }
2303 else
2304 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002305 curr_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002306 }
2307
2308 if (function_die != NULL)
2309 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002310 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002311 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002312 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002313 }
2314
2315 if (sc.function != NULL)
2316 {
2317 resolved |= eSymbolContextFunction;
2318
2319 if (resolve_scope & eSymbolContextBlock)
2320 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002321 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002322
2323 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002324 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002325 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002326 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002327 if (sc.block)
2328 resolved |= eSymbolContextBlock;
2329 }
2330 }
2331 }
2332 }
2333 }
2334 }
2335 }
2336 return resolved;
2337}
2338
2339
2340
2341uint32_t
2342SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2343{
2344 const uint32_t prev_size = sc_list.GetSize();
2345 if (resolve_scope & eSymbolContextCompUnit)
2346 {
2347 DWARFDebugInfo* debug_info = DebugInfo();
2348 if (debug_info)
2349 {
2350 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00002351 DWARFCompileUnit* curr_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002352
Greg Clayton96d7d742010-11-10 23:42:09 +00002353 for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002354 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002355 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002356 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
2357 if (check_inlines || file_spec_matches_cu_file_spec)
2358 {
2359 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton96d7d742010-11-10 23:42:09 +00002360 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002361 assert(sc.comp_unit != NULL);
2362
2363 uint32_t file_idx = UINT32_MAX;
2364
2365 // If we are looking for inline functions only and we don't
2366 // find it in the support files, we are done.
2367 if (check_inlines)
2368 {
Jim Ingham87df91b2011-09-23 00:54:11 +00002369 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002370 if (file_idx == UINT32_MAX)
2371 continue;
2372 }
2373
2374 if (line != 0)
2375 {
2376 LineTable *line_table = sc.comp_unit->GetLineTable();
2377
2378 if (line_table != NULL && line != 0)
2379 {
2380 // We will have already looked up the file index if
2381 // we are searching for inline entries.
2382 if (!check_inlines)
Jim Ingham87df91b2011-09-23 00:54:11 +00002383 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002384
2385 if (file_idx != UINT32_MAX)
2386 {
2387 uint32_t found_line;
2388 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2389 found_line = sc.line_entry.line;
2390
Greg Clayton016a95e2010-09-14 02:20:48 +00002391 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002392 {
2393 sc.function = NULL;
2394 sc.block = NULL;
2395 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2396 {
2397 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2398 if (file_vm_addr != LLDB_INVALID_ADDRESS)
2399 {
2400 DWARFDebugInfoEntry *function_die = NULL;
2401 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton96d7d742010-11-10 23:42:09 +00002402 curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002403
2404 if (function_die != NULL)
2405 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002406 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002407 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002408 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002409 }
2410
2411 if (sc.function != NULL)
2412 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002413 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002414
2415 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002416 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002417 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002418 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002419 }
2420 }
2421 }
2422
2423 sc_list.Append(sc);
2424 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2425 }
2426 }
2427 }
2428 else if (file_spec_matches_cu_file_spec && !check_inlines)
2429 {
2430 // only append the context if we aren't looking for inline call sites
2431 // by file and line and if the file spec matches that of the compile unit
2432 sc_list.Append(sc);
2433 }
2434 }
2435 else if (file_spec_matches_cu_file_spec && !check_inlines)
2436 {
2437 // only append the context if we aren't looking for inline call sites
2438 // by file and line and if the file spec matches that of the compile unit
2439 sc_list.Append(sc);
2440 }
2441
2442 if (!check_inlines)
2443 break;
2444 }
2445 }
2446 }
2447 }
2448 return sc_list.GetSize() - prev_size;
2449}
2450
2451void
2452SymbolFileDWARF::Index ()
2453{
2454 if (m_indexed)
2455 return;
2456 m_indexed = true;
2457 Timer scoped_timer (__PRETTY_FUNCTION__,
2458 "SymbolFileDWARF::Index (%s)",
2459 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2460
2461 DWARFDebugInfo* debug_info = DebugInfo();
2462 if (debug_info)
2463 {
2464 uint32_t cu_idx = 0;
2465 const uint32_t num_compile_units = GetNumCompileUnits();
2466 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2467 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002468 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002469
Greg Clayton96d7d742010-11-10 23:42:09 +00002470 bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002471
Greg Clayton96d7d742010-11-10 23:42:09 +00002472 curr_cu->Index (cu_idx,
Greg Clayton83c5cd92010-11-14 22:13:40 +00002473 m_function_basename_index,
2474 m_function_fullname_index,
2475 m_function_method_index,
2476 m_function_selector_index,
2477 m_objc_class_selectors_index,
2478 m_global_index,
2479 m_type_index,
Greg Claytond4a2b372011-09-12 23:21:58 +00002480 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002481
2482 // Keep memory down by clearing DIEs if this generate function
2483 // caused them to be parsed
2484 if (clear_dies)
Greg Clayton96d7d742010-11-10 23:42:09 +00002485 curr_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002486 }
2487
Greg Claytond4a2b372011-09-12 23:21:58 +00002488 m_function_basename_index.Finalize();
2489 m_function_fullname_index.Finalize();
2490 m_function_method_index.Finalize();
2491 m_function_selector_index.Finalize();
2492 m_objc_class_selectors_index.Finalize();
2493 m_global_index.Finalize();
2494 m_type_index.Finalize();
2495 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002496
Greg Clayton24739922010-10-13 03:15:28 +00002497#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002498 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002499 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002500 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2501 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002502 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2503 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2504 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2505 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2506 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2507 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002508 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002509 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002510#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002511 }
2512}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002513
2514bool
2515SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2516{
2517 if (namespace_decl == NULL)
2518 {
2519 // Invalid namespace decl which means we aren't matching only things
2520 // in this symbol file, so return true to indicate it matches this
2521 // symbol file.
2522 return true;
2523 }
2524
2525 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2526
2527 if (namespace_ast == NULL)
2528 return true; // No AST in the "namespace_decl", return true since it
2529 // could then match any symbol file, including this one
2530
2531 if (namespace_ast == GetClangASTContext().getASTContext())
2532 return true; // The ASTs match, return true
2533
2534 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002535 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2536
2537 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002538 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002539
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002540 return false;
2541}
2542
Greg Clayton2506a7a2011-10-12 23:34:26 +00002543bool
2544SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2545 DWARFCompileUnit* cu,
2546 const DWARFDebugInfoEntry* die)
2547{
2548 // No namespace specified, so the answesr i
2549 if (namespace_decl == NULL)
2550 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002551
2552 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002553
Greg Clayton2506a7a2011-10-12 23:34:26 +00002554 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2555 if (decl_ctx_die)
2556 {
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002557
Greg Clayton2506a7a2011-10-12 23:34:26 +00002558 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
2559 if (clang_namespace_decl)
2560 {
2561 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002562 {
2563 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002564 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002565 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002566 }
2567
Greg Clayton2506a7a2011-10-12 23:34:26 +00002568 DeclContextToDIEMap::iterator pos = m_decl_ctx_to_die.find(clang_namespace_decl);
2569
2570 if (pos == m_decl_ctx_to_die.end())
Sean Callananebe60672011-10-13 21:50:33 +00002571 {
2572 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002573 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match in a namespace, but its parent is not the requested namespace");
Sean Callananebe60672011-10-13 21:50:33 +00002574
Greg Clayton2506a7a2011-10-12 23:34:26 +00002575 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002576 }
Greg Clayton2506a7a2011-10-12 23:34:26 +00002577
Greg Claytoncb5860a2011-10-13 23:49:28 +00002578 return pos->second.count (decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002579 }
2580 else
2581 {
2582 // We have a namespace_decl that was not NULL but it contained
2583 // a NULL "clang::NamespaceDecl", so this means the global namespace
2584 // So as long the the contained decl context DIE isn't a namespace
2585 // we should be ok.
2586 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2587 return true;
2588 }
2589 }
Sean Callananebe60672011-10-13 21:50:33 +00002590
2591 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002592 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002593
Greg Clayton2506a7a2011-10-12 23:34:26 +00002594 return false;
2595}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002596uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002597SymbolFileDWARF::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 +00002598{
Greg Clayton21f2a492011-10-06 00:09:08 +00002599 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2600
2601 if (log)
2602 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002603 GetObjectFile()->GetModule()->LogMessage (log.get(),
2604 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2605 name.GetCString(),
2606 namespace_decl,
2607 append,
2608 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002609 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002610
2611 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2612 return 0;
2613
Greg Claytonc685f8e2010-09-15 04:15:46 +00002614 DWARFDebugInfo* info = DebugInfo();
2615 if (info == NULL)
2616 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002617
2618 // If we aren't appending the results to this list, then clear the list
2619 if (!append)
2620 variables.Clear();
2621
2622 // Remember how many variables are in the list before we search in case
2623 // we are appending the results to a variable list.
2624 const uint32_t original_size = variables.GetSize();
2625
Greg Claytond4a2b372011-09-12 23:21:58 +00002626 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002627
Greg Clayton97fbc342011-10-20 22:30:33 +00002628 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002629 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002630 if (m_apple_names_ap.get())
2631 {
2632 const char *name_cstr = name.GetCString();
2633 const char *base_name_start;
2634 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002635
Greg Clayton97fbc342011-10-20 22:30:33 +00002636 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2637 base_name_start = name_cstr;
2638
2639 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2640 }
Greg Clayton7f995132011-10-04 22:41:51 +00002641 }
2642 else
2643 {
2644 // Index the DWARF if we haven't already
2645 if (!m_indexed)
2646 Index ();
2647
2648 m_global_index.Find (name, die_offsets);
2649 }
2650
2651 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002652 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002653 {
Greg Clayton7f995132011-10-04 22:41:51 +00002654 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002655 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002656 assert (sc.module_sp);
2657
Greg Claytond4a2b372011-09-12 23:21:58 +00002658 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002659 DWARFCompileUnit* dwarf_cu = NULL;
2660 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002661 for (size_t i=0; i<num_matches; ++i)
2662 {
2663 const dw_offset_t die_offset = die_offsets[i];
2664 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002665
Greg Clayton95d87902011-11-11 03:16:25 +00002666 if (die)
2667 {
2668 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2669 assert(sc.comp_unit != NULL);
2670
2671 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2672 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002673
Greg Clayton95d87902011-11-11 03:16:25 +00002674 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002675
Greg Clayton95d87902011-11-11 03:16:25 +00002676 if (variables.GetSize() - original_size >= max_matches)
2677 break;
2678 }
2679 else
2680 {
2681 if (m_using_apple_tables)
2682 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002683 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2684 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002685 }
2686 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002687 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002688 }
2689
2690 // Return the number of variable that were appended to the list
2691 return variables.GetSize() - original_size;
2692}
2693
2694uint32_t
2695SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2696{
Greg Clayton21f2a492011-10-06 00:09:08 +00002697 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2698
2699 if (log)
2700 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002701 GetObjectFile()->GetModule()->LogMessage (log.get(),
2702 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2703 regex.GetText(),
2704 append,
2705 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002706 }
2707
Greg Claytonc685f8e2010-09-15 04:15:46 +00002708 DWARFDebugInfo* info = DebugInfo();
2709 if (info == NULL)
2710 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002711
2712 // If we aren't appending the results to this list, then clear the list
2713 if (!append)
2714 variables.Clear();
2715
2716 // Remember how many variables are in the list before we search in case
2717 // we are appending the results to a variable list.
2718 const uint32_t original_size = variables.GetSize();
2719
Greg Clayton7f995132011-10-04 22:41:51 +00002720 DIEArray die_offsets;
2721
Greg Clayton97fbc342011-10-20 22:30:33 +00002722 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002723 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002724 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002725 {
2726 DWARFMappedHash::DIEInfoArray hash_data_array;
2727 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2728 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2729 }
Greg Clayton7f995132011-10-04 22:41:51 +00002730 }
2731 else
2732 {
2733 // Index the DWARF if we haven't already
2734 if (!m_indexed)
2735 Index ();
2736
2737 m_global_index.Find (regex, die_offsets);
2738 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002739
Greg Claytonc685f8e2010-09-15 04:15:46 +00002740 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002741 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002742 assert (sc.module_sp);
2743
Greg Claytond4a2b372011-09-12 23:21:58 +00002744 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002745 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002746 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002747 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002748 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002749 DWARFDebugInfo* debug_info = DebugInfo();
2750 for (size_t i=0; i<num_matches; ++i)
2751 {
2752 const dw_offset_t die_offset = die_offsets[i];
2753 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002754
2755 if (die)
2756 {
2757 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002758
Greg Clayton95d87902011-11-11 03:16:25 +00002759 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002760
Greg Clayton95d87902011-11-11 03:16:25 +00002761 if (variables.GetSize() - original_size >= max_matches)
2762 break;
2763 }
2764 else
2765 {
2766 if (m_using_apple_tables)
2767 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002768 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2769 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002770 }
2771 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002772 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002773 }
2774
2775 // Return the number of variable that were appended to the list
2776 return variables.GetSize() - original_size;
2777}
2778
Greg Claytonaa044962011-10-13 00:59:38 +00002779
Jim Ingham4cda6e02011-10-07 22:23:45 +00002780bool
2781SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2782 DWARFCompileUnit *&dwarf_cu,
2783 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002784{
Greg Claytonaa044962011-10-13 00:59:38 +00002785 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2786 return ResolveFunction (dwarf_cu, die, sc_list);
2787}
2788
2789
2790bool
2791SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2792 const DWARFDebugInfoEntry *die,
2793 SymbolContextList& sc_list)
2794{
Greg Clayton9e315582011-09-02 04:03:59 +00002795 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002796
2797 if (die == NULL)
2798 return false;
2799
Jim Ingham4cda6e02011-10-07 22:23:45 +00002800 // If we were passed a die that is not a function, just return false...
2801 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2802 return false;
2803
2804 const DWARFDebugInfoEntry* inlined_die = NULL;
2805 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002806 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002807 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002808
Jim Ingham4cda6e02011-10-07 22:23:45 +00002809 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002810 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002811 if (die->Tag() == DW_TAG_subprogram)
2812 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002813 }
2814 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002815 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002816 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002817 {
2818 Address addr;
2819 // Parse all blocks if needed
2820 if (inlined_die)
2821 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002822 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00002823 assert (sc.block != NULL);
2824 if (sc.block->GetStartAddress (addr) == false)
2825 addr.Clear();
2826 }
2827 else
2828 {
2829 sc.block = NULL;
2830 addr = sc.function->GetAddressRange().GetBaseAddress();
2831 }
2832
2833 if (addr.IsValid())
2834 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002835 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002836 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002837 }
2838 }
2839
Greg Claytonaa044962011-10-13 00:59:38 +00002840 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002841}
2842
Greg Clayton7f995132011-10-04 22:41:51 +00002843void
2844SymbolFileDWARF::FindFunctions (const ConstString &name,
2845 const NameToDIE &name_to_die,
2846 SymbolContextList& sc_list)
2847{
Greg Claytond4a2b372011-09-12 23:21:58 +00002848 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002849 if (name_to_die.Find (name, die_offsets))
2850 {
2851 ParseFunctions (die_offsets, sc_list);
2852 }
2853}
2854
2855
2856void
2857SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2858 const NameToDIE &name_to_die,
2859 SymbolContextList& sc_list)
2860{
2861 DIEArray die_offsets;
2862 if (name_to_die.Find (regex, die_offsets))
2863 {
2864 ParseFunctions (die_offsets, sc_list);
2865 }
2866}
2867
2868
2869void
2870SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2871 const DWARFMappedHash::MemoryTable &memory_table,
2872 SymbolContextList& sc_list)
2873{
2874 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002875 DWARFMappedHash::DIEInfoArray hash_data_array;
2876 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002877 {
Greg Claytond1767f02011-12-08 02:13:16 +00002878 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002879 ParseFunctions (die_offsets, sc_list);
2880 }
2881}
2882
2883void
2884SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2885 SymbolContextList& sc_list)
2886{
2887 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002888 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002889 {
Greg Clayton7f995132011-10-04 22:41:51 +00002890 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00002891
2892 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002893 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002894 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002895 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00002896 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002897 }
2898 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002899}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002900
Jim Ingham4cda6e02011-10-07 22:23:45 +00002901bool
2902SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2903 const DWARFCompileUnit *dwarf_cu,
2904 uint32_t name_type_mask,
2905 const char *partial_name,
2906 const char *base_name_start,
2907 const char *base_name_end)
2908{
2909 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2910 if (name_type_mask == eFunctionNameTypeMethod
2911 || name_type_mask == eFunctionNameTypeBase)
2912 {
Greg Claytonf0705c82011-10-22 03:33:13 +00002913 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2914 if (!containing_decl_ctx)
2915 return false;
2916
2917 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
2918
2919 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2920 return false;
2921 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2922 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002923 }
2924
2925 // Now we need to check whether the name we got back for this type matches the extra specifications
2926 // that were in the name we're looking up:
2927 if (base_name_start != partial_name || *base_name_end != '\0')
2928 {
2929 // First see if the stuff to the left matches the full name. To do that let's see if
2930 // we can pull out the mips linkage name attribute:
2931
2932 Mangled best_name;
2933
2934 DWARFDebugInfoEntry::Attributes attributes;
2935 die->GetAttributes(this, dwarf_cu, NULL, attributes);
2936 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
2937 if (idx != UINT32_MAX)
2938 {
2939 DWARFFormValue form_value;
2940 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
2941 {
2942 const char *name = form_value.AsCString(&get_debug_str_data());
2943 best_name.SetValue (name, true);
2944 }
2945 }
2946 if (best_name)
2947 {
2948 const char *demangled = best_name.GetDemangledName().GetCString();
2949 if (demangled)
2950 {
2951 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00002952 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
2953 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002954 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00002955 else
2956 {
2957 // Sort out the case where our name is something like "Process::Destroy" and the match is
2958 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
2959 // namespace boundaries...
2960
2961 if (partial_name[0] == ':' && partial_name[1] == ':')
2962 {
2963 // The partial name was already on a namespace boundary so all matches are good.
2964 return true;
2965 }
2966 else if (partial_in_demangled == demangled)
2967 {
2968 // They both start the same, so this is an good match.
2969 return true;
2970 }
2971 else
2972 {
2973 if (partial_in_demangled - demangled == 1)
2974 {
2975 // Only one character difference, can't be a namespace boundary...
2976 return false;
2977 }
2978 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
2979 {
2980 // We are on a namespace boundary, so this is also good.
2981 return true;
2982 }
2983 else
2984 return false;
2985 }
2986 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002987 }
2988 }
2989 }
2990
2991 return true;
2992}
Greg Claytonc685f8e2010-09-15 04:15:46 +00002993
Greg Clayton0c5cd902010-06-28 21:30:43 +00002994uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00002995SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00002996 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002997 uint32_t name_type_mask,
2998 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002999 bool append,
3000 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003001{
3002 Timer scoped_timer (__PRETTY_FUNCTION__,
3003 "SymbolFileDWARF::FindFunctions (name = '%s')",
3004 name.AsCString());
3005
Greg Clayton21f2a492011-10-06 00:09:08 +00003006 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3007
3008 if (log)
3009 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003010 GetObjectFile()->GetModule()->LogMessage (log.get(),
3011 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3012 name.GetCString(),
3013 name_type_mask,
3014 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003015 }
3016
Greg Clayton0c5cd902010-06-28 21:30:43 +00003017 // If we aren't appending the results to this list, then clear the list
3018 if (!append)
3019 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003020
3021 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3022 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003023
3024 // If name is empty then we won't find anything.
3025 if (name.IsEmpty())
3026 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003027
3028 // Remember how many sc_list are in the list before we search in case
3029 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003030
Greg Clayton9e315582011-09-02 04:03:59 +00003031 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00003032
Jim Ingham4cda6e02011-10-07 22:23:45 +00003033 const char *name_cstr = name.GetCString();
3034 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
3035 const char *base_name_start = name_cstr;
3036 const char *base_name_end = name_cstr + strlen(name_cstr);
3037
3038 if (name_type_mask & eFunctionNameTypeAuto)
3039 {
3040 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
3041 effective_name_type_mask = eFunctionNameTypeFull;
3042 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
3043 effective_name_type_mask = eFunctionNameTypeFull;
3044 else
3045 {
3046 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3047 effective_name_type_mask |= eFunctionNameTypeSelector;
3048
3049 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3050 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
3051 }
3052 }
3053 else
3054 {
3055 effective_name_type_mask = name_type_mask;
3056 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
3057 {
3058 // If they've asked for a CPP method or function name and it can't be that, we don't
3059 // even need to search for CPP methods or names.
3060 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3061 {
3062 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
3063 if (effective_name_type_mask == eFunctionNameTypeNone)
3064 return 0;
3065 }
3066 }
3067
3068 if (effective_name_type_mask & eFunctionNameTypeSelector)
3069 {
3070 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3071 {
3072 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
3073 if (effective_name_type_mask == eFunctionNameTypeNone)
3074 return 0;
3075 }
3076 }
3077 }
3078
3079 DWARFDebugInfo* info = DebugInfo();
3080 if (info == NULL)
3081 return 0;
3082
Greg Claytonaa044962011-10-13 00:59:38 +00003083 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003084 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003085 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003086 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003087 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003088
3089 DIEArray die_offsets;
3090
3091 uint32_t num_matches = 0;
3092
3093 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003094 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003095 // If they asked for the full name, match what they typed. At some point we may
3096 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3097 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003098 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003099 for (uint32_t i = 0; i < num_matches; i++)
3100 {
Greg Clayton95d87902011-11-11 03:16:25 +00003101 const dw_offset_t die_offset = die_offsets[i];
3102 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003103 if (die)
3104 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003105 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3106 continue;
3107
Sean Callanan9df05fb2012-02-10 22:52:19 +00003108 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3109 continue;
3110
Greg Claytonaa044962011-10-13 00:59:38 +00003111 ResolveFunction (dwarf_cu, die, sc_list);
3112 }
Greg Clayton95d87902011-11-11 03:16:25 +00003113 else
3114 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003115 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3116 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003117 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003118 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003119 }
3120 else
3121 {
3122 if (effective_name_type_mask & eFunctionNameTypeSelector)
3123 {
3124 if (namespace_decl && *namespace_decl)
3125 return 0; // no selectors in namespaces
3126
3127 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3128 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3129 // and if it is an ObjC method name, we're good.
3130
3131 for (uint32_t i = 0; i < num_matches; i++)
3132 {
Greg Clayton95d87902011-11-11 03:16:25 +00003133 const dw_offset_t die_offset = die_offsets[i];
3134 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003135 if (die)
3136 {
3137 const char *die_name = die->GetName(this, dwarf_cu);
3138 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003139 {
3140 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3141 continue;
3142
Greg Clayton97fbc342011-10-20 22:30:33 +00003143 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003144 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003145 }
Greg Clayton95d87902011-11-11 03:16:25 +00003146 else
3147 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003148 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3149 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003150 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003151 }
3152 die_offsets.clear();
3153 }
3154
3155 if (effective_name_type_mask & eFunctionNameTypeMethod
3156 || effective_name_type_mask & eFunctionNameTypeBase)
3157 {
3158 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3159 (namespace_decl && *namespace_decl))
3160 return 0; // no methods in namespaces
3161
3162 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3163 // extract the base name, look that up, and if there is any other information in the name we were
3164 // passed in we have to post-filter based on that.
3165
3166 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3167 std::string base_name(base_name_start, base_name_end - base_name_start);
3168 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3169
3170 for (uint32_t i = 0; i < num_matches; i++)
3171 {
Greg Clayton95d87902011-11-11 03:16:25 +00003172 const dw_offset_t die_offset = die_offsets[i];
3173 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003174 if (die)
3175 {
3176 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3177 continue;
3178
3179 if (!FunctionDieMatchesPartialName(die,
3180 dwarf_cu,
3181 effective_name_type_mask,
3182 name_cstr,
3183 base_name_start,
3184 base_name_end))
3185 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003186
3187 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3188 continue;
Greg Clayton97fbc342011-10-20 22:30:33 +00003189
3190 // If we get to here, the die is good, and we should add it:
3191 ResolveFunction (dwarf_cu, die, sc_list);
3192 }
Greg Clayton95d87902011-11-11 03:16:25 +00003193 else
3194 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003195 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3196 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003197 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003198 }
3199 die_offsets.clear();
3200 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003201 }
3202 }
Greg Clayton7f995132011-10-04 22:41:51 +00003203 }
3204 else
3205 {
3206
3207 // Index the DWARF if we haven't already
3208 if (!m_indexed)
3209 Index ();
3210
Greg Clayton7f995132011-10-04 22:41:51 +00003211 if (name_type_mask & eFunctionNameTypeFull)
3212 FindFunctions (name, m_function_fullname_index, sc_list);
3213
Jim Ingham4cda6e02011-10-07 22:23:45 +00003214 std::string base_name(base_name_start, base_name_end - base_name_start);
3215 ConstString base_name_const(base_name.c_str());
3216 DIEArray die_offsets;
3217 DWARFCompileUnit *dwarf_cu = NULL;
3218
3219 if (effective_name_type_mask & eFunctionNameTypeBase)
3220 {
3221 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003222 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003223 {
Greg Claytonaa044962011-10-13 00:59:38 +00003224 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3225 if (die)
3226 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003227 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3228 continue;
3229
Greg Claytonaa044962011-10-13 00:59:38 +00003230 if (!FunctionDieMatchesPartialName(die,
3231 dwarf_cu,
3232 effective_name_type_mask,
3233 name_cstr,
3234 base_name_start,
3235 base_name_end))
3236 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003237
Sean Callanan9df05fb2012-02-10 22:52:19 +00003238 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3239 continue;
3240
Greg Claytonaa044962011-10-13 00:59:38 +00003241 // If we get to here, the die is good, and we should add it:
3242 ResolveFunction (dwarf_cu, die, sc_list);
3243 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003244 }
3245 die_offsets.clear();
3246 }
3247
Jim Inghamea8005a2011-10-11 01:18:11 +00003248 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003249 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003250 if (namespace_decl && *namespace_decl)
3251 return 0; // no methods in namespaces
3252
Jim Ingham4cda6e02011-10-07 22:23:45 +00003253 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3254 {
Greg Claytonaa044962011-10-13 00:59:38 +00003255 for (uint32_t i = 0; i < num_base; i++)
3256 {
3257 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3258 if (die)
3259 {
3260 if (!FunctionDieMatchesPartialName(die,
3261 dwarf_cu,
3262 effective_name_type_mask,
3263 name_cstr,
3264 base_name_start,
3265 base_name_end))
3266 continue;
3267
Sean Callanan9df05fb2012-02-10 22:52:19 +00003268 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3269 continue;
3270
Greg Claytonaa044962011-10-13 00:59:38 +00003271 // If we get to here, the die is good, and we should add it:
3272 ResolveFunction (dwarf_cu, die, sc_list);
3273 }
3274 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003275 }
3276 die_offsets.clear();
3277 }
Greg Clayton7f995132011-10-04 22:41:51 +00003278
Sean Callanan213fdb82011-10-13 01:49:10 +00003279 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003280 {
Greg Clayton7f995132011-10-04 22:41:51 +00003281 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003282 }
3283
Greg Clayton4d01ace2011-09-29 16:58:15 +00003284 }
3285
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003286 // Return the number of variable that were appended to the list
3287 return sc_list.GetSize() - original_size;
3288}
3289
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003290uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003291SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003292{
3293 Timer scoped_timer (__PRETTY_FUNCTION__,
3294 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3295 regex.GetText());
3296
Greg Clayton21f2a492011-10-06 00:09:08 +00003297 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3298
3299 if (log)
3300 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003301 GetObjectFile()->GetModule()->LogMessage (log.get(),
3302 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3303 regex.GetText(),
3304 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003305 }
3306
3307
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003308 // If we aren't appending the results to this list, then clear the list
3309 if (!append)
3310 sc_list.Clear();
3311
3312 // Remember how many sc_list are in the list before we search in case
3313 // we are appending the results to a variable list.
3314 uint32_t original_size = sc_list.GetSize();
3315
Greg Clayton97fbc342011-10-20 22:30:33 +00003316 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003317 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003318 if (m_apple_names_ap.get())
3319 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003320 }
3321 else
3322 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003323 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003324 if (!m_indexed)
3325 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003326
Greg Clayton7f995132011-10-04 22:41:51 +00003327 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003328
Greg Clayton7f995132011-10-04 22:41:51 +00003329 FindFunctions (regex, m_function_fullname_index, sc_list);
3330 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003331
3332 // Return the number of variable that were appended to the list
3333 return sc_list.GetSize() - original_size;
3334}
Jim Ingham318c9f22011-08-26 19:44:13 +00003335
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003336uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003337SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3338 const ConstString &name,
3339 const lldb_private::ClangNamespaceDecl *namespace_decl,
3340 bool append,
3341 uint32_t max_matches,
3342 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003343{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003344 DWARFDebugInfo* info = DebugInfo();
3345 if (info == NULL)
3346 return 0;
3347
Greg Clayton21f2a492011-10-06 00:09:08 +00003348 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3349
3350 if (log)
3351 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003352 GetObjectFile()->GetModule()->LogMessage (log.get(),
3353 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", append=%u, max_matches=%u, type_list)",
3354 name.GetCString(),
3355 append,
3356 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003357 }
3358
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003359 // If we aren't appending the results to this list, then clear the list
3360 if (!append)
3361 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003362
3363 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3364 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003365
Greg Claytond4a2b372011-09-12 23:21:58 +00003366 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003367
Greg Clayton97fbc342011-10-20 22:30:33 +00003368 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003369 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003370 if (m_apple_types_ap.get())
3371 {
3372 const char *name_cstr = name.GetCString();
3373 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3374 }
Greg Clayton7f995132011-10-04 22:41:51 +00003375 }
3376 else
3377 {
3378 if (!m_indexed)
3379 Index ();
3380
3381 m_type_index.Find (name, die_offsets);
3382 }
3383
Greg Clayton7f995132011-10-04 22:41:51 +00003384 const size_t num_matches = die_offsets.size();
3385
Greg Claytond4a2b372011-09-12 23:21:58 +00003386 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003387 {
Greg Clayton7f995132011-10-04 22:41:51 +00003388 const uint32_t initial_types_size = types.GetSize();
3389 DWARFCompileUnit* dwarf_cu = NULL;
3390 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003391 DWARFDebugInfo* debug_info = DebugInfo();
3392 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003393 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003394 const dw_offset_t die_offset = die_offsets[i];
3395 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3396
Greg Clayton95d87902011-11-11 03:16:25 +00003397 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003398 {
Greg Clayton95d87902011-11-11 03:16:25 +00003399 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3400 continue;
3401
3402 Type *matching_type = ResolveType (dwarf_cu, die);
3403 if (matching_type)
3404 {
3405 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003406 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003407 if (types.GetSize() >= max_matches)
3408 break;
3409 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003410 }
Greg Clayton95d87902011-11-11 03:16:25 +00003411 else
3412 {
3413 if (m_using_apple_tables)
3414 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003415 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3416 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003417 }
3418 }
3419
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003420 }
Greg Clayton7f995132011-10-04 22:41:51 +00003421 return types.GetSize() - initial_types_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003422 }
Greg Clayton7f995132011-10-04 22:41:51 +00003423 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003424}
3425
3426
Greg Clayton526e5af2010-11-13 03:52:47 +00003427ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003428SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003429 const ConstString &name,
3430 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003431{
Greg Clayton21f2a492011-10-06 00:09:08 +00003432 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3433
3434 if (log)
3435 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003436 GetObjectFile()->GetModule()->LogMessage (log.get(),
3437 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3438 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003439 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003440
3441 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3442 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003443
Greg Clayton526e5af2010-11-13 03:52:47 +00003444 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003445 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003446 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003447 {
Greg Clayton7f995132011-10-04 22:41:51 +00003448 DIEArray die_offsets;
3449
Greg Clayton526e5af2010-11-13 03:52:47 +00003450 // Index if we already haven't to make sure the compile units
3451 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003452 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003453 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003454 if (m_apple_namespaces_ap.get())
3455 {
3456 const char *name_cstr = name.GetCString();
3457 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3458 }
Greg Clayton7f995132011-10-04 22:41:51 +00003459 }
3460 else
3461 {
3462 if (!m_indexed)
3463 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003464
Greg Clayton7f995132011-10-04 22:41:51 +00003465 m_namespace_index.Find (name, die_offsets);
3466 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003467
3468 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003469 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003470 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003471 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003472 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003473 DWARFDebugInfo* debug_info = DebugInfo();
3474 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003475 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003476 const dw_offset_t die_offset = die_offsets[i];
3477 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003478
Greg Clayton95d87902011-11-11 03:16:25 +00003479 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003480 {
Greg Clayton95d87902011-11-11 03:16:25 +00003481 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3482 continue;
3483
3484 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3485 if (clang_namespace_decl)
3486 {
3487 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3488 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003489 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003490 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003491 }
Greg Clayton95d87902011-11-11 03:16:25 +00003492 else
3493 {
3494 if (m_using_apple_tables)
3495 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003496 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3497 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003498 }
3499 }
3500
Greg Clayton526e5af2010-11-13 03:52:47 +00003501 }
3502 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003503 }
Greg Clayton526e5af2010-11-13 03:52:47 +00003504 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003505}
3506
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003507uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003508SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003509{
3510 // Remember how many sc_list are in the list before we search in case
3511 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003512 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003513
3514 const uint32_t num_die_offsets = die_offsets.size();
3515 // Parse all of the types we found from the pubtypes matches
3516 uint32_t i;
3517 uint32_t num_matches = 0;
3518 for (i = 0; i < num_die_offsets; ++i)
3519 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003520 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3521 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003522 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003523 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003524 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003525 ++num_matches;
3526 if (num_matches >= max_matches)
3527 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003528 }
3529 }
3530
3531 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003532 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003533}
3534
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003535
3536size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003537SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3538 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003539 DWARFCompileUnit* dwarf_cu,
3540 const DWARFDebugInfoEntry *parent_die,
3541 bool skip_artificial,
3542 bool &is_static,
3543 TypeList* type_list,
3544 std::vector<clang_type_t>& function_param_types,
3545 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003546 unsigned &type_quals,
3547 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003548{
3549 if (parent_die == NULL)
3550 return 0;
3551
Greg Claytond88d7592010-09-15 08:33:30 +00003552 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3553
Greg Clayton7fedea22010-11-16 02:10:54 +00003554 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003555 const DWARFDebugInfoEntry *die;
3556 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3557 {
3558 dw_tag_t tag = die->Tag();
3559 switch (tag)
3560 {
3561 case DW_TAG_formal_parameter:
3562 {
3563 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003564 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003565 if (num_attributes > 0)
3566 {
3567 const char *name = NULL;
3568 Declaration decl;
3569 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003570 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003571 // one of None, Auto, Register, Extern, Static, PrivateExtern
3572
Sean Callanane2ef6e32010-09-23 03:01:22 +00003573 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003574 uint32_t i;
3575 for (i=0; i<num_attributes; ++i)
3576 {
3577 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3578 DWARFFormValue form_value;
3579 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3580 {
3581 switch (attr)
3582 {
3583 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3584 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3585 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3586 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3587 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003588 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003589 case DW_AT_location:
3590 // if (form_value.BlockData())
3591 // {
3592 // const DataExtractor& debug_info_data = debug_info();
3593 // uint32_t block_length = form_value.Unsigned();
3594 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3595 // }
3596 // else
3597 // {
3598 // }
3599 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003600 case DW_AT_const_value:
3601 case DW_AT_default_value:
3602 case DW_AT_description:
3603 case DW_AT_endianity:
3604 case DW_AT_is_optional:
3605 case DW_AT_segment:
3606 case DW_AT_variable_parameter:
3607 default:
3608 case DW_AT_abstract_origin:
3609 case DW_AT_sibling:
3610 break;
3611 }
3612 }
3613 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003614
Greg Clayton0fffff52010-09-24 05:15:53 +00003615 bool skip = false;
3616 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003617 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003618 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003619 {
3620 // In order to determine if a C++ member function is
3621 // "const" we have to look at the const-ness of "this"...
3622 // Ugly, but that
3623 if (arg_idx == 0)
3624 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003625 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003626 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003627 // Often times compilers omit the "this" name for the
3628 // specification DIEs, so we can't rely upon the name
3629 // being in the formal parameter DIE...
3630 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003631 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003632 Type *this_type = ResolveTypeUID (param_type_die_offset);
3633 if (this_type)
3634 {
3635 uint32_t encoding_mask = this_type->GetEncodingMask();
3636 if (encoding_mask & Type::eEncodingIsPointerUID)
3637 {
3638 is_static = false;
3639
3640 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3641 type_quals |= clang::Qualifiers::Const;
3642 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3643 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003644 }
3645 }
3646 }
3647 }
3648 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003649 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003650 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003651 else
3652 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003653
Greg Clayton0fffff52010-09-24 05:15:53 +00003654 // HACK: Objective C formal parameters "self" and "_cmd"
3655 // are not marked as artificial in the DWARF...
Greg Clayton96d7d742010-11-10 23:42:09 +00003656 CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3657 if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Clayton0fffff52010-09-24 05:15:53 +00003658 {
3659 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3660 skip = true;
3661 }
3662 }
3663 }
3664
3665 if (!skip)
3666 {
3667 Type *type = ResolveTypeUID(param_type_die_offset);
3668 if (type)
3669 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003670 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003671
Greg Clayton42ce2f32011-12-12 21:50:19 +00003672 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3673 type->GetClangForwardType(),
3674 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003675 assert(param_var_decl);
3676 function_param_decls.push_back(param_var_decl);
3677 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003678 }
3679 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003680 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003681 }
3682 break;
3683
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003684 case DW_TAG_template_type_parameter:
3685 case DW_TAG_template_value_parameter:
3686 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
3687 break;
3688
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003689 default:
3690 break;
3691 }
3692 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003693 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003694}
3695
3696size_t
3697SymbolFileDWARF::ParseChildEnumerators
3698(
3699 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003700 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003701 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003702 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003703 const DWARFDebugInfoEntry *parent_die
3704)
3705{
3706 if (parent_die == NULL)
3707 return 0;
3708
3709 size_t enumerators_added = 0;
3710 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003711 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3712
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003713 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3714 {
3715 const dw_tag_t tag = die->Tag();
3716 if (tag == DW_TAG_enumerator)
3717 {
3718 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003719 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003720 if (num_child_attributes > 0)
3721 {
3722 const char *name = NULL;
3723 bool got_value = false;
3724 int64_t enum_value = 0;
3725 Declaration decl;
3726
3727 uint32_t i;
3728 for (i=0; i<num_child_attributes; ++i)
3729 {
3730 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3731 DWARFFormValue form_value;
3732 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3733 {
3734 switch (attr)
3735 {
3736 case DW_AT_const_value:
3737 got_value = true;
3738 enum_value = form_value.Unsigned();
3739 break;
3740
3741 case DW_AT_name:
3742 name = form_value.AsCString(&get_debug_str_data());
3743 break;
3744
3745 case DW_AT_description:
3746 default:
3747 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3748 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3749 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3750 case DW_AT_sibling:
3751 break;
3752 }
3753 }
3754 }
3755
3756 if (name && name[0] && got_value)
3757 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003758 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3759 enumerator_clang_type,
3760 decl,
3761 name,
3762 enum_value,
3763 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003764 ++enumerators_added;
3765 }
3766 }
3767 }
3768 }
3769 return enumerators_added;
3770}
3771
3772void
3773SymbolFileDWARF::ParseChildArrayInfo
3774(
3775 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003776 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003777 const DWARFDebugInfoEntry *parent_die,
3778 int64_t& first_index,
3779 std::vector<uint64_t>& element_orders,
3780 uint32_t& byte_stride,
3781 uint32_t& bit_stride
3782)
3783{
3784 if (parent_die == NULL)
3785 return;
3786
3787 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003788 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003789 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3790 {
3791 const dw_tag_t tag = die->Tag();
3792 switch (tag)
3793 {
3794 case DW_TAG_enumerator:
3795 {
3796 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003797 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003798 if (num_child_attributes > 0)
3799 {
3800 const char *name = NULL;
3801 bool got_value = false;
3802 int64_t enum_value = 0;
3803
3804 uint32_t i;
3805 for (i=0; i<num_child_attributes; ++i)
3806 {
3807 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3808 DWARFFormValue form_value;
3809 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3810 {
3811 switch (attr)
3812 {
3813 case DW_AT_const_value:
3814 got_value = true;
3815 enum_value = form_value.Unsigned();
3816 break;
3817
3818 case DW_AT_name:
3819 name = form_value.AsCString(&get_debug_str_data());
3820 break;
3821
3822 case DW_AT_description:
3823 default:
3824 case DW_AT_decl_file:
3825 case DW_AT_decl_line:
3826 case DW_AT_decl_column:
3827 case DW_AT_sibling:
3828 break;
3829 }
3830 }
3831 }
3832 }
3833 }
3834 break;
3835
3836 case DW_TAG_subrange_type:
3837 {
3838 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003839 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003840 if (num_child_attributes > 0)
3841 {
3842 const char *name = NULL;
3843 bool got_value = false;
3844 uint64_t byte_size = 0;
3845 int64_t enum_value = 0;
3846 uint64_t num_elements = 0;
3847 uint64_t lower_bound = 0;
3848 uint64_t upper_bound = 0;
3849 uint32_t i;
3850 for (i=0; i<num_child_attributes; ++i)
3851 {
3852 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3853 DWARFFormValue form_value;
3854 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3855 {
3856 switch (attr)
3857 {
3858 case DW_AT_const_value:
3859 got_value = true;
3860 enum_value = form_value.Unsigned();
3861 break;
3862
3863 case DW_AT_name:
3864 name = form_value.AsCString(&get_debug_str_data());
3865 break;
3866
3867 case DW_AT_count:
3868 num_elements = form_value.Unsigned();
3869 break;
3870
3871 case DW_AT_bit_stride:
3872 bit_stride = form_value.Unsigned();
3873 break;
3874
3875 case DW_AT_byte_stride:
3876 byte_stride = form_value.Unsigned();
3877 break;
3878
3879 case DW_AT_byte_size:
3880 byte_size = form_value.Unsigned();
3881 break;
3882
3883 case DW_AT_lower_bound:
3884 lower_bound = form_value.Unsigned();
3885 break;
3886
3887 case DW_AT_upper_bound:
3888 upper_bound = form_value.Unsigned();
3889 break;
3890
3891 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892 case DW_AT_abstract_origin:
3893 case DW_AT_accessibility:
3894 case DW_AT_allocated:
3895 case DW_AT_associated:
3896 case DW_AT_data_location:
3897 case DW_AT_declaration:
3898 case DW_AT_description:
3899 case DW_AT_sibling:
3900 case DW_AT_threads_scaled:
3901 case DW_AT_type:
3902 case DW_AT_visibility:
3903 break;
3904 }
3905 }
3906 }
3907
3908 if (upper_bound > lower_bound)
3909 num_elements = upper_bound - lower_bound + 1;
3910
3911 if (num_elements > 0)
3912 element_orders.push_back (num_elements);
3913 }
3914 }
3915 break;
3916 }
3917 }
3918}
3919
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003920TypeSP
Greg Clayton96d7d742010-11-10 23:42:09 +00003921SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003922{
3923 TypeSP type_sp;
3924 if (die != NULL)
3925 {
Greg Clayton96d7d742010-11-10 23:42:09 +00003926 assert(curr_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00003927 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003928 if (type_ptr == NULL)
3929 {
Greg Claytonca512b32011-01-14 04:54:56 +00003930 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu);
3931 assert (lldb_cu);
3932 SymbolContext sc(lldb_cu);
Greg Clayton96d7d742010-11-10 23:42:09 +00003933 type_sp = ParseType(sc, curr_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003934 }
3935 else if (type_ptr != DIE_IS_BEING_PARSED)
3936 {
3937 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00003938 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003939 }
3940
3941 }
3942 return type_sp;
3943}
3944
3945clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00003946SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003947{
3948 if (die_offset != DW_INVALID_OFFSET)
3949 {
3950 DWARFCompileUnitSP cu_sp;
3951 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00003952 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003953 }
3954 return NULL;
3955}
3956
Sean Callanan72e49402011-08-05 23:43:37 +00003957clang::DeclContext *
3958SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
3959{
3960 if (die_offset != DW_INVALID_OFFSET)
3961 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00003962 DWARFDebugInfo* debug_info = DebugInfo();
3963 if (debug_info)
3964 {
3965 DWARFCompileUnitSP cu_sp;
3966 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
3967 if (die)
3968 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
3969 }
Sean Callanan72e49402011-08-05 23:43:37 +00003970 }
3971 return NULL;
3972}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003973
Greg Clayton96d7d742010-11-10 23:42:09 +00003974clang::NamespaceDecl *
3975SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
3976{
Greg Clayton030a2042011-10-14 21:34:45 +00003977 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00003978 {
Greg Clayton030a2042011-10-14 21:34:45 +00003979 // See if we already parsed this namespace DIE and associated it with a
3980 // uniqued namespace declaration
3981 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
3982 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003983 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00003984 else
3985 {
3986 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00003987 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (curr_cu, die, NULL);
3988 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
3989 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3990 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00003991 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00003992 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00003993 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003994 GetObjectFile()->GetModule()->LogMessage (log.get(),
3995 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
3996 GetClangASTContext().getASTContext(),
3997 MakeUserID(die->GetOffset()),
3998 namespace_name,
3999 namespace_decl,
4000 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00004001 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004002 else
4003 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004004 GetObjectFile()->GetModule()->LogMessage (log.get(),
4005 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
4006 GetClangASTContext().getASTContext(),
4007 MakeUserID(die->GetOffset()),
4008 namespace_decl,
4009 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00004010 }
Greg Clayton030a2042011-10-14 21:34:45 +00004011 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004012
4013 if (namespace_decl)
4014 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4015 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004016 }
4017 }
4018 return NULL;
4019}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004020
4021clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004022SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004023{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004024 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4025 if (clang_decl_ctx)
4026 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004027 // If this DIE has a specification, or an abstract origin, then trace to those.
4028
Greg Claytoncab36a32011-12-08 05:16:30 +00004029 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004030 if (die_offset != DW_INVALID_OFFSET)
4031 return GetClangDeclContextForDIEOffset (sc, die_offset);
4032
Greg Claytoncab36a32011-12-08 05:16:30 +00004033 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004034 if (die_offset != DW_INVALID_OFFSET)
4035 return GetClangDeclContextForDIEOffset (sc, die_offset);
4036
Greg Clayton3bffb082011-12-10 02:15:28 +00004037 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4038 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004039 GetObjectFile()->GetModule()->LogMessage(log.get(), "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 +00004040 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004041 bool assert_not_being_parsed = true;
4042 ResolveTypeUID (cu, die, assert_not_being_parsed);
4043
Greg Clayton5cf58b92011-10-05 22:22:08 +00004044 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4045
4046 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004047}
4048
4049clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004050SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004051{
Greg Claytonca512b32011-01-14 04:54:56 +00004052 if (m_clang_tu_decl == NULL)
4053 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004054
Greg Clayton2bc22f82011-09-30 03:20:47 +00004055 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004056
4057 if (decl_ctx_die_copy)
4058 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004059
4060 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004061 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004062
Greg Clayton2bc22f82011-09-30 03:20:47 +00004063 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4064 if (pos != m_die_to_decl_ctx.end())
4065 return pos->second;
4066
4067 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004068 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004069 case DW_TAG_compile_unit:
4070 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004071
Greg Clayton2bc22f82011-09-30 03:20:47 +00004072 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004073 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004074 break;
4075
4076 case DW_TAG_structure_type:
4077 case DW_TAG_union_type:
4078 case DW_TAG_class_type:
4079 {
4080 Type* type = ResolveType (cu, decl_ctx_die);
4081 if (type)
4082 {
4083 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4084 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004085 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004086 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4087 if (decl_ctx)
4088 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004089 }
4090 }
Greg Claytonca512b32011-01-14 04:54:56 +00004091 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004092 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004093
Greg Clayton2bc22f82011-09-30 03:20:47 +00004094 default:
4095 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004096 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004097 }
Greg Clayton7a345282010-11-09 23:46:37 +00004098 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004099}
4100
Greg Clayton2bc22f82011-09-30 03:20:47 +00004101
4102const DWARFDebugInfoEntry *
4103SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4104{
4105 if (cu && die)
4106 {
4107 const DWARFDebugInfoEntry * const decl_die = die;
4108
4109 while (die != NULL)
4110 {
4111 // If this is the original DIE that we are searching for a declaration
4112 // for, then don't look in the cache as we don't want our own decl
4113 // context to be our decl context...
4114 if (decl_die != die)
4115 {
4116 switch (die->Tag())
4117 {
4118 case DW_TAG_compile_unit:
4119 case DW_TAG_namespace:
4120 case DW_TAG_structure_type:
4121 case DW_TAG_union_type:
4122 case DW_TAG_class_type:
4123 return die;
4124
4125 default:
4126 break;
4127 }
4128 }
4129
4130 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4131 if (die_offset != DW_INVALID_OFFSET)
4132 {
4133 DWARFCompileUnit *spec_cu = cu;
4134 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4135 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4136 if (spec_die_decl_ctx_die)
4137 return spec_die_decl_ctx_die;
4138 }
4139
4140 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4141 if (die_offset != DW_INVALID_OFFSET)
4142 {
4143 DWARFCompileUnit *abs_cu = cu;
4144 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4145 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4146 if (abs_die_decl_ctx_die)
4147 return abs_die_decl_ctx_die;
4148 }
4149
4150 die = die->GetParent();
4151 }
4152 }
4153 return NULL;
4154}
4155
4156
Greg Clayton901c5ca2011-12-03 04:40:03 +00004157Symbol *
4158SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4159{
4160 Symbol *objc_class_symbol = NULL;
4161 if (m_obj_file)
4162 {
4163 Symtab *symtab = m_obj_file->GetSymtab();
4164 if (symtab)
4165 {
4166 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4167 eSymbolTypeObjCClass,
4168 Symtab::eDebugNo,
4169 Symtab::eVisibilityAny);
4170 }
4171 }
4172 return objc_class_symbol;
4173}
4174
Greg Claytonc7f03b62012-01-12 04:33:28 +00004175// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4176// then we can end up looking through all class types for a complete type and never find
4177// the full definition. We need to know if this attribute is supported, so we determine
4178// this here and cache th result. We also need to worry about the debug map DWARF file
4179// if we are doing darwin DWARF in .o file debugging.
4180bool
4181SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4182{
4183 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4184 {
4185 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4186 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4187 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4188 else
4189 {
4190 DWARFDebugInfo* debug_info = DebugInfo();
4191 const uint32_t num_compile_units = GetNumCompileUnits();
4192 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4193 {
4194 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4195 if (curr_cu != cu && curr_cu->Supports_DW_AT_APPLE_objc_complete_type())
4196 {
4197 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4198 break;
4199 }
4200 }
4201 }
4202 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && m_debug_map_symfile)
4203 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4204 }
4205 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4206}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004207
4208// This function can be used when a DIE is found that is a forward declaration
4209// DIE and we want to try and find a type that has the complete definition.
4210TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004211SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4212 const ConstString &type_name,
4213 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004214{
4215
4216 TypeSP type_sp;
4217
Greg Claytonc7f03b62012-01-12 04:33:28 +00004218 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004219 return type_sp;
4220
4221 DIEArray die_offsets;
4222
4223 if (m_using_apple_tables)
4224 {
4225 if (m_apple_types_ap.get())
4226 {
4227 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004228 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004229 }
4230 }
4231 else
4232 {
4233 if (!m_indexed)
4234 Index ();
4235
4236 m_type_index.Find (type_name, die_offsets);
4237 }
4238
Greg Clayton901c5ca2011-12-03 04:40:03 +00004239 const size_t num_matches = die_offsets.size();
4240
Greg Clayton901c5ca2011-12-03 04:40:03 +00004241 DWARFCompileUnit* type_cu = NULL;
4242 const DWARFDebugInfoEntry* type_die = NULL;
4243 if (num_matches)
4244 {
4245 DWARFDebugInfo* debug_info = DebugInfo();
4246 for (size_t i=0; i<num_matches; ++i)
4247 {
4248 const dw_offset_t die_offset = die_offsets[i];
4249 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4250
4251 if (type_die)
4252 {
4253 bool try_resolving_type = false;
4254
4255 // Don't try and resolve the DIE we are looking for with the DIE itself!
4256 if (type_die != die)
4257 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004258 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004259 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004260 case DW_TAG_class_type:
4261 case DW_TAG_structure_type:
4262 try_resolving_type = true;
4263 break;
4264 default:
4265 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004266 }
4267 }
4268
4269 if (try_resolving_type)
4270 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004271 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004272 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004273
4274 if (try_resolving_type)
4275 {
4276 Type *resolved_type = ResolveType (type_cu, type_die, false);
4277 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4278 {
4279 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4280 MakeUserID(die->GetOffset()),
4281 MakeUserID(curr_cu->GetOffset()),
4282 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4283 MakeUserID(type_die->GetOffset()),
4284 MakeUserID(type_cu->GetOffset()));
4285
Greg Claytonc7f03b62012-01-12 04:33:28 +00004286 if (die)
4287 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004288 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004289 break;
4290 }
4291 }
4292 }
4293 }
4294 else
4295 {
4296 if (m_using_apple_tables)
4297 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004298 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4299 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004300 }
4301 }
4302
4303 }
4304 }
4305 return type_sp;
4306}
4307
Greg Clayton80c26302012-02-05 06:12:47 +00004308//----------------------------------------------------------------------
4309// This function helps to ensure that the declaration contexts match for
4310// two different DIEs. Often times debug information will refer to a
4311// forward declaration of a type (the equivalent of "struct my_struct;".
4312// There will often be a declaration of that type elsewhere that has the
4313// full definition. When we go looking for the full type "my_struct", we
4314// will find one or more matches in the accelerator tables and we will
4315// then need to make sure the type was in the same declaration context
4316// as the original DIE. This function can efficiently compare two DIEs
4317// and will return true when the declaration context matches, and false
4318// when they don't.
4319//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004320bool
4321SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4322 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4323{
4324 assert (die1 != die2);
4325 DWARFDIECollection decl_ctx_1;
4326 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004327 //The declaration DIE stack is a stack of the declaration context
4328 // DIEs all the way back to the compile unit. If a type "T" is
4329 // declared inside a class "B", and class "B" is declared inside
4330 // a class "A" and class "A" is in a namespace "lldb", and the
4331 // namespace is in a compile unit, there will be a stack of DIEs:
4332 //
4333 // [0] DW_TAG_class_type for "B"
4334 // [1] DW_TAG_class_type for "A"
4335 // [2] DW_TAG_namespace for "lldb"
4336 // [3] DW_TAG_compile_unit for the source file.
4337 //
4338 // We grab both contexts and make sure that everything matches
4339 // all the way back to the compiler unit.
4340
4341 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004342 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004343 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004344 // Make sure the context arrays have the same size, otherwise
4345 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004346 const size_t count1 = decl_ctx_1.Size();
4347 const size_t count2 = decl_ctx_2.Size();
4348 if (count1 != count2)
4349 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004350
4351 // Make sure the DW_TAG values match all the way back up the the
4352 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004353 const DWARFDebugInfoEntry *decl_ctx_die1;
4354 const DWARFDebugInfoEntry *decl_ctx_die2;
4355 size_t i;
4356 for (i=0; i<count1; i++)
4357 {
4358 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4359 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4360 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4361 return false;
4362 }
Greg Clayton890ff562012-02-02 05:48:16 +00004363#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004364
4365 // Make sure the top item in the decl context die array is always
4366 // DW_TAG_compile_unit. If it isn't then something went wrong in
4367 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004368 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004369
Greg Clayton890ff562012-02-02 05:48:16 +00004370#endif
4371 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004372 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004373 for (i=0; i<count1 - 1; i++)
4374 {
4375 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4376 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4377 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004378 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004379 // If the string was from a DW_FORM_strp, then the pointer will often
4380 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004381 if (name1 == name2)
4382 continue;
4383
4384 // Name pointers are not equal, so only compare the strings
4385 // if both are not NULL.
4386 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004387 {
Greg Clayton5569e642012-02-06 01:44:54 +00004388 // If the strings don't compare, we are done...
4389 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004390 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004391 }
4392 else
4393 {
4394 // One name was NULL while the other wasn't
4395 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004396 }
4397 }
Greg Clayton80c26302012-02-05 06:12:47 +00004398 // We made it through all of the checks and the declaration contexts
4399 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004400 return true;
4401}
Greg Clayton220a0072011-12-09 08:48:30 +00004402
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004403// This function can be used when a DIE is found that is a forward declaration
4404// DIE and we want to try and find a type that has the complete definition.
4405TypeSP
Greg Clayton7f995132011-10-04 22:41:51 +00004406SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
4407 const DWARFDebugInfoEntry *die,
4408 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004409{
4410 TypeSP type_sp;
4411
Greg Clayton1a65ae12011-01-25 23:55:37 +00004412 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004413 return type_sp;
4414
Greg Clayton7f995132011-10-04 22:41:51 +00004415 DIEArray die_offsets;
4416
Greg Clayton97fbc342011-10-20 22:30:33 +00004417 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004418 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004419 if (m_apple_types_ap.get())
4420 {
Greg Claytond1767f02011-12-08 02:13:16 +00004421 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4422 {
Greg Claytonae920b62012-01-06 00:17:16 +00004423 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004424 }
4425 else
4426 {
4427 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4428 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004429 }
Greg Clayton7f995132011-10-04 22:41:51 +00004430 }
4431 else
4432 {
4433 if (!m_indexed)
4434 Index ();
4435
4436 m_type_index.Find (type_name, die_offsets);
4437 }
Greg Clayton7f995132011-10-04 22:41:51 +00004438
4439 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004440
Greg Clayton934cb052011-12-03 00:27:05 +00004441 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004442
4443 DWARFCompileUnit* type_cu = NULL;
4444 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004445 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004446 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004447 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004448 for (size_t i=0; i<num_matches; ++i)
4449 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004450 const dw_offset_t die_offset = die_offsets[i];
4451 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004452
Greg Clayton95d87902011-11-11 03:16:25 +00004453 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004454 {
Greg Clayton934cb052011-12-03 00:27:05 +00004455 bool try_resolving_type = false;
4456
4457 // Don't try and resolve the DIE we are looking for with the DIE itself!
4458 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004459 {
Greg Clayton934cb052011-12-03 00:27:05 +00004460 const dw_tag_t type_die_tag = type_die->Tag();
4461 // Make sure the tags match
4462 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004463 {
Greg Clayton934cb052011-12-03 00:27:05 +00004464 // The tags match, lets try resolving this type
4465 try_resolving_type = true;
4466 }
4467 else
4468 {
4469 // The tags don't match, but we need to watch our for a
4470 // forward declaration for a struct and ("struct foo")
4471 // ends up being a class ("class foo { ... };") or
4472 // vice versa.
4473 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004474 {
Greg Clayton934cb052011-12-03 00:27:05 +00004475 case DW_TAG_class_type:
4476 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4477 try_resolving_type = (die_tag == DW_TAG_structure_type);
4478 break;
4479 case DW_TAG_structure_type:
4480 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4481 try_resolving_type = (die_tag == DW_TAG_class_type);
4482 break;
4483 default:
4484 // Tags don't match, don't event try to resolve
4485 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004486 break;
4487 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004488 }
4489 }
Greg Clayton934cb052011-12-03 00:27:05 +00004490
4491 if (try_resolving_type)
4492 {
Greg Clayton890ff562012-02-02 05:48:16 +00004493 // Make sure the decl contexts match all the way up
4494 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004495 {
Greg Clayton890ff562012-02-02 05:48:16 +00004496 Type *resolved_type = ResolveType (type_cu, type_die, false);
4497 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4498 {
4499 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4500 MakeUserID(die->GetOffset()),
4501 MakeUserID(curr_cu->GetOffset()),
4502 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4503 MakeUserID(type_die->GetOffset()),
4504 MakeUserID(type_cu->GetOffset()));
4505
4506 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004507 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004508 break;
4509 }
Greg Clayton934cb052011-12-03 00:27:05 +00004510 }
4511 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004512 }
Greg Clayton95d87902011-11-11 03:16:25 +00004513 else
4514 {
4515 if (m_using_apple_tables)
4516 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004517 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4518 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004519 }
4520 }
4521
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004522 }
4523 }
4524 return type_sp;
4525}
4526
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004527TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00004528SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004529{
4530 TypeSP type_sp;
4531
Greg Clayton1be10fc2010-09-29 01:12:09 +00004532 if (type_is_new_ptr)
4533 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00004534
4535#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4536 static DIEStack g_die_stack;
4537 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
4538#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00004539
Sean Callananc7fbf732010-08-06 00:32:49 +00004540 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004541 if (die != NULL)
4542 {
Greg Clayton21f2a492011-10-06 00:09:08 +00004543 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00004544 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00004545 {
4546 const DWARFDebugInfoEntry *context_die;
4547 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
4548
Greg Clayton1fba87112012-02-09 20:03:49 +00004549 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
Sean Callanan5b26f272012-02-04 08:49:35 +00004550 die->GetOffset(),
4551 context,
4552 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00004553 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00004554 die->GetName(this, dwarf_cu));
4555
4556#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4557 scoped_die_logger.Push (dwarf_cu, die);
4558 g_die_stack.LogDIEs(log.get(), this);
4559#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00004560 }
Greg Clayton3bffb082011-12-10 02:15:28 +00004561//
4562// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4563// if (log && dwarf_cu)
4564// {
4565// StreamString s;
4566// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00004567// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00004568//
4569// }
Jim Ingham16746d12011-08-25 23:21:43 +00004570
Greg Clayton594e5ed2010-09-27 21:07:38 +00004571 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004572 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00004573 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004574 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004575 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00004576 if (type_is_new_ptr)
4577 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004578
Greg Clayton594e5ed2010-09-27 21:07:38 +00004579 const dw_tag_t tag = die->Tag();
4580
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004581 bool is_forward_declaration = false;
4582 DWARFDebugInfoEntry::Attributes attributes;
4583 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00004584 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00004585 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
4586 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00004587 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00004588 Declaration decl;
4589
Greg Clayton4957bf62010-09-30 21:49:03 +00004590 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00004591 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004592
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004593 dw_attr_t attr;
4594
4595 switch (tag)
4596 {
4597 case DW_TAG_base_type:
4598 case DW_TAG_pointer_type:
4599 case DW_TAG_reference_type:
4600 case DW_TAG_typedef:
4601 case DW_TAG_const_type:
4602 case DW_TAG_restrict_type:
4603 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00004604 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004605 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004606 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004607 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004608
Greg Claytond88d7592010-09-15 08:33:30 +00004609 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004610 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004611 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
4612
4613 if (num_attributes > 0)
4614 {
4615 uint32_t i;
4616 for (i=0; i<num_attributes; ++i)
4617 {
4618 attr = attributes.AttributeAtIndex(i);
4619 DWARFFormValue form_value;
4620 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4621 {
4622 switch (attr)
4623 {
4624 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4625 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4626 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4627 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00004628
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004629 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00004630 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
4631 // include the "&"...
4632 if (tag == DW_TAG_reference_type)
4633 {
4634 if (strchr (type_name_cstr, '&') == NULL)
4635 type_name_cstr = NULL;
4636 }
4637 if (type_name_cstr)
4638 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004639 break;
Greg Clayton36909642011-03-15 04:38:20 +00004640 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004641 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
4642 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
4643 default:
4644 case DW_AT_sibling:
4645 break;
4646 }
4647 }
4648 }
4649 }
4650
Greg Clayton81c22f62011-10-19 18:09:39 +00004651 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\") type => 0x%8.8x\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
Greg Claytonc93237c2010-10-01 20:48:32 +00004652
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004653 switch (tag)
4654 {
4655 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00004656 break;
4657
Greg Claytond4436412011-10-28 21:00:00 +00004658 case DW_TAG_unspecified_type:
4659 if (strcmp(type_name_cstr, "nullptr_t") == 0)
4660 {
4661 resolve_state = Type::eResolveStateFull;
4662 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
4663 break;
4664 }
4665 // Fall through to base type below in case we can handle the type there...
4666
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004667 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00004668 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004669 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
4670 encoding,
4671 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004672 break;
4673
Greg Clayton526e5af2010-11-13 03:52:47 +00004674 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
4675 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
4676 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
4677 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
4678 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
4679 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004680 }
4681
Greg Claytonc7f03b62012-01-12 04:33:28 +00004682 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004683 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004684 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
4685 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004686 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004687 static ConstString g_objc_type_name_id("id");
4688 static ConstString g_objc_type_name_Class("Class");
4689 static ConstString g_objc_type_name_selector("SEL");
4690
4691 if (type_name_const_str == g_objc_type_name_id)
4692 {
4693 if (log)
4694 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
4695 die->GetOffset(),
4696 DW_TAG_value_to_name(die->Tag()),
4697 die->GetName(this, dwarf_cu));
4698 clang_type = ast.GetBuiltInType_objc_id();
4699 encoding_data_type = Type::eEncodingIsUID;
4700 encoding_uid = LLDB_INVALID_UID;
4701 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00004702
Greg Claytonc7f03b62012-01-12 04:33:28 +00004703 }
4704 else if (type_name_const_str == g_objc_type_name_Class)
4705 {
4706 if (log)
4707 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
4708 die->GetOffset(),
4709 DW_TAG_value_to_name(die->Tag()),
4710 die->GetName(this, dwarf_cu));
4711 clang_type = ast.GetBuiltInType_objc_Class();
4712 encoding_data_type = Type::eEncodingIsUID;
4713 encoding_uid = LLDB_INVALID_UID;
4714 resolve_state = Type::eResolveStateFull;
4715 }
4716 else if (type_name_const_str == g_objc_type_name_selector)
4717 {
4718 if (log)
4719 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
4720 die->GetOffset(),
4721 DW_TAG_value_to_name(die->Tag()),
4722 die->GetName(this, dwarf_cu));
4723 clang_type = ast.GetBuiltInType_objc_selector();
4724 encoding_data_type = Type::eEncodingIsUID;
4725 encoding_uid = LLDB_INVALID_UID;
4726 resolve_state = Type::eResolveStateFull;
4727 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004728 }
4729 }
4730
Greg Clayton81c22f62011-10-19 18:09:39 +00004731 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004732 this,
4733 type_name_const_str,
4734 byte_size,
4735 NULL,
4736 encoding_uid,
4737 encoding_data_type,
4738 &decl,
4739 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004740 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004741
Greg Clayton594e5ed2010-09-27 21:07:38 +00004742 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004743
4744// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
4745// if (encoding_type != NULL)
4746// {
4747// if (encoding_type != DIE_IS_BEING_PARSED)
4748// type_sp->SetEncodingType(encoding_type);
4749// else
4750// m_indirect_fixups.push_back(type_sp.get());
4751// }
4752 }
4753 break;
4754
4755 case DW_TAG_structure_type:
4756 case DW_TAG_union_type:
4757 case DW_TAG_class_type:
4758 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004759 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004760 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004761
Greg Clayton9e409562010-07-28 02:04:09 +00004762 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00004763 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004764 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00004765 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004766 if (num_attributes > 0)
4767 {
4768 uint32_t i;
4769 for (i=0; i<num_attributes; ++i)
4770 {
4771 attr = attributes.AttributeAtIndex(i);
4772 DWARFFormValue form_value;
4773 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4774 {
4775 switch (attr)
4776 {
Greg Clayton9e409562010-07-28 02:04:09 +00004777 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00004778 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
4779 {
4780 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
4781 // point to the compile unit file, so we clear this invalid value
4782 // so that we can still unique types efficiently.
4783 decl.SetFile(FileSpec ("<invalid>", false));
4784 }
4785 else
4786 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00004787 break;
4788
4789 case DW_AT_decl_line:
4790 decl.SetLine(form_value.Unsigned());
4791 break;
4792
4793 case DW_AT_decl_column:
4794 decl.SetColumn(form_value.Unsigned());
4795 break;
4796
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004797 case DW_AT_name:
4798 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004799 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004800 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004801
4802 case DW_AT_byte_size:
4803 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00004804 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004805 break;
4806
4807 case DW_AT_accessibility:
4808 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
4809 break;
4810
4811 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00004812 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00004813 break;
4814
4815 case DW_AT_APPLE_runtime_class:
4816 class_language = (LanguageType)form_value.Signed();
4817 break;
4818
Greg Clayton18774842011-11-29 23:40:34 +00004819 case DW_AT_APPLE_objc_complete_type:
4820 is_complete_objc_class = form_value.Signed();
4821 break;
4822
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004823 case DW_AT_allocated:
4824 case DW_AT_associated:
4825 case DW_AT_data_location:
4826 case DW_AT_description:
4827 case DW_AT_start_scope:
4828 case DW_AT_visibility:
4829 default:
4830 case DW_AT_sibling:
4831 break;
4832 }
4833 }
4834 }
4835 }
4836
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004837 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00004838
Greg Clayton123edca2012-03-02 00:07:15 +00004839 // Only try and unique the type if it has a name.
4840 if (type_name_const_str &&
4841 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00004842 this,
4843 dwarf_cu,
4844 die,
4845 decl,
4846 byte_size_valid ? byte_size : -1,
4847 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004848 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00004849 // We have already parsed this type or from another
4850 // compile unit. GCC loves to use the "one definition
4851 // rule" which can result in multiple definitions
4852 // of the same class over and over in each compile
4853 // unit.
4854 type_sp = unique_ast_entry.m_type_sp;
4855 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00004856 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00004857 m_die_to_type[die] = type_sp.get();
4858 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004859 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004860 }
4861
Greg Clayton81c22f62011-10-19 18:09:39 +00004862 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004863
4864 int tag_decl_kind = -1;
4865 AccessType default_accessibility = eAccessNone;
4866 if (tag == DW_TAG_structure_type)
4867 {
4868 tag_decl_kind = clang::TTK_Struct;
4869 default_accessibility = eAccessPublic;
4870 }
4871 else if (tag == DW_TAG_union_type)
4872 {
4873 tag_decl_kind = clang::TTK_Union;
4874 default_accessibility = eAccessPublic;
4875 }
4876 else if (tag == DW_TAG_class_type)
4877 {
4878 tag_decl_kind = clang::TTK_Class;
4879 default_accessibility = eAccessPrivate;
4880 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00004881
4882 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
4883 die->HasChildren() == false &&
4884 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
4885 {
4886 // Work around an issue with clang at the moment where
4887 // forward declarations for objective C classes are emitted
4888 // as:
4889 // DW_TAG_structure_type [2]
4890 // DW_AT_name( "ForwardObjcClass" )
4891 // DW_AT_byte_size( 0x00 )
4892 // DW_AT_decl_file( "..." )
4893 // DW_AT_decl_line( 1 )
4894 //
4895 // Note that there is no DW_AT_declaration and there are
4896 // no children, and the byte size is zero.
4897 is_forward_declaration = true;
4898 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004899
Greg Clayton18774842011-11-29 23:40:34 +00004900 if (class_language == eLanguageTypeObjC)
4901 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004902 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004903 {
4904 // We have a valid eSymbolTypeObjCClass class symbol whose
4905 // name matches the current objective C class that we
4906 // are trying to find and this DIE isn't the complete
4907 // definition (we checked is_complete_objc_class above and
4908 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00004909 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004910
Greg Clayton901c5ca2011-12-03 04:40:03 +00004911 if (!type_sp && m_debug_map_symfile)
4912 {
4913 // We weren't able to find a full declaration in
4914 // this DWARF, see if we have a declaration anywhere
4915 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00004916 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004917 }
4918
4919 if (type_sp)
4920 {
4921 if (log)
4922 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004923 GetObjectFile()->GetModule()->LogMessage (log.get(),
4924 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
4925 this,
4926 die->GetOffset(),
4927 DW_TAG_value_to_name(tag),
4928 type_name_cstr,
4929 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004930 }
4931
4932 // We found a real definition for this type elsewhere
4933 // so lets use it and cache the fact that we found
4934 // a complete type for this die
4935 m_die_to_type[die] = type_sp.get();
4936 return type_sp;
4937 }
4938 }
4939 }
4940
4941
4942 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004943 {
4944 // We have a forward declaration to a type and we need
4945 // to try and find a full declaration. We look in the
4946 // current type index just in case we have a forward
4947 // declaration followed by an actual declarations in the
4948 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00004949 if (log)
4950 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004951 GetObjectFile()->GetModule()->LogMessage (log.get(),
4952 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
4953 this,
4954 die->GetOffset(),
4955 DW_TAG_value_to_name(tag),
4956 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00004957 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004958
4959 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
4960
4961 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00004962 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004963 // We weren't able to find a full declaration in
4964 // this DWARF, see if we have a declaration anywhere
4965 // else...
4966 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
Greg Clayton4272cc72011-02-02 02:24:04 +00004967 }
4968
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004969 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00004970 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00004971 if (log)
4972 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004973 GetObjectFile()->GetModule()->LogMessage (log.get(),
4974 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
4975 this,
4976 die->GetOffset(),
4977 DW_TAG_value_to_name(tag),
4978 type_name_cstr,
4979 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00004980 }
4981
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004982 // We found a real definition for this type elsewhere
4983 // so lets use it and cache the fact that we found
4984 // a complete type for this die
4985 m_die_to_type[die] = type_sp.get();
4986 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004987 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004988 }
4989 assert (tag_decl_kind != -1);
4990 bool clang_type_was_created = false;
4991 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
4992 if (clang_type == NULL)
4993 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00004994 const DWARFDebugInfoEntry *decl_ctx_die;
4995
4996 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00004997 if (accessibility == eAccessNone && decl_ctx)
4998 {
4999 // Check the decl context that contains this class/struct/union.
5000 // If it is a class we must give it an accessability.
5001 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
5002 if (DeclKindIsCXXClass (containing_decl_kind))
5003 accessibility = default_accessibility;
5004 }
5005
Greg Claytonf0705c82011-10-22 03:33:13 +00005006 if (type_name_cstr && strchr (type_name_cstr, '<'))
5007 {
5008 ClangASTContext::TemplateParameterInfos template_param_infos;
5009 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
5010 {
5011 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00005012 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00005013 type_name_cstr,
5014 tag_decl_kind,
5015 template_param_infos);
5016
5017 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
5018 class_template_decl,
5019 tag_decl_kind,
5020 template_param_infos);
5021 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
5022 clang_type_was_created = true;
5023 }
5024 }
5025
5026 if (!clang_type_was_created)
5027 {
5028 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00005029 clang_type = ast.CreateRecordType (decl_ctx,
5030 accessibility,
5031 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00005032 tag_decl_kind,
Greg Claytonf0705c82011-10-22 03:33:13 +00005033 class_language);
5034 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005035 }
5036
5037 // Store a forward declaration to this class type in case any
5038 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00005039 // types for function prototypes.
5040 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00005041 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005042 this,
5043 type_name_const_str,
5044 byte_size,
5045 NULL,
5046 LLDB_INVALID_UID,
5047 Type::eEncodingIsUID,
5048 &decl,
5049 clang_type,
5050 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00005051
5052 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005053
5054
5055 // Add our type to the unique type map so we don't
5056 // end up creating many copies of the same type over
5057 // and over in the ASTContext for our module
5058 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00005059 unique_ast_entry.m_symfile = this;
5060 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005061 unique_ast_entry.m_die = die;
5062 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00005063 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00005064 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
5065 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005066
Sean Callanan12014a02011-12-08 23:45:45 +00005067 if (!is_forward_declaration)
5068 {
5069 if (die->HasChildren() == false)
5070 {
5071 // No children for this struct/union/class, lets finish it
5072 ast.StartTagDeclarationDefinition (clang_type);
5073 ast.CompleteTagDeclarationDefinition (clang_type);
5074 }
5075 else if (clang_type_was_created)
5076 {
5077 // Leave this as a forward declaration until we need
5078 // to know the details of the type. lldb_private::Type
5079 // will automatically call the SymbolFile virtual function
5080 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
5081 // When the definition needs to be defined.
5082 m_forward_decl_die_to_clang_type[die] = clang_type;
5083 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
5084 ClangASTContext::SetHasExternalStorage (clang_type, true);
5085 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005086 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005087
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005088 }
5089 break;
5090
5091 case DW_TAG_enumeration_type:
5092 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005093 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005094 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005095
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005096 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005097
Greg Claytond88d7592010-09-15 08:33:30 +00005098 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005099 if (num_attributes > 0)
5100 {
5101 uint32_t i;
5102
5103 for (i=0; i<num_attributes; ++i)
5104 {
5105 attr = attributes.AttributeAtIndex(i);
5106 DWARFFormValue form_value;
5107 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5108 {
5109 switch (attr)
5110 {
Greg Clayton7a345282010-11-09 23:46:37 +00005111 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5112 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5113 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005114 case DW_AT_name:
5115 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005116 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005117 break;
Greg Clayton7a345282010-11-09 23:46:37 +00005118 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005119 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00005120 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5121 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005122 case DW_AT_allocated:
5123 case DW_AT_associated:
5124 case DW_AT_bit_stride:
5125 case DW_AT_byte_stride:
5126 case DW_AT_data_location:
5127 case DW_AT_description:
5128 case DW_AT_start_scope:
5129 case DW_AT_visibility:
5130 case DW_AT_specification:
5131 case DW_AT_abstract_origin:
5132 case DW_AT_sibling:
5133 break;
5134 }
5135 }
5136 }
5137
Greg Clayton81c22f62011-10-19 18:09:39 +00005138 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00005139
Greg Clayton1be10fc2010-09-29 01:12:09 +00005140 clang_type_t enumerator_clang_type = NULL;
5141 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5142 if (clang_type == NULL)
5143 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005144 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
5145 DW_ATE_signed,
5146 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00005147 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00005148 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00005149 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005150 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00005151 }
5152 else
5153 {
5154 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
5155 assert (enumerator_clang_type != NULL);
5156 }
5157
Greg Claytona2721472011-06-25 00:44:06 +00005158 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
5159
Greg Clayton81c22f62011-10-19 18:09:39 +00005160 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005161 this,
5162 type_name_const_str,
5163 byte_size,
5164 NULL,
5165 encoding_uid,
5166 Type::eEncodingIsUID,
5167 &decl,
5168 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005169 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005170
Greg Clayton6beaaa62011-01-17 03:46:26 +00005171 ast.StartTagDeclarationDefinition (clang_type);
5172 if (die->HasChildren())
5173 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005174 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
5175 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005176 }
5177 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005178 }
5179 }
5180 break;
5181
Jim Inghamb0be4422010-08-12 01:20:14 +00005182 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005183 case DW_TAG_subprogram:
5184 case DW_TAG_subroutine_type:
5185 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005186 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005187 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005188
5189 const char *mangled = NULL;
5190 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00005191 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005192 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00005193 bool is_static = false;
5194 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00005195 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00005196 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00005197 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
5198 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00005199
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005200 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00005201 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005202
5203
Greg Claytond88d7592010-09-15 08:33:30 +00005204 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005205 if (num_attributes > 0)
5206 {
5207 uint32_t i;
5208 for (i=0; i<num_attributes; ++i)
5209 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005210 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005211 DWARFFormValue form_value;
5212 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5213 {
5214 switch (attr)
5215 {
5216 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5217 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5218 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5219 case DW_AT_name:
5220 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005221 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005222 break;
5223
5224 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
5225 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005226 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005227 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00005228 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
5229 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00005230 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00005231 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5232
Greg Claytonf51de672010-10-01 02:31:07 +00005233
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005234 case DW_AT_external:
5235 if (form_value.Unsigned())
5236 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00005237 if (storage == clang::SC_None)
5238 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005239 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00005240 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005241 }
5242 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005243
Greg Clayton72da3972011-08-16 18:40:23 +00005244 case DW_AT_specification:
5245 specification_die_offset = form_value.Reference(dwarf_cu);
5246 break;
5247
5248 case DW_AT_abstract_origin:
5249 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
5250 break;
5251
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005252 case DW_AT_allocated:
5253 case DW_AT_associated:
5254 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005255 case DW_AT_calling_convention:
5256 case DW_AT_data_location:
5257 case DW_AT_elemental:
5258 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005259 case DW_AT_frame_base:
5260 case DW_AT_high_pc:
5261 case DW_AT_low_pc:
5262 case DW_AT_object_pointer:
5263 case DW_AT_prototyped:
5264 case DW_AT_pure:
5265 case DW_AT_ranges:
5266 case DW_AT_recursive:
5267 case DW_AT_return_addr:
5268 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005269 case DW_AT_start_scope:
5270 case DW_AT_static_link:
5271 case DW_AT_trampoline:
5272 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005273 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005274 case DW_AT_description:
5275 case DW_AT_sibling:
5276 break;
5277 }
5278 }
5279 }
Greg Clayton24739922010-10-13 03:15:28 +00005280 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005281
Greg Clayton81c22f62011-10-19 18:09:39 +00005282 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00005283
Greg Clayton24739922010-10-13 03:15:28 +00005284 clang_type_t return_clang_type = NULL;
5285 Type *func_type = NULL;
5286
5287 if (type_die_offset != DW_INVALID_OFFSET)
5288 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00005289
Greg Clayton24739922010-10-13 03:15:28 +00005290 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00005291 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00005292 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005293 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005294
Greg Claytonf51de672010-10-01 02:31:07 +00005295
Greg Clayton24739922010-10-13 03:15:28 +00005296 std::vector<clang_type_t> function_param_types;
5297 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005298
Greg Clayton24739922010-10-13 03:15:28 +00005299 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00005300
Greg Claytoncb5860a2011-10-13 23:49:28 +00005301 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
5302 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00005303 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
5304
Greg Claytonf0705c82011-10-22 03:33:13 +00005305 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00005306 // Start off static. This will be set to false in ParseChildParameters(...)
5307 // if we find a "this" paramters as the first parameter
5308 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00005309 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005310 ClangASTContext::TemplateParameterInfos template_param_infos;
5311
Greg Clayton24739922010-10-13 03:15:28 +00005312 if (die->HasChildren())
5313 {
Greg Clayton0fffff52010-09-24 05:15:53 +00005314 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005315 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00005316 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005317 dwarf_cu,
5318 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00005319 skip_artificial,
5320 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005321 type_list,
5322 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00005323 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005324 type_quals,
5325 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00005326 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005327
Greg Clayton24739922010-10-13 03:15:28 +00005328 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005329 clang_type = ast.CreateFunctionType (return_clang_type,
5330 &function_param_types[0],
5331 function_param_types.size(),
5332 is_variadic,
5333 type_quals);
5334
Greg Clayton24739922010-10-13 03:15:28 +00005335 if (type_name_cstr)
5336 {
5337 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00005338 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005339 {
Greg Clayton7c810422012-01-18 23:40:49 +00005340 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00005341 ConstString class_name_no_category;
5342 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00005343 {
Greg Claytone42ae842012-01-19 03:24:53 +00005344 // Use the class name with no category if there is one
5345 if (class_name_no_category)
5346 class_name = class_name_no_category;
5347
Greg Clayton24739922010-10-13 03:15:28 +00005348 SymbolContext empty_sc;
5349 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00005350 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00005351 {
Greg Clayton24739922010-10-13 03:15:28 +00005352 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00005353 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00005354
5355 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00005356 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005357 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
5358 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
5359 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00005360 }
Greg Clayton24739922010-10-13 03:15:28 +00005361 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005362
Greg Clayton24739922010-10-13 03:15:28 +00005363 if (class_opaque_type)
5364 {
5365 // If accessibility isn't set to anything valid, assume public for
5366 // now...
5367 if (accessibility == eAccessNone)
5368 accessibility = eAccessPublic;
5369
5370 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005371 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
5372 type_name_cstr,
5373 clang_type,
5374 accessibility);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005375 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton24739922010-10-13 03:15:28 +00005376 type_handled = objc_method_decl != NULL;
5377 }
5378 }
Greg Clayton5113dc82011-08-12 06:47:54 +00005379 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00005380 {
5381 // Look at the parent of this DIE and see if is is
5382 // a class or struct and see if this is actually a
5383 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00005384 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00005385 if (class_type)
5386 {
Greg Clayton72da3972011-08-16 18:40:23 +00005387 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00005388 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005389 // We have a specification which we are going to base our function
5390 // prototype off of, so we need this type to be completed so that the
5391 // m_die_to_decl_ctx for the method in the specification has a valid
5392 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005393 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00005394 // If we have a specification, then the function type should have been
5395 // made with the specification and not with this die.
5396 DWARFCompileUnitSP spec_cu_sp;
5397 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005398 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005399 if (spec_clang_decl_ctx)
5400 {
5401 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
5402 }
5403 else
Jim Inghamc1663042011-09-29 22:12:35 +00005404 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005405 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
5406 MakeUserID(die->GetOffset()),
5407 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005408 }
Greg Clayton72da3972011-08-16 18:40:23 +00005409 type_handled = true;
5410 }
5411 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
5412 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005413 // We have a specification which we are going to base our function
5414 // prototype off of, so we need this type to be completed so that the
5415 // m_die_to_decl_ctx for the method in the abstract origin has a valid
5416 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005417 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00005418
Greg Clayton72da3972011-08-16 18:40:23 +00005419 DWARFCompileUnitSP abs_cu_sp;
5420 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005421 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005422 if (abs_clang_decl_ctx)
5423 {
5424 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
5425 }
5426 else
Jim Inghamc1663042011-09-29 22:12:35 +00005427 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005428 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
5429 MakeUserID(die->GetOffset()),
5430 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005431 }
Greg Clayton72da3972011-08-16 18:40:23 +00005432 type_handled = true;
5433 }
5434 else
5435 {
5436 clang_type_t class_opaque_type = class_type->GetClangForwardType();
5437 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00005438 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005439 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00005440 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005441 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
5442 // in the DWARF for C++ methods... Default to public for now...
5443 if (accessibility == eAccessNone)
5444 accessibility = eAccessPublic;
5445
5446 if (!is_static && !die->HasChildren())
5447 {
5448 // We have a C++ member function with no children (this pointer!)
5449 // and clang will get mad if we try and make a function that isn't
5450 // well formed in the DWARF, so we will just skip it...
5451 type_handled = true;
5452 }
5453 else
5454 {
5455 clang::CXXMethodDecl *cxx_method_decl;
5456 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00005457 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8llx from %s/%s",
Greg Clayton20568dd2011-10-13 23:13:20 +00005458 type_name_cstr,
5459 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00005460 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00005461 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
5462 m_obj_file->GetFileSpec().GetFilename().GetCString());
5463
Sean Callananc1b732d2011-11-01 18:07:13 +00005464 const bool is_attr_used = false;
5465
Greg Clayton20568dd2011-10-13 23:13:20 +00005466 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
5467 type_name_cstr,
5468 clang_type,
5469 accessibility,
5470 is_virtual,
5471 is_static,
5472 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00005473 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00005474 is_attr_used,
5475 is_artificial);
Greg Clayton20568dd2011-10-13 23:13:20 +00005476 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
5477
Greg Claytonf49e65a2011-11-10 18:31:53 +00005478 Host::SetCrashDescription (NULL);
5479
Greg Clayton20568dd2011-10-13 23:13:20 +00005480 type_handled = cxx_method_decl != NULL;
5481 }
Greg Clayton72da3972011-08-16 18:40:23 +00005482 }
5483 else
5484 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005485 // We were asked to parse the type for a method in a class, yet the
5486 // class hasn't been asked to complete itself through the
5487 // clang::ExternalASTSource protocol, so we need to just have the
5488 // class complete itself and do things the right way, then our
5489 // DIE should then have an entry in the m_die_to_type map. First
5490 // we need to modify the m_die_to_type so it doesn't think we are
5491 // trying to parse this DIE anymore...
5492 m_die_to_type[die] = NULL;
5493
5494 // Now we get the full type to force our class type to complete itself
5495 // using the clang::ExternalASTSource protocol which will parse all
5496 // base classes and all methods (including the method for this DIE).
5497 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005498
Greg Clayton20568dd2011-10-13 23:13:20 +00005499 // The type for this DIE should have been filled in the function call above
5500 type_ptr = m_die_to_type[die];
5501 if (type_ptr)
5502 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00005503 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00005504 break;
5505 }
Greg Clayton72da3972011-08-16 18:40:23 +00005506 }
Greg Clayton931180e2011-01-27 06:44:37 +00005507 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005508 }
5509 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005510 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005511 }
Greg Clayton24739922010-10-13 03:15:28 +00005512
5513 if (!type_handled)
5514 {
5515 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00005516 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
5517 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005518 clang_type,
5519 storage,
5520 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005521
5522// if (template_param_infos.GetSize() > 0)
5523// {
5524// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
5525// function_decl,
5526// type_name_cstr,
5527// template_param_infos);
5528//
5529// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
5530// func_template_decl,
5531// template_param_infos);
5532// }
Greg Clayton24739922010-10-13 03:15:28 +00005533 // Add the decl to our DIE to decl context map
5534 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00005535 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00005536 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005537 ast.SetFunctionParameters (function_decl,
5538 &function_param_decls.front(),
5539 function_param_decls.size());
Greg Clayton24739922010-10-13 03:15:28 +00005540 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005541 }
Greg Clayton81c22f62011-10-19 18:09:39 +00005542 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005543 this,
5544 type_name_const_str,
5545 0,
5546 NULL,
5547 LLDB_INVALID_UID,
5548 Type::eEncodingIsUID,
5549 &decl,
5550 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005551 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00005552 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005553 }
5554 break;
5555
5556 case DW_TAG_array_type:
5557 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005558 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005559 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005560
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005561 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005562 int64_t first_index = 0;
5563 uint32_t byte_stride = 0;
5564 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00005565 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005566
5567 if (num_attributes > 0)
5568 {
5569 uint32_t i;
5570 for (i=0; i<num_attributes; ++i)
5571 {
5572 attr = attributes.AttributeAtIndex(i);
5573 DWARFFormValue form_value;
5574 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5575 {
5576 switch (attr)
5577 {
5578 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5579 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5580 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5581 case DW_AT_name:
5582 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005583 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005584 break;
5585
5586 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005587 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005588 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
5589 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005590 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005591 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005592 case DW_AT_allocated:
5593 case DW_AT_associated:
5594 case DW_AT_data_location:
5595 case DW_AT_description:
5596 case DW_AT_ordering:
5597 case DW_AT_start_scope:
5598 case DW_AT_visibility:
5599 case DW_AT_specification:
5600 case DW_AT_abstract_origin:
5601 case DW_AT_sibling:
5602 break;
5603 }
5604 }
5605 }
5606
Greg Clayton81c22f62011-10-19 18:09:39 +00005607 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00005608
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005609 Type *element_type = ResolveTypeUID(type_die_offset);
5610
5611 if (element_type)
5612 {
5613 std::vector<uint64_t> element_orders;
5614 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00005615 // We have an array that claims to have no members, lets give it at least one member...
5616 if (element_orders.empty())
5617 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005618 if (byte_stride == 0 && bit_stride == 0)
5619 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00005620 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005621 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
5622 uint64_t num_elements = 0;
5623 std::vector<uint64_t>::const_reverse_iterator pos;
5624 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
5625 for (pos = element_orders.rbegin(); pos != end; ++pos)
5626 {
5627 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005628 clang_type = ast.CreateArrayType (array_element_type,
5629 num_elements,
5630 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005631 array_element_type = clang_type;
5632 array_element_bit_stride = array_element_bit_stride * num_elements;
5633 }
5634 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00005635 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005636 this,
5637 empty_name,
5638 array_element_bit_stride / 8,
5639 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00005640 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005641 Type::eEncodingIsUID,
5642 &decl,
5643 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005644 Type::eResolveStateFull));
5645 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005646 }
5647 }
5648 }
5649 break;
5650
Greg Clayton9b81a312010-06-12 01:20:30 +00005651 case DW_TAG_ptr_to_member_type:
5652 {
5653 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
5654 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
5655
Greg Claytond88d7592010-09-15 08:33:30 +00005656 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00005657
5658 if (num_attributes > 0) {
5659 uint32_t i;
5660 for (i=0; i<num_attributes; ++i)
5661 {
5662 attr = attributes.AttributeAtIndex(i);
5663 DWARFFormValue form_value;
5664 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5665 {
5666 switch (attr)
5667 {
5668 case DW_AT_type:
5669 type_die_offset = form_value.Reference(dwarf_cu); break;
5670 case DW_AT_containing_type:
5671 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
5672 }
5673 }
5674 }
5675
5676 Type *pointee_type = ResolveTypeUID(type_die_offset);
5677 Type *class_type = ResolveTypeUID(containing_type_die_offset);
5678
Greg Clayton526e5af2010-11-13 03:52:47 +00005679 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
5680 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00005681
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005682 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
5683 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00005684
Greg Clayton526e5af2010-11-13 03:52:47 +00005685 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
5686 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00005687
Greg Clayton81c22f62011-10-19 18:09:39 +00005688 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005689 this,
5690 type_name_const_str,
5691 byte_size,
5692 NULL,
5693 LLDB_INVALID_UID,
5694 Type::eEncodingIsUID,
5695 NULL,
5696 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005697 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00005698 }
5699
5700 break;
5701 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005702 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00005703 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005704 break;
5705 }
5706
5707 if (type_sp.get())
5708 {
5709 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5710 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
5711
5712 SymbolContextScope * symbol_context_scope = NULL;
5713 if (sc_parent_tag == DW_TAG_compile_unit)
5714 {
5715 symbol_context_scope = sc.comp_unit;
5716 }
5717 else if (sc.function != NULL)
5718 {
Greg Clayton81c22f62011-10-19 18:09:39 +00005719 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005720 if (symbol_context_scope == NULL)
5721 symbol_context_scope = sc.function;
5722 }
5723
5724 if (symbol_context_scope != NULL)
5725 {
5726 type_sp->SetSymbolContextScope(symbol_context_scope);
5727 }
5728
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005729 // We are ready to put this type into the uniqued list up at the module level
5730 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00005731
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005732 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005733 }
5734 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00005735 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005736 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00005737 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005738 }
5739 }
5740 return type_sp;
5741}
5742
5743size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00005744SymbolFileDWARF::ParseTypes
5745(
5746 const SymbolContext& sc,
5747 DWARFCompileUnit* dwarf_cu,
5748 const DWARFDebugInfoEntry *die,
5749 bool parse_siblings,
5750 bool parse_children
5751)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005752{
5753 size_t types_added = 0;
5754 while (die != NULL)
5755 {
5756 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005757 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005758 {
5759 if (type_is_new)
5760 ++types_added;
5761 }
5762
5763 if (parse_children && die->HasChildren())
5764 {
5765 if (die->Tag() == DW_TAG_subprogram)
5766 {
5767 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00005768 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005769 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
5770 }
5771 else
5772 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
5773 }
5774
5775 if (parse_siblings)
5776 die = die->GetSibling();
5777 else
5778 die = NULL;
5779 }
5780 return types_added;
5781}
5782
5783
5784size_t
5785SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
5786{
5787 assert(sc.comp_unit && sc.function);
5788 size_t functions_added = 0;
5789 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5790 if (dwarf_cu)
5791 {
5792 dw_offset_t function_die_offset = sc.function->GetID();
5793 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
5794 if (function_die)
5795 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00005796 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005797 }
5798 }
5799
5800 return functions_added;
5801}
5802
5803
5804size_t
5805SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
5806{
5807 // At least a compile unit must be valid
5808 assert(sc.comp_unit);
5809 size_t types_added = 0;
5810 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5811 if (dwarf_cu)
5812 {
5813 if (sc.function)
5814 {
5815 dw_offset_t function_die_offset = sc.function->GetID();
5816 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
5817 if (func_die && func_die->HasChildren())
5818 {
5819 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
5820 }
5821 }
5822 else
5823 {
5824 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
5825 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
5826 {
5827 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
5828 }
5829 }
5830 }
5831
5832 return types_added;
5833}
5834
5835size_t
5836SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
5837{
5838 if (sc.comp_unit != NULL)
5839 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00005840 DWARFDebugInfo* info = DebugInfo();
5841 if (info == NULL)
5842 return 0;
5843
5844 uint32_t cu_idx = UINT32_MAX;
5845 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005846
5847 if (dwarf_cu == NULL)
5848 return 0;
5849
5850 if (sc.function)
5851 {
5852 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00005853
5854 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005855 if (func_lo_pc != DW_INVALID_ADDRESS)
5856 {
5857 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00005858
Greg Claytone38a5ed2012-01-05 03:57:59 +00005859 // Let all blocks know they have parse all their variables
5860 sc.function->GetBlock (false).SetDidParseVariables (true, true);
5861 return num_variables;
5862 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005863 }
5864 else if (sc.comp_unit)
5865 {
5866 uint32_t vars_added = 0;
5867 VariableListSP variables (sc.comp_unit->GetVariableList(false));
5868
5869 if (variables.get() == NULL)
5870 {
5871 variables.reset(new VariableList());
5872 sc.comp_unit->SetVariableList(variables);
5873
Greg Claytond4a2b372011-09-12 23:21:58 +00005874 DWARFCompileUnit* match_dwarf_cu = NULL;
5875 const DWARFDebugInfoEntry* die = NULL;
5876 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00005877 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00005878 {
Greg Clayton97fbc342011-10-20 22:30:33 +00005879 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00005880 {
5881 DWARFMappedHash::DIEInfoArray hash_data_array;
5882 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
5883 dwarf_cu->GetNextCompileUnitOffset(),
5884 hash_data_array))
5885 {
5886 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
5887 }
5888 }
Greg Clayton7f995132011-10-04 22:41:51 +00005889 }
5890 else
5891 {
5892 // Index if we already haven't to make sure the compile units
5893 // get indexed and make their global DIE index list
5894 if (!m_indexed)
5895 Index ();
5896
5897 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
5898 dwarf_cu->GetNextCompileUnitOffset(),
5899 die_offsets);
5900 }
5901
5902 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00005903 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005904 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005905 DWARFDebugInfo* debug_info = DebugInfo();
5906 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005907 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005908 const dw_offset_t die_offset = die_offsets[i];
5909 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00005910 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00005911 {
Greg Clayton95d87902011-11-11 03:16:25 +00005912 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
5913 if (var_sp)
5914 {
5915 variables->AddVariableIfUnique (var_sp);
5916 ++vars_added;
5917 }
Greg Claytond4a2b372011-09-12 23:21:58 +00005918 }
Greg Clayton95d87902011-11-11 03:16:25 +00005919 else
5920 {
5921 if (m_using_apple_tables)
5922 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005923 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 +00005924 }
5925 }
5926
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005927 }
5928 }
5929 }
5930 return vars_added;
5931 }
5932 }
5933 return 0;
5934}
5935
5936
5937VariableSP
5938SymbolFileDWARF::ParseVariableDIE
5939(
5940 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00005941 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00005942 const DWARFDebugInfoEntry *die,
5943 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005944)
5945{
5946
Greg Clayton83c5cd92010-11-14 22:13:40 +00005947 VariableSP var_sp (m_die_to_variable_sp[die]);
5948 if (var_sp)
5949 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005950
5951 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00005952
5953 if ((tag == DW_TAG_variable) ||
5954 (tag == DW_TAG_constant) ||
5955 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005956 {
Greg Clayton7f995132011-10-04 22:41:51 +00005957 DWARFDebugInfoEntry::Attributes attributes;
5958 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
5959 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005960 {
Greg Clayton7f995132011-10-04 22:41:51 +00005961 const char *name = NULL;
5962 const char *mangled = NULL;
5963 Declaration decl;
5964 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00005965 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00005966 DWARFExpression location;
5967 bool is_external = false;
5968 bool is_artificial = false;
5969 bool location_is_const_value_data = false;
5970 AccessType accessibility = eAccessNone;
5971
5972 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005973 {
Greg Clayton7f995132011-10-04 22:41:51 +00005974 dw_attr_t attr = attributes.AttributeAtIndex(i);
5975 DWARFFormValue form_value;
5976 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005977 {
Greg Clayton7f995132011-10-04 22:41:51 +00005978 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005979 {
Greg Clayton7f995132011-10-04 22:41:51 +00005980 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5981 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5982 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5983 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
5984 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00005985 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00005986 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
5987 case DW_AT_const_value:
5988 location_is_const_value_data = true;
5989 // Fall through...
5990 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005991 {
Greg Clayton7f995132011-10-04 22:41:51 +00005992 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005993 {
Greg Clayton7f995132011-10-04 22:41:51 +00005994 const DataExtractor& debug_info_data = get_debug_info_data();
5995
5996 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
5997 uint32_t block_length = form_value.Unsigned();
5998 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
5999 }
6000 else
6001 {
6002 const DataExtractor& debug_loc_data = get_debug_loc_data();
6003 const dw_offset_t debug_loc_offset = form_value.Unsigned();
6004
6005 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
6006 if (loc_list_length > 0)
6007 {
6008 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
6009 assert (func_low_pc != LLDB_INVALID_ADDRESS);
6010 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
6011 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006012 }
6013 }
Greg Clayton7f995132011-10-04 22:41:51 +00006014 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006015
Greg Clayton7f995132011-10-04 22:41:51 +00006016 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
6017 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
6018 case DW_AT_declaration:
6019 case DW_AT_description:
6020 case DW_AT_endianity:
6021 case DW_AT_segment:
6022 case DW_AT_start_scope:
6023 case DW_AT_visibility:
6024 default:
6025 case DW_AT_abstract_origin:
6026 case DW_AT_sibling:
6027 case DW_AT_specification:
6028 break;
6029 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006030 }
6031 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006032
Greg Clayton7f995132011-10-04 22:41:51 +00006033 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006034 {
Greg Clayton7f995132011-10-04 22:41:51 +00006035 ValueType scope = eValueTypeInvalid;
6036
6037 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6038 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006039 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00006040
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006041 // DWARF doesn't specify if a DW_TAG_variable is a local, global
6042 // or static variable, so we have to do a little digging by
6043 // looking at the location of a varaible to see if it contains
6044 // a DW_OP_addr opcode _somewhere_ in the definition. I say
6045 // somewhere because clang likes to combine small global variables
6046 // into the same symbol and have locations like:
6047 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
6048 // So if we don't have a DW_TAG_formal_parameter, we can look at
6049 // the location to see if it contains a DW_OP_addr opcode, and
6050 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00006051 if (tag == DW_TAG_formal_parameter)
6052 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00006053 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006054 {
Greg Clayton96c09682012-01-04 22:56:43 +00006055 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00006056 // Check if the location has a DW_OP_addr with any address value...
6057 addr_t location_has_op_addr = false;
6058 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00006059 {
6060 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
6061 if (op_error)
6062 {
6063 StreamString strm;
6064 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006065 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 +00006066 }
6067 }
Greg Claytond1767f02011-12-08 02:13:16 +00006068
6069 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006070 {
Greg Claytond1767f02011-12-08 02:13:16 +00006071 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006072 {
Greg Claytond1767f02011-12-08 02:13:16 +00006073 scope = eValueTypeVariableGlobal;
6074
6075 if (m_debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006076 {
Greg Claytond1767f02011-12-08 02:13:16 +00006077 // When leaving the DWARF in the .o files on darwin,
6078 // when we have a global variable that wasn't initialized,
6079 // the .o file might not have allocated a virtual
6080 // address for the global variable. In this case it will
6081 // have created a symbol for the global variable
6082 // that is undefined and external and the value will
6083 // be the byte size of the variable. When we do the
6084 // address map in SymbolFileDWARFDebugMap we rely on
6085 // having an address, we need to do some magic here
6086 // so we can get the correct address for our global
6087 // variable. The address for all of these entries
6088 // will be zero, and there will be an undefined symbol
6089 // in this object file, and the executable will have
6090 // a matching symbol with a good address. So here we
6091 // dig up the correct address and replace it in the
6092 // location for the variable, and set the variable's
6093 // symbol context scope to be that of the main executable
6094 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00006095 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006096 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006097
Greg Claytond1767f02011-12-08 02:13:16 +00006098 // we have a possible uninitialized extern global
6099 Symtab *symtab = m_obj_file->GetSymtab();
6100 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006101 {
Greg Claytond1767f02011-12-08 02:13:16 +00006102 ConstString const_name(name);
6103 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
6104 eSymbolTypeUndefined,
6105 Symtab::eDebugNo,
6106 Symtab::eVisibilityExtern);
6107
6108 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006109 {
Greg Claytond1767f02011-12-08 02:13:16 +00006110 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
6111 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006112 {
Greg Claytond1767f02011-12-08 02:13:16 +00006113 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
6114 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
6115 eSymbolTypeData,
6116 Symtab::eDebugYes,
6117 Symtab::eVisibilityExtern);
6118 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006119 {
Greg Claytone7612132012-03-07 21:03:09 +00006120 if (defined_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006121 {
Greg Claytone7612132012-03-07 21:03:09 +00006122 const addr_t defined_addr = defined_symbol->GetAddress().GetFileAddress();
Greg Claytond1767f02011-12-08 02:13:16 +00006123 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006124 {
Greg Claytond1767f02011-12-08 02:13:16 +00006125 if (location.Update_DW_OP_addr (defined_addr))
6126 {
6127 symbol_context_scope = defined_symbol;
6128 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006129 }
6130 }
6131 }
6132 }
6133 }
6134 }
6135 }
6136 }
6137 }
Greg Claytond1767f02011-12-08 02:13:16 +00006138 else
6139 {
6140 scope = eValueTypeVariableStatic;
6141 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006142 }
6143 else
Greg Claytond1767f02011-12-08 02:13:16 +00006144 {
6145 scope = eValueTypeVariableLocal;
6146 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006147 }
Greg Clayton7f995132011-10-04 22:41:51 +00006148
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006149 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00006150 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006151 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00006152 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006153 case DW_TAG_subprogram:
6154 case DW_TAG_inlined_subroutine:
6155 case DW_TAG_lexical_block:
6156 if (sc.function)
6157 {
6158 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
6159 if (symbol_context_scope == NULL)
6160 symbol_context_scope = sc.function;
6161 }
6162 break;
6163
6164 default:
6165 symbol_context_scope = sc.comp_unit;
6166 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00006167 }
Greg Clayton7f995132011-10-04 22:41:51 +00006168 }
6169
Greg Clayton5cf58b92011-10-05 22:22:08 +00006170 if (symbol_context_scope)
6171 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006172 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
6173 name,
6174 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00006175 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00006176 scope,
6177 symbol_context_scope,
6178 &decl,
6179 location,
6180 is_external,
6181 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00006182
6183 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
6184 }
6185 else
6186 {
6187 // Not ready to parse this variable yet. It might be a global
6188 // or static variable that is in a function scope and the function
6189 // in the symbol context wasn't filled in yet
6190 return var_sp;
6191 }
Greg Clayton7f995132011-10-04 22:41:51 +00006192 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006193 }
Greg Clayton7f995132011-10-04 22:41:51 +00006194 // Cache var_sp even if NULL (the variable was just a specification or
6195 // was missing vital information to be able to be displayed in the debugger
6196 // (missing location due to optimization, etc)) so we don't re-parse
6197 // this DIE over and over later...
6198 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006199 }
6200 return var_sp;
6201}
6202
Greg Claytonc662ec82011-06-17 22:10:16 +00006203
6204const DWARFDebugInfoEntry *
6205SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
6206 dw_offset_t spec_block_die_offset,
6207 DWARFCompileUnit **result_die_cu_handle)
6208{
6209 // Give the concrete function die specified by "func_die_offset", find the
6210 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6211 // to "spec_block_die_offset"
6212 DWARFDebugInfo* info = DebugInfo();
6213
6214 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
6215 if (die)
6216 {
6217 assert (*result_die_cu_handle);
6218 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
6219 }
6220 return NULL;
6221}
6222
6223
6224const DWARFDebugInfoEntry *
6225SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
6226 const DWARFDebugInfoEntry *die,
6227 dw_offset_t spec_block_die_offset,
6228 DWARFCompileUnit **result_die_cu_handle)
6229{
6230 if (die)
6231 {
6232 switch (die->Tag())
6233 {
6234 case DW_TAG_subprogram:
6235 case DW_TAG_inlined_subroutine:
6236 case DW_TAG_lexical_block:
6237 {
6238 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
6239 {
6240 *result_die_cu_handle = dwarf_cu;
6241 return die;
6242 }
6243
6244 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
6245 {
6246 *result_die_cu_handle = dwarf_cu;
6247 return die;
6248 }
6249 }
6250 break;
6251 }
6252
6253 // Give the concrete function die specified by "func_die_offset", find the
6254 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6255 // to "spec_block_die_offset"
6256 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
6257 {
6258 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
6259 child_die,
6260 spec_block_die_offset,
6261 result_die_cu_handle);
6262 if (result_die)
6263 return result_die;
6264 }
6265 }
6266
6267 *result_die_cu_handle = NULL;
6268 return NULL;
6269}
6270
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006271size_t
6272SymbolFileDWARF::ParseVariables
6273(
6274 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006275 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006276 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006277 const DWARFDebugInfoEntry *orig_die,
6278 bool parse_siblings,
6279 bool parse_children,
6280 VariableList* cc_variable_list
6281)
6282{
6283 if (orig_die == NULL)
6284 return 0;
6285
Greg Claytonc662ec82011-06-17 22:10:16 +00006286 VariableListSP variable_list_sp;
6287
6288 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006289 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00006290 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006291 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006292 dw_tag_t tag = die->Tag();
6293
6294 // Check to see if we have already parsed this variable or constant?
6295 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006296 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006297 if (cc_variable_list)
6298 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006299 }
6300 else
6301 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006302 // We haven't already parsed it, lets do that now.
6303 if ((tag == DW_TAG_variable) ||
6304 (tag == DW_TAG_constant) ||
6305 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006306 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006307 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00006308 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006309 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
6310 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6311 switch (parent_tag)
6312 {
6313 case DW_TAG_compile_unit:
6314 if (sc.comp_unit != NULL)
6315 {
6316 variable_list_sp = sc.comp_unit->GetVariableList(false);
6317 if (variable_list_sp.get() == NULL)
6318 {
6319 variable_list_sp.reset(new VariableList());
6320 sc.comp_unit->SetVariableList(variable_list_sp);
6321 }
6322 }
6323 else
6324 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006325 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
6326 MakeUserID(sc_parent_die->GetOffset()),
6327 DW_TAG_value_to_name (parent_tag),
6328 MakeUserID(orig_die->GetOffset()),
6329 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006330 }
6331 break;
6332
6333 case DW_TAG_subprogram:
6334 case DW_TAG_inlined_subroutine:
6335 case DW_TAG_lexical_block:
6336 if (sc.function != NULL)
6337 {
6338 // Check to see if we already have parsed the variables for the given scope
6339
Greg Clayton81c22f62011-10-19 18:09:39 +00006340 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006341 if (block == NULL)
6342 {
6343 // This must be a specification or abstract origin with
6344 // a concrete block couterpart in the current function. We need
6345 // to find the concrete block so we can correctly add the
6346 // variable to it
6347 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
6348 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
6349 sc_parent_die->GetOffset(),
6350 &concrete_block_die_cu);
6351 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00006352 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006353 }
6354
6355 if (block != NULL)
6356 {
6357 const bool can_create = false;
6358 variable_list_sp = block->GetBlockVariableList (can_create);
6359 if (variable_list_sp.get() == NULL)
6360 {
6361 variable_list_sp.reset(new VariableList());
6362 block->SetVariableList(variable_list_sp);
6363 }
6364 }
6365 }
6366 break;
6367
6368 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00006369 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
6370 MakeUserID(orig_die->GetOffset()),
6371 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006372 break;
6373 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00006374 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006375
6376 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006377 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00006378 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
6379 if (var_sp)
6380 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006381 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00006382 if (cc_variable_list)
6383 cc_variable_list->AddVariableIfUnique (var_sp);
6384 ++vars_added;
6385 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006386 }
6387 }
6388 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006389
6390 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
6391
6392 if (!skip_children && parse_children && die->HasChildren())
6393 {
6394 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
6395 }
6396
6397 if (parse_siblings)
6398 die = die->GetSibling();
6399 else
6400 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006401 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006402 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006403}
6404
6405//------------------------------------------------------------------
6406// PluginInterface protocol
6407//------------------------------------------------------------------
6408const char *
6409SymbolFileDWARF::GetPluginName()
6410{
6411 return "SymbolFileDWARF";
6412}
6413
6414const char *
6415SymbolFileDWARF::GetShortPluginName()
6416{
6417 return GetPluginNameStatic();
6418}
6419
6420uint32_t
6421SymbolFileDWARF::GetPluginVersion()
6422{
6423 return 1;
6424}
6425
6426void
Greg Clayton6beaaa62011-01-17 03:46:26 +00006427SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
6428{
6429 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6430 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6431 if (clang_type)
6432 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6433}
6434
6435void
6436SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
6437{
6438 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6439 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6440 if (clang_type)
6441 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6442}
6443
Greg Claytona2721472011-06-25 00:44:06 +00006444void
Sean Callanancc427fa2011-07-30 02:42:06 +00006445SymbolFileDWARF::DumpIndexes ()
6446{
6447 StreamFile s(stdout, false);
6448
6449 s.Printf ("DWARF index for (%s) '%s/%s':",
6450 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
6451 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
6452 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
6453 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
6454 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
6455 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
6456 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
6457 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
6458 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
6459 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
6460 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
6461}
6462
6463void
6464SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
6465 const char *name,
6466 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00006467{
Sean Callanancc427fa2011-07-30 02:42:06 +00006468 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00006469
6470 if (iter == m_decl_ctx_to_die.end())
6471 return;
6472
Greg Claytoncb5860a2011-10-13 23:49:28 +00006473 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00006474 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006475 const DWARFDebugInfoEntry *context_die = *pos;
6476
6477 if (!results)
6478 return;
6479
6480 DWARFDebugInfo* info = DebugInfo();
6481
6482 DIEArray die_offsets;
6483
6484 DWARFCompileUnit* dwarf_cu = NULL;
6485 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00006486
6487 if (m_using_apple_tables)
6488 {
6489 if (m_apple_types_ap.get())
6490 m_apple_types_ap->FindByName (name, die_offsets);
6491 }
6492 else
6493 {
6494 if (!m_indexed)
6495 Index ();
6496
6497 m_type_index.Find (ConstString(name), die_offsets);
6498 }
6499
Greg Clayton220a0072011-12-09 08:48:30 +00006500 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006501
6502 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00006503 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006504 for (size_t i = 0; i < num_matches; ++i)
6505 {
6506 const dw_offset_t die_offset = die_offsets[i];
6507 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00006508
Greg Claytoncb5860a2011-10-13 23:49:28 +00006509 if (die->GetParent() != context_die)
6510 continue;
6511
6512 Type *matching_type = ResolveType (dwarf_cu, die);
6513
Greg Clayton42ce2f32011-12-12 21:50:19 +00006514 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006515 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
6516
6517 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
6518 {
6519 clang::TagDecl *tag_decl = tag_type->getDecl();
6520 results->push_back(tag_decl);
6521 }
6522 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
6523 {
6524 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
6525 results->push_back(typedef_decl);
6526 }
Greg Claytona2721472011-06-25 00:44:06 +00006527 }
6528 }
6529 }
6530}
6531
6532void
6533SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00006534 const clang::DeclContext *decl_context,
6535 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00006536 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
6537{
Greg Clayton85ae2e12011-10-18 23:36:41 +00006538
6539 switch (decl_context->getDeclKind())
6540 {
6541 case clang::Decl::Namespace:
6542 case clang::Decl::TranslationUnit:
6543 {
6544 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6545 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
6546 }
6547 break;
6548 default:
6549 break;
6550 }
Greg Claytona2721472011-06-25 00:44:06 +00006551}
Greg Claytoncaab74e2012-01-28 00:48:57 +00006552
6553bool
6554SymbolFileDWARF::LayoutRecordType (void *baton,
6555 const clang::RecordDecl *record_decl,
6556 uint64_t &size,
6557 uint64_t &alignment,
6558 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6559 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6560 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6561{
6562 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6563 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
6564}
6565
6566
6567bool
6568SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
6569 uint64_t &bit_size,
6570 uint64_t &alignment,
6571 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6572 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6573 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6574{
6575 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
6576 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
6577 bool success = false;
6578 base_offsets.clear();
6579 vbase_offsets.clear();
6580 if (pos != m_record_decl_to_layout_map.end())
6581 {
6582 bit_size = pos->second.bit_size;
6583 alignment = pos->second.alignment;
6584 field_offsets.swap(pos->second.field_offsets);
6585 m_record_decl_to_layout_map.erase(pos);
6586 success = true;
6587 }
6588 else
6589 {
6590 bit_size = 0;
6591 alignment = 0;
6592 field_offsets.clear();
6593 }
6594
6595 if (log)
6596 GetObjectFile()->GetModule()->LogMessage (log.get(),
6597 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
6598 record_decl,
6599 bit_size,
6600 alignment,
6601 (uint32_t)field_offsets.size(),
6602 (uint32_t)base_offsets.size(),
6603 (uint32_t)vbase_offsets.size(),
6604 success);
6605 return success;
6606}
6607
6608
6609