blob: 73f4166126bf430179e8f045e4a938e4ba329128 [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();
Jim Ingham28eb5712012-10-12 17:34:26 +0000924 if (die)
925 {
926 const uint32_t language = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
927 if (language)
928 return (lldb::LanguageType)language;
929 }
Greg Clayton1f746072012-08-29 21:13:06 +0000930 }
931 return eLanguageTypeUnknown;
932}
933
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934size_t
935SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
936{
937 assert (sc.comp_unit);
938 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +0000939 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 if (dwarf_cu)
941 {
942 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +0000943 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +0000945 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946 {
947 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000948 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949 {
950 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
951 ++functions_added;
952 }
953 }
954 //FixupTypes();
955 }
956 return functions_added;
957}
958
959bool
960SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
961{
962 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +0000963 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Claytonda2455b2012-11-01 17:28:37 +0000964 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965 {
Greg Claytonda2455b2012-11-01 17:28:37 +0000966 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967
Greg Claytonda2455b2012-11-01 17:28:37 +0000968 if (cu_die)
969 {
970 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
971 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 +0000972
Greg Claytonda2455b2012-11-01 17:28:37 +0000973 // All file indexes in DWARF are one based and a file of index zero is
974 // supposed to be the compile unit itself.
975 support_files.Append (*sc.comp_unit);
976
977 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
978 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979 }
980 return false;
981}
982
983struct ParseDWARFLineTableCallbackInfo
984{
985 LineTable* line_table;
986 const SectionList *section_list;
987 lldb::addr_t prev_sect_file_base_addr;
988 lldb::addr_t curr_sect_file_base_addr;
989 bool is_oso_for_debug_map;
990 bool prev_in_final_executable;
991 DWARFDebugLine::Row prev_row;
992 SectionSP prev_section_sp;
993 SectionSP curr_section_sp;
994};
995
996//----------------------------------------------------------------------
997// ParseStatementTableCallback
998//----------------------------------------------------------------------
999static void
1000ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1001{
1002 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
1003 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1004 {
1005 // Just started parsing the line table
1006 }
1007 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1008 {
1009 // Done parsing line table, nothing to do for the cleanup
1010 }
1011 else
1012 {
1013 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
1014 // We have a new row, lets append it
1015
1016 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
1017 {
1018 info->prev_section_sp = info->curr_section_sp;
1019 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
1020 // If this is an end sequence entry, then we subtract one from the
1021 // address to make sure we get an address that is not the end of
1022 // a section.
1023 if (state.end_sequence && state.address != 0)
1024 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
1025 else
1026 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
1027
1028 if (info->curr_section_sp.get())
1029 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
1030 else
1031 info->curr_sect_file_base_addr = 0;
1032 }
1033 if (info->curr_section_sp.get())
1034 {
1035 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
1036 // Check for the fancy section magic to determine if we
1037
1038 if (info->is_oso_for_debug_map)
1039 {
1040 // When this is a debug map object file that contains DWARF
1041 // (referenced from an N_OSO debug map nlist entry) we will have
1042 // a file address in the file range for our section from the
1043 // original .o file, and a load address in the executable that
1044 // contains the debug map.
1045 //
1046 // If the sections for the file range and load range are
1047 // different, we have a remapped section for the function and
1048 // this address is resolved. If they are the same, then the
1049 // function for this address didn't make it into the final
1050 // executable.
Jim Inghamf94e1792012-08-11 00:35:26 +00001051 bool curr_in_final_executable = (bool) info->curr_section_sp->GetLinkedSection ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052
1053 // If we are doing DWARF with debug map, then we need to carefully
1054 // add each line table entry as there may be gaps as functions
1055 // get moved around or removed.
1056 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
1057 {
1058 if (info->prev_in_final_executable)
1059 {
1060 bool terminate_previous_entry = false;
1061 if (!curr_in_final_executable)
1062 {
1063 // Check for the case where the previous line entry
1064 // in a function made it into the final executable,
1065 // yet the current line entry falls in a function
1066 // that didn't. The line table used to be contiguous
1067 // through this address range but now it isn't. We
1068 // need to terminate the previous line entry so
1069 // that we can reconstruct the line range correctly
1070 // for it and to keep the line table correct.
1071 terminate_previous_entry = true;
1072 }
1073 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
1074 {
1075 // Check for cases where the line entries used to be
1076 // contiguous address ranges, but now they aren't.
1077 // This can happen when order files specify the
1078 // ordering of the functions.
1079 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
1080 Section *curr_sect = info->curr_section_sp.get();
1081 Section *prev_sect = info->prev_section_sp.get();
1082 assert (curr_sect->GetLinkedSection());
1083 assert (prev_sect->GetLinkedSection());
1084 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
1085 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
1086 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
1087 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
1088 if (object_file_addr_delta != linked_file_addr_delta)
1089 terminate_previous_entry = true;
1090 }
1091
1092 if (terminate_previous_entry)
1093 {
1094 line_table->InsertLineEntry (info->prev_section_sp,
1095 state.address - info->prev_sect_file_base_addr,
1096 info->prev_row.line,
1097 info->prev_row.column,
1098 info->prev_row.file,
1099 false, // is_stmt
1100 false, // basic_block
1101 false, // state.prologue_end
1102 false, // state.epilogue_begin
1103 true); // end_sequence);
1104 }
1105 }
1106 }
1107
1108 if (curr_in_final_executable)
1109 {
1110 line_table->InsertLineEntry (info->curr_section_sp,
1111 curr_line_section_offset,
1112 state.line,
1113 state.column,
1114 state.file,
1115 state.is_stmt,
1116 state.basic_block,
1117 state.prologue_end,
1118 state.epilogue_begin,
1119 state.end_sequence);
1120 info->prev_section_sp = info->curr_section_sp;
1121 }
1122 else
1123 {
1124 // If the current address didn't make it into the final
1125 // executable, the current section will be the __text
1126 // segment in the .o file, so we need to clear this so
1127 // we can catch the next function that did make it into
1128 // the final executable.
1129 info->prev_section_sp.reset();
1130 info->curr_section_sp.reset();
1131 }
1132
1133 info->prev_in_final_executable = curr_in_final_executable;
1134 }
1135 else
1136 {
1137 // We are not in an object file that contains DWARF for an
1138 // N_OSO, this is just a normal DWARF file. The DWARF spec
1139 // guarantees that the addresses will be in increasing order
1140 // so, since we store line tables in file address order, we
1141 // can always just append the line entry without needing to
1142 // search for the correct insertion point (we don't need to
1143 // use LineEntry::InsertLineEntry()).
1144 line_table->AppendLineEntry (info->curr_section_sp,
1145 curr_line_section_offset,
1146 state.line,
1147 state.column,
1148 state.file,
1149 state.is_stmt,
1150 state.basic_block,
1151 state.prologue_end,
1152 state.epilogue_begin,
1153 state.end_sequence);
1154 }
1155 }
1156
1157 info->prev_row = state;
1158 }
1159}
1160
1161bool
1162SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1163{
1164 assert (sc.comp_unit);
1165 if (sc.comp_unit->GetLineTable() != NULL)
1166 return true;
1167
Greg Clayton1f746072012-08-29 21:13:06 +00001168 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169 if (dwarf_cu)
1170 {
1171 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001172 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001174 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1175 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001177 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1178 if (line_table_ap.get())
1179 {
1180 ParseDWARFLineTableCallbackInfo info = {
1181 line_table_ap.get(),
1182 m_obj_file->GetSectionList(),
1183 0,
1184 0,
Greg Clayton1f746072012-08-29 21:13:06 +00001185 GetDebugMapSymfile () != NULL,
Greg Clayton129d12c2011-11-28 03:29:03 +00001186 false,
1187 DWARFDebugLine::Row(),
1188 SectionSP(),
1189 SectionSP()
1190 };
1191 uint32_t offset = cu_line_offset;
1192 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1193 sc.comp_unit->SetLineTable(line_table_ap.release());
1194 return true;
1195 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196 }
1197 }
1198 }
1199 return false;
1200}
1201
1202size_t
1203SymbolFileDWARF::ParseFunctionBlocks
1204(
1205 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001206 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001207 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 const DWARFDebugInfoEntry *die,
1209 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001210 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211)
1212{
1213 size_t blocks_added = 0;
1214 while (die != NULL)
1215 {
1216 dw_tag_t tag = die->Tag();
1217
1218 switch (tag)
1219 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001221 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222 case DW_TAG_lexical_block:
1223 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001224 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001225 if (tag == DW_TAG_subprogram)
1226 {
1227 // Skip any DW_TAG_subprogram DIEs that are inside
1228 // of a normal or inlined functions. These will be
1229 // parsed on their own as separate entities.
1230
1231 if (depth > 0)
1232 break;
1233
1234 block = parent_block;
1235 }
1236 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001237 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001238 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001239 parent_block->AddChild(block_sp);
1240 block = block_sp.get();
1241 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001242 DWARFDebugRanges::RangeList ranges;
1243 const char *name = NULL;
1244 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001246 int decl_file = 0;
1247 int decl_line = 0;
1248 int decl_column = 0;
1249 int call_file = 0;
1250 int call_line = 0;
1251 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001252 if (die->GetDIENamesAndRanges (this,
1253 dwarf_cu,
1254 name,
1255 mangled_name,
1256 ranges,
1257 decl_file, decl_line, decl_column,
1258 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 {
1260 if (tag == DW_TAG_subprogram)
1261 {
1262 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001263 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001265 else if (tag == DW_TAG_inlined_subroutine)
1266 {
1267 // We get called here for inlined subroutines in two ways.
1268 // The first time is when we are making the Function object
1269 // for this inlined concrete instance. Since we're creating a top level block at
1270 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1271 // adjust the containing address.
1272 // The second time is when we are parsing the blocks inside the function that contains
1273 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1274 // function the offset will be for that function.
1275 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1276 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001277 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001278 }
1279 }
1280
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001281 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282
1283 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1284 {
1285 std::auto_ptr<Declaration> decl_ap;
1286 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001287 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1288 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289
1290 std::auto_ptr<Declaration> call_ap;
1291 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001292 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1293 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001295 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296 }
1297
1298 ++blocks_added;
1299
Greg Claytondd7feaf2011-08-12 17:54:33 +00001300 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001301 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001302 blocks_added += ParseFunctionBlocks (sc,
1303 block,
1304 dwarf_cu,
1305 die->GetFirstChild(),
1306 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001307 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001308 }
1309 }
1310 }
1311 break;
1312 default:
1313 break;
1314 }
1315
Greg Claytondd7feaf2011-08-12 17:54:33 +00001316 // Only parse siblings of the block if we are not at depth zero. A depth
1317 // of zero indicates we are currently parsing the top level
1318 // DW_TAG_subprogram DIE
1319
1320 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001322 else
1323 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324 }
1325 return blocks_added;
1326}
1327
Greg Claytonf0705c82011-10-22 03:33:13 +00001328bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001329SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1330 const DWARFDebugInfoEntry *die,
1331 ClangASTContext::TemplateParameterInfos &template_param_infos)
1332{
1333 const dw_tag_t tag = die->Tag();
1334
1335 switch (tag)
1336 {
1337 case DW_TAG_template_type_parameter:
1338 case DW_TAG_template_value_parameter:
1339 {
1340 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1341
1342 DWARFDebugInfoEntry::Attributes attributes;
1343 const size_t num_attributes = die->GetAttributes (this,
1344 dwarf_cu,
1345 fixed_form_sizes,
1346 attributes);
1347 const char *name = NULL;
1348 Type *lldb_type = NULL;
1349 clang_type_t clang_type = NULL;
1350 uint64_t uval64 = 0;
1351 bool uval64_valid = false;
1352 if (num_attributes > 0)
1353 {
1354 DWARFFormValue form_value;
1355 for (size_t i=0; i<num_attributes; ++i)
1356 {
1357 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1358
1359 switch (attr)
1360 {
1361 case DW_AT_name:
1362 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1363 name = form_value.AsCString(&get_debug_str_data());
1364 break;
1365
1366 case DW_AT_type:
1367 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1368 {
1369 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1370 lldb_type = ResolveTypeUID(type_die_offset);
1371 if (lldb_type)
1372 clang_type = lldb_type->GetClangForwardType();
1373 }
1374 break;
1375
1376 case DW_AT_const_value:
1377 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1378 {
1379 uval64_valid = true;
1380 uval64 = form_value.Unsigned();
1381 }
1382 break;
1383 default:
1384 break;
1385 }
1386 }
1387
1388 if (name && lldb_type && clang_type)
1389 {
1390 bool is_signed = false;
1391 template_param_infos.names.push_back(name);
1392 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1393 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1394 {
1395 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
Sean Callanan3d654b32012-09-24 22:25:51 +00001396 template_param_infos.args.push_back (clang::TemplateArgument (*GetClangASTContext().getASTContext(),
1397 llvm::APSInt(apint),
1398 clang_qual_type));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001399 }
1400 else
1401 {
1402 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1403 }
1404 }
1405 else
1406 {
1407 return false;
1408 }
1409
1410 }
1411 }
1412 return true;
1413
1414 default:
1415 break;
1416 }
1417 return false;
1418}
1419
1420bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001421SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1422 const DWARFDebugInfoEntry *parent_die,
1423 ClangASTContext::TemplateParameterInfos &template_param_infos)
1424{
1425
1426 if (parent_die == NULL)
Filipe Cabecinhas52008c62012-05-23 16:24:11 +00001427 return false;
Greg Claytonf0705c82011-10-22 03:33:13 +00001428
Greg Claytonf0705c82011-10-22 03:33:13 +00001429 Args template_parameter_names;
1430 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1431 die != NULL;
1432 die = die->GetSibling())
1433 {
1434 const dw_tag_t tag = die->Tag();
1435
1436 switch (tag)
1437 {
1438 case DW_TAG_template_type_parameter:
1439 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001440 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001441 break;
1442
1443 default:
1444 break;
1445 }
1446 }
1447 if (template_param_infos.args.empty())
1448 return false;
1449 return template_param_infos.args.size() == template_param_infos.names.size();
1450}
1451
1452clang::ClassTemplateDecl *
1453SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001454 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001455 const char *parent_name,
1456 int tag_decl_kind,
1457 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1458{
1459 if (template_param_infos.IsValid())
1460 {
1461 std::string template_basename(parent_name);
1462 template_basename.erase (template_basename.find('<'));
1463 ClangASTContext &ast = GetClangASTContext();
1464
1465 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001466 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001467 template_basename.c_str(),
1468 tag_decl_kind,
1469 template_param_infos);
1470 }
1471 return NULL;
1472}
1473
Sean Callanana0b80ab2012-06-04 22:28:05 +00001474class SymbolFileDWARF::DelayedAddObjCClassProperty
1475{
1476public:
1477 DelayedAddObjCClassProperty
1478 (
1479 clang::ASTContext *ast,
1480 lldb::clang_type_t class_opaque_type,
1481 const char *property_name,
1482 lldb::clang_type_t property_opaque_type, // The property type is only required if you don't have an ivar decl
1483 clang::ObjCIvarDecl *ivar_decl,
1484 const char *property_setter_name,
1485 const char *property_getter_name,
1486 uint32_t property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00001487 const ClangASTMetadata *metadata
Sean Callanana0b80ab2012-06-04 22:28:05 +00001488 ) :
1489 m_ast (ast),
1490 m_class_opaque_type (class_opaque_type),
1491 m_property_name (property_name),
1492 m_property_opaque_type (property_opaque_type),
1493 m_ivar_decl (ivar_decl),
1494 m_property_setter_name (property_setter_name),
1495 m_property_getter_name (property_getter_name),
Jim Ingham379397632012-10-27 02:54:13 +00001496 m_property_attributes (property_attributes)
Sean Callanana0b80ab2012-06-04 22:28:05 +00001497 {
Jim Ingham379397632012-10-27 02:54:13 +00001498 if (metadata != NULL)
1499 {
1500 m_metadata_ap.reset(new ClangASTMetadata());
1501 *(m_metadata_ap.get()) = *metadata;
1502 }
1503 }
1504
1505 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
1506 {
1507 m_ast = rhs.m_ast;
1508 m_class_opaque_type = rhs.m_class_opaque_type;
1509 m_property_name = rhs.m_property_name;
1510 m_property_opaque_type = rhs.m_property_opaque_type;
1511 m_ivar_decl = rhs.m_ivar_decl;
1512 m_property_setter_name = rhs.m_property_setter_name;
1513 m_property_getter_name = rhs.m_property_getter_name;
1514 m_property_attributes = rhs.m_property_attributes;
1515
1516 if (rhs.m_metadata_ap.get())
1517 {
1518 m_metadata_ap.reset (new ClangASTMetadata());
1519 *(m_metadata_ap.get()) = *(rhs.m_metadata_ap.get());
1520 }
1521
Sean Callanana0b80ab2012-06-04 22:28:05 +00001522 }
1523
1524 bool Finalize() const
1525 {
1526 return ClangASTContext::AddObjCClassProperty(m_ast,
1527 m_class_opaque_type,
1528 m_property_name,
1529 m_property_opaque_type,
1530 m_ivar_decl,
1531 m_property_setter_name,
1532 m_property_getter_name,
1533 m_property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00001534 m_metadata_ap.get());
Sean Callanana0b80ab2012-06-04 22:28:05 +00001535 }
1536private:
1537 clang::ASTContext *m_ast;
1538 lldb::clang_type_t m_class_opaque_type;
1539 const char *m_property_name;
1540 lldb::clang_type_t m_property_opaque_type;
1541 clang::ObjCIvarDecl *m_ivar_decl;
1542 const char *m_property_setter_name;
1543 const char *m_property_getter_name;
1544 uint32_t m_property_attributes;
Jim Ingham379397632012-10-27 02:54:13 +00001545 std::auto_ptr<ClangASTMetadata> m_metadata_ap;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001546};
1547
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001548size_t
1549SymbolFileDWARF::ParseChildMembers
1550(
1551 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001552 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001553 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001554 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001555 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001556 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1557 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001558 DWARFDIECollection& member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00001559 DelayedPropertyList& delayed_properties,
Sean Callananc7fbf732010-08-06 00:32:49 +00001560 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001561 bool &is_a_class,
1562 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001563)
1564{
1565 if (parent_die == NULL)
1566 return 0;
1567
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568 size_t count = 0;
1569 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001570 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001571 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001572
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1574 {
1575 dw_tag_t tag = die->Tag();
1576
1577 switch (tag)
1578 {
1579 case DW_TAG_member:
Sean Callanan3d654b32012-09-24 22:25:51 +00001580 case DW_TAG_APPLE_property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001581 {
1582 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001583 const size_t num_attributes = die->GetAttributes (this,
1584 dwarf_cu,
1585 fixed_form_sizes,
1586 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001587 if (num_attributes > 0)
1588 {
1589 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001590 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001591 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001592 const char *prop_name = NULL;
1593 const char *prop_getter_name = NULL;
1594 const char *prop_setter_name = NULL;
1595 uint32_t prop_attributes = 0;
1596
1597
Greg Clayton24739922010-10-13 03:15:28 +00001598 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001599 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001600 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001601 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001602 size_t byte_size = 0;
1603 size_t bit_offset = 0;
1604 size_t bit_size = 0;
1605 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001606 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001607 {
1608 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1609 DWARFFormValue form_value;
1610 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1611 {
1612 switch (attr)
1613 {
1614 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1615 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1616 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1617 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1618 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1619 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1620 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1621 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1622 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001623 if (form_value.BlockData())
1624 {
1625 Value initialValue(0);
1626 Value memberOffset(0);
1627 const DataExtractor& debug_info_data = get_debug_info_data();
1628 uint32_t block_length = form_value.Unsigned();
1629 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1630 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1631 NULL, // clang::ASTContext *
1632 NULL, // ClangExpressionVariableList *
1633 NULL, // ClangExpressionDeclMap *
1634 NULL, // RegisterContext *
1635 debug_info_data,
1636 block_offset,
1637 block_length,
1638 eRegisterKindDWARF,
1639 &initialValue,
1640 memberOffset,
1641 NULL))
1642 {
1643 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1644 }
1645 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001646 break;
1647
Greg Clayton8cf05932010-07-22 18:30:50 +00001648 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1b02c172012-03-14 21:00:47 +00001649 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001650 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1651 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1652 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1653 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1654
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001656 case DW_AT_declaration:
1657 case DW_AT_description:
1658 case DW_AT_mutable:
1659 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001660 case DW_AT_sibling:
1661 break;
1662 }
1663 }
1664 }
Sean Callanana6582262012-04-05 00:12:52 +00001665
1666 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00001667 {
Sean Callanana6582262012-04-05 00:12:52 +00001668 ConstString fixed_getter;
1669 ConstString fixed_setter;
1670
1671 // Check if the property getter/setter were provided as full
1672 // names. We want basenames, so we extract them.
1673
1674 if (prop_getter_name && prop_getter_name[0] == '-')
1675 {
1676 ObjCLanguageRuntime::ParseMethodName (prop_getter_name,
1677 NULL,
1678 &fixed_getter,
1679 NULL,
1680 NULL);
1681 prop_getter_name = fixed_getter.GetCString();
1682 }
1683
1684 if (prop_setter_name && prop_setter_name[0] == '-')
1685 {
1686 ObjCLanguageRuntime::ParseMethodName (prop_setter_name,
1687 NULL,
1688 &fixed_setter,
1689 NULL,
1690 NULL);
1691 prop_setter_name = fixed_setter.GetCString();
1692 }
1693
1694 // If the names haven't been provided, they need to be
1695 // filled in.
1696
1697 if (!prop_getter_name)
1698 {
1699 prop_getter_name = prop_name;
1700 }
1701 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
1702 {
1703 StreamString ss;
1704
1705 ss.Printf("set%c%s:",
1706 toupper(prop_name[0]),
1707 &prop_name[1]);
1708
1709 fixed_setter.SetCString(ss.GetData());
1710 prop_setter_name = fixed_setter.GetCString();
1711 }
Sean Callanan751aac62012-03-29 19:07:06 +00001712 }
1713
1714 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001715 // represents fields that are references with bad byte size
1716 // and bit size/offset information such as:
1717 //
1718 // DW_AT_byte_size( 0x00 )
1719 // DW_AT_bit_size( 0x40 )
1720 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1721 //
1722 // So check the bit offset to make sure it is sane, and if
1723 // the values are not sane, remove them. If we don't do this
1724 // then we will end up with a crash if we try to use this
1725 // type in an expression when clang becomes unhappy with its
1726 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001727
Greg Clayton44953932011-10-25 01:25:35 +00001728 if (bit_offset > 128)
1729 {
1730 bit_size = 0;
1731 bit_offset = 0;
1732 }
1733
1734 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001735 if (class_language == eLanguageTypeObjC ||
1736 class_language == eLanguageTypeObjC_plus_plus)
1737 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001738
1739 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1740 {
1741 // Not all compilers will mark the vtable pointer
1742 // member as artificial (llvm-gcc). We can't have
1743 // the virtual members in our classes otherwise it
1744 // throws off all child offsets since we end up
1745 // having and extra pointer sized member in our
1746 // class layouts.
1747 is_artificial = true;
1748 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749
Greg Clayton24739922010-10-13 03:15:28 +00001750 if (is_artificial == false)
1751 {
1752 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001753 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001754 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001755 {
Sean Callanan751aac62012-03-29 19:07:06 +00001756 if (member_type)
1757 {
1758 if (accessibility == eAccessNone)
1759 accessibility = default_accessibility;
1760 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761
Sean Callanan751aac62012-03-29 19:07:06 +00001762 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
1763 name,
1764 member_type->GetClangLayoutType(),
1765 accessibility,
1766 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00001767
Jim Ingham379397632012-10-27 02:54:13 +00001768 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)field_decl, MakeUserID(die->GetOffset()));
Sean Callanan5b26f272012-02-04 08:49:35 +00001769 }
1770 else
1771 {
Sean Callanan751aac62012-03-29 19:07:06 +00001772 if (name)
1773 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1774 MakeUserID(die->GetOffset()),
1775 name,
1776 encoding_uid);
1777 else
1778 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1779 MakeUserID(die->GetOffset()),
1780 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00001781 }
Sean Callanan751aac62012-03-29 19:07:06 +00001782
1783 if (member_byte_offset != UINT32_MAX || bit_size != 0)
1784 {
1785 /////////////////////////////////////////////////////////////
1786 // How to locate a field given the DWARF debug information
1787 //
1788 // AT_byte_size indicates the size of the word in which the
1789 // bit offset must be interpreted.
1790 //
1791 // AT_data_member_location indicates the byte offset of the
1792 // word from the base address of the structure.
1793 //
1794 // AT_bit_offset indicates how many bits into the word
1795 // (according to the host endianness) the low-order bit of
1796 // the field starts. AT_bit_offset can be negative.
1797 //
1798 // AT_bit_size indicates the size of the field in bits.
1799 /////////////////////////////////////////////////////////////
Sean Callanan5b26f272012-02-04 08:49:35 +00001800
Sean Callanan751aac62012-03-29 19:07:06 +00001801 ByteOrder object_endian = GetObjectFile()->GetModule()->GetArchitecture().GetDefaultEndian();
1802
1803 uint64_t total_bit_offset = 0;
1804
1805 total_bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1806
1807 if (object_endian == eByteOrderLittle)
1808 {
1809 total_bit_offset += byte_size * 8;
1810 total_bit_offset -= (bit_offset + bit_size);
1811 }
1812 else
1813 {
1814 total_bit_offset += bit_offset;
1815 }
1816
1817 layout_info.field_offsets.insert(std::make_pair(field_decl, total_bit_offset));
1818 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001819 }
Sean Callanan751aac62012-03-29 19:07:06 +00001820
Jim Inghame3ae82a2011-11-12 01:36:43 +00001821 if (prop_name != NULL)
1822 {
Sean Callanan751aac62012-03-29 19:07:06 +00001823 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001824
Sean Callanan751aac62012-03-29 19:07:06 +00001825 if (field_decl)
1826 {
1827 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1828 assert (ivar_decl != NULL);
1829 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001830
Jim Ingham379397632012-10-27 02:54:13 +00001831 ClangASTMetadata metadata;
1832 metadata.SetUserID (MakeUserID(die->GetOffset()));
Sean Callanana0b80ab2012-06-04 22:28:05 +00001833 delayed_properties.push_back(DelayedAddObjCClassProperty(GetClangASTContext().getASTContext(),
1834 class_clang_type,
1835 prop_name,
1836 member_type->GetClangLayoutType(),
1837 ivar_decl,
1838 prop_setter_name,
1839 prop_getter_name,
1840 prop_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00001841 &metadata));
Sean Callanan60217122012-04-13 00:10:03 +00001842
Sean Callananad880762012-04-18 01:06:17 +00001843 if (ivar_decl)
Jim Ingham379397632012-10-27 02:54:13 +00001844 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00001845 }
Greg Clayton24739922010-10-13 03:15:28 +00001846 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001848 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001849 }
1850 break;
1851
1852 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001853 // Let the type parsing code handle this one for us.
1854 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001855 break;
1856
1857 case DW_TAG_inheritance:
1858 {
1859 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001860 if (default_accessibility == eAccessNone)
1861 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862 // TODO: implement DW_TAG_inheritance type parsing
1863 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001864 const size_t num_attributes = die->GetAttributes (this,
1865 dwarf_cu,
1866 fixed_form_sizes,
1867 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001868 if (num_attributes > 0)
1869 {
1870 Declaration decl;
1871 DWARFExpression location;
1872 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001873 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001874 bool is_virtual = false;
1875 bool is_base_of_class = true;
Greg Clayton23f59502012-07-17 03:23:13 +00001876 //off_t member_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877 uint32_t i;
1878 for (i=0; i<num_attributes; ++i)
1879 {
1880 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1881 DWARFFormValue form_value;
1882 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1883 {
1884 switch (attr)
1885 {
1886 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1887 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1888 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1889 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00001890// case DW_AT_data_member_location:
1891// if (form_value.BlockData())
1892// {
1893// Value initialValue(0);
1894// Value memberOffset(0);
1895// const DataExtractor& debug_info_data = get_debug_info_data();
1896// uint32_t block_length = form_value.Unsigned();
1897// uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1898// if (DWARFExpression::Evaluate (NULL,
1899// NULL,
1900// NULL,
1901// NULL,
1902// NULL,
1903// debug_info_data,
1904// block_offset,
1905// block_length,
1906// eRegisterKindDWARF,
1907// &initialValue,
1908// memberOffset,
1909// NULL))
1910// {
1911// member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1912// }
1913// }
1914// break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001915
1916 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001917 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001918 break;
1919
1920 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1921 default:
1922 case DW_AT_sibling:
1923 break;
1924 }
1925 }
1926 }
1927
Greg Clayton526e5af2010-11-13 03:52:47 +00001928 Type *base_class_type = ResolveTypeUID(encoding_uid);
1929 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001930
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001931 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1932 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001933 if (class_language == eLanguageTypeObjC)
1934 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001935 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001936 }
1937 else
1938 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001939 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001940 accessibility,
1941 is_virtual,
1942 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001943 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001944 }
1945 }
1946 break;
1947
1948 default:
1949 break;
1950 }
1951 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00001952
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953 return count;
1954}
1955
1956
1957clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001958SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001959{
1960 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001961 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001962 {
1963 DWARFCompileUnitSP cu_sp;
1964 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1965 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001966 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001967 }
1968 return NULL;
1969}
1970
1971clang::DeclContext*
1972SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1973{
Greg Clayton81c22f62011-10-19 18:09:39 +00001974 if (UserIDMatches(type_uid))
1975 return GetClangDeclContextForDIEOffset (sc, type_uid);
1976 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001977}
1978
1979Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001980SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981{
Greg Clayton81c22f62011-10-19 18:09:39 +00001982 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001983 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001984 DWARFDebugInfo* debug_info = DebugInfo();
1985 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001986 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001987 DWARFCompileUnitSP cu_sp;
1988 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001989 const bool assert_not_being_parsed = true;
1990 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001991 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001992 }
1993 return NULL;
1994}
1995
Greg Claytoncab36a32011-12-08 05:16:30 +00001996Type*
1997SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001998{
Greg Claytoncab36a32011-12-08 05:16:30 +00001999 if (die != NULL)
2000 {
Greg Clayton3bffb082011-12-10 02:15:28 +00002001 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
2002 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002003 GetObjectFile()->GetModule()->LogMessage (log.get(),
2004 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
2005 die->GetOffset(),
2006 DW_TAG_value_to_name(die->Tag()),
2007 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00002008
Greg Claytoncab36a32011-12-08 05:16:30 +00002009 // We might be coming in in the middle of a type tree (a class
2010 // withing a class, an enum within a class), so parse any needed
2011 // parent DIEs before we get to this one...
2012 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2013 switch (decl_ctx_die->Tag())
2014 {
2015 case DW_TAG_structure_type:
2016 case DW_TAG_union_type:
2017 case DW_TAG_class_type:
2018 {
2019 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00002020 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002021 GetObjectFile()->GetModule()->LogMessage (log.get(),
2022 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
2023 die->GetOffset(),
2024 DW_TAG_value_to_name(die->Tag()),
2025 die->GetName(this, cu),
2026 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00002027//
2028// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
2029// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
2030// {
2031// if (log)
2032// GetObjectFile()->GetModule()->LogMessage (log.get(),
2033// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
2034// die->GetOffset(),
2035// DW_TAG_value_to_name(die->Tag()),
2036// die->GetName(this, cu),
2037// decl_ctx_die->GetOffset());
2038// // Ask the type to complete itself if it already hasn't since if we
2039// // want a function (method or static) from a class, the class must
2040// // create itself and add it's own methods and class functions.
2041// if (parent_type)
2042// parent_type->GetClangFullType();
2043// }
Greg Claytoncab36a32011-12-08 05:16:30 +00002044 }
2045 break;
2046
2047 default:
2048 break;
2049 }
2050 return ResolveType (cu, die);
2051 }
2052 return NULL;
2053}
2054
Greg Clayton6beaaa62011-01-17 03:46:26 +00002055// This function is used when SymbolFileDWARFDebugMap owns a bunch of
2056// SymbolFileDWARF objects to detect if this DWARF file is the one that
2057// can resolve a clang_type.
2058bool
2059SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
2060{
2061 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2062 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
2063 return die != NULL;
2064}
2065
2066
Greg Clayton1be10fc2010-09-29 01:12:09 +00002067lldb::clang_type_t
2068SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
2069{
2070 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002071 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2072 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002073 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002074 {
2075 // We have already resolved this type...
2076 return clang_type;
2077 }
2078 // Once we start resolving this type, remove it from the forward declaration
2079 // map in case anyone child members or other types require this type to get resolved.
2080 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
2081 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002082 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00002083
Greg Clayton1be10fc2010-09-29 01:12:09 +00002084
Greg Clayton85ae2e12011-10-18 23:36:41 +00002085 // Disable external storage for this type so we don't get anymore
2086 // clang::ExternalASTSource queries for this type.
2087 ClangASTContext::SetHasExternalStorage (clang_type, false);
2088
Greg Clayton450e3f32010-10-12 02:24:53 +00002089 DWARFDebugInfo* debug_info = DebugInfo();
2090
Greg Clayton53eb1c22012-04-02 22:59:12 +00002091 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002092 Type *type = m_die_to_type.lookup (die);
2093
2094 const dw_tag_t tag = die->Tag();
2095
Greg Claytonbaf95da2012-03-30 23:50:54 +00002096 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00002097 if (log)
Greg Claytonbaf95da2012-03-30 23:50:54 +00002098 {
Greg Claytond61c0fc2012-04-23 22:55:20 +00002099 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log.get(),
2100 "0x%8.8llx: %s '%s' resolving forward declaration...",
2101 MakeUserID(die->GetOffset()),
2102 DW_TAG_value_to_name(tag),
2103 type->GetName().AsCString());
Greg Claytonbaf95da2012-03-30 23:50:54 +00002104
Greg Claytonbaf95da2012-03-30 23:50:54 +00002105 }
Greg Clayton1be10fc2010-09-29 01:12:09 +00002106 assert (clang_type);
2107 DWARFDebugInfoEntry::Attributes attributes;
2108
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002109 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002110
2111 switch (tag)
2112 {
2113 case DW_TAG_structure_type:
2114 case DW_TAG_union_type:
2115 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002116 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002117 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002118
Greg Clayton1be10fc2010-09-29 01:12:09 +00002119 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002120 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002121 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002122
Sean Callanan77a1fd32012-02-27 20:07:01 +00002123 LanguageType class_language = eLanguageTypeUnknown;
2124 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
2125 if (is_objc_class)
Greg Clayton219cf312012-03-30 00:51:13 +00002126 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002127 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002128 // For objective C we don't start the definition when
2129 // the class is created.
2130 ast.StartTagDeclarationDefinition (clang_type);
2131 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002132
2133 int tag_decl_kind = -1;
2134 AccessType default_accessibility = eAccessNone;
2135 if (tag == DW_TAG_structure_type)
2136 {
2137 tag_decl_kind = clang::TTK_Struct;
2138 default_accessibility = eAccessPublic;
2139 }
2140 else if (tag == DW_TAG_union_type)
2141 {
2142 tag_decl_kind = clang::TTK_Union;
2143 default_accessibility = eAccessPublic;
2144 }
2145 else if (tag == DW_TAG_class_type)
2146 {
2147 tag_decl_kind = clang::TTK_Class;
2148 default_accessibility = eAccessPrivate;
2149 }
2150
Greg Clayton53eb1c22012-04-02 22:59:12 +00002151 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002152 std::vector<clang::CXXBaseSpecifier *> base_classes;
2153 std::vector<int> member_accessibilities;
2154 bool is_a_class = false;
2155 // Parse members and base classes first
2156 DWARFDIECollection member_function_dies;
Sean Callanana0b80ab2012-06-04 22:28:05 +00002157
2158 DelayedPropertyList delayed_properties;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002159
2160 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002161 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002162 die,
2163 clang_type,
2164 class_language,
2165 base_classes,
2166 member_accessibilities,
2167 member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00002168 delayed_properties,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002169 default_accessibility,
2170 is_a_class,
2171 layout_info);
2172
2173 // Now parse any methods if there were any...
2174 size_t num_functions = member_function_dies.Size();
2175 if (num_functions > 0)
2176 {
2177 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002178 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002179 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002180 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002181 }
2182
2183 if (class_language == eLanguageTypeObjC)
2184 {
Greg Clayton84db9102012-03-26 23:03:23 +00002185 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(ast.getASTContext(), clang_type));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002186 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00002187 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002188
Sean Callanan77a1fd32012-02-27 20:07:01 +00002189 DIEArray method_die_offsets;
2190 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002191 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002192 if (m_apple_objc_ap.get())
2193 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
2194 }
2195 else
2196 {
2197 if (!m_indexed)
2198 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002199
Sean Callanan77a1fd32012-02-27 20:07:01 +00002200 ConstString class_name (class_str.c_str());
2201 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2202 }
2203
2204 if (!method_die_offsets.empty())
2205 {
2206 DWARFDebugInfo* debug_info = DebugInfo();
2207
2208 DWARFCompileUnit* method_cu = NULL;
2209 const size_t num_matches = method_die_offsets.size();
2210 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002211 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002212 const dw_offset_t die_offset = method_die_offsets[i];
2213 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2214
2215 if (method_die)
2216 ResolveType (method_cu, method_die);
2217 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002218 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002219 if (m_using_apple_tables)
2220 {
2221 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2222 die_offset, class_str.c_str());
2223 }
2224 }
2225 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002226 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00002227
2228 for (DelayedPropertyList::const_iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
2229 pi != pe;
2230 ++pi)
2231 pi->Finalize();
Greg Clayton450e3f32010-10-12 02:24:53 +00002232 }
2233 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002234
2235 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2236 // need to tell the clang type it is actually a class.
2237 if (class_language != eLanguageTypeObjC)
2238 {
2239 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2240 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2241 }
2242
2243 // Since DW_TAG_structure_type gets used for both classes
2244 // and structures, we may need to set any DW_TAG_member
2245 // fields to have a "private" access if none was specified.
2246 // When we parsed the child members we tracked that actual
2247 // accessibility value for each DW_TAG_member in the
2248 // "member_accessibilities" array. If the value for the
2249 // member is zero, then it was set to the "default_accessibility"
2250 // which for structs was "public". Below we correct this
2251 // by setting any fields to "private" that weren't correctly
2252 // set.
2253 if (is_a_class && !member_accessibilities.empty())
2254 {
2255 // This is a class and all members that didn't have
2256 // their access specified are private.
2257 ast.SetDefaultAccessForRecordFields (clang_type,
2258 eAccessPrivate,
2259 &member_accessibilities.front(),
2260 member_accessibilities.size());
2261 }
2262
2263 if (!base_classes.empty())
2264 {
2265 ast.SetBaseClassesForClassType (clang_type,
2266 &base_classes.front(),
2267 base_classes.size());
2268
2269 // Clang will copy each CXXBaseSpecifier in "base_classes"
2270 // so we have to free them all.
2271 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2272 base_classes.size());
2273 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002274 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002275 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002276
2277 ast.BuildIndirectFields (clang_type);
2278
Sean Callanan77a1fd32012-02-27 20:07:01 +00002279 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002280
Greg Claytoncaab74e2012-01-28 00:48:57 +00002281 if (!layout_info.field_offsets.empty())
2282 {
2283 if (type)
2284 layout_info.bit_size = type->GetByteSize() * 8;
2285 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002286 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Greg Claytoncaab74e2012-01-28 00:48:57 +00002287 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
2288 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
2289 if (record_type)
2290 {
2291 const clang::RecordDecl *record_decl = record_type->getDecl();
2292
2293 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002294 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002295 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002296 "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 +00002297 clang_type,
2298 record_decl,
2299 layout_info.bit_size,
2300 layout_info.alignment,
2301 (uint32_t)layout_info.field_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002302
Greg Clayton821ac6d2012-01-28 02:22:27 +00002303 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
2304 for (pos = layout_info.field_offsets.begin(); pos != end; ++pos)
2305 {
2306 GetObjectFile()->GetModule()->LogMessage (log.get(),
2307 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field = { bit_offset=%u, name='%s' }",
2308 clang_type,
2309 (uint32_t)pos->second,
2310 pos->first->getNameAsString().c_str());
2311 }
2312 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002313 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2314 }
2315 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002316 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002317
Greg Claytonc93237c2010-10-01 20:48:32 +00002318 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002319
2320 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002321 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002322 if (die->HasChildren())
2323 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002324 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
2325 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002326 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002327 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002328 return clang_type;
2329
2330 default:
2331 assert(false && "not a forward clang type decl!");
2332 break;
2333 }
2334 return NULL;
2335}
2336
Greg Claytonc685f8e2010-09-15 04:15:46 +00002337Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002338SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002339{
2340 if (type_die != NULL)
2341 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002342 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002343
Greg Claytonc685f8e2010-09-15 04:15:46 +00002344 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002345 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002346
Greg Clayton24739922010-10-13 03:15:28 +00002347 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002348 {
2349 if (type != DIE_IS_BEING_PARSED)
2350 return type;
2351
2352 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002353 type_die->GetOffset(),
2354 DW_TAG_value_to_name(type_die->Tag()),
2355 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002356
2357 }
2358 else
2359 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002360 }
2361 return NULL;
2362}
2363
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002364CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002365SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002366{
2367 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002368 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002369 {
2370 // The symbol vendor doesn't know about this compile unit, we
2371 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002372 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002373 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002374 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002375}
2376
2377bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002378SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002379{
2380 sc.Clear();
2381 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002382 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002383
Greg Clayton81c22f62011-10-19 18:09:39 +00002384 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002385 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002386 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002387
2388 if (sc.function)
2389 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002390 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002391 return true;
2392 }
2393
2394 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002395}
2396
2397uint32_t
2398SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2399{
2400 Timer scoped_timer(__PRETTY_FUNCTION__,
2401 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002402 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002403 so_addr.GetOffset(),
2404 resolve_scope);
2405 uint32_t resolved = 0;
2406 if (resolve_scope & ( eSymbolContextCompUnit |
2407 eSymbolContextFunction |
2408 eSymbolContextBlock |
2409 eSymbolContextLineEntry))
2410 {
2411 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2412
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002413 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002414 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002415 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002416 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002417 if (cu_offset != DW_INVALID_OFFSET)
2418 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002419 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002420 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2421 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002422 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002423 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002424 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002425 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002426 resolved |= eSymbolContextCompUnit;
2427
2428 if (resolve_scope & eSymbolContextLineEntry)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002429 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002430 LineTable *line_table = sc.comp_unit->GetLineTable();
2431 if (line_table != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002432 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002433 if (so_addr.IsLinkedAddress())
2434 {
2435 Address linked_addr (so_addr);
2436 linked_addr.ResolveLinkedAddress();
2437 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2438 {
2439 resolved |= eSymbolContextLineEntry;
2440 }
2441 }
2442 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002443 {
2444 resolved |= eSymbolContextLineEntry;
2445 }
2446 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002447 }
2448
2449 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2450 {
2451 DWARFDebugInfoEntry *function_die = NULL;
2452 DWARFDebugInfoEntry *block_die = NULL;
2453 if (resolve_scope & eSymbolContextBlock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002454 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002455 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
2456 }
2457 else
2458 {
2459 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
2460 }
2461
2462 if (function_die != NULL)
2463 {
2464 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2465 if (sc.function == NULL)
2466 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2467 }
2468 else
2469 {
2470 // We might have had a compile unit that had discontiguous
2471 // address ranges where the gaps are symbols that don't have
2472 // any debug info. Discontiguous compile unit address ranges
2473 // should only happen when there aren't other functions from
2474 // other compile units in these gaps. This helps keep the size
2475 // of the aranges down.
2476 sc.comp_unit = NULL;
2477 resolved &= ~eSymbolContextCompUnit;
2478 }
2479
2480 if (sc.function != NULL)
2481 {
2482 resolved |= eSymbolContextFunction;
2483
2484 if (resolve_scope & eSymbolContextBlock)
2485 {
2486 Block& block = sc.function->GetBlock (true);
2487
2488 if (block_die != NULL)
2489 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2490 else
2491 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2492 if (sc.block)
2493 resolved |= eSymbolContextBlock;
2494 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002495 }
2496 }
2497 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002498 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002499 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002500 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
2501 cu_offset,
2502 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002503 }
2504 }
2505 }
2506 }
2507 }
2508 return resolved;
2509}
2510
2511
2512
2513uint32_t
2514SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2515{
2516 const uint32_t prev_size = sc_list.GetSize();
2517 if (resolve_scope & eSymbolContextCompUnit)
2518 {
2519 DWARFDebugInfo* debug_info = DebugInfo();
2520 if (debug_info)
2521 {
2522 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002523 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002524
Greg Clayton53eb1c22012-04-02 22:59:12 +00002525 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002526 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002527 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton1f746072012-08-29 21:13:06 +00002528 const bool full_match = file_spec.GetDirectory();
2529 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 +00002530 if (check_inlines || file_spec_matches_cu_file_spec)
2531 {
2532 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002533 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002534 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002535 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002536 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002537
Greg Clayton526a4ae2012-05-16 22:09:01 +00002538 // If we are looking for inline functions only and we don't
2539 // find it in the support files, we are done.
2540 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002541 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002542 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
2543 if (file_idx == UINT32_MAX)
2544 continue;
2545 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002546
Greg Clayton526a4ae2012-05-16 22:09:01 +00002547 if (line != 0)
2548 {
2549 LineTable *line_table = sc.comp_unit->GetLineTable();
2550
2551 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002552 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002553 // We will have already looked up the file index if
2554 // we are searching for inline entries.
2555 if (!check_inlines)
2556 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002557
Greg Clayton526a4ae2012-05-16 22:09:01 +00002558 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002559 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002560 uint32_t found_line;
2561 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2562 found_line = sc.line_entry.line;
2563
2564 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002565 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002566 sc.function = NULL;
2567 sc.block = NULL;
2568 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002569 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002570 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2571 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002572 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002573 DWARFDebugInfoEntry *function_die = NULL;
2574 DWARFDebugInfoEntry *block_die = NULL;
2575 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002576
Greg Clayton526a4ae2012-05-16 22:09:01 +00002577 if (function_die != NULL)
2578 {
2579 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2580 if (sc.function == NULL)
2581 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2582 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002583
Greg Clayton526a4ae2012-05-16 22:09:01 +00002584 if (sc.function != NULL)
2585 {
2586 Block& block = sc.function->GetBlock (true);
2587
2588 if (block_die != NULL)
2589 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2590 else
2591 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2592 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002593 }
2594 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002595
Greg Clayton526a4ae2012-05-16 22:09:01 +00002596 sc_list.Append(sc);
2597 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2598 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002599 }
2600 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002601 else if (file_spec_matches_cu_file_spec && !check_inlines)
2602 {
2603 // only append the context if we aren't looking for inline call sites
2604 // by file and line and if the file spec matches that of the compile unit
2605 sc_list.Append(sc);
2606 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002607 }
2608 else if (file_spec_matches_cu_file_spec && !check_inlines)
2609 {
2610 // only append the context if we aren't looking for inline call sites
2611 // by file and line and if the file spec matches that of the compile unit
2612 sc_list.Append(sc);
2613 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002614
Greg Clayton526a4ae2012-05-16 22:09:01 +00002615 if (!check_inlines)
2616 break;
2617 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002618 }
2619 }
2620 }
2621 }
2622 return sc_list.GetSize() - prev_size;
2623}
2624
2625void
2626SymbolFileDWARF::Index ()
2627{
2628 if (m_indexed)
2629 return;
2630 m_indexed = true;
2631 Timer scoped_timer (__PRETTY_FUNCTION__,
2632 "SymbolFileDWARF::Index (%s)",
2633 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2634
2635 DWARFDebugInfo* debug_info = DebugInfo();
2636 if (debug_info)
2637 {
2638 uint32_t cu_idx = 0;
2639 const uint32_t num_compile_units = GetNumCompileUnits();
2640 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2641 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002642 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002643
Greg Clayton53eb1c22012-04-02 22:59:12 +00002644 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002645
Greg Clayton53eb1c22012-04-02 22:59:12 +00002646 dwarf_cu->Index (cu_idx,
2647 m_function_basename_index,
2648 m_function_fullname_index,
2649 m_function_method_index,
2650 m_function_selector_index,
2651 m_objc_class_selectors_index,
2652 m_global_index,
2653 m_type_index,
2654 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002655
2656 // Keep memory down by clearing DIEs if this generate function
2657 // caused them to be parsed
2658 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002659 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002660 }
2661
Greg Claytond4a2b372011-09-12 23:21:58 +00002662 m_function_basename_index.Finalize();
2663 m_function_fullname_index.Finalize();
2664 m_function_method_index.Finalize();
2665 m_function_selector_index.Finalize();
2666 m_objc_class_selectors_index.Finalize();
2667 m_global_index.Finalize();
2668 m_type_index.Finalize();
2669 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002670
Greg Clayton24739922010-10-13 03:15:28 +00002671#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002672 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002673 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002674 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2675 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002676 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2677 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2678 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2679 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2680 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2681 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002682 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002683 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002684#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002685 }
2686}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002687
2688bool
2689SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2690{
2691 if (namespace_decl == NULL)
2692 {
2693 // Invalid namespace decl which means we aren't matching only things
2694 // in this symbol file, so return true to indicate it matches this
2695 // symbol file.
2696 return true;
2697 }
2698
2699 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2700
2701 if (namespace_ast == NULL)
2702 return true; // No AST in the "namespace_decl", return true since it
2703 // could then match any symbol file, including this one
2704
2705 if (namespace_ast == GetClangASTContext().getASTContext())
2706 return true; // The ASTs match, return true
2707
2708 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002709 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2710
2711 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002712 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002713
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002714 return false;
2715}
2716
Greg Clayton2506a7a2011-10-12 23:34:26 +00002717bool
2718SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2719 DWARFCompileUnit* cu,
2720 const DWARFDebugInfoEntry* die)
2721{
2722 // No namespace specified, so the answesr i
2723 if (namespace_decl == NULL)
2724 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002725
2726 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002727
Greg Clayton437a1352012-04-09 22:43:43 +00002728 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
2729 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002730 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00002731 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00002732 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00002733
Greg Clayton2506a7a2011-10-12 23:34:26 +00002734 if (clang_namespace_decl)
2735 {
2736 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002737 {
2738 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002739 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002740 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002741 }
2742
Greg Clayton437a1352012-04-09 22:43:43 +00002743 if (clang_namespace_decl == die_clang_decl_ctx)
2744 return true;
2745 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00002746 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00002747 }
2748 else
2749 {
2750 // We have a namespace_decl that was not NULL but it contained
2751 // a NULL "clang::NamespaceDecl", so this means the global namespace
2752 // So as long the the contained decl context DIE isn't a namespace
2753 // we should be ok.
2754 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2755 return true;
2756 }
2757 }
Sean Callananebe60672011-10-13 21:50:33 +00002758
2759 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002760 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002761
Greg Clayton2506a7a2011-10-12 23:34:26 +00002762 return false;
2763}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002764uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002765SymbolFileDWARF::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 +00002766{
Greg Clayton21f2a492011-10-06 00:09:08 +00002767 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2768
2769 if (log)
2770 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002771 GetObjectFile()->GetModule()->LogMessage (log.get(),
2772 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2773 name.GetCString(),
2774 namespace_decl,
2775 append,
2776 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002777 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002778
2779 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2780 return 0;
2781
Greg Claytonc685f8e2010-09-15 04:15:46 +00002782 DWARFDebugInfo* info = DebugInfo();
2783 if (info == NULL)
2784 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002785
2786 // If we aren't appending the results to this list, then clear the list
2787 if (!append)
2788 variables.Clear();
2789
2790 // Remember how many variables are in the list before we search in case
2791 // we are appending the results to a variable list.
2792 const uint32_t original_size = variables.GetSize();
2793
Greg Claytond4a2b372011-09-12 23:21:58 +00002794 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002795
Greg Clayton97fbc342011-10-20 22:30:33 +00002796 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002797 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002798 if (m_apple_names_ap.get())
2799 {
2800 const char *name_cstr = name.GetCString();
2801 const char *base_name_start;
2802 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002803
Greg Clayton97fbc342011-10-20 22:30:33 +00002804 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2805 base_name_start = name_cstr;
2806
2807 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2808 }
Greg Clayton7f995132011-10-04 22:41:51 +00002809 }
2810 else
2811 {
2812 // Index the DWARF if we haven't already
2813 if (!m_indexed)
2814 Index ();
2815
2816 m_global_index.Find (name, die_offsets);
2817 }
2818
Greg Clayton437a1352012-04-09 22:43:43 +00002819 const size_t num_die_matches = die_offsets.size();
2820 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002821 {
Greg Clayton7f995132011-10-04 22:41:51 +00002822 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002823 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002824 assert (sc.module_sp);
2825
Greg Claytond4a2b372011-09-12 23:21:58 +00002826 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002827 DWARFCompileUnit* dwarf_cu = NULL;
2828 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00002829 bool done = false;
2830 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002831 {
2832 const dw_offset_t die_offset = die_offsets[i];
2833 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002834
Greg Clayton95d87902011-11-11 03:16:25 +00002835 if (die)
2836 {
Greg Clayton437a1352012-04-09 22:43:43 +00002837 switch (die->Tag())
2838 {
2839 default:
2840 case DW_TAG_subprogram:
2841 case DW_TAG_inlined_subroutine:
2842 case DW_TAG_try_block:
2843 case DW_TAG_catch_block:
2844 break;
2845
2846 case DW_TAG_variable:
2847 {
2848 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Greg Clayton437a1352012-04-09 22:43:43 +00002849
2850 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2851 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002852
Greg Clayton437a1352012-04-09 22:43:43 +00002853 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002854
Greg Clayton437a1352012-04-09 22:43:43 +00002855 if (variables.GetSize() - original_size >= max_matches)
2856 done = true;
2857 }
2858 break;
2859 }
Greg Clayton95d87902011-11-11 03:16:25 +00002860 }
2861 else
2862 {
2863 if (m_using_apple_tables)
2864 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002865 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2866 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002867 }
2868 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002869 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002870 }
2871
2872 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002873 const uint32_t num_matches = variables.GetSize() - original_size;
2874 if (log && num_matches > 0)
2875 {
2876 GetObjectFile()->GetModule()->LogMessage (log.get(),
2877 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
2878 name.GetCString(),
2879 namespace_decl,
2880 append,
2881 max_matches,
2882 num_matches);
2883 }
2884 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002885}
2886
2887uint32_t
2888SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2889{
Greg Clayton21f2a492011-10-06 00:09:08 +00002890 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2891
2892 if (log)
2893 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002894 GetObjectFile()->GetModule()->LogMessage (log.get(),
2895 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2896 regex.GetText(),
2897 append,
2898 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002899 }
2900
Greg Claytonc685f8e2010-09-15 04:15:46 +00002901 DWARFDebugInfo* info = DebugInfo();
2902 if (info == NULL)
2903 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002904
2905 // If we aren't appending the results to this list, then clear the list
2906 if (!append)
2907 variables.Clear();
2908
2909 // Remember how many variables are in the list before we search in case
2910 // we are appending the results to a variable list.
2911 const uint32_t original_size = variables.GetSize();
2912
Greg Clayton7f995132011-10-04 22:41:51 +00002913 DIEArray die_offsets;
2914
Greg Clayton97fbc342011-10-20 22:30:33 +00002915 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002916 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002917 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002918 {
2919 DWARFMappedHash::DIEInfoArray hash_data_array;
2920 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2921 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2922 }
Greg Clayton7f995132011-10-04 22:41:51 +00002923 }
2924 else
2925 {
2926 // Index the DWARF if we haven't already
2927 if (!m_indexed)
2928 Index ();
2929
2930 m_global_index.Find (regex, die_offsets);
2931 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002932
Greg Claytonc685f8e2010-09-15 04:15:46 +00002933 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002934 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002935 assert (sc.module_sp);
2936
Greg Claytond4a2b372011-09-12 23:21:58 +00002937 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002938 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002939 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002940 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002941 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002942 DWARFDebugInfo* debug_info = DebugInfo();
2943 for (size_t i=0; i<num_matches; ++i)
2944 {
2945 const dw_offset_t die_offset = die_offsets[i];
2946 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002947
2948 if (die)
2949 {
2950 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002951
Greg Clayton95d87902011-11-11 03:16:25 +00002952 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002953
Greg Clayton95d87902011-11-11 03:16:25 +00002954 if (variables.GetSize() - original_size >= max_matches)
2955 break;
2956 }
2957 else
2958 {
2959 if (m_using_apple_tables)
2960 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002961 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2962 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002963 }
2964 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002965 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002966 }
2967
2968 // Return the number of variable that were appended to the list
2969 return variables.GetSize() - original_size;
2970}
2971
Greg Claytonaa044962011-10-13 00:59:38 +00002972
Jim Ingham4cda6e02011-10-07 22:23:45 +00002973bool
2974SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2975 DWARFCompileUnit *&dwarf_cu,
2976 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002977{
Greg Claytonaa044962011-10-13 00:59:38 +00002978 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2979 return ResolveFunction (dwarf_cu, die, sc_list);
2980}
2981
2982
2983bool
2984SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2985 const DWARFDebugInfoEntry *die,
2986 SymbolContextList& sc_list)
2987{
Greg Clayton9e315582011-09-02 04:03:59 +00002988 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002989
2990 if (die == NULL)
2991 return false;
2992
Jim Ingham4cda6e02011-10-07 22:23:45 +00002993 // If we were passed a die that is not a function, just return false...
2994 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2995 return false;
2996
2997 const DWARFDebugInfoEntry* inlined_die = NULL;
2998 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002999 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003000 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00003001
Jim Ingham4cda6e02011-10-07 22:23:45 +00003002 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003003 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003004 if (die->Tag() == DW_TAG_subprogram)
3005 break;
Greg Clayton9e315582011-09-02 04:03:59 +00003006 }
3007 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003008 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00003009 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003010 {
3011 Address addr;
3012 // Parse all blocks if needed
3013 if (inlined_die)
3014 {
Greg Clayton81c22f62011-10-19 18:09:39 +00003015 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00003016 assert (sc.block != NULL);
3017 if (sc.block->GetStartAddress (addr) == false)
3018 addr.Clear();
3019 }
3020 else
3021 {
3022 sc.block = NULL;
3023 addr = sc.function->GetAddressRange().GetBaseAddress();
3024 }
3025
3026 if (addr.IsValid())
3027 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003028 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00003029 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003030 }
3031 }
3032
Greg Claytonaa044962011-10-13 00:59:38 +00003033 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00003034}
3035
Greg Clayton7f995132011-10-04 22:41:51 +00003036void
3037SymbolFileDWARF::FindFunctions (const ConstString &name,
3038 const NameToDIE &name_to_die,
3039 SymbolContextList& sc_list)
3040{
Greg Claytond4a2b372011-09-12 23:21:58 +00003041 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003042 if (name_to_die.Find (name, die_offsets))
3043 {
3044 ParseFunctions (die_offsets, sc_list);
3045 }
3046}
3047
3048
3049void
3050SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3051 const NameToDIE &name_to_die,
3052 SymbolContextList& sc_list)
3053{
3054 DIEArray die_offsets;
3055 if (name_to_die.Find (regex, die_offsets))
3056 {
3057 ParseFunctions (die_offsets, sc_list);
3058 }
3059}
3060
3061
3062void
3063SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3064 const DWARFMappedHash::MemoryTable &memory_table,
3065 SymbolContextList& sc_list)
3066{
3067 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00003068 DWARFMappedHash::DIEInfoArray hash_data_array;
3069 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00003070 {
Greg Claytond1767f02011-12-08 02:13:16 +00003071 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00003072 ParseFunctions (die_offsets, sc_list);
3073 }
3074}
3075
3076void
3077SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
3078 SymbolContextList& sc_list)
3079{
3080 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003081 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00003082 {
Greg Clayton7f995132011-10-04 22:41:51 +00003083 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00003084
3085 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003086 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00003087 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003088 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00003089 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003090 }
3091 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00003092}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003093
Jim Ingham4cda6e02011-10-07 22:23:45 +00003094bool
3095SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
3096 const DWARFCompileUnit *dwarf_cu,
3097 uint32_t name_type_mask,
3098 const char *partial_name,
3099 const char *base_name_start,
3100 const char *base_name_end)
3101{
3102 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
3103 if (name_type_mask == eFunctionNameTypeMethod
3104 || name_type_mask == eFunctionNameTypeBase)
3105 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003106 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
3107 if (!containing_decl_ctx)
3108 return false;
3109
3110 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
3111
3112 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
3113 return false;
3114 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
3115 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003116 }
3117
3118 // Now we need to check whether the name we got back for this type matches the extra specifications
3119 // that were in the name we're looking up:
3120 if (base_name_start != partial_name || *base_name_end != '\0')
3121 {
3122 // First see if the stuff to the left matches the full name. To do that let's see if
3123 // we can pull out the mips linkage name attribute:
3124
3125 Mangled best_name;
3126
3127 DWARFDebugInfoEntry::Attributes attributes;
3128 die->GetAttributes(this, dwarf_cu, NULL, attributes);
3129 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
3130 if (idx != UINT32_MAX)
3131 {
3132 DWARFFormValue form_value;
3133 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3134 {
3135 const char *name = form_value.AsCString(&get_debug_str_data());
Greg Clayton037520e2012-07-18 23:18:10 +00003136 best_name.SetValue (ConstString(name), true);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003137 }
3138 }
3139 if (best_name)
3140 {
3141 const char *demangled = best_name.GetDemangledName().GetCString();
3142 if (demangled)
3143 {
3144 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003145 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3146 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003147 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003148 else
3149 {
3150 // Sort out the case where our name is something like "Process::Destroy" and the match is
3151 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3152 // namespace boundaries...
3153
3154 if (partial_name[0] == ':' && partial_name[1] == ':')
3155 {
3156 // The partial name was already on a namespace boundary so all matches are good.
3157 return true;
3158 }
3159 else if (partial_in_demangled == demangled)
3160 {
3161 // They both start the same, so this is an good match.
3162 return true;
3163 }
3164 else
3165 {
3166 if (partial_in_demangled - demangled == 1)
3167 {
3168 // Only one character difference, can't be a namespace boundary...
3169 return false;
3170 }
3171 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3172 {
3173 // We are on a namespace boundary, so this is also good.
3174 return true;
3175 }
3176 else
3177 return false;
3178 }
3179 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003180 }
3181 }
3182 }
3183
3184 return true;
3185}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003186
Greg Clayton0c5cd902010-06-28 21:30:43 +00003187uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003188SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003189 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003190 uint32_t name_type_mask,
3191 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003192 bool append,
3193 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003194{
3195 Timer scoped_timer (__PRETTY_FUNCTION__,
3196 "SymbolFileDWARF::FindFunctions (name = '%s')",
3197 name.AsCString());
3198
Greg Clayton21f2a492011-10-06 00:09:08 +00003199 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3200
3201 if (log)
3202 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003203 GetObjectFile()->GetModule()->LogMessage (log.get(),
3204 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3205 name.GetCString(),
3206 name_type_mask,
3207 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003208 }
3209
Greg Clayton0c5cd902010-06-28 21:30:43 +00003210 // If we aren't appending the results to this list, then clear the list
3211 if (!append)
3212 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003213
3214 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3215 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003216
3217 // If name is empty then we won't find anything.
3218 if (name.IsEmpty())
3219 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003220
3221 // Remember how many sc_list are in the list before we search in case
3222 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003223
Greg Clayton9e315582011-09-02 04:03:59 +00003224 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00003225
Jim Ingham4cda6e02011-10-07 22:23:45 +00003226 const char *name_cstr = name.GetCString();
3227 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
3228 const char *base_name_start = name_cstr;
3229 const char *base_name_end = name_cstr + strlen(name_cstr);
3230
3231 if (name_type_mask & eFunctionNameTypeAuto)
3232 {
3233 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
3234 effective_name_type_mask = eFunctionNameTypeFull;
3235 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
3236 effective_name_type_mask = eFunctionNameTypeFull;
3237 else
3238 {
3239 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3240 effective_name_type_mask |= eFunctionNameTypeSelector;
3241
3242 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3243 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
3244 }
3245 }
3246 else
3247 {
3248 effective_name_type_mask = name_type_mask;
3249 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
3250 {
3251 // If they've asked for a CPP method or function name and it can't be that, we don't
3252 // even need to search for CPP methods or names.
3253 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3254 {
3255 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
3256 if (effective_name_type_mask == eFunctionNameTypeNone)
3257 return 0;
3258 }
3259 }
3260
3261 if (effective_name_type_mask & eFunctionNameTypeSelector)
3262 {
3263 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3264 {
3265 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
3266 if (effective_name_type_mask == eFunctionNameTypeNone)
3267 return 0;
3268 }
3269 }
3270 }
3271
3272 DWARFDebugInfo* info = DebugInfo();
3273 if (info == NULL)
3274 return 0;
3275
Greg Claytonaa044962011-10-13 00:59:38 +00003276 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003277 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003278 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003279 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003280 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003281
3282 DIEArray die_offsets;
3283
3284 uint32_t num_matches = 0;
3285
3286 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003287 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003288 // If they asked for the full name, match what they typed. At some point we may
3289 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3290 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003291 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003292 for (uint32_t i = 0; i < num_matches; i++)
3293 {
Greg Clayton95d87902011-11-11 03:16:25 +00003294 const dw_offset_t die_offset = die_offsets[i];
3295 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003296 if (die)
3297 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003298 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3299 continue;
3300
Sean Callanan9df05fb2012-02-10 22:52:19 +00003301 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3302 continue;
3303
Greg Claytonaa044962011-10-13 00:59:38 +00003304 ResolveFunction (dwarf_cu, die, sc_list);
3305 }
Greg Clayton95d87902011-11-11 03:16:25 +00003306 else
3307 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003308 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3309 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003310 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003311 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003312 }
3313 else
3314 {
3315 if (effective_name_type_mask & eFunctionNameTypeSelector)
3316 {
3317 if (namespace_decl && *namespace_decl)
3318 return 0; // no selectors in namespaces
3319
3320 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3321 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3322 // and if it is an ObjC method name, we're good.
3323
3324 for (uint32_t i = 0; i < num_matches; i++)
3325 {
Greg Clayton95d87902011-11-11 03:16:25 +00003326 const dw_offset_t die_offset = die_offsets[i];
3327 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003328 if (die)
3329 {
3330 const char *die_name = die->GetName(this, dwarf_cu);
3331 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003332 {
3333 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3334 continue;
3335
Greg Clayton97fbc342011-10-20 22:30:33 +00003336 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003337 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003338 }
Greg Clayton95d87902011-11-11 03:16:25 +00003339 else
3340 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003341 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3342 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003343 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003344 }
3345 die_offsets.clear();
3346 }
3347
3348 if (effective_name_type_mask & eFunctionNameTypeMethod
3349 || effective_name_type_mask & eFunctionNameTypeBase)
3350 {
3351 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3352 (namespace_decl && *namespace_decl))
3353 return 0; // no methods in namespaces
3354
3355 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3356 // extract the base name, look that up, and if there is any other information in the name we were
3357 // passed in we have to post-filter based on that.
3358
3359 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3360 std::string base_name(base_name_start, base_name_end - base_name_start);
3361 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3362
3363 for (uint32_t i = 0; i < num_matches; i++)
3364 {
Greg Clayton95d87902011-11-11 03:16:25 +00003365 const dw_offset_t die_offset = die_offsets[i];
3366 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003367 if (die)
3368 {
3369 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3370 continue;
3371
3372 if (!FunctionDieMatchesPartialName(die,
3373 dwarf_cu,
3374 effective_name_type_mask,
3375 name_cstr,
3376 base_name_start,
3377 base_name_end))
3378 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003379
3380 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3381 continue;
Greg Clayton97fbc342011-10-20 22:30:33 +00003382
3383 // If we get to here, the die is good, and we should add it:
3384 ResolveFunction (dwarf_cu, die, sc_list);
3385 }
Greg Clayton95d87902011-11-11 03:16:25 +00003386 else
3387 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003388 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3389 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003390 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003391 }
3392 die_offsets.clear();
3393 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003394 }
3395 }
Greg Clayton7f995132011-10-04 22:41:51 +00003396 }
3397 else
3398 {
3399
3400 // Index the DWARF if we haven't already
3401 if (!m_indexed)
3402 Index ();
3403
Greg Clayton7f995132011-10-04 22:41:51 +00003404 if (name_type_mask & eFunctionNameTypeFull)
3405 FindFunctions (name, m_function_fullname_index, sc_list);
3406
Jim Ingham4cda6e02011-10-07 22:23:45 +00003407 std::string base_name(base_name_start, base_name_end - base_name_start);
3408 ConstString base_name_const(base_name.c_str());
3409 DIEArray die_offsets;
3410 DWARFCompileUnit *dwarf_cu = NULL;
3411
3412 if (effective_name_type_mask & eFunctionNameTypeBase)
3413 {
3414 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003415 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003416 {
Greg Claytonaa044962011-10-13 00:59:38 +00003417 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3418 if (die)
3419 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003420 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3421 continue;
3422
Greg Claytonaa044962011-10-13 00:59:38 +00003423 if (!FunctionDieMatchesPartialName(die,
3424 dwarf_cu,
3425 effective_name_type_mask,
3426 name_cstr,
3427 base_name_start,
3428 base_name_end))
3429 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003430
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 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003437 }
3438 die_offsets.clear();
3439 }
3440
Jim Inghamea8005a2011-10-11 01:18:11 +00003441 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003442 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003443 if (namespace_decl && *namespace_decl)
3444 return 0; // no methods in namespaces
3445
Jim Ingham4cda6e02011-10-07 22:23:45 +00003446 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3447 {
Greg Claytonaa044962011-10-13 00:59:38 +00003448 for (uint32_t i = 0; i < num_base; i++)
3449 {
3450 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3451 if (die)
3452 {
3453 if (!FunctionDieMatchesPartialName(die,
3454 dwarf_cu,
3455 effective_name_type_mask,
3456 name_cstr,
3457 base_name_start,
3458 base_name_end))
3459 continue;
3460
Sean Callanan9df05fb2012-02-10 22:52:19 +00003461 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3462 continue;
3463
Greg Claytonaa044962011-10-13 00:59:38 +00003464 // If we get to here, the die is good, and we should add it:
3465 ResolveFunction (dwarf_cu, die, sc_list);
3466 }
3467 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003468 }
3469 die_offsets.clear();
3470 }
Greg Clayton7f995132011-10-04 22:41:51 +00003471
Sean Callanan213fdb82011-10-13 01:49:10 +00003472 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003473 {
Greg Clayton7f995132011-10-04 22:41:51 +00003474 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003475 }
3476
Greg Clayton4d01ace2011-09-29 16:58:15 +00003477 }
3478
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003479 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003480 const uint32_t num_matches = sc_list.GetSize() - original_size;
3481
3482 if (log && num_matches > 0)
3483 {
3484 GetObjectFile()->GetModule()->LogMessage (log.get(),
3485 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u",
3486 name.GetCString(),
3487 name_type_mask,
3488 append,
3489 num_matches);
3490 }
3491 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003492}
3493
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003494uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003495SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003496{
3497 Timer scoped_timer (__PRETTY_FUNCTION__,
3498 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3499 regex.GetText());
3500
Greg Clayton21f2a492011-10-06 00:09:08 +00003501 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3502
3503 if (log)
3504 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003505 GetObjectFile()->GetModule()->LogMessage (log.get(),
3506 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3507 regex.GetText(),
3508 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003509 }
3510
3511
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003512 // If we aren't appending the results to this list, then clear the list
3513 if (!append)
3514 sc_list.Clear();
3515
3516 // Remember how many sc_list are in the list before we search in case
3517 // we are appending the results to a variable list.
3518 uint32_t original_size = sc_list.GetSize();
3519
Greg Clayton97fbc342011-10-20 22:30:33 +00003520 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003521 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003522 if (m_apple_names_ap.get())
3523 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003524 }
3525 else
3526 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003527 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003528 if (!m_indexed)
3529 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003530
Greg Clayton7f995132011-10-04 22:41:51 +00003531 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003532
Greg Clayton7f995132011-10-04 22:41:51 +00003533 FindFunctions (regex, m_function_fullname_index, sc_list);
3534 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003535
3536 // Return the number of variable that were appended to the list
3537 return sc_list.GetSize() - original_size;
3538}
Jim Ingham318c9f22011-08-26 19:44:13 +00003539
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003540uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003541SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3542 const ConstString &name,
3543 const lldb_private::ClangNamespaceDecl *namespace_decl,
3544 bool append,
3545 uint32_t max_matches,
3546 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003547{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003548 DWARFDebugInfo* info = DebugInfo();
3549 if (info == NULL)
3550 return 0;
3551
Greg Clayton21f2a492011-10-06 00:09:08 +00003552 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3553
3554 if (log)
3555 {
Greg Clayton437a1352012-04-09 22:43:43 +00003556 if (namespace_decl)
3557 {
3558 GetObjectFile()->GetModule()->LogMessage (log.get(),
3559 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
3560 name.GetCString(),
3561 namespace_decl->GetNamespaceDecl(),
3562 namespace_decl->GetQualifiedName().c_str(),
3563 append,
3564 max_matches);
3565 }
3566 else
3567 {
3568 GetObjectFile()->GetModule()->LogMessage (log.get(),
3569 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
3570 name.GetCString(),
3571 append,
3572 max_matches);
3573 }
Greg Clayton21f2a492011-10-06 00:09:08 +00003574 }
3575
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003576 // If we aren't appending the results to this list, then clear the list
3577 if (!append)
3578 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003579
3580 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3581 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003582
Greg Claytond4a2b372011-09-12 23:21:58 +00003583 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003584
Greg Clayton97fbc342011-10-20 22:30:33 +00003585 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003586 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003587 if (m_apple_types_ap.get())
3588 {
3589 const char *name_cstr = name.GetCString();
3590 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3591 }
Greg Clayton7f995132011-10-04 22:41:51 +00003592 }
3593 else
3594 {
3595 if (!m_indexed)
3596 Index ();
3597
3598 m_type_index.Find (name, die_offsets);
3599 }
3600
Greg Clayton437a1352012-04-09 22:43:43 +00003601 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003602
Greg Clayton437a1352012-04-09 22:43:43 +00003603 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003604 {
Greg Clayton7f995132011-10-04 22:41:51 +00003605 const uint32_t initial_types_size = types.GetSize();
3606 DWARFCompileUnit* dwarf_cu = NULL;
3607 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003608 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003609 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003610 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003611 const dw_offset_t die_offset = die_offsets[i];
3612 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3613
Greg Clayton95d87902011-11-11 03:16:25 +00003614 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003615 {
Greg Clayton95d87902011-11-11 03:16:25 +00003616 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3617 continue;
3618
3619 Type *matching_type = ResolveType (dwarf_cu, die);
3620 if (matching_type)
3621 {
3622 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003623 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003624 if (types.GetSize() >= max_matches)
3625 break;
3626 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003627 }
Greg Clayton95d87902011-11-11 03:16:25 +00003628 else
3629 {
3630 if (m_using_apple_tables)
3631 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003632 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3633 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003634 }
3635 }
3636
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003637 }
Greg Clayton437a1352012-04-09 22:43:43 +00003638 const uint32_t num_matches = types.GetSize() - initial_types_size;
3639 if (log && num_matches)
3640 {
3641 if (namespace_decl)
3642 {
3643 GetObjectFile()->GetModule()->LogMessage (log.get(),
3644 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
3645 name.GetCString(),
3646 namespace_decl->GetNamespaceDecl(),
3647 namespace_decl->GetQualifiedName().c_str(),
3648 append,
3649 max_matches,
3650 num_matches);
3651 }
3652 else
3653 {
3654 GetObjectFile()->GetModule()->LogMessage (log.get(),
3655 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
3656 name.GetCString(),
3657 append,
3658 max_matches,
3659 num_matches);
3660 }
3661 }
3662 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003663 }
Greg Clayton7f995132011-10-04 22:41:51 +00003664 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003665}
3666
3667
Greg Clayton526e5af2010-11-13 03:52:47 +00003668ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003669SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003670 const ConstString &name,
3671 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003672{
Greg Clayton21f2a492011-10-06 00:09:08 +00003673 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3674
3675 if (log)
3676 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003677 GetObjectFile()->GetModule()->LogMessage (log.get(),
3678 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3679 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003680 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003681
3682 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3683 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003684
Greg Clayton526e5af2010-11-13 03:52:47 +00003685 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003686 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003687 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003688 {
Greg Clayton7f995132011-10-04 22:41:51 +00003689 DIEArray die_offsets;
3690
Greg Clayton526e5af2010-11-13 03:52:47 +00003691 // Index if we already haven't to make sure the compile units
3692 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003693 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003694 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003695 if (m_apple_namespaces_ap.get())
3696 {
3697 const char *name_cstr = name.GetCString();
3698 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3699 }
Greg Clayton7f995132011-10-04 22:41:51 +00003700 }
3701 else
3702 {
3703 if (!m_indexed)
3704 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003705
Greg Clayton7f995132011-10-04 22:41:51 +00003706 m_namespace_index.Find (name, die_offsets);
3707 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003708
3709 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003710 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003711 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003712 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003713 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003714 DWARFDebugInfo* debug_info = DebugInfo();
3715 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003716 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003717 const dw_offset_t die_offset = die_offsets[i];
3718 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003719
Greg Clayton95d87902011-11-11 03:16:25 +00003720 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003721 {
Greg Clayton95d87902011-11-11 03:16:25 +00003722 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3723 continue;
3724
3725 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3726 if (clang_namespace_decl)
3727 {
3728 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3729 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003730 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003731 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003732 }
Greg Clayton95d87902011-11-11 03:16:25 +00003733 else
3734 {
3735 if (m_using_apple_tables)
3736 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003737 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3738 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003739 }
3740 }
3741
Greg Clayton526e5af2010-11-13 03:52:47 +00003742 }
3743 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003744 }
Greg Clayton437a1352012-04-09 22:43:43 +00003745 if (log && namespace_decl.GetNamespaceDecl())
3746 {
3747 GetObjectFile()->GetModule()->LogMessage (log.get(),
3748 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
3749 name.GetCString(),
3750 namespace_decl.GetNamespaceDecl(),
3751 namespace_decl.GetQualifiedName().c_str());
3752 }
3753
Greg Clayton526e5af2010-11-13 03:52:47 +00003754 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003755}
3756
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003757uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003758SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003759{
3760 // Remember how many sc_list are in the list before we search in case
3761 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003762 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003763
3764 const uint32_t num_die_offsets = die_offsets.size();
3765 // Parse all of the types we found from the pubtypes matches
3766 uint32_t i;
3767 uint32_t num_matches = 0;
3768 for (i = 0; i < num_die_offsets; ++i)
3769 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003770 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3771 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003772 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003773 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003774 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003775 ++num_matches;
3776 if (num_matches >= max_matches)
3777 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003778 }
3779 }
3780
3781 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003782 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003783}
3784
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003785
3786size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003787SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3788 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003789 DWARFCompileUnit* dwarf_cu,
3790 const DWARFDebugInfoEntry *parent_die,
3791 bool skip_artificial,
3792 bool &is_static,
3793 TypeList* type_list,
3794 std::vector<clang_type_t>& function_param_types,
3795 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003796 unsigned &type_quals,
3797 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003798{
3799 if (parent_die == NULL)
3800 return 0;
3801
Greg Claytond88d7592010-09-15 08:33:30 +00003802 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3803
Greg Clayton7fedea22010-11-16 02:10:54 +00003804 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003805 const DWARFDebugInfoEntry *die;
3806 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3807 {
3808 dw_tag_t tag = die->Tag();
3809 switch (tag)
3810 {
3811 case DW_TAG_formal_parameter:
3812 {
3813 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003814 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003815 if (num_attributes > 0)
3816 {
3817 const char *name = NULL;
3818 Declaration decl;
3819 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003820 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003821 // one of None, Auto, Register, Extern, Static, PrivateExtern
3822
Sean Callanane2ef6e32010-09-23 03:01:22 +00003823 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003824 uint32_t i;
3825 for (i=0; i<num_attributes; ++i)
3826 {
3827 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3828 DWARFFormValue form_value;
3829 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3830 {
3831 switch (attr)
3832 {
3833 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3834 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3835 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3836 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3837 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003838 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003839 case DW_AT_location:
3840 // if (form_value.BlockData())
3841 // {
3842 // const DataExtractor& debug_info_data = debug_info();
3843 // uint32_t block_length = form_value.Unsigned();
3844 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3845 // }
3846 // else
3847 // {
3848 // }
3849 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003850 case DW_AT_const_value:
3851 case DW_AT_default_value:
3852 case DW_AT_description:
3853 case DW_AT_endianity:
3854 case DW_AT_is_optional:
3855 case DW_AT_segment:
3856 case DW_AT_variable_parameter:
3857 default:
3858 case DW_AT_abstract_origin:
3859 case DW_AT_sibling:
3860 break;
3861 }
3862 }
3863 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003864
Greg Clayton0fffff52010-09-24 05:15:53 +00003865 bool skip = false;
3866 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003867 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003868 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003869 {
3870 // In order to determine if a C++ member function is
3871 // "const" we have to look at the const-ness of "this"...
3872 // Ugly, but that
3873 if (arg_idx == 0)
3874 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003875 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003876 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003877 // Often times compilers omit the "this" name for the
3878 // specification DIEs, so we can't rely upon the name
3879 // being in the formal parameter DIE...
3880 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003881 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003882 Type *this_type = ResolveTypeUID (param_type_die_offset);
3883 if (this_type)
3884 {
3885 uint32_t encoding_mask = this_type->GetEncodingMask();
3886 if (encoding_mask & Type::eEncodingIsPointerUID)
3887 {
3888 is_static = false;
3889
3890 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3891 type_quals |= clang::Qualifiers::Const;
3892 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3893 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003894 }
3895 }
3896 }
3897 }
3898 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003899 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003900 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003901 else
3902 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003903
Greg Clayton0fffff52010-09-24 05:15:53 +00003904 // HACK: Objective C formal parameters "self" and "_cmd"
3905 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00003906 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3907 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00003908 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003909 switch (comp_unit->GetLanguage())
3910 {
3911 case eLanguageTypeObjC:
3912 case eLanguageTypeObjC_plus_plus:
3913 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3914 skip = true;
3915 break;
3916 default:
3917 break;
3918 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003919 }
3920 }
3921 }
3922
3923 if (!skip)
3924 {
3925 Type *type = ResolveTypeUID(param_type_die_offset);
3926 if (type)
3927 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003928 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003929
Greg Clayton42ce2f32011-12-12 21:50:19 +00003930 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3931 type->GetClangForwardType(),
3932 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003933 assert(param_var_decl);
3934 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00003935
Jim Ingham379397632012-10-27 02:54:13 +00003936 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00003937 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003938 }
3939 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003940 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003941 }
3942 break;
3943
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003944 case DW_TAG_template_type_parameter:
3945 case DW_TAG_template_value_parameter:
3946 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
3947 break;
3948
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003949 default:
3950 break;
3951 }
3952 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003953 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003954}
3955
3956size_t
3957SymbolFileDWARF::ParseChildEnumerators
3958(
3959 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003960 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003961 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003962 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003963 const DWARFDebugInfoEntry *parent_die
3964)
3965{
3966 if (parent_die == NULL)
3967 return 0;
3968
3969 size_t enumerators_added = 0;
3970 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003971 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3972
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003973 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3974 {
3975 const dw_tag_t tag = die->Tag();
3976 if (tag == DW_TAG_enumerator)
3977 {
3978 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003979 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003980 if (num_child_attributes > 0)
3981 {
3982 const char *name = NULL;
3983 bool got_value = false;
3984 int64_t enum_value = 0;
3985 Declaration decl;
3986
3987 uint32_t i;
3988 for (i=0; i<num_child_attributes; ++i)
3989 {
3990 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3991 DWARFFormValue form_value;
3992 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3993 {
3994 switch (attr)
3995 {
3996 case DW_AT_const_value:
3997 got_value = true;
3998 enum_value = form_value.Unsigned();
3999 break;
4000
4001 case DW_AT_name:
4002 name = form_value.AsCString(&get_debug_str_data());
4003 break;
4004
4005 case DW_AT_description:
4006 default:
4007 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4008 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4009 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4010 case DW_AT_sibling:
4011 break;
4012 }
4013 }
4014 }
4015
4016 if (name && name[0] && got_value)
4017 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004018 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
4019 enumerator_clang_type,
4020 decl,
4021 name,
4022 enum_value,
4023 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004024 ++enumerators_added;
4025 }
4026 }
4027 }
4028 }
4029 return enumerators_added;
4030}
4031
4032void
4033SymbolFileDWARF::ParseChildArrayInfo
4034(
4035 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004036 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004037 const DWARFDebugInfoEntry *parent_die,
4038 int64_t& first_index,
4039 std::vector<uint64_t>& element_orders,
4040 uint32_t& byte_stride,
4041 uint32_t& bit_stride
4042)
4043{
4044 if (parent_die == NULL)
4045 return;
4046
4047 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00004048 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004049 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4050 {
4051 const dw_tag_t tag = die->Tag();
4052 switch (tag)
4053 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004054 case DW_TAG_subrange_type:
4055 {
4056 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004057 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004058 if (num_child_attributes > 0)
4059 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004060 uint64_t num_elements = 0;
4061 uint64_t lower_bound = 0;
4062 uint64_t upper_bound = 0;
4063 uint32_t i;
4064 for (i=0; i<num_child_attributes; ++i)
4065 {
4066 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4067 DWARFFormValue form_value;
4068 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4069 {
4070 switch (attr)
4071 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004072 case DW_AT_name:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004073 break;
4074
4075 case DW_AT_count:
4076 num_elements = form_value.Unsigned();
4077 break;
4078
4079 case DW_AT_bit_stride:
4080 bit_stride = form_value.Unsigned();
4081 break;
4082
4083 case DW_AT_byte_stride:
4084 byte_stride = form_value.Unsigned();
4085 break;
4086
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004087 case DW_AT_lower_bound:
4088 lower_bound = form_value.Unsigned();
4089 break;
4090
4091 case DW_AT_upper_bound:
4092 upper_bound = form_value.Unsigned();
4093 break;
4094
4095 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004096 case DW_AT_abstract_origin:
4097 case DW_AT_accessibility:
4098 case DW_AT_allocated:
4099 case DW_AT_associated:
4100 case DW_AT_data_location:
4101 case DW_AT_declaration:
4102 case DW_AT_description:
4103 case DW_AT_sibling:
4104 case DW_AT_threads_scaled:
4105 case DW_AT_type:
4106 case DW_AT_visibility:
4107 break;
4108 }
4109 }
4110 }
4111
4112 if (upper_bound > lower_bound)
4113 num_elements = upper_bound - lower_bound + 1;
4114
Sean Callananec979ba2012-10-22 23:56:48 +00004115 element_orders.push_back (num_elements);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004116 }
4117 }
4118 break;
4119 }
4120 }
4121}
4122
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004123TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004124SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004125{
4126 TypeSP type_sp;
4127 if (die != NULL)
4128 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004129 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004130 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004131 if (type_ptr == NULL)
4132 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004133 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004134 assert (lldb_cu);
4135 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004136 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004137 }
4138 else if (type_ptr != DIE_IS_BEING_PARSED)
4139 {
4140 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004141 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004142 }
4143
4144 }
4145 return type_sp;
4146}
4147
4148clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004149SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004150{
4151 if (die_offset != DW_INVALID_OFFSET)
4152 {
4153 DWARFCompileUnitSP cu_sp;
4154 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004155 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004156 }
4157 return NULL;
4158}
4159
Sean Callanan72e49402011-08-05 23:43:37 +00004160clang::DeclContext *
4161SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4162{
4163 if (die_offset != DW_INVALID_OFFSET)
4164 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004165 DWARFDebugInfo* debug_info = DebugInfo();
4166 if (debug_info)
4167 {
4168 DWARFCompileUnitSP cu_sp;
4169 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4170 if (die)
4171 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4172 }
Sean Callanan72e49402011-08-05 23:43:37 +00004173 }
4174 return NULL;
4175}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004176
Greg Clayton96d7d742010-11-10 23:42:09 +00004177clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004178SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004179{
Greg Clayton030a2042011-10-14 21:34:45 +00004180 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004181 {
Greg Clayton030a2042011-10-14 21:34:45 +00004182 // See if we already parsed this namespace DIE and associated it with a
4183 // uniqued namespace declaration
4184 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4185 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004186 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004187 else
4188 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004189 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4190 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004191 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
4192 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4193 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004194 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004195 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004196 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004197 GetObjectFile()->GetModule()->LogMessage (log.get(),
4198 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
4199 GetClangASTContext().getASTContext(),
4200 MakeUserID(die->GetOffset()),
4201 namespace_name,
4202 namespace_decl,
4203 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00004204 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004205 else
4206 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004207 GetObjectFile()->GetModule()->LogMessage (log.get(),
4208 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
4209 GetClangASTContext().getASTContext(),
4210 MakeUserID(die->GetOffset()),
4211 namespace_decl,
4212 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00004213 }
Greg Clayton030a2042011-10-14 21:34:45 +00004214 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004215
4216 if (namespace_decl)
4217 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4218 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004219 }
4220 }
4221 return NULL;
4222}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004223
4224clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004225SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004226{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004227 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4228 if (clang_decl_ctx)
4229 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004230 // If this DIE has a specification, or an abstract origin, then trace to those.
4231
Greg Claytoncab36a32011-12-08 05:16:30 +00004232 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004233 if (die_offset != DW_INVALID_OFFSET)
4234 return GetClangDeclContextForDIEOffset (sc, die_offset);
4235
Greg Claytoncab36a32011-12-08 05:16:30 +00004236 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004237 if (die_offset != DW_INVALID_OFFSET)
4238 return GetClangDeclContextForDIEOffset (sc, die_offset);
4239
Greg Clayton3bffb082011-12-10 02:15:28 +00004240 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4241 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004242 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 +00004243 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004244 bool assert_not_being_parsed = true;
4245 ResolveTypeUID (cu, die, assert_not_being_parsed);
4246
Greg Clayton5cf58b92011-10-05 22:22:08 +00004247 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4248
4249 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004250}
4251
4252clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004253SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004254{
Greg Claytonca512b32011-01-14 04:54:56 +00004255 if (m_clang_tu_decl == NULL)
4256 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004257
Greg Clayton2bc22f82011-09-30 03:20:47 +00004258 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004259
4260 if (decl_ctx_die_copy)
4261 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004262
4263 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004264 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004265
Greg Clayton2bc22f82011-09-30 03:20:47 +00004266 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4267 if (pos != m_die_to_decl_ctx.end())
4268 return pos->second;
4269
4270 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004271 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004272 case DW_TAG_compile_unit:
4273 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004274
Greg Clayton2bc22f82011-09-30 03:20:47 +00004275 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004276 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004277 break;
4278
4279 case DW_TAG_structure_type:
4280 case DW_TAG_union_type:
4281 case DW_TAG_class_type:
4282 {
4283 Type* type = ResolveType (cu, decl_ctx_die);
4284 if (type)
4285 {
4286 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4287 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004288 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004289 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4290 if (decl_ctx)
4291 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004292 }
4293 }
Greg Claytonca512b32011-01-14 04:54:56 +00004294 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004295 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004296
Greg Clayton2bc22f82011-09-30 03:20:47 +00004297 default:
4298 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004299 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004300 }
Greg Clayton7a345282010-11-09 23:46:37 +00004301 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004302}
4303
Greg Clayton2bc22f82011-09-30 03:20:47 +00004304
4305const DWARFDebugInfoEntry *
4306SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4307{
4308 if (cu && die)
4309 {
4310 const DWARFDebugInfoEntry * const decl_die = die;
4311
4312 while (die != NULL)
4313 {
4314 // If this is the original DIE that we are searching for a declaration
4315 // for, then don't look in the cache as we don't want our own decl
4316 // context to be our decl context...
4317 if (decl_die != die)
4318 {
4319 switch (die->Tag())
4320 {
4321 case DW_TAG_compile_unit:
4322 case DW_TAG_namespace:
4323 case DW_TAG_structure_type:
4324 case DW_TAG_union_type:
4325 case DW_TAG_class_type:
4326 return die;
4327
4328 default:
4329 break;
4330 }
4331 }
4332
4333 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4334 if (die_offset != DW_INVALID_OFFSET)
4335 {
4336 DWARFCompileUnit *spec_cu = cu;
4337 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4338 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4339 if (spec_die_decl_ctx_die)
4340 return spec_die_decl_ctx_die;
4341 }
4342
4343 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4344 if (die_offset != DW_INVALID_OFFSET)
4345 {
4346 DWARFCompileUnit *abs_cu = cu;
4347 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4348 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4349 if (abs_die_decl_ctx_die)
4350 return abs_die_decl_ctx_die;
4351 }
4352
4353 die = die->GetParent();
4354 }
4355 }
4356 return NULL;
4357}
4358
4359
Greg Clayton901c5ca2011-12-03 04:40:03 +00004360Symbol *
4361SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4362{
4363 Symbol *objc_class_symbol = NULL;
4364 if (m_obj_file)
4365 {
4366 Symtab *symtab = m_obj_file->GetSymtab();
4367 if (symtab)
4368 {
4369 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4370 eSymbolTypeObjCClass,
4371 Symtab::eDebugNo,
4372 Symtab::eVisibilityAny);
4373 }
4374 }
4375 return objc_class_symbol;
4376}
4377
Greg Claytonc7f03b62012-01-12 04:33:28 +00004378// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4379// then we can end up looking through all class types for a complete type and never find
4380// the full definition. We need to know if this attribute is supported, so we determine
4381// this here and cache th result. We also need to worry about the debug map DWARF file
4382// if we are doing darwin DWARF in .o file debugging.
4383bool
4384SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4385{
4386 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4387 {
4388 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4389 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4390 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4391 else
4392 {
4393 DWARFDebugInfo* debug_info = DebugInfo();
4394 const uint32_t num_compile_units = GetNumCompileUnits();
4395 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4396 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004397 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4398 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004399 {
4400 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4401 break;
4402 }
4403 }
4404 }
Greg Clayton1f746072012-08-29 21:13:06 +00004405 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004406 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4407 }
4408 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4409}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004410
4411// This function can be used when a DIE is found that is a forward declaration
4412// DIE and we want to try and find a type that has the complete definition.
4413TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004414SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4415 const ConstString &type_name,
4416 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004417{
4418
4419 TypeSP type_sp;
4420
Greg Claytonc7f03b62012-01-12 04:33:28 +00004421 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004422 return type_sp;
4423
4424 DIEArray die_offsets;
4425
4426 if (m_using_apple_tables)
4427 {
4428 if (m_apple_types_ap.get())
4429 {
4430 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004431 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004432 }
4433 }
4434 else
4435 {
4436 if (!m_indexed)
4437 Index ();
4438
4439 m_type_index.Find (type_name, die_offsets);
4440 }
4441
Greg Clayton901c5ca2011-12-03 04:40:03 +00004442 const size_t num_matches = die_offsets.size();
4443
Greg Clayton901c5ca2011-12-03 04:40:03 +00004444 DWARFCompileUnit* type_cu = NULL;
4445 const DWARFDebugInfoEntry* type_die = NULL;
4446 if (num_matches)
4447 {
4448 DWARFDebugInfo* debug_info = DebugInfo();
4449 for (size_t i=0; i<num_matches; ++i)
4450 {
4451 const dw_offset_t die_offset = die_offsets[i];
4452 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4453
4454 if (type_die)
4455 {
4456 bool try_resolving_type = false;
4457
4458 // Don't try and resolve the DIE we are looking for with the DIE itself!
4459 if (type_die != die)
4460 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004461 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004462 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004463 case DW_TAG_class_type:
4464 case DW_TAG_structure_type:
4465 try_resolving_type = true;
4466 break;
4467 default:
4468 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004469 }
4470 }
4471
4472 if (try_resolving_type)
4473 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004474 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004475 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004476
4477 if (try_resolving_type)
4478 {
4479 Type *resolved_type = ResolveType (type_cu, type_die, false);
4480 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4481 {
4482 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4483 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004484 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton901c5ca2011-12-03 04:40:03 +00004485 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4486 MakeUserID(type_die->GetOffset()),
4487 MakeUserID(type_cu->GetOffset()));
4488
Greg Claytonc7f03b62012-01-12 04:33:28 +00004489 if (die)
4490 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004491 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004492 break;
4493 }
4494 }
4495 }
4496 }
4497 else
4498 {
4499 if (m_using_apple_tables)
4500 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004501 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4502 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004503 }
4504 }
4505
4506 }
4507 }
4508 return type_sp;
4509}
4510
Greg Claytona8022fa2012-04-24 21:22:41 +00004511
Greg Clayton80c26302012-02-05 06:12:47 +00004512//----------------------------------------------------------------------
4513// This function helps to ensure that the declaration contexts match for
4514// two different DIEs. Often times debug information will refer to a
4515// forward declaration of a type (the equivalent of "struct my_struct;".
4516// There will often be a declaration of that type elsewhere that has the
4517// full definition. When we go looking for the full type "my_struct", we
4518// will find one or more matches in the accelerator tables and we will
4519// then need to make sure the type was in the same declaration context
4520// as the original DIE. This function can efficiently compare two DIEs
4521// and will return true when the declaration context matches, and false
4522// when they don't.
4523//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004524bool
4525SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4526 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4527{
Greg Claytona8022fa2012-04-24 21:22:41 +00004528 if (die1 == die2)
4529 return true;
4530
4531#if defined (LLDB_CONFIGURATION_DEBUG)
4532 // You can't and shouldn't call this function with a compile unit from
4533 // two different SymbolFileDWARF instances.
4534 assert (DebugInfo()->ContainsCompileUnit (cu1));
4535 assert (DebugInfo()->ContainsCompileUnit (cu2));
4536#endif
4537
Greg Clayton890ff562012-02-02 05:48:16 +00004538 DWARFDIECollection decl_ctx_1;
4539 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004540 //The declaration DIE stack is a stack of the declaration context
4541 // DIEs all the way back to the compile unit. If a type "T" is
4542 // declared inside a class "B", and class "B" is declared inside
4543 // a class "A" and class "A" is in a namespace "lldb", and the
4544 // namespace is in a compile unit, there will be a stack of DIEs:
4545 //
4546 // [0] DW_TAG_class_type for "B"
4547 // [1] DW_TAG_class_type for "A"
4548 // [2] DW_TAG_namespace for "lldb"
4549 // [3] DW_TAG_compile_unit for the source file.
4550 //
4551 // We grab both contexts and make sure that everything matches
4552 // all the way back to the compiler unit.
4553
4554 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004555 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004556 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004557 // Make sure the context arrays have the same size, otherwise
4558 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004559 const size_t count1 = decl_ctx_1.Size();
4560 const size_t count2 = decl_ctx_2.Size();
4561 if (count1 != count2)
4562 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004563
4564 // Make sure the DW_TAG values match all the way back up the the
4565 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004566 const DWARFDebugInfoEntry *decl_ctx_die1;
4567 const DWARFDebugInfoEntry *decl_ctx_die2;
4568 size_t i;
4569 for (i=0; i<count1; i++)
4570 {
4571 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4572 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4573 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4574 return false;
4575 }
Greg Clayton890ff562012-02-02 05:48:16 +00004576#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004577
4578 // Make sure the top item in the decl context die array is always
4579 // DW_TAG_compile_unit. If it isn't then something went wrong in
4580 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004581 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004582
Greg Clayton890ff562012-02-02 05:48:16 +00004583#endif
4584 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004585 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004586 for (i=0; i<count1 - 1; i++)
4587 {
4588 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4589 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4590 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004591 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004592 // If the string was from a DW_FORM_strp, then the pointer will often
4593 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004594 if (name1 == name2)
4595 continue;
4596
4597 // Name pointers are not equal, so only compare the strings
4598 // if both are not NULL.
4599 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004600 {
Greg Clayton5569e642012-02-06 01:44:54 +00004601 // If the strings don't compare, we are done...
4602 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004603 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004604 }
4605 else
4606 {
4607 // One name was NULL while the other wasn't
4608 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004609 }
4610 }
Greg Clayton80c26302012-02-05 06:12:47 +00004611 // We made it through all of the checks and the declaration contexts
4612 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004613 return true;
4614}
Greg Clayton220a0072011-12-09 08:48:30 +00004615
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004616// This function can be used when a DIE is found that is a forward declaration
4617// DIE and we want to try and find a type that has the complete definition.
Greg Claytona8022fa2012-04-24 21:22:41 +00004618// "cu" and "die" must be from this SymbolFileDWARF
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004619TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004620SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
Greg Clayton7f995132011-10-04 22:41:51 +00004621 const DWARFDebugInfoEntry *die,
4622 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004623{
4624 TypeSP type_sp;
4625
Greg Claytona8022fa2012-04-24 21:22:41 +00004626#if defined (LLDB_CONFIGURATION_DEBUG)
4627 // You can't and shouldn't call this function with a compile unit from
4628 // another SymbolFileDWARF instance.
4629 assert (DebugInfo()->ContainsCompileUnit (cu));
4630#endif
4631
Greg Clayton1a65ae12011-01-25 23:55:37 +00004632 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004633 return type_sp;
4634
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004635 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4636 if (log)
4637 {
4638 std::string qualified_name;
4639 die->GetQualifiedName(this, cu, qualified_name);
4640 GetObjectFile()->GetModule()->LogMessage (log.get(),
4641 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x (%s), name='%s')",
4642 die->GetOffset(),
4643 qualified_name.c_str(),
4644 type_name.GetCString());
4645 }
4646
Greg Clayton7f995132011-10-04 22:41:51 +00004647 DIEArray die_offsets;
4648
Greg Clayton97fbc342011-10-20 22:30:33 +00004649 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004650 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004651 if (m_apple_types_ap.get())
4652 {
Greg Claytond1767f02011-12-08 02:13:16 +00004653 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4654 {
Greg Claytonae920b62012-01-06 00:17:16 +00004655 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004656 }
4657 else
4658 {
4659 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4660 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004661 }
Greg Clayton7f995132011-10-04 22:41:51 +00004662 }
4663 else
4664 {
4665 if (!m_indexed)
4666 Index ();
4667
4668 m_type_index.Find (type_name, die_offsets);
4669 }
Greg Clayton7f995132011-10-04 22:41:51 +00004670
4671 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004672
Greg Clayton934cb052011-12-03 00:27:05 +00004673 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004674
4675 DWARFCompileUnit* type_cu = NULL;
4676 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004677 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004678 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004679 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004680 for (size_t i=0; i<num_matches; ++i)
4681 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004682 const dw_offset_t die_offset = die_offsets[i];
4683 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004684
Greg Clayton95d87902011-11-11 03:16:25 +00004685 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004686 {
Greg Clayton934cb052011-12-03 00:27:05 +00004687 bool try_resolving_type = false;
4688
4689 // Don't try and resolve the DIE we are looking for with the DIE itself!
4690 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004691 {
Greg Clayton934cb052011-12-03 00:27:05 +00004692 const dw_tag_t type_die_tag = type_die->Tag();
4693 // Make sure the tags match
4694 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004695 {
Greg Clayton934cb052011-12-03 00:27:05 +00004696 // The tags match, lets try resolving this type
4697 try_resolving_type = true;
4698 }
4699 else
4700 {
4701 // The tags don't match, but we need to watch our for a
4702 // forward declaration for a struct and ("struct foo")
4703 // ends up being a class ("class foo { ... };") or
4704 // vice versa.
4705 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004706 {
Greg Clayton934cb052011-12-03 00:27:05 +00004707 case DW_TAG_class_type:
4708 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4709 try_resolving_type = (die_tag == DW_TAG_structure_type);
4710 break;
4711 case DW_TAG_structure_type:
4712 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4713 try_resolving_type = (die_tag == DW_TAG_class_type);
4714 break;
4715 default:
4716 // Tags don't match, don't event try to resolve
4717 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004718 break;
4719 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004720 }
4721 }
Greg Clayton934cb052011-12-03 00:27:05 +00004722
4723 if (try_resolving_type)
4724 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004725 if (log)
4726 {
4727 std::string qualified_name;
4728 type_die->GetQualifiedName(this, cu, qualified_name);
4729 GetObjectFile()->GetModule()->LogMessage (log.get(),
4730 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') trying die=0x%8.8x (%s)",
4731 die->GetOffset(),
4732 type_name.GetCString(),
4733 type_die->GetOffset(),
4734 qualified_name.c_str());
4735 }
4736
Greg Clayton890ff562012-02-02 05:48:16 +00004737 // Make sure the decl contexts match all the way up
4738 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004739 {
Greg Clayton890ff562012-02-02 05:48:16 +00004740 Type *resolved_type = ResolveType (type_cu, type_die, false);
4741 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4742 {
4743 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4744 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004745 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton890ff562012-02-02 05:48:16 +00004746 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4747 MakeUserID(type_die->GetOffset()),
4748 MakeUserID(type_cu->GetOffset()));
4749
4750 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004751 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004752 break;
4753 }
Greg Clayton934cb052011-12-03 00:27:05 +00004754 }
4755 }
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004756 else
4757 {
4758 if (log)
4759 {
4760 std::string qualified_name;
4761 type_die->GetQualifiedName(this, cu, qualified_name);
4762 GetObjectFile()->GetModule()->LogMessage (log.get(),
4763 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') ignoring die=0x%8.8x (%s)",
4764 die->GetOffset(),
4765 type_name.GetCString(),
4766 type_die->GetOffset(),
4767 qualified_name.c_str());
4768 }
4769 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004770 }
Greg Clayton95d87902011-11-11 03:16:25 +00004771 else
4772 {
4773 if (m_using_apple_tables)
4774 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004775 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4776 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004777 }
4778 }
4779
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004780 }
4781 }
4782 return type_sp;
4783}
4784
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004785TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004786SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
4787{
4788 TypeSP type_sp;
4789
4790 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
4791 if (dwarf_decl_ctx_count > 0)
4792 {
4793 const ConstString type_name(dwarf_decl_ctx[0].name);
4794 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
4795
4796 if (type_name)
4797 {
4798 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4799 if (log)
4800 {
4801 GetObjectFile()->GetModule()->LogMessage (log.get(),
4802 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
4803 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4804 dwarf_decl_ctx.GetQualifiedName());
4805 }
4806
4807 DIEArray die_offsets;
4808
4809 if (m_using_apple_tables)
4810 {
4811 if (m_apple_types_ap.get())
4812 {
4813 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4814 {
4815 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
4816 }
4817 else
4818 {
4819 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4820 }
4821 }
4822 }
4823 else
4824 {
4825 if (!m_indexed)
4826 Index ();
4827
4828 m_type_index.Find (type_name, die_offsets);
4829 }
4830
4831 const size_t num_matches = die_offsets.size();
4832
4833
4834 DWARFCompileUnit* type_cu = NULL;
4835 const DWARFDebugInfoEntry* type_die = NULL;
4836 if (num_matches)
4837 {
4838 DWARFDebugInfo* debug_info = DebugInfo();
4839 for (size_t i=0; i<num_matches; ++i)
4840 {
4841 const dw_offset_t die_offset = die_offsets[i];
4842 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4843
4844 if (type_die)
4845 {
4846 bool try_resolving_type = false;
4847
4848 // Don't try and resolve the DIE we are looking for with the DIE itself!
4849 const dw_tag_t type_tag = type_die->Tag();
4850 // Make sure the tags match
4851 if (type_tag == tag)
4852 {
4853 // The tags match, lets try resolving this type
4854 try_resolving_type = true;
4855 }
4856 else
4857 {
4858 // The tags don't match, but we need to watch our for a
4859 // forward declaration for a struct and ("struct foo")
4860 // ends up being a class ("class foo { ... };") or
4861 // vice versa.
4862 switch (type_tag)
4863 {
4864 case DW_TAG_class_type:
4865 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4866 try_resolving_type = (tag == DW_TAG_structure_type);
4867 break;
4868 case DW_TAG_structure_type:
4869 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4870 try_resolving_type = (tag == DW_TAG_class_type);
4871 break;
4872 default:
4873 // Tags don't match, don't event try to resolve
4874 // using this type whose name matches....
4875 break;
4876 }
4877 }
4878
4879 if (try_resolving_type)
4880 {
4881 DWARFDeclContext type_dwarf_decl_ctx;
4882 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
4883
4884 if (log)
4885 {
4886 GetObjectFile()->GetModule()->LogMessage (log.get(),
4887 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
4888 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4889 dwarf_decl_ctx.GetQualifiedName(),
4890 type_die->GetOffset(),
4891 type_dwarf_decl_ctx.GetQualifiedName());
4892 }
4893
4894 // Make sure the decl contexts match all the way up
4895 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
4896 {
4897 Type *resolved_type = ResolveType (type_cu, type_die, false);
4898 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4899 {
4900 type_sp = resolved_type->shared_from_this();
4901 break;
4902 }
4903 }
4904 }
4905 else
4906 {
4907 if (log)
4908 {
4909 std::string qualified_name;
4910 type_die->GetQualifiedName(this, type_cu, qualified_name);
4911 GetObjectFile()->GetModule()->LogMessage (log.get(),
4912 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
4913 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4914 dwarf_decl_ctx.GetQualifiedName(),
4915 type_die->GetOffset(),
4916 qualified_name.c_str());
4917 }
4918 }
4919 }
4920 else
4921 {
4922 if (m_using_apple_tables)
4923 {
4924 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4925 die_offset, type_name.GetCString());
4926 }
4927 }
4928
4929 }
4930 }
4931 }
4932 }
4933 return type_sp;
4934}
4935
Greg Clayton4116e932012-05-15 02:33:01 +00004936bool
4937SymbolFileDWARF::CopyUniqueClassMethodTypes (Type *class_type,
4938 DWARFCompileUnit* src_cu,
4939 const DWARFDebugInfoEntry *src_class_die,
4940 DWARFCompileUnit* dst_cu,
4941 const DWARFDebugInfoEntry *dst_class_die)
4942{
4943 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
4944 return false;
4945 if (src_class_die->Tag() != dst_class_die->Tag())
4946 return false;
4947
4948 // We need to complete the class type so we can get all of the method types
4949 // parsed so we can then unique those types to their equivalent counterparts
4950 // in "dst_cu" and "dst_class_die"
4951 class_type->GetClangFullType();
4952
4953 const DWARFDebugInfoEntry *src_die;
4954 const DWARFDebugInfoEntry *dst_die;
4955 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
4956 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
4957 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
4958 {
4959 if (src_die->Tag() == DW_TAG_subprogram)
4960 {
4961 const char *src_name = src_die->GetMangledName (this, src_cu);
4962 if (src_name)
4963 src_name_to_die.Append(ConstString(src_name).GetCString(), src_die);
4964 }
4965 }
4966 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
4967 {
4968 if (dst_die->Tag() == DW_TAG_subprogram)
4969 {
4970 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
4971 if (dst_name)
4972 dst_name_to_die.Append(ConstString(dst_name).GetCString(), dst_die);
4973 }
4974 }
4975 const uint32_t src_size = src_name_to_die.GetSize ();
4976 const uint32_t dst_size = dst_name_to_die.GetSize ();
4977 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
4978
4979 if (src_size && dst_size)
4980 {
Sean Callanan210b8152012-07-07 00:29:33 +00004981 if (src_size != dst_size)
4982 {
4983 if (log)
4984 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)",
4985 src_class_die->GetOffset(),
4986 dst_class_die->GetOffset(),
4987 src_size,
4988 dst_size);
4989
4990 return false;
4991 }
4992
Greg Clayton4116e932012-05-15 02:33:01 +00004993 uint32_t idx;
4994 for (idx = 0; idx < src_size; ++idx)
4995 {
4996 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
4997 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
4998
4999 if (src_die->Tag() != dst_die->Tag())
5000 {
5001 if (log)
5002 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)",
5003 src_class_die->GetOffset(),
5004 dst_class_die->GetOffset(),
5005 src_die->GetOffset(),
5006 DW_TAG_value_to_name(src_die->Tag()),
5007 dst_die->GetOffset(),
5008 DW_TAG_value_to_name(src_die->Tag()));
5009 return false;
5010 }
5011
5012 const char *src_name = src_die->GetMangledName (this, src_cu);
5013 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
5014
5015 // Make sure the names match
5016 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
5017 continue;
5018
5019 if (log)
5020 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)",
5021 src_class_die->GetOffset(),
5022 dst_class_die->GetOffset(),
5023 src_die->GetOffset(),
5024 src_name,
5025 dst_die->GetOffset(),
5026 dst_name);
5027
5028 return false;
5029 }
5030
5031 for (idx = 0; idx < src_size; ++idx)
5032 {
5033 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5034 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
5035
5036 clang::DeclContext *src_decl_ctx = m_die_to_decl_ctx[src_die];
5037 if (src_decl_ctx)
5038 {
5039 if (log)
5040 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x\n", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
5041 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5042 }
5043 else
5044 {
5045 if (log)
5046 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());
5047 }
5048
5049 Type *src_child_type = m_die_to_type[src_die];
5050 if (src_child_type)
5051 {
5052 if (log)
5053 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());
5054 m_die_to_type[dst_die] = src_child_type;
5055 }
5056 else
5057 {
5058 if (log)
5059 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());
5060 }
5061 }
5062 return true;
5063 }
5064 else
5065 {
5066 if (log)
5067 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",
5068 src_class_die->GetOffset(),
5069 dst_class_die->GetOffset(),
5070 src_die->GetOffset(),
5071 src_size,
5072 dst_die->GetOffset(),
5073 dst_size);
5074 }
5075 return false;
5076}
Greg Claytona8022fa2012-04-24 21:22:41 +00005077
5078TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00005079SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005080{
5081 TypeSP type_sp;
5082
Greg Clayton1be10fc2010-09-29 01:12:09 +00005083 if (type_is_new_ptr)
5084 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00005085
5086#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5087 static DIEStack g_die_stack;
5088 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
5089#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00005090
Sean Callananc7fbf732010-08-06 00:32:49 +00005091 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005092 if (die != NULL)
5093 {
Greg Clayton21f2a492011-10-06 00:09:08 +00005094 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005095 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00005096 {
5097 const DWARFDebugInfoEntry *context_die;
5098 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
5099
Greg Clayton1fba87112012-02-09 20:03:49 +00005100 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 +00005101 die->GetOffset(),
5102 context,
5103 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00005104 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00005105 die->GetName(this, dwarf_cu));
5106
5107#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5108 scoped_die_logger.Push (dwarf_cu, die);
5109 g_die_stack.LogDIEs(log.get(), this);
5110#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00005111 }
Greg Clayton3bffb082011-12-10 02:15:28 +00005112//
5113// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
5114// if (log && dwarf_cu)
5115// {
5116// StreamString s;
5117// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005118// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00005119//
5120// }
Jim Ingham16746d12011-08-25 23:21:43 +00005121
Greg Clayton594e5ed2010-09-27 21:07:38 +00005122 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005123 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00005124 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005125 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005126 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005127 if (type_is_new_ptr)
5128 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005129
Greg Clayton594e5ed2010-09-27 21:07:38 +00005130 const dw_tag_t tag = die->Tag();
5131
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005132 bool is_forward_declaration = false;
5133 DWARFDebugInfoEntry::Attributes attributes;
5134 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00005135 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00005136 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
5137 size_t byte_size = 0;
5138 Declaration decl;
5139
Greg Clayton4957bf62010-09-30 21:49:03 +00005140 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005141 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005142
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005143 dw_attr_t attr;
5144
5145 switch (tag)
5146 {
5147 case DW_TAG_base_type:
5148 case DW_TAG_pointer_type:
5149 case DW_TAG_reference_type:
Sean Callanance8af862012-05-21 23:31:51 +00005150 case DW_TAG_rvalue_reference_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005151 case DW_TAG_typedef:
5152 case DW_TAG_const_type:
5153 case DW_TAG_restrict_type:
5154 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00005155 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005156 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005157 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005158 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005159
Greg Claytond88d7592010-09-15 08:33:30 +00005160 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005161 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005162 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
5163
5164 if (num_attributes > 0)
5165 {
5166 uint32_t i;
5167 for (i=0; i<num_attributes; ++i)
5168 {
5169 attr = attributes.AttributeAtIndex(i);
5170 DWARFFormValue form_value;
5171 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5172 {
5173 switch (attr)
5174 {
5175 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5176 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5177 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5178 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00005179
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005180 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00005181 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
5182 // include the "&"...
5183 if (tag == DW_TAG_reference_type)
5184 {
5185 if (strchr (type_name_cstr, '&') == NULL)
5186 type_name_cstr = NULL;
5187 }
5188 if (type_name_cstr)
5189 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005190 break;
Greg Clayton23f59502012-07-17 03:23:13 +00005191 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005192 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
5193 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
5194 default:
5195 case DW_AT_sibling:
5196 break;
5197 }
5198 }
5199 }
5200 }
5201
Greg Clayton81c22f62011-10-19 18:09:39 +00005202 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 +00005203
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005204 switch (tag)
5205 {
5206 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00005207 break;
5208
Greg Claytond4436412011-10-28 21:00:00 +00005209 case DW_TAG_unspecified_type:
5210 if (strcmp(type_name_cstr, "nullptr_t") == 0)
5211 {
5212 resolve_state = Type::eResolveStateFull;
5213 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
5214 break;
5215 }
5216 // Fall through to base type below in case we can handle the type there...
5217
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005218 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00005219 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005220 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
5221 encoding,
5222 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005223 break;
5224
Sean Callanance8af862012-05-21 23:31:51 +00005225 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
5226 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
5227 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
5228 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
5229 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
5230 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
5231 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005232 }
5233
Greg Claytonc7f03b62012-01-12 04:33:28 +00005234 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005235 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005236 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
5237 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005238 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005239 static ConstString g_objc_type_name_id("id");
5240 static ConstString g_objc_type_name_Class("Class");
5241 static ConstString g_objc_type_name_selector("SEL");
5242
5243 if (type_name_const_str == g_objc_type_name_id)
5244 {
5245 if (log)
5246 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
5247 die->GetOffset(),
5248 DW_TAG_value_to_name(die->Tag()),
5249 die->GetName(this, dwarf_cu));
5250 clang_type = ast.GetBuiltInType_objc_id();
5251 encoding_data_type = Type::eEncodingIsUID;
5252 encoding_uid = LLDB_INVALID_UID;
5253 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00005254
Greg Claytonc7f03b62012-01-12 04:33:28 +00005255 }
5256 else if (type_name_const_str == g_objc_type_name_Class)
5257 {
5258 if (log)
5259 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
5260 die->GetOffset(),
5261 DW_TAG_value_to_name(die->Tag()),
5262 die->GetName(this, dwarf_cu));
5263 clang_type = ast.GetBuiltInType_objc_Class();
5264 encoding_data_type = Type::eEncodingIsUID;
5265 encoding_uid = LLDB_INVALID_UID;
5266 resolve_state = Type::eResolveStateFull;
5267 }
5268 else if (type_name_const_str == g_objc_type_name_selector)
5269 {
5270 if (log)
5271 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
5272 die->GetOffset(),
5273 DW_TAG_value_to_name(die->Tag()),
5274 die->GetName(this, dwarf_cu));
5275 clang_type = ast.GetBuiltInType_objc_selector();
5276 encoding_data_type = Type::eEncodingIsUID;
5277 encoding_uid = LLDB_INVALID_UID;
5278 resolve_state = Type::eResolveStateFull;
5279 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005280 }
5281 }
5282
Greg Clayton81c22f62011-10-19 18:09:39 +00005283 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005284 this,
5285 type_name_const_str,
5286 byte_size,
5287 NULL,
5288 encoding_uid,
5289 encoding_data_type,
5290 &decl,
5291 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005292 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005293
Greg Clayton594e5ed2010-09-27 21:07:38 +00005294 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005295
5296// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
5297// if (encoding_type != NULL)
5298// {
5299// if (encoding_type != DIE_IS_BEING_PARSED)
5300// type_sp->SetEncodingType(encoding_type);
5301// else
5302// m_indirect_fixups.push_back(type_sp.get());
5303// }
5304 }
5305 break;
5306
5307 case DW_TAG_structure_type:
5308 case DW_TAG_union_type:
5309 case DW_TAG_class_type:
5310 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005311 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005312 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Greg Clayton23f59502012-07-17 03:23:13 +00005313 bool byte_size_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005314
Greg Clayton9e409562010-07-28 02:04:09 +00005315 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00005316 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005317 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00005318 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005319 if (num_attributes > 0)
5320 {
5321 uint32_t i;
5322 for (i=0; i<num_attributes; ++i)
5323 {
5324 attr = attributes.AttributeAtIndex(i);
5325 DWARFFormValue form_value;
5326 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5327 {
5328 switch (attr)
5329 {
Greg Clayton9e409562010-07-28 02:04:09 +00005330 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00005331 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
5332 {
5333 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
5334 // point to the compile unit file, so we clear this invalid value
5335 // so that we can still unique types efficiently.
5336 decl.SetFile(FileSpec ("<invalid>", false));
5337 }
5338 else
5339 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00005340 break;
5341
5342 case DW_AT_decl_line:
5343 decl.SetLine(form_value.Unsigned());
5344 break;
5345
5346 case DW_AT_decl_column:
5347 decl.SetColumn(form_value.Unsigned());
5348 break;
5349
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005350 case DW_AT_name:
5351 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005352 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005353 break;
Greg Clayton9e409562010-07-28 02:04:09 +00005354
5355 case DW_AT_byte_size:
5356 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00005357 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00005358 break;
5359
5360 case DW_AT_accessibility:
5361 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
5362 break;
5363
5364 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00005365 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00005366 break;
5367
5368 case DW_AT_APPLE_runtime_class:
5369 class_language = (LanguageType)form_value.Signed();
5370 break;
5371
Greg Clayton18774842011-11-29 23:40:34 +00005372 case DW_AT_APPLE_objc_complete_type:
5373 is_complete_objc_class = form_value.Signed();
5374 break;
5375
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005376 case DW_AT_allocated:
5377 case DW_AT_associated:
5378 case DW_AT_data_location:
5379 case DW_AT_description:
5380 case DW_AT_start_scope:
5381 case DW_AT_visibility:
5382 default:
5383 case DW_AT_sibling:
5384 break;
5385 }
5386 }
5387 }
5388 }
5389
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005390 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00005391
Greg Clayton123edca2012-03-02 00:07:15 +00005392 // Only try and unique the type if it has a name.
5393 if (type_name_const_str &&
5394 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00005395 this,
5396 dwarf_cu,
5397 die,
5398 decl,
5399 byte_size_valid ? byte_size : -1,
5400 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005401 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005402 // We have already parsed this type or from another
5403 // compile unit. GCC loves to use the "one definition
5404 // rule" which can result in multiple definitions
5405 // of the same class over and over in each compile
5406 // unit.
5407 type_sp = unique_ast_entry.m_type_sp;
5408 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00005409 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005410 m_die_to_type[die] = type_sp.get();
5411 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005412 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005413 }
5414
Greg Clayton81c22f62011-10-19 18:09:39 +00005415 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 +00005416
5417 int tag_decl_kind = -1;
5418 AccessType default_accessibility = eAccessNone;
5419 if (tag == DW_TAG_structure_type)
5420 {
5421 tag_decl_kind = clang::TTK_Struct;
5422 default_accessibility = eAccessPublic;
5423 }
5424 else if (tag == DW_TAG_union_type)
5425 {
5426 tag_decl_kind = clang::TTK_Union;
5427 default_accessibility = eAccessPublic;
5428 }
5429 else if (tag == DW_TAG_class_type)
5430 {
5431 tag_decl_kind = clang::TTK_Class;
5432 default_accessibility = eAccessPrivate;
5433 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00005434
5435 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
5436 die->HasChildren() == false &&
5437 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
5438 {
5439 // Work around an issue with clang at the moment where
5440 // forward declarations for objective C classes are emitted
5441 // as:
5442 // DW_TAG_structure_type [2]
5443 // DW_AT_name( "ForwardObjcClass" )
5444 // DW_AT_byte_size( 0x00 )
5445 // DW_AT_decl_file( "..." )
5446 // DW_AT_decl_line( 1 )
5447 //
5448 // Note that there is no DW_AT_declaration and there are
5449 // no children, and the byte size is zero.
5450 is_forward_declaration = true;
5451 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005452
Sean Callanan7457f8d2012-04-25 01:03:57 +00005453 if (class_language == eLanguageTypeObjC ||
5454 class_language == eLanguageTypeObjC_plus_plus)
Greg Clayton18774842011-11-29 23:40:34 +00005455 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005456 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00005457 {
5458 // We have a valid eSymbolTypeObjCClass class symbol whose
5459 // name matches the current objective C class that we
5460 // are trying to find and this DIE isn't the complete
5461 // definition (we checked is_complete_objc_class above and
5462 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00005463 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005464
Greg Clayton1f746072012-08-29 21:13:06 +00005465 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton901c5ca2011-12-03 04:40:03 +00005466 {
5467 // We weren't able to find a full declaration in
5468 // this DWARF, see if we have a declaration anywhere
5469 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00005470 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005471 }
5472
5473 if (type_sp)
5474 {
5475 if (log)
5476 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005477 GetObjectFile()->GetModule()->LogMessage (log.get(),
5478 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
5479 this,
5480 die->GetOffset(),
5481 DW_TAG_value_to_name(tag),
5482 type_name_cstr,
5483 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00005484 }
5485
5486 // We found a real definition for this type elsewhere
5487 // so lets use it and cache the fact that we found
5488 // a complete type for this die
5489 m_die_to_type[die] = type_sp.get();
5490 return type_sp;
5491 }
5492 }
5493 }
5494
5495
5496 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005497 {
5498 // We have a forward declaration to a type and we need
5499 // to try and find a full declaration. We look in the
5500 // current type index just in case we have a forward
5501 // declaration followed by an actual declarations in the
5502 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00005503 if (log)
5504 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005505 GetObjectFile()->GetModule()->LogMessage (log.get(),
5506 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
5507 this,
5508 die->GetOffset(),
5509 DW_TAG_value_to_name(tag),
5510 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00005511 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005512
Greg Claytona8022fa2012-04-24 21:22:41 +00005513 DWARFDeclContext die_decl_ctx;
5514 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx);
5515
5516 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
5517 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005518
Greg Clayton1f746072012-08-29 21:13:06 +00005519 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton4272cc72011-02-02 02:24:04 +00005520 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005521 // We weren't able to find a full declaration in
5522 // this DWARF, see if we have a declaration anywhere
5523 // else...
Greg Claytona8022fa2012-04-24 21:22:41 +00005524 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton4272cc72011-02-02 02:24:04 +00005525 }
5526
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005527 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00005528 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00005529 if (log)
5530 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005531 GetObjectFile()->GetModule()->LogMessage (log.get(),
5532 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
5533 this,
5534 die->GetOffset(),
5535 DW_TAG_value_to_name(tag),
5536 type_name_cstr,
5537 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00005538 }
5539
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005540 // We found a real definition for this type elsewhere
5541 // so lets use it and cache the fact that we found
5542 // a complete type for this die
5543 m_die_to_type[die] = type_sp.get();
5544 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005545 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005546 }
5547 assert (tag_decl_kind != -1);
5548 bool clang_type_was_created = false;
5549 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5550 if (clang_type == NULL)
5551 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00005552 const DWARFDebugInfoEntry *decl_ctx_die;
5553
5554 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00005555 if (accessibility == eAccessNone && decl_ctx)
5556 {
5557 // Check the decl context that contains this class/struct/union.
5558 // If it is a class we must give it an accessability.
5559 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
5560 if (DeclKindIsCXXClass (containing_decl_kind))
5561 accessibility = default_accessibility;
5562 }
5563
Greg Claytonf0705c82011-10-22 03:33:13 +00005564 if (type_name_cstr && strchr (type_name_cstr, '<'))
5565 {
5566 ClangASTContext::TemplateParameterInfos template_param_infos;
5567 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
5568 {
5569 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00005570 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00005571 type_name_cstr,
5572 tag_decl_kind,
5573 template_param_infos);
5574
5575 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
5576 class_template_decl,
5577 tag_decl_kind,
5578 template_param_infos);
5579 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
5580 clang_type_was_created = true;
Sean Callanan60217122012-04-13 00:10:03 +00005581
Jim Ingham379397632012-10-27 02:54:13 +00005582 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)class_template_decl, MakeUserID(die->GetOffset()));
5583 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)class_specialization_decl, MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005584 }
5585 }
5586
5587 if (!clang_type_was_created)
5588 {
5589 clang_type_was_created = true;
Jim Ingham379397632012-10-27 02:54:13 +00005590 ClangASTMetadata metadata;
5591 metadata.SetUserID(MakeUserID(die->GetOffset()));
Greg Clayton55561e92011-10-26 03:31:36 +00005592 clang_type = ast.CreateRecordType (decl_ctx,
5593 accessibility,
5594 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00005595 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00005596 class_language,
Jim Ingham379397632012-10-27 02:54:13 +00005597 &metadata);
Greg Claytonf0705c82011-10-22 03:33:13 +00005598 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005599 }
5600
5601 // Store a forward declaration to this class type in case any
5602 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00005603 // types for function prototypes.
5604 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00005605 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005606 this,
5607 type_name_const_str,
5608 byte_size,
5609 NULL,
5610 LLDB_INVALID_UID,
5611 Type::eEncodingIsUID,
5612 &decl,
5613 clang_type,
5614 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00005615
5616 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005617
5618
5619 // Add our type to the unique type map so we don't
5620 // end up creating many copies of the same type over
5621 // and over in the ASTContext for our module
5622 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00005623 unique_ast_entry.m_symfile = this;
5624 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005625 unique_ast_entry.m_die = die;
5626 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00005627 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00005628 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
5629 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005630
Sean Callanan12014a02011-12-08 23:45:45 +00005631 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00005632 {
5633 // Always start the definition for a class type so that
5634 // if the class has child classes or types that require
5635 // the class to be created for use as their decl contexts
5636 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00005637 if (die->HasChildren() == false)
5638 {
5639 // No children for this struct/union/class, lets finish it
5640 ast.StartTagDeclarationDefinition (clang_type);
5641 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan433cd222012-10-19 01:37:25 +00005642
5643 if (tag == DW_TAG_structure_type) // this only applies in C
5644 {
5645 clang::QualType qual_type = clang::QualType::getFromOpaquePtr (clang_type);
5646 const clang::RecordType *record_type = qual_type->getAs<clang::RecordType> ();
5647
5648 if (record_type)
5649 {
5650 clang::RecordDecl *record_decl = record_type->getDecl();
5651
5652 if (record_decl)
5653 {
5654 LayoutInfo layout_info;
5655
5656 layout_info.alignment = 0;
5657 layout_info.bit_size = 0;
5658
5659 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
5660 }
5661 }
5662 }
Sean Callanan12014a02011-12-08 23:45:45 +00005663 }
5664 else if (clang_type_was_created)
5665 {
Greg Clayton219cf312012-03-30 00:51:13 +00005666 // Start the definition if the class is not objective C since
5667 // the underlying decls respond to isCompleteDefinition(). Objective
5668 // C decls dont' respond to isCompleteDefinition() so we can't
5669 // start the declaration definition right away. For C++ classs/union/structs
5670 // we want to start the definition in case the class is needed as the
5671 // declaration context for a contained class or type without the need
5672 // to complete that type..
5673
Sean Callanan7457f8d2012-04-25 01:03:57 +00005674 if (class_language != eLanguageTypeObjC &&
5675 class_language != eLanguageTypeObjC_plus_plus)
Greg Clayton219cf312012-03-30 00:51:13 +00005676 ast.StartTagDeclarationDefinition (clang_type);
5677
Sean Callanan12014a02011-12-08 23:45:45 +00005678 // Leave this as a forward declaration until we need
5679 // to know the details of the type. lldb_private::Type
5680 // will automatically call the SymbolFile virtual function
5681 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
5682 // When the definition needs to be defined.
5683 m_forward_decl_die_to_clang_type[die] = clang_type;
5684 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
5685 ClangASTContext::SetHasExternalStorage (clang_type, true);
5686 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005687 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005688
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005689 }
5690 break;
5691
5692 case DW_TAG_enumeration_type:
5693 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005694 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005695 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005696
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005697 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005698
Greg Claytond88d7592010-09-15 08:33:30 +00005699 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005700 if (num_attributes > 0)
5701 {
5702 uint32_t i;
5703
5704 for (i=0; i<num_attributes; ++i)
5705 {
5706 attr = attributes.AttributeAtIndex(i);
5707 DWARFFormValue form_value;
5708 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5709 {
5710 switch (attr)
5711 {
Greg Clayton7a345282010-11-09 23:46:37 +00005712 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5713 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5714 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005715 case DW_AT_name:
5716 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005717 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005718 break;
Greg Clayton7a345282010-11-09 23:46:37 +00005719 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00005720 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
5721 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5722 case DW_AT_declaration: break; //is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005723 case DW_AT_allocated:
5724 case DW_AT_associated:
5725 case DW_AT_bit_stride:
5726 case DW_AT_byte_stride:
5727 case DW_AT_data_location:
5728 case DW_AT_description:
5729 case DW_AT_start_scope:
5730 case DW_AT_visibility:
5731 case DW_AT_specification:
5732 case DW_AT_abstract_origin:
5733 case DW_AT_sibling:
5734 break;
5735 }
5736 }
5737 }
5738
Greg Clayton81c22f62011-10-19 18:09:39 +00005739 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 +00005740
Greg Clayton1be10fc2010-09-29 01:12:09 +00005741 clang_type_t enumerator_clang_type = NULL;
5742 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5743 if (clang_type == NULL)
5744 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005745 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
5746 DW_ATE_signed,
5747 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00005748 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00005749 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00005750 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005751 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00005752 }
5753 else
5754 {
5755 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
5756 assert (enumerator_clang_type != NULL);
5757 }
5758
Greg Claytona2721472011-06-25 00:44:06 +00005759 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
5760
Greg Clayton81c22f62011-10-19 18:09:39 +00005761 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005762 this,
5763 type_name_const_str,
5764 byte_size,
5765 NULL,
5766 encoding_uid,
5767 Type::eEncodingIsUID,
5768 &decl,
5769 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005770 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005771
Greg Clayton6beaaa62011-01-17 03:46:26 +00005772 ast.StartTagDeclarationDefinition (clang_type);
5773 if (die->HasChildren())
5774 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005775 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
5776 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005777 }
5778 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005779 }
5780 }
5781 break;
5782
Jim Inghamb0be4422010-08-12 01:20:14 +00005783 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005784 case DW_TAG_subprogram:
5785 case DW_TAG_subroutine_type:
5786 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005787 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005788 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005789
Greg Clayton23f59502012-07-17 03:23:13 +00005790 //const char *mangled = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005791 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00005792 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005793 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00005794 bool is_static = false;
5795 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00005796 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00005797 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00005798 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
5799 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Jim Ingham379397632012-10-27 02:54:13 +00005800 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00005801
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005802 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00005803 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005804
5805
Greg Claytond88d7592010-09-15 08:33:30 +00005806 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005807 if (num_attributes > 0)
5808 {
5809 uint32_t i;
5810 for (i=0; i<num_attributes; ++i)
5811 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005812 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005813 DWARFFormValue form_value;
5814 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5815 {
5816 switch (attr)
5817 {
5818 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5819 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5820 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5821 case DW_AT_name:
5822 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005823 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005824 break;
5825
Greg Clayton23f59502012-07-17 03:23:13 +00005826 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&get_debug_str_data()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005827 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005828 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton23f59502012-07-17 03:23:13 +00005829 case DW_AT_declaration: break; // is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00005830 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
5831 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00005832 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00005833 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5834
Greg Claytonf51de672010-10-01 02:31:07 +00005835
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005836 case DW_AT_external:
5837 if (form_value.Unsigned())
5838 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00005839 if (storage == clang::SC_None)
5840 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005841 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00005842 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005843 }
5844 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005845
Greg Clayton72da3972011-08-16 18:40:23 +00005846 case DW_AT_specification:
5847 specification_die_offset = form_value.Reference(dwarf_cu);
5848 break;
5849
5850 case DW_AT_abstract_origin:
5851 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
5852 break;
5853
Jim Ingham379397632012-10-27 02:54:13 +00005854 case DW_AT_object_pointer:
5855 object_pointer_die_offset = form_value.Reference(dwarf_cu);
5856 break;
5857
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005858 case DW_AT_allocated:
5859 case DW_AT_associated:
5860 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005861 case DW_AT_calling_convention:
5862 case DW_AT_data_location:
5863 case DW_AT_elemental:
5864 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005865 case DW_AT_frame_base:
5866 case DW_AT_high_pc:
5867 case DW_AT_low_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005868 case DW_AT_prototyped:
5869 case DW_AT_pure:
5870 case DW_AT_ranges:
5871 case DW_AT_recursive:
5872 case DW_AT_return_addr:
5873 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005874 case DW_AT_start_scope:
5875 case DW_AT_static_link:
5876 case DW_AT_trampoline:
5877 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005878 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005879 case DW_AT_description:
5880 case DW_AT_sibling:
5881 break;
5882 }
5883 }
5884 }
Greg Clayton24739922010-10-13 03:15:28 +00005885 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005886
Jim Ingham379397632012-10-27 02:54:13 +00005887 std::string object_pointer_name;
5888 if (object_pointer_die_offset != DW_INVALID_OFFSET)
5889 {
5890 // Get the name from the object pointer die
5891 StreamString s;
5892 if (DWARFDebugInfoEntry::GetName (this, dwarf_cu, object_pointer_die_offset, s))
5893 {
5894 object_pointer_name.assign(s.GetData());
5895 }
5896 }
5897
Greg Clayton81c22f62011-10-19 18:09:39 +00005898 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 +00005899
Greg Clayton24739922010-10-13 03:15:28 +00005900 clang_type_t return_clang_type = NULL;
5901 Type *func_type = NULL;
5902
5903 if (type_die_offset != DW_INVALID_OFFSET)
5904 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00005905
Greg Clayton24739922010-10-13 03:15:28 +00005906 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00005907 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00005908 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005909 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005910
Greg Claytonf51de672010-10-01 02:31:07 +00005911
Greg Clayton24739922010-10-13 03:15:28 +00005912 std::vector<clang_type_t> function_param_types;
5913 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005914
Greg Clayton24739922010-10-13 03:15:28 +00005915 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00005916
Greg Claytoncb5860a2011-10-13 23:49:28 +00005917 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
5918 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00005919 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
5920
Greg Claytonf0705c82011-10-22 03:33:13 +00005921 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00005922 // Start off static. This will be set to false in ParseChildParameters(...)
5923 // if we find a "this" paramters as the first parameter
5924 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00005925 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005926 ClangASTContext::TemplateParameterInfos template_param_infos;
5927
Greg Clayton24739922010-10-13 03:15:28 +00005928 if (die->HasChildren())
5929 {
Greg Clayton0fffff52010-09-24 05:15:53 +00005930 bool skip_artificial = true;
Jim Ingham379397632012-10-27 02:54:13 +00005931 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00005932 containing_decl_ctx,
Jim Ingham379397632012-10-27 02:54:13 +00005933 dwarf_cu,
5934 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00005935 skip_artificial,
5936 is_static,
Jim Ingham379397632012-10-27 02:54:13 +00005937 type_list,
5938 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00005939 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005940 type_quals,
5941 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00005942 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005943
Greg Clayton24739922010-10-13 03:15:28 +00005944 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005945 clang_type = ast.CreateFunctionType (return_clang_type,
Greg Clayton9cb25c42012-10-30 17:02:18 +00005946 function_param_types.data(),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005947 function_param_types.size(),
5948 is_variadic,
5949 type_quals);
5950
Greg Clayton24739922010-10-13 03:15:28 +00005951 if (type_name_cstr)
5952 {
5953 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00005954 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005955 {
Greg Clayton7c810422012-01-18 23:40:49 +00005956 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00005957 ConstString class_name_no_category;
5958 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00005959 {
Greg Claytone42ae842012-01-19 03:24:53 +00005960 // Use the class name with no category if there is one
5961 if (class_name_no_category)
5962 class_name = class_name_no_category;
5963
Greg Clayton24739922010-10-13 03:15:28 +00005964 SymbolContext empty_sc;
5965 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00005966 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00005967 {
Greg Clayton24739922010-10-13 03:15:28 +00005968 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00005969 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00005970
5971 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00005972 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005973 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
5974 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
5975 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00005976 }
Greg Clayton24739922010-10-13 03:15:28 +00005977 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005978
Greg Clayton24739922010-10-13 03:15:28 +00005979 if (class_opaque_type)
5980 {
5981 // If accessibility isn't set to anything valid, assume public for
5982 // now...
5983 if (accessibility == eAccessNone)
5984 accessibility = eAccessPublic;
5985
Sean Callanan464a5b52012-10-04 22:06:29 +00005986 clang::ObjCMethodDecl *objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
5987 type_name_cstr,
5988 clang_type,
5989 accessibility);
Greg Clayton24739922010-10-13 03:15:28 +00005990 type_handled = objc_method_decl != NULL;
Sean Callanan464a5b52012-10-04 22:06:29 +00005991 if (type_handled)
5992 {
5993 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Jim Ingham379397632012-10-27 02:54:13 +00005994 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)objc_method_decl, MakeUserID(die->GetOffset()));
Sean Callanan464a5b52012-10-04 22:06:29 +00005995 }
Greg Clayton24739922010-10-13 03:15:28 +00005996 }
5997 }
Greg Clayton5113dc82011-08-12 06:47:54 +00005998 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00005999 {
6000 // Look at the parent of this DIE and see if is is
6001 // a class or struct and see if this is actually a
6002 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00006003 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00006004 if (class_type)
6005 {
Greg Clayton4116e932012-05-15 02:33:01 +00006006 if (class_type->GetID() != MakeUserID(decl_ctx_die->GetOffset()))
6007 {
6008 // We uniqued the parent class of this function to another class
6009 // so we now need to associate all dies under "decl_ctx_die" to
6010 // DIEs in the DIE for "class_type"...
6011 DWARFCompileUnitSP class_type_cu_sp;
6012 const DWARFDebugInfoEntry *class_type_die = DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6013 if (class_type_die)
6014 {
6015 if (CopyUniqueClassMethodTypes (class_type,
6016 class_type_cu_sp.get(),
6017 class_type_die,
6018 dwarf_cu,
6019 decl_ctx_die))
6020 {
6021 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00006022 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton4116e932012-05-15 02:33:01 +00006023 {
6024 type_sp = type_ptr->shared_from_this();
6025 break;
6026 }
6027 }
6028 }
6029 }
6030
Greg Clayton72da3972011-08-16 18:40:23 +00006031 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00006032 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006033 // We have a specification which we are going to base our function
6034 // prototype off of, so we need this type to be completed so that the
6035 // m_die_to_decl_ctx for the method in the specification has a valid
6036 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006037 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00006038 // If we have a specification, then the function type should have been
6039 // made with the specification and not with this die.
6040 DWARFCompileUnitSP spec_cu_sp;
6041 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006042 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006043 if (spec_clang_decl_ctx)
6044 {
6045 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
6046 }
6047 else
Jim Inghamc1663042011-09-29 22:12:35 +00006048 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006049 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
6050 MakeUserID(die->GetOffset()),
6051 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006052 }
Greg Clayton72da3972011-08-16 18:40:23 +00006053 type_handled = true;
6054 }
6055 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
6056 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006057 // We have a specification which we are going to base our function
6058 // prototype off of, so we need this type to be completed so that the
6059 // m_die_to_decl_ctx for the method in the abstract origin has a valid
6060 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006061 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00006062
Greg Clayton72da3972011-08-16 18:40:23 +00006063 DWARFCompileUnitSP abs_cu_sp;
6064 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006065 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006066 if (abs_clang_decl_ctx)
6067 {
6068 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
6069 }
6070 else
Jim Inghamc1663042011-09-29 22:12:35 +00006071 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006072 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
6073 MakeUserID(die->GetOffset()),
6074 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006075 }
Greg Clayton72da3972011-08-16 18:40:23 +00006076 type_handled = true;
6077 }
6078 else
6079 {
6080 clang_type_t class_opaque_type = class_type->GetClangForwardType();
6081 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00006082 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006083 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00006084 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006085 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
6086 // in the DWARF for C++ methods... Default to public for now...
6087 if (accessibility == eAccessNone)
6088 accessibility = eAccessPublic;
6089
6090 if (!is_static && !die->HasChildren())
6091 {
6092 // We have a C++ member function with no children (this pointer!)
6093 // and clang will get mad if we try and make a function that isn't
6094 // well formed in the DWARF, so we will just skip it...
6095 type_handled = true;
6096 }
6097 else
6098 {
6099 clang::CXXMethodDecl *cxx_method_decl;
6100 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00006101 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 +00006102 type_name_cstr,
6103 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00006104 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00006105 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
6106 m_obj_file->GetFileSpec().GetFilename().GetCString());
6107
Sean Callananc1b732d2011-11-01 18:07:13 +00006108 const bool is_attr_used = false;
6109
Greg Clayton20568dd2011-10-13 23:13:20 +00006110 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
6111 type_name_cstr,
6112 clang_type,
6113 accessibility,
6114 is_virtual,
6115 is_static,
6116 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00006117 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00006118 is_attr_used,
6119 is_artificial);
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006120
Greg Clayton20568dd2011-10-13 23:13:20 +00006121 type_handled = cxx_method_decl != NULL;
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006122
6123 if (type_handled)
Jim Ingham379397632012-10-27 02:54:13 +00006124 {
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006125 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
6126
6127 Host::SetCrashDescription (NULL);
6128
6129
6130 ClangASTMetadata metadata;
6131 metadata.SetUserID(MakeUserID(die->GetOffset()));
6132
6133 if (!object_pointer_name.empty())
6134 {
6135 metadata.SetObjectPtrName(object_pointer_name.c_str());
6136 if (log)
6137 log->Printf ("Setting object pointer name: %s on method object 0x%ld.\n",
6138 object_pointer_name.c_str(),
6139 (uintptr_t) cxx_method_decl);
6140 }
6141 GetClangASTContext().SetMetadata ((uintptr_t)cxx_method_decl, metadata);
Jim Ingham379397632012-10-27 02:54:13 +00006142 }
Greg Clayton20568dd2011-10-13 23:13:20 +00006143 }
Greg Clayton72da3972011-08-16 18:40:23 +00006144 }
6145 else
6146 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006147 // We were asked to parse the type for a method in a class, yet the
6148 // class hasn't been asked to complete itself through the
6149 // clang::ExternalASTSource protocol, so we need to just have the
6150 // class complete itself and do things the right way, then our
6151 // DIE should then have an entry in the m_die_to_type map. First
6152 // we need to modify the m_die_to_type so it doesn't think we are
6153 // trying to parse this DIE anymore...
6154 m_die_to_type[die] = NULL;
6155
6156 // Now we get the full type to force our class type to complete itself
6157 // using the clang::ExternalASTSource protocol which will parse all
6158 // base classes and all methods (including the method for this DIE).
6159 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006160
Greg Clayton20568dd2011-10-13 23:13:20 +00006161 // The type for this DIE should have been filled in the function call above
6162 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00006163 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton20568dd2011-10-13 23:13:20 +00006164 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006165 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00006166 break;
6167 }
Sean Callanan02eee4d2012-04-12 23:10:00 +00006168
6169 // FIXME This is fixing some even uglier behavior but we really need to
6170 // uniq the methods of each class as well as the class itself.
6171 // <rdar://problem/11240464>
6172 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00006173 }
Greg Clayton931180e2011-01-27 06:44:37 +00006174 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006175 }
6176 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006177 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006178 }
Greg Clayton24739922010-10-13 03:15:28 +00006179
6180 if (!type_handled)
6181 {
6182 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00006183 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
6184 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006185 clang_type,
6186 storage,
6187 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006188
6189// if (template_param_infos.GetSize() > 0)
6190// {
6191// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
6192// function_decl,
6193// type_name_cstr,
6194// template_param_infos);
6195//
6196// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
6197// func_template_decl,
6198// template_param_infos);
6199// }
Greg Clayton24739922010-10-13 03:15:28 +00006200 // Add the decl to our DIE to decl context map
6201 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00006202 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00006203 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006204 ast.SetFunctionParameters (function_decl,
6205 &function_param_decls.front(),
6206 function_param_decls.size());
Sean Callanan60217122012-04-13 00:10:03 +00006207
Jim Ingham379397632012-10-27 02:54:13 +00006208 ClangASTMetadata metadata;
6209 metadata.SetUserID(MakeUserID(die->GetOffset()));
6210
6211 if (!object_pointer_name.empty())
6212 {
6213 metadata.SetObjectPtrName(object_pointer_name.c_str());
Jim Ingham2cffda3f2012-10-30 23:34:07 +00006214 if (log)
6215 log->Printf ("Setting object pointer name: %s on function object 0x%ld.\n",
6216 object_pointer_name.c_str(),
6217 (uintptr_t) function_decl);
Jim Ingham379397632012-10-27 02:54:13 +00006218 }
6219 GetClangASTContext().SetMetadata ((uintptr_t)function_decl, metadata);
Greg Clayton24739922010-10-13 03:15:28 +00006220 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006221 }
Greg Clayton81c22f62011-10-19 18:09:39 +00006222 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006223 this,
6224 type_name_const_str,
6225 0,
6226 NULL,
6227 LLDB_INVALID_UID,
6228 Type::eEncodingIsUID,
6229 &decl,
6230 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006231 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00006232 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006233 }
6234 break;
6235
6236 case DW_TAG_array_type:
6237 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006238 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006239 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006240
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006241 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006242 int64_t first_index = 0;
6243 uint32_t byte_stride = 0;
6244 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00006245 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006246
6247 if (num_attributes > 0)
6248 {
6249 uint32_t i;
6250 for (i=0; i<num_attributes; ++i)
6251 {
6252 attr = attributes.AttributeAtIndex(i);
6253 DWARFFormValue form_value;
6254 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6255 {
6256 switch (attr)
6257 {
6258 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6259 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6260 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6261 case DW_AT_name:
6262 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006263 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006264 break;
6265
6266 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006267 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006268 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
6269 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006270 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
6271 case DW_AT_declaration: break; // is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006272 case DW_AT_allocated:
6273 case DW_AT_associated:
6274 case DW_AT_data_location:
6275 case DW_AT_description:
6276 case DW_AT_ordering:
6277 case DW_AT_start_scope:
6278 case DW_AT_visibility:
6279 case DW_AT_specification:
6280 case DW_AT_abstract_origin:
6281 case DW_AT_sibling:
6282 break;
6283 }
6284 }
6285 }
6286
Greg Clayton81c22f62011-10-19 18:09:39 +00006287 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 +00006288
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006289 Type *element_type = ResolveTypeUID(type_die_offset);
6290
6291 if (element_type)
6292 {
6293 std::vector<uint64_t> element_orders;
6294 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
6295 if (byte_stride == 0 && bit_stride == 0)
6296 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00006297 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006298 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
6299 uint64_t num_elements = 0;
6300 std::vector<uint64_t>::const_reverse_iterator pos;
6301 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
6302 for (pos = element_orders.rbegin(); pos != end; ++pos)
6303 {
6304 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006305 clang_type = ast.CreateArrayType (array_element_type,
6306 num_elements,
6307 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006308 array_element_type = clang_type;
6309 array_element_bit_stride = array_element_bit_stride * num_elements;
6310 }
6311 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00006312 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006313 this,
6314 empty_name,
6315 array_element_bit_stride / 8,
6316 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00006317 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006318 Type::eEncodingIsUID,
6319 &decl,
6320 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006321 Type::eResolveStateFull));
6322 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006323 }
6324 }
6325 }
6326 break;
6327
Greg Clayton9b81a312010-06-12 01:20:30 +00006328 case DW_TAG_ptr_to_member_type:
6329 {
6330 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
6331 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
6332
Greg Claytond88d7592010-09-15 08:33:30 +00006333 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00006334
6335 if (num_attributes > 0) {
6336 uint32_t i;
6337 for (i=0; i<num_attributes; ++i)
6338 {
6339 attr = attributes.AttributeAtIndex(i);
6340 DWARFFormValue form_value;
6341 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6342 {
6343 switch (attr)
6344 {
6345 case DW_AT_type:
6346 type_die_offset = form_value.Reference(dwarf_cu); break;
6347 case DW_AT_containing_type:
6348 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
6349 }
6350 }
6351 }
6352
6353 Type *pointee_type = ResolveTypeUID(type_die_offset);
6354 Type *class_type = ResolveTypeUID(containing_type_die_offset);
6355
Greg Clayton526e5af2010-11-13 03:52:47 +00006356 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
6357 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00006358
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006359 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
6360 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00006361
Greg Clayton526e5af2010-11-13 03:52:47 +00006362 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
6363 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00006364
Greg Clayton81c22f62011-10-19 18:09:39 +00006365 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006366 this,
6367 type_name_const_str,
6368 byte_size,
6369 NULL,
6370 LLDB_INVALID_UID,
6371 Type::eEncodingIsUID,
6372 NULL,
6373 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006374 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00006375 }
6376
6377 break;
6378 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006379 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00006380 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006381 break;
6382 }
6383
6384 if (type_sp.get())
6385 {
6386 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6387 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6388
6389 SymbolContextScope * symbol_context_scope = NULL;
6390 if (sc_parent_tag == DW_TAG_compile_unit)
6391 {
6392 symbol_context_scope = sc.comp_unit;
6393 }
6394 else if (sc.function != NULL)
6395 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006396 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006397 if (symbol_context_scope == NULL)
6398 symbol_context_scope = sc.function;
6399 }
6400
6401 if (symbol_context_scope != NULL)
6402 {
6403 type_sp->SetSymbolContextScope(symbol_context_scope);
6404 }
6405
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006406 // We are ready to put this type into the uniqued list up at the module level
6407 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00006408
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006409 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006410 }
6411 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00006412 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006413 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006414 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006415 }
6416 }
6417 return type_sp;
6418}
6419
6420size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00006421SymbolFileDWARF::ParseTypes
6422(
6423 const SymbolContext& sc,
6424 DWARFCompileUnit* dwarf_cu,
6425 const DWARFDebugInfoEntry *die,
6426 bool parse_siblings,
6427 bool parse_children
6428)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006429{
6430 size_t types_added = 0;
6431 while (die != NULL)
6432 {
6433 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00006434 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006435 {
6436 if (type_is_new)
6437 ++types_added;
6438 }
6439
6440 if (parse_children && die->HasChildren())
6441 {
6442 if (die->Tag() == DW_TAG_subprogram)
6443 {
6444 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00006445 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006446 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
6447 }
6448 else
6449 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
6450 }
6451
6452 if (parse_siblings)
6453 die = die->GetSibling();
6454 else
6455 die = NULL;
6456 }
6457 return types_added;
6458}
6459
6460
6461size_t
6462SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
6463{
6464 assert(sc.comp_unit && sc.function);
6465 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00006466 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006467 if (dwarf_cu)
6468 {
6469 dw_offset_t function_die_offset = sc.function->GetID();
6470 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
6471 if (function_die)
6472 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00006473 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006474 }
6475 }
6476
6477 return functions_added;
6478}
6479
6480
6481size_t
6482SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
6483{
6484 // At least a compile unit must be valid
6485 assert(sc.comp_unit);
6486 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00006487 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006488 if (dwarf_cu)
6489 {
6490 if (sc.function)
6491 {
6492 dw_offset_t function_die_offset = sc.function->GetID();
6493 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
6494 if (func_die && func_die->HasChildren())
6495 {
6496 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
6497 }
6498 }
6499 else
6500 {
6501 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
6502 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
6503 {
6504 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
6505 }
6506 }
6507 }
6508
6509 return types_added;
6510}
6511
6512size_t
6513SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
6514{
6515 if (sc.comp_unit != NULL)
6516 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00006517 DWARFDebugInfo* info = DebugInfo();
6518 if (info == NULL)
6519 return 0;
6520
6521 uint32_t cu_idx = UINT32_MAX;
6522 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006523
6524 if (dwarf_cu == NULL)
6525 return 0;
6526
6527 if (sc.function)
6528 {
6529 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00006530
6531 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 +00006532 if (func_lo_pc != DW_INVALID_ADDRESS)
6533 {
6534 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00006535
Greg Claytone38a5ed2012-01-05 03:57:59 +00006536 // Let all blocks know they have parse all their variables
6537 sc.function->GetBlock (false).SetDidParseVariables (true, true);
6538 return num_variables;
6539 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006540 }
6541 else if (sc.comp_unit)
6542 {
6543 uint32_t vars_added = 0;
6544 VariableListSP variables (sc.comp_unit->GetVariableList(false));
6545
6546 if (variables.get() == NULL)
6547 {
6548 variables.reset(new VariableList());
6549 sc.comp_unit->SetVariableList(variables);
6550
Greg Claytond4a2b372011-09-12 23:21:58 +00006551 DWARFCompileUnit* match_dwarf_cu = NULL;
6552 const DWARFDebugInfoEntry* die = NULL;
6553 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00006554 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00006555 {
Greg Clayton97fbc342011-10-20 22:30:33 +00006556 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00006557 {
6558 DWARFMappedHash::DIEInfoArray hash_data_array;
6559 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
6560 dwarf_cu->GetNextCompileUnitOffset(),
6561 hash_data_array))
6562 {
6563 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
6564 }
6565 }
Greg Clayton7f995132011-10-04 22:41:51 +00006566 }
6567 else
6568 {
6569 // Index if we already haven't to make sure the compile units
6570 // get indexed and make their global DIE index list
6571 if (!m_indexed)
6572 Index ();
6573
6574 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
6575 dwarf_cu->GetNextCompileUnitOffset(),
6576 die_offsets);
6577 }
6578
6579 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00006580 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006581 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006582 DWARFDebugInfo* debug_info = DebugInfo();
6583 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006584 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006585 const dw_offset_t die_offset = die_offsets[i];
6586 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00006587 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00006588 {
Greg Clayton95d87902011-11-11 03:16:25 +00006589 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
6590 if (var_sp)
6591 {
6592 variables->AddVariableIfUnique (var_sp);
6593 ++vars_added;
6594 }
Greg Claytond4a2b372011-09-12 23:21:58 +00006595 }
Greg Clayton95d87902011-11-11 03:16:25 +00006596 else
6597 {
6598 if (m_using_apple_tables)
6599 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006600 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 +00006601 }
6602 }
6603
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006604 }
6605 }
6606 }
6607 return vars_added;
6608 }
6609 }
6610 return 0;
6611}
6612
6613
6614VariableSP
6615SymbolFileDWARF::ParseVariableDIE
6616(
6617 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006618 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006619 const DWARFDebugInfoEntry *die,
6620 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006621)
6622{
6623
Greg Clayton83c5cd92010-11-14 22:13:40 +00006624 VariableSP var_sp (m_die_to_variable_sp[die]);
6625 if (var_sp)
6626 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006627
6628 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00006629
6630 if ((tag == DW_TAG_variable) ||
6631 (tag == DW_TAG_constant) ||
6632 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006633 {
Greg Clayton7f995132011-10-04 22:41:51 +00006634 DWARFDebugInfoEntry::Attributes attributes;
6635 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
6636 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006637 {
Greg Clayton7f995132011-10-04 22:41:51 +00006638 const char *name = NULL;
6639 const char *mangled = NULL;
6640 Declaration decl;
6641 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00006642 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00006643 DWARFExpression location;
6644 bool is_external = false;
6645 bool is_artificial = false;
6646 bool location_is_const_value_data = false;
Greg Clayton23f59502012-07-17 03:23:13 +00006647 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00006648
6649 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006650 {
Greg Clayton7f995132011-10-04 22:41:51 +00006651 dw_attr_t attr = attributes.AttributeAtIndex(i);
6652 DWARFFormValue form_value;
6653 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006654 {
Greg Clayton7f995132011-10-04 22:41:51 +00006655 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006656 {
Greg Clayton7f995132011-10-04 22:41:51 +00006657 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6658 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6659 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6660 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
6661 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00006662 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00006663 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
6664 case DW_AT_const_value:
6665 location_is_const_value_data = true;
6666 // Fall through...
6667 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006668 {
Greg Clayton7f995132011-10-04 22:41:51 +00006669 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006670 {
Greg Clayton7f995132011-10-04 22:41:51 +00006671 const DataExtractor& debug_info_data = get_debug_info_data();
6672
6673 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
6674 uint32_t block_length = form_value.Unsigned();
Greg Clayton7f4c7432012-08-11 00:49:14 +00006675 location.CopyOpcodeData(get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00006676 }
6677 else
6678 {
6679 const DataExtractor& debug_loc_data = get_debug_loc_data();
6680 const dw_offset_t debug_loc_offset = form_value.Unsigned();
6681
6682 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
6683 if (loc_list_length > 0)
6684 {
Greg Clayton7f4c7432012-08-11 00:49:14 +00006685 location.CopyOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00006686 assert (func_low_pc != LLDB_INVALID_ADDRESS);
6687 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
6688 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006689 }
6690 }
Greg Clayton7f995132011-10-04 22:41:51 +00006691 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006692
Greg Clayton7f995132011-10-04 22:41:51 +00006693 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Greg Clayton23f59502012-07-17 03:23:13 +00006694 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00006695 case DW_AT_declaration:
6696 case DW_AT_description:
6697 case DW_AT_endianity:
6698 case DW_AT_segment:
6699 case DW_AT_start_scope:
6700 case DW_AT_visibility:
6701 default:
6702 case DW_AT_abstract_origin:
6703 case DW_AT_sibling:
6704 case DW_AT_specification:
6705 break;
6706 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006707 }
6708 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006709
Greg Clayton7f995132011-10-04 22:41:51 +00006710 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006711 {
Greg Clayton7f995132011-10-04 22:41:51 +00006712 ValueType scope = eValueTypeInvalid;
6713
6714 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6715 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006716 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00006717
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006718 // DWARF doesn't specify if a DW_TAG_variable is a local, global
6719 // or static variable, so we have to do a little digging by
6720 // looking at the location of a varaible to see if it contains
6721 // a DW_OP_addr opcode _somewhere_ in the definition. I say
6722 // somewhere because clang likes to combine small global variables
6723 // into the same symbol and have locations like:
6724 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
6725 // So if we don't have a DW_TAG_formal_parameter, we can look at
6726 // the location to see if it contains a DW_OP_addr opcode, and
6727 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00006728 if (tag == DW_TAG_formal_parameter)
6729 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00006730 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006731 {
Greg Clayton96c09682012-01-04 22:56:43 +00006732 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00006733 // Check if the location has a DW_OP_addr with any address value...
6734 addr_t location_has_op_addr = false;
6735 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00006736 {
6737 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
6738 if (op_error)
6739 {
6740 StreamString strm;
6741 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006742 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 +00006743 }
6744 }
Greg Claytond1767f02011-12-08 02:13:16 +00006745
6746 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006747 {
Greg Claytond1767f02011-12-08 02:13:16 +00006748 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006749 {
Greg Claytond1767f02011-12-08 02:13:16 +00006750 scope = eValueTypeVariableGlobal;
6751
Greg Clayton1f746072012-08-29 21:13:06 +00006752 if (GetDebugMapSymfile ())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006753 {
Greg Claytond1767f02011-12-08 02:13:16 +00006754 // When leaving the DWARF in the .o files on darwin,
6755 // when we have a global variable that wasn't initialized,
6756 // the .o file might not have allocated a virtual
6757 // address for the global variable. In this case it will
6758 // have created a symbol for the global variable
6759 // that is undefined and external and the value will
6760 // be the byte size of the variable. When we do the
6761 // address map in SymbolFileDWARFDebugMap we rely on
6762 // having an address, we need to do some magic here
6763 // so we can get the correct address for our global
6764 // variable. The address for all of these entries
6765 // will be zero, and there will be an undefined symbol
6766 // in this object file, and the executable will have
6767 // a matching symbol with a good address. So here we
6768 // dig up the correct address and replace it in the
6769 // location for the variable, and set the variable's
6770 // symbol context scope to be that of the main executable
6771 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00006772 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006773 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006774
Greg Claytond1767f02011-12-08 02:13:16 +00006775 // we have a possible uninitialized extern global
6776 Symtab *symtab = m_obj_file->GetSymtab();
6777 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006778 {
Greg Claytond1767f02011-12-08 02:13:16 +00006779 ConstString const_name(name);
6780 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
6781 eSymbolTypeUndefined,
6782 Symtab::eDebugNo,
6783 Symtab::eVisibilityExtern);
6784
6785 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006786 {
Greg Claytond1767f02011-12-08 02:13:16 +00006787 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
6788 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006789 {
Greg Claytond1767f02011-12-08 02:13:16 +00006790 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
6791 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
6792 eSymbolTypeData,
6793 Symtab::eDebugYes,
6794 Symtab::eVisibilityExtern);
6795 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006796 {
Greg Claytone7612132012-03-07 21:03:09 +00006797 if (defined_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006798 {
Greg Claytone7612132012-03-07 21:03:09 +00006799 const addr_t defined_addr = defined_symbol->GetAddress().GetFileAddress();
Greg Claytond1767f02011-12-08 02:13:16 +00006800 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006801 {
Greg Claytond1767f02011-12-08 02:13:16 +00006802 if (location.Update_DW_OP_addr (defined_addr))
6803 {
6804 symbol_context_scope = defined_symbol;
6805 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006806 }
6807 }
6808 }
6809 }
6810 }
6811 }
6812 }
6813 }
6814 }
Greg Claytond1767f02011-12-08 02:13:16 +00006815 else
6816 {
6817 scope = eValueTypeVariableStatic;
6818 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006819 }
6820 else
Greg Claytond1767f02011-12-08 02:13:16 +00006821 {
6822 scope = eValueTypeVariableLocal;
6823 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006824 }
Greg Clayton7f995132011-10-04 22:41:51 +00006825
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006826 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00006827 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006828 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00006829 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006830 case DW_TAG_subprogram:
6831 case DW_TAG_inlined_subroutine:
6832 case DW_TAG_lexical_block:
6833 if (sc.function)
6834 {
6835 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
6836 if (symbol_context_scope == NULL)
6837 symbol_context_scope = sc.function;
6838 }
6839 break;
6840
6841 default:
6842 symbol_context_scope = sc.comp_unit;
6843 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00006844 }
Greg Clayton7f995132011-10-04 22:41:51 +00006845 }
6846
Greg Clayton5cf58b92011-10-05 22:22:08 +00006847 if (symbol_context_scope)
6848 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006849 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
6850 name,
6851 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00006852 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00006853 scope,
6854 symbol_context_scope,
6855 &decl,
6856 location,
6857 is_external,
6858 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00006859
6860 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
6861 }
6862 else
6863 {
6864 // Not ready to parse this variable yet. It might be a global
6865 // or static variable that is in a function scope and the function
6866 // in the symbol context wasn't filled in yet
6867 return var_sp;
6868 }
Greg Clayton7f995132011-10-04 22:41:51 +00006869 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006870 }
Greg Clayton7f995132011-10-04 22:41:51 +00006871 // Cache var_sp even if NULL (the variable was just a specification or
6872 // was missing vital information to be able to be displayed in the debugger
6873 // (missing location due to optimization, etc)) so we don't re-parse
6874 // this DIE over and over later...
6875 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006876 }
6877 return var_sp;
6878}
6879
Greg Claytonc662ec82011-06-17 22:10:16 +00006880
6881const DWARFDebugInfoEntry *
6882SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
6883 dw_offset_t spec_block_die_offset,
6884 DWARFCompileUnit **result_die_cu_handle)
6885{
6886 // Give the concrete function die specified by "func_die_offset", find the
6887 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6888 // to "spec_block_die_offset"
6889 DWARFDebugInfo* info = DebugInfo();
6890
6891 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
6892 if (die)
6893 {
6894 assert (*result_die_cu_handle);
6895 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
6896 }
6897 return NULL;
6898}
6899
6900
6901const DWARFDebugInfoEntry *
6902SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
6903 const DWARFDebugInfoEntry *die,
6904 dw_offset_t spec_block_die_offset,
6905 DWARFCompileUnit **result_die_cu_handle)
6906{
6907 if (die)
6908 {
6909 switch (die->Tag())
6910 {
6911 case DW_TAG_subprogram:
6912 case DW_TAG_inlined_subroutine:
6913 case DW_TAG_lexical_block:
6914 {
6915 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
6916 {
6917 *result_die_cu_handle = dwarf_cu;
6918 return die;
6919 }
6920
6921 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
6922 {
6923 *result_die_cu_handle = dwarf_cu;
6924 return die;
6925 }
6926 }
6927 break;
6928 }
6929
6930 // Give the concrete function die specified by "func_die_offset", find the
6931 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6932 // to "spec_block_die_offset"
6933 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
6934 {
6935 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
6936 child_die,
6937 spec_block_die_offset,
6938 result_die_cu_handle);
6939 if (result_die)
6940 return result_die;
6941 }
6942 }
6943
6944 *result_die_cu_handle = NULL;
6945 return NULL;
6946}
6947
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006948size_t
6949SymbolFileDWARF::ParseVariables
6950(
6951 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006952 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006953 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006954 const DWARFDebugInfoEntry *orig_die,
6955 bool parse_siblings,
6956 bool parse_children,
6957 VariableList* cc_variable_list
6958)
6959{
6960 if (orig_die == NULL)
6961 return 0;
6962
Greg Claytonc662ec82011-06-17 22:10:16 +00006963 VariableListSP variable_list_sp;
6964
6965 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006966 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00006967 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006968 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006969 dw_tag_t tag = die->Tag();
6970
6971 // Check to see if we have already parsed this variable or constant?
6972 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006973 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006974 if (cc_variable_list)
6975 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006976 }
6977 else
6978 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006979 // We haven't already parsed it, lets do that now.
6980 if ((tag == DW_TAG_variable) ||
6981 (tag == DW_TAG_constant) ||
6982 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006983 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006984 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00006985 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006986 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
6987 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6988 switch (parent_tag)
6989 {
6990 case DW_TAG_compile_unit:
6991 if (sc.comp_unit != NULL)
6992 {
6993 variable_list_sp = sc.comp_unit->GetVariableList(false);
6994 if (variable_list_sp.get() == NULL)
6995 {
6996 variable_list_sp.reset(new VariableList());
6997 sc.comp_unit->SetVariableList(variable_list_sp);
6998 }
6999 }
7000 else
7001 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00007002 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
7003 MakeUserID(sc_parent_die->GetOffset()),
7004 DW_TAG_value_to_name (parent_tag),
7005 MakeUserID(orig_die->GetOffset()),
7006 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007007 }
7008 break;
7009
7010 case DW_TAG_subprogram:
7011 case DW_TAG_inlined_subroutine:
7012 case DW_TAG_lexical_block:
7013 if (sc.function != NULL)
7014 {
7015 // Check to see if we already have parsed the variables for the given scope
7016
Greg Clayton81c22f62011-10-19 18:09:39 +00007017 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007018 if (block == NULL)
7019 {
7020 // This must be a specification or abstract origin with
7021 // a concrete block couterpart in the current function. We need
7022 // to find the concrete block so we can correctly add the
7023 // variable to it
7024 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
7025 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
7026 sc_parent_die->GetOffset(),
7027 &concrete_block_die_cu);
7028 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00007029 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007030 }
7031
7032 if (block != NULL)
7033 {
7034 const bool can_create = false;
7035 variable_list_sp = block->GetBlockVariableList (can_create);
7036 if (variable_list_sp.get() == NULL)
7037 {
7038 variable_list_sp.reset(new VariableList());
7039 block->SetVariableList(variable_list_sp);
7040 }
7041 }
7042 }
7043 break;
7044
7045 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00007046 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
7047 MakeUserID(orig_die->GetOffset()),
7048 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007049 break;
7050 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00007051 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007052
7053 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007054 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00007055 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
7056 if (var_sp)
7057 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007058 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00007059 if (cc_variable_list)
7060 cc_variable_list->AddVariableIfUnique (var_sp);
7061 ++vars_added;
7062 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007063 }
7064 }
7065 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007066
7067 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
7068
7069 if (!skip_children && parse_children && die->HasChildren())
7070 {
7071 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
7072 }
7073
7074 if (parse_siblings)
7075 die = die->GetSibling();
7076 else
7077 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007078 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007079 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007080}
7081
7082//------------------------------------------------------------------
7083// PluginInterface protocol
7084//------------------------------------------------------------------
7085const char *
7086SymbolFileDWARF::GetPluginName()
7087{
7088 return "SymbolFileDWARF";
7089}
7090
7091const char *
7092SymbolFileDWARF::GetShortPluginName()
7093{
7094 return GetPluginNameStatic();
7095}
7096
7097uint32_t
7098SymbolFileDWARF::GetPluginVersion()
7099{
7100 return 1;
7101}
7102
7103void
Greg Clayton6beaaa62011-01-17 03:46:26 +00007104SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
7105{
7106 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7107 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7108 if (clang_type)
7109 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7110}
7111
7112void
7113SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
7114{
7115 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7116 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7117 if (clang_type)
7118 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7119}
7120
Greg Claytona2721472011-06-25 00:44:06 +00007121void
Sean Callanancc427fa2011-07-30 02:42:06 +00007122SymbolFileDWARF::DumpIndexes ()
7123{
7124 StreamFile s(stdout, false);
7125
7126 s.Printf ("DWARF index for (%s) '%s/%s':",
7127 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
7128 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
7129 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
7130 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
7131 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
7132 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
7133 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
7134 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
7135 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
7136 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
7137 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
7138}
7139
7140void
7141SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
7142 const char *name,
7143 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00007144{
Sean Callanancc427fa2011-07-30 02:42:06 +00007145 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00007146
7147 if (iter == m_decl_ctx_to_die.end())
7148 return;
7149
Greg Claytoncb5860a2011-10-13 23:49:28 +00007150 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00007151 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007152 const DWARFDebugInfoEntry *context_die = *pos;
7153
7154 if (!results)
7155 return;
7156
7157 DWARFDebugInfo* info = DebugInfo();
7158
7159 DIEArray die_offsets;
7160
7161 DWARFCompileUnit* dwarf_cu = NULL;
7162 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00007163
7164 if (m_using_apple_tables)
7165 {
7166 if (m_apple_types_ap.get())
7167 m_apple_types_ap->FindByName (name, die_offsets);
7168 }
7169 else
7170 {
7171 if (!m_indexed)
7172 Index ();
7173
7174 m_type_index.Find (ConstString(name), die_offsets);
7175 }
7176
Greg Clayton220a0072011-12-09 08:48:30 +00007177 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007178
7179 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00007180 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007181 for (size_t i = 0; i < num_matches; ++i)
7182 {
7183 const dw_offset_t die_offset = die_offsets[i];
7184 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00007185
Greg Claytoncb5860a2011-10-13 23:49:28 +00007186 if (die->GetParent() != context_die)
7187 continue;
7188
7189 Type *matching_type = ResolveType (dwarf_cu, die);
7190
Greg Clayton42ce2f32011-12-12 21:50:19 +00007191 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007192 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
7193
7194 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
7195 {
7196 clang::TagDecl *tag_decl = tag_type->getDecl();
7197 results->push_back(tag_decl);
7198 }
7199 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
7200 {
7201 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
7202 results->push_back(typedef_decl);
7203 }
Greg Claytona2721472011-06-25 00:44:06 +00007204 }
7205 }
7206 }
7207}
7208
7209void
7210SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00007211 const clang::DeclContext *decl_context,
7212 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00007213 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
7214{
Greg Clayton85ae2e12011-10-18 23:36:41 +00007215
7216 switch (decl_context->getDeclKind())
7217 {
7218 case clang::Decl::Namespace:
7219 case clang::Decl::TranslationUnit:
7220 {
7221 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7222 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
7223 }
7224 break;
7225 default:
7226 break;
7227 }
Greg Claytona2721472011-06-25 00:44:06 +00007228}
Greg Claytoncaab74e2012-01-28 00:48:57 +00007229
7230bool
7231SymbolFileDWARF::LayoutRecordType (void *baton,
7232 const clang::RecordDecl *record_decl,
7233 uint64_t &size,
7234 uint64_t &alignment,
7235 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7236 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7237 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7238{
7239 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7240 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
7241}
7242
7243
7244bool
7245SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
7246 uint64_t &bit_size,
7247 uint64_t &alignment,
7248 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7249 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7250 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7251{
7252 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
7253 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
7254 bool success = false;
7255 base_offsets.clear();
7256 vbase_offsets.clear();
7257 if (pos != m_record_decl_to_layout_map.end())
7258 {
7259 bit_size = pos->second.bit_size;
7260 alignment = pos->second.alignment;
7261 field_offsets.swap(pos->second.field_offsets);
7262 m_record_decl_to_layout_map.erase(pos);
7263 success = true;
7264 }
7265 else
7266 {
7267 bit_size = 0;
7268 alignment = 0;
7269 field_offsets.clear();
7270 }
7271
7272 if (log)
7273 GetObjectFile()->GetModule()->LogMessage (log.get(),
7274 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
7275 record_decl,
7276 bit_size,
7277 alignment,
7278 (uint32_t)field_offsets.size(),
7279 (uint32_t)base_offsets.size(),
7280 (uint32_t)vbase_offsets.size(),
7281 success);
7282 return success;
7283}
7284
7285
Greg Clayton1f746072012-08-29 21:13:06 +00007286SymbolFileDWARFDebugMap *
7287SymbolFileDWARF::GetDebugMapSymfile ()
7288{
7289 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
7290 {
7291 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
7292 if (module_sp)
7293 {
7294 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
7295 if (sym_vendor)
7296 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
7297 }
7298 }
7299 return m_debug_map_symfile;
7300}
7301
Greg Claytoncaab74e2012-01-28 00:48:57 +00007302