blob: 96472b8a4846402d68e49f88bfa84ae274afc900 [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"
17#include "clang/Basic/Builtins.h"
18#include "clang/Basic/IdentifierTable.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/TargetInfo.h"
22#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000023#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
Sean Callanancc427fa2011-07-30 02:42:06 +000025#include "llvm/Support/Casting.h"
26
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/PluginManager.h"
29#include "lldb/Core/RegularExpression.h"
30#include "lldb/Core/Scalar.h"
31#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000032#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000033#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Core/Timer.h"
35#include "lldb/Core/Value.h"
36
37#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000038#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Symbol/CompileUnit.h"
40#include "lldb/Symbol/LineTable.h"
41#include "lldb/Symbol/ObjectFile.h"
42#include "lldb/Symbol/SymbolVendor.h"
43#include "lldb/Symbol/VariableList.h"
44
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000045#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000046#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000047
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "DWARFCompileUnit.h"
49#include "DWARFDebugAbbrev.h"
50#include "DWARFDebugAranges.h"
51#include "DWARFDebugInfo.h"
52#include "DWARFDebugInfoEntry.h"
53#include "DWARFDebugLine.h"
54#include "DWARFDebugPubnames.h"
55#include "DWARFDebugRanges.h"
56#include "DWARFDIECollection.h"
57#include "DWARFFormValue.h"
58#include "DWARFLocationList.h"
59#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000060#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
62#include <map>
63
Greg Clayton62742b12010-11-11 01:09:45 +000064//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000065
66#ifdef ENABLE_DEBUG_PRINTF
67#include <stdio.h>
68#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
69#else
70#define DEBUG_PRINTF(fmt, ...)
71#endif
72
Greg Clayton594e5ed2010-09-27 21:07:38 +000073#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
75using namespace lldb;
76using namespace lldb_private;
77
78
Sean Callananc7fbf732010-08-06 00:32:49 +000079static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +000080DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081{
82 switch (dwarf_accessibility)
83 {
Sean Callananc7fbf732010-08-06 00:32:49 +000084 case DW_ACCESS_public: return eAccessPublic;
85 case DW_ACCESS_private: return eAccessPrivate;
86 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +000087 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 }
Sean Callananc7fbf732010-08-06 00:32:49 +000089 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090}
91
92void
93SymbolFileDWARF::Initialize()
94{
95 LogChannelDWARF::Initialize();
96 PluginManager::RegisterPlugin (GetPluginNameStatic(),
97 GetPluginDescriptionStatic(),
98 CreateInstance);
99}
100
101void
102SymbolFileDWARF::Terminate()
103{
104 PluginManager::UnregisterPlugin (CreateInstance);
105 LogChannelDWARF::Initialize();
106}
107
108
109const char *
110SymbolFileDWARF::GetPluginNameStatic()
111{
Greg Claytond4a2b372011-09-12 23:21:58 +0000112 return "dwarf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113}
114
115const char *
116SymbolFileDWARF::GetPluginDescriptionStatic()
117{
118 return "DWARF and DWARF3 debug symbol file reader.";
119}
120
121
122SymbolFile*
123SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
124{
125 return new SymbolFileDWARF(obj_file);
126}
127
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000128TypeList *
129SymbolFileDWARF::GetTypeList ()
130{
131 if (m_debug_map_symfile)
132 return m_debug_map_symfile->GetTypeList();
133 return m_obj_file->GetModule()->GetTypeList();
134
135}
136
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137//----------------------------------------------------------------------
138// Gets the first parent that is a lexical block, function or inlined
139// subroutine, or compile unit.
140//----------------------------------------------------------------------
141static const DWARFDebugInfoEntry *
142GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
143{
144 const DWARFDebugInfoEntry *die;
145 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
146 {
147 dw_tag_t tag = die->Tag();
148
149 switch (tag)
150 {
151 case DW_TAG_compile_unit:
152 case DW_TAG_subprogram:
153 case DW_TAG_inlined_subroutine:
154 case DW_TAG_lexical_block:
155 return die;
156 }
157 }
158 return NULL;
159}
160
161
Greg Clayton450e3f32010-10-12 02:24:53 +0000162SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
163 SymbolFile (objfile),
164 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000165 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000167 m_data_debug_abbrev (),
168 m_data_debug_aranges (),
169 m_data_debug_frame (),
170 m_data_debug_info (),
171 m_data_debug_line (),
172 m_data_debug_loc (),
173 m_data_debug_ranges (),
174 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000175 m_data_apple_names (),
176 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000177 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 m_info(),
180 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000181 m_apple_names_ap (),
182 m_apple_types_ap (),
183 m_apple_namespaces_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000184 m_function_basename_index(),
185 m_function_fullname_index(),
186 m_function_method_index(),
187 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000188 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000189 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000190 m_type_index(),
191 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000192 m_indexed (false),
193 m_is_external_ast_source (false),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000194 m_ranges(),
195 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196{
197}
198
199SymbolFileDWARF::~SymbolFileDWARF()
200{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000201 if (m_is_external_ast_source)
202 m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource ();
203}
204
205static const ConstString &
206GetDWARFMachOSegmentName ()
207{
208 static ConstString g_dwarf_section_name ("__DWARF");
209 return g_dwarf_section_name;
210}
211
Greg Claytone576ab22011-02-15 00:19:15 +0000212UniqueDWARFASTTypeMap &
213SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
214{
215 if (m_debug_map_symfile)
216 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
217 return m_unique_ast_type_map;
218}
219
Greg Clayton6beaaa62011-01-17 03:46:26 +0000220ClangASTContext &
221SymbolFileDWARF::GetClangASTContext ()
222{
223 if (m_debug_map_symfile)
224 return m_debug_map_symfile->GetClangASTContext ();
225
226 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
227 if (!m_is_external_ast_source)
228 {
229 m_is_external_ast_source = true;
230 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
231 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
232 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000233 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000234 this));
235
236 ast.SetExternalSource (ast_source_ap);
237 }
238 return ast;
239}
240
241void
242SymbolFileDWARF::InitializeObject()
243{
244 // Install our external AST source callbacks so we can complete Clang types.
245 Module *module = m_obj_file->GetModule();
246 if (module)
247 {
248 const SectionList *section_list = m_obj_file->GetSectionList();
249
250 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
251
252 // Memory map the DWARF mach-o segment so we have everything mmap'ed
253 // to keep our heap memory usage down.
254 if (section)
255 section->MemoryMapSectionDataFromObjectFile(m_obj_file, m_dwarf_data);
256 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000257 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000258 if (m_data_apple_names.GetByteSize() > 0)
259 {
260 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), true));
261 if (!m_apple_names_ap->IsValid())
262 m_apple_names_ap.reset();
263 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000264 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000265 if (m_data_apple_types.GetByteSize() > 0)
266 {
267 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), false));
268 if (!m_apple_types_ap->IsValid())
269 m_apple_types_ap.reset();
270 }
271
272 get_apple_namespaces_data();
273 if (m_data_apple_namespaces.GetByteSize() > 0)
274 {
275 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), false));
276 if (!m_apple_namespaces_ap->IsValid())
277 m_apple_namespaces_ap.reset();
278 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000279
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280}
281
282bool
283SymbolFileDWARF::SupportedVersion(uint16_t version)
284{
285 return version == 2 || version == 3;
286}
287
288uint32_t
289SymbolFileDWARF::GetAbilities ()
290{
291 uint32_t abilities = 0;
292 if (m_obj_file != NULL)
293 {
294 const Section* section = NULL;
295 const SectionList *section_list = m_obj_file->GetSectionList();
296 if (section_list == NULL)
297 return 0;
298
299 uint64_t debug_abbrev_file_size = 0;
300 uint64_t debug_aranges_file_size = 0;
301 uint64_t debug_frame_file_size = 0;
302 uint64_t debug_info_file_size = 0;
303 uint64_t debug_line_file_size = 0;
304 uint64_t debug_loc_file_size = 0;
305 uint64_t debug_macinfo_file_size = 0;
306 uint64_t debug_pubnames_file_size = 0;
307 uint64_t debug_pubtypes_file_size = 0;
308 uint64_t debug_ranges_file_size = 0;
309 uint64_t debug_str_file_size = 0;
310
Greg Clayton6beaaa62011-01-17 03:46:26 +0000311 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312
313 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000314 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315
Greg Clayton4ceb9982010-07-21 22:54:26 +0000316 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 if (section != NULL)
318 {
319 debug_info_file_size = section->GetByteSize();
320
Greg Clayton4ceb9982010-07-21 22:54:26 +0000321 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 if (section)
323 debug_abbrev_file_size = section->GetByteSize();
324 else
325 m_flags.Set (flagsGotDebugAbbrevData);
326
Greg Clayton4ceb9982010-07-21 22:54:26 +0000327 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 if (section)
329 debug_aranges_file_size = section->GetByteSize();
330 else
331 m_flags.Set (flagsGotDebugArangesData);
332
Greg Clayton4ceb9982010-07-21 22:54:26 +0000333 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 if (section)
335 debug_frame_file_size = section->GetByteSize();
336 else
337 m_flags.Set (flagsGotDebugFrameData);
338
Greg Clayton4ceb9982010-07-21 22:54:26 +0000339 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 if (section)
341 debug_line_file_size = section->GetByteSize();
342 else
343 m_flags.Set (flagsGotDebugLineData);
344
Greg Clayton4ceb9982010-07-21 22:54:26 +0000345 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346 if (section)
347 debug_loc_file_size = section->GetByteSize();
348 else
349 m_flags.Set (flagsGotDebugLocData);
350
Greg Clayton4ceb9982010-07-21 22:54:26 +0000351 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 if (section)
353 debug_macinfo_file_size = section->GetByteSize();
354 else
355 m_flags.Set (flagsGotDebugMacInfoData);
356
Greg Clayton4ceb9982010-07-21 22:54:26 +0000357 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 if (section)
359 debug_pubnames_file_size = section->GetByteSize();
360 else
361 m_flags.Set (flagsGotDebugPubNamesData);
362
Greg Clayton4ceb9982010-07-21 22:54:26 +0000363 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364 if (section)
365 debug_pubtypes_file_size = section->GetByteSize();
366 else
367 m_flags.Set (flagsGotDebugPubTypesData);
368
Greg Clayton4ceb9982010-07-21 22:54:26 +0000369 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 if (section)
371 debug_ranges_file_size = section->GetByteSize();
372 else
373 m_flags.Set (flagsGotDebugRangesData);
374
Greg Clayton4ceb9982010-07-21 22:54:26 +0000375 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 if (section)
377 debug_str_file_size = section->GetByteSize();
378 else
379 m_flags.Set (flagsGotDebugStrData);
380 }
381
382 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
383 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
384
385 if (debug_line_file_size > 0)
386 abilities |= LineTables;
387
388 if (debug_aranges_file_size > 0)
389 abilities |= AddressAcceleratorTable;
390
391 if (debug_pubnames_file_size > 0)
392 abilities |= FunctionAcceleratorTable;
393
394 if (debug_pubtypes_file_size > 0)
395 abilities |= TypeAcceleratorTable;
396
397 if (debug_macinfo_file_size > 0)
398 abilities |= MacroInformation;
399
400 if (debug_frame_file_size > 0)
401 abilities |= CallFrameInformation;
402 }
403 return abilities;
404}
405
406const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000407SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408{
409 if (m_flags.IsClear (got_flag))
410 {
411 m_flags.Set (got_flag);
412 const SectionList *section_list = m_obj_file->GetSectionList();
413 if (section_list)
414 {
Greg Clayton4ceb9982010-07-21 22:54:26 +0000415 Section *section = section_list->FindSectionByType(sect_type, true).get();
416 if (section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 {
418 // See if we memory mapped the DWARF segment?
419 if (m_dwarf_data.GetByteSize())
420 {
421 data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize());
422 }
423 else
424 {
425 if (section->ReadSectionDataFromObjectFile(m_obj_file, data) == 0)
426 data.Clear();
427 }
428 }
429 }
430 }
431 return data;
432}
433
434const DataExtractor&
435SymbolFileDWARF::get_debug_abbrev_data()
436{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000437 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438}
439
440const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000441SymbolFileDWARF::get_debug_aranges_data()
442{
443 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
444}
445
446const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447SymbolFileDWARF::get_debug_frame_data()
448{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000449 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450}
451
452const DataExtractor&
453SymbolFileDWARF::get_debug_info_data()
454{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000455 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456}
457
458const DataExtractor&
459SymbolFileDWARF::get_debug_line_data()
460{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000461 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462}
463
464const DataExtractor&
465SymbolFileDWARF::get_debug_loc_data()
466{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000467 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
469
470const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471SymbolFileDWARF::get_debug_ranges_data()
472{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000473 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474}
475
476const DataExtractor&
477SymbolFileDWARF::get_debug_str_data()
478{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000479 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480}
481
Greg Claytonf9eec202011-09-01 23:16:13 +0000482const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000483SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000484{
Greg Clayton17674402011-09-28 17:06:40 +0000485 return GetCachedSectionData (flagsGotDebugNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000486}
487
488const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000489SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000490{
Greg Clayton17674402011-09-28 17:06:40 +0000491 return GetCachedSectionData (flagsGotDebugTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000492}
493
Greg Clayton7f995132011-10-04 22:41:51 +0000494const DataExtractor&
495SymbolFileDWARF::get_apple_namespaces_data()
496{
497 return GetCachedSectionData (flagsGotDebugTypesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
498}
499
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500
501DWARFDebugAbbrev*
502SymbolFileDWARF::DebugAbbrev()
503{
504 if (m_abbr.get() == NULL)
505 {
506 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
507 if (debug_abbrev_data.GetByteSize() > 0)
508 {
509 m_abbr.reset(new DWARFDebugAbbrev());
510 if (m_abbr.get())
511 m_abbr->Parse(debug_abbrev_data);
512 }
513 }
514 return m_abbr.get();
515}
516
517const DWARFDebugAbbrev*
518SymbolFileDWARF::DebugAbbrev() const
519{
520 return m_abbr.get();
521}
522
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523
524DWARFDebugInfo*
525SymbolFileDWARF::DebugInfo()
526{
527 if (m_info.get() == NULL)
528 {
529 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
530 if (get_debug_info_data().GetByteSize() > 0)
531 {
532 m_info.reset(new DWARFDebugInfo());
533 if (m_info.get())
534 {
535 m_info->SetDwarfData(this);
536 }
537 }
538 }
539 return m_info.get();
540}
541
542const DWARFDebugInfo*
543SymbolFileDWARF::DebugInfo() const
544{
545 return m_info.get();
546}
547
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548DWARFCompileUnit*
549SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
550{
551 DWARFDebugInfo* info = DebugInfo();
552 if (info)
553 return info->GetCompileUnit(cu_uid).get();
554 return NULL;
555}
556
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557
558DWARFDebugRanges*
559SymbolFileDWARF::DebugRanges()
560{
561 if (m_ranges.get() == NULL)
562 {
563 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
564 if (get_debug_ranges_data().GetByteSize() > 0)
565 {
566 m_ranges.reset(new DWARFDebugRanges());
567 if (m_ranges.get())
568 m_ranges->Extract(this);
569 }
570 }
571 return m_ranges.get();
572}
573
574const DWARFDebugRanges*
575SymbolFileDWARF::DebugRanges() const
576{
577 return m_ranges.get();
578}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579
580bool
Greg Clayton96d7d742010-11-10 23:42:09 +0000581SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582{
Greg Clayton96d7d742010-11-10 23:42:09 +0000583 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000585 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586 if (cu_die)
587 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000588 const char * cu_die_name = cu_die->GetName(this, curr_cu);
589 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
Jim Ingham0f35ac22011-08-24 23:34:20 +0000590 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591 if (cu_die_name)
592 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000593 FileSpec cu_file_spec;
594
Greg Clayton7bd65b92011-02-09 23:39:34 +0000595 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000597 // If we have a full path to the compile unit, we don't need to resolve
598 // the file. This can be expensive e.g. when the source files are NFS mounted.
599 cu_file_spec.SetFile (cu_die_name, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 }
601 else
602 {
603 std::string fullpath(cu_comp_dir);
604 if (*fullpath.rbegin() != '/')
605 fullpath += '/';
606 fullpath += cu_die_name;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000607 cu_file_spec.SetFile (fullpath.c_str(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608 }
609
Jim Ingham0f35ac22011-08-24 23:34:20 +0000610 compile_unit_sp.reset(new CompileUnit(m_obj_file->GetModule(), curr_cu, cu_file_spec, curr_cu->GetOffset(), cu_language));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611 if (compile_unit_sp.get())
612 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000613 curr_cu->SetUserData(compile_unit_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 return true;
615 }
616 }
617 }
618 }
619 return false;
620}
621
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622uint32_t
623SymbolFileDWARF::GetNumCompileUnits()
624{
625 DWARFDebugInfo* info = DebugInfo();
626 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 return 0;
629}
630
631CompUnitSP
632SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
633{
634 CompUnitSP comp_unit;
635 DWARFDebugInfo* info = DebugInfo();
636 if (info)
637 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000638 DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx);
639 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 {
641 // Our symbol vendor shouldn't be asking us to add a compile unit that
642 // has already been added to it, which this DWARF plug-in knows as it
643 // stores the lldb compile unit (CompileUnit) pointer in each
644 // DWARFCompileUnit object when it gets added.
Greg Clayton96d7d742010-11-10 23:42:09 +0000645 assert(curr_cu->GetUserData() == NULL);
646 ParseCompileUnit(curr_cu, comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647 }
648 }
649 return comp_unit;
650}
651
652static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000653AddRangesToBlock (Block& block,
654 DWARFDebugRanges::RangeList& ranges,
655 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000657 const size_t num_ranges = ranges.GetSize();
658 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000660 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
661 const addr_t range_base = range.GetRangeBase();
662 assert (range_base >= block_base_addr);
663 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000665 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666}
667
668
669Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000670SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671{
672 DWARFDebugRanges::RangeList func_ranges;
673 const char *name = NULL;
674 const char *mangled = NULL;
675 int decl_file = 0;
676 int decl_line = 0;
677 int decl_column = 0;
678 int call_file = 0;
679 int call_line = 0;
680 int call_column = 0;
681 DWARFExpression frame_base;
682
Greg Claytonc93237c2010-10-01 20:48:32 +0000683 assert (die->Tag() == DW_TAG_subprogram);
684
685 if (die->Tag() != DW_TAG_subprogram)
686 return NULL;
687
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000688 if (die->GetDIENamesAndRanges(this, dwarf_cu, name, mangled, func_ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column, &frame_base))
689 {
690 // Union of all ranges in the function DIE (if the function is discontiguous)
691 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000692 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
693 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
695 {
696 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
697 if (func_range.GetBaseAddress().IsValid())
698 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
699 }
700
701 if (func_range.GetBaseAddress().IsValid())
702 {
703 Mangled func_name;
704 if (mangled)
705 func_name.SetValue(mangled, true);
706 else if (name)
707 func_name.SetValue(name, false);
708
709 FunctionSP func_sp;
710 std::auto_ptr<Declaration> decl_ap;
711 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000712 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
713 decl_line,
714 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000716 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000717 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718
719 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
720
721 func_range.GetBaseAddress().ResolveLinkedAddress();
722
723 func_sp.reset(new Function (sc.comp_unit,
724 die->GetOffset(), // UserID is the DIE offset
725 die->GetOffset(),
726 func_name,
727 func_type,
728 func_range)); // first address range
729
730 if (func_sp.get() != NULL)
731 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000732 if (frame_base.IsValid())
733 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 sc.comp_unit->AddFunction(func_sp);
735 return func_sp.get();
736 }
737 }
738 }
739 return NULL;
740}
741
742size_t
743SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
744{
745 assert (sc.comp_unit);
746 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000747 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 if (dwarf_cu)
749 {
750 DWARFDIECollection function_dies;
751 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
752 size_t func_idx;
753 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
754 {
755 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
756 if (sc.comp_unit->FindFunctionByUID (die->GetOffset()).get() == NULL)
757 {
758 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
759 ++functions_added;
760 }
761 }
762 //FixupTypes();
763 }
764 return functions_added;
765}
766
767bool
768SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
769{
770 assert (sc.comp_unit);
Greg Clayton96d7d742010-11-10 23:42:09 +0000771 DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
772 assert (curr_cu);
773 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774
775 if (cu_die)
776 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000777 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
778 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779
780 // All file indexes in DWARF are one based and a file of index zero is
781 // supposed to be the compile unit itself.
782 support_files.Append (*sc.comp_unit);
783
784 return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
785 }
786 return false;
787}
788
789struct ParseDWARFLineTableCallbackInfo
790{
791 LineTable* line_table;
792 const SectionList *section_list;
793 lldb::addr_t prev_sect_file_base_addr;
794 lldb::addr_t curr_sect_file_base_addr;
795 bool is_oso_for_debug_map;
796 bool prev_in_final_executable;
797 DWARFDebugLine::Row prev_row;
798 SectionSP prev_section_sp;
799 SectionSP curr_section_sp;
800};
801
802//----------------------------------------------------------------------
803// ParseStatementTableCallback
804//----------------------------------------------------------------------
805static void
806ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
807{
808 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
809 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
810 {
811 // Just started parsing the line table
812 }
813 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
814 {
815 // Done parsing line table, nothing to do for the cleanup
816 }
817 else
818 {
819 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
820 // We have a new row, lets append it
821
822 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
823 {
824 info->prev_section_sp = info->curr_section_sp;
825 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
826 // If this is an end sequence entry, then we subtract one from the
827 // address to make sure we get an address that is not the end of
828 // a section.
829 if (state.end_sequence && state.address != 0)
830 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
831 else
832 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
833
834 if (info->curr_section_sp.get())
835 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
836 else
837 info->curr_sect_file_base_addr = 0;
838 }
839 if (info->curr_section_sp.get())
840 {
841 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
842 // Check for the fancy section magic to determine if we
843
844 if (info->is_oso_for_debug_map)
845 {
846 // When this is a debug map object file that contains DWARF
847 // (referenced from an N_OSO debug map nlist entry) we will have
848 // a file address in the file range for our section from the
849 // original .o file, and a load address in the executable that
850 // contains the debug map.
851 //
852 // If the sections for the file range and load range are
853 // different, we have a remapped section for the function and
854 // this address is resolved. If they are the same, then the
855 // function for this address didn't make it into the final
856 // executable.
857 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
858
859 // If we are doing DWARF with debug map, then we need to carefully
860 // add each line table entry as there may be gaps as functions
861 // get moved around or removed.
862 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
863 {
864 if (info->prev_in_final_executable)
865 {
866 bool terminate_previous_entry = false;
867 if (!curr_in_final_executable)
868 {
869 // Check for the case where the previous line entry
870 // in a function made it into the final executable,
871 // yet the current line entry falls in a function
872 // that didn't. The line table used to be contiguous
873 // through this address range but now it isn't. We
874 // need to terminate the previous line entry so
875 // that we can reconstruct the line range correctly
876 // for it and to keep the line table correct.
877 terminate_previous_entry = true;
878 }
879 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
880 {
881 // Check for cases where the line entries used to be
882 // contiguous address ranges, but now they aren't.
883 // This can happen when order files specify the
884 // ordering of the functions.
885 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
886 Section *curr_sect = info->curr_section_sp.get();
887 Section *prev_sect = info->prev_section_sp.get();
888 assert (curr_sect->GetLinkedSection());
889 assert (prev_sect->GetLinkedSection());
890 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
891 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
892 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
893 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
894 if (object_file_addr_delta != linked_file_addr_delta)
895 terminate_previous_entry = true;
896 }
897
898 if (terminate_previous_entry)
899 {
900 line_table->InsertLineEntry (info->prev_section_sp,
901 state.address - info->prev_sect_file_base_addr,
902 info->prev_row.line,
903 info->prev_row.column,
904 info->prev_row.file,
905 false, // is_stmt
906 false, // basic_block
907 false, // state.prologue_end
908 false, // state.epilogue_begin
909 true); // end_sequence);
910 }
911 }
912 }
913
914 if (curr_in_final_executable)
915 {
916 line_table->InsertLineEntry (info->curr_section_sp,
917 curr_line_section_offset,
918 state.line,
919 state.column,
920 state.file,
921 state.is_stmt,
922 state.basic_block,
923 state.prologue_end,
924 state.epilogue_begin,
925 state.end_sequence);
926 info->prev_section_sp = info->curr_section_sp;
927 }
928 else
929 {
930 // If the current address didn't make it into the final
931 // executable, the current section will be the __text
932 // segment in the .o file, so we need to clear this so
933 // we can catch the next function that did make it into
934 // the final executable.
935 info->prev_section_sp.reset();
936 info->curr_section_sp.reset();
937 }
938
939 info->prev_in_final_executable = curr_in_final_executable;
940 }
941 else
942 {
943 // We are not in an object file that contains DWARF for an
944 // N_OSO, this is just a normal DWARF file. The DWARF spec
945 // guarantees that the addresses will be in increasing order
946 // so, since we store line tables in file address order, we
947 // can always just append the line entry without needing to
948 // search for the correct insertion point (we don't need to
949 // use LineEntry::InsertLineEntry()).
950 line_table->AppendLineEntry (info->curr_section_sp,
951 curr_line_section_offset,
952 state.line,
953 state.column,
954 state.file,
955 state.is_stmt,
956 state.basic_block,
957 state.prologue_end,
958 state.epilogue_begin,
959 state.end_sequence);
960 }
961 }
962
963 info->prev_row = state;
964 }
965}
966
967bool
968SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
969{
970 assert (sc.comp_unit);
971 if (sc.comp_unit->GetLineTable() != NULL)
972 return true;
973
974 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
975 if (dwarf_cu)
976 {
977 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
978 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
979 if (cu_line_offset != DW_INVALID_OFFSET)
980 {
981 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
982 if (line_table_ap.get())
983 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000984 ParseDWARFLineTableCallbackInfo info = { line_table_ap.get(), m_obj_file->GetSectionList(), 0, 0, m_debug_map_symfile != NULL, false};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985 uint32_t offset = cu_line_offset;
986 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
987 sc.comp_unit->SetLineTable(line_table_ap.release());
988 return true;
989 }
990 }
991 }
992 return false;
993}
994
995size_t
996SymbolFileDWARF::ParseFunctionBlocks
997(
998 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000999 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001000 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001 const DWARFDebugInfoEntry *die,
1002 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001003 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004)
1005{
1006 size_t blocks_added = 0;
1007 while (die != NULL)
1008 {
1009 dw_tag_t tag = die->Tag();
1010
1011 switch (tag)
1012 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001014 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 case DW_TAG_lexical_block:
1016 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001017 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001018 if (tag == DW_TAG_subprogram)
1019 {
1020 // Skip any DW_TAG_subprogram DIEs that are inside
1021 // of a normal or inlined functions. These will be
1022 // parsed on their own as separate entities.
1023
1024 if (depth > 0)
1025 break;
1026
1027 block = parent_block;
1028 }
1029 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001030 {
1031 BlockSP block_sp(new Block (die->GetOffset()));
1032 parent_block->AddChild(block_sp);
1033 block = block_sp.get();
1034 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001035 DWARFDebugRanges::RangeList ranges;
1036 const char *name = NULL;
1037 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 int decl_file = 0;
1040 int decl_line = 0;
1041 int decl_column = 0;
1042 int call_file = 0;
1043 int call_line = 0;
1044 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001045 if (die->GetDIENamesAndRanges (this,
1046 dwarf_cu,
1047 name,
1048 mangled_name,
1049 ranges,
1050 decl_file, decl_line, decl_column,
1051 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 {
1053 if (tag == DW_TAG_subprogram)
1054 {
1055 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001056 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001058 else if (tag == DW_TAG_inlined_subroutine)
1059 {
1060 // We get called here for inlined subroutines in two ways.
1061 // The first time is when we are making the Function object
1062 // for this inlined concrete instance. Since we're creating a top level block at
1063 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1064 // adjust the containing address.
1065 // The second time is when we are parsing the blocks inside the function that contains
1066 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1067 // function the offset will be for that function.
1068 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1069 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001070 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001071 }
1072 }
1073
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001074 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075
1076 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1077 {
1078 std::auto_ptr<Declaration> decl_ap;
1079 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001080 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1081 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082
1083 std::auto_ptr<Declaration> call_ap;
1084 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001085 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1086 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001088 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 }
1090
1091 ++blocks_added;
1092
Greg Claytondd7feaf2011-08-12 17:54:33 +00001093 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001095 blocks_added += ParseFunctionBlocks (sc,
1096 block,
1097 dwarf_cu,
1098 die->GetFirstChild(),
1099 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001100 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101 }
1102 }
1103 }
1104 break;
1105 default:
1106 break;
1107 }
1108
Greg Claytondd7feaf2011-08-12 17:54:33 +00001109 // Only parse siblings of the block if we are not at depth zero. A depth
1110 // of zero indicates we are currently parsing the top level
1111 // DW_TAG_subprogram DIE
1112
1113 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001115 else
1116 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117 }
1118 return blocks_added;
1119}
1120
1121size_t
1122SymbolFileDWARF::ParseChildMembers
1123(
1124 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001125 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001127 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001128 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1130 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001131 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001132 AccessType& default_accessibility,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133 bool &is_a_class
1134)
1135{
1136 if (parent_die == NULL)
1137 return 0;
1138
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 size_t count = 0;
1140 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001141 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001142 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001143
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1145 {
1146 dw_tag_t tag = die->Tag();
1147
1148 switch (tag)
1149 {
1150 case DW_TAG_member:
1151 {
1152 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001153 const size_t num_attributes = die->GetAttributes (this,
1154 dwarf_cu,
1155 fixed_form_sizes,
1156 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 if (num_attributes > 0)
1158 {
1159 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001160 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001161 const char *name = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00001162 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001164 AccessType accessibility = eAccessNone;
Greg Clayton73b472d2010-10-27 03:32:59 +00001165 //off_t member_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 size_t byte_size = 0;
1167 size_t bit_offset = 0;
1168 size_t bit_size = 0;
1169 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001170 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171 {
1172 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1173 DWARFFormValue form_value;
1174 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1175 {
1176 switch (attr)
1177 {
1178 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1179 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1180 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1181 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1182 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1183 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1184 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1185 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1186 case DW_AT_data_member_location:
Greg Clayton73b472d2010-10-27 03:32:59 +00001187// if (form_value.BlockData())
1188// {
1189// Value initialValue(0);
1190// Value memberOffset(0);
1191// const DataExtractor& debug_info_data = get_debug_info_data();
1192// uint32_t block_length = form_value.Unsigned();
1193// uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1194// if (DWARFExpression::Evaluate(NULL, NULL, debug_info_data, NULL, NULL, block_offset, block_length, eRegisterKindDWARF, &initialValue, memberOffset, NULL))
1195// {
1196// member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1197// }
1198// }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001199 break;
1200
Greg Clayton8cf05932010-07-22 18:30:50 +00001201 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton24739922010-10-13 03:15:28 +00001202 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203 case DW_AT_declaration:
1204 case DW_AT_description:
1205 case DW_AT_mutable:
1206 case DW_AT_visibility:
1207 default:
1208 case DW_AT_sibling:
1209 break;
1210 }
1211 }
1212 }
Sean Callanan5a477cf2010-10-30 01:56:10 +00001213
1214 // FIXME: Make Clang ignore Objective-C accessibility for expressions
1215
1216 if (class_language == eLanguageTypeObjC ||
1217 class_language == eLanguageTypeObjC_plus_plus)
1218 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001219
1220 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1221 {
1222 // Not all compilers will mark the vtable pointer
1223 // member as artificial (llvm-gcc). We can't have
1224 // the virtual members in our classes otherwise it
1225 // throws off all child offsets since we end up
1226 // having and extra pointer sized member in our
1227 // class layouts.
1228 is_artificial = true;
1229 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230
Greg Clayton24739922010-10-13 03:15:28 +00001231 if (is_artificial == false)
1232 {
1233 Type *member_type = ResolveTypeUID(encoding_uid);
Greg Claytond16e1e52011-07-12 17:06:17 +00001234 if (member_type)
1235 {
1236 if (accessibility == eAccessNone)
1237 accessibility = default_accessibility;
1238 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239
Greg Claytond16e1e52011-07-12 17:06:17 +00001240 GetClangASTContext().AddFieldToRecordType (class_clang_type,
1241 name,
1242 member_type->GetClangLayoutType(),
1243 accessibility,
1244 bit_size);
1245 }
1246 else
1247 {
1248 if (name)
1249 ReportError ("0x%8.8x: DW_TAG_member '%s' refers to type 0x%8.8x which was unable to be parsed",
1250 die->GetOffset(),
1251 name,
1252 encoding_uid);
1253 else
1254 ReportError ("0x%8.8x: DW_TAG_member refers to type 0x%8.8x which was unable to be parsed",
1255 die->GetOffset(),
1256 encoding_uid);
1257 }
Greg Clayton24739922010-10-13 03:15:28 +00001258 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001260 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261 }
1262 break;
1263
1264 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001265 // Let the type parsing code handle this one for us.
1266 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267 break;
1268
1269 case DW_TAG_inheritance:
1270 {
1271 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001272 if (default_accessibility == eAccessNone)
1273 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274 // TODO: implement DW_TAG_inheritance type parsing
1275 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001276 const size_t num_attributes = die->GetAttributes (this,
1277 dwarf_cu,
1278 fixed_form_sizes,
1279 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280 if (num_attributes > 0)
1281 {
1282 Declaration decl;
1283 DWARFExpression location;
1284 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001285 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286 bool is_virtual = false;
1287 bool is_base_of_class = true;
1288 off_t member_offset = 0;
1289 uint32_t i;
1290 for (i=0; i<num_attributes; ++i)
1291 {
1292 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1293 DWARFFormValue form_value;
1294 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1295 {
1296 switch (attr)
1297 {
1298 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1299 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1300 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1301 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1302 case DW_AT_data_member_location:
1303 if (form_value.BlockData())
1304 {
1305 Value initialValue(0);
1306 Value memberOffset(0);
1307 const DataExtractor& debug_info_data = get_debug_info_data();
1308 uint32_t block_length = form_value.Unsigned();
1309 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001310 if (DWARFExpression::Evaluate (NULL,
1311 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001312 NULL,
1313 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001314 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001315 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001316 block_offset,
1317 block_length,
1318 eRegisterKindDWARF,
1319 &initialValue,
1320 memberOffset,
1321 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001322 {
1323 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1324 }
1325 }
1326 break;
1327
1328 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001329 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330 break;
1331
1332 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1333 default:
1334 case DW_AT_sibling:
1335 break;
1336 }
1337 }
1338 }
1339
Greg Clayton526e5af2010-11-13 03:52:47 +00001340 Type *base_class_type = ResolveTypeUID(encoding_uid);
1341 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001342
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001343 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1344 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001345 if (class_language == eLanguageTypeObjC)
1346 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001347 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001348 }
1349 else
1350 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001351 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001352 accessibility,
1353 is_virtual,
1354 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001355 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356 }
1357 }
1358 break;
1359
1360 default:
1361 break;
1362 }
1363 }
1364 return count;
1365}
1366
1367
1368clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001369SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001370{
1371 DWARFDebugInfo* debug_info = DebugInfo();
1372 if (debug_info)
1373 {
1374 DWARFCompileUnitSP cu_sp;
1375 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1376 if (die)
Sean Callanan72e49402011-08-05 23:43:37 +00001377 return GetClangDeclContextContainingDIE (cu_sp.get(), die);
1378 }
1379 return NULL;
1380}
1381
1382clang::DeclContext*
1383SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1384{
Greg Clayton5cf58b92011-10-05 22:22:08 +00001385 return GetClangDeclContextForDIEOffset (sc, type_uid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386}
1387
1388Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001389SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390{
1391 DWARFDebugInfo* debug_info = DebugInfo();
1392 if (debug_info)
1393 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00001394 DWARFCompileUnitSP cu_sp;
1395 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001396 if (type_die != NULL)
Greg Claytonca512b32011-01-14 04:54:56 +00001397 {
1398 // We might be coming in in the middle of a type tree (a class
1399 // withing a class, an enum within a class), so parse any needed
1400 // parent DIEs before we get to this one...
Greg Clayton5cf58b92011-10-05 22:22:08 +00001401 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu_sp.get(), type_die);
1402 switch (decl_ctx_die->Tag())
Greg Claytonca512b32011-01-14 04:54:56 +00001403 {
1404 case DW_TAG_structure_type:
1405 case DW_TAG_union_type:
1406 case DW_TAG_class_type:
Greg Clayton5cf58b92011-10-05 22:22:08 +00001407 ResolveType(cu_sp.get(), decl_ctx_die);
Greg Claytonca512b32011-01-14 04:54:56 +00001408 break;
1409 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00001410 return ResolveType (cu_sp.get(), type_die);
Greg Claytonca512b32011-01-14 04:54:56 +00001411 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 }
1413 return NULL;
1414}
1415
Greg Clayton6beaaa62011-01-17 03:46:26 +00001416// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1417// SymbolFileDWARF objects to detect if this DWARF file is the one that
1418// can resolve a clang_type.
1419bool
1420SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1421{
1422 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1423 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1424 return die != NULL;
1425}
1426
1427
Greg Clayton1be10fc2010-09-29 01:12:09 +00001428lldb::clang_type_t
1429SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1430{
1431 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001432 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1433 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001434 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001435 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001436// if (m_debug_map_symfile)
1437// {
1438// Type *type = m_die_to_type[die];
1439// if (type && type->GetSymbolFile() != this)
1440// return type->GetClangType();
1441// }
Greg Clayton73b472d2010-10-27 03:32:59 +00001442 // We have already resolved this type...
1443 return clang_type;
1444 }
1445 // Once we start resolving this type, remove it from the forward declaration
1446 // map in case anyone child members or other types require this type to get resolved.
1447 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1448 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001449 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001450
Greg Clayton1be10fc2010-09-29 01:12:09 +00001451
Greg Clayton450e3f32010-10-12 02:24:53 +00001452 DWARFDebugInfo* debug_info = DebugInfo();
1453
Greg Clayton96d7d742010-11-10 23:42:09 +00001454 DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001455 Type *type = m_die_to_type.lookup (die);
1456
1457 const dw_tag_t tag = die->Tag();
1458
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001459 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\") - resolve forward declaration...\n",
1460 die->GetOffset(),
1461 DW_TAG_value_to_name(tag),
1462 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001463 assert (clang_type);
1464 DWARFDebugInfoEntry::Attributes attributes;
1465
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001466 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001467
1468 switch (tag)
1469 {
1470 case DW_TAG_structure_type:
1471 case DW_TAG_union_type:
1472 case DW_TAG_class_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001473 ast.StartTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00001474 if (die->HasChildren())
1475 {
1476 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton450e3f32010-10-12 02:24:53 +00001477 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
1478 if (is_objc_class)
Greg Claytonc93237c2010-10-01 20:48:32 +00001479 class_language = eLanguageTypeObjC;
1480
1481 int tag_decl_kind = -1;
1482 AccessType default_accessibility = eAccessNone;
1483 if (tag == DW_TAG_structure_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00001484 {
Greg Claytonc93237c2010-10-01 20:48:32 +00001485 tag_decl_kind = clang::TTK_Struct;
1486 default_accessibility = eAccessPublic;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001487 }
Greg Claytonc93237c2010-10-01 20:48:32 +00001488 else if (tag == DW_TAG_union_type)
1489 {
1490 tag_decl_kind = clang::TTK_Union;
1491 default_accessibility = eAccessPublic;
1492 }
1493 else if (tag == DW_TAG_class_type)
1494 {
1495 tag_decl_kind = clang::TTK_Class;
1496 default_accessibility = eAccessPrivate;
1497 }
1498
Greg Clayton96d7d742010-11-10 23:42:09 +00001499 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
Greg Claytonc93237c2010-10-01 20:48:32 +00001500 std::vector<clang::CXXBaseSpecifier *> base_classes;
1501 std::vector<int> member_accessibilities;
1502 bool is_a_class = false;
1503 // Parse members and base classes first
1504 DWARFDIECollection member_function_dies;
1505
1506 ParseChildMembers (sc,
Greg Clayton96d7d742010-11-10 23:42:09 +00001507 curr_cu,
Greg Claytonc93237c2010-10-01 20:48:32 +00001508 die,
1509 clang_type,
1510 class_language,
1511 base_classes,
1512 member_accessibilities,
1513 member_function_dies,
1514 default_accessibility,
1515 is_a_class);
1516
1517 // Now parse any methods if there were any...
1518 size_t num_functions = member_function_dies.Size();
1519 if (num_functions > 0)
1520 {
1521 for (size_t i=0; i<num_functions; ++i)
1522 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001523 ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytonc93237c2010-10-01 20:48:32 +00001524 }
1525 }
1526
Greg Clayton450e3f32010-10-12 02:24:53 +00001527 if (class_language == eLanguageTypeObjC)
1528 {
Greg Claytone3055942011-06-30 02:28:26 +00001529 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(clang_type));
Greg Clayton450e3f32010-10-12 02:24:53 +00001530 if (!class_str.empty())
1531 {
1532
1533 ConstString class_name (class_str.c_str());
Greg Claytond4a2b372011-09-12 23:21:58 +00001534 DIEArray method_die_offsets;
1535 if (m_objc_class_selectors_index.Find (class_name, method_die_offsets))
Greg Clayton450e3f32010-10-12 02:24:53 +00001536 {
Greg Claytond4a2b372011-09-12 23:21:58 +00001537 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton450e3f32010-10-12 02:24:53 +00001538
Greg Claytond4a2b372011-09-12 23:21:58 +00001539 DWARFCompileUnit* method_cu = NULL;
1540 const size_t num_matches = method_die_offsets.size();
1541 for (size_t i=0; i<num_matches; ++i)
1542 {
1543 const dw_offset_t die_offset = method_die_offsets[i];
1544 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
Greg Clayton450e3f32010-10-12 02:24:53 +00001545
1546 ResolveType (method_cu, method_die);
1547 }
1548 }
1549 }
1550 }
1551
Greg Claytonc93237c2010-10-01 20:48:32 +00001552 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
1553 // need to tell the clang type it is actually a class.
1554 if (class_language != eLanguageTypeObjC)
1555 {
1556 if (is_a_class && tag_decl_kind != clang::TTK_Class)
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001557 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
Greg Claytonc93237c2010-10-01 20:48:32 +00001558 }
1559
1560 // Since DW_TAG_structure_type gets used for both classes
1561 // and structures, we may need to set any DW_TAG_member
1562 // fields to have a "private" access if none was specified.
1563 // When we parsed the child members we tracked that actual
1564 // accessibility value for each DW_TAG_member in the
1565 // "member_accessibilities" array. If the value for the
1566 // member is zero, then it was set to the "default_accessibility"
1567 // which for structs was "public". Below we correct this
1568 // by setting any fields to "private" that weren't correctly
1569 // set.
1570 if (is_a_class && !member_accessibilities.empty())
1571 {
1572 // This is a class and all members that didn't have
1573 // their access specified are private.
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001574 ast.SetDefaultAccessForRecordFields (clang_type,
1575 eAccessPrivate,
1576 &member_accessibilities.front(),
1577 member_accessibilities.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001578 }
1579
1580 if (!base_classes.empty())
1581 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001582 ast.SetBaseClassesForClassType (clang_type,
1583 &base_classes.front(),
1584 base_classes.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001585
1586 // Clang will copy each CXXBaseSpecifier in "base_classes"
1587 // so we have to free them all.
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001588 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
1589 base_classes.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001590 }
1591
1592 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001593 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00001594 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001595
1596 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001597 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001598 if (die->HasChildren())
1599 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001600 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1601 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001602 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001603 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001604 return clang_type;
1605
1606 default:
1607 assert(false && "not a forward clang type decl!");
1608 break;
1609 }
1610 return NULL;
1611}
1612
Greg Claytonc685f8e2010-09-15 04:15:46 +00001613Type*
Greg Clayton96d7d742010-11-10 23:42:09 +00001614SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00001615{
1616 if (type_die != NULL)
1617 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00001618 Type *type = m_die_to_type.lookup (type_die);
Greg Claytonc685f8e2010-09-15 04:15:46 +00001619 if (type == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001620 type = GetTypeForDIE (curr_cu, type_die).get();
Greg Clayton24739922010-10-13 03:15:28 +00001621 if (assert_not_being_parsed)
1622 assert (type != DIE_IS_BEING_PARSED);
Greg Clayton594e5ed2010-09-27 21:07:38 +00001623 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001624 }
1625 return NULL;
1626}
1627
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001628CompileUnit*
Greg Clayton96d7d742010-11-10 23:42:09 +00001629SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630{
1631 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00001632 if (curr_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001633 {
1634 // The symbol vendor doesn't know about this compile unit, we
1635 // need to parse and add it to the symbol vendor object.
1636 CompUnitSP dc_cu;
Greg Clayton96d7d742010-11-10 23:42:09 +00001637 ParseCompileUnit(curr_cu, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001638 if (dc_cu.get())
1639 {
1640 // Figure out the compile unit index if we weren't given one
Greg Clayton016a95e2010-09-14 02:20:48 +00001641 if (cu_idx == UINT32_MAX)
Greg Clayton96d7d742010-11-10 23:42:09 +00001642 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001643
1644 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx);
Greg Clayton450e3f32010-10-12 02:24:53 +00001645
1646 if (m_debug_map_symfile)
1647 m_debug_map_symfile->SetCompileUnit(this, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001648 }
1649 }
Greg Clayton96d7d742010-11-10 23:42:09 +00001650 return (CompileUnit*)curr_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001651}
1652
1653bool
Greg Clayton96d7d742010-11-10 23:42:09 +00001654SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655{
1656 sc.Clear();
1657 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00001658 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001659
1660 sc.function = sc.comp_unit->FindFunctionByUID (func_die->GetOffset()).get();
1661 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001662 sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00001663
1664 if (sc.function)
1665 {
1666 sc.module_sp = sc.function->CalculateSymbolContextModule();
1667 return true;
1668 }
1669
1670 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671}
1672
1673uint32_t
1674SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1675{
1676 Timer scoped_timer(__PRETTY_FUNCTION__,
1677 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
1678 so_addr.GetSection(),
1679 so_addr.GetOffset(),
1680 resolve_scope);
1681 uint32_t resolved = 0;
1682 if (resolve_scope & ( eSymbolContextCompUnit |
1683 eSymbolContextFunction |
1684 eSymbolContextBlock |
1685 eSymbolContextLineEntry))
1686 {
1687 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1688
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001689 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00001690 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691 {
Greg Claytond4a2b372011-09-12 23:21:58 +00001692 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001693 if (cu_offset != DW_INVALID_OFFSET)
1694 {
1695 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00001696 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
1697 if (curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001698 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001699 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700 assert(sc.comp_unit != NULL);
1701 resolved |= eSymbolContextCompUnit;
1702
1703 if (resolve_scope & eSymbolContextLineEntry)
1704 {
1705 LineTable *line_table = sc.comp_unit->GetLineTable();
1706 if (line_table == NULL)
1707 {
1708 if (ParseCompileUnitLineTable(sc))
1709 line_table = sc.comp_unit->GetLineTable();
1710 }
1711 if (line_table != NULL)
1712 {
1713 if (so_addr.IsLinkedAddress())
1714 {
1715 Address linked_addr (so_addr);
1716 linked_addr.ResolveLinkedAddress();
1717 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
1718 {
1719 resolved |= eSymbolContextLineEntry;
1720 }
1721 }
1722 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
1723 {
1724 resolved |= eSymbolContextLineEntry;
1725 }
1726 }
1727 }
1728
1729 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1730 {
1731 DWARFDebugInfoEntry *function_die = NULL;
1732 DWARFDebugInfoEntry *block_die = NULL;
1733 if (resolve_scope & eSymbolContextBlock)
1734 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001735 curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736 }
1737 else
1738 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001739 curr_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001740 }
1741
1742 if (function_die != NULL)
1743 {
1744 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1745 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001746 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001747 }
1748
1749 if (sc.function != NULL)
1750 {
1751 resolved |= eSymbolContextFunction;
1752
1753 if (resolve_scope & eSymbolContextBlock)
1754 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001755 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756
1757 if (block_die != NULL)
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001758 sc.block = block.FindBlockByID (block_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001759 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001760 sc.block = block.FindBlockByID (function_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761 if (sc.block)
1762 resolved |= eSymbolContextBlock;
1763 }
1764 }
1765 }
1766 }
1767 }
1768 }
1769 }
1770 return resolved;
1771}
1772
1773
1774
1775uint32_t
1776SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
1777{
1778 const uint32_t prev_size = sc_list.GetSize();
1779 if (resolve_scope & eSymbolContextCompUnit)
1780 {
1781 DWARFDebugInfo* debug_info = DebugInfo();
1782 if (debug_info)
1783 {
1784 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00001785 DWARFCompileUnit* curr_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786
Greg Clayton96d7d742010-11-10 23:42:09 +00001787 for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001789 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001790 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
1791 if (check_inlines || file_spec_matches_cu_file_spec)
1792 {
1793 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton96d7d742010-11-10 23:42:09 +00001794 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001795 assert(sc.comp_unit != NULL);
1796
1797 uint32_t file_idx = UINT32_MAX;
1798
1799 // If we are looking for inline functions only and we don't
1800 // find it in the support files, we are done.
1801 if (check_inlines)
1802 {
Jim Ingham87df91b2011-09-23 00:54:11 +00001803 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804 if (file_idx == UINT32_MAX)
1805 continue;
1806 }
1807
1808 if (line != 0)
1809 {
1810 LineTable *line_table = sc.comp_unit->GetLineTable();
1811
1812 if (line_table != NULL && line != 0)
1813 {
1814 // We will have already looked up the file index if
1815 // we are searching for inline entries.
1816 if (!check_inlines)
Jim Ingham87df91b2011-09-23 00:54:11 +00001817 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001818
1819 if (file_idx != UINT32_MAX)
1820 {
1821 uint32_t found_line;
1822 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
1823 found_line = sc.line_entry.line;
1824
Greg Clayton016a95e2010-09-14 02:20:48 +00001825 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826 {
1827 sc.function = NULL;
1828 sc.block = NULL;
1829 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1830 {
1831 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
1832 if (file_vm_addr != LLDB_INVALID_ADDRESS)
1833 {
1834 DWARFDebugInfoEntry *function_die = NULL;
1835 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton96d7d742010-11-10 23:42:09 +00001836 curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001837
1838 if (function_die != NULL)
1839 {
1840 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1841 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001842 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001843 }
1844
1845 if (sc.function != NULL)
1846 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001847 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001848
1849 if (block_die != NULL)
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001850 sc.block = block.FindBlockByID (block_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001851 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001852 sc.block = block.FindBlockByID (function_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853 }
1854 }
1855 }
1856
1857 sc_list.Append(sc);
1858 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1859 }
1860 }
1861 }
1862 else if (file_spec_matches_cu_file_spec && !check_inlines)
1863 {
1864 // only append the context if we aren't looking for inline call sites
1865 // by file and line and if the file spec matches that of the compile unit
1866 sc_list.Append(sc);
1867 }
1868 }
1869 else if (file_spec_matches_cu_file_spec && !check_inlines)
1870 {
1871 // only append the context if we aren't looking for inline call sites
1872 // by file and line and if the file spec matches that of the compile unit
1873 sc_list.Append(sc);
1874 }
1875
1876 if (!check_inlines)
1877 break;
1878 }
1879 }
1880 }
1881 }
1882 return sc_list.GetSize() - prev_size;
1883}
1884
1885void
1886SymbolFileDWARF::Index ()
1887{
1888 if (m_indexed)
1889 return;
1890 m_indexed = true;
1891 Timer scoped_timer (__PRETTY_FUNCTION__,
1892 "SymbolFileDWARF::Index (%s)",
1893 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
1894
1895 DWARFDebugInfo* debug_info = DebugInfo();
1896 if (debug_info)
1897 {
1898 uint32_t cu_idx = 0;
1899 const uint32_t num_compile_units = GetNumCompileUnits();
1900 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1901 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001902 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001903
Greg Clayton96d7d742010-11-10 23:42:09 +00001904 bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001905
Greg Clayton96d7d742010-11-10 23:42:09 +00001906 curr_cu->Index (cu_idx,
Greg Clayton83c5cd92010-11-14 22:13:40 +00001907 m_function_basename_index,
1908 m_function_fullname_index,
1909 m_function_method_index,
1910 m_function_selector_index,
1911 m_objc_class_selectors_index,
1912 m_global_index,
1913 m_type_index,
Greg Claytond4a2b372011-09-12 23:21:58 +00001914 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001915
1916 // Keep memory down by clearing DIEs if this generate function
1917 // caused them to be parsed
1918 if (clear_dies)
Greg Clayton96d7d742010-11-10 23:42:09 +00001919 curr_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001920 }
1921
Greg Claytond4a2b372011-09-12 23:21:58 +00001922 m_function_basename_index.Finalize();
1923 m_function_fullname_index.Finalize();
1924 m_function_method_index.Finalize();
1925 m_function_selector_index.Finalize();
1926 m_objc_class_selectors_index.Finalize();
1927 m_global_index.Finalize();
1928 m_type_index.Finalize();
1929 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00001930
Greg Clayton24739922010-10-13 03:15:28 +00001931#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00001932 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00001933 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00001934 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
1935 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00001936 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
1937 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
1938 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
1939 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
1940 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
1941 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00001942 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00001943 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00001944#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001945 }
1946}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00001947
1948bool
1949SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
1950{
1951 if (namespace_decl == NULL)
1952 {
1953 // Invalid namespace decl which means we aren't matching only things
1954 // in this symbol file, so return true to indicate it matches this
1955 // symbol file.
1956 return true;
1957 }
1958
1959 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
1960
1961 if (namespace_ast == NULL)
1962 return true; // No AST in the "namespace_decl", return true since it
1963 // could then match any symbol file, including this one
1964
1965 if (namespace_ast == GetClangASTContext().getASTContext())
1966 return true; // The ASTs match, return true
1967
1968 // The namespace AST was valid, and it does not match...
1969 return false;
1970}
1971
Greg Clayton2506a7a2011-10-12 23:34:26 +00001972bool
1973SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
1974 DWARFCompileUnit* cu,
1975 const DWARFDebugInfoEntry* die)
1976{
1977 // No namespace specified, so the answesr i
1978 if (namespace_decl == NULL)
1979 return true;
Greg Claytonbfe3dd42011-10-13 00:00:53 +00001980
Greg Clayton2506a7a2011-10-12 23:34:26 +00001981 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1982 if (decl_ctx_die)
1983 {
Greg Claytonbfe3dd42011-10-13 00:00:53 +00001984
Greg Clayton2506a7a2011-10-12 23:34:26 +00001985 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
1986 if (clang_namespace_decl)
1987 {
1988 if (decl_ctx_die->Tag() != DW_TAG_namespace)
1989 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990
Greg Clayton2506a7a2011-10-12 23:34:26 +00001991 DeclContextToDIEMap::iterator pos = m_decl_ctx_to_die.find(clang_namespace_decl);
1992
1993 if (pos == m_decl_ctx_to_die.end())
1994 return false;
1995
1996 return decl_ctx_die == pos->second;
1997 }
1998 else
1999 {
2000 // We have a namespace_decl that was not NULL but it contained
2001 // a NULL "clang::NamespaceDecl", so this means the global namespace
2002 // So as long the the contained decl context DIE isn't a namespace
2003 // we should be ok.
2004 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2005 return true;
2006 }
2007 }
2008 return false;
2009}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002010uint32_t
2011SymbolFileDWARF::FindGlobalVariables (const ConstString &name, bool append, uint32_t max_matches, VariableList& variables)
2012{
Greg Clayton21f2a492011-10-06 00:09:08 +00002013 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2014
2015 if (log)
2016 {
2017 log->Printf ("SymbolFileDWARF::FindGlobalVariables (file=\"%s/%s\", name=\"%s\", append=%u, max_matches=%u, variables)",
2018 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2019 m_obj_file->GetFileSpec().GetFilename().GetCString(),
2020 name.GetCString(), append, max_matches);
2021 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002022 DWARFDebugInfo* info = DebugInfo();
2023 if (info == NULL)
2024 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002025
2026 // If we aren't appending the results to this list, then clear the list
2027 if (!append)
2028 variables.Clear();
2029
2030 // Remember how many variables are in the list before we search in case
2031 // we are appending the results to a variable list.
2032 const uint32_t original_size = variables.GetSize();
2033
Greg Claytond4a2b372011-09-12 23:21:58 +00002034 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002035
2036 if (m_apple_names_ap.get())
2037 {
2038 const char *name_cstr = name.GetCString();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002039 const char *base_name_start;
2040 const char *base_name_end = NULL;
2041
2042 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2043 base_name_start = name_cstr;
2044
2045 m_apple_names_ap->FindByName (base_name_start, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002046 }
2047 else
2048 {
2049 // Index the DWARF if we haven't already
2050 if (!m_indexed)
2051 Index ();
2052
2053 m_global_index.Find (name, die_offsets);
2054 }
2055
2056 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002057 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002058 {
Greg Clayton7f995132011-10-04 22:41:51 +00002059 SymbolContext sc;
2060 sc.module_sp = m_obj_file->GetModule();
2061 assert (sc.module_sp);
2062
Greg Claytond4a2b372011-09-12 23:21:58 +00002063 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002064 DWARFCompileUnit* dwarf_cu = NULL;
2065 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002066 for (size_t i=0; i<num_matches; ++i)
2067 {
2068 const dw_offset_t die_offset = die_offsets[i];
2069 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002070
Greg Claytond4a2b372011-09-12 23:21:58 +00002071 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2072 assert(sc.comp_unit != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073
Greg Claytond4a2b372011-09-12 23:21:58 +00002074 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002075
Greg Claytond4a2b372011-09-12 23:21:58 +00002076 if (variables.GetSize() - original_size >= max_matches)
2077 break;
2078 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002079 }
2080
2081 // Return the number of variable that were appended to the list
2082 return variables.GetSize() - original_size;
2083}
2084
2085uint32_t
2086SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2087{
Greg Clayton21f2a492011-10-06 00:09:08 +00002088 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2089
2090 if (log)
2091 {
2092 log->Printf ("SymbolFileDWARF::FindGlobalVariables (file=\"%s/%s\", regex=\"%s\", append=%u, max_matches=%u, variables)",
2093 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2094 m_obj_file->GetFileSpec().GetFilename().GetCString(),
2095 regex.GetText(), append, max_matches);
2096 }
2097
Greg Claytonc685f8e2010-09-15 04:15:46 +00002098 DWARFDebugInfo* info = DebugInfo();
2099 if (info == NULL)
2100 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002101
2102 // If we aren't appending the results to this list, then clear the list
2103 if (!append)
2104 variables.Clear();
2105
2106 // Remember how many variables are in the list before we search in case
2107 // we are appending the results to a variable list.
2108 const uint32_t original_size = variables.GetSize();
2109
Greg Clayton7f995132011-10-04 22:41:51 +00002110 DIEArray die_offsets;
2111
2112 if (m_apple_names_ap.get())
2113 {
2114 m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, die_offsets);
2115 }
2116 else
2117 {
2118 // Index the DWARF if we haven't already
2119 if (!m_indexed)
2120 Index ();
2121
2122 m_global_index.Find (regex, die_offsets);
2123 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002124
Greg Claytonc685f8e2010-09-15 04:15:46 +00002125 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +00002126 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002127 assert (sc.module_sp);
2128
Greg Claytond4a2b372011-09-12 23:21:58 +00002129 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002130 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002131 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002132 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002133 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002134 DWARFDebugInfo* debug_info = DebugInfo();
2135 for (size_t i=0; i<num_matches; ++i)
2136 {
2137 const dw_offset_t die_offset = die_offsets[i];
2138 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2139 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002140
Greg Claytond4a2b372011-09-12 23:21:58 +00002141 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002142
Greg Claytond4a2b372011-09-12 23:21:58 +00002143 if (variables.GetSize() - original_size >= max_matches)
2144 break;
2145 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002146 }
2147
2148 // Return the number of variable that were appended to the list
2149 return variables.GetSize() - original_size;
2150}
2151
Jim Ingham4cda6e02011-10-07 22:23:45 +00002152bool
2153SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2154 DWARFCompileUnit *&dwarf_cu,
2155 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002156{
Greg Clayton9e315582011-09-02 04:03:59 +00002157 SymbolContext sc;
Greg Clayton9e315582011-09-02 04:03:59 +00002158
Jim Ingham4cda6e02011-10-07 22:23:45 +00002159 DWARFDebugInfo* info = DebugInfo();
2160 bool resolved_it = false;
2161
2162 if (info == NULL)
2163 return resolved_it;
2164
2165 DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2166
2167 // If we were passed a die that is not a function, just return false...
2168 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2169 return false;
2170
2171 const DWARFDebugInfoEntry* inlined_die = NULL;
2172 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002173 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002174 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002175
Jim Ingham4cda6e02011-10-07 22:23:45 +00002176 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002177 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002178 if (die->Tag() == DW_TAG_subprogram)
2179 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002180 }
2181 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002182 assert (die->Tag() == DW_TAG_subprogram);
2183 if (GetFunction (dwarf_cu, die, sc))
2184 {
2185 Address addr;
2186 // Parse all blocks if needed
2187 if (inlined_die)
2188 {
2189 sc.block = sc.function->GetBlock (true).FindBlockByID (inlined_die->GetOffset());
2190 assert (sc.block != NULL);
2191 if (sc.block->GetStartAddress (addr) == false)
2192 addr.Clear();
2193 }
2194 else
2195 {
2196 sc.block = NULL;
2197 addr = sc.function->GetAddressRange().GetBaseAddress();
2198 }
2199
2200 if (addr.IsValid())
2201 {
2202
2203 // We found the function, so we should find the line table
2204 // and line table entry as well
2205 LineTable *line_table = sc.comp_unit->GetLineTable();
2206 if (line_table == NULL)
2207 {
2208 if (ParseCompileUnitLineTable(sc))
2209 line_table = sc.comp_unit->GetLineTable();
2210 }
2211 if (line_table != NULL)
2212 line_table->FindLineEntryByAddress (addr, sc.line_entry);
2213
2214 sc_list.Append(sc);
2215 resolved_it = true;
2216 }
2217 }
2218
2219 return resolved_it;
Greg Clayton9e315582011-09-02 04:03:59 +00002220}
2221
Greg Clayton7f995132011-10-04 22:41:51 +00002222void
2223SymbolFileDWARF::FindFunctions (const ConstString &name,
2224 const NameToDIE &name_to_die,
2225 SymbolContextList& sc_list)
2226{
Greg Claytond4a2b372011-09-12 23:21:58 +00002227 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002228 if (name_to_die.Find (name, die_offsets))
2229 {
2230 ParseFunctions (die_offsets, sc_list);
2231 }
2232}
2233
2234
2235void
2236SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2237 const NameToDIE &name_to_die,
2238 SymbolContextList& sc_list)
2239{
2240 DIEArray die_offsets;
2241 if (name_to_die.Find (regex, die_offsets))
2242 {
2243 ParseFunctions (die_offsets, sc_list);
2244 }
2245}
2246
2247
2248void
2249SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2250 const DWARFMappedHash::MemoryTable &memory_table,
2251 SymbolContextList& sc_list)
2252{
2253 DIEArray die_offsets;
2254 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, die_offsets))
2255 {
2256 ParseFunctions (die_offsets, sc_list);
2257 }
2258}
2259
2260void
2261SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2262 SymbolContextList& sc_list)
2263{
2264 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002265 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002266 {
Greg Clayton7f995132011-10-04 22:41:51 +00002267 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00002268
2269 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002270 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002271 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002272 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00002273 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274 }
2275 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002276}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002277
Jim Ingham4cda6e02011-10-07 22:23:45 +00002278bool
2279SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2280 const DWARFCompileUnit *dwarf_cu,
2281 uint32_t name_type_mask,
2282 const char *partial_name,
2283 const char *base_name_start,
2284 const char *base_name_end)
2285{
2286 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2287 if (name_type_mask == eFunctionNameTypeMethod
2288 || name_type_mask == eFunctionNameTypeBase)
2289 {
2290 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2291 if (!containing_decl_ctx)
2292 return false;
2293
2294 bool is_cxx_method = (containing_decl_ctx->getDeclKind() == clang::Decl::CXXRecord);
2295
2296 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2297 return false;
2298 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2299 return false;
2300 }
2301
2302 // Now we need to check whether the name we got back for this type matches the extra specifications
2303 // that were in the name we're looking up:
2304 if (base_name_start != partial_name || *base_name_end != '\0')
2305 {
2306 // First see if the stuff to the left matches the full name. To do that let's see if
2307 // we can pull out the mips linkage name attribute:
2308
2309 Mangled best_name;
2310
2311 DWARFDebugInfoEntry::Attributes attributes;
2312 die->GetAttributes(this, dwarf_cu, NULL, attributes);
2313 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
2314 if (idx != UINT32_MAX)
2315 {
2316 DWARFFormValue form_value;
2317 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
2318 {
2319 const char *name = form_value.AsCString(&get_debug_str_data());
2320 best_name.SetValue (name, true);
2321 }
2322 }
2323 if (best_name)
2324 {
2325 const char *demangled = best_name.GetDemangledName().GetCString();
2326 if (demangled)
2327 {
2328 std::string name_no_parens(partial_name, base_name_end - partial_name);
2329 if (strstr (demangled, name_no_parens.c_str()) == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002330 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002331 }
2332 }
2333 }
2334
2335 return true;
2336}
Greg Claytonc685f8e2010-09-15 04:15:46 +00002337
Greg Clayton0c5cd902010-06-28 21:30:43 +00002338uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00002339SymbolFileDWARF::FindFunctions (const ConstString &name,
2340 uint32_t name_type_mask,
2341 bool append,
2342 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002343{
2344 Timer scoped_timer (__PRETTY_FUNCTION__,
2345 "SymbolFileDWARF::FindFunctions (name = '%s')",
2346 name.AsCString());
2347
Greg Clayton21f2a492011-10-06 00:09:08 +00002348 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2349
2350 if (log)
2351 {
2352 log->Printf ("SymbolFileDWARF::FindFunctions (file=\"%s/%s\", name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2353 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2354 m_obj_file->GetFileSpec().GetFilename().GetCString(),
2355 name.GetCString(), name_type_mask, append);
2356 }
2357
Greg Clayton0c5cd902010-06-28 21:30:43 +00002358 // If we aren't appending the results to this list, then clear the list
2359 if (!append)
2360 sc_list.Clear();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002361
2362 // If name is empty then we won't find anything.
2363 if (name.IsEmpty())
2364 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002365
2366 // Remember how many sc_list are in the list before we search in case
2367 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002368
Greg Clayton9e315582011-09-02 04:03:59 +00002369 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00002370
Jim Ingham4cda6e02011-10-07 22:23:45 +00002371 const char *name_cstr = name.GetCString();
2372 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
2373 const char *base_name_start = name_cstr;
2374 const char *base_name_end = name_cstr + strlen(name_cstr);
2375
2376 if (name_type_mask & eFunctionNameTypeAuto)
2377 {
2378 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
2379 effective_name_type_mask = eFunctionNameTypeFull;
2380 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
2381 effective_name_type_mask = eFunctionNameTypeFull;
2382 else
2383 {
2384 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2385 effective_name_type_mask |= eFunctionNameTypeSelector;
2386
2387 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2388 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
2389 }
2390 }
2391 else
2392 {
2393 effective_name_type_mask = name_type_mask;
2394 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
2395 {
2396 // If they've asked for a CPP method or function name and it can't be that, we don't
2397 // even need to search for CPP methods or names.
2398 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2399 {
2400 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
2401 if (effective_name_type_mask == eFunctionNameTypeNone)
2402 return 0;
2403 }
2404 }
2405
2406 if (effective_name_type_mask & eFunctionNameTypeSelector)
2407 {
2408 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2409 {
2410 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
2411 if (effective_name_type_mask == eFunctionNameTypeNone)
2412 return 0;
2413 }
2414 }
2415 }
2416
2417 DWARFDebugInfo* info = DebugInfo();
2418 if (info == NULL)
2419 return 0;
2420
Greg Clayton7f995132011-10-04 22:41:51 +00002421 if (m_apple_names_ap.get())
Greg Clayton4d01ace2011-09-29 16:58:15 +00002422 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002423 DIEArray die_offsets;
2424
2425 uint32_t num_matches = 0;
2426
2427 if (effective_name_type_mask & eFunctionNameTypeFull)
2428 {
2429 // If they asked for the full name, match what they typed. At some point we may
2430 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
2431 // dies that we find by exact match.
2432 DWARFCompileUnit *dwarf_cu = NULL;
2433 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2434 for (uint32_t i = 0; i < num_matches; i++)
2435 ResolveFunction (die_offsets[i], dwarf_cu, sc_list);
2436 }
2437 else
2438 {
2439 DWARFCompileUnit* dwarf_cu = NULL;
2440
2441 if (effective_name_type_mask & eFunctionNameTypeSelector)
2442 {
2443 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2444 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
2445 // and if it is an ObjC method name, we're good.
2446
2447 for (uint32_t i = 0; i < num_matches; i++)
2448 {
2449 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2450 assert (die);
2451
2452 const char *die_name = die->GetName(this, dwarf_cu);
2453 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
2454 ResolveFunction (die_offsets[i], dwarf_cu, sc_list);
2455 }
2456 die_offsets.clear();
2457 }
2458
2459 if (effective_name_type_mask & eFunctionNameTypeMethod
2460 || effective_name_type_mask & eFunctionNameTypeBase)
2461 {
2462 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
2463 // extract the base name, look that up, and if there is any other information in the name we were
2464 // passed in we have to post-filter based on that.
2465
2466 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
2467 std::string base_name(base_name_start, base_name_end - base_name_start);
2468 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
2469
2470 for (uint32_t i = 0; i < num_matches; i++)
2471 {
2472 dw_offset_t offset = die_offsets[i];
2473 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (offset, &dwarf_cu);
2474 assert (die);
2475 if (!FunctionDieMatchesPartialName(die,
2476 dwarf_cu,
2477 effective_name_type_mask,
2478 name_cstr,
2479 base_name_start,
2480 base_name_end))
2481 continue;
2482
2483 // If we get to here, the die is good, and we should add it:
2484 ResolveFunction (offset, dwarf_cu, sc_list);
2485 }
2486 die_offsets.clear();
2487 }
2488 }
Greg Clayton7f995132011-10-04 22:41:51 +00002489 }
2490 else
2491 {
2492
2493 // Index the DWARF if we haven't already
2494 if (!m_indexed)
2495 Index ();
2496
Greg Clayton7f995132011-10-04 22:41:51 +00002497 if (name_type_mask & eFunctionNameTypeFull)
2498 FindFunctions (name, m_function_fullname_index, sc_list);
2499
Jim Ingham4cda6e02011-10-07 22:23:45 +00002500 std::string base_name(base_name_start, base_name_end - base_name_start);
2501 ConstString base_name_const(base_name.c_str());
2502 DIEArray die_offsets;
2503 DWARFCompileUnit *dwarf_cu = NULL;
2504
2505 if (effective_name_type_mask & eFunctionNameTypeBase)
2506 {
2507 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
2508 {
2509 for (uint32_t i = 0; i < num_base; i++)
2510 {
2511 dw_offset_t offset = die_offsets[i];
2512 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (offset, &dwarf_cu);
2513 assert (die);
2514 if (!FunctionDieMatchesPartialName(die,
2515 dwarf_cu,
2516 effective_name_type_mask,
2517 name_cstr,
2518 base_name_start,
2519 base_name_end))
2520 continue;
2521
2522 // If we get to here, the die is good, and we should add it:
2523 ResolveFunction (offset, dwarf_cu, sc_list);
2524 }
2525 }
2526 die_offsets.clear();
2527 }
2528
Jim Inghamea8005a2011-10-11 01:18:11 +00002529 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002530 {
2531 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
2532 {
2533 for (uint32_t i = 0; i < num_base; i++)
2534 {
2535 dw_offset_t offset = die_offsets[i];
2536 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (offset, &dwarf_cu);
2537 assert (die);
2538 if (!FunctionDieMatchesPartialName(die,
2539 dwarf_cu,
2540 effective_name_type_mask,
2541 name_cstr,
2542 base_name_start,
2543 base_name_end))
2544 continue;
2545
2546 // If we get to here, the die is good, and we should add it:
2547 ResolveFunction (offset, dwarf_cu, sc_list);
2548 }
2549 }
2550 die_offsets.clear();
2551 }
Greg Clayton7f995132011-10-04 22:41:51 +00002552
Jim Inghamea8005a2011-10-11 01:18:11 +00002553 if (effective_name_type_mask & eFunctionNameTypeSelector)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002554 {
Greg Clayton7f995132011-10-04 22:41:51 +00002555 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002556 }
2557
Greg Clayton4d01ace2011-09-29 16:58:15 +00002558 }
2559
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002560 // Return the number of variable that were appended to the list
2561 return sc_list.GetSize() - original_size;
2562}
2563
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002564uint32_t
2565SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list)
2566{
2567 Timer scoped_timer (__PRETTY_FUNCTION__,
2568 "SymbolFileDWARF::FindFunctions (regex = '%s')",
2569 regex.GetText());
2570
Greg Clayton21f2a492011-10-06 00:09:08 +00002571 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2572
2573 if (log)
2574 {
2575 log->Printf ("SymbolFileDWARF::FindFunctions (file=\"%s/%s\", regex=\"%s\"append=%u, sc_list)",
2576 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2577 m_obj_file->GetFileSpec().GetFilename().GetCString(),
2578 regex.GetText(), append);
2579 }
2580
2581
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002582 // If we aren't appending the results to this list, then clear the list
2583 if (!append)
2584 sc_list.Clear();
2585
2586 // Remember how many sc_list are in the list before we search in case
2587 // we are appending the results to a variable list.
2588 uint32_t original_size = sc_list.GetSize();
2589
Greg Clayton7f995132011-10-04 22:41:51 +00002590 if (m_apple_names_ap.get())
2591 {
2592 FindFunctions (regex, *m_apple_names_ap, sc_list);
2593 }
2594 else
2595 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002596 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00002597 if (!m_indexed)
2598 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002599
Greg Clayton7f995132011-10-04 22:41:51 +00002600 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002601
Greg Clayton7f995132011-10-04 22:41:51 +00002602 FindFunctions (regex, m_function_fullname_index, sc_list);
2603 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002604
2605 // Return the number of variable that were appended to the list
2606 return sc_list.GetSize() - original_size;
2607}
Jim Ingham318c9f22011-08-26 19:44:13 +00002608
Greg Claytond16e1e52011-07-12 17:06:17 +00002609void
2610SymbolFileDWARF::ReportError (const char *format, ...)
2611{
2612 ::fprintf (stderr,
2613 "error: %s/%s ",
2614 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2615 m_obj_file->GetFileSpec().GetFilename().GetCString());
2616
Greg Claytoncfebbcf2011-10-01 01:37:20 +00002617 if (m_obj_file->GetModule()->GetObjectName())
2618 ::fprintf (stderr, "(%s) ", m_obj_file->GetModule()->GetObjectName().GetCString());
2619
Greg Claytond16e1e52011-07-12 17:06:17 +00002620 va_list args;
2621 va_start (args, format);
2622 vfprintf (stderr, format, args);
2623 va_end (args);
2624}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002625
Jim Inghamc1663042011-09-29 22:12:35 +00002626void
2627SymbolFileDWARF::ReportWarning (const char *format, ...)
2628{
2629 ::fprintf (stderr,
2630 "warning: %s/%s ",
2631 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2632 m_obj_file->GetFileSpec().GetFilename().GetCString());
2633
Greg Claytoncfebbcf2011-10-01 01:37:20 +00002634 if (m_obj_file->GetModule()->GetObjectName())
2635 ::fprintf (stderr, "(%s) ", m_obj_file->GetModule()->GetObjectName().GetCString());
2636
Jim Inghamc1663042011-09-29 22:12:35 +00002637 va_list args;
2638 va_start (args, format);
2639 vfprintf (stderr, format, args);
2640 va_end (args);
2641}
2642
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002643uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002644SymbolFileDWARF::FindTypes(const SymbolContext& sc, const ConstString &name, bool append, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002645{
Greg Claytonc685f8e2010-09-15 04:15:46 +00002646 DWARFDebugInfo* info = DebugInfo();
2647 if (info == NULL)
2648 return 0;
2649
Greg Clayton21f2a492011-10-06 00:09:08 +00002650 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2651
2652 if (log)
2653 {
2654 log->Printf ("SymbolFileDWARF::FindFunctions (file=\"%s/%s\", sc, name=\"%s\", append=%u, max_matches=%u, type_list)",
2655 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2656 m_obj_file->GetFileSpec().GetFilename().GetCString(),
2657 name.GetCString(), append, max_matches);
2658 }
2659
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002660 // If we aren't appending the results to this list, then clear the list
2661 if (!append)
2662 types.Clear();
2663
Greg Claytond4a2b372011-09-12 23:21:58 +00002664 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002665
2666 if (m_apple_types_ap.get())
2667 {
2668 const char *name_cstr = name.GetCString();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002669 m_apple_types_ap->FindByName (name_cstr, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002670 }
2671 else
2672 {
2673 if (!m_indexed)
2674 Index ();
2675
2676 m_type_index.Find (name, die_offsets);
2677 }
2678
2679
2680 const size_t num_matches = die_offsets.size();
2681
Greg Claytond4a2b372011-09-12 23:21:58 +00002682 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002683 {
Greg Clayton7f995132011-10-04 22:41:51 +00002684 const uint32_t initial_types_size = types.GetSize();
2685 DWARFCompileUnit* dwarf_cu = NULL;
2686 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002687 DWARFDebugInfo* debug_info = DebugInfo();
2688 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002689 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002690 const dw_offset_t die_offset = die_offsets[i];
2691 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2692
2693 Type *matching_type = ResolveType (dwarf_cu, die);
2694 if (matching_type)
Greg Clayton73bf5db2011-06-17 01:22:15 +00002695 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002696 // We found a type pointer, now find the shared pointer form our type list
2697 TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID()));
2698 if (type_sp)
2699 {
2700 types.InsertUnique (type_sp);
2701 if (types.GetSize() >= max_matches)
2702 break;
2703 }
2704 else
2705 {
Jim Inghamc1663042011-09-29 22:12:35 +00002706 ReportError ("error: can't find shared pointer for type 0x%8.8x.\n", matching_type->GetID());
Greg Claytond4a2b372011-09-12 23:21:58 +00002707 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00002708 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002709 }
Greg Clayton7f995132011-10-04 22:41:51 +00002710 return types.GetSize() - initial_types_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002711 }
Greg Clayton7f995132011-10-04 22:41:51 +00002712 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002713}
2714
2715
Greg Clayton526e5af2010-11-13 03:52:47 +00002716ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00002717SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
2718 const ConstString &name)
2719{
Greg Clayton21f2a492011-10-06 00:09:08 +00002720 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2721
2722 if (log)
2723 {
2724 log->Printf ("SymbolFileDWARF::FindNamespace (file=\"%s/%s\", sc, name=\"%s\")",
2725 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2726 m_obj_file->GetFileSpec().GetFilename().GetCString(),
2727 name.GetCString());
2728 }
2729
Greg Clayton526e5af2010-11-13 03:52:47 +00002730 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00002731 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00002732 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00002733 {
Greg Clayton7f995132011-10-04 22:41:51 +00002734 DIEArray die_offsets;
2735
Greg Clayton526e5af2010-11-13 03:52:47 +00002736 // Index if we already haven't to make sure the compile units
2737 // get indexed and make their global DIE index list
Greg Clayton7f995132011-10-04 22:41:51 +00002738 if (m_apple_namespaces_ap.get())
2739 {
2740 const char *name_cstr = name.GetCString();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002741 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002742 }
2743 else
2744 {
2745 if (!m_indexed)
2746 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00002747
Greg Clayton7f995132011-10-04 22:41:51 +00002748 m_namespace_index.Find (name, die_offsets);
2749 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002750
2751 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00002752 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002753 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002754 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00002755 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002756 DWARFDebugInfo* debug_info = DebugInfo();
2757 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00002758 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002759 const dw_offset_t die_offset = die_offsets[i];
2760 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2761
2762 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
2763 if (clang_namespace_decl)
2764 {
2765 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
2766 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
2767 }
Greg Clayton526e5af2010-11-13 03:52:47 +00002768 }
2769 }
Greg Clayton96d7d742010-11-10 23:42:09 +00002770 }
Greg Clayton526e5af2010-11-13 03:52:47 +00002771 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00002772}
2773
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002774uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002775SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002776{
2777 // Remember how many sc_list are in the list before we search in case
2778 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002779 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002780
2781 const uint32_t num_die_offsets = die_offsets.size();
2782 // Parse all of the types we found from the pubtypes matches
2783 uint32_t i;
2784 uint32_t num_matches = 0;
2785 for (i = 0; i < num_die_offsets; ++i)
2786 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002787 Type *matching_type = ResolveTypeUID (die_offsets[i]);
2788 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002789 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002790 // We found a type pointer, now find the shared pointer form our type list
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002791 TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID()));
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002792 assert (type_sp.get() != NULL);
2793 types.InsertUnique (type_sp);
2794 ++num_matches;
2795 if (num_matches >= max_matches)
2796 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002797 }
2798 }
2799
2800 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002801 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002802}
2803
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002804
2805size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00002806SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
2807 clang::DeclContext *containing_decl_ctx,
2808 TypeSP& type_sp,
2809 DWARFCompileUnit* dwarf_cu,
2810 const DWARFDebugInfoEntry *parent_die,
2811 bool skip_artificial,
2812 bool &is_static,
2813 TypeList* type_list,
2814 std::vector<clang_type_t>& function_param_types,
2815 std::vector<clang::ParmVarDecl*>& function_param_decls,
2816 unsigned &type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002817{
2818 if (parent_die == NULL)
2819 return 0;
2820
Greg Claytond88d7592010-09-15 08:33:30 +00002821 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
2822
Greg Clayton7fedea22010-11-16 02:10:54 +00002823 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002824 const DWARFDebugInfoEntry *die;
2825 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2826 {
2827 dw_tag_t tag = die->Tag();
2828 switch (tag)
2829 {
2830 case DW_TAG_formal_parameter:
2831 {
2832 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00002833 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002834 if (num_attributes > 0)
2835 {
2836 const char *name = NULL;
2837 Declaration decl;
2838 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00002839 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002840 // one of None, Auto, Register, Extern, Static, PrivateExtern
2841
Sean Callanane2ef6e32010-09-23 03:01:22 +00002842 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002843 uint32_t i;
2844 for (i=0; i<num_attributes; ++i)
2845 {
2846 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2847 DWARFFormValue form_value;
2848 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2849 {
2850 switch (attr)
2851 {
2852 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2853 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2854 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2855 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
2856 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00002857 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002858 case DW_AT_location:
2859 // if (form_value.BlockData())
2860 // {
2861 // const DataExtractor& debug_info_data = debug_info();
2862 // uint32_t block_length = form_value.Unsigned();
2863 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
2864 // }
2865 // else
2866 // {
2867 // }
2868 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002869 case DW_AT_const_value:
2870 case DW_AT_default_value:
2871 case DW_AT_description:
2872 case DW_AT_endianity:
2873 case DW_AT_is_optional:
2874 case DW_AT_segment:
2875 case DW_AT_variable_parameter:
2876 default:
2877 case DW_AT_abstract_origin:
2878 case DW_AT_sibling:
2879 break;
2880 }
2881 }
2882 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00002883
Greg Clayton0fffff52010-09-24 05:15:53 +00002884 bool skip = false;
2885 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002886 {
Greg Clayton0fffff52010-09-24 05:15:53 +00002887 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00002888 {
2889 // In order to determine if a C++ member function is
2890 // "const" we have to look at the const-ness of "this"...
2891 // Ugly, but that
2892 if (arg_idx == 0)
2893 {
Greg Clayton5113dc82011-08-12 06:47:54 +00002894 if (containing_decl_ctx->getDeclKind() == clang::Decl::CXXRecord)
Sean Callanan763d72a2011-08-02 22:21:50 +00002895 {
Greg Clayton5113dc82011-08-12 06:47:54 +00002896 // Often times compilers omit the "this" name for the
2897 // specification DIEs, so we can't rely upon the name
2898 // being in the formal parameter DIE...
2899 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00002900 {
Greg Clayton5113dc82011-08-12 06:47:54 +00002901 Type *this_type = ResolveTypeUID (param_type_die_offset);
2902 if (this_type)
2903 {
2904 uint32_t encoding_mask = this_type->GetEncodingMask();
2905 if (encoding_mask & Type::eEncodingIsPointerUID)
2906 {
2907 is_static = false;
2908
2909 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
2910 type_quals |= clang::Qualifiers::Const;
2911 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
2912 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00002913 }
2914 }
2915 }
2916 }
2917 }
Greg Clayton0fffff52010-09-24 05:15:53 +00002918 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00002919 }
Greg Clayton0fffff52010-09-24 05:15:53 +00002920 else
2921 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002922
Greg Clayton0fffff52010-09-24 05:15:53 +00002923 // HACK: Objective C formal parameters "self" and "_cmd"
2924 // are not marked as artificial in the DWARF...
Greg Clayton96d7d742010-11-10 23:42:09 +00002925 CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2926 if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Clayton0fffff52010-09-24 05:15:53 +00002927 {
2928 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
2929 skip = true;
2930 }
2931 }
2932 }
2933
2934 if (!skip)
2935 {
2936 Type *type = ResolveTypeUID(param_type_die_offset);
2937 if (type)
2938 {
Greg Claytonc93237c2010-10-01 20:48:32 +00002939 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00002940
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002941 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name, type->GetClangForwardType(), storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00002942 assert(param_var_decl);
2943 function_param_decls.push_back(param_var_decl);
2944 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002945 }
2946 }
Greg Clayton7fedea22010-11-16 02:10:54 +00002947 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002948 }
2949 break;
2950
2951 default:
2952 break;
2953 }
2954 }
Greg Clayton7fedea22010-11-16 02:10:54 +00002955 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002956}
2957
2958size_t
2959SymbolFileDWARF::ParseChildEnumerators
2960(
2961 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002962 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002963 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002964 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002965 const DWARFDebugInfoEntry *parent_die
2966)
2967{
2968 if (parent_die == NULL)
2969 return 0;
2970
2971 size_t enumerators_added = 0;
2972 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00002973 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
2974
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002975 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2976 {
2977 const dw_tag_t tag = die->Tag();
2978 if (tag == DW_TAG_enumerator)
2979 {
2980 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00002981 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002982 if (num_child_attributes > 0)
2983 {
2984 const char *name = NULL;
2985 bool got_value = false;
2986 int64_t enum_value = 0;
2987 Declaration decl;
2988
2989 uint32_t i;
2990 for (i=0; i<num_child_attributes; ++i)
2991 {
2992 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2993 DWARFFormValue form_value;
2994 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2995 {
2996 switch (attr)
2997 {
2998 case DW_AT_const_value:
2999 got_value = true;
3000 enum_value = form_value.Unsigned();
3001 break;
3002
3003 case DW_AT_name:
3004 name = form_value.AsCString(&get_debug_str_data());
3005 break;
3006
3007 case DW_AT_description:
3008 default:
3009 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3010 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3011 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3012 case DW_AT_sibling:
3013 break;
3014 }
3015 }
3016 }
3017
3018 if (name && name[0] && got_value)
3019 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003020 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3021 enumerator_clang_type,
3022 decl,
3023 name,
3024 enum_value,
3025 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003026 ++enumerators_added;
3027 }
3028 }
3029 }
3030 }
3031 return enumerators_added;
3032}
3033
3034void
3035SymbolFileDWARF::ParseChildArrayInfo
3036(
3037 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003038 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003039 const DWARFDebugInfoEntry *parent_die,
3040 int64_t& first_index,
3041 std::vector<uint64_t>& element_orders,
3042 uint32_t& byte_stride,
3043 uint32_t& bit_stride
3044)
3045{
3046 if (parent_die == NULL)
3047 return;
3048
3049 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003050 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003051 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3052 {
3053 const dw_tag_t tag = die->Tag();
3054 switch (tag)
3055 {
3056 case DW_TAG_enumerator:
3057 {
3058 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003059 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003060 if (num_child_attributes > 0)
3061 {
3062 const char *name = NULL;
3063 bool got_value = false;
3064 int64_t enum_value = 0;
3065
3066 uint32_t i;
3067 for (i=0; i<num_child_attributes; ++i)
3068 {
3069 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3070 DWARFFormValue form_value;
3071 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3072 {
3073 switch (attr)
3074 {
3075 case DW_AT_const_value:
3076 got_value = true;
3077 enum_value = form_value.Unsigned();
3078 break;
3079
3080 case DW_AT_name:
3081 name = form_value.AsCString(&get_debug_str_data());
3082 break;
3083
3084 case DW_AT_description:
3085 default:
3086 case DW_AT_decl_file:
3087 case DW_AT_decl_line:
3088 case DW_AT_decl_column:
3089 case DW_AT_sibling:
3090 break;
3091 }
3092 }
3093 }
3094 }
3095 }
3096 break;
3097
3098 case DW_TAG_subrange_type:
3099 {
3100 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003101 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003102 if (num_child_attributes > 0)
3103 {
3104 const char *name = NULL;
3105 bool got_value = false;
3106 uint64_t byte_size = 0;
3107 int64_t enum_value = 0;
3108 uint64_t num_elements = 0;
3109 uint64_t lower_bound = 0;
3110 uint64_t upper_bound = 0;
3111 uint32_t i;
3112 for (i=0; i<num_child_attributes; ++i)
3113 {
3114 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3115 DWARFFormValue form_value;
3116 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3117 {
3118 switch (attr)
3119 {
3120 case DW_AT_const_value:
3121 got_value = true;
3122 enum_value = form_value.Unsigned();
3123 break;
3124
3125 case DW_AT_name:
3126 name = form_value.AsCString(&get_debug_str_data());
3127 break;
3128
3129 case DW_AT_count:
3130 num_elements = form_value.Unsigned();
3131 break;
3132
3133 case DW_AT_bit_stride:
3134 bit_stride = form_value.Unsigned();
3135 break;
3136
3137 case DW_AT_byte_stride:
3138 byte_stride = form_value.Unsigned();
3139 break;
3140
3141 case DW_AT_byte_size:
3142 byte_size = form_value.Unsigned();
3143 break;
3144
3145 case DW_AT_lower_bound:
3146 lower_bound = form_value.Unsigned();
3147 break;
3148
3149 case DW_AT_upper_bound:
3150 upper_bound = form_value.Unsigned();
3151 break;
3152
3153 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003154 case DW_AT_abstract_origin:
3155 case DW_AT_accessibility:
3156 case DW_AT_allocated:
3157 case DW_AT_associated:
3158 case DW_AT_data_location:
3159 case DW_AT_declaration:
3160 case DW_AT_description:
3161 case DW_AT_sibling:
3162 case DW_AT_threads_scaled:
3163 case DW_AT_type:
3164 case DW_AT_visibility:
3165 break;
3166 }
3167 }
3168 }
3169
3170 if (upper_bound > lower_bound)
3171 num_elements = upper_bound - lower_bound + 1;
3172
3173 if (num_elements > 0)
3174 element_orders.push_back (num_elements);
3175 }
3176 }
3177 break;
3178 }
3179 }
3180}
3181
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003182TypeSP
Greg Clayton96d7d742010-11-10 23:42:09 +00003183SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003184{
3185 TypeSP type_sp;
3186 if (die != NULL)
3187 {
Greg Clayton96d7d742010-11-10 23:42:09 +00003188 assert(curr_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00003189 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003190 if (type_ptr == NULL)
3191 {
Greg Claytonca512b32011-01-14 04:54:56 +00003192 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu);
3193 assert (lldb_cu);
3194 SymbolContext sc(lldb_cu);
Greg Clayton96d7d742010-11-10 23:42:09 +00003195 type_sp = ParseType(sc, curr_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003196 }
3197 else if (type_ptr != DIE_IS_BEING_PARSED)
3198 {
3199 // Grab the existing type from the master types lists
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003200 type_sp = GetTypeList()->FindType(type_ptr->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003201 }
3202
3203 }
3204 return type_sp;
3205}
3206
3207clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00003208SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003209{
3210 if (die_offset != DW_INVALID_OFFSET)
3211 {
3212 DWARFCompileUnitSP cu_sp;
3213 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Sean Callanan72e49402011-08-05 23:43:37 +00003214 return GetClangDeclContextContainingDIE (cu_sp.get(), die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003215 }
3216 return NULL;
3217}
3218
Sean Callanan72e49402011-08-05 23:43:37 +00003219clang::DeclContext *
3220SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
3221{
3222 if (die_offset != DW_INVALID_OFFSET)
3223 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00003224 DWARFDebugInfo* debug_info = DebugInfo();
3225 if (debug_info)
3226 {
3227 DWARFCompileUnitSP cu_sp;
3228 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
3229 if (die)
3230 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
3231 }
Sean Callanan72e49402011-08-05 23:43:37 +00003232 }
3233 return NULL;
3234}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003235
Greg Clayton96d7d742010-11-10 23:42:09 +00003236clang::NamespaceDecl *
3237SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
3238{
3239 if (die->Tag() == DW_TAG_namespace)
3240 {
3241 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
3242 if (namespace_name)
3243 {
3244 Declaration decl; // TODO: fill in the decl object
Sean Callanan72e49402011-08-05 23:43:37 +00003245 clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextContainingDIE (curr_cu, die->GetParent()));
Greg Clayton96d7d742010-11-10 23:42:09 +00003246 if (namespace_decl)
Greg Claytona2721472011-06-25 00:44:06 +00003247 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
Greg Clayton96d7d742010-11-10 23:42:09 +00003248 return namespace_decl;
3249 }
3250 }
3251 return NULL;
3252}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003253
3254clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00003255SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
3256{
Greg Clayton5cf58b92011-10-05 22:22:08 +00003257 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3258 if (clang_decl_ctx)
3259 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00003260 // If this DIE has a specification, or an abstract origin, then trace to those.
3261
3262 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_specification, DW_INVALID_OFFSET);
3263 if (die_offset != DW_INVALID_OFFSET)
3264 return GetClangDeclContextForDIEOffset (sc, die_offset);
3265
3266 die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
3267 if (die_offset != DW_INVALID_OFFSET)
3268 return GetClangDeclContextForDIEOffset (sc, die_offset);
3269
3270 // This is the DIE we want. Parse it, then query our map.
3271
3272 ParseType(sc, curr_cu, die, NULL);
3273
Greg Clayton5cf58b92011-10-05 22:22:08 +00003274 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3275
3276 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00003277}
3278
3279clang::DeclContext *
Greg Clayton2bc22f82011-09-30 03:20:47 +00003280SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003281{
Greg Claytonca512b32011-01-14 04:54:56 +00003282 if (m_clang_tu_decl == NULL)
3283 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003284
Greg Clayton2bc22f82011-09-30 03:20:47 +00003285 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
3286
3287 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003288 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003289 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
3290 if (pos != m_die_to_decl_ctx.end())
3291 return pos->second;
3292
3293 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003294 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003295 case DW_TAG_compile_unit:
3296 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003297
Greg Clayton2bc22f82011-09-30 03:20:47 +00003298 case DW_TAG_namespace:
Greg Claytonca512b32011-01-14 04:54:56 +00003299 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003300 const char *namespace_name = decl_ctx_die->GetAttributeValueAsString(this, cu, DW_AT_name, NULL);
3301 if (namespace_name)
Greg Claytonca512b32011-01-14 04:54:56 +00003302 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003303 Declaration decl; // TODO: fill in the decl object
3304 clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextContainingDIE (cu, decl_ctx_die));
3305 if (namespace_decl)
3306 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, decl_ctx_die);
3307 return namespace_decl;
3308 }
3309 }
3310 break;
3311
3312 case DW_TAG_structure_type:
3313 case DW_TAG_union_type:
3314 case DW_TAG_class_type:
3315 {
3316 Type* type = ResolveType (cu, decl_ctx_die);
3317 if (type)
3318 {
3319 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
3320 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00003321 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003322 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
3323 if (decl_ctx)
3324 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00003325 }
3326 }
Greg Claytonca512b32011-01-14 04:54:56 +00003327 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00003328 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003329
Greg Clayton2bc22f82011-09-30 03:20:47 +00003330 default:
3331 break;
Greg Claytonca512b32011-01-14 04:54:56 +00003332 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003333 }
Greg Clayton7a345282010-11-09 23:46:37 +00003334 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003335}
3336
Greg Clayton2bc22f82011-09-30 03:20:47 +00003337
3338const DWARFDebugInfoEntry *
3339SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
3340{
3341 if (cu && die)
3342 {
3343 const DWARFDebugInfoEntry * const decl_die = die;
3344
3345 while (die != NULL)
3346 {
3347 // If this is the original DIE that we are searching for a declaration
3348 // for, then don't look in the cache as we don't want our own decl
3349 // context to be our decl context...
3350 if (decl_die != die)
3351 {
3352 switch (die->Tag())
3353 {
3354 case DW_TAG_compile_unit:
3355 case DW_TAG_namespace:
3356 case DW_TAG_structure_type:
3357 case DW_TAG_union_type:
3358 case DW_TAG_class_type:
3359 return die;
3360
3361 default:
3362 break;
3363 }
3364 }
3365
3366 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
3367 if (die_offset != DW_INVALID_OFFSET)
3368 {
3369 DWARFCompileUnit *spec_cu = cu;
3370 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
3371 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
3372 if (spec_die_decl_ctx_die)
3373 return spec_die_decl_ctx_die;
3374 }
3375
3376 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
3377 if (die_offset != DW_INVALID_OFFSET)
3378 {
3379 DWARFCompileUnit *abs_cu = cu;
3380 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
3381 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
3382 if (abs_die_decl_ctx_die)
3383 return abs_die_decl_ctx_die;
3384 }
3385
3386 die = die->GetParent();
3387 }
3388 }
3389 return NULL;
3390}
3391
3392
3393
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003394// This function can be used when a DIE is found that is a forward declaration
3395// DIE and we want to try and find a type that has the complete definition.
3396TypeSP
Greg Clayton7f995132011-10-04 22:41:51 +00003397SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
3398 const DWARFDebugInfoEntry *die,
3399 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003400{
3401 TypeSP type_sp;
3402
Greg Clayton1a65ae12011-01-25 23:55:37 +00003403 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003404 return type_sp;
3405
Greg Clayton7f995132011-10-04 22:41:51 +00003406 DIEArray die_offsets;
3407
3408 if (m_apple_types_ap.get())
3409 {
3410 const char *name_cstr = type_name.GetCString();
Jim Ingham4cda6e02011-10-07 22:23:45 +00003411 m_apple_types_ap->FindByName (name_cstr, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00003412 }
3413 else
3414 {
3415 if (!m_indexed)
3416 Index ();
3417
3418 m_type_index.Find (type_name, die_offsets);
3419 }
3420
3421
3422 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00003423
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003424 const dw_tag_t type_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00003425
3426 DWARFCompileUnit* type_cu = NULL;
3427 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003428 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003429 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003430 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003431 for (size_t i=0; i<num_matches; ++i)
3432 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003433 const dw_offset_t die_offset = die_offsets[i];
3434 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003435
3436 if (type_die != die && type_die->Tag() == type_tag)
3437 {
3438 // Hold off on comparing parent DIE tags until
3439 // we know what happens with stuff in namespaces
3440 // for gcc and clang...
3441 //DWARFDebugInfoEntry *parent_die = die->GetParent();
3442 //DWARFDebugInfoEntry *parent_type_die = type_die->GetParent();
3443 //if (parent_die->Tag() == parent_type_die->Tag())
3444 {
3445 Type *resolved_type = ResolveType (type_cu, type_die, false);
3446 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3447 {
3448 DEBUG_PRINTF ("resolved 0x%8.8x (cu 0x%8.8x) from %s to 0x%8.8x (cu 0x%8.8x)\n",
3449 die->GetOffset(),
Greg Clayton96d7d742010-11-10 23:42:09 +00003450 curr_cu->GetOffset(),
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003451 m_obj_file->GetFileSpec().GetFilename().AsCString(),
3452 type_die->GetOffset(),
3453 type_cu->GetOffset());
3454
3455 m_die_to_type[die] = resolved_type;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003456 type_sp = GetTypeList()->FindType(resolved_type->GetID());
Greg Clayton40328bf2010-11-08 02:05:08 +00003457 if (!type_sp)
3458 {
3459 DEBUG_PRINTF("unable to resolve type '%s' from DIE 0x%8.8x\n", type_name.GetCString(), die->GetOffset());
3460 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003461 break;
3462 }
3463 }
3464 }
3465 }
3466 }
3467 return type_sp;
3468}
3469
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003470TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00003471SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003472{
3473 TypeSP type_sp;
3474
Greg Clayton1be10fc2010-09-29 01:12:09 +00003475 if (type_is_new_ptr)
3476 *type_is_new_ptr = false;
3477
Sean Callananc7fbf732010-08-06 00:32:49 +00003478 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003479 if (die != NULL)
3480 {
Greg Clayton21f2a492011-10-06 00:09:08 +00003481 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Jim Ingham16746d12011-08-25 23:21:43 +00003482 if (log && dwarf_cu)
3483 {
Jim Ingham318c9f22011-08-26 19:44:13 +00003484 StreamString s;
Jim Inghamd3d25d92011-08-27 01:24:54 +00003485 die->DumpLocation (this, dwarf_cu, s);
Jim Ingham318c9f22011-08-26 19:44:13 +00003486 log->Printf ("SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
3487
Jim Ingham16746d12011-08-25 23:21:43 +00003488 }
3489
Greg Clayton594e5ed2010-09-27 21:07:38 +00003490 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003491 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00003492 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003493 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003494 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00003495 if (type_is_new_ptr)
3496 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003497
Greg Clayton594e5ed2010-09-27 21:07:38 +00003498 const dw_tag_t tag = die->Tag();
3499
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003500 bool is_forward_declaration = false;
3501 DWARFDebugInfoEntry::Attributes attributes;
3502 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00003503 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00003504 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
3505 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00003506 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00003507 Declaration decl;
3508
Greg Clayton4957bf62010-09-30 21:49:03 +00003509 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00003510 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003511
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003512 dw_attr_t attr;
3513
3514 switch (tag)
3515 {
3516 case DW_TAG_base_type:
3517 case DW_TAG_pointer_type:
3518 case DW_TAG_reference_type:
3519 case DW_TAG_typedef:
3520 case DW_TAG_const_type:
3521 case DW_TAG_restrict_type:
3522 case DW_TAG_volatile_type:
3523 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003524 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00003525 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003526
Greg Claytond88d7592010-09-15 08:33:30 +00003527 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003528 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003529 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
3530
3531 if (num_attributes > 0)
3532 {
3533 uint32_t i;
3534 for (i=0; i<num_attributes; ++i)
3535 {
3536 attr = attributes.AttributeAtIndex(i);
3537 DWARFFormValue form_value;
3538 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3539 {
3540 switch (attr)
3541 {
3542 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3543 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3544 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3545 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00003546
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003547 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00003548 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
3549 // include the "&"...
3550 if (tag == DW_TAG_reference_type)
3551 {
3552 if (strchr (type_name_cstr, '&') == NULL)
3553 type_name_cstr = NULL;
3554 }
3555 if (type_name_cstr)
3556 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003557 break;
Greg Clayton36909642011-03-15 04:38:20 +00003558 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003559 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
3560 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
3561 default:
3562 case DW_AT_sibling:
3563 break;
3564 }
3565 }
3566 }
3567 }
3568
Greg Claytonc93237c2010-10-01 20:48:32 +00003569 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\") type => 0x%8.8x\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
3570
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003571 switch (tag)
3572 {
3573 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00003574 break;
3575
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003576 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00003577 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003578 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
3579 encoding,
3580 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003581 break;
3582
Greg Clayton526e5af2010-11-13 03:52:47 +00003583 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
3584 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
3585 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
3586 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
3587 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
3588 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003589 }
3590
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003591 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
3592 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
3593 {
3594 static ConstString g_objc_type_name_id("id");
3595 static ConstString g_objc_type_name_Class("Class");
3596 static ConstString g_objc_type_name_selector("SEL");
3597
Greg Clayton24739922010-10-13 03:15:28 +00003598 if (type_name_const_str == g_objc_type_name_id)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003599 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003600 clang_type = ast.GetBuiltInType_objc_id();
Greg Clayton526e5af2010-11-13 03:52:47 +00003601 resolve_state = Type::eResolveStateFull;
3602
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003603 }
Greg Clayton24739922010-10-13 03:15:28 +00003604 else if (type_name_const_str == g_objc_type_name_Class)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003605 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003606 clang_type = ast.GetBuiltInType_objc_Class();
Greg Clayton526e5af2010-11-13 03:52:47 +00003607 resolve_state = Type::eResolveStateFull;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003608 }
Greg Clayton24739922010-10-13 03:15:28 +00003609 else if (type_name_const_str == g_objc_type_name_selector)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003610 {
Sean Callananf6c73082010-12-06 23:53:20 +00003611 clang_type = ast.GetBuiltInType_objc_selector();
Greg Clayton526e5af2010-11-13 03:52:47 +00003612 resolve_state = Type::eResolveStateFull;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003613 }
3614 }
3615
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003616 type_sp.reset( new Type (die->GetOffset(),
3617 this,
3618 type_name_const_str,
3619 byte_size,
3620 NULL,
3621 encoding_uid,
3622 encoding_data_type,
3623 &decl,
3624 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00003625 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003626
Greg Clayton594e5ed2010-09-27 21:07:38 +00003627 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003628
3629// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
3630// if (encoding_type != NULL)
3631// {
3632// if (encoding_type != DIE_IS_BEING_PARSED)
3633// type_sp->SetEncodingType(encoding_type);
3634// else
3635// m_indirect_fixups.push_back(type_sp.get());
3636// }
3637 }
3638 break;
3639
3640 case DW_TAG_structure_type:
3641 case DW_TAG_union_type:
3642 case DW_TAG_class_type:
3643 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003644 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00003645 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003646
Greg Clayton9e409562010-07-28 02:04:09 +00003647 LanguageType class_language = eLanguageTypeUnknown;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003648 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00003649 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003650 if (num_attributes > 0)
3651 {
3652 uint32_t i;
3653 for (i=0; i<num_attributes; ++i)
3654 {
3655 attr = attributes.AttributeAtIndex(i);
3656 DWARFFormValue form_value;
3657 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3658 {
3659 switch (attr)
3660 {
Greg Clayton9e409562010-07-28 02:04:09 +00003661 case DW_AT_decl_file:
3662 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
3663 break;
3664
3665 case DW_AT_decl_line:
3666 decl.SetLine(form_value.Unsigned());
3667 break;
3668
3669 case DW_AT_decl_column:
3670 decl.SetColumn(form_value.Unsigned());
3671 break;
3672
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003673 case DW_AT_name:
3674 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00003675 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003676 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003677
3678 case DW_AT_byte_size:
3679 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00003680 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00003681 break;
3682
3683 case DW_AT_accessibility:
3684 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
3685 break;
3686
3687 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00003688 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00003689 break;
3690
3691 case DW_AT_APPLE_runtime_class:
3692 class_language = (LanguageType)form_value.Signed();
3693 break;
3694
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003695 case DW_AT_allocated:
3696 case DW_AT_associated:
3697 case DW_AT_data_location:
3698 case DW_AT_description:
3699 case DW_AT_start_scope:
3700 case DW_AT_visibility:
3701 default:
3702 case DW_AT_sibling:
3703 break;
3704 }
3705 }
3706 }
3707 }
3708
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003709 UniqueDWARFASTType unique_ast_entry;
3710 if (decl.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003711 {
Greg Claytone576ab22011-02-15 00:19:15 +00003712 if (GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Greg Clayton36909642011-03-15 04:38:20 +00003713 this,
3714 dwarf_cu,
Greg Claytone576ab22011-02-15 00:19:15 +00003715 die,
3716 decl,
Greg Clayton36909642011-03-15 04:38:20 +00003717 byte_size_valid ? byte_size : -1,
Greg Claytone576ab22011-02-15 00:19:15 +00003718 unique_ast_entry))
Greg Claytonc615ce42010-11-09 04:42:43 +00003719 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003720 // We have already parsed this type or from another
3721 // compile unit. GCC loves to use the "one definition
3722 // rule" which can result in multiple definitions
3723 // of the same class over and over in each compile
3724 // unit.
3725 type_sp = unique_ast_entry.m_type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00003726 if (type_sp)
3727 {
Greg Clayton4272cc72011-02-02 02:24:04 +00003728 m_die_to_type[die] = type_sp.get();
3729 return type_sp;
3730 }
3731 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003732 }
3733
3734 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3735
3736 int tag_decl_kind = -1;
3737 AccessType default_accessibility = eAccessNone;
3738 if (tag == DW_TAG_structure_type)
3739 {
3740 tag_decl_kind = clang::TTK_Struct;
3741 default_accessibility = eAccessPublic;
3742 }
3743 else if (tag == DW_TAG_union_type)
3744 {
3745 tag_decl_kind = clang::TTK_Union;
3746 default_accessibility = eAccessPublic;
3747 }
3748 else if (tag == DW_TAG_class_type)
3749 {
3750 tag_decl_kind = clang::TTK_Class;
3751 default_accessibility = eAccessPrivate;
3752 }
3753
3754
3755 if (is_forward_declaration)
3756 {
3757 // We have a forward declaration to a type and we need
3758 // to try and find a full declaration. We look in the
3759 // current type index just in case we have a forward
3760 // declaration followed by an actual declarations in the
3761 // DWARF. If this fails, we need to look elsewhere...
3762
3763 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
3764
3765 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00003766 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003767 // We weren't able to find a full declaration in
3768 // this DWARF, see if we have a declaration anywhere
3769 // else...
3770 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
Greg Clayton4272cc72011-02-02 02:24:04 +00003771 }
3772
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003773 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00003774 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003775 // We found a real definition for this type elsewhere
3776 // so lets use it and cache the fact that we found
3777 // a complete type for this die
3778 m_die_to_type[die] = type_sp.get();
3779 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00003780 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003781 }
3782 assert (tag_decl_kind != -1);
3783 bool clang_type_was_created = false;
3784 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
3785 if (clang_type == NULL)
3786 {
3787 clang_type_was_created = true;
3788 clang_type = ast.CreateRecordType (type_name_cstr,
3789 tag_decl_kind,
Sean Callanan72e49402011-08-05 23:43:37 +00003790 GetClangDeclContextContainingDIE (dwarf_cu, die),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003791 class_language);
3792 }
3793
3794 // Store a forward declaration to this class type in case any
3795 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00003796 // types for function prototypes.
3797 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003798 type_sp.reset (new Type (die->GetOffset(),
3799 this,
3800 type_name_const_str,
3801 byte_size,
3802 NULL,
3803 LLDB_INVALID_UID,
3804 Type::eEncodingIsUID,
3805 &decl,
3806 clang_type,
3807 Type::eResolveStateForward));
3808
3809
3810 // Add our type to the unique type map so we don't
3811 // end up creating many copies of the same type over
3812 // and over in the ASTContext for our module
3813 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00003814 unique_ast_entry.m_symfile = this;
3815 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003816 unique_ast_entry.m_die = die;
3817 unique_ast_entry.m_declaration = decl;
Greg Claytone576ab22011-02-15 00:19:15 +00003818 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
3819 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003820
3821 if (die->HasChildren() == false && is_forward_declaration == false)
3822 {
3823 // No children for this struct/union/class, lets finish it
3824 ast.StartTagDeclarationDefinition (clang_type);
3825 ast.CompleteTagDeclarationDefinition (clang_type);
3826 }
3827 else if (clang_type_was_created)
3828 {
3829 // Leave this as a forward declaration until we need
3830 // to know the details of the type. lldb_private::Type
3831 // will automatically call the SymbolFile virtual function
3832 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
3833 // When the definition needs to be defined.
3834 m_forward_decl_die_to_clang_type[die] = clang_type;
3835 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
3836 ClangASTContext::SetHasExternalStorage (clang_type, true);
Greg Claytonc615ce42010-11-09 04:42:43 +00003837 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003838 }
3839 break;
3840
3841 case DW_TAG_enumeration_type:
3842 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003843 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00003844 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003845
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003846 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003847
Greg Claytond88d7592010-09-15 08:33:30 +00003848 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003849 if (num_attributes > 0)
3850 {
3851 uint32_t i;
3852
3853 for (i=0; i<num_attributes; ++i)
3854 {
3855 attr = attributes.AttributeAtIndex(i);
3856 DWARFFormValue form_value;
3857 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3858 {
3859 switch (attr)
3860 {
Greg Clayton7a345282010-11-09 23:46:37 +00003861 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3862 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3863 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003864 case DW_AT_name:
3865 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00003866 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003867 break;
Greg Clayton7a345282010-11-09 23:46:37 +00003868 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00003869 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00003870 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3871 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003872 case DW_AT_allocated:
3873 case DW_AT_associated:
3874 case DW_AT_bit_stride:
3875 case DW_AT_byte_stride:
3876 case DW_AT_data_location:
3877 case DW_AT_description:
3878 case DW_AT_start_scope:
3879 case DW_AT_visibility:
3880 case DW_AT_specification:
3881 case DW_AT_abstract_origin:
3882 case DW_AT_sibling:
3883 break;
3884 }
3885 }
3886 }
3887
Greg Claytonc93237c2010-10-01 20:48:32 +00003888 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3889
Greg Clayton1be10fc2010-09-29 01:12:09 +00003890 clang_type_t enumerator_clang_type = NULL;
3891 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
3892 if (clang_type == NULL)
3893 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003894 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
3895 DW_ATE_signed,
3896 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00003897 clang_type = ast.CreateEnumerationType (type_name_cstr,
Sean Callanan72e49402011-08-05 23:43:37 +00003898 GetClangDeclContextContainingDIE (dwarf_cu, die),
Greg Claytonca512b32011-01-14 04:54:56 +00003899 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003900 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00003901 }
3902 else
3903 {
3904 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
3905 assert (enumerator_clang_type != NULL);
3906 }
3907
Greg Claytona2721472011-06-25 00:44:06 +00003908 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
3909
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003910 type_sp.reset( new Type (die->GetOffset(),
3911 this,
3912 type_name_const_str,
3913 byte_size,
3914 NULL,
3915 encoding_uid,
3916 Type::eEncodingIsUID,
3917 &decl,
3918 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00003919 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003920
Greg Clayton6beaaa62011-01-17 03:46:26 +00003921#if LEAVE_ENUMS_FORWARD_DECLARED
Greg Clayton1be10fc2010-09-29 01:12:09 +00003922 // Leave this as a forward declaration until we need
3923 // to know the details of the type. lldb_private::Type
3924 // will automatically call the SymbolFile virtual function
3925 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
3926 // When the definition needs to be defined.
3927 m_forward_decl_die_to_clang_type[die] = clang_type;
Greg Claytonc93237c2010-10-01 20:48:32 +00003928 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003929 ClangASTContext::SetHasExternalStorage (clang_type, true);
3930#else
3931 ast.StartTagDeclarationDefinition (clang_type);
3932 if (die->HasChildren())
3933 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00003934 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
3935 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00003936 }
3937 ast.CompleteTagDeclarationDefinition (clang_type);
3938#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003939 }
3940 }
3941 break;
3942
Jim Inghamb0be4422010-08-12 01:20:14 +00003943 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003944 case DW_TAG_subprogram:
3945 case DW_TAG_subroutine_type:
3946 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003947 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00003948 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003949
3950 const char *mangled = NULL;
3951 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003952 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003953 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00003954 bool is_static = false;
3955 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00003956 bool is_explicit = false;
Greg Clayton72da3972011-08-16 18:40:23 +00003957 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
3958 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00003959
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003960 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00003961 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003962
3963
Greg Claytond88d7592010-09-15 08:33:30 +00003964 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003965 if (num_attributes > 0)
3966 {
3967 uint32_t i;
3968 for (i=0; i<num_attributes; ++i)
3969 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00003970 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003971 DWARFFormValue form_value;
3972 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3973 {
3974 switch (attr)
3975 {
3976 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3977 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3978 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3979 case DW_AT_name:
3980 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00003981 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003982 break;
3983
3984 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
3985 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00003986 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00003987 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00003988 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
3989 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00003990 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
3991
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003992 case DW_AT_external:
3993 if (form_value.Unsigned())
3994 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00003995 if (storage == clang::SC_None)
3996 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003997 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00003998 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003999 }
4000 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004001
Greg Clayton72da3972011-08-16 18:40:23 +00004002 case DW_AT_specification:
4003 specification_die_offset = form_value.Reference(dwarf_cu);
4004 break;
4005
4006 case DW_AT_abstract_origin:
4007 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
4008 break;
4009
4010
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004011 case DW_AT_allocated:
4012 case DW_AT_associated:
4013 case DW_AT_address_class:
4014 case DW_AT_artificial:
4015 case DW_AT_calling_convention:
4016 case DW_AT_data_location:
4017 case DW_AT_elemental:
4018 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004019 case DW_AT_frame_base:
4020 case DW_AT_high_pc:
4021 case DW_AT_low_pc:
4022 case DW_AT_object_pointer:
4023 case DW_AT_prototyped:
4024 case DW_AT_pure:
4025 case DW_AT_ranges:
4026 case DW_AT_recursive:
4027 case DW_AT_return_addr:
4028 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004029 case DW_AT_start_scope:
4030 case DW_AT_static_link:
4031 case DW_AT_trampoline:
4032 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004033 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004034 case DW_AT_description:
4035 case DW_AT_sibling:
4036 break;
4037 }
4038 }
4039 }
Greg Clayton24739922010-10-13 03:15:28 +00004040 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004041
Greg Clayton24739922010-10-13 03:15:28 +00004042 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00004043
Greg Clayton24739922010-10-13 03:15:28 +00004044 clang_type_t return_clang_type = NULL;
4045 Type *func_type = NULL;
4046
4047 if (type_die_offset != DW_INVALID_OFFSET)
4048 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00004049
Greg Clayton24739922010-10-13 03:15:28 +00004050 if (func_type)
Greg Clayton526e5af2010-11-13 03:52:47 +00004051 return_clang_type = func_type->GetClangLayoutType();
Greg Clayton24739922010-10-13 03:15:28 +00004052 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004053 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004054
Greg Claytonf51de672010-10-01 02:31:07 +00004055
Greg Clayton24739922010-10-13 03:15:28 +00004056 std::vector<clang_type_t> function_param_types;
4057 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004058
Greg Clayton24739922010-10-13 03:15:28 +00004059 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00004060
Greg Clayton5113dc82011-08-12 06:47:54 +00004061 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die);
4062 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
4063
4064 const bool is_cxx_method = containing_decl_kind == clang::Decl::CXXRecord;
4065 // Start off static. This will be set to false in ParseChildParameters(...)
4066 // if we find a "this" paramters as the first parameter
4067 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00004068 is_static = true;
4069
Greg Clayton24739922010-10-13 03:15:28 +00004070 if (die->HasChildren())
4071 {
Greg Clayton0fffff52010-09-24 05:15:53 +00004072 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004073 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00004074 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004075 type_sp,
4076 dwarf_cu,
4077 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00004078 skip_artificial,
4079 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004080 type_list,
4081 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00004082 function_param_decls,
4083 type_quals);
Greg Clayton24739922010-10-13 03:15:28 +00004084 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004085
Greg Clayton24739922010-10-13 03:15:28 +00004086 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004087 clang_type = ast.CreateFunctionType (return_clang_type,
4088 &function_param_types[0],
4089 function_param_types.size(),
4090 is_variadic,
4091 type_quals);
4092
Greg Clayton24739922010-10-13 03:15:28 +00004093 if (type_name_cstr)
4094 {
4095 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00004096 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004097 {
Jim Inghamb7f6b2f2011-09-08 22:13:49 +00004098 if (ObjCLanguageRuntime::IsPossibleObjCMethodName (type_name_cstr))
Greg Clayton0fffff52010-09-24 05:15:53 +00004099 {
Greg Clayton24739922010-10-13 03:15:28 +00004100 // We need to find the DW_TAG_class_type or
4101 // DW_TAG_struct_type by name so we can add this
4102 // as a member function of the class.
4103 const char *class_name_start = type_name_cstr + 2;
4104 const char *class_name_end = ::strchr (class_name_start, ' ');
4105 SymbolContext empty_sc;
4106 clang_type_t class_opaque_type = NULL;
4107 if (class_name_start < class_name_end)
Greg Clayton0fffff52010-09-24 05:15:53 +00004108 {
Greg Clayton24739922010-10-13 03:15:28 +00004109 ConstString class_name (class_name_start, class_name_end - class_name_start);
4110 TypeList types;
4111 const uint32_t match_count = FindTypes (empty_sc, class_name, true, UINT32_MAX, types);
4112 if (match_count > 0)
Greg Clayton0fffff52010-09-24 05:15:53 +00004113 {
Greg Clayton24739922010-10-13 03:15:28 +00004114 for (uint32_t i=0; i<match_count; ++i)
Greg Clayton0fffff52010-09-24 05:15:53 +00004115 {
Greg Clayton24739922010-10-13 03:15:28 +00004116 Type *type = types.GetTypeAtIndex (i).get();
4117 clang_type_t type_clang_forward_type = type->GetClangForwardType();
4118 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
Greg Clayton0fffff52010-09-24 05:15:53 +00004119 {
Greg Clayton24739922010-10-13 03:15:28 +00004120 class_opaque_type = type_clang_forward_type;
4121 break;
Greg Clayton0fffff52010-09-24 05:15:53 +00004122 }
4123 }
4124 }
Greg Clayton24739922010-10-13 03:15:28 +00004125 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004126
Greg Clayton24739922010-10-13 03:15:28 +00004127 if (class_opaque_type)
4128 {
4129 // If accessibility isn't set to anything valid, assume public for
4130 // now...
4131 if (accessibility == eAccessNone)
4132 accessibility = eAccessPublic;
4133
4134 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004135 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
4136 type_name_cstr,
4137 clang_type,
4138 accessibility);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00004139 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton24739922010-10-13 03:15:28 +00004140 type_handled = objc_method_decl != NULL;
4141 }
4142 }
Greg Clayton5113dc82011-08-12 06:47:54 +00004143 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00004144 {
4145 // Look at the parent of this DIE and see if is is
4146 // a class or struct and see if this is actually a
4147 // C++ method
Greg Clayton5113dc82011-08-12 06:47:54 +00004148 Type *class_type = ResolveType (dwarf_cu, m_decl_ctx_to_die[containing_decl_ctx]);
Greg Clayton24739922010-10-13 03:15:28 +00004149 if (class_type)
4150 {
Greg Clayton72da3972011-08-16 18:40:23 +00004151 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00004152 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004153 // We have a specification which we are going to base our function
4154 // prototype off of, so we need this type to be completed so that the
4155 // m_die_to_decl_ctx for the method in the specification has a valid
4156 // clang decl context.
4157 class_type->GetClangFullType();
Greg Clayton72da3972011-08-16 18:40:23 +00004158 // If we have a specification, then the function type should have been
4159 // made with the specification and not with this die.
4160 DWARFCompileUnitSP spec_cu_sp;
4161 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00004162 clang::DeclContext *spec_clang_decl_ctx = GetCachedClangDeclContextForDIE (spec_die);
4163 if (spec_clang_decl_ctx)
4164 {
4165 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
4166 }
4167 else
Jim Inghamc1663042011-09-29 22:12:35 +00004168 {
4169 ReportWarning ("0x%8.8x: DW_AT_specification(0x%8.8x) has no decl\n",
Greg Clayton5cf58b92011-10-05 22:22:08 +00004170 die->GetOffset(),
4171 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00004172 }
Greg Clayton72da3972011-08-16 18:40:23 +00004173 type_handled = true;
4174 }
4175 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
4176 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004177 // We have a specification which we are going to base our function
4178 // prototype off of, so we need this type to be completed so that the
4179 // m_die_to_decl_ctx for the method in the abstract origin has a valid
4180 // clang decl context.
4181 class_type->GetClangFullType();
4182
Greg Clayton72da3972011-08-16 18:40:23 +00004183 DWARFCompileUnitSP abs_cu_sp;
4184 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00004185 clang::DeclContext *abs_clang_decl_ctx = GetCachedClangDeclContextForDIE (abs_die);
4186 if (abs_clang_decl_ctx)
4187 {
4188 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
4189 }
4190 else
Jim Inghamc1663042011-09-29 22:12:35 +00004191 {
4192 ReportWarning ("0x%8.8x: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
Greg Clayton5cf58b92011-10-05 22:22:08 +00004193 die->GetOffset(),
4194 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00004195 }
Greg Clayton72da3972011-08-16 18:40:23 +00004196 type_handled = true;
4197 }
4198 else
4199 {
4200 clang_type_t class_opaque_type = class_type->GetClangForwardType();
4201 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00004202 {
Greg Clayton72da3972011-08-16 18:40:23 +00004203 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
4204 // in the DWARF for C++ methods... Default to public for now...
4205 if (accessibility == eAccessNone)
4206 accessibility = eAccessPublic;
4207
4208 if (!is_static && !die->HasChildren())
4209 {
4210 // We have a C++ member function with no children (this pointer!)
4211 // and clang will get mad if we try and make a function that isn't
4212 // well formed in the DWARF, so we will just skip it...
4213 type_handled = true;
4214 }
4215 else
4216 {
4217 clang::CXXMethodDecl *cxx_method_decl;
4218 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
4219 type_name_cstr,
4220 clang_type,
4221 accessibility,
4222 is_virtual,
4223 is_static,
4224 is_inline,
4225 is_explicit);
4226 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00004227
Greg Clayton72da3972011-08-16 18:40:23 +00004228 type_handled = cxx_method_decl != NULL;
4229 }
Greg Clayton931180e2011-01-27 06:44:37 +00004230 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004231 }
4232 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004233 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004234 }
Greg Clayton24739922010-10-13 03:15:28 +00004235
4236 if (!type_handled)
4237 {
4238 // We just have a function that isn't part of a class
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004239 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (type_name_cstr,
4240 clang_type,
4241 storage,
4242 is_inline);
Greg Clayton24739922010-10-13 03:15:28 +00004243
4244 // Add the decl to our DIE to decl context map
4245 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00004246 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00004247 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004248 ast.SetFunctionParameters (function_decl,
4249 &function_param_decls.front(),
4250 function_param_decls.size());
Greg Clayton24739922010-10-13 03:15:28 +00004251 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004252 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004253 type_sp.reset( new Type (die->GetOffset(),
4254 this,
4255 type_name_const_str,
4256 0,
4257 NULL,
4258 LLDB_INVALID_UID,
4259 Type::eEncodingIsUID,
4260 &decl,
4261 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004262 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00004263 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004264 }
4265 break;
4266
4267 case DW_TAG_array_type:
4268 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004269 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004270 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004271
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004272 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004273 int64_t first_index = 0;
4274 uint32_t byte_stride = 0;
4275 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00004276 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004277
4278 if (num_attributes > 0)
4279 {
4280 uint32_t i;
4281 for (i=0; i<num_attributes; ++i)
4282 {
4283 attr = attributes.AttributeAtIndex(i);
4284 DWARFFormValue form_value;
4285 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4286 {
4287 switch (attr)
4288 {
4289 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4290 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4291 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4292 case DW_AT_name:
4293 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004294 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004295 break;
4296
4297 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00004298 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004299 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
4300 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00004301 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00004302 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004303 case DW_AT_allocated:
4304 case DW_AT_associated:
4305 case DW_AT_data_location:
4306 case DW_AT_description:
4307 case DW_AT_ordering:
4308 case DW_AT_start_scope:
4309 case DW_AT_visibility:
4310 case DW_AT_specification:
4311 case DW_AT_abstract_origin:
4312 case DW_AT_sibling:
4313 break;
4314 }
4315 }
4316 }
4317
Greg Claytonc93237c2010-10-01 20:48:32 +00004318 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
4319
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004320 Type *element_type = ResolveTypeUID(type_die_offset);
4321
4322 if (element_type)
4323 {
4324 std::vector<uint64_t> element_orders;
4325 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00004326 // We have an array that claims to have no members, lets give it at least one member...
4327 if (element_orders.empty())
4328 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004329 if (byte_stride == 0 && bit_stride == 0)
4330 byte_stride = element_type->GetByteSize();
Greg Claytonf4ecaa52011-02-16 23:00:21 +00004331 clang_type_t array_element_type = element_type->GetClangFullType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004332 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
4333 uint64_t num_elements = 0;
4334 std::vector<uint64_t>::const_reverse_iterator pos;
4335 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
4336 for (pos = element_orders.rbegin(); pos != end; ++pos)
4337 {
4338 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004339 clang_type = ast.CreateArrayType (array_element_type,
4340 num_elements,
4341 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004342 array_element_type = clang_type;
4343 array_element_bit_stride = array_element_bit_stride * num_elements;
4344 }
4345 ConstString empty_name;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004346 type_sp.reset( new Type (die->GetOffset(),
4347 this,
4348 empty_name,
4349 array_element_bit_stride / 8,
4350 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00004351 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004352 Type::eEncodingIsUID,
4353 &decl,
4354 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004355 Type::eResolveStateFull));
4356 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004357 }
4358 }
4359 }
4360 break;
4361
Greg Clayton9b81a312010-06-12 01:20:30 +00004362 case DW_TAG_ptr_to_member_type:
4363 {
4364 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
4365 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
4366
Greg Claytond88d7592010-09-15 08:33:30 +00004367 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00004368
4369 if (num_attributes > 0) {
4370 uint32_t i;
4371 for (i=0; i<num_attributes; ++i)
4372 {
4373 attr = attributes.AttributeAtIndex(i);
4374 DWARFFormValue form_value;
4375 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4376 {
4377 switch (attr)
4378 {
4379 case DW_AT_type:
4380 type_die_offset = form_value.Reference(dwarf_cu); break;
4381 case DW_AT_containing_type:
4382 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
4383 }
4384 }
4385 }
4386
4387 Type *pointee_type = ResolveTypeUID(type_die_offset);
4388 Type *class_type = ResolveTypeUID(containing_type_die_offset);
4389
Greg Clayton526e5af2010-11-13 03:52:47 +00004390 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
4391 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00004392
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004393 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
4394 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00004395
Greg Clayton526e5af2010-11-13 03:52:47 +00004396 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
4397 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00004398
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004399 type_sp.reset( new Type (die->GetOffset(),
4400 this,
4401 type_name_const_str,
4402 byte_size,
4403 NULL,
4404 LLDB_INVALID_UID,
4405 Type::eEncodingIsUID,
4406 NULL,
4407 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004408 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00004409 }
4410
4411 break;
4412 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004413 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00004414 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004415 break;
4416 }
4417
4418 if (type_sp.get())
4419 {
4420 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
4421 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4422
4423 SymbolContextScope * symbol_context_scope = NULL;
4424 if (sc_parent_tag == DW_TAG_compile_unit)
4425 {
4426 symbol_context_scope = sc.comp_unit;
4427 }
4428 else if (sc.function != NULL)
4429 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00004430 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004431 if (symbol_context_scope == NULL)
4432 symbol_context_scope = sc.function;
4433 }
4434
4435 if (symbol_context_scope != NULL)
4436 {
4437 type_sp->SetSymbolContextScope(symbol_context_scope);
4438 }
4439
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004440 // We are ready to put this type into the uniqued list up at the module level
4441 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00004442
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004443 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004444 }
4445 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00004446 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004447 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004448 type_sp = type_list->FindType(type_ptr->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004449 }
4450 }
4451 return type_sp;
4452}
4453
4454size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00004455SymbolFileDWARF::ParseTypes
4456(
4457 const SymbolContext& sc,
4458 DWARFCompileUnit* dwarf_cu,
4459 const DWARFDebugInfoEntry *die,
4460 bool parse_siblings,
4461 bool parse_children
4462)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004463{
4464 size_t types_added = 0;
4465 while (die != NULL)
4466 {
4467 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00004468 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004469 {
4470 if (type_is_new)
4471 ++types_added;
4472 }
4473
4474 if (parse_children && die->HasChildren())
4475 {
4476 if (die->Tag() == DW_TAG_subprogram)
4477 {
4478 SymbolContext child_sc(sc);
4479 child_sc.function = sc.comp_unit->FindFunctionByUID(die->GetOffset()).get();
4480 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
4481 }
4482 else
4483 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
4484 }
4485
4486 if (parse_siblings)
4487 die = die->GetSibling();
4488 else
4489 die = NULL;
4490 }
4491 return types_added;
4492}
4493
4494
4495size_t
4496SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
4497{
4498 assert(sc.comp_unit && sc.function);
4499 size_t functions_added = 0;
4500 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
4501 if (dwarf_cu)
4502 {
4503 dw_offset_t function_die_offset = sc.function->GetID();
4504 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
4505 if (function_die)
4506 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00004507 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004508 }
4509 }
4510
4511 return functions_added;
4512}
4513
4514
4515size_t
4516SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
4517{
4518 // At least a compile unit must be valid
4519 assert(sc.comp_unit);
4520 size_t types_added = 0;
4521 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
4522 if (dwarf_cu)
4523 {
4524 if (sc.function)
4525 {
4526 dw_offset_t function_die_offset = sc.function->GetID();
4527 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
4528 if (func_die && func_die->HasChildren())
4529 {
4530 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
4531 }
4532 }
4533 else
4534 {
4535 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
4536 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
4537 {
4538 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
4539 }
4540 }
4541 }
4542
4543 return types_added;
4544}
4545
4546size_t
4547SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
4548{
4549 if (sc.comp_unit != NULL)
4550 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00004551 DWARFDebugInfo* info = DebugInfo();
4552 if (info == NULL)
4553 return 0;
4554
4555 uint32_t cu_idx = UINT32_MAX;
4556 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004557
4558 if (dwarf_cu == NULL)
4559 return 0;
4560
4561 if (sc.function)
4562 {
4563 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00004564
4565 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
4566 assert (func_lo_pc != DW_INVALID_ADDRESS);
4567
Greg Claytonc662ec82011-06-17 22:10:16 +00004568 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
4569
4570 // Let all blocks know they have parse all their variables
4571 sc.function->GetBlock (false).SetDidParseVariables (true, true);
4572
4573 return num_variables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004574 }
4575 else if (sc.comp_unit)
4576 {
4577 uint32_t vars_added = 0;
4578 VariableListSP variables (sc.comp_unit->GetVariableList(false));
4579
4580 if (variables.get() == NULL)
4581 {
4582 variables.reset(new VariableList());
4583 sc.comp_unit->SetVariableList(variables);
4584
Greg Claytond4a2b372011-09-12 23:21:58 +00004585 DWARFCompileUnit* match_dwarf_cu = NULL;
4586 const DWARFDebugInfoEntry* die = NULL;
4587 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00004588 if (m_apple_names_ap.get())
4589 {
4590 // TODO: implement finding all items in
4591 m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
4592 dwarf_cu->GetNextCompileUnitOffset(),
4593 die_offsets);
4594 }
4595 else
4596 {
4597 // Index if we already haven't to make sure the compile units
4598 // get indexed and make their global DIE index list
4599 if (!m_indexed)
4600 Index ();
4601
4602 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
4603 dwarf_cu->GetNextCompileUnitOffset(),
4604 die_offsets);
4605 }
4606
4607 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00004608 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004609 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004610 DWARFDebugInfo* debug_info = DebugInfo();
4611 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004612 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004613 const dw_offset_t die_offset = die_offsets[i];
4614 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
4615 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
4616 if (var_sp)
4617 {
4618 variables->AddVariableIfUnique (var_sp);
4619 ++vars_added;
4620 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004621 }
4622 }
4623 }
4624 return vars_added;
4625 }
4626 }
4627 return 0;
4628}
4629
4630
4631VariableSP
4632SymbolFileDWARF::ParseVariableDIE
4633(
4634 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004635 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00004636 const DWARFDebugInfoEntry *die,
4637 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004638)
4639{
4640
Greg Clayton83c5cd92010-11-14 22:13:40 +00004641 VariableSP var_sp (m_die_to_variable_sp[die]);
4642 if (var_sp)
4643 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004644
4645 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00004646
4647 if ((tag == DW_TAG_variable) ||
4648 (tag == DW_TAG_constant) ||
4649 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004650 {
Greg Clayton7f995132011-10-04 22:41:51 +00004651 DWARFDebugInfoEntry::Attributes attributes;
4652 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
4653 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004654 {
Greg Clayton7f995132011-10-04 22:41:51 +00004655 const char *name = NULL;
4656 const char *mangled = NULL;
4657 Declaration decl;
4658 uint32_t i;
4659 Type *var_type = NULL;
4660 DWARFExpression location;
4661 bool is_external = false;
4662 bool is_artificial = false;
4663 bool location_is_const_value_data = false;
4664 AccessType accessibility = eAccessNone;
4665
4666 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004667 {
Greg Clayton7f995132011-10-04 22:41:51 +00004668 dw_attr_t attr = attributes.AttributeAtIndex(i);
4669 DWARFFormValue form_value;
4670 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004671 {
Greg Clayton7f995132011-10-04 22:41:51 +00004672 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004673 {
Greg Clayton7f995132011-10-04 22:41:51 +00004674 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4675 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4676 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4677 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
4678 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
4679 case DW_AT_type: var_type = ResolveTypeUID(form_value.Reference(dwarf_cu)); break;
4680 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
4681 case DW_AT_const_value:
4682 location_is_const_value_data = true;
4683 // Fall through...
4684 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004685 {
Greg Clayton7f995132011-10-04 22:41:51 +00004686 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004687 {
Greg Clayton7f995132011-10-04 22:41:51 +00004688 const DataExtractor& debug_info_data = get_debug_info_data();
4689
4690 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4691 uint32_t block_length = form_value.Unsigned();
4692 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
4693 }
4694 else
4695 {
4696 const DataExtractor& debug_loc_data = get_debug_loc_data();
4697 const dw_offset_t debug_loc_offset = form_value.Unsigned();
4698
4699 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
4700 if (loc_list_length > 0)
4701 {
4702 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
4703 assert (func_low_pc != LLDB_INVALID_ADDRESS);
4704 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
4705 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004706 }
4707 }
Greg Clayton7f995132011-10-04 22:41:51 +00004708 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004709
Greg Clayton7f995132011-10-04 22:41:51 +00004710 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
4711 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
4712 case DW_AT_declaration:
4713 case DW_AT_description:
4714 case DW_AT_endianity:
4715 case DW_AT_segment:
4716 case DW_AT_start_scope:
4717 case DW_AT_visibility:
4718 default:
4719 case DW_AT_abstract_origin:
4720 case DW_AT_sibling:
4721 case DW_AT_specification:
4722 break;
4723 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004724 }
4725 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004726
Greg Clayton7f995132011-10-04 22:41:51 +00004727 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004728 {
Greg Clayton7f995132011-10-04 22:41:51 +00004729 assert(var_type != DIE_IS_BEING_PARSED);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004730
Greg Clayton7f995132011-10-04 22:41:51 +00004731 ValueType scope = eValueTypeInvalid;
4732
4733 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
4734 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4735
4736 if (tag == DW_TAG_formal_parameter)
4737 scope = eValueTypeVariableArgument;
4738 else if (is_external || parent_tag == DW_TAG_compile_unit)
4739 scope = eValueTypeVariableGlobal;
4740 else
4741 scope = eValueTypeVariableLocal;
4742
4743 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton5cf58b92011-10-05 22:22:08 +00004744 switch (parent_tag)
Greg Clayton7f995132011-10-04 22:41:51 +00004745 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004746 case DW_TAG_subprogram:
4747 case DW_TAG_inlined_subroutine:
4748 case DW_TAG_lexical_block:
4749 if (sc.function)
4750 {
4751 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
4752 if (symbol_context_scope == NULL)
4753 symbol_context_scope = sc.function;
4754 }
4755 break;
4756
4757 default:
Greg Clayton7f995132011-10-04 22:41:51 +00004758 symbol_context_scope = sc.comp_unit;
Greg Clayton5cf58b92011-10-05 22:22:08 +00004759 break;
Greg Clayton7f995132011-10-04 22:41:51 +00004760 }
4761
Greg Clayton5cf58b92011-10-05 22:22:08 +00004762 if (symbol_context_scope)
4763 {
4764 var_sp.reset (new Variable(die->GetOffset(),
4765 name,
4766 mangled,
4767 var_type,
4768 scope,
4769 symbol_context_scope,
4770 &decl,
4771 location,
4772 is_external,
4773 is_artificial));
4774
4775 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
4776 }
4777 else
4778 {
4779 // Not ready to parse this variable yet. It might be a global
4780 // or static variable that is in a function scope and the function
4781 // in the symbol context wasn't filled in yet
4782 return var_sp;
4783 }
Greg Clayton7f995132011-10-04 22:41:51 +00004784 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004785 }
Greg Clayton7f995132011-10-04 22:41:51 +00004786 // Cache var_sp even if NULL (the variable was just a specification or
4787 // was missing vital information to be able to be displayed in the debugger
4788 // (missing location due to optimization, etc)) so we don't re-parse
4789 // this DIE over and over later...
4790 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004791 }
4792 return var_sp;
4793}
4794
Greg Claytonc662ec82011-06-17 22:10:16 +00004795
4796const DWARFDebugInfoEntry *
4797SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
4798 dw_offset_t spec_block_die_offset,
4799 DWARFCompileUnit **result_die_cu_handle)
4800{
4801 // Give the concrete function die specified by "func_die_offset", find the
4802 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4803 // to "spec_block_die_offset"
4804 DWARFDebugInfo* info = DebugInfo();
4805
4806 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
4807 if (die)
4808 {
4809 assert (*result_die_cu_handle);
4810 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
4811 }
4812 return NULL;
4813}
4814
4815
4816const DWARFDebugInfoEntry *
4817SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
4818 const DWARFDebugInfoEntry *die,
4819 dw_offset_t spec_block_die_offset,
4820 DWARFCompileUnit **result_die_cu_handle)
4821{
4822 if (die)
4823 {
4824 switch (die->Tag())
4825 {
4826 case DW_TAG_subprogram:
4827 case DW_TAG_inlined_subroutine:
4828 case DW_TAG_lexical_block:
4829 {
4830 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4831 {
4832 *result_die_cu_handle = dwarf_cu;
4833 return die;
4834 }
4835
4836 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
4837 {
4838 *result_die_cu_handle = dwarf_cu;
4839 return die;
4840 }
4841 }
4842 break;
4843 }
4844
4845 // Give the concrete function die specified by "func_die_offset", find the
4846 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4847 // to "spec_block_die_offset"
4848 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
4849 {
4850 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
4851 child_die,
4852 spec_block_die_offset,
4853 result_die_cu_handle);
4854 if (result_die)
4855 return result_die;
4856 }
4857 }
4858
4859 *result_die_cu_handle = NULL;
4860 return NULL;
4861}
4862
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004863size_t
4864SymbolFileDWARF::ParseVariables
4865(
4866 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004867 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00004868 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004869 const DWARFDebugInfoEntry *orig_die,
4870 bool parse_siblings,
4871 bool parse_children,
4872 VariableList* cc_variable_list
4873)
4874{
4875 if (orig_die == NULL)
4876 return 0;
4877
Greg Claytonc662ec82011-06-17 22:10:16 +00004878 VariableListSP variable_list_sp;
4879
4880 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004881 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004882 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004883 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004884 dw_tag_t tag = die->Tag();
4885
4886 // Check to see if we have already parsed this variable or constant?
4887 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004888 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004889 if (cc_variable_list)
4890 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004891 }
4892 else
4893 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004894 // We haven't already parsed it, lets do that now.
4895 if ((tag == DW_TAG_variable) ||
4896 (tag == DW_TAG_constant) ||
4897 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004898 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004899 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00004900 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004901 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
4902 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4903 switch (parent_tag)
4904 {
4905 case DW_TAG_compile_unit:
4906 if (sc.comp_unit != NULL)
4907 {
4908 variable_list_sp = sc.comp_unit->GetVariableList(false);
4909 if (variable_list_sp.get() == NULL)
4910 {
4911 variable_list_sp.reset(new VariableList());
4912 sc.comp_unit->SetVariableList(variable_list_sp);
4913 }
4914 }
4915 else
4916 {
Jim Inghamc1663042011-09-29 22:12:35 +00004917 ReportError ("parent 0x%8.8x %s with no valid compile unit in symbol context for 0x%8.8x %s.\n",
Greg Claytonc662ec82011-06-17 22:10:16 +00004918 sc_parent_die->GetOffset(),
4919 DW_TAG_value_to_name (parent_tag),
4920 orig_die->GetOffset(),
4921 DW_TAG_value_to_name (orig_die->Tag()));
4922 }
4923 break;
4924
4925 case DW_TAG_subprogram:
4926 case DW_TAG_inlined_subroutine:
4927 case DW_TAG_lexical_block:
4928 if (sc.function != NULL)
4929 {
4930 // Check to see if we already have parsed the variables for the given scope
4931
4932 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
4933 if (block == NULL)
4934 {
4935 // This must be a specification or abstract origin with
4936 // a concrete block couterpart in the current function. We need
4937 // to find the concrete block so we can correctly add the
4938 // variable to it
4939 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
4940 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
4941 sc_parent_die->GetOffset(),
4942 &concrete_block_die_cu);
4943 if (concrete_block_die)
4944 block = sc.function->GetBlock(true).FindBlockByID(concrete_block_die->GetOffset());
4945 }
4946
4947 if (block != NULL)
4948 {
4949 const bool can_create = false;
4950 variable_list_sp = block->GetBlockVariableList (can_create);
4951 if (variable_list_sp.get() == NULL)
4952 {
4953 variable_list_sp.reset(new VariableList());
4954 block->SetVariableList(variable_list_sp);
4955 }
4956 }
4957 }
4958 break;
4959
4960 default:
Jim Inghamc1663042011-09-29 22:12:35 +00004961 ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8x %s.\n",
Greg Claytonc662ec82011-06-17 22:10:16 +00004962 orig_die->GetOffset(),
4963 DW_TAG_value_to_name (orig_die->Tag()));
4964 break;
4965 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00004966 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004967
4968 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004969 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00004970 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
4971 if (var_sp)
4972 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004973 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00004974 if (cc_variable_list)
4975 cc_variable_list->AddVariableIfUnique (var_sp);
4976 ++vars_added;
4977 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004978 }
4979 }
4980 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004981
4982 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4983
4984 if (!skip_children && parse_children && die->HasChildren())
4985 {
4986 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
4987 }
4988
4989 if (parse_siblings)
4990 die = die->GetSibling();
4991 else
4992 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004993 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004994 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004995}
4996
4997//------------------------------------------------------------------
4998// PluginInterface protocol
4999//------------------------------------------------------------------
5000const char *
5001SymbolFileDWARF::GetPluginName()
5002{
5003 return "SymbolFileDWARF";
5004}
5005
5006const char *
5007SymbolFileDWARF::GetShortPluginName()
5008{
5009 return GetPluginNameStatic();
5010}
5011
5012uint32_t
5013SymbolFileDWARF::GetPluginVersion()
5014{
5015 return 1;
5016}
5017
5018void
Greg Clayton6beaaa62011-01-17 03:46:26 +00005019SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
5020{
5021 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
5022 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
5023 if (clang_type)
5024 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
5025}
5026
5027void
5028SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
5029{
5030 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
5031 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
5032 if (clang_type)
5033 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
5034}
5035
Greg Claytona2721472011-06-25 00:44:06 +00005036void
Sean Callanancc427fa2011-07-30 02:42:06 +00005037SymbolFileDWARF::DumpIndexes ()
5038{
5039 StreamFile s(stdout, false);
5040
5041 s.Printf ("DWARF index for (%s) '%s/%s':",
5042 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
5043 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
5044 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
5045 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
5046 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
5047 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
5048 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
5049 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
5050 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
5051 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
5052 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
5053}
5054
5055void
5056SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
5057 const char *name,
5058 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00005059{
Sean Callanancc427fa2011-07-30 02:42:06 +00005060 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00005061
5062 if (iter == m_decl_ctx_to_die.end())
5063 return;
5064
Sean Callanancc427fa2011-07-30 02:42:06 +00005065 const DWARFDebugInfoEntry *context_die = iter->second;
Greg Claytona2721472011-06-25 00:44:06 +00005066
5067 if (!results)
5068 return;
5069
5070 DWARFDebugInfo* info = DebugInfo();
5071
Greg Claytond4a2b372011-09-12 23:21:58 +00005072 DIEArray die_offsets;
Greg Claytona2721472011-06-25 00:44:06 +00005073
Greg Claytond4a2b372011-09-12 23:21:58 +00005074 DWARFCompileUnit* dwarf_cu = NULL;
5075 const DWARFDebugInfoEntry* die = NULL;
5076 size_t num_matches = m_type_index.Find (ConstString(name), die_offsets);
Greg Claytona2721472011-06-25 00:44:06 +00005077
5078 if (num_matches)
5079 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005080 for (size_t i = 0; i < num_matches; ++i)
Greg Claytona2721472011-06-25 00:44:06 +00005081 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005082 const dw_offset_t die_offset = die_offsets[i];
5083 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
5084
Sean Callanancc427fa2011-07-30 02:42:06 +00005085 if (die->GetParent() != context_die)
Greg Claytona2721472011-06-25 00:44:06 +00005086 continue;
5087
Greg Claytond4a2b372011-09-12 23:21:58 +00005088 Type *matching_type = ResolveType (dwarf_cu, die);
Greg Claytona2721472011-06-25 00:44:06 +00005089
5090 lldb::clang_type_t type = matching_type->GetClangFullType();
5091 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
5092
Sean Callanancc427fa2011-07-30 02:42:06 +00005093 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
Greg Claytona2721472011-06-25 00:44:06 +00005094 {
5095 clang::TagDecl *tag_decl = tag_type->getDecl();
5096 results->push_back(tag_decl);
5097 }
Sean Callanancc427fa2011-07-30 02:42:06 +00005098 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
Greg Claytona2721472011-06-25 00:44:06 +00005099 {
5100 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
5101 results->push_back(typedef_decl);
5102 }
5103 }
5104 }
5105}
5106
5107void
5108SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
5109 const clang::DeclContext *DC,
5110 clang::DeclarationName Name,
5111 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
5112{
5113 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
5114
Sean Callanancc427fa2011-07-30 02:42:06 +00005115 symbol_file_dwarf->SearchDeclContext (DC, Name.getAsString().c_str(), results);
Greg Claytona2721472011-06-25 00:44:06 +00005116}