blob: 80c98b03f39524b3ed29934c47c42c5b7d136d2a [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Greg Clayton3c2e3ae2012-02-06 06:42:51 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "clang/Basic/Builtins.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000025#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanancc427fa2011-07-30 02:42:06 +000027#include "llvm/Support/Casting.h"
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Module.h"
30#include "lldb/Core/PluginManager.h"
31#include "lldb/Core/RegularExpression.h"
32#include "lldb/Core/Scalar.h"
33#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000034#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000035#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Core/Timer.h"
37#include "lldb/Core/Value.h"
38
Greg Clayton20568dd2011-10-13 23:13:20 +000039#include "lldb/Host/Host.h"
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000042#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Symbol/CompileUnit.h"
44#include "lldb/Symbol/LineTable.h"
45#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
47#include "lldb/Symbol/VariableList.h"
48
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000049#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000050#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000051
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "DWARFCompileUnit.h"
53#include "DWARFDebugAbbrev.h"
54#include "DWARFDebugAranges.h"
55#include "DWARFDebugInfo.h"
56#include "DWARFDebugInfoEntry.h"
57#include "DWARFDebugLine.h"
58#include "DWARFDebugPubnames.h"
59#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000060#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFDIECollection.h"
62#include "DWARFFormValue.h"
63#include "DWARFLocationList.h"
64#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000065#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
67#include <map>
68
Greg Clayton62742b12010-11-11 01:09:45 +000069//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000070
71#ifdef ENABLE_DEBUG_PRINTF
72#include <stdio.h>
73#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
74#else
75#define DEBUG_PRINTF(fmt, ...)
76#endif
77
Greg Clayton594e5ed2010-09-27 21:07:38 +000078#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079
80using namespace lldb;
81using namespace lldb_private;
82
Greg Clayton219cf312012-03-30 00:51:13 +000083//static inline bool
84//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
85//{
86// switch (tag)
87// {
88// default:
89// break;
90// case DW_TAG_subprogram:
91// case DW_TAG_inlined_subroutine:
92// case DW_TAG_class_type:
93// case DW_TAG_structure_type:
94// case DW_TAG_union_type:
95// return true;
96// }
97// return false;
98//}
99//
Sean Callananc7fbf732010-08-06 00:32:49 +0000100static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000101DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102{
103 switch (dwarf_accessibility)
104 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000105 case DW_ACCESS_public: return eAccessPublic;
106 case DW_ACCESS_private: return eAccessPrivate;
107 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000108 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000110 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111}
112
Greg Clayton1fba87112012-02-09 20:03:49 +0000113#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
114
115class DIEStack
116{
117public:
118
119 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
120 {
121 m_dies.push_back (DIEInfo(cu, die));
122 }
123
124
125 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
126 {
127 StreamString log_strm;
128 const size_t n = m_dies.size();
Greg Clayton43e0af02012-09-18 18:04:04 +0000129 log_strm.Printf("DIEStack[%llu]:\n", (uint64_t)n);
Greg Clayton1fba87112012-02-09 20:03:49 +0000130 for (size_t i=0; i<n; i++)
131 {
132 DWARFCompileUnit *cu = m_dies[i].cu;
133 const DWARFDebugInfoEntry *die = m_dies[i].die;
134 std::string qualified_name;
135 die->GetQualifiedName(dwarf, cu, qualified_name);
Greg Clayton43e0af02012-09-18 18:04:04 +0000136 log_strm.Printf ("[%llu] 0x%8.8x: %s name='%s'\n",
137 (uint64_t)i,
Greg Clayton1fba87112012-02-09 20:03:49 +0000138 die->GetOffset(),
139 DW_TAG_value_to_name(die->Tag()),
140 qualified_name.c_str());
141 }
142 log->PutCString(log_strm.GetData());
143 }
144 void Pop ()
145 {
146 m_dies.pop_back();
147 }
148
149 class ScopedPopper
150 {
151 public:
152 ScopedPopper (DIEStack &die_stack) :
153 m_die_stack (die_stack),
154 m_valid (false)
155 {
156 }
157
158 void
159 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
160 {
161 m_valid = true;
162 m_die_stack.Push (cu, die);
163 }
164
165 ~ScopedPopper ()
166 {
167 if (m_valid)
168 m_die_stack.Pop();
169 }
170
171
172
173 protected:
174 DIEStack &m_die_stack;
175 bool m_valid;
176 };
177
178protected:
179 struct DIEInfo {
180 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
181 cu(c),
182 die(d)
183 {
184 }
185 DWARFCompileUnit *cu;
186 const DWARFDebugInfoEntry *die;
187 };
188 typedef std::vector<DIEInfo> Stack;
189 Stack m_dies;
190};
191#endif
192
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193void
194SymbolFileDWARF::Initialize()
195{
196 LogChannelDWARF::Initialize();
197 PluginManager::RegisterPlugin (GetPluginNameStatic(),
198 GetPluginDescriptionStatic(),
199 CreateInstance);
200}
201
202void
203SymbolFileDWARF::Terminate()
204{
205 PluginManager::UnregisterPlugin (CreateInstance);
206 LogChannelDWARF::Initialize();
207}
208
209
210const char *
211SymbolFileDWARF::GetPluginNameStatic()
212{
Greg Claytond4a2b372011-09-12 23:21:58 +0000213 return "dwarf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214}
215
216const char *
217SymbolFileDWARF::GetPluginDescriptionStatic()
218{
219 return "DWARF and DWARF3 debug symbol file reader.";
220}
221
222
223SymbolFile*
224SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
225{
226 return new SymbolFileDWARF(obj_file);
227}
228
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000229TypeList *
230SymbolFileDWARF::GetTypeList ()
231{
Greg Clayton1f746072012-08-29 21:13:06 +0000232 if (GetDebugMapSymfile ())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000233 return m_debug_map_symfile->GetTypeList();
234 return m_obj_file->GetModule()->GetTypeList();
235
236}
237
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238//----------------------------------------------------------------------
239// Gets the first parent that is a lexical block, function or inlined
240// subroutine, or compile unit.
241//----------------------------------------------------------------------
242static const DWARFDebugInfoEntry *
243GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
244{
245 const DWARFDebugInfoEntry *die;
246 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
247 {
248 dw_tag_t tag = die->Tag();
249
250 switch (tag)
251 {
252 case DW_TAG_compile_unit:
253 case DW_TAG_subprogram:
254 case DW_TAG_inlined_subroutine:
255 case DW_TAG_lexical_block:
256 return die;
257 }
258 }
259 return NULL;
260}
261
262
Greg Clayton450e3f32010-10-12 02:24:53 +0000263SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
264 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000265 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton1f746072012-08-29 21:13:06 +0000266 m_debug_map_module_wp (),
Greg Clayton450e3f32010-10-12 02:24:53 +0000267 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000268 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000270 m_data_debug_abbrev (),
271 m_data_debug_aranges (),
272 m_data_debug_frame (),
273 m_data_debug_info (),
274 m_data_debug_line (),
275 m_data_debug_loc (),
276 m_data_debug_ranges (),
277 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000278 m_data_apple_names (),
279 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000280 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 m_info(),
283 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000284 m_apple_names_ap (),
285 m_apple_types_ap (),
286 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000287 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000288 m_function_basename_index(),
289 m_function_fullname_index(),
290 m_function_method_index(),
291 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000292 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000293 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000294 m_type_index(),
295 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000296 m_indexed (false),
297 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000298 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000299 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000300 m_ranges(),
301 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302{
303}
304
305SymbolFileDWARF::~SymbolFileDWARF()
306{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000307 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000308 {
309 ModuleSP module_sp (m_obj_file->GetModule());
310 if (module_sp)
311 module_sp->GetClangASTContext().RemoveExternalSource ();
312 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000313}
314
315static const ConstString &
316GetDWARFMachOSegmentName ()
317{
318 static ConstString g_dwarf_section_name ("__DWARF");
319 return g_dwarf_section_name;
320}
321
Greg Claytone576ab22011-02-15 00:19:15 +0000322UniqueDWARFASTTypeMap &
323SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
324{
Greg Clayton1f746072012-08-29 21:13:06 +0000325 if (GetDebugMapSymfile ())
Greg Claytone576ab22011-02-15 00:19:15 +0000326 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
327 return m_unique_ast_type_map;
328}
329
Greg Clayton6beaaa62011-01-17 03:46:26 +0000330ClangASTContext &
331SymbolFileDWARF::GetClangASTContext ()
332{
Greg Clayton1f746072012-08-29 21:13:06 +0000333 if (GetDebugMapSymfile ())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000334 return m_debug_map_symfile->GetClangASTContext ();
335
336 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
337 if (!m_is_external_ast_source)
338 {
339 m_is_external_ast_source = true;
340 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
341 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
342 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000343 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000344 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000345 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000346 ast.SetExternalSource (ast_source_ap);
347 }
348 return ast;
349}
350
351void
352SymbolFileDWARF::InitializeObject()
353{
354 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000355 ModuleSP module_sp (m_obj_file->GetModule());
356 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000357 {
358 const SectionList *section_list = m_obj_file->GetSectionList();
359
360 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
361
362 // Memory map the DWARF mach-o segment so we have everything mmap'ed
363 // to keep our heap memory usage down.
364 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000365 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000366 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000367 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000368 if (m_data_apple_names.GetByteSize() > 0)
369 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000370 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
371 if (m_apple_names_ap->IsValid())
372 m_using_apple_tables = true;
373 else
Greg Clayton7f995132011-10-04 22:41:51 +0000374 m_apple_names_ap.reset();
375 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000376 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000377 if (m_data_apple_types.GetByteSize() > 0)
378 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000379 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
380 if (m_apple_types_ap->IsValid())
381 m_using_apple_tables = true;
382 else
Greg Clayton7f995132011-10-04 22:41:51 +0000383 m_apple_types_ap.reset();
384 }
385
386 get_apple_namespaces_data();
387 if (m_data_apple_namespaces.GetByteSize() > 0)
388 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000389 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
390 if (m_apple_namespaces_ap->IsValid())
391 m_using_apple_tables = true;
392 else
Greg Clayton7f995132011-10-04 22:41:51 +0000393 m_apple_namespaces_ap.reset();
394 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000395
Greg Clayton5009f9d2011-10-27 17:55:14 +0000396 get_apple_objc_data();
397 if (m_data_apple_objc.GetByteSize() > 0)
398 {
399 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
400 if (m_apple_objc_ap->IsValid())
401 m_using_apple_tables = true;
402 else
403 m_apple_objc_ap.reset();
404 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405}
406
407bool
408SymbolFileDWARF::SupportedVersion(uint16_t version)
409{
410 return version == 2 || version == 3;
411}
412
413uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000414SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415{
416 uint32_t abilities = 0;
417 if (m_obj_file != NULL)
418 {
419 const Section* section = NULL;
420 const SectionList *section_list = m_obj_file->GetSectionList();
421 if (section_list == NULL)
422 return 0;
423
424 uint64_t debug_abbrev_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 uint64_t debug_info_file_size = 0;
426 uint64_t debug_line_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427
Greg Clayton6beaaa62011-01-17 03:46:26 +0000428 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429
430 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000431 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432
Greg Clayton4ceb9982010-07-21 22:54:26 +0000433 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 if (section != NULL)
435 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000436 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437
Greg Clayton4ceb9982010-07-21 22:54:26 +0000438 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000440 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 else
442 m_flags.Set (flagsGotDebugAbbrevData);
443
Greg Clayton4ceb9982010-07-21 22:54:26 +0000444 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000445 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 m_flags.Set (flagsGotDebugArangesData);
447
Greg Clayton4ceb9982010-07-21 22:54:26 +0000448 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000449 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 m_flags.Set (flagsGotDebugFrameData);
451
Greg Clayton4ceb9982010-07-21 22:54:26 +0000452 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000454 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 else
456 m_flags.Set (flagsGotDebugLineData);
457
Greg Clayton4ceb9982010-07-21 22:54:26 +0000458 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000459 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 m_flags.Set (flagsGotDebugLocData);
461
Greg Clayton4ceb9982010-07-21 22:54:26 +0000462 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000463 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 m_flags.Set (flagsGotDebugMacInfoData);
465
Greg Clayton4ceb9982010-07-21 22:54:26 +0000466 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000467 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468 m_flags.Set (flagsGotDebugPubNamesData);
469
Greg Clayton4ceb9982010-07-21 22:54:26 +0000470 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000471 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 m_flags.Set (flagsGotDebugPubTypesData);
473
Greg Clayton4ceb9982010-07-21 22:54:26 +0000474 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000475 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 m_flags.Set (flagsGotDebugRangesData);
477
Greg Clayton4ceb9982010-07-21 22:54:26 +0000478 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000479 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 m_flags.Set (flagsGotDebugStrData);
481 }
Greg Clayton6c596612012-05-18 21:47:20 +0000482 else
483 {
484 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString();
485 if (symfile_dir_cstr)
486 {
487 if (strcasestr(symfile_dir_cstr, ".dsym"))
488 {
489 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo)
490 {
491 // We have a dSYM file that didn't have a any debug info.
492 // If the string table has a size of 1, then it was made from
493 // an executable with no debug info, or from an executable that
494 // was stripped.
495 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
496 if (section && section->GetFileSize() == 1)
497 {
498 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info.");
499 }
500 }
501 }
502 }
503 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504
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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510 }
511 return abilities;
512}
513
514const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000515SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516{
517 if (m_flags.IsClear (got_flag))
518 {
519 m_flags.Set (got_flag);
520 const SectionList *section_list = m_obj_file->GetSectionList();
521 if (section_list)
522 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000523 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
524 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 {
526 // See if we memory mapped the DWARF segment?
527 if (m_dwarf_data.GetByteSize())
528 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000529 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 }
531 else
532 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000533 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534 data.Clear();
535 }
536 }
537 }
538 }
539 return data;
540}
541
542const DataExtractor&
543SymbolFileDWARF::get_debug_abbrev_data()
544{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000545 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546}
547
548const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000549SymbolFileDWARF::get_debug_aranges_data()
550{
551 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
552}
553
554const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555SymbolFileDWARF::get_debug_frame_data()
556{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000557 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558}
559
560const DataExtractor&
561SymbolFileDWARF::get_debug_info_data()
562{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000563 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564}
565
566const DataExtractor&
567SymbolFileDWARF::get_debug_line_data()
568{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000569 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570}
571
572const DataExtractor&
573SymbolFileDWARF::get_debug_loc_data()
574{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000575 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576}
577
578const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579SymbolFileDWARF::get_debug_ranges_data()
580{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000581 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582}
583
584const DataExtractor&
585SymbolFileDWARF::get_debug_str_data()
586{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000587 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588}
589
Greg Claytonf9eec202011-09-01 23:16:13 +0000590const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000591SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000592{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000593 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000594}
595
596const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000597SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000598{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000599 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000600}
601
Greg Clayton7f995132011-10-04 22:41:51 +0000602const DataExtractor&
603SymbolFileDWARF::get_apple_namespaces_data()
604{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000605 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
606}
607
608const DataExtractor&
609SymbolFileDWARF::get_apple_objc_data()
610{
611 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000612}
613
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614
615DWARFDebugAbbrev*
616SymbolFileDWARF::DebugAbbrev()
617{
618 if (m_abbr.get() == NULL)
619 {
620 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
621 if (debug_abbrev_data.GetByteSize() > 0)
622 {
623 m_abbr.reset(new DWARFDebugAbbrev());
624 if (m_abbr.get())
625 m_abbr->Parse(debug_abbrev_data);
626 }
627 }
628 return m_abbr.get();
629}
630
631const DWARFDebugAbbrev*
632SymbolFileDWARF::DebugAbbrev() const
633{
634 return m_abbr.get();
635}
636
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637
638DWARFDebugInfo*
639SymbolFileDWARF::DebugInfo()
640{
641 if (m_info.get() == NULL)
642 {
643 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
644 if (get_debug_info_data().GetByteSize() > 0)
645 {
646 m_info.reset(new DWARFDebugInfo());
647 if (m_info.get())
648 {
649 m_info->SetDwarfData(this);
650 }
651 }
652 }
653 return m_info.get();
654}
655
656const DWARFDebugInfo*
657SymbolFileDWARF::DebugInfo() const
658{
659 return m_info.get();
660}
661
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662DWARFCompileUnit*
Greg Clayton1f746072012-08-29 21:13:06 +0000663SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664{
665 DWARFDebugInfo* info = DebugInfo();
Greg Clayton1f746072012-08-29 21:13:06 +0000666 if (info)
667 {
668 if (GetDebugMapSymfile ())
669 {
670 // The debug map symbol file made the compile units for this DWARF
671 // file which is .o file with DWARF in it, and we should have
672 // only 1 compile unit which is at offset zero in the DWARF.
673 // TODO: modify to support LTO .o files where each .o file might
674 // have multiple DW_TAG_compile_unit tags.
675 return info->GetCompileUnit(0).get();
676 }
677 else
678 {
679 // Just a normal DWARF file whose user ID for the compile unit is
680 // the DWARF offset itself
681 return info->GetCompileUnit((dw_offset_t)comp_unit->GetID()).get();
682 }
683 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 return NULL;
685}
686
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687
688DWARFDebugRanges*
689SymbolFileDWARF::DebugRanges()
690{
691 if (m_ranges.get() == NULL)
692 {
693 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
694 if (get_debug_ranges_data().GetByteSize() > 0)
695 {
696 m_ranges.reset(new DWARFDebugRanges());
697 if (m_ranges.get())
698 m_ranges->Extract(this);
699 }
700 }
701 return m_ranges.get();
702}
703
704const DWARFDebugRanges*
705SymbolFileDWARF::DebugRanges() const
706{
707 return m_ranges.get();
708}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709
Greg Clayton53eb1c22012-04-02 22:59:12 +0000710lldb::CompUnitSP
711SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000713 CompUnitSP cu_sp;
714 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000716 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
717 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000719 // We already parsed this compile unit, had out a shared pointer to it
720 cu_sp = comp_unit->shared_from_this();
721 }
722 else
723 {
Greg Clayton1f746072012-08-29 21:13:06 +0000724 if (GetDebugMapSymfile ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 {
Greg Clayton1f746072012-08-29 21:13:06 +0000726 // Let the debug map create the compile unit
727 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
728 dwarf_cu->SetUserData(cu_sp.get());
729 }
730 else
731 {
732 ModuleSP module_sp (m_obj_file->GetModule());
733 if (module_sp)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000734 {
Greg Clayton1f746072012-08-29 21:13:06 +0000735 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
736 if (cu_die)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000737 {
Greg Clayton1f746072012-08-29 21:13:06 +0000738 const char * cu_die_name = cu_die->GetName(this, dwarf_cu);
739 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
740 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
741 if (cu_die_name)
742 {
743 std::string ramapped_file;
744 FileSpec cu_file_spec;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000745
Greg Clayton1f746072012-08-29 21:13:06 +0000746 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Greg Clayton53eb1c22012-04-02 22:59:12 +0000747 {
Greg Clayton1f746072012-08-29 21:13:06 +0000748 // If we have a full path to the compile unit, we don't need to resolve
749 // the file. This can be expensive e.g. when the source files are NFS mounted.
750 if (module_sp->RemapSourceFile(cu_die_name, ramapped_file))
751 cu_file_spec.SetFile (ramapped_file.c_str(), false);
752 else
753 cu_file_spec.SetFile (cu_die_name, false);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000754 }
755 else
756 {
Greg Clayton1f746072012-08-29 21:13:06 +0000757 std::string fullpath(cu_comp_dir);
758 if (*fullpath.rbegin() != '/')
759 fullpath += '/';
760 fullpath += cu_die_name;
761 if (module_sp->RemapSourceFile (fullpath.c_str(), ramapped_file))
762 cu_file_spec.SetFile (ramapped_file.c_str(), false);
763 else
764 cu_file_spec.SetFile (fullpath.c_str(), false);
765 }
766
767 cu_sp.reset(new CompileUnit (module_sp,
768 dwarf_cu,
769 cu_file_spec,
770 MakeUserID(dwarf_cu->GetOffset()),
771 cu_language));
772 if (cu_sp)
773 {
774 dwarf_cu->SetUserData(cu_sp.get());
775
Greg Clayton53eb1c22012-04-02 22:59:12 +0000776 // 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)
Greg Clayton037520e2012-07-18 23:18:10 +0000878 func_name.SetValue(ConstString(mangled), true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879 else if (name)
Greg Clayton037520e2012-07-18 23:18:10 +0000880 func_name.SetValue(ConstString(name), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881
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
Greg Clayton1f746072012-08-29 21:13:06 +0000916lldb::LanguageType
917SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
918{
919 assert (sc.comp_unit);
920 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
921 if (dwarf_cu)
922 {
923 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly();
924 const uint32_t language = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
925 if (language)
926 return (lldb::LanguageType)language;
927 }
928 return eLanguageTypeUnknown;
929}
930
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931size_t
932SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
933{
934 assert (sc.comp_unit);
935 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +0000936 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000937 if (dwarf_cu)
938 {
939 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +0000940 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +0000942 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943 {
944 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000945 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946 {
947 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
948 ++functions_added;
949 }
950 }
951 //FixupTypes();
952 }
953 return functions_added;
954}
955
956bool
957SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
958{
959 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +0000960 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000961 assert (dwarf_cu);
962 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963
964 if (cu_die)
965 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000966 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
967 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 +0000968
969 // All file indexes in DWARF are one based and a file of index zero is
970 // supposed to be the compile unit itself.
971 support_files.Append (*sc.comp_unit);
972
Greg Claytond804d282012-03-15 21:01:31 +0000973 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 +0000974 }
975 return false;
976}
977
978struct ParseDWARFLineTableCallbackInfo
979{
980 LineTable* line_table;
981 const SectionList *section_list;
982 lldb::addr_t prev_sect_file_base_addr;
983 lldb::addr_t curr_sect_file_base_addr;
984 bool is_oso_for_debug_map;
985 bool prev_in_final_executable;
986 DWARFDebugLine::Row prev_row;
987 SectionSP prev_section_sp;
988 SectionSP curr_section_sp;
989};
990
991//----------------------------------------------------------------------
992// ParseStatementTableCallback
993//----------------------------------------------------------------------
994static void
995ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
996{
997 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
998 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
999 {
1000 // Just started parsing the line table
1001 }
1002 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1003 {
1004 // Done parsing line table, nothing to do for the cleanup
1005 }
1006 else
1007 {
1008 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
1009 // We have a new row, lets append it
1010
1011 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
1012 {
1013 info->prev_section_sp = info->curr_section_sp;
1014 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
1015 // If this is an end sequence entry, then we subtract one from the
1016 // address to make sure we get an address that is not the end of
1017 // a section.
1018 if (state.end_sequence && state.address != 0)
1019 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
1020 else
1021 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
1022
1023 if (info->curr_section_sp.get())
1024 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
1025 else
1026 info->curr_sect_file_base_addr = 0;
1027 }
1028 if (info->curr_section_sp.get())
1029 {
1030 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
1031 // Check for the fancy section magic to determine if we
1032
1033 if (info->is_oso_for_debug_map)
1034 {
1035 // When this is a debug map object file that contains DWARF
1036 // (referenced from an N_OSO debug map nlist entry) we will have
1037 // a file address in the file range for our section from the
1038 // original .o file, and a load address in the executable that
1039 // contains the debug map.
1040 //
1041 // If the sections for the file range and load range are
1042 // different, we have a remapped section for the function and
1043 // this address is resolved. If they are the same, then the
1044 // function for this address didn't make it into the final
1045 // executable.
Jim Inghamf94e1792012-08-11 00:35:26 +00001046 bool curr_in_final_executable = (bool) info->curr_section_sp->GetLinkedSection ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047
1048 // If we are doing DWARF with debug map, then we need to carefully
1049 // add each line table entry as there may be gaps as functions
1050 // get moved around or removed.
1051 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
1052 {
1053 if (info->prev_in_final_executable)
1054 {
1055 bool terminate_previous_entry = false;
1056 if (!curr_in_final_executable)
1057 {
1058 // Check for the case where the previous line entry
1059 // in a function made it into the final executable,
1060 // yet the current line entry falls in a function
1061 // that didn't. The line table used to be contiguous
1062 // through this address range but now it isn't. We
1063 // need to terminate the previous line entry so
1064 // that we can reconstruct the line range correctly
1065 // for it and to keep the line table correct.
1066 terminate_previous_entry = true;
1067 }
1068 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
1069 {
1070 // Check for cases where the line entries used to be
1071 // contiguous address ranges, but now they aren't.
1072 // This can happen when order files specify the
1073 // ordering of the functions.
1074 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
1075 Section *curr_sect = info->curr_section_sp.get();
1076 Section *prev_sect = info->prev_section_sp.get();
1077 assert (curr_sect->GetLinkedSection());
1078 assert (prev_sect->GetLinkedSection());
1079 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
1080 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
1081 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
1082 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
1083 if (object_file_addr_delta != linked_file_addr_delta)
1084 terminate_previous_entry = true;
1085 }
1086
1087 if (terminate_previous_entry)
1088 {
1089 line_table->InsertLineEntry (info->prev_section_sp,
1090 state.address - info->prev_sect_file_base_addr,
1091 info->prev_row.line,
1092 info->prev_row.column,
1093 info->prev_row.file,
1094 false, // is_stmt
1095 false, // basic_block
1096 false, // state.prologue_end
1097 false, // state.epilogue_begin
1098 true); // end_sequence);
1099 }
1100 }
1101 }
1102
1103 if (curr_in_final_executable)
1104 {
1105 line_table->InsertLineEntry (info->curr_section_sp,
1106 curr_line_section_offset,
1107 state.line,
1108 state.column,
1109 state.file,
1110 state.is_stmt,
1111 state.basic_block,
1112 state.prologue_end,
1113 state.epilogue_begin,
1114 state.end_sequence);
1115 info->prev_section_sp = info->curr_section_sp;
1116 }
1117 else
1118 {
1119 // If the current address didn't make it into the final
1120 // executable, the current section will be the __text
1121 // segment in the .o file, so we need to clear this so
1122 // we can catch the next function that did make it into
1123 // the final executable.
1124 info->prev_section_sp.reset();
1125 info->curr_section_sp.reset();
1126 }
1127
1128 info->prev_in_final_executable = curr_in_final_executable;
1129 }
1130 else
1131 {
1132 // We are not in an object file that contains DWARF for an
1133 // N_OSO, this is just a normal DWARF file. The DWARF spec
1134 // guarantees that the addresses will be in increasing order
1135 // so, since we store line tables in file address order, we
1136 // can always just append the line entry without needing to
1137 // search for the correct insertion point (we don't need to
1138 // use LineEntry::InsertLineEntry()).
1139 line_table->AppendLineEntry (info->curr_section_sp,
1140 curr_line_section_offset,
1141 state.line,
1142 state.column,
1143 state.file,
1144 state.is_stmt,
1145 state.basic_block,
1146 state.prologue_end,
1147 state.epilogue_begin,
1148 state.end_sequence);
1149 }
1150 }
1151
1152 info->prev_row = state;
1153 }
1154}
1155
1156bool
1157SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1158{
1159 assert (sc.comp_unit);
1160 if (sc.comp_unit->GetLineTable() != NULL)
1161 return true;
1162
Greg Clayton1f746072012-08-29 21:13:06 +00001163 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001164 if (dwarf_cu)
1165 {
1166 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001167 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001169 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1170 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001172 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1173 if (line_table_ap.get())
1174 {
1175 ParseDWARFLineTableCallbackInfo info = {
1176 line_table_ap.get(),
1177 m_obj_file->GetSectionList(),
1178 0,
1179 0,
Greg Clayton1f746072012-08-29 21:13:06 +00001180 GetDebugMapSymfile () != NULL,
Greg Clayton129d12c2011-11-28 03:29:03 +00001181 false,
1182 DWARFDebugLine::Row(),
1183 SectionSP(),
1184 SectionSP()
1185 };
1186 uint32_t offset = cu_line_offset;
1187 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1188 sc.comp_unit->SetLineTable(line_table_ap.release());
1189 return true;
1190 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 }
1192 }
1193 }
1194 return false;
1195}
1196
1197size_t
1198SymbolFileDWARF::ParseFunctionBlocks
1199(
1200 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001201 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001202 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203 const DWARFDebugInfoEntry *die,
1204 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001205 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206)
1207{
1208 size_t blocks_added = 0;
1209 while (die != NULL)
1210 {
1211 dw_tag_t tag = die->Tag();
1212
1213 switch (tag)
1214 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001216 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 case DW_TAG_lexical_block:
1218 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001219 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001220 if (tag == DW_TAG_subprogram)
1221 {
1222 // Skip any DW_TAG_subprogram DIEs that are inside
1223 // of a normal or inlined functions. These will be
1224 // parsed on their own as separate entities.
1225
1226 if (depth > 0)
1227 break;
1228
1229 block = parent_block;
1230 }
1231 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001232 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001233 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001234 parent_block->AddChild(block_sp);
1235 block = block_sp.get();
1236 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001237 DWARFDebugRanges::RangeList ranges;
1238 const char *name = NULL;
1239 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241 int decl_file = 0;
1242 int decl_line = 0;
1243 int decl_column = 0;
1244 int call_file = 0;
1245 int call_line = 0;
1246 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001247 if (die->GetDIENamesAndRanges (this,
1248 dwarf_cu,
1249 name,
1250 mangled_name,
1251 ranges,
1252 decl_file, decl_line, decl_column,
1253 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 {
1255 if (tag == DW_TAG_subprogram)
1256 {
1257 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001258 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001260 else if (tag == DW_TAG_inlined_subroutine)
1261 {
1262 // We get called here for inlined subroutines in two ways.
1263 // The first time is when we are making the Function object
1264 // for this inlined concrete instance. Since we're creating a top level block at
1265 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1266 // adjust the containing address.
1267 // The second time is when we are parsing the blocks inside the function that contains
1268 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1269 // function the offset will be for that function.
1270 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1271 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001272 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001273 }
1274 }
1275
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001276 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001277
1278 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1279 {
1280 std::auto_ptr<Declaration> decl_ap;
1281 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001282 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1283 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001284
1285 std::auto_ptr<Declaration> call_ap;
1286 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001287 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1288 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001290 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291 }
1292
1293 ++blocks_added;
1294
Greg Claytondd7feaf2011-08-12 17:54:33 +00001295 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001297 blocks_added += ParseFunctionBlocks (sc,
1298 block,
1299 dwarf_cu,
1300 die->GetFirstChild(),
1301 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001302 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303 }
1304 }
1305 }
1306 break;
1307 default:
1308 break;
1309 }
1310
Greg Claytondd7feaf2011-08-12 17:54:33 +00001311 // Only parse siblings of the block if we are not at depth zero. A depth
1312 // of zero indicates we are currently parsing the top level
1313 // DW_TAG_subprogram DIE
1314
1315 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001317 else
1318 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319 }
1320 return blocks_added;
1321}
1322
Greg Claytonf0705c82011-10-22 03:33:13 +00001323bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001324SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1325 const DWARFDebugInfoEntry *die,
1326 ClangASTContext::TemplateParameterInfos &template_param_infos)
1327{
1328 const dw_tag_t tag = die->Tag();
1329
1330 switch (tag)
1331 {
1332 case DW_TAG_template_type_parameter:
1333 case DW_TAG_template_value_parameter:
1334 {
1335 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1336
1337 DWARFDebugInfoEntry::Attributes attributes;
1338 const size_t num_attributes = die->GetAttributes (this,
1339 dwarf_cu,
1340 fixed_form_sizes,
1341 attributes);
1342 const char *name = NULL;
1343 Type *lldb_type = NULL;
1344 clang_type_t clang_type = NULL;
1345 uint64_t uval64 = 0;
1346 bool uval64_valid = false;
1347 if (num_attributes > 0)
1348 {
1349 DWARFFormValue form_value;
1350 for (size_t i=0; i<num_attributes; ++i)
1351 {
1352 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1353
1354 switch (attr)
1355 {
1356 case DW_AT_name:
1357 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1358 name = form_value.AsCString(&get_debug_str_data());
1359 break;
1360
1361 case DW_AT_type:
1362 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1363 {
1364 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1365 lldb_type = ResolveTypeUID(type_die_offset);
1366 if (lldb_type)
1367 clang_type = lldb_type->GetClangForwardType();
1368 }
1369 break;
1370
1371 case DW_AT_const_value:
1372 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1373 {
1374 uval64_valid = true;
1375 uval64 = form_value.Unsigned();
1376 }
1377 break;
1378 default:
1379 break;
1380 }
1381 }
1382
1383 if (name && lldb_type && clang_type)
1384 {
1385 bool is_signed = false;
1386 template_param_infos.names.push_back(name);
1387 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1388 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1389 {
1390 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
Sean Callanan3d654b32012-09-24 22:25:51 +00001391 template_param_infos.args.push_back (clang::TemplateArgument (*GetClangASTContext().getASTContext(),
1392 llvm::APSInt(apint),
1393 clang_qual_type));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001394 }
1395 else
1396 {
1397 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1398 }
1399 }
1400 else
1401 {
1402 return false;
1403 }
1404
1405 }
1406 }
1407 return true;
1408
1409 default:
1410 break;
1411 }
1412 return false;
1413}
1414
1415bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001416SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1417 const DWARFDebugInfoEntry *parent_die,
1418 ClangASTContext::TemplateParameterInfos &template_param_infos)
1419{
1420
1421 if (parent_die == NULL)
Filipe Cabecinhas52008c62012-05-23 16:24:11 +00001422 return false;
Greg Claytonf0705c82011-10-22 03:33:13 +00001423
Greg Claytonf0705c82011-10-22 03:33:13 +00001424 Args template_parameter_names;
1425 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1426 die != NULL;
1427 die = die->GetSibling())
1428 {
1429 const dw_tag_t tag = die->Tag();
1430
1431 switch (tag)
1432 {
1433 case DW_TAG_template_type_parameter:
1434 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001435 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001436 break;
1437
1438 default:
1439 break;
1440 }
1441 }
1442 if (template_param_infos.args.empty())
1443 return false;
1444 return template_param_infos.args.size() == template_param_infos.names.size();
1445}
1446
1447clang::ClassTemplateDecl *
1448SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001449 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001450 const char *parent_name,
1451 int tag_decl_kind,
1452 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1453{
1454 if (template_param_infos.IsValid())
1455 {
1456 std::string template_basename(parent_name);
1457 template_basename.erase (template_basename.find('<'));
1458 ClangASTContext &ast = GetClangASTContext();
1459
1460 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001461 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001462 template_basename.c_str(),
1463 tag_decl_kind,
1464 template_param_infos);
1465 }
1466 return NULL;
1467}
1468
Sean Callanana0b80ab2012-06-04 22:28:05 +00001469class SymbolFileDWARF::DelayedAddObjCClassProperty
1470{
1471public:
1472 DelayedAddObjCClassProperty
1473 (
1474 clang::ASTContext *ast,
1475 lldb::clang_type_t class_opaque_type,
1476 const char *property_name,
1477 lldb::clang_type_t property_opaque_type, // The property type is only required if you don't have an ivar decl
1478 clang::ObjCIvarDecl *ivar_decl,
1479 const char *property_setter_name,
1480 const char *property_getter_name,
1481 uint32_t property_attributes,
1482 uint64_t metadata = 0
1483 ) :
1484 m_ast (ast),
1485 m_class_opaque_type (class_opaque_type),
1486 m_property_name (property_name),
1487 m_property_opaque_type (property_opaque_type),
1488 m_ivar_decl (ivar_decl),
1489 m_property_setter_name (property_setter_name),
1490 m_property_getter_name (property_getter_name),
1491 m_property_attributes (property_attributes),
1492 m_metadata (metadata)
1493 {
1494 }
1495
1496 bool Finalize() const
1497 {
1498 return ClangASTContext::AddObjCClassProperty(m_ast,
1499 m_class_opaque_type,
1500 m_property_name,
1501 m_property_opaque_type,
1502 m_ivar_decl,
1503 m_property_setter_name,
1504 m_property_getter_name,
1505 m_property_attributes,
1506 m_metadata);
1507 }
1508private:
1509 clang::ASTContext *m_ast;
1510 lldb::clang_type_t m_class_opaque_type;
1511 const char *m_property_name;
1512 lldb::clang_type_t m_property_opaque_type;
1513 clang::ObjCIvarDecl *m_ivar_decl;
1514 const char *m_property_setter_name;
1515 const char *m_property_getter_name;
1516 uint32_t m_property_attributes;
1517 uint64_t m_metadata;
1518};
1519
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520size_t
1521SymbolFileDWARF::ParseChildMembers
1522(
1523 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001524 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001525 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001526 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001527 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001528 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1529 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001530 DWARFDIECollection& member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00001531 DelayedPropertyList& delayed_properties,
Sean Callananc7fbf732010-08-06 00:32:49 +00001532 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001533 bool &is_a_class,
1534 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535)
1536{
1537 if (parent_die == NULL)
1538 return 0;
1539
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001540 size_t count = 0;
1541 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001542 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001543 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001544
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001545 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1546 {
1547 dw_tag_t tag = die->Tag();
1548
1549 switch (tag)
1550 {
1551 case DW_TAG_member:
Sean Callanan3d654b32012-09-24 22:25:51 +00001552 case DW_TAG_APPLE_property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001553 {
1554 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001555 const size_t num_attributes = die->GetAttributes (this,
1556 dwarf_cu,
1557 fixed_form_sizes,
1558 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001559 if (num_attributes > 0)
1560 {
1561 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001562 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001563 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001564 const char *prop_name = NULL;
1565 const char *prop_getter_name = NULL;
1566 const char *prop_setter_name = NULL;
1567 uint32_t prop_attributes = 0;
1568
1569
Greg Clayton24739922010-10-13 03:15:28 +00001570 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001572 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001573 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001574 size_t byte_size = 0;
1575 size_t bit_offset = 0;
1576 size_t bit_size = 0;
1577 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001578 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579 {
1580 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1581 DWARFFormValue form_value;
1582 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1583 {
1584 switch (attr)
1585 {
1586 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1587 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1588 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1589 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1590 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1591 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1592 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1593 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1594 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001595 if (form_value.BlockData())
1596 {
1597 Value initialValue(0);
1598 Value memberOffset(0);
1599 const DataExtractor& debug_info_data = get_debug_info_data();
1600 uint32_t block_length = form_value.Unsigned();
1601 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1602 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1603 NULL, // clang::ASTContext *
1604 NULL, // ClangExpressionVariableList *
1605 NULL, // ClangExpressionDeclMap *
1606 NULL, // RegisterContext *
1607 debug_info_data,
1608 block_offset,
1609 block_length,
1610 eRegisterKindDWARF,
1611 &initialValue,
1612 memberOffset,
1613 NULL))
1614 {
1615 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1616 }
1617 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001618 break;
1619
Greg Clayton8cf05932010-07-22 18:30:50 +00001620 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1b02c172012-03-14 21:00:47 +00001621 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001622 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1623 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1624 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1625 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1626
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001627 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001628 case DW_AT_declaration:
1629 case DW_AT_description:
1630 case DW_AT_mutable:
1631 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001632 case DW_AT_sibling:
1633 break;
1634 }
1635 }
1636 }
Sean Callanana6582262012-04-05 00:12:52 +00001637
1638 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00001639 {
Sean Callanana6582262012-04-05 00:12:52 +00001640 ConstString fixed_getter;
1641 ConstString fixed_setter;
1642
1643 // Check if the property getter/setter were provided as full
1644 // names. We want basenames, so we extract them.
1645
1646 if (prop_getter_name && prop_getter_name[0] == '-')
1647 {
1648 ObjCLanguageRuntime::ParseMethodName (prop_getter_name,
1649 NULL,
1650 &fixed_getter,
1651 NULL,
1652 NULL);
1653 prop_getter_name = fixed_getter.GetCString();
1654 }
1655
1656 if (prop_setter_name && prop_setter_name[0] == '-')
1657 {
1658 ObjCLanguageRuntime::ParseMethodName (prop_setter_name,
1659 NULL,
1660 &fixed_setter,
1661 NULL,
1662 NULL);
1663 prop_setter_name = fixed_setter.GetCString();
1664 }
1665
1666 // If the names haven't been provided, they need to be
1667 // filled in.
1668
1669 if (!prop_getter_name)
1670 {
1671 prop_getter_name = prop_name;
1672 }
1673 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
1674 {
1675 StreamString ss;
1676
1677 ss.Printf("set%c%s:",
1678 toupper(prop_name[0]),
1679 &prop_name[1]);
1680
1681 fixed_setter.SetCString(ss.GetData());
1682 prop_setter_name = fixed_setter.GetCString();
1683 }
Sean Callanan751aac62012-03-29 19:07:06 +00001684 }
1685
1686 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001687 // represents fields that are references with bad byte size
1688 // and bit size/offset information such as:
1689 //
1690 // DW_AT_byte_size( 0x00 )
1691 // DW_AT_bit_size( 0x40 )
1692 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1693 //
1694 // So check the bit offset to make sure it is sane, and if
1695 // the values are not sane, remove them. If we don't do this
1696 // then we will end up with a crash if we try to use this
1697 // type in an expression when clang becomes unhappy with its
1698 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001699
Greg Clayton44953932011-10-25 01:25:35 +00001700 if (bit_offset > 128)
1701 {
1702 bit_size = 0;
1703 bit_offset = 0;
1704 }
1705
1706 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001707 if (class_language == eLanguageTypeObjC ||
1708 class_language == eLanguageTypeObjC_plus_plus)
1709 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001710
1711 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1712 {
1713 // Not all compilers will mark the vtable pointer
1714 // member as artificial (llvm-gcc). We can't have
1715 // the virtual members in our classes otherwise it
1716 // throws off all child offsets since we end up
1717 // having and extra pointer sized member in our
1718 // class layouts.
1719 is_artificial = true;
1720 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001721
Greg Clayton24739922010-10-13 03:15:28 +00001722 if (is_artificial == false)
1723 {
1724 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001725 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001726 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001727 {
Sean Callanan751aac62012-03-29 19:07:06 +00001728 if (member_type)
1729 {
1730 if (accessibility == eAccessNone)
1731 accessibility = default_accessibility;
1732 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733
Sean Callanan751aac62012-03-29 19:07:06 +00001734 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
1735 name,
1736 member_type->GetClangLayoutType(),
1737 accessibility,
1738 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00001739
1740 GetClangASTContext().SetMetadata((uintptr_t)field_decl, MakeUserID(die->GetOffset()));
Sean Callanan5b26f272012-02-04 08:49:35 +00001741 }
1742 else
1743 {
Sean Callanan751aac62012-03-29 19:07:06 +00001744 if (name)
1745 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1746 MakeUserID(die->GetOffset()),
1747 name,
1748 encoding_uid);
1749 else
1750 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1751 MakeUserID(die->GetOffset()),
1752 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00001753 }
Sean Callanan751aac62012-03-29 19:07:06 +00001754
1755 if (member_byte_offset != UINT32_MAX || bit_size != 0)
1756 {
1757 /////////////////////////////////////////////////////////////
1758 // How to locate a field given the DWARF debug information
1759 //
1760 // AT_byte_size indicates the size of the word in which the
1761 // bit offset must be interpreted.
1762 //
1763 // AT_data_member_location indicates the byte offset of the
1764 // word from the base address of the structure.
1765 //
1766 // AT_bit_offset indicates how many bits into the word
1767 // (according to the host endianness) the low-order bit of
1768 // the field starts. AT_bit_offset can be negative.
1769 //
1770 // AT_bit_size indicates the size of the field in bits.
1771 /////////////////////////////////////////////////////////////
Sean Callanan5b26f272012-02-04 08:49:35 +00001772
Sean Callanan751aac62012-03-29 19:07:06 +00001773 ByteOrder object_endian = GetObjectFile()->GetModule()->GetArchitecture().GetDefaultEndian();
1774
1775 uint64_t total_bit_offset = 0;
1776
1777 total_bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1778
1779 if (object_endian == eByteOrderLittle)
1780 {
1781 total_bit_offset += byte_size * 8;
1782 total_bit_offset -= (bit_offset + bit_size);
1783 }
1784 else
1785 {
1786 total_bit_offset += bit_offset;
1787 }
1788
1789 layout_info.field_offsets.insert(std::make_pair(field_decl, total_bit_offset));
1790 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001791 }
Sean Callanan751aac62012-03-29 19:07:06 +00001792
Jim Inghame3ae82a2011-11-12 01:36:43 +00001793 if (prop_name != NULL)
1794 {
Sean Callanan751aac62012-03-29 19:07:06 +00001795 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001796
Sean Callanan751aac62012-03-29 19:07:06 +00001797 if (field_decl)
1798 {
1799 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1800 assert (ivar_decl != NULL);
1801 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001802
Sean Callanana0b80ab2012-06-04 22:28:05 +00001803 delayed_properties.push_back(DelayedAddObjCClassProperty(GetClangASTContext().getASTContext(),
1804 class_clang_type,
1805 prop_name,
1806 member_type->GetClangLayoutType(),
1807 ivar_decl,
1808 prop_setter_name,
1809 prop_getter_name,
1810 prop_attributes,
1811 MakeUserID(die->GetOffset())));
Sean Callanan60217122012-04-13 00:10:03 +00001812
Sean Callananad880762012-04-18 01:06:17 +00001813 if (ivar_decl)
1814 GetClangASTContext().SetMetadata((uintptr_t)ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00001815 }
Greg Clayton24739922010-10-13 03:15:28 +00001816 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001817 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001818 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819 }
1820 break;
1821
1822 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001823 // Let the type parsing code handle this one for us.
1824 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001825 break;
1826
1827 case DW_TAG_inheritance:
1828 {
1829 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001830 if (default_accessibility == eAccessNone)
1831 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001832 // TODO: implement DW_TAG_inheritance type parsing
1833 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001834 const size_t num_attributes = die->GetAttributes (this,
1835 dwarf_cu,
1836 fixed_form_sizes,
1837 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001838 if (num_attributes > 0)
1839 {
1840 Declaration decl;
1841 DWARFExpression location;
1842 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001843 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001844 bool is_virtual = false;
1845 bool is_base_of_class = true;
Greg Clayton23f59502012-07-17 03:23:13 +00001846 //off_t member_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847 uint32_t i;
1848 for (i=0; i<num_attributes; ++i)
1849 {
1850 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1851 DWARFFormValue form_value;
1852 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1853 {
1854 switch (attr)
1855 {
1856 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1857 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1858 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1859 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00001860// case DW_AT_data_member_location:
1861// if (form_value.BlockData())
1862// {
1863// Value initialValue(0);
1864// Value memberOffset(0);
1865// const DataExtractor& debug_info_data = get_debug_info_data();
1866// uint32_t block_length = form_value.Unsigned();
1867// uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1868// if (DWARFExpression::Evaluate (NULL,
1869// NULL,
1870// NULL,
1871// NULL,
1872// NULL,
1873// debug_info_data,
1874// block_offset,
1875// block_length,
1876// eRegisterKindDWARF,
1877// &initialValue,
1878// memberOffset,
1879// NULL))
1880// {
1881// member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1882// }
1883// }
1884// break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001885
1886 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001887 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001888 break;
1889
1890 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1891 default:
1892 case DW_AT_sibling:
1893 break;
1894 }
1895 }
1896 }
1897
Greg Clayton526e5af2010-11-13 03:52:47 +00001898 Type *base_class_type = ResolveTypeUID(encoding_uid);
1899 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001900
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001901 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1902 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001903 if (class_language == eLanguageTypeObjC)
1904 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001905 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001906 }
1907 else
1908 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001909 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001910 accessibility,
1911 is_virtual,
1912 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001913 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001914 }
1915 }
1916 break;
1917
1918 default:
1919 break;
1920 }
1921 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00001922
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001923 return count;
1924}
1925
1926
1927clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001928SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001929{
1930 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001931 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001932 {
1933 DWARFCompileUnitSP cu_sp;
1934 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1935 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001936 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001937 }
1938 return NULL;
1939}
1940
1941clang::DeclContext*
1942SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1943{
Greg Clayton81c22f62011-10-19 18:09:39 +00001944 if (UserIDMatches(type_uid))
1945 return GetClangDeclContextForDIEOffset (sc, type_uid);
1946 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001947}
1948
1949Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001950SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001951{
Greg Clayton81c22f62011-10-19 18:09:39 +00001952 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001954 DWARFDebugInfo* debug_info = DebugInfo();
1955 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001956 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001957 DWARFCompileUnitSP cu_sp;
1958 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001959 const bool assert_not_being_parsed = true;
1960 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001961 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001962 }
1963 return NULL;
1964}
1965
Greg Claytoncab36a32011-12-08 05:16:30 +00001966Type*
1967SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001968{
Greg Claytoncab36a32011-12-08 05:16:30 +00001969 if (die != NULL)
1970 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001971 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1972 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001973 GetObjectFile()->GetModule()->LogMessage (log.get(),
1974 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1975 die->GetOffset(),
1976 DW_TAG_value_to_name(die->Tag()),
1977 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001978
Greg Claytoncab36a32011-12-08 05:16:30 +00001979 // We might be coming in in the middle of a type tree (a class
1980 // withing a class, an enum within a class), so parse any needed
1981 // parent DIEs before we get to this one...
1982 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1983 switch (decl_ctx_die->Tag())
1984 {
1985 case DW_TAG_structure_type:
1986 case DW_TAG_union_type:
1987 case DW_TAG_class_type:
1988 {
1989 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001990 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001991 GetObjectFile()->GetModule()->LogMessage (log.get(),
1992 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1993 die->GetOffset(),
1994 DW_TAG_value_to_name(die->Tag()),
1995 die->GetName(this, cu),
1996 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00001997//
1998// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
1999// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
2000// {
2001// if (log)
2002// GetObjectFile()->GetModule()->LogMessage (log.get(),
2003// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
2004// die->GetOffset(),
2005// DW_TAG_value_to_name(die->Tag()),
2006// die->GetName(this, cu),
2007// decl_ctx_die->GetOffset());
2008// // Ask the type to complete itself if it already hasn't since if we
2009// // want a function (method or static) from a class, the class must
2010// // create itself and add it's own methods and class functions.
2011// if (parent_type)
2012// parent_type->GetClangFullType();
2013// }
Greg Claytoncab36a32011-12-08 05:16:30 +00002014 }
2015 break;
2016
2017 default:
2018 break;
2019 }
2020 return ResolveType (cu, die);
2021 }
2022 return NULL;
2023}
2024
Greg Clayton6beaaa62011-01-17 03:46:26 +00002025// This function is used when SymbolFileDWARFDebugMap owns a bunch of
2026// SymbolFileDWARF objects to detect if this DWARF file is the one that
2027// can resolve a clang_type.
2028bool
2029SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
2030{
2031 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2032 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
2033 return die != NULL;
2034}
2035
2036
Greg Clayton1be10fc2010-09-29 01:12:09 +00002037lldb::clang_type_t
2038SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
2039{
2040 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002041 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2042 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002043 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002044 {
2045 // We have already resolved this type...
2046 return clang_type;
2047 }
2048 // Once we start resolving this type, remove it from the forward declaration
2049 // map in case anyone child members or other types require this type to get resolved.
2050 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
2051 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002052 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00002053
Greg Clayton1be10fc2010-09-29 01:12:09 +00002054
Greg Clayton85ae2e12011-10-18 23:36:41 +00002055 // Disable external storage for this type so we don't get anymore
2056 // clang::ExternalASTSource queries for this type.
2057 ClangASTContext::SetHasExternalStorage (clang_type, false);
2058
Greg Clayton450e3f32010-10-12 02:24:53 +00002059 DWARFDebugInfo* debug_info = DebugInfo();
2060
Greg Clayton53eb1c22012-04-02 22:59:12 +00002061 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002062 Type *type = m_die_to_type.lookup (die);
2063
2064 const dw_tag_t tag = die->Tag();
2065
Greg Claytonbaf95da2012-03-30 23:50:54 +00002066 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00002067 if (log)
Greg Claytonbaf95da2012-03-30 23:50:54 +00002068 {
Greg Claytond61c0fc2012-04-23 22:55:20 +00002069 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log.get(),
2070 "0x%8.8llx: %s '%s' resolving forward declaration...",
2071 MakeUserID(die->GetOffset()),
2072 DW_TAG_value_to_name(tag),
2073 type->GetName().AsCString());
Greg Claytonbaf95da2012-03-30 23:50:54 +00002074
Greg Claytonbaf95da2012-03-30 23:50:54 +00002075 }
Greg Clayton1be10fc2010-09-29 01:12:09 +00002076 assert (clang_type);
2077 DWARFDebugInfoEntry::Attributes attributes;
2078
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002079 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002080
2081 switch (tag)
2082 {
2083 case DW_TAG_structure_type:
2084 case DW_TAG_union_type:
2085 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002086 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002087 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002088
Greg Clayton1be10fc2010-09-29 01:12:09 +00002089 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002090 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002091 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002092
Sean Callanan77a1fd32012-02-27 20:07:01 +00002093 LanguageType class_language = eLanguageTypeUnknown;
2094 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
2095 if (is_objc_class)
Greg Clayton219cf312012-03-30 00:51:13 +00002096 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002097 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002098 // For objective C we don't start the definition when
2099 // the class is created.
2100 ast.StartTagDeclarationDefinition (clang_type);
2101 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002102
2103 int tag_decl_kind = -1;
2104 AccessType default_accessibility = eAccessNone;
2105 if (tag == DW_TAG_structure_type)
2106 {
2107 tag_decl_kind = clang::TTK_Struct;
2108 default_accessibility = eAccessPublic;
2109 }
2110 else if (tag == DW_TAG_union_type)
2111 {
2112 tag_decl_kind = clang::TTK_Union;
2113 default_accessibility = eAccessPublic;
2114 }
2115 else if (tag == DW_TAG_class_type)
2116 {
2117 tag_decl_kind = clang::TTK_Class;
2118 default_accessibility = eAccessPrivate;
2119 }
2120
Greg Clayton53eb1c22012-04-02 22:59:12 +00002121 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002122 std::vector<clang::CXXBaseSpecifier *> base_classes;
2123 std::vector<int> member_accessibilities;
2124 bool is_a_class = false;
2125 // Parse members and base classes first
2126 DWARFDIECollection member_function_dies;
Sean Callanana0b80ab2012-06-04 22:28:05 +00002127
2128 DelayedPropertyList delayed_properties;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002129
2130 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002131 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002132 die,
2133 clang_type,
2134 class_language,
2135 base_classes,
2136 member_accessibilities,
2137 member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00002138 delayed_properties,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002139 default_accessibility,
2140 is_a_class,
2141 layout_info);
2142
2143 // Now parse any methods if there were any...
2144 size_t num_functions = member_function_dies.Size();
2145 if (num_functions > 0)
2146 {
2147 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002148 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002149 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002150 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002151 }
2152
2153 if (class_language == eLanguageTypeObjC)
2154 {
Greg Clayton84db9102012-03-26 23:03:23 +00002155 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(ast.getASTContext(), clang_type));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002156 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00002157 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002158
Sean Callanan77a1fd32012-02-27 20:07:01 +00002159 DIEArray method_die_offsets;
2160 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002161 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002162 if (m_apple_objc_ap.get())
2163 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
2164 }
2165 else
2166 {
2167 if (!m_indexed)
2168 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002169
Sean Callanan77a1fd32012-02-27 20:07:01 +00002170 ConstString class_name (class_str.c_str());
2171 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2172 }
2173
2174 if (!method_die_offsets.empty())
2175 {
2176 DWARFDebugInfo* debug_info = DebugInfo();
2177
2178 DWARFCompileUnit* method_cu = NULL;
2179 const size_t num_matches = method_die_offsets.size();
2180 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002181 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002182 const dw_offset_t die_offset = method_die_offsets[i];
2183 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2184
2185 if (method_die)
2186 ResolveType (method_cu, method_die);
2187 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002188 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002189 if (m_using_apple_tables)
2190 {
2191 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2192 die_offset, class_str.c_str());
2193 }
2194 }
2195 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002196 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00002197
2198 for (DelayedPropertyList::const_iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
2199 pi != pe;
2200 ++pi)
2201 pi->Finalize();
Greg Clayton450e3f32010-10-12 02:24:53 +00002202 }
2203 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002204
2205 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2206 // need to tell the clang type it is actually a class.
2207 if (class_language != eLanguageTypeObjC)
2208 {
2209 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2210 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2211 }
2212
2213 // Since DW_TAG_structure_type gets used for both classes
2214 // and structures, we may need to set any DW_TAG_member
2215 // fields to have a "private" access if none was specified.
2216 // When we parsed the child members we tracked that actual
2217 // accessibility value for each DW_TAG_member in the
2218 // "member_accessibilities" array. If the value for the
2219 // member is zero, then it was set to the "default_accessibility"
2220 // which for structs was "public". Below we correct this
2221 // by setting any fields to "private" that weren't correctly
2222 // set.
2223 if (is_a_class && !member_accessibilities.empty())
2224 {
2225 // This is a class and all members that didn't have
2226 // their access specified are private.
2227 ast.SetDefaultAccessForRecordFields (clang_type,
2228 eAccessPrivate,
2229 &member_accessibilities.front(),
2230 member_accessibilities.size());
2231 }
2232
2233 if (!base_classes.empty())
2234 {
2235 ast.SetBaseClassesForClassType (clang_type,
2236 &base_classes.front(),
2237 base_classes.size());
2238
2239 // Clang will copy each CXXBaseSpecifier in "base_classes"
2240 // so we have to free them all.
2241 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2242 base_classes.size());
2243 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002244 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002245 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002246
2247 ast.BuildIndirectFields (clang_type);
2248
Sean Callanan77a1fd32012-02-27 20:07:01 +00002249 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002250
Greg Claytoncaab74e2012-01-28 00:48:57 +00002251 if (!layout_info.field_offsets.empty())
2252 {
2253 if (type)
2254 layout_info.bit_size = type->GetByteSize() * 8;
2255 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002256 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Greg Claytoncaab74e2012-01-28 00:48:57 +00002257 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
2258 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
2259 if (record_type)
2260 {
2261 const clang::RecordDecl *record_decl = record_type->getDecl();
2262
2263 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002264 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002265 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002266 "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 +00002267 clang_type,
2268 record_decl,
2269 layout_info.bit_size,
2270 layout_info.alignment,
2271 (uint32_t)layout_info.field_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002272
Greg Clayton821ac6d2012-01-28 02:22:27 +00002273 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
2274 for (pos = layout_info.field_offsets.begin(); pos != end; ++pos)
2275 {
2276 GetObjectFile()->GetModule()->LogMessage (log.get(),
2277 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field = { bit_offset=%u, name='%s' }",
2278 clang_type,
2279 (uint32_t)pos->second,
2280 pos->first->getNameAsString().c_str());
2281 }
2282 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002283 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2284 }
2285 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002286 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002287
Greg Claytonc93237c2010-10-01 20:48:32 +00002288 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002289
2290 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002291 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002292 if (die->HasChildren())
2293 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002294 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
2295 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002296 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002297 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002298 return clang_type;
2299
2300 default:
2301 assert(false && "not a forward clang type decl!");
2302 break;
2303 }
2304 return NULL;
2305}
2306
Greg Claytonc685f8e2010-09-15 04:15:46 +00002307Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002308SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002309{
2310 if (type_die != NULL)
2311 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002312 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002313
Greg Claytonc685f8e2010-09-15 04:15:46 +00002314 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002315 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002316
Greg Clayton24739922010-10-13 03:15:28 +00002317 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002318 {
2319 if (type != DIE_IS_BEING_PARSED)
2320 return type;
2321
2322 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002323 type_die->GetOffset(),
2324 DW_TAG_value_to_name(type_die->Tag()),
2325 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002326
2327 }
2328 else
2329 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002330 }
2331 return NULL;
2332}
2333
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002334CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002335SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002336{
2337 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002338 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002339 {
2340 // The symbol vendor doesn't know about this compile unit, we
2341 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002342 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002343 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002344 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002345}
2346
2347bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002348SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002349{
2350 sc.Clear();
2351 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002352 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002353
Greg Clayton81c22f62011-10-19 18:09:39 +00002354 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_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, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002357
2358 if (sc.function)
2359 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002360 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002361 return true;
2362 }
2363
2364 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002365}
2366
2367uint32_t
2368SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2369{
2370 Timer scoped_timer(__PRETTY_FUNCTION__,
2371 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002372 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002373 so_addr.GetOffset(),
2374 resolve_scope);
2375 uint32_t resolved = 0;
2376 if (resolve_scope & ( eSymbolContextCompUnit |
2377 eSymbolContextFunction |
2378 eSymbolContextBlock |
2379 eSymbolContextLineEntry))
2380 {
2381 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2382
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002383 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002384 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002385 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002386 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002387 if (cu_offset != DW_INVALID_OFFSET)
2388 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002389 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002390 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2391 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002392 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002393 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002394 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002395 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002396 resolved |= eSymbolContextCompUnit;
2397
2398 if (resolve_scope & eSymbolContextLineEntry)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002399 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002400 LineTable *line_table = sc.comp_unit->GetLineTable();
2401 if (line_table != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002402 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002403 if (so_addr.IsLinkedAddress())
2404 {
2405 Address linked_addr (so_addr);
2406 linked_addr.ResolveLinkedAddress();
2407 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2408 {
2409 resolved |= eSymbolContextLineEntry;
2410 }
2411 }
2412 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002413 {
2414 resolved |= eSymbolContextLineEntry;
2415 }
2416 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002417 }
2418
2419 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2420 {
2421 DWARFDebugInfoEntry *function_die = NULL;
2422 DWARFDebugInfoEntry *block_die = NULL;
2423 if (resolve_scope & eSymbolContextBlock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002424 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002425 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
2426 }
2427 else
2428 {
2429 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
2430 }
2431
2432 if (function_die != NULL)
2433 {
2434 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2435 if (sc.function == NULL)
2436 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2437 }
2438 else
2439 {
2440 // We might have had a compile unit that had discontiguous
2441 // address ranges where the gaps are symbols that don't have
2442 // any debug info. Discontiguous compile unit address ranges
2443 // should only happen when there aren't other functions from
2444 // other compile units in these gaps. This helps keep the size
2445 // of the aranges down.
2446 sc.comp_unit = NULL;
2447 resolved &= ~eSymbolContextCompUnit;
2448 }
2449
2450 if (sc.function != NULL)
2451 {
2452 resolved |= eSymbolContextFunction;
2453
2454 if (resolve_scope & eSymbolContextBlock)
2455 {
2456 Block& block = sc.function->GetBlock (true);
2457
2458 if (block_die != NULL)
2459 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2460 else
2461 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2462 if (sc.block)
2463 resolved |= eSymbolContextBlock;
2464 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002465 }
2466 }
2467 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002468 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002469 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002470 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
2471 cu_offset,
2472 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002473 }
2474 }
2475 }
2476 }
2477 }
2478 return resolved;
2479}
2480
2481
2482
2483uint32_t
2484SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2485{
2486 const uint32_t prev_size = sc_list.GetSize();
2487 if (resolve_scope & eSymbolContextCompUnit)
2488 {
2489 DWARFDebugInfo* debug_info = DebugInfo();
2490 if (debug_info)
2491 {
2492 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002493 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002494
Greg Clayton53eb1c22012-04-02 22:59:12 +00002495 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002496 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002497 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton1f746072012-08-29 21:13:06 +00002498 const bool full_match = file_spec.GetDirectory();
2499 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002500 if (check_inlines || file_spec_matches_cu_file_spec)
2501 {
2502 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002503 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002504 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002505 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002506 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002507
Greg Clayton526a4ae2012-05-16 22:09:01 +00002508 // If we are looking for inline functions only and we don't
2509 // find it in the support files, we are done.
2510 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002511 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002512 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
2513 if (file_idx == UINT32_MAX)
2514 continue;
2515 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002516
Greg Clayton526a4ae2012-05-16 22:09:01 +00002517 if (line != 0)
2518 {
2519 LineTable *line_table = sc.comp_unit->GetLineTable();
2520
2521 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002522 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002523 // We will have already looked up the file index if
2524 // we are searching for inline entries.
2525 if (!check_inlines)
2526 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002527
Greg Clayton526a4ae2012-05-16 22:09:01 +00002528 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002529 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002530 uint32_t found_line;
2531 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2532 found_line = sc.line_entry.line;
2533
2534 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002535 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002536 sc.function = NULL;
2537 sc.block = NULL;
2538 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002539 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002540 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2541 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002542 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002543 DWARFDebugInfoEntry *function_die = NULL;
2544 DWARFDebugInfoEntry *block_die = NULL;
2545 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002546
Greg Clayton526a4ae2012-05-16 22:09:01 +00002547 if (function_die != NULL)
2548 {
2549 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2550 if (sc.function == NULL)
2551 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2552 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002553
Greg Clayton526a4ae2012-05-16 22:09:01 +00002554 if (sc.function != NULL)
2555 {
2556 Block& block = sc.function->GetBlock (true);
2557
2558 if (block_die != NULL)
2559 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2560 else
2561 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2562 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002563 }
2564 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002565
Greg Clayton526a4ae2012-05-16 22:09:01 +00002566 sc_list.Append(sc);
2567 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2568 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002569 }
2570 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002571 else if (file_spec_matches_cu_file_spec && !check_inlines)
2572 {
2573 // only append the context if we aren't looking for inline call sites
2574 // by file and line and if the file spec matches that of the compile unit
2575 sc_list.Append(sc);
2576 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002577 }
2578 else if (file_spec_matches_cu_file_spec && !check_inlines)
2579 {
2580 // only append the context if we aren't looking for inline call sites
2581 // by file and line and if the file spec matches that of the compile unit
2582 sc_list.Append(sc);
2583 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002584
Greg Clayton526a4ae2012-05-16 22:09:01 +00002585 if (!check_inlines)
2586 break;
2587 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002588 }
2589 }
2590 }
2591 }
2592 return sc_list.GetSize() - prev_size;
2593}
2594
2595void
2596SymbolFileDWARF::Index ()
2597{
2598 if (m_indexed)
2599 return;
2600 m_indexed = true;
2601 Timer scoped_timer (__PRETTY_FUNCTION__,
2602 "SymbolFileDWARF::Index (%s)",
2603 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2604
2605 DWARFDebugInfo* debug_info = DebugInfo();
2606 if (debug_info)
2607 {
2608 uint32_t cu_idx = 0;
2609 const uint32_t num_compile_units = GetNumCompileUnits();
2610 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2611 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002612 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002613
Greg Clayton53eb1c22012-04-02 22:59:12 +00002614 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002615
Greg Clayton53eb1c22012-04-02 22:59:12 +00002616 dwarf_cu->Index (cu_idx,
2617 m_function_basename_index,
2618 m_function_fullname_index,
2619 m_function_method_index,
2620 m_function_selector_index,
2621 m_objc_class_selectors_index,
2622 m_global_index,
2623 m_type_index,
2624 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002625
2626 // Keep memory down by clearing DIEs if this generate function
2627 // caused them to be parsed
2628 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002629 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002630 }
2631
Greg Claytond4a2b372011-09-12 23:21:58 +00002632 m_function_basename_index.Finalize();
2633 m_function_fullname_index.Finalize();
2634 m_function_method_index.Finalize();
2635 m_function_selector_index.Finalize();
2636 m_objc_class_selectors_index.Finalize();
2637 m_global_index.Finalize();
2638 m_type_index.Finalize();
2639 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002640
Greg Clayton24739922010-10-13 03:15:28 +00002641#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002642 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002643 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002644 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2645 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002646 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2647 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2648 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2649 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2650 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2651 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002652 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002653 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002654#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002655 }
2656}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002657
2658bool
2659SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2660{
2661 if (namespace_decl == NULL)
2662 {
2663 // Invalid namespace decl which means we aren't matching only things
2664 // in this symbol file, so return true to indicate it matches this
2665 // symbol file.
2666 return true;
2667 }
2668
2669 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2670
2671 if (namespace_ast == NULL)
2672 return true; // No AST in the "namespace_decl", return true since it
2673 // could then match any symbol file, including this one
2674
2675 if (namespace_ast == GetClangASTContext().getASTContext())
2676 return true; // The ASTs match, return true
2677
2678 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002679 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2680
2681 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002682 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002683
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002684 return false;
2685}
2686
Greg Clayton2506a7a2011-10-12 23:34:26 +00002687bool
2688SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2689 DWARFCompileUnit* cu,
2690 const DWARFDebugInfoEntry* die)
2691{
2692 // No namespace specified, so the answesr i
2693 if (namespace_decl == NULL)
2694 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002695
2696 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002697
Greg Clayton437a1352012-04-09 22:43:43 +00002698 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
2699 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002700 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00002701 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00002702 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00002703
Greg Clayton2506a7a2011-10-12 23:34:26 +00002704 if (clang_namespace_decl)
2705 {
2706 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002707 {
2708 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002709 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002710 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002711 }
2712
Greg Clayton437a1352012-04-09 22:43:43 +00002713 if (clang_namespace_decl == die_clang_decl_ctx)
2714 return true;
2715 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00002716 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00002717 }
2718 else
2719 {
2720 // We have a namespace_decl that was not NULL but it contained
2721 // a NULL "clang::NamespaceDecl", so this means the global namespace
2722 // So as long the the contained decl context DIE isn't a namespace
2723 // we should be ok.
2724 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2725 return true;
2726 }
2727 }
Sean Callananebe60672011-10-13 21:50:33 +00002728
2729 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002730 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002731
Greg Clayton2506a7a2011-10-12 23:34:26 +00002732 return false;
2733}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002734uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002735SymbolFileDWARF::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 +00002736{
Greg Clayton21f2a492011-10-06 00:09:08 +00002737 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2738
2739 if (log)
2740 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002741 GetObjectFile()->GetModule()->LogMessage (log.get(),
2742 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2743 name.GetCString(),
2744 namespace_decl,
2745 append,
2746 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002747 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002748
2749 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2750 return 0;
2751
Greg Claytonc685f8e2010-09-15 04:15:46 +00002752 DWARFDebugInfo* info = DebugInfo();
2753 if (info == NULL)
2754 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002755
2756 // If we aren't appending the results to this list, then clear the list
2757 if (!append)
2758 variables.Clear();
2759
2760 // Remember how many variables are in the list before we search in case
2761 // we are appending the results to a variable list.
2762 const uint32_t original_size = variables.GetSize();
2763
Greg Claytond4a2b372011-09-12 23:21:58 +00002764 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002765
Greg Clayton97fbc342011-10-20 22:30:33 +00002766 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002767 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002768 if (m_apple_names_ap.get())
2769 {
2770 const char *name_cstr = name.GetCString();
2771 const char *base_name_start;
2772 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002773
Greg Clayton97fbc342011-10-20 22:30:33 +00002774 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2775 base_name_start = name_cstr;
2776
2777 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2778 }
Greg Clayton7f995132011-10-04 22:41:51 +00002779 }
2780 else
2781 {
2782 // Index the DWARF if we haven't already
2783 if (!m_indexed)
2784 Index ();
2785
2786 m_global_index.Find (name, die_offsets);
2787 }
2788
Greg Clayton437a1352012-04-09 22:43:43 +00002789 const size_t num_die_matches = die_offsets.size();
2790 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002791 {
Greg Clayton7f995132011-10-04 22:41:51 +00002792 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002793 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002794 assert (sc.module_sp);
2795
Greg Claytond4a2b372011-09-12 23:21:58 +00002796 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002797 DWARFCompileUnit* dwarf_cu = NULL;
2798 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00002799 bool done = false;
2800 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002801 {
2802 const dw_offset_t die_offset = die_offsets[i];
2803 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002804
Greg Clayton95d87902011-11-11 03:16:25 +00002805 if (die)
2806 {
Greg Clayton437a1352012-04-09 22:43:43 +00002807 switch (die->Tag())
2808 {
2809 default:
2810 case DW_TAG_subprogram:
2811 case DW_TAG_inlined_subroutine:
2812 case DW_TAG_try_block:
2813 case DW_TAG_catch_block:
2814 break;
2815
2816 case DW_TAG_variable:
2817 {
2818 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Greg Clayton437a1352012-04-09 22:43:43 +00002819
2820 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2821 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002822
Greg Clayton437a1352012-04-09 22:43:43 +00002823 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002824
Greg Clayton437a1352012-04-09 22:43:43 +00002825 if (variables.GetSize() - original_size >= max_matches)
2826 done = true;
2827 }
2828 break;
2829 }
Greg Clayton95d87902011-11-11 03:16:25 +00002830 }
2831 else
2832 {
2833 if (m_using_apple_tables)
2834 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002835 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2836 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002837 }
2838 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002839 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002840 }
2841
2842 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002843 const uint32_t num_matches = variables.GetSize() - original_size;
2844 if (log && num_matches > 0)
2845 {
2846 GetObjectFile()->GetModule()->LogMessage (log.get(),
2847 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
2848 name.GetCString(),
2849 namespace_decl,
2850 append,
2851 max_matches,
2852 num_matches);
2853 }
2854 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002855}
2856
2857uint32_t
2858SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2859{
Greg Clayton21f2a492011-10-06 00:09:08 +00002860 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2861
2862 if (log)
2863 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002864 GetObjectFile()->GetModule()->LogMessage (log.get(),
2865 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2866 regex.GetText(),
2867 append,
2868 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002869 }
2870
Greg Claytonc685f8e2010-09-15 04:15:46 +00002871 DWARFDebugInfo* info = DebugInfo();
2872 if (info == NULL)
2873 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002874
2875 // If we aren't appending the results to this list, then clear the list
2876 if (!append)
2877 variables.Clear();
2878
2879 // Remember how many variables are in the list before we search in case
2880 // we are appending the results to a variable list.
2881 const uint32_t original_size = variables.GetSize();
2882
Greg Clayton7f995132011-10-04 22:41:51 +00002883 DIEArray die_offsets;
2884
Greg Clayton97fbc342011-10-20 22:30:33 +00002885 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002886 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002887 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002888 {
2889 DWARFMappedHash::DIEInfoArray hash_data_array;
2890 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2891 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2892 }
Greg Clayton7f995132011-10-04 22:41:51 +00002893 }
2894 else
2895 {
2896 // Index the DWARF if we haven't already
2897 if (!m_indexed)
2898 Index ();
2899
2900 m_global_index.Find (regex, die_offsets);
2901 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002902
Greg Claytonc685f8e2010-09-15 04:15:46 +00002903 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002904 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002905 assert (sc.module_sp);
2906
Greg Claytond4a2b372011-09-12 23:21:58 +00002907 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002908 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002909 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002910 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002911 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002912 DWARFDebugInfo* debug_info = DebugInfo();
2913 for (size_t i=0; i<num_matches; ++i)
2914 {
2915 const dw_offset_t die_offset = die_offsets[i];
2916 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002917
2918 if (die)
2919 {
2920 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002921
Greg Clayton95d87902011-11-11 03:16:25 +00002922 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002923
Greg Clayton95d87902011-11-11 03:16:25 +00002924 if (variables.GetSize() - original_size >= max_matches)
2925 break;
2926 }
2927 else
2928 {
2929 if (m_using_apple_tables)
2930 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002931 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2932 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002933 }
2934 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002935 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002936 }
2937
2938 // Return the number of variable that were appended to the list
2939 return variables.GetSize() - original_size;
2940}
2941
Greg Claytonaa044962011-10-13 00:59:38 +00002942
Jim Ingham4cda6e02011-10-07 22:23:45 +00002943bool
2944SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2945 DWARFCompileUnit *&dwarf_cu,
2946 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002947{
Greg Claytonaa044962011-10-13 00:59:38 +00002948 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2949 return ResolveFunction (dwarf_cu, die, sc_list);
2950}
2951
2952
2953bool
2954SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2955 const DWARFDebugInfoEntry *die,
2956 SymbolContextList& sc_list)
2957{
Greg Clayton9e315582011-09-02 04:03:59 +00002958 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002959
2960 if (die == NULL)
2961 return false;
2962
Jim Ingham4cda6e02011-10-07 22:23:45 +00002963 // If we were passed a die that is not a function, just return false...
2964 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2965 return false;
2966
2967 const DWARFDebugInfoEntry* inlined_die = NULL;
2968 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002969 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002970 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002971
Jim Ingham4cda6e02011-10-07 22:23:45 +00002972 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002973 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002974 if (die->Tag() == DW_TAG_subprogram)
2975 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002976 }
2977 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002978 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002979 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002980 {
2981 Address addr;
2982 // Parse all blocks if needed
2983 if (inlined_die)
2984 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002985 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00002986 assert (sc.block != NULL);
2987 if (sc.block->GetStartAddress (addr) == false)
2988 addr.Clear();
2989 }
2990 else
2991 {
2992 sc.block = NULL;
2993 addr = sc.function->GetAddressRange().GetBaseAddress();
2994 }
2995
2996 if (addr.IsValid())
2997 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002998 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002999 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003000 }
3001 }
3002
Greg Claytonaa044962011-10-13 00:59:38 +00003003 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00003004}
3005
Greg Clayton7f995132011-10-04 22:41:51 +00003006void
3007SymbolFileDWARF::FindFunctions (const ConstString &name,
3008 const NameToDIE &name_to_die,
3009 SymbolContextList& sc_list)
3010{
Greg Claytond4a2b372011-09-12 23:21:58 +00003011 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003012 if (name_to_die.Find (name, die_offsets))
3013 {
3014 ParseFunctions (die_offsets, sc_list);
3015 }
3016}
3017
3018
3019void
3020SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3021 const NameToDIE &name_to_die,
3022 SymbolContextList& sc_list)
3023{
3024 DIEArray die_offsets;
3025 if (name_to_die.Find (regex, die_offsets))
3026 {
3027 ParseFunctions (die_offsets, sc_list);
3028 }
3029}
3030
3031
3032void
3033SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3034 const DWARFMappedHash::MemoryTable &memory_table,
3035 SymbolContextList& sc_list)
3036{
3037 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00003038 DWARFMappedHash::DIEInfoArray hash_data_array;
3039 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00003040 {
Greg Claytond1767f02011-12-08 02:13:16 +00003041 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00003042 ParseFunctions (die_offsets, sc_list);
3043 }
3044}
3045
3046void
3047SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
3048 SymbolContextList& sc_list)
3049{
3050 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003051 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00003052 {
Greg Clayton7f995132011-10-04 22:41:51 +00003053 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00003054
3055 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003056 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00003057 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003058 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00003059 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003060 }
3061 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00003062}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003063
Jim Ingham4cda6e02011-10-07 22:23:45 +00003064bool
3065SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
3066 const DWARFCompileUnit *dwarf_cu,
3067 uint32_t name_type_mask,
3068 const char *partial_name,
3069 const char *base_name_start,
3070 const char *base_name_end)
3071{
3072 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
3073 if (name_type_mask == eFunctionNameTypeMethod
3074 || name_type_mask == eFunctionNameTypeBase)
3075 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003076 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
3077 if (!containing_decl_ctx)
3078 return false;
3079
3080 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
3081
3082 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
3083 return false;
3084 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
3085 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003086 }
3087
3088 // Now we need to check whether the name we got back for this type matches the extra specifications
3089 // that were in the name we're looking up:
3090 if (base_name_start != partial_name || *base_name_end != '\0')
3091 {
3092 // First see if the stuff to the left matches the full name. To do that let's see if
3093 // we can pull out the mips linkage name attribute:
3094
3095 Mangled best_name;
3096
3097 DWARFDebugInfoEntry::Attributes attributes;
3098 die->GetAttributes(this, dwarf_cu, NULL, attributes);
3099 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
3100 if (idx != UINT32_MAX)
3101 {
3102 DWARFFormValue form_value;
3103 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3104 {
3105 const char *name = form_value.AsCString(&get_debug_str_data());
Greg Clayton037520e2012-07-18 23:18:10 +00003106 best_name.SetValue (ConstString(name), true);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003107 }
3108 }
3109 if (best_name)
3110 {
3111 const char *demangled = best_name.GetDemangledName().GetCString();
3112 if (demangled)
3113 {
3114 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003115 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3116 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003117 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003118 else
3119 {
3120 // Sort out the case where our name is something like "Process::Destroy" and the match is
3121 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3122 // namespace boundaries...
3123
3124 if (partial_name[0] == ':' && partial_name[1] == ':')
3125 {
3126 // The partial name was already on a namespace boundary so all matches are good.
3127 return true;
3128 }
3129 else if (partial_in_demangled == demangled)
3130 {
3131 // They both start the same, so this is an good match.
3132 return true;
3133 }
3134 else
3135 {
3136 if (partial_in_demangled - demangled == 1)
3137 {
3138 // Only one character difference, can't be a namespace boundary...
3139 return false;
3140 }
3141 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3142 {
3143 // We are on a namespace boundary, so this is also good.
3144 return true;
3145 }
3146 else
3147 return false;
3148 }
3149 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003150 }
3151 }
3152 }
3153
3154 return true;
3155}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003156
Greg Clayton0c5cd902010-06-28 21:30:43 +00003157uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003158SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003159 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003160 uint32_t name_type_mask,
3161 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003162 bool append,
3163 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003164{
3165 Timer scoped_timer (__PRETTY_FUNCTION__,
3166 "SymbolFileDWARF::FindFunctions (name = '%s')",
3167 name.AsCString());
3168
Greg Clayton21f2a492011-10-06 00:09:08 +00003169 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3170
3171 if (log)
3172 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003173 GetObjectFile()->GetModule()->LogMessage (log.get(),
3174 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3175 name.GetCString(),
3176 name_type_mask,
3177 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003178 }
3179
Greg Clayton0c5cd902010-06-28 21:30:43 +00003180 // If we aren't appending the results to this list, then clear the list
3181 if (!append)
3182 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003183
3184 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3185 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003186
3187 // If name is empty then we won't find anything.
3188 if (name.IsEmpty())
3189 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003190
3191 // Remember how many sc_list are in the list before we search in case
3192 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003193
Greg Clayton9e315582011-09-02 04:03:59 +00003194 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00003195
Jim Ingham4cda6e02011-10-07 22:23:45 +00003196 const char *name_cstr = name.GetCString();
3197 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
3198 const char *base_name_start = name_cstr;
3199 const char *base_name_end = name_cstr + strlen(name_cstr);
3200
3201 if (name_type_mask & eFunctionNameTypeAuto)
3202 {
3203 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
3204 effective_name_type_mask = eFunctionNameTypeFull;
3205 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
3206 effective_name_type_mask = eFunctionNameTypeFull;
3207 else
3208 {
3209 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3210 effective_name_type_mask |= eFunctionNameTypeSelector;
3211
3212 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3213 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
3214 }
3215 }
3216 else
3217 {
3218 effective_name_type_mask = name_type_mask;
3219 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
3220 {
3221 // If they've asked for a CPP method or function name and it can't be that, we don't
3222 // even need to search for CPP methods or names.
3223 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3224 {
3225 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
3226 if (effective_name_type_mask == eFunctionNameTypeNone)
3227 return 0;
3228 }
3229 }
3230
3231 if (effective_name_type_mask & eFunctionNameTypeSelector)
3232 {
3233 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3234 {
3235 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
3236 if (effective_name_type_mask == eFunctionNameTypeNone)
3237 return 0;
3238 }
3239 }
3240 }
3241
3242 DWARFDebugInfo* info = DebugInfo();
3243 if (info == NULL)
3244 return 0;
3245
Greg Claytonaa044962011-10-13 00:59:38 +00003246 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003247 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003248 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003249 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003250 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003251
3252 DIEArray die_offsets;
3253
3254 uint32_t num_matches = 0;
3255
3256 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003257 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003258 // If they asked for the full name, match what they typed. At some point we may
3259 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3260 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003261 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003262 for (uint32_t i = 0; i < num_matches; i++)
3263 {
Greg Clayton95d87902011-11-11 03:16:25 +00003264 const dw_offset_t die_offset = die_offsets[i];
3265 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003266 if (die)
3267 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003268 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3269 continue;
3270
Sean Callanan9df05fb2012-02-10 22:52:19 +00003271 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3272 continue;
3273
Greg Claytonaa044962011-10-13 00:59:38 +00003274 ResolveFunction (dwarf_cu, die, sc_list);
3275 }
Greg Clayton95d87902011-11-11 03:16:25 +00003276 else
3277 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003278 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3279 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003280 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003281 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003282 }
3283 else
3284 {
3285 if (effective_name_type_mask & eFunctionNameTypeSelector)
3286 {
3287 if (namespace_decl && *namespace_decl)
3288 return 0; // no selectors in namespaces
3289
3290 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3291 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3292 // and if it is an ObjC method name, we're good.
3293
3294 for (uint32_t i = 0; i < num_matches; i++)
3295 {
Greg Clayton95d87902011-11-11 03:16:25 +00003296 const dw_offset_t die_offset = die_offsets[i];
3297 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003298 if (die)
3299 {
3300 const char *die_name = die->GetName(this, dwarf_cu);
3301 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003302 {
3303 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3304 continue;
3305
Greg Clayton97fbc342011-10-20 22:30:33 +00003306 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003307 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003308 }
Greg Clayton95d87902011-11-11 03:16:25 +00003309 else
3310 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003311 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3312 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003313 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003314 }
3315 die_offsets.clear();
3316 }
3317
3318 if (effective_name_type_mask & eFunctionNameTypeMethod
3319 || effective_name_type_mask & eFunctionNameTypeBase)
3320 {
3321 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3322 (namespace_decl && *namespace_decl))
3323 return 0; // no methods in namespaces
3324
3325 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3326 // extract the base name, look that up, and if there is any other information in the name we were
3327 // passed in we have to post-filter based on that.
3328
3329 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3330 std::string base_name(base_name_start, base_name_end - base_name_start);
3331 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3332
3333 for (uint32_t i = 0; i < num_matches; i++)
3334 {
Greg Clayton95d87902011-11-11 03:16:25 +00003335 const dw_offset_t die_offset = die_offsets[i];
3336 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003337 if (die)
3338 {
3339 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3340 continue;
3341
3342 if (!FunctionDieMatchesPartialName(die,
3343 dwarf_cu,
3344 effective_name_type_mask,
3345 name_cstr,
3346 base_name_start,
3347 base_name_end))
3348 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003349
3350 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3351 continue;
Greg Clayton97fbc342011-10-20 22:30:33 +00003352
3353 // If we get to here, the die is good, and we should add it:
3354 ResolveFunction (dwarf_cu, die, sc_list);
3355 }
Greg Clayton95d87902011-11-11 03:16:25 +00003356 else
3357 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003358 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3359 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003360 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003361 }
3362 die_offsets.clear();
3363 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003364 }
3365 }
Greg Clayton7f995132011-10-04 22:41:51 +00003366 }
3367 else
3368 {
3369
3370 // Index the DWARF if we haven't already
3371 if (!m_indexed)
3372 Index ();
3373
Greg Clayton7f995132011-10-04 22:41:51 +00003374 if (name_type_mask & eFunctionNameTypeFull)
3375 FindFunctions (name, m_function_fullname_index, sc_list);
3376
Jim Ingham4cda6e02011-10-07 22:23:45 +00003377 std::string base_name(base_name_start, base_name_end - base_name_start);
3378 ConstString base_name_const(base_name.c_str());
3379 DIEArray die_offsets;
3380 DWARFCompileUnit *dwarf_cu = NULL;
3381
3382 if (effective_name_type_mask & eFunctionNameTypeBase)
3383 {
3384 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003385 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003386 {
Greg Claytonaa044962011-10-13 00:59:38 +00003387 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3388 if (die)
3389 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003390 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3391 continue;
3392
Greg Claytonaa044962011-10-13 00:59:38 +00003393 if (!FunctionDieMatchesPartialName(die,
3394 dwarf_cu,
3395 effective_name_type_mask,
3396 name_cstr,
3397 base_name_start,
3398 base_name_end))
3399 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003400
Sean Callanan9df05fb2012-02-10 22:52:19 +00003401 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3402 continue;
3403
Greg Claytonaa044962011-10-13 00:59:38 +00003404 // If we get to here, the die is good, and we should add it:
3405 ResolveFunction (dwarf_cu, die, sc_list);
3406 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003407 }
3408 die_offsets.clear();
3409 }
3410
Jim Inghamea8005a2011-10-11 01:18:11 +00003411 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003412 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003413 if (namespace_decl && *namespace_decl)
3414 return 0; // no methods in namespaces
3415
Jim Ingham4cda6e02011-10-07 22:23:45 +00003416 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3417 {
Greg Claytonaa044962011-10-13 00:59:38 +00003418 for (uint32_t i = 0; i < num_base; i++)
3419 {
3420 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3421 if (die)
3422 {
3423 if (!FunctionDieMatchesPartialName(die,
3424 dwarf_cu,
3425 effective_name_type_mask,
3426 name_cstr,
3427 base_name_start,
3428 base_name_end))
3429 continue;
3430
Sean Callanan9df05fb2012-02-10 22:52:19 +00003431 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3432 continue;
3433
Greg Claytonaa044962011-10-13 00:59:38 +00003434 // If we get to here, the die is good, and we should add it:
3435 ResolveFunction (dwarf_cu, die, sc_list);
3436 }
3437 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003438 }
3439 die_offsets.clear();
3440 }
Greg Clayton7f995132011-10-04 22:41:51 +00003441
Sean Callanan213fdb82011-10-13 01:49:10 +00003442 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003443 {
Greg Clayton7f995132011-10-04 22:41:51 +00003444 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003445 }
3446
Greg Clayton4d01ace2011-09-29 16:58:15 +00003447 }
3448
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003449 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003450 const uint32_t num_matches = sc_list.GetSize() - original_size;
3451
3452 if (log && num_matches > 0)
3453 {
3454 GetObjectFile()->GetModule()->LogMessage (log.get(),
3455 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u",
3456 name.GetCString(),
3457 name_type_mask,
3458 append,
3459 num_matches);
3460 }
3461 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003462}
3463
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003464uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003465SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003466{
3467 Timer scoped_timer (__PRETTY_FUNCTION__,
3468 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3469 regex.GetText());
3470
Greg Clayton21f2a492011-10-06 00:09:08 +00003471 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3472
3473 if (log)
3474 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003475 GetObjectFile()->GetModule()->LogMessage (log.get(),
3476 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3477 regex.GetText(),
3478 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003479 }
3480
3481
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003482 // If we aren't appending the results to this list, then clear the list
3483 if (!append)
3484 sc_list.Clear();
3485
3486 // Remember how many sc_list are in the list before we search in case
3487 // we are appending the results to a variable list.
3488 uint32_t original_size = sc_list.GetSize();
3489
Greg Clayton97fbc342011-10-20 22:30:33 +00003490 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003491 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003492 if (m_apple_names_ap.get())
3493 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003494 }
3495 else
3496 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003497 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003498 if (!m_indexed)
3499 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003500
Greg Clayton7f995132011-10-04 22:41:51 +00003501 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003502
Greg Clayton7f995132011-10-04 22:41:51 +00003503 FindFunctions (regex, m_function_fullname_index, sc_list);
3504 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003505
3506 // Return the number of variable that were appended to the list
3507 return sc_list.GetSize() - original_size;
3508}
Jim Ingham318c9f22011-08-26 19:44:13 +00003509
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003510uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003511SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3512 const ConstString &name,
3513 const lldb_private::ClangNamespaceDecl *namespace_decl,
3514 bool append,
3515 uint32_t max_matches,
3516 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003517{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003518 DWARFDebugInfo* info = DebugInfo();
3519 if (info == NULL)
3520 return 0;
3521
Greg Clayton21f2a492011-10-06 00:09:08 +00003522 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3523
3524 if (log)
3525 {
Greg Clayton437a1352012-04-09 22:43:43 +00003526 if (namespace_decl)
3527 {
3528 GetObjectFile()->GetModule()->LogMessage (log.get(),
3529 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
3530 name.GetCString(),
3531 namespace_decl->GetNamespaceDecl(),
3532 namespace_decl->GetQualifiedName().c_str(),
3533 append,
3534 max_matches);
3535 }
3536 else
3537 {
3538 GetObjectFile()->GetModule()->LogMessage (log.get(),
3539 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
3540 name.GetCString(),
3541 append,
3542 max_matches);
3543 }
Greg Clayton21f2a492011-10-06 00:09:08 +00003544 }
3545
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003546 // If we aren't appending the results to this list, then clear the list
3547 if (!append)
3548 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003549
3550 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3551 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003552
Greg Claytond4a2b372011-09-12 23:21:58 +00003553 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003554
Greg Clayton97fbc342011-10-20 22:30:33 +00003555 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003556 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003557 if (m_apple_types_ap.get())
3558 {
3559 const char *name_cstr = name.GetCString();
3560 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3561 }
Greg Clayton7f995132011-10-04 22:41:51 +00003562 }
3563 else
3564 {
3565 if (!m_indexed)
3566 Index ();
3567
3568 m_type_index.Find (name, die_offsets);
3569 }
3570
Greg Clayton437a1352012-04-09 22:43:43 +00003571 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003572
Greg Clayton437a1352012-04-09 22:43:43 +00003573 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003574 {
Greg Clayton7f995132011-10-04 22:41:51 +00003575 const uint32_t initial_types_size = types.GetSize();
3576 DWARFCompileUnit* dwarf_cu = NULL;
3577 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003578 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003579 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003580 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003581 const dw_offset_t die_offset = die_offsets[i];
3582 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3583
Greg Clayton95d87902011-11-11 03:16:25 +00003584 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003585 {
Greg Clayton95d87902011-11-11 03:16:25 +00003586 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3587 continue;
3588
3589 Type *matching_type = ResolveType (dwarf_cu, die);
3590 if (matching_type)
3591 {
3592 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003593 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003594 if (types.GetSize() >= max_matches)
3595 break;
3596 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003597 }
Greg Clayton95d87902011-11-11 03:16:25 +00003598 else
3599 {
3600 if (m_using_apple_tables)
3601 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003602 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3603 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003604 }
3605 }
3606
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003607 }
Greg Clayton437a1352012-04-09 22:43:43 +00003608 const uint32_t num_matches = types.GetSize() - initial_types_size;
3609 if (log && num_matches)
3610 {
3611 if (namespace_decl)
3612 {
3613 GetObjectFile()->GetModule()->LogMessage (log.get(),
3614 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
3615 name.GetCString(),
3616 namespace_decl->GetNamespaceDecl(),
3617 namespace_decl->GetQualifiedName().c_str(),
3618 append,
3619 max_matches,
3620 num_matches);
3621 }
3622 else
3623 {
3624 GetObjectFile()->GetModule()->LogMessage (log.get(),
3625 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
3626 name.GetCString(),
3627 append,
3628 max_matches,
3629 num_matches);
3630 }
3631 }
3632 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003633 }
Greg Clayton7f995132011-10-04 22:41:51 +00003634 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003635}
3636
3637
Greg Clayton526e5af2010-11-13 03:52:47 +00003638ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003639SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003640 const ConstString &name,
3641 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003642{
Greg Clayton21f2a492011-10-06 00:09:08 +00003643 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3644
3645 if (log)
3646 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003647 GetObjectFile()->GetModule()->LogMessage (log.get(),
3648 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3649 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003650 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003651
3652 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3653 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003654
Greg Clayton526e5af2010-11-13 03:52:47 +00003655 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003656 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003657 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003658 {
Greg Clayton7f995132011-10-04 22:41:51 +00003659 DIEArray die_offsets;
3660
Greg Clayton526e5af2010-11-13 03:52:47 +00003661 // Index if we already haven't to make sure the compile units
3662 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003663 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003664 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003665 if (m_apple_namespaces_ap.get())
3666 {
3667 const char *name_cstr = name.GetCString();
3668 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3669 }
Greg Clayton7f995132011-10-04 22:41:51 +00003670 }
3671 else
3672 {
3673 if (!m_indexed)
3674 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003675
Greg Clayton7f995132011-10-04 22:41:51 +00003676 m_namespace_index.Find (name, die_offsets);
3677 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003678
3679 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003680 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003681 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003682 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003683 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003684 DWARFDebugInfo* debug_info = DebugInfo();
3685 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003686 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003687 const dw_offset_t die_offset = die_offsets[i];
3688 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003689
Greg Clayton95d87902011-11-11 03:16:25 +00003690 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003691 {
Greg Clayton95d87902011-11-11 03:16:25 +00003692 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3693 continue;
3694
3695 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3696 if (clang_namespace_decl)
3697 {
3698 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3699 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003700 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003701 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003702 }
Greg Clayton95d87902011-11-11 03:16:25 +00003703 else
3704 {
3705 if (m_using_apple_tables)
3706 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003707 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3708 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003709 }
3710 }
3711
Greg Clayton526e5af2010-11-13 03:52:47 +00003712 }
3713 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003714 }
Greg Clayton437a1352012-04-09 22:43:43 +00003715 if (log && namespace_decl.GetNamespaceDecl())
3716 {
3717 GetObjectFile()->GetModule()->LogMessage (log.get(),
3718 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
3719 name.GetCString(),
3720 namespace_decl.GetNamespaceDecl(),
3721 namespace_decl.GetQualifiedName().c_str());
3722 }
3723
Greg Clayton526e5af2010-11-13 03:52:47 +00003724 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003725}
3726
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003727uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003728SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003729{
3730 // Remember how many sc_list are in the list before we search in case
3731 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003732 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003733
3734 const uint32_t num_die_offsets = die_offsets.size();
3735 // Parse all of the types we found from the pubtypes matches
3736 uint32_t i;
3737 uint32_t num_matches = 0;
3738 for (i = 0; i < num_die_offsets; ++i)
3739 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003740 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3741 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003742 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003743 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003744 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003745 ++num_matches;
3746 if (num_matches >= max_matches)
3747 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003748 }
3749 }
3750
3751 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003752 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003753}
3754
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003755
3756size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003757SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3758 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003759 DWARFCompileUnit* dwarf_cu,
3760 const DWARFDebugInfoEntry *parent_die,
3761 bool skip_artificial,
3762 bool &is_static,
3763 TypeList* type_list,
3764 std::vector<clang_type_t>& function_param_types,
3765 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003766 unsigned &type_quals,
3767 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003768{
3769 if (parent_die == NULL)
3770 return 0;
3771
Greg Claytond88d7592010-09-15 08:33:30 +00003772 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3773
Greg Clayton7fedea22010-11-16 02:10:54 +00003774 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003775 const DWARFDebugInfoEntry *die;
3776 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3777 {
3778 dw_tag_t tag = die->Tag();
3779 switch (tag)
3780 {
3781 case DW_TAG_formal_parameter:
3782 {
3783 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003784 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003785 if (num_attributes > 0)
3786 {
3787 const char *name = NULL;
3788 Declaration decl;
3789 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003790 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003791 // one of None, Auto, Register, Extern, Static, PrivateExtern
3792
Sean Callanane2ef6e32010-09-23 03:01:22 +00003793 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003794 uint32_t i;
3795 for (i=0; i<num_attributes; ++i)
3796 {
3797 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3798 DWARFFormValue form_value;
3799 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3800 {
3801 switch (attr)
3802 {
3803 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3804 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3805 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3806 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3807 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003808 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003809 case DW_AT_location:
3810 // if (form_value.BlockData())
3811 // {
3812 // const DataExtractor& debug_info_data = debug_info();
3813 // uint32_t block_length = form_value.Unsigned();
3814 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3815 // }
3816 // else
3817 // {
3818 // }
3819 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003820 case DW_AT_const_value:
3821 case DW_AT_default_value:
3822 case DW_AT_description:
3823 case DW_AT_endianity:
3824 case DW_AT_is_optional:
3825 case DW_AT_segment:
3826 case DW_AT_variable_parameter:
3827 default:
3828 case DW_AT_abstract_origin:
3829 case DW_AT_sibling:
3830 break;
3831 }
3832 }
3833 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003834
Greg Clayton0fffff52010-09-24 05:15:53 +00003835 bool skip = false;
3836 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003837 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003838 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003839 {
3840 // In order to determine if a C++ member function is
3841 // "const" we have to look at the const-ness of "this"...
3842 // Ugly, but that
3843 if (arg_idx == 0)
3844 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003845 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003846 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003847 // Often times compilers omit the "this" name for the
3848 // specification DIEs, so we can't rely upon the name
3849 // being in the formal parameter DIE...
3850 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003851 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003852 Type *this_type = ResolveTypeUID (param_type_die_offset);
3853 if (this_type)
3854 {
3855 uint32_t encoding_mask = this_type->GetEncodingMask();
3856 if (encoding_mask & Type::eEncodingIsPointerUID)
3857 {
3858 is_static = false;
3859
3860 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3861 type_quals |= clang::Qualifiers::Const;
3862 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3863 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003864 }
3865 }
3866 }
3867 }
3868 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003869 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003870 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003871 else
3872 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003873
Greg Clayton0fffff52010-09-24 05:15:53 +00003874 // HACK: Objective C formal parameters "self" and "_cmd"
3875 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00003876 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3877 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00003878 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003879 switch (comp_unit->GetLanguage())
3880 {
3881 case eLanguageTypeObjC:
3882 case eLanguageTypeObjC_plus_plus:
3883 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3884 skip = true;
3885 break;
3886 default:
3887 break;
3888 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003889 }
3890 }
3891 }
3892
3893 if (!skip)
3894 {
3895 Type *type = ResolveTypeUID(param_type_die_offset);
3896 if (type)
3897 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003898 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003899
Greg Clayton42ce2f32011-12-12 21:50:19 +00003900 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3901 type->GetClangForwardType(),
3902 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003903 assert(param_var_decl);
3904 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00003905
3906 GetClangASTContext().SetMetadata((uintptr_t)param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00003907 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003908 }
3909 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003910 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003911 }
3912 break;
3913
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003914 case DW_TAG_template_type_parameter:
3915 case DW_TAG_template_value_parameter:
3916 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
3917 break;
3918
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003919 default:
3920 break;
3921 }
3922 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003923 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003924}
3925
3926size_t
3927SymbolFileDWARF::ParseChildEnumerators
3928(
3929 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003930 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003931 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003932 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003933 const DWARFDebugInfoEntry *parent_die
3934)
3935{
3936 if (parent_die == NULL)
3937 return 0;
3938
3939 size_t enumerators_added = 0;
3940 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003941 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3942
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003943 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3944 {
3945 const dw_tag_t tag = die->Tag();
3946 if (tag == DW_TAG_enumerator)
3947 {
3948 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003949 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003950 if (num_child_attributes > 0)
3951 {
3952 const char *name = NULL;
3953 bool got_value = false;
3954 int64_t enum_value = 0;
3955 Declaration decl;
3956
3957 uint32_t i;
3958 for (i=0; i<num_child_attributes; ++i)
3959 {
3960 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3961 DWARFFormValue form_value;
3962 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3963 {
3964 switch (attr)
3965 {
3966 case DW_AT_const_value:
3967 got_value = true;
3968 enum_value = form_value.Unsigned();
3969 break;
3970
3971 case DW_AT_name:
3972 name = form_value.AsCString(&get_debug_str_data());
3973 break;
3974
3975 case DW_AT_description:
3976 default:
3977 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3978 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3979 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3980 case DW_AT_sibling:
3981 break;
3982 }
3983 }
3984 }
3985
3986 if (name && name[0] && got_value)
3987 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003988 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3989 enumerator_clang_type,
3990 decl,
3991 name,
3992 enum_value,
3993 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003994 ++enumerators_added;
3995 }
3996 }
3997 }
3998 }
3999 return enumerators_added;
4000}
4001
4002void
4003SymbolFileDWARF::ParseChildArrayInfo
4004(
4005 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004006 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004007 const DWARFDebugInfoEntry *parent_die,
4008 int64_t& first_index,
4009 std::vector<uint64_t>& element_orders,
4010 uint32_t& byte_stride,
4011 uint32_t& bit_stride
4012)
4013{
4014 if (parent_die == NULL)
4015 return;
4016
4017 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00004018 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004019 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4020 {
4021 const dw_tag_t tag = die->Tag();
4022 switch (tag)
4023 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004024 case DW_TAG_subrange_type:
4025 {
4026 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004027 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004028 if (num_child_attributes > 0)
4029 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004030 uint64_t num_elements = 0;
4031 uint64_t lower_bound = 0;
4032 uint64_t upper_bound = 0;
4033 uint32_t i;
4034 for (i=0; i<num_child_attributes; ++i)
4035 {
4036 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4037 DWARFFormValue form_value;
4038 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4039 {
4040 switch (attr)
4041 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004042 case DW_AT_name:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004043 break;
4044
4045 case DW_AT_count:
4046 num_elements = form_value.Unsigned();
4047 break;
4048
4049 case DW_AT_bit_stride:
4050 bit_stride = form_value.Unsigned();
4051 break;
4052
4053 case DW_AT_byte_stride:
4054 byte_stride = form_value.Unsigned();
4055 break;
4056
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004057 case DW_AT_lower_bound:
4058 lower_bound = form_value.Unsigned();
4059 break;
4060
4061 case DW_AT_upper_bound:
4062 upper_bound = form_value.Unsigned();
4063 break;
4064
4065 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004066 case DW_AT_abstract_origin:
4067 case DW_AT_accessibility:
4068 case DW_AT_allocated:
4069 case DW_AT_associated:
4070 case DW_AT_data_location:
4071 case DW_AT_declaration:
4072 case DW_AT_description:
4073 case DW_AT_sibling:
4074 case DW_AT_threads_scaled:
4075 case DW_AT_type:
4076 case DW_AT_visibility:
4077 break;
4078 }
4079 }
4080 }
4081
4082 if (upper_bound > lower_bound)
4083 num_elements = upper_bound - lower_bound + 1;
4084
4085 if (num_elements > 0)
4086 element_orders.push_back (num_elements);
4087 }
4088 }
4089 break;
4090 }
4091 }
4092}
4093
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004094TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004095SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004096{
4097 TypeSP type_sp;
4098 if (die != NULL)
4099 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004100 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004101 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004102 if (type_ptr == NULL)
4103 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004104 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004105 assert (lldb_cu);
4106 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004107 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004108 }
4109 else if (type_ptr != DIE_IS_BEING_PARSED)
4110 {
4111 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004112 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004113 }
4114
4115 }
4116 return type_sp;
4117}
4118
4119clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004120SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004121{
4122 if (die_offset != DW_INVALID_OFFSET)
4123 {
4124 DWARFCompileUnitSP cu_sp;
4125 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004126 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004127 }
4128 return NULL;
4129}
4130
Sean Callanan72e49402011-08-05 23:43:37 +00004131clang::DeclContext *
4132SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4133{
4134 if (die_offset != DW_INVALID_OFFSET)
4135 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004136 DWARFDebugInfo* debug_info = DebugInfo();
4137 if (debug_info)
4138 {
4139 DWARFCompileUnitSP cu_sp;
4140 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4141 if (die)
4142 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4143 }
Sean Callanan72e49402011-08-05 23:43:37 +00004144 }
4145 return NULL;
4146}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004147
Greg Clayton96d7d742010-11-10 23:42:09 +00004148clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004149SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004150{
Greg Clayton030a2042011-10-14 21:34:45 +00004151 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004152 {
Greg Clayton030a2042011-10-14 21:34:45 +00004153 // See if we already parsed this namespace DIE and associated it with a
4154 // uniqued namespace declaration
4155 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4156 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004157 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004158 else
4159 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004160 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4161 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004162 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
4163 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4164 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004165 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004166 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004167 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004168 GetObjectFile()->GetModule()->LogMessage (log.get(),
4169 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
4170 GetClangASTContext().getASTContext(),
4171 MakeUserID(die->GetOffset()),
4172 namespace_name,
4173 namespace_decl,
4174 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00004175 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004176 else
4177 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004178 GetObjectFile()->GetModule()->LogMessage (log.get(),
4179 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
4180 GetClangASTContext().getASTContext(),
4181 MakeUserID(die->GetOffset()),
4182 namespace_decl,
4183 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00004184 }
Greg Clayton030a2042011-10-14 21:34:45 +00004185 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004186
4187 if (namespace_decl)
4188 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4189 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004190 }
4191 }
4192 return NULL;
4193}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004194
4195clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004196SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004197{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004198 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4199 if (clang_decl_ctx)
4200 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004201 // If this DIE has a specification, or an abstract origin, then trace to those.
4202
Greg Claytoncab36a32011-12-08 05:16:30 +00004203 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004204 if (die_offset != DW_INVALID_OFFSET)
4205 return GetClangDeclContextForDIEOffset (sc, die_offset);
4206
Greg Claytoncab36a32011-12-08 05:16:30 +00004207 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004208 if (die_offset != DW_INVALID_OFFSET)
4209 return GetClangDeclContextForDIEOffset (sc, die_offset);
4210
Greg Clayton3bffb082011-12-10 02:15:28 +00004211 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4212 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004213 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 +00004214 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004215 bool assert_not_being_parsed = true;
4216 ResolveTypeUID (cu, die, assert_not_being_parsed);
4217
Greg Clayton5cf58b92011-10-05 22:22:08 +00004218 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4219
4220 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004221}
4222
4223clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004224SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004225{
Greg Claytonca512b32011-01-14 04:54:56 +00004226 if (m_clang_tu_decl == NULL)
4227 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004228
Greg Clayton2bc22f82011-09-30 03:20:47 +00004229 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004230
4231 if (decl_ctx_die_copy)
4232 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004233
4234 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004235 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004236
Greg Clayton2bc22f82011-09-30 03:20:47 +00004237 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4238 if (pos != m_die_to_decl_ctx.end())
4239 return pos->second;
4240
4241 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004242 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004243 case DW_TAG_compile_unit:
4244 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004245
Greg Clayton2bc22f82011-09-30 03:20:47 +00004246 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004247 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004248 break;
4249
4250 case DW_TAG_structure_type:
4251 case DW_TAG_union_type:
4252 case DW_TAG_class_type:
4253 {
4254 Type* type = ResolveType (cu, decl_ctx_die);
4255 if (type)
4256 {
4257 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4258 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004259 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004260 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4261 if (decl_ctx)
4262 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004263 }
4264 }
Greg Claytonca512b32011-01-14 04:54:56 +00004265 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004266 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004267
Greg Clayton2bc22f82011-09-30 03:20:47 +00004268 default:
4269 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004270 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004271 }
Greg Clayton7a345282010-11-09 23:46:37 +00004272 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004273}
4274
Greg Clayton2bc22f82011-09-30 03:20:47 +00004275
4276const DWARFDebugInfoEntry *
4277SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4278{
4279 if (cu && die)
4280 {
4281 const DWARFDebugInfoEntry * const decl_die = die;
4282
4283 while (die != NULL)
4284 {
4285 // If this is the original DIE that we are searching for a declaration
4286 // for, then don't look in the cache as we don't want our own decl
4287 // context to be our decl context...
4288 if (decl_die != die)
4289 {
4290 switch (die->Tag())
4291 {
4292 case DW_TAG_compile_unit:
4293 case DW_TAG_namespace:
4294 case DW_TAG_structure_type:
4295 case DW_TAG_union_type:
4296 case DW_TAG_class_type:
4297 return die;
4298
4299 default:
4300 break;
4301 }
4302 }
4303
4304 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4305 if (die_offset != DW_INVALID_OFFSET)
4306 {
4307 DWARFCompileUnit *spec_cu = cu;
4308 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4309 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4310 if (spec_die_decl_ctx_die)
4311 return spec_die_decl_ctx_die;
4312 }
4313
4314 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4315 if (die_offset != DW_INVALID_OFFSET)
4316 {
4317 DWARFCompileUnit *abs_cu = cu;
4318 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4319 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4320 if (abs_die_decl_ctx_die)
4321 return abs_die_decl_ctx_die;
4322 }
4323
4324 die = die->GetParent();
4325 }
4326 }
4327 return NULL;
4328}
4329
4330
Greg Clayton901c5ca2011-12-03 04:40:03 +00004331Symbol *
4332SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4333{
4334 Symbol *objc_class_symbol = NULL;
4335 if (m_obj_file)
4336 {
4337 Symtab *symtab = m_obj_file->GetSymtab();
4338 if (symtab)
4339 {
4340 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4341 eSymbolTypeObjCClass,
4342 Symtab::eDebugNo,
4343 Symtab::eVisibilityAny);
4344 }
4345 }
4346 return objc_class_symbol;
4347}
4348
Greg Claytonc7f03b62012-01-12 04:33:28 +00004349// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4350// then we can end up looking through all class types for a complete type and never find
4351// the full definition. We need to know if this attribute is supported, so we determine
4352// this here and cache th result. We also need to worry about the debug map DWARF file
4353// if we are doing darwin DWARF in .o file debugging.
4354bool
4355SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4356{
4357 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4358 {
4359 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4360 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4361 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4362 else
4363 {
4364 DWARFDebugInfo* debug_info = DebugInfo();
4365 const uint32_t num_compile_units = GetNumCompileUnits();
4366 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4367 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004368 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4369 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004370 {
4371 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4372 break;
4373 }
4374 }
4375 }
Greg Clayton1f746072012-08-29 21:13:06 +00004376 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004377 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4378 }
4379 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4380}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004381
4382// This function can be used when a DIE is found that is a forward declaration
4383// DIE and we want to try and find a type that has the complete definition.
4384TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004385SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4386 const ConstString &type_name,
4387 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004388{
4389
4390 TypeSP type_sp;
4391
Greg Claytonc7f03b62012-01-12 04:33:28 +00004392 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004393 return type_sp;
4394
4395 DIEArray die_offsets;
4396
4397 if (m_using_apple_tables)
4398 {
4399 if (m_apple_types_ap.get())
4400 {
4401 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004402 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004403 }
4404 }
4405 else
4406 {
4407 if (!m_indexed)
4408 Index ();
4409
4410 m_type_index.Find (type_name, die_offsets);
4411 }
4412
Greg Clayton901c5ca2011-12-03 04:40:03 +00004413 const size_t num_matches = die_offsets.size();
4414
Greg Clayton901c5ca2011-12-03 04:40:03 +00004415 DWARFCompileUnit* type_cu = NULL;
4416 const DWARFDebugInfoEntry* type_die = NULL;
4417 if (num_matches)
4418 {
4419 DWARFDebugInfo* debug_info = DebugInfo();
4420 for (size_t i=0; i<num_matches; ++i)
4421 {
4422 const dw_offset_t die_offset = die_offsets[i];
4423 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4424
4425 if (type_die)
4426 {
4427 bool try_resolving_type = false;
4428
4429 // Don't try and resolve the DIE we are looking for with the DIE itself!
4430 if (type_die != die)
4431 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004432 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004433 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004434 case DW_TAG_class_type:
4435 case DW_TAG_structure_type:
4436 try_resolving_type = true;
4437 break;
4438 default:
4439 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004440 }
4441 }
4442
4443 if (try_resolving_type)
4444 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004445 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004446 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004447
4448 if (try_resolving_type)
4449 {
4450 Type *resolved_type = ResolveType (type_cu, type_die, false);
4451 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4452 {
4453 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4454 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004455 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton901c5ca2011-12-03 04:40:03 +00004456 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4457 MakeUserID(type_die->GetOffset()),
4458 MakeUserID(type_cu->GetOffset()));
4459
Greg Claytonc7f03b62012-01-12 04:33:28 +00004460 if (die)
4461 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004462 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004463 break;
4464 }
4465 }
4466 }
4467 }
4468 else
4469 {
4470 if (m_using_apple_tables)
4471 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004472 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4473 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004474 }
4475 }
4476
4477 }
4478 }
4479 return type_sp;
4480}
4481
Greg Claytona8022fa2012-04-24 21:22:41 +00004482
Greg Clayton80c26302012-02-05 06:12:47 +00004483//----------------------------------------------------------------------
4484// This function helps to ensure that the declaration contexts match for
4485// two different DIEs. Often times debug information will refer to a
4486// forward declaration of a type (the equivalent of "struct my_struct;".
4487// There will often be a declaration of that type elsewhere that has the
4488// full definition. When we go looking for the full type "my_struct", we
4489// will find one or more matches in the accelerator tables and we will
4490// then need to make sure the type was in the same declaration context
4491// as the original DIE. This function can efficiently compare two DIEs
4492// and will return true when the declaration context matches, and false
4493// when they don't.
4494//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004495bool
4496SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4497 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4498{
Greg Claytona8022fa2012-04-24 21:22:41 +00004499 if (die1 == die2)
4500 return true;
4501
4502#if defined (LLDB_CONFIGURATION_DEBUG)
4503 // You can't and shouldn't call this function with a compile unit from
4504 // two different SymbolFileDWARF instances.
4505 assert (DebugInfo()->ContainsCompileUnit (cu1));
4506 assert (DebugInfo()->ContainsCompileUnit (cu2));
4507#endif
4508
Greg Clayton890ff562012-02-02 05:48:16 +00004509 DWARFDIECollection decl_ctx_1;
4510 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004511 //The declaration DIE stack is a stack of the declaration context
4512 // DIEs all the way back to the compile unit. If a type "T" is
4513 // declared inside a class "B", and class "B" is declared inside
4514 // a class "A" and class "A" is in a namespace "lldb", and the
4515 // namespace is in a compile unit, there will be a stack of DIEs:
4516 //
4517 // [0] DW_TAG_class_type for "B"
4518 // [1] DW_TAG_class_type for "A"
4519 // [2] DW_TAG_namespace for "lldb"
4520 // [3] DW_TAG_compile_unit for the source file.
4521 //
4522 // We grab both contexts and make sure that everything matches
4523 // all the way back to the compiler unit.
4524
4525 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004526 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004527 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004528 // Make sure the context arrays have the same size, otherwise
4529 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004530 const size_t count1 = decl_ctx_1.Size();
4531 const size_t count2 = decl_ctx_2.Size();
4532 if (count1 != count2)
4533 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004534
4535 // Make sure the DW_TAG values match all the way back up the the
4536 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004537 const DWARFDebugInfoEntry *decl_ctx_die1;
4538 const DWARFDebugInfoEntry *decl_ctx_die2;
4539 size_t i;
4540 for (i=0; i<count1; i++)
4541 {
4542 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4543 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4544 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4545 return false;
4546 }
Greg Clayton890ff562012-02-02 05:48:16 +00004547#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004548
4549 // Make sure the top item in the decl context die array is always
4550 // DW_TAG_compile_unit. If it isn't then something went wrong in
4551 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004552 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004553
Greg Clayton890ff562012-02-02 05:48:16 +00004554#endif
4555 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004556 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004557 for (i=0; i<count1 - 1; i++)
4558 {
4559 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4560 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4561 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004562 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004563 // If the string was from a DW_FORM_strp, then the pointer will often
4564 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004565 if (name1 == name2)
4566 continue;
4567
4568 // Name pointers are not equal, so only compare the strings
4569 // if both are not NULL.
4570 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004571 {
Greg Clayton5569e642012-02-06 01:44:54 +00004572 // If the strings don't compare, we are done...
4573 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004574 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004575 }
4576 else
4577 {
4578 // One name was NULL while the other wasn't
4579 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004580 }
4581 }
Greg Clayton80c26302012-02-05 06:12:47 +00004582 // We made it through all of the checks and the declaration contexts
4583 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004584 return true;
4585}
Greg Clayton220a0072011-12-09 08:48:30 +00004586
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004587// This function can be used when a DIE is found that is a forward declaration
4588// DIE and we want to try and find a type that has the complete definition.
Greg Claytona8022fa2012-04-24 21:22:41 +00004589// "cu" and "die" must be from this SymbolFileDWARF
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004590TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004591SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
Greg Clayton7f995132011-10-04 22:41:51 +00004592 const DWARFDebugInfoEntry *die,
4593 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004594{
4595 TypeSP type_sp;
4596
Greg Claytona8022fa2012-04-24 21:22:41 +00004597#if defined (LLDB_CONFIGURATION_DEBUG)
4598 // You can't and shouldn't call this function with a compile unit from
4599 // another SymbolFileDWARF instance.
4600 assert (DebugInfo()->ContainsCompileUnit (cu));
4601#endif
4602
Greg Clayton1a65ae12011-01-25 23:55:37 +00004603 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004604 return type_sp;
4605
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004606 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4607 if (log)
4608 {
4609 std::string qualified_name;
4610 die->GetQualifiedName(this, cu, qualified_name);
4611 GetObjectFile()->GetModule()->LogMessage (log.get(),
4612 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x (%s), name='%s')",
4613 die->GetOffset(),
4614 qualified_name.c_str(),
4615 type_name.GetCString());
4616 }
4617
Greg Clayton7f995132011-10-04 22:41:51 +00004618 DIEArray die_offsets;
4619
Greg Clayton97fbc342011-10-20 22:30:33 +00004620 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004621 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004622 if (m_apple_types_ap.get())
4623 {
Greg Claytond1767f02011-12-08 02:13:16 +00004624 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4625 {
Greg Claytonae920b62012-01-06 00:17:16 +00004626 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004627 }
4628 else
4629 {
4630 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4631 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004632 }
Greg Clayton7f995132011-10-04 22:41:51 +00004633 }
4634 else
4635 {
4636 if (!m_indexed)
4637 Index ();
4638
4639 m_type_index.Find (type_name, die_offsets);
4640 }
Greg Clayton7f995132011-10-04 22:41:51 +00004641
4642 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004643
Greg Clayton934cb052011-12-03 00:27:05 +00004644 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004645
4646 DWARFCompileUnit* type_cu = NULL;
4647 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004648 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004649 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004650 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004651 for (size_t i=0; i<num_matches; ++i)
4652 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004653 const dw_offset_t die_offset = die_offsets[i];
4654 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004655
Greg Clayton95d87902011-11-11 03:16:25 +00004656 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004657 {
Greg Clayton934cb052011-12-03 00:27:05 +00004658 bool try_resolving_type = false;
4659
4660 // Don't try and resolve the DIE we are looking for with the DIE itself!
4661 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004662 {
Greg Clayton934cb052011-12-03 00:27:05 +00004663 const dw_tag_t type_die_tag = type_die->Tag();
4664 // Make sure the tags match
4665 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004666 {
Greg Clayton934cb052011-12-03 00:27:05 +00004667 // The tags match, lets try resolving this type
4668 try_resolving_type = true;
4669 }
4670 else
4671 {
4672 // The tags don't match, but we need to watch our for a
4673 // forward declaration for a struct and ("struct foo")
4674 // ends up being a class ("class foo { ... };") or
4675 // vice versa.
4676 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004677 {
Greg Clayton934cb052011-12-03 00:27:05 +00004678 case DW_TAG_class_type:
4679 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4680 try_resolving_type = (die_tag == DW_TAG_structure_type);
4681 break;
4682 case DW_TAG_structure_type:
4683 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4684 try_resolving_type = (die_tag == DW_TAG_class_type);
4685 break;
4686 default:
4687 // Tags don't match, don't event try to resolve
4688 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004689 break;
4690 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004691 }
4692 }
Greg Clayton934cb052011-12-03 00:27:05 +00004693
4694 if (try_resolving_type)
4695 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004696 if (log)
4697 {
4698 std::string qualified_name;
4699 type_die->GetQualifiedName(this, cu, qualified_name);
4700 GetObjectFile()->GetModule()->LogMessage (log.get(),
4701 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') trying die=0x%8.8x (%s)",
4702 die->GetOffset(),
4703 type_name.GetCString(),
4704 type_die->GetOffset(),
4705 qualified_name.c_str());
4706 }
4707
Greg Clayton890ff562012-02-02 05:48:16 +00004708 // Make sure the decl contexts match all the way up
4709 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004710 {
Greg Clayton890ff562012-02-02 05:48:16 +00004711 Type *resolved_type = ResolveType (type_cu, type_die, false);
4712 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4713 {
4714 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4715 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004716 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton890ff562012-02-02 05:48:16 +00004717 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4718 MakeUserID(type_die->GetOffset()),
4719 MakeUserID(type_cu->GetOffset()));
4720
4721 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004722 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004723 break;
4724 }
Greg Clayton934cb052011-12-03 00:27:05 +00004725 }
4726 }
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004727 else
4728 {
4729 if (log)
4730 {
4731 std::string qualified_name;
4732 type_die->GetQualifiedName(this, cu, qualified_name);
4733 GetObjectFile()->GetModule()->LogMessage (log.get(),
4734 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') ignoring die=0x%8.8x (%s)",
4735 die->GetOffset(),
4736 type_name.GetCString(),
4737 type_die->GetOffset(),
4738 qualified_name.c_str());
4739 }
4740 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004741 }
Greg Clayton95d87902011-11-11 03:16:25 +00004742 else
4743 {
4744 if (m_using_apple_tables)
4745 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004746 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4747 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004748 }
4749 }
4750
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004751 }
4752 }
4753 return type_sp;
4754}
4755
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004756TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004757SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
4758{
4759 TypeSP type_sp;
4760
4761 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
4762 if (dwarf_decl_ctx_count > 0)
4763 {
4764 const ConstString type_name(dwarf_decl_ctx[0].name);
4765 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
4766
4767 if (type_name)
4768 {
4769 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4770 if (log)
4771 {
4772 GetObjectFile()->GetModule()->LogMessage (log.get(),
4773 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
4774 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4775 dwarf_decl_ctx.GetQualifiedName());
4776 }
4777
4778 DIEArray die_offsets;
4779
4780 if (m_using_apple_tables)
4781 {
4782 if (m_apple_types_ap.get())
4783 {
4784 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4785 {
4786 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
4787 }
4788 else
4789 {
4790 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4791 }
4792 }
4793 }
4794 else
4795 {
4796 if (!m_indexed)
4797 Index ();
4798
4799 m_type_index.Find (type_name, die_offsets);
4800 }
4801
4802 const size_t num_matches = die_offsets.size();
4803
4804
4805 DWARFCompileUnit* type_cu = NULL;
4806 const DWARFDebugInfoEntry* type_die = NULL;
4807 if (num_matches)
4808 {
4809 DWARFDebugInfo* debug_info = DebugInfo();
4810 for (size_t i=0; i<num_matches; ++i)
4811 {
4812 const dw_offset_t die_offset = die_offsets[i];
4813 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4814
4815 if (type_die)
4816 {
4817 bool try_resolving_type = false;
4818
4819 // Don't try and resolve the DIE we are looking for with the DIE itself!
4820 const dw_tag_t type_tag = type_die->Tag();
4821 // Make sure the tags match
4822 if (type_tag == tag)
4823 {
4824 // The tags match, lets try resolving this type
4825 try_resolving_type = true;
4826 }
4827 else
4828 {
4829 // The tags don't match, but we need to watch our for a
4830 // forward declaration for a struct and ("struct foo")
4831 // ends up being a class ("class foo { ... };") or
4832 // vice versa.
4833 switch (type_tag)
4834 {
4835 case DW_TAG_class_type:
4836 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4837 try_resolving_type = (tag == DW_TAG_structure_type);
4838 break;
4839 case DW_TAG_structure_type:
4840 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4841 try_resolving_type = (tag == DW_TAG_class_type);
4842 break;
4843 default:
4844 // Tags don't match, don't event try to resolve
4845 // using this type whose name matches....
4846 break;
4847 }
4848 }
4849
4850 if (try_resolving_type)
4851 {
4852 DWARFDeclContext type_dwarf_decl_ctx;
4853 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
4854
4855 if (log)
4856 {
4857 GetObjectFile()->GetModule()->LogMessage (log.get(),
4858 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
4859 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4860 dwarf_decl_ctx.GetQualifiedName(),
4861 type_die->GetOffset(),
4862 type_dwarf_decl_ctx.GetQualifiedName());
4863 }
4864
4865 // Make sure the decl contexts match all the way up
4866 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
4867 {
4868 Type *resolved_type = ResolveType (type_cu, type_die, false);
4869 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4870 {
4871 type_sp = resolved_type->shared_from_this();
4872 break;
4873 }
4874 }
4875 }
4876 else
4877 {
4878 if (log)
4879 {
4880 std::string qualified_name;
4881 type_die->GetQualifiedName(this, type_cu, qualified_name);
4882 GetObjectFile()->GetModule()->LogMessage (log.get(),
4883 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
4884 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4885 dwarf_decl_ctx.GetQualifiedName(),
4886 type_die->GetOffset(),
4887 qualified_name.c_str());
4888 }
4889 }
4890 }
4891 else
4892 {
4893 if (m_using_apple_tables)
4894 {
4895 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4896 die_offset, type_name.GetCString());
4897 }
4898 }
4899
4900 }
4901 }
4902 }
4903 }
4904 return type_sp;
4905}
4906
Greg Clayton4116e932012-05-15 02:33:01 +00004907bool
4908SymbolFileDWARF::CopyUniqueClassMethodTypes (Type *class_type,
4909 DWARFCompileUnit* src_cu,
4910 const DWARFDebugInfoEntry *src_class_die,
4911 DWARFCompileUnit* dst_cu,
4912 const DWARFDebugInfoEntry *dst_class_die)
4913{
4914 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
4915 return false;
4916 if (src_class_die->Tag() != dst_class_die->Tag())
4917 return false;
4918
4919 // We need to complete the class type so we can get all of the method types
4920 // parsed so we can then unique those types to their equivalent counterparts
4921 // in "dst_cu" and "dst_class_die"
4922 class_type->GetClangFullType();
4923
4924 const DWARFDebugInfoEntry *src_die;
4925 const DWARFDebugInfoEntry *dst_die;
4926 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
4927 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
4928 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
4929 {
4930 if (src_die->Tag() == DW_TAG_subprogram)
4931 {
4932 const char *src_name = src_die->GetMangledName (this, src_cu);
4933 if (src_name)
4934 src_name_to_die.Append(ConstString(src_name).GetCString(), src_die);
4935 }
4936 }
4937 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
4938 {
4939 if (dst_die->Tag() == DW_TAG_subprogram)
4940 {
4941 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
4942 if (dst_name)
4943 dst_name_to_die.Append(ConstString(dst_name).GetCString(), dst_die);
4944 }
4945 }
4946 const uint32_t src_size = src_name_to_die.GetSize ();
4947 const uint32_t dst_size = dst_name_to_die.GetSize ();
4948 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
4949
4950 if (src_size && dst_size)
4951 {
Sean Callanan210b8152012-07-07 00:29:33 +00004952 if (src_size != dst_size)
4953 {
4954 if (log)
4955 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)",
4956 src_class_die->GetOffset(),
4957 dst_class_die->GetOffset(),
4958 src_size,
4959 dst_size);
4960
4961 return false;
4962 }
4963
Greg Clayton4116e932012-05-15 02:33:01 +00004964 uint32_t idx;
4965 for (idx = 0; idx < src_size; ++idx)
4966 {
4967 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
4968 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
4969
4970 if (src_die->Tag() != dst_die->Tag())
4971 {
4972 if (log)
4973 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
4974 src_class_die->GetOffset(),
4975 dst_class_die->GetOffset(),
4976 src_die->GetOffset(),
4977 DW_TAG_value_to_name(src_die->Tag()),
4978 dst_die->GetOffset(),
4979 DW_TAG_value_to_name(src_die->Tag()));
4980 return false;
4981 }
4982
4983 const char *src_name = src_die->GetMangledName (this, src_cu);
4984 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
4985
4986 // Make sure the names match
4987 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
4988 continue;
4989
4990 if (log)
4991 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
4992 src_class_die->GetOffset(),
4993 dst_class_die->GetOffset(),
4994 src_die->GetOffset(),
4995 src_name,
4996 dst_die->GetOffset(),
4997 dst_name);
4998
4999 return false;
5000 }
5001
5002 for (idx = 0; idx < src_size; ++idx)
5003 {
5004 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5005 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
5006
5007 clang::DeclContext *src_decl_ctx = m_die_to_decl_ctx[src_die];
5008 if (src_decl_ctx)
5009 {
5010 if (log)
5011 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x\n", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
5012 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5013 }
5014 else
5015 {
5016 if (log)
5017 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found\n", src_die->GetOffset(), dst_die->GetOffset());
5018 }
5019
5020 Type *src_child_type = m_die_to_type[src_die];
5021 if (src_child_type)
5022 {
5023 if (log)
5024 log->Printf ("uniquing type %p (uid=0x%llx) from 0x%8.8x for 0x%8.8x\n", src_child_type, src_child_type->GetID(), src_die->GetOffset(), dst_die->GetOffset());
5025 m_die_to_type[dst_die] = src_child_type;
5026 }
5027 else
5028 {
5029 if (log)
5030 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found\n", src_die->GetOffset(), dst_die->GetOffset());
5031 }
5032 }
5033 return true;
5034 }
5035 else
5036 {
5037 if (log)
5038 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x has %u methods and 0x%8.8x has %u",
5039 src_class_die->GetOffset(),
5040 dst_class_die->GetOffset(),
5041 src_die->GetOffset(),
5042 src_size,
5043 dst_die->GetOffset(),
5044 dst_size);
5045 }
5046 return false;
5047}
Greg Claytona8022fa2012-04-24 21:22:41 +00005048
5049TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00005050SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005051{
5052 TypeSP type_sp;
5053
Greg Clayton1be10fc2010-09-29 01:12:09 +00005054 if (type_is_new_ptr)
5055 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00005056
5057#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5058 static DIEStack g_die_stack;
5059 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
5060#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00005061
Sean Callananc7fbf732010-08-06 00:32:49 +00005062 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005063 if (die != NULL)
5064 {
Greg Clayton21f2a492011-10-06 00:09:08 +00005065 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005066 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00005067 {
5068 const DWARFDebugInfoEntry *context_die;
5069 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
5070
Greg Clayton1fba87112012-02-09 20:03:49 +00005071 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 +00005072 die->GetOffset(),
5073 context,
5074 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00005075 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00005076 die->GetName(this, dwarf_cu));
5077
5078#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5079 scoped_die_logger.Push (dwarf_cu, die);
5080 g_die_stack.LogDIEs(log.get(), this);
5081#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00005082 }
Greg Clayton3bffb082011-12-10 02:15:28 +00005083//
5084// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
5085// if (log && dwarf_cu)
5086// {
5087// StreamString s;
5088// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005089// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00005090//
5091// }
Jim Ingham16746d12011-08-25 23:21:43 +00005092
Greg Clayton594e5ed2010-09-27 21:07:38 +00005093 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005094 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00005095 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005096 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005097 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005098 if (type_is_new_ptr)
5099 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005100
Greg Clayton594e5ed2010-09-27 21:07:38 +00005101 const dw_tag_t tag = die->Tag();
5102
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005103 bool is_forward_declaration = false;
5104 DWARFDebugInfoEntry::Attributes attributes;
5105 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00005106 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00005107 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
5108 size_t byte_size = 0;
5109 Declaration decl;
5110
Greg Clayton4957bf62010-09-30 21:49:03 +00005111 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005112 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005113
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005114 dw_attr_t attr;
5115
5116 switch (tag)
5117 {
5118 case DW_TAG_base_type:
5119 case DW_TAG_pointer_type:
5120 case DW_TAG_reference_type:
Sean Callanance8af862012-05-21 23:31:51 +00005121 case DW_TAG_rvalue_reference_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005122 case DW_TAG_typedef:
5123 case DW_TAG_const_type:
5124 case DW_TAG_restrict_type:
5125 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00005126 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005127 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005128 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005129 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005130
Greg Claytond88d7592010-09-15 08:33:30 +00005131 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005132 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005133 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
5134
5135 if (num_attributes > 0)
5136 {
5137 uint32_t i;
5138 for (i=0; i<num_attributes; ++i)
5139 {
5140 attr = attributes.AttributeAtIndex(i);
5141 DWARFFormValue form_value;
5142 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5143 {
5144 switch (attr)
5145 {
5146 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5147 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5148 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5149 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00005150
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005151 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00005152 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
5153 // include the "&"...
5154 if (tag == DW_TAG_reference_type)
5155 {
5156 if (strchr (type_name_cstr, '&') == NULL)
5157 type_name_cstr = NULL;
5158 }
5159 if (type_name_cstr)
5160 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005161 break;
Greg Clayton23f59502012-07-17 03:23:13 +00005162 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005163 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
5164 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
5165 default:
5166 case DW_AT_sibling:
5167 break;
5168 }
5169 }
5170 }
5171 }
5172
Greg Clayton81c22f62011-10-19 18:09:39 +00005173 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 +00005174
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005175 switch (tag)
5176 {
5177 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00005178 break;
5179
Greg Claytond4436412011-10-28 21:00:00 +00005180 case DW_TAG_unspecified_type:
5181 if (strcmp(type_name_cstr, "nullptr_t") == 0)
5182 {
5183 resolve_state = Type::eResolveStateFull;
5184 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
5185 break;
5186 }
5187 // Fall through to base type below in case we can handle the type there...
5188
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005189 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00005190 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005191 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
5192 encoding,
5193 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005194 break;
5195
Sean Callanance8af862012-05-21 23:31:51 +00005196 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
5197 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
5198 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
5199 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
5200 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
5201 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
5202 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005203 }
5204
Greg Claytonc7f03b62012-01-12 04:33:28 +00005205 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005206 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005207 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
5208 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005209 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005210 static ConstString g_objc_type_name_id("id");
5211 static ConstString g_objc_type_name_Class("Class");
5212 static ConstString g_objc_type_name_selector("SEL");
5213
5214 if (type_name_const_str == g_objc_type_name_id)
5215 {
5216 if (log)
5217 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
5218 die->GetOffset(),
5219 DW_TAG_value_to_name(die->Tag()),
5220 die->GetName(this, dwarf_cu));
5221 clang_type = ast.GetBuiltInType_objc_id();
5222 encoding_data_type = Type::eEncodingIsUID;
5223 encoding_uid = LLDB_INVALID_UID;
5224 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00005225
Greg Claytonc7f03b62012-01-12 04:33:28 +00005226 }
5227 else if (type_name_const_str == g_objc_type_name_Class)
5228 {
5229 if (log)
5230 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
5231 die->GetOffset(),
5232 DW_TAG_value_to_name(die->Tag()),
5233 die->GetName(this, dwarf_cu));
5234 clang_type = ast.GetBuiltInType_objc_Class();
5235 encoding_data_type = Type::eEncodingIsUID;
5236 encoding_uid = LLDB_INVALID_UID;
5237 resolve_state = Type::eResolveStateFull;
5238 }
5239 else if (type_name_const_str == g_objc_type_name_selector)
5240 {
5241 if (log)
5242 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
5243 die->GetOffset(),
5244 DW_TAG_value_to_name(die->Tag()),
5245 die->GetName(this, dwarf_cu));
5246 clang_type = ast.GetBuiltInType_objc_selector();
5247 encoding_data_type = Type::eEncodingIsUID;
5248 encoding_uid = LLDB_INVALID_UID;
5249 resolve_state = Type::eResolveStateFull;
5250 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005251 }
5252 }
5253
Greg Clayton81c22f62011-10-19 18:09:39 +00005254 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005255 this,
5256 type_name_const_str,
5257 byte_size,
5258 NULL,
5259 encoding_uid,
5260 encoding_data_type,
5261 &decl,
5262 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005263 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005264
Greg Clayton594e5ed2010-09-27 21:07:38 +00005265 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005266
5267// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
5268// if (encoding_type != NULL)
5269// {
5270// if (encoding_type != DIE_IS_BEING_PARSED)
5271// type_sp->SetEncodingType(encoding_type);
5272// else
5273// m_indirect_fixups.push_back(type_sp.get());
5274// }
5275 }
5276 break;
5277
5278 case DW_TAG_structure_type:
5279 case DW_TAG_union_type:
5280 case DW_TAG_class_type:
5281 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005282 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005283 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Greg Clayton23f59502012-07-17 03:23:13 +00005284 bool byte_size_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005285
Greg Clayton9e409562010-07-28 02:04:09 +00005286 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00005287 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005288 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00005289 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005290 if (num_attributes > 0)
5291 {
5292 uint32_t i;
5293 for (i=0; i<num_attributes; ++i)
5294 {
5295 attr = attributes.AttributeAtIndex(i);
5296 DWARFFormValue form_value;
5297 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5298 {
5299 switch (attr)
5300 {
Greg Clayton9e409562010-07-28 02:04:09 +00005301 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00005302 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
5303 {
5304 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
5305 // point to the compile unit file, so we clear this invalid value
5306 // so that we can still unique types efficiently.
5307 decl.SetFile(FileSpec ("<invalid>", false));
5308 }
5309 else
5310 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00005311 break;
5312
5313 case DW_AT_decl_line:
5314 decl.SetLine(form_value.Unsigned());
5315 break;
5316
5317 case DW_AT_decl_column:
5318 decl.SetColumn(form_value.Unsigned());
5319 break;
5320
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005321 case DW_AT_name:
5322 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005323 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005324 break;
Greg Clayton9e409562010-07-28 02:04:09 +00005325
5326 case DW_AT_byte_size:
5327 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00005328 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00005329 break;
5330
5331 case DW_AT_accessibility:
5332 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
5333 break;
5334
5335 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00005336 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00005337 break;
5338
5339 case DW_AT_APPLE_runtime_class:
5340 class_language = (LanguageType)form_value.Signed();
5341 break;
5342
Greg Clayton18774842011-11-29 23:40:34 +00005343 case DW_AT_APPLE_objc_complete_type:
5344 is_complete_objc_class = form_value.Signed();
5345 break;
5346
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005347 case DW_AT_allocated:
5348 case DW_AT_associated:
5349 case DW_AT_data_location:
5350 case DW_AT_description:
5351 case DW_AT_start_scope:
5352 case DW_AT_visibility:
5353 default:
5354 case DW_AT_sibling:
5355 break;
5356 }
5357 }
5358 }
5359 }
5360
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005361 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00005362
Greg Clayton123edca2012-03-02 00:07:15 +00005363 // Only try and unique the type if it has a name.
5364 if (type_name_const_str &&
5365 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00005366 this,
5367 dwarf_cu,
5368 die,
5369 decl,
5370 byte_size_valid ? byte_size : -1,
5371 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005372 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005373 // We have already parsed this type or from another
5374 // compile unit. GCC loves to use the "one definition
5375 // rule" which can result in multiple definitions
5376 // of the same class over and over in each compile
5377 // unit.
5378 type_sp = unique_ast_entry.m_type_sp;
5379 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00005380 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005381 m_die_to_type[die] = type_sp.get();
5382 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005383 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005384 }
5385
Greg Clayton81c22f62011-10-19 18:09:39 +00005386 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 +00005387
5388 int tag_decl_kind = -1;
5389 AccessType default_accessibility = eAccessNone;
5390 if (tag == DW_TAG_structure_type)
5391 {
5392 tag_decl_kind = clang::TTK_Struct;
5393 default_accessibility = eAccessPublic;
5394 }
5395 else if (tag == DW_TAG_union_type)
5396 {
5397 tag_decl_kind = clang::TTK_Union;
5398 default_accessibility = eAccessPublic;
5399 }
5400 else if (tag == DW_TAG_class_type)
5401 {
5402 tag_decl_kind = clang::TTK_Class;
5403 default_accessibility = eAccessPrivate;
5404 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00005405
5406 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
5407 die->HasChildren() == false &&
5408 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
5409 {
5410 // Work around an issue with clang at the moment where
5411 // forward declarations for objective C classes are emitted
5412 // as:
5413 // DW_TAG_structure_type [2]
5414 // DW_AT_name( "ForwardObjcClass" )
5415 // DW_AT_byte_size( 0x00 )
5416 // DW_AT_decl_file( "..." )
5417 // DW_AT_decl_line( 1 )
5418 //
5419 // Note that there is no DW_AT_declaration and there are
5420 // no children, and the byte size is zero.
5421 is_forward_declaration = true;
5422 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005423
Sean Callanan7457f8d2012-04-25 01:03:57 +00005424 if (class_language == eLanguageTypeObjC ||
5425 class_language == eLanguageTypeObjC_plus_plus)
Greg Clayton18774842011-11-29 23:40:34 +00005426 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005427 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00005428 {
5429 // We have a valid eSymbolTypeObjCClass class symbol whose
5430 // name matches the current objective C class that we
5431 // are trying to find and this DIE isn't the complete
5432 // definition (we checked is_complete_objc_class above and
5433 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00005434 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005435
Greg Clayton1f746072012-08-29 21:13:06 +00005436 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton901c5ca2011-12-03 04:40:03 +00005437 {
5438 // We weren't able to find a full declaration in
5439 // this DWARF, see if we have a declaration anywhere
5440 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00005441 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005442 }
5443
5444 if (type_sp)
5445 {
5446 if (log)
5447 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005448 GetObjectFile()->GetModule()->LogMessage (log.get(),
5449 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
5450 this,
5451 die->GetOffset(),
5452 DW_TAG_value_to_name(tag),
5453 type_name_cstr,
5454 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00005455 }
5456
5457 // We found a real definition for this type elsewhere
5458 // so lets use it and cache the fact that we found
5459 // a complete type for this die
5460 m_die_to_type[die] = type_sp.get();
5461 return type_sp;
5462 }
5463 }
5464 }
5465
5466
5467 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005468 {
5469 // We have a forward declaration to a type and we need
5470 // to try and find a full declaration. We look in the
5471 // current type index just in case we have a forward
5472 // declaration followed by an actual declarations in the
5473 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00005474 if (log)
5475 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005476 GetObjectFile()->GetModule()->LogMessage (log.get(),
5477 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
5478 this,
5479 die->GetOffset(),
5480 DW_TAG_value_to_name(tag),
5481 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00005482 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005483
Greg Claytona8022fa2012-04-24 21:22:41 +00005484 DWARFDeclContext die_decl_ctx;
5485 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx);
5486
5487 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
5488 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005489
Greg Clayton1f746072012-08-29 21:13:06 +00005490 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton4272cc72011-02-02 02:24:04 +00005491 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005492 // We weren't able to find a full declaration in
5493 // this DWARF, see if we have a declaration anywhere
5494 // else...
Greg Claytona8022fa2012-04-24 21:22:41 +00005495 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton4272cc72011-02-02 02:24:04 +00005496 }
5497
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005498 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00005499 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00005500 if (log)
5501 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005502 GetObjectFile()->GetModule()->LogMessage (log.get(),
5503 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
5504 this,
5505 die->GetOffset(),
5506 DW_TAG_value_to_name(tag),
5507 type_name_cstr,
5508 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00005509 }
5510
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005511 // We found a real definition for this type elsewhere
5512 // so lets use it and cache the fact that we found
5513 // a complete type for this die
5514 m_die_to_type[die] = type_sp.get();
5515 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005516 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005517 }
5518 assert (tag_decl_kind != -1);
5519 bool clang_type_was_created = false;
5520 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5521 if (clang_type == NULL)
5522 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00005523 const DWARFDebugInfoEntry *decl_ctx_die;
5524
5525 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00005526 if (accessibility == eAccessNone && decl_ctx)
5527 {
5528 // Check the decl context that contains this class/struct/union.
5529 // If it is a class we must give it an accessability.
5530 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
5531 if (DeclKindIsCXXClass (containing_decl_kind))
5532 accessibility = default_accessibility;
5533 }
5534
Greg Claytonf0705c82011-10-22 03:33:13 +00005535 if (type_name_cstr && strchr (type_name_cstr, '<'))
5536 {
5537 ClangASTContext::TemplateParameterInfos template_param_infos;
5538 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
5539 {
5540 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00005541 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00005542 type_name_cstr,
5543 tag_decl_kind,
5544 template_param_infos);
5545
5546 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
5547 class_template_decl,
5548 tag_decl_kind,
5549 template_param_infos);
5550 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
5551 clang_type_was_created = true;
Sean Callanan60217122012-04-13 00:10:03 +00005552
5553 GetClangASTContext().SetMetadata((uintptr_t)class_template_decl, MakeUserID(die->GetOffset()));
5554 GetClangASTContext().SetMetadata((uintptr_t)class_specialization_decl, MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005555 }
5556 }
5557
5558 if (!clang_type_was_created)
5559 {
5560 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00005561 clang_type = ast.CreateRecordType (decl_ctx,
5562 accessibility,
5563 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00005564 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00005565 class_language,
Sean Callananad880762012-04-18 01:06:17 +00005566 MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005567 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005568 }
5569
5570 // Store a forward declaration to this class type in case any
5571 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00005572 // types for function prototypes.
5573 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00005574 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005575 this,
5576 type_name_const_str,
5577 byte_size,
5578 NULL,
5579 LLDB_INVALID_UID,
5580 Type::eEncodingIsUID,
5581 &decl,
5582 clang_type,
5583 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00005584
5585 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005586
5587
5588 // Add our type to the unique type map so we don't
5589 // end up creating many copies of the same type over
5590 // and over in the ASTContext for our module
5591 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00005592 unique_ast_entry.m_symfile = this;
5593 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005594 unique_ast_entry.m_die = die;
5595 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00005596 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00005597 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
5598 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005599
Sean Callanan12014a02011-12-08 23:45:45 +00005600 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00005601 {
5602 // Always start the definition for a class type so that
5603 // if the class has child classes or types that require
5604 // the class to be created for use as their decl contexts
5605 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00005606 if (die->HasChildren() == false)
5607 {
5608 // No children for this struct/union/class, lets finish it
5609 ast.StartTagDeclarationDefinition (clang_type);
5610 ast.CompleteTagDeclarationDefinition (clang_type);
5611 }
5612 else if (clang_type_was_created)
5613 {
Greg Clayton219cf312012-03-30 00:51:13 +00005614 // Start the definition if the class is not objective C since
5615 // the underlying decls respond to isCompleteDefinition(). Objective
5616 // C decls dont' respond to isCompleteDefinition() so we can't
5617 // start the declaration definition right away. For C++ classs/union/structs
5618 // we want to start the definition in case the class is needed as the
5619 // declaration context for a contained class or type without the need
5620 // to complete that type..
5621
Sean Callanan7457f8d2012-04-25 01:03:57 +00005622 if (class_language != eLanguageTypeObjC &&
5623 class_language != eLanguageTypeObjC_plus_plus)
Greg Clayton219cf312012-03-30 00:51:13 +00005624 ast.StartTagDeclarationDefinition (clang_type);
5625
Sean Callanan12014a02011-12-08 23:45:45 +00005626 // Leave this as a forward declaration until we need
5627 // to know the details of the type. lldb_private::Type
5628 // will automatically call the SymbolFile virtual function
5629 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
5630 // When the definition needs to be defined.
5631 m_forward_decl_die_to_clang_type[die] = clang_type;
5632 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
5633 ClangASTContext::SetHasExternalStorage (clang_type, true);
5634 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005635 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005636
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005637 }
5638 break;
5639
5640 case DW_TAG_enumeration_type:
5641 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005642 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005643 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005644
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005645 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005646
Greg Claytond88d7592010-09-15 08:33:30 +00005647 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005648 if (num_attributes > 0)
5649 {
5650 uint32_t i;
5651
5652 for (i=0; i<num_attributes; ++i)
5653 {
5654 attr = attributes.AttributeAtIndex(i);
5655 DWARFFormValue form_value;
5656 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5657 {
5658 switch (attr)
5659 {
Greg Clayton7a345282010-11-09 23:46:37 +00005660 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5661 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5662 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005663 case DW_AT_name:
5664 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005665 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005666 break;
Greg Clayton7a345282010-11-09 23:46:37 +00005667 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00005668 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
5669 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5670 case DW_AT_declaration: break; //is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005671 case DW_AT_allocated:
5672 case DW_AT_associated:
5673 case DW_AT_bit_stride:
5674 case DW_AT_byte_stride:
5675 case DW_AT_data_location:
5676 case DW_AT_description:
5677 case DW_AT_start_scope:
5678 case DW_AT_visibility:
5679 case DW_AT_specification:
5680 case DW_AT_abstract_origin:
5681 case DW_AT_sibling:
5682 break;
5683 }
5684 }
5685 }
5686
Greg Clayton81c22f62011-10-19 18:09:39 +00005687 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 +00005688
Greg Clayton1be10fc2010-09-29 01:12:09 +00005689 clang_type_t enumerator_clang_type = NULL;
5690 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5691 if (clang_type == NULL)
5692 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005693 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
5694 DW_ATE_signed,
5695 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00005696 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00005697 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00005698 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005699 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00005700 }
5701 else
5702 {
5703 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
5704 assert (enumerator_clang_type != NULL);
5705 }
5706
Greg Claytona2721472011-06-25 00:44:06 +00005707 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
5708
Greg Clayton81c22f62011-10-19 18:09:39 +00005709 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005710 this,
5711 type_name_const_str,
5712 byte_size,
5713 NULL,
5714 encoding_uid,
5715 Type::eEncodingIsUID,
5716 &decl,
5717 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005718 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005719
Greg Clayton6beaaa62011-01-17 03:46:26 +00005720 ast.StartTagDeclarationDefinition (clang_type);
5721 if (die->HasChildren())
5722 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005723 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
5724 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005725 }
5726 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005727 }
5728 }
5729 break;
5730
Jim Inghamb0be4422010-08-12 01:20:14 +00005731 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005732 case DW_TAG_subprogram:
5733 case DW_TAG_subroutine_type:
5734 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005735 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005736 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005737
Greg Clayton23f59502012-07-17 03:23:13 +00005738 //const char *mangled = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005739 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00005740 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005741 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00005742 bool is_static = false;
5743 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00005744 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00005745 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00005746 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
5747 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00005748
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005749 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00005750 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005751
5752
Greg Claytond88d7592010-09-15 08:33:30 +00005753 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005754 if (num_attributes > 0)
5755 {
5756 uint32_t i;
5757 for (i=0; i<num_attributes; ++i)
5758 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005759 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005760 DWARFFormValue form_value;
5761 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5762 {
5763 switch (attr)
5764 {
5765 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5766 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5767 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5768 case DW_AT_name:
5769 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005770 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005771 break;
5772
Greg Clayton23f59502012-07-17 03:23:13 +00005773 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&get_debug_str_data()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005774 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005775 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton23f59502012-07-17 03:23:13 +00005776 case DW_AT_declaration: break; // is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00005777 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
5778 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00005779 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00005780 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5781
Greg Claytonf51de672010-10-01 02:31:07 +00005782
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005783 case DW_AT_external:
5784 if (form_value.Unsigned())
5785 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00005786 if (storage == clang::SC_None)
5787 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005788 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00005789 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005790 }
5791 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005792
Greg Clayton72da3972011-08-16 18:40:23 +00005793 case DW_AT_specification:
5794 specification_die_offset = form_value.Reference(dwarf_cu);
5795 break;
5796
5797 case DW_AT_abstract_origin:
5798 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
5799 break;
5800
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005801 case DW_AT_allocated:
5802 case DW_AT_associated:
5803 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005804 case DW_AT_calling_convention:
5805 case DW_AT_data_location:
5806 case DW_AT_elemental:
5807 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005808 case DW_AT_frame_base:
5809 case DW_AT_high_pc:
5810 case DW_AT_low_pc:
5811 case DW_AT_object_pointer:
5812 case DW_AT_prototyped:
5813 case DW_AT_pure:
5814 case DW_AT_ranges:
5815 case DW_AT_recursive:
5816 case DW_AT_return_addr:
5817 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005818 case DW_AT_start_scope:
5819 case DW_AT_static_link:
5820 case DW_AT_trampoline:
5821 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005822 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005823 case DW_AT_description:
5824 case DW_AT_sibling:
5825 break;
5826 }
5827 }
5828 }
Greg Clayton24739922010-10-13 03:15:28 +00005829 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005830
Greg Clayton81c22f62011-10-19 18:09:39 +00005831 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 +00005832
Greg Clayton24739922010-10-13 03:15:28 +00005833 clang_type_t return_clang_type = NULL;
5834 Type *func_type = NULL;
5835
5836 if (type_die_offset != DW_INVALID_OFFSET)
5837 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00005838
Greg Clayton24739922010-10-13 03:15:28 +00005839 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00005840 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00005841 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005842 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005843
Greg Claytonf51de672010-10-01 02:31:07 +00005844
Greg Clayton24739922010-10-13 03:15:28 +00005845 std::vector<clang_type_t> function_param_types;
5846 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005847
Greg Clayton24739922010-10-13 03:15:28 +00005848 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00005849
Greg Claytoncb5860a2011-10-13 23:49:28 +00005850 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
5851 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00005852 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
5853
Greg Claytonf0705c82011-10-22 03:33:13 +00005854 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00005855 // Start off static. This will be set to false in ParseChildParameters(...)
5856 // if we find a "this" paramters as the first parameter
5857 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00005858 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005859 ClangASTContext::TemplateParameterInfos template_param_infos;
5860
Greg Clayton24739922010-10-13 03:15:28 +00005861 if (die->HasChildren())
5862 {
Greg Clayton0fffff52010-09-24 05:15:53 +00005863 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005864 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00005865 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005866 dwarf_cu,
5867 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00005868 skip_artificial,
5869 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005870 type_list,
5871 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00005872 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005873 type_quals,
5874 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00005875 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005876
Greg Clayton24739922010-10-13 03:15:28 +00005877 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005878 clang_type = ast.CreateFunctionType (return_clang_type,
5879 &function_param_types[0],
5880 function_param_types.size(),
5881 is_variadic,
5882 type_quals);
5883
Greg Clayton24739922010-10-13 03:15:28 +00005884 if (type_name_cstr)
5885 {
5886 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00005887 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005888 {
Greg Clayton7c810422012-01-18 23:40:49 +00005889 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00005890 ConstString class_name_no_category;
5891 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00005892 {
Greg Claytone42ae842012-01-19 03:24:53 +00005893 // Use the class name with no category if there is one
5894 if (class_name_no_category)
5895 class_name = class_name_no_category;
5896
Greg Clayton24739922010-10-13 03:15:28 +00005897 SymbolContext empty_sc;
5898 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00005899 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00005900 {
Greg Clayton24739922010-10-13 03:15:28 +00005901 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00005902 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00005903
5904 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00005905 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005906 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
5907 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
5908 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00005909 }
Greg Clayton24739922010-10-13 03:15:28 +00005910 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005911
Greg Clayton24739922010-10-13 03:15:28 +00005912 if (class_opaque_type)
5913 {
5914 // If accessibility isn't set to anything valid, assume public for
5915 // now...
5916 if (accessibility == eAccessNone)
5917 accessibility = eAccessPublic;
5918
Sean Callanan464a5b52012-10-04 22:06:29 +00005919 clang::ObjCMethodDecl *objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
5920 type_name_cstr,
5921 clang_type,
5922 accessibility);
Greg Clayton24739922010-10-13 03:15:28 +00005923 type_handled = objc_method_decl != NULL;
Sean Callanan464a5b52012-10-04 22:06:29 +00005924 if (type_handled)
5925 {
5926 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
5927 GetClangASTContext().SetMetadata((uintptr_t)objc_method_decl, MakeUserID(die->GetOffset()));
5928 }
Greg Clayton24739922010-10-13 03:15:28 +00005929 }
5930 }
Greg Clayton5113dc82011-08-12 06:47:54 +00005931 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00005932 {
5933 // Look at the parent of this DIE and see if is is
5934 // a class or struct and see if this is actually a
5935 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00005936 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00005937 if (class_type)
5938 {
Greg Clayton4116e932012-05-15 02:33:01 +00005939 if (class_type->GetID() != MakeUserID(decl_ctx_die->GetOffset()))
5940 {
5941 // We uniqued the parent class of this function to another class
5942 // so we now need to associate all dies under "decl_ctx_die" to
5943 // DIEs in the DIE for "class_type"...
5944 DWARFCompileUnitSP class_type_cu_sp;
5945 const DWARFDebugInfoEntry *class_type_die = DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
5946 if (class_type_die)
5947 {
5948 if (CopyUniqueClassMethodTypes (class_type,
5949 class_type_cu_sp.get(),
5950 class_type_die,
5951 dwarf_cu,
5952 decl_ctx_die))
5953 {
5954 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00005955 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton4116e932012-05-15 02:33:01 +00005956 {
5957 type_sp = type_ptr->shared_from_this();
5958 break;
5959 }
5960 }
5961 }
5962 }
5963
Greg Clayton72da3972011-08-16 18:40:23 +00005964 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00005965 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005966 // We have a specification which we are going to base our function
5967 // prototype off of, so we need this type to be completed so that the
5968 // m_die_to_decl_ctx for the method in the specification has a valid
5969 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005970 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00005971 // If we have a specification, then the function type should have been
5972 // made with the specification and not with this die.
5973 DWARFCompileUnitSP spec_cu_sp;
5974 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005975 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005976 if (spec_clang_decl_ctx)
5977 {
5978 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
5979 }
5980 else
Jim Inghamc1663042011-09-29 22:12:35 +00005981 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005982 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
5983 MakeUserID(die->GetOffset()),
5984 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005985 }
Greg Clayton72da3972011-08-16 18:40:23 +00005986 type_handled = true;
5987 }
5988 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
5989 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005990 // We have a specification which we are going to base our function
5991 // prototype off of, so we need this type to be completed so that the
5992 // m_die_to_decl_ctx for the method in the abstract origin has a valid
5993 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005994 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00005995
Greg Clayton72da3972011-08-16 18:40:23 +00005996 DWARFCompileUnitSP abs_cu_sp;
5997 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005998 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005999 if (abs_clang_decl_ctx)
6000 {
6001 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
6002 }
6003 else
Jim Inghamc1663042011-09-29 22:12:35 +00006004 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006005 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
6006 MakeUserID(die->GetOffset()),
6007 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006008 }
Greg Clayton72da3972011-08-16 18:40:23 +00006009 type_handled = true;
6010 }
6011 else
6012 {
6013 clang_type_t class_opaque_type = class_type->GetClangForwardType();
6014 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00006015 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006016 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00006017 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006018 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
6019 // in the DWARF for C++ methods... Default to public for now...
6020 if (accessibility == eAccessNone)
6021 accessibility = eAccessPublic;
6022
6023 if (!is_static && !die->HasChildren())
6024 {
6025 // We have a C++ member function with no children (this pointer!)
6026 // and clang will get mad if we try and make a function that isn't
6027 // well formed in the DWARF, so we will just skip it...
6028 type_handled = true;
6029 }
6030 else
6031 {
6032 clang::CXXMethodDecl *cxx_method_decl;
6033 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00006034 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 +00006035 type_name_cstr,
6036 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00006037 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00006038 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
6039 m_obj_file->GetFileSpec().GetFilename().GetCString());
6040
Sean Callananc1b732d2011-11-01 18:07:13 +00006041 const bool is_attr_used = false;
6042
Greg Clayton20568dd2011-10-13 23:13:20 +00006043 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
6044 type_name_cstr,
6045 clang_type,
6046 accessibility,
6047 is_virtual,
6048 is_static,
6049 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00006050 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00006051 is_attr_used,
6052 is_artificial);
Greg Clayton20568dd2011-10-13 23:13:20 +00006053 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
6054
Greg Claytonf49e65a2011-11-10 18:31:53 +00006055 Host::SetCrashDescription (NULL);
6056
Greg Clayton20568dd2011-10-13 23:13:20 +00006057 type_handled = cxx_method_decl != NULL;
Sean Callanan60217122012-04-13 00:10:03 +00006058
6059 GetClangASTContext().SetMetadata((uintptr_t)cxx_method_decl, MakeUserID(die->GetOffset()));
Greg Clayton20568dd2011-10-13 23:13:20 +00006060 }
Greg Clayton72da3972011-08-16 18:40:23 +00006061 }
6062 else
6063 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006064 // We were asked to parse the type for a method in a class, yet the
6065 // class hasn't been asked to complete itself through the
6066 // clang::ExternalASTSource protocol, so we need to just have the
6067 // class complete itself and do things the right way, then our
6068 // DIE should then have an entry in the m_die_to_type map. First
6069 // we need to modify the m_die_to_type so it doesn't think we are
6070 // trying to parse this DIE anymore...
6071 m_die_to_type[die] = NULL;
6072
6073 // Now we get the full type to force our class type to complete itself
6074 // using the clang::ExternalASTSource protocol which will parse all
6075 // base classes and all methods (including the method for this DIE).
6076 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006077
Greg Clayton20568dd2011-10-13 23:13:20 +00006078 // The type for this DIE should have been filled in the function call above
6079 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00006080 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton20568dd2011-10-13 23:13:20 +00006081 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006082 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00006083 break;
6084 }
Sean Callanan02eee4d2012-04-12 23:10:00 +00006085
6086 // FIXME This is fixing some even uglier behavior but we really need to
6087 // uniq the methods of each class as well as the class itself.
6088 // <rdar://problem/11240464>
6089 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00006090 }
Greg Clayton931180e2011-01-27 06:44:37 +00006091 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006092 }
6093 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006094 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006095 }
Greg Clayton24739922010-10-13 03:15:28 +00006096
6097 if (!type_handled)
6098 {
6099 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00006100 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
6101 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006102 clang_type,
6103 storage,
6104 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006105
6106// if (template_param_infos.GetSize() > 0)
6107// {
6108// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
6109// function_decl,
6110// type_name_cstr,
6111// template_param_infos);
6112//
6113// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
6114// func_template_decl,
6115// template_param_infos);
6116// }
Greg Clayton24739922010-10-13 03:15:28 +00006117 // Add the decl to our DIE to decl context map
6118 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00006119 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00006120 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006121 ast.SetFunctionParameters (function_decl,
6122 &function_param_decls.front(),
6123 function_param_decls.size());
Sean Callanan60217122012-04-13 00:10:03 +00006124
6125 GetClangASTContext().SetMetadata((uintptr_t)function_decl, MakeUserID(die->GetOffset()));
Greg Clayton24739922010-10-13 03:15:28 +00006126 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006127 }
Greg Clayton81c22f62011-10-19 18:09:39 +00006128 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006129 this,
6130 type_name_const_str,
6131 0,
6132 NULL,
6133 LLDB_INVALID_UID,
6134 Type::eEncodingIsUID,
6135 &decl,
6136 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006137 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00006138 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006139 }
6140 break;
6141
6142 case DW_TAG_array_type:
6143 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006144 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006145 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006146
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006147 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006148 int64_t first_index = 0;
6149 uint32_t byte_stride = 0;
6150 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00006151 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006152
6153 if (num_attributes > 0)
6154 {
6155 uint32_t i;
6156 for (i=0; i<num_attributes; ++i)
6157 {
6158 attr = attributes.AttributeAtIndex(i);
6159 DWARFFormValue form_value;
6160 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6161 {
6162 switch (attr)
6163 {
6164 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6165 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6166 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6167 case DW_AT_name:
6168 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006169 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006170 break;
6171
6172 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006173 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006174 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
6175 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006176 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
6177 case DW_AT_declaration: break; // is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006178 case DW_AT_allocated:
6179 case DW_AT_associated:
6180 case DW_AT_data_location:
6181 case DW_AT_description:
6182 case DW_AT_ordering:
6183 case DW_AT_start_scope:
6184 case DW_AT_visibility:
6185 case DW_AT_specification:
6186 case DW_AT_abstract_origin:
6187 case DW_AT_sibling:
6188 break;
6189 }
6190 }
6191 }
6192
Greg Clayton81c22f62011-10-19 18:09:39 +00006193 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 +00006194
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006195 Type *element_type = ResolveTypeUID(type_die_offset);
6196
6197 if (element_type)
6198 {
6199 std::vector<uint64_t> element_orders;
6200 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
6201 if (byte_stride == 0 && bit_stride == 0)
6202 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00006203 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006204 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
6205 uint64_t num_elements = 0;
6206 std::vector<uint64_t>::const_reverse_iterator pos;
6207 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
6208 for (pos = element_orders.rbegin(); pos != end; ++pos)
6209 {
6210 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006211 clang_type = ast.CreateArrayType (array_element_type,
6212 num_elements,
6213 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006214 array_element_type = clang_type;
6215 array_element_bit_stride = array_element_bit_stride * num_elements;
6216 }
6217 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00006218 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006219 this,
6220 empty_name,
6221 array_element_bit_stride / 8,
6222 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00006223 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006224 Type::eEncodingIsUID,
6225 &decl,
6226 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006227 Type::eResolveStateFull));
6228 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006229 }
6230 }
6231 }
6232 break;
6233
Greg Clayton9b81a312010-06-12 01:20:30 +00006234 case DW_TAG_ptr_to_member_type:
6235 {
6236 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
6237 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
6238
Greg Claytond88d7592010-09-15 08:33:30 +00006239 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00006240
6241 if (num_attributes > 0) {
6242 uint32_t i;
6243 for (i=0; i<num_attributes; ++i)
6244 {
6245 attr = attributes.AttributeAtIndex(i);
6246 DWARFFormValue form_value;
6247 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6248 {
6249 switch (attr)
6250 {
6251 case DW_AT_type:
6252 type_die_offset = form_value.Reference(dwarf_cu); break;
6253 case DW_AT_containing_type:
6254 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
6255 }
6256 }
6257 }
6258
6259 Type *pointee_type = ResolveTypeUID(type_die_offset);
6260 Type *class_type = ResolveTypeUID(containing_type_die_offset);
6261
Greg Clayton526e5af2010-11-13 03:52:47 +00006262 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
6263 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00006264
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006265 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
6266 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00006267
Greg Clayton526e5af2010-11-13 03:52:47 +00006268 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
6269 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00006270
Greg Clayton81c22f62011-10-19 18:09:39 +00006271 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006272 this,
6273 type_name_const_str,
6274 byte_size,
6275 NULL,
6276 LLDB_INVALID_UID,
6277 Type::eEncodingIsUID,
6278 NULL,
6279 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006280 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00006281 }
6282
6283 break;
6284 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006285 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00006286 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006287 break;
6288 }
6289
6290 if (type_sp.get())
6291 {
6292 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6293 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6294
6295 SymbolContextScope * symbol_context_scope = NULL;
6296 if (sc_parent_tag == DW_TAG_compile_unit)
6297 {
6298 symbol_context_scope = sc.comp_unit;
6299 }
6300 else if (sc.function != NULL)
6301 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006302 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006303 if (symbol_context_scope == NULL)
6304 symbol_context_scope = sc.function;
6305 }
6306
6307 if (symbol_context_scope != NULL)
6308 {
6309 type_sp->SetSymbolContextScope(symbol_context_scope);
6310 }
6311
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006312 // We are ready to put this type into the uniqued list up at the module level
6313 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00006314
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006315 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006316 }
6317 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00006318 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006319 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006320 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006321 }
6322 }
6323 return type_sp;
6324}
6325
6326size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00006327SymbolFileDWARF::ParseTypes
6328(
6329 const SymbolContext& sc,
6330 DWARFCompileUnit* dwarf_cu,
6331 const DWARFDebugInfoEntry *die,
6332 bool parse_siblings,
6333 bool parse_children
6334)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006335{
6336 size_t types_added = 0;
6337 while (die != NULL)
6338 {
6339 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00006340 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006341 {
6342 if (type_is_new)
6343 ++types_added;
6344 }
6345
6346 if (parse_children && die->HasChildren())
6347 {
6348 if (die->Tag() == DW_TAG_subprogram)
6349 {
6350 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00006351 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006352 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
6353 }
6354 else
6355 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
6356 }
6357
6358 if (parse_siblings)
6359 die = die->GetSibling();
6360 else
6361 die = NULL;
6362 }
6363 return types_added;
6364}
6365
6366
6367size_t
6368SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
6369{
6370 assert(sc.comp_unit && sc.function);
6371 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00006372 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006373 if (dwarf_cu)
6374 {
6375 dw_offset_t function_die_offset = sc.function->GetID();
6376 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
6377 if (function_die)
6378 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00006379 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006380 }
6381 }
6382
6383 return functions_added;
6384}
6385
6386
6387size_t
6388SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
6389{
6390 // At least a compile unit must be valid
6391 assert(sc.comp_unit);
6392 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00006393 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006394 if (dwarf_cu)
6395 {
6396 if (sc.function)
6397 {
6398 dw_offset_t function_die_offset = sc.function->GetID();
6399 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
6400 if (func_die && func_die->HasChildren())
6401 {
6402 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
6403 }
6404 }
6405 else
6406 {
6407 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
6408 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
6409 {
6410 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
6411 }
6412 }
6413 }
6414
6415 return types_added;
6416}
6417
6418size_t
6419SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
6420{
6421 if (sc.comp_unit != NULL)
6422 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00006423 DWARFDebugInfo* info = DebugInfo();
6424 if (info == NULL)
6425 return 0;
6426
6427 uint32_t cu_idx = UINT32_MAX;
6428 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006429
6430 if (dwarf_cu == NULL)
6431 return 0;
6432
6433 if (sc.function)
6434 {
6435 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00006436
6437 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 +00006438 if (func_lo_pc != DW_INVALID_ADDRESS)
6439 {
6440 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00006441
Greg Claytone38a5ed2012-01-05 03:57:59 +00006442 // Let all blocks know they have parse all their variables
6443 sc.function->GetBlock (false).SetDidParseVariables (true, true);
6444 return num_variables;
6445 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006446 }
6447 else if (sc.comp_unit)
6448 {
6449 uint32_t vars_added = 0;
6450 VariableListSP variables (sc.comp_unit->GetVariableList(false));
6451
6452 if (variables.get() == NULL)
6453 {
6454 variables.reset(new VariableList());
6455 sc.comp_unit->SetVariableList(variables);
6456
Greg Claytond4a2b372011-09-12 23:21:58 +00006457 DWARFCompileUnit* match_dwarf_cu = NULL;
6458 const DWARFDebugInfoEntry* die = NULL;
6459 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00006460 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00006461 {
Greg Clayton97fbc342011-10-20 22:30:33 +00006462 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00006463 {
6464 DWARFMappedHash::DIEInfoArray hash_data_array;
6465 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
6466 dwarf_cu->GetNextCompileUnitOffset(),
6467 hash_data_array))
6468 {
6469 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
6470 }
6471 }
Greg Clayton7f995132011-10-04 22:41:51 +00006472 }
6473 else
6474 {
6475 // Index if we already haven't to make sure the compile units
6476 // get indexed and make their global DIE index list
6477 if (!m_indexed)
6478 Index ();
6479
6480 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
6481 dwarf_cu->GetNextCompileUnitOffset(),
6482 die_offsets);
6483 }
6484
6485 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00006486 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006487 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006488 DWARFDebugInfo* debug_info = DebugInfo();
6489 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006490 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006491 const dw_offset_t die_offset = die_offsets[i];
6492 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00006493 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00006494 {
Greg Clayton95d87902011-11-11 03:16:25 +00006495 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
6496 if (var_sp)
6497 {
6498 variables->AddVariableIfUnique (var_sp);
6499 ++vars_added;
6500 }
Greg Claytond4a2b372011-09-12 23:21:58 +00006501 }
Greg Clayton95d87902011-11-11 03:16:25 +00006502 else
6503 {
6504 if (m_using_apple_tables)
6505 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006506 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 +00006507 }
6508 }
6509
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006510 }
6511 }
6512 }
6513 return vars_added;
6514 }
6515 }
6516 return 0;
6517}
6518
6519
6520VariableSP
6521SymbolFileDWARF::ParseVariableDIE
6522(
6523 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006524 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006525 const DWARFDebugInfoEntry *die,
6526 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006527)
6528{
6529
Greg Clayton83c5cd92010-11-14 22:13:40 +00006530 VariableSP var_sp (m_die_to_variable_sp[die]);
6531 if (var_sp)
6532 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006533
6534 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00006535
6536 if ((tag == DW_TAG_variable) ||
6537 (tag == DW_TAG_constant) ||
6538 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006539 {
Greg Clayton7f995132011-10-04 22:41:51 +00006540 DWARFDebugInfoEntry::Attributes attributes;
6541 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
6542 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006543 {
Greg Clayton7f995132011-10-04 22:41:51 +00006544 const char *name = NULL;
6545 const char *mangled = NULL;
6546 Declaration decl;
6547 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00006548 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00006549 DWARFExpression location;
6550 bool is_external = false;
6551 bool is_artificial = false;
6552 bool location_is_const_value_data = false;
Greg Clayton23f59502012-07-17 03:23:13 +00006553 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00006554
6555 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006556 {
Greg Clayton7f995132011-10-04 22:41:51 +00006557 dw_attr_t attr = attributes.AttributeAtIndex(i);
6558 DWARFFormValue form_value;
6559 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006560 {
Greg Clayton7f995132011-10-04 22:41:51 +00006561 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006562 {
Greg Clayton7f995132011-10-04 22:41:51 +00006563 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6564 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6565 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6566 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
6567 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00006568 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00006569 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
6570 case DW_AT_const_value:
6571 location_is_const_value_data = true;
6572 // Fall through...
6573 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006574 {
Greg Clayton7f995132011-10-04 22:41:51 +00006575 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006576 {
Greg Clayton7f995132011-10-04 22:41:51 +00006577 const DataExtractor& debug_info_data = get_debug_info_data();
6578
6579 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
6580 uint32_t block_length = form_value.Unsigned();
Greg Clayton7f4c7432012-08-11 00:49:14 +00006581 location.CopyOpcodeData(get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00006582 }
6583 else
6584 {
6585 const DataExtractor& debug_loc_data = get_debug_loc_data();
6586 const dw_offset_t debug_loc_offset = form_value.Unsigned();
6587
6588 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
6589 if (loc_list_length > 0)
6590 {
Greg Clayton7f4c7432012-08-11 00:49:14 +00006591 location.CopyOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00006592 assert (func_low_pc != LLDB_INVALID_ADDRESS);
6593 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
6594 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006595 }
6596 }
Greg Clayton7f995132011-10-04 22:41:51 +00006597 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006598
Greg Clayton7f995132011-10-04 22:41:51 +00006599 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Greg Clayton23f59502012-07-17 03:23:13 +00006600 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00006601 case DW_AT_declaration:
6602 case DW_AT_description:
6603 case DW_AT_endianity:
6604 case DW_AT_segment:
6605 case DW_AT_start_scope:
6606 case DW_AT_visibility:
6607 default:
6608 case DW_AT_abstract_origin:
6609 case DW_AT_sibling:
6610 case DW_AT_specification:
6611 break;
6612 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006613 }
6614 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006615
Greg Clayton7f995132011-10-04 22:41:51 +00006616 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006617 {
Greg Clayton7f995132011-10-04 22:41:51 +00006618 ValueType scope = eValueTypeInvalid;
6619
6620 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6621 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006622 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00006623
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006624 // DWARF doesn't specify if a DW_TAG_variable is a local, global
6625 // or static variable, so we have to do a little digging by
6626 // looking at the location of a varaible to see if it contains
6627 // a DW_OP_addr opcode _somewhere_ in the definition. I say
6628 // somewhere because clang likes to combine small global variables
6629 // into the same symbol and have locations like:
6630 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
6631 // So if we don't have a DW_TAG_formal_parameter, we can look at
6632 // the location to see if it contains a DW_OP_addr opcode, and
6633 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00006634 if (tag == DW_TAG_formal_parameter)
6635 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00006636 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006637 {
Greg Clayton96c09682012-01-04 22:56:43 +00006638 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00006639 // Check if the location has a DW_OP_addr with any address value...
6640 addr_t location_has_op_addr = false;
6641 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00006642 {
6643 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
6644 if (op_error)
6645 {
6646 StreamString strm;
6647 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006648 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 +00006649 }
6650 }
Greg Claytond1767f02011-12-08 02:13:16 +00006651
6652 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006653 {
Greg Claytond1767f02011-12-08 02:13:16 +00006654 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006655 {
Greg Claytond1767f02011-12-08 02:13:16 +00006656 scope = eValueTypeVariableGlobal;
6657
Greg Clayton1f746072012-08-29 21:13:06 +00006658 if (GetDebugMapSymfile ())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006659 {
Greg Claytond1767f02011-12-08 02:13:16 +00006660 // When leaving the DWARF in the .o files on darwin,
6661 // when we have a global variable that wasn't initialized,
6662 // the .o file might not have allocated a virtual
6663 // address for the global variable. In this case it will
6664 // have created a symbol for the global variable
6665 // that is undefined and external and the value will
6666 // be the byte size of the variable. When we do the
6667 // address map in SymbolFileDWARFDebugMap we rely on
6668 // having an address, we need to do some magic here
6669 // so we can get the correct address for our global
6670 // variable. The address for all of these entries
6671 // will be zero, and there will be an undefined symbol
6672 // in this object file, and the executable will have
6673 // a matching symbol with a good address. So here we
6674 // dig up the correct address and replace it in the
6675 // location for the variable, and set the variable's
6676 // symbol context scope to be that of the main executable
6677 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00006678 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006679 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006680
Greg Claytond1767f02011-12-08 02:13:16 +00006681 // we have a possible uninitialized extern global
6682 Symtab *symtab = m_obj_file->GetSymtab();
6683 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006684 {
Greg Claytond1767f02011-12-08 02:13:16 +00006685 ConstString const_name(name);
6686 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
6687 eSymbolTypeUndefined,
6688 Symtab::eDebugNo,
6689 Symtab::eVisibilityExtern);
6690
6691 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006692 {
Greg Claytond1767f02011-12-08 02:13:16 +00006693 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
6694 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006695 {
Greg Claytond1767f02011-12-08 02:13:16 +00006696 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
6697 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
6698 eSymbolTypeData,
6699 Symtab::eDebugYes,
6700 Symtab::eVisibilityExtern);
6701 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006702 {
Greg Claytone7612132012-03-07 21:03:09 +00006703 if (defined_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006704 {
Greg Claytone7612132012-03-07 21:03:09 +00006705 const addr_t defined_addr = defined_symbol->GetAddress().GetFileAddress();
Greg Claytond1767f02011-12-08 02:13:16 +00006706 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006707 {
Greg Claytond1767f02011-12-08 02:13:16 +00006708 if (location.Update_DW_OP_addr (defined_addr))
6709 {
6710 symbol_context_scope = defined_symbol;
6711 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006712 }
6713 }
6714 }
6715 }
6716 }
6717 }
6718 }
6719 }
6720 }
Greg Claytond1767f02011-12-08 02:13:16 +00006721 else
6722 {
6723 scope = eValueTypeVariableStatic;
6724 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006725 }
6726 else
Greg Claytond1767f02011-12-08 02:13:16 +00006727 {
6728 scope = eValueTypeVariableLocal;
6729 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006730 }
Greg Clayton7f995132011-10-04 22:41:51 +00006731
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006732 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00006733 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006734 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00006735 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006736 case DW_TAG_subprogram:
6737 case DW_TAG_inlined_subroutine:
6738 case DW_TAG_lexical_block:
6739 if (sc.function)
6740 {
6741 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
6742 if (symbol_context_scope == NULL)
6743 symbol_context_scope = sc.function;
6744 }
6745 break;
6746
6747 default:
6748 symbol_context_scope = sc.comp_unit;
6749 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00006750 }
Greg Clayton7f995132011-10-04 22:41:51 +00006751 }
6752
Greg Clayton5cf58b92011-10-05 22:22:08 +00006753 if (symbol_context_scope)
6754 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006755 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
6756 name,
6757 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00006758 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00006759 scope,
6760 symbol_context_scope,
6761 &decl,
6762 location,
6763 is_external,
6764 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00006765
6766 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
6767 }
6768 else
6769 {
6770 // Not ready to parse this variable yet. It might be a global
6771 // or static variable that is in a function scope and the function
6772 // in the symbol context wasn't filled in yet
6773 return var_sp;
6774 }
Greg Clayton7f995132011-10-04 22:41:51 +00006775 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006776 }
Greg Clayton7f995132011-10-04 22:41:51 +00006777 // Cache var_sp even if NULL (the variable was just a specification or
6778 // was missing vital information to be able to be displayed in the debugger
6779 // (missing location due to optimization, etc)) so we don't re-parse
6780 // this DIE over and over later...
6781 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006782 }
6783 return var_sp;
6784}
6785
Greg Claytonc662ec82011-06-17 22:10:16 +00006786
6787const DWARFDebugInfoEntry *
6788SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
6789 dw_offset_t spec_block_die_offset,
6790 DWARFCompileUnit **result_die_cu_handle)
6791{
6792 // Give the concrete function die specified by "func_die_offset", find the
6793 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6794 // to "spec_block_die_offset"
6795 DWARFDebugInfo* info = DebugInfo();
6796
6797 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
6798 if (die)
6799 {
6800 assert (*result_die_cu_handle);
6801 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
6802 }
6803 return NULL;
6804}
6805
6806
6807const DWARFDebugInfoEntry *
6808SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
6809 const DWARFDebugInfoEntry *die,
6810 dw_offset_t spec_block_die_offset,
6811 DWARFCompileUnit **result_die_cu_handle)
6812{
6813 if (die)
6814 {
6815 switch (die->Tag())
6816 {
6817 case DW_TAG_subprogram:
6818 case DW_TAG_inlined_subroutine:
6819 case DW_TAG_lexical_block:
6820 {
6821 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
6822 {
6823 *result_die_cu_handle = dwarf_cu;
6824 return die;
6825 }
6826
6827 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
6828 {
6829 *result_die_cu_handle = dwarf_cu;
6830 return die;
6831 }
6832 }
6833 break;
6834 }
6835
6836 // Give the concrete function die specified by "func_die_offset", find the
6837 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6838 // to "spec_block_die_offset"
6839 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
6840 {
6841 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
6842 child_die,
6843 spec_block_die_offset,
6844 result_die_cu_handle);
6845 if (result_die)
6846 return result_die;
6847 }
6848 }
6849
6850 *result_die_cu_handle = NULL;
6851 return NULL;
6852}
6853
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006854size_t
6855SymbolFileDWARF::ParseVariables
6856(
6857 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006858 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006859 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006860 const DWARFDebugInfoEntry *orig_die,
6861 bool parse_siblings,
6862 bool parse_children,
6863 VariableList* cc_variable_list
6864)
6865{
6866 if (orig_die == NULL)
6867 return 0;
6868
Greg Claytonc662ec82011-06-17 22:10:16 +00006869 VariableListSP variable_list_sp;
6870
6871 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006872 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00006873 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006874 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006875 dw_tag_t tag = die->Tag();
6876
6877 // Check to see if we have already parsed this variable or constant?
6878 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006879 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006880 if (cc_variable_list)
6881 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006882 }
6883 else
6884 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006885 // We haven't already parsed it, lets do that now.
6886 if ((tag == DW_TAG_variable) ||
6887 (tag == DW_TAG_constant) ||
6888 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006889 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006890 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00006891 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006892 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
6893 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6894 switch (parent_tag)
6895 {
6896 case DW_TAG_compile_unit:
6897 if (sc.comp_unit != NULL)
6898 {
6899 variable_list_sp = sc.comp_unit->GetVariableList(false);
6900 if (variable_list_sp.get() == NULL)
6901 {
6902 variable_list_sp.reset(new VariableList());
6903 sc.comp_unit->SetVariableList(variable_list_sp);
6904 }
6905 }
6906 else
6907 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006908 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
6909 MakeUserID(sc_parent_die->GetOffset()),
6910 DW_TAG_value_to_name (parent_tag),
6911 MakeUserID(orig_die->GetOffset()),
6912 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006913 }
6914 break;
6915
6916 case DW_TAG_subprogram:
6917 case DW_TAG_inlined_subroutine:
6918 case DW_TAG_lexical_block:
6919 if (sc.function != NULL)
6920 {
6921 // Check to see if we already have parsed the variables for the given scope
6922
Greg Clayton81c22f62011-10-19 18:09:39 +00006923 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006924 if (block == NULL)
6925 {
6926 // This must be a specification or abstract origin with
6927 // a concrete block couterpart in the current function. We need
6928 // to find the concrete block so we can correctly add the
6929 // variable to it
6930 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
6931 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
6932 sc_parent_die->GetOffset(),
6933 &concrete_block_die_cu);
6934 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00006935 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006936 }
6937
6938 if (block != NULL)
6939 {
6940 const bool can_create = false;
6941 variable_list_sp = block->GetBlockVariableList (can_create);
6942 if (variable_list_sp.get() == NULL)
6943 {
6944 variable_list_sp.reset(new VariableList());
6945 block->SetVariableList(variable_list_sp);
6946 }
6947 }
6948 }
6949 break;
6950
6951 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00006952 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
6953 MakeUserID(orig_die->GetOffset()),
6954 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006955 break;
6956 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00006957 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006958
6959 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006960 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00006961 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
6962 if (var_sp)
6963 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006964 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00006965 if (cc_variable_list)
6966 cc_variable_list->AddVariableIfUnique (var_sp);
6967 ++vars_added;
6968 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006969 }
6970 }
6971 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006972
6973 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
6974
6975 if (!skip_children && parse_children && die->HasChildren())
6976 {
6977 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
6978 }
6979
6980 if (parse_siblings)
6981 die = die->GetSibling();
6982 else
6983 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006984 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006985 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006986}
6987
6988//------------------------------------------------------------------
6989// PluginInterface protocol
6990//------------------------------------------------------------------
6991const char *
6992SymbolFileDWARF::GetPluginName()
6993{
6994 return "SymbolFileDWARF";
6995}
6996
6997const char *
6998SymbolFileDWARF::GetShortPluginName()
6999{
7000 return GetPluginNameStatic();
7001}
7002
7003uint32_t
7004SymbolFileDWARF::GetPluginVersion()
7005{
7006 return 1;
7007}
7008
7009void
Greg Clayton6beaaa62011-01-17 03:46:26 +00007010SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
7011{
7012 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7013 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7014 if (clang_type)
7015 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7016}
7017
7018void
7019SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
7020{
7021 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7022 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7023 if (clang_type)
7024 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7025}
7026
Greg Claytona2721472011-06-25 00:44:06 +00007027void
Sean Callanancc427fa2011-07-30 02:42:06 +00007028SymbolFileDWARF::DumpIndexes ()
7029{
7030 StreamFile s(stdout, false);
7031
7032 s.Printf ("DWARF index for (%s) '%s/%s':",
7033 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
7034 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
7035 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
7036 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
7037 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
7038 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
7039 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
7040 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
7041 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
7042 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
7043 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
7044}
7045
7046void
7047SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
7048 const char *name,
7049 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00007050{
Sean Callanancc427fa2011-07-30 02:42:06 +00007051 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00007052
7053 if (iter == m_decl_ctx_to_die.end())
7054 return;
7055
Greg Claytoncb5860a2011-10-13 23:49:28 +00007056 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00007057 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007058 const DWARFDebugInfoEntry *context_die = *pos;
7059
7060 if (!results)
7061 return;
7062
7063 DWARFDebugInfo* info = DebugInfo();
7064
7065 DIEArray die_offsets;
7066
7067 DWARFCompileUnit* dwarf_cu = NULL;
7068 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00007069
7070 if (m_using_apple_tables)
7071 {
7072 if (m_apple_types_ap.get())
7073 m_apple_types_ap->FindByName (name, die_offsets);
7074 }
7075 else
7076 {
7077 if (!m_indexed)
7078 Index ();
7079
7080 m_type_index.Find (ConstString(name), die_offsets);
7081 }
7082
Greg Clayton220a0072011-12-09 08:48:30 +00007083 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007084
7085 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00007086 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007087 for (size_t i = 0; i < num_matches; ++i)
7088 {
7089 const dw_offset_t die_offset = die_offsets[i];
7090 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00007091
Greg Claytoncb5860a2011-10-13 23:49:28 +00007092 if (die->GetParent() != context_die)
7093 continue;
7094
7095 Type *matching_type = ResolveType (dwarf_cu, die);
7096
Greg Clayton42ce2f32011-12-12 21:50:19 +00007097 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007098 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
7099
7100 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
7101 {
7102 clang::TagDecl *tag_decl = tag_type->getDecl();
7103 results->push_back(tag_decl);
7104 }
7105 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
7106 {
7107 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
7108 results->push_back(typedef_decl);
7109 }
Greg Claytona2721472011-06-25 00:44:06 +00007110 }
7111 }
7112 }
7113}
7114
7115void
7116SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00007117 const clang::DeclContext *decl_context,
7118 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00007119 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
7120{
Greg Clayton85ae2e12011-10-18 23:36:41 +00007121
7122 switch (decl_context->getDeclKind())
7123 {
7124 case clang::Decl::Namespace:
7125 case clang::Decl::TranslationUnit:
7126 {
7127 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7128 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
7129 }
7130 break;
7131 default:
7132 break;
7133 }
Greg Claytona2721472011-06-25 00:44:06 +00007134}
Greg Claytoncaab74e2012-01-28 00:48:57 +00007135
7136bool
7137SymbolFileDWARF::LayoutRecordType (void *baton,
7138 const clang::RecordDecl *record_decl,
7139 uint64_t &size,
7140 uint64_t &alignment,
7141 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7142 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7143 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7144{
7145 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7146 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
7147}
7148
7149
7150bool
7151SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
7152 uint64_t &bit_size,
7153 uint64_t &alignment,
7154 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7155 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7156 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7157{
7158 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
7159 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
7160 bool success = false;
7161 base_offsets.clear();
7162 vbase_offsets.clear();
7163 if (pos != m_record_decl_to_layout_map.end())
7164 {
7165 bit_size = pos->second.bit_size;
7166 alignment = pos->second.alignment;
7167 field_offsets.swap(pos->second.field_offsets);
7168 m_record_decl_to_layout_map.erase(pos);
7169 success = true;
7170 }
7171 else
7172 {
7173 bit_size = 0;
7174 alignment = 0;
7175 field_offsets.clear();
7176 }
7177
7178 if (log)
7179 GetObjectFile()->GetModule()->LogMessage (log.get(),
7180 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
7181 record_decl,
7182 bit_size,
7183 alignment,
7184 (uint32_t)field_offsets.size(),
7185 (uint32_t)base_offsets.size(),
7186 (uint32_t)vbase_offsets.size(),
7187 success);
7188 return success;
7189}
7190
7191
Greg Clayton1f746072012-08-29 21:13:06 +00007192SymbolFileDWARFDebugMap *
7193SymbolFileDWARF::GetDebugMapSymfile ()
7194{
7195 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
7196 {
7197 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
7198 if (module_sp)
7199 {
7200 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
7201 if (sym_vendor)
7202 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
7203 }
7204 }
7205 return m_debug_map_symfile;
7206}
7207
Greg Claytoncaab74e2012-01-28 00:48:57 +00007208