blob: 5f1a457d774ff147269e85b6e83381aea3e2f140 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Greg Clayton3c2e3ae2012-02-06 06:42:51 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "clang/Basic/Builtins.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000025#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanancc427fa2011-07-30 02:42:06 +000027#include "llvm/Support/Casting.h"
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Module.h"
30#include "lldb/Core/PluginManager.h"
31#include "lldb/Core/RegularExpression.h"
32#include "lldb/Core/Scalar.h"
33#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000034#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000035#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Core/Timer.h"
37#include "lldb/Core/Value.h"
38
Greg Clayton20568dd2011-10-13 23:13:20 +000039#include "lldb/Host/Host.h"
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000042#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Symbol/CompileUnit.h"
44#include "lldb/Symbol/LineTable.h"
45#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
47#include "lldb/Symbol/VariableList.h"
48
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000049#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000050#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000051
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "DWARFCompileUnit.h"
53#include "DWARFDebugAbbrev.h"
54#include "DWARFDebugAranges.h"
55#include "DWARFDebugInfo.h"
56#include "DWARFDebugInfoEntry.h"
57#include "DWARFDebugLine.h"
58#include "DWARFDebugPubnames.h"
59#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000060#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFDIECollection.h"
62#include "DWARFFormValue.h"
63#include "DWARFLocationList.h"
64#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000065#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
67#include <map>
68
Greg Clayton62742b12010-11-11 01:09:45 +000069//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000070
71#ifdef ENABLE_DEBUG_PRINTF
72#include <stdio.h>
73#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
74#else
75#define DEBUG_PRINTF(fmt, ...)
76#endif
77
Greg Clayton594e5ed2010-09-27 21:07:38 +000078#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079
80using namespace lldb;
81using namespace lldb_private;
82
Greg Clayton219cf312012-03-30 00:51:13 +000083//static inline bool
84//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
85//{
86// switch (tag)
87// {
88// default:
89// break;
90// case DW_TAG_subprogram:
91// case DW_TAG_inlined_subroutine:
92// case DW_TAG_class_type:
93// case DW_TAG_structure_type:
94// case DW_TAG_union_type:
95// return true;
96// }
97// return false;
98//}
99//
Sean Callananc7fbf732010-08-06 00:32:49 +0000100static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000101DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102{
103 switch (dwarf_accessibility)
104 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000105 case DW_ACCESS_public: return eAccessPublic;
106 case DW_ACCESS_private: return eAccessPrivate;
107 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000108 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000110 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111}
112
Greg Clayton1fba87112012-02-09 20:03:49 +0000113#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
114
115class DIEStack
116{
117public:
118
119 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
120 {
121 m_dies.push_back (DIEInfo(cu, die));
122 }
123
124
125 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
126 {
127 StreamString log_strm;
128 const size_t n = m_dies.size();
129 log_strm.Printf("DIEStack[%zu]:\n", n);
130 for (size_t i=0; i<n; i++)
131 {
132 DWARFCompileUnit *cu = m_dies[i].cu;
133 const DWARFDebugInfoEntry *die = m_dies[i].die;
134 std::string qualified_name;
135 die->GetQualifiedName(dwarf, cu, qualified_name);
136 log_strm.Printf ("[%zu] 0x%8.8x: %s name='%s'\n",
137 i,
138 die->GetOffset(),
139 DW_TAG_value_to_name(die->Tag()),
140 qualified_name.c_str());
141 }
142 log->PutCString(log_strm.GetData());
143 }
144 void Pop ()
145 {
146 m_dies.pop_back();
147 }
148
149 class ScopedPopper
150 {
151 public:
152 ScopedPopper (DIEStack &die_stack) :
153 m_die_stack (die_stack),
154 m_valid (false)
155 {
156 }
157
158 void
159 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
160 {
161 m_valid = true;
162 m_die_stack.Push (cu, die);
163 }
164
165 ~ScopedPopper ()
166 {
167 if (m_valid)
168 m_die_stack.Pop();
169 }
170
171
172
173 protected:
174 DIEStack &m_die_stack;
175 bool m_valid;
176 };
177
178protected:
179 struct DIEInfo {
180 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
181 cu(c),
182 die(d)
183 {
184 }
185 DWARFCompileUnit *cu;
186 const DWARFDebugInfoEntry *die;
187 };
188 typedef std::vector<DIEInfo> Stack;
189 Stack m_dies;
190};
191#endif
192
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193void
194SymbolFileDWARF::Initialize()
195{
196 LogChannelDWARF::Initialize();
197 PluginManager::RegisterPlugin (GetPluginNameStatic(),
198 GetPluginDescriptionStatic(),
199 CreateInstance);
200}
201
202void
203SymbolFileDWARF::Terminate()
204{
205 PluginManager::UnregisterPlugin (CreateInstance);
206 LogChannelDWARF::Initialize();
207}
208
209
210const char *
211SymbolFileDWARF::GetPluginNameStatic()
212{
Greg Claytond4a2b372011-09-12 23:21:58 +0000213 return "dwarf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214}
215
216const char *
217SymbolFileDWARF::GetPluginDescriptionStatic()
218{
219 return "DWARF and DWARF3 debug symbol file reader.";
220}
221
222
223SymbolFile*
224SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
225{
226 return new SymbolFileDWARF(obj_file);
227}
228
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000229TypeList *
230SymbolFileDWARF::GetTypeList ()
231{
232 if (m_debug_map_symfile)
233 return m_debug_map_symfile->GetTypeList();
234 return m_obj_file->GetModule()->GetTypeList();
235
236}
237
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238//----------------------------------------------------------------------
239// Gets the first parent that is a lexical block, function or inlined
240// subroutine, or compile unit.
241//----------------------------------------------------------------------
242static const DWARFDebugInfoEntry *
243GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
244{
245 const DWARFDebugInfoEntry *die;
246 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
247 {
248 dw_tag_t tag = die->Tag();
249
250 switch (tag)
251 {
252 case DW_TAG_compile_unit:
253 case DW_TAG_subprogram:
254 case DW_TAG_inlined_subroutine:
255 case DW_TAG_lexical_block:
256 return die;
257 }
258 }
259 return NULL;
260}
261
262
Greg Clayton450e3f32010-10-12 02:24:53 +0000263SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
264 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000265 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton450e3f32010-10-12 02:24:53 +0000266 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000267 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000269 m_data_debug_abbrev (),
270 m_data_debug_aranges (),
271 m_data_debug_frame (),
272 m_data_debug_info (),
273 m_data_debug_line (),
274 m_data_debug_loc (),
275 m_data_debug_ranges (),
276 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000277 m_data_apple_names (),
278 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000279 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 m_info(),
282 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000283 m_apple_names_ap (),
284 m_apple_types_ap (),
285 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000286 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000287 m_function_basename_index(),
288 m_function_fullname_index(),
289 m_function_method_index(),
290 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000291 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000292 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000293 m_type_index(),
294 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000295 m_indexed (false),
296 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000297 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000298 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000299 m_ranges(),
300 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301{
302}
303
304SymbolFileDWARF::~SymbolFileDWARF()
305{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000306 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000307 {
308 ModuleSP module_sp (m_obj_file->GetModule());
309 if (module_sp)
310 module_sp->GetClangASTContext().RemoveExternalSource ();
311 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000312}
313
314static const ConstString &
315GetDWARFMachOSegmentName ()
316{
317 static ConstString g_dwarf_section_name ("__DWARF");
318 return g_dwarf_section_name;
319}
320
Greg Claytone576ab22011-02-15 00:19:15 +0000321UniqueDWARFASTTypeMap &
322SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
323{
324 if (m_debug_map_symfile)
325 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
326 return m_unique_ast_type_map;
327}
328
Greg Clayton6beaaa62011-01-17 03:46:26 +0000329ClangASTContext &
330SymbolFileDWARF::GetClangASTContext ()
331{
332 if (m_debug_map_symfile)
333 return m_debug_map_symfile->GetClangASTContext ();
334
335 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
336 if (!m_is_external_ast_source)
337 {
338 m_is_external_ast_source = true;
339 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
340 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
341 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000342 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000343 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000344 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000345 ast.SetExternalSource (ast_source_ap);
346 }
347 return ast;
348}
349
350void
351SymbolFileDWARF::InitializeObject()
352{
353 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000354 ModuleSP module_sp (m_obj_file->GetModule());
355 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000356 {
357 const SectionList *section_list = m_obj_file->GetSectionList();
358
359 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
360
361 // Memory map the DWARF mach-o segment so we have everything mmap'ed
362 // to keep our heap memory usage down.
363 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000364 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000365 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000366 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000367 if (m_data_apple_names.GetByteSize() > 0)
368 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000369 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
370 if (m_apple_names_ap->IsValid())
371 m_using_apple_tables = true;
372 else
Greg Clayton7f995132011-10-04 22:41:51 +0000373 m_apple_names_ap.reset();
374 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000375 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000376 if (m_data_apple_types.GetByteSize() > 0)
377 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000378 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
379 if (m_apple_types_ap->IsValid())
380 m_using_apple_tables = true;
381 else
Greg Clayton7f995132011-10-04 22:41:51 +0000382 m_apple_types_ap.reset();
383 }
384
385 get_apple_namespaces_data();
386 if (m_data_apple_namespaces.GetByteSize() > 0)
387 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000388 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
389 if (m_apple_namespaces_ap->IsValid())
390 m_using_apple_tables = true;
391 else
Greg Clayton7f995132011-10-04 22:41:51 +0000392 m_apple_namespaces_ap.reset();
393 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000394
Greg Clayton5009f9d2011-10-27 17:55:14 +0000395 get_apple_objc_data();
396 if (m_data_apple_objc.GetByteSize() > 0)
397 {
398 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
399 if (m_apple_objc_ap->IsValid())
400 m_using_apple_tables = true;
401 else
402 m_apple_objc_ap.reset();
403 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404}
405
406bool
407SymbolFileDWARF::SupportedVersion(uint16_t version)
408{
409 return version == 2 || version == 3;
410}
411
412uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000413SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414{
415 uint32_t abilities = 0;
416 if (m_obj_file != NULL)
417 {
418 const Section* section = NULL;
419 const SectionList *section_list = m_obj_file->GetSectionList();
420 if (section_list == NULL)
421 return 0;
422
423 uint64_t debug_abbrev_file_size = 0;
424 uint64_t debug_aranges_file_size = 0;
425 uint64_t debug_frame_file_size = 0;
426 uint64_t debug_info_file_size = 0;
427 uint64_t debug_line_file_size = 0;
428 uint64_t debug_loc_file_size = 0;
429 uint64_t debug_macinfo_file_size = 0;
430 uint64_t debug_pubnames_file_size = 0;
431 uint64_t debug_pubtypes_file_size = 0;
432 uint64_t debug_ranges_file_size = 0;
433 uint64_t debug_str_file_size = 0;
434
Greg Clayton6beaaa62011-01-17 03:46:26 +0000435 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436
437 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000438 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439
Greg Clayton4ceb9982010-07-21 22:54:26 +0000440 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 if (section != NULL)
442 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000443 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444
Greg Clayton4ceb9982010-07-21 22:54:26 +0000445 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000447 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 else
449 m_flags.Set (flagsGotDebugAbbrevData);
450
Greg Clayton4ceb9982010-07-21 22:54:26 +0000451 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000453 debug_aranges_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 else
455 m_flags.Set (flagsGotDebugArangesData);
456
Greg Clayton4ceb9982010-07-21 22:54:26 +0000457 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000459 debug_frame_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 else
461 m_flags.Set (flagsGotDebugFrameData);
462
Greg Clayton4ceb9982010-07-21 22:54:26 +0000463 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000465 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 else
467 m_flags.Set (flagsGotDebugLineData);
468
Greg Clayton4ceb9982010-07-21 22:54:26 +0000469 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000471 debug_loc_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 else
473 m_flags.Set (flagsGotDebugLocData);
474
Greg Clayton4ceb9982010-07-21 22:54:26 +0000475 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000477 debug_macinfo_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 else
479 m_flags.Set (flagsGotDebugMacInfoData);
480
Greg Clayton4ceb9982010-07-21 22:54:26 +0000481 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000483 debug_pubnames_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484 else
485 m_flags.Set (flagsGotDebugPubNamesData);
486
Greg Clayton4ceb9982010-07-21 22:54:26 +0000487 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000489 debug_pubtypes_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 else
491 m_flags.Set (flagsGotDebugPubTypesData);
492
Greg Clayton4ceb9982010-07-21 22:54:26 +0000493 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000495 debug_ranges_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496 else
497 m_flags.Set (flagsGotDebugRangesData);
498
Greg Clayton4ceb9982010-07-21 22:54:26 +0000499 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000501 debug_str_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 else
503 m_flags.Set (flagsGotDebugStrData);
504 }
505
506 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
507 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
508
509 if (debug_line_file_size > 0)
510 abilities |= LineTables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511 }
512 return abilities;
513}
514
515const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000516SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517{
518 if (m_flags.IsClear (got_flag))
519 {
520 m_flags.Set (got_flag);
521 const SectionList *section_list = m_obj_file->GetSectionList();
522 if (section_list)
523 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000524 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
525 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 {
527 // See if we memory mapped the DWARF segment?
528 if (m_dwarf_data.GetByteSize())
529 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000530 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 }
532 else
533 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000534 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 data.Clear();
536 }
537 }
538 }
539 }
540 return data;
541}
542
543const DataExtractor&
544SymbolFileDWARF::get_debug_abbrev_data()
545{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000546 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547}
548
549const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000550SymbolFileDWARF::get_debug_aranges_data()
551{
552 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
553}
554
555const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556SymbolFileDWARF::get_debug_frame_data()
557{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000558 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559}
560
561const DataExtractor&
562SymbolFileDWARF::get_debug_info_data()
563{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000564 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565}
566
567const DataExtractor&
568SymbolFileDWARF::get_debug_line_data()
569{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000570 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571}
572
573const DataExtractor&
574SymbolFileDWARF::get_debug_loc_data()
575{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000576 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577}
578
579const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580SymbolFileDWARF::get_debug_ranges_data()
581{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000582 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583}
584
585const DataExtractor&
586SymbolFileDWARF::get_debug_str_data()
587{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000588 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589}
590
Greg Claytonf9eec202011-09-01 23:16:13 +0000591const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000592SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000593{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000594 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000595}
596
597const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000598SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000599{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000600 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000601}
602
Greg Clayton7f995132011-10-04 22:41:51 +0000603const DataExtractor&
604SymbolFileDWARF::get_apple_namespaces_data()
605{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000606 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
607}
608
609const DataExtractor&
610SymbolFileDWARF::get_apple_objc_data()
611{
612 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000613}
614
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615
616DWARFDebugAbbrev*
617SymbolFileDWARF::DebugAbbrev()
618{
619 if (m_abbr.get() == NULL)
620 {
621 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
622 if (debug_abbrev_data.GetByteSize() > 0)
623 {
624 m_abbr.reset(new DWARFDebugAbbrev());
625 if (m_abbr.get())
626 m_abbr->Parse(debug_abbrev_data);
627 }
628 }
629 return m_abbr.get();
630}
631
632const DWARFDebugAbbrev*
633SymbolFileDWARF::DebugAbbrev() const
634{
635 return m_abbr.get();
636}
637
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638
639DWARFDebugInfo*
640SymbolFileDWARF::DebugInfo()
641{
642 if (m_info.get() == NULL)
643 {
644 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
645 if (get_debug_info_data().GetByteSize() > 0)
646 {
647 m_info.reset(new DWARFDebugInfo());
648 if (m_info.get())
649 {
650 m_info->SetDwarfData(this);
651 }
652 }
653 }
654 return m_info.get();
655}
656
657const DWARFDebugInfo*
658SymbolFileDWARF::DebugInfo() const
659{
660 return m_info.get();
661}
662
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663DWARFCompileUnit*
664SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
665{
666 DWARFDebugInfo* info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +0000667 if (info && UserIDMatches(cu_uid))
668 return info->GetCompileUnit((dw_offset_t)cu_uid).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 return NULL;
670}
671
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672
673DWARFDebugRanges*
674SymbolFileDWARF::DebugRanges()
675{
676 if (m_ranges.get() == NULL)
677 {
678 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
679 if (get_debug_ranges_data().GetByteSize() > 0)
680 {
681 m_ranges.reset(new DWARFDebugRanges());
682 if (m_ranges.get())
683 m_ranges->Extract(this);
684 }
685 }
686 return m_ranges.get();
687}
688
689const DWARFDebugRanges*
690SymbolFileDWARF::DebugRanges() const
691{
692 return m_ranges.get();
693}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694
Greg Clayton53eb1c22012-04-02 22:59:12 +0000695lldb::CompUnitSP
696SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000698 CompUnitSP cu_sp;
699 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000701 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
702 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000704 // We already parsed this compile unit, had out a shared pointer to it
705 cu_sp = comp_unit->shared_from_this();
706 }
707 else
708 {
709 ModuleSP module_sp (m_obj_file->GetModule());
710 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000712 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
713 if (cu_die)
714 {
715 const char * cu_die_name = cu_die->GetName(this, dwarf_cu);
716 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
717 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
718 if (cu_die_name)
719 {
720 std::string ramapped_file;
721 FileSpec cu_file_spec;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000722
Greg Clayton53eb1c22012-04-02 22:59:12 +0000723 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
724 {
725 // If we have a full path to the compile unit, we don't need to resolve
726 // the file. This can be expensive e.g. when the source files are NFS mounted.
727 if (module_sp->RemapSourceFile(cu_die_name, ramapped_file))
728 cu_file_spec.SetFile (ramapped_file.c_str(), false);
729 else
730 cu_file_spec.SetFile (cu_die_name, false);
731 }
732 else
733 {
734 std::string fullpath(cu_comp_dir);
735 if (*fullpath.rbegin() != '/')
736 fullpath += '/';
737 fullpath += cu_die_name;
738 if (module_sp->RemapSourceFile (fullpath.c_str(), ramapped_file))
739 cu_file_spec.SetFile (ramapped_file.c_str(), false);
740 else
741 cu_file_spec.SetFile (fullpath.c_str(), false);
742 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743
Greg Clayton53eb1c22012-04-02 22:59:12 +0000744 cu_sp.reset(new CompileUnit (module_sp,
745 dwarf_cu,
746 cu_file_spec,
747 MakeUserID(dwarf_cu->GetOffset()),
748 cu_language));
749 if (cu_sp)
750 {
751 dwarf_cu->SetUserData(cu_sp.get());
752
753 if (m_debug_map_symfile)
754 {
755 // Let the symbol file register the compile unit with
756 // the symbol vendor using its compile unit index
757 // when we are doing DWARF in .o files + debug map
758 m_debug_map_symfile->SetCompileUnit(this, cu_sp);
759 }
760 else
761 {
762 // Figure out the compile unit index if we weren't given one
763 if (cu_idx == UINT32_MAX)
764 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
765
766 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
767 }
768 }
769 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 }
771 }
772 }
773 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000774 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775}
776
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777uint32_t
778SymbolFileDWARF::GetNumCompileUnits()
779{
780 DWARFDebugInfo* info = DebugInfo();
781 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783 return 0;
784}
785
786CompUnitSP
787SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
788{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000789 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 DWARFDebugInfo* info = DebugInfo();
791 if (info)
792 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000793 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
794 if (dwarf_cu)
795 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000797 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798}
799
800static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000801AddRangesToBlock (Block& block,
802 DWARFDebugRanges::RangeList& ranges,
803 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000805 const size_t num_ranges = ranges.GetSize();
806 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000808 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
809 const addr_t range_base = range.GetRangeBase();
810 assert (range_base >= block_base_addr);
811 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000813 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814}
815
816
817Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000818SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819{
820 DWARFDebugRanges::RangeList func_ranges;
821 const char *name = NULL;
822 const char *mangled = NULL;
823 int decl_file = 0;
824 int decl_line = 0;
825 int decl_column = 0;
826 int call_file = 0;
827 int call_line = 0;
828 int call_column = 0;
829 DWARFExpression frame_base;
830
Greg Claytonc93237c2010-10-01 20:48:32 +0000831 assert (die->Tag() == DW_TAG_subprogram);
832
833 if (die->Tag() != DW_TAG_subprogram)
834 return NULL;
835
Greg Clayton3c2e3ae2012-02-06 06:42:51 +0000836 if (die->GetDIENamesAndRanges (this,
837 dwarf_cu,
838 name,
839 mangled,
840 func_ranges,
841 decl_file,
842 decl_line,
843 decl_column,
844 call_file,
845 call_line,
846 call_column,
847 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 {
849 // Union of all ranges in the function DIE (if the function is discontiguous)
850 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000851 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
852 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000853 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
854 {
855 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
856 if (func_range.GetBaseAddress().IsValid())
857 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
858 }
859
860 if (func_range.GetBaseAddress().IsValid())
861 {
862 Mangled func_name;
863 if (mangled)
864 func_name.SetValue(mangled, true);
865 else if (name)
866 func_name.SetValue(name, false);
867
868 FunctionSP func_sp;
869 std::auto_ptr<Declaration> decl_ap;
870 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000871 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
872 decl_line,
873 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000875 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000876 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877
878 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
879
880 func_range.GetBaseAddress().ResolveLinkedAddress();
881
Greg Clayton81c22f62011-10-19 18:09:39 +0000882 const user_id_t func_user_id = MakeUserID(die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883 func_sp.reset(new Function (sc.comp_unit,
Greg Clayton81c22f62011-10-19 18:09:39 +0000884 func_user_id, // UserID is the DIE offset
885 func_user_id,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886 func_name,
887 func_type,
888 func_range)); // first address range
889
890 if (func_sp.get() != NULL)
891 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000892 if (frame_base.IsValid())
893 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000894 sc.comp_unit->AddFunction(func_sp);
895 return func_sp.get();
896 }
897 }
898 }
899 return NULL;
900}
901
902size_t
903SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
904{
905 assert (sc.comp_unit);
906 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000907 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908 if (dwarf_cu)
909 {
910 DWARFDIECollection function_dies;
911 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
912 size_t func_idx;
913 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
914 {
915 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000916 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000917 {
918 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
919 ++functions_added;
920 }
921 }
922 //FixupTypes();
923 }
924 return functions_added;
925}
926
927bool
928SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
929{
930 assert (sc.comp_unit);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000931 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
932 assert (dwarf_cu);
933 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934
935 if (cu_die)
936 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000937 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
938 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 +0000939
940 // All file indexes in DWARF are one based and a file of index zero is
941 // supposed to be the compile unit itself.
942 support_files.Append (*sc.comp_unit);
943
Greg Claytond804d282012-03-15 21:01:31 +0000944 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945 }
946 return false;
947}
948
949struct ParseDWARFLineTableCallbackInfo
950{
951 LineTable* line_table;
952 const SectionList *section_list;
953 lldb::addr_t prev_sect_file_base_addr;
954 lldb::addr_t curr_sect_file_base_addr;
955 bool is_oso_for_debug_map;
956 bool prev_in_final_executable;
957 DWARFDebugLine::Row prev_row;
958 SectionSP prev_section_sp;
959 SectionSP curr_section_sp;
960};
961
962//----------------------------------------------------------------------
963// ParseStatementTableCallback
964//----------------------------------------------------------------------
965static void
966ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
967{
968 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
969 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
970 {
971 // Just started parsing the line table
972 }
973 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
974 {
975 // Done parsing line table, nothing to do for the cleanup
976 }
977 else
978 {
979 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
980 // We have a new row, lets append it
981
982 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
983 {
984 info->prev_section_sp = info->curr_section_sp;
985 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
986 // If this is an end sequence entry, then we subtract one from the
987 // address to make sure we get an address that is not the end of
988 // a section.
989 if (state.end_sequence && state.address != 0)
990 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
991 else
992 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
993
994 if (info->curr_section_sp.get())
995 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
996 else
997 info->curr_sect_file_base_addr = 0;
998 }
999 if (info->curr_section_sp.get())
1000 {
1001 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
1002 // Check for the fancy section magic to determine if we
1003
1004 if (info->is_oso_for_debug_map)
1005 {
1006 // When this is a debug map object file that contains DWARF
1007 // (referenced from an N_OSO debug map nlist entry) we will have
1008 // a file address in the file range for our section from the
1009 // original .o file, and a load address in the executable that
1010 // contains the debug map.
1011 //
1012 // If the sections for the file range and load range are
1013 // different, we have a remapped section for the function and
1014 // this address is resolved. If they are the same, then the
1015 // function for this address didn't make it into the final
1016 // executable.
1017 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
1018
1019 // If we are doing DWARF with debug map, then we need to carefully
1020 // add each line table entry as there may be gaps as functions
1021 // get moved around or removed.
1022 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
1023 {
1024 if (info->prev_in_final_executable)
1025 {
1026 bool terminate_previous_entry = false;
1027 if (!curr_in_final_executable)
1028 {
1029 // Check for the case where the previous line entry
1030 // in a function made it into the final executable,
1031 // yet the current line entry falls in a function
1032 // that didn't. The line table used to be contiguous
1033 // through this address range but now it isn't. We
1034 // need to terminate the previous line entry so
1035 // that we can reconstruct the line range correctly
1036 // for it and to keep the line table correct.
1037 terminate_previous_entry = true;
1038 }
1039 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
1040 {
1041 // Check for cases where the line entries used to be
1042 // contiguous address ranges, but now they aren't.
1043 // This can happen when order files specify the
1044 // ordering of the functions.
1045 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
1046 Section *curr_sect = info->curr_section_sp.get();
1047 Section *prev_sect = info->prev_section_sp.get();
1048 assert (curr_sect->GetLinkedSection());
1049 assert (prev_sect->GetLinkedSection());
1050 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
1051 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
1052 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
1053 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
1054 if (object_file_addr_delta != linked_file_addr_delta)
1055 terminate_previous_entry = true;
1056 }
1057
1058 if (terminate_previous_entry)
1059 {
1060 line_table->InsertLineEntry (info->prev_section_sp,
1061 state.address - info->prev_sect_file_base_addr,
1062 info->prev_row.line,
1063 info->prev_row.column,
1064 info->prev_row.file,
1065 false, // is_stmt
1066 false, // basic_block
1067 false, // state.prologue_end
1068 false, // state.epilogue_begin
1069 true); // end_sequence);
1070 }
1071 }
1072 }
1073
1074 if (curr_in_final_executable)
1075 {
1076 line_table->InsertLineEntry (info->curr_section_sp,
1077 curr_line_section_offset,
1078 state.line,
1079 state.column,
1080 state.file,
1081 state.is_stmt,
1082 state.basic_block,
1083 state.prologue_end,
1084 state.epilogue_begin,
1085 state.end_sequence);
1086 info->prev_section_sp = info->curr_section_sp;
1087 }
1088 else
1089 {
1090 // If the current address didn't make it into the final
1091 // executable, the current section will be the __text
1092 // segment in the .o file, so we need to clear this so
1093 // we can catch the next function that did make it into
1094 // the final executable.
1095 info->prev_section_sp.reset();
1096 info->curr_section_sp.reset();
1097 }
1098
1099 info->prev_in_final_executable = curr_in_final_executable;
1100 }
1101 else
1102 {
1103 // We are not in an object file that contains DWARF for an
1104 // N_OSO, this is just a normal DWARF file. The DWARF spec
1105 // guarantees that the addresses will be in increasing order
1106 // so, since we store line tables in file address order, we
1107 // can always just append the line entry without needing to
1108 // search for the correct insertion point (we don't need to
1109 // use LineEntry::InsertLineEntry()).
1110 line_table->AppendLineEntry (info->curr_section_sp,
1111 curr_line_section_offset,
1112 state.line,
1113 state.column,
1114 state.file,
1115 state.is_stmt,
1116 state.basic_block,
1117 state.prologue_end,
1118 state.epilogue_begin,
1119 state.end_sequence);
1120 }
1121 }
1122
1123 info->prev_row = state;
1124 }
1125}
1126
1127bool
1128SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1129{
1130 assert (sc.comp_unit);
1131 if (sc.comp_unit->GetLineTable() != NULL)
1132 return true;
1133
1134 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
1135 if (dwarf_cu)
1136 {
1137 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001138 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001140 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1141 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001143 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1144 if (line_table_ap.get())
1145 {
1146 ParseDWARFLineTableCallbackInfo info = {
1147 line_table_ap.get(),
1148 m_obj_file->GetSectionList(),
1149 0,
1150 0,
1151 m_debug_map_symfile != NULL,
1152 false,
1153 DWARFDebugLine::Row(),
1154 SectionSP(),
1155 SectionSP()
1156 };
1157 uint32_t offset = cu_line_offset;
1158 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1159 sc.comp_unit->SetLineTable(line_table_ap.release());
1160 return true;
1161 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162 }
1163 }
1164 }
1165 return false;
1166}
1167
1168size_t
1169SymbolFileDWARF::ParseFunctionBlocks
1170(
1171 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001172 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001173 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 const DWARFDebugInfoEntry *die,
1175 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001176 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177)
1178{
1179 size_t blocks_added = 0;
1180 while (die != NULL)
1181 {
1182 dw_tag_t tag = die->Tag();
1183
1184 switch (tag)
1185 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001187 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 case DW_TAG_lexical_block:
1189 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001190 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001191 if (tag == DW_TAG_subprogram)
1192 {
1193 // Skip any DW_TAG_subprogram DIEs that are inside
1194 // of a normal or inlined functions. These will be
1195 // parsed on their own as separate entities.
1196
1197 if (depth > 0)
1198 break;
1199
1200 block = parent_block;
1201 }
1202 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001203 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001204 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001205 parent_block->AddChild(block_sp);
1206 block = block_sp.get();
1207 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001208 DWARFDebugRanges::RangeList ranges;
1209 const char *name = NULL;
1210 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212 int decl_file = 0;
1213 int decl_line = 0;
1214 int decl_column = 0;
1215 int call_file = 0;
1216 int call_line = 0;
1217 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001218 if (die->GetDIENamesAndRanges (this,
1219 dwarf_cu,
1220 name,
1221 mangled_name,
1222 ranges,
1223 decl_file, decl_line, decl_column,
1224 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225 {
1226 if (tag == DW_TAG_subprogram)
1227 {
1228 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001229 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001231 else if (tag == DW_TAG_inlined_subroutine)
1232 {
1233 // We get called here for inlined subroutines in two ways.
1234 // The first time is when we are making the Function object
1235 // for this inlined concrete instance. Since we're creating a top level block at
1236 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1237 // adjust the containing address.
1238 // The second time is when we are parsing the blocks inside the function that contains
1239 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1240 // function the offset will be for that function.
1241 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1242 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001243 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001244 }
1245 }
1246
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001247 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001248
1249 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1250 {
1251 std::auto_ptr<Declaration> decl_ap;
1252 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001253 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1254 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255
1256 std::auto_ptr<Declaration> call_ap;
1257 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001258 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1259 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001260
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001261 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262 }
1263
1264 ++blocks_added;
1265
Greg Claytondd7feaf2011-08-12 17:54:33 +00001266 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001268 blocks_added += ParseFunctionBlocks (sc,
1269 block,
1270 dwarf_cu,
1271 die->GetFirstChild(),
1272 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001273 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274 }
1275 }
1276 }
1277 break;
1278 default:
1279 break;
1280 }
1281
Greg Claytondd7feaf2011-08-12 17:54:33 +00001282 // Only parse siblings of the block if we are not at depth zero. A depth
1283 // of zero indicates we are currently parsing the top level
1284 // DW_TAG_subprogram DIE
1285
1286 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001288 else
1289 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290 }
1291 return blocks_added;
1292}
1293
Greg Claytonf0705c82011-10-22 03:33:13 +00001294bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001295SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1296 const DWARFDebugInfoEntry *die,
1297 ClangASTContext::TemplateParameterInfos &template_param_infos)
1298{
1299 const dw_tag_t tag = die->Tag();
1300
1301 switch (tag)
1302 {
1303 case DW_TAG_template_type_parameter:
1304 case DW_TAG_template_value_parameter:
1305 {
1306 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1307
1308 DWARFDebugInfoEntry::Attributes attributes;
1309 const size_t num_attributes = die->GetAttributes (this,
1310 dwarf_cu,
1311 fixed_form_sizes,
1312 attributes);
1313 const char *name = NULL;
1314 Type *lldb_type = NULL;
1315 clang_type_t clang_type = NULL;
1316 uint64_t uval64 = 0;
1317 bool uval64_valid = false;
1318 if (num_attributes > 0)
1319 {
1320 DWARFFormValue form_value;
1321 for (size_t i=0; i<num_attributes; ++i)
1322 {
1323 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1324
1325 switch (attr)
1326 {
1327 case DW_AT_name:
1328 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1329 name = form_value.AsCString(&get_debug_str_data());
1330 break;
1331
1332 case DW_AT_type:
1333 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1334 {
1335 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1336 lldb_type = ResolveTypeUID(type_die_offset);
1337 if (lldb_type)
1338 clang_type = lldb_type->GetClangForwardType();
1339 }
1340 break;
1341
1342 case DW_AT_const_value:
1343 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1344 {
1345 uval64_valid = true;
1346 uval64 = form_value.Unsigned();
1347 }
1348 break;
1349 default:
1350 break;
1351 }
1352 }
1353
1354 if (name && lldb_type && clang_type)
1355 {
1356 bool is_signed = false;
1357 template_param_infos.names.push_back(name);
1358 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1359 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1360 {
1361 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1362 template_param_infos.args.push_back (clang::TemplateArgument (llvm::APSInt(apint), clang_qual_type));
1363 }
1364 else
1365 {
1366 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1367 }
1368 }
1369 else
1370 {
1371 return false;
1372 }
1373
1374 }
1375 }
1376 return true;
1377
1378 default:
1379 break;
1380 }
1381 return false;
1382}
1383
1384bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001385SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1386 const DWARFDebugInfoEntry *parent_die,
1387 ClangASTContext::TemplateParameterInfos &template_param_infos)
1388{
1389
1390 if (parent_die == NULL)
1391 return NULL;
1392
Greg Claytonf0705c82011-10-22 03:33:13 +00001393 Args template_parameter_names;
1394 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1395 die != NULL;
1396 die = die->GetSibling())
1397 {
1398 const dw_tag_t tag = die->Tag();
1399
1400 switch (tag)
1401 {
1402 case DW_TAG_template_type_parameter:
1403 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001404 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001405 break;
1406
1407 default:
1408 break;
1409 }
1410 }
1411 if (template_param_infos.args.empty())
1412 return false;
1413 return template_param_infos.args.size() == template_param_infos.names.size();
1414}
1415
1416clang::ClassTemplateDecl *
1417SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001418 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001419 const char *parent_name,
1420 int tag_decl_kind,
1421 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1422{
1423 if (template_param_infos.IsValid())
1424 {
1425 std::string template_basename(parent_name);
1426 template_basename.erase (template_basename.find('<'));
1427 ClangASTContext &ast = GetClangASTContext();
1428
1429 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001430 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001431 template_basename.c_str(),
1432 tag_decl_kind,
1433 template_param_infos);
1434 }
1435 return NULL;
1436}
1437
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438size_t
1439SymbolFileDWARF::ParseChildMembers
1440(
1441 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001442 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001443 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001444 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001445 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001446 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1447 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001448 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001449 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001450 bool &is_a_class,
1451 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452)
1453{
1454 if (parent_die == NULL)
1455 return 0;
1456
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457 size_t count = 0;
1458 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001459 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001460 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001461
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001462 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1463 {
1464 dw_tag_t tag = die->Tag();
1465
1466 switch (tag)
1467 {
1468 case DW_TAG_member:
Bill Wendling9e5bf872012-04-03 08:45:33 +00001469 case DW_TAG_APPLE_Property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001470 {
1471 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001472 const size_t num_attributes = die->GetAttributes (this,
1473 dwarf_cu,
1474 fixed_form_sizes,
1475 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 if (num_attributes > 0)
1477 {
1478 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001479 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001481 const char *prop_name = NULL;
1482 const char *prop_getter_name = NULL;
1483 const char *prop_setter_name = NULL;
1484 uint32_t prop_attributes = 0;
1485
1486
Greg Clayton24739922010-10-13 03:15:28 +00001487 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001488 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001489 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001490 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491 size_t byte_size = 0;
1492 size_t bit_offset = 0;
1493 size_t bit_size = 0;
1494 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001495 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001496 {
1497 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1498 DWARFFormValue form_value;
1499 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1500 {
1501 switch (attr)
1502 {
1503 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1504 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1505 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1506 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1507 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1508 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1509 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1510 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1511 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001512 if (form_value.BlockData())
1513 {
1514 Value initialValue(0);
1515 Value memberOffset(0);
1516 const DataExtractor& debug_info_data = get_debug_info_data();
1517 uint32_t block_length = form_value.Unsigned();
1518 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1519 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1520 NULL, // clang::ASTContext *
1521 NULL, // ClangExpressionVariableList *
1522 NULL, // ClangExpressionDeclMap *
1523 NULL, // RegisterContext *
1524 debug_info_data,
1525 block_offset,
1526 block_length,
1527 eRegisterKindDWARF,
1528 &initialValue,
1529 memberOffset,
1530 NULL))
1531 {
1532 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1533 }
1534 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535 break;
1536
Greg Clayton8cf05932010-07-22 18:30:50 +00001537 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1b02c172012-03-14 21:00:47 +00001538 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001539 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1540 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1541 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1542 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1543
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001544 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001545 case DW_AT_declaration:
1546 case DW_AT_description:
1547 case DW_AT_mutable:
1548 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001549 case DW_AT_sibling:
1550 break;
1551 }
1552 }
1553 }
Sean Callanana6582262012-04-05 00:12:52 +00001554
1555 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00001556 {
Sean Callanana6582262012-04-05 00:12:52 +00001557 ConstString fixed_getter;
1558 ConstString fixed_setter;
1559
1560 // Check if the property getter/setter were provided as full
1561 // names. We want basenames, so we extract them.
1562
1563 if (prop_getter_name && prop_getter_name[0] == '-')
1564 {
1565 ObjCLanguageRuntime::ParseMethodName (prop_getter_name,
1566 NULL,
1567 &fixed_getter,
1568 NULL,
1569 NULL);
1570 prop_getter_name = fixed_getter.GetCString();
1571 }
1572
1573 if (prop_setter_name && prop_setter_name[0] == '-')
1574 {
1575 ObjCLanguageRuntime::ParseMethodName (prop_setter_name,
1576 NULL,
1577 &fixed_setter,
1578 NULL,
1579 NULL);
1580 prop_setter_name = fixed_setter.GetCString();
1581 }
1582
1583 // If the names haven't been provided, they need to be
1584 // filled in.
1585
1586 if (!prop_getter_name)
1587 {
1588 prop_getter_name = prop_name;
1589 }
1590 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
1591 {
1592 StreamString ss;
1593
1594 ss.Printf("set%c%s:",
1595 toupper(prop_name[0]),
1596 &prop_name[1]);
1597
1598 fixed_setter.SetCString(ss.GetData());
1599 prop_setter_name = fixed_setter.GetCString();
1600 }
Sean Callanan751aac62012-03-29 19:07:06 +00001601 }
1602
1603 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001604 // represents fields that are references with bad byte size
1605 // and bit size/offset information such as:
1606 //
1607 // DW_AT_byte_size( 0x00 )
1608 // DW_AT_bit_size( 0x40 )
1609 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1610 //
1611 // So check the bit offset to make sure it is sane, and if
1612 // the values are not sane, remove them. If we don't do this
1613 // then we will end up with a crash if we try to use this
1614 // type in an expression when clang becomes unhappy with its
1615 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001616
Greg Clayton44953932011-10-25 01:25:35 +00001617 if (bit_offset > 128)
1618 {
1619 bit_size = 0;
1620 bit_offset = 0;
1621 }
1622
1623 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001624 if (class_language == eLanguageTypeObjC ||
1625 class_language == eLanguageTypeObjC_plus_plus)
1626 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001627
1628 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1629 {
1630 // Not all compilers will mark the vtable pointer
1631 // member as artificial (llvm-gcc). We can't have
1632 // the virtual members in our classes otherwise it
1633 // throws off all child offsets since we end up
1634 // having and extra pointer sized member in our
1635 // class layouts.
1636 is_artificial = true;
1637 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001638
Greg Clayton24739922010-10-13 03:15:28 +00001639 if (is_artificial == false)
1640 {
1641 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001642 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001643 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001644 {
Sean Callanan751aac62012-03-29 19:07:06 +00001645 if (member_type)
1646 {
1647 if (accessibility == eAccessNone)
1648 accessibility = default_accessibility;
1649 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001650
Sean Callanan751aac62012-03-29 19:07:06 +00001651 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
1652 name,
1653 member_type->GetClangLayoutType(),
1654 accessibility,
1655 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00001656
1657 GetClangASTContext().SetMetadata((uintptr_t)field_decl, MakeUserID(die->GetOffset()));
Sean Callanan5b26f272012-02-04 08:49:35 +00001658 }
1659 else
1660 {
Sean Callanan751aac62012-03-29 19:07:06 +00001661 if (name)
1662 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1663 MakeUserID(die->GetOffset()),
1664 name,
1665 encoding_uid);
1666 else
1667 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1668 MakeUserID(die->GetOffset()),
1669 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00001670 }
Sean Callanan751aac62012-03-29 19:07:06 +00001671
1672 if (member_byte_offset != UINT32_MAX || bit_size != 0)
1673 {
1674 /////////////////////////////////////////////////////////////
1675 // How to locate a field given the DWARF debug information
1676 //
1677 // AT_byte_size indicates the size of the word in which the
1678 // bit offset must be interpreted.
1679 //
1680 // AT_data_member_location indicates the byte offset of the
1681 // word from the base address of the structure.
1682 //
1683 // AT_bit_offset indicates how many bits into the word
1684 // (according to the host endianness) the low-order bit of
1685 // the field starts. AT_bit_offset can be negative.
1686 //
1687 // AT_bit_size indicates the size of the field in bits.
1688 /////////////////////////////////////////////////////////////
Sean Callanan5b26f272012-02-04 08:49:35 +00001689
Sean Callanan751aac62012-03-29 19:07:06 +00001690 ByteOrder object_endian = GetObjectFile()->GetModule()->GetArchitecture().GetDefaultEndian();
1691
1692 uint64_t total_bit_offset = 0;
1693
1694 total_bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1695
1696 if (object_endian == eByteOrderLittle)
1697 {
1698 total_bit_offset += byte_size * 8;
1699 total_bit_offset -= (bit_offset + bit_size);
1700 }
1701 else
1702 {
1703 total_bit_offset += bit_offset;
1704 }
1705
1706 layout_info.field_offsets.insert(std::make_pair(field_decl, total_bit_offset));
1707 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001708 }
Sean Callanan751aac62012-03-29 19:07:06 +00001709
Jim Inghame3ae82a2011-11-12 01:36:43 +00001710 if (prop_name != NULL)
1711 {
Sean Callanan751aac62012-03-29 19:07:06 +00001712 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001713
Sean Callanan751aac62012-03-29 19:07:06 +00001714 if (field_decl)
1715 {
1716 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1717 assert (ivar_decl != NULL);
1718 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001719
Jim Inghame3ae82a2011-11-12 01:36:43 +00001720 GetClangASTContext().AddObjCClassProperty (class_clang_type,
1721 prop_name,
Sean Callanan751aac62012-03-29 19:07:06 +00001722 member_type->GetClangLayoutType(),
Jim Inghame3ae82a2011-11-12 01:36:43 +00001723 ivar_decl,
1724 prop_setter_name,
1725 prop_getter_name,
Sean Callananad880762012-04-18 01:06:17 +00001726 prop_attributes,
1727 MakeUserID(die->GetOffset()));
Sean Callanan60217122012-04-13 00:10:03 +00001728
Sean Callananad880762012-04-18 01:06:17 +00001729 if (ivar_decl)
1730 GetClangASTContext().SetMetadata((uintptr_t)ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00001731 }
Greg Clayton24739922010-10-13 03:15:28 +00001732 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001734 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735 }
1736 break;
1737
1738 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001739 // Let the type parsing code handle this one for us.
1740 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001741 break;
1742
1743 case DW_TAG_inheritance:
1744 {
1745 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001746 if (default_accessibility == eAccessNone)
1747 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748 // TODO: implement DW_TAG_inheritance type parsing
1749 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001750 const size_t num_attributes = die->GetAttributes (this,
1751 dwarf_cu,
1752 fixed_form_sizes,
1753 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001754 if (num_attributes > 0)
1755 {
1756 Declaration decl;
1757 DWARFExpression location;
1758 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001759 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001760 bool is_virtual = false;
1761 bool is_base_of_class = true;
1762 off_t member_offset = 0;
1763 uint32_t i;
1764 for (i=0; i<num_attributes; ++i)
1765 {
1766 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1767 DWARFFormValue form_value;
1768 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1769 {
1770 switch (attr)
1771 {
1772 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1773 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1774 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1775 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1776 case DW_AT_data_member_location:
1777 if (form_value.BlockData())
1778 {
1779 Value initialValue(0);
1780 Value memberOffset(0);
1781 const DataExtractor& debug_info_data = get_debug_info_data();
1782 uint32_t block_length = form_value.Unsigned();
1783 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001784 if (DWARFExpression::Evaluate (NULL,
1785 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001786 NULL,
1787 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001788 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001789 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001790 block_offset,
1791 block_length,
1792 eRegisterKindDWARF,
1793 &initialValue,
1794 memberOffset,
1795 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796 {
1797 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1798 }
1799 }
1800 break;
1801
1802 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001803 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804 break;
1805
1806 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1807 default:
1808 case DW_AT_sibling:
1809 break;
1810 }
1811 }
1812 }
1813
Greg Clayton526e5af2010-11-13 03:52:47 +00001814 Type *base_class_type = ResolveTypeUID(encoding_uid);
1815 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001816
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001817 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1818 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001819 if (class_language == eLanguageTypeObjC)
1820 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001821 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001822 }
1823 else
1824 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001825 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001826 accessibility,
1827 is_virtual,
1828 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001829 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001830 }
1831 }
1832 break;
1833
1834 default:
1835 break;
1836 }
1837 }
1838 return count;
1839}
1840
1841
1842clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001843SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001844{
1845 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001846 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847 {
1848 DWARFCompileUnitSP cu_sp;
1849 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1850 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001851 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001852 }
1853 return NULL;
1854}
1855
1856clang::DeclContext*
1857SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1858{
Greg Clayton81c22f62011-10-19 18:09:39 +00001859 if (UserIDMatches(type_uid))
1860 return GetClangDeclContextForDIEOffset (sc, type_uid);
1861 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862}
1863
1864Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001865SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001866{
Greg Clayton81c22f62011-10-19 18:09:39 +00001867 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001868 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001869 DWARFDebugInfo* debug_info = DebugInfo();
1870 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001871 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001872 DWARFCompileUnitSP cu_sp;
1873 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001874 const bool assert_not_being_parsed = true;
1875 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001876 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877 }
1878 return NULL;
1879}
1880
Greg Claytoncab36a32011-12-08 05:16:30 +00001881Type*
1882SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001883{
Greg Claytoncab36a32011-12-08 05:16:30 +00001884 if (die != NULL)
1885 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001886 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1887 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001888 GetObjectFile()->GetModule()->LogMessage (log.get(),
1889 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1890 die->GetOffset(),
1891 DW_TAG_value_to_name(die->Tag()),
1892 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001893
Greg Claytoncab36a32011-12-08 05:16:30 +00001894 // We might be coming in in the middle of a type tree (a class
1895 // withing a class, an enum within a class), so parse any needed
1896 // parent DIEs before we get to this one...
1897 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1898 switch (decl_ctx_die->Tag())
1899 {
1900 case DW_TAG_structure_type:
1901 case DW_TAG_union_type:
1902 case DW_TAG_class_type:
1903 {
1904 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001905 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001906 GetObjectFile()->GetModule()->LogMessage (log.get(),
1907 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1908 die->GetOffset(),
1909 DW_TAG_value_to_name(die->Tag()),
1910 die->GetName(this, cu),
1911 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00001912//
1913// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
1914// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
1915// {
1916// if (log)
1917// GetObjectFile()->GetModule()->LogMessage (log.get(),
1918// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
1919// die->GetOffset(),
1920// DW_TAG_value_to_name(die->Tag()),
1921// die->GetName(this, cu),
1922// decl_ctx_die->GetOffset());
1923// // Ask the type to complete itself if it already hasn't since if we
1924// // want a function (method or static) from a class, the class must
1925// // create itself and add it's own methods and class functions.
1926// if (parent_type)
1927// parent_type->GetClangFullType();
1928// }
Greg Claytoncab36a32011-12-08 05:16:30 +00001929 }
1930 break;
1931
1932 default:
1933 break;
1934 }
1935 return ResolveType (cu, die);
1936 }
1937 return NULL;
1938}
1939
Greg Clayton6beaaa62011-01-17 03:46:26 +00001940// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1941// SymbolFileDWARF objects to detect if this DWARF file is the one that
1942// can resolve a clang_type.
1943bool
1944SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1945{
1946 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1947 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1948 return die != NULL;
1949}
1950
1951
Greg Clayton1be10fc2010-09-29 01:12:09 +00001952lldb::clang_type_t
1953SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1954{
1955 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001956 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1957 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001958 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001959 {
1960 // We have already resolved this type...
1961 return clang_type;
1962 }
1963 // Once we start resolving this type, remove it from the forward declaration
1964 // map in case anyone child members or other types require this type to get resolved.
1965 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1966 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001967 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001968
Greg Clayton1be10fc2010-09-29 01:12:09 +00001969
Greg Clayton85ae2e12011-10-18 23:36:41 +00001970 // Disable external storage for this type so we don't get anymore
1971 // clang::ExternalASTSource queries for this type.
1972 ClangASTContext::SetHasExternalStorage (clang_type, false);
1973
Greg Clayton450e3f32010-10-12 02:24:53 +00001974 DWARFDebugInfo* debug_info = DebugInfo();
1975
Greg Clayton53eb1c22012-04-02 22:59:12 +00001976 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001977 Type *type = m_die_to_type.lookup (die);
1978
1979 const dw_tag_t tag = die->Tag();
1980
Greg Claytonbaf95da2012-03-30 23:50:54 +00001981 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00001982 if (log)
Greg Claytonbaf95da2012-03-30 23:50:54 +00001983 {
Greg Claytond61c0fc2012-04-23 22:55:20 +00001984 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log.get(),
1985 "0x%8.8llx: %s '%s' resolving forward declaration...",
1986 MakeUserID(die->GetOffset()),
1987 DW_TAG_value_to_name(tag),
1988 type->GetName().AsCString());
Greg Claytonbaf95da2012-03-30 23:50:54 +00001989
Greg Claytonbaf95da2012-03-30 23:50:54 +00001990 }
Greg Clayton1be10fc2010-09-29 01:12:09 +00001991 assert (clang_type);
1992 DWARFDebugInfoEntry::Attributes attributes;
1993
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001994 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001995
1996 switch (tag)
1997 {
1998 case DW_TAG_structure_type:
1999 case DW_TAG_union_type:
2000 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002001 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002002 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002003
Greg Clayton1be10fc2010-09-29 01:12:09 +00002004 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002005 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002006 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002007
Sean Callanan77a1fd32012-02-27 20:07:01 +00002008 LanguageType class_language = eLanguageTypeUnknown;
2009 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
2010 if (is_objc_class)
Greg Clayton219cf312012-03-30 00:51:13 +00002011 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002012 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002013 // For objective C we don't start the definition when
2014 // the class is created.
2015 ast.StartTagDeclarationDefinition (clang_type);
2016 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002017
2018 int tag_decl_kind = -1;
2019 AccessType default_accessibility = eAccessNone;
2020 if (tag == DW_TAG_structure_type)
2021 {
2022 tag_decl_kind = clang::TTK_Struct;
2023 default_accessibility = eAccessPublic;
2024 }
2025 else if (tag == DW_TAG_union_type)
2026 {
2027 tag_decl_kind = clang::TTK_Union;
2028 default_accessibility = eAccessPublic;
2029 }
2030 else if (tag == DW_TAG_class_type)
2031 {
2032 tag_decl_kind = clang::TTK_Class;
2033 default_accessibility = eAccessPrivate;
2034 }
2035
Greg Clayton53eb1c22012-04-02 22:59:12 +00002036 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002037 std::vector<clang::CXXBaseSpecifier *> base_classes;
2038 std::vector<int> member_accessibilities;
2039 bool is_a_class = false;
2040 // Parse members and base classes first
2041 DWARFDIECollection member_function_dies;
2042
2043 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002044 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002045 die,
2046 clang_type,
2047 class_language,
2048 base_classes,
2049 member_accessibilities,
2050 member_function_dies,
2051 default_accessibility,
2052 is_a_class,
2053 layout_info);
2054
2055 // Now parse any methods if there were any...
2056 size_t num_functions = member_function_dies.Size();
2057 if (num_functions > 0)
2058 {
2059 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002060 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002061 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002062 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002063 }
2064
2065 if (class_language == eLanguageTypeObjC)
2066 {
Greg Clayton84db9102012-03-26 23:03:23 +00002067 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(ast.getASTContext(), clang_type));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002068 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00002069 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002070
Sean Callanan77a1fd32012-02-27 20:07:01 +00002071 DIEArray method_die_offsets;
2072 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002073 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002074 if (m_apple_objc_ap.get())
2075 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
2076 }
2077 else
2078 {
2079 if (!m_indexed)
2080 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002081
Sean Callanan77a1fd32012-02-27 20:07:01 +00002082 ConstString class_name (class_str.c_str());
2083 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2084 }
2085
2086 if (!method_die_offsets.empty())
2087 {
2088 DWARFDebugInfo* debug_info = DebugInfo();
2089
2090 DWARFCompileUnit* method_cu = NULL;
2091 const size_t num_matches = method_die_offsets.size();
2092 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002093 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002094 const dw_offset_t die_offset = method_die_offsets[i];
2095 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2096
2097 if (method_die)
2098 ResolveType (method_cu, method_die);
2099 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002100 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002101 if (m_using_apple_tables)
2102 {
2103 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2104 die_offset, class_str.c_str());
2105 }
2106 }
2107 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002108 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002109 }
2110 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002111
2112 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2113 // need to tell the clang type it is actually a class.
2114 if (class_language != eLanguageTypeObjC)
2115 {
2116 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2117 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2118 }
2119
2120 // Since DW_TAG_structure_type gets used for both classes
2121 // and structures, we may need to set any DW_TAG_member
2122 // fields to have a "private" access if none was specified.
2123 // When we parsed the child members we tracked that actual
2124 // accessibility value for each DW_TAG_member in the
2125 // "member_accessibilities" array. If the value for the
2126 // member is zero, then it was set to the "default_accessibility"
2127 // which for structs was "public". Below we correct this
2128 // by setting any fields to "private" that weren't correctly
2129 // set.
2130 if (is_a_class && !member_accessibilities.empty())
2131 {
2132 // This is a class and all members that didn't have
2133 // their access specified are private.
2134 ast.SetDefaultAccessForRecordFields (clang_type,
2135 eAccessPrivate,
2136 &member_accessibilities.front(),
2137 member_accessibilities.size());
2138 }
2139
2140 if (!base_classes.empty())
2141 {
2142 ast.SetBaseClassesForClassType (clang_type,
2143 &base_classes.front(),
2144 base_classes.size());
2145
2146 // Clang will copy each CXXBaseSpecifier in "base_classes"
2147 // so we have to free them all.
2148 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2149 base_classes.size());
2150 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002151 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002152 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002153
2154 ast.BuildIndirectFields (clang_type);
2155
Sean Callanan77a1fd32012-02-27 20:07:01 +00002156 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002157
Greg Claytoncaab74e2012-01-28 00:48:57 +00002158 if (!layout_info.field_offsets.empty())
2159 {
2160 if (type)
2161 layout_info.bit_size = type->GetByteSize() * 8;
2162 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002163 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Greg Claytoncaab74e2012-01-28 00:48:57 +00002164 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
2165 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
2166 if (record_type)
2167 {
2168 const clang::RecordDecl *record_decl = record_type->getDecl();
2169
2170 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002171 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002172 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002173 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) caching layout info for record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u], base_offsets[0], vbase_offsets[0])",
Greg Claytoncaab74e2012-01-28 00:48:57 +00002174 clang_type,
2175 record_decl,
2176 layout_info.bit_size,
2177 layout_info.alignment,
2178 (uint32_t)layout_info.field_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002179
Greg Clayton821ac6d2012-01-28 02:22:27 +00002180 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
2181 for (pos = layout_info.field_offsets.begin(); pos != end; ++pos)
2182 {
2183 GetObjectFile()->GetModule()->LogMessage (log.get(),
2184 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field = { bit_offset=%u, name='%s' }",
2185 clang_type,
2186 (uint32_t)pos->second,
2187 pos->first->getNameAsString().c_str());
2188 }
2189 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002190 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2191 }
2192 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002193 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002194
Greg Claytonc93237c2010-10-01 20:48:32 +00002195 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002196
2197 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002198 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002199 if (die->HasChildren())
2200 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002201 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
2202 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002203 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002204 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002205 return clang_type;
2206
2207 default:
2208 assert(false && "not a forward clang type decl!");
2209 break;
2210 }
2211 return NULL;
2212}
2213
Greg Claytonc685f8e2010-09-15 04:15:46 +00002214Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002215SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002216{
2217 if (type_die != NULL)
2218 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002219 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002220
Greg Claytonc685f8e2010-09-15 04:15:46 +00002221 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002222 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002223
Greg Clayton24739922010-10-13 03:15:28 +00002224 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002225 {
2226 if (type != DIE_IS_BEING_PARSED)
2227 return type;
2228
2229 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002230 type_die->GetOffset(),
2231 DW_TAG_value_to_name(type_die->Tag()),
2232 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002233
2234 }
2235 else
2236 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002237 }
2238 return NULL;
2239}
2240
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002241CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002242SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243{
2244 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002245 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002246 {
2247 // The symbol vendor doesn't know about this compile unit, we
2248 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002249 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002250 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002251 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002252}
2253
2254bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002255SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002256{
2257 sc.Clear();
2258 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002259 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260
Greg Clayton81c22f62011-10-19 18:09:39 +00002261 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002262 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002263 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002264
2265 if (sc.function)
2266 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002267 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002268 return true;
2269 }
2270
2271 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002272}
2273
2274uint32_t
2275SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2276{
2277 Timer scoped_timer(__PRETTY_FUNCTION__,
2278 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002279 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002280 so_addr.GetOffset(),
2281 resolve_scope);
2282 uint32_t resolved = 0;
2283 if (resolve_scope & ( eSymbolContextCompUnit |
2284 eSymbolContextFunction |
2285 eSymbolContextBlock |
2286 eSymbolContextLineEntry))
2287 {
2288 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2289
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002290 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002291 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002292 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002293 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002294 if (cu_offset != DW_INVALID_OFFSET)
2295 {
2296 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002297 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2298 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002299 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002300 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002301 assert(sc.comp_unit != NULL);
2302 resolved |= eSymbolContextCompUnit;
2303
2304 if (resolve_scope & eSymbolContextLineEntry)
2305 {
2306 LineTable *line_table = sc.comp_unit->GetLineTable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002307 if (line_table != NULL)
2308 {
2309 if (so_addr.IsLinkedAddress())
2310 {
2311 Address linked_addr (so_addr);
2312 linked_addr.ResolveLinkedAddress();
2313 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2314 {
2315 resolved |= eSymbolContextLineEntry;
2316 }
2317 }
2318 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
2319 {
2320 resolved |= eSymbolContextLineEntry;
2321 }
2322 }
2323 }
2324
2325 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2326 {
2327 DWARFDebugInfoEntry *function_die = NULL;
2328 DWARFDebugInfoEntry *block_die = NULL;
2329 if (resolve_scope & eSymbolContextBlock)
2330 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002331 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002332 }
2333 else
2334 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002335 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002336 }
2337
2338 if (function_die != NULL)
2339 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002340 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002341 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002342 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002343 }
Greg Clayton12b834d2012-03-29 21:43:25 +00002344 else
2345 {
2346 // We might have had a compile unit that had discontiguous
2347 // address ranges where the gaps are symbols that don't have
2348 // any debug info. Discontiguous compile unit address ranges
2349 // should only happen when there aren't other functions from
2350 // other compile units in these gaps. This helps keep the size
2351 // of the aranges down.
2352 sc.comp_unit = NULL;
2353 resolved &= ~eSymbolContextCompUnit;
2354 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002355
2356 if (sc.function != NULL)
2357 {
2358 resolved |= eSymbolContextFunction;
2359
2360 if (resolve_scope & eSymbolContextBlock)
2361 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002362 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002363
2364 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002365 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002366 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002367 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002368 if (sc.block)
2369 resolved |= eSymbolContextBlock;
2370 }
2371 }
2372 }
2373 }
2374 }
2375 }
2376 }
2377 return resolved;
2378}
2379
2380
2381
2382uint32_t
2383SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2384{
2385 const uint32_t prev_size = sc_list.GetSize();
2386 if (resolve_scope & eSymbolContextCompUnit)
2387 {
2388 DWARFDebugInfo* debug_info = DebugInfo();
2389 if (debug_info)
2390 {
2391 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002392 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002393
Greg Clayton53eb1c22012-04-02 22:59:12 +00002394 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002395 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002396 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002397 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
2398 if (check_inlines || file_spec_matches_cu_file_spec)
2399 {
2400 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002401 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002402 assert(sc.comp_unit != NULL);
2403
2404 uint32_t file_idx = UINT32_MAX;
2405
2406 // If we are looking for inline functions only and we don't
2407 // find it in the support files, we are done.
2408 if (check_inlines)
2409 {
Jim Ingham87df91b2011-09-23 00:54:11 +00002410 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002411 if (file_idx == UINT32_MAX)
2412 continue;
2413 }
2414
2415 if (line != 0)
2416 {
2417 LineTable *line_table = sc.comp_unit->GetLineTable();
2418
2419 if (line_table != NULL && line != 0)
2420 {
2421 // We will have already looked up the file index if
2422 // we are searching for inline entries.
2423 if (!check_inlines)
Jim Ingham87df91b2011-09-23 00:54:11 +00002424 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002425
2426 if (file_idx != UINT32_MAX)
2427 {
2428 uint32_t found_line;
2429 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2430 found_line = sc.line_entry.line;
2431
Greg Clayton016a95e2010-09-14 02:20:48 +00002432 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002433 {
2434 sc.function = NULL;
2435 sc.block = NULL;
2436 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2437 {
2438 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2439 if (file_vm_addr != LLDB_INVALID_ADDRESS)
2440 {
2441 DWARFDebugInfoEntry *function_die = NULL;
2442 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002443 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002444
2445 if (function_die != NULL)
2446 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002447 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002448 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002449 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002450 }
2451
2452 if (sc.function != NULL)
2453 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002454 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002455
2456 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002457 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002458 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002459 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002460 }
2461 }
2462 }
2463
2464 sc_list.Append(sc);
2465 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2466 }
2467 }
2468 }
2469 else if (file_spec_matches_cu_file_spec && !check_inlines)
2470 {
2471 // only append the context if we aren't looking for inline call sites
2472 // by file and line and if the file spec matches that of the compile unit
2473 sc_list.Append(sc);
2474 }
2475 }
2476 else if (file_spec_matches_cu_file_spec && !check_inlines)
2477 {
2478 // only append the context if we aren't looking for inline call sites
2479 // by file and line and if the file spec matches that of the compile unit
2480 sc_list.Append(sc);
2481 }
2482
2483 if (!check_inlines)
2484 break;
2485 }
2486 }
2487 }
2488 }
2489 return sc_list.GetSize() - prev_size;
2490}
2491
2492void
2493SymbolFileDWARF::Index ()
2494{
2495 if (m_indexed)
2496 return;
2497 m_indexed = true;
2498 Timer scoped_timer (__PRETTY_FUNCTION__,
2499 "SymbolFileDWARF::Index (%s)",
2500 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2501
2502 DWARFDebugInfo* debug_info = DebugInfo();
2503 if (debug_info)
2504 {
2505 uint32_t cu_idx = 0;
2506 const uint32_t num_compile_units = GetNumCompileUnits();
2507 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2508 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002509 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002510
Greg Clayton53eb1c22012-04-02 22:59:12 +00002511 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002512
Greg Clayton53eb1c22012-04-02 22:59:12 +00002513 dwarf_cu->Index (cu_idx,
2514 m_function_basename_index,
2515 m_function_fullname_index,
2516 m_function_method_index,
2517 m_function_selector_index,
2518 m_objc_class_selectors_index,
2519 m_global_index,
2520 m_type_index,
2521 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002522
2523 // Keep memory down by clearing DIEs if this generate function
2524 // caused them to be parsed
2525 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002526 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002527 }
2528
Greg Claytond4a2b372011-09-12 23:21:58 +00002529 m_function_basename_index.Finalize();
2530 m_function_fullname_index.Finalize();
2531 m_function_method_index.Finalize();
2532 m_function_selector_index.Finalize();
2533 m_objc_class_selectors_index.Finalize();
2534 m_global_index.Finalize();
2535 m_type_index.Finalize();
2536 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002537
Greg Clayton24739922010-10-13 03:15:28 +00002538#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002539 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002540 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002541 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2542 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002543 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2544 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2545 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2546 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2547 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2548 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002549 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002550 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002551#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002552 }
2553}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002554
2555bool
2556SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2557{
2558 if (namespace_decl == NULL)
2559 {
2560 // Invalid namespace decl which means we aren't matching only things
2561 // in this symbol file, so return true to indicate it matches this
2562 // symbol file.
2563 return true;
2564 }
2565
2566 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2567
2568 if (namespace_ast == NULL)
2569 return true; // No AST in the "namespace_decl", return true since it
2570 // could then match any symbol file, including this one
2571
2572 if (namespace_ast == GetClangASTContext().getASTContext())
2573 return true; // The ASTs match, return true
2574
2575 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002576 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2577
2578 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002579 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002580
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002581 return false;
2582}
2583
Greg Clayton2506a7a2011-10-12 23:34:26 +00002584bool
2585SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2586 DWARFCompileUnit* cu,
2587 const DWARFDebugInfoEntry* die)
2588{
2589 // No namespace specified, so the answesr i
2590 if (namespace_decl == NULL)
2591 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002592
2593 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002594
Greg Clayton437a1352012-04-09 22:43:43 +00002595 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
2596 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002597 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00002598 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00002599 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00002600
Greg Clayton2506a7a2011-10-12 23:34:26 +00002601 if (clang_namespace_decl)
2602 {
2603 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002604 {
2605 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002606 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002607 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002608 }
2609
Greg Clayton437a1352012-04-09 22:43:43 +00002610 if (clang_namespace_decl == die_clang_decl_ctx)
2611 return true;
2612 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00002613 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00002614 }
2615 else
2616 {
2617 // We have a namespace_decl that was not NULL but it contained
2618 // a NULL "clang::NamespaceDecl", so this means the global namespace
2619 // So as long the the contained decl context DIE isn't a namespace
2620 // we should be ok.
2621 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2622 return true;
2623 }
2624 }
Sean Callananebe60672011-10-13 21:50:33 +00002625
2626 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002627 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002628
Greg Clayton2506a7a2011-10-12 23:34:26 +00002629 return false;
2630}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002631uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002632SymbolFileDWARF::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 +00002633{
Greg Clayton21f2a492011-10-06 00:09:08 +00002634 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2635
2636 if (log)
2637 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002638 GetObjectFile()->GetModule()->LogMessage (log.get(),
2639 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2640 name.GetCString(),
2641 namespace_decl,
2642 append,
2643 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002644 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002645
2646 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2647 return 0;
2648
Greg Claytonc685f8e2010-09-15 04:15:46 +00002649 DWARFDebugInfo* info = DebugInfo();
2650 if (info == NULL)
2651 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002652
2653 // If we aren't appending the results to this list, then clear the list
2654 if (!append)
2655 variables.Clear();
2656
2657 // Remember how many variables are in the list before we search in case
2658 // we are appending the results to a variable list.
2659 const uint32_t original_size = variables.GetSize();
2660
Greg Claytond4a2b372011-09-12 23:21:58 +00002661 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002662
Greg Clayton97fbc342011-10-20 22:30:33 +00002663 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002664 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002665 if (m_apple_names_ap.get())
2666 {
2667 const char *name_cstr = name.GetCString();
2668 const char *base_name_start;
2669 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002670
Greg Clayton97fbc342011-10-20 22:30:33 +00002671 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2672 base_name_start = name_cstr;
2673
2674 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2675 }
Greg Clayton7f995132011-10-04 22:41:51 +00002676 }
2677 else
2678 {
2679 // Index the DWARF if we haven't already
2680 if (!m_indexed)
2681 Index ();
2682
2683 m_global_index.Find (name, die_offsets);
2684 }
2685
Greg Clayton437a1352012-04-09 22:43:43 +00002686 const size_t num_die_matches = die_offsets.size();
2687 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002688 {
Greg Clayton7f995132011-10-04 22:41:51 +00002689 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002690 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002691 assert (sc.module_sp);
2692
Greg Claytond4a2b372011-09-12 23:21:58 +00002693 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002694 DWARFCompileUnit* dwarf_cu = NULL;
2695 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00002696 bool done = false;
2697 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002698 {
2699 const dw_offset_t die_offset = die_offsets[i];
2700 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002701
Greg Clayton95d87902011-11-11 03:16:25 +00002702 if (die)
2703 {
Greg Clayton437a1352012-04-09 22:43:43 +00002704 switch (die->Tag())
2705 {
2706 default:
2707 case DW_TAG_subprogram:
2708 case DW_TAG_inlined_subroutine:
2709 case DW_TAG_try_block:
2710 case DW_TAG_catch_block:
2711 break;
2712
2713 case DW_TAG_variable:
2714 {
2715 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2716 assert(sc.comp_unit != NULL);
2717
2718 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2719 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002720
Greg Clayton437a1352012-04-09 22:43:43 +00002721 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002722
Greg Clayton437a1352012-04-09 22:43:43 +00002723 if (variables.GetSize() - original_size >= max_matches)
2724 done = true;
2725 }
2726 break;
2727 }
Greg Clayton95d87902011-11-11 03:16:25 +00002728 }
2729 else
2730 {
2731 if (m_using_apple_tables)
2732 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002733 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2734 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002735 }
2736 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002737 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002738 }
2739
2740 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002741 const uint32_t num_matches = variables.GetSize() - original_size;
2742 if (log && num_matches > 0)
2743 {
2744 GetObjectFile()->GetModule()->LogMessage (log.get(),
2745 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
2746 name.GetCString(),
2747 namespace_decl,
2748 append,
2749 max_matches,
2750 num_matches);
2751 }
2752 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002753}
2754
2755uint32_t
2756SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2757{
Greg Clayton21f2a492011-10-06 00:09:08 +00002758 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2759
2760 if (log)
2761 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002762 GetObjectFile()->GetModule()->LogMessage (log.get(),
2763 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2764 regex.GetText(),
2765 append,
2766 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002767 }
2768
Greg Claytonc685f8e2010-09-15 04:15:46 +00002769 DWARFDebugInfo* info = DebugInfo();
2770 if (info == NULL)
2771 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002772
2773 // If we aren't appending the results to this list, then clear the list
2774 if (!append)
2775 variables.Clear();
2776
2777 // Remember how many variables are in the list before we search in case
2778 // we are appending the results to a variable list.
2779 const uint32_t original_size = variables.GetSize();
2780
Greg Clayton7f995132011-10-04 22:41:51 +00002781 DIEArray die_offsets;
2782
Greg Clayton97fbc342011-10-20 22:30:33 +00002783 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002784 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002785 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002786 {
2787 DWARFMappedHash::DIEInfoArray hash_data_array;
2788 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2789 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2790 }
Greg Clayton7f995132011-10-04 22:41:51 +00002791 }
2792 else
2793 {
2794 // Index the DWARF if we haven't already
2795 if (!m_indexed)
2796 Index ();
2797
2798 m_global_index.Find (regex, die_offsets);
2799 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002800
Greg Claytonc685f8e2010-09-15 04:15:46 +00002801 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002802 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002803 assert (sc.module_sp);
2804
Greg Claytond4a2b372011-09-12 23:21:58 +00002805 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002806 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002807 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002808 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002809 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002810 DWARFDebugInfo* debug_info = DebugInfo();
2811 for (size_t i=0; i<num_matches; ++i)
2812 {
2813 const dw_offset_t die_offset = die_offsets[i];
2814 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002815
2816 if (die)
2817 {
2818 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002819
Greg Clayton95d87902011-11-11 03:16:25 +00002820 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002821
Greg Clayton95d87902011-11-11 03:16:25 +00002822 if (variables.GetSize() - original_size >= max_matches)
2823 break;
2824 }
2825 else
2826 {
2827 if (m_using_apple_tables)
2828 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002829 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2830 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002831 }
2832 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002833 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002834 }
2835
2836 // Return the number of variable that were appended to the list
2837 return variables.GetSize() - original_size;
2838}
2839
Greg Claytonaa044962011-10-13 00:59:38 +00002840
Jim Ingham4cda6e02011-10-07 22:23:45 +00002841bool
2842SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2843 DWARFCompileUnit *&dwarf_cu,
2844 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002845{
Greg Claytonaa044962011-10-13 00:59:38 +00002846 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2847 return ResolveFunction (dwarf_cu, die, sc_list);
2848}
2849
2850
2851bool
2852SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2853 const DWARFDebugInfoEntry *die,
2854 SymbolContextList& sc_list)
2855{
Greg Clayton9e315582011-09-02 04:03:59 +00002856 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002857
2858 if (die == NULL)
2859 return false;
2860
Jim Ingham4cda6e02011-10-07 22:23:45 +00002861 // If we were passed a die that is not a function, just return false...
2862 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2863 return false;
2864
2865 const DWARFDebugInfoEntry* inlined_die = NULL;
2866 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002867 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002868 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002869
Jim Ingham4cda6e02011-10-07 22:23:45 +00002870 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002871 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002872 if (die->Tag() == DW_TAG_subprogram)
2873 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002874 }
2875 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002876 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002877 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002878 {
2879 Address addr;
2880 // Parse all blocks if needed
2881 if (inlined_die)
2882 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002883 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00002884 assert (sc.block != NULL);
2885 if (sc.block->GetStartAddress (addr) == false)
2886 addr.Clear();
2887 }
2888 else
2889 {
2890 sc.block = NULL;
2891 addr = sc.function->GetAddressRange().GetBaseAddress();
2892 }
2893
2894 if (addr.IsValid())
2895 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002896 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002897 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002898 }
2899 }
2900
Greg Claytonaa044962011-10-13 00:59:38 +00002901 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002902}
2903
Greg Clayton7f995132011-10-04 22:41:51 +00002904void
2905SymbolFileDWARF::FindFunctions (const ConstString &name,
2906 const NameToDIE &name_to_die,
2907 SymbolContextList& sc_list)
2908{
Greg Claytond4a2b372011-09-12 23:21:58 +00002909 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002910 if (name_to_die.Find (name, die_offsets))
2911 {
2912 ParseFunctions (die_offsets, sc_list);
2913 }
2914}
2915
2916
2917void
2918SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2919 const NameToDIE &name_to_die,
2920 SymbolContextList& sc_list)
2921{
2922 DIEArray die_offsets;
2923 if (name_to_die.Find (regex, die_offsets))
2924 {
2925 ParseFunctions (die_offsets, sc_list);
2926 }
2927}
2928
2929
2930void
2931SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2932 const DWARFMappedHash::MemoryTable &memory_table,
2933 SymbolContextList& sc_list)
2934{
2935 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002936 DWARFMappedHash::DIEInfoArray hash_data_array;
2937 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002938 {
Greg Claytond1767f02011-12-08 02:13:16 +00002939 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002940 ParseFunctions (die_offsets, sc_list);
2941 }
2942}
2943
2944void
2945SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2946 SymbolContextList& sc_list)
2947{
2948 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002949 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002950 {
Greg Clayton7f995132011-10-04 22:41:51 +00002951 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00002952
2953 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002954 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002955 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002956 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00002957 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002958 }
2959 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002960}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002961
Jim Ingham4cda6e02011-10-07 22:23:45 +00002962bool
2963SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2964 const DWARFCompileUnit *dwarf_cu,
2965 uint32_t name_type_mask,
2966 const char *partial_name,
2967 const char *base_name_start,
2968 const char *base_name_end)
2969{
2970 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2971 if (name_type_mask == eFunctionNameTypeMethod
2972 || name_type_mask == eFunctionNameTypeBase)
2973 {
Greg Claytonf0705c82011-10-22 03:33:13 +00002974 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2975 if (!containing_decl_ctx)
2976 return false;
2977
2978 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
2979
2980 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2981 return false;
2982 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2983 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002984 }
2985
2986 // Now we need to check whether the name we got back for this type matches the extra specifications
2987 // that were in the name we're looking up:
2988 if (base_name_start != partial_name || *base_name_end != '\0')
2989 {
2990 // First see if the stuff to the left matches the full name. To do that let's see if
2991 // we can pull out the mips linkage name attribute:
2992
2993 Mangled best_name;
2994
2995 DWARFDebugInfoEntry::Attributes attributes;
2996 die->GetAttributes(this, dwarf_cu, NULL, attributes);
2997 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
2998 if (idx != UINT32_MAX)
2999 {
3000 DWARFFormValue form_value;
3001 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3002 {
3003 const char *name = form_value.AsCString(&get_debug_str_data());
3004 best_name.SetValue (name, true);
3005 }
3006 }
3007 if (best_name)
3008 {
3009 const char *demangled = best_name.GetDemangledName().GetCString();
3010 if (demangled)
3011 {
3012 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003013 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3014 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003015 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003016 else
3017 {
3018 // Sort out the case where our name is something like "Process::Destroy" and the match is
3019 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3020 // namespace boundaries...
3021
3022 if (partial_name[0] == ':' && partial_name[1] == ':')
3023 {
3024 // The partial name was already on a namespace boundary so all matches are good.
3025 return true;
3026 }
3027 else if (partial_in_demangled == demangled)
3028 {
3029 // They both start the same, so this is an good match.
3030 return true;
3031 }
3032 else
3033 {
3034 if (partial_in_demangled - demangled == 1)
3035 {
3036 // Only one character difference, can't be a namespace boundary...
3037 return false;
3038 }
3039 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3040 {
3041 // We are on a namespace boundary, so this is also good.
3042 return true;
3043 }
3044 else
3045 return false;
3046 }
3047 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003048 }
3049 }
3050 }
3051
3052 return true;
3053}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003054
Greg Clayton0c5cd902010-06-28 21:30:43 +00003055uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003056SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003057 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003058 uint32_t name_type_mask,
3059 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003060 bool append,
3061 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003062{
3063 Timer scoped_timer (__PRETTY_FUNCTION__,
3064 "SymbolFileDWARF::FindFunctions (name = '%s')",
3065 name.AsCString());
3066
Greg Clayton21f2a492011-10-06 00:09:08 +00003067 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3068
3069 if (log)
3070 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003071 GetObjectFile()->GetModule()->LogMessage (log.get(),
3072 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3073 name.GetCString(),
3074 name_type_mask,
3075 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003076 }
3077
Greg Clayton0c5cd902010-06-28 21:30:43 +00003078 // If we aren't appending the results to this list, then clear the list
3079 if (!append)
3080 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003081
3082 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3083 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003084
3085 // If name is empty then we won't find anything.
3086 if (name.IsEmpty())
3087 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003088
3089 // Remember how many sc_list are in the list before we search in case
3090 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003091
Greg Clayton9e315582011-09-02 04:03:59 +00003092 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00003093
Jim Ingham4cda6e02011-10-07 22:23:45 +00003094 const char *name_cstr = name.GetCString();
3095 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
3096 const char *base_name_start = name_cstr;
3097 const char *base_name_end = name_cstr + strlen(name_cstr);
3098
3099 if (name_type_mask & eFunctionNameTypeAuto)
3100 {
3101 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
3102 effective_name_type_mask = eFunctionNameTypeFull;
3103 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
3104 effective_name_type_mask = eFunctionNameTypeFull;
3105 else
3106 {
3107 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3108 effective_name_type_mask |= eFunctionNameTypeSelector;
3109
3110 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3111 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
3112 }
3113 }
3114 else
3115 {
3116 effective_name_type_mask = name_type_mask;
3117 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
3118 {
3119 // If they've asked for a CPP method or function name and it can't be that, we don't
3120 // even need to search for CPP methods or names.
3121 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3122 {
3123 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
3124 if (effective_name_type_mask == eFunctionNameTypeNone)
3125 return 0;
3126 }
3127 }
3128
3129 if (effective_name_type_mask & eFunctionNameTypeSelector)
3130 {
3131 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3132 {
3133 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
3134 if (effective_name_type_mask == eFunctionNameTypeNone)
3135 return 0;
3136 }
3137 }
3138 }
3139
3140 DWARFDebugInfo* info = DebugInfo();
3141 if (info == NULL)
3142 return 0;
3143
Greg Claytonaa044962011-10-13 00:59:38 +00003144 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003145 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003146 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003147 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003148 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003149
3150 DIEArray die_offsets;
3151
3152 uint32_t num_matches = 0;
3153
3154 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003155 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003156 // If they asked for the full name, match what they typed. At some point we may
3157 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3158 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003159 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003160 for (uint32_t i = 0; i < num_matches; i++)
3161 {
Greg Clayton95d87902011-11-11 03:16:25 +00003162 const dw_offset_t die_offset = die_offsets[i];
3163 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003164 if (die)
3165 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003166 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3167 continue;
3168
Sean Callanan9df05fb2012-02-10 22:52:19 +00003169 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3170 continue;
3171
Greg Claytonaa044962011-10-13 00:59:38 +00003172 ResolveFunction (dwarf_cu, die, sc_list);
3173 }
Greg Clayton95d87902011-11-11 03:16:25 +00003174 else
3175 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003176 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3177 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003178 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003179 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003180 }
3181 else
3182 {
3183 if (effective_name_type_mask & eFunctionNameTypeSelector)
3184 {
3185 if (namespace_decl && *namespace_decl)
3186 return 0; // no selectors in namespaces
3187
3188 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3189 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3190 // and if it is an ObjC method name, we're good.
3191
3192 for (uint32_t i = 0; i < num_matches; i++)
3193 {
Greg Clayton95d87902011-11-11 03:16:25 +00003194 const dw_offset_t die_offset = die_offsets[i];
3195 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003196 if (die)
3197 {
3198 const char *die_name = die->GetName(this, dwarf_cu);
3199 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003200 {
3201 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3202 continue;
3203
Greg Clayton97fbc342011-10-20 22:30:33 +00003204 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003205 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003206 }
Greg Clayton95d87902011-11-11 03:16:25 +00003207 else
3208 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003209 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3210 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003211 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003212 }
3213 die_offsets.clear();
3214 }
3215
3216 if (effective_name_type_mask & eFunctionNameTypeMethod
3217 || effective_name_type_mask & eFunctionNameTypeBase)
3218 {
3219 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3220 (namespace_decl && *namespace_decl))
3221 return 0; // no methods in namespaces
3222
3223 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3224 // extract the base name, look that up, and if there is any other information in the name we were
3225 // passed in we have to post-filter based on that.
3226
3227 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3228 std::string base_name(base_name_start, base_name_end - base_name_start);
3229 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3230
3231 for (uint32_t i = 0; i < num_matches; i++)
3232 {
Greg Clayton95d87902011-11-11 03:16:25 +00003233 const dw_offset_t die_offset = die_offsets[i];
3234 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003235 if (die)
3236 {
3237 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3238 continue;
3239
3240 if (!FunctionDieMatchesPartialName(die,
3241 dwarf_cu,
3242 effective_name_type_mask,
3243 name_cstr,
3244 base_name_start,
3245 base_name_end))
3246 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003247
3248 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3249 continue;
Greg Clayton97fbc342011-10-20 22:30:33 +00003250
3251 // If we get to here, the die is good, and we should add it:
3252 ResolveFunction (dwarf_cu, die, sc_list);
3253 }
Greg Clayton95d87902011-11-11 03:16:25 +00003254 else
3255 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003256 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3257 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003258 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003259 }
3260 die_offsets.clear();
3261 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003262 }
3263 }
Greg Clayton7f995132011-10-04 22:41:51 +00003264 }
3265 else
3266 {
3267
3268 // Index the DWARF if we haven't already
3269 if (!m_indexed)
3270 Index ();
3271
Greg Clayton7f995132011-10-04 22:41:51 +00003272 if (name_type_mask & eFunctionNameTypeFull)
3273 FindFunctions (name, m_function_fullname_index, sc_list);
3274
Jim Ingham4cda6e02011-10-07 22:23:45 +00003275 std::string base_name(base_name_start, base_name_end - base_name_start);
3276 ConstString base_name_const(base_name.c_str());
3277 DIEArray die_offsets;
3278 DWARFCompileUnit *dwarf_cu = NULL;
3279
3280 if (effective_name_type_mask & eFunctionNameTypeBase)
3281 {
3282 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003283 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003284 {
Greg Claytonaa044962011-10-13 00:59:38 +00003285 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3286 if (die)
3287 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003288 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3289 continue;
3290
Greg Claytonaa044962011-10-13 00:59:38 +00003291 if (!FunctionDieMatchesPartialName(die,
3292 dwarf_cu,
3293 effective_name_type_mask,
3294 name_cstr,
3295 base_name_start,
3296 base_name_end))
3297 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003298
Sean Callanan9df05fb2012-02-10 22:52:19 +00003299 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3300 continue;
3301
Greg Claytonaa044962011-10-13 00:59:38 +00003302 // If we get to here, the die is good, and we should add it:
3303 ResolveFunction (dwarf_cu, die, sc_list);
3304 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003305 }
3306 die_offsets.clear();
3307 }
3308
Jim Inghamea8005a2011-10-11 01:18:11 +00003309 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003310 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003311 if (namespace_decl && *namespace_decl)
3312 return 0; // no methods in namespaces
3313
Jim Ingham4cda6e02011-10-07 22:23:45 +00003314 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3315 {
Greg Claytonaa044962011-10-13 00:59:38 +00003316 for (uint32_t i = 0; i < num_base; i++)
3317 {
3318 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3319 if (die)
3320 {
3321 if (!FunctionDieMatchesPartialName(die,
3322 dwarf_cu,
3323 effective_name_type_mask,
3324 name_cstr,
3325 base_name_start,
3326 base_name_end))
3327 continue;
3328
Sean Callanan9df05fb2012-02-10 22:52:19 +00003329 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3330 continue;
3331
Greg Claytonaa044962011-10-13 00:59:38 +00003332 // If we get to here, the die is good, and we should add it:
3333 ResolveFunction (dwarf_cu, die, sc_list);
3334 }
3335 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003336 }
3337 die_offsets.clear();
3338 }
Greg Clayton7f995132011-10-04 22:41:51 +00003339
Sean Callanan213fdb82011-10-13 01:49:10 +00003340 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003341 {
Greg Clayton7f995132011-10-04 22:41:51 +00003342 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003343 }
3344
Greg Clayton4d01ace2011-09-29 16:58:15 +00003345 }
3346
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003347 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003348 const uint32_t num_matches = sc_list.GetSize() - original_size;
3349
3350 if (log && num_matches > 0)
3351 {
3352 GetObjectFile()->GetModule()->LogMessage (log.get(),
3353 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u",
3354 name.GetCString(),
3355 name_type_mask,
3356 append,
3357 num_matches);
3358 }
3359 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003360}
3361
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003362uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003363SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003364{
3365 Timer scoped_timer (__PRETTY_FUNCTION__,
3366 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3367 regex.GetText());
3368
Greg Clayton21f2a492011-10-06 00:09:08 +00003369 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3370
3371 if (log)
3372 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003373 GetObjectFile()->GetModule()->LogMessage (log.get(),
3374 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3375 regex.GetText(),
3376 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003377 }
3378
3379
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003380 // If we aren't appending the results to this list, then clear the list
3381 if (!append)
3382 sc_list.Clear();
3383
3384 // Remember how many sc_list are in the list before we search in case
3385 // we are appending the results to a variable list.
3386 uint32_t original_size = sc_list.GetSize();
3387
Greg Clayton97fbc342011-10-20 22:30:33 +00003388 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003389 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003390 if (m_apple_names_ap.get())
3391 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003392 }
3393 else
3394 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003395 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003396 if (!m_indexed)
3397 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003398
Greg Clayton7f995132011-10-04 22:41:51 +00003399 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003400
Greg Clayton7f995132011-10-04 22:41:51 +00003401 FindFunctions (regex, m_function_fullname_index, sc_list);
3402 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003403
3404 // Return the number of variable that were appended to the list
3405 return sc_list.GetSize() - original_size;
3406}
Jim Ingham318c9f22011-08-26 19:44:13 +00003407
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003408uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003409SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3410 const ConstString &name,
3411 const lldb_private::ClangNamespaceDecl *namespace_decl,
3412 bool append,
3413 uint32_t max_matches,
3414 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003415{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003416 DWARFDebugInfo* info = DebugInfo();
3417 if (info == NULL)
3418 return 0;
3419
Greg Clayton21f2a492011-10-06 00:09:08 +00003420 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3421
3422 if (log)
3423 {
Greg Clayton437a1352012-04-09 22:43:43 +00003424 if (namespace_decl)
3425 {
3426 GetObjectFile()->GetModule()->LogMessage (log.get(),
3427 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
3428 name.GetCString(),
3429 namespace_decl->GetNamespaceDecl(),
3430 namespace_decl->GetQualifiedName().c_str(),
3431 append,
3432 max_matches);
3433 }
3434 else
3435 {
3436 GetObjectFile()->GetModule()->LogMessage (log.get(),
3437 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
3438 name.GetCString(),
3439 append,
3440 max_matches);
3441 }
Greg Clayton21f2a492011-10-06 00:09:08 +00003442 }
3443
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003444 // If we aren't appending the results to this list, then clear the list
3445 if (!append)
3446 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003447
3448 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3449 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003450
Greg Claytond4a2b372011-09-12 23:21:58 +00003451 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003452
Greg Clayton97fbc342011-10-20 22:30:33 +00003453 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003454 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003455 if (m_apple_types_ap.get())
3456 {
3457 const char *name_cstr = name.GetCString();
3458 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3459 }
Greg Clayton7f995132011-10-04 22:41:51 +00003460 }
3461 else
3462 {
3463 if (!m_indexed)
3464 Index ();
3465
3466 m_type_index.Find (name, die_offsets);
3467 }
3468
Greg Clayton437a1352012-04-09 22:43:43 +00003469 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003470
Greg Clayton437a1352012-04-09 22:43:43 +00003471 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003472 {
Greg Clayton7f995132011-10-04 22:41:51 +00003473 const uint32_t initial_types_size = types.GetSize();
3474 DWARFCompileUnit* dwarf_cu = NULL;
3475 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003476 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003477 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003478 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003479 const dw_offset_t die_offset = die_offsets[i];
3480 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3481
Greg Clayton95d87902011-11-11 03:16:25 +00003482 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003483 {
Greg Clayton95d87902011-11-11 03:16:25 +00003484 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3485 continue;
3486
3487 Type *matching_type = ResolveType (dwarf_cu, die);
3488 if (matching_type)
3489 {
3490 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003491 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003492 if (types.GetSize() >= max_matches)
3493 break;
3494 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003495 }
Greg Clayton95d87902011-11-11 03:16:25 +00003496 else
3497 {
3498 if (m_using_apple_tables)
3499 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003500 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3501 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003502 }
3503 }
3504
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003505 }
Greg Clayton437a1352012-04-09 22:43:43 +00003506 const uint32_t num_matches = types.GetSize() - initial_types_size;
3507 if (log && num_matches)
3508 {
3509 if (namespace_decl)
3510 {
3511 GetObjectFile()->GetModule()->LogMessage (log.get(),
3512 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
3513 name.GetCString(),
3514 namespace_decl->GetNamespaceDecl(),
3515 namespace_decl->GetQualifiedName().c_str(),
3516 append,
3517 max_matches,
3518 num_matches);
3519 }
3520 else
3521 {
3522 GetObjectFile()->GetModule()->LogMessage (log.get(),
3523 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
3524 name.GetCString(),
3525 append,
3526 max_matches,
3527 num_matches);
3528 }
3529 }
3530 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003531 }
Greg Clayton7f995132011-10-04 22:41:51 +00003532 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003533}
3534
3535
Greg Clayton526e5af2010-11-13 03:52:47 +00003536ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003537SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003538 const ConstString &name,
3539 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003540{
Greg Clayton21f2a492011-10-06 00:09:08 +00003541 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3542
3543 if (log)
3544 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003545 GetObjectFile()->GetModule()->LogMessage (log.get(),
3546 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3547 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003548 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003549
3550 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3551 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003552
Greg Clayton526e5af2010-11-13 03:52:47 +00003553 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003554 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003555 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003556 {
Greg Clayton7f995132011-10-04 22:41:51 +00003557 DIEArray die_offsets;
3558
Greg Clayton526e5af2010-11-13 03:52:47 +00003559 // Index if we already haven't to make sure the compile units
3560 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003561 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003562 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003563 if (m_apple_namespaces_ap.get())
3564 {
3565 const char *name_cstr = name.GetCString();
3566 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3567 }
Greg Clayton7f995132011-10-04 22:41:51 +00003568 }
3569 else
3570 {
3571 if (!m_indexed)
3572 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003573
Greg Clayton7f995132011-10-04 22:41:51 +00003574 m_namespace_index.Find (name, die_offsets);
3575 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003576
3577 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003578 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003579 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003580 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003581 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003582 DWARFDebugInfo* debug_info = DebugInfo();
3583 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003584 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003585 const dw_offset_t die_offset = die_offsets[i];
3586 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003587
Greg Clayton95d87902011-11-11 03:16:25 +00003588 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003589 {
Greg Clayton95d87902011-11-11 03:16:25 +00003590 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3591 continue;
3592
3593 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3594 if (clang_namespace_decl)
3595 {
3596 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3597 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003598 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003599 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003600 }
Greg Clayton95d87902011-11-11 03:16:25 +00003601 else
3602 {
3603 if (m_using_apple_tables)
3604 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003605 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3606 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003607 }
3608 }
3609
Greg Clayton526e5af2010-11-13 03:52:47 +00003610 }
3611 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003612 }
Greg Clayton437a1352012-04-09 22:43:43 +00003613 if (log && namespace_decl.GetNamespaceDecl())
3614 {
3615 GetObjectFile()->GetModule()->LogMessage (log.get(),
3616 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
3617 name.GetCString(),
3618 namespace_decl.GetNamespaceDecl(),
3619 namespace_decl.GetQualifiedName().c_str());
3620 }
3621
Greg Clayton526e5af2010-11-13 03:52:47 +00003622 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003623}
3624
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003625uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003626SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003627{
3628 // Remember how many sc_list are in the list before we search in case
3629 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003630 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003631
3632 const uint32_t num_die_offsets = die_offsets.size();
3633 // Parse all of the types we found from the pubtypes matches
3634 uint32_t i;
3635 uint32_t num_matches = 0;
3636 for (i = 0; i < num_die_offsets; ++i)
3637 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003638 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3639 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003640 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003641 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003642 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003643 ++num_matches;
3644 if (num_matches >= max_matches)
3645 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003646 }
3647 }
3648
3649 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003650 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003651}
3652
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003653
3654size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003655SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3656 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003657 DWARFCompileUnit* dwarf_cu,
3658 const DWARFDebugInfoEntry *parent_die,
3659 bool skip_artificial,
3660 bool &is_static,
3661 TypeList* type_list,
3662 std::vector<clang_type_t>& function_param_types,
3663 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003664 unsigned &type_quals,
3665 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003666{
3667 if (parent_die == NULL)
3668 return 0;
3669
Greg Claytond88d7592010-09-15 08:33:30 +00003670 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3671
Greg Clayton7fedea22010-11-16 02:10:54 +00003672 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003673 const DWARFDebugInfoEntry *die;
3674 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3675 {
3676 dw_tag_t tag = die->Tag();
3677 switch (tag)
3678 {
3679 case DW_TAG_formal_parameter:
3680 {
3681 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003682 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003683 if (num_attributes > 0)
3684 {
3685 const char *name = NULL;
3686 Declaration decl;
3687 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003688 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003689 // one of None, Auto, Register, Extern, Static, PrivateExtern
3690
Sean Callanane2ef6e32010-09-23 03:01:22 +00003691 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003692 uint32_t i;
3693 for (i=0; i<num_attributes; ++i)
3694 {
3695 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3696 DWARFFormValue form_value;
3697 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3698 {
3699 switch (attr)
3700 {
3701 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3702 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3703 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3704 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3705 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003706 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003707 case DW_AT_location:
3708 // if (form_value.BlockData())
3709 // {
3710 // const DataExtractor& debug_info_data = debug_info();
3711 // uint32_t block_length = form_value.Unsigned();
3712 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3713 // }
3714 // else
3715 // {
3716 // }
3717 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003718 case DW_AT_const_value:
3719 case DW_AT_default_value:
3720 case DW_AT_description:
3721 case DW_AT_endianity:
3722 case DW_AT_is_optional:
3723 case DW_AT_segment:
3724 case DW_AT_variable_parameter:
3725 default:
3726 case DW_AT_abstract_origin:
3727 case DW_AT_sibling:
3728 break;
3729 }
3730 }
3731 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003732
Greg Clayton0fffff52010-09-24 05:15:53 +00003733 bool skip = false;
3734 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003735 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003736 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003737 {
3738 // In order to determine if a C++ member function is
3739 // "const" we have to look at the const-ness of "this"...
3740 // Ugly, but that
3741 if (arg_idx == 0)
3742 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003743 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003744 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003745 // Often times compilers omit the "this" name for the
3746 // specification DIEs, so we can't rely upon the name
3747 // being in the formal parameter DIE...
3748 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003749 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003750 Type *this_type = ResolveTypeUID (param_type_die_offset);
3751 if (this_type)
3752 {
3753 uint32_t encoding_mask = this_type->GetEncodingMask();
3754 if (encoding_mask & Type::eEncodingIsPointerUID)
3755 {
3756 is_static = false;
3757
3758 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3759 type_quals |= clang::Qualifiers::Const;
3760 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3761 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003762 }
3763 }
3764 }
3765 }
3766 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003767 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003768 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003769 else
3770 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003771
Greg Clayton0fffff52010-09-24 05:15:53 +00003772 // HACK: Objective C formal parameters "self" and "_cmd"
3773 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00003774 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3775 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00003776 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003777 switch (comp_unit->GetLanguage())
3778 {
3779 case eLanguageTypeObjC:
3780 case eLanguageTypeObjC_plus_plus:
3781 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3782 skip = true;
3783 break;
3784 default:
3785 break;
3786 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003787 }
3788 }
3789 }
3790
3791 if (!skip)
3792 {
3793 Type *type = ResolveTypeUID(param_type_die_offset);
3794 if (type)
3795 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003796 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003797
Greg Clayton42ce2f32011-12-12 21:50:19 +00003798 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3799 type->GetClangForwardType(),
3800 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003801 assert(param_var_decl);
3802 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00003803
3804 GetClangASTContext().SetMetadata((uintptr_t)param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00003805 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003806 }
3807 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003808 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003809 }
3810 break;
3811
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003812 case DW_TAG_template_type_parameter:
3813 case DW_TAG_template_value_parameter:
3814 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
3815 break;
3816
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003817 default:
3818 break;
3819 }
3820 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003821 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003822}
3823
3824size_t
3825SymbolFileDWARF::ParseChildEnumerators
3826(
3827 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003828 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003829 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003830 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003831 const DWARFDebugInfoEntry *parent_die
3832)
3833{
3834 if (parent_die == NULL)
3835 return 0;
3836
3837 size_t enumerators_added = 0;
3838 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003839 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3840
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003841 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3842 {
3843 const dw_tag_t tag = die->Tag();
3844 if (tag == DW_TAG_enumerator)
3845 {
3846 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003847 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003848 if (num_child_attributes > 0)
3849 {
3850 const char *name = NULL;
3851 bool got_value = false;
3852 int64_t enum_value = 0;
3853 Declaration decl;
3854
3855 uint32_t i;
3856 for (i=0; i<num_child_attributes; ++i)
3857 {
3858 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3859 DWARFFormValue form_value;
3860 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3861 {
3862 switch (attr)
3863 {
3864 case DW_AT_const_value:
3865 got_value = true;
3866 enum_value = form_value.Unsigned();
3867 break;
3868
3869 case DW_AT_name:
3870 name = form_value.AsCString(&get_debug_str_data());
3871 break;
3872
3873 case DW_AT_description:
3874 default:
3875 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3876 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3877 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3878 case DW_AT_sibling:
3879 break;
3880 }
3881 }
3882 }
3883
3884 if (name && name[0] && got_value)
3885 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003886 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3887 enumerator_clang_type,
3888 decl,
3889 name,
3890 enum_value,
3891 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892 ++enumerators_added;
3893 }
3894 }
3895 }
3896 }
3897 return enumerators_added;
3898}
3899
3900void
3901SymbolFileDWARF::ParseChildArrayInfo
3902(
3903 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003904 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003905 const DWARFDebugInfoEntry *parent_die,
3906 int64_t& first_index,
3907 std::vector<uint64_t>& element_orders,
3908 uint32_t& byte_stride,
3909 uint32_t& bit_stride
3910)
3911{
3912 if (parent_die == NULL)
3913 return;
3914
3915 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003916 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003917 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3918 {
3919 const dw_tag_t tag = die->Tag();
3920 switch (tag)
3921 {
3922 case DW_TAG_enumerator:
3923 {
3924 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003925 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003926 if (num_child_attributes > 0)
3927 {
3928 const char *name = NULL;
3929 bool got_value = false;
3930 int64_t enum_value = 0;
3931
3932 uint32_t i;
3933 for (i=0; i<num_child_attributes; ++i)
3934 {
3935 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3936 DWARFFormValue form_value;
3937 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3938 {
3939 switch (attr)
3940 {
3941 case DW_AT_const_value:
3942 got_value = true;
3943 enum_value = form_value.Unsigned();
3944 break;
3945
3946 case DW_AT_name:
3947 name = form_value.AsCString(&get_debug_str_data());
3948 break;
3949
3950 case DW_AT_description:
3951 default:
3952 case DW_AT_decl_file:
3953 case DW_AT_decl_line:
3954 case DW_AT_decl_column:
3955 case DW_AT_sibling:
3956 break;
3957 }
3958 }
3959 }
3960 }
3961 }
3962 break;
3963
3964 case DW_TAG_subrange_type:
3965 {
3966 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003967 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003968 if (num_child_attributes > 0)
3969 {
3970 const char *name = NULL;
3971 bool got_value = false;
3972 uint64_t byte_size = 0;
3973 int64_t enum_value = 0;
3974 uint64_t num_elements = 0;
3975 uint64_t lower_bound = 0;
3976 uint64_t upper_bound = 0;
3977 uint32_t i;
3978 for (i=0; i<num_child_attributes; ++i)
3979 {
3980 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3981 DWARFFormValue form_value;
3982 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3983 {
3984 switch (attr)
3985 {
3986 case DW_AT_const_value:
3987 got_value = true;
3988 enum_value = form_value.Unsigned();
3989 break;
3990
3991 case DW_AT_name:
3992 name = form_value.AsCString(&get_debug_str_data());
3993 break;
3994
3995 case DW_AT_count:
3996 num_elements = form_value.Unsigned();
3997 break;
3998
3999 case DW_AT_bit_stride:
4000 bit_stride = form_value.Unsigned();
4001 break;
4002
4003 case DW_AT_byte_stride:
4004 byte_stride = form_value.Unsigned();
4005 break;
4006
4007 case DW_AT_byte_size:
4008 byte_size = form_value.Unsigned();
4009 break;
4010
4011 case DW_AT_lower_bound:
4012 lower_bound = form_value.Unsigned();
4013 break;
4014
4015 case DW_AT_upper_bound:
4016 upper_bound = form_value.Unsigned();
4017 break;
4018
4019 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004020 case DW_AT_abstract_origin:
4021 case DW_AT_accessibility:
4022 case DW_AT_allocated:
4023 case DW_AT_associated:
4024 case DW_AT_data_location:
4025 case DW_AT_declaration:
4026 case DW_AT_description:
4027 case DW_AT_sibling:
4028 case DW_AT_threads_scaled:
4029 case DW_AT_type:
4030 case DW_AT_visibility:
4031 break;
4032 }
4033 }
4034 }
4035
4036 if (upper_bound > lower_bound)
4037 num_elements = upper_bound - lower_bound + 1;
4038
4039 if (num_elements > 0)
4040 element_orders.push_back (num_elements);
4041 }
4042 }
4043 break;
4044 }
4045 }
4046}
4047
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004048TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004049SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004050{
4051 TypeSP type_sp;
4052 if (die != NULL)
4053 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004054 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004055 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004056 if (type_ptr == NULL)
4057 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004058 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004059 assert (lldb_cu);
4060 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004061 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004062 }
4063 else if (type_ptr != DIE_IS_BEING_PARSED)
4064 {
4065 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004066 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004067 }
4068
4069 }
4070 return type_sp;
4071}
4072
4073clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004074SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004075{
4076 if (die_offset != DW_INVALID_OFFSET)
4077 {
4078 DWARFCompileUnitSP cu_sp;
4079 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004080 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004081 }
4082 return NULL;
4083}
4084
Sean Callanan72e49402011-08-05 23:43:37 +00004085clang::DeclContext *
4086SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4087{
4088 if (die_offset != DW_INVALID_OFFSET)
4089 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004090 DWARFDebugInfo* debug_info = DebugInfo();
4091 if (debug_info)
4092 {
4093 DWARFCompileUnitSP cu_sp;
4094 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4095 if (die)
4096 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4097 }
Sean Callanan72e49402011-08-05 23:43:37 +00004098 }
4099 return NULL;
4100}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004101
Greg Clayton96d7d742010-11-10 23:42:09 +00004102clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004103SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004104{
Greg Clayton030a2042011-10-14 21:34:45 +00004105 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004106 {
Greg Clayton030a2042011-10-14 21:34:45 +00004107 // See if we already parsed this namespace DIE and associated it with a
4108 // uniqued namespace declaration
4109 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4110 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004111 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004112 else
4113 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004114 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4115 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004116 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
4117 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4118 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004119 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004120 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004121 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004122 GetObjectFile()->GetModule()->LogMessage (log.get(),
4123 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
4124 GetClangASTContext().getASTContext(),
4125 MakeUserID(die->GetOffset()),
4126 namespace_name,
4127 namespace_decl,
4128 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00004129 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004130 else
4131 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004132 GetObjectFile()->GetModule()->LogMessage (log.get(),
4133 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
4134 GetClangASTContext().getASTContext(),
4135 MakeUserID(die->GetOffset()),
4136 namespace_decl,
4137 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00004138 }
Greg Clayton030a2042011-10-14 21:34:45 +00004139 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004140
4141 if (namespace_decl)
4142 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4143 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004144 }
4145 }
4146 return NULL;
4147}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004148
4149clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004150SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004151{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004152 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4153 if (clang_decl_ctx)
4154 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004155 // If this DIE has a specification, or an abstract origin, then trace to those.
4156
Greg Claytoncab36a32011-12-08 05:16:30 +00004157 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004158 if (die_offset != DW_INVALID_OFFSET)
4159 return GetClangDeclContextForDIEOffset (sc, die_offset);
4160
Greg Claytoncab36a32011-12-08 05:16:30 +00004161 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004162 if (die_offset != DW_INVALID_OFFSET)
4163 return GetClangDeclContextForDIEOffset (sc, die_offset);
4164
Greg Clayton3bffb082011-12-10 02:15:28 +00004165 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4166 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004167 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 +00004168 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004169 bool assert_not_being_parsed = true;
4170 ResolveTypeUID (cu, die, assert_not_being_parsed);
4171
Greg Clayton5cf58b92011-10-05 22:22:08 +00004172 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4173
4174 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004175}
4176
4177clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004178SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004179{
Greg Claytonca512b32011-01-14 04:54:56 +00004180 if (m_clang_tu_decl == NULL)
4181 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004182
Greg Clayton2bc22f82011-09-30 03:20:47 +00004183 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004184
4185 if (decl_ctx_die_copy)
4186 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004187
4188 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004189 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004190
Greg Clayton2bc22f82011-09-30 03:20:47 +00004191 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4192 if (pos != m_die_to_decl_ctx.end())
4193 return pos->second;
4194
4195 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004196 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004197 case DW_TAG_compile_unit:
4198 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004199
Greg Clayton2bc22f82011-09-30 03:20:47 +00004200 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004201 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004202 break;
4203
4204 case DW_TAG_structure_type:
4205 case DW_TAG_union_type:
4206 case DW_TAG_class_type:
4207 {
4208 Type* type = ResolveType (cu, decl_ctx_die);
4209 if (type)
4210 {
4211 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4212 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004213 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004214 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4215 if (decl_ctx)
4216 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004217 }
4218 }
Greg Claytonca512b32011-01-14 04:54:56 +00004219 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004220 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004221
Greg Clayton2bc22f82011-09-30 03:20:47 +00004222 default:
4223 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004224 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004225 }
Greg Clayton7a345282010-11-09 23:46:37 +00004226 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004227}
4228
Greg Clayton2bc22f82011-09-30 03:20:47 +00004229
4230const DWARFDebugInfoEntry *
4231SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4232{
4233 if (cu && die)
4234 {
4235 const DWARFDebugInfoEntry * const decl_die = die;
4236
4237 while (die != NULL)
4238 {
4239 // If this is the original DIE that we are searching for a declaration
4240 // for, then don't look in the cache as we don't want our own decl
4241 // context to be our decl context...
4242 if (decl_die != die)
4243 {
4244 switch (die->Tag())
4245 {
4246 case DW_TAG_compile_unit:
4247 case DW_TAG_namespace:
4248 case DW_TAG_structure_type:
4249 case DW_TAG_union_type:
4250 case DW_TAG_class_type:
4251 return die;
4252
4253 default:
4254 break;
4255 }
4256 }
4257
4258 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4259 if (die_offset != DW_INVALID_OFFSET)
4260 {
4261 DWARFCompileUnit *spec_cu = cu;
4262 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4263 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4264 if (spec_die_decl_ctx_die)
4265 return spec_die_decl_ctx_die;
4266 }
4267
4268 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4269 if (die_offset != DW_INVALID_OFFSET)
4270 {
4271 DWARFCompileUnit *abs_cu = cu;
4272 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4273 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4274 if (abs_die_decl_ctx_die)
4275 return abs_die_decl_ctx_die;
4276 }
4277
4278 die = die->GetParent();
4279 }
4280 }
4281 return NULL;
4282}
4283
4284
Greg Clayton901c5ca2011-12-03 04:40:03 +00004285Symbol *
4286SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4287{
4288 Symbol *objc_class_symbol = NULL;
4289 if (m_obj_file)
4290 {
4291 Symtab *symtab = m_obj_file->GetSymtab();
4292 if (symtab)
4293 {
4294 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4295 eSymbolTypeObjCClass,
4296 Symtab::eDebugNo,
4297 Symtab::eVisibilityAny);
4298 }
4299 }
4300 return objc_class_symbol;
4301}
4302
Greg Claytonc7f03b62012-01-12 04:33:28 +00004303// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4304// then we can end up looking through all class types for a complete type and never find
4305// the full definition. We need to know if this attribute is supported, so we determine
4306// this here and cache th result. We also need to worry about the debug map DWARF file
4307// if we are doing darwin DWARF in .o file debugging.
4308bool
4309SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4310{
4311 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4312 {
4313 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4314 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4315 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4316 else
4317 {
4318 DWARFDebugInfo* debug_info = DebugInfo();
4319 const uint32_t num_compile_units = GetNumCompileUnits();
4320 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4321 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004322 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4323 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004324 {
4325 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4326 break;
4327 }
4328 }
4329 }
4330 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && m_debug_map_symfile)
4331 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4332 }
4333 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4334}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004335
4336// This function can be used when a DIE is found that is a forward declaration
4337// DIE and we want to try and find a type that has the complete definition.
4338TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004339SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4340 const ConstString &type_name,
4341 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004342{
4343
4344 TypeSP type_sp;
4345
Greg Claytonc7f03b62012-01-12 04:33:28 +00004346 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004347 return type_sp;
4348
4349 DIEArray die_offsets;
4350
4351 if (m_using_apple_tables)
4352 {
4353 if (m_apple_types_ap.get())
4354 {
4355 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004356 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004357 }
4358 }
4359 else
4360 {
4361 if (!m_indexed)
4362 Index ();
4363
4364 m_type_index.Find (type_name, die_offsets);
4365 }
4366
Greg Clayton901c5ca2011-12-03 04:40:03 +00004367 const size_t num_matches = die_offsets.size();
4368
Greg Clayton901c5ca2011-12-03 04:40:03 +00004369 DWARFCompileUnit* type_cu = NULL;
4370 const DWARFDebugInfoEntry* type_die = NULL;
4371 if (num_matches)
4372 {
4373 DWARFDebugInfo* debug_info = DebugInfo();
4374 for (size_t i=0; i<num_matches; ++i)
4375 {
4376 const dw_offset_t die_offset = die_offsets[i];
4377 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4378
4379 if (type_die)
4380 {
4381 bool try_resolving_type = false;
4382
4383 // Don't try and resolve the DIE we are looking for with the DIE itself!
4384 if (type_die != die)
4385 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004386 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004387 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004388 case DW_TAG_class_type:
4389 case DW_TAG_structure_type:
4390 try_resolving_type = true;
4391 break;
4392 default:
4393 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004394 }
4395 }
4396
4397 if (try_resolving_type)
4398 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004399 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004400 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004401
4402 if (try_resolving_type)
4403 {
4404 Type *resolved_type = ResolveType (type_cu, type_die, false);
4405 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4406 {
4407 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4408 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004409 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton901c5ca2011-12-03 04:40:03 +00004410 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4411 MakeUserID(type_die->GetOffset()),
4412 MakeUserID(type_cu->GetOffset()));
4413
Greg Claytonc7f03b62012-01-12 04:33:28 +00004414 if (die)
4415 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004416 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004417 break;
4418 }
4419 }
4420 }
4421 }
4422 else
4423 {
4424 if (m_using_apple_tables)
4425 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004426 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4427 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004428 }
4429 }
4430
4431 }
4432 }
4433 return type_sp;
4434}
4435
Greg Claytona8022fa2012-04-24 21:22:41 +00004436
Greg Clayton80c26302012-02-05 06:12:47 +00004437//----------------------------------------------------------------------
4438// This function helps to ensure that the declaration contexts match for
4439// two different DIEs. Often times debug information will refer to a
4440// forward declaration of a type (the equivalent of "struct my_struct;".
4441// There will often be a declaration of that type elsewhere that has the
4442// full definition. When we go looking for the full type "my_struct", we
4443// will find one or more matches in the accelerator tables and we will
4444// then need to make sure the type was in the same declaration context
4445// as the original DIE. This function can efficiently compare two DIEs
4446// and will return true when the declaration context matches, and false
4447// when they don't.
4448//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004449bool
4450SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4451 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4452{
Greg Claytona8022fa2012-04-24 21:22:41 +00004453 if (die1 == die2)
4454 return true;
4455
4456#if defined (LLDB_CONFIGURATION_DEBUG)
4457 // You can't and shouldn't call this function with a compile unit from
4458 // two different SymbolFileDWARF instances.
4459 assert (DebugInfo()->ContainsCompileUnit (cu1));
4460 assert (DebugInfo()->ContainsCompileUnit (cu2));
4461#endif
4462
Greg Clayton890ff562012-02-02 05:48:16 +00004463 DWARFDIECollection decl_ctx_1;
4464 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004465 //The declaration DIE stack is a stack of the declaration context
4466 // DIEs all the way back to the compile unit. If a type "T" is
4467 // declared inside a class "B", and class "B" is declared inside
4468 // a class "A" and class "A" is in a namespace "lldb", and the
4469 // namespace is in a compile unit, there will be a stack of DIEs:
4470 //
4471 // [0] DW_TAG_class_type for "B"
4472 // [1] DW_TAG_class_type for "A"
4473 // [2] DW_TAG_namespace for "lldb"
4474 // [3] DW_TAG_compile_unit for the source file.
4475 //
4476 // We grab both contexts and make sure that everything matches
4477 // all the way back to the compiler unit.
4478
4479 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004480 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004481 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004482 // Make sure the context arrays have the same size, otherwise
4483 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004484 const size_t count1 = decl_ctx_1.Size();
4485 const size_t count2 = decl_ctx_2.Size();
4486 if (count1 != count2)
4487 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004488
4489 // Make sure the DW_TAG values match all the way back up the the
4490 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004491 const DWARFDebugInfoEntry *decl_ctx_die1;
4492 const DWARFDebugInfoEntry *decl_ctx_die2;
4493 size_t i;
4494 for (i=0; i<count1; i++)
4495 {
4496 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4497 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4498 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4499 return false;
4500 }
Greg Clayton890ff562012-02-02 05:48:16 +00004501#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004502
4503 // Make sure the top item in the decl context die array is always
4504 // DW_TAG_compile_unit. If it isn't then something went wrong in
4505 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004506 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004507
Greg Clayton890ff562012-02-02 05:48:16 +00004508#endif
4509 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004510 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004511 for (i=0; i<count1 - 1; i++)
4512 {
4513 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4514 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4515 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004516 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004517 // If the string was from a DW_FORM_strp, then the pointer will often
4518 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004519 if (name1 == name2)
4520 continue;
4521
4522 // Name pointers are not equal, so only compare the strings
4523 // if both are not NULL.
4524 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004525 {
Greg Clayton5569e642012-02-06 01:44:54 +00004526 // If the strings don't compare, we are done...
4527 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004528 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004529 }
4530 else
4531 {
4532 // One name was NULL while the other wasn't
4533 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004534 }
4535 }
Greg Clayton80c26302012-02-05 06:12:47 +00004536 // We made it through all of the checks and the declaration contexts
4537 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004538 return true;
4539}
Greg Clayton220a0072011-12-09 08:48:30 +00004540
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004541// This function can be used when a DIE is found that is a forward declaration
4542// DIE and we want to try and find a type that has the complete definition.
Greg Claytona8022fa2012-04-24 21:22:41 +00004543// "cu" and "die" must be from this SymbolFileDWARF
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004544TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004545SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
Greg Clayton7f995132011-10-04 22:41:51 +00004546 const DWARFDebugInfoEntry *die,
4547 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004548{
4549 TypeSP type_sp;
4550
Greg Claytona8022fa2012-04-24 21:22:41 +00004551#if defined (LLDB_CONFIGURATION_DEBUG)
4552 // You can't and shouldn't call this function with a compile unit from
4553 // another SymbolFileDWARF instance.
4554 assert (DebugInfo()->ContainsCompileUnit (cu));
4555#endif
4556
Greg Clayton1a65ae12011-01-25 23:55:37 +00004557 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004558 return type_sp;
4559
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004560 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4561 if (log)
4562 {
4563 std::string qualified_name;
4564 die->GetQualifiedName(this, cu, qualified_name);
4565 GetObjectFile()->GetModule()->LogMessage (log.get(),
4566 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x (%s), name='%s')",
4567 die->GetOffset(),
4568 qualified_name.c_str(),
4569 type_name.GetCString());
4570 }
4571
Greg Clayton7f995132011-10-04 22:41:51 +00004572 DIEArray die_offsets;
4573
Greg Clayton97fbc342011-10-20 22:30:33 +00004574 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004575 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004576 if (m_apple_types_ap.get())
4577 {
Greg Claytond1767f02011-12-08 02:13:16 +00004578 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4579 {
Greg Claytonae920b62012-01-06 00:17:16 +00004580 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004581 }
4582 else
4583 {
4584 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4585 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004586 }
Greg Clayton7f995132011-10-04 22:41:51 +00004587 }
4588 else
4589 {
4590 if (!m_indexed)
4591 Index ();
4592
4593 m_type_index.Find (type_name, die_offsets);
4594 }
Greg Clayton7f995132011-10-04 22:41:51 +00004595
4596 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004597
Greg Clayton934cb052011-12-03 00:27:05 +00004598 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004599
4600 DWARFCompileUnit* type_cu = NULL;
4601 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004602 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004603 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004604 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004605 for (size_t i=0; i<num_matches; ++i)
4606 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004607 const dw_offset_t die_offset = die_offsets[i];
4608 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004609
Greg Clayton95d87902011-11-11 03:16:25 +00004610 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004611 {
Greg Clayton934cb052011-12-03 00:27:05 +00004612 bool try_resolving_type = false;
4613
4614 // Don't try and resolve the DIE we are looking for with the DIE itself!
4615 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004616 {
Greg Clayton934cb052011-12-03 00:27:05 +00004617 const dw_tag_t type_die_tag = type_die->Tag();
4618 // Make sure the tags match
4619 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004620 {
Greg Clayton934cb052011-12-03 00:27:05 +00004621 // The tags match, lets try resolving this type
4622 try_resolving_type = true;
4623 }
4624 else
4625 {
4626 // The tags don't match, but we need to watch our for a
4627 // forward declaration for a struct and ("struct foo")
4628 // ends up being a class ("class foo { ... };") or
4629 // vice versa.
4630 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004631 {
Greg Clayton934cb052011-12-03 00:27:05 +00004632 case DW_TAG_class_type:
4633 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4634 try_resolving_type = (die_tag == DW_TAG_structure_type);
4635 break;
4636 case DW_TAG_structure_type:
4637 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4638 try_resolving_type = (die_tag == DW_TAG_class_type);
4639 break;
4640 default:
4641 // Tags don't match, don't event try to resolve
4642 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004643 break;
4644 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004645 }
4646 }
Greg Clayton934cb052011-12-03 00:27:05 +00004647
4648 if (try_resolving_type)
4649 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004650 if (log)
4651 {
4652 std::string qualified_name;
4653 type_die->GetQualifiedName(this, cu, qualified_name);
4654 GetObjectFile()->GetModule()->LogMessage (log.get(),
4655 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') trying die=0x%8.8x (%s)",
4656 die->GetOffset(),
4657 type_name.GetCString(),
4658 type_die->GetOffset(),
4659 qualified_name.c_str());
4660 }
4661
Greg Clayton890ff562012-02-02 05:48:16 +00004662 // Make sure the decl contexts match all the way up
4663 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004664 {
Greg Clayton890ff562012-02-02 05:48:16 +00004665 Type *resolved_type = ResolveType (type_cu, type_die, false);
4666 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4667 {
4668 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4669 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004670 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton890ff562012-02-02 05:48:16 +00004671 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4672 MakeUserID(type_die->GetOffset()),
4673 MakeUserID(type_cu->GetOffset()));
4674
4675 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004676 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004677 break;
4678 }
Greg Clayton934cb052011-12-03 00:27:05 +00004679 }
4680 }
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004681 else
4682 {
4683 if (log)
4684 {
4685 std::string qualified_name;
4686 type_die->GetQualifiedName(this, cu, qualified_name);
4687 GetObjectFile()->GetModule()->LogMessage (log.get(),
4688 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') ignoring die=0x%8.8x (%s)",
4689 die->GetOffset(),
4690 type_name.GetCString(),
4691 type_die->GetOffset(),
4692 qualified_name.c_str());
4693 }
4694 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004695 }
Greg Clayton95d87902011-11-11 03:16:25 +00004696 else
4697 {
4698 if (m_using_apple_tables)
4699 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004700 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4701 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004702 }
4703 }
4704
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004705 }
4706 }
4707 return type_sp;
4708}
4709
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004710TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004711SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
4712{
4713 TypeSP type_sp;
4714
4715 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
4716 if (dwarf_decl_ctx_count > 0)
4717 {
4718 const ConstString type_name(dwarf_decl_ctx[0].name);
4719 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
4720
4721 if (type_name)
4722 {
4723 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4724 if (log)
4725 {
4726 GetObjectFile()->GetModule()->LogMessage (log.get(),
4727 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
4728 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4729 dwarf_decl_ctx.GetQualifiedName());
4730 }
4731
4732 DIEArray die_offsets;
4733
4734 if (m_using_apple_tables)
4735 {
4736 if (m_apple_types_ap.get())
4737 {
4738 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4739 {
4740 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
4741 }
4742 else
4743 {
4744 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4745 }
4746 }
4747 }
4748 else
4749 {
4750 if (!m_indexed)
4751 Index ();
4752
4753 m_type_index.Find (type_name, die_offsets);
4754 }
4755
4756 const size_t num_matches = die_offsets.size();
4757
4758
4759 DWARFCompileUnit* type_cu = NULL;
4760 const DWARFDebugInfoEntry* type_die = NULL;
4761 if (num_matches)
4762 {
4763 DWARFDebugInfo* debug_info = DebugInfo();
4764 for (size_t i=0; i<num_matches; ++i)
4765 {
4766 const dw_offset_t die_offset = die_offsets[i];
4767 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4768
4769 if (type_die)
4770 {
4771 bool try_resolving_type = false;
4772
4773 // Don't try and resolve the DIE we are looking for with the DIE itself!
4774 const dw_tag_t type_tag = type_die->Tag();
4775 // Make sure the tags match
4776 if (type_tag == tag)
4777 {
4778 // The tags match, lets try resolving this type
4779 try_resolving_type = true;
4780 }
4781 else
4782 {
4783 // The tags don't match, but we need to watch our for a
4784 // forward declaration for a struct and ("struct foo")
4785 // ends up being a class ("class foo { ... };") or
4786 // vice versa.
4787 switch (type_tag)
4788 {
4789 case DW_TAG_class_type:
4790 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4791 try_resolving_type = (tag == DW_TAG_structure_type);
4792 break;
4793 case DW_TAG_structure_type:
4794 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4795 try_resolving_type = (tag == DW_TAG_class_type);
4796 break;
4797 default:
4798 // Tags don't match, don't event try to resolve
4799 // using this type whose name matches....
4800 break;
4801 }
4802 }
4803
4804 if (try_resolving_type)
4805 {
4806 DWARFDeclContext type_dwarf_decl_ctx;
4807 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
4808
4809 if (log)
4810 {
4811 GetObjectFile()->GetModule()->LogMessage (log.get(),
4812 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
4813 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4814 dwarf_decl_ctx.GetQualifiedName(),
4815 type_die->GetOffset(),
4816 type_dwarf_decl_ctx.GetQualifiedName());
4817 }
4818
4819 // Make sure the decl contexts match all the way up
4820 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
4821 {
4822 Type *resolved_type = ResolveType (type_cu, type_die, false);
4823 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4824 {
4825 type_sp = resolved_type->shared_from_this();
4826 break;
4827 }
4828 }
4829 }
4830 else
4831 {
4832 if (log)
4833 {
4834 std::string qualified_name;
4835 type_die->GetQualifiedName(this, type_cu, qualified_name);
4836 GetObjectFile()->GetModule()->LogMessage (log.get(),
4837 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
4838 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4839 dwarf_decl_ctx.GetQualifiedName(),
4840 type_die->GetOffset(),
4841 qualified_name.c_str());
4842 }
4843 }
4844 }
4845 else
4846 {
4847 if (m_using_apple_tables)
4848 {
4849 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4850 die_offset, type_name.GetCString());
4851 }
4852 }
4853
4854 }
4855 }
4856 }
4857 }
4858 return type_sp;
4859}
4860
Greg Clayton4116e932012-05-15 02:33:01 +00004861bool
4862SymbolFileDWARF::CopyUniqueClassMethodTypes (Type *class_type,
4863 DWARFCompileUnit* src_cu,
4864 const DWARFDebugInfoEntry *src_class_die,
4865 DWARFCompileUnit* dst_cu,
4866 const DWARFDebugInfoEntry *dst_class_die)
4867{
4868 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
4869 return false;
4870 if (src_class_die->Tag() != dst_class_die->Tag())
4871 return false;
4872
4873 // We need to complete the class type so we can get all of the method types
4874 // parsed so we can then unique those types to their equivalent counterparts
4875 // in "dst_cu" and "dst_class_die"
4876 class_type->GetClangFullType();
4877
4878 const DWARFDebugInfoEntry *src_die;
4879 const DWARFDebugInfoEntry *dst_die;
4880 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
4881 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
4882 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
4883 {
4884 if (src_die->Tag() == DW_TAG_subprogram)
4885 {
4886 const char *src_name = src_die->GetMangledName (this, src_cu);
4887 if (src_name)
4888 src_name_to_die.Append(ConstString(src_name).GetCString(), src_die);
4889 }
4890 }
4891 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
4892 {
4893 if (dst_die->Tag() == DW_TAG_subprogram)
4894 {
4895 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
4896 if (dst_name)
4897 dst_name_to_die.Append(ConstString(dst_name).GetCString(), dst_die);
4898 }
4899 }
4900 const uint32_t src_size = src_name_to_die.GetSize ();
4901 const uint32_t dst_size = dst_name_to_die.GetSize ();
4902 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
4903
4904 if (src_size && dst_size)
4905 {
4906 uint32_t idx;
4907 for (idx = 0; idx < src_size; ++idx)
4908 {
4909 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
4910 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
4911
4912 if (src_die->Tag() != dst_die->Tag())
4913 {
4914 if (log)
4915 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)",
4916 src_class_die->GetOffset(),
4917 dst_class_die->GetOffset(),
4918 src_die->GetOffset(),
4919 DW_TAG_value_to_name(src_die->Tag()),
4920 dst_die->GetOffset(),
4921 DW_TAG_value_to_name(src_die->Tag()));
4922 return false;
4923 }
4924
4925 const char *src_name = src_die->GetMangledName (this, src_cu);
4926 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
4927
4928 // Make sure the names match
4929 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
4930 continue;
4931
4932 if (log)
4933 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)",
4934 src_class_die->GetOffset(),
4935 dst_class_die->GetOffset(),
4936 src_die->GetOffset(),
4937 src_name,
4938 dst_die->GetOffset(),
4939 dst_name);
4940
4941 return false;
4942 }
4943
4944 for (idx = 0; idx < src_size; ++idx)
4945 {
4946 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
4947 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
4948
4949 clang::DeclContext *src_decl_ctx = m_die_to_decl_ctx[src_die];
4950 if (src_decl_ctx)
4951 {
4952 if (log)
4953 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x\n", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
4954 LinkDeclContextToDIE (src_decl_ctx, dst_die);
4955 }
4956 else
4957 {
4958 if (log)
4959 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found\n", src_die->GetOffset(), dst_die->GetOffset());
4960 }
4961
4962 Type *src_child_type = m_die_to_type[src_die];
4963 if (src_child_type)
4964 {
4965 if (log)
4966 log->Printf ("uniquing type %p (uid=0x%llx) from 0x%8.8x for 0x%8.8x\n", src_child_type, src_child_type->GetID(), src_die->GetOffset(), dst_die->GetOffset());
4967 m_die_to_type[dst_die] = src_child_type;
4968 }
4969 else
4970 {
4971 if (log)
4972 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found\n", src_die->GetOffset(), dst_die->GetOffset());
4973 }
4974 }
4975 return true;
4976 }
4977 else
4978 {
4979 if (log)
4980 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x has %u methods and 0x%8.8x has %u",
4981 src_class_die->GetOffset(),
4982 dst_class_die->GetOffset(),
4983 src_die->GetOffset(),
4984 src_size,
4985 dst_die->GetOffset(),
4986 dst_size);
4987 }
4988 return false;
4989}
Greg Claytona8022fa2012-04-24 21:22:41 +00004990
4991TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00004992SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004993{
4994 TypeSP type_sp;
4995
Greg Clayton1be10fc2010-09-29 01:12:09 +00004996 if (type_is_new_ptr)
4997 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00004998
4999#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5000 static DIEStack g_die_stack;
5001 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
5002#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00005003
Sean Callananc7fbf732010-08-06 00:32:49 +00005004 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005005 if (die != NULL)
5006 {
Greg Clayton21f2a492011-10-06 00:09:08 +00005007 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005008 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00005009 {
5010 const DWARFDebugInfoEntry *context_die;
5011 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
5012
Greg Clayton1fba87112012-02-09 20:03:49 +00005013 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 +00005014 die->GetOffset(),
5015 context,
5016 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00005017 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00005018 die->GetName(this, dwarf_cu));
5019
5020#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5021 scoped_die_logger.Push (dwarf_cu, die);
5022 g_die_stack.LogDIEs(log.get(), this);
5023#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00005024 }
Greg Clayton3bffb082011-12-10 02:15:28 +00005025//
5026// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
5027// if (log && dwarf_cu)
5028// {
5029// StreamString s;
5030// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005031// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00005032//
5033// }
Jim Ingham16746d12011-08-25 23:21:43 +00005034
Greg Clayton594e5ed2010-09-27 21:07:38 +00005035 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005036 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00005037 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005038 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005039 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005040 if (type_is_new_ptr)
5041 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005042
Greg Clayton594e5ed2010-09-27 21:07:38 +00005043 const dw_tag_t tag = die->Tag();
5044
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005045 bool is_forward_declaration = false;
5046 DWARFDebugInfoEntry::Attributes attributes;
5047 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00005048 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00005049 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
5050 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00005051 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00005052 Declaration decl;
5053
Greg Clayton4957bf62010-09-30 21:49:03 +00005054 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005055 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005056
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005057 dw_attr_t attr;
5058
5059 switch (tag)
5060 {
5061 case DW_TAG_base_type:
5062 case DW_TAG_pointer_type:
5063 case DW_TAG_reference_type:
5064 case DW_TAG_typedef:
5065 case DW_TAG_const_type:
5066 case DW_TAG_restrict_type:
5067 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00005068 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005069 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005070 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005071 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005072
Greg Claytond88d7592010-09-15 08:33:30 +00005073 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005074 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005075 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
5076
5077 if (num_attributes > 0)
5078 {
5079 uint32_t i;
5080 for (i=0; i<num_attributes; ++i)
5081 {
5082 attr = attributes.AttributeAtIndex(i);
5083 DWARFFormValue form_value;
5084 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5085 {
5086 switch (attr)
5087 {
5088 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5089 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5090 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5091 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00005092
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005093 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00005094 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
5095 // include the "&"...
5096 if (tag == DW_TAG_reference_type)
5097 {
5098 if (strchr (type_name_cstr, '&') == NULL)
5099 type_name_cstr = NULL;
5100 }
5101 if (type_name_cstr)
5102 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005103 break;
Greg Clayton36909642011-03-15 04:38:20 +00005104 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005105 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
5106 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
5107 default:
5108 case DW_AT_sibling:
5109 break;
5110 }
5111 }
5112 }
5113 }
5114
Greg Clayton81c22f62011-10-19 18:09:39 +00005115 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\") type => 0x%8.8x\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
Greg Claytonc93237c2010-10-01 20:48:32 +00005116
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005117 switch (tag)
5118 {
5119 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00005120 break;
5121
Greg Claytond4436412011-10-28 21:00:00 +00005122 case DW_TAG_unspecified_type:
5123 if (strcmp(type_name_cstr, "nullptr_t") == 0)
5124 {
5125 resolve_state = Type::eResolveStateFull;
5126 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
5127 break;
5128 }
5129 // Fall through to base type below in case we can handle the type there...
5130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005131 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00005132 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005133 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
5134 encoding,
5135 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005136 break;
5137
Greg Clayton526e5af2010-11-13 03:52:47 +00005138 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
5139 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
5140 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
5141 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
5142 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
5143 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005144 }
5145
Greg Claytonc7f03b62012-01-12 04:33:28 +00005146 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005147 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005148 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
5149 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005150 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005151 static ConstString g_objc_type_name_id("id");
5152 static ConstString g_objc_type_name_Class("Class");
5153 static ConstString g_objc_type_name_selector("SEL");
5154
5155 if (type_name_const_str == g_objc_type_name_id)
5156 {
5157 if (log)
5158 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
5159 die->GetOffset(),
5160 DW_TAG_value_to_name(die->Tag()),
5161 die->GetName(this, dwarf_cu));
5162 clang_type = ast.GetBuiltInType_objc_id();
5163 encoding_data_type = Type::eEncodingIsUID;
5164 encoding_uid = LLDB_INVALID_UID;
5165 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00005166
Greg Claytonc7f03b62012-01-12 04:33:28 +00005167 }
5168 else if (type_name_const_str == g_objc_type_name_Class)
5169 {
5170 if (log)
5171 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
5172 die->GetOffset(),
5173 DW_TAG_value_to_name(die->Tag()),
5174 die->GetName(this, dwarf_cu));
5175 clang_type = ast.GetBuiltInType_objc_Class();
5176 encoding_data_type = Type::eEncodingIsUID;
5177 encoding_uid = LLDB_INVALID_UID;
5178 resolve_state = Type::eResolveStateFull;
5179 }
5180 else if (type_name_const_str == g_objc_type_name_selector)
5181 {
5182 if (log)
5183 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
5184 die->GetOffset(),
5185 DW_TAG_value_to_name(die->Tag()),
5186 die->GetName(this, dwarf_cu));
5187 clang_type = ast.GetBuiltInType_objc_selector();
5188 encoding_data_type = Type::eEncodingIsUID;
5189 encoding_uid = LLDB_INVALID_UID;
5190 resolve_state = Type::eResolveStateFull;
5191 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005192 }
5193 }
5194
Greg Clayton81c22f62011-10-19 18:09:39 +00005195 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005196 this,
5197 type_name_const_str,
5198 byte_size,
5199 NULL,
5200 encoding_uid,
5201 encoding_data_type,
5202 &decl,
5203 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005204 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005205
Greg Clayton594e5ed2010-09-27 21:07:38 +00005206 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005207
5208// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
5209// if (encoding_type != NULL)
5210// {
5211// if (encoding_type != DIE_IS_BEING_PARSED)
5212// type_sp->SetEncodingType(encoding_type);
5213// else
5214// m_indirect_fixups.push_back(type_sp.get());
5215// }
5216 }
5217 break;
5218
5219 case DW_TAG_structure_type:
5220 case DW_TAG_union_type:
5221 case DW_TAG_class_type:
5222 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005223 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005224 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005225
Greg Clayton9e409562010-07-28 02:04:09 +00005226 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00005227 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005228 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00005229 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005230 if (num_attributes > 0)
5231 {
5232 uint32_t i;
5233 for (i=0; i<num_attributes; ++i)
5234 {
5235 attr = attributes.AttributeAtIndex(i);
5236 DWARFFormValue form_value;
5237 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5238 {
5239 switch (attr)
5240 {
Greg Clayton9e409562010-07-28 02:04:09 +00005241 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00005242 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
5243 {
5244 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
5245 // point to the compile unit file, so we clear this invalid value
5246 // so that we can still unique types efficiently.
5247 decl.SetFile(FileSpec ("<invalid>", false));
5248 }
5249 else
5250 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00005251 break;
5252
5253 case DW_AT_decl_line:
5254 decl.SetLine(form_value.Unsigned());
5255 break;
5256
5257 case DW_AT_decl_column:
5258 decl.SetColumn(form_value.Unsigned());
5259 break;
5260
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005261 case DW_AT_name:
5262 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005263 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005264 break;
Greg Clayton9e409562010-07-28 02:04:09 +00005265
5266 case DW_AT_byte_size:
5267 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00005268 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00005269 break;
5270
5271 case DW_AT_accessibility:
5272 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
5273 break;
5274
5275 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00005276 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00005277 break;
5278
5279 case DW_AT_APPLE_runtime_class:
5280 class_language = (LanguageType)form_value.Signed();
5281 break;
5282
Greg Clayton18774842011-11-29 23:40:34 +00005283 case DW_AT_APPLE_objc_complete_type:
5284 is_complete_objc_class = form_value.Signed();
5285 break;
5286
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005287 case DW_AT_allocated:
5288 case DW_AT_associated:
5289 case DW_AT_data_location:
5290 case DW_AT_description:
5291 case DW_AT_start_scope:
5292 case DW_AT_visibility:
5293 default:
5294 case DW_AT_sibling:
5295 break;
5296 }
5297 }
5298 }
5299 }
5300
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005301 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00005302
Greg Clayton123edca2012-03-02 00:07:15 +00005303 // Only try and unique the type if it has a name.
5304 if (type_name_const_str &&
5305 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00005306 this,
5307 dwarf_cu,
5308 die,
5309 decl,
5310 byte_size_valid ? byte_size : -1,
5311 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005312 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005313 // We have already parsed this type or from another
5314 // compile unit. GCC loves to use the "one definition
5315 // rule" which can result in multiple definitions
5316 // of the same class over and over in each compile
5317 // unit.
5318 type_sp = unique_ast_entry.m_type_sp;
5319 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00005320 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005321 m_die_to_type[die] = type_sp.get();
5322 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005323 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005324 }
5325
Greg Clayton81c22f62011-10-19 18:09:39 +00005326 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005327
5328 int tag_decl_kind = -1;
5329 AccessType default_accessibility = eAccessNone;
5330 if (tag == DW_TAG_structure_type)
5331 {
5332 tag_decl_kind = clang::TTK_Struct;
5333 default_accessibility = eAccessPublic;
5334 }
5335 else if (tag == DW_TAG_union_type)
5336 {
5337 tag_decl_kind = clang::TTK_Union;
5338 default_accessibility = eAccessPublic;
5339 }
5340 else if (tag == DW_TAG_class_type)
5341 {
5342 tag_decl_kind = clang::TTK_Class;
5343 default_accessibility = eAccessPrivate;
5344 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00005345
5346 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
5347 die->HasChildren() == false &&
5348 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
5349 {
5350 // Work around an issue with clang at the moment where
5351 // forward declarations for objective C classes are emitted
5352 // as:
5353 // DW_TAG_structure_type [2]
5354 // DW_AT_name( "ForwardObjcClass" )
5355 // DW_AT_byte_size( 0x00 )
5356 // DW_AT_decl_file( "..." )
5357 // DW_AT_decl_line( 1 )
5358 //
5359 // Note that there is no DW_AT_declaration and there are
5360 // no children, and the byte size is zero.
5361 is_forward_declaration = true;
5362 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005363
Sean Callanan7457f8d2012-04-25 01:03:57 +00005364 if (class_language == eLanguageTypeObjC ||
5365 class_language == eLanguageTypeObjC_plus_plus)
Greg Clayton18774842011-11-29 23:40:34 +00005366 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005367 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00005368 {
5369 // We have a valid eSymbolTypeObjCClass class symbol whose
5370 // name matches the current objective C class that we
5371 // are trying to find and this DIE isn't the complete
5372 // definition (we checked is_complete_objc_class above and
5373 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00005374 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005375
Greg Clayton901c5ca2011-12-03 04:40:03 +00005376 if (!type_sp && m_debug_map_symfile)
5377 {
5378 // We weren't able to find a full declaration in
5379 // this DWARF, see if we have a declaration anywhere
5380 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00005381 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005382 }
5383
5384 if (type_sp)
5385 {
5386 if (log)
5387 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005388 GetObjectFile()->GetModule()->LogMessage (log.get(),
5389 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
5390 this,
5391 die->GetOffset(),
5392 DW_TAG_value_to_name(tag),
5393 type_name_cstr,
5394 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00005395 }
5396
5397 // We found a real definition for this type elsewhere
5398 // so lets use it and cache the fact that we found
5399 // a complete type for this die
5400 m_die_to_type[die] = type_sp.get();
5401 return type_sp;
5402 }
5403 }
5404 }
5405
5406
5407 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005408 {
5409 // We have a forward declaration to a type and we need
5410 // to try and find a full declaration. We look in the
5411 // current type index just in case we have a forward
5412 // declaration followed by an actual declarations in the
5413 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00005414 if (log)
5415 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005416 GetObjectFile()->GetModule()->LogMessage (log.get(),
5417 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
5418 this,
5419 die->GetOffset(),
5420 DW_TAG_value_to_name(tag),
5421 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00005422 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005423
Greg Claytona8022fa2012-04-24 21:22:41 +00005424 DWARFDeclContext die_decl_ctx;
5425 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx);
5426
5427 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
5428 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005429
5430 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00005431 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005432 // We weren't able to find a full declaration in
5433 // this DWARF, see if we have a declaration anywhere
5434 // else...
Greg Claytona8022fa2012-04-24 21:22:41 +00005435 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton4272cc72011-02-02 02:24:04 +00005436 }
5437
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005438 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00005439 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00005440 if (log)
5441 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005442 GetObjectFile()->GetModule()->LogMessage (log.get(),
5443 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
5444 this,
5445 die->GetOffset(),
5446 DW_TAG_value_to_name(tag),
5447 type_name_cstr,
5448 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00005449 }
5450
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005451 // We found a real definition for this type elsewhere
5452 // so lets use it and cache the fact that we found
5453 // a complete type for this die
5454 m_die_to_type[die] = type_sp.get();
5455 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005456 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005457 }
5458 assert (tag_decl_kind != -1);
5459 bool clang_type_was_created = false;
5460 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5461 if (clang_type == NULL)
5462 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00005463 const DWARFDebugInfoEntry *decl_ctx_die;
5464
5465 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00005466 if (accessibility == eAccessNone && decl_ctx)
5467 {
5468 // Check the decl context that contains this class/struct/union.
5469 // If it is a class we must give it an accessability.
5470 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
5471 if (DeclKindIsCXXClass (containing_decl_kind))
5472 accessibility = default_accessibility;
5473 }
5474
Greg Claytonf0705c82011-10-22 03:33:13 +00005475 if (type_name_cstr && strchr (type_name_cstr, '<'))
5476 {
5477 ClangASTContext::TemplateParameterInfos template_param_infos;
5478 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
5479 {
5480 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00005481 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00005482 type_name_cstr,
5483 tag_decl_kind,
5484 template_param_infos);
5485
5486 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
5487 class_template_decl,
5488 tag_decl_kind,
5489 template_param_infos);
5490 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
5491 clang_type_was_created = true;
Sean Callanan60217122012-04-13 00:10:03 +00005492
5493 GetClangASTContext().SetMetadata((uintptr_t)class_template_decl, MakeUserID(die->GetOffset()));
5494 GetClangASTContext().SetMetadata((uintptr_t)class_specialization_decl, MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005495 }
5496 }
5497
5498 if (!clang_type_was_created)
5499 {
5500 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00005501 clang_type = ast.CreateRecordType (decl_ctx,
5502 accessibility,
5503 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00005504 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00005505 class_language,
Sean Callananad880762012-04-18 01:06:17 +00005506 MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005507 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005508 }
5509
5510 // Store a forward declaration to this class type in case any
5511 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00005512 // types for function prototypes.
5513 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00005514 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005515 this,
5516 type_name_const_str,
5517 byte_size,
5518 NULL,
5519 LLDB_INVALID_UID,
5520 Type::eEncodingIsUID,
5521 &decl,
5522 clang_type,
5523 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00005524
5525 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005526
5527
5528 // Add our type to the unique type map so we don't
5529 // end up creating many copies of the same type over
5530 // and over in the ASTContext for our module
5531 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00005532 unique_ast_entry.m_symfile = this;
5533 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005534 unique_ast_entry.m_die = die;
5535 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00005536 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00005537 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
5538 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005539
Sean Callanan12014a02011-12-08 23:45:45 +00005540 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00005541 {
5542 // Always start the definition for a class type so that
5543 // if the class has child classes or types that require
5544 // the class to be created for use as their decl contexts
5545 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00005546 if (die->HasChildren() == false)
5547 {
5548 // No children for this struct/union/class, lets finish it
5549 ast.StartTagDeclarationDefinition (clang_type);
5550 ast.CompleteTagDeclarationDefinition (clang_type);
5551 }
5552 else if (clang_type_was_created)
5553 {
Greg Clayton219cf312012-03-30 00:51:13 +00005554 // Start the definition if the class is not objective C since
5555 // the underlying decls respond to isCompleteDefinition(). Objective
5556 // C decls dont' respond to isCompleteDefinition() so we can't
5557 // start the declaration definition right away. For C++ classs/union/structs
5558 // we want to start the definition in case the class is needed as the
5559 // declaration context for a contained class or type without the need
5560 // to complete that type..
5561
Sean Callanan7457f8d2012-04-25 01:03:57 +00005562 if (class_language != eLanguageTypeObjC &&
5563 class_language != eLanguageTypeObjC_plus_plus)
Greg Clayton219cf312012-03-30 00:51:13 +00005564 ast.StartTagDeclarationDefinition (clang_type);
5565
Sean Callanan12014a02011-12-08 23:45:45 +00005566 // Leave this as a forward declaration until we need
5567 // to know the details of the type. lldb_private::Type
5568 // will automatically call the SymbolFile virtual function
5569 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
5570 // When the definition needs to be defined.
5571 m_forward_decl_die_to_clang_type[die] = clang_type;
5572 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
5573 ClangASTContext::SetHasExternalStorage (clang_type, true);
5574 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005575 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005576
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005577 }
5578 break;
5579
5580 case DW_TAG_enumeration_type:
5581 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005582 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005583 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005584
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005585 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005586
Greg Claytond88d7592010-09-15 08:33:30 +00005587 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005588 if (num_attributes > 0)
5589 {
5590 uint32_t i;
5591
5592 for (i=0; i<num_attributes; ++i)
5593 {
5594 attr = attributes.AttributeAtIndex(i);
5595 DWARFFormValue form_value;
5596 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5597 {
5598 switch (attr)
5599 {
Greg Clayton7a345282010-11-09 23:46:37 +00005600 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5601 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5602 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005603 case DW_AT_name:
5604 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005605 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005606 break;
Greg Clayton7a345282010-11-09 23:46:37 +00005607 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005608 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00005609 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5610 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005611 case DW_AT_allocated:
5612 case DW_AT_associated:
5613 case DW_AT_bit_stride:
5614 case DW_AT_byte_stride:
5615 case DW_AT_data_location:
5616 case DW_AT_description:
5617 case DW_AT_start_scope:
5618 case DW_AT_visibility:
5619 case DW_AT_specification:
5620 case DW_AT_abstract_origin:
5621 case DW_AT_sibling:
5622 break;
5623 }
5624 }
5625 }
5626
Greg Clayton81c22f62011-10-19 18:09:39 +00005627 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00005628
Greg Clayton1be10fc2010-09-29 01:12:09 +00005629 clang_type_t enumerator_clang_type = NULL;
5630 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5631 if (clang_type == NULL)
5632 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005633 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
5634 DW_ATE_signed,
5635 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00005636 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00005637 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00005638 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005639 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00005640 }
5641 else
5642 {
5643 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
5644 assert (enumerator_clang_type != NULL);
5645 }
5646
Greg Claytona2721472011-06-25 00:44:06 +00005647 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
5648
Greg Clayton81c22f62011-10-19 18:09:39 +00005649 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005650 this,
5651 type_name_const_str,
5652 byte_size,
5653 NULL,
5654 encoding_uid,
5655 Type::eEncodingIsUID,
5656 &decl,
5657 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005658 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005659
Greg Clayton6beaaa62011-01-17 03:46:26 +00005660 ast.StartTagDeclarationDefinition (clang_type);
5661 if (die->HasChildren())
5662 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005663 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
5664 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005665 }
5666 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005667 }
5668 }
5669 break;
5670
Jim Inghamb0be4422010-08-12 01:20:14 +00005671 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005672 case DW_TAG_subprogram:
5673 case DW_TAG_subroutine_type:
5674 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005675 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005676 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005677
5678 const char *mangled = NULL;
5679 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00005680 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005681 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00005682 bool is_static = false;
5683 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00005684 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00005685 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00005686 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
5687 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00005688
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005689 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00005690 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005691
5692
Greg Claytond88d7592010-09-15 08:33:30 +00005693 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005694 if (num_attributes > 0)
5695 {
5696 uint32_t i;
5697 for (i=0; i<num_attributes; ++i)
5698 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005699 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005700 DWARFFormValue form_value;
5701 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5702 {
5703 switch (attr)
5704 {
5705 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5706 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5707 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5708 case DW_AT_name:
5709 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005710 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005711 break;
5712
5713 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
5714 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005715 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005716 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00005717 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
5718 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00005719 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00005720 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5721
Greg Claytonf51de672010-10-01 02:31:07 +00005722
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005723 case DW_AT_external:
5724 if (form_value.Unsigned())
5725 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00005726 if (storage == clang::SC_None)
5727 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005728 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00005729 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005730 }
5731 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005732
Greg Clayton72da3972011-08-16 18:40:23 +00005733 case DW_AT_specification:
5734 specification_die_offset = form_value.Reference(dwarf_cu);
5735 break;
5736
5737 case DW_AT_abstract_origin:
5738 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
5739 break;
5740
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005741 case DW_AT_allocated:
5742 case DW_AT_associated:
5743 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005744 case DW_AT_calling_convention:
5745 case DW_AT_data_location:
5746 case DW_AT_elemental:
5747 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005748 case DW_AT_frame_base:
5749 case DW_AT_high_pc:
5750 case DW_AT_low_pc:
5751 case DW_AT_object_pointer:
5752 case DW_AT_prototyped:
5753 case DW_AT_pure:
5754 case DW_AT_ranges:
5755 case DW_AT_recursive:
5756 case DW_AT_return_addr:
5757 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005758 case DW_AT_start_scope:
5759 case DW_AT_static_link:
5760 case DW_AT_trampoline:
5761 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005762 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005763 case DW_AT_description:
5764 case DW_AT_sibling:
5765 break;
5766 }
5767 }
5768 }
Greg Clayton24739922010-10-13 03:15:28 +00005769 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005770
Greg Clayton81c22f62011-10-19 18:09:39 +00005771 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00005772
Greg Clayton24739922010-10-13 03:15:28 +00005773 clang_type_t return_clang_type = NULL;
5774 Type *func_type = NULL;
5775
5776 if (type_die_offset != DW_INVALID_OFFSET)
5777 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00005778
Greg Clayton24739922010-10-13 03:15:28 +00005779 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00005780 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00005781 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005782 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005783
Greg Claytonf51de672010-10-01 02:31:07 +00005784
Greg Clayton24739922010-10-13 03:15:28 +00005785 std::vector<clang_type_t> function_param_types;
5786 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005787
Greg Clayton24739922010-10-13 03:15:28 +00005788 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00005789
Greg Claytoncb5860a2011-10-13 23:49:28 +00005790 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
5791 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00005792 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
5793
Greg Claytonf0705c82011-10-22 03:33:13 +00005794 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00005795 // Start off static. This will be set to false in ParseChildParameters(...)
5796 // if we find a "this" paramters as the first parameter
5797 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00005798 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005799 ClangASTContext::TemplateParameterInfos template_param_infos;
5800
Greg Clayton24739922010-10-13 03:15:28 +00005801 if (die->HasChildren())
5802 {
Greg Clayton0fffff52010-09-24 05:15:53 +00005803 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005804 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00005805 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005806 dwarf_cu,
5807 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00005808 skip_artificial,
5809 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005810 type_list,
5811 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00005812 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005813 type_quals,
5814 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00005815 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005816
Greg Clayton24739922010-10-13 03:15:28 +00005817 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005818 clang_type = ast.CreateFunctionType (return_clang_type,
5819 &function_param_types[0],
5820 function_param_types.size(),
5821 is_variadic,
5822 type_quals);
5823
Greg Clayton24739922010-10-13 03:15:28 +00005824 if (type_name_cstr)
5825 {
5826 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00005827 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005828 {
Greg Clayton7c810422012-01-18 23:40:49 +00005829 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00005830 ConstString class_name_no_category;
5831 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00005832 {
Greg Claytone42ae842012-01-19 03:24:53 +00005833 // Use the class name with no category if there is one
5834 if (class_name_no_category)
5835 class_name = class_name_no_category;
5836
Greg Clayton24739922010-10-13 03:15:28 +00005837 SymbolContext empty_sc;
5838 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00005839 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00005840 {
Greg Clayton24739922010-10-13 03:15:28 +00005841 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00005842 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00005843
5844 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00005845 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005846 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
5847 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
5848 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00005849 }
Greg Clayton24739922010-10-13 03:15:28 +00005850 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005851
Greg Clayton24739922010-10-13 03:15:28 +00005852 if (class_opaque_type)
5853 {
5854 // If accessibility isn't set to anything valid, assume public for
5855 // now...
5856 if (accessibility == eAccessNone)
5857 accessibility = eAccessPublic;
5858
5859 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005860 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
5861 type_name_cstr,
5862 clang_type,
5863 accessibility);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005864 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton24739922010-10-13 03:15:28 +00005865 type_handled = objc_method_decl != NULL;
Sean Callanan60217122012-04-13 00:10:03 +00005866 GetClangASTContext().SetMetadata((uintptr_t)objc_method_decl, MakeUserID(die->GetOffset()));
Greg Clayton24739922010-10-13 03:15:28 +00005867 }
5868 }
Greg Clayton5113dc82011-08-12 06:47:54 +00005869 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00005870 {
5871 // Look at the parent of this DIE and see if is is
5872 // a class or struct and see if this is actually a
5873 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00005874 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00005875 if (class_type)
5876 {
Greg Clayton4116e932012-05-15 02:33:01 +00005877 if (class_type->GetID() != MakeUserID(decl_ctx_die->GetOffset()))
5878 {
5879 // We uniqued the parent class of this function to another class
5880 // so we now need to associate all dies under "decl_ctx_die" to
5881 // DIEs in the DIE for "class_type"...
5882 DWARFCompileUnitSP class_type_cu_sp;
5883 const DWARFDebugInfoEntry *class_type_die = DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
5884 if (class_type_die)
5885 {
5886 if (CopyUniqueClassMethodTypes (class_type,
5887 class_type_cu_sp.get(),
5888 class_type_die,
5889 dwarf_cu,
5890 decl_ctx_die))
5891 {
5892 type_ptr = m_die_to_type[die];
5893 if (type_ptr)
5894 {
5895 type_sp = type_ptr->shared_from_this();
5896 break;
5897 }
5898 }
5899 }
5900 }
5901
Greg Clayton72da3972011-08-16 18:40:23 +00005902 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00005903 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005904 // We have a specification which we are going to base our function
5905 // prototype off of, so we need this type to be completed so that the
5906 // m_die_to_decl_ctx for the method in the specification has a valid
5907 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005908 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00005909 // If we have a specification, then the function type should have been
5910 // made with the specification and not with this die.
5911 DWARFCompileUnitSP spec_cu_sp;
5912 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005913 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005914 if (spec_clang_decl_ctx)
5915 {
5916 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
5917 }
5918 else
Jim Inghamc1663042011-09-29 22:12:35 +00005919 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005920 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
5921 MakeUserID(die->GetOffset()),
5922 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005923 }
Greg Clayton72da3972011-08-16 18:40:23 +00005924 type_handled = true;
5925 }
5926 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
5927 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005928 // We have a specification which we are going to base our function
5929 // prototype off of, so we need this type to be completed so that the
5930 // m_die_to_decl_ctx for the method in the abstract origin has a valid
5931 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005932 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00005933
Greg Clayton72da3972011-08-16 18:40:23 +00005934 DWARFCompileUnitSP abs_cu_sp;
5935 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00005936 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005937 if (abs_clang_decl_ctx)
5938 {
5939 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
5940 }
5941 else
Jim Inghamc1663042011-09-29 22:12:35 +00005942 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005943 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
5944 MakeUserID(die->GetOffset()),
5945 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005946 }
Greg Clayton72da3972011-08-16 18:40:23 +00005947 type_handled = true;
5948 }
5949 else
5950 {
5951 clang_type_t class_opaque_type = class_type->GetClangForwardType();
5952 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00005953 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005954 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00005955 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005956 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
5957 // in the DWARF for C++ methods... Default to public for now...
5958 if (accessibility == eAccessNone)
5959 accessibility = eAccessPublic;
5960
5961 if (!is_static && !die->HasChildren())
5962 {
5963 // We have a C++ member function with no children (this pointer!)
5964 // and clang will get mad if we try and make a function that isn't
5965 // well formed in the DWARF, so we will just skip it...
5966 type_handled = true;
5967 }
5968 else
5969 {
5970 clang::CXXMethodDecl *cxx_method_decl;
5971 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00005972 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8llx from %s/%s",
Greg Clayton20568dd2011-10-13 23:13:20 +00005973 type_name_cstr,
5974 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00005975 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00005976 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
5977 m_obj_file->GetFileSpec().GetFilename().GetCString());
5978
Sean Callananc1b732d2011-11-01 18:07:13 +00005979 const bool is_attr_used = false;
5980
Greg Clayton20568dd2011-10-13 23:13:20 +00005981 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
5982 type_name_cstr,
5983 clang_type,
5984 accessibility,
5985 is_virtual,
5986 is_static,
5987 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00005988 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00005989 is_attr_used,
5990 is_artificial);
Greg Clayton20568dd2011-10-13 23:13:20 +00005991 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
5992
Greg Claytonf49e65a2011-11-10 18:31:53 +00005993 Host::SetCrashDescription (NULL);
5994
Greg Clayton20568dd2011-10-13 23:13:20 +00005995 type_handled = cxx_method_decl != NULL;
Sean Callanan60217122012-04-13 00:10:03 +00005996
5997 GetClangASTContext().SetMetadata((uintptr_t)cxx_method_decl, MakeUserID(die->GetOffset()));
Greg Clayton20568dd2011-10-13 23:13:20 +00005998 }
Greg Clayton72da3972011-08-16 18:40:23 +00005999 }
6000 else
6001 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006002 // We were asked to parse the type for a method in a class, yet the
6003 // class hasn't been asked to complete itself through the
6004 // clang::ExternalASTSource protocol, so we need to just have the
6005 // class complete itself and do things the right way, then our
6006 // DIE should then have an entry in the m_die_to_type map. First
6007 // we need to modify the m_die_to_type so it doesn't think we are
6008 // trying to parse this DIE anymore...
6009 m_die_to_type[die] = NULL;
6010
6011 // Now we get the full type to force our class type to complete itself
6012 // using the clang::ExternalASTSource protocol which will parse all
6013 // base classes and all methods (including the method for this DIE).
6014 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006015
Greg Clayton20568dd2011-10-13 23:13:20 +00006016 // The type for this DIE should have been filled in the function call above
6017 type_ptr = m_die_to_type[die];
6018 if (type_ptr)
6019 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006020 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00006021 break;
6022 }
Sean Callanan02eee4d2012-04-12 23:10:00 +00006023
6024 // FIXME This is fixing some even uglier behavior but we really need to
6025 // uniq the methods of each class as well as the class itself.
6026 // <rdar://problem/11240464>
6027 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00006028 }
Greg Clayton931180e2011-01-27 06:44:37 +00006029 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006030 }
6031 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006032 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006033 }
Greg Clayton24739922010-10-13 03:15:28 +00006034
6035 if (!type_handled)
6036 {
6037 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00006038 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
6039 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006040 clang_type,
6041 storage,
6042 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006043
6044// if (template_param_infos.GetSize() > 0)
6045// {
6046// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
6047// function_decl,
6048// type_name_cstr,
6049// template_param_infos);
6050//
6051// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
6052// func_template_decl,
6053// template_param_infos);
6054// }
Greg Clayton24739922010-10-13 03:15:28 +00006055 // Add the decl to our DIE to decl context map
6056 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00006057 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00006058 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006059 ast.SetFunctionParameters (function_decl,
6060 &function_param_decls.front(),
6061 function_param_decls.size());
Sean Callanan60217122012-04-13 00:10:03 +00006062
6063 GetClangASTContext().SetMetadata((uintptr_t)function_decl, MakeUserID(die->GetOffset()));
Greg Clayton24739922010-10-13 03:15:28 +00006064 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006065 }
Greg Clayton81c22f62011-10-19 18:09:39 +00006066 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006067 this,
6068 type_name_const_str,
6069 0,
6070 NULL,
6071 LLDB_INVALID_UID,
6072 Type::eEncodingIsUID,
6073 &decl,
6074 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006075 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00006076 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006077 }
6078 break;
6079
6080 case DW_TAG_array_type:
6081 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006082 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006083 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006084
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006085 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006086 int64_t first_index = 0;
6087 uint32_t byte_stride = 0;
6088 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00006089 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006090
6091 if (num_attributes > 0)
6092 {
6093 uint32_t i;
6094 for (i=0; i<num_attributes; ++i)
6095 {
6096 attr = attributes.AttributeAtIndex(i);
6097 DWARFFormValue form_value;
6098 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6099 {
6100 switch (attr)
6101 {
6102 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6103 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6104 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6105 case DW_AT_name:
6106 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006107 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006108 break;
6109
6110 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00006111 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006112 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
6113 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00006114 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00006115 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006116 case DW_AT_allocated:
6117 case DW_AT_associated:
6118 case DW_AT_data_location:
6119 case DW_AT_description:
6120 case DW_AT_ordering:
6121 case DW_AT_start_scope:
6122 case DW_AT_visibility:
6123 case DW_AT_specification:
6124 case DW_AT_abstract_origin:
6125 case DW_AT_sibling:
6126 break;
6127 }
6128 }
6129 }
6130
Greg Clayton81c22f62011-10-19 18:09:39 +00006131 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006132
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006133 Type *element_type = ResolveTypeUID(type_die_offset);
6134
6135 if (element_type)
6136 {
6137 std::vector<uint64_t> element_orders;
6138 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00006139 // We have an array that claims to have no members, lets give it at least one member...
6140 if (element_orders.empty())
6141 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006142 if (byte_stride == 0 && bit_stride == 0)
6143 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00006144 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006145 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
6146 uint64_t num_elements = 0;
6147 std::vector<uint64_t>::const_reverse_iterator pos;
6148 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
6149 for (pos = element_orders.rbegin(); pos != end; ++pos)
6150 {
6151 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006152 clang_type = ast.CreateArrayType (array_element_type,
6153 num_elements,
6154 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006155 array_element_type = clang_type;
6156 array_element_bit_stride = array_element_bit_stride * num_elements;
6157 }
6158 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00006159 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006160 this,
6161 empty_name,
6162 array_element_bit_stride / 8,
6163 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00006164 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006165 Type::eEncodingIsUID,
6166 &decl,
6167 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006168 Type::eResolveStateFull));
6169 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006170 }
6171 }
6172 }
6173 break;
6174
Greg Clayton9b81a312010-06-12 01:20:30 +00006175 case DW_TAG_ptr_to_member_type:
6176 {
6177 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
6178 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
6179
Greg Claytond88d7592010-09-15 08:33:30 +00006180 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00006181
6182 if (num_attributes > 0) {
6183 uint32_t i;
6184 for (i=0; i<num_attributes; ++i)
6185 {
6186 attr = attributes.AttributeAtIndex(i);
6187 DWARFFormValue form_value;
6188 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6189 {
6190 switch (attr)
6191 {
6192 case DW_AT_type:
6193 type_die_offset = form_value.Reference(dwarf_cu); break;
6194 case DW_AT_containing_type:
6195 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
6196 }
6197 }
6198 }
6199
6200 Type *pointee_type = ResolveTypeUID(type_die_offset);
6201 Type *class_type = ResolveTypeUID(containing_type_die_offset);
6202
Greg Clayton526e5af2010-11-13 03:52:47 +00006203 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
6204 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00006205
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006206 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
6207 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00006208
Greg Clayton526e5af2010-11-13 03:52:47 +00006209 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
6210 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00006211
Greg Clayton81c22f62011-10-19 18:09:39 +00006212 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006213 this,
6214 type_name_const_str,
6215 byte_size,
6216 NULL,
6217 LLDB_INVALID_UID,
6218 Type::eEncodingIsUID,
6219 NULL,
6220 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006221 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00006222 }
6223
6224 break;
6225 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006226 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00006227 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006228 break;
6229 }
6230
6231 if (type_sp.get())
6232 {
6233 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6234 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6235
6236 SymbolContextScope * symbol_context_scope = NULL;
6237 if (sc_parent_tag == DW_TAG_compile_unit)
6238 {
6239 symbol_context_scope = sc.comp_unit;
6240 }
6241 else if (sc.function != NULL)
6242 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006243 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006244 if (symbol_context_scope == NULL)
6245 symbol_context_scope = sc.function;
6246 }
6247
6248 if (symbol_context_scope != NULL)
6249 {
6250 type_sp->SetSymbolContextScope(symbol_context_scope);
6251 }
6252
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006253 // We are ready to put this type into the uniqued list up at the module level
6254 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00006255
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006256 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006257 }
6258 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00006259 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006260 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006261 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006262 }
6263 }
6264 return type_sp;
6265}
6266
6267size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00006268SymbolFileDWARF::ParseTypes
6269(
6270 const SymbolContext& sc,
6271 DWARFCompileUnit* dwarf_cu,
6272 const DWARFDebugInfoEntry *die,
6273 bool parse_siblings,
6274 bool parse_children
6275)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006276{
6277 size_t types_added = 0;
6278 while (die != NULL)
6279 {
6280 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00006281 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006282 {
6283 if (type_is_new)
6284 ++types_added;
6285 }
6286
6287 if (parse_children && die->HasChildren())
6288 {
6289 if (die->Tag() == DW_TAG_subprogram)
6290 {
6291 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00006292 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006293 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
6294 }
6295 else
6296 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
6297 }
6298
6299 if (parse_siblings)
6300 die = die->GetSibling();
6301 else
6302 die = NULL;
6303 }
6304 return types_added;
6305}
6306
6307
6308size_t
6309SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
6310{
6311 assert(sc.comp_unit && sc.function);
6312 size_t functions_added = 0;
6313 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
6314 if (dwarf_cu)
6315 {
6316 dw_offset_t function_die_offset = sc.function->GetID();
6317 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
6318 if (function_die)
6319 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00006320 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006321 }
6322 }
6323
6324 return functions_added;
6325}
6326
6327
6328size_t
6329SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
6330{
6331 // At least a compile unit must be valid
6332 assert(sc.comp_unit);
6333 size_t types_added = 0;
6334 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
6335 if (dwarf_cu)
6336 {
6337 if (sc.function)
6338 {
6339 dw_offset_t function_die_offset = sc.function->GetID();
6340 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
6341 if (func_die && func_die->HasChildren())
6342 {
6343 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
6344 }
6345 }
6346 else
6347 {
6348 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
6349 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
6350 {
6351 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
6352 }
6353 }
6354 }
6355
6356 return types_added;
6357}
6358
6359size_t
6360SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
6361{
6362 if (sc.comp_unit != NULL)
6363 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00006364 DWARFDebugInfo* info = DebugInfo();
6365 if (info == NULL)
6366 return 0;
6367
6368 uint32_t cu_idx = UINT32_MAX;
6369 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006370
6371 if (dwarf_cu == NULL)
6372 return 0;
6373
6374 if (sc.function)
6375 {
6376 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00006377
6378 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006379 if (func_lo_pc != DW_INVALID_ADDRESS)
6380 {
6381 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00006382
Greg Claytone38a5ed2012-01-05 03:57:59 +00006383 // Let all blocks know they have parse all their variables
6384 sc.function->GetBlock (false).SetDidParseVariables (true, true);
6385 return num_variables;
6386 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006387 }
6388 else if (sc.comp_unit)
6389 {
6390 uint32_t vars_added = 0;
6391 VariableListSP variables (sc.comp_unit->GetVariableList(false));
6392
6393 if (variables.get() == NULL)
6394 {
6395 variables.reset(new VariableList());
6396 sc.comp_unit->SetVariableList(variables);
6397
Greg Claytond4a2b372011-09-12 23:21:58 +00006398 DWARFCompileUnit* match_dwarf_cu = NULL;
6399 const DWARFDebugInfoEntry* die = NULL;
6400 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00006401 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00006402 {
Greg Clayton97fbc342011-10-20 22:30:33 +00006403 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00006404 {
6405 DWARFMappedHash::DIEInfoArray hash_data_array;
6406 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
6407 dwarf_cu->GetNextCompileUnitOffset(),
6408 hash_data_array))
6409 {
6410 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
6411 }
6412 }
Greg Clayton7f995132011-10-04 22:41:51 +00006413 }
6414 else
6415 {
6416 // Index if we already haven't to make sure the compile units
6417 // get indexed and make their global DIE index list
6418 if (!m_indexed)
6419 Index ();
6420
6421 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
6422 dwarf_cu->GetNextCompileUnitOffset(),
6423 die_offsets);
6424 }
6425
6426 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00006427 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006428 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006429 DWARFDebugInfo* debug_info = DebugInfo();
6430 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006431 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006432 const dw_offset_t die_offset = die_offsets[i];
6433 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00006434 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00006435 {
Greg Clayton95d87902011-11-11 03:16:25 +00006436 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
6437 if (var_sp)
6438 {
6439 variables->AddVariableIfUnique (var_sp);
6440 ++vars_added;
6441 }
Greg Claytond4a2b372011-09-12 23:21:58 +00006442 }
Greg Clayton95d87902011-11-11 03:16:25 +00006443 else
6444 {
6445 if (m_using_apple_tables)
6446 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006447 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 +00006448 }
6449 }
6450
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006451 }
6452 }
6453 }
6454 return vars_added;
6455 }
6456 }
6457 return 0;
6458}
6459
6460
6461VariableSP
6462SymbolFileDWARF::ParseVariableDIE
6463(
6464 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006465 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006466 const DWARFDebugInfoEntry *die,
6467 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006468)
6469{
6470
Greg Clayton83c5cd92010-11-14 22:13:40 +00006471 VariableSP var_sp (m_die_to_variable_sp[die]);
6472 if (var_sp)
6473 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006474
6475 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00006476
6477 if ((tag == DW_TAG_variable) ||
6478 (tag == DW_TAG_constant) ||
6479 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006480 {
Greg Clayton7f995132011-10-04 22:41:51 +00006481 DWARFDebugInfoEntry::Attributes attributes;
6482 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
6483 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006484 {
Greg Clayton7f995132011-10-04 22:41:51 +00006485 const char *name = NULL;
6486 const char *mangled = NULL;
6487 Declaration decl;
6488 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00006489 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00006490 DWARFExpression location;
6491 bool is_external = false;
6492 bool is_artificial = false;
6493 bool location_is_const_value_data = false;
6494 AccessType accessibility = eAccessNone;
6495
6496 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006497 {
Greg Clayton7f995132011-10-04 22:41:51 +00006498 dw_attr_t attr = attributes.AttributeAtIndex(i);
6499 DWARFFormValue form_value;
6500 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006501 {
Greg Clayton7f995132011-10-04 22:41:51 +00006502 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006503 {
Greg Clayton7f995132011-10-04 22:41:51 +00006504 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6505 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6506 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6507 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
6508 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00006509 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00006510 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
6511 case DW_AT_const_value:
6512 location_is_const_value_data = true;
6513 // Fall through...
6514 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006515 {
Greg Clayton7f995132011-10-04 22:41:51 +00006516 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006517 {
Greg Clayton7f995132011-10-04 22:41:51 +00006518 const DataExtractor& debug_info_data = get_debug_info_data();
6519
6520 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
6521 uint32_t block_length = form_value.Unsigned();
6522 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
6523 }
6524 else
6525 {
6526 const DataExtractor& debug_loc_data = get_debug_loc_data();
6527 const dw_offset_t debug_loc_offset = form_value.Unsigned();
6528
6529 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
6530 if (loc_list_length > 0)
6531 {
6532 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
6533 assert (func_low_pc != LLDB_INVALID_ADDRESS);
6534 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
6535 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006536 }
6537 }
Greg Clayton7f995132011-10-04 22:41:51 +00006538 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006539
Greg Clayton7f995132011-10-04 22:41:51 +00006540 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
6541 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
6542 case DW_AT_declaration:
6543 case DW_AT_description:
6544 case DW_AT_endianity:
6545 case DW_AT_segment:
6546 case DW_AT_start_scope:
6547 case DW_AT_visibility:
6548 default:
6549 case DW_AT_abstract_origin:
6550 case DW_AT_sibling:
6551 case DW_AT_specification:
6552 break;
6553 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006554 }
6555 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006556
Greg Clayton7f995132011-10-04 22:41:51 +00006557 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006558 {
Greg Clayton7f995132011-10-04 22:41:51 +00006559 ValueType scope = eValueTypeInvalid;
6560
6561 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6562 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006563 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00006564
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006565 // DWARF doesn't specify if a DW_TAG_variable is a local, global
6566 // or static variable, so we have to do a little digging by
6567 // looking at the location of a varaible to see if it contains
6568 // a DW_OP_addr opcode _somewhere_ in the definition. I say
6569 // somewhere because clang likes to combine small global variables
6570 // into the same symbol and have locations like:
6571 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
6572 // So if we don't have a DW_TAG_formal_parameter, we can look at
6573 // the location to see if it contains a DW_OP_addr opcode, and
6574 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00006575 if (tag == DW_TAG_formal_parameter)
6576 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00006577 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006578 {
Greg Clayton96c09682012-01-04 22:56:43 +00006579 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00006580 // Check if the location has a DW_OP_addr with any address value...
6581 addr_t location_has_op_addr = false;
6582 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00006583 {
6584 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
6585 if (op_error)
6586 {
6587 StreamString strm;
6588 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006589 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 +00006590 }
6591 }
Greg Claytond1767f02011-12-08 02:13:16 +00006592
6593 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006594 {
Greg Claytond1767f02011-12-08 02:13:16 +00006595 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006596 {
Greg Claytond1767f02011-12-08 02:13:16 +00006597 scope = eValueTypeVariableGlobal;
6598
6599 if (m_debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006600 {
Greg Claytond1767f02011-12-08 02:13:16 +00006601 // When leaving the DWARF in the .o files on darwin,
6602 // when we have a global variable that wasn't initialized,
6603 // the .o file might not have allocated a virtual
6604 // address for the global variable. In this case it will
6605 // have created a symbol for the global variable
6606 // that is undefined and external and the value will
6607 // be the byte size of the variable. When we do the
6608 // address map in SymbolFileDWARFDebugMap we rely on
6609 // having an address, we need to do some magic here
6610 // so we can get the correct address for our global
6611 // variable. The address for all of these entries
6612 // will be zero, and there will be an undefined symbol
6613 // in this object file, and the executable will have
6614 // a matching symbol with a good address. So here we
6615 // dig up the correct address and replace it in the
6616 // location for the variable, and set the variable's
6617 // symbol context scope to be that of the main executable
6618 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00006619 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006620 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006621
Greg Claytond1767f02011-12-08 02:13:16 +00006622 // we have a possible uninitialized extern global
6623 Symtab *symtab = m_obj_file->GetSymtab();
6624 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006625 {
Greg Claytond1767f02011-12-08 02:13:16 +00006626 ConstString const_name(name);
6627 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
6628 eSymbolTypeUndefined,
6629 Symtab::eDebugNo,
6630 Symtab::eVisibilityExtern);
6631
6632 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006633 {
Greg Claytond1767f02011-12-08 02:13:16 +00006634 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
6635 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006636 {
Greg Claytond1767f02011-12-08 02:13:16 +00006637 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
6638 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
6639 eSymbolTypeData,
6640 Symtab::eDebugYes,
6641 Symtab::eVisibilityExtern);
6642 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006643 {
Greg Claytone7612132012-03-07 21:03:09 +00006644 if (defined_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006645 {
Greg Claytone7612132012-03-07 21:03:09 +00006646 const addr_t defined_addr = defined_symbol->GetAddress().GetFileAddress();
Greg Claytond1767f02011-12-08 02:13:16 +00006647 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006648 {
Greg Claytond1767f02011-12-08 02:13:16 +00006649 if (location.Update_DW_OP_addr (defined_addr))
6650 {
6651 symbol_context_scope = defined_symbol;
6652 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006653 }
6654 }
6655 }
6656 }
6657 }
6658 }
6659 }
6660 }
6661 }
Greg Claytond1767f02011-12-08 02:13:16 +00006662 else
6663 {
6664 scope = eValueTypeVariableStatic;
6665 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006666 }
6667 else
Greg Claytond1767f02011-12-08 02:13:16 +00006668 {
6669 scope = eValueTypeVariableLocal;
6670 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006671 }
Greg Clayton7f995132011-10-04 22:41:51 +00006672
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006673 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00006674 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006675 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00006676 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006677 case DW_TAG_subprogram:
6678 case DW_TAG_inlined_subroutine:
6679 case DW_TAG_lexical_block:
6680 if (sc.function)
6681 {
6682 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
6683 if (symbol_context_scope == NULL)
6684 symbol_context_scope = sc.function;
6685 }
6686 break;
6687
6688 default:
6689 symbol_context_scope = sc.comp_unit;
6690 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00006691 }
Greg Clayton7f995132011-10-04 22:41:51 +00006692 }
6693
Greg Clayton5cf58b92011-10-05 22:22:08 +00006694 if (symbol_context_scope)
6695 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006696 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
6697 name,
6698 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00006699 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00006700 scope,
6701 symbol_context_scope,
6702 &decl,
6703 location,
6704 is_external,
6705 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00006706
6707 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
6708 }
6709 else
6710 {
6711 // Not ready to parse this variable yet. It might be a global
6712 // or static variable that is in a function scope and the function
6713 // in the symbol context wasn't filled in yet
6714 return var_sp;
6715 }
Greg Clayton7f995132011-10-04 22:41:51 +00006716 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006717 }
Greg Clayton7f995132011-10-04 22:41:51 +00006718 // Cache var_sp even if NULL (the variable was just a specification or
6719 // was missing vital information to be able to be displayed in the debugger
6720 // (missing location due to optimization, etc)) so we don't re-parse
6721 // this DIE over and over later...
6722 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006723 }
6724 return var_sp;
6725}
6726
Greg Claytonc662ec82011-06-17 22:10:16 +00006727
6728const DWARFDebugInfoEntry *
6729SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
6730 dw_offset_t spec_block_die_offset,
6731 DWARFCompileUnit **result_die_cu_handle)
6732{
6733 // Give the concrete function die specified by "func_die_offset", find the
6734 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6735 // to "spec_block_die_offset"
6736 DWARFDebugInfo* info = DebugInfo();
6737
6738 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
6739 if (die)
6740 {
6741 assert (*result_die_cu_handle);
6742 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
6743 }
6744 return NULL;
6745}
6746
6747
6748const DWARFDebugInfoEntry *
6749SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
6750 const DWARFDebugInfoEntry *die,
6751 dw_offset_t spec_block_die_offset,
6752 DWARFCompileUnit **result_die_cu_handle)
6753{
6754 if (die)
6755 {
6756 switch (die->Tag())
6757 {
6758 case DW_TAG_subprogram:
6759 case DW_TAG_inlined_subroutine:
6760 case DW_TAG_lexical_block:
6761 {
6762 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
6763 {
6764 *result_die_cu_handle = dwarf_cu;
6765 return die;
6766 }
6767
6768 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
6769 {
6770 *result_die_cu_handle = dwarf_cu;
6771 return die;
6772 }
6773 }
6774 break;
6775 }
6776
6777 // Give the concrete function die specified by "func_die_offset", find the
6778 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6779 // to "spec_block_die_offset"
6780 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
6781 {
6782 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
6783 child_die,
6784 spec_block_die_offset,
6785 result_die_cu_handle);
6786 if (result_die)
6787 return result_die;
6788 }
6789 }
6790
6791 *result_die_cu_handle = NULL;
6792 return NULL;
6793}
6794
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006795size_t
6796SymbolFileDWARF::ParseVariables
6797(
6798 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006799 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006800 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006801 const DWARFDebugInfoEntry *orig_die,
6802 bool parse_siblings,
6803 bool parse_children,
6804 VariableList* cc_variable_list
6805)
6806{
6807 if (orig_die == NULL)
6808 return 0;
6809
Greg Claytonc662ec82011-06-17 22:10:16 +00006810 VariableListSP variable_list_sp;
6811
6812 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006813 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00006814 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006815 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006816 dw_tag_t tag = die->Tag();
6817
6818 // Check to see if we have already parsed this variable or constant?
6819 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006820 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006821 if (cc_variable_list)
6822 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006823 }
6824 else
6825 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006826 // We haven't already parsed it, lets do that now.
6827 if ((tag == DW_TAG_variable) ||
6828 (tag == DW_TAG_constant) ||
6829 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006830 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006831 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00006832 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006833 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
6834 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6835 switch (parent_tag)
6836 {
6837 case DW_TAG_compile_unit:
6838 if (sc.comp_unit != NULL)
6839 {
6840 variable_list_sp = sc.comp_unit->GetVariableList(false);
6841 if (variable_list_sp.get() == NULL)
6842 {
6843 variable_list_sp.reset(new VariableList());
6844 sc.comp_unit->SetVariableList(variable_list_sp);
6845 }
6846 }
6847 else
6848 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006849 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
6850 MakeUserID(sc_parent_die->GetOffset()),
6851 DW_TAG_value_to_name (parent_tag),
6852 MakeUserID(orig_die->GetOffset()),
6853 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006854 }
6855 break;
6856
6857 case DW_TAG_subprogram:
6858 case DW_TAG_inlined_subroutine:
6859 case DW_TAG_lexical_block:
6860 if (sc.function != NULL)
6861 {
6862 // Check to see if we already have parsed the variables for the given scope
6863
Greg Clayton81c22f62011-10-19 18:09:39 +00006864 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006865 if (block == NULL)
6866 {
6867 // This must be a specification or abstract origin with
6868 // a concrete block couterpart in the current function. We need
6869 // to find the concrete block so we can correctly add the
6870 // variable to it
6871 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
6872 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
6873 sc_parent_die->GetOffset(),
6874 &concrete_block_die_cu);
6875 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00006876 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006877 }
6878
6879 if (block != NULL)
6880 {
6881 const bool can_create = false;
6882 variable_list_sp = block->GetBlockVariableList (can_create);
6883 if (variable_list_sp.get() == NULL)
6884 {
6885 variable_list_sp.reset(new VariableList());
6886 block->SetVariableList(variable_list_sp);
6887 }
6888 }
6889 }
6890 break;
6891
6892 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00006893 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
6894 MakeUserID(orig_die->GetOffset()),
6895 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006896 break;
6897 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00006898 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006899
6900 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006901 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00006902 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
6903 if (var_sp)
6904 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006905 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00006906 if (cc_variable_list)
6907 cc_variable_list->AddVariableIfUnique (var_sp);
6908 ++vars_added;
6909 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006910 }
6911 }
6912 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006913
6914 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
6915
6916 if (!skip_children && parse_children && die->HasChildren())
6917 {
6918 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
6919 }
6920
6921 if (parse_siblings)
6922 die = die->GetSibling();
6923 else
6924 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006925 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006926 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006927}
6928
6929//------------------------------------------------------------------
6930// PluginInterface protocol
6931//------------------------------------------------------------------
6932const char *
6933SymbolFileDWARF::GetPluginName()
6934{
6935 return "SymbolFileDWARF";
6936}
6937
6938const char *
6939SymbolFileDWARF::GetShortPluginName()
6940{
6941 return GetPluginNameStatic();
6942}
6943
6944uint32_t
6945SymbolFileDWARF::GetPluginVersion()
6946{
6947 return 1;
6948}
6949
6950void
Greg Clayton6beaaa62011-01-17 03:46:26 +00006951SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
6952{
6953 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6954 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6955 if (clang_type)
6956 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6957}
6958
6959void
6960SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
6961{
6962 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6963 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6964 if (clang_type)
6965 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6966}
6967
Greg Claytona2721472011-06-25 00:44:06 +00006968void
Sean Callanancc427fa2011-07-30 02:42:06 +00006969SymbolFileDWARF::DumpIndexes ()
6970{
6971 StreamFile s(stdout, false);
6972
6973 s.Printf ("DWARF index for (%s) '%s/%s':",
6974 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
6975 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
6976 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
6977 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
6978 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
6979 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
6980 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
6981 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
6982 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
6983 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
6984 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
6985}
6986
6987void
6988SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
6989 const char *name,
6990 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00006991{
Sean Callanancc427fa2011-07-30 02:42:06 +00006992 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00006993
6994 if (iter == m_decl_ctx_to_die.end())
6995 return;
6996
Greg Claytoncb5860a2011-10-13 23:49:28 +00006997 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00006998 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006999 const DWARFDebugInfoEntry *context_die = *pos;
7000
7001 if (!results)
7002 return;
7003
7004 DWARFDebugInfo* info = DebugInfo();
7005
7006 DIEArray die_offsets;
7007
7008 DWARFCompileUnit* dwarf_cu = NULL;
7009 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00007010
7011 if (m_using_apple_tables)
7012 {
7013 if (m_apple_types_ap.get())
7014 m_apple_types_ap->FindByName (name, die_offsets);
7015 }
7016 else
7017 {
7018 if (!m_indexed)
7019 Index ();
7020
7021 m_type_index.Find (ConstString(name), die_offsets);
7022 }
7023
Greg Clayton220a0072011-12-09 08:48:30 +00007024 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007025
7026 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00007027 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007028 for (size_t i = 0; i < num_matches; ++i)
7029 {
7030 const dw_offset_t die_offset = die_offsets[i];
7031 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00007032
Greg Claytoncb5860a2011-10-13 23:49:28 +00007033 if (die->GetParent() != context_die)
7034 continue;
7035
7036 Type *matching_type = ResolveType (dwarf_cu, die);
7037
Greg Clayton42ce2f32011-12-12 21:50:19 +00007038 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007039 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
7040
7041 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
7042 {
7043 clang::TagDecl *tag_decl = tag_type->getDecl();
7044 results->push_back(tag_decl);
7045 }
7046 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
7047 {
7048 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
7049 results->push_back(typedef_decl);
7050 }
Greg Claytona2721472011-06-25 00:44:06 +00007051 }
7052 }
7053 }
7054}
7055
7056void
7057SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00007058 const clang::DeclContext *decl_context,
7059 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00007060 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
7061{
Greg Clayton85ae2e12011-10-18 23:36:41 +00007062
7063 switch (decl_context->getDeclKind())
7064 {
7065 case clang::Decl::Namespace:
7066 case clang::Decl::TranslationUnit:
7067 {
7068 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7069 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
7070 }
7071 break;
7072 default:
7073 break;
7074 }
Greg Claytona2721472011-06-25 00:44:06 +00007075}
Greg Claytoncaab74e2012-01-28 00:48:57 +00007076
7077bool
7078SymbolFileDWARF::LayoutRecordType (void *baton,
7079 const clang::RecordDecl *record_decl,
7080 uint64_t &size,
7081 uint64_t &alignment,
7082 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7083 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7084 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7085{
7086 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7087 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
7088}
7089
7090
7091bool
7092SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
7093 uint64_t &bit_size,
7094 uint64_t &alignment,
7095 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7096 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7097 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7098{
7099 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
7100 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
7101 bool success = false;
7102 base_offsets.clear();
7103 vbase_offsets.clear();
7104 if (pos != m_record_decl_to_layout_map.end())
7105 {
7106 bit_size = pos->second.bit_size;
7107 alignment = pos->second.alignment;
7108 field_offsets.swap(pos->second.field_offsets);
7109 m_record_decl_to_layout_map.erase(pos);
7110 success = true;
7111 }
7112 else
7113 {
7114 bit_size = 0;
7115 alignment = 0;
7116 field_offsets.clear();
7117 }
7118
7119 if (log)
7120 GetObjectFile()->GetModule()->LogMessage (log.get(),
7121 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
7122 record_decl,
7123 bit_size,
7124 alignment,
7125 (uint32_t)field_offsets.size(),
7126 (uint32_t)base_offsets.size(),
7127 (uint32_t)vbase_offsets.size(),
7128 success);
7129 return success;
7130}
7131
7132
7133