blob: 1ad818a91f99a920eac293bb2788f90342540058 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Greg Clayton3c2e3ae2012-02-06 06:42:51 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "clang/Basic/Builtins.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000025#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanancc427fa2011-07-30 02:42:06 +000027#include "llvm/Support/Casting.h"
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Module.h"
30#include "lldb/Core/PluginManager.h"
31#include "lldb/Core/RegularExpression.h"
32#include "lldb/Core/Scalar.h"
33#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000034#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000035#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Core/Timer.h"
37#include "lldb/Core/Value.h"
38
Greg Clayton20568dd2011-10-13 23:13:20 +000039#include "lldb/Host/Host.h"
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000042#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Symbol/CompileUnit.h"
44#include "lldb/Symbol/LineTable.h"
45#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
47#include "lldb/Symbol/VariableList.h"
48
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000049#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000050#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000051
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "DWARFCompileUnit.h"
53#include "DWARFDebugAbbrev.h"
54#include "DWARFDebugAranges.h"
55#include "DWARFDebugInfo.h"
56#include "DWARFDebugInfoEntry.h"
57#include "DWARFDebugLine.h"
58#include "DWARFDebugPubnames.h"
59#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000060#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFDIECollection.h"
62#include "DWARFFormValue.h"
63#include "DWARFLocationList.h"
64#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000065#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
67#include <map>
68
Greg Clayton62742b12010-11-11 01:09:45 +000069//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000070
71#ifdef ENABLE_DEBUG_PRINTF
72#include <stdio.h>
73#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
74#else
75#define DEBUG_PRINTF(fmt, ...)
76#endif
77
Greg Clayton594e5ed2010-09-27 21:07:38 +000078#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079
80using namespace lldb;
81using namespace lldb_private;
82
Greg Clayton219cf312012-03-30 00:51:13 +000083//static inline bool
84//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
85//{
86// switch (tag)
87// {
88// default:
89// break;
90// case DW_TAG_subprogram:
91// case DW_TAG_inlined_subroutine:
92// case DW_TAG_class_type:
93// case DW_TAG_structure_type:
94// case DW_TAG_union_type:
95// return true;
96// }
97// return false;
98//}
99//
Sean Callananc7fbf732010-08-06 00:32:49 +0000100static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000101DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102{
103 switch (dwarf_accessibility)
104 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000105 case DW_ACCESS_public: return eAccessPublic;
106 case DW_ACCESS_private: return eAccessPrivate;
107 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000108 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000110 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111}
112
Greg Clayton1fba87112012-02-09 20:03:49 +0000113#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
114
115class DIEStack
116{
117public:
118
119 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
120 {
121 m_dies.push_back (DIEInfo(cu, die));
122 }
123
124
125 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
126 {
127 StreamString log_strm;
128 const size_t n = m_dies.size();
129 log_strm.Printf("DIEStack[%zu]:\n", n);
130 for (size_t i=0; i<n; i++)
131 {
132 DWARFCompileUnit *cu = m_dies[i].cu;
133 const DWARFDebugInfoEntry *die = m_dies[i].die;
134 std::string qualified_name;
135 die->GetQualifiedName(dwarf, cu, qualified_name);
136 log_strm.Printf ("[%zu] 0x%8.8x: %s name='%s'\n",
137 i,
138 die->GetOffset(),
139 DW_TAG_value_to_name(die->Tag()),
140 qualified_name.c_str());
141 }
142 log->PutCString(log_strm.GetData());
143 }
144 void Pop ()
145 {
146 m_dies.pop_back();
147 }
148
149 class ScopedPopper
150 {
151 public:
152 ScopedPopper (DIEStack &die_stack) :
153 m_die_stack (die_stack),
154 m_valid (false)
155 {
156 }
157
158 void
159 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
160 {
161 m_valid = true;
162 m_die_stack.Push (cu, die);
163 }
164
165 ~ScopedPopper ()
166 {
167 if (m_valid)
168 m_die_stack.Pop();
169 }
170
171
172
173 protected:
174 DIEStack &m_die_stack;
175 bool m_valid;
176 };
177
178protected:
179 struct DIEInfo {
180 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
181 cu(c),
182 die(d)
183 {
184 }
185 DWARFCompileUnit *cu;
186 const DWARFDebugInfoEntry *die;
187 };
188 typedef std::vector<DIEInfo> Stack;
189 Stack m_dies;
190};
191#endif
192
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193void
194SymbolFileDWARF::Initialize()
195{
196 LogChannelDWARF::Initialize();
197 PluginManager::RegisterPlugin (GetPluginNameStatic(),
198 GetPluginDescriptionStatic(),
199 CreateInstance);
200}
201
202void
203SymbolFileDWARF::Terminate()
204{
205 PluginManager::UnregisterPlugin (CreateInstance);
206 LogChannelDWARF::Initialize();
207}
208
209
210const char *
211SymbolFileDWARF::GetPluginNameStatic()
212{
Greg Claytond4a2b372011-09-12 23:21:58 +0000213 return "dwarf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214}
215
216const char *
217SymbolFileDWARF::GetPluginDescriptionStatic()
218{
219 return "DWARF and DWARF3 debug symbol file reader.";
220}
221
222
223SymbolFile*
224SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
225{
226 return new SymbolFileDWARF(obj_file);
227}
228
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000229TypeList *
230SymbolFileDWARF::GetTypeList ()
231{
232 if (m_debug_map_symfile)
233 return m_debug_map_symfile->GetTypeList();
234 return m_obj_file->GetModule()->GetTypeList();
235
236}
237
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238//----------------------------------------------------------------------
239// Gets the first parent that is a lexical block, function or inlined
240// subroutine, or compile unit.
241//----------------------------------------------------------------------
242static const DWARFDebugInfoEntry *
243GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
244{
245 const DWARFDebugInfoEntry *die;
246 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
247 {
248 dw_tag_t tag = die->Tag();
249
250 switch (tag)
251 {
252 case DW_TAG_compile_unit:
253 case DW_TAG_subprogram:
254 case DW_TAG_inlined_subroutine:
255 case DW_TAG_lexical_block:
256 return die;
257 }
258 }
259 return NULL;
260}
261
262
Greg Clayton450e3f32010-10-12 02:24:53 +0000263SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
264 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000265 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 +0000266 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000267 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000269 m_data_debug_abbrev (),
270 m_data_debug_aranges (),
271 m_data_debug_frame (),
272 m_data_debug_info (),
273 m_data_debug_line (),
274 m_data_debug_loc (),
275 m_data_debug_ranges (),
276 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000277 m_data_apple_names (),
278 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000279 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 m_info(),
282 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000283 m_apple_names_ap (),
284 m_apple_types_ap (),
285 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000286 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000287 m_function_basename_index(),
288 m_function_fullname_index(),
289 m_function_method_index(),
290 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000291 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000292 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000293 m_type_index(),
294 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000295 m_indexed (false),
296 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000297 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000298 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000299 m_ranges(),
300 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301{
302}
303
304SymbolFileDWARF::~SymbolFileDWARF()
305{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000306 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000307 {
308 ModuleSP module_sp (m_obj_file->GetModule());
309 if (module_sp)
310 module_sp->GetClangASTContext().RemoveExternalSource ();
311 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000312}
313
314static const ConstString &
315GetDWARFMachOSegmentName ()
316{
317 static ConstString g_dwarf_section_name ("__DWARF");
318 return g_dwarf_section_name;
319}
320
Greg Claytone576ab22011-02-15 00:19:15 +0000321UniqueDWARFASTTypeMap &
322SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
323{
324 if (m_debug_map_symfile)
325 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
326 return m_unique_ast_type_map;
327}
328
Greg Clayton6beaaa62011-01-17 03:46:26 +0000329ClangASTContext &
330SymbolFileDWARF::GetClangASTContext ()
331{
332 if (m_debug_map_symfile)
333 return m_debug_map_symfile->GetClangASTContext ();
334
335 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
336 if (!m_is_external_ast_source)
337 {
338 m_is_external_ast_source = true;
339 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
340 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
341 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000342 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000343 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000344 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000345 ast.SetExternalSource (ast_source_ap);
346 }
347 return ast;
348}
349
350void
351SymbolFileDWARF::InitializeObject()
352{
353 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000354 ModuleSP module_sp (m_obj_file->GetModule());
355 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000356 {
357 const SectionList *section_list = m_obj_file->GetSectionList();
358
359 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
360
361 // Memory map the DWARF mach-o segment so we have everything mmap'ed
362 // to keep our heap memory usage down.
363 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000364 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000365 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000366 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000367 if (m_data_apple_names.GetByteSize() > 0)
368 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000369 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
370 if (m_apple_names_ap->IsValid())
371 m_using_apple_tables = true;
372 else
Greg Clayton7f995132011-10-04 22:41:51 +0000373 m_apple_names_ap.reset();
374 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000375 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000376 if (m_data_apple_types.GetByteSize() > 0)
377 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000378 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
379 if (m_apple_types_ap->IsValid())
380 m_using_apple_tables = true;
381 else
Greg Clayton7f995132011-10-04 22:41:51 +0000382 m_apple_types_ap.reset();
383 }
384
385 get_apple_namespaces_data();
386 if (m_data_apple_namespaces.GetByteSize() > 0)
387 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000388 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
389 if (m_apple_namespaces_ap->IsValid())
390 m_using_apple_tables = true;
391 else
Greg Clayton7f995132011-10-04 22:41:51 +0000392 m_apple_namespaces_ap.reset();
393 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000394
Greg Clayton5009f9d2011-10-27 17:55:14 +0000395 get_apple_objc_data();
396 if (m_data_apple_objc.GetByteSize() > 0)
397 {
398 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
399 if (m_apple_objc_ap->IsValid())
400 m_using_apple_tables = true;
401 else
402 m_apple_objc_ap.reset();
403 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404}
405
406bool
407SymbolFileDWARF::SupportedVersion(uint16_t version)
408{
409 return version == 2 || version == 3;
410}
411
412uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000413SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414{
415 uint32_t abilities = 0;
416 if (m_obj_file != NULL)
417 {
418 const Section* section = NULL;
419 const SectionList *section_list = m_obj_file->GetSectionList();
420 if (section_list == NULL)
421 return 0;
422
423 uint64_t debug_abbrev_file_size = 0;
424 uint64_t debug_aranges_file_size = 0;
425 uint64_t debug_frame_file_size = 0;
426 uint64_t debug_info_file_size = 0;
427 uint64_t debug_line_file_size = 0;
428 uint64_t debug_loc_file_size = 0;
429 uint64_t debug_macinfo_file_size = 0;
430 uint64_t debug_pubnames_file_size = 0;
431 uint64_t debug_pubtypes_file_size = 0;
432 uint64_t debug_ranges_file_size = 0;
433 uint64_t debug_str_file_size = 0;
434
Greg Clayton6beaaa62011-01-17 03:46:26 +0000435 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436
437 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000438 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439
Greg Clayton4ceb9982010-07-21 22:54:26 +0000440 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 if (section != NULL)
442 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000443 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444
Greg Clayton4ceb9982010-07-21 22:54:26 +0000445 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000447 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 else
449 m_flags.Set (flagsGotDebugAbbrevData);
450
Greg Clayton4ceb9982010-07-21 22:54:26 +0000451 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000453 debug_aranges_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 else
455 m_flags.Set (flagsGotDebugArangesData);
456
Greg Clayton4ceb9982010-07-21 22:54:26 +0000457 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000459 debug_frame_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 else
461 m_flags.Set (flagsGotDebugFrameData);
462
Greg Clayton4ceb9982010-07-21 22:54:26 +0000463 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000465 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 else
467 m_flags.Set (flagsGotDebugLineData);
468
Greg Clayton4ceb9982010-07-21 22:54:26 +0000469 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000471 debug_loc_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 else
473 m_flags.Set (flagsGotDebugLocData);
474
Greg Clayton4ceb9982010-07-21 22:54:26 +0000475 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000477 debug_macinfo_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 else
479 m_flags.Set (flagsGotDebugMacInfoData);
480
Greg Clayton4ceb9982010-07-21 22:54:26 +0000481 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000483 debug_pubnames_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484 else
485 m_flags.Set (flagsGotDebugPubNamesData);
486
Greg Clayton4ceb9982010-07-21 22:54:26 +0000487 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000489 debug_pubtypes_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 else
491 m_flags.Set (flagsGotDebugPubTypesData);
492
Greg Clayton4ceb9982010-07-21 22:54:26 +0000493 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000495 debug_ranges_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496 else
497 m_flags.Set (flagsGotDebugRangesData);
498
Greg Clayton4ceb9982010-07-21 22:54:26 +0000499 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000501 debug_str_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 else
503 m_flags.Set (flagsGotDebugStrData);
504 }
505
506 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
507 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
508
509 if (debug_line_file_size > 0)
510 abilities |= LineTables;
511
512 if (debug_aranges_file_size > 0)
513 abilities |= AddressAcceleratorTable;
514
515 if (debug_pubnames_file_size > 0)
516 abilities |= FunctionAcceleratorTable;
517
518 if (debug_pubtypes_file_size > 0)
519 abilities |= TypeAcceleratorTable;
520
521 if (debug_macinfo_file_size > 0)
522 abilities |= MacroInformation;
523
524 if (debug_frame_file_size > 0)
525 abilities |= CallFrameInformation;
526 }
527 return abilities;
528}
529
530const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000531SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532{
533 if (m_flags.IsClear (got_flag))
534 {
535 m_flags.Set (got_flag);
536 const SectionList *section_list = m_obj_file->GetSectionList();
537 if (section_list)
538 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000539 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
540 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541 {
542 // See if we memory mapped the DWARF segment?
543 if (m_dwarf_data.GetByteSize())
544 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000545 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546 }
547 else
548 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000549 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550 data.Clear();
551 }
552 }
553 }
554 }
555 return data;
556}
557
558const DataExtractor&
559SymbolFileDWARF::get_debug_abbrev_data()
560{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000561 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562}
563
564const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000565SymbolFileDWARF::get_debug_aranges_data()
566{
567 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
568}
569
570const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571SymbolFileDWARF::get_debug_frame_data()
572{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000573 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574}
575
576const DataExtractor&
577SymbolFileDWARF::get_debug_info_data()
578{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000579 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580}
581
582const DataExtractor&
583SymbolFileDWARF::get_debug_line_data()
584{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000585 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586}
587
588const DataExtractor&
589SymbolFileDWARF::get_debug_loc_data()
590{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000591 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592}
593
594const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595SymbolFileDWARF::get_debug_ranges_data()
596{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000597 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598}
599
600const DataExtractor&
601SymbolFileDWARF::get_debug_str_data()
602{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000603 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604}
605
Greg Claytonf9eec202011-09-01 23:16:13 +0000606const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000607SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000608{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000609 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000610}
611
612const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000613SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000614{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000615 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000616}
617
Greg Clayton7f995132011-10-04 22:41:51 +0000618const DataExtractor&
619SymbolFileDWARF::get_apple_namespaces_data()
620{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000621 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
622}
623
624const DataExtractor&
625SymbolFileDWARF::get_apple_objc_data()
626{
627 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000628}
629
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630
631DWARFDebugAbbrev*
632SymbolFileDWARF::DebugAbbrev()
633{
634 if (m_abbr.get() == NULL)
635 {
636 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
637 if (debug_abbrev_data.GetByteSize() > 0)
638 {
639 m_abbr.reset(new DWARFDebugAbbrev());
640 if (m_abbr.get())
641 m_abbr->Parse(debug_abbrev_data);
642 }
643 }
644 return m_abbr.get();
645}
646
647const DWARFDebugAbbrev*
648SymbolFileDWARF::DebugAbbrev() const
649{
650 return m_abbr.get();
651}
652
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653
654DWARFDebugInfo*
655SymbolFileDWARF::DebugInfo()
656{
657 if (m_info.get() == NULL)
658 {
659 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
660 if (get_debug_info_data().GetByteSize() > 0)
661 {
662 m_info.reset(new DWARFDebugInfo());
663 if (m_info.get())
664 {
665 m_info->SetDwarfData(this);
666 }
667 }
668 }
669 return m_info.get();
670}
671
672const DWARFDebugInfo*
673SymbolFileDWARF::DebugInfo() const
674{
675 return m_info.get();
676}
677
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678DWARFCompileUnit*
679SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
680{
681 DWARFDebugInfo* info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +0000682 if (info && UserIDMatches(cu_uid))
683 return info->GetCompileUnit((dw_offset_t)cu_uid).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 return NULL;
685}
686
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687
688DWARFDebugRanges*
689SymbolFileDWARF::DebugRanges()
690{
691 if (m_ranges.get() == NULL)
692 {
693 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
694 if (get_debug_ranges_data().GetByteSize() > 0)
695 {
696 m_ranges.reset(new DWARFDebugRanges());
697 if (m_ranges.get())
698 m_ranges->Extract(this);
699 }
700 }
701 return m_ranges.get();
702}
703
704const DWARFDebugRanges*
705SymbolFileDWARF::DebugRanges() const
706{
707 return m_ranges.get();
708}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709
Greg Clayton53eb1c22012-04-02 22:59:12 +0000710lldb::CompUnitSP
711SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000713 CompUnitSP cu_sp;
714 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000716 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
717 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000719 // We already parsed this compile unit, had out a shared pointer to it
720 cu_sp = comp_unit->shared_from_this();
721 }
722 else
723 {
724 ModuleSP module_sp (m_obj_file->GetModule());
725 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000727 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
728 if (cu_die)
729 {
730 const char * cu_die_name = cu_die->GetName(this, dwarf_cu);
731 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
732 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
733 if (cu_die_name)
734 {
735 std::string ramapped_file;
736 FileSpec cu_file_spec;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000737
Greg Clayton53eb1c22012-04-02 22:59:12 +0000738 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
739 {
740 // If we have a full path to the compile unit, we don't need to resolve
741 // the file. This can be expensive e.g. when the source files are NFS mounted.
742 if (module_sp->RemapSourceFile(cu_die_name, ramapped_file))
743 cu_file_spec.SetFile (ramapped_file.c_str(), false);
744 else
745 cu_file_spec.SetFile (cu_die_name, false);
746 }
747 else
748 {
749 std::string fullpath(cu_comp_dir);
750 if (*fullpath.rbegin() != '/')
751 fullpath += '/';
752 fullpath += cu_die_name;
753 if (module_sp->RemapSourceFile (fullpath.c_str(), ramapped_file))
754 cu_file_spec.SetFile (ramapped_file.c_str(), false);
755 else
756 cu_file_spec.SetFile (fullpath.c_str(), false);
757 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758
Greg Clayton53eb1c22012-04-02 22:59:12 +0000759 cu_sp.reset(new CompileUnit (module_sp,
760 dwarf_cu,
761 cu_file_spec,
762 MakeUserID(dwarf_cu->GetOffset()),
763 cu_language));
764 if (cu_sp)
765 {
766 dwarf_cu->SetUserData(cu_sp.get());
767
768 if (m_debug_map_symfile)
769 {
770 // Let the symbol file register the compile unit with
771 // the symbol vendor using its compile unit index
772 // when we are doing DWARF in .o files + debug map
773 m_debug_map_symfile->SetCompileUnit(this, cu_sp);
774 }
775 else
776 {
777 // Figure out the compile unit index if we weren't given one
778 if (cu_idx == UINT32_MAX)
779 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
780
781 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
782 }
783 }
784 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 }
786 }
787 }
788 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000789 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790}
791
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792uint32_t
793SymbolFileDWARF::GetNumCompileUnits()
794{
795 DWARFDebugInfo* info = DebugInfo();
796 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798 return 0;
799}
800
801CompUnitSP
802SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
803{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000804 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 DWARFDebugInfo* info = DebugInfo();
806 if (info)
807 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000808 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
809 if (dwarf_cu)
810 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000812 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813}
814
815static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000816AddRangesToBlock (Block& block,
817 DWARFDebugRanges::RangeList& ranges,
818 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000820 const size_t num_ranges = ranges.GetSize();
821 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000822 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000823 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
824 const addr_t range_base = range.GetRangeBase();
825 assert (range_base >= block_base_addr);
826 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000827 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000828 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829}
830
831
832Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000833SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834{
835 DWARFDebugRanges::RangeList func_ranges;
836 const char *name = NULL;
837 const char *mangled = NULL;
838 int decl_file = 0;
839 int decl_line = 0;
840 int decl_column = 0;
841 int call_file = 0;
842 int call_line = 0;
843 int call_column = 0;
844 DWARFExpression frame_base;
845
Greg Claytonc93237c2010-10-01 20:48:32 +0000846 assert (die->Tag() == DW_TAG_subprogram);
847
848 if (die->Tag() != DW_TAG_subprogram)
849 return NULL;
850
Greg Clayton3c2e3ae2012-02-06 06:42:51 +0000851 if (die->GetDIENamesAndRanges (this,
852 dwarf_cu,
853 name,
854 mangled,
855 func_ranges,
856 decl_file,
857 decl_line,
858 decl_column,
859 call_file,
860 call_line,
861 call_column,
862 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863 {
864 // Union of all ranges in the function DIE (if the function is discontiguous)
865 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000866 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
867 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
869 {
870 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
871 if (func_range.GetBaseAddress().IsValid())
872 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
873 }
874
875 if (func_range.GetBaseAddress().IsValid())
876 {
877 Mangled func_name;
878 if (mangled)
879 func_name.SetValue(mangled, true);
880 else if (name)
881 func_name.SetValue(name, false);
882
883 FunctionSP func_sp;
884 std::auto_ptr<Declaration> decl_ap;
885 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000886 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
887 decl_line,
888 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000890 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000891 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892
893 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
894
895 func_range.GetBaseAddress().ResolveLinkedAddress();
896
Greg Clayton81c22f62011-10-19 18:09:39 +0000897 const user_id_t func_user_id = MakeUserID(die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898 func_sp.reset(new Function (sc.comp_unit,
Greg Clayton81c22f62011-10-19 18:09:39 +0000899 func_user_id, // UserID is the DIE offset
900 func_user_id,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901 func_name,
902 func_type,
903 func_range)); // first address range
904
905 if (func_sp.get() != NULL)
906 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000907 if (frame_base.IsValid())
908 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909 sc.comp_unit->AddFunction(func_sp);
910 return func_sp.get();
911 }
912 }
913 }
914 return NULL;
915}
916
917size_t
918SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
919{
920 assert (sc.comp_unit);
921 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000922 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923 if (dwarf_cu)
924 {
925 DWARFDIECollection function_dies;
926 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
927 size_t func_idx;
928 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
929 {
930 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000931 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932 {
933 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
934 ++functions_added;
935 }
936 }
937 //FixupTypes();
938 }
939 return functions_added;
940}
941
942bool
943SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
944{
945 assert (sc.comp_unit);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000946 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
947 assert (dwarf_cu);
948 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949
950 if (cu_die)
951 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000952 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
953 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954
955 // All file indexes in DWARF are one based and a file of index zero is
956 // supposed to be the compile unit itself.
957 support_files.Append (*sc.comp_unit);
958
Greg Claytond804d282012-03-15 21:01:31 +0000959 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 +0000960 }
961 return false;
962}
963
964struct ParseDWARFLineTableCallbackInfo
965{
966 LineTable* line_table;
967 const SectionList *section_list;
968 lldb::addr_t prev_sect_file_base_addr;
969 lldb::addr_t curr_sect_file_base_addr;
970 bool is_oso_for_debug_map;
971 bool prev_in_final_executable;
972 DWARFDebugLine::Row prev_row;
973 SectionSP prev_section_sp;
974 SectionSP curr_section_sp;
975};
976
977//----------------------------------------------------------------------
978// ParseStatementTableCallback
979//----------------------------------------------------------------------
980static void
981ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
982{
983 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
984 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
985 {
986 // Just started parsing the line table
987 }
988 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
989 {
990 // Done parsing line table, nothing to do for the cleanup
991 }
992 else
993 {
994 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
995 // We have a new row, lets append it
996
997 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
998 {
999 info->prev_section_sp = info->curr_section_sp;
1000 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
1001 // If this is an end sequence entry, then we subtract one from the
1002 // address to make sure we get an address that is not the end of
1003 // a section.
1004 if (state.end_sequence && state.address != 0)
1005 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
1006 else
1007 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
1008
1009 if (info->curr_section_sp.get())
1010 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
1011 else
1012 info->curr_sect_file_base_addr = 0;
1013 }
1014 if (info->curr_section_sp.get())
1015 {
1016 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
1017 // Check for the fancy section magic to determine if we
1018
1019 if (info->is_oso_for_debug_map)
1020 {
1021 // When this is a debug map object file that contains DWARF
1022 // (referenced from an N_OSO debug map nlist entry) we will have
1023 // a file address in the file range for our section from the
1024 // original .o file, and a load address in the executable that
1025 // contains the debug map.
1026 //
1027 // If the sections for the file range and load range are
1028 // different, we have a remapped section for the function and
1029 // this address is resolved. If they are the same, then the
1030 // function for this address didn't make it into the final
1031 // executable.
1032 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
1033
1034 // If we are doing DWARF with debug map, then we need to carefully
1035 // add each line table entry as there may be gaps as functions
1036 // get moved around or removed.
1037 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
1038 {
1039 if (info->prev_in_final_executable)
1040 {
1041 bool terminate_previous_entry = false;
1042 if (!curr_in_final_executable)
1043 {
1044 // Check for the case where the previous line entry
1045 // in a function made it into the final executable,
1046 // yet the current line entry falls in a function
1047 // that didn't. The line table used to be contiguous
1048 // through this address range but now it isn't. We
1049 // need to terminate the previous line entry so
1050 // that we can reconstruct the line range correctly
1051 // for it and to keep the line table correct.
1052 terminate_previous_entry = true;
1053 }
1054 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
1055 {
1056 // Check for cases where the line entries used to be
1057 // contiguous address ranges, but now they aren't.
1058 // This can happen when order files specify the
1059 // ordering of the functions.
1060 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
1061 Section *curr_sect = info->curr_section_sp.get();
1062 Section *prev_sect = info->prev_section_sp.get();
1063 assert (curr_sect->GetLinkedSection());
1064 assert (prev_sect->GetLinkedSection());
1065 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
1066 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
1067 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
1068 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
1069 if (object_file_addr_delta != linked_file_addr_delta)
1070 terminate_previous_entry = true;
1071 }
1072
1073 if (terminate_previous_entry)
1074 {
1075 line_table->InsertLineEntry (info->prev_section_sp,
1076 state.address - info->prev_sect_file_base_addr,
1077 info->prev_row.line,
1078 info->prev_row.column,
1079 info->prev_row.file,
1080 false, // is_stmt
1081 false, // basic_block
1082 false, // state.prologue_end
1083 false, // state.epilogue_begin
1084 true); // end_sequence);
1085 }
1086 }
1087 }
1088
1089 if (curr_in_final_executable)
1090 {
1091 line_table->InsertLineEntry (info->curr_section_sp,
1092 curr_line_section_offset,
1093 state.line,
1094 state.column,
1095 state.file,
1096 state.is_stmt,
1097 state.basic_block,
1098 state.prologue_end,
1099 state.epilogue_begin,
1100 state.end_sequence);
1101 info->prev_section_sp = info->curr_section_sp;
1102 }
1103 else
1104 {
1105 // If the current address didn't make it into the final
1106 // executable, the current section will be the __text
1107 // segment in the .o file, so we need to clear this so
1108 // we can catch the next function that did make it into
1109 // the final executable.
1110 info->prev_section_sp.reset();
1111 info->curr_section_sp.reset();
1112 }
1113
1114 info->prev_in_final_executable = curr_in_final_executable;
1115 }
1116 else
1117 {
1118 // We are not in an object file that contains DWARF for an
1119 // N_OSO, this is just a normal DWARF file. The DWARF spec
1120 // guarantees that the addresses will be in increasing order
1121 // so, since we store line tables in file address order, we
1122 // can always just append the line entry without needing to
1123 // search for the correct insertion point (we don't need to
1124 // use LineEntry::InsertLineEntry()).
1125 line_table->AppendLineEntry (info->curr_section_sp,
1126 curr_line_section_offset,
1127 state.line,
1128 state.column,
1129 state.file,
1130 state.is_stmt,
1131 state.basic_block,
1132 state.prologue_end,
1133 state.epilogue_begin,
1134 state.end_sequence);
1135 }
1136 }
1137
1138 info->prev_row = state;
1139 }
1140}
1141
1142bool
1143SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1144{
1145 assert (sc.comp_unit);
1146 if (sc.comp_unit->GetLineTable() != NULL)
1147 return true;
1148
1149 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
1150 if (dwarf_cu)
1151 {
1152 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001153 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001155 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1156 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001158 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1159 if (line_table_ap.get())
1160 {
1161 ParseDWARFLineTableCallbackInfo info = {
1162 line_table_ap.get(),
1163 m_obj_file->GetSectionList(),
1164 0,
1165 0,
1166 m_debug_map_symfile != NULL,
1167 false,
1168 DWARFDebugLine::Row(),
1169 SectionSP(),
1170 SectionSP()
1171 };
1172 uint32_t offset = cu_line_offset;
1173 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1174 sc.comp_unit->SetLineTable(line_table_ap.release());
1175 return true;
1176 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177 }
1178 }
1179 }
1180 return false;
1181}
1182
1183size_t
1184SymbolFileDWARF::ParseFunctionBlocks
1185(
1186 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001187 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001188 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001189 const DWARFDebugInfoEntry *die,
1190 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001191 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192)
1193{
1194 size_t blocks_added = 0;
1195 while (die != NULL)
1196 {
1197 dw_tag_t tag = die->Tag();
1198
1199 switch (tag)
1200 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001202 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203 case DW_TAG_lexical_block:
1204 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001205 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001206 if (tag == DW_TAG_subprogram)
1207 {
1208 // Skip any DW_TAG_subprogram DIEs that are inside
1209 // of a normal or inlined functions. These will be
1210 // parsed on their own as separate entities.
1211
1212 if (depth > 0)
1213 break;
1214
1215 block = parent_block;
1216 }
1217 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001218 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001219 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001220 parent_block->AddChild(block_sp);
1221 block = block_sp.get();
1222 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001223 DWARFDebugRanges::RangeList ranges;
1224 const char *name = NULL;
1225 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227 int decl_file = 0;
1228 int decl_line = 0;
1229 int decl_column = 0;
1230 int call_file = 0;
1231 int call_line = 0;
1232 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001233 if (die->GetDIENamesAndRanges (this,
1234 dwarf_cu,
1235 name,
1236 mangled_name,
1237 ranges,
1238 decl_file, decl_line, decl_column,
1239 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240 {
1241 if (tag == DW_TAG_subprogram)
1242 {
1243 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001244 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001246 else if (tag == DW_TAG_inlined_subroutine)
1247 {
1248 // We get called here for inlined subroutines in two ways.
1249 // The first time is when we are making the Function object
1250 // for this inlined concrete instance. Since we're creating a top level block at
1251 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1252 // adjust the containing address.
1253 // The second time is when we are parsing the blocks inside the function that contains
1254 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1255 // function the offset will be for that function.
1256 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1257 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001258 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001259 }
1260 }
1261
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001262 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263
1264 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1265 {
1266 std::auto_ptr<Declaration> decl_ap;
1267 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001268 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1269 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270
1271 std::auto_ptr<Declaration> call_ap;
1272 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001273 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1274 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001276 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001277 }
1278
1279 ++blocks_added;
1280
Greg Claytondd7feaf2011-08-12 17:54:33 +00001281 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001283 blocks_added += ParseFunctionBlocks (sc,
1284 block,
1285 dwarf_cu,
1286 die->GetFirstChild(),
1287 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001288 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289 }
1290 }
1291 }
1292 break;
1293 default:
1294 break;
1295 }
1296
Greg Claytondd7feaf2011-08-12 17:54:33 +00001297 // Only parse siblings of the block if we are not at depth zero. A depth
1298 // of zero indicates we are currently parsing the top level
1299 // DW_TAG_subprogram DIE
1300
1301 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001302 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001303 else
1304 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001305 }
1306 return blocks_added;
1307}
1308
Greg Claytonf0705c82011-10-22 03:33:13 +00001309bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001310SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1311 const DWARFDebugInfoEntry *die,
1312 ClangASTContext::TemplateParameterInfos &template_param_infos)
1313{
1314 const dw_tag_t tag = die->Tag();
1315
1316 switch (tag)
1317 {
1318 case DW_TAG_template_type_parameter:
1319 case DW_TAG_template_value_parameter:
1320 {
1321 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1322
1323 DWARFDebugInfoEntry::Attributes attributes;
1324 const size_t num_attributes = die->GetAttributes (this,
1325 dwarf_cu,
1326 fixed_form_sizes,
1327 attributes);
1328 const char *name = NULL;
1329 Type *lldb_type = NULL;
1330 clang_type_t clang_type = NULL;
1331 uint64_t uval64 = 0;
1332 bool uval64_valid = false;
1333 if (num_attributes > 0)
1334 {
1335 DWARFFormValue form_value;
1336 for (size_t i=0; i<num_attributes; ++i)
1337 {
1338 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1339
1340 switch (attr)
1341 {
1342 case DW_AT_name:
1343 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1344 name = form_value.AsCString(&get_debug_str_data());
1345 break;
1346
1347 case DW_AT_type:
1348 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1349 {
1350 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1351 lldb_type = ResolveTypeUID(type_die_offset);
1352 if (lldb_type)
1353 clang_type = lldb_type->GetClangForwardType();
1354 }
1355 break;
1356
1357 case DW_AT_const_value:
1358 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1359 {
1360 uval64_valid = true;
1361 uval64 = form_value.Unsigned();
1362 }
1363 break;
1364 default:
1365 break;
1366 }
1367 }
1368
1369 if (name && lldb_type && clang_type)
1370 {
1371 bool is_signed = false;
1372 template_param_infos.names.push_back(name);
1373 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1374 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1375 {
1376 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1377 template_param_infos.args.push_back (clang::TemplateArgument (llvm::APSInt(apint), clang_qual_type));
1378 }
1379 else
1380 {
1381 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1382 }
1383 }
1384 else
1385 {
1386 return false;
1387 }
1388
1389 }
1390 }
1391 return true;
1392
1393 default:
1394 break;
1395 }
1396 return false;
1397}
1398
1399bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001400SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1401 const DWARFDebugInfoEntry *parent_die,
1402 ClangASTContext::TemplateParameterInfos &template_param_infos)
1403{
1404
1405 if (parent_die == NULL)
1406 return NULL;
1407
Greg Claytonf0705c82011-10-22 03:33:13 +00001408 Args template_parameter_names;
1409 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1410 die != NULL;
1411 die = die->GetSibling())
1412 {
1413 const dw_tag_t tag = die->Tag();
1414
1415 switch (tag)
1416 {
1417 case DW_TAG_template_type_parameter:
1418 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001419 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001420 break;
1421
1422 default:
1423 break;
1424 }
1425 }
1426 if (template_param_infos.args.empty())
1427 return false;
1428 return template_param_infos.args.size() == template_param_infos.names.size();
1429}
1430
1431clang::ClassTemplateDecl *
1432SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001433 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001434 const char *parent_name,
1435 int tag_decl_kind,
1436 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1437{
1438 if (template_param_infos.IsValid())
1439 {
1440 std::string template_basename(parent_name);
1441 template_basename.erase (template_basename.find('<'));
1442 ClangASTContext &ast = GetClangASTContext();
1443
1444 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001445 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001446 template_basename.c_str(),
1447 tag_decl_kind,
1448 template_param_infos);
1449 }
1450 return NULL;
1451}
1452
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001453size_t
1454SymbolFileDWARF::ParseChildMembers
1455(
1456 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001457 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001459 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001460 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1462 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001463 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001464 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001465 bool &is_a_class,
1466 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467)
1468{
1469 if (parent_die == NULL)
1470 return 0;
1471
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472 size_t count = 0;
1473 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001474 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001475 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001476
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001477 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1478 {
1479 dw_tag_t tag = die->Tag();
1480
1481 switch (tag)
1482 {
1483 case DW_TAG_member:
Bill Wendling9e5bf872012-04-03 08:45:33 +00001484 case DW_TAG_APPLE_Property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001485 {
1486 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001487 const size_t num_attributes = die->GetAttributes (this,
1488 dwarf_cu,
1489 fixed_form_sizes,
1490 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491 if (num_attributes > 0)
1492 {
1493 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001494 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001496 const char *prop_name = NULL;
1497 const char *prop_getter_name = NULL;
1498 const char *prop_setter_name = NULL;
1499 uint32_t prop_attributes = 0;
1500
1501
Greg Clayton24739922010-10-13 03:15:28 +00001502 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001504 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001505 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506 size_t byte_size = 0;
1507 size_t bit_offset = 0;
1508 size_t bit_size = 0;
1509 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001510 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511 {
1512 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1513 DWARFFormValue form_value;
1514 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1515 {
1516 switch (attr)
1517 {
1518 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1519 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1520 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1521 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1522 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1523 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1524 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1525 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1526 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001527 if (form_value.BlockData())
1528 {
1529 Value initialValue(0);
1530 Value memberOffset(0);
1531 const DataExtractor& debug_info_data = get_debug_info_data();
1532 uint32_t block_length = form_value.Unsigned();
1533 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1534 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1535 NULL, // clang::ASTContext *
1536 NULL, // ClangExpressionVariableList *
1537 NULL, // ClangExpressionDeclMap *
1538 NULL, // RegisterContext *
1539 debug_info_data,
1540 block_offset,
1541 block_length,
1542 eRegisterKindDWARF,
1543 &initialValue,
1544 memberOffset,
1545 NULL))
1546 {
1547 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1548 }
1549 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001550 break;
1551
Greg Clayton8cf05932010-07-22 18:30:50 +00001552 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1b02c172012-03-14 21:00:47 +00001553 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001554 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1555 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1556 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1557 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1558
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001559 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001560 case DW_AT_declaration:
1561 case DW_AT_description:
1562 case DW_AT_mutable:
1563 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564 case DW_AT_sibling:
1565 break;
1566 }
1567 }
1568 }
Sean Callanana6582262012-04-05 00:12:52 +00001569
1570 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00001571 {
Sean Callanana6582262012-04-05 00:12:52 +00001572 ConstString fixed_getter;
1573 ConstString fixed_setter;
1574
1575 // Check if the property getter/setter were provided as full
1576 // names. We want basenames, so we extract them.
1577
1578 if (prop_getter_name && prop_getter_name[0] == '-')
1579 {
1580 ObjCLanguageRuntime::ParseMethodName (prop_getter_name,
1581 NULL,
1582 &fixed_getter,
1583 NULL,
1584 NULL);
1585 prop_getter_name = fixed_getter.GetCString();
1586 }
1587
1588 if (prop_setter_name && prop_setter_name[0] == '-')
1589 {
1590 ObjCLanguageRuntime::ParseMethodName (prop_setter_name,
1591 NULL,
1592 &fixed_setter,
1593 NULL,
1594 NULL);
1595 prop_setter_name = fixed_setter.GetCString();
1596 }
1597
1598 // If the names haven't been provided, they need to be
1599 // filled in.
1600
1601 if (!prop_getter_name)
1602 {
1603 prop_getter_name = prop_name;
1604 }
1605 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
1606 {
1607 StreamString ss;
1608
1609 ss.Printf("set%c%s:",
1610 toupper(prop_name[0]),
1611 &prop_name[1]);
1612
1613 fixed_setter.SetCString(ss.GetData());
1614 prop_setter_name = fixed_setter.GetCString();
1615 }
Sean Callanan751aac62012-03-29 19:07:06 +00001616 }
1617
1618 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001619 // represents fields that are references with bad byte size
1620 // and bit size/offset information such as:
1621 //
1622 // DW_AT_byte_size( 0x00 )
1623 // DW_AT_bit_size( 0x40 )
1624 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1625 //
1626 // So check the bit offset to make sure it is sane, and if
1627 // the values are not sane, remove them. If we don't do this
1628 // then we will end up with a crash if we try to use this
1629 // type in an expression when clang becomes unhappy with its
1630 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001631
Greg Clayton44953932011-10-25 01:25:35 +00001632 if (bit_offset > 128)
1633 {
1634 bit_size = 0;
1635 bit_offset = 0;
1636 }
1637
1638 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001639 if (class_language == eLanguageTypeObjC ||
1640 class_language == eLanguageTypeObjC_plus_plus)
1641 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001642
1643 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1644 {
1645 // Not all compilers will mark the vtable pointer
1646 // member as artificial (llvm-gcc). We can't have
1647 // the virtual members in our classes otherwise it
1648 // throws off all child offsets since we end up
1649 // having and extra pointer sized member in our
1650 // class layouts.
1651 is_artificial = true;
1652 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653
Greg Clayton24739922010-10-13 03:15:28 +00001654 if (is_artificial == false)
1655 {
1656 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001657 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001658 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001659 {
Sean Callanan751aac62012-03-29 19:07:06 +00001660 if (member_type)
1661 {
1662 if (accessibility == eAccessNone)
1663 accessibility = default_accessibility;
1664 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001665
Sean Callanan751aac62012-03-29 19:07:06 +00001666 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
1667 name,
1668 member_type->GetClangLayoutType(),
1669 accessibility,
1670 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00001671
1672 GetClangASTContext().SetMetadata((uintptr_t)field_decl, MakeUserID(die->GetOffset()));
Sean Callanan5b26f272012-02-04 08:49:35 +00001673 }
1674 else
1675 {
Sean Callanan751aac62012-03-29 19:07:06 +00001676 if (name)
1677 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1678 MakeUserID(die->GetOffset()),
1679 name,
1680 encoding_uid);
1681 else
1682 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1683 MakeUserID(die->GetOffset()),
1684 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00001685 }
Sean Callanan751aac62012-03-29 19:07:06 +00001686
1687 if (member_byte_offset != UINT32_MAX || bit_size != 0)
1688 {
1689 /////////////////////////////////////////////////////////////
1690 // How to locate a field given the DWARF debug information
1691 //
1692 // AT_byte_size indicates the size of the word in which the
1693 // bit offset must be interpreted.
1694 //
1695 // AT_data_member_location indicates the byte offset of the
1696 // word from the base address of the structure.
1697 //
1698 // AT_bit_offset indicates how many bits into the word
1699 // (according to the host endianness) the low-order bit of
1700 // the field starts. AT_bit_offset can be negative.
1701 //
1702 // AT_bit_size indicates the size of the field in bits.
1703 /////////////////////////////////////////////////////////////
Sean Callanan5b26f272012-02-04 08:49:35 +00001704
Sean Callanan751aac62012-03-29 19:07:06 +00001705 ByteOrder object_endian = GetObjectFile()->GetModule()->GetArchitecture().GetDefaultEndian();
1706
1707 uint64_t total_bit_offset = 0;
1708
1709 total_bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1710
1711 if (object_endian == eByteOrderLittle)
1712 {
1713 total_bit_offset += byte_size * 8;
1714 total_bit_offset -= (bit_offset + bit_size);
1715 }
1716 else
1717 {
1718 total_bit_offset += bit_offset;
1719 }
1720
1721 layout_info.field_offsets.insert(std::make_pair(field_decl, total_bit_offset));
1722 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001723 }
Sean Callanan751aac62012-03-29 19:07:06 +00001724
Jim Inghame3ae82a2011-11-12 01:36:43 +00001725 if (prop_name != NULL)
1726 {
Sean Callanan751aac62012-03-29 19:07:06 +00001727 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001728
Sean Callanan751aac62012-03-29 19:07:06 +00001729 if (field_decl)
1730 {
1731 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1732 assert (ivar_decl != NULL);
1733 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001734
Jim Inghame3ae82a2011-11-12 01:36:43 +00001735 GetClangASTContext().AddObjCClassProperty (class_clang_type,
1736 prop_name,
Sean Callanan751aac62012-03-29 19:07:06 +00001737 member_type->GetClangLayoutType(),
Jim Inghame3ae82a2011-11-12 01:36:43 +00001738 ivar_decl,
1739 prop_setter_name,
1740 prop_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00001741 prop_attributes,
1742 MakeUserID(die->GetOffset()));
Sean Callanan60217122012-04-13 00:10:03 +00001743
Sean Callananad880762012-04-18 01:06:17 +00001744 if (ivar_decl)
1745 GetClangASTContext().SetMetadata((uintptr_t)ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00001746 }
Greg Clayton24739922010-10-13 03:15:28 +00001747 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001749 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001750 }
1751 break;
1752
1753 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001754 // Let the type parsing code handle this one for us.
1755 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756 break;
1757
1758 case DW_TAG_inheritance:
1759 {
1760 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001761 if (default_accessibility == eAccessNone)
1762 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001763 // TODO: implement DW_TAG_inheritance type parsing
1764 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001765 const size_t num_attributes = die->GetAttributes (this,
1766 dwarf_cu,
1767 fixed_form_sizes,
1768 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769 if (num_attributes > 0)
1770 {
1771 Declaration decl;
1772 DWARFExpression location;
1773 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001774 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775 bool is_virtual = false;
1776 bool is_base_of_class = true;
1777 off_t member_offset = 0;
1778 uint32_t i;
1779 for (i=0; i<num_attributes; ++i)
1780 {
1781 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1782 DWARFFormValue form_value;
1783 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1784 {
1785 switch (attr)
1786 {
1787 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1788 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1789 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1790 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1791 case DW_AT_data_member_location:
1792 if (form_value.BlockData())
1793 {
1794 Value initialValue(0);
1795 Value memberOffset(0);
1796 const DataExtractor& debug_info_data = get_debug_info_data();
1797 uint32_t block_length = form_value.Unsigned();
1798 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001799 if (DWARFExpression::Evaluate (NULL,
1800 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001801 NULL,
1802 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001803 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001804 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001805 block_offset,
1806 block_length,
1807 eRegisterKindDWARF,
1808 &initialValue,
1809 memberOffset,
1810 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001811 {
1812 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1813 }
1814 }
1815 break;
1816
1817 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001818 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819 break;
1820
1821 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1822 default:
1823 case DW_AT_sibling:
1824 break;
1825 }
1826 }
1827 }
1828
Greg Clayton526e5af2010-11-13 03:52:47 +00001829 Type *base_class_type = ResolveTypeUID(encoding_uid);
1830 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001831
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001832 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1833 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001834 if (class_language == eLanguageTypeObjC)
1835 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001836 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001837 }
1838 else
1839 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001840 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001841 accessibility,
1842 is_virtual,
1843 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001844 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001845 }
1846 }
1847 break;
1848
1849 default:
1850 break;
1851 }
1852 }
1853 return count;
1854}
1855
1856
1857clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001858SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001859{
1860 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001861 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862 {
1863 DWARFCompileUnitSP cu_sp;
1864 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1865 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001866 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001867 }
1868 return NULL;
1869}
1870
1871clang::DeclContext*
1872SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1873{
Greg Clayton81c22f62011-10-19 18:09:39 +00001874 if (UserIDMatches(type_uid))
1875 return GetClangDeclContextForDIEOffset (sc, type_uid);
1876 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877}
1878
1879Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001880SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001881{
Greg Clayton81c22f62011-10-19 18:09:39 +00001882 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001883 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001884 DWARFDebugInfo* debug_info = DebugInfo();
1885 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001886 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001887 DWARFCompileUnitSP cu_sp;
1888 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001889 const bool assert_not_being_parsed = true;
1890 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001891 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001892 }
1893 return NULL;
1894}
1895
Greg Claytoncab36a32011-12-08 05:16:30 +00001896Type*
1897SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001898{
Greg Claytoncab36a32011-12-08 05:16:30 +00001899 if (die != NULL)
1900 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001901 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1902 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001903 GetObjectFile()->GetModule()->LogMessage (log.get(),
1904 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1905 die->GetOffset(),
1906 DW_TAG_value_to_name(die->Tag()),
1907 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001908
Greg Claytoncab36a32011-12-08 05:16:30 +00001909 // We might be coming in in the middle of a type tree (a class
1910 // withing a class, an enum within a class), so parse any needed
1911 // parent DIEs before we get to this one...
1912 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1913 switch (decl_ctx_die->Tag())
1914 {
1915 case DW_TAG_structure_type:
1916 case DW_TAG_union_type:
1917 case DW_TAG_class_type:
1918 {
1919 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001920 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001921 GetObjectFile()->GetModule()->LogMessage (log.get(),
1922 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1923 die->GetOffset(),
1924 DW_TAG_value_to_name(die->Tag()),
1925 die->GetName(this, cu),
1926 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00001927//
1928// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
1929// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
1930// {
1931// if (log)
1932// GetObjectFile()->GetModule()->LogMessage (log.get(),
1933// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
1934// die->GetOffset(),
1935// DW_TAG_value_to_name(die->Tag()),
1936// die->GetName(this, cu),
1937// decl_ctx_die->GetOffset());
1938// // Ask the type to complete itself if it already hasn't since if we
1939// // want a function (method or static) from a class, the class must
1940// // create itself and add it's own methods and class functions.
1941// if (parent_type)
1942// parent_type->GetClangFullType();
1943// }
Greg Claytoncab36a32011-12-08 05:16:30 +00001944 }
1945 break;
1946
1947 default:
1948 break;
1949 }
1950 return ResolveType (cu, die);
1951 }
1952 return NULL;
1953}
1954
Greg Clayton6beaaa62011-01-17 03:46:26 +00001955// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1956// SymbolFileDWARF objects to detect if this DWARF file is the one that
1957// can resolve a clang_type.
1958bool
1959SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1960{
1961 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1962 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1963 return die != NULL;
1964}
1965
1966
Greg Clayton1be10fc2010-09-29 01:12:09 +00001967lldb::clang_type_t
1968SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1969{
1970 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001971 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1972 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001973 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001974 {
1975 // We have already resolved this type...
1976 return clang_type;
1977 }
1978 // Once we start resolving this type, remove it from the forward declaration
1979 // map in case anyone child members or other types require this type to get resolved.
1980 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1981 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001982 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001983
Greg Clayton1be10fc2010-09-29 01:12:09 +00001984
Greg Clayton85ae2e12011-10-18 23:36:41 +00001985 // Disable external storage for this type so we don't get anymore
1986 // clang::ExternalASTSource queries for this type.
1987 ClangASTContext::SetHasExternalStorage (clang_type, false);
1988
Greg Clayton450e3f32010-10-12 02:24:53 +00001989 DWARFDebugInfo* debug_info = DebugInfo();
1990
Greg Clayton53eb1c22012-04-02 22:59:12 +00001991 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001992 Type *type = m_die_to_type.lookup (die);
1993
1994 const dw_tag_t tag = die->Tag();
1995
Greg Claytonbaf95da2012-03-30 23:50:54 +00001996 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00001997 if (log)
Greg Claytonbaf95da2012-03-30 23:50:54 +00001998 {
Greg Claytond61c0fc2012-04-23 22:55:20 +00001999 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log.get(),
2000 "0x%8.8llx: %s '%s' resolving forward declaration...",
2001 MakeUserID(die->GetOffset()),
2002 DW_TAG_value_to_name(tag),
2003 type->GetName().AsCString());
Greg Claytonbaf95da2012-03-30 23:50:54 +00002004
Greg Claytonbaf95da2012-03-30 23:50:54 +00002005 }
Greg Clayton1be10fc2010-09-29 01:12:09 +00002006 assert (clang_type);
2007 DWARFDebugInfoEntry::Attributes attributes;
2008
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002009 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002010
2011 switch (tag)
2012 {
2013 case DW_TAG_structure_type:
2014 case DW_TAG_union_type:
2015 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002016 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002017 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002018
Greg Clayton1be10fc2010-09-29 01:12:09 +00002019 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002020 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002021 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002022
Sean Callanan77a1fd32012-02-27 20:07:01 +00002023 LanguageType class_language = eLanguageTypeUnknown;
2024 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
2025 if (is_objc_class)
Greg Clayton219cf312012-03-30 00:51:13 +00002026 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002027 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002028 // For objective C we don't start the definition when
2029 // the class is created.
2030 ast.StartTagDeclarationDefinition (clang_type);
2031 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002032
2033 int tag_decl_kind = -1;
2034 AccessType default_accessibility = eAccessNone;
2035 if (tag == DW_TAG_structure_type)
2036 {
2037 tag_decl_kind = clang::TTK_Struct;
2038 default_accessibility = eAccessPublic;
2039 }
2040 else if (tag == DW_TAG_union_type)
2041 {
2042 tag_decl_kind = clang::TTK_Union;
2043 default_accessibility = eAccessPublic;
2044 }
2045 else if (tag == DW_TAG_class_type)
2046 {
2047 tag_decl_kind = clang::TTK_Class;
2048 default_accessibility = eAccessPrivate;
2049 }
2050
Greg Clayton53eb1c22012-04-02 22:59:12 +00002051 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002052 std::vector<clang::CXXBaseSpecifier *> base_classes;
2053 std::vector<int> member_accessibilities;
2054 bool is_a_class = false;
2055 // Parse members and base classes first
2056 DWARFDIECollection member_function_dies;
2057
2058 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002059 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002060 die,
2061 clang_type,
2062 class_language,
2063 base_classes,
2064 member_accessibilities,
2065 member_function_dies,
2066 default_accessibility,
2067 is_a_class,
2068 layout_info);
2069
2070 // Now parse any methods if there were any...
2071 size_t num_functions = member_function_dies.Size();
2072 if (num_functions > 0)
2073 {
2074 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002075 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002076 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002077 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002078 }
2079
2080 if (class_language == eLanguageTypeObjC)
2081 {
Greg Clayton84db9102012-03-26 23:03:23 +00002082 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(ast.getASTContext(), clang_type));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002083 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00002084 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002085
Sean Callanan77a1fd32012-02-27 20:07:01 +00002086 DIEArray method_die_offsets;
2087 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002088 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002089 if (m_apple_objc_ap.get())
2090 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
2091 }
2092 else
2093 {
2094 if (!m_indexed)
2095 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002096
Sean Callanan77a1fd32012-02-27 20:07:01 +00002097 ConstString class_name (class_str.c_str());
2098 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2099 }
2100
2101 if (!method_die_offsets.empty())
2102 {
2103 DWARFDebugInfo* debug_info = DebugInfo();
2104
2105 DWARFCompileUnit* method_cu = NULL;
2106 const size_t num_matches = method_die_offsets.size();
2107 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002108 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002109 const dw_offset_t die_offset = method_die_offsets[i];
2110 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2111
2112 if (method_die)
2113 ResolveType (method_cu, method_die);
2114 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002115 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002116 if (m_using_apple_tables)
2117 {
2118 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2119 die_offset, class_str.c_str());
2120 }
2121 }
2122 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002123 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002124 }
2125 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002126
2127 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2128 // need to tell the clang type it is actually a class.
2129 if (class_language != eLanguageTypeObjC)
2130 {
2131 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2132 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2133 }
2134
2135 // Since DW_TAG_structure_type gets used for both classes
2136 // and structures, we may need to set any DW_TAG_member
2137 // fields to have a "private" access if none was specified.
2138 // When we parsed the child members we tracked that actual
2139 // accessibility value for each DW_TAG_member in the
2140 // "member_accessibilities" array. If the value for the
2141 // member is zero, then it was set to the "default_accessibility"
2142 // which for structs was "public". Below we correct this
2143 // by setting any fields to "private" that weren't correctly
2144 // set.
2145 if (is_a_class && !member_accessibilities.empty())
2146 {
2147 // This is a class and all members that didn't have
2148 // their access specified are private.
2149 ast.SetDefaultAccessForRecordFields (clang_type,
2150 eAccessPrivate,
2151 &member_accessibilities.front(),
2152 member_accessibilities.size());
2153 }
2154
2155 if (!base_classes.empty())
2156 {
2157 ast.SetBaseClassesForClassType (clang_type,
2158 &base_classes.front(),
2159 base_classes.size());
2160
2161 // Clang will copy each CXXBaseSpecifier in "base_classes"
2162 // so we have to free them all.
2163 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2164 base_classes.size());
2165 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002166 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002167 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002168
2169 ast.BuildIndirectFields (clang_type);
2170
Sean Callanan77a1fd32012-02-27 20:07:01 +00002171 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002172
Greg Claytoncaab74e2012-01-28 00:48:57 +00002173 if (!layout_info.field_offsets.empty())
2174 {
2175 if (type)
2176 layout_info.bit_size = type->GetByteSize() * 8;
2177 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002178 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Greg Claytoncaab74e2012-01-28 00:48:57 +00002179 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
2180 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
2181 if (record_type)
2182 {
2183 const clang::RecordDecl *record_decl = record_type->getDecl();
2184
2185 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002186 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002187 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002188 "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 +00002189 clang_type,
2190 record_decl,
2191 layout_info.bit_size,
2192 layout_info.alignment,
2193 (uint32_t)layout_info.field_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002194
Greg Clayton821ac6d2012-01-28 02:22:27 +00002195 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
2196 for (pos = layout_info.field_offsets.begin(); pos != end; ++pos)
2197 {
2198 GetObjectFile()->GetModule()->LogMessage (log.get(),
2199 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field = { bit_offset=%u, name='%s' }",
2200 clang_type,
2201 (uint32_t)pos->second,
2202 pos->first->getNameAsString().c_str());
2203 }
2204 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002205 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2206 }
2207 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002208 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002209
Greg Claytonc93237c2010-10-01 20:48:32 +00002210 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002211
2212 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002213 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002214 if (die->HasChildren())
2215 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002216 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
2217 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002218 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002219 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002220 return clang_type;
2221
2222 default:
2223 assert(false && "not a forward clang type decl!");
2224 break;
2225 }
2226 return NULL;
2227}
2228
Greg Claytonc685f8e2010-09-15 04:15:46 +00002229Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002230SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002231{
2232 if (type_die != NULL)
2233 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002234 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002235
Greg Claytonc685f8e2010-09-15 04:15:46 +00002236 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002237 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002238
Greg Clayton24739922010-10-13 03:15:28 +00002239 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002240 {
2241 if (type != DIE_IS_BEING_PARSED)
2242 return type;
2243
2244 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002245 type_die->GetOffset(),
2246 DW_TAG_value_to_name(type_die->Tag()),
2247 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002248
2249 }
2250 else
2251 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002252 }
2253 return NULL;
2254}
2255
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002256CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002257SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002258{
2259 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002260 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002261 {
2262 // The symbol vendor doesn't know about this compile unit, we
2263 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002264 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002265 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002266 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002267}
2268
2269bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002270SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002271{
2272 sc.Clear();
2273 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002274 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002275
Greg Clayton81c22f62011-10-19 18:09:39 +00002276 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002277 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002278 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002279
2280 if (sc.function)
2281 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002282 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002283 return true;
2284 }
2285
2286 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002287}
2288
2289uint32_t
2290SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2291{
2292 Timer scoped_timer(__PRETTY_FUNCTION__,
2293 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002294 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002295 so_addr.GetOffset(),
2296 resolve_scope);
2297 uint32_t resolved = 0;
2298 if (resolve_scope & ( eSymbolContextCompUnit |
2299 eSymbolContextFunction |
2300 eSymbolContextBlock |
2301 eSymbolContextLineEntry))
2302 {
2303 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2304
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002305 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002306 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002307 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002308 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002309 if (cu_offset != DW_INVALID_OFFSET)
2310 {
2311 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002312 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2313 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002314 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002315 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002316 assert(sc.comp_unit != NULL);
2317 resolved |= eSymbolContextCompUnit;
2318
2319 if (resolve_scope & eSymbolContextLineEntry)
2320 {
2321 LineTable *line_table = sc.comp_unit->GetLineTable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002322 if (line_table != NULL)
2323 {
2324 if (so_addr.IsLinkedAddress())
2325 {
2326 Address linked_addr (so_addr);
2327 linked_addr.ResolveLinkedAddress();
2328 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2329 {
2330 resolved |= eSymbolContextLineEntry;
2331 }
2332 }
2333 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
2334 {
2335 resolved |= eSymbolContextLineEntry;
2336 }
2337 }
2338 }
2339
2340 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2341 {
2342 DWARFDebugInfoEntry *function_die = NULL;
2343 DWARFDebugInfoEntry *block_die = NULL;
2344 if (resolve_scope & eSymbolContextBlock)
2345 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002346 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002347 }
2348 else
2349 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002350 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002351 }
2352
2353 if (function_die != NULL)
2354 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002355 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002356 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002357 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002358 }
Greg Clayton12b834d2012-03-29 21:43:25 +00002359 else
2360 {
2361 // We might have had a compile unit that had discontiguous
2362 // address ranges where the gaps are symbols that don't have
2363 // any debug info. Discontiguous compile unit address ranges
2364 // should only happen when there aren't other functions from
2365 // other compile units in these gaps. This helps keep the size
2366 // of the aranges down.
2367 sc.comp_unit = NULL;
2368 resolved &= ~eSymbolContextCompUnit;
2369 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002370
2371 if (sc.function != NULL)
2372 {
2373 resolved |= eSymbolContextFunction;
2374
2375 if (resolve_scope & eSymbolContextBlock)
2376 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002377 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002378
2379 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002380 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002381 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002382 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002383 if (sc.block)
2384 resolved |= eSymbolContextBlock;
2385 }
2386 }
2387 }
2388 }
2389 }
2390 }
2391 }
2392 return resolved;
2393}
2394
2395
2396
2397uint32_t
2398SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2399{
2400 const uint32_t prev_size = sc_list.GetSize();
2401 if (resolve_scope & eSymbolContextCompUnit)
2402 {
2403 DWARFDebugInfo* debug_info = DebugInfo();
2404 if (debug_info)
2405 {
2406 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002407 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002408
Greg Clayton53eb1c22012-04-02 22:59:12 +00002409 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002410 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002411 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002412 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
2413 if (check_inlines || file_spec_matches_cu_file_spec)
2414 {
2415 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002416 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002417 assert(sc.comp_unit != NULL);
2418
2419 uint32_t file_idx = UINT32_MAX;
2420
2421 // If we are looking for inline functions only and we don't
2422 // find it in the support files, we are done.
2423 if (check_inlines)
2424 {
Jim Ingham87df91b2011-09-23 00:54:11 +00002425 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002426 if (file_idx == UINT32_MAX)
2427 continue;
2428 }
2429
2430 if (line != 0)
2431 {
2432 LineTable *line_table = sc.comp_unit->GetLineTable();
2433
2434 if (line_table != NULL && line != 0)
2435 {
2436 // We will have already looked up the file index if
2437 // we are searching for inline entries.
2438 if (!check_inlines)
Jim Ingham87df91b2011-09-23 00:54:11 +00002439 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002440
2441 if (file_idx != UINT32_MAX)
2442 {
2443 uint32_t found_line;
2444 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2445 found_line = sc.line_entry.line;
2446
Greg Clayton016a95e2010-09-14 02:20:48 +00002447 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002448 {
2449 sc.function = NULL;
2450 sc.block = NULL;
2451 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2452 {
2453 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2454 if (file_vm_addr != LLDB_INVALID_ADDRESS)
2455 {
2456 DWARFDebugInfoEntry *function_die = NULL;
2457 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002458 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002459
2460 if (function_die != NULL)
2461 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002462 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002463 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002464 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002465 }
2466
2467 if (sc.function != NULL)
2468 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002469 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002470
2471 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002472 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002473 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002474 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002475 }
2476 }
2477 }
2478
2479 sc_list.Append(sc);
2480 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2481 }
2482 }
2483 }
2484 else if (file_spec_matches_cu_file_spec && !check_inlines)
2485 {
2486 // only append the context if we aren't looking for inline call sites
2487 // by file and line and if the file spec matches that of the compile unit
2488 sc_list.Append(sc);
2489 }
2490 }
2491 else if (file_spec_matches_cu_file_spec && !check_inlines)
2492 {
2493 // only append the context if we aren't looking for inline call sites
2494 // by file and line and if the file spec matches that of the compile unit
2495 sc_list.Append(sc);
2496 }
2497
2498 if (!check_inlines)
2499 break;
2500 }
2501 }
2502 }
2503 }
2504 return sc_list.GetSize() - prev_size;
2505}
2506
2507void
2508SymbolFileDWARF::Index ()
2509{
2510 if (m_indexed)
2511 return;
2512 m_indexed = true;
2513 Timer scoped_timer (__PRETTY_FUNCTION__,
2514 "SymbolFileDWARF::Index (%s)",
2515 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2516
2517 DWARFDebugInfo* debug_info = DebugInfo();
2518 if (debug_info)
2519 {
2520 uint32_t cu_idx = 0;
2521 const uint32_t num_compile_units = GetNumCompileUnits();
2522 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2523 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002524 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002525
Greg Clayton53eb1c22012-04-02 22:59:12 +00002526 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002527
Greg Clayton53eb1c22012-04-02 22:59:12 +00002528 dwarf_cu->Index (cu_idx,
2529 m_function_basename_index,
2530 m_function_fullname_index,
2531 m_function_method_index,
2532 m_function_selector_index,
2533 m_objc_class_selectors_index,
2534 m_global_index,
2535 m_type_index,
2536 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002537
2538 // Keep memory down by clearing DIEs if this generate function
2539 // caused them to be parsed
2540 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002541 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002542 }
2543
Greg Claytond4a2b372011-09-12 23:21:58 +00002544 m_function_basename_index.Finalize();
2545 m_function_fullname_index.Finalize();
2546 m_function_method_index.Finalize();
2547 m_function_selector_index.Finalize();
2548 m_objc_class_selectors_index.Finalize();
2549 m_global_index.Finalize();
2550 m_type_index.Finalize();
2551 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002552
Greg Clayton24739922010-10-13 03:15:28 +00002553#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002554 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002555 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002556 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2557 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002558 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2559 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2560 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2561 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2562 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2563 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002564 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002565 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002566#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002567 }
2568}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002569
2570bool
2571SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2572{
2573 if (namespace_decl == NULL)
2574 {
2575 // Invalid namespace decl which means we aren't matching only things
2576 // in this symbol file, so return true to indicate it matches this
2577 // symbol file.
2578 return true;
2579 }
2580
2581 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2582
2583 if (namespace_ast == NULL)
2584 return true; // No AST in the "namespace_decl", return true since it
2585 // could then match any symbol file, including this one
2586
2587 if (namespace_ast == GetClangASTContext().getASTContext())
2588 return true; // The ASTs match, return true
2589
2590 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002591 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2592
2593 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002594 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002595
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002596 return false;
2597}
2598
Greg Clayton2506a7a2011-10-12 23:34:26 +00002599bool
2600SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2601 DWARFCompileUnit* cu,
2602 const DWARFDebugInfoEntry* die)
2603{
2604 // No namespace specified, so the answesr i
2605 if (namespace_decl == NULL)
2606 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002607
2608 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002609
Greg Clayton437a1352012-04-09 22:43:43 +00002610 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
2611 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002612 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00002613 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00002614 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00002615
Greg Clayton2506a7a2011-10-12 23:34:26 +00002616 if (clang_namespace_decl)
2617 {
2618 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002619 {
2620 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002621 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002622 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002623 }
2624
Greg Clayton437a1352012-04-09 22:43:43 +00002625 if (clang_namespace_decl == die_clang_decl_ctx)
2626 return true;
2627 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00002628 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00002629 }
2630 else
2631 {
2632 // We have a namespace_decl that was not NULL but it contained
2633 // a NULL "clang::NamespaceDecl", so this means the global namespace
2634 // So as long the the contained decl context DIE isn't a namespace
2635 // we should be ok.
2636 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2637 return true;
2638 }
2639 }
Sean Callananebe60672011-10-13 21:50:33 +00002640
2641 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002642 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002643
Greg Clayton2506a7a2011-10-12 23:34:26 +00002644 return false;
2645}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002646uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002647SymbolFileDWARF::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 +00002648{
Greg Clayton21f2a492011-10-06 00:09:08 +00002649 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2650
2651 if (log)
2652 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002653 GetObjectFile()->GetModule()->LogMessage (log.get(),
2654 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2655 name.GetCString(),
2656 namespace_decl,
2657 append,
2658 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002659 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002660
2661 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2662 return 0;
2663
Greg Claytonc685f8e2010-09-15 04:15:46 +00002664 DWARFDebugInfo* info = DebugInfo();
2665 if (info == NULL)
2666 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002667
2668 // If we aren't appending the results to this list, then clear the list
2669 if (!append)
2670 variables.Clear();
2671
2672 // Remember how many variables are in the list before we search in case
2673 // we are appending the results to a variable list.
2674 const uint32_t original_size = variables.GetSize();
2675
Greg Claytond4a2b372011-09-12 23:21:58 +00002676 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002677
Greg Clayton97fbc342011-10-20 22:30:33 +00002678 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002679 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002680 if (m_apple_names_ap.get())
2681 {
2682 const char *name_cstr = name.GetCString();
2683 const char *base_name_start;
2684 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002685
Greg Clayton97fbc342011-10-20 22:30:33 +00002686 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2687 base_name_start = name_cstr;
2688
2689 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2690 }
Greg Clayton7f995132011-10-04 22:41:51 +00002691 }
2692 else
2693 {
2694 // Index the DWARF if we haven't already
2695 if (!m_indexed)
2696 Index ();
2697
2698 m_global_index.Find (name, die_offsets);
2699 }
2700
Greg Clayton437a1352012-04-09 22:43:43 +00002701 const size_t num_die_matches = die_offsets.size();
2702 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002703 {
Greg Clayton7f995132011-10-04 22:41:51 +00002704 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002705 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002706 assert (sc.module_sp);
2707
Greg Claytond4a2b372011-09-12 23:21:58 +00002708 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002709 DWARFCompileUnit* dwarf_cu = NULL;
2710 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00002711 bool done = false;
2712 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002713 {
2714 const dw_offset_t die_offset = die_offsets[i];
2715 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002716
Greg Clayton95d87902011-11-11 03:16:25 +00002717 if (die)
2718 {
Greg Clayton437a1352012-04-09 22:43:43 +00002719 switch (die->Tag())
2720 {
2721 default:
2722 case DW_TAG_subprogram:
2723 case DW_TAG_inlined_subroutine:
2724 case DW_TAG_try_block:
2725 case DW_TAG_catch_block:
2726 break;
2727
2728 case DW_TAG_variable:
2729 {
2730 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2731 assert(sc.comp_unit != NULL);
2732
2733 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2734 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002735
Greg Clayton437a1352012-04-09 22:43:43 +00002736 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002737
Greg Clayton437a1352012-04-09 22:43:43 +00002738 if (variables.GetSize() - original_size >= max_matches)
2739 done = true;
2740 }
2741 break;
2742 }
Greg Clayton95d87902011-11-11 03:16:25 +00002743 }
2744 else
2745 {
2746 if (m_using_apple_tables)
2747 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002748 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2749 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002750 }
2751 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002752 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002753 }
2754
2755 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002756 const uint32_t num_matches = variables.GetSize() - original_size;
2757 if (log && num_matches > 0)
2758 {
2759 GetObjectFile()->GetModule()->LogMessage (log.get(),
2760 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
2761 name.GetCString(),
2762 namespace_decl,
2763 append,
2764 max_matches,
2765 num_matches);
2766 }
2767 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002768}
2769
2770uint32_t
2771SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2772{
Greg Clayton21f2a492011-10-06 00:09:08 +00002773 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2774
2775 if (log)
2776 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002777 GetObjectFile()->GetModule()->LogMessage (log.get(),
2778 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2779 regex.GetText(),
2780 append,
2781 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002782 }
2783
Greg Claytonc685f8e2010-09-15 04:15:46 +00002784 DWARFDebugInfo* info = DebugInfo();
2785 if (info == NULL)
2786 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002787
2788 // If we aren't appending the results to this list, then clear the list
2789 if (!append)
2790 variables.Clear();
2791
2792 // Remember how many variables are in the list before we search in case
2793 // we are appending the results to a variable list.
2794 const uint32_t original_size = variables.GetSize();
2795
Greg Clayton7f995132011-10-04 22:41:51 +00002796 DIEArray die_offsets;
2797
Greg Clayton97fbc342011-10-20 22:30:33 +00002798 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002799 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002800 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002801 {
2802 DWARFMappedHash::DIEInfoArray hash_data_array;
2803 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2804 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2805 }
Greg Clayton7f995132011-10-04 22:41:51 +00002806 }
2807 else
2808 {
2809 // Index the DWARF if we haven't already
2810 if (!m_indexed)
2811 Index ();
2812
2813 m_global_index.Find (regex, die_offsets);
2814 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002815
Greg Claytonc685f8e2010-09-15 04:15:46 +00002816 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002817 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002818 assert (sc.module_sp);
2819
Greg Claytond4a2b372011-09-12 23:21:58 +00002820 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002821 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002822 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002823 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002824 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002825 DWARFDebugInfo* debug_info = DebugInfo();
2826 for (size_t i=0; i<num_matches; ++i)
2827 {
2828 const dw_offset_t die_offset = die_offsets[i];
2829 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002830
2831 if (die)
2832 {
2833 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002834
Greg Clayton95d87902011-11-11 03:16:25 +00002835 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002836
Greg Clayton95d87902011-11-11 03:16:25 +00002837 if (variables.GetSize() - original_size >= max_matches)
2838 break;
2839 }
2840 else
2841 {
2842 if (m_using_apple_tables)
2843 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002844 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2845 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002846 }
2847 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002848 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002849 }
2850
2851 // Return the number of variable that were appended to the list
2852 return variables.GetSize() - original_size;
2853}
2854
Greg Claytonaa044962011-10-13 00:59:38 +00002855
Jim Ingham4cda6e02011-10-07 22:23:45 +00002856bool
2857SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2858 DWARFCompileUnit *&dwarf_cu,
2859 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002860{
Greg Claytonaa044962011-10-13 00:59:38 +00002861 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2862 return ResolveFunction (dwarf_cu, die, sc_list);
2863}
2864
2865
2866bool
2867SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2868 const DWARFDebugInfoEntry *die,
2869 SymbolContextList& sc_list)
2870{
Greg Clayton9e315582011-09-02 04:03:59 +00002871 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002872
2873 if (die == NULL)
2874 return false;
2875
Jim Ingham4cda6e02011-10-07 22:23:45 +00002876 // If we were passed a die that is not a function, just return false...
2877 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2878 return false;
2879
2880 const DWARFDebugInfoEntry* inlined_die = NULL;
2881 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002882 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002883 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002884
Jim Ingham4cda6e02011-10-07 22:23:45 +00002885 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002886 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002887 if (die->Tag() == DW_TAG_subprogram)
2888 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002889 }
2890 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002891 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002892 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002893 {
2894 Address addr;
2895 // Parse all blocks if needed
2896 if (inlined_die)
2897 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002898 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00002899 assert (sc.block != NULL);
2900 if (sc.block->GetStartAddress (addr) == false)
2901 addr.Clear();
2902 }
2903 else
2904 {
2905 sc.block = NULL;
2906 addr = sc.function->GetAddressRange().GetBaseAddress();
2907 }
2908
2909 if (addr.IsValid())
2910 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002911 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002912 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002913 }
2914 }
2915
Greg Claytonaa044962011-10-13 00:59:38 +00002916 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002917}
2918
Greg Clayton7f995132011-10-04 22:41:51 +00002919void
2920SymbolFileDWARF::FindFunctions (const ConstString &name,
2921 const NameToDIE &name_to_die,
2922 SymbolContextList& sc_list)
2923{
Greg Claytond4a2b372011-09-12 23:21:58 +00002924 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002925 if (name_to_die.Find (name, die_offsets))
2926 {
2927 ParseFunctions (die_offsets, sc_list);
2928 }
2929}
2930
2931
2932void
2933SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2934 const NameToDIE &name_to_die,
2935 SymbolContextList& sc_list)
2936{
2937 DIEArray die_offsets;
2938 if (name_to_die.Find (regex, die_offsets))
2939 {
2940 ParseFunctions (die_offsets, sc_list);
2941 }
2942}
2943
2944
2945void
2946SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2947 const DWARFMappedHash::MemoryTable &memory_table,
2948 SymbolContextList& sc_list)
2949{
2950 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002951 DWARFMappedHash::DIEInfoArray hash_data_array;
2952 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002953 {
Greg Claytond1767f02011-12-08 02:13:16 +00002954 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002955 ParseFunctions (die_offsets, sc_list);
2956 }
2957}
2958
2959void
2960SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2961 SymbolContextList& sc_list)
2962{
2963 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002964 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002965 {
Greg Clayton7f995132011-10-04 22:41:51 +00002966 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00002967
2968 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002969 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002970 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002971 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00002972 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002973 }
2974 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002975}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002976
Jim Ingham4cda6e02011-10-07 22:23:45 +00002977bool
2978SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2979 const DWARFCompileUnit *dwarf_cu,
2980 uint32_t name_type_mask,
2981 const char *partial_name,
2982 const char *base_name_start,
2983 const char *base_name_end)
2984{
2985 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2986 if (name_type_mask == eFunctionNameTypeMethod
2987 || name_type_mask == eFunctionNameTypeBase)
2988 {
Greg Claytonf0705c82011-10-22 03:33:13 +00002989 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2990 if (!containing_decl_ctx)
2991 return false;
2992
2993 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
2994
2995 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2996 return false;
2997 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2998 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002999 }
3000
3001 // Now we need to check whether the name we got back for this type matches the extra specifications
3002 // that were in the name we're looking up:
3003 if (base_name_start != partial_name || *base_name_end != '\0')
3004 {
3005 // First see if the stuff to the left matches the full name. To do that let's see if
3006 // we can pull out the mips linkage name attribute:
3007
3008 Mangled best_name;
3009
3010 DWARFDebugInfoEntry::Attributes attributes;
3011 die->GetAttributes(this, dwarf_cu, NULL, attributes);
3012 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
3013 if (idx != UINT32_MAX)
3014 {
3015 DWARFFormValue form_value;
3016 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3017 {
3018 const char *name = form_value.AsCString(&get_debug_str_data());
3019 best_name.SetValue (name, true);
3020 }
3021 }
3022 if (best_name)
3023 {
3024 const char *demangled = best_name.GetDemangledName().GetCString();
3025 if (demangled)
3026 {
3027 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003028 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3029 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003030 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003031 else
3032 {
3033 // Sort out the case where our name is something like "Process::Destroy" and the match is
3034 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3035 // namespace boundaries...
3036
3037 if (partial_name[0] == ':' && partial_name[1] == ':')
3038 {
3039 // The partial name was already on a namespace boundary so all matches are good.
3040 return true;
3041 }
3042 else if (partial_in_demangled == demangled)
3043 {
3044 // They both start the same, so this is an good match.
3045 return true;
3046 }
3047 else
3048 {
3049 if (partial_in_demangled - demangled == 1)
3050 {
3051 // Only one character difference, can't be a namespace boundary...
3052 return false;
3053 }
3054 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3055 {
3056 // We are on a namespace boundary, so this is also good.
3057 return true;
3058 }
3059 else
3060 return false;
3061 }
3062 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003063 }
3064 }
3065 }
3066
3067 return true;
3068}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003069
Greg Clayton0c5cd902010-06-28 21:30:43 +00003070uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003071SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003072 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003073 uint32_t name_type_mask,
3074 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003075 bool append,
3076 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003077{
3078 Timer scoped_timer (__PRETTY_FUNCTION__,
3079 "SymbolFileDWARF::FindFunctions (name = '%s')",
3080 name.AsCString());
3081
Greg Clayton21f2a492011-10-06 00:09:08 +00003082 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3083
3084 if (log)
3085 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003086 GetObjectFile()->GetModule()->LogMessage (log.get(),
3087 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3088 name.GetCString(),
3089 name_type_mask,
3090 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003091 }
3092
Greg Clayton0c5cd902010-06-28 21:30:43 +00003093 // If we aren't appending the results to this list, then clear the list
3094 if (!append)
3095 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003096
3097 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3098 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003099
3100 // If name is empty then we won't find anything.
3101 if (name.IsEmpty())
3102 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003103
3104 // Remember how many sc_list are in the list before we search in case
3105 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003106
Greg Clayton9e315582011-09-02 04:03:59 +00003107 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00003108
Jim Ingham4cda6e02011-10-07 22:23:45 +00003109 const char *name_cstr = name.GetCString();
3110 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
3111 const char *base_name_start = name_cstr;
3112 const char *base_name_end = name_cstr + strlen(name_cstr);
3113
3114 if (name_type_mask & eFunctionNameTypeAuto)
3115 {
3116 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
3117 effective_name_type_mask = eFunctionNameTypeFull;
3118 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
3119 effective_name_type_mask = eFunctionNameTypeFull;
3120 else
3121 {
3122 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3123 effective_name_type_mask |= eFunctionNameTypeSelector;
3124
3125 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3126 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
3127 }
3128 }
3129 else
3130 {
3131 effective_name_type_mask = name_type_mask;
3132 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
3133 {
3134 // If they've asked for a CPP method or function name and it can't be that, we don't
3135 // even need to search for CPP methods or names.
3136 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3137 {
3138 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
3139 if (effective_name_type_mask == eFunctionNameTypeNone)
3140 return 0;
3141 }
3142 }
3143
3144 if (effective_name_type_mask & eFunctionNameTypeSelector)
3145 {
3146 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3147 {
3148 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
3149 if (effective_name_type_mask == eFunctionNameTypeNone)
3150 return 0;
3151 }
3152 }
3153 }
3154
3155 DWARFDebugInfo* info = DebugInfo();
3156 if (info == NULL)
3157 return 0;
3158
Greg Claytonaa044962011-10-13 00:59:38 +00003159 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003160 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003161 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003162 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003163 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003164
3165 DIEArray die_offsets;
3166
3167 uint32_t num_matches = 0;
3168
3169 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003170 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003171 // If they asked for the full name, match what they typed. At some point we may
3172 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3173 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003174 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003175 for (uint32_t i = 0; i < num_matches; i++)
3176 {
Greg Clayton95d87902011-11-11 03:16:25 +00003177 const dw_offset_t die_offset = die_offsets[i];
3178 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003179 if (die)
3180 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003181 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3182 continue;
3183
Sean Callanan9df05fb2012-02-10 22:52:19 +00003184 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3185 continue;
3186
Greg Claytonaa044962011-10-13 00:59:38 +00003187 ResolveFunction (dwarf_cu, die, sc_list);
3188 }
Greg Clayton95d87902011-11-11 03:16:25 +00003189 else
3190 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003191 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3192 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003193 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003194 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003195 }
3196 else
3197 {
3198 if (effective_name_type_mask & eFunctionNameTypeSelector)
3199 {
3200 if (namespace_decl && *namespace_decl)
3201 return 0; // no selectors in namespaces
3202
3203 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3204 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3205 // and if it is an ObjC method name, we're good.
3206
3207 for (uint32_t i = 0; i < num_matches; i++)
3208 {
Greg Clayton95d87902011-11-11 03:16:25 +00003209 const dw_offset_t die_offset = die_offsets[i];
3210 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003211 if (die)
3212 {
3213 const char *die_name = die->GetName(this, dwarf_cu);
3214 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003215 {
3216 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3217 continue;
3218
Greg Clayton97fbc342011-10-20 22:30:33 +00003219 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003220 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003221 }
Greg Clayton95d87902011-11-11 03:16:25 +00003222 else
3223 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003224 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3225 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003226 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003227 }
3228 die_offsets.clear();
3229 }
3230
3231 if (effective_name_type_mask & eFunctionNameTypeMethod
3232 || effective_name_type_mask & eFunctionNameTypeBase)
3233 {
3234 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3235 (namespace_decl && *namespace_decl))
3236 return 0; // no methods in namespaces
3237
3238 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3239 // extract the base name, look that up, and if there is any other information in the name we were
3240 // passed in we have to post-filter based on that.
3241
3242 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3243 std::string base_name(base_name_start, base_name_end - base_name_start);
3244 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3245
3246 for (uint32_t i = 0; i < num_matches; i++)
3247 {
Greg Clayton95d87902011-11-11 03:16:25 +00003248 const dw_offset_t die_offset = die_offsets[i];
3249 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003250 if (die)
3251 {
3252 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3253 continue;
3254
3255 if (!FunctionDieMatchesPartialName(die,
3256 dwarf_cu,
3257 effective_name_type_mask,
3258 name_cstr,
3259 base_name_start,
3260 base_name_end))
3261 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003262
3263 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3264 continue;
Greg Clayton97fbc342011-10-20 22:30:33 +00003265
3266 // If we get to here, the die is good, and we should add it:
3267 ResolveFunction (dwarf_cu, die, sc_list);
3268 }
Greg Clayton95d87902011-11-11 03:16:25 +00003269 else
3270 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003271 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3272 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003273 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003274 }
3275 die_offsets.clear();
3276 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003277 }
3278 }
Greg Clayton7f995132011-10-04 22:41:51 +00003279 }
3280 else
3281 {
3282
3283 // Index the DWARF if we haven't already
3284 if (!m_indexed)
3285 Index ();
3286
Greg Clayton7f995132011-10-04 22:41:51 +00003287 if (name_type_mask & eFunctionNameTypeFull)
3288 FindFunctions (name, m_function_fullname_index, sc_list);
3289
Jim Ingham4cda6e02011-10-07 22:23:45 +00003290 std::string base_name(base_name_start, base_name_end - base_name_start);
3291 ConstString base_name_const(base_name.c_str());
3292 DIEArray die_offsets;
3293 DWARFCompileUnit *dwarf_cu = NULL;
3294
3295 if (effective_name_type_mask & eFunctionNameTypeBase)
3296 {
3297 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003298 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003299 {
Greg Claytonaa044962011-10-13 00:59:38 +00003300 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3301 if (die)
3302 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003303 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3304 continue;
3305
Greg Claytonaa044962011-10-13 00:59:38 +00003306 if (!FunctionDieMatchesPartialName(die,
3307 dwarf_cu,
3308 effective_name_type_mask,
3309 name_cstr,
3310 base_name_start,
3311 base_name_end))
3312 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003313
Sean Callanan9df05fb2012-02-10 22:52:19 +00003314 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3315 continue;
3316
Greg Claytonaa044962011-10-13 00:59:38 +00003317 // If we get to here, the die is good, and we should add it:
3318 ResolveFunction (dwarf_cu, die, sc_list);
3319 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003320 }
3321 die_offsets.clear();
3322 }
3323
Jim Inghamea8005a2011-10-11 01:18:11 +00003324 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003325 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003326 if (namespace_decl && *namespace_decl)
3327 return 0; // no methods in namespaces
3328
Jim Ingham4cda6e02011-10-07 22:23:45 +00003329 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3330 {
Greg Claytonaa044962011-10-13 00:59:38 +00003331 for (uint32_t i = 0; i < num_base; i++)
3332 {
3333 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3334 if (die)
3335 {
3336 if (!FunctionDieMatchesPartialName(die,
3337 dwarf_cu,
3338 effective_name_type_mask,
3339 name_cstr,
3340 base_name_start,
3341 base_name_end))
3342 continue;
3343
Sean Callanan9df05fb2012-02-10 22:52:19 +00003344 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3345 continue;
3346
Greg Claytonaa044962011-10-13 00:59:38 +00003347 // If we get to here, the die is good, and we should add it:
3348 ResolveFunction (dwarf_cu, die, sc_list);
3349 }
3350 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003351 }
3352 die_offsets.clear();
3353 }
Greg Clayton7f995132011-10-04 22:41:51 +00003354
Sean Callanan213fdb82011-10-13 01:49:10 +00003355 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003356 {
Greg Clayton7f995132011-10-04 22:41:51 +00003357 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003358 }
3359
Greg Clayton4d01ace2011-09-29 16:58:15 +00003360 }
3361
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003362 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003363 const uint32_t num_matches = sc_list.GetSize() - original_size;
3364
3365 if (log && num_matches > 0)
3366 {
3367 GetObjectFile()->GetModule()->LogMessage (log.get(),
3368 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u",
3369 name.GetCString(),
3370 name_type_mask,
3371 append,
3372 num_matches);
3373 }
3374 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003375}
3376
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003377uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003378SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003379{
3380 Timer scoped_timer (__PRETTY_FUNCTION__,
3381 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3382 regex.GetText());
3383
Greg Clayton21f2a492011-10-06 00:09:08 +00003384 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3385
3386 if (log)
3387 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003388 GetObjectFile()->GetModule()->LogMessage (log.get(),
3389 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3390 regex.GetText(),
3391 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003392 }
3393
3394
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003395 // If we aren't appending the results to this list, then clear the list
3396 if (!append)
3397 sc_list.Clear();
3398
3399 // Remember how many sc_list are in the list before we search in case
3400 // we are appending the results to a variable list.
3401 uint32_t original_size = sc_list.GetSize();
3402
Greg Clayton97fbc342011-10-20 22:30:33 +00003403 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003404 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003405 if (m_apple_names_ap.get())
3406 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003407 }
3408 else
3409 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003410 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003411 if (!m_indexed)
3412 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003413
Greg Clayton7f995132011-10-04 22:41:51 +00003414 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003415
Greg Clayton7f995132011-10-04 22:41:51 +00003416 FindFunctions (regex, m_function_fullname_index, sc_list);
3417 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003418
3419 // Return the number of variable that were appended to the list
3420 return sc_list.GetSize() - original_size;
3421}
Jim Ingham318c9f22011-08-26 19:44:13 +00003422
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003423uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003424SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3425 const ConstString &name,
3426 const lldb_private::ClangNamespaceDecl *namespace_decl,
3427 bool append,
3428 uint32_t max_matches,
3429 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003430{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003431 DWARFDebugInfo* info = DebugInfo();
3432 if (info == NULL)
3433 return 0;
3434
Greg Clayton21f2a492011-10-06 00:09:08 +00003435 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3436
3437 if (log)
3438 {
Greg Clayton437a1352012-04-09 22:43:43 +00003439 if (namespace_decl)
3440 {
3441 GetObjectFile()->GetModule()->LogMessage (log.get(),
3442 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
3443 name.GetCString(),
3444 namespace_decl->GetNamespaceDecl(),
3445 namespace_decl->GetQualifiedName().c_str(),
3446 append,
3447 max_matches);
3448 }
3449 else
3450 {
3451 GetObjectFile()->GetModule()->LogMessage (log.get(),
3452 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
3453 name.GetCString(),
3454 append,
3455 max_matches);
3456 }
Greg Clayton21f2a492011-10-06 00:09:08 +00003457 }
3458
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003459 // If we aren't appending the results to this list, then clear the list
3460 if (!append)
3461 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003462
3463 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3464 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003465
Greg Claytond4a2b372011-09-12 23:21:58 +00003466 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003467
Greg Clayton97fbc342011-10-20 22:30:33 +00003468 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003469 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003470 if (m_apple_types_ap.get())
3471 {
3472 const char *name_cstr = name.GetCString();
3473 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3474 }
Greg Clayton7f995132011-10-04 22:41:51 +00003475 }
3476 else
3477 {
3478 if (!m_indexed)
3479 Index ();
3480
3481 m_type_index.Find (name, die_offsets);
3482 }
3483
Greg Clayton437a1352012-04-09 22:43:43 +00003484 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003485
Greg Clayton437a1352012-04-09 22:43:43 +00003486 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003487 {
Greg Clayton7f995132011-10-04 22:41:51 +00003488 const uint32_t initial_types_size = types.GetSize();
3489 DWARFCompileUnit* dwarf_cu = NULL;
3490 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003491 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003492 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003493 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003494 const dw_offset_t die_offset = die_offsets[i];
3495 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3496
Greg Clayton95d87902011-11-11 03:16:25 +00003497 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003498 {
Greg Clayton95d87902011-11-11 03:16:25 +00003499 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3500 continue;
3501
3502 Type *matching_type = ResolveType (dwarf_cu, die);
3503 if (matching_type)
3504 {
3505 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003506 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003507 if (types.GetSize() >= max_matches)
3508 break;
3509 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003510 }
Greg Clayton95d87902011-11-11 03:16:25 +00003511 else
3512 {
3513 if (m_using_apple_tables)
3514 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003515 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3516 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003517 }
3518 }
3519
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003520 }
Greg Clayton437a1352012-04-09 22:43:43 +00003521 const uint32_t num_matches = types.GetSize() - initial_types_size;
3522 if (log && num_matches)
3523 {
3524 if (namespace_decl)
3525 {
3526 GetObjectFile()->GetModule()->LogMessage (log.get(),
3527 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
3528 name.GetCString(),
3529 namespace_decl->GetNamespaceDecl(),
3530 namespace_decl->GetQualifiedName().c_str(),
3531 append,
3532 max_matches,
3533 num_matches);
3534 }
3535 else
3536 {
3537 GetObjectFile()->GetModule()->LogMessage (log.get(),
3538 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
3539 name.GetCString(),
3540 append,
3541 max_matches,
3542 num_matches);
3543 }
3544 }
3545 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003546 }
Greg Clayton7f995132011-10-04 22:41:51 +00003547 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003548}
3549
3550
Greg Clayton526e5af2010-11-13 03:52:47 +00003551ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003552SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003553 const ConstString &name,
3554 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003555{
Greg Clayton21f2a492011-10-06 00:09:08 +00003556 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3557
3558 if (log)
3559 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003560 GetObjectFile()->GetModule()->LogMessage (log.get(),
3561 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3562 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003563 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003564
3565 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3566 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003567
Greg Clayton526e5af2010-11-13 03:52:47 +00003568 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003569 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003570 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003571 {
Greg Clayton7f995132011-10-04 22:41:51 +00003572 DIEArray die_offsets;
3573
Greg Clayton526e5af2010-11-13 03:52:47 +00003574 // Index if we already haven't to make sure the compile units
3575 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003576 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003577 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003578 if (m_apple_namespaces_ap.get())
3579 {
3580 const char *name_cstr = name.GetCString();
3581 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3582 }
Greg Clayton7f995132011-10-04 22:41:51 +00003583 }
3584 else
3585 {
3586 if (!m_indexed)
3587 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003588
Greg Clayton7f995132011-10-04 22:41:51 +00003589 m_namespace_index.Find (name, die_offsets);
3590 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003591
3592 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003593 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003594 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003595 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003596 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003597 DWARFDebugInfo* debug_info = DebugInfo();
3598 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003599 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003600 const dw_offset_t die_offset = die_offsets[i];
3601 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003602
Greg Clayton95d87902011-11-11 03:16:25 +00003603 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003604 {
Greg Clayton95d87902011-11-11 03:16:25 +00003605 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3606 continue;
3607
3608 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3609 if (clang_namespace_decl)
3610 {
3611 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3612 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003613 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003614 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003615 }
Greg Clayton95d87902011-11-11 03:16:25 +00003616 else
3617 {
3618 if (m_using_apple_tables)
3619 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003620 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3621 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003622 }
3623 }
3624
Greg Clayton526e5af2010-11-13 03:52:47 +00003625 }
3626 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003627 }
Greg Clayton437a1352012-04-09 22:43:43 +00003628 if (log && namespace_decl.GetNamespaceDecl())
3629 {
3630 GetObjectFile()->GetModule()->LogMessage (log.get(),
3631 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
3632 name.GetCString(),
3633 namespace_decl.GetNamespaceDecl(),
3634 namespace_decl.GetQualifiedName().c_str());
3635 }
3636
Greg Clayton526e5af2010-11-13 03:52:47 +00003637 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003638}
3639
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003640uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003641SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003642{
3643 // Remember how many sc_list are in the list before we search in case
3644 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003645 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003646
3647 const uint32_t num_die_offsets = die_offsets.size();
3648 // Parse all of the types we found from the pubtypes matches
3649 uint32_t i;
3650 uint32_t num_matches = 0;
3651 for (i = 0; i < num_die_offsets; ++i)
3652 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003653 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3654 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003655 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003656 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003657 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003658 ++num_matches;
3659 if (num_matches >= max_matches)
3660 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003661 }
3662 }
3663
3664 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003665 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003666}
3667
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003668
3669size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003670SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3671 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003672 DWARFCompileUnit* dwarf_cu,
3673 const DWARFDebugInfoEntry *parent_die,
3674 bool skip_artificial,
3675 bool &is_static,
3676 TypeList* type_list,
3677 std::vector<clang_type_t>& function_param_types,
3678 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003679 unsigned &type_quals,
3680 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003681{
3682 if (parent_die == NULL)
3683 return 0;
3684
Greg Claytond88d7592010-09-15 08:33:30 +00003685 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3686
Greg Clayton7fedea22010-11-16 02:10:54 +00003687 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003688 const DWARFDebugInfoEntry *die;
3689 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3690 {
3691 dw_tag_t tag = die->Tag();
3692 switch (tag)
3693 {
3694 case DW_TAG_formal_parameter:
3695 {
3696 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003697 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003698 if (num_attributes > 0)
3699 {
3700 const char *name = NULL;
3701 Declaration decl;
3702 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003703 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003704 // one of None, Auto, Register, Extern, Static, PrivateExtern
3705
Sean Callanane2ef6e32010-09-23 03:01:22 +00003706 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003707 uint32_t i;
3708 for (i=0; i<num_attributes; ++i)
3709 {
3710 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3711 DWARFFormValue form_value;
3712 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3713 {
3714 switch (attr)
3715 {
3716 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3717 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3718 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3719 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3720 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003721 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003722 case DW_AT_location:
3723 // if (form_value.BlockData())
3724 // {
3725 // const DataExtractor& debug_info_data = debug_info();
3726 // uint32_t block_length = form_value.Unsigned();
3727 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3728 // }
3729 // else
3730 // {
3731 // }
3732 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003733 case DW_AT_const_value:
3734 case DW_AT_default_value:
3735 case DW_AT_description:
3736 case DW_AT_endianity:
3737 case DW_AT_is_optional:
3738 case DW_AT_segment:
3739 case DW_AT_variable_parameter:
3740 default:
3741 case DW_AT_abstract_origin:
3742 case DW_AT_sibling:
3743 break;
3744 }
3745 }
3746 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003747
Greg Clayton0fffff52010-09-24 05:15:53 +00003748 bool skip = false;
3749 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003750 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003751 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003752 {
3753 // In order to determine if a C++ member function is
3754 // "const" we have to look at the const-ness of "this"...
3755 // Ugly, but that
3756 if (arg_idx == 0)
3757 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003758 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003759 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003760 // Often times compilers omit the "this" name for the
3761 // specification DIEs, so we can't rely upon the name
3762 // being in the formal parameter DIE...
3763 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003764 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003765 Type *this_type = ResolveTypeUID (param_type_die_offset);
3766 if (this_type)
3767 {
3768 uint32_t encoding_mask = this_type->GetEncodingMask();
3769 if (encoding_mask & Type::eEncodingIsPointerUID)
3770 {
3771 is_static = false;
3772
3773 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3774 type_quals |= clang::Qualifiers::Const;
3775 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3776 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003777 }
3778 }
3779 }
3780 }
3781 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003782 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003783 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003784 else
3785 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003786
Greg Clayton0fffff52010-09-24 05:15:53 +00003787 // HACK: Objective C formal parameters "self" and "_cmd"
3788 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00003789 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3790 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00003791 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003792 switch (comp_unit->GetLanguage())
3793 {
3794 case eLanguageTypeObjC:
3795 case eLanguageTypeObjC_plus_plus:
3796 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3797 skip = true;
3798 break;
3799 default:
3800 break;
3801 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003802 }
3803 }
3804 }
3805
3806 if (!skip)
3807 {
3808 Type *type = ResolveTypeUID(param_type_die_offset);
3809 if (type)
3810 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003811 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003812
Greg Clayton42ce2f32011-12-12 21:50:19 +00003813 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3814 type->GetClangForwardType(),
3815 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003816 assert(param_var_decl);
3817 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00003818
3819 GetClangASTContext().SetMetadata((uintptr_t)param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00003820 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003821 }
3822 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003823 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003824 }
3825 break;
3826
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003827 case DW_TAG_template_type_parameter:
3828 case DW_TAG_template_value_parameter:
3829 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
3830 break;
3831
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003832 default:
3833 break;
3834 }
3835 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003836 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003837}
3838
3839size_t
3840SymbolFileDWARF::ParseChildEnumerators
3841(
3842 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003843 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003844 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003845 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003846 const DWARFDebugInfoEntry *parent_die
3847)
3848{
3849 if (parent_die == NULL)
3850 return 0;
3851
3852 size_t enumerators_added = 0;
3853 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003854 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3855
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003856 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3857 {
3858 const dw_tag_t tag = die->Tag();
3859 if (tag == DW_TAG_enumerator)
3860 {
3861 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003862 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003863 if (num_child_attributes > 0)
3864 {
3865 const char *name = NULL;
3866 bool got_value = false;
3867 int64_t enum_value = 0;
3868 Declaration decl;
3869
3870 uint32_t i;
3871 for (i=0; i<num_child_attributes; ++i)
3872 {
3873 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3874 DWARFFormValue form_value;
3875 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3876 {
3877 switch (attr)
3878 {
3879 case DW_AT_const_value:
3880 got_value = true;
3881 enum_value = form_value.Unsigned();
3882 break;
3883
3884 case DW_AT_name:
3885 name = form_value.AsCString(&get_debug_str_data());
3886 break;
3887
3888 case DW_AT_description:
3889 default:
3890 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3891 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3892 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3893 case DW_AT_sibling:
3894 break;
3895 }
3896 }
3897 }
3898
3899 if (name && name[0] && got_value)
3900 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003901 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3902 enumerator_clang_type,
3903 decl,
3904 name,
3905 enum_value,
3906 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003907 ++enumerators_added;
3908 }
3909 }
3910 }
3911 }
3912 return enumerators_added;
3913}
3914
3915void
3916SymbolFileDWARF::ParseChildArrayInfo
3917(
3918 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003919 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003920 const DWARFDebugInfoEntry *parent_die,
3921 int64_t& first_index,
3922 std::vector<uint64_t>& element_orders,
3923 uint32_t& byte_stride,
3924 uint32_t& bit_stride
3925)
3926{
3927 if (parent_die == NULL)
3928 return;
3929
3930 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003931 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003932 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3933 {
3934 const dw_tag_t tag = die->Tag();
3935 switch (tag)
3936 {
3937 case DW_TAG_enumerator:
3938 {
3939 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003940 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003941 if (num_child_attributes > 0)
3942 {
3943 const char *name = NULL;
3944 bool got_value = false;
3945 int64_t enum_value = 0;
3946
3947 uint32_t i;
3948 for (i=0; i<num_child_attributes; ++i)
3949 {
3950 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3951 DWARFFormValue form_value;
3952 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3953 {
3954 switch (attr)
3955 {
3956 case DW_AT_const_value:
3957 got_value = true;
3958 enum_value = form_value.Unsigned();
3959 break;
3960
3961 case DW_AT_name:
3962 name = form_value.AsCString(&get_debug_str_data());
3963 break;
3964
3965 case DW_AT_description:
3966 default:
3967 case DW_AT_decl_file:
3968 case DW_AT_decl_line:
3969 case DW_AT_decl_column:
3970 case DW_AT_sibling:
3971 break;
3972 }
3973 }
3974 }
3975 }
3976 }
3977 break;
3978
3979 case DW_TAG_subrange_type:
3980 {
3981 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003982 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003983 if (num_child_attributes > 0)
3984 {
3985 const char *name = NULL;
3986 bool got_value = false;
3987 uint64_t byte_size = 0;
3988 int64_t enum_value = 0;
3989 uint64_t num_elements = 0;
3990 uint64_t lower_bound = 0;
3991 uint64_t upper_bound = 0;
3992 uint32_t i;
3993 for (i=0; i<num_child_attributes; ++i)
3994 {
3995 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3996 DWARFFormValue form_value;
3997 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3998 {
3999 switch (attr)
4000 {
4001 case DW_AT_const_value:
4002 got_value = true;
4003 enum_value = form_value.Unsigned();
4004 break;
4005
4006 case DW_AT_name:
4007 name = form_value.AsCString(&get_debug_str_data());
4008 break;
4009
4010 case DW_AT_count:
4011 num_elements = form_value.Unsigned();
4012 break;
4013
4014 case DW_AT_bit_stride:
4015 bit_stride = form_value.Unsigned();
4016 break;
4017
4018 case DW_AT_byte_stride:
4019 byte_stride = form_value.Unsigned();
4020 break;
4021
4022 case DW_AT_byte_size:
4023 byte_size = form_value.Unsigned();
4024 break;
4025
4026 case DW_AT_lower_bound:
4027 lower_bound = form_value.Unsigned();
4028 break;
4029
4030 case DW_AT_upper_bound:
4031 upper_bound = form_value.Unsigned();
4032 break;
4033
4034 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004035 case DW_AT_abstract_origin:
4036 case DW_AT_accessibility:
4037 case DW_AT_allocated:
4038 case DW_AT_associated:
4039 case DW_AT_data_location:
4040 case DW_AT_declaration:
4041 case DW_AT_description:
4042 case DW_AT_sibling:
4043 case DW_AT_threads_scaled:
4044 case DW_AT_type:
4045 case DW_AT_visibility:
4046 break;
4047 }
4048 }
4049 }
4050
4051 if (upper_bound > lower_bound)
4052 num_elements = upper_bound - lower_bound + 1;
4053
4054 if (num_elements > 0)
4055 element_orders.push_back (num_elements);
4056 }
4057 }
4058 break;
4059 }
4060 }
4061}
4062
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004063TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004064SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004065{
4066 TypeSP type_sp;
4067 if (die != NULL)
4068 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004069 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004070 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004071 if (type_ptr == NULL)
4072 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004073 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004074 assert (lldb_cu);
4075 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004076 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004077 }
4078 else if (type_ptr != DIE_IS_BEING_PARSED)
4079 {
4080 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004081 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004082 }
4083
4084 }
4085 return type_sp;
4086}
4087
4088clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004089SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004090{
4091 if (die_offset != DW_INVALID_OFFSET)
4092 {
4093 DWARFCompileUnitSP cu_sp;
4094 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004095 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004096 }
4097 return NULL;
4098}
4099
Sean Callanan72e49402011-08-05 23:43:37 +00004100clang::DeclContext *
4101SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4102{
4103 if (die_offset != DW_INVALID_OFFSET)
4104 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004105 DWARFDebugInfo* debug_info = DebugInfo();
4106 if (debug_info)
4107 {
4108 DWARFCompileUnitSP cu_sp;
4109 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4110 if (die)
4111 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4112 }
Sean Callanan72e49402011-08-05 23:43:37 +00004113 }
4114 return NULL;
4115}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004116
Greg Clayton96d7d742010-11-10 23:42:09 +00004117clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004118SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004119{
Greg Clayton030a2042011-10-14 21:34:45 +00004120 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004121 {
Greg Clayton030a2042011-10-14 21:34:45 +00004122 // See if we already parsed this namespace DIE and associated it with a
4123 // uniqued namespace declaration
4124 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4125 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004126 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004127 else
4128 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004129 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4130 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004131 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
4132 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4133 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004134 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004135 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004136 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004137 GetObjectFile()->GetModule()->LogMessage (log.get(),
4138 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
4139 GetClangASTContext().getASTContext(),
4140 MakeUserID(die->GetOffset()),
4141 namespace_name,
4142 namespace_decl,
4143 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00004144 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004145 else
4146 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004147 GetObjectFile()->GetModule()->LogMessage (log.get(),
4148 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
4149 GetClangASTContext().getASTContext(),
4150 MakeUserID(die->GetOffset()),
4151 namespace_decl,
4152 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00004153 }
Greg Clayton030a2042011-10-14 21:34:45 +00004154 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004155
4156 if (namespace_decl)
4157 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4158 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004159 }
4160 }
4161 return NULL;
4162}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004163
4164clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004165SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004166{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004167 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4168 if (clang_decl_ctx)
4169 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004170 // If this DIE has a specification, or an abstract origin, then trace to those.
4171
Greg Claytoncab36a32011-12-08 05:16:30 +00004172 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004173 if (die_offset != DW_INVALID_OFFSET)
4174 return GetClangDeclContextForDIEOffset (sc, die_offset);
4175
Greg Claytoncab36a32011-12-08 05:16:30 +00004176 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004177 if (die_offset != DW_INVALID_OFFSET)
4178 return GetClangDeclContextForDIEOffset (sc, die_offset);
4179
Greg Clayton3bffb082011-12-10 02:15:28 +00004180 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4181 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004182 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 +00004183 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004184 bool assert_not_being_parsed = true;
4185 ResolveTypeUID (cu, die, assert_not_being_parsed);
4186
Greg Clayton5cf58b92011-10-05 22:22:08 +00004187 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4188
4189 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004190}
4191
4192clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004193SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004194{
Greg Claytonca512b32011-01-14 04:54:56 +00004195 if (m_clang_tu_decl == NULL)
4196 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004197
Greg Clayton2bc22f82011-09-30 03:20:47 +00004198 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004199
4200 if (decl_ctx_die_copy)
4201 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004202
4203 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004204 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004205
Greg Clayton2bc22f82011-09-30 03:20:47 +00004206 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4207 if (pos != m_die_to_decl_ctx.end())
4208 return pos->second;
4209
4210 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004211 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004212 case DW_TAG_compile_unit:
4213 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004214
Greg Clayton2bc22f82011-09-30 03:20:47 +00004215 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004216 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004217 break;
4218
4219 case DW_TAG_structure_type:
4220 case DW_TAG_union_type:
4221 case DW_TAG_class_type:
4222 {
4223 Type* type = ResolveType (cu, decl_ctx_die);
4224 if (type)
4225 {
4226 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4227 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004228 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004229 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4230 if (decl_ctx)
4231 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004232 }
4233 }
Greg Claytonca512b32011-01-14 04:54:56 +00004234 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004235 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004236
Greg Clayton2bc22f82011-09-30 03:20:47 +00004237 default:
4238 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004239 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004240 }
Greg Clayton7a345282010-11-09 23:46:37 +00004241 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004242}
4243
Greg Clayton2bc22f82011-09-30 03:20:47 +00004244
4245const DWARFDebugInfoEntry *
4246SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4247{
4248 if (cu && die)
4249 {
4250 const DWARFDebugInfoEntry * const decl_die = die;
4251
4252 while (die != NULL)
4253 {
4254 // If this is the original DIE that we are searching for a declaration
4255 // for, then don't look in the cache as we don't want our own decl
4256 // context to be our decl context...
4257 if (decl_die != die)
4258 {
4259 switch (die->Tag())
4260 {
4261 case DW_TAG_compile_unit:
4262 case DW_TAG_namespace:
4263 case DW_TAG_structure_type:
4264 case DW_TAG_union_type:
4265 case DW_TAG_class_type:
4266 return die;
4267
4268 default:
4269 break;
4270 }
4271 }
4272
4273 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4274 if (die_offset != DW_INVALID_OFFSET)
4275 {
4276 DWARFCompileUnit *spec_cu = cu;
4277 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4278 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4279 if (spec_die_decl_ctx_die)
4280 return spec_die_decl_ctx_die;
4281 }
4282
4283 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4284 if (die_offset != DW_INVALID_OFFSET)
4285 {
4286 DWARFCompileUnit *abs_cu = cu;
4287 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4288 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4289 if (abs_die_decl_ctx_die)
4290 return abs_die_decl_ctx_die;
4291 }
4292
4293 die = die->GetParent();
4294 }
4295 }
4296 return NULL;
4297}
4298
4299
Greg Clayton901c5ca2011-12-03 04:40:03 +00004300Symbol *
4301SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4302{
4303 Symbol *objc_class_symbol = NULL;
4304 if (m_obj_file)
4305 {
4306 Symtab *symtab = m_obj_file->GetSymtab();
4307 if (symtab)
4308 {
4309 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4310 eSymbolTypeObjCClass,
4311 Symtab::eDebugNo,
4312 Symtab::eVisibilityAny);
4313 }
4314 }
4315 return objc_class_symbol;
4316}
4317
Greg Claytonc7f03b62012-01-12 04:33:28 +00004318// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4319// then we can end up looking through all class types for a complete type and never find
4320// the full definition. We need to know if this attribute is supported, so we determine
4321// this here and cache th result. We also need to worry about the debug map DWARF file
4322// if we are doing darwin DWARF in .o file debugging.
4323bool
4324SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4325{
4326 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4327 {
4328 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4329 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4330 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4331 else
4332 {
4333 DWARFDebugInfo* debug_info = DebugInfo();
4334 const uint32_t num_compile_units = GetNumCompileUnits();
4335 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4336 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004337 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4338 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004339 {
4340 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4341 break;
4342 }
4343 }
4344 }
4345 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && m_debug_map_symfile)
4346 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4347 }
4348 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4349}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004350
4351// This function can be used when a DIE is found that is a forward declaration
4352// DIE and we want to try and find a type that has the complete definition.
4353TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004354SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4355 const ConstString &type_name,
4356 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004357{
4358
4359 TypeSP type_sp;
4360
Greg Claytonc7f03b62012-01-12 04:33:28 +00004361 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004362 return type_sp;
4363
4364 DIEArray die_offsets;
4365
4366 if (m_using_apple_tables)
4367 {
4368 if (m_apple_types_ap.get())
4369 {
4370 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004371 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004372 }
4373 }
4374 else
4375 {
4376 if (!m_indexed)
4377 Index ();
4378
4379 m_type_index.Find (type_name, die_offsets);
4380 }
4381
Greg Clayton901c5ca2011-12-03 04:40:03 +00004382 const size_t num_matches = die_offsets.size();
4383
Greg Clayton901c5ca2011-12-03 04:40:03 +00004384 DWARFCompileUnit* type_cu = NULL;
4385 const DWARFDebugInfoEntry* type_die = NULL;
4386 if (num_matches)
4387 {
4388 DWARFDebugInfo* debug_info = DebugInfo();
4389 for (size_t i=0; i<num_matches; ++i)
4390 {
4391 const dw_offset_t die_offset = die_offsets[i];
4392 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4393
4394 if (type_die)
4395 {
4396 bool try_resolving_type = false;
4397
4398 // Don't try and resolve the DIE we are looking for with the DIE itself!
4399 if (type_die != die)
4400 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004401 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004402 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004403 case DW_TAG_class_type:
4404 case DW_TAG_structure_type:
4405 try_resolving_type = true;
4406 break;
4407 default:
4408 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004409 }
4410 }
4411
4412 if (try_resolving_type)
4413 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004414 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004415 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004416
4417 if (try_resolving_type)
4418 {
4419 Type *resolved_type = ResolveType (type_cu, type_die, false);
4420 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4421 {
4422 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4423 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004424 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton901c5ca2011-12-03 04:40:03 +00004425 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4426 MakeUserID(type_die->GetOffset()),
4427 MakeUserID(type_cu->GetOffset()));
4428
Greg Claytonc7f03b62012-01-12 04:33:28 +00004429 if (die)
4430 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004431 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004432 break;
4433 }
4434 }
4435 }
4436 }
4437 else
4438 {
4439 if (m_using_apple_tables)
4440 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004441 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4442 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004443 }
4444 }
4445
4446 }
4447 }
4448 return type_sp;
4449}
4450
Greg Claytona8022fa2012-04-24 21:22:41 +00004451
Greg Clayton80c26302012-02-05 06:12:47 +00004452//----------------------------------------------------------------------
4453// This function helps to ensure that the declaration contexts match for
4454// two different DIEs. Often times debug information will refer to a
4455// forward declaration of a type (the equivalent of "struct my_struct;".
4456// There will often be a declaration of that type elsewhere that has the
4457// full definition. When we go looking for the full type "my_struct", we
4458// will find one or more matches in the accelerator tables and we will
4459// then need to make sure the type was in the same declaration context
4460// as the original DIE. This function can efficiently compare two DIEs
4461// and will return true when the declaration context matches, and false
4462// when they don't.
4463//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004464bool
4465SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4466 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4467{
Greg Claytona8022fa2012-04-24 21:22:41 +00004468 if (die1 == die2)
4469 return true;
4470
4471#if defined (LLDB_CONFIGURATION_DEBUG)
4472 // You can't and shouldn't call this function with a compile unit from
4473 // two different SymbolFileDWARF instances.
4474 assert (DebugInfo()->ContainsCompileUnit (cu1));
4475 assert (DebugInfo()->ContainsCompileUnit (cu2));
4476#endif
4477
Greg Clayton890ff562012-02-02 05:48:16 +00004478 DWARFDIECollection decl_ctx_1;
4479 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004480 //The declaration DIE stack is a stack of the declaration context
4481 // DIEs all the way back to the compile unit. If a type "T" is
4482 // declared inside a class "B", and class "B" is declared inside
4483 // a class "A" and class "A" is in a namespace "lldb", and the
4484 // namespace is in a compile unit, there will be a stack of DIEs:
4485 //
4486 // [0] DW_TAG_class_type for "B"
4487 // [1] DW_TAG_class_type for "A"
4488 // [2] DW_TAG_namespace for "lldb"
4489 // [3] DW_TAG_compile_unit for the source file.
4490 //
4491 // We grab both contexts and make sure that everything matches
4492 // all the way back to the compiler unit.
4493
4494 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004495 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004496 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004497 // Make sure the context arrays have the same size, otherwise
4498 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004499 const size_t count1 = decl_ctx_1.Size();
4500 const size_t count2 = decl_ctx_2.Size();
4501 if (count1 != count2)
4502 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004503
4504 // Make sure the DW_TAG values match all the way back up the the
4505 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004506 const DWARFDebugInfoEntry *decl_ctx_die1;
4507 const DWARFDebugInfoEntry *decl_ctx_die2;
4508 size_t i;
4509 for (i=0; i<count1; i++)
4510 {
4511 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4512 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4513 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4514 return false;
4515 }
Greg Clayton890ff562012-02-02 05:48:16 +00004516#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004517
4518 // Make sure the top item in the decl context die array is always
4519 // DW_TAG_compile_unit. If it isn't then something went wrong in
4520 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004521 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004522
Greg Clayton890ff562012-02-02 05:48:16 +00004523#endif
4524 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004525 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004526 for (i=0; i<count1 - 1; i++)
4527 {
4528 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4529 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4530 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004531 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004532 // If the string was from a DW_FORM_strp, then the pointer will often
4533 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004534 if (name1 == name2)
4535 continue;
4536
4537 // Name pointers are not equal, so only compare the strings
4538 // if both are not NULL.
4539 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004540 {
Greg Clayton5569e642012-02-06 01:44:54 +00004541 // If the strings don't compare, we are done...
4542 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004543 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004544 }
4545 else
4546 {
4547 // One name was NULL while the other wasn't
4548 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004549 }
4550 }
Greg Clayton80c26302012-02-05 06:12:47 +00004551 // We made it through all of the checks and the declaration contexts
4552 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004553 return true;
4554}
Greg Clayton220a0072011-12-09 08:48:30 +00004555
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004556// This function can be used when a DIE is found that is a forward declaration
4557// DIE and we want to try and find a type that has the complete definition.
Greg Claytona8022fa2012-04-24 21:22:41 +00004558// "cu" and "die" must be from this SymbolFileDWARF
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004559TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004560SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
Greg Clayton7f995132011-10-04 22:41:51 +00004561 const DWARFDebugInfoEntry *die,
4562 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004563{
4564 TypeSP type_sp;
4565
Greg Claytona8022fa2012-04-24 21:22:41 +00004566#if defined (LLDB_CONFIGURATION_DEBUG)
4567 // You can't and shouldn't call this function with a compile unit from
4568 // another SymbolFileDWARF instance.
4569 assert (DebugInfo()->ContainsCompileUnit (cu));
4570#endif
4571
Greg Clayton1a65ae12011-01-25 23:55:37 +00004572 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004573 return type_sp;
4574
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004575 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4576 if (log)
4577 {
4578 std::string qualified_name;
4579 die->GetQualifiedName(this, cu, qualified_name);
4580 GetObjectFile()->GetModule()->LogMessage (log.get(),
4581 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x (%s), name='%s')",
4582 die->GetOffset(),
4583 qualified_name.c_str(),
4584 type_name.GetCString());
4585 }
4586
Greg Clayton7f995132011-10-04 22:41:51 +00004587 DIEArray die_offsets;
4588
Greg Clayton97fbc342011-10-20 22:30:33 +00004589 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004590 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004591 if (m_apple_types_ap.get())
4592 {
Greg Claytond1767f02011-12-08 02:13:16 +00004593 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4594 {
Greg Claytonae920b62012-01-06 00:17:16 +00004595 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004596 }
4597 else
4598 {
4599 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4600 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004601 }
Greg Clayton7f995132011-10-04 22:41:51 +00004602 }
4603 else
4604 {
4605 if (!m_indexed)
4606 Index ();
4607
4608 m_type_index.Find (type_name, die_offsets);
4609 }
Greg Clayton7f995132011-10-04 22:41:51 +00004610
4611 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004612
Greg Clayton934cb052011-12-03 00:27:05 +00004613 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004614
4615 DWARFCompileUnit* type_cu = NULL;
4616 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004617 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004618 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004619 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004620 for (size_t i=0; i<num_matches; ++i)
4621 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004622 const dw_offset_t die_offset = die_offsets[i];
4623 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004624
Greg Clayton95d87902011-11-11 03:16:25 +00004625 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004626 {
Greg Clayton934cb052011-12-03 00:27:05 +00004627 bool try_resolving_type = false;
4628
4629 // Don't try and resolve the DIE we are looking for with the DIE itself!
4630 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004631 {
Greg Clayton934cb052011-12-03 00:27:05 +00004632 const dw_tag_t type_die_tag = type_die->Tag();
4633 // Make sure the tags match
4634 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004635 {
Greg Clayton934cb052011-12-03 00:27:05 +00004636 // The tags match, lets try resolving this type
4637 try_resolving_type = true;
4638 }
4639 else
4640 {
4641 // The tags don't match, but we need to watch our for a
4642 // forward declaration for a struct and ("struct foo")
4643 // ends up being a class ("class foo { ... };") or
4644 // vice versa.
4645 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004646 {
Greg Clayton934cb052011-12-03 00:27:05 +00004647 case DW_TAG_class_type:
4648 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4649 try_resolving_type = (die_tag == DW_TAG_structure_type);
4650 break;
4651 case DW_TAG_structure_type:
4652 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4653 try_resolving_type = (die_tag == DW_TAG_class_type);
4654 break;
4655 default:
4656 // Tags don't match, don't event try to resolve
4657 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004658 break;
4659 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004660 }
4661 }
Greg Clayton934cb052011-12-03 00:27:05 +00004662
4663 if (try_resolving_type)
4664 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004665 if (log)
4666 {
4667 std::string qualified_name;
4668 type_die->GetQualifiedName(this, cu, qualified_name);
4669 GetObjectFile()->GetModule()->LogMessage (log.get(),
4670 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') trying die=0x%8.8x (%s)",
4671 die->GetOffset(),
4672 type_name.GetCString(),
4673 type_die->GetOffset(),
4674 qualified_name.c_str());
4675 }
4676
Greg Clayton890ff562012-02-02 05:48:16 +00004677 // Make sure the decl contexts match all the way up
4678 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004679 {
Greg Clayton890ff562012-02-02 05:48:16 +00004680 Type *resolved_type = ResolveType (type_cu, type_die, false);
4681 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4682 {
4683 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4684 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004685 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton890ff562012-02-02 05:48:16 +00004686 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4687 MakeUserID(type_die->GetOffset()),
4688 MakeUserID(type_cu->GetOffset()));
4689
4690 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004691 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004692 break;
4693 }
Greg Clayton934cb052011-12-03 00:27:05 +00004694 }
4695 }
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004696 else
4697 {
4698 if (log)
4699 {
4700 std::string qualified_name;
4701 type_die->GetQualifiedName(this, cu, qualified_name);
4702 GetObjectFile()->GetModule()->LogMessage (log.get(),
4703 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') ignoring die=0x%8.8x (%s)",
4704 die->GetOffset(),
4705 type_name.GetCString(),
4706 type_die->GetOffset(),
4707 qualified_name.c_str());
4708 }
4709 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004710 }
Greg Clayton95d87902011-11-11 03:16:25 +00004711 else
4712 {
4713 if (m_using_apple_tables)
4714 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004715 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4716 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004717 }
4718 }
4719
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004720 }
4721 }
4722 return type_sp;
4723}
4724
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004725TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004726SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
4727{
4728 TypeSP type_sp;
4729
4730 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
4731 if (dwarf_decl_ctx_count > 0)
4732 {
4733 const ConstString type_name(dwarf_decl_ctx[0].name);
4734 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
4735
4736 if (type_name)
4737 {
4738 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4739 if (log)
4740 {
4741 GetObjectFile()->GetModule()->LogMessage (log.get(),
4742 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
4743 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4744 dwarf_decl_ctx.GetQualifiedName());
4745 }
4746
4747 DIEArray die_offsets;
4748
4749 if (m_using_apple_tables)
4750 {
4751 if (m_apple_types_ap.get())
4752 {
4753 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4754 {
4755 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
4756 }
4757 else
4758 {
4759 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4760 }
4761 }
4762 }
4763 else
4764 {
4765 if (!m_indexed)
4766 Index ();
4767
4768 m_type_index.Find (type_name, die_offsets);
4769 }
4770
4771 const size_t num_matches = die_offsets.size();
4772
4773
4774 DWARFCompileUnit* type_cu = NULL;
4775 const DWARFDebugInfoEntry* type_die = NULL;
4776 if (num_matches)
4777 {
4778 DWARFDebugInfo* debug_info = DebugInfo();
4779 for (size_t i=0; i<num_matches; ++i)
4780 {
4781 const dw_offset_t die_offset = die_offsets[i];
4782 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4783
4784 if (type_die)
4785 {
4786 bool try_resolving_type = false;
4787
4788 // Don't try and resolve the DIE we are looking for with the DIE itself!
4789 const dw_tag_t type_tag = type_die->Tag();
4790 // Make sure the tags match
4791 if (type_tag == tag)
4792 {
4793 // The tags match, lets try resolving this type
4794 try_resolving_type = true;
4795 }
4796 else
4797 {
4798 // The tags don't match, but we need to watch our for a
4799 // forward declaration for a struct and ("struct foo")
4800 // ends up being a class ("class foo { ... };") or
4801 // vice versa.
4802 switch (type_tag)
4803 {
4804 case DW_TAG_class_type:
4805 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4806 try_resolving_type = (tag == DW_TAG_structure_type);
4807 break;
4808 case DW_TAG_structure_type:
4809 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4810 try_resolving_type = (tag == DW_TAG_class_type);
4811 break;
4812 default:
4813 // Tags don't match, don't event try to resolve
4814 // using this type whose name matches....
4815 break;
4816 }
4817 }
4818
4819 if (try_resolving_type)
4820 {
4821 DWARFDeclContext type_dwarf_decl_ctx;
4822 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
4823
4824 if (log)
4825 {
4826 GetObjectFile()->GetModule()->LogMessage (log.get(),
4827 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
4828 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4829 dwarf_decl_ctx.GetQualifiedName(),
4830 type_die->GetOffset(),
4831 type_dwarf_decl_ctx.GetQualifiedName());
4832 }
4833
4834 // Make sure the decl contexts match all the way up
4835 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
4836 {
4837 Type *resolved_type = ResolveType (type_cu, type_die, false);
4838 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4839 {
4840 type_sp = resolved_type->shared_from_this();
4841 break;
4842 }
4843 }
4844 }
4845 else
4846 {
4847 if (log)
4848 {
4849 std::string qualified_name;
4850 type_die->GetQualifiedName(this, type_cu, qualified_name);
4851 GetObjectFile()->GetModule()->LogMessage (log.get(),
4852 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
4853 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4854 dwarf_decl_ctx.GetQualifiedName(),
4855 type_die->GetOffset(),
4856 qualified_name.c_str());
4857 }
4858 }
4859 }
4860 else
4861 {
4862 if (m_using_apple_tables)
4863 {
4864 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4865 die_offset, type_name.GetCString());
4866 }
4867 }
4868
4869 }
4870 }
4871 }
4872 }
4873 return type_sp;
4874}
4875
4876
4877TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00004878SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004879{
4880 TypeSP type_sp;
4881
Greg Clayton1be10fc2010-09-29 01:12:09 +00004882 if (type_is_new_ptr)
4883 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00004884
4885#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4886 static DIEStack g_die_stack;
4887 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
4888#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00004889
Sean Callananc7fbf732010-08-06 00:32:49 +00004890 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004891 if (die != NULL)
4892 {
Greg Clayton21f2a492011-10-06 00:09:08 +00004893 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00004894 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00004895 {
4896 const DWARFDebugInfoEntry *context_die;
4897 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
4898
Greg Clayton1fba87112012-02-09 20:03:49 +00004899 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 +00004900 die->GetOffset(),
4901 context,
4902 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00004903 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00004904 die->GetName(this, dwarf_cu));
4905
4906#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4907 scoped_die_logger.Push (dwarf_cu, die);
4908 g_die_stack.LogDIEs(log.get(), this);
4909#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00004910 }
Greg Clayton3bffb082011-12-10 02:15:28 +00004911//
4912// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4913// if (log && dwarf_cu)
4914// {
4915// StreamString s;
4916// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00004917// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00004918//
4919// }
Jim Ingham16746d12011-08-25 23:21:43 +00004920
Greg Clayton594e5ed2010-09-27 21:07:38 +00004921 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004922 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00004923 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004924 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004925 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00004926 if (type_is_new_ptr)
4927 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004928
Greg Clayton594e5ed2010-09-27 21:07:38 +00004929 const dw_tag_t tag = die->Tag();
4930
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004931 bool is_forward_declaration = false;
4932 DWARFDebugInfoEntry::Attributes attributes;
4933 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00004934 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00004935 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
4936 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00004937 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00004938 Declaration decl;
4939
Greg Clayton4957bf62010-09-30 21:49:03 +00004940 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00004941 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004942
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004943 dw_attr_t attr;
4944
4945 switch (tag)
4946 {
4947 case DW_TAG_base_type:
4948 case DW_TAG_pointer_type:
4949 case DW_TAG_reference_type:
4950 case DW_TAG_typedef:
4951 case DW_TAG_const_type:
4952 case DW_TAG_restrict_type:
4953 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00004954 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004955 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004956 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004957 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004958
Greg Claytond88d7592010-09-15 08:33:30 +00004959 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004960 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004961 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
4962
4963 if (num_attributes > 0)
4964 {
4965 uint32_t i;
4966 for (i=0; i<num_attributes; ++i)
4967 {
4968 attr = attributes.AttributeAtIndex(i);
4969 DWARFFormValue form_value;
4970 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4971 {
4972 switch (attr)
4973 {
4974 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4975 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4976 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4977 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00004978
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004979 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00004980 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
4981 // include the "&"...
4982 if (tag == DW_TAG_reference_type)
4983 {
4984 if (strchr (type_name_cstr, '&') == NULL)
4985 type_name_cstr = NULL;
4986 }
4987 if (type_name_cstr)
4988 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004989 break;
Greg Clayton36909642011-03-15 04:38:20 +00004990 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004991 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
4992 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
4993 default:
4994 case DW_AT_sibling:
4995 break;
4996 }
4997 }
4998 }
4999 }
5000
Greg Clayton81c22f62011-10-19 18:09:39 +00005001 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 +00005002
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005003 switch (tag)
5004 {
5005 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00005006 break;
5007
Greg Claytond4436412011-10-28 21:00:00 +00005008 case DW_TAG_unspecified_type:
5009 if (strcmp(type_name_cstr, "nullptr_t") == 0)
5010 {
5011 resolve_state = Type::eResolveStateFull;
5012 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
5013 break;
5014 }
5015 // Fall through to base type below in case we can handle the type there...
5016
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005017 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00005018 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005019 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
5020 encoding,
5021 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005022 break;
5023
Greg Clayton526e5af2010-11-13 03:52:47 +00005024 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
5025 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
5026 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
5027 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
5028 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
5029 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005030 }
5031
Greg Claytonc7f03b62012-01-12 04:33:28 +00005032 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005033 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005034 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
5035 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005036 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005037 static ConstString g_objc_type_name_id("id");
5038 static ConstString g_objc_type_name_Class("Class");
5039 static ConstString g_objc_type_name_selector("SEL");
5040
5041 if (type_name_const_str == g_objc_type_name_id)
5042 {
5043 if (log)
5044 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
5045 die->GetOffset(),
5046 DW_TAG_value_to_name(die->Tag()),
5047 die->GetName(this, dwarf_cu));
5048 clang_type = ast.GetBuiltInType_objc_id();
5049 encoding_data_type = Type::eEncodingIsUID;
5050 encoding_uid = LLDB_INVALID_UID;
5051 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00005052
Greg Claytonc7f03b62012-01-12 04:33:28 +00005053 }
5054 else if (type_name_const_str == g_objc_type_name_Class)
5055 {
5056 if (log)
5057 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
5058 die->GetOffset(),
5059 DW_TAG_value_to_name(die->Tag()),
5060 die->GetName(this, dwarf_cu));
5061 clang_type = ast.GetBuiltInType_objc_Class();
5062 encoding_data_type = Type::eEncodingIsUID;
5063 encoding_uid = LLDB_INVALID_UID;
5064 resolve_state = Type::eResolveStateFull;
5065 }
5066 else if (type_name_const_str == g_objc_type_name_selector)
5067 {
5068 if (log)
5069 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
5070 die->GetOffset(),
5071 DW_TAG_value_to_name(die->Tag()),
5072 die->GetName(this, dwarf_cu));
5073 clang_type = ast.GetBuiltInType_objc_selector();
5074 encoding_data_type = Type::eEncodingIsUID;
5075 encoding_uid = LLDB_INVALID_UID;
5076 resolve_state = Type::eResolveStateFull;
5077 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005078 }
5079 }
5080
Greg Clayton81c22f62011-10-19 18:09:39 +00005081 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005082 this,
5083 type_name_const_str,
5084 byte_size,
5085 NULL,
5086 encoding_uid,
5087 encoding_data_type,
5088 &decl,
5089 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005090 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005091
Greg Clayton594e5ed2010-09-27 21:07:38 +00005092 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005093
5094// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
5095// if (encoding_type != NULL)
5096// {
5097// if (encoding_type != DIE_IS_BEING_PARSED)
5098// type_sp->SetEncodingType(encoding_type);
5099// else
5100// m_indirect_fixups.push_back(type_sp.get());
5101// }
5102 }
5103 break;
5104
5105 case DW_TAG_structure_type:
5106 case DW_TAG_union_type:
5107 case DW_TAG_class_type:
5108 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005109 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005110 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005111
Greg Clayton9e409562010-07-28 02:04:09 +00005112 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00005113 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005114 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00005115 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005116 if (num_attributes > 0)
5117 {
5118 uint32_t i;
5119 for (i=0; i<num_attributes; ++i)
5120 {
5121 attr = attributes.AttributeAtIndex(i);
5122 DWARFFormValue form_value;
5123 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5124 {
5125 switch (attr)
5126 {
Greg Clayton9e409562010-07-28 02:04:09 +00005127 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00005128 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
5129 {
5130 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
5131 // point to the compile unit file, so we clear this invalid value
5132 // so that we can still unique types efficiently.
5133 decl.SetFile(FileSpec ("<invalid>", false));
5134 }
5135 else
5136 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00005137 break;
5138
5139 case DW_AT_decl_line:
5140 decl.SetLine(form_value.Unsigned());
5141 break;
5142
5143 case DW_AT_decl_column:
5144 decl.SetColumn(form_value.Unsigned());
5145 break;
5146
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005147 case DW_AT_name:
5148 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005149 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005150 break;
Greg Clayton9e409562010-07-28 02:04:09 +00005151
5152 case DW_AT_byte_size:
5153 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00005154 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00005155 break;
5156
5157 case DW_AT_accessibility:
5158 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
5159 break;
5160
5161 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00005162 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00005163 break;
5164
5165 case DW_AT_APPLE_runtime_class:
5166 class_language = (LanguageType)form_value.Signed();
5167 break;
5168
Greg Clayton18774842011-11-29 23:40:34 +00005169 case DW_AT_APPLE_objc_complete_type:
5170 is_complete_objc_class = form_value.Signed();
5171 break;
5172
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005173 case DW_AT_allocated:
5174 case DW_AT_associated:
5175 case DW_AT_data_location:
5176 case DW_AT_description:
5177 case DW_AT_start_scope:
5178 case DW_AT_visibility:
5179 default:
5180 case DW_AT_sibling:
5181 break;
5182 }
5183 }
5184 }
5185 }
5186
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005187 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00005188
Greg Clayton123edca2012-03-02 00:07:15 +00005189 // Only try and unique the type if it has a name.
5190 if (type_name_const_str &&
5191 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00005192 this,
5193 dwarf_cu,
5194 die,
5195 decl,
5196 byte_size_valid ? byte_size : -1,
5197 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005198 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005199 // We have already parsed this type or from another
5200 // compile unit. GCC loves to use the "one definition
5201 // rule" which can result in multiple definitions
5202 // of the same class over and over in each compile
5203 // unit.
5204 type_sp = unique_ast_entry.m_type_sp;
5205 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00005206 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005207 m_die_to_type[die] = type_sp.get();
5208 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005209 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005210 }
5211
Greg Clayton81c22f62011-10-19 18:09:39 +00005212 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 +00005213
5214 int tag_decl_kind = -1;
5215 AccessType default_accessibility = eAccessNone;
5216 if (tag == DW_TAG_structure_type)
5217 {
5218 tag_decl_kind = clang::TTK_Struct;
5219 default_accessibility = eAccessPublic;
5220 }
5221 else if (tag == DW_TAG_union_type)
5222 {
5223 tag_decl_kind = clang::TTK_Union;
5224 default_accessibility = eAccessPublic;
5225 }
5226 else if (tag == DW_TAG_class_type)
5227 {
5228 tag_decl_kind = clang::TTK_Class;
5229 default_accessibility = eAccessPrivate;
5230 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00005231
5232 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
5233 die->HasChildren() == false &&
5234 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
5235 {
5236 // Work around an issue with clang at the moment where
5237 // forward declarations for objective C classes are emitted
5238 // as:
5239 // DW_TAG_structure_type [2]
5240 // DW_AT_name( "ForwardObjcClass" )
5241 // DW_AT_byte_size( 0x00 )
5242 // DW_AT_decl_file( "..." )
5243 // DW_AT_decl_line( 1 )
5244 //
5245 // Note that there is no DW_AT_declaration and there are
5246 // no children, and the byte size is zero.
5247 is_forward_declaration = true;
5248 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005249
Greg Clayton18774842011-11-29 23:40:34 +00005250 if (class_language == eLanguageTypeObjC)
5251 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005252 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00005253 {
5254 // We have a valid eSymbolTypeObjCClass class symbol whose
5255 // name matches the current objective C class that we
5256 // are trying to find and this DIE isn't the complete
5257 // definition (we checked is_complete_objc_class above and
5258 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00005259 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005260
Greg Clayton901c5ca2011-12-03 04:40:03 +00005261 if (!type_sp && m_debug_map_symfile)
5262 {
5263 // We weren't able to find a full declaration in
5264 // this DWARF, see if we have a declaration anywhere
5265 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00005266 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005267 }
5268
5269 if (type_sp)
5270 {
5271 if (log)
5272 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005273 GetObjectFile()->GetModule()->LogMessage (log.get(),
5274 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
5275 this,
5276 die->GetOffset(),
5277 DW_TAG_value_to_name(tag),
5278 type_name_cstr,
5279 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00005280 }
5281
5282 // We found a real definition for this type elsewhere
5283 // so lets use it and cache the fact that we found
5284 // a complete type for this die
5285 m_die_to_type[die] = type_sp.get();
5286 return type_sp;
5287 }
5288 }
5289 }
5290
5291
5292 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005293 {
5294 // We have a forward declaration to a type and we need
5295 // to try and find a full declaration. We look in the
5296 // current type index just in case we have a forward
5297 // declaration followed by an actual declarations in the
5298 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00005299 if (log)
5300 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005301 GetObjectFile()->GetModule()->LogMessage (log.get(),
5302 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
5303 this,
5304 die->GetOffset(),
5305 DW_TAG_value_to_name(tag),
5306 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00005307 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005308
Greg Claytona8022fa2012-04-24 21:22:41 +00005309 DWARFDeclContext die_decl_ctx;
5310 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx);
5311
5312 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
5313 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005314
5315 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00005316 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005317 // We weren't able to find a full declaration in
5318 // this DWARF, see if we have a declaration anywhere
5319 // else...
Greg Claytona8022fa2012-04-24 21:22:41 +00005320 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton4272cc72011-02-02 02:24:04 +00005321 }
5322
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005323 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00005324 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00005325 if (log)
5326 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005327 GetObjectFile()->GetModule()->LogMessage (log.get(),
5328 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
5329 this,
5330 die->GetOffset(),
5331 DW_TAG_value_to_name(tag),
5332 type_name_cstr,
5333 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00005334 }
5335
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005336 // We found a real definition for this type elsewhere
5337 // so lets use it and cache the fact that we found
5338 // a complete type for this die
5339 m_die_to_type[die] = type_sp.get();
5340 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005341 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005342 }
5343 assert (tag_decl_kind != -1);
5344 bool clang_type_was_created = false;
5345 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5346 if (clang_type == NULL)
5347 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00005348 const DWARFDebugInfoEntry *decl_ctx_die;
5349
5350 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00005351 if (accessibility == eAccessNone && decl_ctx)
5352 {
5353 // Check the decl context that contains this class/struct/union.
5354 // If it is a class we must give it an accessability.
5355 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
5356 if (DeclKindIsCXXClass (containing_decl_kind))
5357 accessibility = default_accessibility;
5358 }
5359
Greg Claytonf0705c82011-10-22 03:33:13 +00005360 if (type_name_cstr && strchr (type_name_cstr, '<'))
5361 {
5362 ClangASTContext::TemplateParameterInfos template_param_infos;
5363 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
5364 {
5365 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00005366 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00005367 type_name_cstr,
5368 tag_decl_kind,
5369 template_param_infos);
5370
5371 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
5372 class_template_decl,
5373 tag_decl_kind,
5374 template_param_infos);
5375 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
5376 clang_type_was_created = true;
Sean Callanan60217122012-04-13 00:10:03 +00005377
5378 GetClangASTContext().SetMetadata((uintptr_t)class_template_decl, MakeUserID(die->GetOffset()));
5379 GetClangASTContext().SetMetadata((uintptr_t)class_specialization_decl, MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005380 }
5381 }
5382
5383 if (!clang_type_was_created)
5384 {
5385 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00005386 clang_type = ast.CreateRecordType (decl_ctx,
5387 accessibility,
5388 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00005389 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00005390 class_language,
Sean Callananad880762012-04-18 01:06:17 +00005391 MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005392 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005393 }
5394
5395 // Store a forward declaration to this class type in case any
5396 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00005397 // types for function prototypes.
5398 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00005399 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005400 this,
5401 type_name_const_str,
5402 byte_size,
5403 NULL,
5404 LLDB_INVALID_UID,
5405 Type::eEncodingIsUID,
5406 &decl,
5407 clang_type,
5408 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00005409
5410 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005411
5412
5413 // Add our type to the unique type map so we don't
5414 // end up creating many copies of the same type over
5415 // and over in the ASTContext for our module
5416 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00005417 unique_ast_entry.m_symfile = this;
5418 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005419 unique_ast_entry.m_die = die;
5420 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00005421 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00005422 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
5423 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005424
Sean Callanan12014a02011-12-08 23:45:45 +00005425 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00005426 {
5427 // Always start the definition for a class type so that
5428 // if the class has child classes or types that require
5429 // the class to be created for use as their decl contexts
5430 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00005431 if (die->HasChildren() == false)
5432 {
5433 // No children for this struct/union/class, lets finish it
5434 ast.StartTagDeclarationDefinition (clang_type);
5435 ast.CompleteTagDeclarationDefinition (clang_type);
5436 }
5437 else if (clang_type_was_created)
5438 {
Greg Clayton219cf312012-03-30 00:51:13 +00005439 // Start the definition if the class is not objective C since
5440 // the underlying decls respond to isCompleteDefinition(). Objective
5441 // C decls dont' respond to isCompleteDefinition() so we can't
5442 // start the declaration definition right away. For C++ classs/union/structs
5443 // we want to start the definition in case the class is needed as the
5444 // declaration context for a contained class or type without the need
5445 // to complete that type..
5446
5447 if (class_language != eLanguageTypeObjC)
5448 ast.StartTagDeclarationDefinition (clang_type);
5449
Sean Callanan12014a02011-12-08 23:45:45 +00005450 // Leave this as a forward declaration until we need
5451 // to know the details of the type. lldb_private::Type
5452 // will automatically call the SymbolFile virtual function
5453 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
5454 // When the definition needs to be defined.
5455 m_forward_decl_die_to_clang_type[die] = clang_type;
5456 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
5457 ClangASTContext::SetHasExternalStorage (clang_type, true);
5458 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005459 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005460
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005461 }
5462 break;
5463
5464 case DW_TAG_enumeration_type:
5465 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005466 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005467 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005468
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005469 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005470
Greg Claytond88d7592010-09-15 08:33:30 +00005471 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005472 if (num_attributes > 0)
5473 {
5474 uint32_t i;
5475
5476 for (i=0; i<num_attributes; ++i)
5477 {
5478 attr = attributes.AttributeAtIndex(i);
5479 DWARFFormValue form_value;
5480 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5481 {
5482 switch (attr)
5483 {
Greg Clayton7a345282010-11-09 23:46:37 +00005484 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5485 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5486 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005487 case DW_AT_name:
5488 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005489 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005490 break;
Greg Clayton7a345282010-11-09 23:46:37 +00005491 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005492 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00005493 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5494 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005495 case DW_AT_allocated:
5496 case DW_AT_associated:
5497 case DW_AT_bit_stride:
5498 case DW_AT_byte_stride:
5499 case DW_AT_data_location:
5500 case DW_AT_description:
5501 case DW_AT_start_scope:
5502 case DW_AT_visibility:
5503 case DW_AT_specification:
5504 case DW_AT_abstract_origin:
5505 case DW_AT_sibling:
5506 break;
5507 }
5508 }
5509 }
5510
Greg Clayton81c22f62011-10-19 18:09:39 +00005511 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 +00005512
Greg Clayton1be10fc2010-09-29 01:12:09 +00005513 clang_type_t enumerator_clang_type = NULL;
5514 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5515 if (clang_type == NULL)
5516 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005517 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
5518 DW_ATE_signed,
5519 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00005520 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00005521 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00005522 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005523 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00005524 }
5525 else
5526 {
5527 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
5528 assert (enumerator_clang_type != NULL);
5529 }
5530
Greg Claytona2721472011-06-25 00:44:06 +00005531 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
5532
Greg Clayton81c22f62011-10-19 18:09:39 +00005533 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005534 this,
5535 type_name_const_str,
5536 byte_size,
5537 NULL,
5538 encoding_uid,
5539 Type::eEncodingIsUID,
5540 &decl,
5541 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005542 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005543
Greg Clayton6beaaa62011-01-17 03:46:26 +00005544 ast.StartTagDeclarationDefinition (clang_type);
5545 if (die->HasChildren())
5546 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005547 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
5548 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005549 }
5550 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005551 }
5552 }
5553 break;
5554
Jim Inghamb0be4422010-08-12 01:20:14 +00005555 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005556 case DW_TAG_subprogram:
5557 case DW_TAG_subroutine_type:
5558 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005559 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005560 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005561
5562 const char *mangled = NULL;
5563 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00005564 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005565 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00005566 bool is_static = false;
5567 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00005568 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00005569 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00005570 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
5571 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00005572
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005573 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00005574 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005575
5576
Greg Claytond88d7592010-09-15 08:33:30 +00005577 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005578 if (num_attributes > 0)
5579 {
5580 uint32_t i;
5581 for (i=0; i<num_attributes; ++i)
5582 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005583 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005584 DWARFFormValue form_value;
5585 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5586 {
5587 switch (attr)
5588 {
5589 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5590 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5591 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5592 case DW_AT_name:
5593 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005594 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005595 break;
5596
5597 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
5598 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005599 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005600 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00005601 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
5602 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00005603 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00005604 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5605
Greg Claytonf51de672010-10-01 02:31:07 +00005606
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005607 case DW_AT_external:
5608 if (form_value.Unsigned())
5609 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00005610 if (storage == clang::SC_None)
5611 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005612 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00005613 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005614 }
5615 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005616
Greg Clayton72da3972011-08-16 18:40:23 +00005617 case DW_AT_specification:
5618 specification_die_offset = form_value.Reference(dwarf_cu);
5619 break;
5620
5621 case DW_AT_abstract_origin:
5622 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
5623 break;
5624
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005625 case DW_AT_allocated:
5626 case DW_AT_associated:
5627 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005628 case DW_AT_calling_convention:
5629 case DW_AT_data_location:
5630 case DW_AT_elemental:
5631 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005632 case DW_AT_frame_base:
5633 case DW_AT_high_pc:
5634 case DW_AT_low_pc:
5635 case DW_AT_object_pointer:
5636 case DW_AT_prototyped:
5637 case DW_AT_pure:
5638 case DW_AT_ranges:
5639 case DW_AT_recursive:
5640 case DW_AT_return_addr:
5641 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005642 case DW_AT_start_scope:
5643 case DW_AT_static_link:
5644 case DW_AT_trampoline:
5645 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005646 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005647 case DW_AT_description:
5648 case DW_AT_sibling:
5649 break;
5650 }
5651 }
5652 }
Greg Clayton24739922010-10-13 03:15:28 +00005653 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005654
Greg Clayton81c22f62011-10-19 18:09:39 +00005655 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 +00005656
Greg Clayton24739922010-10-13 03:15:28 +00005657 clang_type_t return_clang_type = NULL;
5658 Type *func_type = NULL;
5659
5660 if (type_die_offset != DW_INVALID_OFFSET)
5661 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00005662
Greg Clayton24739922010-10-13 03:15:28 +00005663 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00005664 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00005665 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005666 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005667
Greg Claytonf51de672010-10-01 02:31:07 +00005668
Greg Clayton24739922010-10-13 03:15:28 +00005669 std::vector<clang_type_t> function_param_types;
5670 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005671
Greg Clayton24739922010-10-13 03:15:28 +00005672 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00005673
Greg Claytoncb5860a2011-10-13 23:49:28 +00005674 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
5675 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00005676 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
5677
Greg Claytonf0705c82011-10-22 03:33:13 +00005678 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00005679 // Start off static. This will be set to false in ParseChildParameters(...)
5680 // if we find a "this" paramters as the first parameter
5681 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00005682 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005683 ClangASTContext::TemplateParameterInfos template_param_infos;
5684
Greg Clayton24739922010-10-13 03:15:28 +00005685 if (die->HasChildren())
5686 {
Greg Clayton0fffff52010-09-24 05:15:53 +00005687 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005688 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00005689 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005690 dwarf_cu,
5691 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00005692 skip_artificial,
5693 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005694 type_list,
5695 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00005696 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005697 type_quals,
5698 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00005699 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005700
Greg Clayton24739922010-10-13 03:15:28 +00005701 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005702 clang_type = ast.CreateFunctionType (return_clang_type,
5703 &function_param_types[0],
5704 function_param_types.size(),
5705 is_variadic,
5706 type_quals);
5707
Greg Clayton24739922010-10-13 03:15:28 +00005708 if (type_name_cstr)
5709 {
5710 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00005711 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005712 {
Greg Clayton7c810422012-01-18 23:40:49 +00005713 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00005714 ConstString class_name_no_category;
5715 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00005716 {
Greg Claytone42ae842012-01-19 03:24:53 +00005717 // Use the class name with no category if there is one
5718 if (class_name_no_category)
5719 class_name = class_name_no_category;
5720
Greg Clayton24739922010-10-13 03:15:28 +00005721 SymbolContext empty_sc;
5722 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00005723 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00005724 {
Greg Clayton24739922010-10-13 03:15:28 +00005725 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00005726 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00005727
5728 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00005729 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005730 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
5731 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
5732 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00005733 }
Greg Clayton24739922010-10-13 03:15:28 +00005734 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005735
Greg Clayton24739922010-10-13 03:15:28 +00005736 if (class_opaque_type)
5737 {
5738 // If accessibility isn't set to anything valid, assume public for
5739 // now...
5740 if (accessibility == eAccessNone)
5741 accessibility = eAccessPublic;
5742
5743 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005744 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
5745 type_name_cstr,
5746 clang_type,
5747 accessibility);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005748 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton24739922010-10-13 03:15:28 +00005749 type_handled = objc_method_decl != NULL;
Sean Callanan60217122012-04-13 00:10:03 +00005750 GetClangASTContext().SetMetadata((uintptr_t)objc_method_decl, MakeUserID(die->GetOffset()));
Greg Clayton24739922010-10-13 03:15:28 +00005751 }
5752 }
Greg Clayton5113dc82011-08-12 06:47:54 +00005753 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00005754 {
5755 // Look at the parent of this DIE and see if is is
5756 // a class or struct and see if this is actually a
5757 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00005758 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00005759 if (class_type)
5760 {
Greg Clayton72da3972011-08-16 18:40:23 +00005761 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00005762 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005763 // We have a specification which we are going to base our function
5764 // prototype off of, so we need this type to be completed so that the
5765 // m_die_to_decl_ctx for the method in the specification has a valid
5766 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005767 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00005768 // If we have a specification, then the function type should have been
5769 // made with the specification and not with this die.
5770 DWARFCompileUnitSP spec_cu_sp;
5771 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005772 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005773 if (spec_clang_decl_ctx)
5774 {
5775 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
5776 }
5777 else
Jim Inghamc1663042011-09-29 22:12:35 +00005778 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005779 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
5780 MakeUserID(die->GetOffset()),
5781 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005782 }
Greg Clayton72da3972011-08-16 18:40:23 +00005783 type_handled = true;
5784 }
5785 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
5786 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005787 // We have a specification which we are going to base our function
5788 // prototype off of, so we need this type to be completed so that the
5789 // m_die_to_decl_ctx for the method in the abstract origin has a valid
5790 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005791 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00005792
Greg Clayton72da3972011-08-16 18:40:23 +00005793 DWARFCompileUnitSP abs_cu_sp;
5794 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005795 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005796 if (abs_clang_decl_ctx)
5797 {
5798 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
5799 }
5800 else
Jim Inghamc1663042011-09-29 22:12:35 +00005801 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005802 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
5803 MakeUserID(die->GetOffset()),
5804 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005805 }
Greg Clayton72da3972011-08-16 18:40:23 +00005806 type_handled = true;
5807 }
5808 else
5809 {
5810 clang_type_t class_opaque_type = class_type->GetClangForwardType();
5811 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00005812 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005813 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00005814 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005815 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
5816 // in the DWARF for C++ methods... Default to public for now...
5817 if (accessibility == eAccessNone)
5818 accessibility = eAccessPublic;
5819
5820 if (!is_static && !die->HasChildren())
5821 {
5822 // We have a C++ member function with no children (this pointer!)
5823 // and clang will get mad if we try and make a function that isn't
5824 // well formed in the DWARF, so we will just skip it...
5825 type_handled = true;
5826 }
5827 else
5828 {
5829 clang::CXXMethodDecl *cxx_method_decl;
5830 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00005831 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 +00005832 type_name_cstr,
5833 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00005834 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00005835 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
5836 m_obj_file->GetFileSpec().GetFilename().GetCString());
5837
Sean Callananc1b732d2011-11-01 18:07:13 +00005838 const bool is_attr_used = false;
5839
Greg Clayton20568dd2011-10-13 23:13:20 +00005840 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
5841 type_name_cstr,
5842 clang_type,
5843 accessibility,
5844 is_virtual,
5845 is_static,
5846 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00005847 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00005848 is_attr_used,
5849 is_artificial);
Greg Clayton20568dd2011-10-13 23:13:20 +00005850 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
5851
Greg Claytonf49e65a2011-11-10 18:31:53 +00005852 Host::SetCrashDescription (NULL);
5853
Greg Clayton20568dd2011-10-13 23:13:20 +00005854 type_handled = cxx_method_decl != NULL;
Sean Callanan60217122012-04-13 00:10:03 +00005855
5856 GetClangASTContext().SetMetadata((uintptr_t)cxx_method_decl, MakeUserID(die->GetOffset()));
Greg Clayton20568dd2011-10-13 23:13:20 +00005857 }
Greg Clayton72da3972011-08-16 18:40:23 +00005858 }
5859 else
5860 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005861 // We were asked to parse the type for a method in a class, yet the
5862 // class hasn't been asked to complete itself through the
5863 // clang::ExternalASTSource protocol, so we need to just have the
5864 // class complete itself and do things the right way, then our
5865 // DIE should then have an entry in the m_die_to_type map. First
5866 // we need to modify the m_die_to_type so it doesn't think we are
5867 // trying to parse this DIE anymore...
5868 m_die_to_type[die] = NULL;
5869
5870 // Now we get the full type to force our class type to complete itself
5871 // using the clang::ExternalASTSource protocol which will parse all
5872 // base classes and all methods (including the method for this DIE).
5873 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005874
Greg Clayton20568dd2011-10-13 23:13:20 +00005875 // The type for this DIE should have been filled in the function call above
5876 type_ptr = m_die_to_type[die];
5877 if (type_ptr)
5878 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00005879 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00005880 break;
5881 }
Sean Callanan02eee4d2012-04-12 23:10:00 +00005882
5883 // FIXME This is fixing some even uglier behavior but we really need to
5884 // uniq the methods of each class as well as the class itself.
5885 // <rdar://problem/11240464>
5886 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00005887 }
Greg Clayton931180e2011-01-27 06:44:37 +00005888 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005889 }
5890 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005891 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005892 }
Greg Clayton24739922010-10-13 03:15:28 +00005893
5894 if (!type_handled)
5895 {
5896 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00005897 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
5898 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005899 clang_type,
5900 storage,
5901 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005902
5903// if (template_param_infos.GetSize() > 0)
5904// {
5905// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
5906// function_decl,
5907// type_name_cstr,
5908// template_param_infos);
5909//
5910// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
5911// func_template_decl,
5912// template_param_infos);
5913// }
Greg Clayton24739922010-10-13 03:15:28 +00005914 // Add the decl to our DIE to decl context map
5915 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00005916 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00005917 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005918 ast.SetFunctionParameters (function_decl,
5919 &function_param_decls.front(),
5920 function_param_decls.size());
Sean Callanan60217122012-04-13 00:10:03 +00005921
5922 GetClangASTContext().SetMetadata((uintptr_t)function_decl, MakeUserID(die->GetOffset()));
Greg Clayton24739922010-10-13 03:15:28 +00005923 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005924 }
Greg Clayton81c22f62011-10-19 18:09:39 +00005925 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005926 this,
5927 type_name_const_str,
5928 0,
5929 NULL,
5930 LLDB_INVALID_UID,
5931 Type::eEncodingIsUID,
5932 &decl,
5933 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005934 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00005935 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005936 }
5937 break;
5938
5939 case DW_TAG_array_type:
5940 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005941 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005942 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005943
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005944 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005945 int64_t first_index = 0;
5946 uint32_t byte_stride = 0;
5947 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00005948 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005949
5950 if (num_attributes > 0)
5951 {
5952 uint32_t i;
5953 for (i=0; i<num_attributes; ++i)
5954 {
5955 attr = attributes.AttributeAtIndex(i);
5956 DWARFFormValue form_value;
5957 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5958 {
5959 switch (attr)
5960 {
5961 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5962 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5963 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5964 case DW_AT_name:
5965 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005966 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005967 break;
5968
5969 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005970 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005971 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
5972 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005973 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005974 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005975 case DW_AT_allocated:
5976 case DW_AT_associated:
5977 case DW_AT_data_location:
5978 case DW_AT_description:
5979 case DW_AT_ordering:
5980 case DW_AT_start_scope:
5981 case DW_AT_visibility:
5982 case DW_AT_specification:
5983 case DW_AT_abstract_origin:
5984 case DW_AT_sibling:
5985 break;
5986 }
5987 }
5988 }
5989
Greg Clayton81c22f62011-10-19 18:09:39 +00005990 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 +00005991
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005992 Type *element_type = ResolveTypeUID(type_die_offset);
5993
5994 if (element_type)
5995 {
5996 std::vector<uint64_t> element_orders;
5997 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00005998 // We have an array that claims to have no members, lets give it at least one member...
5999 if (element_orders.empty())
6000 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006001 if (byte_stride == 0 && bit_stride == 0)
6002 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00006003 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006004 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
6005 uint64_t num_elements = 0;
6006 std::vector<uint64_t>::const_reverse_iterator pos;
6007 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
6008 for (pos = element_orders.rbegin(); pos != end; ++pos)
6009 {
6010 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006011 clang_type = ast.CreateArrayType (array_element_type,
6012 num_elements,
6013 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006014 array_element_type = clang_type;
6015 array_element_bit_stride = array_element_bit_stride * num_elements;
6016 }
6017 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00006018 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006019 this,
6020 empty_name,
6021 array_element_bit_stride / 8,
6022 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00006023 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006024 Type::eEncodingIsUID,
6025 &decl,
6026 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006027 Type::eResolveStateFull));
6028 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006029 }
6030 }
6031 }
6032 break;
6033
Greg Clayton9b81a312010-06-12 01:20:30 +00006034 case DW_TAG_ptr_to_member_type:
6035 {
6036 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
6037 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
6038
Greg Claytond88d7592010-09-15 08:33:30 +00006039 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00006040
6041 if (num_attributes > 0) {
6042 uint32_t i;
6043 for (i=0; i<num_attributes; ++i)
6044 {
6045 attr = attributes.AttributeAtIndex(i);
6046 DWARFFormValue form_value;
6047 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6048 {
6049 switch (attr)
6050 {
6051 case DW_AT_type:
6052 type_die_offset = form_value.Reference(dwarf_cu); break;
6053 case DW_AT_containing_type:
6054 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
6055 }
6056 }
6057 }
6058
6059 Type *pointee_type = ResolveTypeUID(type_die_offset);
6060 Type *class_type = ResolveTypeUID(containing_type_die_offset);
6061
Greg Clayton526e5af2010-11-13 03:52:47 +00006062 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
6063 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00006064
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006065 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
6066 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00006067
Greg Clayton526e5af2010-11-13 03:52:47 +00006068 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
6069 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00006070
Greg Clayton81c22f62011-10-19 18:09:39 +00006071 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006072 this,
6073 type_name_const_str,
6074 byte_size,
6075 NULL,
6076 LLDB_INVALID_UID,
6077 Type::eEncodingIsUID,
6078 NULL,
6079 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006080 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00006081 }
6082
6083 break;
6084 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006085 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00006086 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006087 break;
6088 }
6089
6090 if (type_sp.get())
6091 {
6092 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6093 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6094
6095 SymbolContextScope * symbol_context_scope = NULL;
6096 if (sc_parent_tag == DW_TAG_compile_unit)
6097 {
6098 symbol_context_scope = sc.comp_unit;
6099 }
6100 else if (sc.function != NULL)
6101 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006102 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006103 if (symbol_context_scope == NULL)
6104 symbol_context_scope = sc.function;
6105 }
6106
6107 if (symbol_context_scope != NULL)
6108 {
6109 type_sp->SetSymbolContextScope(symbol_context_scope);
6110 }
6111
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006112 // We are ready to put this type into the uniqued list up at the module level
6113 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00006114
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006115 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006116 }
6117 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00006118 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006119 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006120 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006121 }
6122 }
6123 return type_sp;
6124}
6125
6126size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00006127SymbolFileDWARF::ParseTypes
6128(
6129 const SymbolContext& sc,
6130 DWARFCompileUnit* dwarf_cu,
6131 const DWARFDebugInfoEntry *die,
6132 bool parse_siblings,
6133 bool parse_children
6134)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006135{
6136 size_t types_added = 0;
6137 while (die != NULL)
6138 {
6139 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00006140 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006141 {
6142 if (type_is_new)
6143 ++types_added;
6144 }
6145
6146 if (parse_children && die->HasChildren())
6147 {
6148 if (die->Tag() == DW_TAG_subprogram)
6149 {
6150 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00006151 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006152 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
6153 }
6154 else
6155 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
6156 }
6157
6158 if (parse_siblings)
6159 die = die->GetSibling();
6160 else
6161 die = NULL;
6162 }
6163 return types_added;
6164}
6165
6166
6167size_t
6168SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
6169{
6170 assert(sc.comp_unit && sc.function);
6171 size_t functions_added = 0;
6172 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
6173 if (dwarf_cu)
6174 {
6175 dw_offset_t function_die_offset = sc.function->GetID();
6176 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
6177 if (function_die)
6178 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00006179 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006180 }
6181 }
6182
6183 return functions_added;
6184}
6185
6186
6187size_t
6188SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
6189{
6190 // At least a compile unit must be valid
6191 assert(sc.comp_unit);
6192 size_t types_added = 0;
6193 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
6194 if (dwarf_cu)
6195 {
6196 if (sc.function)
6197 {
6198 dw_offset_t function_die_offset = sc.function->GetID();
6199 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
6200 if (func_die && func_die->HasChildren())
6201 {
6202 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
6203 }
6204 }
6205 else
6206 {
6207 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
6208 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
6209 {
6210 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
6211 }
6212 }
6213 }
6214
6215 return types_added;
6216}
6217
6218size_t
6219SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
6220{
6221 if (sc.comp_unit != NULL)
6222 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00006223 DWARFDebugInfo* info = DebugInfo();
6224 if (info == NULL)
6225 return 0;
6226
6227 uint32_t cu_idx = UINT32_MAX;
6228 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006229
6230 if (dwarf_cu == NULL)
6231 return 0;
6232
6233 if (sc.function)
6234 {
6235 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00006236
6237 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 +00006238 if (func_lo_pc != DW_INVALID_ADDRESS)
6239 {
6240 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00006241
Greg Claytone38a5ed2012-01-05 03:57:59 +00006242 // Let all blocks know they have parse all their variables
6243 sc.function->GetBlock (false).SetDidParseVariables (true, true);
6244 return num_variables;
6245 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006246 }
6247 else if (sc.comp_unit)
6248 {
6249 uint32_t vars_added = 0;
6250 VariableListSP variables (sc.comp_unit->GetVariableList(false));
6251
6252 if (variables.get() == NULL)
6253 {
6254 variables.reset(new VariableList());
6255 sc.comp_unit->SetVariableList(variables);
6256
Greg Claytond4a2b372011-09-12 23:21:58 +00006257 DWARFCompileUnit* match_dwarf_cu = NULL;
6258 const DWARFDebugInfoEntry* die = NULL;
6259 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00006260 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00006261 {
Greg Clayton97fbc342011-10-20 22:30:33 +00006262 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00006263 {
6264 DWARFMappedHash::DIEInfoArray hash_data_array;
6265 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
6266 dwarf_cu->GetNextCompileUnitOffset(),
6267 hash_data_array))
6268 {
6269 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
6270 }
6271 }
Greg Clayton7f995132011-10-04 22:41:51 +00006272 }
6273 else
6274 {
6275 // Index if we already haven't to make sure the compile units
6276 // get indexed and make their global DIE index list
6277 if (!m_indexed)
6278 Index ();
6279
6280 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
6281 dwarf_cu->GetNextCompileUnitOffset(),
6282 die_offsets);
6283 }
6284
6285 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00006286 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006287 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006288 DWARFDebugInfo* debug_info = DebugInfo();
6289 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006290 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006291 const dw_offset_t die_offset = die_offsets[i];
6292 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00006293 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00006294 {
Greg Clayton95d87902011-11-11 03:16:25 +00006295 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
6296 if (var_sp)
6297 {
6298 variables->AddVariableIfUnique (var_sp);
6299 ++vars_added;
6300 }
Greg Claytond4a2b372011-09-12 23:21:58 +00006301 }
Greg Clayton95d87902011-11-11 03:16:25 +00006302 else
6303 {
6304 if (m_using_apple_tables)
6305 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006306 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 +00006307 }
6308 }
6309
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006310 }
6311 }
6312 }
6313 return vars_added;
6314 }
6315 }
6316 return 0;
6317}
6318
6319
6320VariableSP
6321SymbolFileDWARF::ParseVariableDIE
6322(
6323 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006324 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006325 const DWARFDebugInfoEntry *die,
6326 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006327)
6328{
6329
Greg Clayton83c5cd92010-11-14 22:13:40 +00006330 VariableSP var_sp (m_die_to_variable_sp[die]);
6331 if (var_sp)
6332 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006333
6334 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00006335
6336 if ((tag == DW_TAG_variable) ||
6337 (tag == DW_TAG_constant) ||
6338 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006339 {
Greg Clayton7f995132011-10-04 22:41:51 +00006340 DWARFDebugInfoEntry::Attributes attributes;
6341 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
6342 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006343 {
Greg Clayton7f995132011-10-04 22:41:51 +00006344 const char *name = NULL;
6345 const char *mangled = NULL;
6346 Declaration decl;
6347 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00006348 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00006349 DWARFExpression location;
6350 bool is_external = false;
6351 bool is_artificial = false;
6352 bool location_is_const_value_data = false;
6353 AccessType accessibility = eAccessNone;
6354
6355 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006356 {
Greg Clayton7f995132011-10-04 22:41:51 +00006357 dw_attr_t attr = attributes.AttributeAtIndex(i);
6358 DWARFFormValue form_value;
6359 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006360 {
Greg Clayton7f995132011-10-04 22:41:51 +00006361 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006362 {
Greg Clayton7f995132011-10-04 22:41:51 +00006363 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6364 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6365 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6366 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
6367 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00006368 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00006369 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
6370 case DW_AT_const_value:
6371 location_is_const_value_data = true;
6372 // Fall through...
6373 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006374 {
Greg Clayton7f995132011-10-04 22:41:51 +00006375 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006376 {
Greg Clayton7f995132011-10-04 22:41:51 +00006377 const DataExtractor& debug_info_data = get_debug_info_data();
6378
6379 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
6380 uint32_t block_length = form_value.Unsigned();
6381 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
6382 }
6383 else
6384 {
6385 const DataExtractor& debug_loc_data = get_debug_loc_data();
6386 const dw_offset_t debug_loc_offset = form_value.Unsigned();
6387
6388 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
6389 if (loc_list_length > 0)
6390 {
6391 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
6392 assert (func_low_pc != LLDB_INVALID_ADDRESS);
6393 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
6394 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006395 }
6396 }
Greg Clayton7f995132011-10-04 22:41:51 +00006397 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006398
Greg Clayton7f995132011-10-04 22:41:51 +00006399 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
6400 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
6401 case DW_AT_declaration:
6402 case DW_AT_description:
6403 case DW_AT_endianity:
6404 case DW_AT_segment:
6405 case DW_AT_start_scope:
6406 case DW_AT_visibility:
6407 default:
6408 case DW_AT_abstract_origin:
6409 case DW_AT_sibling:
6410 case DW_AT_specification:
6411 break;
6412 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006413 }
6414 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006415
Greg Clayton7f995132011-10-04 22:41:51 +00006416 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006417 {
Greg Clayton7f995132011-10-04 22:41:51 +00006418 ValueType scope = eValueTypeInvalid;
6419
6420 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6421 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006422 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00006423
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006424 // DWARF doesn't specify if a DW_TAG_variable is a local, global
6425 // or static variable, so we have to do a little digging by
6426 // looking at the location of a varaible to see if it contains
6427 // a DW_OP_addr opcode _somewhere_ in the definition. I say
6428 // somewhere because clang likes to combine small global variables
6429 // into the same symbol and have locations like:
6430 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
6431 // So if we don't have a DW_TAG_formal_parameter, we can look at
6432 // the location to see if it contains a DW_OP_addr opcode, and
6433 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00006434 if (tag == DW_TAG_formal_parameter)
6435 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00006436 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006437 {
Greg Clayton96c09682012-01-04 22:56:43 +00006438 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00006439 // Check if the location has a DW_OP_addr with any address value...
6440 addr_t location_has_op_addr = false;
6441 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00006442 {
6443 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
6444 if (op_error)
6445 {
6446 StreamString strm;
6447 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006448 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 +00006449 }
6450 }
Greg Claytond1767f02011-12-08 02:13:16 +00006451
6452 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006453 {
Greg Claytond1767f02011-12-08 02:13:16 +00006454 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006455 {
Greg Claytond1767f02011-12-08 02:13:16 +00006456 scope = eValueTypeVariableGlobal;
6457
6458 if (m_debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006459 {
Greg Claytond1767f02011-12-08 02:13:16 +00006460 // When leaving the DWARF in the .o files on darwin,
6461 // when we have a global variable that wasn't initialized,
6462 // the .o file might not have allocated a virtual
6463 // address for the global variable. In this case it will
6464 // have created a symbol for the global variable
6465 // that is undefined and external and the value will
6466 // be the byte size of the variable. When we do the
6467 // address map in SymbolFileDWARFDebugMap we rely on
6468 // having an address, we need to do some magic here
6469 // so we can get the correct address for our global
6470 // variable. The address for all of these entries
6471 // will be zero, and there will be an undefined symbol
6472 // in this object file, and the executable will have
6473 // a matching symbol with a good address. So here we
6474 // dig up the correct address and replace it in the
6475 // location for the variable, and set the variable's
6476 // symbol context scope to be that of the main executable
6477 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00006478 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006479 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006480
Greg Claytond1767f02011-12-08 02:13:16 +00006481 // we have a possible uninitialized extern global
6482 Symtab *symtab = m_obj_file->GetSymtab();
6483 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006484 {
Greg Claytond1767f02011-12-08 02:13:16 +00006485 ConstString const_name(name);
6486 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
6487 eSymbolTypeUndefined,
6488 Symtab::eDebugNo,
6489 Symtab::eVisibilityExtern);
6490
6491 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006492 {
Greg Claytond1767f02011-12-08 02:13:16 +00006493 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
6494 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006495 {
Greg Claytond1767f02011-12-08 02:13:16 +00006496 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
6497 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
6498 eSymbolTypeData,
6499 Symtab::eDebugYes,
6500 Symtab::eVisibilityExtern);
6501 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006502 {
Greg Claytone7612132012-03-07 21:03:09 +00006503 if (defined_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006504 {
Greg Claytone7612132012-03-07 21:03:09 +00006505 const addr_t defined_addr = defined_symbol->GetAddress().GetFileAddress();
Greg Claytond1767f02011-12-08 02:13:16 +00006506 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006507 {
Greg Claytond1767f02011-12-08 02:13:16 +00006508 if (location.Update_DW_OP_addr (defined_addr))
6509 {
6510 symbol_context_scope = defined_symbol;
6511 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006512 }
6513 }
6514 }
6515 }
6516 }
6517 }
6518 }
6519 }
6520 }
Greg Claytond1767f02011-12-08 02:13:16 +00006521 else
6522 {
6523 scope = eValueTypeVariableStatic;
6524 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006525 }
6526 else
Greg Claytond1767f02011-12-08 02:13:16 +00006527 {
6528 scope = eValueTypeVariableLocal;
6529 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006530 }
Greg Clayton7f995132011-10-04 22:41:51 +00006531
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006532 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00006533 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006534 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00006535 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006536 case DW_TAG_subprogram:
6537 case DW_TAG_inlined_subroutine:
6538 case DW_TAG_lexical_block:
6539 if (sc.function)
6540 {
6541 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
6542 if (symbol_context_scope == NULL)
6543 symbol_context_scope = sc.function;
6544 }
6545 break;
6546
6547 default:
6548 symbol_context_scope = sc.comp_unit;
6549 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00006550 }
Greg Clayton7f995132011-10-04 22:41:51 +00006551 }
6552
Greg Clayton5cf58b92011-10-05 22:22:08 +00006553 if (symbol_context_scope)
6554 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006555 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
6556 name,
6557 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00006558 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00006559 scope,
6560 symbol_context_scope,
6561 &decl,
6562 location,
6563 is_external,
6564 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00006565
6566 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
6567 }
6568 else
6569 {
6570 // Not ready to parse this variable yet. It might be a global
6571 // or static variable that is in a function scope and the function
6572 // in the symbol context wasn't filled in yet
6573 return var_sp;
6574 }
Greg Clayton7f995132011-10-04 22:41:51 +00006575 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006576 }
Greg Clayton7f995132011-10-04 22:41:51 +00006577 // Cache var_sp even if NULL (the variable was just a specification or
6578 // was missing vital information to be able to be displayed in the debugger
6579 // (missing location due to optimization, etc)) so we don't re-parse
6580 // this DIE over and over later...
6581 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006582 }
6583 return var_sp;
6584}
6585
Greg Claytonc662ec82011-06-17 22:10:16 +00006586
6587const DWARFDebugInfoEntry *
6588SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
6589 dw_offset_t spec_block_die_offset,
6590 DWARFCompileUnit **result_die_cu_handle)
6591{
6592 // Give the concrete function die specified by "func_die_offset", find the
6593 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6594 // to "spec_block_die_offset"
6595 DWARFDebugInfo* info = DebugInfo();
6596
6597 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
6598 if (die)
6599 {
6600 assert (*result_die_cu_handle);
6601 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
6602 }
6603 return NULL;
6604}
6605
6606
6607const DWARFDebugInfoEntry *
6608SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
6609 const DWARFDebugInfoEntry *die,
6610 dw_offset_t spec_block_die_offset,
6611 DWARFCompileUnit **result_die_cu_handle)
6612{
6613 if (die)
6614 {
6615 switch (die->Tag())
6616 {
6617 case DW_TAG_subprogram:
6618 case DW_TAG_inlined_subroutine:
6619 case DW_TAG_lexical_block:
6620 {
6621 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
6622 {
6623 *result_die_cu_handle = dwarf_cu;
6624 return die;
6625 }
6626
6627 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
6628 {
6629 *result_die_cu_handle = dwarf_cu;
6630 return die;
6631 }
6632 }
6633 break;
6634 }
6635
6636 // Give the concrete function die specified by "func_die_offset", find the
6637 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6638 // to "spec_block_die_offset"
6639 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
6640 {
6641 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
6642 child_die,
6643 spec_block_die_offset,
6644 result_die_cu_handle);
6645 if (result_die)
6646 return result_die;
6647 }
6648 }
6649
6650 *result_die_cu_handle = NULL;
6651 return NULL;
6652}
6653
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006654size_t
6655SymbolFileDWARF::ParseVariables
6656(
6657 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006658 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006659 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006660 const DWARFDebugInfoEntry *orig_die,
6661 bool parse_siblings,
6662 bool parse_children,
6663 VariableList* cc_variable_list
6664)
6665{
6666 if (orig_die == NULL)
6667 return 0;
6668
Greg Claytonc662ec82011-06-17 22:10:16 +00006669 VariableListSP variable_list_sp;
6670
6671 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006672 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00006673 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006674 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006675 dw_tag_t tag = die->Tag();
6676
6677 // Check to see if we have already parsed this variable or constant?
6678 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006679 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006680 if (cc_variable_list)
6681 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006682 }
6683 else
6684 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006685 // We haven't already parsed it, lets do that now.
6686 if ((tag == DW_TAG_variable) ||
6687 (tag == DW_TAG_constant) ||
6688 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006689 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006690 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00006691 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006692 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
6693 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6694 switch (parent_tag)
6695 {
6696 case DW_TAG_compile_unit:
6697 if (sc.comp_unit != NULL)
6698 {
6699 variable_list_sp = sc.comp_unit->GetVariableList(false);
6700 if (variable_list_sp.get() == NULL)
6701 {
6702 variable_list_sp.reset(new VariableList());
6703 sc.comp_unit->SetVariableList(variable_list_sp);
6704 }
6705 }
6706 else
6707 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006708 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
6709 MakeUserID(sc_parent_die->GetOffset()),
6710 DW_TAG_value_to_name (parent_tag),
6711 MakeUserID(orig_die->GetOffset()),
6712 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006713 }
6714 break;
6715
6716 case DW_TAG_subprogram:
6717 case DW_TAG_inlined_subroutine:
6718 case DW_TAG_lexical_block:
6719 if (sc.function != NULL)
6720 {
6721 // Check to see if we already have parsed the variables for the given scope
6722
Greg Clayton81c22f62011-10-19 18:09:39 +00006723 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006724 if (block == NULL)
6725 {
6726 // This must be a specification or abstract origin with
6727 // a concrete block couterpart in the current function. We need
6728 // to find the concrete block so we can correctly add the
6729 // variable to it
6730 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
6731 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
6732 sc_parent_die->GetOffset(),
6733 &concrete_block_die_cu);
6734 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00006735 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006736 }
6737
6738 if (block != NULL)
6739 {
6740 const bool can_create = false;
6741 variable_list_sp = block->GetBlockVariableList (can_create);
6742 if (variable_list_sp.get() == NULL)
6743 {
6744 variable_list_sp.reset(new VariableList());
6745 block->SetVariableList(variable_list_sp);
6746 }
6747 }
6748 }
6749 break;
6750
6751 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00006752 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
6753 MakeUserID(orig_die->GetOffset()),
6754 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006755 break;
6756 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00006757 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006758
6759 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006760 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00006761 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
6762 if (var_sp)
6763 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006764 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00006765 if (cc_variable_list)
6766 cc_variable_list->AddVariableIfUnique (var_sp);
6767 ++vars_added;
6768 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006769 }
6770 }
6771 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006772
6773 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
6774
6775 if (!skip_children && parse_children && die->HasChildren())
6776 {
6777 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
6778 }
6779
6780 if (parse_siblings)
6781 die = die->GetSibling();
6782 else
6783 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006784 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006785 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006786}
6787
6788//------------------------------------------------------------------
6789// PluginInterface protocol
6790//------------------------------------------------------------------
6791const char *
6792SymbolFileDWARF::GetPluginName()
6793{
6794 return "SymbolFileDWARF";
6795}
6796
6797const char *
6798SymbolFileDWARF::GetShortPluginName()
6799{
6800 return GetPluginNameStatic();
6801}
6802
6803uint32_t
6804SymbolFileDWARF::GetPluginVersion()
6805{
6806 return 1;
6807}
6808
6809void
Greg Clayton6beaaa62011-01-17 03:46:26 +00006810SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
6811{
6812 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6813 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6814 if (clang_type)
6815 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6816}
6817
6818void
6819SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
6820{
6821 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6822 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6823 if (clang_type)
6824 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6825}
6826
Greg Claytona2721472011-06-25 00:44:06 +00006827void
Sean Callanancc427fa2011-07-30 02:42:06 +00006828SymbolFileDWARF::DumpIndexes ()
6829{
6830 StreamFile s(stdout, false);
6831
6832 s.Printf ("DWARF index for (%s) '%s/%s':",
6833 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
6834 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
6835 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
6836 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
6837 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
6838 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
6839 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
6840 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
6841 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
6842 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
6843 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
6844}
6845
6846void
6847SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
6848 const char *name,
6849 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00006850{
Sean Callanancc427fa2011-07-30 02:42:06 +00006851 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00006852
6853 if (iter == m_decl_ctx_to_die.end())
6854 return;
6855
Greg Claytoncb5860a2011-10-13 23:49:28 +00006856 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00006857 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006858 const DWARFDebugInfoEntry *context_die = *pos;
6859
6860 if (!results)
6861 return;
6862
6863 DWARFDebugInfo* info = DebugInfo();
6864
6865 DIEArray die_offsets;
6866
6867 DWARFCompileUnit* dwarf_cu = NULL;
6868 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00006869
6870 if (m_using_apple_tables)
6871 {
6872 if (m_apple_types_ap.get())
6873 m_apple_types_ap->FindByName (name, die_offsets);
6874 }
6875 else
6876 {
6877 if (!m_indexed)
6878 Index ();
6879
6880 m_type_index.Find (ConstString(name), die_offsets);
6881 }
6882
Greg Clayton220a0072011-12-09 08:48:30 +00006883 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006884
6885 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00006886 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006887 for (size_t i = 0; i < num_matches; ++i)
6888 {
6889 const dw_offset_t die_offset = die_offsets[i];
6890 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00006891
Greg Claytoncb5860a2011-10-13 23:49:28 +00006892 if (die->GetParent() != context_die)
6893 continue;
6894
6895 Type *matching_type = ResolveType (dwarf_cu, die);
6896
Greg Clayton42ce2f32011-12-12 21:50:19 +00006897 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006898 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
6899
6900 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
6901 {
6902 clang::TagDecl *tag_decl = tag_type->getDecl();
6903 results->push_back(tag_decl);
6904 }
6905 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
6906 {
6907 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
6908 results->push_back(typedef_decl);
6909 }
Greg Claytona2721472011-06-25 00:44:06 +00006910 }
6911 }
6912 }
6913}
6914
6915void
6916SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00006917 const clang::DeclContext *decl_context,
6918 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00006919 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
6920{
Greg Clayton85ae2e12011-10-18 23:36:41 +00006921
6922 switch (decl_context->getDeclKind())
6923 {
6924 case clang::Decl::Namespace:
6925 case clang::Decl::TranslationUnit:
6926 {
6927 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6928 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
6929 }
6930 break;
6931 default:
6932 break;
6933 }
Greg Claytona2721472011-06-25 00:44:06 +00006934}
Greg Claytoncaab74e2012-01-28 00:48:57 +00006935
6936bool
6937SymbolFileDWARF::LayoutRecordType (void *baton,
6938 const clang::RecordDecl *record_decl,
6939 uint64_t &size,
6940 uint64_t &alignment,
6941 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6942 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6943 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6944{
6945 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6946 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
6947}
6948
6949
6950bool
6951SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
6952 uint64_t &bit_size,
6953 uint64_t &alignment,
6954 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6955 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6956 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6957{
6958 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
6959 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
6960 bool success = false;
6961 base_offsets.clear();
6962 vbase_offsets.clear();
6963 if (pos != m_record_decl_to_layout_map.end())
6964 {
6965 bit_size = pos->second.bit_size;
6966 alignment = pos->second.alignment;
6967 field_offsets.swap(pos->second.field_offsets);
6968 m_record_decl_to_layout_map.erase(pos);
6969 success = true;
6970 }
6971 else
6972 {
6973 bit_size = 0;
6974 alignment = 0;
6975 field_offsets.clear();
6976 }
6977
6978 if (log)
6979 GetObjectFile()->GetModule()->LogMessage (log.get(),
6980 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
6981 record_decl,
6982 bit_size,
6983 alignment,
6984 (uint32_t)field_offsets.size(),
6985 (uint32_t)base_offsets.size(),
6986 (uint32_t)vbase_offsets.size(),
6987 success);
6988 return success;
6989}
6990
6991
6992