blob: 7660569766bf125d96a5cecc47298186c14b061b [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Greg Clayton3c2e3ae2012-02-06 06:42:51 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "clang/Basic/Builtins.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000025#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanancc427fa2011-07-30 02:42:06 +000027#include "llvm/Support/Casting.h"
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Module.h"
30#include "lldb/Core/PluginManager.h"
31#include "lldb/Core/RegularExpression.h"
32#include "lldb/Core/Scalar.h"
33#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000034#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000035#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Core/Timer.h"
37#include "lldb/Core/Value.h"
38
Greg Clayton20568dd2011-10-13 23:13:20 +000039#include "lldb/Host/Host.h"
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000042#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Symbol/CompileUnit.h"
44#include "lldb/Symbol/LineTable.h"
45#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
47#include "lldb/Symbol/VariableList.h"
48
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000049#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000050#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000051
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "DWARFCompileUnit.h"
53#include "DWARFDebugAbbrev.h"
54#include "DWARFDebugAranges.h"
55#include "DWARFDebugInfo.h"
56#include "DWARFDebugInfoEntry.h"
57#include "DWARFDebugLine.h"
58#include "DWARFDebugPubnames.h"
59#include "DWARFDebugRanges.h"
60#include "DWARFDIECollection.h"
61#include "DWARFFormValue.h"
62#include "DWARFLocationList.h"
63#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000064#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065
66#include <map>
67
Greg Clayton62742b12010-11-11 01:09:45 +000068//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000069
70#ifdef ENABLE_DEBUG_PRINTF
71#include <stdio.h>
72#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
73#else
74#define DEBUG_PRINTF(fmt, ...)
75#endif
76
Greg Clayton594e5ed2010-09-27 21:07:38 +000077#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
79using namespace lldb;
80using namespace lldb_private;
81
Greg Clayton219cf312012-03-30 00:51:13 +000082//static inline bool
83//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
84//{
85// switch (tag)
86// {
87// default:
88// break;
89// case DW_TAG_subprogram:
90// case DW_TAG_inlined_subroutine:
91// case DW_TAG_class_type:
92// case DW_TAG_structure_type:
93// case DW_TAG_union_type:
94// return true;
95// }
96// return false;
97//}
98//
Sean Callananc7fbf732010-08-06 00:32:49 +000099static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000100DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101{
102 switch (dwarf_accessibility)
103 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000104 case DW_ACCESS_public: return eAccessPublic;
105 case DW_ACCESS_private: return eAccessPrivate;
106 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000107 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000109 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
Greg Clayton1fba87112012-02-09 20:03:49 +0000112#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
113
114class DIEStack
115{
116public:
117
118 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
119 {
120 m_dies.push_back (DIEInfo(cu, die));
121 }
122
123
124 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
125 {
126 StreamString log_strm;
127 const size_t n = m_dies.size();
128 log_strm.Printf("DIEStack[%zu]:\n", n);
129 for (size_t i=0; i<n; i++)
130 {
131 DWARFCompileUnit *cu = m_dies[i].cu;
132 const DWARFDebugInfoEntry *die = m_dies[i].die;
133 std::string qualified_name;
134 die->GetQualifiedName(dwarf, cu, qualified_name);
135 log_strm.Printf ("[%zu] 0x%8.8x: %s name='%s'\n",
136 i,
137 die->GetOffset(),
138 DW_TAG_value_to_name(die->Tag()),
139 qualified_name.c_str());
140 }
141 log->PutCString(log_strm.GetData());
142 }
143 void Pop ()
144 {
145 m_dies.pop_back();
146 }
147
148 class ScopedPopper
149 {
150 public:
151 ScopedPopper (DIEStack &die_stack) :
152 m_die_stack (die_stack),
153 m_valid (false)
154 {
155 }
156
157 void
158 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
159 {
160 m_valid = true;
161 m_die_stack.Push (cu, die);
162 }
163
164 ~ScopedPopper ()
165 {
166 if (m_valid)
167 m_die_stack.Pop();
168 }
169
170
171
172 protected:
173 DIEStack &m_die_stack;
174 bool m_valid;
175 };
176
177protected:
178 struct DIEInfo {
179 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
180 cu(c),
181 die(d)
182 {
183 }
184 DWARFCompileUnit *cu;
185 const DWARFDebugInfoEntry *die;
186 };
187 typedef std::vector<DIEInfo> Stack;
188 Stack m_dies;
189};
190#endif
191
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192void
193SymbolFileDWARF::Initialize()
194{
195 LogChannelDWARF::Initialize();
196 PluginManager::RegisterPlugin (GetPluginNameStatic(),
197 GetPluginDescriptionStatic(),
198 CreateInstance);
199}
200
201void
202SymbolFileDWARF::Terminate()
203{
204 PluginManager::UnregisterPlugin (CreateInstance);
205 LogChannelDWARF::Initialize();
206}
207
208
209const char *
210SymbolFileDWARF::GetPluginNameStatic()
211{
Greg Claytond4a2b372011-09-12 23:21:58 +0000212 return "dwarf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213}
214
215const char *
216SymbolFileDWARF::GetPluginDescriptionStatic()
217{
218 return "DWARF and DWARF3 debug symbol file reader.";
219}
220
221
222SymbolFile*
223SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
224{
225 return new SymbolFileDWARF(obj_file);
226}
227
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000228TypeList *
229SymbolFileDWARF::GetTypeList ()
230{
231 if (m_debug_map_symfile)
232 return m_debug_map_symfile->GetTypeList();
233 return m_obj_file->GetModule()->GetTypeList();
234
235}
236
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237//----------------------------------------------------------------------
238// Gets the first parent that is a lexical block, function or inlined
239// subroutine, or compile unit.
240//----------------------------------------------------------------------
241static const DWARFDebugInfoEntry *
242GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
243{
244 const DWARFDebugInfoEntry *die;
245 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
246 {
247 dw_tag_t tag = die->Tag();
248
249 switch (tag)
250 {
251 case DW_TAG_compile_unit:
252 case DW_TAG_subprogram:
253 case DW_TAG_inlined_subroutine:
254 case DW_TAG_lexical_block:
255 return die;
256 }
257 }
258 return NULL;
259}
260
261
Greg Clayton450e3f32010-10-12 02:24:53 +0000262SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
263 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000264 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton450e3f32010-10-12 02:24:53 +0000265 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000266 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000268 m_data_debug_abbrev (),
269 m_data_debug_aranges (),
270 m_data_debug_frame (),
271 m_data_debug_info (),
272 m_data_debug_line (),
273 m_data_debug_loc (),
274 m_data_debug_ranges (),
275 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000276 m_data_apple_names (),
277 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000278 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 m_info(),
281 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000282 m_apple_names_ap (),
283 m_apple_types_ap (),
284 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000285 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000286 m_function_basename_index(),
287 m_function_fullname_index(),
288 m_function_method_index(),
289 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000290 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000291 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000292 m_type_index(),
293 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000294 m_indexed (false),
295 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000296 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000297 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000298 m_ranges(),
299 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300{
301}
302
303SymbolFileDWARF::~SymbolFileDWARF()
304{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000305 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000306 {
307 ModuleSP module_sp (m_obj_file->GetModule());
308 if (module_sp)
309 module_sp->GetClangASTContext().RemoveExternalSource ();
310 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000311}
312
313static const ConstString &
314GetDWARFMachOSegmentName ()
315{
316 static ConstString g_dwarf_section_name ("__DWARF");
317 return g_dwarf_section_name;
318}
319
Greg Claytone576ab22011-02-15 00:19:15 +0000320UniqueDWARFASTTypeMap &
321SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
322{
323 if (m_debug_map_symfile)
324 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
325 return m_unique_ast_type_map;
326}
327
Greg Clayton6beaaa62011-01-17 03:46:26 +0000328ClangASTContext &
329SymbolFileDWARF::GetClangASTContext ()
330{
331 if (m_debug_map_symfile)
332 return m_debug_map_symfile->GetClangASTContext ();
333
334 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
335 if (!m_is_external_ast_source)
336 {
337 m_is_external_ast_source = true;
338 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
339 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
340 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000341 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000342 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000343 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000344 ast.SetExternalSource (ast_source_ap);
345 }
346 return ast;
347}
348
349void
350SymbolFileDWARF::InitializeObject()
351{
352 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000353 ModuleSP module_sp (m_obj_file->GetModule());
354 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000355 {
356 const SectionList *section_list = m_obj_file->GetSectionList();
357
358 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
359
360 // Memory map the DWARF mach-o segment so we have everything mmap'ed
361 // to keep our heap memory usage down.
362 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000363 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000364 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000365 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000366 if (m_data_apple_names.GetByteSize() > 0)
367 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000368 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
369 if (m_apple_names_ap->IsValid())
370 m_using_apple_tables = true;
371 else
Greg Clayton7f995132011-10-04 22:41:51 +0000372 m_apple_names_ap.reset();
373 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000374 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000375 if (m_data_apple_types.GetByteSize() > 0)
376 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000377 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
378 if (m_apple_types_ap->IsValid())
379 m_using_apple_tables = true;
380 else
Greg Clayton7f995132011-10-04 22:41:51 +0000381 m_apple_types_ap.reset();
382 }
383
384 get_apple_namespaces_data();
385 if (m_data_apple_namespaces.GetByteSize() > 0)
386 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000387 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
388 if (m_apple_namespaces_ap->IsValid())
389 m_using_apple_tables = true;
390 else
Greg Clayton7f995132011-10-04 22:41:51 +0000391 m_apple_namespaces_ap.reset();
392 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000393
Greg Clayton5009f9d2011-10-27 17:55:14 +0000394 get_apple_objc_data();
395 if (m_data_apple_objc.GetByteSize() > 0)
396 {
397 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
398 if (m_apple_objc_ap->IsValid())
399 m_using_apple_tables = true;
400 else
401 m_apple_objc_ap.reset();
402 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
405bool
406SymbolFileDWARF::SupportedVersion(uint16_t version)
407{
408 return version == 2 || version == 3;
409}
410
411uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000412SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413{
414 uint32_t abilities = 0;
415 if (m_obj_file != NULL)
416 {
417 const Section* section = NULL;
418 const SectionList *section_list = m_obj_file->GetSectionList();
419 if (section_list == NULL)
420 return 0;
421
422 uint64_t debug_abbrev_file_size = 0;
423 uint64_t debug_aranges_file_size = 0;
424 uint64_t debug_frame_file_size = 0;
425 uint64_t debug_info_file_size = 0;
426 uint64_t debug_line_file_size = 0;
427 uint64_t debug_loc_file_size = 0;
428 uint64_t debug_macinfo_file_size = 0;
429 uint64_t debug_pubnames_file_size = 0;
430 uint64_t debug_pubtypes_file_size = 0;
431 uint64_t debug_ranges_file_size = 0;
432 uint64_t debug_str_file_size = 0;
433
Greg Clayton6beaaa62011-01-17 03:46:26 +0000434 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435
436 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000437 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438
Greg Clayton4ceb9982010-07-21 22:54:26 +0000439 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 if (section != NULL)
441 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000442 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443
Greg Clayton4ceb9982010-07-21 22:54:26 +0000444 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000446 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 else
448 m_flags.Set (flagsGotDebugAbbrevData);
449
Greg Clayton4ceb9982010-07-21 22:54:26 +0000450 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000452 debug_aranges_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 else
454 m_flags.Set (flagsGotDebugArangesData);
455
Greg Clayton4ceb9982010-07-21 22:54:26 +0000456 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000458 debug_frame_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 else
460 m_flags.Set (flagsGotDebugFrameData);
461
Greg Clayton4ceb9982010-07-21 22:54:26 +0000462 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000464 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 else
466 m_flags.Set (flagsGotDebugLineData);
467
Greg Clayton4ceb9982010-07-21 22:54:26 +0000468 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000470 debug_loc_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 else
472 m_flags.Set (flagsGotDebugLocData);
473
Greg Clayton4ceb9982010-07-21 22:54:26 +0000474 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000476 debug_macinfo_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 else
478 m_flags.Set (flagsGotDebugMacInfoData);
479
Greg Clayton4ceb9982010-07-21 22:54:26 +0000480 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000482 debug_pubnames_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 else
484 m_flags.Set (flagsGotDebugPubNamesData);
485
Greg Clayton4ceb9982010-07-21 22:54:26 +0000486 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000488 debug_pubtypes_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 else
490 m_flags.Set (flagsGotDebugPubTypesData);
491
Greg Clayton4ceb9982010-07-21 22:54:26 +0000492 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000494 debug_ranges_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495 else
496 m_flags.Set (flagsGotDebugRangesData);
497
Greg Clayton4ceb9982010-07-21 22:54:26 +0000498 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000500 debug_str_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501 else
502 m_flags.Set (flagsGotDebugStrData);
503 }
504
505 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
506 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
507
508 if (debug_line_file_size > 0)
509 abilities |= LineTables;
510
511 if (debug_aranges_file_size > 0)
512 abilities |= AddressAcceleratorTable;
513
514 if (debug_pubnames_file_size > 0)
515 abilities |= FunctionAcceleratorTable;
516
517 if (debug_pubtypes_file_size > 0)
518 abilities |= TypeAcceleratorTable;
519
520 if (debug_macinfo_file_size > 0)
521 abilities |= MacroInformation;
522
523 if (debug_frame_file_size > 0)
524 abilities |= CallFrameInformation;
525 }
526 return abilities;
527}
528
529const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000530SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531{
532 if (m_flags.IsClear (got_flag))
533 {
534 m_flags.Set (got_flag);
535 const SectionList *section_list = m_obj_file->GetSectionList();
536 if (section_list)
537 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000538 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
539 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 {
541 // See if we memory mapped the DWARF segment?
542 if (m_dwarf_data.GetByteSize())
543 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000544 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 }
546 else
547 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000548 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 data.Clear();
550 }
551 }
552 }
553 }
554 return data;
555}
556
557const DataExtractor&
558SymbolFileDWARF::get_debug_abbrev_data()
559{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000560 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561}
562
563const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000564SymbolFileDWARF::get_debug_aranges_data()
565{
566 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
567}
568
569const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570SymbolFileDWARF::get_debug_frame_data()
571{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000572 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573}
574
575const DataExtractor&
576SymbolFileDWARF::get_debug_info_data()
577{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000578 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579}
580
581const DataExtractor&
582SymbolFileDWARF::get_debug_line_data()
583{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000584 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
587const DataExtractor&
588SymbolFileDWARF::get_debug_loc_data()
589{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000590 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591}
592
593const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594SymbolFileDWARF::get_debug_ranges_data()
595{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000596 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597}
598
599const DataExtractor&
600SymbolFileDWARF::get_debug_str_data()
601{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000602 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603}
604
Greg Claytonf9eec202011-09-01 23:16:13 +0000605const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000606SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000607{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000608 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000609}
610
611const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000612SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000613{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000614 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000615}
616
Greg Clayton7f995132011-10-04 22:41:51 +0000617const DataExtractor&
618SymbolFileDWARF::get_apple_namespaces_data()
619{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000620 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
621}
622
623const DataExtractor&
624SymbolFileDWARF::get_apple_objc_data()
625{
626 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000627}
628
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629
630DWARFDebugAbbrev*
631SymbolFileDWARF::DebugAbbrev()
632{
633 if (m_abbr.get() == NULL)
634 {
635 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
636 if (debug_abbrev_data.GetByteSize() > 0)
637 {
638 m_abbr.reset(new DWARFDebugAbbrev());
639 if (m_abbr.get())
640 m_abbr->Parse(debug_abbrev_data);
641 }
642 }
643 return m_abbr.get();
644}
645
646const DWARFDebugAbbrev*
647SymbolFileDWARF::DebugAbbrev() const
648{
649 return m_abbr.get();
650}
651
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652
653DWARFDebugInfo*
654SymbolFileDWARF::DebugInfo()
655{
656 if (m_info.get() == NULL)
657 {
658 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
659 if (get_debug_info_data().GetByteSize() > 0)
660 {
661 m_info.reset(new DWARFDebugInfo());
662 if (m_info.get())
663 {
664 m_info->SetDwarfData(this);
665 }
666 }
667 }
668 return m_info.get();
669}
670
671const DWARFDebugInfo*
672SymbolFileDWARF::DebugInfo() const
673{
674 return m_info.get();
675}
676
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677DWARFCompileUnit*
678SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
679{
680 DWARFDebugInfo* info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +0000681 if (info && UserIDMatches(cu_uid))
682 return info->GetCompileUnit((dw_offset_t)cu_uid).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 return NULL;
684}
685
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686
687DWARFDebugRanges*
688SymbolFileDWARF::DebugRanges()
689{
690 if (m_ranges.get() == NULL)
691 {
692 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
693 if (get_debug_ranges_data().GetByteSize() > 0)
694 {
695 m_ranges.reset(new DWARFDebugRanges());
696 if (m_ranges.get())
697 m_ranges->Extract(this);
698 }
699 }
700 return m_ranges.get();
701}
702
703const DWARFDebugRanges*
704SymbolFileDWARF::DebugRanges() const
705{
706 return m_ranges.get();
707}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708
Greg Clayton53eb1c22012-04-02 22:59:12 +0000709lldb::CompUnitSP
710SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000712 CompUnitSP cu_sp;
713 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000715 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
716 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000718 // We already parsed this compile unit, had out a shared pointer to it
719 cu_sp = comp_unit->shared_from_this();
720 }
721 else
722 {
723 ModuleSP module_sp (m_obj_file->GetModule());
724 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000726 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
727 if (cu_die)
728 {
729 const char * cu_die_name = cu_die->GetName(this, dwarf_cu);
730 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
731 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
732 if (cu_die_name)
733 {
734 std::string ramapped_file;
735 FileSpec cu_file_spec;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000736
Greg Clayton53eb1c22012-04-02 22:59:12 +0000737 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
738 {
739 // If we have a full path to the compile unit, we don't need to resolve
740 // the file. This can be expensive e.g. when the source files are NFS mounted.
741 if (module_sp->RemapSourceFile(cu_die_name, ramapped_file))
742 cu_file_spec.SetFile (ramapped_file.c_str(), false);
743 else
744 cu_file_spec.SetFile (cu_die_name, false);
745 }
746 else
747 {
748 std::string fullpath(cu_comp_dir);
749 if (*fullpath.rbegin() != '/')
750 fullpath += '/';
751 fullpath += cu_die_name;
752 if (module_sp->RemapSourceFile (fullpath.c_str(), ramapped_file))
753 cu_file_spec.SetFile (ramapped_file.c_str(), false);
754 else
755 cu_file_spec.SetFile (fullpath.c_str(), false);
756 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757
Greg Clayton53eb1c22012-04-02 22:59:12 +0000758 cu_sp.reset(new CompileUnit (module_sp,
759 dwarf_cu,
760 cu_file_spec,
761 MakeUserID(dwarf_cu->GetOffset()),
762 cu_language));
763 if (cu_sp)
764 {
765 dwarf_cu->SetUserData(cu_sp.get());
766
767 if (m_debug_map_symfile)
768 {
769 // Let the symbol file register the compile unit with
770 // the symbol vendor using its compile unit index
771 // when we are doing DWARF in .o files + debug map
772 m_debug_map_symfile->SetCompileUnit(this, cu_sp);
773 }
774 else
775 {
776 // Figure out the compile unit index if we weren't given one
777 if (cu_idx == UINT32_MAX)
778 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
779
780 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
781 }
782 }
783 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784 }
785 }
786 }
787 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000788 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789}
790
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791uint32_t
792SymbolFileDWARF::GetNumCompileUnits()
793{
794 DWARFDebugInfo* info = DebugInfo();
795 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 return 0;
798}
799
800CompUnitSP
801SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
802{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000803 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804 DWARFDebugInfo* info = DebugInfo();
805 if (info)
806 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000807 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
808 if (dwarf_cu)
809 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000811 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812}
813
814static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000815AddRangesToBlock (Block& block,
816 DWARFDebugRanges::RangeList& ranges,
817 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000819 const size_t num_ranges = ranges.GetSize();
820 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000822 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
823 const addr_t range_base = range.GetRangeBase();
824 assert (range_base >= block_base_addr);
825 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000827 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828}
829
830
831Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000832SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833{
834 DWARFDebugRanges::RangeList func_ranges;
835 const char *name = NULL;
836 const char *mangled = NULL;
837 int decl_file = 0;
838 int decl_line = 0;
839 int decl_column = 0;
840 int call_file = 0;
841 int call_line = 0;
842 int call_column = 0;
843 DWARFExpression frame_base;
844
Greg Claytonc93237c2010-10-01 20:48:32 +0000845 assert (die->Tag() == DW_TAG_subprogram);
846
847 if (die->Tag() != DW_TAG_subprogram)
848 return NULL;
849
Greg Clayton3c2e3ae2012-02-06 06:42:51 +0000850 if (die->GetDIENamesAndRanges (this,
851 dwarf_cu,
852 name,
853 mangled,
854 func_ranges,
855 decl_file,
856 decl_line,
857 decl_column,
858 call_file,
859 call_line,
860 call_column,
861 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862 {
863 // Union of all ranges in the function DIE (if the function is discontiguous)
864 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000865 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
866 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
868 {
869 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
870 if (func_range.GetBaseAddress().IsValid())
871 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
872 }
873
874 if (func_range.GetBaseAddress().IsValid())
875 {
876 Mangled func_name;
877 if (mangled)
878 func_name.SetValue(mangled, true);
879 else if (name)
880 func_name.SetValue(name, false);
881
882 FunctionSP func_sp;
883 std::auto_ptr<Declaration> decl_ap;
884 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000885 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
886 decl_line,
887 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000889 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000890 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891
892 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
893
894 func_range.GetBaseAddress().ResolveLinkedAddress();
895
Greg Clayton81c22f62011-10-19 18:09:39 +0000896 const user_id_t func_user_id = MakeUserID(die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897 func_sp.reset(new Function (sc.comp_unit,
Greg Clayton81c22f62011-10-19 18:09:39 +0000898 func_user_id, // UserID is the DIE offset
899 func_user_id,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900 func_name,
901 func_type,
902 func_range)); // first address range
903
904 if (func_sp.get() != NULL)
905 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000906 if (frame_base.IsValid())
907 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908 sc.comp_unit->AddFunction(func_sp);
909 return func_sp.get();
910 }
911 }
912 }
913 return NULL;
914}
915
916size_t
917SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
918{
919 assert (sc.comp_unit);
920 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000921 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000922 if (dwarf_cu)
923 {
924 DWARFDIECollection function_dies;
925 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
926 size_t func_idx;
927 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
928 {
929 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000930 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931 {
932 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
933 ++functions_added;
934 }
935 }
936 //FixupTypes();
937 }
938 return functions_added;
939}
940
941bool
942SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
943{
944 assert (sc.comp_unit);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000945 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
946 assert (dwarf_cu);
947 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948
949 if (cu_die)
950 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000951 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
952 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 +0000953
954 // All file indexes in DWARF are one based and a file of index zero is
955 // supposed to be the compile unit itself.
956 support_files.Append (*sc.comp_unit);
957
Greg Claytond804d282012-03-15 21:01:31 +0000958 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 +0000959 }
960 return false;
961}
962
963struct ParseDWARFLineTableCallbackInfo
964{
965 LineTable* line_table;
966 const SectionList *section_list;
967 lldb::addr_t prev_sect_file_base_addr;
968 lldb::addr_t curr_sect_file_base_addr;
969 bool is_oso_for_debug_map;
970 bool prev_in_final_executable;
971 DWARFDebugLine::Row prev_row;
972 SectionSP prev_section_sp;
973 SectionSP curr_section_sp;
974};
975
976//----------------------------------------------------------------------
977// ParseStatementTableCallback
978//----------------------------------------------------------------------
979static void
980ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
981{
982 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
983 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
984 {
985 // Just started parsing the line table
986 }
987 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
988 {
989 // Done parsing line table, nothing to do for the cleanup
990 }
991 else
992 {
993 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
994 // We have a new row, lets append it
995
996 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
997 {
998 info->prev_section_sp = info->curr_section_sp;
999 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
1000 // If this is an end sequence entry, then we subtract one from the
1001 // address to make sure we get an address that is not the end of
1002 // a section.
1003 if (state.end_sequence && state.address != 0)
1004 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
1005 else
1006 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
1007
1008 if (info->curr_section_sp.get())
1009 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
1010 else
1011 info->curr_sect_file_base_addr = 0;
1012 }
1013 if (info->curr_section_sp.get())
1014 {
1015 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
1016 // Check for the fancy section magic to determine if we
1017
1018 if (info->is_oso_for_debug_map)
1019 {
1020 // When this is a debug map object file that contains DWARF
1021 // (referenced from an N_OSO debug map nlist entry) we will have
1022 // a file address in the file range for our section from the
1023 // original .o file, and a load address in the executable that
1024 // contains the debug map.
1025 //
1026 // If the sections for the file range and load range are
1027 // different, we have a remapped section for the function and
1028 // this address is resolved. If they are the same, then the
1029 // function for this address didn't make it into the final
1030 // executable.
1031 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
1032
1033 // If we are doing DWARF with debug map, then we need to carefully
1034 // add each line table entry as there may be gaps as functions
1035 // get moved around or removed.
1036 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
1037 {
1038 if (info->prev_in_final_executable)
1039 {
1040 bool terminate_previous_entry = false;
1041 if (!curr_in_final_executable)
1042 {
1043 // Check for the case where the previous line entry
1044 // in a function made it into the final executable,
1045 // yet the current line entry falls in a function
1046 // that didn't. The line table used to be contiguous
1047 // through this address range but now it isn't. We
1048 // need to terminate the previous line entry so
1049 // that we can reconstruct the line range correctly
1050 // for it and to keep the line table correct.
1051 terminate_previous_entry = true;
1052 }
1053 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
1054 {
1055 // Check for cases where the line entries used to be
1056 // contiguous address ranges, but now they aren't.
1057 // This can happen when order files specify the
1058 // ordering of the functions.
1059 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
1060 Section *curr_sect = info->curr_section_sp.get();
1061 Section *prev_sect = info->prev_section_sp.get();
1062 assert (curr_sect->GetLinkedSection());
1063 assert (prev_sect->GetLinkedSection());
1064 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
1065 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
1066 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
1067 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
1068 if (object_file_addr_delta != linked_file_addr_delta)
1069 terminate_previous_entry = true;
1070 }
1071
1072 if (terminate_previous_entry)
1073 {
1074 line_table->InsertLineEntry (info->prev_section_sp,
1075 state.address - info->prev_sect_file_base_addr,
1076 info->prev_row.line,
1077 info->prev_row.column,
1078 info->prev_row.file,
1079 false, // is_stmt
1080 false, // basic_block
1081 false, // state.prologue_end
1082 false, // state.epilogue_begin
1083 true); // end_sequence);
1084 }
1085 }
1086 }
1087
1088 if (curr_in_final_executable)
1089 {
1090 line_table->InsertLineEntry (info->curr_section_sp,
1091 curr_line_section_offset,
1092 state.line,
1093 state.column,
1094 state.file,
1095 state.is_stmt,
1096 state.basic_block,
1097 state.prologue_end,
1098 state.epilogue_begin,
1099 state.end_sequence);
1100 info->prev_section_sp = info->curr_section_sp;
1101 }
1102 else
1103 {
1104 // If the current address didn't make it into the final
1105 // executable, the current section will be the __text
1106 // segment in the .o file, so we need to clear this so
1107 // we can catch the next function that did make it into
1108 // the final executable.
1109 info->prev_section_sp.reset();
1110 info->curr_section_sp.reset();
1111 }
1112
1113 info->prev_in_final_executable = curr_in_final_executable;
1114 }
1115 else
1116 {
1117 // We are not in an object file that contains DWARF for an
1118 // N_OSO, this is just a normal DWARF file. The DWARF spec
1119 // guarantees that the addresses will be in increasing order
1120 // so, since we store line tables in file address order, we
1121 // can always just append the line entry without needing to
1122 // search for the correct insertion point (we don't need to
1123 // use LineEntry::InsertLineEntry()).
1124 line_table->AppendLineEntry (info->curr_section_sp,
1125 curr_line_section_offset,
1126 state.line,
1127 state.column,
1128 state.file,
1129 state.is_stmt,
1130 state.basic_block,
1131 state.prologue_end,
1132 state.epilogue_begin,
1133 state.end_sequence);
1134 }
1135 }
1136
1137 info->prev_row = state;
1138 }
1139}
1140
1141bool
1142SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1143{
1144 assert (sc.comp_unit);
1145 if (sc.comp_unit->GetLineTable() != NULL)
1146 return true;
1147
1148 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
1149 if (dwarf_cu)
1150 {
1151 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001152 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001154 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1155 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001157 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1158 if (line_table_ap.get())
1159 {
1160 ParseDWARFLineTableCallbackInfo info = {
1161 line_table_ap.get(),
1162 m_obj_file->GetSectionList(),
1163 0,
1164 0,
1165 m_debug_map_symfile != NULL,
1166 false,
1167 DWARFDebugLine::Row(),
1168 SectionSP(),
1169 SectionSP()
1170 };
1171 uint32_t offset = cu_line_offset;
1172 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1173 sc.comp_unit->SetLineTable(line_table_ap.release());
1174 return true;
1175 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176 }
1177 }
1178 }
1179 return false;
1180}
1181
1182size_t
1183SymbolFileDWARF::ParseFunctionBlocks
1184(
1185 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001186 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001187 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 const DWARFDebugInfoEntry *die,
1189 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001190 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191)
1192{
1193 size_t blocks_added = 0;
1194 while (die != NULL)
1195 {
1196 dw_tag_t tag = die->Tag();
1197
1198 switch (tag)
1199 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001201 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202 case DW_TAG_lexical_block:
1203 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001204 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001205 if (tag == DW_TAG_subprogram)
1206 {
1207 // Skip any DW_TAG_subprogram DIEs that are inside
1208 // of a normal or inlined functions. These will be
1209 // parsed on their own as separate entities.
1210
1211 if (depth > 0)
1212 break;
1213
1214 block = parent_block;
1215 }
1216 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001217 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001218 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001219 parent_block->AddChild(block_sp);
1220 block = block_sp.get();
1221 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001222 DWARFDebugRanges::RangeList ranges;
1223 const char *name = NULL;
1224 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 int decl_file = 0;
1227 int decl_line = 0;
1228 int decl_column = 0;
1229 int call_file = 0;
1230 int call_line = 0;
1231 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001232 if (die->GetDIENamesAndRanges (this,
1233 dwarf_cu,
1234 name,
1235 mangled_name,
1236 ranges,
1237 decl_file, decl_line, decl_column,
1238 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 {
1240 if (tag == DW_TAG_subprogram)
1241 {
1242 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001243 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001245 else if (tag == DW_TAG_inlined_subroutine)
1246 {
1247 // We get called here for inlined subroutines in two ways.
1248 // The first time is when we are making the Function object
1249 // for this inlined concrete instance. Since we're creating a top level block at
1250 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1251 // adjust the containing address.
1252 // The second time is when we are parsing the blocks inside the function that contains
1253 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1254 // function the offset will be for that function.
1255 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1256 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001257 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001258 }
1259 }
1260
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001261 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262
1263 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1264 {
1265 std::auto_ptr<Declaration> decl_ap;
1266 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001267 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1268 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269
1270 std::auto_ptr<Declaration> call_ap;
1271 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001272 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1273 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001275 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001276 }
1277
1278 ++blocks_added;
1279
Greg Claytondd7feaf2011-08-12 17:54:33 +00001280 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001282 blocks_added += ParseFunctionBlocks (sc,
1283 block,
1284 dwarf_cu,
1285 die->GetFirstChild(),
1286 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001287 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001288 }
1289 }
1290 }
1291 break;
1292 default:
1293 break;
1294 }
1295
Greg Claytondd7feaf2011-08-12 17:54:33 +00001296 // Only parse siblings of the block if we are not at depth zero. A depth
1297 // of zero indicates we are currently parsing the top level
1298 // DW_TAG_subprogram DIE
1299
1300 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001301 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001302 else
1303 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001304 }
1305 return blocks_added;
1306}
1307
Greg Claytonf0705c82011-10-22 03:33:13 +00001308bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001309SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1310 const DWARFDebugInfoEntry *die,
1311 ClangASTContext::TemplateParameterInfos &template_param_infos)
1312{
1313 const dw_tag_t tag = die->Tag();
1314
1315 switch (tag)
1316 {
1317 case DW_TAG_template_type_parameter:
1318 case DW_TAG_template_value_parameter:
1319 {
1320 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1321
1322 DWARFDebugInfoEntry::Attributes attributes;
1323 const size_t num_attributes = die->GetAttributes (this,
1324 dwarf_cu,
1325 fixed_form_sizes,
1326 attributes);
1327 const char *name = NULL;
1328 Type *lldb_type = NULL;
1329 clang_type_t clang_type = NULL;
1330 uint64_t uval64 = 0;
1331 bool uval64_valid = false;
1332 if (num_attributes > 0)
1333 {
1334 DWARFFormValue form_value;
1335 for (size_t i=0; i<num_attributes; ++i)
1336 {
1337 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1338
1339 switch (attr)
1340 {
1341 case DW_AT_name:
1342 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1343 name = form_value.AsCString(&get_debug_str_data());
1344 break;
1345
1346 case DW_AT_type:
1347 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1348 {
1349 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1350 lldb_type = ResolveTypeUID(type_die_offset);
1351 if (lldb_type)
1352 clang_type = lldb_type->GetClangForwardType();
1353 }
1354 break;
1355
1356 case DW_AT_const_value:
1357 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1358 {
1359 uval64_valid = true;
1360 uval64 = form_value.Unsigned();
1361 }
1362 break;
1363 default:
1364 break;
1365 }
1366 }
1367
1368 if (name && lldb_type && clang_type)
1369 {
1370 bool is_signed = false;
1371 template_param_infos.names.push_back(name);
1372 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1373 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1374 {
1375 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1376 template_param_infos.args.push_back (clang::TemplateArgument (llvm::APSInt(apint), clang_qual_type));
1377 }
1378 else
1379 {
1380 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1381 }
1382 }
1383 else
1384 {
1385 return false;
1386 }
1387
1388 }
1389 }
1390 return true;
1391
1392 default:
1393 break;
1394 }
1395 return false;
1396}
1397
1398bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001399SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1400 const DWARFDebugInfoEntry *parent_die,
1401 ClangASTContext::TemplateParameterInfos &template_param_infos)
1402{
1403
1404 if (parent_die == NULL)
1405 return NULL;
1406
Greg Claytonf0705c82011-10-22 03:33:13 +00001407 Args template_parameter_names;
1408 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1409 die != NULL;
1410 die = die->GetSibling())
1411 {
1412 const dw_tag_t tag = die->Tag();
1413
1414 switch (tag)
1415 {
1416 case DW_TAG_template_type_parameter:
1417 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001418 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001419 break;
1420
1421 default:
1422 break;
1423 }
1424 }
1425 if (template_param_infos.args.empty())
1426 return false;
1427 return template_param_infos.args.size() == template_param_infos.names.size();
1428}
1429
1430clang::ClassTemplateDecl *
1431SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001432 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001433 const char *parent_name,
1434 int tag_decl_kind,
1435 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1436{
1437 if (template_param_infos.IsValid())
1438 {
1439 std::string template_basename(parent_name);
1440 template_basename.erase (template_basename.find('<'));
1441 ClangASTContext &ast = GetClangASTContext();
1442
1443 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001444 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001445 template_basename.c_str(),
1446 tag_decl_kind,
1447 template_param_infos);
1448 }
1449 return NULL;
1450}
1451
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452size_t
1453SymbolFileDWARF::ParseChildMembers
1454(
1455 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001456 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001458 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001459 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1461 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001462 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001463 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001464 bool &is_a_class,
1465 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466)
1467{
1468 if (parent_die == NULL)
1469 return 0;
1470
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 size_t count = 0;
1472 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001473 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001474 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001475
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1477 {
1478 dw_tag_t tag = die->Tag();
1479
1480 switch (tag)
1481 {
1482 case DW_TAG_member:
Bill Wendling9e5bf872012-04-03 08:45:33 +00001483 case DW_TAG_APPLE_Property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 {
1485 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001486 const size_t num_attributes = die->GetAttributes (this,
1487 dwarf_cu,
1488 fixed_form_sizes,
1489 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001490 if (num_attributes > 0)
1491 {
1492 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001493 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001494 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001495 const char *prop_name = NULL;
1496 const char *prop_getter_name = NULL;
1497 const char *prop_setter_name = NULL;
1498 uint32_t prop_attributes = 0;
1499
1500
Greg Clayton24739922010-10-13 03:15:28 +00001501 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001503 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001504 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001505 size_t byte_size = 0;
1506 size_t bit_offset = 0;
1507 size_t bit_size = 0;
1508 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001509 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510 {
1511 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1512 DWARFFormValue form_value;
1513 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1514 {
1515 switch (attr)
1516 {
1517 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1518 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1519 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1520 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1521 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1522 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1523 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1524 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1525 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001526 if (form_value.BlockData())
1527 {
1528 Value initialValue(0);
1529 Value memberOffset(0);
1530 const DataExtractor& debug_info_data = get_debug_info_data();
1531 uint32_t block_length = form_value.Unsigned();
1532 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1533 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1534 NULL, // clang::ASTContext *
1535 NULL, // ClangExpressionVariableList *
1536 NULL, // ClangExpressionDeclMap *
1537 NULL, // RegisterContext *
1538 debug_info_data,
1539 block_offset,
1540 block_length,
1541 eRegisterKindDWARF,
1542 &initialValue,
1543 memberOffset,
1544 NULL))
1545 {
1546 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1547 }
1548 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001549 break;
1550
Greg Clayton8cf05932010-07-22 18:30:50 +00001551 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1b02c172012-03-14 21:00:47 +00001552 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001553 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1554 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1555 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1556 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1557
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001558 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001559 case DW_AT_declaration:
1560 case DW_AT_description:
1561 case DW_AT_mutable:
1562 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001563 case DW_AT_sibling:
1564 break;
1565 }
1566 }
1567 }
Sean Callanana6582262012-04-05 00:12:52 +00001568
1569 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00001570 {
Sean Callanana6582262012-04-05 00:12:52 +00001571 ConstString fixed_getter;
1572 ConstString fixed_setter;
1573
1574 // Check if the property getter/setter were provided as full
1575 // names. We want basenames, so we extract them.
1576
1577 if (prop_getter_name && prop_getter_name[0] == '-')
1578 {
1579 ObjCLanguageRuntime::ParseMethodName (prop_getter_name,
1580 NULL,
1581 &fixed_getter,
1582 NULL,
1583 NULL);
1584 prop_getter_name = fixed_getter.GetCString();
1585 }
1586
1587 if (prop_setter_name && prop_setter_name[0] == '-')
1588 {
1589 ObjCLanguageRuntime::ParseMethodName (prop_setter_name,
1590 NULL,
1591 &fixed_setter,
1592 NULL,
1593 NULL);
1594 prop_setter_name = fixed_setter.GetCString();
1595 }
1596
1597 // If the names haven't been provided, they need to be
1598 // filled in.
1599
1600 if (!prop_getter_name)
1601 {
1602 prop_getter_name = prop_name;
1603 }
1604 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
1605 {
1606 StreamString ss;
1607
1608 ss.Printf("set%c%s:",
1609 toupper(prop_name[0]),
1610 &prop_name[1]);
1611
1612 fixed_setter.SetCString(ss.GetData());
1613 prop_setter_name = fixed_setter.GetCString();
1614 }
Sean Callanan751aac62012-03-29 19:07:06 +00001615 }
1616
1617 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001618 // represents fields that are references with bad byte size
1619 // and bit size/offset information such as:
1620 //
1621 // DW_AT_byte_size( 0x00 )
1622 // DW_AT_bit_size( 0x40 )
1623 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1624 //
1625 // So check the bit offset to make sure it is sane, and if
1626 // the values are not sane, remove them. If we don't do this
1627 // then we will end up with a crash if we try to use this
1628 // type in an expression when clang becomes unhappy with its
1629 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001630
Greg Clayton44953932011-10-25 01:25:35 +00001631 if (bit_offset > 128)
1632 {
1633 bit_size = 0;
1634 bit_offset = 0;
1635 }
1636
1637 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001638 if (class_language == eLanguageTypeObjC ||
1639 class_language == eLanguageTypeObjC_plus_plus)
1640 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001641
1642 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1643 {
1644 // Not all compilers will mark the vtable pointer
1645 // member as artificial (llvm-gcc). We can't have
1646 // the virtual members in our classes otherwise it
1647 // throws off all child offsets since we end up
1648 // having and extra pointer sized member in our
1649 // class layouts.
1650 is_artificial = true;
1651 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001652
Greg Clayton24739922010-10-13 03:15:28 +00001653 if (is_artificial == false)
1654 {
1655 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001656 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001657 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001658 {
Sean Callanan751aac62012-03-29 19:07:06 +00001659 if (member_type)
1660 {
1661 if (accessibility == eAccessNone)
1662 accessibility = default_accessibility;
1663 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664
Sean Callanan751aac62012-03-29 19:07:06 +00001665 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
1666 name,
1667 member_type->GetClangLayoutType(),
1668 accessibility,
1669 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00001670
1671 GetClangASTContext().SetMetadata((uintptr_t)field_decl, MakeUserID(die->GetOffset()));
Sean Callanan5b26f272012-02-04 08:49:35 +00001672 }
1673 else
1674 {
Sean Callanan751aac62012-03-29 19:07:06 +00001675 if (name)
1676 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1677 MakeUserID(die->GetOffset()),
1678 name,
1679 encoding_uid);
1680 else
1681 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1682 MakeUserID(die->GetOffset()),
1683 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00001684 }
Sean Callanan751aac62012-03-29 19:07:06 +00001685
1686 if (member_byte_offset != UINT32_MAX || bit_size != 0)
1687 {
1688 /////////////////////////////////////////////////////////////
1689 // How to locate a field given the DWARF debug information
1690 //
1691 // AT_byte_size indicates the size of the word in which the
1692 // bit offset must be interpreted.
1693 //
1694 // AT_data_member_location indicates the byte offset of the
1695 // word from the base address of the structure.
1696 //
1697 // AT_bit_offset indicates how many bits into the word
1698 // (according to the host endianness) the low-order bit of
1699 // the field starts. AT_bit_offset can be negative.
1700 //
1701 // AT_bit_size indicates the size of the field in bits.
1702 /////////////////////////////////////////////////////////////
Sean Callanan5b26f272012-02-04 08:49:35 +00001703
Sean Callanan751aac62012-03-29 19:07:06 +00001704 ByteOrder object_endian = GetObjectFile()->GetModule()->GetArchitecture().GetDefaultEndian();
1705
1706 uint64_t total_bit_offset = 0;
1707
1708 total_bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1709
1710 if (object_endian == eByteOrderLittle)
1711 {
1712 total_bit_offset += byte_size * 8;
1713 total_bit_offset -= (bit_offset + bit_size);
1714 }
1715 else
1716 {
1717 total_bit_offset += bit_offset;
1718 }
1719
1720 layout_info.field_offsets.insert(std::make_pair(field_decl, total_bit_offset));
1721 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001722 }
Sean Callanan751aac62012-03-29 19:07:06 +00001723
Jim Inghame3ae82a2011-11-12 01:36:43 +00001724 if (prop_name != NULL)
1725 {
Sean Callanan751aac62012-03-29 19:07:06 +00001726 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001727
Sean Callanan751aac62012-03-29 19:07:06 +00001728 if (field_decl)
1729 {
1730 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1731 assert (ivar_decl != NULL);
1732 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001733
Jim Inghame3ae82a2011-11-12 01:36:43 +00001734 GetClangASTContext().AddObjCClassProperty (class_clang_type,
1735 prop_name,
Sean Callanan751aac62012-03-29 19:07:06 +00001736 member_type->GetClangLayoutType(),
Jim Inghame3ae82a2011-11-12 01:36:43 +00001737 ivar_decl,
1738 prop_setter_name,
1739 prop_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00001740 prop_attributes,
1741 MakeUserID(die->GetOffset()));
Sean Callanan60217122012-04-13 00:10:03 +00001742
Sean Callananad880762012-04-18 01:06:17 +00001743 if (ivar_decl)
1744 GetClangASTContext().SetMetadata((uintptr_t)ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00001745 }
Greg Clayton24739922010-10-13 03:15:28 +00001746 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001747 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001748 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749 }
1750 break;
1751
1752 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001753 // Let the type parsing code handle this one for us.
1754 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001755 break;
1756
1757 case DW_TAG_inheritance:
1758 {
1759 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001760 if (default_accessibility == eAccessNone)
1761 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001762 // TODO: implement DW_TAG_inheritance type parsing
1763 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001764 const size_t num_attributes = die->GetAttributes (this,
1765 dwarf_cu,
1766 fixed_form_sizes,
1767 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768 if (num_attributes > 0)
1769 {
1770 Declaration decl;
1771 DWARFExpression location;
1772 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001773 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001774 bool is_virtual = false;
1775 bool is_base_of_class = true;
1776 off_t member_offset = 0;
1777 uint32_t i;
1778 for (i=0; i<num_attributes; ++i)
1779 {
1780 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1781 DWARFFormValue form_value;
1782 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1783 {
1784 switch (attr)
1785 {
1786 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1787 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1788 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1789 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1790 case DW_AT_data_member_location:
1791 if (form_value.BlockData())
1792 {
1793 Value initialValue(0);
1794 Value memberOffset(0);
1795 const DataExtractor& debug_info_data = get_debug_info_data();
1796 uint32_t block_length = form_value.Unsigned();
1797 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001798 if (DWARFExpression::Evaluate (NULL,
1799 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001800 NULL,
1801 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001802 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001803 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001804 block_offset,
1805 block_length,
1806 eRegisterKindDWARF,
1807 &initialValue,
1808 memberOffset,
1809 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001810 {
1811 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1812 }
1813 }
1814 break;
1815
1816 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001817 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001818 break;
1819
1820 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1821 default:
1822 case DW_AT_sibling:
1823 break;
1824 }
1825 }
1826 }
1827
Greg Clayton526e5af2010-11-13 03:52:47 +00001828 Type *base_class_type = ResolveTypeUID(encoding_uid);
1829 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001830
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001831 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1832 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001833 if (class_language == eLanguageTypeObjC)
1834 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001835 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001836 }
1837 else
1838 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001839 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001840 accessibility,
1841 is_virtual,
1842 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001843 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001844 }
1845 }
1846 break;
1847
1848 default:
1849 break;
1850 }
1851 }
1852 return count;
1853}
1854
1855
1856clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001857SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001858{
1859 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001860 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001861 {
1862 DWARFCompileUnitSP cu_sp;
1863 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1864 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001865 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001866 }
1867 return NULL;
1868}
1869
1870clang::DeclContext*
1871SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1872{
Greg Clayton81c22f62011-10-19 18:09:39 +00001873 if (UserIDMatches(type_uid))
1874 return GetClangDeclContextForDIEOffset (sc, type_uid);
1875 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001876}
1877
1878Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001879SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001880{
Greg Clayton81c22f62011-10-19 18:09:39 +00001881 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001882 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001883 DWARFDebugInfo* debug_info = DebugInfo();
1884 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001885 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001886 DWARFCompileUnitSP cu_sp;
1887 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001888 const bool assert_not_being_parsed = true;
1889 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001890 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001891 }
1892 return NULL;
1893}
1894
Greg Claytoncab36a32011-12-08 05:16:30 +00001895Type*
1896SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001897{
Greg Claytoncab36a32011-12-08 05:16:30 +00001898 if (die != NULL)
1899 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001900 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1901 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001902 GetObjectFile()->GetModule()->LogMessage (log.get(),
1903 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1904 die->GetOffset(),
1905 DW_TAG_value_to_name(die->Tag()),
1906 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001907
Greg Claytoncab36a32011-12-08 05:16:30 +00001908 // We might be coming in in the middle of a type tree (a class
1909 // withing a class, an enum within a class), so parse any needed
1910 // parent DIEs before we get to this one...
1911 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1912 switch (decl_ctx_die->Tag())
1913 {
1914 case DW_TAG_structure_type:
1915 case DW_TAG_union_type:
1916 case DW_TAG_class_type:
1917 {
1918 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001919 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001920 GetObjectFile()->GetModule()->LogMessage (log.get(),
1921 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1922 die->GetOffset(),
1923 DW_TAG_value_to_name(die->Tag()),
1924 die->GetName(this, cu),
1925 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00001926//
1927// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
1928// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
1929// {
1930// if (log)
1931// GetObjectFile()->GetModule()->LogMessage (log.get(),
1932// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
1933// die->GetOffset(),
1934// DW_TAG_value_to_name(die->Tag()),
1935// die->GetName(this, cu),
1936// decl_ctx_die->GetOffset());
1937// // Ask the type to complete itself if it already hasn't since if we
1938// // want a function (method or static) from a class, the class must
1939// // create itself and add it's own methods and class functions.
1940// if (parent_type)
1941// parent_type->GetClangFullType();
1942// }
Greg Claytoncab36a32011-12-08 05:16:30 +00001943 }
1944 break;
1945
1946 default:
1947 break;
1948 }
1949 return ResolveType (cu, die);
1950 }
1951 return NULL;
1952}
1953
Greg Clayton6beaaa62011-01-17 03:46:26 +00001954// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1955// SymbolFileDWARF objects to detect if this DWARF file is the one that
1956// can resolve a clang_type.
1957bool
1958SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1959{
1960 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1961 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1962 return die != NULL;
1963}
1964
1965
Greg Clayton1be10fc2010-09-29 01:12:09 +00001966lldb::clang_type_t
1967SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1968{
1969 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001970 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1971 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001972 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001973 {
1974 // We have already resolved this type...
1975 return clang_type;
1976 }
1977 // Once we start resolving this type, remove it from the forward declaration
1978 // map in case anyone child members or other types require this type to get resolved.
1979 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1980 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001981 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001982
Greg Clayton1be10fc2010-09-29 01:12:09 +00001983
Greg Clayton85ae2e12011-10-18 23:36:41 +00001984 // Disable external storage for this type so we don't get anymore
1985 // clang::ExternalASTSource queries for this type.
1986 ClangASTContext::SetHasExternalStorage (clang_type, false);
1987
Greg Clayton450e3f32010-10-12 02:24:53 +00001988 DWARFDebugInfo* debug_info = DebugInfo();
1989
Greg Clayton53eb1c22012-04-02 22:59:12 +00001990 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001991 Type *type = m_die_to_type.lookup (die);
1992
1993 const dw_tag_t tag = die->Tag();
1994
Greg Claytonbaf95da2012-03-30 23:50:54 +00001995 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00001996 if (log)
Greg Claytonbaf95da2012-03-30 23:50:54 +00001997 {
Greg Claytond61c0fc2012-04-23 22:55:20 +00001998 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log.get(),
1999 "0x%8.8llx: %s '%s' resolving forward declaration...",
2000 MakeUserID(die->GetOffset()),
2001 DW_TAG_value_to_name(tag),
2002 type->GetName().AsCString());
Greg Claytonbaf95da2012-03-30 23:50:54 +00002003
Greg Claytonbaf95da2012-03-30 23:50:54 +00002004 }
Greg Clayton1be10fc2010-09-29 01:12:09 +00002005 assert (clang_type);
2006 DWARFDebugInfoEntry::Attributes attributes;
2007
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002008 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002009
2010 switch (tag)
2011 {
2012 case DW_TAG_structure_type:
2013 case DW_TAG_union_type:
2014 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002015 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002016 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002017
Greg Clayton1be10fc2010-09-29 01:12:09 +00002018 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002019 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002020 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002021
Sean Callanan77a1fd32012-02-27 20:07:01 +00002022 LanguageType class_language = eLanguageTypeUnknown;
2023 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
2024 if (is_objc_class)
Greg Clayton219cf312012-03-30 00:51:13 +00002025 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002026 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002027 // For objective C we don't start the definition when
2028 // the class is created.
2029 ast.StartTagDeclarationDefinition (clang_type);
2030 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002031
2032 int tag_decl_kind = -1;
2033 AccessType default_accessibility = eAccessNone;
2034 if (tag == DW_TAG_structure_type)
2035 {
2036 tag_decl_kind = clang::TTK_Struct;
2037 default_accessibility = eAccessPublic;
2038 }
2039 else if (tag == DW_TAG_union_type)
2040 {
2041 tag_decl_kind = clang::TTK_Union;
2042 default_accessibility = eAccessPublic;
2043 }
2044 else if (tag == DW_TAG_class_type)
2045 {
2046 tag_decl_kind = clang::TTK_Class;
2047 default_accessibility = eAccessPrivate;
2048 }
2049
Greg Clayton53eb1c22012-04-02 22:59:12 +00002050 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002051 std::vector<clang::CXXBaseSpecifier *> base_classes;
2052 std::vector<int> member_accessibilities;
2053 bool is_a_class = false;
2054 // Parse members and base classes first
2055 DWARFDIECollection member_function_dies;
2056
2057 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002058 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002059 die,
2060 clang_type,
2061 class_language,
2062 base_classes,
2063 member_accessibilities,
2064 member_function_dies,
2065 default_accessibility,
2066 is_a_class,
2067 layout_info);
2068
2069 // Now parse any methods if there were any...
2070 size_t num_functions = member_function_dies.Size();
2071 if (num_functions > 0)
2072 {
2073 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002074 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002075 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002076 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002077 }
2078
2079 if (class_language == eLanguageTypeObjC)
2080 {
Greg Clayton84db9102012-03-26 23:03:23 +00002081 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(ast.getASTContext(), clang_type));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002082 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00002083 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002084
Sean Callanan77a1fd32012-02-27 20:07:01 +00002085 DIEArray method_die_offsets;
2086 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002087 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002088 if (m_apple_objc_ap.get())
2089 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
2090 }
2091 else
2092 {
2093 if (!m_indexed)
2094 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002095
Sean Callanan77a1fd32012-02-27 20:07:01 +00002096 ConstString class_name (class_str.c_str());
2097 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2098 }
2099
2100 if (!method_die_offsets.empty())
2101 {
2102 DWARFDebugInfo* debug_info = DebugInfo();
2103
2104 DWARFCompileUnit* method_cu = NULL;
2105 const size_t num_matches = method_die_offsets.size();
2106 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002107 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002108 const dw_offset_t die_offset = method_die_offsets[i];
2109 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2110
2111 if (method_die)
2112 ResolveType (method_cu, method_die);
2113 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002114 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002115 if (m_using_apple_tables)
2116 {
2117 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2118 die_offset, class_str.c_str());
2119 }
2120 }
2121 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002122 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002123 }
2124 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002125
2126 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2127 // need to tell the clang type it is actually a class.
2128 if (class_language != eLanguageTypeObjC)
2129 {
2130 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2131 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2132 }
2133
2134 // Since DW_TAG_structure_type gets used for both classes
2135 // and structures, we may need to set any DW_TAG_member
2136 // fields to have a "private" access if none was specified.
2137 // When we parsed the child members we tracked that actual
2138 // accessibility value for each DW_TAG_member in the
2139 // "member_accessibilities" array. If the value for the
2140 // member is zero, then it was set to the "default_accessibility"
2141 // which for structs was "public". Below we correct this
2142 // by setting any fields to "private" that weren't correctly
2143 // set.
2144 if (is_a_class && !member_accessibilities.empty())
2145 {
2146 // This is a class and all members that didn't have
2147 // their access specified are private.
2148 ast.SetDefaultAccessForRecordFields (clang_type,
2149 eAccessPrivate,
2150 &member_accessibilities.front(),
2151 member_accessibilities.size());
2152 }
2153
2154 if (!base_classes.empty())
2155 {
2156 ast.SetBaseClassesForClassType (clang_type,
2157 &base_classes.front(),
2158 base_classes.size());
2159
2160 // Clang will copy each CXXBaseSpecifier in "base_classes"
2161 // so we have to free them all.
2162 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2163 base_classes.size());
2164 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002165 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002166 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002167
2168 ast.BuildIndirectFields (clang_type);
2169
Sean Callanan77a1fd32012-02-27 20:07:01 +00002170 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002171
Greg Claytoncaab74e2012-01-28 00:48:57 +00002172 if (!layout_info.field_offsets.empty())
2173 {
2174 if (type)
2175 layout_info.bit_size = type->GetByteSize() * 8;
2176 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002177 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Greg Claytoncaab74e2012-01-28 00:48:57 +00002178 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
2179 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
2180 if (record_type)
2181 {
2182 const clang::RecordDecl *record_decl = record_type->getDecl();
2183
2184 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002185 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002186 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002187 "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 +00002188 clang_type,
2189 record_decl,
2190 layout_info.bit_size,
2191 layout_info.alignment,
2192 (uint32_t)layout_info.field_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002193
Greg Clayton821ac6d2012-01-28 02:22:27 +00002194 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
2195 for (pos = layout_info.field_offsets.begin(); pos != end; ++pos)
2196 {
2197 GetObjectFile()->GetModule()->LogMessage (log.get(),
2198 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field = { bit_offset=%u, name='%s' }",
2199 clang_type,
2200 (uint32_t)pos->second,
2201 pos->first->getNameAsString().c_str());
2202 }
2203 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002204 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2205 }
2206 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002207 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002208
Greg Claytonc93237c2010-10-01 20:48:32 +00002209 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002210
2211 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002212 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002213 if (die->HasChildren())
2214 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002215 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
2216 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002217 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002218 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002219 return clang_type;
2220
2221 default:
2222 assert(false && "not a forward clang type decl!");
2223 break;
2224 }
2225 return NULL;
2226}
2227
Greg Claytonc685f8e2010-09-15 04:15:46 +00002228Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002229SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002230{
2231 if (type_die != NULL)
2232 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002233 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002234
Greg Claytonc685f8e2010-09-15 04:15:46 +00002235 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002236 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002237
Greg Clayton24739922010-10-13 03:15:28 +00002238 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002239 {
2240 if (type != DIE_IS_BEING_PARSED)
2241 return type;
2242
2243 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002244 type_die->GetOffset(),
2245 DW_TAG_value_to_name(type_die->Tag()),
2246 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002247
2248 }
2249 else
2250 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002251 }
2252 return NULL;
2253}
2254
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002255CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002256SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002257{
2258 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002259 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260 {
2261 // The symbol vendor doesn't know about this compile unit, we
2262 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002263 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002264 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002265 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002266}
2267
2268bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002269SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002270{
2271 sc.Clear();
2272 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002273 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274
Greg Clayton81c22f62011-10-19 18:09:39 +00002275 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002276 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002277 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002278
2279 if (sc.function)
2280 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002281 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002282 return true;
2283 }
2284
2285 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002286}
2287
2288uint32_t
2289SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2290{
2291 Timer scoped_timer(__PRETTY_FUNCTION__,
2292 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002293 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002294 so_addr.GetOffset(),
2295 resolve_scope);
2296 uint32_t resolved = 0;
2297 if (resolve_scope & ( eSymbolContextCompUnit |
2298 eSymbolContextFunction |
2299 eSymbolContextBlock |
2300 eSymbolContextLineEntry))
2301 {
2302 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2303
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002304 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002305 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002306 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002307 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002308 if (cu_offset != DW_INVALID_OFFSET)
2309 {
2310 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002311 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2312 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002313 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002314 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002315 assert(sc.comp_unit != NULL);
2316 resolved |= eSymbolContextCompUnit;
2317
2318 if (resolve_scope & eSymbolContextLineEntry)
2319 {
2320 LineTable *line_table = sc.comp_unit->GetLineTable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002321 if (line_table != NULL)
2322 {
2323 if (so_addr.IsLinkedAddress())
2324 {
2325 Address linked_addr (so_addr);
2326 linked_addr.ResolveLinkedAddress();
2327 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2328 {
2329 resolved |= eSymbolContextLineEntry;
2330 }
2331 }
2332 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
2333 {
2334 resolved |= eSymbolContextLineEntry;
2335 }
2336 }
2337 }
2338
2339 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2340 {
2341 DWARFDebugInfoEntry *function_die = NULL;
2342 DWARFDebugInfoEntry *block_die = NULL;
2343 if (resolve_scope & eSymbolContextBlock)
2344 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002345 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002346 }
2347 else
2348 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002349 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002350 }
2351
2352 if (function_die != NULL)
2353 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002354 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002355 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002356 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002357 }
Greg Clayton12b834d2012-03-29 21:43:25 +00002358 else
2359 {
2360 // We might have had a compile unit that had discontiguous
2361 // address ranges where the gaps are symbols that don't have
2362 // any debug info. Discontiguous compile unit address ranges
2363 // should only happen when there aren't other functions from
2364 // other compile units in these gaps. This helps keep the size
2365 // of the aranges down.
2366 sc.comp_unit = NULL;
2367 resolved &= ~eSymbolContextCompUnit;
2368 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002369
2370 if (sc.function != NULL)
2371 {
2372 resolved |= eSymbolContextFunction;
2373
2374 if (resolve_scope & eSymbolContextBlock)
2375 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002376 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002377
2378 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002379 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002380 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002381 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002382 if (sc.block)
2383 resolved |= eSymbolContextBlock;
2384 }
2385 }
2386 }
2387 }
2388 }
2389 }
2390 }
2391 return resolved;
2392}
2393
2394
2395
2396uint32_t
2397SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2398{
2399 const uint32_t prev_size = sc_list.GetSize();
2400 if (resolve_scope & eSymbolContextCompUnit)
2401 {
2402 DWARFDebugInfo* debug_info = DebugInfo();
2403 if (debug_info)
2404 {
2405 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002406 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002407
Greg Clayton53eb1c22012-04-02 22:59:12 +00002408 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002409 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002410 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002411 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
2412 if (check_inlines || file_spec_matches_cu_file_spec)
2413 {
2414 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002415 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002416 assert(sc.comp_unit != NULL);
2417
2418 uint32_t file_idx = UINT32_MAX;
2419
2420 // If we are looking for inline functions only and we don't
2421 // find it in the support files, we are done.
2422 if (check_inlines)
2423 {
Jim Ingham87df91b2011-09-23 00:54:11 +00002424 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002425 if (file_idx == UINT32_MAX)
2426 continue;
2427 }
2428
2429 if (line != 0)
2430 {
2431 LineTable *line_table = sc.comp_unit->GetLineTable();
2432
2433 if (line_table != NULL && line != 0)
2434 {
2435 // We will have already looked up the file index if
2436 // we are searching for inline entries.
2437 if (!check_inlines)
Jim Ingham87df91b2011-09-23 00:54:11 +00002438 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002439
2440 if (file_idx != UINT32_MAX)
2441 {
2442 uint32_t found_line;
2443 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2444 found_line = sc.line_entry.line;
2445
Greg Clayton016a95e2010-09-14 02:20:48 +00002446 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002447 {
2448 sc.function = NULL;
2449 sc.block = NULL;
2450 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2451 {
2452 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2453 if (file_vm_addr != LLDB_INVALID_ADDRESS)
2454 {
2455 DWARFDebugInfoEntry *function_die = NULL;
2456 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002457 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002458
2459 if (function_die != NULL)
2460 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002461 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002462 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002463 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002464 }
2465
2466 if (sc.function != NULL)
2467 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002468 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002469
2470 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002471 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002472 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002473 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002474 }
2475 }
2476 }
2477
2478 sc_list.Append(sc);
2479 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2480 }
2481 }
2482 }
2483 else if (file_spec_matches_cu_file_spec && !check_inlines)
2484 {
2485 // only append the context if we aren't looking for inline call sites
2486 // by file and line and if the file spec matches that of the compile unit
2487 sc_list.Append(sc);
2488 }
2489 }
2490 else if (file_spec_matches_cu_file_spec && !check_inlines)
2491 {
2492 // only append the context if we aren't looking for inline call sites
2493 // by file and line and if the file spec matches that of the compile unit
2494 sc_list.Append(sc);
2495 }
2496
2497 if (!check_inlines)
2498 break;
2499 }
2500 }
2501 }
2502 }
2503 return sc_list.GetSize() - prev_size;
2504}
2505
2506void
2507SymbolFileDWARF::Index ()
2508{
2509 if (m_indexed)
2510 return;
2511 m_indexed = true;
2512 Timer scoped_timer (__PRETTY_FUNCTION__,
2513 "SymbolFileDWARF::Index (%s)",
2514 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2515
2516 DWARFDebugInfo* debug_info = DebugInfo();
2517 if (debug_info)
2518 {
2519 uint32_t cu_idx = 0;
2520 const uint32_t num_compile_units = GetNumCompileUnits();
2521 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2522 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002523 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002524
Greg Clayton53eb1c22012-04-02 22:59:12 +00002525 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002526
Greg Clayton53eb1c22012-04-02 22:59:12 +00002527 dwarf_cu->Index (cu_idx,
2528 m_function_basename_index,
2529 m_function_fullname_index,
2530 m_function_method_index,
2531 m_function_selector_index,
2532 m_objc_class_selectors_index,
2533 m_global_index,
2534 m_type_index,
2535 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002536
2537 // Keep memory down by clearing DIEs if this generate function
2538 // caused them to be parsed
2539 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002540 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002541 }
2542
Greg Claytond4a2b372011-09-12 23:21:58 +00002543 m_function_basename_index.Finalize();
2544 m_function_fullname_index.Finalize();
2545 m_function_method_index.Finalize();
2546 m_function_selector_index.Finalize();
2547 m_objc_class_selectors_index.Finalize();
2548 m_global_index.Finalize();
2549 m_type_index.Finalize();
2550 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002551
Greg Clayton24739922010-10-13 03:15:28 +00002552#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002553 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002554 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002555 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2556 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002557 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2558 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2559 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2560 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2561 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2562 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002563 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002564 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002565#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002566 }
2567}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002568
2569bool
2570SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2571{
2572 if (namespace_decl == NULL)
2573 {
2574 // Invalid namespace decl which means we aren't matching only things
2575 // in this symbol file, so return true to indicate it matches this
2576 // symbol file.
2577 return true;
2578 }
2579
2580 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2581
2582 if (namespace_ast == NULL)
2583 return true; // No AST in the "namespace_decl", return true since it
2584 // could then match any symbol file, including this one
2585
2586 if (namespace_ast == GetClangASTContext().getASTContext())
2587 return true; // The ASTs match, return true
2588
2589 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002590 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2591
2592 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002593 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002594
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002595 return false;
2596}
2597
Greg Clayton2506a7a2011-10-12 23:34:26 +00002598bool
2599SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2600 DWARFCompileUnit* cu,
2601 const DWARFDebugInfoEntry* die)
2602{
2603 // No namespace specified, so the answesr i
2604 if (namespace_decl == NULL)
2605 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002606
2607 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002608
Greg Clayton437a1352012-04-09 22:43:43 +00002609 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
2610 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002611 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00002612 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00002613 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00002614
Greg Clayton2506a7a2011-10-12 23:34:26 +00002615 if (clang_namespace_decl)
2616 {
2617 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002618 {
2619 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002620 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002621 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002622 }
2623
Greg Clayton437a1352012-04-09 22:43:43 +00002624 if (clang_namespace_decl == die_clang_decl_ctx)
2625 return true;
2626 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00002627 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00002628 }
2629 else
2630 {
2631 // We have a namespace_decl that was not NULL but it contained
2632 // a NULL "clang::NamespaceDecl", so this means the global namespace
2633 // So as long the the contained decl context DIE isn't a namespace
2634 // we should be ok.
2635 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2636 return true;
2637 }
2638 }
Sean Callananebe60672011-10-13 21:50:33 +00002639
2640 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002641 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002642
Greg Clayton2506a7a2011-10-12 23:34:26 +00002643 return false;
2644}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002645uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002646SymbolFileDWARF::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 +00002647{
Greg Clayton21f2a492011-10-06 00:09:08 +00002648 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2649
2650 if (log)
2651 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002652 GetObjectFile()->GetModule()->LogMessage (log.get(),
2653 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2654 name.GetCString(),
2655 namespace_decl,
2656 append,
2657 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002658 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002659
2660 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2661 return 0;
2662
Greg Claytonc685f8e2010-09-15 04:15:46 +00002663 DWARFDebugInfo* info = DebugInfo();
2664 if (info == NULL)
2665 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002666
2667 // If we aren't appending the results to this list, then clear the list
2668 if (!append)
2669 variables.Clear();
2670
2671 // Remember how many variables are in the list before we search in case
2672 // we are appending the results to a variable list.
2673 const uint32_t original_size = variables.GetSize();
2674
Greg Claytond4a2b372011-09-12 23:21:58 +00002675 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002676
Greg Clayton97fbc342011-10-20 22:30:33 +00002677 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002678 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002679 if (m_apple_names_ap.get())
2680 {
2681 const char *name_cstr = name.GetCString();
2682 const char *base_name_start;
2683 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002684
Greg Clayton97fbc342011-10-20 22:30:33 +00002685 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2686 base_name_start = name_cstr;
2687
2688 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2689 }
Greg Clayton7f995132011-10-04 22:41:51 +00002690 }
2691 else
2692 {
2693 // Index the DWARF if we haven't already
2694 if (!m_indexed)
2695 Index ();
2696
2697 m_global_index.Find (name, die_offsets);
2698 }
2699
Greg Clayton437a1352012-04-09 22:43:43 +00002700 const size_t num_die_matches = die_offsets.size();
2701 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002702 {
Greg Clayton7f995132011-10-04 22:41:51 +00002703 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002704 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002705 assert (sc.module_sp);
2706
Greg Claytond4a2b372011-09-12 23:21:58 +00002707 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002708 DWARFCompileUnit* dwarf_cu = NULL;
2709 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00002710 bool done = false;
2711 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002712 {
2713 const dw_offset_t die_offset = die_offsets[i];
2714 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002715
Greg Clayton95d87902011-11-11 03:16:25 +00002716 if (die)
2717 {
Greg Clayton437a1352012-04-09 22:43:43 +00002718 switch (die->Tag())
2719 {
2720 default:
2721 case DW_TAG_subprogram:
2722 case DW_TAG_inlined_subroutine:
2723 case DW_TAG_try_block:
2724 case DW_TAG_catch_block:
2725 break;
2726
2727 case DW_TAG_variable:
2728 {
2729 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2730 assert(sc.comp_unit != NULL);
2731
2732 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2733 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002734
Greg Clayton437a1352012-04-09 22:43:43 +00002735 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002736
Greg Clayton437a1352012-04-09 22:43:43 +00002737 if (variables.GetSize() - original_size >= max_matches)
2738 done = true;
2739 }
2740 break;
2741 }
Greg Clayton95d87902011-11-11 03:16:25 +00002742 }
2743 else
2744 {
2745 if (m_using_apple_tables)
2746 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002747 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2748 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002749 }
2750 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002751 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002752 }
2753
2754 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002755 const uint32_t num_matches = variables.GetSize() - original_size;
2756 if (log && num_matches > 0)
2757 {
2758 GetObjectFile()->GetModule()->LogMessage (log.get(),
2759 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
2760 name.GetCString(),
2761 namespace_decl,
2762 append,
2763 max_matches,
2764 num_matches);
2765 }
2766 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002767}
2768
2769uint32_t
2770SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2771{
Greg Clayton21f2a492011-10-06 00:09:08 +00002772 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2773
2774 if (log)
2775 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002776 GetObjectFile()->GetModule()->LogMessage (log.get(),
2777 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2778 regex.GetText(),
2779 append,
2780 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002781 }
2782
Greg Claytonc685f8e2010-09-15 04:15:46 +00002783 DWARFDebugInfo* info = DebugInfo();
2784 if (info == NULL)
2785 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002786
2787 // If we aren't appending the results to this list, then clear the list
2788 if (!append)
2789 variables.Clear();
2790
2791 // Remember how many variables are in the list before we search in case
2792 // we are appending the results to a variable list.
2793 const uint32_t original_size = variables.GetSize();
2794
Greg Clayton7f995132011-10-04 22:41:51 +00002795 DIEArray die_offsets;
2796
Greg Clayton97fbc342011-10-20 22:30:33 +00002797 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002798 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002799 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002800 {
2801 DWARFMappedHash::DIEInfoArray hash_data_array;
2802 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2803 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2804 }
Greg Clayton7f995132011-10-04 22:41:51 +00002805 }
2806 else
2807 {
2808 // Index the DWARF if we haven't already
2809 if (!m_indexed)
2810 Index ();
2811
2812 m_global_index.Find (regex, die_offsets);
2813 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002814
Greg Claytonc685f8e2010-09-15 04:15:46 +00002815 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002816 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002817 assert (sc.module_sp);
2818
Greg Claytond4a2b372011-09-12 23:21:58 +00002819 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002820 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002821 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002822 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002823 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002824 DWARFDebugInfo* debug_info = DebugInfo();
2825 for (size_t i=0; i<num_matches; ++i)
2826 {
2827 const dw_offset_t die_offset = die_offsets[i];
2828 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002829
2830 if (die)
2831 {
2832 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002833
Greg Clayton95d87902011-11-11 03:16:25 +00002834 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002835
Greg Clayton95d87902011-11-11 03:16:25 +00002836 if (variables.GetSize() - original_size >= max_matches)
2837 break;
2838 }
2839 else
2840 {
2841 if (m_using_apple_tables)
2842 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002843 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2844 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002845 }
2846 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002847 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002848 }
2849
2850 // Return the number of variable that were appended to the list
2851 return variables.GetSize() - original_size;
2852}
2853
Greg Claytonaa044962011-10-13 00:59:38 +00002854
Jim Ingham4cda6e02011-10-07 22:23:45 +00002855bool
2856SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2857 DWARFCompileUnit *&dwarf_cu,
2858 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002859{
Greg Claytonaa044962011-10-13 00:59:38 +00002860 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2861 return ResolveFunction (dwarf_cu, die, sc_list);
2862}
2863
2864
2865bool
2866SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2867 const DWARFDebugInfoEntry *die,
2868 SymbolContextList& sc_list)
2869{
Greg Clayton9e315582011-09-02 04:03:59 +00002870 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002871
2872 if (die == NULL)
2873 return false;
2874
Jim Ingham4cda6e02011-10-07 22:23:45 +00002875 // If we were passed a die that is not a function, just return false...
2876 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2877 return false;
2878
2879 const DWARFDebugInfoEntry* inlined_die = NULL;
2880 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002881 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002882 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002883
Jim Ingham4cda6e02011-10-07 22:23:45 +00002884 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002885 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002886 if (die->Tag() == DW_TAG_subprogram)
2887 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002888 }
2889 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002890 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002891 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002892 {
2893 Address addr;
2894 // Parse all blocks if needed
2895 if (inlined_die)
2896 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002897 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00002898 assert (sc.block != NULL);
2899 if (sc.block->GetStartAddress (addr) == false)
2900 addr.Clear();
2901 }
2902 else
2903 {
2904 sc.block = NULL;
2905 addr = sc.function->GetAddressRange().GetBaseAddress();
2906 }
2907
2908 if (addr.IsValid())
2909 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002910 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002911 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002912 }
2913 }
2914
Greg Claytonaa044962011-10-13 00:59:38 +00002915 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002916}
2917
Greg Clayton7f995132011-10-04 22:41:51 +00002918void
2919SymbolFileDWARF::FindFunctions (const ConstString &name,
2920 const NameToDIE &name_to_die,
2921 SymbolContextList& sc_list)
2922{
Greg Claytond4a2b372011-09-12 23:21:58 +00002923 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002924 if (name_to_die.Find (name, die_offsets))
2925 {
2926 ParseFunctions (die_offsets, sc_list);
2927 }
2928}
2929
2930
2931void
2932SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2933 const NameToDIE &name_to_die,
2934 SymbolContextList& sc_list)
2935{
2936 DIEArray die_offsets;
2937 if (name_to_die.Find (regex, die_offsets))
2938 {
2939 ParseFunctions (die_offsets, sc_list);
2940 }
2941}
2942
2943
2944void
2945SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2946 const DWARFMappedHash::MemoryTable &memory_table,
2947 SymbolContextList& sc_list)
2948{
2949 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002950 DWARFMappedHash::DIEInfoArray hash_data_array;
2951 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002952 {
Greg Claytond1767f02011-12-08 02:13:16 +00002953 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002954 ParseFunctions (die_offsets, sc_list);
2955 }
2956}
2957
2958void
2959SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2960 SymbolContextList& sc_list)
2961{
2962 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002963 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002964 {
Greg Clayton7f995132011-10-04 22:41:51 +00002965 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00002966
2967 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002968 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002969 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002970 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00002971 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002972 }
2973 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002974}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002975
Jim Ingham4cda6e02011-10-07 22:23:45 +00002976bool
2977SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2978 const DWARFCompileUnit *dwarf_cu,
2979 uint32_t name_type_mask,
2980 const char *partial_name,
2981 const char *base_name_start,
2982 const char *base_name_end)
2983{
2984 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2985 if (name_type_mask == eFunctionNameTypeMethod
2986 || name_type_mask == eFunctionNameTypeBase)
2987 {
Greg Claytonf0705c82011-10-22 03:33:13 +00002988 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2989 if (!containing_decl_ctx)
2990 return false;
2991
2992 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
2993
2994 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2995 return false;
2996 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2997 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002998 }
2999
3000 // Now we need to check whether the name we got back for this type matches the extra specifications
3001 // that were in the name we're looking up:
3002 if (base_name_start != partial_name || *base_name_end != '\0')
3003 {
3004 // First see if the stuff to the left matches the full name. To do that let's see if
3005 // we can pull out the mips linkage name attribute:
3006
3007 Mangled best_name;
3008
3009 DWARFDebugInfoEntry::Attributes attributes;
3010 die->GetAttributes(this, dwarf_cu, NULL, attributes);
3011 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
3012 if (idx != UINT32_MAX)
3013 {
3014 DWARFFormValue form_value;
3015 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3016 {
3017 const char *name = form_value.AsCString(&get_debug_str_data());
3018 best_name.SetValue (name, true);
3019 }
3020 }
3021 if (best_name)
3022 {
3023 const char *demangled = best_name.GetDemangledName().GetCString();
3024 if (demangled)
3025 {
3026 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003027 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3028 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003029 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003030 else
3031 {
3032 // Sort out the case where our name is something like "Process::Destroy" and the match is
3033 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3034 // namespace boundaries...
3035
3036 if (partial_name[0] == ':' && partial_name[1] == ':')
3037 {
3038 // The partial name was already on a namespace boundary so all matches are good.
3039 return true;
3040 }
3041 else if (partial_in_demangled == demangled)
3042 {
3043 // They both start the same, so this is an good match.
3044 return true;
3045 }
3046 else
3047 {
3048 if (partial_in_demangled - demangled == 1)
3049 {
3050 // Only one character difference, can't be a namespace boundary...
3051 return false;
3052 }
3053 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3054 {
3055 // We are on a namespace boundary, so this is also good.
3056 return true;
3057 }
3058 else
3059 return false;
3060 }
3061 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003062 }
3063 }
3064 }
3065
3066 return true;
3067}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003068
Greg Clayton0c5cd902010-06-28 21:30:43 +00003069uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003070SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003071 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003072 uint32_t name_type_mask,
3073 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003074 bool append,
3075 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003076{
3077 Timer scoped_timer (__PRETTY_FUNCTION__,
3078 "SymbolFileDWARF::FindFunctions (name = '%s')",
3079 name.AsCString());
3080
Greg Clayton21f2a492011-10-06 00:09:08 +00003081 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3082
3083 if (log)
3084 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003085 GetObjectFile()->GetModule()->LogMessage (log.get(),
3086 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3087 name.GetCString(),
3088 name_type_mask,
3089 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003090 }
3091
Greg Clayton0c5cd902010-06-28 21:30:43 +00003092 // If we aren't appending the results to this list, then clear the list
3093 if (!append)
3094 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003095
3096 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3097 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003098
3099 // If name is empty then we won't find anything.
3100 if (name.IsEmpty())
3101 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003102
3103 // Remember how many sc_list are in the list before we search in case
3104 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003105
Greg Clayton9e315582011-09-02 04:03:59 +00003106 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00003107
Jim Ingham4cda6e02011-10-07 22:23:45 +00003108 const char *name_cstr = name.GetCString();
3109 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
3110 const char *base_name_start = name_cstr;
3111 const char *base_name_end = name_cstr + strlen(name_cstr);
3112
3113 if (name_type_mask & eFunctionNameTypeAuto)
3114 {
3115 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
3116 effective_name_type_mask = eFunctionNameTypeFull;
3117 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
3118 effective_name_type_mask = eFunctionNameTypeFull;
3119 else
3120 {
3121 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3122 effective_name_type_mask |= eFunctionNameTypeSelector;
3123
3124 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3125 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
3126 }
3127 }
3128 else
3129 {
3130 effective_name_type_mask = name_type_mask;
3131 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
3132 {
3133 // If they've asked for a CPP method or function name and it can't be that, we don't
3134 // even need to search for CPP methods or names.
3135 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3136 {
3137 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
3138 if (effective_name_type_mask == eFunctionNameTypeNone)
3139 return 0;
3140 }
3141 }
3142
3143 if (effective_name_type_mask & eFunctionNameTypeSelector)
3144 {
3145 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3146 {
3147 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
3148 if (effective_name_type_mask == eFunctionNameTypeNone)
3149 return 0;
3150 }
3151 }
3152 }
3153
3154 DWARFDebugInfo* info = DebugInfo();
3155 if (info == NULL)
3156 return 0;
3157
Greg Claytonaa044962011-10-13 00:59:38 +00003158 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003159 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003160 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003161 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003162 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003163
3164 DIEArray die_offsets;
3165
3166 uint32_t num_matches = 0;
3167
3168 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003169 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003170 // If they asked for the full name, match what they typed. At some point we may
3171 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3172 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003173 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003174 for (uint32_t i = 0; i < num_matches; i++)
3175 {
Greg Clayton95d87902011-11-11 03:16:25 +00003176 const dw_offset_t die_offset = die_offsets[i];
3177 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003178 if (die)
3179 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003180 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3181 continue;
3182
Sean Callanan9df05fb2012-02-10 22:52:19 +00003183 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3184 continue;
3185
Greg Claytonaa044962011-10-13 00:59:38 +00003186 ResolveFunction (dwarf_cu, die, sc_list);
3187 }
Greg Clayton95d87902011-11-11 03:16:25 +00003188 else
3189 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003190 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3191 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003192 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003193 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003194 }
3195 else
3196 {
3197 if (effective_name_type_mask & eFunctionNameTypeSelector)
3198 {
3199 if (namespace_decl && *namespace_decl)
3200 return 0; // no selectors in namespaces
3201
3202 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3203 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3204 // and if it is an ObjC method name, we're good.
3205
3206 for (uint32_t i = 0; i < num_matches; i++)
3207 {
Greg Clayton95d87902011-11-11 03:16:25 +00003208 const dw_offset_t die_offset = die_offsets[i];
3209 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003210 if (die)
3211 {
3212 const char *die_name = die->GetName(this, dwarf_cu);
3213 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003214 {
3215 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3216 continue;
3217
Greg Clayton97fbc342011-10-20 22:30:33 +00003218 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003219 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003220 }
Greg Clayton95d87902011-11-11 03:16:25 +00003221 else
3222 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003223 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3224 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003225 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003226 }
3227 die_offsets.clear();
3228 }
3229
3230 if (effective_name_type_mask & eFunctionNameTypeMethod
3231 || effective_name_type_mask & eFunctionNameTypeBase)
3232 {
3233 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3234 (namespace_decl && *namespace_decl))
3235 return 0; // no methods in namespaces
3236
3237 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3238 // extract the base name, look that up, and if there is any other information in the name we were
3239 // passed in we have to post-filter based on that.
3240
3241 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3242 std::string base_name(base_name_start, base_name_end - base_name_start);
3243 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3244
3245 for (uint32_t i = 0; i < num_matches; i++)
3246 {
Greg Clayton95d87902011-11-11 03:16:25 +00003247 const dw_offset_t die_offset = die_offsets[i];
3248 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003249 if (die)
3250 {
3251 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3252 continue;
3253
3254 if (!FunctionDieMatchesPartialName(die,
3255 dwarf_cu,
3256 effective_name_type_mask,
3257 name_cstr,
3258 base_name_start,
3259 base_name_end))
3260 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003261
3262 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3263 continue;
Greg Clayton97fbc342011-10-20 22:30:33 +00003264
3265 // If we get to here, the die is good, and we should add it:
3266 ResolveFunction (dwarf_cu, die, sc_list);
3267 }
Greg Clayton95d87902011-11-11 03:16:25 +00003268 else
3269 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003270 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3271 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003272 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003273 }
3274 die_offsets.clear();
3275 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003276 }
3277 }
Greg Clayton7f995132011-10-04 22:41:51 +00003278 }
3279 else
3280 {
3281
3282 // Index the DWARF if we haven't already
3283 if (!m_indexed)
3284 Index ();
3285
Greg Clayton7f995132011-10-04 22:41:51 +00003286 if (name_type_mask & eFunctionNameTypeFull)
3287 FindFunctions (name, m_function_fullname_index, sc_list);
3288
Jim Ingham4cda6e02011-10-07 22:23:45 +00003289 std::string base_name(base_name_start, base_name_end - base_name_start);
3290 ConstString base_name_const(base_name.c_str());
3291 DIEArray die_offsets;
3292 DWARFCompileUnit *dwarf_cu = NULL;
3293
3294 if (effective_name_type_mask & eFunctionNameTypeBase)
3295 {
3296 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003297 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003298 {
Greg Claytonaa044962011-10-13 00:59:38 +00003299 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3300 if (die)
3301 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003302 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3303 continue;
3304
Greg Claytonaa044962011-10-13 00:59:38 +00003305 if (!FunctionDieMatchesPartialName(die,
3306 dwarf_cu,
3307 effective_name_type_mask,
3308 name_cstr,
3309 base_name_start,
3310 base_name_end))
3311 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003312
Sean Callanan9df05fb2012-02-10 22:52:19 +00003313 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3314 continue;
3315
Greg Claytonaa044962011-10-13 00:59:38 +00003316 // If we get to here, the die is good, and we should add it:
3317 ResolveFunction (dwarf_cu, die, sc_list);
3318 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003319 }
3320 die_offsets.clear();
3321 }
3322
Jim Inghamea8005a2011-10-11 01:18:11 +00003323 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003324 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003325 if (namespace_decl && *namespace_decl)
3326 return 0; // no methods in namespaces
3327
Jim Ingham4cda6e02011-10-07 22:23:45 +00003328 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3329 {
Greg Claytonaa044962011-10-13 00:59:38 +00003330 for (uint32_t i = 0; i < num_base; i++)
3331 {
3332 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3333 if (die)
3334 {
3335 if (!FunctionDieMatchesPartialName(die,
3336 dwarf_cu,
3337 effective_name_type_mask,
3338 name_cstr,
3339 base_name_start,
3340 base_name_end))
3341 continue;
3342
Sean Callanan9df05fb2012-02-10 22:52:19 +00003343 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3344 continue;
3345
Greg Claytonaa044962011-10-13 00:59:38 +00003346 // If we get to here, the die is good, and we should add it:
3347 ResolveFunction (dwarf_cu, die, sc_list);
3348 }
3349 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003350 }
3351 die_offsets.clear();
3352 }
Greg Clayton7f995132011-10-04 22:41:51 +00003353
Sean Callanan213fdb82011-10-13 01:49:10 +00003354 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003355 {
Greg Clayton7f995132011-10-04 22:41:51 +00003356 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003357 }
3358
Greg Clayton4d01ace2011-09-29 16:58:15 +00003359 }
3360
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003361 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003362 const uint32_t num_matches = sc_list.GetSize() - original_size;
3363
3364 if (log && num_matches > 0)
3365 {
3366 GetObjectFile()->GetModule()->LogMessage (log.get(),
3367 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u",
3368 name.GetCString(),
3369 name_type_mask,
3370 append,
3371 num_matches);
3372 }
3373 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003374}
3375
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003376uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003377SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003378{
3379 Timer scoped_timer (__PRETTY_FUNCTION__,
3380 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3381 regex.GetText());
3382
Greg Clayton21f2a492011-10-06 00:09:08 +00003383 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3384
3385 if (log)
3386 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003387 GetObjectFile()->GetModule()->LogMessage (log.get(),
3388 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3389 regex.GetText(),
3390 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003391 }
3392
3393
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003394 // If we aren't appending the results to this list, then clear the list
3395 if (!append)
3396 sc_list.Clear();
3397
3398 // Remember how many sc_list are in the list before we search in case
3399 // we are appending the results to a variable list.
3400 uint32_t original_size = sc_list.GetSize();
3401
Greg Clayton97fbc342011-10-20 22:30:33 +00003402 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003403 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003404 if (m_apple_names_ap.get())
3405 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003406 }
3407 else
3408 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003409 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003410 if (!m_indexed)
3411 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003412
Greg Clayton7f995132011-10-04 22:41:51 +00003413 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003414
Greg Clayton7f995132011-10-04 22:41:51 +00003415 FindFunctions (regex, m_function_fullname_index, sc_list);
3416 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003417
3418 // Return the number of variable that were appended to the list
3419 return sc_list.GetSize() - original_size;
3420}
Jim Ingham318c9f22011-08-26 19:44:13 +00003421
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003422uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003423SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3424 const ConstString &name,
3425 const lldb_private::ClangNamespaceDecl *namespace_decl,
3426 bool append,
3427 uint32_t max_matches,
3428 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003429{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003430 DWARFDebugInfo* info = DebugInfo();
3431 if (info == NULL)
3432 return 0;
3433
Greg Clayton21f2a492011-10-06 00:09:08 +00003434 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3435
3436 if (log)
3437 {
Greg Clayton437a1352012-04-09 22:43:43 +00003438 if (namespace_decl)
3439 {
3440 GetObjectFile()->GetModule()->LogMessage (log.get(),
3441 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
3442 name.GetCString(),
3443 namespace_decl->GetNamespaceDecl(),
3444 namespace_decl->GetQualifiedName().c_str(),
3445 append,
3446 max_matches);
3447 }
3448 else
3449 {
3450 GetObjectFile()->GetModule()->LogMessage (log.get(),
3451 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
3452 name.GetCString(),
3453 append,
3454 max_matches);
3455 }
Greg Clayton21f2a492011-10-06 00:09:08 +00003456 }
3457
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003458 // If we aren't appending the results to this list, then clear the list
3459 if (!append)
3460 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003461
3462 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3463 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003464
Greg Claytond4a2b372011-09-12 23:21:58 +00003465 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003466
Greg Clayton97fbc342011-10-20 22:30:33 +00003467 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003468 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003469 if (m_apple_types_ap.get())
3470 {
3471 const char *name_cstr = name.GetCString();
3472 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3473 }
Greg Clayton7f995132011-10-04 22:41:51 +00003474 }
3475 else
3476 {
3477 if (!m_indexed)
3478 Index ();
3479
3480 m_type_index.Find (name, die_offsets);
3481 }
3482
Greg Clayton437a1352012-04-09 22:43:43 +00003483 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003484
Greg Clayton437a1352012-04-09 22:43:43 +00003485 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003486 {
Greg Clayton7f995132011-10-04 22:41:51 +00003487 const uint32_t initial_types_size = types.GetSize();
3488 DWARFCompileUnit* dwarf_cu = NULL;
3489 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003490 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003491 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003492 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003493 const dw_offset_t die_offset = die_offsets[i];
3494 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3495
Greg Clayton95d87902011-11-11 03:16:25 +00003496 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003497 {
Greg Clayton95d87902011-11-11 03:16:25 +00003498 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3499 continue;
3500
3501 Type *matching_type = ResolveType (dwarf_cu, die);
3502 if (matching_type)
3503 {
3504 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003505 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003506 if (types.GetSize() >= max_matches)
3507 break;
3508 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003509 }
Greg Clayton95d87902011-11-11 03:16:25 +00003510 else
3511 {
3512 if (m_using_apple_tables)
3513 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003514 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3515 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003516 }
3517 }
3518
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003519 }
Greg Clayton437a1352012-04-09 22:43:43 +00003520 const uint32_t num_matches = types.GetSize() - initial_types_size;
3521 if (log && num_matches)
3522 {
3523 if (namespace_decl)
3524 {
3525 GetObjectFile()->GetModule()->LogMessage (log.get(),
3526 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
3527 name.GetCString(),
3528 namespace_decl->GetNamespaceDecl(),
3529 namespace_decl->GetQualifiedName().c_str(),
3530 append,
3531 max_matches,
3532 num_matches);
3533 }
3534 else
3535 {
3536 GetObjectFile()->GetModule()->LogMessage (log.get(),
3537 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
3538 name.GetCString(),
3539 append,
3540 max_matches,
3541 num_matches);
3542 }
3543 }
3544 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003545 }
Greg Clayton7f995132011-10-04 22:41:51 +00003546 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003547}
3548
3549
Greg Clayton526e5af2010-11-13 03:52:47 +00003550ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003551SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003552 const ConstString &name,
3553 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003554{
Greg Clayton21f2a492011-10-06 00:09:08 +00003555 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3556
3557 if (log)
3558 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003559 GetObjectFile()->GetModule()->LogMessage (log.get(),
3560 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3561 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003562 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003563
3564 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3565 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003566
Greg Clayton526e5af2010-11-13 03:52:47 +00003567 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003568 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003569 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003570 {
Greg Clayton7f995132011-10-04 22:41:51 +00003571 DIEArray die_offsets;
3572
Greg Clayton526e5af2010-11-13 03:52:47 +00003573 // Index if we already haven't to make sure the compile units
3574 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003575 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003576 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003577 if (m_apple_namespaces_ap.get())
3578 {
3579 const char *name_cstr = name.GetCString();
3580 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3581 }
Greg Clayton7f995132011-10-04 22:41:51 +00003582 }
3583 else
3584 {
3585 if (!m_indexed)
3586 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003587
Greg Clayton7f995132011-10-04 22:41:51 +00003588 m_namespace_index.Find (name, die_offsets);
3589 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003590
3591 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003592 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003593 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003594 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003595 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003596 DWARFDebugInfo* debug_info = DebugInfo();
3597 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003598 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003599 const dw_offset_t die_offset = die_offsets[i];
3600 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003601
Greg Clayton95d87902011-11-11 03:16:25 +00003602 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003603 {
Greg Clayton95d87902011-11-11 03:16:25 +00003604 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3605 continue;
3606
3607 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3608 if (clang_namespace_decl)
3609 {
3610 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3611 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003612 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003613 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003614 }
Greg Clayton95d87902011-11-11 03:16:25 +00003615 else
3616 {
3617 if (m_using_apple_tables)
3618 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003619 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3620 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003621 }
3622 }
3623
Greg Clayton526e5af2010-11-13 03:52:47 +00003624 }
3625 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003626 }
Greg Clayton437a1352012-04-09 22:43:43 +00003627 if (log && namespace_decl.GetNamespaceDecl())
3628 {
3629 GetObjectFile()->GetModule()->LogMessage (log.get(),
3630 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
3631 name.GetCString(),
3632 namespace_decl.GetNamespaceDecl(),
3633 namespace_decl.GetQualifiedName().c_str());
3634 }
3635
Greg Clayton526e5af2010-11-13 03:52:47 +00003636 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003637}
3638
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003639uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003640SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003641{
3642 // Remember how many sc_list are in the list before we search in case
3643 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003644 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003645
3646 const uint32_t num_die_offsets = die_offsets.size();
3647 // Parse all of the types we found from the pubtypes matches
3648 uint32_t i;
3649 uint32_t num_matches = 0;
3650 for (i = 0; i < num_die_offsets; ++i)
3651 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003652 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3653 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003654 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003655 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003656 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003657 ++num_matches;
3658 if (num_matches >= max_matches)
3659 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003660 }
3661 }
3662
3663 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003664 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003665}
3666
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003667
3668size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003669SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3670 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003671 DWARFCompileUnit* dwarf_cu,
3672 const DWARFDebugInfoEntry *parent_die,
3673 bool skip_artificial,
3674 bool &is_static,
3675 TypeList* type_list,
3676 std::vector<clang_type_t>& function_param_types,
3677 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003678 unsigned &type_quals,
3679 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003680{
3681 if (parent_die == NULL)
3682 return 0;
3683
Greg Claytond88d7592010-09-15 08:33:30 +00003684 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3685
Greg Clayton7fedea22010-11-16 02:10:54 +00003686 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003687 const DWARFDebugInfoEntry *die;
3688 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3689 {
3690 dw_tag_t tag = die->Tag();
3691 switch (tag)
3692 {
3693 case DW_TAG_formal_parameter:
3694 {
3695 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003696 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003697 if (num_attributes > 0)
3698 {
3699 const char *name = NULL;
3700 Declaration decl;
3701 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003702 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003703 // one of None, Auto, Register, Extern, Static, PrivateExtern
3704
Sean Callanane2ef6e32010-09-23 03:01:22 +00003705 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003706 uint32_t i;
3707 for (i=0; i<num_attributes; ++i)
3708 {
3709 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3710 DWARFFormValue form_value;
3711 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3712 {
3713 switch (attr)
3714 {
3715 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3716 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3717 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3718 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3719 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003720 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003721 case DW_AT_location:
3722 // if (form_value.BlockData())
3723 // {
3724 // const DataExtractor& debug_info_data = debug_info();
3725 // uint32_t block_length = form_value.Unsigned();
3726 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3727 // }
3728 // else
3729 // {
3730 // }
3731 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003732 case DW_AT_const_value:
3733 case DW_AT_default_value:
3734 case DW_AT_description:
3735 case DW_AT_endianity:
3736 case DW_AT_is_optional:
3737 case DW_AT_segment:
3738 case DW_AT_variable_parameter:
3739 default:
3740 case DW_AT_abstract_origin:
3741 case DW_AT_sibling:
3742 break;
3743 }
3744 }
3745 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003746
Greg Clayton0fffff52010-09-24 05:15:53 +00003747 bool skip = false;
3748 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003749 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003750 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003751 {
3752 // In order to determine if a C++ member function is
3753 // "const" we have to look at the const-ness of "this"...
3754 // Ugly, but that
3755 if (arg_idx == 0)
3756 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003757 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003758 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003759 // Often times compilers omit the "this" name for the
3760 // specification DIEs, so we can't rely upon the name
3761 // being in the formal parameter DIE...
3762 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003763 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003764 Type *this_type = ResolveTypeUID (param_type_die_offset);
3765 if (this_type)
3766 {
3767 uint32_t encoding_mask = this_type->GetEncodingMask();
3768 if (encoding_mask & Type::eEncodingIsPointerUID)
3769 {
3770 is_static = false;
3771
3772 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3773 type_quals |= clang::Qualifiers::Const;
3774 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3775 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003776 }
3777 }
3778 }
3779 }
3780 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003781 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003782 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003783 else
3784 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003785
Greg Clayton0fffff52010-09-24 05:15:53 +00003786 // HACK: Objective C formal parameters "self" and "_cmd"
3787 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00003788 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3789 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00003790 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003791 switch (comp_unit->GetLanguage())
3792 {
3793 case eLanguageTypeObjC:
3794 case eLanguageTypeObjC_plus_plus:
3795 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3796 skip = true;
3797 break;
3798 default:
3799 break;
3800 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003801 }
3802 }
3803 }
3804
3805 if (!skip)
3806 {
3807 Type *type = ResolveTypeUID(param_type_die_offset);
3808 if (type)
3809 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003810 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003811
Greg Clayton42ce2f32011-12-12 21:50:19 +00003812 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3813 type->GetClangForwardType(),
3814 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003815 assert(param_var_decl);
3816 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00003817
3818 GetClangASTContext().SetMetadata((uintptr_t)param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00003819 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003820 }
3821 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003822 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003823 }
3824 break;
3825
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003826 case DW_TAG_template_type_parameter:
3827 case DW_TAG_template_value_parameter:
3828 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
3829 break;
3830
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003831 default:
3832 break;
3833 }
3834 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003835 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003836}
3837
3838size_t
3839SymbolFileDWARF::ParseChildEnumerators
3840(
3841 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003842 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003843 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003844 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003845 const DWARFDebugInfoEntry *parent_die
3846)
3847{
3848 if (parent_die == NULL)
3849 return 0;
3850
3851 size_t enumerators_added = 0;
3852 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003853 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3854
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003855 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3856 {
3857 const dw_tag_t tag = die->Tag();
3858 if (tag == DW_TAG_enumerator)
3859 {
3860 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003861 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003862 if (num_child_attributes > 0)
3863 {
3864 const char *name = NULL;
3865 bool got_value = false;
3866 int64_t enum_value = 0;
3867 Declaration decl;
3868
3869 uint32_t i;
3870 for (i=0; i<num_child_attributes; ++i)
3871 {
3872 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3873 DWARFFormValue form_value;
3874 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3875 {
3876 switch (attr)
3877 {
3878 case DW_AT_const_value:
3879 got_value = true;
3880 enum_value = form_value.Unsigned();
3881 break;
3882
3883 case DW_AT_name:
3884 name = form_value.AsCString(&get_debug_str_data());
3885 break;
3886
3887 case DW_AT_description:
3888 default:
3889 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3890 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3891 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3892 case DW_AT_sibling:
3893 break;
3894 }
3895 }
3896 }
3897
3898 if (name && name[0] && got_value)
3899 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003900 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3901 enumerator_clang_type,
3902 decl,
3903 name,
3904 enum_value,
3905 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003906 ++enumerators_added;
3907 }
3908 }
3909 }
3910 }
3911 return enumerators_added;
3912}
3913
3914void
3915SymbolFileDWARF::ParseChildArrayInfo
3916(
3917 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003918 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003919 const DWARFDebugInfoEntry *parent_die,
3920 int64_t& first_index,
3921 std::vector<uint64_t>& element_orders,
3922 uint32_t& byte_stride,
3923 uint32_t& bit_stride
3924)
3925{
3926 if (parent_die == NULL)
3927 return;
3928
3929 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003930 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003931 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3932 {
3933 const dw_tag_t tag = die->Tag();
3934 switch (tag)
3935 {
3936 case DW_TAG_enumerator:
3937 {
3938 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003939 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003940 if (num_child_attributes > 0)
3941 {
3942 const char *name = NULL;
3943 bool got_value = false;
3944 int64_t enum_value = 0;
3945
3946 uint32_t i;
3947 for (i=0; i<num_child_attributes; ++i)
3948 {
3949 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3950 DWARFFormValue form_value;
3951 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3952 {
3953 switch (attr)
3954 {
3955 case DW_AT_const_value:
3956 got_value = true;
3957 enum_value = form_value.Unsigned();
3958 break;
3959
3960 case DW_AT_name:
3961 name = form_value.AsCString(&get_debug_str_data());
3962 break;
3963
3964 case DW_AT_description:
3965 default:
3966 case DW_AT_decl_file:
3967 case DW_AT_decl_line:
3968 case DW_AT_decl_column:
3969 case DW_AT_sibling:
3970 break;
3971 }
3972 }
3973 }
3974 }
3975 }
3976 break;
3977
3978 case DW_TAG_subrange_type:
3979 {
3980 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003981 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003982 if (num_child_attributes > 0)
3983 {
3984 const char *name = NULL;
3985 bool got_value = false;
3986 uint64_t byte_size = 0;
3987 int64_t enum_value = 0;
3988 uint64_t num_elements = 0;
3989 uint64_t lower_bound = 0;
3990 uint64_t upper_bound = 0;
3991 uint32_t i;
3992 for (i=0; i<num_child_attributes; ++i)
3993 {
3994 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3995 DWARFFormValue form_value;
3996 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3997 {
3998 switch (attr)
3999 {
4000 case DW_AT_const_value:
4001 got_value = true;
4002 enum_value = form_value.Unsigned();
4003 break;
4004
4005 case DW_AT_name:
4006 name = form_value.AsCString(&get_debug_str_data());
4007 break;
4008
4009 case DW_AT_count:
4010 num_elements = form_value.Unsigned();
4011 break;
4012
4013 case DW_AT_bit_stride:
4014 bit_stride = form_value.Unsigned();
4015 break;
4016
4017 case DW_AT_byte_stride:
4018 byte_stride = form_value.Unsigned();
4019 break;
4020
4021 case DW_AT_byte_size:
4022 byte_size = form_value.Unsigned();
4023 break;
4024
4025 case DW_AT_lower_bound:
4026 lower_bound = form_value.Unsigned();
4027 break;
4028
4029 case DW_AT_upper_bound:
4030 upper_bound = form_value.Unsigned();
4031 break;
4032
4033 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004034 case DW_AT_abstract_origin:
4035 case DW_AT_accessibility:
4036 case DW_AT_allocated:
4037 case DW_AT_associated:
4038 case DW_AT_data_location:
4039 case DW_AT_declaration:
4040 case DW_AT_description:
4041 case DW_AT_sibling:
4042 case DW_AT_threads_scaled:
4043 case DW_AT_type:
4044 case DW_AT_visibility:
4045 break;
4046 }
4047 }
4048 }
4049
4050 if (upper_bound > lower_bound)
4051 num_elements = upper_bound - lower_bound + 1;
4052
4053 if (num_elements > 0)
4054 element_orders.push_back (num_elements);
4055 }
4056 }
4057 break;
4058 }
4059 }
4060}
4061
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004062TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004063SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004064{
4065 TypeSP type_sp;
4066 if (die != NULL)
4067 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004068 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004069 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004070 if (type_ptr == NULL)
4071 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004072 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004073 assert (lldb_cu);
4074 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004075 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004076 }
4077 else if (type_ptr != DIE_IS_BEING_PARSED)
4078 {
4079 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004080 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004081 }
4082
4083 }
4084 return type_sp;
4085}
4086
4087clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004088SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004089{
4090 if (die_offset != DW_INVALID_OFFSET)
4091 {
4092 DWARFCompileUnitSP cu_sp;
4093 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004094 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004095 }
4096 return NULL;
4097}
4098
Sean Callanan72e49402011-08-05 23:43:37 +00004099clang::DeclContext *
4100SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4101{
4102 if (die_offset != DW_INVALID_OFFSET)
4103 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004104 DWARFDebugInfo* debug_info = DebugInfo();
4105 if (debug_info)
4106 {
4107 DWARFCompileUnitSP cu_sp;
4108 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4109 if (die)
4110 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4111 }
Sean Callanan72e49402011-08-05 23:43:37 +00004112 }
4113 return NULL;
4114}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004115
Greg Clayton96d7d742010-11-10 23:42:09 +00004116clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004117SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004118{
Greg Clayton030a2042011-10-14 21:34:45 +00004119 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004120 {
Greg Clayton030a2042011-10-14 21:34:45 +00004121 // See if we already parsed this namespace DIE and associated it with a
4122 // uniqued namespace declaration
4123 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4124 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004125 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004126 else
4127 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004128 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4129 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004130 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
4131 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4132 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004133 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004134 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004135 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004136 GetObjectFile()->GetModule()->LogMessage (log.get(),
4137 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
4138 GetClangASTContext().getASTContext(),
4139 MakeUserID(die->GetOffset()),
4140 namespace_name,
4141 namespace_decl,
4142 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00004143 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004144 else
4145 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004146 GetObjectFile()->GetModule()->LogMessage (log.get(),
4147 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
4148 GetClangASTContext().getASTContext(),
4149 MakeUserID(die->GetOffset()),
4150 namespace_decl,
4151 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00004152 }
Greg Clayton030a2042011-10-14 21:34:45 +00004153 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004154
4155 if (namespace_decl)
4156 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4157 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004158 }
4159 }
4160 return NULL;
4161}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004162
4163clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004164SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004165{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004166 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4167 if (clang_decl_ctx)
4168 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004169 // If this DIE has a specification, or an abstract origin, then trace to those.
4170
Greg Claytoncab36a32011-12-08 05:16:30 +00004171 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004172 if (die_offset != DW_INVALID_OFFSET)
4173 return GetClangDeclContextForDIEOffset (sc, die_offset);
4174
Greg Claytoncab36a32011-12-08 05:16:30 +00004175 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004176 if (die_offset != DW_INVALID_OFFSET)
4177 return GetClangDeclContextForDIEOffset (sc, die_offset);
4178
Greg Clayton3bffb082011-12-10 02:15:28 +00004179 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4180 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004181 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 +00004182 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004183 bool assert_not_being_parsed = true;
4184 ResolveTypeUID (cu, die, assert_not_being_parsed);
4185
Greg Clayton5cf58b92011-10-05 22:22:08 +00004186 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4187
4188 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004189}
4190
4191clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004192SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004193{
Greg Claytonca512b32011-01-14 04:54:56 +00004194 if (m_clang_tu_decl == NULL)
4195 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004196
Greg Clayton2bc22f82011-09-30 03:20:47 +00004197 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004198
4199 if (decl_ctx_die_copy)
4200 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004201
4202 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004203 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004204
Greg Clayton2bc22f82011-09-30 03:20:47 +00004205 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4206 if (pos != m_die_to_decl_ctx.end())
4207 return pos->second;
4208
4209 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004210 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004211 case DW_TAG_compile_unit:
4212 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004213
Greg Clayton2bc22f82011-09-30 03:20:47 +00004214 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004215 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004216 break;
4217
4218 case DW_TAG_structure_type:
4219 case DW_TAG_union_type:
4220 case DW_TAG_class_type:
4221 {
4222 Type* type = ResolveType (cu, decl_ctx_die);
4223 if (type)
4224 {
4225 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4226 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004227 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004228 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4229 if (decl_ctx)
4230 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004231 }
4232 }
Greg Claytonca512b32011-01-14 04:54:56 +00004233 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004234 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004235
Greg Clayton2bc22f82011-09-30 03:20:47 +00004236 default:
4237 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004238 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004239 }
Greg Clayton7a345282010-11-09 23:46:37 +00004240 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004241}
4242
Greg Clayton2bc22f82011-09-30 03:20:47 +00004243
4244const DWARFDebugInfoEntry *
4245SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4246{
4247 if (cu && die)
4248 {
4249 const DWARFDebugInfoEntry * const decl_die = die;
4250
4251 while (die != NULL)
4252 {
4253 // If this is the original DIE that we are searching for a declaration
4254 // for, then don't look in the cache as we don't want our own decl
4255 // context to be our decl context...
4256 if (decl_die != die)
4257 {
4258 switch (die->Tag())
4259 {
4260 case DW_TAG_compile_unit:
4261 case DW_TAG_namespace:
4262 case DW_TAG_structure_type:
4263 case DW_TAG_union_type:
4264 case DW_TAG_class_type:
4265 return die;
4266
4267 default:
4268 break;
4269 }
4270 }
4271
4272 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4273 if (die_offset != DW_INVALID_OFFSET)
4274 {
4275 DWARFCompileUnit *spec_cu = cu;
4276 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4277 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4278 if (spec_die_decl_ctx_die)
4279 return spec_die_decl_ctx_die;
4280 }
4281
4282 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4283 if (die_offset != DW_INVALID_OFFSET)
4284 {
4285 DWARFCompileUnit *abs_cu = cu;
4286 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4287 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4288 if (abs_die_decl_ctx_die)
4289 return abs_die_decl_ctx_die;
4290 }
4291
4292 die = die->GetParent();
4293 }
4294 }
4295 return NULL;
4296}
4297
4298
Greg Clayton901c5ca2011-12-03 04:40:03 +00004299Symbol *
4300SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4301{
4302 Symbol *objc_class_symbol = NULL;
4303 if (m_obj_file)
4304 {
4305 Symtab *symtab = m_obj_file->GetSymtab();
4306 if (symtab)
4307 {
4308 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4309 eSymbolTypeObjCClass,
4310 Symtab::eDebugNo,
4311 Symtab::eVisibilityAny);
4312 }
4313 }
4314 return objc_class_symbol;
4315}
4316
Greg Claytonc7f03b62012-01-12 04:33:28 +00004317// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4318// then we can end up looking through all class types for a complete type and never find
4319// the full definition. We need to know if this attribute is supported, so we determine
4320// this here and cache th result. We also need to worry about the debug map DWARF file
4321// if we are doing darwin DWARF in .o file debugging.
4322bool
4323SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4324{
4325 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4326 {
4327 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4328 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4329 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4330 else
4331 {
4332 DWARFDebugInfo* debug_info = DebugInfo();
4333 const uint32_t num_compile_units = GetNumCompileUnits();
4334 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4335 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004336 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4337 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004338 {
4339 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4340 break;
4341 }
4342 }
4343 }
4344 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && m_debug_map_symfile)
4345 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4346 }
4347 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4348}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004349
4350// This function can be used when a DIE is found that is a forward declaration
4351// DIE and we want to try and find a type that has the complete definition.
4352TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004353SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4354 const ConstString &type_name,
4355 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004356{
4357
4358 TypeSP type_sp;
4359
Greg Claytonc7f03b62012-01-12 04:33:28 +00004360 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004361 return type_sp;
4362
4363 DIEArray die_offsets;
4364
4365 if (m_using_apple_tables)
4366 {
4367 if (m_apple_types_ap.get())
4368 {
4369 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004370 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004371 }
4372 }
4373 else
4374 {
4375 if (!m_indexed)
4376 Index ();
4377
4378 m_type_index.Find (type_name, die_offsets);
4379 }
4380
Greg Clayton901c5ca2011-12-03 04:40:03 +00004381 const size_t num_matches = die_offsets.size();
4382
Greg Clayton901c5ca2011-12-03 04:40:03 +00004383 DWARFCompileUnit* type_cu = NULL;
4384 const DWARFDebugInfoEntry* type_die = NULL;
4385 if (num_matches)
4386 {
4387 DWARFDebugInfo* debug_info = DebugInfo();
4388 for (size_t i=0; i<num_matches; ++i)
4389 {
4390 const dw_offset_t die_offset = die_offsets[i];
4391 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4392
4393 if (type_die)
4394 {
4395 bool try_resolving_type = false;
4396
4397 // Don't try and resolve the DIE we are looking for with the DIE itself!
4398 if (type_die != die)
4399 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004400 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004401 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004402 case DW_TAG_class_type:
4403 case DW_TAG_structure_type:
4404 try_resolving_type = true;
4405 break;
4406 default:
4407 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004408 }
4409 }
4410
4411 if (try_resolving_type)
4412 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004413 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004414 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004415
4416 if (try_resolving_type)
4417 {
4418 Type *resolved_type = ResolveType (type_cu, type_die, false);
4419 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4420 {
4421 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4422 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004423 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton901c5ca2011-12-03 04:40:03 +00004424 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4425 MakeUserID(type_die->GetOffset()),
4426 MakeUserID(type_cu->GetOffset()));
4427
Greg Claytonc7f03b62012-01-12 04:33:28 +00004428 if (die)
4429 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004430 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004431 break;
4432 }
4433 }
4434 }
4435 }
4436 else
4437 {
4438 if (m_using_apple_tables)
4439 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004440 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4441 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004442 }
4443 }
4444
4445 }
4446 }
4447 return type_sp;
4448}
4449
Greg Clayton80c26302012-02-05 06:12:47 +00004450//----------------------------------------------------------------------
4451// This function helps to ensure that the declaration contexts match for
4452// two different DIEs. Often times debug information will refer to a
4453// forward declaration of a type (the equivalent of "struct my_struct;".
4454// There will often be a declaration of that type elsewhere that has the
4455// full definition. When we go looking for the full type "my_struct", we
4456// will find one or more matches in the accelerator tables and we will
4457// then need to make sure the type was in the same declaration context
4458// as the original DIE. This function can efficiently compare two DIEs
4459// and will return true when the declaration context matches, and false
4460// when they don't.
4461//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004462bool
4463SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4464 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4465{
4466 assert (die1 != die2);
4467 DWARFDIECollection decl_ctx_1;
4468 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004469 //The declaration DIE stack is a stack of the declaration context
4470 // DIEs all the way back to the compile unit. If a type "T" is
4471 // declared inside a class "B", and class "B" is declared inside
4472 // a class "A" and class "A" is in a namespace "lldb", and the
4473 // namespace is in a compile unit, there will be a stack of DIEs:
4474 //
4475 // [0] DW_TAG_class_type for "B"
4476 // [1] DW_TAG_class_type for "A"
4477 // [2] DW_TAG_namespace for "lldb"
4478 // [3] DW_TAG_compile_unit for the source file.
4479 //
4480 // We grab both contexts and make sure that everything matches
4481 // all the way back to the compiler unit.
4482
4483 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004484 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004485 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004486 // Make sure the context arrays have the same size, otherwise
4487 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004488 const size_t count1 = decl_ctx_1.Size();
4489 const size_t count2 = decl_ctx_2.Size();
4490 if (count1 != count2)
4491 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004492
4493 // Make sure the DW_TAG values match all the way back up the the
4494 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004495 const DWARFDebugInfoEntry *decl_ctx_die1;
4496 const DWARFDebugInfoEntry *decl_ctx_die2;
4497 size_t i;
4498 for (i=0; i<count1; i++)
4499 {
4500 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4501 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4502 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4503 return false;
4504 }
Greg Clayton890ff562012-02-02 05:48:16 +00004505#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004506
4507 // Make sure the top item in the decl context die array is always
4508 // DW_TAG_compile_unit. If it isn't then something went wrong in
4509 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004510 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004511
Greg Clayton890ff562012-02-02 05:48:16 +00004512#endif
4513 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004514 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004515 for (i=0; i<count1 - 1; i++)
4516 {
4517 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4518 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4519 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004520 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004521 // If the string was from a DW_FORM_strp, then the pointer will often
4522 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004523 if (name1 == name2)
4524 continue;
4525
4526 // Name pointers are not equal, so only compare the strings
4527 // if both are not NULL.
4528 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004529 {
Greg Clayton5569e642012-02-06 01:44:54 +00004530 // If the strings don't compare, we are done...
4531 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004532 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004533 }
4534 else
4535 {
4536 // One name was NULL while the other wasn't
4537 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004538 }
4539 }
Greg Clayton80c26302012-02-05 06:12:47 +00004540 // We made it through all of the checks and the declaration contexts
4541 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004542 return true;
4543}
Greg Clayton220a0072011-12-09 08:48:30 +00004544
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004545// This function can be used when a DIE is found that is a forward declaration
4546// DIE and we want to try and find a type that has the complete definition.
4547TypeSP
Greg Clayton7f995132011-10-04 22:41:51 +00004548SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
4549 const DWARFDebugInfoEntry *die,
4550 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004551{
4552 TypeSP type_sp;
4553
Greg Clayton1a65ae12011-01-25 23:55:37 +00004554 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004555 return type_sp;
4556
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004557 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4558 if (log)
4559 {
4560 std::string qualified_name;
4561 die->GetQualifiedName(this, cu, qualified_name);
4562 GetObjectFile()->GetModule()->LogMessage (log.get(),
4563 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x (%s), name='%s')",
4564 die->GetOffset(),
4565 qualified_name.c_str(),
4566 type_name.GetCString());
4567 }
4568
Greg Clayton7f995132011-10-04 22:41:51 +00004569 DIEArray die_offsets;
4570
Greg Clayton97fbc342011-10-20 22:30:33 +00004571 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004572 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004573 if (m_apple_types_ap.get())
4574 {
Greg Claytond1767f02011-12-08 02:13:16 +00004575 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4576 {
Greg Claytonae920b62012-01-06 00:17:16 +00004577 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004578 }
4579 else
4580 {
4581 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4582 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004583 }
Greg Clayton7f995132011-10-04 22:41:51 +00004584 }
4585 else
4586 {
4587 if (!m_indexed)
4588 Index ();
4589
4590 m_type_index.Find (type_name, die_offsets);
4591 }
Greg Clayton7f995132011-10-04 22:41:51 +00004592
4593 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004594
Greg Clayton934cb052011-12-03 00:27:05 +00004595 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004596
4597 DWARFCompileUnit* type_cu = NULL;
4598 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004599 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004600 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004601 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004602 for (size_t i=0; i<num_matches; ++i)
4603 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004604 const dw_offset_t die_offset = die_offsets[i];
4605 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004606
Greg Clayton95d87902011-11-11 03:16:25 +00004607 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004608 {
Greg Clayton934cb052011-12-03 00:27:05 +00004609 bool try_resolving_type = false;
4610
4611 // Don't try and resolve the DIE we are looking for with the DIE itself!
4612 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004613 {
Greg Clayton934cb052011-12-03 00:27:05 +00004614 const dw_tag_t type_die_tag = type_die->Tag();
4615 // Make sure the tags match
4616 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004617 {
Greg Clayton934cb052011-12-03 00:27:05 +00004618 // The tags match, lets try resolving this type
4619 try_resolving_type = true;
4620 }
4621 else
4622 {
4623 // The tags don't match, but we need to watch our for a
4624 // forward declaration for a struct and ("struct foo")
4625 // ends up being a class ("class foo { ... };") or
4626 // vice versa.
4627 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004628 {
Greg Clayton934cb052011-12-03 00:27:05 +00004629 case DW_TAG_class_type:
4630 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4631 try_resolving_type = (die_tag == DW_TAG_structure_type);
4632 break;
4633 case DW_TAG_structure_type:
4634 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4635 try_resolving_type = (die_tag == DW_TAG_class_type);
4636 break;
4637 default:
4638 // Tags don't match, don't event try to resolve
4639 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004640 break;
4641 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004642 }
4643 }
Greg Clayton934cb052011-12-03 00:27:05 +00004644
4645 if (try_resolving_type)
4646 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004647 if (log)
4648 {
4649 std::string qualified_name;
4650 type_die->GetQualifiedName(this, cu, qualified_name);
4651 GetObjectFile()->GetModule()->LogMessage (log.get(),
4652 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') trying die=0x%8.8x (%s)",
4653 die->GetOffset(),
4654 type_name.GetCString(),
4655 type_die->GetOffset(),
4656 qualified_name.c_str());
4657 }
4658
Greg Clayton890ff562012-02-02 05:48:16 +00004659 // Make sure the decl contexts match all the way up
4660 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004661 {
Greg Clayton890ff562012-02-02 05:48:16 +00004662 Type *resolved_type = ResolveType (type_cu, type_die, false);
4663 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4664 {
4665 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4666 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004667 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton890ff562012-02-02 05:48:16 +00004668 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4669 MakeUserID(type_die->GetOffset()),
4670 MakeUserID(type_cu->GetOffset()));
4671
4672 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004673 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004674 break;
4675 }
Greg Clayton934cb052011-12-03 00:27:05 +00004676 }
4677 }
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004678 else
4679 {
4680 if (log)
4681 {
4682 std::string qualified_name;
4683 type_die->GetQualifiedName(this, cu, qualified_name);
4684 GetObjectFile()->GetModule()->LogMessage (log.get(),
4685 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') ignoring die=0x%8.8x (%s)",
4686 die->GetOffset(),
4687 type_name.GetCString(),
4688 type_die->GetOffset(),
4689 qualified_name.c_str());
4690 }
4691 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004692 }
Greg Clayton95d87902011-11-11 03:16:25 +00004693 else
4694 {
4695 if (m_using_apple_tables)
4696 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004697 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4698 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004699 }
4700 }
4701
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004702 }
4703 }
4704 return type_sp;
4705}
4706
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004707TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00004708SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004709{
4710 TypeSP type_sp;
4711
Greg Clayton1be10fc2010-09-29 01:12:09 +00004712 if (type_is_new_ptr)
4713 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00004714
4715#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4716 static DIEStack g_die_stack;
4717 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
4718#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00004719
Sean Callananc7fbf732010-08-06 00:32:49 +00004720 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004721 if (die != NULL)
4722 {
Greg Clayton21f2a492011-10-06 00:09:08 +00004723 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00004724 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00004725 {
4726 const DWARFDebugInfoEntry *context_die;
4727 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
4728
Greg Clayton1fba87112012-02-09 20:03:49 +00004729 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 +00004730 die->GetOffset(),
4731 context,
4732 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00004733 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00004734 die->GetName(this, dwarf_cu));
4735
4736#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4737 scoped_die_logger.Push (dwarf_cu, die);
4738 g_die_stack.LogDIEs(log.get(), this);
4739#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00004740 }
Greg Clayton3bffb082011-12-10 02:15:28 +00004741//
4742// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4743// if (log && dwarf_cu)
4744// {
4745// StreamString s;
4746// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00004747// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00004748//
4749// }
Jim Ingham16746d12011-08-25 23:21:43 +00004750
Greg Clayton594e5ed2010-09-27 21:07:38 +00004751 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004752 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00004753 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004754 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004755 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00004756 if (type_is_new_ptr)
4757 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004758
Greg Clayton594e5ed2010-09-27 21:07:38 +00004759 const dw_tag_t tag = die->Tag();
4760
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004761 bool is_forward_declaration = false;
4762 DWARFDebugInfoEntry::Attributes attributes;
4763 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00004764 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00004765 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
4766 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00004767 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00004768 Declaration decl;
4769
Greg Clayton4957bf62010-09-30 21:49:03 +00004770 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00004771 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004772
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004773 dw_attr_t attr;
4774
4775 switch (tag)
4776 {
4777 case DW_TAG_base_type:
4778 case DW_TAG_pointer_type:
4779 case DW_TAG_reference_type:
4780 case DW_TAG_typedef:
4781 case DW_TAG_const_type:
4782 case DW_TAG_restrict_type:
4783 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00004784 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004785 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004786 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004787 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004788
Greg Claytond88d7592010-09-15 08:33:30 +00004789 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004790 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004791 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
4792
4793 if (num_attributes > 0)
4794 {
4795 uint32_t i;
4796 for (i=0; i<num_attributes; ++i)
4797 {
4798 attr = attributes.AttributeAtIndex(i);
4799 DWARFFormValue form_value;
4800 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4801 {
4802 switch (attr)
4803 {
4804 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4805 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4806 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4807 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00004808
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004809 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00004810 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
4811 // include the "&"...
4812 if (tag == DW_TAG_reference_type)
4813 {
4814 if (strchr (type_name_cstr, '&') == NULL)
4815 type_name_cstr = NULL;
4816 }
4817 if (type_name_cstr)
4818 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004819 break;
Greg Clayton36909642011-03-15 04:38:20 +00004820 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004821 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
4822 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
4823 default:
4824 case DW_AT_sibling:
4825 break;
4826 }
4827 }
4828 }
4829 }
4830
Greg Clayton81c22f62011-10-19 18:09:39 +00004831 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 +00004832
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004833 switch (tag)
4834 {
4835 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00004836 break;
4837
Greg Claytond4436412011-10-28 21:00:00 +00004838 case DW_TAG_unspecified_type:
4839 if (strcmp(type_name_cstr, "nullptr_t") == 0)
4840 {
4841 resolve_state = Type::eResolveStateFull;
4842 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
4843 break;
4844 }
4845 // Fall through to base type below in case we can handle the type there...
4846
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004847 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00004848 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004849 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
4850 encoding,
4851 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004852 break;
4853
Greg Clayton526e5af2010-11-13 03:52:47 +00004854 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
4855 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
4856 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
4857 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
4858 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
4859 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004860 }
4861
Greg Claytonc7f03b62012-01-12 04:33:28 +00004862 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004863 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004864 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
4865 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004866 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004867 static ConstString g_objc_type_name_id("id");
4868 static ConstString g_objc_type_name_Class("Class");
4869 static ConstString g_objc_type_name_selector("SEL");
4870
4871 if (type_name_const_str == g_objc_type_name_id)
4872 {
4873 if (log)
4874 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
4875 die->GetOffset(),
4876 DW_TAG_value_to_name(die->Tag()),
4877 die->GetName(this, dwarf_cu));
4878 clang_type = ast.GetBuiltInType_objc_id();
4879 encoding_data_type = Type::eEncodingIsUID;
4880 encoding_uid = LLDB_INVALID_UID;
4881 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00004882
Greg Claytonc7f03b62012-01-12 04:33:28 +00004883 }
4884 else if (type_name_const_str == g_objc_type_name_Class)
4885 {
4886 if (log)
4887 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
4888 die->GetOffset(),
4889 DW_TAG_value_to_name(die->Tag()),
4890 die->GetName(this, dwarf_cu));
4891 clang_type = ast.GetBuiltInType_objc_Class();
4892 encoding_data_type = Type::eEncodingIsUID;
4893 encoding_uid = LLDB_INVALID_UID;
4894 resolve_state = Type::eResolveStateFull;
4895 }
4896 else if (type_name_const_str == g_objc_type_name_selector)
4897 {
4898 if (log)
4899 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
4900 die->GetOffset(),
4901 DW_TAG_value_to_name(die->Tag()),
4902 die->GetName(this, dwarf_cu));
4903 clang_type = ast.GetBuiltInType_objc_selector();
4904 encoding_data_type = Type::eEncodingIsUID;
4905 encoding_uid = LLDB_INVALID_UID;
4906 resolve_state = Type::eResolveStateFull;
4907 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004908 }
4909 }
4910
Greg Clayton81c22f62011-10-19 18:09:39 +00004911 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004912 this,
4913 type_name_const_str,
4914 byte_size,
4915 NULL,
4916 encoding_uid,
4917 encoding_data_type,
4918 &decl,
4919 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004920 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004921
Greg Clayton594e5ed2010-09-27 21:07:38 +00004922 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004923
4924// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
4925// if (encoding_type != NULL)
4926// {
4927// if (encoding_type != DIE_IS_BEING_PARSED)
4928// type_sp->SetEncodingType(encoding_type);
4929// else
4930// m_indirect_fixups.push_back(type_sp.get());
4931// }
4932 }
4933 break;
4934
4935 case DW_TAG_structure_type:
4936 case DW_TAG_union_type:
4937 case DW_TAG_class_type:
4938 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004939 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004940 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004941
Greg Clayton9e409562010-07-28 02:04:09 +00004942 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00004943 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004944 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00004945 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004946 if (num_attributes > 0)
4947 {
4948 uint32_t i;
4949 for (i=0; i<num_attributes; ++i)
4950 {
4951 attr = attributes.AttributeAtIndex(i);
4952 DWARFFormValue form_value;
4953 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4954 {
4955 switch (attr)
4956 {
Greg Clayton9e409562010-07-28 02:04:09 +00004957 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00004958 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
4959 {
4960 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
4961 // point to the compile unit file, so we clear this invalid value
4962 // so that we can still unique types efficiently.
4963 decl.SetFile(FileSpec ("<invalid>", false));
4964 }
4965 else
4966 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00004967 break;
4968
4969 case DW_AT_decl_line:
4970 decl.SetLine(form_value.Unsigned());
4971 break;
4972
4973 case DW_AT_decl_column:
4974 decl.SetColumn(form_value.Unsigned());
4975 break;
4976
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004977 case DW_AT_name:
4978 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004979 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004980 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004981
4982 case DW_AT_byte_size:
4983 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00004984 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004985 break;
4986
4987 case DW_AT_accessibility:
4988 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
4989 break;
4990
4991 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00004992 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00004993 break;
4994
4995 case DW_AT_APPLE_runtime_class:
4996 class_language = (LanguageType)form_value.Signed();
4997 break;
4998
Greg Clayton18774842011-11-29 23:40:34 +00004999 case DW_AT_APPLE_objc_complete_type:
5000 is_complete_objc_class = form_value.Signed();
5001 break;
5002
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005003 case DW_AT_allocated:
5004 case DW_AT_associated:
5005 case DW_AT_data_location:
5006 case DW_AT_description:
5007 case DW_AT_start_scope:
5008 case DW_AT_visibility:
5009 default:
5010 case DW_AT_sibling:
5011 break;
5012 }
5013 }
5014 }
5015 }
5016
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005017 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00005018
Greg Clayton123edca2012-03-02 00:07:15 +00005019 // Only try and unique the type if it has a name.
5020 if (type_name_const_str &&
5021 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00005022 this,
5023 dwarf_cu,
5024 die,
5025 decl,
5026 byte_size_valid ? byte_size : -1,
5027 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005028 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005029 // We have already parsed this type or from another
5030 // compile unit. GCC loves to use the "one definition
5031 // rule" which can result in multiple definitions
5032 // of the same class over and over in each compile
5033 // unit.
5034 type_sp = unique_ast_entry.m_type_sp;
5035 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00005036 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005037 m_die_to_type[die] = type_sp.get();
5038 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005039 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005040 }
5041
Greg Clayton81c22f62011-10-19 18:09:39 +00005042 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 +00005043
5044 int tag_decl_kind = -1;
5045 AccessType default_accessibility = eAccessNone;
5046 if (tag == DW_TAG_structure_type)
5047 {
5048 tag_decl_kind = clang::TTK_Struct;
5049 default_accessibility = eAccessPublic;
5050 }
5051 else if (tag == DW_TAG_union_type)
5052 {
5053 tag_decl_kind = clang::TTK_Union;
5054 default_accessibility = eAccessPublic;
5055 }
5056 else if (tag == DW_TAG_class_type)
5057 {
5058 tag_decl_kind = clang::TTK_Class;
5059 default_accessibility = eAccessPrivate;
5060 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00005061
5062 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
5063 die->HasChildren() == false &&
5064 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
5065 {
5066 // Work around an issue with clang at the moment where
5067 // forward declarations for objective C classes are emitted
5068 // as:
5069 // DW_TAG_structure_type [2]
5070 // DW_AT_name( "ForwardObjcClass" )
5071 // DW_AT_byte_size( 0x00 )
5072 // DW_AT_decl_file( "..." )
5073 // DW_AT_decl_line( 1 )
5074 //
5075 // Note that there is no DW_AT_declaration and there are
5076 // no children, and the byte size is zero.
5077 is_forward_declaration = true;
5078 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005079
Greg Clayton18774842011-11-29 23:40:34 +00005080 if (class_language == eLanguageTypeObjC)
5081 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005082 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00005083 {
5084 // We have a valid eSymbolTypeObjCClass class symbol whose
5085 // name matches the current objective C class that we
5086 // are trying to find and this DIE isn't the complete
5087 // definition (we checked is_complete_objc_class above and
5088 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00005089 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005090
Greg Clayton901c5ca2011-12-03 04:40:03 +00005091 if (!type_sp && m_debug_map_symfile)
5092 {
5093 // We weren't able to find a full declaration in
5094 // this DWARF, see if we have a declaration anywhere
5095 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00005096 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005097 }
5098
5099 if (type_sp)
5100 {
5101 if (log)
5102 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005103 GetObjectFile()->GetModule()->LogMessage (log.get(),
5104 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
5105 this,
5106 die->GetOffset(),
5107 DW_TAG_value_to_name(tag),
5108 type_name_cstr,
5109 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00005110 }
5111
5112 // We found a real definition for this type elsewhere
5113 // so lets use it and cache the fact that we found
5114 // a complete type for this die
5115 m_die_to_type[die] = type_sp.get();
5116 return type_sp;
5117 }
5118 }
5119 }
5120
5121
5122 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005123 {
5124 // We have a forward declaration to a type and we need
5125 // to try and find a full declaration. We look in the
5126 // current type index just in case we have a forward
5127 // declaration followed by an actual declarations in the
5128 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00005129 if (log)
5130 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005131 GetObjectFile()->GetModule()->LogMessage (log.get(),
5132 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
5133 this,
5134 die->GetOffset(),
5135 DW_TAG_value_to_name(tag),
5136 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00005137 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005138
5139 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
5140
5141 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00005142 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005143 // We weren't able to find a full declaration in
5144 // this DWARF, see if we have a declaration anywhere
5145 // else...
5146 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
Greg Clayton4272cc72011-02-02 02:24:04 +00005147 }
5148
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005149 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00005150 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00005151 if (log)
5152 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005153 GetObjectFile()->GetModule()->LogMessage (log.get(),
5154 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
5155 this,
5156 die->GetOffset(),
5157 DW_TAG_value_to_name(tag),
5158 type_name_cstr,
5159 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00005160 }
5161
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005162 // We found a real definition for this type elsewhere
5163 // so lets use it and cache the fact that we found
5164 // a complete type for this die
5165 m_die_to_type[die] = type_sp.get();
5166 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005167 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005168 }
5169 assert (tag_decl_kind != -1);
5170 bool clang_type_was_created = false;
5171 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5172 if (clang_type == NULL)
5173 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00005174 const DWARFDebugInfoEntry *decl_ctx_die;
5175
5176 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00005177 if (accessibility == eAccessNone && decl_ctx)
5178 {
5179 // Check the decl context that contains this class/struct/union.
5180 // If it is a class we must give it an accessability.
5181 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
5182 if (DeclKindIsCXXClass (containing_decl_kind))
5183 accessibility = default_accessibility;
5184 }
5185
Greg Claytonf0705c82011-10-22 03:33:13 +00005186 if (type_name_cstr && strchr (type_name_cstr, '<'))
5187 {
5188 ClangASTContext::TemplateParameterInfos template_param_infos;
5189 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
5190 {
5191 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00005192 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00005193 type_name_cstr,
5194 tag_decl_kind,
5195 template_param_infos);
5196
5197 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
5198 class_template_decl,
5199 tag_decl_kind,
5200 template_param_infos);
5201 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
5202 clang_type_was_created = true;
Sean Callanan60217122012-04-13 00:10:03 +00005203
5204 GetClangASTContext().SetMetadata((uintptr_t)class_template_decl, MakeUserID(die->GetOffset()));
5205 GetClangASTContext().SetMetadata((uintptr_t)class_specialization_decl, MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005206 }
5207 }
5208
5209 if (!clang_type_was_created)
5210 {
5211 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00005212 clang_type = ast.CreateRecordType (decl_ctx,
5213 accessibility,
5214 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00005215 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00005216 class_language,
Sean Callananad880762012-04-18 01:06:17 +00005217 MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005218 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005219 }
5220
5221 // Store a forward declaration to this class type in case any
5222 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00005223 // types for function prototypes.
5224 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00005225 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005226 this,
5227 type_name_const_str,
5228 byte_size,
5229 NULL,
5230 LLDB_INVALID_UID,
5231 Type::eEncodingIsUID,
5232 &decl,
5233 clang_type,
5234 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00005235
5236 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005237
5238
5239 // Add our type to the unique type map so we don't
5240 // end up creating many copies of the same type over
5241 // and over in the ASTContext for our module
5242 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00005243 unique_ast_entry.m_symfile = this;
5244 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005245 unique_ast_entry.m_die = die;
5246 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00005247 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00005248 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
5249 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005250
Sean Callanan12014a02011-12-08 23:45:45 +00005251 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00005252 {
5253 // Always start the definition for a class type so that
5254 // if the class has child classes or types that require
5255 // the class to be created for use as their decl contexts
5256 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00005257 if (die->HasChildren() == false)
5258 {
5259 // No children for this struct/union/class, lets finish it
5260 ast.StartTagDeclarationDefinition (clang_type);
5261 ast.CompleteTagDeclarationDefinition (clang_type);
5262 }
5263 else if (clang_type_was_created)
5264 {
Greg Clayton219cf312012-03-30 00:51:13 +00005265 // Start the definition if the class is not objective C since
5266 // the underlying decls respond to isCompleteDefinition(). Objective
5267 // C decls dont' respond to isCompleteDefinition() so we can't
5268 // start the declaration definition right away. For C++ classs/union/structs
5269 // we want to start the definition in case the class is needed as the
5270 // declaration context for a contained class or type without the need
5271 // to complete that type..
5272
5273 if (class_language != eLanguageTypeObjC)
5274 ast.StartTagDeclarationDefinition (clang_type);
5275
Sean Callanan12014a02011-12-08 23:45:45 +00005276 // Leave this as a forward declaration until we need
5277 // to know the details of the type. lldb_private::Type
5278 // will automatically call the SymbolFile virtual function
5279 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
5280 // When the definition needs to be defined.
5281 m_forward_decl_die_to_clang_type[die] = clang_type;
5282 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
5283 ClangASTContext::SetHasExternalStorage (clang_type, true);
5284 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005285 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005286
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005287 }
5288 break;
5289
5290 case DW_TAG_enumeration_type:
5291 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005292 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005293 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005294
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005295 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005296
Greg Claytond88d7592010-09-15 08:33:30 +00005297 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005298 if (num_attributes > 0)
5299 {
5300 uint32_t i;
5301
5302 for (i=0; i<num_attributes; ++i)
5303 {
5304 attr = attributes.AttributeAtIndex(i);
5305 DWARFFormValue form_value;
5306 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5307 {
5308 switch (attr)
5309 {
Greg Clayton7a345282010-11-09 23:46:37 +00005310 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5311 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5312 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005313 case DW_AT_name:
5314 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005315 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005316 break;
Greg Clayton7a345282010-11-09 23:46:37 +00005317 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005318 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00005319 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5320 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005321 case DW_AT_allocated:
5322 case DW_AT_associated:
5323 case DW_AT_bit_stride:
5324 case DW_AT_byte_stride:
5325 case DW_AT_data_location:
5326 case DW_AT_description:
5327 case DW_AT_start_scope:
5328 case DW_AT_visibility:
5329 case DW_AT_specification:
5330 case DW_AT_abstract_origin:
5331 case DW_AT_sibling:
5332 break;
5333 }
5334 }
5335 }
5336
Greg Clayton81c22f62011-10-19 18:09:39 +00005337 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 +00005338
Greg Clayton1be10fc2010-09-29 01:12:09 +00005339 clang_type_t enumerator_clang_type = NULL;
5340 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5341 if (clang_type == NULL)
5342 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005343 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
5344 DW_ATE_signed,
5345 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00005346 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00005347 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00005348 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005349 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00005350 }
5351 else
5352 {
5353 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
5354 assert (enumerator_clang_type != NULL);
5355 }
5356
Greg Claytona2721472011-06-25 00:44:06 +00005357 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
5358
Greg Clayton81c22f62011-10-19 18:09:39 +00005359 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005360 this,
5361 type_name_const_str,
5362 byte_size,
5363 NULL,
5364 encoding_uid,
5365 Type::eEncodingIsUID,
5366 &decl,
5367 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005368 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005369
Greg Clayton6beaaa62011-01-17 03:46:26 +00005370 ast.StartTagDeclarationDefinition (clang_type);
5371 if (die->HasChildren())
5372 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005373 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
5374 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005375 }
5376 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005377 }
5378 }
5379 break;
5380
Jim Inghamb0be4422010-08-12 01:20:14 +00005381 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005382 case DW_TAG_subprogram:
5383 case DW_TAG_subroutine_type:
5384 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005385 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005386 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005387
5388 const char *mangled = NULL;
5389 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00005390 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005391 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00005392 bool is_static = false;
5393 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00005394 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00005395 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00005396 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
5397 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00005398
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005399 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00005400 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005401
5402
Greg Claytond88d7592010-09-15 08:33:30 +00005403 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005404 if (num_attributes > 0)
5405 {
5406 uint32_t i;
5407 for (i=0; i<num_attributes; ++i)
5408 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005409 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005410 DWARFFormValue form_value;
5411 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5412 {
5413 switch (attr)
5414 {
5415 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5416 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5417 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5418 case DW_AT_name:
5419 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005420 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005421 break;
5422
5423 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
5424 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005425 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005426 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00005427 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
5428 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00005429 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00005430 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5431
Greg Claytonf51de672010-10-01 02:31:07 +00005432
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005433 case DW_AT_external:
5434 if (form_value.Unsigned())
5435 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00005436 if (storage == clang::SC_None)
5437 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005438 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00005439 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005440 }
5441 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005442
Greg Clayton72da3972011-08-16 18:40:23 +00005443 case DW_AT_specification:
5444 specification_die_offset = form_value.Reference(dwarf_cu);
5445 break;
5446
5447 case DW_AT_abstract_origin:
5448 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
5449 break;
5450
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005451 case DW_AT_allocated:
5452 case DW_AT_associated:
5453 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005454 case DW_AT_calling_convention:
5455 case DW_AT_data_location:
5456 case DW_AT_elemental:
5457 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005458 case DW_AT_frame_base:
5459 case DW_AT_high_pc:
5460 case DW_AT_low_pc:
5461 case DW_AT_object_pointer:
5462 case DW_AT_prototyped:
5463 case DW_AT_pure:
5464 case DW_AT_ranges:
5465 case DW_AT_recursive:
5466 case DW_AT_return_addr:
5467 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005468 case DW_AT_start_scope:
5469 case DW_AT_static_link:
5470 case DW_AT_trampoline:
5471 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005472 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005473 case DW_AT_description:
5474 case DW_AT_sibling:
5475 break;
5476 }
5477 }
5478 }
Greg Clayton24739922010-10-13 03:15:28 +00005479 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005480
Greg Clayton81c22f62011-10-19 18:09:39 +00005481 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 +00005482
Greg Clayton24739922010-10-13 03:15:28 +00005483 clang_type_t return_clang_type = NULL;
5484 Type *func_type = NULL;
5485
5486 if (type_die_offset != DW_INVALID_OFFSET)
5487 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00005488
Greg Clayton24739922010-10-13 03:15:28 +00005489 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00005490 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00005491 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005492 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005493
Greg Claytonf51de672010-10-01 02:31:07 +00005494
Greg Clayton24739922010-10-13 03:15:28 +00005495 std::vector<clang_type_t> function_param_types;
5496 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005497
Greg Clayton24739922010-10-13 03:15:28 +00005498 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00005499
Greg Claytoncb5860a2011-10-13 23:49:28 +00005500 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
5501 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00005502 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
5503
Greg Claytonf0705c82011-10-22 03:33:13 +00005504 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00005505 // Start off static. This will be set to false in ParseChildParameters(...)
5506 // if we find a "this" paramters as the first parameter
5507 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00005508 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005509 ClangASTContext::TemplateParameterInfos template_param_infos;
5510
Greg Clayton24739922010-10-13 03:15:28 +00005511 if (die->HasChildren())
5512 {
Greg Clayton0fffff52010-09-24 05:15:53 +00005513 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005514 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00005515 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005516 dwarf_cu,
5517 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00005518 skip_artificial,
5519 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005520 type_list,
5521 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00005522 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005523 type_quals,
5524 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00005525 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005526
Greg Clayton24739922010-10-13 03:15:28 +00005527 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005528 clang_type = ast.CreateFunctionType (return_clang_type,
5529 &function_param_types[0],
5530 function_param_types.size(),
5531 is_variadic,
5532 type_quals);
5533
Greg Clayton24739922010-10-13 03:15:28 +00005534 if (type_name_cstr)
5535 {
5536 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00005537 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005538 {
Greg Clayton7c810422012-01-18 23:40:49 +00005539 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00005540 ConstString class_name_no_category;
5541 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00005542 {
Greg Claytone42ae842012-01-19 03:24:53 +00005543 // Use the class name with no category if there is one
5544 if (class_name_no_category)
5545 class_name = class_name_no_category;
5546
Greg Clayton24739922010-10-13 03:15:28 +00005547 SymbolContext empty_sc;
5548 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00005549 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00005550 {
Greg Clayton24739922010-10-13 03:15:28 +00005551 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00005552 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00005553
5554 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00005555 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005556 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
5557 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
5558 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00005559 }
Greg Clayton24739922010-10-13 03:15:28 +00005560 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005561
Greg Clayton24739922010-10-13 03:15:28 +00005562 if (class_opaque_type)
5563 {
5564 // If accessibility isn't set to anything valid, assume public for
5565 // now...
5566 if (accessibility == eAccessNone)
5567 accessibility = eAccessPublic;
5568
5569 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005570 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
5571 type_name_cstr,
5572 clang_type,
5573 accessibility);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005574 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton24739922010-10-13 03:15:28 +00005575 type_handled = objc_method_decl != NULL;
Sean Callanan60217122012-04-13 00:10:03 +00005576 GetClangASTContext().SetMetadata((uintptr_t)objc_method_decl, MakeUserID(die->GetOffset()));
Greg Clayton24739922010-10-13 03:15:28 +00005577 }
5578 }
Greg Clayton5113dc82011-08-12 06:47:54 +00005579 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00005580 {
5581 // Look at the parent of this DIE and see if is is
5582 // a class or struct and see if this is actually a
5583 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00005584 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00005585 if (class_type)
5586 {
Greg Clayton72da3972011-08-16 18:40:23 +00005587 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00005588 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005589 // We have a specification which we are going to base our function
5590 // prototype off of, so we need this type to be completed so that the
5591 // m_die_to_decl_ctx for the method in the specification has a valid
5592 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005593 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00005594 // If we have a specification, then the function type should have been
5595 // made with the specification and not with this die.
5596 DWARFCompileUnitSP spec_cu_sp;
5597 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005598 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005599 if (spec_clang_decl_ctx)
5600 {
5601 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
5602 }
5603 else
Jim Inghamc1663042011-09-29 22:12:35 +00005604 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005605 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
5606 MakeUserID(die->GetOffset()),
5607 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005608 }
Greg Clayton72da3972011-08-16 18:40:23 +00005609 type_handled = true;
5610 }
5611 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
5612 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005613 // We have a specification which we are going to base our function
5614 // prototype off of, so we need this type to be completed so that the
5615 // m_die_to_decl_ctx for the method in the abstract origin has a valid
5616 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005617 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00005618
Greg Clayton72da3972011-08-16 18:40:23 +00005619 DWARFCompileUnitSP abs_cu_sp;
5620 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005621 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005622 if (abs_clang_decl_ctx)
5623 {
5624 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
5625 }
5626 else
Jim Inghamc1663042011-09-29 22:12:35 +00005627 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005628 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
5629 MakeUserID(die->GetOffset()),
5630 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005631 }
Greg Clayton72da3972011-08-16 18:40:23 +00005632 type_handled = true;
5633 }
5634 else
5635 {
5636 clang_type_t class_opaque_type = class_type->GetClangForwardType();
5637 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00005638 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005639 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00005640 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005641 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
5642 // in the DWARF for C++ methods... Default to public for now...
5643 if (accessibility == eAccessNone)
5644 accessibility = eAccessPublic;
5645
5646 if (!is_static && !die->HasChildren())
5647 {
5648 // We have a C++ member function with no children (this pointer!)
5649 // and clang will get mad if we try and make a function that isn't
5650 // well formed in the DWARF, so we will just skip it...
5651 type_handled = true;
5652 }
5653 else
5654 {
5655 clang::CXXMethodDecl *cxx_method_decl;
5656 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00005657 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 +00005658 type_name_cstr,
5659 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00005660 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00005661 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
5662 m_obj_file->GetFileSpec().GetFilename().GetCString());
5663
Sean Callananc1b732d2011-11-01 18:07:13 +00005664 const bool is_attr_used = false;
5665
Greg Clayton20568dd2011-10-13 23:13:20 +00005666 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
5667 type_name_cstr,
5668 clang_type,
5669 accessibility,
5670 is_virtual,
5671 is_static,
5672 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00005673 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00005674 is_attr_used,
5675 is_artificial);
Greg Clayton20568dd2011-10-13 23:13:20 +00005676 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
5677
Greg Claytonf49e65a2011-11-10 18:31:53 +00005678 Host::SetCrashDescription (NULL);
5679
Greg Clayton20568dd2011-10-13 23:13:20 +00005680 type_handled = cxx_method_decl != NULL;
Sean Callanan60217122012-04-13 00:10:03 +00005681
5682 GetClangASTContext().SetMetadata((uintptr_t)cxx_method_decl, MakeUserID(die->GetOffset()));
Greg Clayton20568dd2011-10-13 23:13:20 +00005683 }
Greg Clayton72da3972011-08-16 18:40:23 +00005684 }
5685 else
5686 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005687 // We were asked to parse the type for a method in a class, yet the
5688 // class hasn't been asked to complete itself through the
5689 // clang::ExternalASTSource protocol, so we need to just have the
5690 // class complete itself and do things the right way, then our
5691 // DIE should then have an entry in the m_die_to_type map. First
5692 // we need to modify the m_die_to_type so it doesn't think we are
5693 // trying to parse this DIE anymore...
5694 m_die_to_type[die] = NULL;
5695
5696 // Now we get the full type to force our class type to complete itself
5697 // using the clang::ExternalASTSource protocol which will parse all
5698 // base classes and all methods (including the method for this DIE).
5699 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005700
Greg Clayton20568dd2011-10-13 23:13:20 +00005701 // The type for this DIE should have been filled in the function call above
5702 type_ptr = m_die_to_type[die];
5703 if (type_ptr)
5704 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00005705 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00005706 break;
5707 }
Sean Callanan02eee4d2012-04-12 23:10:00 +00005708
5709 // FIXME This is fixing some even uglier behavior but we really need to
5710 // uniq the methods of each class as well as the class itself.
5711 // <rdar://problem/11240464>
5712 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00005713 }
Greg Clayton931180e2011-01-27 06:44:37 +00005714 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005715 }
5716 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005717 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005718 }
Greg Clayton24739922010-10-13 03:15:28 +00005719
5720 if (!type_handled)
5721 {
5722 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00005723 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
5724 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005725 clang_type,
5726 storage,
5727 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005728
5729// if (template_param_infos.GetSize() > 0)
5730// {
5731// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
5732// function_decl,
5733// type_name_cstr,
5734// template_param_infos);
5735//
5736// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
5737// func_template_decl,
5738// template_param_infos);
5739// }
Greg Clayton24739922010-10-13 03:15:28 +00005740 // Add the decl to our DIE to decl context map
5741 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00005742 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00005743 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005744 ast.SetFunctionParameters (function_decl,
5745 &function_param_decls.front(),
5746 function_param_decls.size());
Sean Callanan60217122012-04-13 00:10:03 +00005747
5748 GetClangASTContext().SetMetadata((uintptr_t)function_decl, MakeUserID(die->GetOffset()));
Greg Clayton24739922010-10-13 03:15:28 +00005749 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005750 }
Greg Clayton81c22f62011-10-19 18:09:39 +00005751 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005752 this,
5753 type_name_const_str,
5754 0,
5755 NULL,
5756 LLDB_INVALID_UID,
5757 Type::eEncodingIsUID,
5758 &decl,
5759 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005760 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00005761 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005762 }
5763 break;
5764
5765 case DW_TAG_array_type:
5766 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005767 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005768 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005769
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005770 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005771 int64_t first_index = 0;
5772 uint32_t byte_stride = 0;
5773 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00005774 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005775
5776 if (num_attributes > 0)
5777 {
5778 uint32_t i;
5779 for (i=0; i<num_attributes; ++i)
5780 {
5781 attr = attributes.AttributeAtIndex(i);
5782 DWARFFormValue form_value;
5783 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5784 {
5785 switch (attr)
5786 {
5787 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5788 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5789 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5790 case DW_AT_name:
5791 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005792 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005793 break;
5794
5795 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005796 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005797 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
5798 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005799 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005800 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005801 case DW_AT_allocated:
5802 case DW_AT_associated:
5803 case DW_AT_data_location:
5804 case DW_AT_description:
5805 case DW_AT_ordering:
5806 case DW_AT_start_scope:
5807 case DW_AT_visibility:
5808 case DW_AT_specification:
5809 case DW_AT_abstract_origin:
5810 case DW_AT_sibling:
5811 break;
5812 }
5813 }
5814 }
5815
Greg Clayton81c22f62011-10-19 18:09:39 +00005816 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 +00005817
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005818 Type *element_type = ResolveTypeUID(type_die_offset);
5819
5820 if (element_type)
5821 {
5822 std::vector<uint64_t> element_orders;
5823 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00005824 // We have an array that claims to have no members, lets give it at least one member...
5825 if (element_orders.empty())
5826 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005827 if (byte_stride == 0 && bit_stride == 0)
5828 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00005829 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005830 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
5831 uint64_t num_elements = 0;
5832 std::vector<uint64_t>::const_reverse_iterator pos;
5833 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
5834 for (pos = element_orders.rbegin(); pos != end; ++pos)
5835 {
5836 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005837 clang_type = ast.CreateArrayType (array_element_type,
5838 num_elements,
5839 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005840 array_element_type = clang_type;
5841 array_element_bit_stride = array_element_bit_stride * num_elements;
5842 }
5843 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00005844 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005845 this,
5846 empty_name,
5847 array_element_bit_stride / 8,
5848 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00005849 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005850 Type::eEncodingIsUID,
5851 &decl,
5852 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005853 Type::eResolveStateFull));
5854 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005855 }
5856 }
5857 }
5858 break;
5859
Greg Clayton9b81a312010-06-12 01:20:30 +00005860 case DW_TAG_ptr_to_member_type:
5861 {
5862 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
5863 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
5864
Greg Claytond88d7592010-09-15 08:33:30 +00005865 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00005866
5867 if (num_attributes > 0) {
5868 uint32_t i;
5869 for (i=0; i<num_attributes; ++i)
5870 {
5871 attr = attributes.AttributeAtIndex(i);
5872 DWARFFormValue form_value;
5873 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5874 {
5875 switch (attr)
5876 {
5877 case DW_AT_type:
5878 type_die_offset = form_value.Reference(dwarf_cu); break;
5879 case DW_AT_containing_type:
5880 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
5881 }
5882 }
5883 }
5884
5885 Type *pointee_type = ResolveTypeUID(type_die_offset);
5886 Type *class_type = ResolveTypeUID(containing_type_die_offset);
5887
Greg Clayton526e5af2010-11-13 03:52:47 +00005888 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
5889 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00005890
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005891 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
5892 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00005893
Greg Clayton526e5af2010-11-13 03:52:47 +00005894 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
5895 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00005896
Greg Clayton81c22f62011-10-19 18:09:39 +00005897 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005898 this,
5899 type_name_const_str,
5900 byte_size,
5901 NULL,
5902 LLDB_INVALID_UID,
5903 Type::eEncodingIsUID,
5904 NULL,
5905 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005906 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00005907 }
5908
5909 break;
5910 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005911 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00005912 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005913 break;
5914 }
5915
5916 if (type_sp.get())
5917 {
5918 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5919 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
5920
5921 SymbolContextScope * symbol_context_scope = NULL;
5922 if (sc_parent_tag == DW_TAG_compile_unit)
5923 {
5924 symbol_context_scope = sc.comp_unit;
5925 }
5926 else if (sc.function != NULL)
5927 {
Greg Clayton81c22f62011-10-19 18:09:39 +00005928 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005929 if (symbol_context_scope == NULL)
5930 symbol_context_scope = sc.function;
5931 }
5932
5933 if (symbol_context_scope != NULL)
5934 {
5935 type_sp->SetSymbolContextScope(symbol_context_scope);
5936 }
5937
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005938 // We are ready to put this type into the uniqued list up at the module level
5939 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00005940
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005941 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005942 }
5943 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00005944 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005945 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00005946 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005947 }
5948 }
5949 return type_sp;
5950}
5951
5952size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00005953SymbolFileDWARF::ParseTypes
5954(
5955 const SymbolContext& sc,
5956 DWARFCompileUnit* dwarf_cu,
5957 const DWARFDebugInfoEntry *die,
5958 bool parse_siblings,
5959 bool parse_children
5960)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005961{
5962 size_t types_added = 0;
5963 while (die != NULL)
5964 {
5965 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005966 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005967 {
5968 if (type_is_new)
5969 ++types_added;
5970 }
5971
5972 if (parse_children && die->HasChildren())
5973 {
5974 if (die->Tag() == DW_TAG_subprogram)
5975 {
5976 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00005977 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005978 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
5979 }
5980 else
5981 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
5982 }
5983
5984 if (parse_siblings)
5985 die = die->GetSibling();
5986 else
5987 die = NULL;
5988 }
5989 return types_added;
5990}
5991
5992
5993size_t
5994SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
5995{
5996 assert(sc.comp_unit && sc.function);
5997 size_t functions_added = 0;
5998 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5999 if (dwarf_cu)
6000 {
6001 dw_offset_t function_die_offset = sc.function->GetID();
6002 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
6003 if (function_die)
6004 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00006005 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006006 }
6007 }
6008
6009 return functions_added;
6010}
6011
6012
6013size_t
6014SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
6015{
6016 // At least a compile unit must be valid
6017 assert(sc.comp_unit);
6018 size_t types_added = 0;
6019 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
6020 if (dwarf_cu)
6021 {
6022 if (sc.function)
6023 {
6024 dw_offset_t function_die_offset = sc.function->GetID();
6025 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
6026 if (func_die && func_die->HasChildren())
6027 {
6028 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
6029 }
6030 }
6031 else
6032 {
6033 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
6034 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
6035 {
6036 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
6037 }
6038 }
6039 }
6040
6041 return types_added;
6042}
6043
6044size_t
6045SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
6046{
6047 if (sc.comp_unit != NULL)
6048 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00006049 DWARFDebugInfo* info = DebugInfo();
6050 if (info == NULL)
6051 return 0;
6052
6053 uint32_t cu_idx = UINT32_MAX;
6054 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006055
6056 if (dwarf_cu == NULL)
6057 return 0;
6058
6059 if (sc.function)
6060 {
6061 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00006062
6063 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 +00006064 if (func_lo_pc != DW_INVALID_ADDRESS)
6065 {
6066 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00006067
Greg Claytone38a5ed2012-01-05 03:57:59 +00006068 // Let all blocks know they have parse all their variables
6069 sc.function->GetBlock (false).SetDidParseVariables (true, true);
6070 return num_variables;
6071 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006072 }
6073 else if (sc.comp_unit)
6074 {
6075 uint32_t vars_added = 0;
6076 VariableListSP variables (sc.comp_unit->GetVariableList(false));
6077
6078 if (variables.get() == NULL)
6079 {
6080 variables.reset(new VariableList());
6081 sc.comp_unit->SetVariableList(variables);
6082
Greg Claytond4a2b372011-09-12 23:21:58 +00006083 DWARFCompileUnit* match_dwarf_cu = NULL;
6084 const DWARFDebugInfoEntry* die = NULL;
6085 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00006086 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00006087 {
Greg Clayton97fbc342011-10-20 22:30:33 +00006088 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00006089 {
6090 DWARFMappedHash::DIEInfoArray hash_data_array;
6091 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
6092 dwarf_cu->GetNextCompileUnitOffset(),
6093 hash_data_array))
6094 {
6095 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
6096 }
6097 }
Greg Clayton7f995132011-10-04 22:41:51 +00006098 }
6099 else
6100 {
6101 // Index if we already haven't to make sure the compile units
6102 // get indexed and make their global DIE index list
6103 if (!m_indexed)
6104 Index ();
6105
6106 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
6107 dwarf_cu->GetNextCompileUnitOffset(),
6108 die_offsets);
6109 }
6110
6111 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00006112 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006113 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006114 DWARFDebugInfo* debug_info = DebugInfo();
6115 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006116 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006117 const dw_offset_t die_offset = die_offsets[i];
6118 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00006119 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00006120 {
Greg Clayton95d87902011-11-11 03:16:25 +00006121 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
6122 if (var_sp)
6123 {
6124 variables->AddVariableIfUnique (var_sp);
6125 ++vars_added;
6126 }
Greg Claytond4a2b372011-09-12 23:21:58 +00006127 }
Greg Clayton95d87902011-11-11 03:16:25 +00006128 else
6129 {
6130 if (m_using_apple_tables)
6131 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006132 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 +00006133 }
6134 }
6135
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006136 }
6137 }
6138 }
6139 return vars_added;
6140 }
6141 }
6142 return 0;
6143}
6144
6145
6146VariableSP
6147SymbolFileDWARF::ParseVariableDIE
6148(
6149 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006150 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006151 const DWARFDebugInfoEntry *die,
6152 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006153)
6154{
6155
Greg Clayton83c5cd92010-11-14 22:13:40 +00006156 VariableSP var_sp (m_die_to_variable_sp[die]);
6157 if (var_sp)
6158 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006159
6160 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00006161
6162 if ((tag == DW_TAG_variable) ||
6163 (tag == DW_TAG_constant) ||
6164 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006165 {
Greg Clayton7f995132011-10-04 22:41:51 +00006166 DWARFDebugInfoEntry::Attributes attributes;
6167 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
6168 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006169 {
Greg Clayton7f995132011-10-04 22:41:51 +00006170 const char *name = NULL;
6171 const char *mangled = NULL;
6172 Declaration decl;
6173 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00006174 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00006175 DWARFExpression location;
6176 bool is_external = false;
6177 bool is_artificial = false;
6178 bool location_is_const_value_data = false;
6179 AccessType accessibility = eAccessNone;
6180
6181 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006182 {
Greg Clayton7f995132011-10-04 22:41:51 +00006183 dw_attr_t attr = attributes.AttributeAtIndex(i);
6184 DWARFFormValue form_value;
6185 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006186 {
Greg Clayton7f995132011-10-04 22:41:51 +00006187 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006188 {
Greg Clayton7f995132011-10-04 22:41:51 +00006189 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6190 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6191 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6192 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
6193 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00006194 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00006195 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
6196 case DW_AT_const_value:
6197 location_is_const_value_data = true;
6198 // Fall through...
6199 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006200 {
Greg Clayton7f995132011-10-04 22:41:51 +00006201 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006202 {
Greg Clayton7f995132011-10-04 22:41:51 +00006203 const DataExtractor& debug_info_data = get_debug_info_data();
6204
6205 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
6206 uint32_t block_length = form_value.Unsigned();
6207 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
6208 }
6209 else
6210 {
6211 const DataExtractor& debug_loc_data = get_debug_loc_data();
6212 const dw_offset_t debug_loc_offset = form_value.Unsigned();
6213
6214 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
6215 if (loc_list_length > 0)
6216 {
6217 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
6218 assert (func_low_pc != LLDB_INVALID_ADDRESS);
6219 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
6220 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006221 }
6222 }
Greg Clayton7f995132011-10-04 22:41:51 +00006223 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006224
Greg Clayton7f995132011-10-04 22:41:51 +00006225 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
6226 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
6227 case DW_AT_declaration:
6228 case DW_AT_description:
6229 case DW_AT_endianity:
6230 case DW_AT_segment:
6231 case DW_AT_start_scope:
6232 case DW_AT_visibility:
6233 default:
6234 case DW_AT_abstract_origin:
6235 case DW_AT_sibling:
6236 case DW_AT_specification:
6237 break;
6238 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006239 }
6240 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006241
Greg Clayton7f995132011-10-04 22:41:51 +00006242 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006243 {
Greg Clayton7f995132011-10-04 22:41:51 +00006244 ValueType scope = eValueTypeInvalid;
6245
6246 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6247 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006248 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00006249
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006250 // DWARF doesn't specify if a DW_TAG_variable is a local, global
6251 // or static variable, so we have to do a little digging by
6252 // looking at the location of a varaible to see if it contains
6253 // a DW_OP_addr opcode _somewhere_ in the definition. I say
6254 // somewhere because clang likes to combine small global variables
6255 // into the same symbol and have locations like:
6256 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
6257 // So if we don't have a DW_TAG_formal_parameter, we can look at
6258 // the location to see if it contains a DW_OP_addr opcode, and
6259 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00006260 if (tag == DW_TAG_formal_parameter)
6261 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00006262 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006263 {
Greg Clayton96c09682012-01-04 22:56:43 +00006264 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00006265 // Check if the location has a DW_OP_addr with any address value...
6266 addr_t location_has_op_addr = false;
6267 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00006268 {
6269 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
6270 if (op_error)
6271 {
6272 StreamString strm;
6273 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006274 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 +00006275 }
6276 }
Greg Claytond1767f02011-12-08 02:13:16 +00006277
6278 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006279 {
Greg Claytond1767f02011-12-08 02:13:16 +00006280 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006281 {
Greg Claytond1767f02011-12-08 02:13:16 +00006282 scope = eValueTypeVariableGlobal;
6283
6284 if (m_debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006285 {
Greg Claytond1767f02011-12-08 02:13:16 +00006286 // When leaving the DWARF in the .o files on darwin,
6287 // when we have a global variable that wasn't initialized,
6288 // the .o file might not have allocated a virtual
6289 // address for the global variable. In this case it will
6290 // have created a symbol for the global variable
6291 // that is undefined and external and the value will
6292 // be the byte size of the variable. When we do the
6293 // address map in SymbolFileDWARFDebugMap we rely on
6294 // having an address, we need to do some magic here
6295 // so we can get the correct address for our global
6296 // variable. The address for all of these entries
6297 // will be zero, and there will be an undefined symbol
6298 // in this object file, and the executable will have
6299 // a matching symbol with a good address. So here we
6300 // dig up the correct address and replace it in the
6301 // location for the variable, and set the variable's
6302 // symbol context scope to be that of the main executable
6303 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00006304 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006305 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006306
Greg Claytond1767f02011-12-08 02:13:16 +00006307 // we have a possible uninitialized extern global
6308 Symtab *symtab = m_obj_file->GetSymtab();
6309 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006310 {
Greg Claytond1767f02011-12-08 02:13:16 +00006311 ConstString const_name(name);
6312 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
6313 eSymbolTypeUndefined,
6314 Symtab::eDebugNo,
6315 Symtab::eVisibilityExtern);
6316
6317 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006318 {
Greg Claytond1767f02011-12-08 02:13:16 +00006319 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
6320 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006321 {
Greg Claytond1767f02011-12-08 02:13:16 +00006322 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
6323 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
6324 eSymbolTypeData,
6325 Symtab::eDebugYes,
6326 Symtab::eVisibilityExtern);
6327 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006328 {
Greg Claytone7612132012-03-07 21:03:09 +00006329 if (defined_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006330 {
Greg Claytone7612132012-03-07 21:03:09 +00006331 const addr_t defined_addr = defined_symbol->GetAddress().GetFileAddress();
Greg Claytond1767f02011-12-08 02:13:16 +00006332 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006333 {
Greg Claytond1767f02011-12-08 02:13:16 +00006334 if (location.Update_DW_OP_addr (defined_addr))
6335 {
6336 symbol_context_scope = defined_symbol;
6337 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006338 }
6339 }
6340 }
6341 }
6342 }
6343 }
6344 }
6345 }
6346 }
Greg Claytond1767f02011-12-08 02:13:16 +00006347 else
6348 {
6349 scope = eValueTypeVariableStatic;
6350 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006351 }
6352 else
Greg Claytond1767f02011-12-08 02:13:16 +00006353 {
6354 scope = eValueTypeVariableLocal;
6355 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006356 }
Greg Clayton7f995132011-10-04 22:41:51 +00006357
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006358 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00006359 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006360 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00006361 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006362 case DW_TAG_subprogram:
6363 case DW_TAG_inlined_subroutine:
6364 case DW_TAG_lexical_block:
6365 if (sc.function)
6366 {
6367 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
6368 if (symbol_context_scope == NULL)
6369 symbol_context_scope = sc.function;
6370 }
6371 break;
6372
6373 default:
6374 symbol_context_scope = sc.comp_unit;
6375 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00006376 }
Greg Clayton7f995132011-10-04 22:41:51 +00006377 }
6378
Greg Clayton5cf58b92011-10-05 22:22:08 +00006379 if (symbol_context_scope)
6380 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006381 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
6382 name,
6383 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00006384 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00006385 scope,
6386 symbol_context_scope,
6387 &decl,
6388 location,
6389 is_external,
6390 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00006391
6392 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
6393 }
6394 else
6395 {
6396 // Not ready to parse this variable yet. It might be a global
6397 // or static variable that is in a function scope and the function
6398 // in the symbol context wasn't filled in yet
6399 return var_sp;
6400 }
Greg Clayton7f995132011-10-04 22:41:51 +00006401 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006402 }
Greg Clayton7f995132011-10-04 22:41:51 +00006403 // Cache var_sp even if NULL (the variable was just a specification or
6404 // was missing vital information to be able to be displayed in the debugger
6405 // (missing location due to optimization, etc)) so we don't re-parse
6406 // this DIE over and over later...
6407 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006408 }
6409 return var_sp;
6410}
6411
Greg Claytonc662ec82011-06-17 22:10:16 +00006412
6413const DWARFDebugInfoEntry *
6414SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
6415 dw_offset_t spec_block_die_offset,
6416 DWARFCompileUnit **result_die_cu_handle)
6417{
6418 // Give the concrete function die specified by "func_die_offset", find the
6419 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6420 // to "spec_block_die_offset"
6421 DWARFDebugInfo* info = DebugInfo();
6422
6423 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
6424 if (die)
6425 {
6426 assert (*result_die_cu_handle);
6427 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
6428 }
6429 return NULL;
6430}
6431
6432
6433const DWARFDebugInfoEntry *
6434SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
6435 const DWARFDebugInfoEntry *die,
6436 dw_offset_t spec_block_die_offset,
6437 DWARFCompileUnit **result_die_cu_handle)
6438{
6439 if (die)
6440 {
6441 switch (die->Tag())
6442 {
6443 case DW_TAG_subprogram:
6444 case DW_TAG_inlined_subroutine:
6445 case DW_TAG_lexical_block:
6446 {
6447 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
6448 {
6449 *result_die_cu_handle = dwarf_cu;
6450 return die;
6451 }
6452
6453 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
6454 {
6455 *result_die_cu_handle = dwarf_cu;
6456 return die;
6457 }
6458 }
6459 break;
6460 }
6461
6462 // Give the concrete function die specified by "func_die_offset", find the
6463 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6464 // to "spec_block_die_offset"
6465 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
6466 {
6467 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
6468 child_die,
6469 spec_block_die_offset,
6470 result_die_cu_handle);
6471 if (result_die)
6472 return result_die;
6473 }
6474 }
6475
6476 *result_die_cu_handle = NULL;
6477 return NULL;
6478}
6479
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006480size_t
6481SymbolFileDWARF::ParseVariables
6482(
6483 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006484 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006485 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006486 const DWARFDebugInfoEntry *orig_die,
6487 bool parse_siblings,
6488 bool parse_children,
6489 VariableList* cc_variable_list
6490)
6491{
6492 if (orig_die == NULL)
6493 return 0;
6494
Greg Claytonc662ec82011-06-17 22:10:16 +00006495 VariableListSP variable_list_sp;
6496
6497 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006498 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00006499 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006500 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006501 dw_tag_t tag = die->Tag();
6502
6503 // Check to see if we have already parsed this variable or constant?
6504 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006505 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006506 if (cc_variable_list)
6507 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006508 }
6509 else
6510 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006511 // We haven't already parsed it, lets do that now.
6512 if ((tag == DW_TAG_variable) ||
6513 (tag == DW_TAG_constant) ||
6514 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006515 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006516 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00006517 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006518 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
6519 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6520 switch (parent_tag)
6521 {
6522 case DW_TAG_compile_unit:
6523 if (sc.comp_unit != NULL)
6524 {
6525 variable_list_sp = sc.comp_unit->GetVariableList(false);
6526 if (variable_list_sp.get() == NULL)
6527 {
6528 variable_list_sp.reset(new VariableList());
6529 sc.comp_unit->SetVariableList(variable_list_sp);
6530 }
6531 }
6532 else
6533 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006534 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
6535 MakeUserID(sc_parent_die->GetOffset()),
6536 DW_TAG_value_to_name (parent_tag),
6537 MakeUserID(orig_die->GetOffset()),
6538 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006539 }
6540 break;
6541
6542 case DW_TAG_subprogram:
6543 case DW_TAG_inlined_subroutine:
6544 case DW_TAG_lexical_block:
6545 if (sc.function != NULL)
6546 {
6547 // Check to see if we already have parsed the variables for the given scope
6548
Greg Clayton81c22f62011-10-19 18:09:39 +00006549 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006550 if (block == NULL)
6551 {
6552 // This must be a specification or abstract origin with
6553 // a concrete block couterpart in the current function. We need
6554 // to find the concrete block so we can correctly add the
6555 // variable to it
6556 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
6557 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
6558 sc_parent_die->GetOffset(),
6559 &concrete_block_die_cu);
6560 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00006561 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006562 }
6563
6564 if (block != NULL)
6565 {
6566 const bool can_create = false;
6567 variable_list_sp = block->GetBlockVariableList (can_create);
6568 if (variable_list_sp.get() == NULL)
6569 {
6570 variable_list_sp.reset(new VariableList());
6571 block->SetVariableList(variable_list_sp);
6572 }
6573 }
6574 }
6575 break;
6576
6577 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00006578 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
6579 MakeUserID(orig_die->GetOffset()),
6580 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006581 break;
6582 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00006583 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006584
6585 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006586 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00006587 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
6588 if (var_sp)
6589 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006590 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00006591 if (cc_variable_list)
6592 cc_variable_list->AddVariableIfUnique (var_sp);
6593 ++vars_added;
6594 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006595 }
6596 }
6597 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006598
6599 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
6600
6601 if (!skip_children && parse_children && die->HasChildren())
6602 {
6603 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
6604 }
6605
6606 if (parse_siblings)
6607 die = die->GetSibling();
6608 else
6609 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006610 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006611 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006612}
6613
6614//------------------------------------------------------------------
6615// PluginInterface protocol
6616//------------------------------------------------------------------
6617const char *
6618SymbolFileDWARF::GetPluginName()
6619{
6620 return "SymbolFileDWARF";
6621}
6622
6623const char *
6624SymbolFileDWARF::GetShortPluginName()
6625{
6626 return GetPluginNameStatic();
6627}
6628
6629uint32_t
6630SymbolFileDWARF::GetPluginVersion()
6631{
6632 return 1;
6633}
6634
6635void
Greg Clayton6beaaa62011-01-17 03:46:26 +00006636SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
6637{
6638 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6639 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6640 if (clang_type)
6641 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6642}
6643
6644void
6645SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
6646{
6647 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6648 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6649 if (clang_type)
6650 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6651}
6652
Greg Claytona2721472011-06-25 00:44:06 +00006653void
Sean Callanancc427fa2011-07-30 02:42:06 +00006654SymbolFileDWARF::DumpIndexes ()
6655{
6656 StreamFile s(stdout, false);
6657
6658 s.Printf ("DWARF index for (%s) '%s/%s':",
6659 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
6660 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
6661 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
6662 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
6663 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
6664 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
6665 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
6666 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
6667 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
6668 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
6669 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
6670}
6671
6672void
6673SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
6674 const char *name,
6675 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00006676{
Sean Callanancc427fa2011-07-30 02:42:06 +00006677 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00006678
6679 if (iter == m_decl_ctx_to_die.end())
6680 return;
6681
Greg Claytoncb5860a2011-10-13 23:49:28 +00006682 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00006683 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006684 const DWARFDebugInfoEntry *context_die = *pos;
6685
6686 if (!results)
6687 return;
6688
6689 DWARFDebugInfo* info = DebugInfo();
6690
6691 DIEArray die_offsets;
6692
6693 DWARFCompileUnit* dwarf_cu = NULL;
6694 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00006695
6696 if (m_using_apple_tables)
6697 {
6698 if (m_apple_types_ap.get())
6699 m_apple_types_ap->FindByName (name, die_offsets);
6700 }
6701 else
6702 {
6703 if (!m_indexed)
6704 Index ();
6705
6706 m_type_index.Find (ConstString(name), die_offsets);
6707 }
6708
Greg Clayton220a0072011-12-09 08:48:30 +00006709 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006710
6711 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00006712 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006713 for (size_t i = 0; i < num_matches; ++i)
6714 {
6715 const dw_offset_t die_offset = die_offsets[i];
6716 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00006717
Greg Claytoncb5860a2011-10-13 23:49:28 +00006718 if (die->GetParent() != context_die)
6719 continue;
6720
6721 Type *matching_type = ResolveType (dwarf_cu, die);
6722
Greg Clayton42ce2f32011-12-12 21:50:19 +00006723 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006724 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
6725
6726 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
6727 {
6728 clang::TagDecl *tag_decl = tag_type->getDecl();
6729 results->push_back(tag_decl);
6730 }
6731 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
6732 {
6733 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
6734 results->push_back(typedef_decl);
6735 }
Greg Claytona2721472011-06-25 00:44:06 +00006736 }
6737 }
6738 }
6739}
6740
6741void
6742SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00006743 const clang::DeclContext *decl_context,
6744 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00006745 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
6746{
Greg Clayton85ae2e12011-10-18 23:36:41 +00006747
6748 switch (decl_context->getDeclKind())
6749 {
6750 case clang::Decl::Namespace:
6751 case clang::Decl::TranslationUnit:
6752 {
6753 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6754 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
6755 }
6756 break;
6757 default:
6758 break;
6759 }
Greg Claytona2721472011-06-25 00:44:06 +00006760}
Greg Claytoncaab74e2012-01-28 00:48:57 +00006761
6762bool
6763SymbolFileDWARF::LayoutRecordType (void *baton,
6764 const clang::RecordDecl *record_decl,
6765 uint64_t &size,
6766 uint64_t &alignment,
6767 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6768 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6769 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6770{
6771 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6772 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
6773}
6774
6775
6776bool
6777SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
6778 uint64_t &bit_size,
6779 uint64_t &alignment,
6780 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6781 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6782 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6783{
6784 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
6785 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
6786 bool success = false;
6787 base_offsets.clear();
6788 vbase_offsets.clear();
6789 if (pos != m_record_decl_to_layout_map.end())
6790 {
6791 bit_size = pos->second.bit_size;
6792 alignment = pos->second.alignment;
6793 field_offsets.swap(pos->second.field_offsets);
6794 m_record_decl_to_layout_map.erase(pos);
6795 success = true;
6796 }
6797 else
6798 {
6799 bit_size = 0;
6800 alignment = 0;
6801 field_offsets.clear();
6802 }
6803
6804 if (log)
6805 GetObjectFile()->GetModule()->LogMessage (log.get(),
6806 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
6807 record_decl,
6808 bit_size,
6809 alignment,
6810 (uint32_t)field_offsets.size(),
6811 (uint32_t)base_offsets.size(),
6812 (uint32_t)vbase_offsets.size(),
6813 success);
6814 return success;
6815}
6816
6817
6818