blob: ab5163bf85b9946c88dfb9ca3334824210393553 [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();
Daniel Malead01b2952012-11-29 21:49:15 +0000129 log_strm.Printf("DIEStack[%" PRIu64 "]:\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);
Daniel Malead01b2952012-11-29 21:49:15 +0000136 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n",
Greg Clayton43e0af02012-09-18 18:04:04 +0000137 (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
Greg Clayton9422dd62013-03-04 21:46:16 +0000894 if (FixupAddress (func_range.GetBaseAddress()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895 {
Greg Clayton9422dd62013-03-04 21:46:16 +0000896 const user_id_t func_user_id = MakeUserID(die->GetOffset());
897 func_sp.reset(new Function (sc.comp_unit,
898 MakeUserID(func_user_id), // UserID is the DIE offset
899 MakeUserID(func_user_id),
900 func_name,
901 func_type,
902 func_range)); // first address range
903
904 if (func_sp.get() != NULL)
905 {
906 if (frame_base.IsValid())
907 func_sp->GetFrameBaseExpression() = frame_base;
908 sc.comp_unit->AddFunction(func_sp);
909 return func_sp.get();
910 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000911 }
912 }
913 }
914 return NULL;
915}
916
Greg Clayton9422dd62013-03-04 21:46:16 +0000917bool
918SymbolFileDWARF::FixupAddress (Address &addr)
919{
920 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
921 if (debug_map_symfile)
922 {
923 return debug_map_symfile->LinkOSOAddress(addr);
924 }
925 // This is a normal DWARF file, no address fixups need to happen
926 return true;
927}
Greg Clayton1f746072012-08-29 21:13:06 +0000928lldb::LanguageType
929SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
930{
931 assert (sc.comp_unit);
932 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
933 if (dwarf_cu)
934 {
935 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly();
Jim Ingham28eb5712012-10-12 17:34:26 +0000936 if (die)
937 {
938 const uint32_t language = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
939 if (language)
940 return (lldb::LanguageType)language;
941 }
Greg Clayton1f746072012-08-29 21:13:06 +0000942 }
943 return eLanguageTypeUnknown;
944}
945
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946size_t
947SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
948{
949 assert (sc.comp_unit);
950 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +0000951 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952 if (dwarf_cu)
953 {
954 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +0000955 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +0000957 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958 {
959 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000960 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961 {
962 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
963 ++functions_added;
964 }
965 }
966 //FixupTypes();
967 }
968 return functions_added;
969}
970
971bool
972SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
973{
974 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +0000975 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Claytonda2455b2012-11-01 17:28:37 +0000976 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 {
Greg Claytonda2455b2012-11-01 17:28:37 +0000978 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000979
Greg Claytonda2455b2012-11-01 17:28:37 +0000980 if (cu_die)
981 {
982 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
983 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 +0000984
Greg Claytonda2455b2012-11-01 17:28:37 +0000985 // All file indexes in DWARF are one based and a file of index zero is
986 // supposed to be the compile unit itself.
987 support_files.Append (*sc.comp_unit);
988
989 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
990 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000991 }
992 return false;
993}
994
995struct ParseDWARFLineTableCallbackInfo
996{
997 LineTable* line_table;
Greg Clayton9422dd62013-03-04 21:46:16 +0000998 std::auto_ptr<LineSequence> sequence_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000999};
1000
1001//----------------------------------------------------------------------
1002// ParseStatementTableCallback
1003//----------------------------------------------------------------------
1004static void
1005ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1006{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001007 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1008 {
1009 // Just started parsing the line table
1010 }
1011 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1012 {
1013 // Done parsing line table, nothing to do for the cleanup
1014 }
1015 else
1016 {
1017 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
Greg Clayton9422dd62013-03-04 21:46:16 +00001018 LineTable* line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019
Greg Clayton9422dd62013-03-04 21:46:16 +00001020 // If this is our first time here, we need to create a
1021 // sequence container.
1022 if (!info->sequence_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001024 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1025 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001027 line_table->AppendLineEntryToSequence (info->sequence_ap.get(),
1028 state.address,
1029 state.line,
1030 state.column,
1031 state.file,
1032 state.is_stmt,
1033 state.basic_block,
1034 state.prologue_end,
1035 state.epilogue_begin,
1036 state.end_sequence);
1037 if (state.end_sequence)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001039 // First, put the current sequence into the line table.
1040 line_table->InsertSequence(info->sequence_ap.get());
1041 // Then, empty it to prepare for the next sequence.
1042 info->sequence_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001043 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044 }
1045}
1046
1047bool
1048SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1049{
1050 assert (sc.comp_unit);
1051 if (sc.comp_unit->GetLineTable() != NULL)
1052 return true;
1053
Greg Clayton1f746072012-08-29 21:13:06 +00001054 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055 if (dwarf_cu)
1056 {
1057 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001058 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001060 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1061 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001063 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1064 if (line_table_ap.get())
1065 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001066 ParseDWARFLineTableCallbackInfo info;
1067 info.line_table = line_table_ap.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00001068 lldb::offset_t offset = cu_line_offset;
Greg Clayton129d12c2011-11-28 03:29:03 +00001069 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
Greg Clayton9422dd62013-03-04 21:46:16 +00001070 if (m_debug_map_symfile)
1071 {
1072 // We have an object file that has a line table with addresses
1073 // that are not linked. We need to link the line table and convert
1074 // the addresses that are relative to the .o file into addresses
1075 // for the main executable.
1076 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get()));
1077 }
1078 else
1079 {
1080 sc.comp_unit->SetLineTable(line_table_ap.release());
1081 return true;
1082 }
Greg Clayton129d12c2011-11-28 03:29:03 +00001083 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 }
1085 }
1086 }
1087 return false;
1088}
1089
1090size_t
1091SymbolFileDWARF::ParseFunctionBlocks
1092(
1093 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001094 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001095 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096 const DWARFDebugInfoEntry *die,
1097 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001098 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099)
1100{
1101 size_t blocks_added = 0;
1102 while (die != NULL)
1103 {
1104 dw_tag_t tag = die->Tag();
1105
1106 switch (tag)
1107 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001109 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110 case DW_TAG_lexical_block:
1111 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001112 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001113 if (tag == DW_TAG_subprogram)
1114 {
1115 // Skip any DW_TAG_subprogram DIEs that are inside
1116 // of a normal or inlined functions. These will be
1117 // parsed on their own as separate entities.
1118
1119 if (depth > 0)
1120 break;
1121
1122 block = parent_block;
1123 }
1124 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001125 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001126 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001127 parent_block->AddChild(block_sp);
1128 block = block_sp.get();
1129 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001130 DWARFDebugRanges::RangeList ranges;
1131 const char *name = NULL;
1132 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 int decl_file = 0;
1135 int decl_line = 0;
1136 int decl_column = 0;
1137 int call_file = 0;
1138 int call_line = 0;
1139 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001140 if (die->GetDIENamesAndRanges (this,
1141 dwarf_cu,
1142 name,
1143 mangled_name,
1144 ranges,
1145 decl_file, decl_line, decl_column,
1146 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147 {
1148 if (tag == DW_TAG_subprogram)
1149 {
1150 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001151 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001153 else if (tag == DW_TAG_inlined_subroutine)
1154 {
1155 // We get called here for inlined subroutines in two ways.
1156 // The first time is when we are making the Function object
1157 // for this inlined concrete instance. Since we're creating a top level block at
1158 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1159 // adjust the containing address.
1160 // The second time is when we are parsing the blocks inside the function that contains
1161 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1162 // function the offset will be for that function.
1163 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1164 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001165 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001166 }
1167 }
1168
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001169 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170
1171 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1172 {
1173 std::auto_ptr<Declaration> decl_ap;
1174 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001175 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1176 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177
1178 std::auto_ptr<Declaration> call_ap;
1179 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001180 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1181 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001183 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001184 }
1185
1186 ++blocks_added;
1187
Greg Claytondd7feaf2011-08-12 17:54:33 +00001188 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001189 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001190 blocks_added += ParseFunctionBlocks (sc,
1191 block,
1192 dwarf_cu,
1193 die->GetFirstChild(),
1194 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001195 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196 }
1197 }
1198 }
1199 break;
1200 default:
1201 break;
1202 }
1203
Greg Claytondd7feaf2011-08-12 17:54:33 +00001204 // Only parse siblings of the block if we are not at depth zero. A depth
1205 // of zero indicates we are currently parsing the top level
1206 // DW_TAG_subprogram DIE
1207
1208 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001210 else
1211 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212 }
1213 return blocks_added;
1214}
1215
Greg Claytonf0705c82011-10-22 03:33:13 +00001216bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001217SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1218 const DWARFDebugInfoEntry *die,
1219 ClangASTContext::TemplateParameterInfos &template_param_infos)
1220{
1221 const dw_tag_t tag = die->Tag();
1222
1223 switch (tag)
1224 {
1225 case DW_TAG_template_type_parameter:
1226 case DW_TAG_template_value_parameter:
1227 {
1228 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1229
1230 DWARFDebugInfoEntry::Attributes attributes;
1231 const size_t num_attributes = die->GetAttributes (this,
1232 dwarf_cu,
1233 fixed_form_sizes,
1234 attributes);
1235 const char *name = NULL;
1236 Type *lldb_type = NULL;
1237 clang_type_t clang_type = NULL;
1238 uint64_t uval64 = 0;
1239 bool uval64_valid = false;
1240 if (num_attributes > 0)
1241 {
1242 DWARFFormValue form_value;
1243 for (size_t i=0; i<num_attributes; ++i)
1244 {
1245 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1246
1247 switch (attr)
1248 {
1249 case DW_AT_name:
1250 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1251 name = form_value.AsCString(&get_debug_str_data());
1252 break;
1253
1254 case DW_AT_type:
1255 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1256 {
1257 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1258 lldb_type = ResolveTypeUID(type_die_offset);
1259 if (lldb_type)
1260 clang_type = lldb_type->GetClangForwardType();
1261 }
1262 break;
1263
1264 case DW_AT_const_value:
1265 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1266 {
1267 uval64_valid = true;
1268 uval64 = form_value.Unsigned();
1269 }
1270 break;
1271 default:
1272 break;
1273 }
1274 }
1275
1276 if (name && lldb_type && clang_type)
1277 {
1278 bool is_signed = false;
1279 template_param_infos.names.push_back(name);
1280 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1281 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1282 {
1283 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
Sean Callanan3d654b32012-09-24 22:25:51 +00001284 template_param_infos.args.push_back (clang::TemplateArgument (*GetClangASTContext().getASTContext(),
1285 llvm::APSInt(apint),
1286 clang_qual_type));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001287 }
1288 else
1289 {
1290 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1291 }
1292 }
1293 else
1294 {
1295 return false;
1296 }
1297
1298 }
1299 }
1300 return true;
1301
1302 default:
1303 break;
1304 }
1305 return false;
1306}
1307
1308bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001309SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1310 const DWARFDebugInfoEntry *parent_die,
1311 ClangASTContext::TemplateParameterInfos &template_param_infos)
1312{
1313
1314 if (parent_die == NULL)
Filipe Cabecinhas52008c62012-05-23 16:24:11 +00001315 return false;
Greg Claytonf0705c82011-10-22 03:33:13 +00001316
Greg Claytonf0705c82011-10-22 03:33:13 +00001317 Args template_parameter_names;
1318 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1319 die != NULL;
1320 die = die->GetSibling())
1321 {
1322 const dw_tag_t tag = die->Tag();
1323
1324 switch (tag)
1325 {
1326 case DW_TAG_template_type_parameter:
1327 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001328 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001329 break;
1330
1331 default:
1332 break;
1333 }
1334 }
1335 if (template_param_infos.args.empty())
1336 return false;
1337 return template_param_infos.args.size() == template_param_infos.names.size();
1338}
1339
1340clang::ClassTemplateDecl *
1341SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001342 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001343 const char *parent_name,
1344 int tag_decl_kind,
1345 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1346{
1347 if (template_param_infos.IsValid())
1348 {
1349 std::string template_basename(parent_name);
1350 template_basename.erase (template_basename.find('<'));
1351 ClangASTContext &ast = GetClangASTContext();
1352
1353 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001354 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001355 template_basename.c_str(),
1356 tag_decl_kind,
1357 template_param_infos);
1358 }
1359 return NULL;
1360}
1361
Sean Callanana0b80ab2012-06-04 22:28:05 +00001362class SymbolFileDWARF::DelayedAddObjCClassProperty
1363{
1364public:
1365 DelayedAddObjCClassProperty
1366 (
1367 clang::ASTContext *ast,
1368 lldb::clang_type_t class_opaque_type,
1369 const char *property_name,
1370 lldb::clang_type_t property_opaque_type, // The property type is only required if you don't have an ivar decl
1371 clang::ObjCIvarDecl *ivar_decl,
1372 const char *property_setter_name,
1373 const char *property_getter_name,
1374 uint32_t property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00001375 const ClangASTMetadata *metadata
Sean Callanana0b80ab2012-06-04 22:28:05 +00001376 ) :
1377 m_ast (ast),
1378 m_class_opaque_type (class_opaque_type),
1379 m_property_name (property_name),
1380 m_property_opaque_type (property_opaque_type),
1381 m_ivar_decl (ivar_decl),
1382 m_property_setter_name (property_setter_name),
1383 m_property_getter_name (property_getter_name),
Jim Ingham379397632012-10-27 02:54:13 +00001384 m_property_attributes (property_attributes)
Sean Callanana0b80ab2012-06-04 22:28:05 +00001385 {
Jim Ingham379397632012-10-27 02:54:13 +00001386 if (metadata != NULL)
1387 {
1388 m_metadata_ap.reset(new ClangASTMetadata());
1389 *(m_metadata_ap.get()) = *metadata;
1390 }
1391 }
1392
1393 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
1394 {
Daniel Malead4c5be62012-11-12 21:02:14 +00001395 *this = rhs;
1396 }
1397
1398 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs)
1399 {
Jim Ingham379397632012-10-27 02:54:13 +00001400 m_ast = rhs.m_ast;
1401 m_class_opaque_type = rhs.m_class_opaque_type;
1402 m_property_name = rhs.m_property_name;
1403 m_property_opaque_type = rhs.m_property_opaque_type;
1404 m_ivar_decl = rhs.m_ivar_decl;
1405 m_property_setter_name = rhs.m_property_setter_name;
1406 m_property_getter_name = rhs.m_property_getter_name;
1407 m_property_attributes = rhs.m_property_attributes;
1408
1409 if (rhs.m_metadata_ap.get())
1410 {
1411 m_metadata_ap.reset (new ClangASTMetadata());
1412 *(m_metadata_ap.get()) = *(rhs.m_metadata_ap.get());
1413 }
Daniel Malead4c5be62012-11-12 21:02:14 +00001414 return *this;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001415 }
1416
1417 bool Finalize() const
1418 {
1419 return ClangASTContext::AddObjCClassProperty(m_ast,
1420 m_class_opaque_type,
1421 m_property_name,
1422 m_property_opaque_type,
1423 m_ivar_decl,
1424 m_property_setter_name,
1425 m_property_getter_name,
1426 m_property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00001427 m_metadata_ap.get());
Sean Callanana0b80ab2012-06-04 22:28:05 +00001428 }
1429private:
1430 clang::ASTContext *m_ast;
1431 lldb::clang_type_t m_class_opaque_type;
1432 const char *m_property_name;
1433 lldb::clang_type_t m_property_opaque_type;
1434 clang::ObjCIvarDecl *m_ivar_decl;
1435 const char *m_property_setter_name;
1436 const char *m_property_getter_name;
1437 uint32_t m_property_attributes;
Jim Ingham379397632012-10-27 02:54:13 +00001438 std::auto_ptr<ClangASTMetadata> m_metadata_ap;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001439};
1440
Sean Callananfaa0bb32012-12-05 23:37:14 +00001441struct BitfieldInfo
1442{
1443 uint64_t bit_size;
1444 uint64_t bit_offset;
1445
1446 BitfieldInfo () :
1447 bit_size (LLDB_INVALID_ADDRESS),
1448 bit_offset (LLDB_INVALID_ADDRESS)
1449 {
1450 }
1451
1452 bool IsValid ()
1453 {
1454 return (bit_size != LLDB_INVALID_ADDRESS) &&
1455 (bit_offset != LLDB_INVALID_ADDRESS);
1456 }
1457};
1458
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459size_t
1460SymbolFileDWARF::ParseChildMembers
1461(
1462 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001463 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001465 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001466 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1468 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001469 DWARFDIECollection& member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00001470 DelayedPropertyList& delayed_properties,
Sean Callananc7fbf732010-08-06 00:32:49 +00001471 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001472 bool &is_a_class,
1473 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001474)
1475{
1476 if (parent_die == NULL)
1477 return 0;
1478
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001479 size_t count = 0;
1480 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001481 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001482 uint32_t member_idx = 0;
Sean Callananfaa0bb32012-12-05 23:37:14 +00001483 BitfieldInfo last_field_info;
Greg Claytond88d7592010-09-15 08:33:30 +00001484
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001485 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1486 {
1487 dw_tag_t tag = die->Tag();
1488
1489 switch (tag)
1490 {
1491 case DW_TAG_member:
Sean Callanan3d654b32012-09-24 22:25:51 +00001492 case DW_TAG_APPLE_property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001493 {
1494 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001495 const size_t num_attributes = die->GetAttributes (this,
1496 dwarf_cu,
1497 fixed_form_sizes,
1498 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499 if (num_attributes > 0)
1500 {
1501 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001502 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001504 const char *prop_name = NULL;
1505 const char *prop_getter_name = NULL;
1506 const char *prop_setter_name = NULL;
Greg Claytone528efd72013-02-26 23:45:18 +00001507 uint32_t prop_attributes = 0;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001508
1509
Greg Clayton24739922010-10-13 03:15:28 +00001510 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001512 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001513 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514 size_t byte_size = 0;
1515 size_t bit_offset = 0;
1516 size_t bit_size = 0;
Greg Claytone528efd72013-02-26 23:45:18 +00001517 bool is_external = false; // On DW_TAG_members, this means the member is static
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001518 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001519 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520 {
1521 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1522 DWARFFormValue form_value;
1523 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1524 {
1525 switch (attr)
1526 {
1527 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1528 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1529 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1530 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1531 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1532 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1533 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1534 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1535 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001536 if (form_value.BlockData())
1537 {
1538 Value initialValue(0);
1539 Value memberOffset(0);
1540 const DataExtractor& debug_info_data = get_debug_info_data();
1541 uint32_t block_length = form_value.Unsigned();
1542 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1543 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1544 NULL, // clang::ASTContext *
1545 NULL, // ClangExpressionVariableList *
1546 NULL, // ClangExpressionDeclMap *
1547 NULL, // RegisterContext *
1548 debug_info_data,
1549 block_offset,
1550 block_length,
1551 eRegisterKindDWARF,
1552 &initialValue,
1553 memberOffset,
1554 NULL))
1555 {
1556 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1557 }
1558 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001559 break;
1560
Greg Clayton8cf05932010-07-22 18:30:50 +00001561 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Claytone528efd72013-02-26 23:45:18 +00001562 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001563 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1564 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1565 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1566 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
Greg Claytone528efd72013-02-26 23:45:18 +00001567 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001568
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001569 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001570 case DW_AT_declaration:
1571 case DW_AT_description:
1572 case DW_AT_mutable:
1573 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001574 case DW_AT_sibling:
1575 break;
1576 }
1577 }
1578 }
Sean Callanana6582262012-04-05 00:12:52 +00001579
1580 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00001581 {
Sean Callanana6582262012-04-05 00:12:52 +00001582 ConstString fixed_getter;
1583 ConstString fixed_setter;
1584
1585 // Check if the property getter/setter were provided as full
1586 // names. We want basenames, so we extract them.
1587
1588 if (prop_getter_name && prop_getter_name[0] == '-')
1589 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00001590 ObjCLanguageRuntime::MethodName prop_getter_method(prop_getter_name, true);
1591 prop_getter_name = prop_getter_method.GetSelector().GetCString();
Sean Callanana6582262012-04-05 00:12:52 +00001592 }
1593
1594 if (prop_setter_name && prop_setter_name[0] == '-')
1595 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00001596 ObjCLanguageRuntime::MethodName prop_setter_method(prop_setter_name, true);
1597 prop_setter_name = prop_setter_method.GetSelector().GetCString();
Sean Callanana6582262012-04-05 00:12:52 +00001598 }
1599
1600 // If the names haven't been provided, they need to be
1601 // filled in.
1602
1603 if (!prop_getter_name)
1604 {
1605 prop_getter_name = prop_name;
1606 }
1607 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
1608 {
1609 StreamString ss;
1610
1611 ss.Printf("set%c%s:",
1612 toupper(prop_name[0]),
1613 &prop_name[1]);
1614
1615 fixed_setter.SetCString(ss.GetData());
1616 prop_setter_name = fixed_setter.GetCString();
1617 }
Sean Callanan751aac62012-03-29 19:07:06 +00001618 }
1619
1620 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001621 // represents fields that are references with bad byte size
1622 // and bit size/offset information such as:
1623 //
1624 // DW_AT_byte_size( 0x00 )
1625 // DW_AT_bit_size( 0x40 )
1626 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1627 //
1628 // So check the bit offset to make sure it is sane, and if
1629 // the values are not sane, remove them. If we don't do this
1630 // then we will end up with a crash if we try to use this
1631 // type in an expression when clang becomes unhappy with its
1632 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001633
Greg Clayton44953932011-10-25 01:25:35 +00001634 if (bit_offset > 128)
1635 {
1636 bit_size = 0;
1637 bit_offset = 0;
1638 }
1639
1640 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001641 if (class_language == eLanguageTypeObjC ||
1642 class_language == eLanguageTypeObjC_plus_plus)
1643 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001644
1645 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1646 {
1647 // Not all compilers will mark the vtable pointer
1648 // member as artificial (llvm-gcc). We can't have
1649 // the virtual members in our classes otherwise it
1650 // throws off all child offsets since we end up
1651 // having and extra pointer sized member in our
1652 // class layouts.
1653 is_artificial = true;
1654 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655
Greg Claytone528efd72013-02-26 23:45:18 +00001656 // Skip static members
1657 if (is_external && member_byte_offset == UINT32_MAX)
1658 break;
1659
Greg Clayton24739922010-10-13 03:15:28 +00001660 if (is_artificial == false)
1661 {
1662 Type *member_type = ResolveTypeUID(encoding_uid);
Sean Callananfa3ab452012-12-14 00:54:13 +00001663
Jim Inghame3ae82a2011-11-12 01:36:43 +00001664 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001665 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001666 {
Sean Callanan751aac62012-03-29 19:07:06 +00001667 if (member_type)
1668 {
1669 if (accessibility == eAccessNone)
1670 accessibility = default_accessibility;
1671 member_accessibilities.push_back(accessibility);
Sean Callananfaa0bb32012-12-05 23:37:14 +00001672
1673 BitfieldInfo this_field_info;
1674
1675 this_field_info.bit_size = bit_size;
1676
1677 if (member_byte_offset != UINT32_MAX || bit_size != 0)
Greg Clayton88bc7f32012-11-06 00:20:41 +00001678 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00001679 /////////////////////////////////////////////////////////////
1680 // How to locate a field given the DWARF debug information
1681 //
1682 // AT_byte_size indicates the size of the word in which the
1683 // bit offset must be interpreted.
1684 //
1685 // AT_data_member_location indicates the byte offset of the
1686 // word from the base address of the structure.
1687 //
1688 // AT_bit_offset indicates how many bits into the word
1689 // (according to the host endianness) the low-order bit of
1690 // the field starts. AT_bit_offset can be negative.
1691 //
1692 // AT_bit_size indicates the size of the field in bits.
1693 /////////////////////////////////////////////////////////////
1694
1695 this_field_info.bit_offset = 0;
1696
1697 this_field_info.bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1698
1699 if (GetObjectFile()->GetByteOrder() == eByteOrderLittle)
1700 {
1701 this_field_info.bit_offset += byte_size * 8;
1702 this_field_info.bit_offset -= (bit_offset + bit_size);
1703 }
1704 else
1705 {
1706 this_field_info.bit_offset += bit_offset;
1707 }
1708 }
1709
1710 // If the member to be emitted did not start on a character boundary and there is
1711 // empty space between the last field and this one, then we need to emit an
1712 // anonymous member filling up the space up to its start. There are three cases
1713 // here:
1714 //
1715 // 1 If the previous member ended on a character boundary, then we can emit an
1716 // anonymous member starting at the most recent character boundary.
1717 //
1718 // 2 If the previous member did not end on a character boundary and the distance
1719 // from the end of the previous member to the current member is less than a
1720 // word width, then we can emit an anonymous member starting right after the
1721 // previous member and right before this member.
1722 //
1723 // 3 If the previous member did not end on a character boundary and the distance
1724 // from the end of the previous member to the current member is greater than
1725 // or equal a word width, then we act as in Case 1.
1726
1727 const uint64_t character_width = 8;
1728 const uint64_t word_width = 32;
1729
1730 if (this_field_info.IsValid())
1731 {
1732 // Objective-C has invalid DW_AT_bit_offset values in older versions
1733 // of clang, so we have to be careful and only insert unnammed bitfields
Greg Clayton37c36e42012-11-27 00:59:26 +00001734 // if we have a new enough clang.
1735 bool detect_unnamed_bitfields = true;
Greg Clayton88bc7f32012-11-06 00:20:41 +00001736
Greg Clayton37c36e42012-11-27 00:59:26 +00001737 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus)
1738 detect_unnamed_bitfields = dwarf_cu->Supports_unnamed_objc_bitfields ();
1739
1740 if (detect_unnamed_bitfields)
Greg Clayton88bc7f32012-11-06 00:20:41 +00001741 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00001742 BitfieldInfo anon_field_info;
1743
1744 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned
Greg Clayton88bc7f32012-11-06 00:20:41 +00001745 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00001746 uint64_t last_field_end = 0;
Greg Clayton88bc7f32012-11-06 00:20:41 +00001747
Sean Callananfaa0bb32012-12-05 23:37:14 +00001748 if (last_field_info.IsValid())
1749 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
Greg Clayton88bc7f32012-11-06 00:20:41 +00001750
Sean Callananfaa0bb32012-12-05 23:37:14 +00001751 if (this_field_info.bit_offset != last_field_end)
1752 {
1753 if (((last_field_end % character_width) == 0) || // case 1
1754 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3
1755 {
1756 anon_field_info.bit_size = this_field_info.bit_offset % character_width;
1757 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size;
1758 }
1759 else // case 2
1760 {
1761 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end;
1762 anon_field_info.bit_offset = last_field_end;
1763 }
Greg Clayton88bc7f32012-11-06 00:20:41 +00001764 }
Greg Clayton88bc7f32012-11-06 00:20:41 +00001765 }
1766
Sean Callananfaa0bb32012-12-05 23:37:14 +00001767 if (anon_field_info.IsValid())
Greg Clayton88bc7f32012-11-06 00:20:41 +00001768 {
Greg Clayton88bc7f32012-11-06 00:20:41 +00001769 clang::FieldDecl *unnamed_bitfield_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
1770 NULL,
Sean Callananfaa0bb32012-12-05 23:37:14 +00001771 GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width),
Greg Clayton88bc7f32012-11-06 00:20:41 +00001772 accessibility,
Sean Callananfaa0bb32012-12-05 23:37:14 +00001773 anon_field_info.bit_size);
Greg Clayton88bc7f32012-11-06 00:20:41 +00001774
Sean Callananfaa0bb32012-12-05 23:37:14 +00001775 layout_info.field_offsets.insert(std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset));
Greg Clayton88bc7f32012-11-06 00:20:41 +00001776 }
1777 }
1778 }
Sean Callananfaa0bb32012-12-05 23:37:14 +00001779
Sean Callananfa3ab452012-12-14 00:54:13 +00001780 clang_type_t member_clang_type = member_type->GetClangLayoutType();
1781
1782 {
1783 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>).
1784 // If the current field is at the end of the structure, then there is definitely no room for extra
1785 // elements and we override the type to array[0].
1786
1787 clang_type_t member_array_element_type;
1788 uint64_t member_array_size;
1789 bool member_array_is_incomplete;
1790
1791 if (GetClangASTContext().IsArrayType(member_clang_type,
1792 &member_array_element_type,
1793 &member_array_size,
1794 &member_array_is_incomplete) &&
1795 !member_array_is_incomplete)
1796 {
1797 uint64_t parent_byte_size = parent_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, UINT64_MAX);
1798
1799 if (member_byte_offset >= parent_byte_size)
1800 {
1801 if (member_array_size != 1)
1802 {
1803 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which extends beyond the bounds of 0x%8.8" PRIx64,
1804 MakeUserID(die->GetOffset()),
1805 name,
1806 encoding_uid,
1807 MakeUserID(parent_die->GetOffset()));
1808 }
1809
1810 member_clang_type = GetClangASTContext().CreateArrayType(member_array_element_type, 0);
1811 }
1812 }
1813 }
1814
Greg Clayton88bc7f32012-11-06 00:20:41 +00001815 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
Sean Callanan751aac62012-03-29 19:07:06 +00001816 name,
Sean Callananfa3ab452012-12-14 00:54:13 +00001817 member_clang_type,
Sean Callanan751aac62012-03-29 19:07:06 +00001818 accessibility,
1819 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00001820
Jim Ingham379397632012-10-27 02:54:13 +00001821 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)field_decl, MakeUserID(die->GetOffset()));
Sean Callananfaa0bb32012-12-05 23:37:14 +00001822
1823 if (this_field_info.IsValid())
1824 {
1825 layout_info.field_offsets.insert(std::make_pair(field_decl, this_field_info.bit_offset));
1826 last_field_info = this_field_info;
1827 }
Sean Callanan5b26f272012-02-04 08:49:35 +00001828 }
1829 else
1830 {
Sean Callanan751aac62012-03-29 19:07:06 +00001831 if (name)
Daniel Malead01b2952012-11-29 21:49:15 +00001832 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
Sean Callanan751aac62012-03-29 19:07:06 +00001833 MakeUserID(die->GetOffset()),
1834 name,
1835 encoding_uid);
1836 else
Daniel Malead01b2952012-11-29 21:49:15 +00001837 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
Sean Callanan751aac62012-03-29 19:07:06 +00001838 MakeUserID(die->GetOffset()),
1839 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00001840 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001841 }
Sean Callanan751aac62012-03-29 19:07:06 +00001842
Jim Inghame3ae82a2011-11-12 01:36:43 +00001843 if (prop_name != NULL)
1844 {
Sean Callanan751aac62012-03-29 19:07:06 +00001845 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001846
Sean Callanan751aac62012-03-29 19:07:06 +00001847 if (field_decl)
1848 {
1849 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1850 assert (ivar_decl != NULL);
1851 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001852
Jim Ingham379397632012-10-27 02:54:13 +00001853 ClangASTMetadata metadata;
1854 metadata.SetUserID (MakeUserID(die->GetOffset()));
Sean Callanana0b80ab2012-06-04 22:28:05 +00001855 delayed_properties.push_back(DelayedAddObjCClassProperty(GetClangASTContext().getASTContext(),
1856 class_clang_type,
1857 prop_name,
1858 member_type->GetClangLayoutType(),
1859 ivar_decl,
1860 prop_setter_name,
1861 prop_getter_name,
1862 prop_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00001863 &metadata));
Sean Callanan60217122012-04-13 00:10:03 +00001864
Sean Callananad880762012-04-18 01:06:17 +00001865 if (ivar_decl)
Jim Ingham379397632012-10-27 02:54:13 +00001866 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00001867 }
Greg Clayton24739922010-10-13 03:15:28 +00001868 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001869 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001870 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001871 }
1872 break;
1873
1874 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001875 // Let the type parsing code handle this one for us.
1876 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877 break;
1878
1879 case DW_TAG_inheritance:
1880 {
1881 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001882 if (default_accessibility == eAccessNone)
1883 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001884 // TODO: implement DW_TAG_inheritance type parsing
1885 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001886 const size_t num_attributes = die->GetAttributes (this,
1887 dwarf_cu,
1888 fixed_form_sizes,
1889 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001890 if (num_attributes > 0)
1891 {
1892 Declaration decl;
1893 DWARFExpression location;
1894 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001895 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001896 bool is_virtual = false;
1897 bool is_base_of_class = true;
Greg Clayton2508b9b2012-11-01 23:20:02 +00001898 off_t member_byte_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001899 uint32_t i;
1900 for (i=0; i<num_attributes; ++i)
1901 {
1902 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1903 DWARFFormValue form_value;
1904 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1905 {
1906 switch (attr)
1907 {
1908 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1909 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1910 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1911 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton2508b9b2012-11-01 23:20:02 +00001912 case DW_AT_data_member_location:
1913 if (form_value.BlockData())
1914 {
1915 Value initialValue(0);
1916 Value memberOffset(0);
1917 const DataExtractor& debug_info_data = get_debug_info_data();
1918 uint32_t block_length = form_value.Unsigned();
1919 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1920 if (DWARFExpression::Evaluate (NULL,
1921 NULL,
1922 NULL,
1923 NULL,
1924 NULL,
1925 debug_info_data,
1926 block_offset,
1927 block_length,
1928 eRegisterKindDWARF,
1929 &initialValue,
1930 memberOffset,
1931 NULL))
1932 {
1933 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1934 }
1935 }
1936 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001937
1938 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001939 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001940 break;
1941
1942 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1943 default:
1944 case DW_AT_sibling:
1945 break;
1946 }
1947 }
1948 }
1949
Greg Clayton526e5af2010-11-13 03:52:47 +00001950 Type *base_class_type = ResolveTypeUID(encoding_uid);
1951 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001952
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001953 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1954 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001955 if (class_language == eLanguageTypeObjC)
1956 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001957 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001958 }
1959 else
1960 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001961 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001962 accessibility,
1963 is_virtual,
1964 is_base_of_class));
Greg Clayton2508b9b2012-11-01 23:20:02 +00001965
1966 if (is_virtual)
1967 {
1968 layout_info.vbase_offsets.insert(std::make_pair(ClangASTType::GetAsCXXRecordDecl(class_clang_type),
1969 clang::CharUnits::fromQuantity(member_byte_offset)));
1970 }
1971 else
1972 {
1973 layout_info.base_offsets.insert(std::make_pair(ClangASTType::GetAsCXXRecordDecl(class_clang_type),
1974 clang::CharUnits::fromQuantity(member_byte_offset)));
1975 }
Greg Clayton9e409562010-07-28 02:04:09 +00001976 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001977 }
1978 }
1979 break;
1980
1981 default:
1982 break;
1983 }
1984 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00001985
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986 return count;
1987}
1988
1989
1990clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001991SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001992{
1993 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001994 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001995 {
1996 DWARFCompileUnitSP cu_sp;
1997 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1998 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001999 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00002000 }
2001 return NULL;
2002}
2003
2004clang::DeclContext*
2005SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
2006{
Greg Clayton81c22f62011-10-19 18:09:39 +00002007 if (UserIDMatches(type_uid))
2008 return GetClangDeclContextForDIEOffset (sc, type_uid);
2009 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002010}
2011
2012Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00002013SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002014{
Greg Clayton81c22f62011-10-19 18:09:39 +00002015 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002016 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002017 DWARFDebugInfo* debug_info = DebugInfo();
2018 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00002019 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002020 DWARFCompileUnitSP cu_sp;
2021 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00002022 const bool assert_not_being_parsed = true;
2023 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00002024 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002025 }
2026 return NULL;
2027}
2028
Greg Claytoncab36a32011-12-08 05:16:30 +00002029Type*
2030SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00002031{
Greg Claytoncab36a32011-12-08 05:16:30 +00002032 if (die != NULL)
2033 {
Greg Clayton3bffb082011-12-10 02:15:28 +00002034 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
2035 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002036 GetObjectFile()->GetModule()->LogMessage (log.get(),
2037 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
2038 die->GetOffset(),
2039 DW_TAG_value_to_name(die->Tag()),
2040 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00002041
Greg Claytoncab36a32011-12-08 05:16:30 +00002042 // We might be coming in in the middle of a type tree (a class
2043 // withing a class, an enum within a class), so parse any needed
2044 // parent DIEs before we get to this one...
2045 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2046 switch (decl_ctx_die->Tag())
2047 {
2048 case DW_TAG_structure_type:
2049 case DW_TAG_union_type:
2050 case DW_TAG_class_type:
2051 {
2052 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00002053 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002054 GetObjectFile()->GetModule()->LogMessage (log.get(),
2055 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
2056 die->GetOffset(),
2057 DW_TAG_value_to_name(die->Tag()),
2058 die->GetName(this, cu),
2059 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00002060//
2061// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
2062// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
2063// {
2064// if (log)
2065// GetObjectFile()->GetModule()->LogMessage (log.get(),
2066// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
2067// die->GetOffset(),
2068// DW_TAG_value_to_name(die->Tag()),
2069// die->GetName(this, cu),
2070// decl_ctx_die->GetOffset());
2071// // Ask the type to complete itself if it already hasn't since if we
2072// // want a function (method or static) from a class, the class must
2073// // create itself and add it's own methods and class functions.
2074// if (parent_type)
2075// parent_type->GetClangFullType();
2076// }
Greg Claytoncab36a32011-12-08 05:16:30 +00002077 }
2078 break;
2079
2080 default:
2081 break;
2082 }
2083 return ResolveType (cu, die);
2084 }
2085 return NULL;
2086}
2087
Greg Clayton6beaaa62011-01-17 03:46:26 +00002088// This function is used when SymbolFileDWARFDebugMap owns a bunch of
2089// SymbolFileDWARF objects to detect if this DWARF file is the one that
2090// can resolve a clang_type.
2091bool
2092SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
2093{
2094 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2095 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
2096 return die != NULL;
2097}
2098
2099
Greg Clayton1be10fc2010-09-29 01:12:09 +00002100lldb::clang_type_t
2101SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
2102{
2103 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002104 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2105 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002106 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002107 {
2108 // We have already resolved this type...
2109 return clang_type;
2110 }
2111 // Once we start resolving this type, remove it from the forward declaration
2112 // map in case anyone child members or other types require this type to get resolved.
2113 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
2114 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002115 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00002116
Greg Clayton1be10fc2010-09-29 01:12:09 +00002117
Greg Clayton85ae2e12011-10-18 23:36:41 +00002118 // Disable external storage for this type so we don't get anymore
2119 // clang::ExternalASTSource queries for this type.
2120 ClangASTContext::SetHasExternalStorage (clang_type, false);
2121
Greg Clayton450e3f32010-10-12 02:24:53 +00002122 DWARFDebugInfo* debug_info = DebugInfo();
2123
Greg Clayton53eb1c22012-04-02 22:59:12 +00002124 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002125 Type *type = m_die_to_type.lookup (die);
2126
2127 const dw_tag_t tag = die->Tag();
2128
Greg Claytonbaf95da2012-03-30 23:50:54 +00002129 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00002130 if (log)
Greg Claytonbaf95da2012-03-30 23:50:54 +00002131 {
Greg Claytond61c0fc2012-04-23 22:55:20 +00002132 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log.get(),
Daniel Malead01b2952012-11-29 21:49:15 +00002133 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
Greg Claytond61c0fc2012-04-23 22:55:20 +00002134 MakeUserID(die->GetOffset()),
2135 DW_TAG_value_to_name(tag),
2136 type->GetName().AsCString());
Greg Claytonbaf95da2012-03-30 23:50:54 +00002137
Greg Claytonbaf95da2012-03-30 23:50:54 +00002138 }
Greg Clayton1be10fc2010-09-29 01:12:09 +00002139 assert (clang_type);
2140 DWARFDebugInfoEntry::Attributes attributes;
2141
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002142 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002143
2144 switch (tag)
2145 {
2146 case DW_TAG_structure_type:
2147 case DW_TAG_union_type:
2148 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002149 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002150 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002151
Greg Clayton1be10fc2010-09-29 01:12:09 +00002152 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002153 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002154 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002155
Sean Callanan77a1fd32012-02-27 20:07:01 +00002156 LanguageType class_language = eLanguageTypeUnknown;
2157 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
2158 if (is_objc_class)
Greg Clayton219cf312012-03-30 00:51:13 +00002159 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002160 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002161 // For objective C we don't start the definition when
2162 // the class is created.
2163 ast.StartTagDeclarationDefinition (clang_type);
2164 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002165
2166 int tag_decl_kind = -1;
2167 AccessType default_accessibility = eAccessNone;
2168 if (tag == DW_TAG_structure_type)
2169 {
2170 tag_decl_kind = clang::TTK_Struct;
2171 default_accessibility = eAccessPublic;
2172 }
2173 else if (tag == DW_TAG_union_type)
2174 {
2175 tag_decl_kind = clang::TTK_Union;
2176 default_accessibility = eAccessPublic;
2177 }
2178 else if (tag == DW_TAG_class_type)
2179 {
2180 tag_decl_kind = clang::TTK_Class;
2181 default_accessibility = eAccessPrivate;
2182 }
2183
Greg Clayton53eb1c22012-04-02 22:59:12 +00002184 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002185 std::vector<clang::CXXBaseSpecifier *> base_classes;
2186 std::vector<int> member_accessibilities;
2187 bool is_a_class = false;
2188 // Parse members and base classes first
2189 DWARFDIECollection member_function_dies;
Sean Callanana0b80ab2012-06-04 22:28:05 +00002190
2191 DelayedPropertyList delayed_properties;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002192 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002193 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002194 die,
2195 clang_type,
2196 class_language,
2197 base_classes,
2198 member_accessibilities,
2199 member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00002200 delayed_properties,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002201 default_accessibility,
2202 is_a_class,
2203 layout_info);
2204
2205 // Now parse any methods if there were any...
2206 size_t num_functions = member_function_dies.Size();
2207 if (num_functions > 0)
2208 {
2209 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002210 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002211 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002212 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002213 }
2214
2215 if (class_language == eLanguageTypeObjC)
2216 {
Greg Clayton84db9102012-03-26 23:03:23 +00002217 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(ast.getASTContext(), clang_type));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002218 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00002219 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002220
Sean Callanan77a1fd32012-02-27 20:07:01 +00002221 DIEArray method_die_offsets;
2222 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002223 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002224 if (m_apple_objc_ap.get())
2225 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
2226 }
2227 else
2228 {
2229 if (!m_indexed)
2230 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002231
Sean Callanan77a1fd32012-02-27 20:07:01 +00002232 ConstString class_name (class_str.c_str());
2233 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2234 }
2235
2236 if (!method_die_offsets.empty())
2237 {
2238 DWARFDebugInfo* debug_info = DebugInfo();
2239
2240 DWARFCompileUnit* method_cu = NULL;
2241 const size_t num_matches = method_die_offsets.size();
2242 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002243 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002244 const dw_offset_t die_offset = method_die_offsets[i];
2245 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2246
2247 if (method_die)
2248 ResolveType (method_cu, method_die);
2249 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002250 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002251 if (m_using_apple_tables)
2252 {
2253 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2254 die_offset, class_str.c_str());
2255 }
2256 }
2257 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002258 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00002259
2260 for (DelayedPropertyList::const_iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
2261 pi != pe;
2262 ++pi)
2263 pi->Finalize();
Greg Clayton450e3f32010-10-12 02:24:53 +00002264 }
2265 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002266
2267 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2268 // need to tell the clang type it is actually a class.
2269 if (class_language != eLanguageTypeObjC)
2270 {
2271 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2272 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2273 }
2274
2275 // Since DW_TAG_structure_type gets used for both classes
2276 // and structures, we may need to set any DW_TAG_member
2277 // fields to have a "private" access if none was specified.
2278 // When we parsed the child members we tracked that actual
2279 // accessibility value for each DW_TAG_member in the
2280 // "member_accessibilities" array. If the value for the
2281 // member is zero, then it was set to the "default_accessibility"
2282 // which for structs was "public". Below we correct this
2283 // by setting any fields to "private" that weren't correctly
2284 // set.
2285 if (is_a_class && !member_accessibilities.empty())
2286 {
2287 // This is a class and all members that didn't have
2288 // their access specified are private.
2289 ast.SetDefaultAccessForRecordFields (clang_type,
2290 eAccessPrivate,
2291 &member_accessibilities.front(),
2292 member_accessibilities.size());
2293 }
2294
2295 if (!base_classes.empty())
2296 {
2297 ast.SetBaseClassesForClassType (clang_type,
2298 &base_classes.front(),
2299 base_classes.size());
2300
2301 // Clang will copy each CXXBaseSpecifier in "base_classes"
2302 // so we have to free them all.
2303 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2304 base_classes.size());
2305 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002306 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002307 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002308
2309 ast.BuildIndirectFields (clang_type);
2310
Sean Callanan77a1fd32012-02-27 20:07:01 +00002311 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002312
Greg Clayton2508b9b2012-11-01 23:20:02 +00002313 if (!layout_info.field_offsets.empty() ||
2314 !layout_info.base_offsets.empty() ||
2315 !layout_info.vbase_offsets.empty() )
Greg Claytoncaab74e2012-01-28 00:48:57 +00002316 {
2317 if (type)
2318 layout_info.bit_size = type->GetByteSize() * 8;
2319 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002320 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Greg Clayton2508b9b2012-11-01 23:20:02 +00002321
2322 clang::CXXRecordDecl *record_decl = ClangASTType::GetAsCXXRecordDecl(clang_type);
2323 if (record_decl)
Greg Claytoncaab74e2012-01-28 00:48:57 +00002324 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002325 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002326 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002327 GetObjectFile()->GetModule()->LogMessage (log.get(),
Daniel Malead01b2952012-11-29 21:49:15 +00002328 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) caching layout info for record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])",
Greg Claytoncaab74e2012-01-28 00:48:57 +00002329 clang_type,
2330 record_decl,
2331 layout_info.bit_size,
2332 layout_info.alignment,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002333 (uint32_t)layout_info.field_offsets.size(),
2334 (uint32_t)layout_info.base_offsets.size(),
2335 (uint32_t)layout_info.vbase_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002336
Greg Clayton2508b9b2012-11-01 23:20:02 +00002337 uint32_t idx;
2338 {
Greg Clayton821ac6d2012-01-28 02:22:27 +00002339 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
Greg Clayton2508b9b2012-11-01 23:20:02 +00002340 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002341 {
2342 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton2508b9b2012-11-01 23:20:02 +00002343 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }",
Greg Clayton821ac6d2012-01-28 02:22:27 +00002344 clang_type,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002345 idx,
Greg Clayton821ac6d2012-01-28 02:22:27 +00002346 (uint32_t)pos->second,
2347 pos->first->getNameAsString().c_str());
2348 }
Greg Clayton2508b9b2012-11-01 23:20:02 +00002349 }
2350
2351 {
2352 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos, base_end = layout_info.base_offsets.end();
2353 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx)
2354 {
2355 GetObjectFile()->GetModule()->LogMessage (log.get(),
2356 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }",
2357 clang_type,
2358 idx,
2359 (uint32_t)base_pos->second.getQuantity(),
2360 base_pos->first->getNameAsString().c_str());
2361 }
2362 }
2363 {
2364 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos, vbase_end = layout_info.vbase_offsets.end();
2365 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx)
2366 {
2367 GetObjectFile()->GetModule()->LogMessage (log.get(),
2368 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }",
2369 clang_type,
2370 idx,
2371 (uint32_t)vbase_pos->second.getQuantity(),
2372 vbase_pos->first->getNameAsString().c_str());
2373 }
2374 }
Greg Clayton821ac6d2012-01-28 02:22:27 +00002375 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002376 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2377 }
2378 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002379 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002380
Greg Claytonc93237c2010-10-01 20:48:32 +00002381 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002382
2383 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002384 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002385 if (die->HasChildren())
2386 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002387 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Greg Clayton3e067532013-03-05 23:54:39 +00002388 bool is_signed = false;
2389 ast.IsIntegerType(clang_type, is_signed);
2390 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002391 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002392 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002393 return clang_type;
2394
2395 default:
2396 assert(false && "not a forward clang type decl!");
2397 break;
2398 }
2399 return NULL;
2400}
2401
Greg Claytonc685f8e2010-09-15 04:15:46 +00002402Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002403SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002404{
2405 if (type_die != NULL)
2406 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002407 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002408
Greg Claytonc685f8e2010-09-15 04:15:46 +00002409 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002410 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002411
Greg Clayton24739922010-10-13 03:15:28 +00002412 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002413 {
2414 if (type != DIE_IS_BEING_PARSED)
2415 return type;
2416
2417 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002418 type_die->GetOffset(),
2419 DW_TAG_value_to_name(type_die->Tag()),
2420 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002421
2422 }
2423 else
2424 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002425 }
2426 return NULL;
2427}
2428
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002429CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002430SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002431{
2432 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002433 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002434 {
2435 // The symbol vendor doesn't know about this compile unit, we
2436 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002437 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002438 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002439 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002440}
2441
2442bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002443SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002444{
Greg Clayton72310352013-02-23 04:12:47 +00002445 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002446 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002447 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002448
Greg Clayton81c22f62011-10-19 18:09:39 +00002449 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002450 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002451 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002452
2453 if (sc.function)
2454 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002455 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002456 return true;
2457 }
2458
2459 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002460}
2461
2462uint32_t
2463SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2464{
2465 Timer scoped_timer(__PRETTY_FUNCTION__,
Daniel Malead01b2952012-11-29 21:49:15 +00002466 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002467 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002468 so_addr.GetOffset(),
2469 resolve_scope);
2470 uint32_t resolved = 0;
2471 if (resolve_scope & ( eSymbolContextCompUnit |
2472 eSymbolContextFunction |
2473 eSymbolContextBlock |
2474 eSymbolContextLineEntry))
2475 {
2476 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2477
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002478 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002479 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002480 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002481 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002482 if (cu_offset != DW_INVALID_OFFSET)
2483 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002484 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002485 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2486 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002487 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002488 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002489 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002490 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002491 resolved |= eSymbolContextCompUnit;
2492
Greg Clayton6ab80132012-12-12 17:30:52 +00002493 bool force_check_line_table = false;
Greg Clayton526a4ae2012-05-16 22:09:01 +00002494 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2495 {
2496 DWARFDebugInfoEntry *function_die = NULL;
2497 DWARFDebugInfoEntry *block_die = NULL;
2498 if (resolve_scope & eSymbolContextBlock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002499 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002500 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
2501 }
2502 else
2503 {
2504 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
2505 }
2506
2507 if (function_die != NULL)
2508 {
2509 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2510 if (sc.function == NULL)
2511 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2512 }
2513 else
2514 {
2515 // We might have had a compile unit that had discontiguous
2516 // address ranges where the gaps are symbols that don't have
2517 // any debug info. Discontiguous compile unit address ranges
2518 // should only happen when there aren't other functions from
2519 // other compile units in these gaps. This helps keep the size
2520 // of the aranges down.
Greg Clayton6ab80132012-12-12 17:30:52 +00002521 force_check_line_table = true;
Greg Clayton526a4ae2012-05-16 22:09:01 +00002522 }
2523
2524 if (sc.function != NULL)
2525 {
2526 resolved |= eSymbolContextFunction;
2527
2528 if (resolve_scope & eSymbolContextBlock)
2529 {
2530 Block& block = sc.function->GetBlock (true);
2531
2532 if (block_die != NULL)
2533 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2534 else
2535 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2536 if (sc.block)
2537 resolved |= eSymbolContextBlock;
2538 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002539 }
2540 }
Greg Clayton6ab80132012-12-12 17:30:52 +00002541
2542 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table)
2543 {
2544 LineTable *line_table = sc.comp_unit->GetLineTable();
2545 if (line_table != NULL)
2546 {
Greg Clayton9422dd62013-03-04 21:46:16 +00002547 // And address that makes it into this function should be in terms
2548 // of this debug file if there is no debug map, or it will be an
2549 // address in the .o file which needs to be fixed up to be in terms
2550 // of the debug map executable. Either way, calling FixupAddress()
2551 // will work for us.
2552 Address exe_so_addr (so_addr);
2553 if (FixupAddress(exe_so_addr))
Greg Clayton6ab80132012-12-12 17:30:52 +00002554 {
Greg Clayton9422dd62013-03-04 21:46:16 +00002555 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry))
Greg Clayton6ab80132012-12-12 17:30:52 +00002556 {
2557 resolved |= eSymbolContextLineEntry;
2558 }
2559 }
Greg Clayton6ab80132012-12-12 17:30:52 +00002560 }
2561 }
2562
2563 if (force_check_line_table && !(resolved & eSymbolContextLineEntry))
2564 {
2565 // We might have had a compile unit that had discontiguous
2566 // address ranges where the gaps are symbols that don't have
2567 // any debug info. Discontiguous compile unit address ranges
2568 // should only happen when there aren't other functions from
2569 // other compile units in these gaps. This helps keep the size
2570 // of the aranges down.
2571 sc.comp_unit = NULL;
2572 resolved &= ~eSymbolContextCompUnit;
2573 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002574 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002575 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002576 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002577 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
2578 cu_offset,
2579 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002580 }
2581 }
2582 }
2583 }
2584 }
2585 return resolved;
2586}
2587
2588
2589
2590uint32_t
2591SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2592{
2593 const uint32_t prev_size = sc_list.GetSize();
2594 if (resolve_scope & eSymbolContextCompUnit)
2595 {
2596 DWARFDebugInfo* debug_info = DebugInfo();
2597 if (debug_info)
2598 {
2599 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002600 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002601
Greg Clayton53eb1c22012-04-02 22:59:12 +00002602 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002603 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002604 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton1f746072012-08-29 21:13:06 +00002605 const bool full_match = file_spec.GetDirectory();
2606 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 +00002607 if (check_inlines || file_spec_matches_cu_file_spec)
2608 {
2609 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002610 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002611 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002612 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002613 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002614
Greg Clayton526a4ae2012-05-16 22:09:01 +00002615 // If we are looking for inline functions only and we don't
2616 // find it in the support files, we are done.
2617 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002618 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002619 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
2620 if (file_idx == UINT32_MAX)
2621 continue;
2622 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002623
Greg Clayton526a4ae2012-05-16 22:09:01 +00002624 if (line != 0)
2625 {
2626 LineTable *line_table = sc.comp_unit->GetLineTable();
2627
2628 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002629 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002630 // We will have already looked up the file index if
2631 // we are searching for inline entries.
2632 if (!check_inlines)
2633 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002634
Greg Clayton526a4ae2012-05-16 22:09:01 +00002635 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002636 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002637 uint32_t found_line;
2638 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2639 found_line = sc.line_entry.line;
2640
2641 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002642 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002643 sc.function = NULL;
2644 sc.block = NULL;
2645 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002646 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002647 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2648 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002649 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002650 DWARFDebugInfoEntry *function_die = NULL;
2651 DWARFDebugInfoEntry *block_die = NULL;
2652 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002653
Greg Clayton526a4ae2012-05-16 22:09:01 +00002654 if (function_die != NULL)
2655 {
2656 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2657 if (sc.function == NULL)
2658 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2659 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002660
Greg Clayton526a4ae2012-05-16 22:09:01 +00002661 if (sc.function != NULL)
2662 {
2663 Block& block = sc.function->GetBlock (true);
2664
2665 if (block_die != NULL)
2666 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2667 else
2668 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2669 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002670 }
2671 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002672
Greg Clayton526a4ae2012-05-16 22:09:01 +00002673 sc_list.Append(sc);
2674 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2675 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002676 }
2677 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002678 else if (file_spec_matches_cu_file_spec && !check_inlines)
2679 {
2680 // only append the context if we aren't looking for inline call sites
2681 // by file and line and if the file spec matches that of the compile unit
2682 sc_list.Append(sc);
2683 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002684 }
2685 else if (file_spec_matches_cu_file_spec && !check_inlines)
2686 {
2687 // only append the context if we aren't looking for inline call sites
2688 // by file and line and if the file spec matches that of the compile unit
2689 sc_list.Append(sc);
2690 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002691
Greg Clayton526a4ae2012-05-16 22:09:01 +00002692 if (!check_inlines)
2693 break;
2694 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002695 }
2696 }
2697 }
2698 }
2699 return sc_list.GetSize() - prev_size;
2700}
2701
2702void
2703SymbolFileDWARF::Index ()
2704{
2705 if (m_indexed)
2706 return;
2707 m_indexed = true;
2708 Timer scoped_timer (__PRETTY_FUNCTION__,
2709 "SymbolFileDWARF::Index (%s)",
2710 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2711
2712 DWARFDebugInfo* debug_info = DebugInfo();
2713 if (debug_info)
2714 {
2715 uint32_t cu_idx = 0;
2716 const uint32_t num_compile_units = GetNumCompileUnits();
2717 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2718 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002719 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002720
Greg Clayton53eb1c22012-04-02 22:59:12 +00002721 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002722
Greg Clayton53eb1c22012-04-02 22:59:12 +00002723 dwarf_cu->Index (cu_idx,
2724 m_function_basename_index,
2725 m_function_fullname_index,
2726 m_function_method_index,
2727 m_function_selector_index,
2728 m_objc_class_selectors_index,
2729 m_global_index,
2730 m_type_index,
2731 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002732
2733 // Keep memory down by clearing DIEs if this generate function
2734 // caused them to be parsed
2735 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002736 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002737 }
2738
Greg Claytond4a2b372011-09-12 23:21:58 +00002739 m_function_basename_index.Finalize();
2740 m_function_fullname_index.Finalize();
2741 m_function_method_index.Finalize();
2742 m_function_selector_index.Finalize();
2743 m_objc_class_selectors_index.Finalize();
2744 m_global_index.Finalize();
2745 m_type_index.Finalize();
2746 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002747
Greg Clayton24739922010-10-13 03:15:28 +00002748#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002749 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002750 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002751 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2752 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002753 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2754 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2755 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2756 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2757 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2758 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002759 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002760 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002761#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002762 }
2763}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002764
2765bool
2766SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2767{
2768 if (namespace_decl == NULL)
2769 {
2770 // Invalid namespace decl which means we aren't matching only things
2771 // in this symbol file, so return true to indicate it matches this
2772 // symbol file.
2773 return true;
2774 }
2775
2776 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2777
2778 if (namespace_ast == NULL)
2779 return true; // No AST in the "namespace_decl", return true since it
2780 // could then match any symbol file, including this one
2781
2782 if (namespace_ast == GetClangASTContext().getASTContext())
2783 return true; // The ASTs match, return true
2784
2785 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002786 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2787
2788 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002789 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002790
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002791 return false;
2792}
2793
Greg Clayton2506a7a2011-10-12 23:34:26 +00002794bool
2795SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2796 DWARFCompileUnit* cu,
2797 const DWARFDebugInfoEntry* die)
2798{
2799 // No namespace specified, so the answesr i
2800 if (namespace_decl == NULL)
2801 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002802
2803 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002804
Greg Clayton437a1352012-04-09 22:43:43 +00002805 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
2806 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002807 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00002808 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00002809 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00002810
Greg Clayton2506a7a2011-10-12 23:34:26 +00002811 if (clang_namespace_decl)
2812 {
2813 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002814 {
2815 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002816 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002817 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002818 }
2819
Greg Clayton437a1352012-04-09 22:43:43 +00002820 if (clang_namespace_decl == die_clang_decl_ctx)
2821 return true;
2822 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00002823 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00002824 }
2825 else
2826 {
2827 // We have a namespace_decl that was not NULL but it contained
2828 // a NULL "clang::NamespaceDecl", so this means the global namespace
2829 // So as long the the contained decl context DIE isn't a namespace
2830 // we should be ok.
2831 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2832 return true;
2833 }
2834 }
Sean Callananebe60672011-10-13 21:50:33 +00002835
2836 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002837 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002838
Greg Clayton2506a7a2011-10-12 23:34:26 +00002839 return false;
2840}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002841uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002842SymbolFileDWARF::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 +00002843{
Greg Clayton21f2a492011-10-06 00:09:08 +00002844 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2845
2846 if (log)
2847 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002848 GetObjectFile()->GetModule()->LogMessage (log.get(),
2849 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2850 name.GetCString(),
2851 namespace_decl,
2852 append,
2853 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002854 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002855
2856 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2857 return 0;
2858
Greg Claytonc685f8e2010-09-15 04:15:46 +00002859 DWARFDebugInfo* info = DebugInfo();
2860 if (info == NULL)
2861 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002862
2863 // If we aren't appending the results to this list, then clear the list
2864 if (!append)
2865 variables.Clear();
2866
2867 // Remember how many variables are in the list before we search in case
2868 // we are appending the results to a variable list.
2869 const uint32_t original_size = variables.GetSize();
2870
Greg Claytond4a2b372011-09-12 23:21:58 +00002871 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002872
Greg Clayton97fbc342011-10-20 22:30:33 +00002873 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002874 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002875 if (m_apple_names_ap.get())
2876 {
2877 const char *name_cstr = name.GetCString();
2878 const char *base_name_start;
2879 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002880
Greg Clayton97fbc342011-10-20 22:30:33 +00002881 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2882 base_name_start = name_cstr;
2883
2884 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2885 }
Greg Clayton7f995132011-10-04 22:41:51 +00002886 }
2887 else
2888 {
2889 // Index the DWARF if we haven't already
2890 if (!m_indexed)
2891 Index ();
2892
2893 m_global_index.Find (name, die_offsets);
2894 }
2895
Greg Clayton437a1352012-04-09 22:43:43 +00002896 const size_t num_die_matches = die_offsets.size();
2897 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002898 {
Greg Clayton7f995132011-10-04 22:41:51 +00002899 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002900 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002901 assert (sc.module_sp);
2902
Greg Claytond4a2b372011-09-12 23:21:58 +00002903 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002904 DWARFCompileUnit* dwarf_cu = NULL;
2905 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00002906 bool done = false;
2907 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002908 {
2909 const dw_offset_t die_offset = die_offsets[i];
2910 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002911
Greg Clayton95d87902011-11-11 03:16:25 +00002912 if (die)
2913 {
Greg Clayton437a1352012-04-09 22:43:43 +00002914 switch (die->Tag())
2915 {
2916 default:
2917 case DW_TAG_subprogram:
2918 case DW_TAG_inlined_subroutine:
2919 case DW_TAG_try_block:
2920 case DW_TAG_catch_block:
2921 break;
2922
2923 case DW_TAG_variable:
2924 {
2925 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Greg Clayton437a1352012-04-09 22:43:43 +00002926
2927 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2928 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002929
Greg Clayton437a1352012-04-09 22:43:43 +00002930 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002931
Greg Clayton437a1352012-04-09 22:43:43 +00002932 if (variables.GetSize() - original_size >= max_matches)
2933 done = true;
2934 }
2935 break;
2936 }
Greg Clayton95d87902011-11-11 03:16:25 +00002937 }
2938 else
2939 {
2940 if (m_using_apple_tables)
2941 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002942 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2943 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002944 }
2945 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002946 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002947 }
2948
2949 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002950 const uint32_t num_matches = variables.GetSize() - original_size;
2951 if (log && num_matches > 0)
2952 {
2953 GetObjectFile()->GetModule()->LogMessage (log.get(),
2954 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
2955 name.GetCString(),
2956 namespace_decl,
2957 append,
2958 max_matches,
2959 num_matches);
2960 }
2961 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002962}
2963
2964uint32_t
2965SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2966{
Greg Clayton21f2a492011-10-06 00:09:08 +00002967 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2968
2969 if (log)
2970 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002971 GetObjectFile()->GetModule()->LogMessage (log.get(),
2972 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2973 regex.GetText(),
2974 append,
2975 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002976 }
2977
Greg Claytonc685f8e2010-09-15 04:15:46 +00002978 DWARFDebugInfo* info = DebugInfo();
2979 if (info == NULL)
2980 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002981
2982 // If we aren't appending the results to this list, then clear the list
2983 if (!append)
2984 variables.Clear();
2985
2986 // Remember how many variables are in the list before we search in case
2987 // we are appending the results to a variable list.
2988 const uint32_t original_size = variables.GetSize();
2989
Greg Clayton7f995132011-10-04 22:41:51 +00002990 DIEArray die_offsets;
2991
Greg Clayton97fbc342011-10-20 22:30:33 +00002992 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002993 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002994 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002995 {
2996 DWARFMappedHash::DIEInfoArray hash_data_array;
2997 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2998 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2999 }
Greg Clayton7f995132011-10-04 22:41:51 +00003000 }
3001 else
3002 {
3003 // Index the DWARF if we haven't already
3004 if (!m_indexed)
3005 Index ();
3006
3007 m_global_index.Find (regex, die_offsets);
3008 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003009
Greg Claytonc685f8e2010-09-15 04:15:46 +00003010 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00003011 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00003012 assert (sc.module_sp);
3013
Greg Claytond4a2b372011-09-12 23:21:58 +00003014 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00003015 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003016 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003017 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003018 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003019 DWARFDebugInfo* debug_info = DebugInfo();
3020 for (size_t i=0; i<num_matches; ++i)
3021 {
3022 const dw_offset_t die_offset = die_offsets[i];
3023 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00003024
3025 if (die)
3026 {
3027 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003028
Greg Clayton95d87902011-11-11 03:16:25 +00003029 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003030
Greg Clayton95d87902011-11-11 03:16:25 +00003031 if (variables.GetSize() - original_size >= max_matches)
3032 break;
3033 }
3034 else
3035 {
3036 if (m_using_apple_tables)
3037 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003038 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
3039 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00003040 }
3041 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003042 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003043 }
3044
3045 // Return the number of variable that were appended to the list
3046 return variables.GetSize() - original_size;
3047}
3048
Greg Claytonaa044962011-10-13 00:59:38 +00003049
Jim Ingham4cda6e02011-10-07 22:23:45 +00003050bool
3051SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
3052 DWARFCompileUnit *&dwarf_cu,
3053 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00003054{
Greg Claytonaa044962011-10-13 00:59:38 +00003055 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3056 return ResolveFunction (dwarf_cu, die, sc_list);
3057}
3058
3059
3060bool
3061SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
3062 const DWARFDebugInfoEntry *die,
3063 SymbolContextList& sc_list)
3064{
Greg Clayton9e315582011-09-02 04:03:59 +00003065 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00003066
3067 if (die == NULL)
3068 return false;
3069
Jim Ingham4cda6e02011-10-07 22:23:45 +00003070 // If we were passed a die that is not a function, just return false...
3071 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
3072 return false;
3073
3074 const DWARFDebugInfoEntry* inlined_die = NULL;
3075 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00003076 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003077 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00003078
Jim Ingham4cda6e02011-10-07 22:23:45 +00003079 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003080 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003081 if (die->Tag() == DW_TAG_subprogram)
3082 break;
Greg Clayton9e315582011-09-02 04:03:59 +00003083 }
3084 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003085 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00003086 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003087 {
3088 Address addr;
3089 // Parse all blocks if needed
3090 if (inlined_die)
3091 {
Greg Clayton81c22f62011-10-19 18:09:39 +00003092 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00003093 assert (sc.block != NULL);
3094 if (sc.block->GetStartAddress (addr) == false)
3095 addr.Clear();
3096 }
3097 else
3098 {
3099 sc.block = NULL;
3100 addr = sc.function->GetAddressRange().GetBaseAddress();
3101 }
3102
3103 if (addr.IsValid())
3104 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003105 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00003106 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003107 }
3108 }
3109
Greg Claytonaa044962011-10-13 00:59:38 +00003110 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00003111}
3112
Greg Clayton7f995132011-10-04 22:41:51 +00003113void
3114SymbolFileDWARF::FindFunctions (const ConstString &name,
3115 const NameToDIE &name_to_die,
3116 SymbolContextList& sc_list)
3117{
Greg Claytond4a2b372011-09-12 23:21:58 +00003118 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003119 if (name_to_die.Find (name, die_offsets))
3120 {
3121 ParseFunctions (die_offsets, sc_list);
3122 }
3123}
3124
3125
3126void
3127SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3128 const NameToDIE &name_to_die,
3129 SymbolContextList& sc_list)
3130{
3131 DIEArray die_offsets;
3132 if (name_to_die.Find (regex, die_offsets))
3133 {
3134 ParseFunctions (die_offsets, sc_list);
3135 }
3136}
3137
3138
3139void
3140SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3141 const DWARFMappedHash::MemoryTable &memory_table,
3142 SymbolContextList& sc_list)
3143{
3144 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00003145 DWARFMappedHash::DIEInfoArray hash_data_array;
3146 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00003147 {
Greg Claytond1767f02011-12-08 02:13:16 +00003148 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00003149 ParseFunctions (die_offsets, sc_list);
3150 }
3151}
3152
3153void
3154SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
3155 SymbolContextList& sc_list)
3156{
3157 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003158 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00003159 {
Greg Clayton7f995132011-10-04 22:41:51 +00003160 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00003161
3162 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003163 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00003164 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003165 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00003166 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003167 }
3168 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00003169}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003170
Jim Ingham4cda6e02011-10-07 22:23:45 +00003171bool
3172SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
3173 const DWARFCompileUnit *dwarf_cu,
3174 uint32_t name_type_mask,
3175 const char *partial_name,
3176 const char *base_name_start,
3177 const char *base_name_end)
3178{
Greg Claytonfbea0f62012-11-15 18:05:43 +00003179 // If we are looking only for methods, throw away all the ones that are or aren't in C++ classes:
3180 if (name_type_mask == eFunctionNameTypeMethod || name_type_mask == eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003181 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003182 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
3183 if (!containing_decl_ctx)
3184 return false;
3185
3186 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
3187
Greg Claytonfbea0f62012-11-15 18:05:43 +00003188 if (name_type_mask == eFunctionNameTypeMethod)
3189 {
3190 if (is_cxx_method == false)
3191 return false;
3192 }
3193
3194 if (name_type_mask == eFunctionNameTypeBase)
3195 {
3196 if (is_cxx_method == true)
3197 return false;
3198 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003199 }
3200
3201 // Now we need to check whether the name we got back for this type matches the extra specifications
3202 // that were in the name we're looking up:
3203 if (base_name_start != partial_name || *base_name_end != '\0')
3204 {
3205 // First see if the stuff to the left matches the full name. To do that let's see if
3206 // we can pull out the mips linkage name attribute:
3207
3208 Mangled best_name;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003209 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonfbea0f62012-11-15 18:05:43 +00003210 DWARFFormValue form_value;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003211 die->GetAttributes(this, dwarf_cu, NULL, attributes);
3212 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
Greg Clayton71415542012-12-08 00:24:40 +00003213 if (idx == UINT32_MAX)
3214 idx = attributes.FindAttributeIndex(DW_AT_linkage_name);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003215 if (idx != UINT32_MAX)
3216 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003217 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3218 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003219 const char *mangled_name = form_value.AsCString(&get_debug_str_data());
3220 if (mangled_name)
3221 best_name.SetValue (ConstString(mangled_name), true);
3222 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003223 }
Greg Claytonfbea0f62012-11-15 18:05:43 +00003224
3225 if (!best_name)
3226 {
3227 idx = attributes.FindAttributeIndex(DW_AT_name);
3228 if (idx != UINT32_MAX && attributes.ExtractFormValueAtIndex(this, idx, form_value))
3229 {
3230 const char *name = form_value.AsCString(&get_debug_str_data());
3231 best_name.SetValue (ConstString(name), false);
3232 }
3233 }
3234
3235 if (best_name.GetDemangledName())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003236 {
3237 const char *demangled = best_name.GetDemangledName().GetCString();
3238 if (demangled)
3239 {
3240 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003241 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3242 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003243 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003244 else
3245 {
3246 // Sort out the case where our name is something like "Process::Destroy" and the match is
3247 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3248 // namespace boundaries...
3249
3250 if (partial_name[0] == ':' && partial_name[1] == ':')
3251 {
3252 // The partial name was already on a namespace boundary so all matches are good.
3253 return true;
3254 }
3255 else if (partial_in_demangled == demangled)
3256 {
3257 // They both start the same, so this is an good match.
3258 return true;
3259 }
3260 else
3261 {
3262 if (partial_in_demangled - demangled == 1)
3263 {
3264 // Only one character difference, can't be a namespace boundary...
3265 return false;
3266 }
3267 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3268 {
3269 // We are on a namespace boundary, so this is also good.
3270 return true;
3271 }
3272 else
3273 return false;
3274 }
3275 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003276 }
3277 }
3278 }
3279
3280 return true;
3281}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003282
Greg Clayton0c5cd902010-06-28 21:30:43 +00003283uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003284SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003285 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003286 uint32_t name_type_mask,
3287 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003288 bool append,
3289 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003290{
3291 Timer scoped_timer (__PRETTY_FUNCTION__,
3292 "SymbolFileDWARF::FindFunctions (name = '%s')",
3293 name.AsCString());
3294
Greg Clayton21f2a492011-10-06 00:09:08 +00003295 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3296
3297 if (log)
3298 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003299 GetObjectFile()->GetModule()->LogMessage (log.get(),
3300 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3301 name.GetCString(),
3302 name_type_mask,
3303 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003304 }
3305
Greg Clayton0c5cd902010-06-28 21:30:43 +00003306 // If we aren't appending the results to this list, then clear the list
3307 if (!append)
3308 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003309
3310 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3311 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003312
3313 // If name is empty then we won't find anything.
3314 if (name.IsEmpty())
3315 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003316
3317 // Remember how many sc_list are in the list before we search in case
3318 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003319
Greg Clayton9e315582011-09-02 04:03:59 +00003320 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00003321
Jim Ingham4cda6e02011-10-07 22:23:45 +00003322 const char *name_cstr = name.GetCString();
3323 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
3324 const char *base_name_start = name_cstr;
3325 const char *base_name_end = name_cstr + strlen(name_cstr);
3326
3327 if (name_type_mask & eFunctionNameTypeAuto)
3328 {
3329 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
3330 effective_name_type_mask = eFunctionNameTypeFull;
3331 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
3332 effective_name_type_mask = eFunctionNameTypeFull;
3333 else
3334 {
3335 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3336 effective_name_type_mask |= eFunctionNameTypeSelector;
3337
3338 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3339 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
3340 }
3341 }
3342 else
3343 {
3344 effective_name_type_mask = name_type_mask;
3345 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
3346 {
3347 // If they've asked for a CPP method or function name and it can't be that, we don't
3348 // even need to search for CPP methods or names.
3349 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3350 {
3351 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
3352 if (effective_name_type_mask == eFunctionNameTypeNone)
3353 return 0;
3354 }
3355 }
3356
3357 if (effective_name_type_mask & eFunctionNameTypeSelector)
3358 {
3359 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3360 {
3361 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
3362 if (effective_name_type_mask == eFunctionNameTypeNone)
3363 return 0;
3364 }
3365 }
3366 }
3367
3368 DWARFDebugInfo* info = DebugInfo();
3369 if (info == NULL)
3370 return 0;
3371
Greg Claytonaa044962011-10-13 00:59:38 +00003372 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003373 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003374 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003375 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003376 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003377
3378 DIEArray die_offsets;
3379
3380 uint32_t num_matches = 0;
3381
3382 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003383 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003384 // If they asked for the full name, match what they typed. At some point we may
3385 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3386 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003387 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003388 for (uint32_t i = 0; i < num_matches; i++)
3389 {
Greg Clayton95d87902011-11-11 03:16:25 +00003390 const dw_offset_t die_offset = die_offsets[i];
3391 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003392 if (die)
3393 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003394 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3395 continue;
3396
Sean Callanan9df05fb2012-02-10 22:52:19 +00003397 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3398 continue;
3399
Greg Claytonaa044962011-10-13 00:59:38 +00003400 ResolveFunction (dwarf_cu, die, sc_list);
3401 }
Greg Clayton95d87902011-11-11 03:16:25 +00003402 else
3403 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003404 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3405 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003406 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003407 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003408 }
3409 else
3410 {
3411 if (effective_name_type_mask & eFunctionNameTypeSelector)
3412 {
3413 if (namespace_decl && *namespace_decl)
3414 return 0; // no selectors in namespaces
3415
3416 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3417 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3418 // and if it is an ObjC method name, we're good.
3419
3420 for (uint32_t i = 0; i < num_matches; i++)
3421 {
Greg Clayton95d87902011-11-11 03:16:25 +00003422 const dw_offset_t die_offset = die_offsets[i];
3423 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003424 if (die)
3425 {
3426 const char *die_name = die->GetName(this, dwarf_cu);
3427 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003428 {
3429 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3430 continue;
3431
Greg Clayton97fbc342011-10-20 22:30:33 +00003432 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003433 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003434 }
Greg Clayton95d87902011-11-11 03:16:25 +00003435 else
3436 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003437 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3438 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003439 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003440 }
3441 die_offsets.clear();
3442 }
3443
3444 if (effective_name_type_mask & eFunctionNameTypeMethod
3445 || effective_name_type_mask & eFunctionNameTypeBase)
3446 {
3447 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3448 (namespace_decl && *namespace_decl))
3449 return 0; // no methods in namespaces
3450
3451 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3452 // extract the base name, look that up, and if there is any other information in the name we were
3453 // passed in we have to post-filter based on that.
3454
3455 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3456 std::string base_name(base_name_start, base_name_end - base_name_start);
3457 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3458
3459 for (uint32_t i = 0; i < num_matches; i++)
3460 {
Greg Clayton95d87902011-11-11 03:16:25 +00003461 const dw_offset_t die_offset = die_offsets[i];
3462 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003463 if (die)
3464 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003465 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3466 continue;
3467
Greg Clayton97fbc342011-10-20 22:30:33 +00003468 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3469 continue;
3470
Greg Claytonfbea0f62012-11-15 18:05:43 +00003471 if (!FunctionDieMatchesPartialName(die,
Greg Clayton97fbc342011-10-20 22:30:33 +00003472 dwarf_cu,
Greg Claytonfbea0f62012-11-15 18:05:43 +00003473 effective_name_type_mask,
Greg Clayton97fbc342011-10-20 22:30:33 +00003474 name_cstr,
3475 base_name_start,
3476 base_name_end))
3477 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003478
Greg Clayton97fbc342011-10-20 22:30:33 +00003479 // If we get to here, the die is good, and we should add it:
3480 ResolveFunction (dwarf_cu, die, sc_list);
3481 }
Greg Clayton95d87902011-11-11 03:16:25 +00003482 else
3483 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003484 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3485 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003486 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003487 }
3488 die_offsets.clear();
3489 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003490 }
3491 }
Greg Clayton7f995132011-10-04 22:41:51 +00003492 }
3493 else
3494 {
3495
3496 // Index the DWARF if we haven't already
3497 if (!m_indexed)
3498 Index ();
3499
Greg Clayton7f995132011-10-04 22:41:51 +00003500 if (name_type_mask & eFunctionNameTypeFull)
3501 FindFunctions (name, m_function_fullname_index, sc_list);
3502
Jim Ingham4cda6e02011-10-07 22:23:45 +00003503 std::string base_name(base_name_start, base_name_end - base_name_start);
3504 ConstString base_name_const(base_name.c_str());
3505 DIEArray die_offsets;
3506 DWARFCompileUnit *dwarf_cu = NULL;
3507
3508 if (effective_name_type_mask & eFunctionNameTypeBase)
3509 {
3510 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003511 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003512 {
Greg Claytonaa044962011-10-13 00:59:38 +00003513 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3514 if (die)
3515 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003516 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3517 continue;
3518
Sean Callanan213fdb82011-10-13 01:49:10 +00003519 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3520 continue;
3521
Greg Claytonaa044962011-10-13 00:59:38 +00003522 if (!FunctionDieMatchesPartialName(die,
3523 dwarf_cu,
Greg Claytonfbea0f62012-11-15 18:05:43 +00003524 eFunctionNameTypeBase,
Greg Claytonaa044962011-10-13 00:59:38 +00003525 name_cstr,
3526 base_name_start,
3527 base_name_end))
3528 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003529
Greg Claytonaa044962011-10-13 00:59:38 +00003530 // If we get to here, the die is good, and we should add it:
3531 ResolveFunction (dwarf_cu, die, sc_list);
3532 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003533 }
3534 die_offsets.clear();
3535 }
3536
Jim Inghamea8005a2011-10-11 01:18:11 +00003537 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003538 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003539 if (namespace_decl && *namespace_decl)
3540 return 0; // no methods in namespaces
3541
Jim Ingham4cda6e02011-10-07 22:23:45 +00003542 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3543 {
Greg Claytonaa044962011-10-13 00:59:38 +00003544 for (uint32_t i = 0; i < num_base; i++)
3545 {
3546 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3547 if (die)
3548 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003549 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3550 continue;
3551
3552 if (!FunctionDieMatchesPartialName(die,
Greg Claytonaa044962011-10-13 00:59:38 +00003553 dwarf_cu,
Greg Claytonfbea0f62012-11-15 18:05:43 +00003554 eFunctionNameTypeMethod,
Greg Claytonaa044962011-10-13 00:59:38 +00003555 name_cstr,
3556 base_name_start,
3557 base_name_end))
3558 continue;
3559
3560 // If we get to here, the die is good, and we should add it:
3561 ResolveFunction (dwarf_cu, die, sc_list);
3562 }
3563 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003564 }
3565 die_offsets.clear();
3566 }
Greg Clayton7f995132011-10-04 22:41:51 +00003567
Sean Callanan213fdb82011-10-13 01:49:10 +00003568 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003569 {
Greg Clayton7f995132011-10-04 22:41:51 +00003570 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003571 }
3572
Greg Clayton4d01ace2011-09-29 16:58:15 +00003573 }
3574
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003575 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003576 const uint32_t num_matches = sc_list.GetSize() - original_size;
3577
3578 if (log && num_matches > 0)
3579 {
3580 GetObjectFile()->GetModule()->LogMessage (log.get(),
3581 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u",
3582 name.GetCString(),
3583 name_type_mask,
3584 append,
3585 num_matches);
3586 }
3587 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003588}
3589
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003590uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003591SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003592{
3593 Timer scoped_timer (__PRETTY_FUNCTION__,
3594 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3595 regex.GetText());
3596
Greg Clayton21f2a492011-10-06 00:09:08 +00003597 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3598
3599 if (log)
3600 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003601 GetObjectFile()->GetModule()->LogMessage (log.get(),
3602 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3603 regex.GetText(),
3604 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003605 }
3606
3607
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003608 // If we aren't appending the results to this list, then clear the list
3609 if (!append)
3610 sc_list.Clear();
3611
3612 // Remember how many sc_list are in the list before we search in case
3613 // we are appending the results to a variable list.
3614 uint32_t original_size = sc_list.GetSize();
3615
Greg Clayton97fbc342011-10-20 22:30:33 +00003616 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003617 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003618 if (m_apple_names_ap.get())
3619 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003620 }
3621 else
3622 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003623 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003624 if (!m_indexed)
3625 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003626
Greg Clayton7f995132011-10-04 22:41:51 +00003627 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003628
Greg Clayton7f995132011-10-04 22:41:51 +00003629 FindFunctions (regex, m_function_fullname_index, sc_list);
3630 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003631
3632 // Return the number of variable that were appended to the list
3633 return sc_list.GetSize() - original_size;
3634}
Jim Ingham318c9f22011-08-26 19:44:13 +00003635
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003636uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003637SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3638 const ConstString &name,
3639 const lldb_private::ClangNamespaceDecl *namespace_decl,
3640 bool append,
3641 uint32_t max_matches,
3642 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003643{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003644 DWARFDebugInfo* info = DebugInfo();
3645 if (info == NULL)
3646 return 0;
3647
Greg Clayton21f2a492011-10-06 00:09:08 +00003648 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3649
3650 if (log)
3651 {
Greg Clayton437a1352012-04-09 22:43:43 +00003652 if (namespace_decl)
3653 {
3654 GetObjectFile()->GetModule()->LogMessage (log.get(),
3655 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
3656 name.GetCString(),
3657 namespace_decl->GetNamespaceDecl(),
3658 namespace_decl->GetQualifiedName().c_str(),
3659 append,
3660 max_matches);
3661 }
3662 else
3663 {
3664 GetObjectFile()->GetModule()->LogMessage (log.get(),
3665 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
3666 name.GetCString(),
3667 append,
3668 max_matches);
3669 }
Greg Clayton21f2a492011-10-06 00:09:08 +00003670 }
3671
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003672 // If we aren't appending the results to this list, then clear the list
3673 if (!append)
3674 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003675
3676 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3677 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003678
Greg Claytond4a2b372011-09-12 23:21:58 +00003679 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003680
Greg Clayton97fbc342011-10-20 22:30:33 +00003681 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003682 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003683 if (m_apple_types_ap.get())
3684 {
3685 const char *name_cstr = name.GetCString();
3686 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3687 }
Greg Clayton7f995132011-10-04 22:41:51 +00003688 }
3689 else
3690 {
3691 if (!m_indexed)
3692 Index ();
3693
3694 m_type_index.Find (name, die_offsets);
3695 }
3696
Greg Clayton437a1352012-04-09 22:43:43 +00003697 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003698
Greg Clayton437a1352012-04-09 22:43:43 +00003699 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003700 {
Greg Clayton7f995132011-10-04 22:41:51 +00003701 const uint32_t initial_types_size = types.GetSize();
3702 DWARFCompileUnit* dwarf_cu = NULL;
3703 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003704 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003705 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003706 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003707 const dw_offset_t die_offset = die_offsets[i];
3708 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3709
Greg Clayton95d87902011-11-11 03:16:25 +00003710 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003711 {
Greg Clayton95d87902011-11-11 03:16:25 +00003712 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3713 continue;
3714
3715 Type *matching_type = ResolveType (dwarf_cu, die);
3716 if (matching_type)
3717 {
3718 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003719 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003720 if (types.GetSize() >= max_matches)
3721 break;
3722 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003723 }
Greg Clayton95d87902011-11-11 03:16:25 +00003724 else
3725 {
3726 if (m_using_apple_tables)
3727 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003728 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3729 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003730 }
3731 }
3732
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003733 }
Greg Clayton437a1352012-04-09 22:43:43 +00003734 const uint32_t num_matches = types.GetSize() - initial_types_size;
3735 if (log && num_matches)
3736 {
3737 if (namespace_decl)
3738 {
3739 GetObjectFile()->GetModule()->LogMessage (log.get(),
3740 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
3741 name.GetCString(),
3742 namespace_decl->GetNamespaceDecl(),
3743 namespace_decl->GetQualifiedName().c_str(),
3744 append,
3745 max_matches,
3746 num_matches);
3747 }
3748 else
3749 {
3750 GetObjectFile()->GetModule()->LogMessage (log.get(),
3751 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
3752 name.GetCString(),
3753 append,
3754 max_matches,
3755 num_matches);
3756 }
3757 }
3758 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003759 }
Greg Clayton7f995132011-10-04 22:41:51 +00003760 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003761}
3762
3763
Greg Clayton526e5af2010-11-13 03:52:47 +00003764ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003765SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003766 const ConstString &name,
3767 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003768{
Greg Clayton21f2a492011-10-06 00:09:08 +00003769 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3770
3771 if (log)
3772 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003773 GetObjectFile()->GetModule()->LogMessage (log.get(),
3774 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3775 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003776 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003777
3778 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3779 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003780
Greg Clayton526e5af2010-11-13 03:52:47 +00003781 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003782 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003783 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003784 {
Greg Clayton7f995132011-10-04 22:41:51 +00003785 DIEArray die_offsets;
3786
Greg Clayton526e5af2010-11-13 03:52:47 +00003787 // Index if we already haven't to make sure the compile units
3788 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003789 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003790 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003791 if (m_apple_namespaces_ap.get())
3792 {
3793 const char *name_cstr = name.GetCString();
3794 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3795 }
Greg Clayton7f995132011-10-04 22:41:51 +00003796 }
3797 else
3798 {
3799 if (!m_indexed)
3800 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003801
Greg Clayton7f995132011-10-04 22:41:51 +00003802 m_namespace_index.Find (name, die_offsets);
3803 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003804
3805 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003806 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003807 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003808 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003809 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003810 DWARFDebugInfo* debug_info = DebugInfo();
3811 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003812 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003813 const dw_offset_t die_offset = die_offsets[i];
3814 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003815
Greg Clayton95d87902011-11-11 03:16:25 +00003816 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003817 {
Greg Clayton95d87902011-11-11 03:16:25 +00003818 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3819 continue;
3820
3821 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3822 if (clang_namespace_decl)
3823 {
3824 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3825 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003826 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003827 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003828 }
Greg Clayton95d87902011-11-11 03:16:25 +00003829 else
3830 {
3831 if (m_using_apple_tables)
3832 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003833 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3834 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003835 }
3836 }
3837
Greg Clayton526e5af2010-11-13 03:52:47 +00003838 }
3839 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003840 }
Greg Clayton437a1352012-04-09 22:43:43 +00003841 if (log && namespace_decl.GetNamespaceDecl())
3842 {
3843 GetObjectFile()->GetModule()->LogMessage (log.get(),
3844 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
3845 name.GetCString(),
3846 namespace_decl.GetNamespaceDecl(),
3847 namespace_decl.GetQualifiedName().c_str());
3848 }
3849
Greg Clayton526e5af2010-11-13 03:52:47 +00003850 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003851}
3852
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003853uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003854SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003855{
3856 // Remember how many sc_list are in the list before we search in case
3857 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003858 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003859
3860 const uint32_t num_die_offsets = die_offsets.size();
3861 // Parse all of the types we found from the pubtypes matches
3862 uint32_t i;
3863 uint32_t num_matches = 0;
3864 for (i = 0; i < num_die_offsets; ++i)
3865 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003866 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3867 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003868 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003869 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003870 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003871 ++num_matches;
3872 if (num_matches >= max_matches)
3873 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003874 }
3875 }
3876
3877 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003878 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003879}
3880
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003881
3882size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003883SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3884 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003885 DWARFCompileUnit* dwarf_cu,
3886 const DWARFDebugInfoEntry *parent_die,
3887 bool skip_artificial,
3888 bool &is_static,
3889 TypeList* type_list,
3890 std::vector<clang_type_t>& function_param_types,
3891 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003892 unsigned &type_quals,
3893 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003894{
3895 if (parent_die == NULL)
3896 return 0;
3897
Greg Claytond88d7592010-09-15 08:33:30 +00003898 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3899
Greg Clayton7fedea22010-11-16 02:10:54 +00003900 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003901 const DWARFDebugInfoEntry *die;
3902 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3903 {
3904 dw_tag_t tag = die->Tag();
3905 switch (tag)
3906 {
3907 case DW_TAG_formal_parameter:
3908 {
3909 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003910 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003911 if (num_attributes > 0)
3912 {
3913 const char *name = NULL;
3914 Declaration decl;
3915 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003916 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003917 // one of None, Auto, Register, Extern, Static, PrivateExtern
3918
Sean Callanane2ef6e32010-09-23 03:01:22 +00003919 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003920 uint32_t i;
3921 for (i=0; i<num_attributes; ++i)
3922 {
3923 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3924 DWARFFormValue form_value;
3925 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3926 {
3927 switch (attr)
3928 {
3929 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3930 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3931 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3932 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3933 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003934 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003935 case DW_AT_location:
3936 // if (form_value.BlockData())
3937 // {
3938 // const DataExtractor& debug_info_data = debug_info();
3939 // uint32_t block_length = form_value.Unsigned();
3940 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3941 // }
3942 // else
3943 // {
3944 // }
3945 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003946 case DW_AT_const_value:
3947 case DW_AT_default_value:
3948 case DW_AT_description:
3949 case DW_AT_endianity:
3950 case DW_AT_is_optional:
3951 case DW_AT_segment:
3952 case DW_AT_variable_parameter:
3953 default:
3954 case DW_AT_abstract_origin:
3955 case DW_AT_sibling:
3956 break;
3957 }
3958 }
3959 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003960
Greg Clayton0fffff52010-09-24 05:15:53 +00003961 bool skip = false;
3962 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003963 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003964 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003965 {
3966 // In order to determine if a C++ member function is
3967 // "const" we have to look at the const-ness of "this"...
3968 // Ugly, but that
3969 if (arg_idx == 0)
3970 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003971 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003972 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003973 // Often times compilers omit the "this" name for the
3974 // specification DIEs, so we can't rely upon the name
3975 // being in the formal parameter DIE...
3976 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003977 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003978 Type *this_type = ResolveTypeUID (param_type_die_offset);
3979 if (this_type)
3980 {
3981 uint32_t encoding_mask = this_type->GetEncodingMask();
3982 if (encoding_mask & Type::eEncodingIsPointerUID)
3983 {
3984 is_static = false;
3985
3986 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3987 type_quals |= clang::Qualifiers::Const;
3988 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3989 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003990 }
3991 }
3992 }
3993 }
3994 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003995 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003996 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003997 else
3998 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003999
Greg Clayton0fffff52010-09-24 05:15:53 +00004000 // HACK: Objective C formal parameters "self" and "_cmd"
4001 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00004002 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
4003 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00004004 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004005 switch (comp_unit->GetLanguage())
4006 {
4007 case eLanguageTypeObjC:
4008 case eLanguageTypeObjC_plus_plus:
4009 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
4010 skip = true;
4011 break;
4012 default:
4013 break;
4014 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004015 }
4016 }
4017 }
4018
4019 if (!skip)
4020 {
4021 Type *type = ResolveTypeUID(param_type_die_offset);
4022 if (type)
4023 {
Greg Claytonc93237c2010-10-01 20:48:32 +00004024 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00004025
Greg Clayton42ce2f32011-12-12 21:50:19 +00004026 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
4027 type->GetClangForwardType(),
4028 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00004029 assert(param_var_decl);
4030 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00004031
Jim Ingham379397632012-10-27 02:54:13 +00004032 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00004033 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004034 }
4035 }
Greg Clayton7fedea22010-11-16 02:10:54 +00004036 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004037 }
4038 break;
4039
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00004040 case DW_TAG_template_type_parameter:
4041 case DW_TAG_template_value_parameter:
4042 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
4043 break;
4044
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004045 default:
4046 break;
4047 }
4048 }
Greg Clayton7fedea22010-11-16 02:10:54 +00004049 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004050}
4051
4052size_t
4053SymbolFileDWARF::ParseChildEnumerators
4054(
4055 const SymbolContext& sc,
Greg Clayton3e067532013-03-05 23:54:39 +00004056 clang_type_t enumerator_clang_type,
4057 bool is_signed,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004058 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00004059 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004060 const DWARFDebugInfoEntry *parent_die
4061)
4062{
4063 if (parent_die == NULL)
4064 return 0;
4065
4066 size_t enumerators_added = 0;
4067 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00004068 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
4069
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004070 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4071 {
4072 const dw_tag_t tag = die->Tag();
4073 if (tag == DW_TAG_enumerator)
4074 {
4075 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004076 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004077 if (num_child_attributes > 0)
4078 {
4079 const char *name = NULL;
4080 bool got_value = false;
4081 int64_t enum_value = 0;
4082 Declaration decl;
4083
4084 uint32_t i;
4085 for (i=0; i<num_child_attributes; ++i)
4086 {
4087 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4088 DWARFFormValue form_value;
4089 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4090 {
4091 switch (attr)
4092 {
4093 case DW_AT_const_value:
4094 got_value = true;
Greg Clayton3e067532013-03-05 23:54:39 +00004095 if (is_signed)
4096 enum_value = form_value.Signed();
4097 else
4098 enum_value = form_value.Unsigned();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004099 break;
4100
4101 case DW_AT_name:
4102 name = form_value.AsCString(&get_debug_str_data());
4103 break;
4104
4105 case DW_AT_description:
4106 default:
4107 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4108 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4109 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4110 case DW_AT_sibling:
4111 break;
4112 }
4113 }
4114 }
4115
4116 if (name && name[0] && got_value)
4117 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004118 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
4119 enumerator_clang_type,
4120 decl,
4121 name,
4122 enum_value,
4123 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004124 ++enumerators_added;
4125 }
4126 }
4127 }
4128 }
4129 return enumerators_added;
4130}
4131
4132void
4133SymbolFileDWARF::ParseChildArrayInfo
4134(
4135 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004136 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004137 const DWARFDebugInfoEntry *parent_die,
4138 int64_t& first_index,
4139 std::vector<uint64_t>& element_orders,
4140 uint32_t& byte_stride,
4141 uint32_t& bit_stride
4142)
4143{
4144 if (parent_die == NULL)
4145 return;
4146
4147 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00004148 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004149 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4150 {
4151 const dw_tag_t tag = die->Tag();
4152 switch (tag)
4153 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004154 case DW_TAG_subrange_type:
4155 {
4156 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004157 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004158 if (num_child_attributes > 0)
4159 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004160 uint64_t num_elements = 0;
4161 uint64_t lower_bound = 0;
4162 uint64_t upper_bound = 0;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004163 bool upper_bound_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004164 uint32_t i;
4165 for (i=0; i<num_child_attributes; ++i)
4166 {
4167 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4168 DWARFFormValue form_value;
4169 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4170 {
4171 switch (attr)
4172 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004173 case DW_AT_name:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004174 break;
4175
4176 case DW_AT_count:
4177 num_elements = form_value.Unsigned();
4178 break;
4179
4180 case DW_AT_bit_stride:
4181 bit_stride = form_value.Unsigned();
4182 break;
4183
4184 case DW_AT_byte_stride:
4185 byte_stride = form_value.Unsigned();
4186 break;
4187
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004188 case DW_AT_lower_bound:
4189 lower_bound = form_value.Unsigned();
4190 break;
4191
4192 case DW_AT_upper_bound:
Greg Clayton4ef877f2012-12-06 02:33:54 +00004193 upper_bound_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004194 upper_bound = form_value.Unsigned();
4195 break;
4196
4197 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004198 case DW_AT_abstract_origin:
4199 case DW_AT_accessibility:
4200 case DW_AT_allocated:
4201 case DW_AT_associated:
4202 case DW_AT_data_location:
4203 case DW_AT_declaration:
4204 case DW_AT_description:
4205 case DW_AT_sibling:
4206 case DW_AT_threads_scaled:
4207 case DW_AT_type:
4208 case DW_AT_visibility:
4209 break;
4210 }
4211 }
4212 }
4213
Greg Claytone6a07792012-12-05 21:59:39 +00004214 if (num_elements == 0)
4215 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004216 if (upper_bound_valid && upper_bound >= lower_bound)
Greg Claytone6a07792012-12-05 21:59:39 +00004217 num_elements = upper_bound - lower_bound + 1;
4218 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004219
Sean Callananec979ba2012-10-22 23:56:48 +00004220 element_orders.push_back (num_elements);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004221 }
4222 }
4223 break;
4224 }
4225 }
4226}
4227
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004228TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004229SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004230{
4231 TypeSP type_sp;
4232 if (die != NULL)
4233 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004234 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004235 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004236 if (type_ptr == NULL)
4237 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004238 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004239 assert (lldb_cu);
4240 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004241 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004242 }
4243 else if (type_ptr != DIE_IS_BEING_PARSED)
4244 {
4245 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004246 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004247 }
4248
4249 }
4250 return type_sp;
4251}
4252
4253clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004254SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004255{
4256 if (die_offset != DW_INVALID_OFFSET)
4257 {
4258 DWARFCompileUnitSP cu_sp;
4259 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004260 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004261 }
4262 return NULL;
4263}
4264
Sean Callanan72e49402011-08-05 23:43:37 +00004265clang::DeclContext *
4266SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4267{
4268 if (die_offset != DW_INVALID_OFFSET)
4269 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004270 DWARFDebugInfo* debug_info = DebugInfo();
4271 if (debug_info)
4272 {
4273 DWARFCompileUnitSP cu_sp;
4274 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4275 if (die)
4276 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4277 }
Sean Callanan72e49402011-08-05 23:43:37 +00004278 }
4279 return NULL;
4280}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004281
Greg Clayton96d7d742010-11-10 23:42:09 +00004282clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004283SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004284{
Greg Clayton030a2042011-10-14 21:34:45 +00004285 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004286 {
Greg Clayton030a2042011-10-14 21:34:45 +00004287 // See if we already parsed this namespace DIE and associated it with a
4288 // uniqued namespace declaration
4289 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4290 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004291 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004292 else
4293 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004294 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4295 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004296 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
4297 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4298 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004299 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004300 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004301 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004302 GetObjectFile()->GetModule()->LogMessage (log.get(),
Daniel Malead01b2952012-11-29 21:49:15 +00004303 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
Greg Claytone38a5ed2012-01-05 03:57:59 +00004304 GetClangASTContext().getASTContext(),
4305 MakeUserID(die->GetOffset()),
4306 namespace_name,
4307 namespace_decl,
4308 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00004309 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004310 else
4311 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004312 GetObjectFile()->GetModule()->LogMessage (log.get(),
Daniel Malead01b2952012-11-29 21:49:15 +00004313 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
Greg Claytone38a5ed2012-01-05 03:57:59 +00004314 GetClangASTContext().getASTContext(),
4315 MakeUserID(die->GetOffset()),
4316 namespace_decl,
4317 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00004318 }
Greg Clayton030a2042011-10-14 21:34:45 +00004319 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004320
4321 if (namespace_decl)
4322 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4323 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004324 }
4325 }
4326 return NULL;
4327}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004328
4329clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004330SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004331{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004332 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4333 if (clang_decl_ctx)
4334 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004335 // If this DIE has a specification, or an abstract origin, then trace to those.
4336
Greg Claytoncab36a32011-12-08 05:16:30 +00004337 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004338 if (die_offset != DW_INVALID_OFFSET)
4339 return GetClangDeclContextForDIEOffset (sc, die_offset);
4340
Greg Claytoncab36a32011-12-08 05:16:30 +00004341 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004342 if (die_offset != DW_INVALID_OFFSET)
4343 return GetClangDeclContextForDIEOffset (sc, die_offset);
4344
Greg Clayton3bffb082011-12-10 02:15:28 +00004345 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4346 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004347 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 +00004348 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004349 bool assert_not_being_parsed = true;
4350 ResolveTypeUID (cu, die, assert_not_being_parsed);
4351
Greg Clayton5cf58b92011-10-05 22:22:08 +00004352 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4353
4354 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004355}
4356
4357clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004358SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004359{
Greg Claytonca512b32011-01-14 04:54:56 +00004360 if (m_clang_tu_decl == NULL)
4361 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004362
Greg Clayton2bc22f82011-09-30 03:20:47 +00004363 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004364
4365 if (decl_ctx_die_copy)
4366 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004367
4368 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004369 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004370
Greg Clayton2bc22f82011-09-30 03:20:47 +00004371 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4372 if (pos != m_die_to_decl_ctx.end())
4373 return pos->second;
4374
4375 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004376 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004377 case DW_TAG_compile_unit:
4378 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004379
Greg Clayton2bc22f82011-09-30 03:20:47 +00004380 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004381 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004382 break;
4383
4384 case DW_TAG_structure_type:
4385 case DW_TAG_union_type:
4386 case DW_TAG_class_type:
4387 {
4388 Type* type = ResolveType (cu, decl_ctx_die);
4389 if (type)
4390 {
4391 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4392 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004393 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004394 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4395 if (decl_ctx)
4396 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004397 }
4398 }
Greg Claytonca512b32011-01-14 04:54:56 +00004399 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004400 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004401
Greg Clayton2bc22f82011-09-30 03:20:47 +00004402 default:
4403 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004404 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004405 }
Greg Clayton7a345282010-11-09 23:46:37 +00004406 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004407}
4408
Greg Clayton2bc22f82011-09-30 03:20:47 +00004409
4410const DWARFDebugInfoEntry *
4411SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4412{
4413 if (cu && die)
4414 {
4415 const DWARFDebugInfoEntry * const decl_die = die;
4416
4417 while (die != NULL)
4418 {
4419 // If this is the original DIE that we are searching for a declaration
4420 // for, then don't look in the cache as we don't want our own decl
4421 // context to be our decl context...
4422 if (decl_die != die)
4423 {
4424 switch (die->Tag())
4425 {
4426 case DW_TAG_compile_unit:
4427 case DW_TAG_namespace:
4428 case DW_TAG_structure_type:
4429 case DW_TAG_union_type:
4430 case DW_TAG_class_type:
4431 return die;
4432
4433 default:
4434 break;
4435 }
4436 }
4437
4438 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4439 if (die_offset != DW_INVALID_OFFSET)
4440 {
4441 DWARFCompileUnit *spec_cu = cu;
4442 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4443 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4444 if (spec_die_decl_ctx_die)
4445 return spec_die_decl_ctx_die;
4446 }
4447
4448 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4449 if (die_offset != DW_INVALID_OFFSET)
4450 {
4451 DWARFCompileUnit *abs_cu = cu;
4452 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4453 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4454 if (abs_die_decl_ctx_die)
4455 return abs_die_decl_ctx_die;
4456 }
4457
4458 die = die->GetParent();
4459 }
4460 }
4461 return NULL;
4462}
4463
4464
Greg Clayton901c5ca2011-12-03 04:40:03 +00004465Symbol *
4466SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4467{
4468 Symbol *objc_class_symbol = NULL;
4469 if (m_obj_file)
4470 {
4471 Symtab *symtab = m_obj_file->GetSymtab();
4472 if (symtab)
4473 {
4474 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4475 eSymbolTypeObjCClass,
4476 Symtab::eDebugNo,
4477 Symtab::eVisibilityAny);
4478 }
4479 }
4480 return objc_class_symbol;
4481}
4482
Greg Claytonc7f03b62012-01-12 04:33:28 +00004483// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4484// then we can end up looking through all class types for a complete type and never find
4485// the full definition. We need to know if this attribute is supported, so we determine
4486// this here and cache th result. We also need to worry about the debug map DWARF file
4487// if we are doing darwin DWARF in .o file debugging.
4488bool
4489SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4490{
4491 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4492 {
4493 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4494 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4495 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4496 else
4497 {
4498 DWARFDebugInfo* debug_info = DebugInfo();
4499 const uint32_t num_compile_units = GetNumCompileUnits();
4500 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4501 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004502 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4503 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004504 {
4505 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4506 break;
4507 }
4508 }
4509 }
Greg Clayton1f746072012-08-29 21:13:06 +00004510 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004511 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4512 }
4513 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4514}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004515
4516// This function can be used when a DIE is found that is a forward declaration
4517// DIE and we want to try and find a type that has the complete definition.
4518TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004519SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4520 const ConstString &type_name,
4521 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004522{
4523
4524 TypeSP type_sp;
4525
Greg Claytonc7f03b62012-01-12 04:33:28 +00004526 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004527 return type_sp;
4528
4529 DIEArray die_offsets;
4530
4531 if (m_using_apple_tables)
4532 {
4533 if (m_apple_types_ap.get())
4534 {
4535 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004536 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004537 }
4538 }
4539 else
4540 {
4541 if (!m_indexed)
4542 Index ();
4543
4544 m_type_index.Find (type_name, die_offsets);
4545 }
4546
Greg Clayton901c5ca2011-12-03 04:40:03 +00004547 const size_t num_matches = die_offsets.size();
4548
Greg Clayton901c5ca2011-12-03 04:40:03 +00004549 DWARFCompileUnit* type_cu = NULL;
4550 const DWARFDebugInfoEntry* type_die = NULL;
4551 if (num_matches)
4552 {
4553 DWARFDebugInfo* debug_info = DebugInfo();
4554 for (size_t i=0; i<num_matches; ++i)
4555 {
4556 const dw_offset_t die_offset = die_offsets[i];
4557 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4558
4559 if (type_die)
4560 {
4561 bool try_resolving_type = false;
4562
4563 // Don't try and resolve the DIE we are looking for with the DIE itself!
4564 if (type_die != die)
4565 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004566 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004567 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004568 case DW_TAG_class_type:
4569 case DW_TAG_structure_type:
4570 try_resolving_type = true;
4571 break;
4572 default:
4573 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004574 }
4575 }
4576
4577 if (try_resolving_type)
4578 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004579 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004580 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004581
4582 if (try_resolving_type)
4583 {
4584 Type *resolved_type = ResolveType (type_cu, type_die, false);
4585 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4586 {
Daniel Malead01b2952012-11-29 21:49:15 +00004587 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ") from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n",
Greg Clayton901c5ca2011-12-03 04:40:03 +00004588 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004589 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton901c5ca2011-12-03 04:40:03 +00004590 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4591 MakeUserID(type_die->GetOffset()),
4592 MakeUserID(type_cu->GetOffset()));
4593
Greg Claytonc7f03b62012-01-12 04:33:28 +00004594 if (die)
4595 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004596 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004597 break;
4598 }
4599 }
4600 }
4601 }
4602 else
4603 {
4604 if (m_using_apple_tables)
4605 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004606 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4607 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004608 }
4609 }
4610
4611 }
4612 }
4613 return type_sp;
4614}
4615
Greg Claytona8022fa2012-04-24 21:22:41 +00004616
Greg Clayton80c26302012-02-05 06:12:47 +00004617//----------------------------------------------------------------------
4618// This function helps to ensure that the declaration contexts match for
4619// two different DIEs. Often times debug information will refer to a
4620// forward declaration of a type (the equivalent of "struct my_struct;".
4621// There will often be a declaration of that type elsewhere that has the
4622// full definition. When we go looking for the full type "my_struct", we
4623// will find one or more matches in the accelerator tables and we will
4624// then need to make sure the type was in the same declaration context
4625// as the original DIE. This function can efficiently compare two DIEs
4626// and will return true when the declaration context matches, and false
4627// when they don't.
4628//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004629bool
4630SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4631 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4632{
Greg Claytona8022fa2012-04-24 21:22:41 +00004633 if (die1 == die2)
4634 return true;
4635
4636#if defined (LLDB_CONFIGURATION_DEBUG)
4637 // You can't and shouldn't call this function with a compile unit from
4638 // two different SymbolFileDWARF instances.
4639 assert (DebugInfo()->ContainsCompileUnit (cu1));
4640 assert (DebugInfo()->ContainsCompileUnit (cu2));
4641#endif
4642
Greg Clayton890ff562012-02-02 05:48:16 +00004643 DWARFDIECollection decl_ctx_1;
4644 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004645 //The declaration DIE stack is a stack of the declaration context
4646 // DIEs all the way back to the compile unit. If a type "T" is
4647 // declared inside a class "B", and class "B" is declared inside
4648 // a class "A" and class "A" is in a namespace "lldb", and the
4649 // namespace is in a compile unit, there will be a stack of DIEs:
4650 //
4651 // [0] DW_TAG_class_type for "B"
4652 // [1] DW_TAG_class_type for "A"
4653 // [2] DW_TAG_namespace for "lldb"
4654 // [3] DW_TAG_compile_unit for the source file.
4655 //
4656 // We grab both contexts and make sure that everything matches
4657 // all the way back to the compiler unit.
4658
4659 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004660 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004661 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004662 // Make sure the context arrays have the same size, otherwise
4663 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004664 const size_t count1 = decl_ctx_1.Size();
4665 const size_t count2 = decl_ctx_2.Size();
4666 if (count1 != count2)
4667 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004668
4669 // Make sure the DW_TAG values match all the way back up the the
4670 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004671 const DWARFDebugInfoEntry *decl_ctx_die1;
4672 const DWARFDebugInfoEntry *decl_ctx_die2;
4673 size_t i;
4674 for (i=0; i<count1; i++)
4675 {
4676 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4677 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4678 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4679 return false;
4680 }
Greg Clayton890ff562012-02-02 05:48:16 +00004681#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004682
4683 // Make sure the top item in the decl context die array is always
4684 // DW_TAG_compile_unit. If it isn't then something went wrong in
4685 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004686 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004687
Greg Clayton890ff562012-02-02 05:48:16 +00004688#endif
4689 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004690 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004691 for (i=0; i<count1 - 1; i++)
4692 {
4693 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4694 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4695 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004696 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004697 // If the string was from a DW_FORM_strp, then the pointer will often
4698 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004699 if (name1 == name2)
4700 continue;
4701
4702 // Name pointers are not equal, so only compare the strings
4703 // if both are not NULL.
4704 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004705 {
Greg Clayton5569e642012-02-06 01:44:54 +00004706 // If the strings don't compare, we are done...
4707 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004708 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004709 }
4710 else
4711 {
4712 // One name was NULL while the other wasn't
4713 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004714 }
4715 }
Greg Clayton80c26302012-02-05 06:12:47 +00004716 // We made it through all of the checks and the declaration contexts
4717 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004718 return true;
4719}
Greg Clayton220a0072011-12-09 08:48:30 +00004720
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004721// This function can be used when a DIE is found that is a forward declaration
4722// DIE and we want to try and find a type that has the complete definition.
Greg Claytona8022fa2012-04-24 21:22:41 +00004723// "cu" and "die" must be from this SymbolFileDWARF
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004724TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004725SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
Greg Clayton7f995132011-10-04 22:41:51 +00004726 const DWARFDebugInfoEntry *die,
4727 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004728{
4729 TypeSP type_sp;
4730
Greg Claytona8022fa2012-04-24 21:22:41 +00004731#if defined (LLDB_CONFIGURATION_DEBUG)
4732 // You can't and shouldn't call this function with a compile unit from
4733 // another SymbolFileDWARF instance.
4734 assert (DebugInfo()->ContainsCompileUnit (cu));
4735#endif
4736
Greg Clayton1a65ae12011-01-25 23:55:37 +00004737 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004738 return type_sp;
4739
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00004740 std::string qualified_name;
4741
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004742 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4743 if (log)
4744 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004745 die->GetQualifiedName(this, cu, qualified_name);
4746 GetObjectFile()->GetModule()->LogMessage (log.get(),
4747 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x (%s), name='%s')",
4748 die->GetOffset(),
4749 qualified_name.c_str(),
4750 type_name.GetCString());
4751 }
4752
Greg Clayton7f995132011-10-04 22:41:51 +00004753 DIEArray die_offsets;
4754
Greg Clayton97fbc342011-10-20 22:30:33 +00004755 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004756 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004757 if (m_apple_types_ap.get())
4758 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00004759 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
4760 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
4761 if (has_tag && has_qualified_name_hash)
Greg Claytond1767f02011-12-08 02:13:16 +00004762 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00004763 if (qualified_name.empty())
4764 die->GetQualifiedName(this, cu, qualified_name);
4765
4766 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name.c_str());
4767 if (log)
4768 GetObjectFile()->GetModule()->LogMessage (log.get(),"FindByNameAndTagAndQualifiedNameHash()");
4769 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), die->Tag(), qualified_name_hash, die_offsets);
4770 }
4771 else if (has_tag > 1)
4772 {
4773 if (log)
4774 GetObjectFile()->GetModule()->LogMessage (log.get(),"FindByNameAndTag()");
Greg Claytonae920b62012-01-06 00:17:16 +00004775 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004776 }
4777 else
4778 {
4779 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4780 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004781 }
Greg Clayton7f995132011-10-04 22:41:51 +00004782 }
4783 else
4784 {
4785 if (!m_indexed)
4786 Index ();
4787
4788 m_type_index.Find (type_name, die_offsets);
4789 }
Greg Clayton7f995132011-10-04 22:41:51 +00004790
4791 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004792
Greg Clayton934cb052011-12-03 00:27:05 +00004793 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004794
4795 DWARFCompileUnit* type_cu = NULL;
4796 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004797 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004798 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004799 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004800 for (size_t i=0; i<num_matches; ++i)
4801 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004802 const dw_offset_t die_offset = die_offsets[i];
4803 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004804
Greg Clayton95d87902011-11-11 03:16:25 +00004805 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004806 {
Greg Clayton934cb052011-12-03 00:27:05 +00004807 bool try_resolving_type = false;
4808
4809 // Don't try and resolve the DIE we are looking for with the DIE itself!
4810 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004811 {
Greg Clayton934cb052011-12-03 00:27:05 +00004812 const dw_tag_t type_die_tag = type_die->Tag();
4813 // Make sure the tags match
4814 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004815 {
Greg Clayton934cb052011-12-03 00:27:05 +00004816 // The tags match, lets try resolving this type
4817 try_resolving_type = true;
4818 }
4819 else
4820 {
4821 // The tags don't match, but we need to watch our for a
4822 // forward declaration for a struct and ("struct foo")
4823 // ends up being a class ("class foo { ... };") or
4824 // vice versa.
4825 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004826 {
Greg Clayton934cb052011-12-03 00:27:05 +00004827 case DW_TAG_class_type:
4828 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4829 try_resolving_type = (die_tag == DW_TAG_structure_type);
4830 break;
4831 case DW_TAG_structure_type:
4832 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4833 try_resolving_type = (die_tag == DW_TAG_class_type);
4834 break;
4835 default:
4836 // Tags don't match, don't event try to resolve
4837 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004838 break;
4839 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004840 }
4841 }
Greg Clayton934cb052011-12-03 00:27:05 +00004842
4843 if (try_resolving_type)
4844 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004845 if (log)
4846 {
4847 std::string qualified_name;
4848 type_die->GetQualifiedName(this, cu, qualified_name);
4849 GetObjectFile()->GetModule()->LogMessage (log.get(),
4850 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') trying die=0x%8.8x (%s)",
4851 die->GetOffset(),
4852 type_name.GetCString(),
4853 type_die->GetOffset(),
4854 qualified_name.c_str());
4855 }
4856
Greg Clayton890ff562012-02-02 05:48:16 +00004857 // Make sure the decl contexts match all the way up
4858 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004859 {
Greg Clayton890ff562012-02-02 05:48:16 +00004860 Type *resolved_type = ResolveType (type_cu, type_die, false);
4861 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4862 {
Daniel Malead01b2952012-11-29 21:49:15 +00004863 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ") from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n",
Greg Clayton890ff562012-02-02 05:48:16 +00004864 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004865 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton890ff562012-02-02 05:48:16 +00004866 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4867 MakeUserID(type_die->GetOffset()),
4868 MakeUserID(type_cu->GetOffset()));
4869
4870 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004871 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004872 break;
4873 }
Greg Clayton934cb052011-12-03 00:27:05 +00004874 }
4875 }
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004876 else
4877 {
4878 if (log)
4879 {
4880 std::string qualified_name;
4881 type_die->GetQualifiedName(this, cu, qualified_name);
4882 GetObjectFile()->GetModule()->LogMessage (log.get(),
4883 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') ignoring die=0x%8.8x (%s)",
4884 die->GetOffset(),
4885 type_name.GetCString(),
4886 type_die->GetOffset(),
4887 qualified_name.c_str());
4888 }
4889 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004890 }
Greg Clayton95d87902011-11-11 03:16:25 +00004891 else
4892 {
4893 if (m_using_apple_tables)
4894 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004895 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4896 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004897 }
4898 }
4899
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004900 }
4901 }
4902 return type_sp;
4903}
4904
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004905TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004906SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
4907{
4908 TypeSP type_sp;
4909
4910 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
4911 if (dwarf_decl_ctx_count > 0)
4912 {
4913 const ConstString type_name(dwarf_decl_ctx[0].name);
4914 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
4915
4916 if (type_name)
4917 {
4918 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4919 if (log)
4920 {
4921 GetObjectFile()->GetModule()->LogMessage (log.get(),
4922 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
4923 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4924 dwarf_decl_ctx.GetQualifiedName());
4925 }
4926
4927 DIEArray die_offsets;
4928
4929 if (m_using_apple_tables)
4930 {
4931 if (m_apple_types_ap.get())
4932 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00004933 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
4934 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
4935 if (has_tag && has_qualified_name_hash)
Greg Claytona8022fa2012-04-24 21:22:41 +00004936 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00004937 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
4938 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name);
4939 if (log)
4940 GetObjectFile()->GetModule()->LogMessage (log.get(),"FindByNameAndTagAndQualifiedNameHash()");
4941 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets);
4942 }
4943 else if (has_tag)
4944 {
4945 if (log)
4946 GetObjectFile()->GetModule()->LogMessage (log.get(),"FindByNameAndTag()");
Greg Claytona8022fa2012-04-24 21:22:41 +00004947 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
4948 }
4949 else
4950 {
4951 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4952 }
4953 }
4954 }
4955 else
4956 {
4957 if (!m_indexed)
4958 Index ();
4959
4960 m_type_index.Find (type_name, die_offsets);
4961 }
4962
4963 const size_t num_matches = die_offsets.size();
4964
4965
4966 DWARFCompileUnit* type_cu = NULL;
4967 const DWARFDebugInfoEntry* type_die = NULL;
4968 if (num_matches)
4969 {
4970 DWARFDebugInfo* debug_info = DebugInfo();
4971 for (size_t i=0; i<num_matches; ++i)
4972 {
4973 const dw_offset_t die_offset = die_offsets[i];
4974 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4975
4976 if (type_die)
4977 {
4978 bool try_resolving_type = false;
4979
4980 // Don't try and resolve the DIE we are looking for with the DIE itself!
4981 const dw_tag_t type_tag = type_die->Tag();
4982 // Make sure the tags match
4983 if (type_tag == tag)
4984 {
4985 // The tags match, lets try resolving this type
4986 try_resolving_type = true;
4987 }
4988 else
4989 {
4990 // The tags don't match, but we need to watch our for a
4991 // forward declaration for a struct and ("struct foo")
4992 // ends up being a class ("class foo { ... };") or
4993 // vice versa.
4994 switch (type_tag)
4995 {
4996 case DW_TAG_class_type:
4997 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4998 try_resolving_type = (tag == DW_TAG_structure_type);
4999 break;
5000 case DW_TAG_structure_type:
5001 // We had a "struct foo", see if we ended up with a "class foo { ... };"
5002 try_resolving_type = (tag == DW_TAG_class_type);
5003 break;
5004 default:
5005 // Tags don't match, don't event try to resolve
5006 // using this type whose name matches....
5007 break;
5008 }
5009 }
5010
5011 if (try_resolving_type)
5012 {
5013 DWARFDeclContext type_dwarf_decl_ctx;
5014 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
5015
5016 if (log)
5017 {
5018 GetObjectFile()->GetModule()->LogMessage (log.get(),
5019 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
5020 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5021 dwarf_decl_ctx.GetQualifiedName(),
5022 type_die->GetOffset(),
5023 type_dwarf_decl_ctx.GetQualifiedName());
5024 }
5025
5026 // Make sure the decl contexts match all the way up
5027 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
5028 {
5029 Type *resolved_type = ResolveType (type_cu, type_die, false);
5030 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
5031 {
5032 type_sp = resolved_type->shared_from_this();
5033 break;
5034 }
5035 }
5036 }
5037 else
5038 {
5039 if (log)
5040 {
5041 std::string qualified_name;
5042 type_die->GetQualifiedName(this, type_cu, qualified_name);
5043 GetObjectFile()->GetModule()->LogMessage (log.get(),
5044 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
5045 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5046 dwarf_decl_ctx.GetQualifiedName(),
5047 type_die->GetOffset(),
5048 qualified_name.c_str());
5049 }
5050 }
5051 }
5052 else
5053 {
5054 if (m_using_apple_tables)
5055 {
5056 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
5057 die_offset, type_name.GetCString());
5058 }
5059 }
5060
5061 }
5062 }
5063 }
5064 }
5065 return type_sp;
5066}
5067
Greg Clayton4116e932012-05-15 02:33:01 +00005068bool
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005069SymbolFileDWARF::CopyUniqueClassMethodTypes (SymbolFileDWARF *src_symfile,
Sean Callanan2367f8a2013-02-26 01:12:25 +00005070 Type *class_type,
Greg Clayton4116e932012-05-15 02:33:01 +00005071 DWARFCompileUnit* src_cu,
5072 const DWARFDebugInfoEntry *src_class_die,
5073 DWARFCompileUnit* dst_cu,
Sean Callanan2367f8a2013-02-26 01:12:25 +00005074 const DWARFDebugInfoEntry *dst_class_die,
5075 llvm::SmallVectorImpl <const DWARFDebugInfoEntry *> &failures)
Greg Clayton4116e932012-05-15 02:33:01 +00005076{
5077 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
5078 return false;
5079 if (src_class_die->Tag() != dst_class_die->Tag())
5080 return false;
5081
5082 // We need to complete the class type so we can get all of the method types
5083 // parsed so we can then unique those types to their equivalent counterparts
5084 // in "dst_cu" and "dst_class_die"
5085 class_type->GetClangFullType();
5086
5087 const DWARFDebugInfoEntry *src_die;
5088 const DWARFDebugInfoEntry *dst_die;
5089 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
5090 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
Greg Clayton65ec1032012-11-12 21:27:20 +00005091 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die_artificial;
5092 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die_artificial;
Greg Clayton4116e932012-05-15 02:33:01 +00005093 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
5094 {
5095 if (src_die->Tag() == DW_TAG_subprogram)
5096 {
Greg Clayton84afacd2012-11-07 23:09:32 +00005097 // Make sure this is a declaration and not a concrete instance by looking
5098 // for DW_AT_declaration set to 1. Sometimes concrete function instances
5099 // are placed inside the class definitions and shouldn't be included in
5100 // the list of things are are tracking here.
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005101 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_declaration, 0) == 1)
Greg Clayton84afacd2012-11-07 23:09:32 +00005102 {
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005103 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
Greg Clayton84afacd2012-11-07 23:09:32 +00005104 if (src_name)
Greg Clayton65ec1032012-11-12 21:27:20 +00005105 {
5106 ConstString src_const_name(src_name);
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005107 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_artificial, 0))
Greg Clayton65ec1032012-11-12 21:27:20 +00005108 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die);
5109 else
5110 src_name_to_die.Append(src_const_name.GetCString(), src_die);
5111 }
Greg Clayton84afacd2012-11-07 23:09:32 +00005112 }
Greg Clayton4116e932012-05-15 02:33:01 +00005113 }
5114 }
5115 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
5116 {
5117 if (dst_die->Tag() == DW_TAG_subprogram)
5118 {
Greg Clayton84afacd2012-11-07 23:09:32 +00005119 // Make sure this is a declaration and not a concrete instance by looking
5120 // for DW_AT_declaration set to 1. Sometimes concrete function instances
5121 // are placed inside the class definitions and shouldn't be included in
5122 // the list of things are are tracking here.
5123 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_declaration, 0) == 1)
5124 {
5125 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
5126 if (dst_name)
Greg Clayton65ec1032012-11-12 21:27:20 +00005127 {
5128 ConstString dst_const_name(dst_name);
5129 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_artificial, 0))
5130 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die);
5131 else
5132 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die);
5133 }
Greg Clayton84afacd2012-11-07 23:09:32 +00005134 }
Greg Clayton4116e932012-05-15 02:33:01 +00005135 }
5136 }
5137 const uint32_t src_size = src_name_to_die.GetSize ();
5138 const uint32_t dst_size = dst_name_to_die.GetSize ();
5139 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
Sean Callanan2367f8a2013-02-26 01:12:25 +00005140
5141 // Is everything kosher so we can go through the members at top speed?
5142 bool fast_path = true;
5143
5144 if (src_size != dst_size)
Greg Clayton4116e932012-05-15 02:33:01 +00005145 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00005146 if (src_size != 0 && dst_size != 0)
5147 {
5148 if (log)
5149 log->Printf("warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)",
5150 src_class_die->GetOffset(),
5151 dst_class_die->GetOffset(),
5152 src_size,
5153 dst_size);
5154 }
5155
5156 fast_path = false;
5157 }
5158
5159 uint32_t idx;
5160
5161 if (fast_path)
5162 {
Greg Clayton4116e932012-05-15 02:33:01 +00005163 for (idx = 0; idx < src_size; ++idx)
5164 {
5165 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5166 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
5167
5168 if (src_die->Tag() != dst_die->Tag())
5169 {
5170 if (log)
5171 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)",
5172 src_class_die->GetOffset(),
5173 dst_class_die->GetOffset(),
5174 src_die->GetOffset(),
5175 DW_TAG_value_to_name(src_die->Tag()),
5176 dst_die->GetOffset(),
5177 DW_TAG_value_to_name(src_die->Tag()));
Sean Callanan2367f8a2013-02-26 01:12:25 +00005178 fast_path = false;
Greg Clayton4116e932012-05-15 02:33:01 +00005179 }
5180
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005181 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
Greg Clayton4116e932012-05-15 02:33:01 +00005182 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
5183
5184 // Make sure the names match
5185 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
5186 continue;
5187
5188 if (log)
5189 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)",
5190 src_class_die->GetOffset(),
5191 dst_class_die->GetOffset(),
5192 src_die->GetOffset(),
5193 src_name,
5194 dst_die->GetOffset(),
5195 dst_name);
5196
Sean Callanan2367f8a2013-02-26 01:12:25 +00005197 fast_path = false;
Greg Clayton4116e932012-05-15 02:33:01 +00005198 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005199 }
Greg Clayton4116e932012-05-15 02:33:01 +00005200
Sean Callanan2367f8a2013-02-26 01:12:25 +00005201 // Now do the work of linking the DeclContexts and Types.
5202 if (fast_path)
5203 {
5204 // We can do this quickly. Just run across the tables index-for-index since
5205 // we know each node has matching names and tags.
Greg Clayton4116e932012-05-15 02:33:01 +00005206 for (idx = 0; idx < src_size; ++idx)
5207 {
5208 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5209 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
5210
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005211 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
Greg Clayton4116e932012-05-15 02:33:01 +00005212 if (src_decl_ctx)
5213 {
5214 if (log)
Greg Clayton65ec1032012-11-12 21:27:20 +00005215 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005216 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5217 }
5218 else
5219 {
5220 if (log)
Greg Clayton65ec1032012-11-12 21:27:20 +00005221 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005222 }
5223
5224 Type *src_child_type = m_die_to_type[src_die];
5225 if (src_child_type)
5226 {
5227 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00005228 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", src_child_type, src_child_type->GetID(), src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005229 m_die_to_type[dst_die] = src_child_type;
5230 }
5231 else
5232 {
5233 if (log)
Greg Clayton65ec1032012-11-12 21:27:20 +00005234 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5235 }
5236 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005237 }
5238 else
5239 {
5240 // We must do this slowly. For each member of the destination, look
5241 // up a member in the source with the same name, check its tag, and
5242 // unique them if everything matches up. Report failures.
Greg Clayton65ec1032012-11-12 21:27:20 +00005243
Sean Callanan2367f8a2013-02-26 01:12:25 +00005244 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty())
5245 {
5246 src_name_to_die.Sort();
Greg Clayton65ec1032012-11-12 21:27:20 +00005247
Sean Callanan2367f8a2013-02-26 01:12:25 +00005248 for (idx = 0; idx < dst_size; ++idx)
5249 {
5250 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx);
5251 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
5252 src_die = src_name_to_die.Find(dst_name, NULL);
5253
5254 if (src_die && (src_die->Tag() == dst_die->Tag()))
5255 {
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005256 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
Sean Callanan2367f8a2013-02-26 01:12:25 +00005257 if (src_decl_ctx)
5258 {
5259 if (log)
5260 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
5261 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5262 }
5263 else
5264 {
5265 if (log)
5266 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5267 }
5268
5269 Type *src_child_type = m_die_to_type[src_die];
5270 if (src_child_type)
5271 {
5272 if (log)
5273 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", src_child_type, src_child_type->GetID(), src_die->GetOffset(), dst_die->GetOffset());
5274 m_die_to_type[dst_die] = src_child_type;
5275 }
5276 else
5277 {
5278 if (log)
5279 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5280 }
5281 }
5282 else
5283 {
5284 if (log)
5285 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005286
Sean Callanan2367f8a2013-02-26 01:12:25 +00005287 failures.push_back(dst_die);
5288 }
5289 }
5290 }
5291 }
5292
5293 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize ();
5294 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize ();
5295
5296 UniqueCStringMap<const DWARFDebugInfoEntry *> name_to_die_artificial_not_in_src;
5297
5298 if (src_size_artificial && dst_size_artificial)
5299 {
5300 dst_name_to_die_artificial.Sort();
5301
Greg Clayton65ec1032012-11-12 21:27:20 +00005302 for (idx = 0; idx < src_size_artificial; ++idx)
5303 {
5304 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx);
5305 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
5306 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, NULL);
5307
5308 if (dst_die)
5309 {
Greg Clayton65ec1032012-11-12 21:27:20 +00005310 // Both classes have the artificial types, link them
5311 clang::DeclContext *src_decl_ctx = m_die_to_decl_ctx[src_die];
5312 if (src_decl_ctx)
5313 {
5314 if (log)
5315 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
5316 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5317 }
5318 else
5319 {
5320 if (log)
5321 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5322 }
5323
5324 Type *src_child_type = m_die_to_type[src_die];
5325 if (src_child_type)
5326 {
5327 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +00005328 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", src_child_type, src_child_type->GetID(), src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005329 m_die_to_type[dst_die] = src_child_type;
5330 }
5331 else
5332 {
5333 if (log)
5334 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5335 }
5336 }
5337 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005338 }
Greg Clayton65ec1032012-11-12 21:27:20 +00005339
Sean Callanan2367f8a2013-02-26 01:12:25 +00005340 if (dst_size_artificial)
Greg Clayton4116e932012-05-15 02:33:01 +00005341 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00005342 for (idx = 0; idx < dst_size_artificial; ++idx)
5343 {
5344 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx);
5345 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
5346 if (log)
5347 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die->GetOffset(), dst_name_artificial);
5348
5349 failures.push_back(dst_die);
5350 }
Greg Clayton4116e932012-05-15 02:33:01 +00005351 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005352
5353 return (failures.size() != 0);
Greg Clayton4116e932012-05-15 02:33:01 +00005354}
Greg Claytona8022fa2012-04-24 21:22:41 +00005355
5356TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00005357SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005358{
5359 TypeSP type_sp;
5360
Greg Clayton1be10fc2010-09-29 01:12:09 +00005361 if (type_is_new_ptr)
5362 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00005363
5364#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5365 static DIEStack g_die_stack;
5366 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
5367#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00005368
Sean Callananc7fbf732010-08-06 00:32:49 +00005369 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005370 if (die != NULL)
5371 {
Greg Clayton21f2a492011-10-06 00:09:08 +00005372 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005373 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00005374 {
5375 const DWARFDebugInfoEntry *context_die;
5376 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
5377
Greg Clayton1fba87112012-02-09 20:03:49 +00005378 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 +00005379 die->GetOffset(),
5380 context,
5381 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00005382 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00005383 die->GetName(this, dwarf_cu));
5384
5385#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5386 scoped_die_logger.Push (dwarf_cu, die);
5387 g_die_stack.LogDIEs(log.get(), this);
5388#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00005389 }
Greg Clayton3bffb082011-12-10 02:15:28 +00005390//
5391// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
5392// if (log && dwarf_cu)
5393// {
5394// StreamString s;
5395// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005396// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00005397//
5398// }
Jim Ingham16746d12011-08-25 23:21:43 +00005399
Greg Clayton594e5ed2010-09-27 21:07:38 +00005400 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005401 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00005402 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005403 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005404 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005405 if (type_is_new_ptr)
5406 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005407
Greg Clayton594e5ed2010-09-27 21:07:38 +00005408 const dw_tag_t tag = die->Tag();
5409
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005410 bool is_forward_declaration = false;
5411 DWARFDebugInfoEntry::Attributes attributes;
5412 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00005413 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00005414 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
5415 size_t byte_size = 0;
5416 Declaration decl;
5417
Greg Clayton4957bf62010-09-30 21:49:03 +00005418 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005419 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005420
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005421 dw_attr_t attr;
5422
5423 switch (tag)
5424 {
5425 case DW_TAG_base_type:
5426 case DW_TAG_pointer_type:
5427 case DW_TAG_reference_type:
Sean Callanance8af862012-05-21 23:31:51 +00005428 case DW_TAG_rvalue_reference_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005429 case DW_TAG_typedef:
5430 case DW_TAG_const_type:
5431 case DW_TAG_restrict_type:
5432 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00005433 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005434 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005435 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005436 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005437
Greg Claytond88d7592010-09-15 08:33:30 +00005438 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005439 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005440 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
5441
5442 if (num_attributes > 0)
5443 {
5444 uint32_t i;
5445 for (i=0; i<num_attributes; ++i)
5446 {
5447 attr = attributes.AttributeAtIndex(i);
5448 DWARFFormValue form_value;
5449 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5450 {
5451 switch (attr)
5452 {
5453 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5454 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5455 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5456 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00005457
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005458 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00005459 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
5460 // include the "&"...
5461 if (tag == DW_TAG_reference_type)
5462 {
5463 if (strchr (type_name_cstr, '&') == NULL)
5464 type_name_cstr = NULL;
5465 }
5466 if (type_name_cstr)
5467 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005468 break;
Greg Clayton23f59502012-07-17 03:23:13 +00005469 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005470 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
5471 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
5472 default:
5473 case DW_AT_sibling:
5474 break;
5475 }
5476 }
5477 }
5478 }
5479
Daniel Malead01b2952012-11-29 21:49:15 +00005480 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %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 +00005481
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005482 switch (tag)
5483 {
5484 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00005485 break;
5486
Greg Claytond4436412011-10-28 21:00:00 +00005487 case DW_TAG_unspecified_type:
5488 if (strcmp(type_name_cstr, "nullptr_t") == 0)
5489 {
5490 resolve_state = Type::eResolveStateFull;
5491 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
5492 break;
5493 }
5494 // Fall through to base type below in case we can handle the type there...
5495
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005496 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00005497 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005498 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
5499 encoding,
5500 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005501 break;
5502
Sean Callanance8af862012-05-21 23:31:51 +00005503 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
5504 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
5505 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
5506 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
5507 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
5508 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
5509 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005510 }
5511
Sean Callanan82587052013-01-03 00:05:56 +00005512 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005513 {
Sean Callanan82587052013-01-03 00:05:56 +00005514 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
5515
5516 if (translation_unit_is_objc)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005517 {
Sean Callanan82587052013-01-03 00:05:56 +00005518 if (type_name_cstr != NULL)
Greg Claytonc7f03b62012-01-12 04:33:28 +00005519 {
Sean Callanan82587052013-01-03 00:05:56 +00005520 static ConstString g_objc_type_name_id("id");
5521 static ConstString g_objc_type_name_Class("Class");
5522 static ConstString g_objc_type_name_selector("SEL");
5523
5524 if (type_name_const_str == g_objc_type_name_id)
5525 {
5526 if (log)
5527 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
5528 die->GetOffset(),
5529 DW_TAG_value_to_name(die->Tag()),
5530 die->GetName(this, dwarf_cu));
5531 clang_type = ast.GetBuiltInType_objc_id();
5532 encoding_data_type = Type::eEncodingIsUID;
5533 encoding_uid = LLDB_INVALID_UID;
5534 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00005535
Sean Callanan82587052013-01-03 00:05:56 +00005536 }
5537 else if (type_name_const_str == g_objc_type_name_Class)
5538 {
5539 if (log)
5540 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
5541 die->GetOffset(),
5542 DW_TAG_value_to_name(die->Tag()),
5543 die->GetName(this, dwarf_cu));
5544 clang_type = ast.GetBuiltInType_objc_Class();
5545 encoding_data_type = Type::eEncodingIsUID;
5546 encoding_uid = LLDB_INVALID_UID;
5547 resolve_state = Type::eResolveStateFull;
5548 }
5549 else if (type_name_const_str == g_objc_type_name_selector)
5550 {
5551 if (log)
5552 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
5553 die->GetOffset(),
5554 DW_TAG_value_to_name(die->Tag()),
5555 die->GetName(this, dwarf_cu));
5556 clang_type = ast.GetBuiltInType_objc_selector();
5557 encoding_data_type = Type::eEncodingIsUID;
5558 encoding_uid = LLDB_INVALID_UID;
5559 resolve_state = Type::eResolveStateFull;
5560 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00005561 }
Sean Callanan82587052013-01-03 00:05:56 +00005562 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID)
Greg Claytonc7f03b62012-01-12 04:33:28 +00005563 {
Sean Callanan82587052013-01-03 00:05:56 +00005564 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id".
5565
5566 DWARFDebugInfoEntry* encoding_die = dwarf_cu->GetDIEPtr(encoding_uid);
5567
5568 if (encoding_die && encoding_die->Tag() == DW_TAG_structure_type)
5569 {
5570 if (const char *struct_name = encoding_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL))
5571 {
5572 if (!strcmp(struct_name, "objc_object"))
5573 {
5574 if (log)
5575 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.",
5576 die->GetOffset(),
5577 DW_TAG_value_to_name(die->Tag()),
5578 die->GetName(this, dwarf_cu));
5579 clang_type = ast.GetBuiltInType_objc_id();
5580 encoding_data_type = Type::eEncodingIsUID;
5581 encoding_uid = LLDB_INVALID_UID;
5582 resolve_state = Type::eResolveStateFull;
5583 }
5584 }
5585 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00005586 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005587 }
5588 }
5589
Greg Clayton81c22f62011-10-19 18:09:39 +00005590 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005591 this,
5592 type_name_const_str,
5593 byte_size,
5594 NULL,
5595 encoding_uid,
5596 encoding_data_type,
5597 &decl,
5598 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005599 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005600
Greg Clayton594e5ed2010-09-27 21:07:38 +00005601 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005602
5603// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
5604// if (encoding_type != NULL)
5605// {
5606// if (encoding_type != DIE_IS_BEING_PARSED)
5607// type_sp->SetEncodingType(encoding_type);
5608// else
5609// m_indirect_fixups.push_back(type_sp.get());
5610// }
5611 }
5612 break;
5613
5614 case DW_TAG_structure_type:
5615 case DW_TAG_union_type:
5616 case DW_TAG_class_type:
5617 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005618 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005619 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Greg Clayton23f59502012-07-17 03:23:13 +00005620 bool byte_size_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005621
Greg Clayton9e409562010-07-28 02:04:09 +00005622 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00005623 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005624 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00005625 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005626 if (num_attributes > 0)
5627 {
5628 uint32_t i;
5629 for (i=0; i<num_attributes; ++i)
5630 {
5631 attr = attributes.AttributeAtIndex(i);
5632 DWARFFormValue form_value;
5633 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5634 {
5635 switch (attr)
5636 {
Greg Clayton9e409562010-07-28 02:04:09 +00005637 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00005638 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
5639 {
5640 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
5641 // point to the compile unit file, so we clear this invalid value
5642 // so that we can still unique types efficiently.
5643 decl.SetFile(FileSpec ("<invalid>", false));
5644 }
5645 else
5646 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00005647 break;
5648
5649 case DW_AT_decl_line:
5650 decl.SetLine(form_value.Unsigned());
5651 break;
5652
5653 case DW_AT_decl_column:
5654 decl.SetColumn(form_value.Unsigned());
5655 break;
5656
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005657 case DW_AT_name:
5658 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005659 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005660 break;
Greg Clayton9e409562010-07-28 02:04:09 +00005661
5662 case DW_AT_byte_size:
5663 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00005664 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00005665 break;
5666
5667 case DW_AT_accessibility:
5668 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
5669 break;
5670
5671 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00005672 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00005673 break;
5674
5675 case DW_AT_APPLE_runtime_class:
5676 class_language = (LanguageType)form_value.Signed();
5677 break;
5678
Greg Clayton18774842011-11-29 23:40:34 +00005679 case DW_AT_APPLE_objc_complete_type:
5680 is_complete_objc_class = form_value.Signed();
5681 break;
5682
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005683 case DW_AT_allocated:
5684 case DW_AT_associated:
5685 case DW_AT_data_location:
5686 case DW_AT_description:
5687 case DW_AT_start_scope:
5688 case DW_AT_visibility:
5689 default:
5690 case DW_AT_sibling:
5691 break;
5692 }
5693 }
5694 }
5695 }
5696
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005697 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00005698
Greg Clayton123edca2012-03-02 00:07:15 +00005699 // Only try and unique the type if it has a name.
5700 if (type_name_const_str &&
5701 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00005702 this,
5703 dwarf_cu,
5704 die,
5705 decl,
5706 byte_size_valid ? byte_size : -1,
5707 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005708 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005709 // We have already parsed this type or from another
5710 // compile unit. GCC loves to use the "one definition
5711 // rule" which can result in multiple definitions
5712 // of the same class over and over in each compile
5713 // unit.
5714 type_sp = unique_ast_entry.m_type_sp;
5715 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00005716 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005717 m_die_to_type[die] = type_sp.get();
5718 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005719 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005720 }
5721
Daniel Malead01b2952012-11-29 21:49:15 +00005722 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005723
5724 int tag_decl_kind = -1;
5725 AccessType default_accessibility = eAccessNone;
5726 if (tag == DW_TAG_structure_type)
5727 {
5728 tag_decl_kind = clang::TTK_Struct;
5729 default_accessibility = eAccessPublic;
5730 }
5731 else if (tag == DW_TAG_union_type)
5732 {
5733 tag_decl_kind = clang::TTK_Union;
5734 default_accessibility = eAccessPublic;
5735 }
5736 else if (tag == DW_TAG_class_type)
5737 {
5738 tag_decl_kind = clang::TTK_Class;
5739 default_accessibility = eAccessPrivate;
5740 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00005741
5742 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
5743 die->HasChildren() == false &&
5744 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
5745 {
5746 // Work around an issue with clang at the moment where
5747 // forward declarations for objective C classes are emitted
5748 // as:
5749 // DW_TAG_structure_type [2]
5750 // DW_AT_name( "ForwardObjcClass" )
5751 // DW_AT_byte_size( 0x00 )
5752 // DW_AT_decl_file( "..." )
5753 // DW_AT_decl_line( 1 )
5754 //
5755 // Note that there is no DW_AT_declaration and there are
5756 // no children, and the byte size is zero.
5757 is_forward_declaration = true;
5758 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005759
Sean Callanan7457f8d2012-04-25 01:03:57 +00005760 if (class_language == eLanguageTypeObjC ||
5761 class_language == eLanguageTypeObjC_plus_plus)
Greg Clayton18774842011-11-29 23:40:34 +00005762 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005763 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00005764 {
5765 // We have a valid eSymbolTypeObjCClass class symbol whose
5766 // name matches the current objective C class that we
5767 // are trying to find and this DIE isn't the complete
5768 // definition (we checked is_complete_objc_class above and
5769 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00005770 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005771
Greg Clayton1f746072012-08-29 21:13:06 +00005772 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton901c5ca2011-12-03 04:40:03 +00005773 {
5774 // We weren't able to find a full declaration in
5775 // this DWARF, see if we have a declaration anywhere
5776 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00005777 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005778 }
5779
5780 if (type_sp)
5781 {
5782 if (log)
5783 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005784 GetObjectFile()->GetModule()->LogMessage (log.get(),
Daniel Malead01b2952012-11-29 21:49:15 +00005785 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64,
Greg Claytone38a5ed2012-01-05 03:57:59 +00005786 this,
5787 die->GetOffset(),
5788 DW_TAG_value_to_name(tag),
5789 type_name_cstr,
5790 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00005791 }
5792
5793 // We found a real definition for this type elsewhere
5794 // so lets use it and cache the fact that we found
5795 // a complete type for this die
5796 m_die_to_type[die] = type_sp.get();
5797 return type_sp;
5798 }
5799 }
5800 }
5801
5802
5803 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005804 {
5805 // We have a forward declaration to a type and we need
5806 // to try and find a full declaration. We look in the
5807 // current type index just in case we have a forward
5808 // declaration followed by an actual declarations in the
5809 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00005810 if (log)
5811 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005812 GetObjectFile()->GetModule()->LogMessage (log.get(),
5813 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
5814 this,
5815 die->GetOffset(),
5816 DW_TAG_value_to_name(tag),
5817 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00005818 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005819
Greg Claytona8022fa2012-04-24 21:22:41 +00005820 DWARFDeclContext die_decl_ctx;
5821 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx);
5822
5823 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
5824 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005825
Greg Clayton1f746072012-08-29 21:13:06 +00005826 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton4272cc72011-02-02 02:24:04 +00005827 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005828 // We weren't able to find a full declaration in
5829 // this DWARF, see if we have a declaration anywhere
5830 // else...
Greg Claytona8022fa2012-04-24 21:22:41 +00005831 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton4272cc72011-02-02 02:24:04 +00005832 }
5833
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005834 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00005835 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00005836 if (log)
5837 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005838 GetObjectFile()->GetModule()->LogMessage (log.get(),
Daniel Malead01b2952012-11-29 21:49:15 +00005839 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
Greg Claytone38a5ed2012-01-05 03:57:59 +00005840 this,
5841 die->GetOffset(),
5842 DW_TAG_value_to_name(tag),
5843 type_name_cstr,
5844 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00005845 }
5846
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005847 // We found a real definition for this type elsewhere
5848 // so lets use it and cache the fact that we found
5849 // a complete type for this die
5850 m_die_to_type[die] = type_sp.get();
5851 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005852 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005853 }
5854 assert (tag_decl_kind != -1);
5855 bool clang_type_was_created = false;
5856 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5857 if (clang_type == NULL)
5858 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00005859 const DWARFDebugInfoEntry *decl_ctx_die;
5860
5861 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00005862 if (accessibility == eAccessNone && decl_ctx)
5863 {
5864 // Check the decl context that contains this class/struct/union.
5865 // If it is a class we must give it an accessability.
5866 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
5867 if (DeclKindIsCXXClass (containing_decl_kind))
5868 accessibility = default_accessibility;
5869 }
5870
Greg Claytonf0705c82011-10-22 03:33:13 +00005871 if (type_name_cstr && strchr (type_name_cstr, '<'))
5872 {
5873 ClangASTContext::TemplateParameterInfos template_param_infos;
5874 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
5875 {
5876 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00005877 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00005878 type_name_cstr,
5879 tag_decl_kind,
5880 template_param_infos);
5881
5882 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
5883 class_template_decl,
5884 tag_decl_kind,
5885 template_param_infos);
5886 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
5887 clang_type_was_created = true;
Sean Callanan60217122012-04-13 00:10:03 +00005888
Jim Ingham379397632012-10-27 02:54:13 +00005889 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)class_template_decl, MakeUserID(die->GetOffset()));
5890 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)class_specialization_decl, MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005891 }
5892 }
5893
5894 if (!clang_type_was_created)
5895 {
5896 clang_type_was_created = true;
Jim Ingham379397632012-10-27 02:54:13 +00005897 ClangASTMetadata metadata;
5898 metadata.SetUserID(MakeUserID(die->GetOffset()));
Greg Clayton55561e92011-10-26 03:31:36 +00005899 clang_type = ast.CreateRecordType (decl_ctx,
5900 accessibility,
5901 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00005902 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00005903 class_language,
Jim Ingham379397632012-10-27 02:54:13 +00005904 &metadata);
Greg Claytonf0705c82011-10-22 03:33:13 +00005905 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005906 }
5907
5908 // Store a forward declaration to this class type in case any
5909 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00005910 // types for function prototypes.
5911 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00005912 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005913 this,
5914 type_name_const_str,
5915 byte_size,
5916 NULL,
5917 LLDB_INVALID_UID,
5918 Type::eEncodingIsUID,
5919 &decl,
5920 clang_type,
5921 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00005922
5923 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005924
5925
5926 // Add our type to the unique type map so we don't
5927 // end up creating many copies of the same type over
5928 // and over in the ASTContext for our module
5929 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00005930 unique_ast_entry.m_symfile = this;
5931 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005932 unique_ast_entry.m_die = die;
5933 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00005934 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00005935 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
5936 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005937
Sean Callanan12014a02011-12-08 23:45:45 +00005938 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00005939 {
5940 // Always start the definition for a class type so that
5941 // if the class has child classes or types that require
5942 // the class to be created for use as their decl contexts
5943 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00005944 if (die->HasChildren() == false)
5945 {
5946 // No children for this struct/union/class, lets finish it
5947 ast.StartTagDeclarationDefinition (clang_type);
5948 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan433cd222012-10-19 01:37:25 +00005949
5950 if (tag == DW_TAG_structure_type) // this only applies in C
5951 {
5952 clang::QualType qual_type = clang::QualType::getFromOpaquePtr (clang_type);
5953 const clang::RecordType *record_type = qual_type->getAs<clang::RecordType> ();
5954
5955 if (record_type)
5956 {
5957 clang::RecordDecl *record_decl = record_type->getDecl();
5958
5959 if (record_decl)
5960 {
5961 LayoutInfo layout_info;
5962
5963 layout_info.alignment = 0;
5964 layout_info.bit_size = 0;
5965
5966 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
5967 }
5968 }
5969 }
Sean Callanan12014a02011-12-08 23:45:45 +00005970 }
5971 else if (clang_type_was_created)
5972 {
Greg Clayton219cf312012-03-30 00:51:13 +00005973 // Start the definition if the class is not objective C since
5974 // the underlying decls respond to isCompleteDefinition(). Objective
5975 // C decls dont' respond to isCompleteDefinition() so we can't
5976 // start the declaration definition right away. For C++ classs/union/structs
5977 // we want to start the definition in case the class is needed as the
5978 // declaration context for a contained class or type without the need
5979 // to complete that type..
5980
Sean Callanan7457f8d2012-04-25 01:03:57 +00005981 if (class_language != eLanguageTypeObjC &&
5982 class_language != eLanguageTypeObjC_plus_plus)
Greg Clayton219cf312012-03-30 00:51:13 +00005983 ast.StartTagDeclarationDefinition (clang_type);
5984
Sean Callanan12014a02011-12-08 23:45:45 +00005985 // Leave this as a forward declaration until we need
5986 // to know the details of the type. lldb_private::Type
5987 // will automatically call the SymbolFile virtual function
5988 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
5989 // When the definition needs to be defined.
5990 m_forward_decl_die_to_clang_type[die] = clang_type;
5991 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
5992 ClangASTContext::SetHasExternalStorage (clang_type, true);
5993 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005994 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005995
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005996 }
5997 break;
5998
5999 case DW_TAG_enumeration_type:
6000 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006001 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006002 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006003
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006004 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006005
Greg Claytond88d7592010-09-15 08:33:30 +00006006 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006007 if (num_attributes > 0)
6008 {
6009 uint32_t i;
6010
6011 for (i=0; i<num_attributes; ++i)
6012 {
6013 attr = attributes.AttributeAtIndex(i);
6014 DWARFFormValue form_value;
6015 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6016 {
6017 switch (attr)
6018 {
Greg Clayton7a345282010-11-09 23:46:37 +00006019 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6020 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6021 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006022 case DW_AT_name:
6023 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006024 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006025 break;
Greg Clayton7a345282010-11-09 23:46:37 +00006026 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006027 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
6028 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
6029 case DW_AT_declaration: break; //is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006030 case DW_AT_allocated:
6031 case DW_AT_associated:
6032 case DW_AT_bit_stride:
6033 case DW_AT_byte_stride:
6034 case DW_AT_data_location:
6035 case DW_AT_description:
6036 case DW_AT_start_scope:
6037 case DW_AT_visibility:
6038 case DW_AT_specification:
6039 case DW_AT_abstract_origin:
6040 case DW_AT_sibling:
6041 break;
6042 }
6043 }
6044 }
6045
Daniel Malead01b2952012-11-29 21:49:15 +00006046 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006047
Greg Clayton1be10fc2010-09-29 01:12:09 +00006048 clang_type_t enumerator_clang_type = NULL;
6049 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
6050 if (clang_type == NULL)
6051 {
Greg Clayton5d14fc02013-03-06 06:23:54 +00006052 if (encoding_uid != DW_INVALID_OFFSET)
6053 {
6054 Type *enumerator_type = ResolveTypeUID(encoding_uid);
6055 if (enumerator_type)
6056 enumerator_clang_type = enumerator_type->GetClangFullType();
6057 }
6058
6059 if (enumerator_clang_type == NULL)
6060 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
6061 DW_ATE_signed,
6062 byte_size * 8);
6063
Greg Claytonca512b32011-01-14 04:54:56 +00006064 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00006065 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00006066 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006067 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00006068 }
6069 else
6070 {
6071 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00006072 }
6073
Greg Claytona2721472011-06-25 00:44:06 +00006074 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
6075
Greg Clayton81c22f62011-10-19 18:09:39 +00006076 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006077 this,
6078 type_name_const_str,
6079 byte_size,
6080 NULL,
6081 encoding_uid,
6082 Type::eEncodingIsUID,
6083 &decl,
6084 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006085 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006086
Greg Clayton6beaaa62011-01-17 03:46:26 +00006087 ast.StartTagDeclarationDefinition (clang_type);
6088 if (die->HasChildren())
6089 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00006090 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Greg Clayton3e067532013-03-05 23:54:39 +00006091 bool is_signed = false;
Greg Clayton5d14fc02013-03-06 06:23:54 +00006092 ast.IsIntegerType(enumerator_clang_type, is_signed);
Greg Clayton3e067532013-03-05 23:54:39 +00006093 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006094 }
6095 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006096 }
6097 }
6098 break;
6099
Jim Inghamb0be4422010-08-12 01:20:14 +00006100 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006101 case DW_TAG_subprogram:
6102 case DW_TAG_subroutine_type:
6103 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006104 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006105 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006106
Greg Clayton23f59502012-07-17 03:23:13 +00006107 //const char *mangled = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006108 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00006109 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006110 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00006111 bool is_static = false;
6112 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00006113 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00006114 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00006115 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
6116 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Jim Ingham379397632012-10-27 02:54:13 +00006117 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00006118
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006119 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00006120 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006121
6122
Greg Claytond88d7592010-09-15 08:33:30 +00006123 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006124 if (num_attributes > 0)
6125 {
6126 uint32_t i;
6127 for (i=0; i<num_attributes; ++i)
6128 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00006129 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006130 DWARFFormValue form_value;
6131 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6132 {
6133 switch (attr)
6134 {
6135 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6136 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6137 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6138 case DW_AT_name:
6139 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006140 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006141 break;
6142
Greg Clayton71415542012-12-08 00:24:40 +00006143 case DW_AT_linkage_name:
Greg Clayton23f59502012-07-17 03:23:13 +00006144 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&get_debug_str_data()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006145 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00006146 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006147 case DW_AT_declaration: break; // is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00006148 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
6149 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00006150 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00006151 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
6152
Greg Claytonf51de672010-10-01 02:31:07 +00006153
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006154 case DW_AT_external:
6155 if (form_value.Unsigned())
6156 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00006157 if (storage == clang::SC_None)
6158 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006159 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00006160 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006161 }
6162 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006163
Greg Clayton72da3972011-08-16 18:40:23 +00006164 case DW_AT_specification:
6165 specification_die_offset = form_value.Reference(dwarf_cu);
6166 break;
6167
6168 case DW_AT_abstract_origin:
6169 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
6170 break;
6171
Jim Ingham379397632012-10-27 02:54:13 +00006172 case DW_AT_object_pointer:
6173 object_pointer_die_offset = form_value.Reference(dwarf_cu);
6174 break;
6175
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006176 case DW_AT_allocated:
6177 case DW_AT_associated:
6178 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006179 case DW_AT_calling_convention:
6180 case DW_AT_data_location:
6181 case DW_AT_elemental:
6182 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006183 case DW_AT_frame_base:
6184 case DW_AT_high_pc:
6185 case DW_AT_low_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006186 case DW_AT_prototyped:
6187 case DW_AT_pure:
6188 case DW_AT_ranges:
6189 case DW_AT_recursive:
6190 case DW_AT_return_addr:
6191 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006192 case DW_AT_start_scope:
6193 case DW_AT_static_link:
6194 case DW_AT_trampoline:
6195 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006196 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006197 case DW_AT_description:
6198 case DW_AT_sibling:
6199 break;
6200 }
6201 }
6202 }
Greg Clayton24739922010-10-13 03:15:28 +00006203 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006204
Jim Ingham379397632012-10-27 02:54:13 +00006205 std::string object_pointer_name;
6206 if (object_pointer_die_offset != DW_INVALID_OFFSET)
6207 {
6208 // Get the name from the object pointer die
6209 StreamString s;
6210 if (DWARFDebugInfoEntry::GetName (this, dwarf_cu, object_pointer_die_offset, s))
6211 {
6212 object_pointer_name.assign(s.GetData());
6213 }
6214 }
6215
Daniel Malead01b2952012-11-29 21:49:15 +00006216 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006217
Greg Clayton24739922010-10-13 03:15:28 +00006218 clang_type_t return_clang_type = NULL;
6219 Type *func_type = NULL;
6220
6221 if (type_die_offset != DW_INVALID_OFFSET)
6222 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00006223
Greg Clayton24739922010-10-13 03:15:28 +00006224 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00006225 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00006226 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006227 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006228
Greg Claytonf51de672010-10-01 02:31:07 +00006229
Greg Clayton24739922010-10-13 03:15:28 +00006230 std::vector<clang_type_t> function_param_types;
6231 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006232
Greg Clayton24739922010-10-13 03:15:28 +00006233 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00006234
Greg Claytoncb5860a2011-10-13 23:49:28 +00006235 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
6236 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00006237 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
6238
Greg Claytonf0705c82011-10-22 03:33:13 +00006239 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00006240 // Start off static. This will be set to false in ParseChildParameters(...)
6241 // if we find a "this" paramters as the first parameter
6242 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00006243 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006244 ClangASTContext::TemplateParameterInfos template_param_infos;
6245
Greg Clayton24739922010-10-13 03:15:28 +00006246 if (die->HasChildren())
6247 {
Greg Clayton0fffff52010-09-24 05:15:53 +00006248 bool skip_artificial = true;
Jim Ingham379397632012-10-27 02:54:13 +00006249 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00006250 containing_decl_ctx,
Jim Ingham379397632012-10-27 02:54:13 +00006251 dwarf_cu,
6252 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00006253 skip_artificial,
6254 is_static,
Jim Ingham379397632012-10-27 02:54:13 +00006255 type_list,
6256 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00006257 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006258 type_quals,
6259 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00006260 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006261
Greg Clayton24739922010-10-13 03:15:28 +00006262 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006263 clang_type = ast.CreateFunctionType (return_clang_type,
Greg Clayton9cb25c42012-10-30 17:02:18 +00006264 function_param_types.data(),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006265 function_param_types.size(),
6266 is_variadic,
6267 type_quals);
6268
Greg Clayton24739922010-10-13 03:15:28 +00006269 if (type_name_cstr)
6270 {
6271 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00006272 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006273 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00006274 ObjCLanguageRuntime::MethodName objc_method (type_name_cstr, true);
6275 if (objc_method.IsValid(true))
Greg Clayton0fffff52010-09-24 05:15:53 +00006276 {
Greg Clayton24739922010-10-13 03:15:28 +00006277 SymbolContext empty_sc;
6278 clang_type_t class_opaque_type = NULL;
Greg Clayton1b3815c2013-01-30 00:18:29 +00006279 ConstString class_name(objc_method.GetClassName());
Greg Clayton7c810422012-01-18 23:40:49 +00006280 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00006281 {
Greg Clayton24739922010-10-13 03:15:28 +00006282 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00006283 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00006284
6285 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00006286 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00006287 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
6288 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
6289 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00006290 }
Greg Clayton24739922010-10-13 03:15:28 +00006291 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006292
Greg Clayton24739922010-10-13 03:15:28 +00006293 if (class_opaque_type)
6294 {
6295 // If accessibility isn't set to anything valid, assume public for
6296 // now...
6297 if (accessibility == eAccessNone)
6298 accessibility = eAccessPublic;
6299
Sean Callanan464a5b52012-10-04 22:06:29 +00006300 clang::ObjCMethodDecl *objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
6301 type_name_cstr,
6302 clang_type,
6303 accessibility);
Greg Clayton24739922010-10-13 03:15:28 +00006304 type_handled = objc_method_decl != NULL;
Sean Callanan464a5b52012-10-04 22:06:29 +00006305 if (type_handled)
6306 {
6307 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Jim Ingham379397632012-10-27 02:54:13 +00006308 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)objc_method_decl, MakeUserID(die->GetOffset()));
Sean Callanan464a5b52012-10-04 22:06:29 +00006309 }
Sean Callananc3a1d562013-02-06 23:21:59 +00006310 else
6311 {
6312 GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invaliad Objective-C method 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
6313 die->GetOffset(),
6314 tag,
6315 DW_TAG_value_to_name(tag));
6316 }
Greg Clayton24739922010-10-13 03:15:28 +00006317 }
6318 }
Greg Clayton5113dc82011-08-12 06:47:54 +00006319 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00006320 {
6321 // Look at the parent of this DIE and see if is is
6322 // a class or struct and see if this is actually a
6323 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00006324 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00006325 if (class_type)
6326 {
Greg Clayton4116e932012-05-15 02:33:01 +00006327 if (class_type->GetID() != MakeUserID(decl_ctx_die->GetOffset()))
6328 {
6329 // We uniqued the parent class of this function to another class
6330 // so we now need to associate all dies under "decl_ctx_die" to
6331 // DIEs in the DIE for "class_type"...
Greg Clayton5d650c6a2013-02-26 01:31:37 +00006332 SymbolFileDWARF *class_symfile = NULL;
Greg Clayton4116e932012-05-15 02:33:01 +00006333 DWARFCompileUnitSP class_type_cu_sp;
Greg Clayton5d650c6a2013-02-26 01:31:37 +00006334 const DWARFDebugInfoEntry *class_type_die = NULL;
Sean Callanan2367f8a2013-02-26 01:12:25 +00006335
6336 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
6337 if (debug_map_symfile)
6338 {
6339 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID()));
6340 class_type_die = class_symfile->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6341 }
6342 else
6343 {
6344 class_symfile = this;
6345 class_type_die = DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6346 }
Greg Clayton4116e932012-05-15 02:33:01 +00006347 if (class_type_die)
6348 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00006349 llvm::SmallVector<const DWARFDebugInfoEntry *, 0> failures;
6350
6351 CopyUniqueClassMethodTypes (class_symfile,
6352 class_type,
6353 class_type_cu_sp.get(),
6354 class_type_die,
6355 dwarf_cu,
6356 decl_ctx_die,
6357 failures);
6358
6359 // FIXME do something with these failures that's smarter than
6360 // just dropping them on the ground. Unfortunately classes don't
6361 // like having stuff added to them after their definitions are
6362 // complete...
6363
6364 type_ptr = m_die_to_type[die];
6365 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton4116e932012-05-15 02:33:01 +00006366 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00006367 type_sp = type_ptr->shared_from_this();
6368 break;
Greg Clayton4116e932012-05-15 02:33:01 +00006369 }
6370 }
6371 }
6372
Greg Clayton72da3972011-08-16 18:40:23 +00006373 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00006374 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006375 // We have a specification which we are going to base our function
6376 // prototype off of, so we need this type to be completed so that the
6377 // m_die_to_decl_ctx for the method in the specification has a valid
6378 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006379 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00006380 // If we have a specification, then the function type should have been
6381 // made with the specification and not with this die.
6382 DWARFCompileUnitSP spec_cu_sp;
6383 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006384 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006385 if (spec_clang_decl_ctx)
6386 {
6387 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
6388 }
6389 else
Jim Inghamc1663042011-09-29 22:12:35 +00006390 {
Daniel Malead01b2952012-11-29 21:49:15 +00006391 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x) has no decl\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00006392 MakeUserID(die->GetOffset()),
6393 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006394 }
Greg Clayton72da3972011-08-16 18:40:23 +00006395 type_handled = true;
6396 }
6397 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
6398 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006399 // We have a specification which we are going to base our function
6400 // prototype off of, so we need this type to be completed so that the
6401 // m_die_to_decl_ctx for the method in the abstract origin has a valid
6402 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006403 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00006404
Greg Clayton72da3972011-08-16 18:40:23 +00006405 DWARFCompileUnitSP abs_cu_sp;
6406 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006407 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006408 if (abs_clang_decl_ctx)
6409 {
6410 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
6411 }
6412 else
Jim Inghamc1663042011-09-29 22:12:35 +00006413 {
Daniel Malead01b2952012-11-29 21:49:15 +00006414 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x) has no decl\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00006415 MakeUserID(die->GetOffset()),
6416 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006417 }
Greg Clayton72da3972011-08-16 18:40:23 +00006418 type_handled = true;
6419 }
6420 else
6421 {
6422 clang_type_t class_opaque_type = class_type->GetClangForwardType();
6423 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00006424 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006425 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00006426 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006427 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
6428 // in the DWARF for C++ methods... Default to public for now...
6429 if (accessibility == eAccessNone)
6430 accessibility = eAccessPublic;
6431
6432 if (!is_static && !die->HasChildren())
6433 {
6434 // We have a C++ member function with no children (this pointer!)
6435 // and clang will get mad if we try and make a function that isn't
6436 // well formed in the DWARF, so we will just skip it...
6437 type_handled = true;
6438 }
6439 else
6440 {
6441 clang::CXXMethodDecl *cxx_method_decl;
6442 // REMOVE THE CRASH DESCRIPTION BELOW
Daniel Malead01b2952012-11-29 21:49:15 +00006443 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s/%s",
Greg Clayton20568dd2011-10-13 23:13:20 +00006444 type_name_cstr,
6445 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00006446 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00006447 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
6448 m_obj_file->GetFileSpec().GetFilename().GetCString());
6449
Sean Callananc1b732d2011-11-01 18:07:13 +00006450 const bool is_attr_used = false;
6451
Greg Clayton20568dd2011-10-13 23:13:20 +00006452 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
6453 type_name_cstr,
6454 clang_type,
6455 accessibility,
6456 is_virtual,
6457 is_static,
6458 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00006459 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00006460 is_attr_used,
6461 is_artificial);
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006462
Greg Clayton20568dd2011-10-13 23:13:20 +00006463 type_handled = cxx_method_decl != NULL;
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006464
6465 if (type_handled)
Jim Ingham379397632012-10-27 02:54:13 +00006466 {
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006467 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
6468
6469 Host::SetCrashDescription (NULL);
6470
6471
6472 ClangASTMetadata metadata;
6473 metadata.SetUserID(MakeUserID(die->GetOffset()));
6474
6475 if (!object_pointer_name.empty())
6476 {
6477 metadata.SetObjectPtrName(object_pointer_name.c_str());
6478 if (log)
6479 log->Printf ("Setting object pointer name: %s on method object 0x%ld.\n",
6480 object_pointer_name.c_str(),
6481 (uintptr_t) cxx_method_decl);
6482 }
6483 GetClangASTContext().SetMetadata ((uintptr_t)cxx_method_decl, metadata);
Jim Ingham379397632012-10-27 02:54:13 +00006484 }
Greg Clayton20568dd2011-10-13 23:13:20 +00006485 }
Greg Clayton72da3972011-08-16 18:40:23 +00006486 }
6487 else
6488 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006489 // We were asked to parse the type for a method in a class, yet the
6490 // class hasn't been asked to complete itself through the
6491 // clang::ExternalASTSource protocol, so we need to just have the
6492 // class complete itself and do things the right way, then our
6493 // DIE should then have an entry in the m_die_to_type map. First
6494 // we need to modify the m_die_to_type so it doesn't think we are
6495 // trying to parse this DIE anymore...
6496 m_die_to_type[die] = NULL;
6497
6498 // Now we get the full type to force our class type to complete itself
6499 // using the clang::ExternalASTSource protocol which will parse all
6500 // base classes and all methods (including the method for this DIE).
6501 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006502
Greg Clayton20568dd2011-10-13 23:13:20 +00006503 // The type for this DIE should have been filled in the function call above
6504 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00006505 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton20568dd2011-10-13 23:13:20 +00006506 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006507 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00006508 break;
6509 }
Sean Callanan02eee4d2012-04-12 23:10:00 +00006510
6511 // FIXME This is fixing some even uglier behavior but we really need to
6512 // uniq the methods of each class as well as the class itself.
6513 // <rdar://problem/11240464>
6514 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00006515 }
Greg Clayton931180e2011-01-27 06:44:37 +00006516 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006517 }
6518 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006519 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006520 }
Greg Clayton24739922010-10-13 03:15:28 +00006521
6522 if (!type_handled)
6523 {
6524 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00006525 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
6526 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006527 clang_type,
6528 storage,
6529 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006530
6531// if (template_param_infos.GetSize() > 0)
6532// {
6533// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
6534// function_decl,
6535// type_name_cstr,
6536// template_param_infos);
6537//
6538// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
6539// func_template_decl,
6540// template_param_infos);
6541// }
Greg Clayton24739922010-10-13 03:15:28 +00006542 // Add the decl to our DIE to decl context map
6543 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00006544 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00006545 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006546 ast.SetFunctionParameters (function_decl,
6547 &function_param_decls.front(),
6548 function_param_decls.size());
Sean Callanan60217122012-04-13 00:10:03 +00006549
Jim Ingham379397632012-10-27 02:54:13 +00006550 ClangASTMetadata metadata;
6551 metadata.SetUserID(MakeUserID(die->GetOffset()));
6552
6553 if (!object_pointer_name.empty())
6554 {
6555 metadata.SetObjectPtrName(object_pointer_name.c_str());
Jim Ingham2cffda3f2012-10-30 23:34:07 +00006556 if (log)
6557 log->Printf ("Setting object pointer name: %s on function object 0x%ld.\n",
6558 object_pointer_name.c_str(),
6559 (uintptr_t) function_decl);
Jim Ingham379397632012-10-27 02:54:13 +00006560 }
6561 GetClangASTContext().SetMetadata ((uintptr_t)function_decl, metadata);
Greg Clayton24739922010-10-13 03:15:28 +00006562 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006563 }
Greg Clayton81c22f62011-10-19 18:09:39 +00006564 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006565 this,
6566 type_name_const_str,
6567 0,
6568 NULL,
6569 LLDB_INVALID_UID,
6570 Type::eEncodingIsUID,
6571 &decl,
6572 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006573 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00006574 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006575 }
6576 break;
6577
6578 case DW_TAG_array_type:
6579 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006580 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006581 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006582
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006583 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006584 int64_t first_index = 0;
6585 uint32_t byte_stride = 0;
6586 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00006587 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006588
6589 if (num_attributes > 0)
6590 {
6591 uint32_t i;
6592 for (i=0; i<num_attributes; ++i)
6593 {
6594 attr = attributes.AttributeAtIndex(i);
6595 DWARFFormValue form_value;
6596 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6597 {
6598 switch (attr)
6599 {
6600 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6601 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6602 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6603 case DW_AT_name:
6604 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006605 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006606 break;
6607
6608 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006609 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006610 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
6611 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006612 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
6613 case DW_AT_declaration: break; // is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006614 case DW_AT_allocated:
6615 case DW_AT_associated:
6616 case DW_AT_data_location:
6617 case DW_AT_description:
6618 case DW_AT_ordering:
6619 case DW_AT_start_scope:
6620 case DW_AT_visibility:
6621 case DW_AT_specification:
6622 case DW_AT_abstract_origin:
6623 case DW_AT_sibling:
6624 break;
6625 }
6626 }
6627 }
6628
Daniel Malead01b2952012-11-29 21:49:15 +00006629 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006630
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006631 Type *element_type = ResolveTypeUID(type_die_offset);
6632
6633 if (element_type)
6634 {
6635 std::vector<uint64_t> element_orders;
6636 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
6637 if (byte_stride == 0 && bit_stride == 0)
6638 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00006639 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006640 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
6641 uint64_t num_elements = 0;
6642 std::vector<uint64_t>::const_reverse_iterator pos;
6643 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
6644 for (pos = element_orders.rbegin(); pos != end; ++pos)
6645 {
6646 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006647 clang_type = ast.CreateArrayType (array_element_type,
Greg Clayton4ef877f2012-12-06 02:33:54 +00006648 num_elements);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006649 array_element_type = clang_type;
Greg Clayton4ef877f2012-12-06 02:33:54 +00006650 array_element_bit_stride = num_elements ? array_element_bit_stride * num_elements : array_element_bit_stride;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006651 }
6652 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00006653 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006654 this,
6655 empty_name,
6656 array_element_bit_stride / 8,
6657 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00006658 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006659 Type::eEncodingIsUID,
6660 &decl,
6661 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006662 Type::eResolveStateFull));
6663 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006664 }
6665 }
6666 }
6667 break;
6668
Greg Clayton9b81a312010-06-12 01:20:30 +00006669 case DW_TAG_ptr_to_member_type:
6670 {
6671 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
6672 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
6673
Greg Claytond88d7592010-09-15 08:33:30 +00006674 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00006675
6676 if (num_attributes > 0) {
6677 uint32_t i;
6678 for (i=0; i<num_attributes; ++i)
6679 {
6680 attr = attributes.AttributeAtIndex(i);
6681 DWARFFormValue form_value;
6682 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6683 {
6684 switch (attr)
6685 {
6686 case DW_AT_type:
6687 type_die_offset = form_value.Reference(dwarf_cu); break;
6688 case DW_AT_containing_type:
6689 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
6690 }
6691 }
6692 }
6693
6694 Type *pointee_type = ResolveTypeUID(type_die_offset);
6695 Type *class_type = ResolveTypeUID(containing_type_die_offset);
6696
Greg Clayton526e5af2010-11-13 03:52:47 +00006697 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
6698 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00006699
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006700 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
6701 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00006702
Greg Clayton526e5af2010-11-13 03:52:47 +00006703 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
6704 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00006705
Greg Clayton81c22f62011-10-19 18:09:39 +00006706 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006707 this,
6708 type_name_const_str,
6709 byte_size,
6710 NULL,
6711 LLDB_INVALID_UID,
6712 Type::eEncodingIsUID,
6713 NULL,
6714 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006715 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00006716 }
6717
6718 break;
6719 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006720 default:
Greg Clayton84afacd2012-11-07 23:09:32 +00006721 GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
6722 die->GetOffset(),
6723 tag,
6724 DW_TAG_value_to_name(tag));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006725 break;
6726 }
6727
6728 if (type_sp.get())
6729 {
6730 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6731 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6732
6733 SymbolContextScope * symbol_context_scope = NULL;
6734 if (sc_parent_tag == DW_TAG_compile_unit)
6735 {
6736 symbol_context_scope = sc.comp_unit;
6737 }
6738 else if (sc.function != NULL)
6739 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006740 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006741 if (symbol_context_scope == NULL)
6742 symbol_context_scope = sc.function;
6743 }
6744
6745 if (symbol_context_scope != NULL)
6746 {
6747 type_sp->SetSymbolContextScope(symbol_context_scope);
6748 }
6749
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006750 // We are ready to put this type into the uniqued list up at the module level
6751 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00006752
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006753 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006754 }
6755 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00006756 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006757 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006758 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006759 }
6760 }
6761 return type_sp;
6762}
6763
6764size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00006765SymbolFileDWARF::ParseTypes
6766(
6767 const SymbolContext& sc,
6768 DWARFCompileUnit* dwarf_cu,
6769 const DWARFDebugInfoEntry *die,
6770 bool parse_siblings,
6771 bool parse_children
6772)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006773{
6774 size_t types_added = 0;
6775 while (die != NULL)
6776 {
6777 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00006778 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006779 {
6780 if (type_is_new)
6781 ++types_added;
6782 }
6783
6784 if (parse_children && die->HasChildren())
6785 {
6786 if (die->Tag() == DW_TAG_subprogram)
6787 {
6788 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00006789 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006790 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
6791 }
6792 else
6793 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
6794 }
6795
6796 if (parse_siblings)
6797 die = die->GetSibling();
6798 else
6799 die = NULL;
6800 }
6801 return types_added;
6802}
6803
6804
6805size_t
6806SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
6807{
6808 assert(sc.comp_unit && sc.function);
6809 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00006810 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006811 if (dwarf_cu)
6812 {
6813 dw_offset_t function_die_offset = sc.function->GetID();
6814 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
6815 if (function_die)
6816 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00006817 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006818 }
6819 }
6820
6821 return functions_added;
6822}
6823
6824
6825size_t
6826SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
6827{
6828 // At least a compile unit must be valid
6829 assert(sc.comp_unit);
6830 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00006831 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006832 if (dwarf_cu)
6833 {
6834 if (sc.function)
6835 {
6836 dw_offset_t function_die_offset = sc.function->GetID();
6837 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
6838 if (func_die && func_die->HasChildren())
6839 {
6840 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
6841 }
6842 }
6843 else
6844 {
6845 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
6846 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
6847 {
6848 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
6849 }
6850 }
6851 }
6852
6853 return types_added;
6854}
6855
6856size_t
6857SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
6858{
6859 if (sc.comp_unit != NULL)
6860 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00006861 DWARFDebugInfo* info = DebugInfo();
6862 if (info == NULL)
6863 return 0;
6864
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006865 if (sc.function)
6866 {
Greg Clayton9422dd62013-03-04 21:46:16 +00006867 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitContainingDIE(sc.function->GetID()).get();
6868
6869 if (dwarf_cu == NULL)
6870 return 0;
6871
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006872 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00006873
Greg Claytonc7bece562013-01-25 18:06:21 +00006874 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
6875 if (func_lo_pc != LLDB_INVALID_ADDRESS)
Greg Claytone38a5ed2012-01-05 03:57:59 +00006876 {
6877 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00006878
Greg Claytone38a5ed2012-01-05 03:57:59 +00006879 // Let all blocks know they have parse all their variables
6880 sc.function->GetBlock (false).SetDidParseVariables (true, true);
6881 return num_variables;
6882 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006883 }
6884 else if (sc.comp_unit)
6885 {
Greg Clayton9422dd62013-03-04 21:46:16 +00006886 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID()).get();
6887
6888 if (dwarf_cu == NULL)
6889 return 0;
6890
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006891 uint32_t vars_added = 0;
6892 VariableListSP variables (sc.comp_unit->GetVariableList(false));
6893
6894 if (variables.get() == NULL)
6895 {
6896 variables.reset(new VariableList());
6897 sc.comp_unit->SetVariableList(variables);
6898
Greg Claytond4a2b372011-09-12 23:21:58 +00006899 DWARFCompileUnit* match_dwarf_cu = NULL;
6900 const DWARFDebugInfoEntry* die = NULL;
6901 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00006902 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00006903 {
Greg Clayton97fbc342011-10-20 22:30:33 +00006904 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00006905 {
6906 DWARFMappedHash::DIEInfoArray hash_data_array;
6907 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
6908 dwarf_cu->GetNextCompileUnitOffset(),
6909 hash_data_array))
6910 {
6911 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
6912 }
6913 }
Greg Clayton7f995132011-10-04 22:41:51 +00006914 }
6915 else
6916 {
6917 // Index if we already haven't to make sure the compile units
6918 // get indexed and make their global DIE index list
6919 if (!m_indexed)
6920 Index ();
6921
6922 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
6923 dwarf_cu->GetNextCompileUnitOffset(),
6924 die_offsets);
6925 }
6926
6927 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00006928 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006929 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006930 DWARFDebugInfo* debug_info = DebugInfo();
6931 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006932 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006933 const dw_offset_t die_offset = die_offsets[i];
6934 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00006935 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00006936 {
Greg Clayton95d87902011-11-11 03:16:25 +00006937 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
6938 if (var_sp)
6939 {
6940 variables->AddVariableIfUnique (var_sp);
6941 ++vars_added;
6942 }
Greg Claytond4a2b372011-09-12 23:21:58 +00006943 }
Greg Clayton95d87902011-11-11 03:16:25 +00006944 else
6945 {
6946 if (m_using_apple_tables)
6947 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006948 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 +00006949 }
6950 }
6951
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006952 }
6953 }
6954 }
6955 return vars_added;
6956 }
6957 }
6958 return 0;
6959}
6960
6961
6962VariableSP
6963SymbolFileDWARF::ParseVariableDIE
6964(
6965 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006966 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006967 const DWARFDebugInfoEntry *die,
6968 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006969)
6970{
6971
Greg Clayton83c5cd92010-11-14 22:13:40 +00006972 VariableSP var_sp (m_die_to_variable_sp[die]);
6973 if (var_sp)
6974 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006975
6976 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00006977
6978 if ((tag == DW_TAG_variable) ||
6979 (tag == DW_TAG_constant) ||
6980 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006981 {
Greg Clayton7f995132011-10-04 22:41:51 +00006982 DWARFDebugInfoEntry::Attributes attributes;
6983 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
6984 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006985 {
Greg Clayton7f995132011-10-04 22:41:51 +00006986 const char *name = NULL;
6987 const char *mangled = NULL;
6988 Declaration decl;
6989 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00006990 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00006991 DWARFExpression location;
6992 bool is_external = false;
6993 bool is_artificial = false;
6994 bool location_is_const_value_data = false;
Andrew Kaylorb32581f2013-02-13 19:57:06 +00006995 bool has_explicit_location = false;
Greg Clayton23f59502012-07-17 03:23:13 +00006996 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00006997
6998 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006999 {
Greg Clayton7f995132011-10-04 22:41:51 +00007000 dw_attr_t attr = attributes.AttributeAtIndex(i);
7001 DWARFFormValue form_value;
7002 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007003 {
Greg Clayton7f995132011-10-04 22:41:51 +00007004 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007005 {
Greg Clayton7f995132011-10-04 22:41:51 +00007006 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
7007 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
7008 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
7009 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton71415542012-12-08 00:24:40 +00007010 case DW_AT_linkage_name:
Greg Clayton7f995132011-10-04 22:41:51 +00007011 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00007012 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00007013 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
7014 case DW_AT_const_value:
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007015 // If we have already found a DW_AT_location attribute, ignore this attribute.
7016 if (!has_explicit_location)
7017 {
7018 location_is_const_value_data = true;
7019 // The constant value will be either a block, a data value or a string.
7020 const DataExtractor& debug_info_data = get_debug_info_data();
7021 if (DWARFFormValue::IsBlockForm(form_value.Form()))
7022 {
7023 // Retrieve the value as a block expression.
7024 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
7025 uint32_t block_length = form_value.Unsigned();
7026 location.CopyOpcodeData(debug_info_data, block_offset, block_length);
7027 }
7028 else if (DWARFFormValue::IsDataForm(form_value.Form()))
7029 {
7030 // Retrieve the value as a data expression.
7031 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
7032 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
7033 uint32_t data_length = fixed_form_sizes[form_value.Form()];
7034 location.CopyOpcodeData(debug_info_data, data_offset, data_length);
7035 }
7036 else
7037 {
7038 // Retrieve the value as a string expression.
7039 if (form_value.Form() == DW_FORM_strp)
7040 {
7041 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
7042 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
7043 uint32_t data_length = fixed_form_sizes[form_value.Form()];
7044 location.CopyOpcodeData(debug_info_data, data_offset, data_length);
7045 }
7046 else
7047 {
7048 const char *str = form_value.AsCString(&debug_info_data);
7049 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart();
7050 uint32_t string_length = strlen(str) + 1;
7051 location.CopyOpcodeData(debug_info_data, string_offset, string_length);
7052 }
7053 }
7054 }
7055 break;
Greg Clayton7f995132011-10-04 22:41:51 +00007056 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007057 {
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007058 location_is_const_value_data = false;
7059 has_explicit_location = true;
Greg Clayton7f995132011-10-04 22:41:51 +00007060 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007061 {
Greg Clayton7f995132011-10-04 22:41:51 +00007062 const DataExtractor& debug_info_data = get_debug_info_data();
7063
7064 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
7065 uint32_t block_length = form_value.Unsigned();
Greg Clayton7f4c7432012-08-11 00:49:14 +00007066 location.CopyOpcodeData(get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00007067 }
7068 else
7069 {
7070 const DataExtractor& debug_loc_data = get_debug_loc_data();
7071 const dw_offset_t debug_loc_offset = form_value.Unsigned();
7072
7073 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
7074 if (loc_list_length > 0)
7075 {
Greg Clayton7f4c7432012-08-11 00:49:14 +00007076 location.CopyOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00007077 assert (func_low_pc != LLDB_INVALID_ADDRESS);
7078 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
7079 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007080 }
7081 }
Greg Clayton7f995132011-10-04 22:41:51 +00007082 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007083
Greg Clayton7f995132011-10-04 22:41:51 +00007084 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Greg Clayton23f59502012-07-17 03:23:13 +00007085 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00007086 case DW_AT_declaration:
7087 case DW_AT_description:
7088 case DW_AT_endianity:
7089 case DW_AT_segment:
7090 case DW_AT_start_scope:
7091 case DW_AT_visibility:
7092 default:
7093 case DW_AT_abstract_origin:
7094 case DW_AT_sibling:
7095 case DW_AT_specification:
7096 break;
7097 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007098 }
7099 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007100
Greg Clayton7f995132011-10-04 22:41:51 +00007101 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007102 {
Greg Clayton7f995132011-10-04 22:41:51 +00007103 ValueType scope = eValueTypeInvalid;
7104
7105 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
7106 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007107 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00007108
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007109 // DWARF doesn't specify if a DW_TAG_variable is a local, global
7110 // or static variable, so we have to do a little digging by
7111 // looking at the location of a varaible to see if it contains
7112 // a DW_OP_addr opcode _somewhere_ in the definition. I say
7113 // somewhere because clang likes to combine small global variables
7114 // into the same symbol and have locations like:
7115 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
7116 // So if we don't have a DW_TAG_formal_parameter, we can look at
7117 // the location to see if it contains a DW_OP_addr opcode, and
7118 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00007119 if (tag == DW_TAG_formal_parameter)
7120 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00007121 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007122 {
Greg Clayton96c09682012-01-04 22:56:43 +00007123 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00007124 // Check if the location has a DW_OP_addr with any address value...
Greg Clayton9422dd62013-03-04 21:46:16 +00007125 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
Greg Claytond1767f02011-12-08 02:13:16 +00007126 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00007127 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007128 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error);
Greg Clayton96c09682012-01-04 22:56:43 +00007129 if (op_error)
7130 {
7131 StreamString strm;
7132 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00007133 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 +00007134 }
7135 }
Greg Claytond1767f02011-12-08 02:13:16 +00007136
Greg Clayton9422dd62013-03-04 21:46:16 +00007137 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007138 {
Greg Claytond1767f02011-12-08 02:13:16 +00007139 if (is_external)
Greg Claytond1767f02011-12-08 02:13:16 +00007140 scope = eValueTypeVariableGlobal;
Greg Clayton9422dd62013-03-04 21:46:16 +00007141 else
7142 scope = eValueTypeVariableStatic;
7143
7144
7145 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile ();
7146
7147 if (debug_map_symfile)
7148 {
7149 // When leaving the DWARF in the .o files on darwin,
7150 // when we have a global variable that wasn't initialized,
7151 // the .o file might not have allocated a virtual
7152 // address for the global variable. In this case it will
7153 // have created a symbol for the global variable
7154 // that is undefined/data and external and the value will
7155 // be the byte size of the variable. When we do the
7156 // address map in SymbolFileDWARFDebugMap we rely on
7157 // having an address, we need to do some magic here
7158 // so we can get the correct address for our global
7159 // variable. The address for all of these entries
7160 // will be zero, and there will be an undefined symbol
7161 // in this object file, and the executable will have
7162 // a matching symbol with a good address. So here we
7163 // dig up the correct address and replace it in the
7164 // location for the variable, and set the variable's
7165 // symbol context scope to be that of the main executable
7166 // so the file address will resolve correctly.
7167 bool linked_oso_file_addr = false;
7168 if (is_external && location_DW_OP_addr == 0)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007169 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007170
7171 // we have a possible uninitialized extern global
7172 ConstString const_name(mangled ? mangled : name);
7173 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
7174 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007175 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007176 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
7177 if (debug_map_symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007178 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007179 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
7180 eSymbolTypeData,
7181 Symtab::eDebugYes,
7182 Symtab::eVisibilityExtern);
7183 if (exe_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007184 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007185 if (exe_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007186 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007187 const addr_t exe_file_addr = exe_symbol->GetAddress().GetFileAddress();
7188 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007189 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007190 if (location.Update_DW_OP_addr (exe_file_addr))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007191 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007192 linked_oso_file_addr = true;
7193 symbol_context_scope = exe_symbol;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007194 }
7195 }
7196 }
7197 }
7198 }
7199 }
7200 }
Greg Clayton9422dd62013-03-04 21:46:16 +00007201
7202 if (!linked_oso_file_addr)
7203 {
7204 // The DW_OP_addr is not zero, but it contains a .o file address which
7205 // needs to be linked up correctly.
7206 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
7207 if (exe_file_addr != LLDB_INVALID_ADDRESS)
7208 {
7209 // Update the file address for this variable
7210 location.Update_DW_OP_addr (exe_file_addr);
7211 }
7212 else
7213 {
7214 // Variable didn't make it into the final executable
7215 return var_sp;
7216 }
7217 }
Greg Claytond1767f02011-12-08 02:13:16 +00007218 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007219 }
7220 else
Greg Claytond1767f02011-12-08 02:13:16 +00007221 {
7222 scope = eValueTypeVariableLocal;
7223 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007224 }
Greg Clayton7f995132011-10-04 22:41:51 +00007225
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007226 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00007227 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007228 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00007229 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007230 case DW_TAG_subprogram:
7231 case DW_TAG_inlined_subroutine:
7232 case DW_TAG_lexical_block:
7233 if (sc.function)
7234 {
7235 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
7236 if (symbol_context_scope == NULL)
7237 symbol_context_scope = sc.function;
7238 }
7239 break;
7240
7241 default:
7242 symbol_context_scope = sc.comp_unit;
7243 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00007244 }
Greg Clayton7f995132011-10-04 22:41:51 +00007245 }
7246
Greg Clayton5cf58b92011-10-05 22:22:08 +00007247 if (symbol_context_scope)
7248 {
Greg Clayton81c22f62011-10-19 18:09:39 +00007249 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
7250 name,
7251 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00007252 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00007253 scope,
7254 symbol_context_scope,
7255 &decl,
7256 location,
7257 is_external,
7258 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00007259
7260 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
7261 }
7262 else
7263 {
7264 // Not ready to parse this variable yet. It might be a global
7265 // or static variable that is in a function scope and the function
7266 // in the symbol context wasn't filled in yet
7267 return var_sp;
7268 }
Greg Clayton7f995132011-10-04 22:41:51 +00007269 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007270 }
Greg Clayton7f995132011-10-04 22:41:51 +00007271 // Cache var_sp even if NULL (the variable was just a specification or
7272 // was missing vital information to be able to be displayed in the debugger
7273 // (missing location due to optimization, etc)) so we don't re-parse
7274 // this DIE over and over later...
7275 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007276 }
7277 return var_sp;
7278}
7279
Greg Claytonc662ec82011-06-17 22:10:16 +00007280
7281const DWARFDebugInfoEntry *
7282SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
7283 dw_offset_t spec_block_die_offset,
7284 DWARFCompileUnit **result_die_cu_handle)
7285{
7286 // Give the concrete function die specified by "func_die_offset", find the
7287 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
7288 // to "spec_block_die_offset"
7289 DWARFDebugInfo* info = DebugInfo();
7290
7291 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
7292 if (die)
7293 {
7294 assert (*result_die_cu_handle);
7295 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
7296 }
7297 return NULL;
7298}
7299
7300
7301const DWARFDebugInfoEntry *
7302SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
7303 const DWARFDebugInfoEntry *die,
7304 dw_offset_t spec_block_die_offset,
7305 DWARFCompileUnit **result_die_cu_handle)
7306{
7307 if (die)
7308 {
7309 switch (die->Tag())
7310 {
7311 case DW_TAG_subprogram:
7312 case DW_TAG_inlined_subroutine:
7313 case DW_TAG_lexical_block:
7314 {
7315 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
7316 {
7317 *result_die_cu_handle = dwarf_cu;
7318 return die;
7319 }
7320
7321 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
7322 {
7323 *result_die_cu_handle = dwarf_cu;
7324 return die;
7325 }
7326 }
7327 break;
7328 }
7329
7330 // Give the concrete function die specified by "func_die_offset", find the
7331 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
7332 // to "spec_block_die_offset"
7333 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
7334 {
7335 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
7336 child_die,
7337 spec_block_die_offset,
7338 result_die_cu_handle);
7339 if (result_die)
7340 return result_die;
7341 }
7342 }
7343
7344 *result_die_cu_handle = NULL;
7345 return NULL;
7346}
7347
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007348size_t
7349SymbolFileDWARF::ParseVariables
7350(
7351 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00007352 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00007353 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007354 const DWARFDebugInfoEntry *orig_die,
7355 bool parse_siblings,
7356 bool parse_children,
7357 VariableList* cc_variable_list
7358)
7359{
7360 if (orig_die == NULL)
7361 return 0;
7362
Greg Claytonc662ec82011-06-17 22:10:16 +00007363 VariableListSP variable_list_sp;
7364
7365 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007366 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00007367 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007368 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007369 dw_tag_t tag = die->Tag();
7370
7371 // Check to see if we have already parsed this variable or constant?
7372 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007373 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007374 if (cc_variable_list)
7375 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007376 }
7377 else
7378 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007379 // We haven't already parsed it, lets do that now.
7380 if ((tag == DW_TAG_variable) ||
7381 (tag == DW_TAG_constant) ||
7382 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007383 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007384 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00007385 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007386 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
7387 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
7388 switch (parent_tag)
7389 {
7390 case DW_TAG_compile_unit:
7391 if (sc.comp_unit != NULL)
7392 {
7393 variable_list_sp = sc.comp_unit->GetVariableList(false);
7394 if (variable_list_sp.get() == NULL)
7395 {
7396 variable_list_sp.reset(new VariableList());
7397 sc.comp_unit->SetVariableList(variable_list_sp);
7398 }
7399 }
7400 else
7401 {
Daniel Malead01b2952012-11-29 21:49:15 +00007402 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8" PRIx64 " %s with no valid compile unit in symbol context for 0x%8.8" PRIx64 " %s.\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00007403 MakeUserID(sc_parent_die->GetOffset()),
7404 DW_TAG_value_to_name (parent_tag),
7405 MakeUserID(orig_die->GetOffset()),
7406 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007407 }
7408 break;
7409
7410 case DW_TAG_subprogram:
7411 case DW_TAG_inlined_subroutine:
7412 case DW_TAG_lexical_block:
7413 if (sc.function != NULL)
7414 {
7415 // Check to see if we already have parsed the variables for the given scope
7416
Greg Clayton81c22f62011-10-19 18:09:39 +00007417 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007418 if (block == NULL)
7419 {
7420 // This must be a specification or abstract origin with
7421 // a concrete block couterpart in the current function. We need
7422 // to find the concrete block so we can correctly add the
7423 // variable to it
7424 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
7425 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
7426 sc_parent_die->GetOffset(),
7427 &concrete_block_die_cu);
7428 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00007429 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007430 }
7431
7432 if (block != NULL)
7433 {
7434 const bool can_create = false;
7435 variable_list_sp = block->GetBlockVariableList (can_create);
7436 if (variable_list_sp.get() == NULL)
7437 {
7438 variable_list_sp.reset(new VariableList());
7439 block->SetVariableList(variable_list_sp);
7440 }
7441 }
7442 }
7443 break;
7444
7445 default:
Daniel Malead01b2952012-11-29 21:49:15 +00007446 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8" PRIx64 " %s.\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00007447 MakeUserID(orig_die->GetOffset()),
7448 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007449 break;
7450 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00007451 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007452
7453 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007454 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00007455 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
7456 if (var_sp)
7457 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007458 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00007459 if (cc_variable_list)
7460 cc_variable_list->AddVariableIfUnique (var_sp);
7461 ++vars_added;
7462 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007463 }
7464 }
7465 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007466
7467 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
7468
7469 if (!skip_children && parse_children && die->HasChildren())
7470 {
7471 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
7472 }
7473
7474 if (parse_siblings)
7475 die = die->GetSibling();
7476 else
7477 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007478 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007479 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007480}
7481
7482//------------------------------------------------------------------
7483// PluginInterface protocol
7484//------------------------------------------------------------------
7485const char *
7486SymbolFileDWARF::GetPluginName()
7487{
7488 return "SymbolFileDWARF";
7489}
7490
7491const char *
7492SymbolFileDWARF::GetShortPluginName()
7493{
7494 return GetPluginNameStatic();
7495}
7496
7497uint32_t
7498SymbolFileDWARF::GetPluginVersion()
7499{
7500 return 1;
7501}
7502
7503void
Greg Clayton6beaaa62011-01-17 03:46:26 +00007504SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
7505{
7506 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7507 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7508 if (clang_type)
7509 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7510}
7511
7512void
7513SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
7514{
7515 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7516 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7517 if (clang_type)
7518 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7519}
7520
Greg Claytona2721472011-06-25 00:44:06 +00007521void
Sean Callanancc427fa2011-07-30 02:42:06 +00007522SymbolFileDWARF::DumpIndexes ()
7523{
7524 StreamFile s(stdout, false);
7525
7526 s.Printf ("DWARF index for (%s) '%s/%s':",
7527 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
7528 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
7529 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
7530 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
7531 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
7532 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
7533 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
7534 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
7535 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
7536 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
7537 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
7538}
7539
7540void
7541SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
7542 const char *name,
7543 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00007544{
Sean Callanancc427fa2011-07-30 02:42:06 +00007545 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00007546
7547 if (iter == m_decl_ctx_to_die.end())
7548 return;
7549
Greg Claytoncb5860a2011-10-13 23:49:28 +00007550 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00007551 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007552 const DWARFDebugInfoEntry *context_die = *pos;
7553
7554 if (!results)
7555 return;
7556
7557 DWARFDebugInfo* info = DebugInfo();
7558
7559 DIEArray die_offsets;
7560
7561 DWARFCompileUnit* dwarf_cu = NULL;
7562 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00007563
7564 if (m_using_apple_tables)
7565 {
7566 if (m_apple_types_ap.get())
7567 m_apple_types_ap->FindByName (name, die_offsets);
7568 }
7569 else
7570 {
7571 if (!m_indexed)
7572 Index ();
7573
7574 m_type_index.Find (ConstString(name), die_offsets);
7575 }
7576
Greg Clayton220a0072011-12-09 08:48:30 +00007577 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007578
7579 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00007580 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007581 for (size_t i = 0; i < num_matches; ++i)
7582 {
7583 const dw_offset_t die_offset = die_offsets[i];
7584 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00007585
Greg Claytoncb5860a2011-10-13 23:49:28 +00007586 if (die->GetParent() != context_die)
7587 continue;
7588
7589 Type *matching_type = ResolveType (dwarf_cu, die);
7590
Greg Clayton42ce2f32011-12-12 21:50:19 +00007591 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007592 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
7593
7594 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
7595 {
7596 clang::TagDecl *tag_decl = tag_type->getDecl();
7597 results->push_back(tag_decl);
7598 }
7599 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
7600 {
7601 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
7602 results->push_back(typedef_decl);
7603 }
Greg Claytona2721472011-06-25 00:44:06 +00007604 }
7605 }
7606 }
7607}
7608
7609void
7610SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00007611 const clang::DeclContext *decl_context,
7612 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00007613 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
7614{
Greg Clayton85ae2e12011-10-18 23:36:41 +00007615
7616 switch (decl_context->getDeclKind())
7617 {
7618 case clang::Decl::Namespace:
7619 case clang::Decl::TranslationUnit:
7620 {
7621 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7622 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
7623 }
7624 break;
7625 default:
7626 break;
7627 }
Greg Claytona2721472011-06-25 00:44:06 +00007628}
Greg Claytoncaab74e2012-01-28 00:48:57 +00007629
7630bool
7631SymbolFileDWARF::LayoutRecordType (void *baton,
7632 const clang::RecordDecl *record_decl,
7633 uint64_t &size,
7634 uint64_t &alignment,
7635 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7636 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7637 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7638{
7639 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7640 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
7641}
7642
7643
7644bool
7645SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
7646 uint64_t &bit_size,
7647 uint64_t &alignment,
7648 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7649 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7650 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7651{
7652 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
7653 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
7654 bool success = false;
7655 base_offsets.clear();
7656 vbase_offsets.clear();
7657 if (pos != m_record_decl_to_layout_map.end())
7658 {
7659 bit_size = pos->second.bit_size;
7660 alignment = pos->second.alignment;
7661 field_offsets.swap(pos->second.field_offsets);
Greg Clayton2508b9b2012-11-01 23:20:02 +00007662 base_offsets.swap (pos->second.base_offsets);
7663 vbase_offsets.swap (pos->second.vbase_offsets);
Greg Claytoncaab74e2012-01-28 00:48:57 +00007664 m_record_decl_to_layout_map.erase(pos);
7665 success = true;
7666 }
7667 else
7668 {
7669 bit_size = 0;
7670 alignment = 0;
7671 field_offsets.clear();
7672 }
7673
7674 if (log)
7675 GetObjectFile()->GetModule()->LogMessage (log.get(),
Daniel Malead01b2952012-11-29 21:49:15 +00007676 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
Greg Claytoncaab74e2012-01-28 00:48:57 +00007677 record_decl,
7678 bit_size,
7679 alignment,
7680 (uint32_t)field_offsets.size(),
7681 (uint32_t)base_offsets.size(),
7682 (uint32_t)vbase_offsets.size(),
7683 success);
7684 return success;
7685}
7686
7687
Greg Clayton1f746072012-08-29 21:13:06 +00007688SymbolFileDWARFDebugMap *
7689SymbolFileDWARF::GetDebugMapSymfile ()
7690{
7691 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
7692 {
7693 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
7694 if (module_sp)
7695 {
7696 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
7697 if (sym_vendor)
7698 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
7699 }
7700 }
7701 return m_debug_map_symfile;
7702}
7703
Greg Claytoncaab74e2012-01-28 00:48:57 +00007704