blob: a80aa4907dff4ae98d4e4581fc23a7c79c3d8a60 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "clang/Basic/Builtins.h"
19#include "clang/Basic/IdentifierTable.h"
20#include "clang/Basic/LangOptions.h"
21#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000024#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Sean Callanancc427fa2011-07-30 02:42:06 +000026#include "llvm/Support/Casting.h"
27
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Core/Module.h"
29#include "lldb/Core/PluginManager.h"
30#include "lldb/Core/RegularExpression.h"
31#include "lldb/Core/Scalar.h"
32#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000033#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000034#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Core/Timer.h"
36#include "lldb/Core/Value.h"
37
Greg Clayton20568dd2011-10-13 23:13:20 +000038#include "lldb/Host/Host.h"
39
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000041#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Symbol/CompileUnit.h"
43#include "lldb/Symbol/LineTable.h"
44#include "lldb/Symbol/ObjectFile.h"
45#include "lldb/Symbol/SymbolVendor.h"
46#include "lldb/Symbol/VariableList.h"
47
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000048#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000049#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000050
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "DWARFCompileUnit.h"
52#include "DWARFDebugAbbrev.h"
53#include "DWARFDebugAranges.h"
54#include "DWARFDebugInfo.h"
55#include "DWARFDebugInfoEntry.h"
56#include "DWARFDebugLine.h"
57#include "DWARFDebugPubnames.h"
58#include "DWARFDebugRanges.h"
59#include "DWARFDIECollection.h"
60#include "DWARFFormValue.h"
61#include "DWARFLocationList.h"
62#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000063#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
65#include <map>
66
Greg Clayton62742b12010-11-11 01:09:45 +000067//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000068
69#ifdef ENABLE_DEBUG_PRINTF
70#include <stdio.h>
71#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
72#else
73#define DEBUG_PRINTF(fmt, ...)
74#endif
75
Greg Clayton594e5ed2010-09-27 21:07:38 +000076#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077
78using namespace lldb;
79using namespace lldb_private;
80
Greg Clayton3bffb082011-12-10 02:15:28 +000081static inline bool
82DW_TAG_is_function_tag (dw_tag_t tag)
83{
84 return tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine;
85}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086
Sean Callananc7fbf732010-08-06 00:32:49 +000087static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +000088DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089{
90 switch (dwarf_accessibility)
91 {
Sean Callananc7fbf732010-08-06 00:32:49 +000092 case DW_ACCESS_public: return eAccessPublic;
93 case DW_ACCESS_private: return eAccessPrivate;
94 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +000095 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096 }
Sean Callananc7fbf732010-08-06 00:32:49 +000097 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098}
99
100void
101SymbolFileDWARF::Initialize()
102{
103 LogChannelDWARF::Initialize();
104 PluginManager::RegisterPlugin (GetPluginNameStatic(),
105 GetPluginDescriptionStatic(),
106 CreateInstance);
107}
108
109void
110SymbolFileDWARF::Terminate()
111{
112 PluginManager::UnregisterPlugin (CreateInstance);
113 LogChannelDWARF::Initialize();
114}
115
116
117const char *
118SymbolFileDWARF::GetPluginNameStatic()
119{
Greg Claytond4a2b372011-09-12 23:21:58 +0000120 return "dwarf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121}
122
123const char *
124SymbolFileDWARF::GetPluginDescriptionStatic()
125{
126 return "DWARF and DWARF3 debug symbol file reader.";
127}
128
129
130SymbolFile*
131SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
132{
133 return new SymbolFileDWARF(obj_file);
134}
135
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000136TypeList *
137SymbolFileDWARF::GetTypeList ()
138{
139 if (m_debug_map_symfile)
140 return m_debug_map_symfile->GetTypeList();
141 return m_obj_file->GetModule()->GetTypeList();
142
143}
144
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145//----------------------------------------------------------------------
146// Gets the first parent that is a lexical block, function or inlined
147// subroutine, or compile unit.
148//----------------------------------------------------------------------
149static const DWARFDebugInfoEntry *
150GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
151{
152 const DWARFDebugInfoEntry *die;
153 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
154 {
155 dw_tag_t tag = die->Tag();
156
157 switch (tag)
158 {
159 case DW_TAG_compile_unit:
160 case DW_TAG_subprogram:
161 case DW_TAG_inlined_subroutine:
162 case DW_TAG_lexical_block:
163 return die;
164 }
165 }
166 return NULL;
167}
168
169
Greg Clayton450e3f32010-10-12 02:24:53 +0000170SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
171 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000172 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton450e3f32010-10-12 02:24:53 +0000173 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000174 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000176 m_data_debug_abbrev (),
177 m_data_debug_aranges (),
178 m_data_debug_frame (),
179 m_data_debug_info (),
180 m_data_debug_line (),
181 m_data_debug_loc (),
182 m_data_debug_ranges (),
183 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000184 m_data_apple_names (),
185 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000186 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 m_info(),
189 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000190 m_apple_names_ap (),
191 m_apple_types_ap (),
192 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000193 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000194 m_function_basename_index(),
195 m_function_fullname_index(),
196 m_function_method_index(),
197 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000198 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000199 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000200 m_type_index(),
201 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000202 m_indexed (false),
203 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000204 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000205 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000206 m_ranges(),
207 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208{
209}
210
211SymbolFileDWARF::~SymbolFileDWARF()
212{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000213 if (m_is_external_ast_source)
214 m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource ();
215}
216
217static const ConstString &
218GetDWARFMachOSegmentName ()
219{
220 static ConstString g_dwarf_section_name ("__DWARF");
221 return g_dwarf_section_name;
222}
223
Greg Claytone576ab22011-02-15 00:19:15 +0000224UniqueDWARFASTTypeMap &
225SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
226{
227 if (m_debug_map_symfile)
228 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
229 return m_unique_ast_type_map;
230}
231
Greg Clayton6beaaa62011-01-17 03:46:26 +0000232ClangASTContext &
233SymbolFileDWARF::GetClangASTContext ()
234{
235 if (m_debug_map_symfile)
236 return m_debug_map_symfile->GetClangASTContext ();
237
238 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
239 if (!m_is_external_ast_source)
240 {
241 m_is_external_ast_source = true;
242 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
243 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
244 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000245 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000246 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000247 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000248 ast.SetExternalSource (ast_source_ap);
249 }
250 return ast;
251}
252
253void
254SymbolFileDWARF::InitializeObject()
255{
256 // Install our external AST source callbacks so we can complete Clang types.
257 Module *module = m_obj_file->GetModule();
258 if (module)
259 {
260 const SectionList *section_list = m_obj_file->GetSectionList();
261
262 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
263
264 // Memory map the DWARF mach-o segment so we have everything mmap'ed
265 // to keep our heap memory usage down.
266 if (section)
267 section->MemoryMapSectionDataFromObjectFile(m_obj_file, m_dwarf_data);
268 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000269 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000270 if (m_data_apple_names.GetByteSize() > 0)
271 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000272 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
273 if (m_apple_names_ap->IsValid())
274 m_using_apple_tables = true;
275 else
Greg Clayton7f995132011-10-04 22:41:51 +0000276 m_apple_names_ap.reset();
277 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000278 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000279 if (m_data_apple_types.GetByteSize() > 0)
280 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000281 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
282 if (m_apple_types_ap->IsValid())
283 m_using_apple_tables = true;
284 else
Greg Clayton7f995132011-10-04 22:41:51 +0000285 m_apple_types_ap.reset();
286 }
287
288 get_apple_namespaces_data();
289 if (m_data_apple_namespaces.GetByteSize() > 0)
290 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000291 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
292 if (m_apple_namespaces_ap->IsValid())
293 m_using_apple_tables = true;
294 else
Greg Clayton7f995132011-10-04 22:41:51 +0000295 m_apple_namespaces_ap.reset();
296 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000297
Greg Clayton5009f9d2011-10-27 17:55:14 +0000298 get_apple_objc_data();
299 if (m_data_apple_objc.GetByteSize() > 0)
300 {
301 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
302 if (m_apple_objc_ap->IsValid())
303 m_using_apple_tables = true;
304 else
305 m_apple_objc_ap.reset();
306 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307}
308
309bool
310SymbolFileDWARF::SupportedVersion(uint16_t version)
311{
312 return version == 2 || version == 3;
313}
314
315uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000316SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317{
318 uint32_t abilities = 0;
319 if (m_obj_file != NULL)
320 {
321 const Section* section = NULL;
322 const SectionList *section_list = m_obj_file->GetSectionList();
323 if (section_list == NULL)
324 return 0;
325
326 uint64_t debug_abbrev_file_size = 0;
327 uint64_t debug_aranges_file_size = 0;
328 uint64_t debug_frame_file_size = 0;
329 uint64_t debug_info_file_size = 0;
330 uint64_t debug_line_file_size = 0;
331 uint64_t debug_loc_file_size = 0;
332 uint64_t debug_macinfo_file_size = 0;
333 uint64_t debug_pubnames_file_size = 0;
334 uint64_t debug_pubtypes_file_size = 0;
335 uint64_t debug_ranges_file_size = 0;
336 uint64_t debug_str_file_size = 0;
337
Greg Clayton6beaaa62011-01-17 03:46:26 +0000338 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339
340 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000341 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342
Greg Clayton4ceb9982010-07-21 22:54:26 +0000343 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 if (section != NULL)
345 {
346 debug_info_file_size = section->GetByteSize();
347
Greg Clayton4ceb9982010-07-21 22:54:26 +0000348 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 if (section)
350 debug_abbrev_file_size = section->GetByteSize();
351 else
352 m_flags.Set (flagsGotDebugAbbrevData);
353
Greg Clayton4ceb9982010-07-21 22:54:26 +0000354 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 if (section)
356 debug_aranges_file_size = section->GetByteSize();
357 else
358 m_flags.Set (flagsGotDebugArangesData);
359
Greg Clayton4ceb9982010-07-21 22:54:26 +0000360 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361 if (section)
362 debug_frame_file_size = section->GetByteSize();
363 else
364 m_flags.Set (flagsGotDebugFrameData);
365
Greg Clayton4ceb9982010-07-21 22:54:26 +0000366 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367 if (section)
368 debug_line_file_size = section->GetByteSize();
369 else
370 m_flags.Set (flagsGotDebugLineData);
371
Greg Clayton4ceb9982010-07-21 22:54:26 +0000372 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 if (section)
374 debug_loc_file_size = section->GetByteSize();
375 else
376 m_flags.Set (flagsGotDebugLocData);
377
Greg Clayton4ceb9982010-07-21 22:54:26 +0000378 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 if (section)
380 debug_macinfo_file_size = section->GetByteSize();
381 else
382 m_flags.Set (flagsGotDebugMacInfoData);
383
Greg Clayton4ceb9982010-07-21 22:54:26 +0000384 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 if (section)
386 debug_pubnames_file_size = section->GetByteSize();
387 else
388 m_flags.Set (flagsGotDebugPubNamesData);
389
Greg Clayton4ceb9982010-07-21 22:54:26 +0000390 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 if (section)
392 debug_pubtypes_file_size = section->GetByteSize();
393 else
394 m_flags.Set (flagsGotDebugPubTypesData);
395
Greg Clayton4ceb9982010-07-21 22:54:26 +0000396 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 if (section)
398 debug_ranges_file_size = section->GetByteSize();
399 else
400 m_flags.Set (flagsGotDebugRangesData);
401
Greg Clayton4ceb9982010-07-21 22:54:26 +0000402 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 if (section)
404 debug_str_file_size = section->GetByteSize();
405 else
406 m_flags.Set (flagsGotDebugStrData);
407 }
408
409 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
410 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
411
412 if (debug_line_file_size > 0)
413 abilities |= LineTables;
414
415 if (debug_aranges_file_size > 0)
416 abilities |= AddressAcceleratorTable;
417
418 if (debug_pubnames_file_size > 0)
419 abilities |= FunctionAcceleratorTable;
420
421 if (debug_pubtypes_file_size > 0)
422 abilities |= TypeAcceleratorTable;
423
424 if (debug_macinfo_file_size > 0)
425 abilities |= MacroInformation;
426
427 if (debug_frame_file_size > 0)
428 abilities |= CallFrameInformation;
429 }
430 return abilities;
431}
432
433const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000434SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435{
436 if (m_flags.IsClear (got_flag))
437 {
438 m_flags.Set (got_flag);
439 const SectionList *section_list = m_obj_file->GetSectionList();
440 if (section_list)
441 {
Greg Clayton4ceb9982010-07-21 22:54:26 +0000442 Section *section = section_list->FindSectionByType(sect_type, true).get();
443 if (section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 {
445 // See if we memory mapped the DWARF segment?
446 if (m_dwarf_data.GetByteSize())
447 {
448 data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize());
449 }
450 else
451 {
452 if (section->ReadSectionDataFromObjectFile(m_obj_file, data) == 0)
453 data.Clear();
454 }
455 }
456 }
457 }
458 return data;
459}
460
461const DataExtractor&
462SymbolFileDWARF::get_debug_abbrev_data()
463{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000464 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465}
466
467const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000468SymbolFileDWARF::get_debug_aranges_data()
469{
470 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
471}
472
473const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474SymbolFileDWARF::get_debug_frame_data()
475{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000476 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477}
478
479const DataExtractor&
480SymbolFileDWARF::get_debug_info_data()
481{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000482 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483}
484
485const DataExtractor&
486SymbolFileDWARF::get_debug_line_data()
487{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000488 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489}
490
491const DataExtractor&
492SymbolFileDWARF::get_debug_loc_data()
493{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000494 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495}
496
497const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498SymbolFileDWARF::get_debug_ranges_data()
499{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000500 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501}
502
503const DataExtractor&
504SymbolFileDWARF::get_debug_str_data()
505{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000506 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507}
508
Greg Claytonf9eec202011-09-01 23:16:13 +0000509const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000510SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000511{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000512 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000513}
514
515const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000516SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000517{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000518 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000519}
520
Greg Clayton7f995132011-10-04 22:41:51 +0000521const DataExtractor&
522SymbolFileDWARF::get_apple_namespaces_data()
523{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000524 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
525}
526
527const DataExtractor&
528SymbolFileDWARF::get_apple_objc_data()
529{
530 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000531}
532
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533
534DWARFDebugAbbrev*
535SymbolFileDWARF::DebugAbbrev()
536{
537 if (m_abbr.get() == NULL)
538 {
539 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
540 if (debug_abbrev_data.GetByteSize() > 0)
541 {
542 m_abbr.reset(new DWARFDebugAbbrev());
543 if (m_abbr.get())
544 m_abbr->Parse(debug_abbrev_data);
545 }
546 }
547 return m_abbr.get();
548}
549
550const DWARFDebugAbbrev*
551SymbolFileDWARF::DebugAbbrev() const
552{
553 return m_abbr.get();
554}
555
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556
557DWARFDebugInfo*
558SymbolFileDWARF::DebugInfo()
559{
560 if (m_info.get() == NULL)
561 {
562 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
563 if (get_debug_info_data().GetByteSize() > 0)
564 {
565 m_info.reset(new DWARFDebugInfo());
566 if (m_info.get())
567 {
568 m_info->SetDwarfData(this);
569 }
570 }
571 }
572 return m_info.get();
573}
574
575const DWARFDebugInfo*
576SymbolFileDWARF::DebugInfo() const
577{
578 return m_info.get();
579}
580
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581DWARFCompileUnit*
582SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
583{
584 DWARFDebugInfo* info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +0000585 if (info && UserIDMatches(cu_uid))
586 return info->GetCompileUnit((dw_offset_t)cu_uid).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 return NULL;
588}
589
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590
591DWARFDebugRanges*
592SymbolFileDWARF::DebugRanges()
593{
594 if (m_ranges.get() == NULL)
595 {
596 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
597 if (get_debug_ranges_data().GetByteSize() > 0)
598 {
599 m_ranges.reset(new DWARFDebugRanges());
600 if (m_ranges.get())
601 m_ranges->Extract(this);
602 }
603 }
604 return m_ranges.get();
605}
606
607const DWARFDebugRanges*
608SymbolFileDWARF::DebugRanges() const
609{
610 return m_ranges.get();
611}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612
613bool
Greg Clayton96d7d742010-11-10 23:42:09 +0000614SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615{
Greg Clayton96d7d742010-11-10 23:42:09 +0000616 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000618 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 if (cu_die)
620 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000621 const char * cu_die_name = cu_die->GetName(this, curr_cu);
622 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
Jim Ingham0f35ac22011-08-24 23:34:20 +0000623 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624 if (cu_die_name)
625 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000626 FileSpec cu_file_spec;
627
Greg Clayton7bd65b92011-02-09 23:39:34 +0000628 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000630 // If we have a full path to the compile unit, we don't need to resolve
631 // the file. This can be expensive e.g. when the source files are NFS mounted.
632 cu_file_spec.SetFile (cu_die_name, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 }
634 else
635 {
636 std::string fullpath(cu_comp_dir);
637 if (*fullpath.rbegin() != '/')
638 fullpath += '/';
639 fullpath += cu_die_name;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000640 cu_file_spec.SetFile (fullpath.c_str(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641 }
642
Greg Clayton81c22f62011-10-19 18:09:39 +0000643 compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(),
644 curr_cu,
645 cu_file_spec,
646 MakeUserID(curr_cu->GetOffset()),
647 cu_language));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 if (compile_unit_sp.get())
649 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000650 curr_cu->SetUserData(compile_unit_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 return true;
652 }
653 }
654 }
655 }
656 return false;
657}
658
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659uint32_t
660SymbolFileDWARF::GetNumCompileUnits()
661{
662 DWARFDebugInfo* info = DebugInfo();
663 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 return 0;
666}
667
668CompUnitSP
669SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
670{
671 CompUnitSP comp_unit;
672 DWARFDebugInfo* info = DebugInfo();
673 if (info)
674 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000675 DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx);
676 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677 {
678 // Our symbol vendor shouldn't be asking us to add a compile unit that
679 // has already been added to it, which this DWARF plug-in knows as it
680 // stores the lldb compile unit (CompileUnit) pointer in each
681 // DWARFCompileUnit object when it gets added.
Greg Clayton96d7d742010-11-10 23:42:09 +0000682 assert(curr_cu->GetUserData() == NULL);
683 ParseCompileUnit(curr_cu, comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 }
685 }
686 return comp_unit;
687}
688
689static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000690AddRangesToBlock (Block& block,
691 DWARFDebugRanges::RangeList& ranges,
692 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000694 const size_t num_ranges = ranges.GetSize();
695 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000697 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
698 const addr_t range_base = range.GetRangeBase();
699 assert (range_base >= block_base_addr);
700 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000702 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703}
704
705
706Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000707SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708{
709 DWARFDebugRanges::RangeList func_ranges;
710 const char *name = NULL;
711 const char *mangled = NULL;
712 int decl_file = 0;
713 int decl_line = 0;
714 int decl_column = 0;
715 int call_file = 0;
716 int call_line = 0;
717 int call_column = 0;
718 DWARFExpression frame_base;
719
Greg Claytonc93237c2010-10-01 20:48:32 +0000720 assert (die->Tag() == DW_TAG_subprogram);
721
722 if (die->Tag() != DW_TAG_subprogram)
723 return NULL;
724
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 if (die->GetDIENamesAndRanges(this, dwarf_cu, name, mangled, func_ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column, &frame_base))
726 {
727 // Union of all ranges in the function DIE (if the function is discontiguous)
728 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000729 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
730 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
732 {
733 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
734 if (func_range.GetBaseAddress().IsValid())
735 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
736 }
737
738 if (func_range.GetBaseAddress().IsValid())
739 {
740 Mangled func_name;
741 if (mangled)
742 func_name.SetValue(mangled, true);
743 else if (name)
744 func_name.SetValue(name, false);
745
746 FunctionSP func_sp;
747 std::auto_ptr<Declaration> decl_ap;
748 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000749 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
750 decl_line,
751 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000753 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000754 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755
756 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
757
758 func_range.GetBaseAddress().ResolveLinkedAddress();
759
Greg Clayton81c22f62011-10-19 18:09:39 +0000760 const user_id_t func_user_id = MakeUserID(die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 func_sp.reset(new Function (sc.comp_unit,
Greg Clayton81c22f62011-10-19 18:09:39 +0000762 func_user_id, // UserID is the DIE offset
763 func_user_id,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764 func_name,
765 func_type,
766 func_range)); // first address range
767
768 if (func_sp.get() != NULL)
769 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000770 if (frame_base.IsValid())
771 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772 sc.comp_unit->AddFunction(func_sp);
773 return func_sp.get();
774 }
775 }
776 }
777 return NULL;
778}
779
780size_t
781SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
782{
783 assert (sc.comp_unit);
784 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000785 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 if (dwarf_cu)
787 {
788 DWARFDIECollection function_dies;
789 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
790 size_t func_idx;
791 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
792 {
793 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000794 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 {
796 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
797 ++functions_added;
798 }
799 }
800 //FixupTypes();
801 }
802 return functions_added;
803}
804
805bool
806SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
807{
808 assert (sc.comp_unit);
Greg Clayton96d7d742010-11-10 23:42:09 +0000809 DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
810 assert (curr_cu);
811 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812
813 if (cu_die)
814 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000815 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
816 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 +0000817
818 // All file indexes in DWARF are one based and a file of index zero is
819 // supposed to be the compile unit itself.
820 support_files.Append (*sc.comp_unit);
821
822 return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
823 }
824 return false;
825}
826
827struct ParseDWARFLineTableCallbackInfo
828{
829 LineTable* line_table;
830 const SectionList *section_list;
831 lldb::addr_t prev_sect_file_base_addr;
832 lldb::addr_t curr_sect_file_base_addr;
833 bool is_oso_for_debug_map;
834 bool prev_in_final_executable;
835 DWARFDebugLine::Row prev_row;
836 SectionSP prev_section_sp;
837 SectionSP curr_section_sp;
838};
839
840//----------------------------------------------------------------------
841// ParseStatementTableCallback
842//----------------------------------------------------------------------
843static void
844ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
845{
846 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
847 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
848 {
849 // Just started parsing the line table
850 }
851 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
852 {
853 // Done parsing line table, nothing to do for the cleanup
854 }
855 else
856 {
857 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
858 // We have a new row, lets append it
859
860 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
861 {
862 info->prev_section_sp = info->curr_section_sp;
863 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
864 // If this is an end sequence entry, then we subtract one from the
865 // address to make sure we get an address that is not the end of
866 // a section.
867 if (state.end_sequence && state.address != 0)
868 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
869 else
870 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
871
872 if (info->curr_section_sp.get())
873 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
874 else
875 info->curr_sect_file_base_addr = 0;
876 }
877 if (info->curr_section_sp.get())
878 {
879 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
880 // Check for the fancy section magic to determine if we
881
882 if (info->is_oso_for_debug_map)
883 {
884 // When this is a debug map object file that contains DWARF
885 // (referenced from an N_OSO debug map nlist entry) we will have
886 // a file address in the file range for our section from the
887 // original .o file, and a load address in the executable that
888 // contains the debug map.
889 //
890 // If the sections for the file range and load range are
891 // different, we have a remapped section for the function and
892 // this address is resolved. If they are the same, then the
893 // function for this address didn't make it into the final
894 // executable.
895 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
896
897 // If we are doing DWARF with debug map, then we need to carefully
898 // add each line table entry as there may be gaps as functions
899 // get moved around or removed.
900 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
901 {
902 if (info->prev_in_final_executable)
903 {
904 bool terminate_previous_entry = false;
905 if (!curr_in_final_executable)
906 {
907 // Check for the case where the previous line entry
908 // in a function made it into the final executable,
909 // yet the current line entry falls in a function
910 // that didn't. The line table used to be contiguous
911 // through this address range but now it isn't. We
912 // need to terminate the previous line entry so
913 // that we can reconstruct the line range correctly
914 // for it and to keep the line table correct.
915 terminate_previous_entry = true;
916 }
917 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
918 {
919 // Check for cases where the line entries used to be
920 // contiguous address ranges, but now they aren't.
921 // This can happen when order files specify the
922 // ordering of the functions.
923 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
924 Section *curr_sect = info->curr_section_sp.get();
925 Section *prev_sect = info->prev_section_sp.get();
926 assert (curr_sect->GetLinkedSection());
927 assert (prev_sect->GetLinkedSection());
928 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
929 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
930 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
931 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
932 if (object_file_addr_delta != linked_file_addr_delta)
933 terminate_previous_entry = true;
934 }
935
936 if (terminate_previous_entry)
937 {
938 line_table->InsertLineEntry (info->prev_section_sp,
939 state.address - info->prev_sect_file_base_addr,
940 info->prev_row.line,
941 info->prev_row.column,
942 info->prev_row.file,
943 false, // is_stmt
944 false, // basic_block
945 false, // state.prologue_end
946 false, // state.epilogue_begin
947 true); // end_sequence);
948 }
949 }
950 }
951
952 if (curr_in_final_executable)
953 {
954 line_table->InsertLineEntry (info->curr_section_sp,
955 curr_line_section_offset,
956 state.line,
957 state.column,
958 state.file,
959 state.is_stmt,
960 state.basic_block,
961 state.prologue_end,
962 state.epilogue_begin,
963 state.end_sequence);
964 info->prev_section_sp = info->curr_section_sp;
965 }
966 else
967 {
968 // If the current address didn't make it into the final
969 // executable, the current section will be the __text
970 // segment in the .o file, so we need to clear this so
971 // we can catch the next function that did make it into
972 // the final executable.
973 info->prev_section_sp.reset();
974 info->curr_section_sp.reset();
975 }
976
977 info->prev_in_final_executable = curr_in_final_executable;
978 }
979 else
980 {
981 // We are not in an object file that contains DWARF for an
982 // N_OSO, this is just a normal DWARF file. The DWARF spec
983 // guarantees that the addresses will be in increasing order
984 // so, since we store line tables in file address order, we
985 // can always just append the line entry without needing to
986 // search for the correct insertion point (we don't need to
987 // use LineEntry::InsertLineEntry()).
988 line_table->AppendLineEntry (info->curr_section_sp,
989 curr_line_section_offset,
990 state.line,
991 state.column,
992 state.file,
993 state.is_stmt,
994 state.basic_block,
995 state.prologue_end,
996 state.epilogue_begin,
997 state.end_sequence);
998 }
999 }
1000
1001 info->prev_row = state;
1002 }
1003}
1004
1005bool
1006SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1007{
1008 assert (sc.comp_unit);
1009 if (sc.comp_unit->GetLineTable() != NULL)
1010 return true;
1011
1012 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
1013 if (dwarf_cu)
1014 {
1015 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001016 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001018 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1019 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001021 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1022 if (line_table_ap.get())
1023 {
1024 ParseDWARFLineTableCallbackInfo info = {
1025 line_table_ap.get(),
1026 m_obj_file->GetSectionList(),
1027 0,
1028 0,
1029 m_debug_map_symfile != NULL,
1030 false,
1031 DWARFDebugLine::Row(),
1032 SectionSP(),
1033 SectionSP()
1034 };
1035 uint32_t offset = cu_line_offset;
1036 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1037 sc.comp_unit->SetLineTable(line_table_ap.release());
1038 return true;
1039 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040 }
1041 }
1042 }
1043 return false;
1044}
1045
1046size_t
1047SymbolFileDWARF::ParseFunctionBlocks
1048(
1049 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001050 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001051 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001052 const DWARFDebugInfoEntry *die,
1053 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001054 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055)
1056{
1057 size_t blocks_added = 0;
1058 while (die != NULL)
1059 {
1060 dw_tag_t tag = die->Tag();
1061
1062 switch (tag)
1063 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001065 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066 case DW_TAG_lexical_block:
1067 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001068 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001069 if (tag == DW_TAG_subprogram)
1070 {
1071 // Skip any DW_TAG_subprogram DIEs that are inside
1072 // of a normal or inlined functions. These will be
1073 // parsed on their own as separate entities.
1074
1075 if (depth > 0)
1076 break;
1077
1078 block = parent_block;
1079 }
1080 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001081 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001082 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001083 parent_block->AddChild(block_sp);
1084 block = block_sp.get();
1085 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001086 DWARFDebugRanges::RangeList ranges;
1087 const char *name = NULL;
1088 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090 int decl_file = 0;
1091 int decl_line = 0;
1092 int decl_column = 0;
1093 int call_file = 0;
1094 int call_line = 0;
1095 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001096 if (die->GetDIENamesAndRanges (this,
1097 dwarf_cu,
1098 name,
1099 mangled_name,
1100 ranges,
1101 decl_file, decl_line, decl_column,
1102 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001103 {
1104 if (tag == DW_TAG_subprogram)
1105 {
1106 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001107 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001109 else if (tag == DW_TAG_inlined_subroutine)
1110 {
1111 // We get called here for inlined subroutines in two ways.
1112 // The first time is when we are making the Function object
1113 // for this inlined concrete instance. Since we're creating a top level block at
1114 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1115 // adjust the containing address.
1116 // The second time is when we are parsing the blocks inside the function that contains
1117 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1118 // function the offset will be for that function.
1119 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1120 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001121 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001122 }
1123 }
1124
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001125 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126
1127 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1128 {
1129 std::auto_ptr<Declaration> decl_ap;
1130 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001131 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1132 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133
1134 std::auto_ptr<Declaration> call_ap;
1135 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001136 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1137 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001139 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140 }
1141
1142 ++blocks_added;
1143
Greg Claytondd7feaf2011-08-12 17:54:33 +00001144 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001146 blocks_added += ParseFunctionBlocks (sc,
1147 block,
1148 dwarf_cu,
1149 die->GetFirstChild(),
1150 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001151 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152 }
1153 }
1154 }
1155 break;
1156 default:
1157 break;
1158 }
1159
Greg Claytondd7feaf2011-08-12 17:54:33 +00001160 // Only parse siblings of the block if we are not at depth zero. A depth
1161 // of zero indicates we are currently parsing the top level
1162 // DW_TAG_subprogram DIE
1163
1164 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001166 else
1167 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168 }
1169 return blocks_added;
1170}
1171
Greg Claytonf0705c82011-10-22 03:33:13 +00001172bool
1173SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1174 const DWARFDebugInfoEntry *parent_die,
1175 ClangASTContext::TemplateParameterInfos &template_param_infos)
1176{
1177
1178 if (parent_die == NULL)
1179 return NULL;
1180
1181 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1182
1183 Args template_parameter_names;
1184 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1185 die != NULL;
1186 die = die->GetSibling())
1187 {
1188 const dw_tag_t tag = die->Tag();
1189
1190 switch (tag)
1191 {
1192 case DW_TAG_template_type_parameter:
1193 case DW_TAG_template_value_parameter:
1194 {
1195 DWARFDebugInfoEntry::Attributes attributes;
1196 const size_t num_attributes = die->GetAttributes (this,
1197 dwarf_cu,
1198 fixed_form_sizes,
1199 attributes);
1200 const char *name = NULL;
1201 Type *lldb_type = NULL;
1202 clang_type_t clang_type = NULL;
1203 uint64_t uval64 = 0;
1204 bool uval64_valid = false;
1205 if (num_attributes > 0)
1206 {
1207 DWARFFormValue form_value;
1208 for (size_t i=0; i<num_attributes; ++i)
1209 {
1210 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1211
1212 switch (attr)
1213 {
1214 case DW_AT_name:
1215 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1216 name = form_value.AsCString(&get_debug_str_data());
1217 break;
1218
1219 case DW_AT_type:
1220 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1221 {
1222 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1223 lldb_type = ResolveTypeUID(type_die_offset);
1224 if (lldb_type)
1225 clang_type = lldb_type->GetClangForwardType();
1226 }
1227 break;
1228
1229 case DW_AT_const_value:
1230 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1231 {
1232 uval64_valid = true;
1233 uval64 = form_value.Unsigned();
1234 }
1235 break;
1236 default:
1237 break;
1238 }
1239 }
1240
1241 if (name && lldb_type && clang_type)
1242 {
1243 bool is_signed = false;
1244 template_param_infos.names.push_back(name);
1245 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1246 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1247 {
1248 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1249 template_param_infos.args.push_back (clang::TemplateArgument (llvm::APSInt(apint), clang_qual_type));
1250 }
1251 else
1252 {
1253 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1254 }
1255 }
1256 else
1257 {
1258 return false;
1259 }
1260
1261 }
1262 }
1263 break;
1264
1265 default:
1266 break;
1267 }
1268 }
1269 if (template_param_infos.args.empty())
1270 return false;
1271 return template_param_infos.args.size() == template_param_infos.names.size();
1272}
1273
1274clang::ClassTemplateDecl *
1275SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001276 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001277 const char *parent_name,
1278 int tag_decl_kind,
1279 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1280{
1281 if (template_param_infos.IsValid())
1282 {
1283 std::string template_basename(parent_name);
1284 template_basename.erase (template_basename.find('<'));
1285 ClangASTContext &ast = GetClangASTContext();
1286
1287 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001288 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001289 template_basename.c_str(),
1290 tag_decl_kind,
1291 template_param_infos);
1292 }
1293 return NULL;
1294}
1295
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296size_t
1297SymbolFileDWARF::ParseChildMembers
1298(
1299 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001300 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001301 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001302 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001303 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001304 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1305 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001306 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001307 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001308 bool &is_a_class,
1309 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310)
1311{
1312 if (parent_die == NULL)
1313 return 0;
1314
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315 size_t count = 0;
1316 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001317 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001318 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001319
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001320 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1321 {
1322 dw_tag_t tag = die->Tag();
1323
1324 switch (tag)
1325 {
1326 case DW_TAG_member:
1327 {
1328 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001329 const size_t num_attributes = die->GetAttributes (this,
1330 dwarf_cu,
1331 fixed_form_sizes,
1332 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333 if (num_attributes > 0)
1334 {
1335 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001336 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001338 const char *prop_name = NULL;
1339 const char *prop_getter_name = NULL;
1340 const char *prop_setter_name = NULL;
1341 uint32_t prop_attributes = 0;
1342
1343
Greg Clayton24739922010-10-13 03:15:28 +00001344 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001346 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001347 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348 size_t byte_size = 0;
1349 size_t bit_offset = 0;
1350 size_t bit_size = 0;
1351 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001352 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001353 {
1354 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1355 DWARFFormValue form_value;
1356 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1357 {
1358 switch (attr)
1359 {
1360 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1361 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1362 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1363 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1364 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1365 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1366 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1367 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1368 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001369 if (form_value.BlockData())
1370 {
1371 Value initialValue(0);
1372 Value memberOffset(0);
1373 const DataExtractor& debug_info_data = get_debug_info_data();
1374 uint32_t block_length = form_value.Unsigned();
1375 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1376 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1377 NULL, // clang::ASTContext *
1378 NULL, // ClangExpressionVariableList *
1379 NULL, // ClangExpressionDeclMap *
1380 NULL, // RegisterContext *
1381 debug_info_data,
1382 block_offset,
1383 block_length,
1384 eRegisterKindDWARF,
1385 &initialValue,
1386 memberOffset,
1387 NULL))
1388 {
1389 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1390 }
1391 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392 break;
1393
Greg Clayton8cf05932010-07-22 18:30:50 +00001394 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton24739922010-10-13 03:15:28 +00001395 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001396 case DW_AT_declaration:
1397 case DW_AT_description:
1398 case DW_AT_mutable:
1399 case DW_AT_visibility:
Jim Inghame3ae82a2011-11-12 01:36:43 +00001400
1401 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1402 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1403 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1404 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1405
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406 default:
1407 case DW_AT_sibling:
1408 break;
1409 }
1410 }
1411 }
Sean Callanan5a477cf2010-10-30 01:56:10 +00001412
Greg Clayton44953932011-10-25 01:25:35 +00001413 // Clang has a DWARF generation bug where sometimes it
1414 // represents fields that are references with bad byte size
1415 // and bit size/offset information such as:
1416 //
1417 // DW_AT_byte_size( 0x00 )
1418 // DW_AT_bit_size( 0x40 )
1419 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1420 //
1421 // So check the bit offset to make sure it is sane, and if
1422 // the values are not sane, remove them. If we don't do this
1423 // then we will end up with a crash if we try to use this
1424 // type in an expression when clang becomes unhappy with its
1425 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001426
Greg Clayton44953932011-10-25 01:25:35 +00001427 if (bit_offset > 128)
1428 {
1429 bit_size = 0;
1430 bit_offset = 0;
1431 }
1432
1433 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001434 if (class_language == eLanguageTypeObjC ||
1435 class_language == eLanguageTypeObjC_plus_plus)
1436 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001437
1438 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1439 {
1440 // Not all compilers will mark the vtable pointer
1441 // member as artificial (llvm-gcc). We can't have
1442 // the virtual members in our classes otherwise it
1443 // throws off all child offsets since we end up
1444 // having and extra pointer sized member in our
1445 // class layouts.
1446 is_artificial = true;
1447 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001448
Greg Clayton24739922010-10-13 03:15:28 +00001449 if (is_artificial == false)
1450 {
1451 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001452 clang::FieldDecl *field_decl = NULL;
Greg Claytond16e1e52011-07-12 17:06:17 +00001453 if (member_type)
1454 {
1455 if (accessibility == eAccessNone)
1456 accessibility = default_accessibility;
1457 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458
Jim Inghame3ae82a2011-11-12 01:36:43 +00001459 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
Greg Clayton42ce2f32011-12-12 21:50:19 +00001460 name,
1461 member_type->GetClangLayoutType(),
1462 accessibility,
1463 bit_size);
Greg Claytond16e1e52011-07-12 17:06:17 +00001464 }
1465 else
1466 {
1467 if (name)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001468 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1469 MakeUserID(die->GetOffset()),
1470 name,
1471 encoding_uid);
Greg Claytond16e1e52011-07-12 17:06:17 +00001472 else
Greg Claytone38a5ed2012-01-05 03:57:59 +00001473 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1474 MakeUserID(die->GetOffset()),
1475 encoding_uid);
Greg Claytond16e1e52011-07-12 17:06:17 +00001476 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001477
Greg Claytoncaab74e2012-01-28 00:48:57 +00001478 if (member_byte_offset != UINT32_MAX)
1479 {
1480 // Set the field offset in bits
1481 layout_info.field_offsets.insert(std::make_pair(field_decl, member_byte_offset * 8));
1482 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001483 if (prop_name != NULL)
1484 {
1485
1486 clang::ObjCIvarDecl *ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1487 assert (ivar_decl != NULL);
1488
1489
1490 GetClangASTContext().AddObjCClassProperty (class_clang_type,
1491 prop_name,
1492 0,
1493 ivar_decl,
1494 prop_setter_name,
1495 prop_getter_name,
1496 prop_attributes);
1497 }
Greg Clayton24739922010-10-13 03:15:28 +00001498 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001500 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001501 }
1502 break;
1503
1504 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001505 // Let the type parsing code handle this one for us.
1506 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001507 break;
1508
1509 case DW_TAG_inheritance:
1510 {
1511 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001512 if (default_accessibility == eAccessNone)
1513 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514 // TODO: implement DW_TAG_inheritance type parsing
1515 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001516 const size_t num_attributes = die->GetAttributes (this,
1517 dwarf_cu,
1518 fixed_form_sizes,
1519 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520 if (num_attributes > 0)
1521 {
1522 Declaration decl;
1523 DWARFExpression location;
1524 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001525 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001526 bool is_virtual = false;
1527 bool is_base_of_class = true;
1528 off_t member_offset = 0;
1529 uint32_t i;
1530 for (i=0; i<num_attributes; ++i)
1531 {
1532 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1533 DWARFFormValue form_value;
1534 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1535 {
1536 switch (attr)
1537 {
1538 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1539 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1540 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1541 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1542 case DW_AT_data_member_location:
1543 if (form_value.BlockData())
1544 {
1545 Value initialValue(0);
1546 Value memberOffset(0);
1547 const DataExtractor& debug_info_data = get_debug_info_data();
1548 uint32_t block_length = form_value.Unsigned();
1549 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001550 if (DWARFExpression::Evaluate (NULL,
1551 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001552 NULL,
1553 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001554 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001555 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001556 block_offset,
1557 block_length,
1558 eRegisterKindDWARF,
1559 &initialValue,
1560 memberOffset,
1561 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001562 {
1563 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1564 }
1565 }
1566 break;
1567
1568 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001569 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570 break;
1571
1572 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1573 default:
1574 case DW_AT_sibling:
1575 break;
1576 }
1577 }
1578 }
1579
Greg Clayton526e5af2010-11-13 03:52:47 +00001580 Type *base_class_type = ResolveTypeUID(encoding_uid);
1581 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001582
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001583 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1584 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001585 if (class_language == eLanguageTypeObjC)
1586 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001587 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001588 }
1589 else
1590 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001591 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001592 accessibility,
1593 is_virtual,
1594 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001595 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001596 }
1597 }
1598 break;
1599
1600 default:
1601 break;
1602 }
1603 }
1604 return count;
1605}
1606
1607
1608clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001609SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001610{
1611 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001612 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001613 {
1614 DWARFCompileUnitSP cu_sp;
1615 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1616 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001617 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001618 }
1619 return NULL;
1620}
1621
1622clang::DeclContext*
1623SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1624{
Greg Clayton81c22f62011-10-19 18:09:39 +00001625 if (UserIDMatches(type_uid))
1626 return GetClangDeclContextForDIEOffset (sc, type_uid);
1627 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001628}
1629
1630Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001631SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001632{
Greg Clayton81c22f62011-10-19 18:09:39 +00001633 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001635 DWARFDebugInfo* debug_info = DebugInfo();
1636 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001637 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001638 DWARFCompileUnitSP cu_sp;
1639 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001640 const bool assert_not_being_parsed = true;
1641 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001642 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001643 }
1644 return NULL;
1645}
1646
Greg Claytoncab36a32011-12-08 05:16:30 +00001647Type*
1648SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
1649{
1650 if (die != NULL)
1651 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001652 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1653 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001654 GetObjectFile()->GetModule()->LogMessage (log.get(),
1655 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1656 die->GetOffset(),
1657 DW_TAG_value_to_name(die->Tag()),
1658 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001659
Greg Claytoncab36a32011-12-08 05:16:30 +00001660 // We might be coming in in the middle of a type tree (a class
1661 // withing a class, an enum within a class), so parse any needed
1662 // parent DIEs before we get to this one...
1663 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1664 switch (decl_ctx_die->Tag())
1665 {
1666 case DW_TAG_structure_type:
1667 case DW_TAG_union_type:
1668 case DW_TAG_class_type:
1669 {
1670 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001671 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001672 GetObjectFile()->GetModule()->LogMessage (log.get(),
1673 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1674 die->GetOffset(),
1675 DW_TAG_value_to_name(die->Tag()),
1676 die->GetName(this, cu),
1677 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001678
Greg Claytoncab36a32011-12-08 05:16:30 +00001679 Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
Greg Clayton3bffb082011-12-10 02:15:28 +00001680 if (DW_TAG_is_function_tag(die->Tag()))
1681 {
1682 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001683 GetObjectFile()->GetModule()->LogMessage (log.get(),
1684 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
1685 die->GetOffset(),
1686 DW_TAG_value_to_name(die->Tag()),
1687 die->GetName(this, cu),
1688 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001689 // Ask the type to complete itself if it already hasn't since if we
1690 // want a function (method or static) from a class, the class must
1691 // create itself and add it's own methods and class functions.
1692 if (parent_type)
1693 parent_type->GetClangFullType();
1694 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001695 }
1696 break;
1697
1698 default:
1699 break;
1700 }
1701 return ResolveType (cu, die);
1702 }
1703 return NULL;
1704}
1705
Greg Clayton6beaaa62011-01-17 03:46:26 +00001706// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1707// SymbolFileDWARF objects to detect if this DWARF file is the one that
1708// can resolve a clang_type.
1709bool
1710SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1711{
1712 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1713 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1714 return die != NULL;
1715}
1716
1717
Greg Clayton1be10fc2010-09-29 01:12:09 +00001718lldb::clang_type_t
1719SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1720{
1721 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001722 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1723 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001724 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001725 {
1726 // We have already resolved this type...
1727 return clang_type;
1728 }
1729 // Once we start resolving this type, remove it from the forward declaration
1730 // map in case anyone child members or other types require this type to get resolved.
1731 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1732 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001733 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001734
Greg Clayton1be10fc2010-09-29 01:12:09 +00001735
Greg Clayton85ae2e12011-10-18 23:36:41 +00001736 // Disable external storage for this type so we don't get anymore
1737 // clang::ExternalASTSource queries for this type.
1738 ClangASTContext::SetHasExternalStorage (clang_type, false);
1739
Greg Clayton450e3f32010-10-12 02:24:53 +00001740 DWARFDebugInfo* debug_info = DebugInfo();
1741
Greg Clayton96d7d742010-11-10 23:42:09 +00001742 DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001743 Type *type = m_die_to_type.lookup (die);
1744
1745 const dw_tag_t tag = die->Tag();
1746
Greg Clayton3bffb082011-12-10 02:15:28 +00001747 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1748 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001749 GetObjectFile()->GetModule()->LogMessage (log.get(),
1750 "0x%8.8llx: %s '%s' resolving forward declaration...\n",
1751 MakeUserID(die->GetOffset()),
1752 DW_TAG_value_to_name(tag),
1753 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001754 assert (clang_type);
1755 DWARFDebugInfoEntry::Attributes attributes;
1756
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001757 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001758
1759 switch (tag)
1760 {
1761 case DW_TAG_structure_type:
1762 case DW_TAG_union_type:
1763 case DW_TAG_class_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001764 ast.StartTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00001765 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001766 LayoutInfo layout_info;
1767 if (die->HasChildren())
Greg Clayton1be10fc2010-09-29 01:12:09 +00001768 {
Greg Claytonc93237c2010-10-01 20:48:32 +00001769
Greg Claytoncaab74e2012-01-28 00:48:57 +00001770 LanguageType class_language = eLanguageTypeUnknown;
1771 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
1772 if (is_objc_class)
1773 class_language = eLanguageTypeObjC;
Greg Claytonc93237c2010-10-01 20:48:32 +00001774
Greg Claytoncaab74e2012-01-28 00:48:57 +00001775 int tag_decl_kind = -1;
1776 AccessType default_accessibility = eAccessNone;
1777 if (tag == DW_TAG_structure_type)
Greg Claytonc93237c2010-10-01 20:48:32 +00001778 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001779 tag_decl_kind = clang::TTK_Struct;
1780 default_accessibility = eAccessPublic;
Greg Claytonc93237c2010-10-01 20:48:32 +00001781 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001782 else if (tag == DW_TAG_union_type)
Greg Clayton450e3f32010-10-12 02:24:53 +00001783 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001784 tag_decl_kind = clang::TTK_Union;
1785 default_accessibility = eAccessPublic;
1786 }
1787 else if (tag == DW_TAG_class_type)
1788 {
1789 tag_decl_kind = clang::TTK_Class;
1790 default_accessibility = eAccessPrivate;
1791 }
1792
1793 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1794 std::vector<clang::CXXBaseSpecifier *> base_classes;
1795 std::vector<int> member_accessibilities;
1796 bool is_a_class = false;
1797 // Parse members and base classes first
1798 DWARFDIECollection member_function_dies;
1799
1800 ParseChildMembers (sc,
1801 curr_cu,
1802 die,
1803 clang_type,
1804 class_language,
1805 base_classes,
1806 member_accessibilities,
1807 member_function_dies,
1808 default_accessibility,
1809 is_a_class,
1810 layout_info);
1811
1812 // Now parse any methods if there were any...
1813 size_t num_functions = member_function_dies.Size();
1814 if (num_functions > 0)
1815 {
1816 for (size_t i=0; i<num_functions; ++i)
1817 {
1818 ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i));
1819 }
1820 }
Greg Clayton450e3f32010-10-12 02:24:53 +00001821
Greg Claytoncaab74e2012-01-28 00:48:57 +00001822 if (class_language == eLanguageTypeObjC)
1823 {
1824 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(clang_type));
1825 if (!class_str.empty())
Greg Clayton5009f9d2011-10-27 17:55:14 +00001826 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001827
1828 DIEArray method_die_offsets;
1829 if (m_using_apple_tables)
Greg Claytond4a2b372011-09-12 23:21:58 +00001830 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001831 if (m_apple_objc_ap.get())
1832 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
1833 }
1834 else
1835 {
1836 if (!m_indexed)
1837 Index ();
Greg Clayton450e3f32010-10-12 02:24:53 +00001838
Greg Claytoncaab74e2012-01-28 00:48:57 +00001839 ConstString class_name (class_str.c_str());
1840 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
1841 }
1842
1843 if (!method_die_offsets.empty())
1844 {
1845 DWARFDebugInfo* debug_info = DebugInfo();
1846
1847 DWARFCompileUnit* method_cu = NULL;
1848 const size_t num_matches = method_die_offsets.size();
1849 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00001850 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001851 const dw_offset_t die_offset = method_die_offsets[i];
1852 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
1853
1854 if (method_die)
1855 ResolveType (method_cu, method_die);
1856 else
Greg Clayton95d87902011-11-11 03:16:25 +00001857 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001858 if (m_using_apple_tables)
1859 {
1860 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
1861 die_offset, class_str.c_str());
1862 }
1863 }
1864 }
Greg Clayton450e3f32010-10-12 02:24:53 +00001865 }
1866 }
1867 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001868
1869 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
1870 // need to tell the clang type it is actually a class.
1871 if (class_language != eLanguageTypeObjC)
1872 {
1873 if (is_a_class && tag_decl_kind != clang::TTK_Class)
1874 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
1875 }
Greg Claytonc93237c2010-10-01 20:48:32 +00001876
Greg Claytoncaab74e2012-01-28 00:48:57 +00001877 // Since DW_TAG_structure_type gets used for both classes
1878 // and structures, we may need to set any DW_TAG_member
1879 // fields to have a "private" access if none was specified.
1880 // When we parsed the child members we tracked that actual
1881 // accessibility value for each DW_TAG_member in the
1882 // "member_accessibilities" array. If the value for the
1883 // member is zero, then it was set to the "default_accessibility"
1884 // which for structs was "public". Below we correct this
1885 // by setting any fields to "private" that weren't correctly
1886 // set.
1887 if (is_a_class && !member_accessibilities.empty())
1888 {
1889 // This is a class and all members that didn't have
1890 // their access specified are private.
1891 ast.SetDefaultAccessForRecordFields (clang_type,
1892 eAccessPrivate,
1893 &member_accessibilities.front(),
1894 member_accessibilities.size());
1895 }
Greg Claytonc93237c2010-10-01 20:48:32 +00001896
Greg Claytoncaab74e2012-01-28 00:48:57 +00001897 if (!base_classes.empty())
1898 {
1899 ast.SetBaseClassesForClassType (clang_type,
1900 &base_classes.front(),
1901 base_classes.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001902
Greg Claytoncaab74e2012-01-28 00:48:57 +00001903 // Clang will copy each CXXBaseSpecifier in "base_classes"
1904 // so we have to free them all.
1905 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
1906 base_classes.size());
1907 }
1908
Greg Claytonc93237c2010-10-01 20:48:32 +00001909 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001910#if 0
1911 // Disable assisted layout until we get the clang side hooked up
1912 if (!layout_info.field_offsets.empty())
1913 {
1914 if (type)
1915 layout_info.bit_size = type->GetByteSize() * 8;
1916 if (layout_info.bit_size == 0)
1917 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_byte_size, 0) * 8;
1918 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
1919 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
1920 if (record_type)
1921 {
1922 const clang::RecordDecl *record_decl = record_type->getDecl();
1923
1924 if (log)
1925 GetObjectFile()->GetModule()->LogMessage (log.get(),
1926 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[0], vbase_offsets[0])",
1927 clang_type,
1928 record_decl,
1929 layout_info.bit_size,
1930 layout_info.alignment,
1931 (uint32_t)layout_info.field_offsets.size());
1932
1933
1934 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
1935 }
1936 }
1937#endif
Greg Claytonc93237c2010-10-01 20:48:32 +00001938 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001939 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00001940 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001941
1942 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001943 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001944 if (die->HasChildren())
1945 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001946 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1947 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001948 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001949 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001950 return clang_type;
1951
1952 default:
1953 assert(false && "not a forward clang type decl!");
1954 break;
1955 }
1956 return NULL;
1957}
1958
Greg Claytonc685f8e2010-09-15 04:15:46 +00001959Type*
Greg Clayton96d7d742010-11-10 23:42:09 +00001960SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00001961{
1962 if (type_die != NULL)
1963 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00001964 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00001965
Greg Claytonc685f8e2010-09-15 04:15:46 +00001966 if (type == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001967 type = GetTypeForDIE (curr_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00001968
Greg Clayton24739922010-10-13 03:15:28 +00001969 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00001970 {
1971 if (type != DIE_IS_BEING_PARSED)
1972 return type;
1973
1974 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
1975 type_die->GetOffset(),
1976 DW_TAG_value_to_name(type_die->Tag()),
1977 type_die->GetName(this, curr_cu));
1978
1979 }
1980 else
1981 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001982 }
1983 return NULL;
1984}
1985
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986CompileUnit*
Greg Clayton96d7d742010-11-10 23:42:09 +00001987SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001988{
1989 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00001990 if (curr_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001991 {
1992 // The symbol vendor doesn't know about this compile unit, we
1993 // need to parse and add it to the symbol vendor object.
1994 CompUnitSP dc_cu;
Greg Clayton96d7d742010-11-10 23:42:09 +00001995 ParseCompileUnit(curr_cu, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001996 if (dc_cu.get())
1997 {
1998 // Figure out the compile unit index if we weren't given one
Greg Clayton016a95e2010-09-14 02:20:48 +00001999 if (cu_idx == UINT32_MAX)
Greg Clayton96d7d742010-11-10 23:42:09 +00002000 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002001
2002 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx);
Greg Clayton450e3f32010-10-12 02:24:53 +00002003
2004 if (m_debug_map_symfile)
2005 m_debug_map_symfile->SetCompileUnit(this, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002006 }
2007 }
Greg Clayton96d7d742010-11-10 23:42:09 +00002008 return (CompileUnit*)curr_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002009}
2010
2011bool
Greg Clayton96d7d742010-11-10 23:42:09 +00002012SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002013{
2014 sc.Clear();
2015 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00002016 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002017
Greg Clayton81c22f62011-10-19 18:09:39 +00002018 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002019 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002020 sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002021
2022 if (sc.function)
2023 {
2024 sc.module_sp = sc.function->CalculateSymbolContextModule();
2025 return true;
2026 }
2027
2028 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002029}
2030
2031uint32_t
2032SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2033{
2034 Timer scoped_timer(__PRETTY_FUNCTION__,
2035 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
2036 so_addr.GetSection(),
2037 so_addr.GetOffset(),
2038 resolve_scope);
2039 uint32_t resolved = 0;
2040 if (resolve_scope & ( eSymbolContextCompUnit |
2041 eSymbolContextFunction |
2042 eSymbolContextBlock |
2043 eSymbolContextLineEntry))
2044 {
2045 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2046
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002048 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002049 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002050 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002051 if (cu_offset != DW_INVALID_OFFSET)
2052 {
2053 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00002054 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2055 if (curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002056 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002057 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002058 assert(sc.comp_unit != NULL);
2059 resolved |= eSymbolContextCompUnit;
2060
2061 if (resolve_scope & eSymbolContextLineEntry)
2062 {
2063 LineTable *line_table = sc.comp_unit->GetLineTable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002064 if (line_table != NULL)
2065 {
2066 if (so_addr.IsLinkedAddress())
2067 {
2068 Address linked_addr (so_addr);
2069 linked_addr.ResolveLinkedAddress();
2070 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2071 {
2072 resolved |= eSymbolContextLineEntry;
2073 }
2074 }
2075 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
2076 {
2077 resolved |= eSymbolContextLineEntry;
2078 }
2079 }
2080 }
2081
2082 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2083 {
2084 DWARFDebugInfoEntry *function_die = NULL;
2085 DWARFDebugInfoEntry *block_die = NULL;
2086 if (resolve_scope & eSymbolContextBlock)
2087 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002088 curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002089 }
2090 else
2091 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002092 curr_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002093 }
2094
2095 if (function_die != NULL)
2096 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002097 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002098 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002099 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002100 }
2101
2102 if (sc.function != NULL)
2103 {
2104 resolved |= eSymbolContextFunction;
2105
2106 if (resolve_scope & eSymbolContextBlock)
2107 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002108 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002109
2110 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002111 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002112 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002113 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002114 if (sc.block)
2115 resolved |= eSymbolContextBlock;
2116 }
2117 }
2118 }
2119 }
2120 }
2121 }
2122 }
2123 return resolved;
2124}
2125
2126
2127
2128uint32_t
2129SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2130{
2131 const uint32_t prev_size = sc_list.GetSize();
2132 if (resolve_scope & eSymbolContextCompUnit)
2133 {
2134 DWARFDebugInfo* debug_info = DebugInfo();
2135 if (debug_info)
2136 {
2137 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00002138 DWARFCompileUnit* curr_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002139
Greg Clayton96d7d742010-11-10 23:42:09 +00002140 for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002141 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002142 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002143 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
2144 if (check_inlines || file_spec_matches_cu_file_spec)
2145 {
2146 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton96d7d742010-11-10 23:42:09 +00002147 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002148 assert(sc.comp_unit != NULL);
2149
2150 uint32_t file_idx = UINT32_MAX;
2151
2152 // If we are looking for inline functions only and we don't
2153 // find it in the support files, we are done.
2154 if (check_inlines)
2155 {
Jim Ingham87df91b2011-09-23 00:54:11 +00002156 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002157 if (file_idx == UINT32_MAX)
2158 continue;
2159 }
2160
2161 if (line != 0)
2162 {
2163 LineTable *line_table = sc.comp_unit->GetLineTable();
2164
2165 if (line_table != NULL && line != 0)
2166 {
2167 // We will have already looked up the file index if
2168 // we are searching for inline entries.
2169 if (!check_inlines)
Jim Ingham87df91b2011-09-23 00:54:11 +00002170 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002171
2172 if (file_idx != UINT32_MAX)
2173 {
2174 uint32_t found_line;
2175 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2176 found_line = sc.line_entry.line;
2177
Greg Clayton016a95e2010-09-14 02:20:48 +00002178 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002179 {
2180 sc.function = NULL;
2181 sc.block = NULL;
2182 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2183 {
2184 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2185 if (file_vm_addr != LLDB_INVALID_ADDRESS)
2186 {
2187 DWARFDebugInfoEntry *function_die = NULL;
2188 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton96d7d742010-11-10 23:42:09 +00002189 curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002190
2191 if (function_die != NULL)
2192 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002193 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002194 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002195 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002196 }
2197
2198 if (sc.function != NULL)
2199 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002200 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002201
2202 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002203 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002204 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002205 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002206 }
2207 }
2208 }
2209
2210 sc_list.Append(sc);
2211 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2212 }
2213 }
2214 }
2215 else if (file_spec_matches_cu_file_spec && !check_inlines)
2216 {
2217 // only append the context if we aren't looking for inline call sites
2218 // by file and line and if the file spec matches that of the compile unit
2219 sc_list.Append(sc);
2220 }
2221 }
2222 else if (file_spec_matches_cu_file_spec && !check_inlines)
2223 {
2224 // only append the context if we aren't looking for inline call sites
2225 // by file and line and if the file spec matches that of the compile unit
2226 sc_list.Append(sc);
2227 }
2228
2229 if (!check_inlines)
2230 break;
2231 }
2232 }
2233 }
2234 }
2235 return sc_list.GetSize() - prev_size;
2236}
2237
2238void
2239SymbolFileDWARF::Index ()
2240{
2241 if (m_indexed)
2242 return;
2243 m_indexed = true;
2244 Timer scoped_timer (__PRETTY_FUNCTION__,
2245 "SymbolFileDWARF::Index (%s)",
2246 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2247
2248 DWARFDebugInfo* debug_info = DebugInfo();
2249 if (debug_info)
2250 {
2251 uint32_t cu_idx = 0;
2252 const uint32_t num_compile_units = GetNumCompileUnits();
2253 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2254 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002255 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002256
Greg Clayton96d7d742010-11-10 23:42:09 +00002257 bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002258
Greg Clayton96d7d742010-11-10 23:42:09 +00002259 curr_cu->Index (cu_idx,
Greg Clayton83c5cd92010-11-14 22:13:40 +00002260 m_function_basename_index,
2261 m_function_fullname_index,
2262 m_function_method_index,
2263 m_function_selector_index,
2264 m_objc_class_selectors_index,
2265 m_global_index,
2266 m_type_index,
Greg Claytond4a2b372011-09-12 23:21:58 +00002267 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002268
2269 // Keep memory down by clearing DIEs if this generate function
2270 // caused them to be parsed
2271 if (clear_dies)
Greg Clayton96d7d742010-11-10 23:42:09 +00002272 curr_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002273 }
2274
Greg Claytond4a2b372011-09-12 23:21:58 +00002275 m_function_basename_index.Finalize();
2276 m_function_fullname_index.Finalize();
2277 m_function_method_index.Finalize();
2278 m_function_selector_index.Finalize();
2279 m_objc_class_selectors_index.Finalize();
2280 m_global_index.Finalize();
2281 m_type_index.Finalize();
2282 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002283
Greg Clayton24739922010-10-13 03:15:28 +00002284#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002285 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002286 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002287 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2288 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002289 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2290 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2291 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2292 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2293 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2294 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002295 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002296 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002297#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002298 }
2299}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002300
2301bool
2302SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2303{
2304 if (namespace_decl == NULL)
2305 {
2306 // Invalid namespace decl which means we aren't matching only things
2307 // in this symbol file, so return true to indicate it matches this
2308 // symbol file.
2309 return true;
2310 }
2311
2312 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2313
2314 if (namespace_ast == NULL)
2315 return true; // No AST in the "namespace_decl", return true since it
2316 // could then match any symbol file, including this one
2317
2318 if (namespace_ast == GetClangASTContext().getASTContext())
2319 return true; // The ASTs match, return true
2320
2321 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002322 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2323
2324 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002325 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002326
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002327 return false;
2328}
2329
Greg Clayton2506a7a2011-10-12 23:34:26 +00002330bool
2331SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2332 DWARFCompileUnit* cu,
2333 const DWARFDebugInfoEntry* die)
2334{
2335 // No namespace specified, so the answesr i
2336 if (namespace_decl == NULL)
2337 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002338
2339 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002340
Greg Clayton2506a7a2011-10-12 23:34:26 +00002341 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2342 if (decl_ctx_die)
2343 {
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002344
Greg Clayton2506a7a2011-10-12 23:34:26 +00002345 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
2346 if (clang_namespace_decl)
2347 {
2348 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002349 {
2350 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002351 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002352 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002353 }
2354
Greg Clayton2506a7a2011-10-12 23:34:26 +00002355 DeclContextToDIEMap::iterator pos = m_decl_ctx_to_die.find(clang_namespace_decl);
2356
2357 if (pos == m_decl_ctx_to_die.end())
Sean Callananebe60672011-10-13 21:50:33 +00002358 {
2359 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002360 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match in a namespace, but its parent is not the requested namespace");
Sean Callananebe60672011-10-13 21:50:33 +00002361
Greg Clayton2506a7a2011-10-12 23:34:26 +00002362 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002363 }
Greg Clayton2506a7a2011-10-12 23:34:26 +00002364
Greg Claytoncb5860a2011-10-13 23:49:28 +00002365 return pos->second.count (decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002366 }
2367 else
2368 {
2369 // We have a namespace_decl that was not NULL but it contained
2370 // a NULL "clang::NamespaceDecl", so this means the global namespace
2371 // So as long the the contained decl context DIE isn't a namespace
2372 // we should be ok.
2373 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2374 return true;
2375 }
2376 }
Sean Callananebe60672011-10-13 21:50:33 +00002377
2378 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002379 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002380
Greg Clayton2506a7a2011-10-12 23:34:26 +00002381 return false;
2382}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002383uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002384SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002385{
Greg Clayton21f2a492011-10-06 00:09:08 +00002386 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2387
2388 if (log)
2389 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002390 GetObjectFile()->GetModule()->LogMessage (log.get(),
2391 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2392 name.GetCString(),
2393 namespace_decl,
2394 append,
2395 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002396 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002397
2398 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2399 return 0;
2400
Greg Claytonc685f8e2010-09-15 04:15:46 +00002401 DWARFDebugInfo* info = DebugInfo();
2402 if (info == NULL)
2403 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002404
2405 // If we aren't appending the results to this list, then clear the list
2406 if (!append)
2407 variables.Clear();
2408
2409 // Remember how many variables are in the list before we search in case
2410 // we are appending the results to a variable list.
2411 const uint32_t original_size = variables.GetSize();
2412
Greg Claytond4a2b372011-09-12 23:21:58 +00002413 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002414
Greg Clayton97fbc342011-10-20 22:30:33 +00002415 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002416 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002417 if (m_apple_names_ap.get())
2418 {
2419 const char *name_cstr = name.GetCString();
2420 const char *base_name_start;
2421 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002422
Greg Clayton97fbc342011-10-20 22:30:33 +00002423 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2424 base_name_start = name_cstr;
2425
2426 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2427 }
Greg Clayton7f995132011-10-04 22:41:51 +00002428 }
2429 else
2430 {
2431 // Index the DWARF if we haven't already
2432 if (!m_indexed)
2433 Index ();
2434
2435 m_global_index.Find (name, die_offsets);
2436 }
2437
2438 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002439 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002440 {
Greg Clayton7f995132011-10-04 22:41:51 +00002441 SymbolContext sc;
2442 sc.module_sp = m_obj_file->GetModule();
2443 assert (sc.module_sp);
2444
Greg Claytond4a2b372011-09-12 23:21:58 +00002445 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002446 DWARFCompileUnit* dwarf_cu = NULL;
2447 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002448 for (size_t i=0; i<num_matches; ++i)
2449 {
2450 const dw_offset_t die_offset = die_offsets[i];
2451 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002452
Greg Clayton95d87902011-11-11 03:16:25 +00002453 if (die)
2454 {
2455 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2456 assert(sc.comp_unit != NULL);
2457
2458 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2459 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002460
Greg Clayton95d87902011-11-11 03:16:25 +00002461 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002462
Greg Clayton95d87902011-11-11 03:16:25 +00002463 if (variables.GetSize() - original_size >= max_matches)
2464 break;
2465 }
2466 else
2467 {
2468 if (m_using_apple_tables)
2469 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002470 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2471 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002472 }
2473 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002474 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002475 }
2476
2477 // Return the number of variable that were appended to the list
2478 return variables.GetSize() - original_size;
2479}
2480
2481uint32_t
2482SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2483{
Greg Clayton21f2a492011-10-06 00:09:08 +00002484 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2485
2486 if (log)
2487 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002488 GetObjectFile()->GetModule()->LogMessage (log.get(),
2489 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2490 regex.GetText(),
2491 append,
2492 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002493 }
2494
Greg Claytonc685f8e2010-09-15 04:15:46 +00002495 DWARFDebugInfo* info = DebugInfo();
2496 if (info == NULL)
2497 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002498
2499 // If we aren't appending the results to this list, then clear the list
2500 if (!append)
2501 variables.Clear();
2502
2503 // Remember how many variables are in the list before we search in case
2504 // we are appending the results to a variable list.
2505 const uint32_t original_size = variables.GetSize();
2506
Greg Clayton7f995132011-10-04 22:41:51 +00002507 DIEArray die_offsets;
2508
Greg Clayton97fbc342011-10-20 22:30:33 +00002509 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002510 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002511 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002512 {
2513 DWARFMappedHash::DIEInfoArray hash_data_array;
2514 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2515 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2516 }
Greg Clayton7f995132011-10-04 22:41:51 +00002517 }
2518 else
2519 {
2520 // Index the DWARF if we haven't already
2521 if (!m_indexed)
2522 Index ();
2523
2524 m_global_index.Find (regex, die_offsets);
2525 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002526
Greg Claytonc685f8e2010-09-15 04:15:46 +00002527 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +00002528 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002529 assert (sc.module_sp);
2530
Greg Claytond4a2b372011-09-12 23:21:58 +00002531 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002532 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002533 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002534 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002535 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002536 DWARFDebugInfo* debug_info = DebugInfo();
2537 for (size_t i=0; i<num_matches; ++i)
2538 {
2539 const dw_offset_t die_offset = die_offsets[i];
2540 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002541
2542 if (die)
2543 {
2544 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002545
Greg Clayton95d87902011-11-11 03:16:25 +00002546 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002547
Greg Clayton95d87902011-11-11 03:16:25 +00002548 if (variables.GetSize() - original_size >= max_matches)
2549 break;
2550 }
2551 else
2552 {
2553 if (m_using_apple_tables)
2554 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002555 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2556 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002557 }
2558 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002559 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002560 }
2561
2562 // Return the number of variable that were appended to the list
2563 return variables.GetSize() - original_size;
2564}
2565
Greg Claytonaa044962011-10-13 00:59:38 +00002566
Jim Ingham4cda6e02011-10-07 22:23:45 +00002567bool
2568SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2569 DWARFCompileUnit *&dwarf_cu,
2570 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002571{
Greg Claytonaa044962011-10-13 00:59:38 +00002572 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2573 return ResolveFunction (dwarf_cu, die, sc_list);
2574}
2575
2576
2577bool
2578SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2579 const DWARFDebugInfoEntry *die,
2580 SymbolContextList& sc_list)
2581{
Greg Clayton9e315582011-09-02 04:03:59 +00002582 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002583
2584 if (die == NULL)
2585 return false;
2586
Jim Ingham4cda6e02011-10-07 22:23:45 +00002587 // If we were passed a die that is not a function, just return false...
2588 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2589 return false;
2590
2591 const DWARFDebugInfoEntry* inlined_die = NULL;
2592 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002593 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002594 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002595
Jim Ingham4cda6e02011-10-07 22:23:45 +00002596 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002597 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002598 if (die->Tag() == DW_TAG_subprogram)
2599 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002600 }
2601 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002602 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002603 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002604 {
2605 Address addr;
2606 // Parse all blocks if needed
2607 if (inlined_die)
2608 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002609 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00002610 assert (sc.block != NULL);
2611 if (sc.block->GetStartAddress (addr) == false)
2612 addr.Clear();
2613 }
2614 else
2615 {
2616 sc.block = NULL;
2617 addr = sc.function->GetAddressRange().GetBaseAddress();
2618 }
2619
2620 if (addr.IsValid())
2621 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002622 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002623 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002624 }
2625 }
2626
Greg Claytonaa044962011-10-13 00:59:38 +00002627 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002628}
2629
Greg Clayton7f995132011-10-04 22:41:51 +00002630void
2631SymbolFileDWARF::FindFunctions (const ConstString &name,
2632 const NameToDIE &name_to_die,
2633 SymbolContextList& sc_list)
2634{
Greg Claytond4a2b372011-09-12 23:21:58 +00002635 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002636 if (name_to_die.Find (name, die_offsets))
2637 {
2638 ParseFunctions (die_offsets, sc_list);
2639 }
2640}
2641
2642
2643void
2644SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2645 const NameToDIE &name_to_die,
2646 SymbolContextList& sc_list)
2647{
2648 DIEArray die_offsets;
2649 if (name_to_die.Find (regex, die_offsets))
2650 {
2651 ParseFunctions (die_offsets, sc_list);
2652 }
2653}
2654
2655
2656void
2657SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2658 const DWARFMappedHash::MemoryTable &memory_table,
2659 SymbolContextList& sc_list)
2660{
2661 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002662 DWARFMappedHash::DIEInfoArray hash_data_array;
2663 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002664 {
Greg Claytond1767f02011-12-08 02:13:16 +00002665 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002666 ParseFunctions (die_offsets, sc_list);
2667 }
2668}
2669
2670void
2671SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2672 SymbolContextList& sc_list)
2673{
2674 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002675 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002676 {
Greg Clayton7f995132011-10-04 22:41:51 +00002677 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00002678
2679 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002680 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002681 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002682 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00002683 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002684 }
2685 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002686}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002687
Jim Ingham4cda6e02011-10-07 22:23:45 +00002688bool
2689SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2690 const DWARFCompileUnit *dwarf_cu,
2691 uint32_t name_type_mask,
2692 const char *partial_name,
2693 const char *base_name_start,
2694 const char *base_name_end)
2695{
2696 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2697 if (name_type_mask == eFunctionNameTypeMethod
2698 || name_type_mask == eFunctionNameTypeBase)
2699 {
Greg Claytonf0705c82011-10-22 03:33:13 +00002700 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2701 if (!containing_decl_ctx)
2702 return false;
2703
2704 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
2705
2706 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2707 return false;
2708 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2709 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002710 }
2711
2712 // Now we need to check whether the name we got back for this type matches the extra specifications
2713 // that were in the name we're looking up:
2714 if (base_name_start != partial_name || *base_name_end != '\0')
2715 {
2716 // First see if the stuff to the left matches the full name. To do that let's see if
2717 // we can pull out the mips linkage name attribute:
2718
2719 Mangled best_name;
2720
2721 DWARFDebugInfoEntry::Attributes attributes;
2722 die->GetAttributes(this, dwarf_cu, NULL, attributes);
2723 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
2724 if (idx != UINT32_MAX)
2725 {
2726 DWARFFormValue form_value;
2727 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
2728 {
2729 const char *name = form_value.AsCString(&get_debug_str_data());
2730 best_name.SetValue (name, true);
2731 }
2732 }
2733 if (best_name)
2734 {
2735 const char *demangled = best_name.GetDemangledName().GetCString();
2736 if (demangled)
2737 {
2738 std::string name_no_parens(partial_name, base_name_end - partial_name);
2739 if (strstr (demangled, name_no_parens.c_str()) == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002740 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002741 }
2742 }
2743 }
2744
2745 return true;
2746}
Greg Claytonc685f8e2010-09-15 04:15:46 +00002747
Greg Clayton0c5cd902010-06-28 21:30:43 +00002748uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00002749SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00002750 const lldb_private::ClangNamespaceDecl *namespace_decl,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002751 uint32_t name_type_mask,
2752 bool append,
2753 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002754{
2755 Timer scoped_timer (__PRETTY_FUNCTION__,
2756 "SymbolFileDWARF::FindFunctions (name = '%s')",
2757 name.AsCString());
2758
Greg Clayton21f2a492011-10-06 00:09:08 +00002759 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2760
2761 if (log)
2762 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002763 GetObjectFile()->GetModule()->LogMessage (log.get(),
2764 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2765 name.GetCString(),
2766 name_type_mask,
2767 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002768 }
2769
Greg Clayton0c5cd902010-06-28 21:30:43 +00002770 // If we aren't appending the results to this list, then clear the list
2771 if (!append)
2772 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00002773
2774 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2775 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002776
2777 // If name is empty then we won't find anything.
2778 if (name.IsEmpty())
2779 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002780
2781 // Remember how many sc_list are in the list before we search in case
2782 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002783
Greg Clayton9e315582011-09-02 04:03:59 +00002784 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00002785
Jim Ingham4cda6e02011-10-07 22:23:45 +00002786 const char *name_cstr = name.GetCString();
2787 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
2788 const char *base_name_start = name_cstr;
2789 const char *base_name_end = name_cstr + strlen(name_cstr);
2790
2791 if (name_type_mask & eFunctionNameTypeAuto)
2792 {
2793 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
2794 effective_name_type_mask = eFunctionNameTypeFull;
2795 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
2796 effective_name_type_mask = eFunctionNameTypeFull;
2797 else
2798 {
2799 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2800 effective_name_type_mask |= eFunctionNameTypeSelector;
2801
2802 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2803 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
2804 }
2805 }
2806 else
2807 {
2808 effective_name_type_mask = name_type_mask;
2809 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
2810 {
2811 // If they've asked for a CPP method or function name and it can't be that, we don't
2812 // even need to search for CPP methods or names.
2813 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2814 {
2815 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
2816 if (effective_name_type_mask == eFunctionNameTypeNone)
2817 return 0;
2818 }
2819 }
2820
2821 if (effective_name_type_mask & eFunctionNameTypeSelector)
2822 {
2823 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2824 {
2825 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
2826 if (effective_name_type_mask == eFunctionNameTypeNone)
2827 return 0;
2828 }
2829 }
2830 }
2831
2832 DWARFDebugInfo* info = DebugInfo();
2833 if (info == NULL)
2834 return 0;
2835
Greg Claytonaa044962011-10-13 00:59:38 +00002836 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00002837 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00002838 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002839 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00002840 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002841
2842 DIEArray die_offsets;
2843
2844 uint32_t num_matches = 0;
2845
2846 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00002847 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002848 // If they asked for the full name, match what they typed. At some point we may
2849 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
2850 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00002851 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002852 for (uint32_t i = 0; i < num_matches; i++)
2853 {
Greg Clayton95d87902011-11-11 03:16:25 +00002854 const dw_offset_t die_offset = die_offsets[i];
2855 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00002856 if (die)
2857 {
Sean Callanan213fdb82011-10-13 01:49:10 +00002858 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2859 continue;
2860
Greg Claytonaa044962011-10-13 00:59:38 +00002861 ResolveFunction (dwarf_cu, die, sc_list);
2862 }
Greg Clayton95d87902011-11-11 03:16:25 +00002863 else
2864 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002865 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2866 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002867 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002868 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002869 }
2870 else
2871 {
2872 if (effective_name_type_mask & eFunctionNameTypeSelector)
2873 {
2874 if (namespace_decl && *namespace_decl)
2875 return 0; // no selectors in namespaces
2876
2877 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2878 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
2879 // and if it is an ObjC method name, we're good.
2880
2881 for (uint32_t i = 0; i < num_matches; i++)
2882 {
Greg Clayton95d87902011-11-11 03:16:25 +00002883 const dw_offset_t die_offset = die_offsets[i];
2884 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00002885 if (die)
2886 {
2887 const char *die_name = die->GetName(this, dwarf_cu);
2888 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
2889 ResolveFunction (dwarf_cu, die, sc_list);
2890 }
Greg Clayton95d87902011-11-11 03:16:25 +00002891 else
2892 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002893 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2894 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002895 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002896 }
2897 die_offsets.clear();
2898 }
2899
2900 if (effective_name_type_mask & eFunctionNameTypeMethod
2901 || effective_name_type_mask & eFunctionNameTypeBase)
2902 {
2903 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
2904 (namespace_decl && *namespace_decl))
2905 return 0; // no methods in namespaces
2906
2907 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
2908 // extract the base name, look that up, and if there is any other information in the name we were
2909 // passed in we have to post-filter based on that.
2910
2911 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
2912 std::string base_name(base_name_start, base_name_end - base_name_start);
2913 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
2914
2915 for (uint32_t i = 0; i < num_matches; i++)
2916 {
Greg Clayton95d87902011-11-11 03:16:25 +00002917 const dw_offset_t die_offset = die_offsets[i];
2918 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00002919 if (die)
2920 {
2921 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2922 continue;
2923
2924 if (!FunctionDieMatchesPartialName(die,
2925 dwarf_cu,
2926 effective_name_type_mask,
2927 name_cstr,
2928 base_name_start,
2929 base_name_end))
2930 continue;
2931
2932 // If we get to here, the die is good, and we should add it:
2933 ResolveFunction (dwarf_cu, die, sc_list);
2934 }
Greg Clayton95d87902011-11-11 03:16:25 +00002935 else
2936 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002937 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2938 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002939 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002940 }
2941 die_offsets.clear();
2942 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002943 }
2944 }
Greg Clayton7f995132011-10-04 22:41:51 +00002945 }
2946 else
2947 {
2948
2949 // Index the DWARF if we haven't already
2950 if (!m_indexed)
2951 Index ();
2952
Greg Clayton7f995132011-10-04 22:41:51 +00002953 if (name_type_mask & eFunctionNameTypeFull)
2954 FindFunctions (name, m_function_fullname_index, sc_list);
2955
Jim Ingham4cda6e02011-10-07 22:23:45 +00002956 std::string base_name(base_name_start, base_name_end - base_name_start);
2957 ConstString base_name_const(base_name.c_str());
2958 DIEArray die_offsets;
2959 DWARFCompileUnit *dwarf_cu = NULL;
2960
2961 if (effective_name_type_mask & eFunctionNameTypeBase)
2962 {
2963 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00002964 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002965 {
Greg Claytonaa044962011-10-13 00:59:38 +00002966 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2967 if (die)
2968 {
Sean Callanan213fdb82011-10-13 01:49:10 +00002969 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2970 continue;
2971
Greg Claytonaa044962011-10-13 00:59:38 +00002972 if (!FunctionDieMatchesPartialName(die,
2973 dwarf_cu,
2974 effective_name_type_mask,
2975 name_cstr,
2976 base_name_start,
2977 base_name_end))
2978 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002979
Greg Claytonaa044962011-10-13 00:59:38 +00002980 // If we get to here, the die is good, and we should add it:
2981 ResolveFunction (dwarf_cu, die, sc_list);
2982 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002983 }
2984 die_offsets.clear();
2985 }
2986
Jim Inghamea8005a2011-10-11 01:18:11 +00002987 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002988 {
Sean Callanan213fdb82011-10-13 01:49:10 +00002989 if (namespace_decl && *namespace_decl)
2990 return 0; // no methods in namespaces
2991
Jim Ingham4cda6e02011-10-07 22:23:45 +00002992 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
2993 {
Greg Claytonaa044962011-10-13 00:59:38 +00002994 for (uint32_t i = 0; i < num_base; i++)
2995 {
2996 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2997 if (die)
2998 {
2999 if (!FunctionDieMatchesPartialName(die,
3000 dwarf_cu,
3001 effective_name_type_mask,
3002 name_cstr,
3003 base_name_start,
3004 base_name_end))
3005 continue;
3006
3007 // If we get to here, the die is good, and we should add it:
3008 ResolveFunction (dwarf_cu, die, sc_list);
3009 }
3010 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003011 }
3012 die_offsets.clear();
3013 }
Greg Clayton7f995132011-10-04 22:41:51 +00003014
Sean Callanan213fdb82011-10-13 01:49:10 +00003015 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003016 {
Greg Clayton7f995132011-10-04 22:41:51 +00003017 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003018 }
3019
Greg Clayton4d01ace2011-09-29 16:58:15 +00003020 }
3021
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003022 // Return the number of variable that were appended to the list
3023 return sc_list.GetSize() - original_size;
3024}
3025
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003026uint32_t
3027SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list)
3028{
3029 Timer scoped_timer (__PRETTY_FUNCTION__,
3030 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3031 regex.GetText());
3032
Greg Clayton21f2a492011-10-06 00:09:08 +00003033 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3034
3035 if (log)
3036 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003037 GetObjectFile()->GetModule()->LogMessage (log.get(),
3038 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3039 regex.GetText(),
3040 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003041 }
3042
3043
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003044 // If we aren't appending the results to this list, then clear the list
3045 if (!append)
3046 sc_list.Clear();
3047
3048 // Remember how many sc_list are in the list before we search in case
3049 // we are appending the results to a variable list.
3050 uint32_t original_size = sc_list.GetSize();
3051
Greg Clayton97fbc342011-10-20 22:30:33 +00003052 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003053 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003054 if (m_apple_names_ap.get())
3055 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003056 }
3057 else
3058 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003059 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003060 if (!m_indexed)
3061 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003062
Greg Clayton7f995132011-10-04 22:41:51 +00003063 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003064
Greg Clayton7f995132011-10-04 22:41:51 +00003065 FindFunctions (regex, m_function_fullname_index, sc_list);
3066 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003067
3068 // Return the number of variable that were appended to the list
3069 return sc_list.GetSize() - original_size;
3070}
Jim Ingham318c9f22011-08-26 19:44:13 +00003071
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003072uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003073SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3074 const ConstString &name,
3075 const lldb_private::ClangNamespaceDecl *namespace_decl,
3076 bool append,
3077 uint32_t max_matches,
3078 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003079{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003080 DWARFDebugInfo* info = DebugInfo();
3081 if (info == NULL)
3082 return 0;
3083
Greg Clayton21f2a492011-10-06 00:09:08 +00003084 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3085
3086 if (log)
3087 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003088 GetObjectFile()->GetModule()->LogMessage (log.get(),
3089 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", append=%u, max_matches=%u, type_list)",
3090 name.GetCString(),
3091 append,
3092 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003093 }
3094
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003095 // If we aren't appending the results to this list, then clear the list
3096 if (!append)
3097 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003098
3099 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3100 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003101
Greg Claytond4a2b372011-09-12 23:21:58 +00003102 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003103
Greg Clayton97fbc342011-10-20 22:30:33 +00003104 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003105 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003106 if (m_apple_types_ap.get())
3107 {
3108 const char *name_cstr = name.GetCString();
3109 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3110 }
Greg Clayton7f995132011-10-04 22:41:51 +00003111 }
3112 else
3113 {
3114 if (!m_indexed)
3115 Index ();
3116
3117 m_type_index.Find (name, die_offsets);
3118 }
3119
Greg Clayton7f995132011-10-04 22:41:51 +00003120 const size_t num_matches = die_offsets.size();
3121
Greg Claytond4a2b372011-09-12 23:21:58 +00003122 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003123 {
Greg Clayton7f995132011-10-04 22:41:51 +00003124 const uint32_t initial_types_size = types.GetSize();
3125 DWARFCompileUnit* dwarf_cu = NULL;
3126 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003127 DWARFDebugInfo* debug_info = DebugInfo();
3128 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003129 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003130 const dw_offset_t die_offset = die_offsets[i];
3131 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3132
Greg Clayton95d87902011-11-11 03:16:25 +00003133 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003134 {
Greg Clayton95d87902011-11-11 03:16:25 +00003135 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3136 continue;
3137
3138 Type *matching_type = ResolveType (dwarf_cu, die);
3139 if (matching_type)
3140 {
3141 // We found a type pointer, now find the shared pointer form our type list
3142 types.InsertUnique (TypeSP (matching_type));
3143 if (types.GetSize() >= max_matches)
3144 break;
3145 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003146 }
Greg Clayton95d87902011-11-11 03:16:25 +00003147 else
3148 {
3149 if (m_using_apple_tables)
3150 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003151 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3152 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003153 }
3154 }
3155
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003156 }
Greg Clayton7f995132011-10-04 22:41:51 +00003157 return types.GetSize() - initial_types_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003158 }
Greg Clayton7f995132011-10-04 22:41:51 +00003159 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003160}
3161
3162
Greg Clayton526e5af2010-11-13 03:52:47 +00003163ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003164SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003165 const ConstString &name,
3166 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003167{
Greg Clayton21f2a492011-10-06 00:09:08 +00003168 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3169
3170 if (log)
3171 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003172 GetObjectFile()->GetModule()->LogMessage (log.get(),
3173 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3174 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003175 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003176
3177 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3178 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003179
Greg Clayton526e5af2010-11-13 03:52:47 +00003180 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003181 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003182 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003183 {
Greg Clayton7f995132011-10-04 22:41:51 +00003184 DIEArray die_offsets;
3185
Greg Clayton526e5af2010-11-13 03:52:47 +00003186 // Index if we already haven't to make sure the compile units
3187 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003188 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003189 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003190 if (m_apple_namespaces_ap.get())
3191 {
3192 const char *name_cstr = name.GetCString();
3193 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3194 }
Greg Clayton7f995132011-10-04 22:41:51 +00003195 }
3196 else
3197 {
3198 if (!m_indexed)
3199 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003200
Greg Clayton7f995132011-10-04 22:41:51 +00003201 m_namespace_index.Find (name, die_offsets);
3202 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003203
3204 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003205 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003206 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003207 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003208 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003209 DWARFDebugInfo* debug_info = DebugInfo();
3210 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003211 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003212 const dw_offset_t die_offset = die_offsets[i];
3213 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003214
Greg Clayton95d87902011-11-11 03:16:25 +00003215 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003216 {
Greg Clayton95d87902011-11-11 03:16:25 +00003217 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3218 continue;
3219
3220 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3221 if (clang_namespace_decl)
3222 {
3223 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3224 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003225 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003226 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003227 }
Greg Clayton95d87902011-11-11 03:16:25 +00003228 else
3229 {
3230 if (m_using_apple_tables)
3231 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003232 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3233 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003234 }
3235 }
3236
Greg Clayton526e5af2010-11-13 03:52:47 +00003237 }
3238 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003239 }
Greg Clayton526e5af2010-11-13 03:52:47 +00003240 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003241}
3242
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003243uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003244SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003245{
3246 // Remember how many sc_list are in the list before we search in case
3247 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003248 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003249
3250 const uint32_t num_die_offsets = die_offsets.size();
3251 // Parse all of the types we found from the pubtypes matches
3252 uint32_t i;
3253 uint32_t num_matches = 0;
3254 for (i = 0; i < num_die_offsets; ++i)
3255 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003256 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3257 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003258 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003259 // We found a type pointer, now find the shared pointer form our type list
Greg Clayton85ae2e12011-10-18 23:36:41 +00003260 types.InsertUnique (TypeSP (matching_type));
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003261 ++num_matches;
3262 if (num_matches >= max_matches)
3263 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003264 }
3265 }
3266
3267 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003268 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003269}
3270
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003271
3272size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003273SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3274 clang::DeclContext *containing_decl_ctx,
3275 TypeSP& type_sp,
3276 DWARFCompileUnit* dwarf_cu,
3277 const DWARFDebugInfoEntry *parent_die,
3278 bool skip_artificial,
3279 bool &is_static,
3280 TypeList* type_list,
3281 std::vector<clang_type_t>& function_param_types,
3282 std::vector<clang::ParmVarDecl*>& function_param_decls,
3283 unsigned &type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003284{
3285 if (parent_die == NULL)
3286 return 0;
3287
Greg Claytond88d7592010-09-15 08:33:30 +00003288 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3289
Greg Clayton7fedea22010-11-16 02:10:54 +00003290 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003291 const DWARFDebugInfoEntry *die;
3292 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3293 {
3294 dw_tag_t tag = die->Tag();
3295 switch (tag)
3296 {
3297 case DW_TAG_formal_parameter:
3298 {
3299 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003300 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003301 if (num_attributes > 0)
3302 {
3303 const char *name = NULL;
3304 Declaration decl;
3305 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003306 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003307 // one of None, Auto, Register, Extern, Static, PrivateExtern
3308
Sean Callanane2ef6e32010-09-23 03:01:22 +00003309 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003310 uint32_t i;
3311 for (i=0; i<num_attributes; ++i)
3312 {
3313 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3314 DWARFFormValue form_value;
3315 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3316 {
3317 switch (attr)
3318 {
3319 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3320 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3321 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3322 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3323 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003324 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003325 case DW_AT_location:
3326 // if (form_value.BlockData())
3327 // {
3328 // const DataExtractor& debug_info_data = debug_info();
3329 // uint32_t block_length = form_value.Unsigned();
3330 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3331 // }
3332 // else
3333 // {
3334 // }
3335 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003336 case DW_AT_const_value:
3337 case DW_AT_default_value:
3338 case DW_AT_description:
3339 case DW_AT_endianity:
3340 case DW_AT_is_optional:
3341 case DW_AT_segment:
3342 case DW_AT_variable_parameter:
3343 default:
3344 case DW_AT_abstract_origin:
3345 case DW_AT_sibling:
3346 break;
3347 }
3348 }
3349 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003350
Greg Clayton0fffff52010-09-24 05:15:53 +00003351 bool skip = false;
3352 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003353 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003354 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003355 {
3356 // In order to determine if a C++ member function is
3357 // "const" we have to look at the const-ness of "this"...
3358 // Ugly, but that
3359 if (arg_idx == 0)
3360 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003361 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003362 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003363 // Often times compilers omit the "this" name for the
3364 // specification DIEs, so we can't rely upon the name
3365 // being in the formal parameter DIE...
3366 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003367 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003368 Type *this_type = ResolveTypeUID (param_type_die_offset);
3369 if (this_type)
3370 {
3371 uint32_t encoding_mask = this_type->GetEncodingMask();
3372 if (encoding_mask & Type::eEncodingIsPointerUID)
3373 {
3374 is_static = false;
3375
3376 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3377 type_quals |= clang::Qualifiers::Const;
3378 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3379 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003380 }
3381 }
3382 }
3383 }
3384 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003385 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003386 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003387 else
3388 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003389
Greg Clayton0fffff52010-09-24 05:15:53 +00003390 // HACK: Objective C formal parameters "self" and "_cmd"
3391 // are not marked as artificial in the DWARF...
Greg Clayton96d7d742010-11-10 23:42:09 +00003392 CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3393 if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Clayton0fffff52010-09-24 05:15:53 +00003394 {
3395 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3396 skip = true;
3397 }
3398 }
3399 }
3400
3401 if (!skip)
3402 {
3403 Type *type = ResolveTypeUID(param_type_die_offset);
3404 if (type)
3405 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003406 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003407
Greg Clayton42ce2f32011-12-12 21:50:19 +00003408 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3409 type->GetClangForwardType(),
3410 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003411 assert(param_var_decl);
3412 function_param_decls.push_back(param_var_decl);
3413 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003414 }
3415 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003416 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003417 }
3418 break;
3419
3420 default:
3421 break;
3422 }
3423 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003424 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003425}
3426
3427size_t
3428SymbolFileDWARF::ParseChildEnumerators
3429(
3430 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003431 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003432 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003433 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003434 const DWARFDebugInfoEntry *parent_die
3435)
3436{
3437 if (parent_die == NULL)
3438 return 0;
3439
3440 size_t enumerators_added = 0;
3441 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003442 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3443
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003444 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3445 {
3446 const dw_tag_t tag = die->Tag();
3447 if (tag == DW_TAG_enumerator)
3448 {
3449 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003450 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003451 if (num_child_attributes > 0)
3452 {
3453 const char *name = NULL;
3454 bool got_value = false;
3455 int64_t enum_value = 0;
3456 Declaration decl;
3457
3458 uint32_t i;
3459 for (i=0; i<num_child_attributes; ++i)
3460 {
3461 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3462 DWARFFormValue form_value;
3463 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3464 {
3465 switch (attr)
3466 {
3467 case DW_AT_const_value:
3468 got_value = true;
3469 enum_value = form_value.Unsigned();
3470 break;
3471
3472 case DW_AT_name:
3473 name = form_value.AsCString(&get_debug_str_data());
3474 break;
3475
3476 case DW_AT_description:
3477 default:
3478 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3479 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3480 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3481 case DW_AT_sibling:
3482 break;
3483 }
3484 }
3485 }
3486
3487 if (name && name[0] && got_value)
3488 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003489 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3490 enumerator_clang_type,
3491 decl,
3492 name,
3493 enum_value,
3494 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003495 ++enumerators_added;
3496 }
3497 }
3498 }
3499 }
3500 return enumerators_added;
3501}
3502
3503void
3504SymbolFileDWARF::ParseChildArrayInfo
3505(
3506 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003507 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003508 const DWARFDebugInfoEntry *parent_die,
3509 int64_t& first_index,
3510 std::vector<uint64_t>& element_orders,
3511 uint32_t& byte_stride,
3512 uint32_t& bit_stride
3513)
3514{
3515 if (parent_die == NULL)
3516 return;
3517
3518 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003519 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003520 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3521 {
3522 const dw_tag_t tag = die->Tag();
3523 switch (tag)
3524 {
3525 case DW_TAG_enumerator:
3526 {
3527 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003528 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003529 if (num_child_attributes > 0)
3530 {
3531 const char *name = NULL;
3532 bool got_value = false;
3533 int64_t enum_value = 0;
3534
3535 uint32_t i;
3536 for (i=0; i<num_child_attributes; ++i)
3537 {
3538 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3539 DWARFFormValue form_value;
3540 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3541 {
3542 switch (attr)
3543 {
3544 case DW_AT_const_value:
3545 got_value = true;
3546 enum_value = form_value.Unsigned();
3547 break;
3548
3549 case DW_AT_name:
3550 name = form_value.AsCString(&get_debug_str_data());
3551 break;
3552
3553 case DW_AT_description:
3554 default:
3555 case DW_AT_decl_file:
3556 case DW_AT_decl_line:
3557 case DW_AT_decl_column:
3558 case DW_AT_sibling:
3559 break;
3560 }
3561 }
3562 }
3563 }
3564 }
3565 break;
3566
3567 case DW_TAG_subrange_type:
3568 {
3569 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003570 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003571 if (num_child_attributes > 0)
3572 {
3573 const char *name = NULL;
3574 bool got_value = false;
3575 uint64_t byte_size = 0;
3576 int64_t enum_value = 0;
3577 uint64_t num_elements = 0;
3578 uint64_t lower_bound = 0;
3579 uint64_t upper_bound = 0;
3580 uint32_t i;
3581 for (i=0; i<num_child_attributes; ++i)
3582 {
3583 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3584 DWARFFormValue form_value;
3585 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3586 {
3587 switch (attr)
3588 {
3589 case DW_AT_const_value:
3590 got_value = true;
3591 enum_value = form_value.Unsigned();
3592 break;
3593
3594 case DW_AT_name:
3595 name = form_value.AsCString(&get_debug_str_data());
3596 break;
3597
3598 case DW_AT_count:
3599 num_elements = form_value.Unsigned();
3600 break;
3601
3602 case DW_AT_bit_stride:
3603 bit_stride = form_value.Unsigned();
3604 break;
3605
3606 case DW_AT_byte_stride:
3607 byte_stride = form_value.Unsigned();
3608 break;
3609
3610 case DW_AT_byte_size:
3611 byte_size = form_value.Unsigned();
3612 break;
3613
3614 case DW_AT_lower_bound:
3615 lower_bound = form_value.Unsigned();
3616 break;
3617
3618 case DW_AT_upper_bound:
3619 upper_bound = form_value.Unsigned();
3620 break;
3621
3622 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003623 case DW_AT_abstract_origin:
3624 case DW_AT_accessibility:
3625 case DW_AT_allocated:
3626 case DW_AT_associated:
3627 case DW_AT_data_location:
3628 case DW_AT_declaration:
3629 case DW_AT_description:
3630 case DW_AT_sibling:
3631 case DW_AT_threads_scaled:
3632 case DW_AT_type:
3633 case DW_AT_visibility:
3634 break;
3635 }
3636 }
3637 }
3638
3639 if (upper_bound > lower_bound)
3640 num_elements = upper_bound - lower_bound + 1;
3641
3642 if (num_elements > 0)
3643 element_orders.push_back (num_elements);
3644 }
3645 }
3646 break;
3647 }
3648 }
3649}
3650
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003651TypeSP
Greg Clayton96d7d742010-11-10 23:42:09 +00003652SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003653{
3654 TypeSP type_sp;
3655 if (die != NULL)
3656 {
Greg Clayton96d7d742010-11-10 23:42:09 +00003657 assert(curr_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00003658 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003659 if (type_ptr == NULL)
3660 {
Greg Claytonca512b32011-01-14 04:54:56 +00003661 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu);
3662 assert (lldb_cu);
3663 SymbolContext sc(lldb_cu);
Greg Clayton96d7d742010-11-10 23:42:09 +00003664 type_sp = ParseType(sc, curr_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003665 }
3666 else if (type_ptr != DIE_IS_BEING_PARSED)
3667 {
3668 // Grab the existing type from the master types lists
Greg Clayton85ae2e12011-10-18 23:36:41 +00003669 type_sp = type_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003670 }
3671
3672 }
3673 return type_sp;
3674}
3675
3676clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00003677SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003678{
3679 if (die_offset != DW_INVALID_OFFSET)
3680 {
3681 DWARFCompileUnitSP cu_sp;
3682 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00003683 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003684 }
3685 return NULL;
3686}
3687
Sean Callanan72e49402011-08-05 23:43:37 +00003688clang::DeclContext *
3689SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
3690{
3691 if (die_offset != DW_INVALID_OFFSET)
3692 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00003693 DWARFDebugInfo* debug_info = DebugInfo();
3694 if (debug_info)
3695 {
3696 DWARFCompileUnitSP cu_sp;
3697 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
3698 if (die)
3699 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
3700 }
Sean Callanan72e49402011-08-05 23:43:37 +00003701 }
3702 return NULL;
3703}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003704
Greg Clayton96d7d742010-11-10 23:42:09 +00003705clang::NamespaceDecl *
3706SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
3707{
Greg Clayton030a2042011-10-14 21:34:45 +00003708 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00003709 {
Greg Clayton030a2042011-10-14 21:34:45 +00003710 // See if we already parsed this namespace DIE and associated it with a
3711 // uniqued namespace declaration
3712 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
3713 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003714 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00003715 else
3716 {
3717 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00003718 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (curr_cu, die, NULL);
3719 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
3720 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3721 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00003722 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00003723 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00003724 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003725 GetObjectFile()->GetModule()->LogMessage (log.get(),
3726 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
3727 GetClangASTContext().getASTContext(),
3728 MakeUserID(die->GetOffset()),
3729 namespace_name,
3730 namespace_decl,
3731 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00003732 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00003733 else
3734 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003735 GetObjectFile()->GetModule()->LogMessage (log.get(),
3736 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
3737 GetClangASTContext().getASTContext(),
3738 MakeUserID(die->GetOffset()),
3739 namespace_decl,
3740 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00003741 }
Greg Clayton030a2042011-10-14 21:34:45 +00003742 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00003743
3744 if (namespace_decl)
3745 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
3746 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003747 }
3748 }
3749 return NULL;
3750}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003751
3752clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00003753SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00003754{
Greg Clayton5cf58b92011-10-05 22:22:08 +00003755 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3756 if (clang_decl_ctx)
3757 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00003758 // If this DIE has a specification, or an abstract origin, then trace to those.
3759
Greg Claytoncab36a32011-12-08 05:16:30 +00003760 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00003761 if (die_offset != DW_INVALID_OFFSET)
3762 return GetClangDeclContextForDIEOffset (sc, die_offset);
3763
Greg Claytoncab36a32011-12-08 05:16:30 +00003764 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00003765 if (die_offset != DW_INVALID_OFFSET)
3766 return GetClangDeclContextForDIEOffset (sc, die_offset);
3767
Greg Clayton3bffb082011-12-10 02:15:28 +00003768 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3769 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00003770 GetObjectFile()->GetModule()->LogMessage(log.get(), "SymbolFileDWARF::GetClangDeclContextForDIE (die = 0x%8.8x) %s '%s'", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), die->GetName(this, cu));
Sean Callanan72e49402011-08-05 23:43:37 +00003771 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00003772 bool assert_not_being_parsed = true;
3773 ResolveTypeUID (cu, die, assert_not_being_parsed);
3774
Greg Clayton5cf58b92011-10-05 22:22:08 +00003775 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3776
3777 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00003778}
3779
3780clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00003781SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003782{
Greg Claytonca512b32011-01-14 04:54:56 +00003783 if (m_clang_tu_decl == NULL)
3784 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003785
Greg Clayton2bc22f82011-09-30 03:20:47 +00003786 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00003787
3788 if (decl_ctx_die_copy)
3789 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003790
3791 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003792 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00003793
Greg Clayton2bc22f82011-09-30 03:20:47 +00003794 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
3795 if (pos != m_die_to_decl_ctx.end())
3796 return pos->second;
3797
3798 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003799 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003800 case DW_TAG_compile_unit:
3801 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003802
Greg Clayton2bc22f82011-09-30 03:20:47 +00003803 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00003804 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00003805 break;
3806
3807 case DW_TAG_structure_type:
3808 case DW_TAG_union_type:
3809 case DW_TAG_class_type:
3810 {
3811 Type* type = ResolveType (cu, decl_ctx_die);
3812 if (type)
3813 {
3814 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
3815 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00003816 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003817 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
3818 if (decl_ctx)
3819 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00003820 }
3821 }
Greg Claytonca512b32011-01-14 04:54:56 +00003822 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00003823 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003824
Greg Clayton2bc22f82011-09-30 03:20:47 +00003825 default:
3826 break;
Greg Claytonca512b32011-01-14 04:54:56 +00003827 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003828 }
Greg Clayton7a345282010-11-09 23:46:37 +00003829 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003830}
3831
Greg Clayton2bc22f82011-09-30 03:20:47 +00003832
3833const DWARFDebugInfoEntry *
3834SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
3835{
3836 if (cu && die)
3837 {
3838 const DWARFDebugInfoEntry * const decl_die = die;
3839
3840 while (die != NULL)
3841 {
3842 // If this is the original DIE that we are searching for a declaration
3843 // for, then don't look in the cache as we don't want our own decl
3844 // context to be our decl context...
3845 if (decl_die != die)
3846 {
3847 switch (die->Tag())
3848 {
3849 case DW_TAG_compile_unit:
3850 case DW_TAG_namespace:
3851 case DW_TAG_structure_type:
3852 case DW_TAG_union_type:
3853 case DW_TAG_class_type:
3854 return die;
3855
3856 default:
3857 break;
3858 }
3859 }
3860
3861 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
3862 if (die_offset != DW_INVALID_OFFSET)
3863 {
3864 DWARFCompileUnit *spec_cu = cu;
3865 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
3866 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
3867 if (spec_die_decl_ctx_die)
3868 return spec_die_decl_ctx_die;
3869 }
3870
3871 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
3872 if (die_offset != DW_INVALID_OFFSET)
3873 {
3874 DWARFCompileUnit *abs_cu = cu;
3875 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
3876 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
3877 if (abs_die_decl_ctx_die)
3878 return abs_die_decl_ctx_die;
3879 }
3880
3881 die = die->GetParent();
3882 }
3883 }
3884 return NULL;
3885}
3886
3887
Greg Clayton901c5ca2011-12-03 04:40:03 +00003888Symbol *
3889SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
3890{
3891 Symbol *objc_class_symbol = NULL;
3892 if (m_obj_file)
3893 {
3894 Symtab *symtab = m_obj_file->GetSymtab();
3895 if (symtab)
3896 {
3897 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
3898 eSymbolTypeObjCClass,
3899 Symtab::eDebugNo,
3900 Symtab::eVisibilityAny);
3901 }
3902 }
3903 return objc_class_symbol;
3904}
3905
Greg Claytonc7f03b62012-01-12 04:33:28 +00003906// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
3907// then we can end up looking through all class types for a complete type and never find
3908// the full definition. We need to know if this attribute is supported, so we determine
3909// this here and cache th result. We also need to worry about the debug map DWARF file
3910// if we are doing darwin DWARF in .o file debugging.
3911bool
3912SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
3913{
3914 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
3915 {
3916 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
3917 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
3918 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3919 else
3920 {
3921 DWARFDebugInfo* debug_info = DebugInfo();
3922 const uint32_t num_compile_units = GetNumCompileUnits();
3923 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
3924 {
3925 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3926 if (curr_cu != cu && curr_cu->Supports_DW_AT_APPLE_objc_complete_type())
3927 {
3928 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
3929 break;
3930 }
3931 }
3932 }
3933 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && m_debug_map_symfile)
3934 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
3935 }
3936 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
3937}
Greg Clayton901c5ca2011-12-03 04:40:03 +00003938
3939// This function can be used when a DIE is found that is a forward declaration
3940// DIE and we want to try and find a type that has the complete definition.
3941TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00003942SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
3943 const ConstString &type_name,
3944 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00003945{
3946
3947 TypeSP type_sp;
3948
Greg Claytonc7f03b62012-01-12 04:33:28 +00003949 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00003950 return type_sp;
3951
3952 DIEArray die_offsets;
3953
3954 if (m_using_apple_tables)
3955 {
3956 if (m_apple_types_ap.get())
3957 {
3958 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00003959 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003960 }
3961 }
3962 else
3963 {
3964 if (!m_indexed)
3965 Index ();
3966
3967 m_type_index.Find (type_name, die_offsets);
3968 }
3969
Greg Clayton901c5ca2011-12-03 04:40:03 +00003970 const size_t num_matches = die_offsets.size();
3971
Greg Clayton901c5ca2011-12-03 04:40:03 +00003972 DWARFCompileUnit* type_cu = NULL;
3973 const DWARFDebugInfoEntry* type_die = NULL;
3974 if (num_matches)
3975 {
3976 DWARFDebugInfo* debug_info = DebugInfo();
3977 for (size_t i=0; i<num_matches; ++i)
3978 {
3979 const dw_offset_t die_offset = die_offsets[i];
3980 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
3981
3982 if (type_die)
3983 {
3984 bool try_resolving_type = false;
3985
3986 // Don't try and resolve the DIE we are looking for with the DIE itself!
3987 if (type_die != die)
3988 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00003989 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00003990 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00003991 case DW_TAG_class_type:
3992 case DW_TAG_structure_type:
3993 try_resolving_type = true;
3994 break;
3995 default:
3996 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00003997 }
3998 }
3999
4000 if (try_resolving_type)
4001 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004002 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004003 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004004
4005 if (try_resolving_type)
4006 {
4007 Type *resolved_type = ResolveType (type_cu, type_die, false);
4008 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4009 {
4010 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4011 MakeUserID(die->GetOffset()),
4012 MakeUserID(curr_cu->GetOffset()),
4013 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4014 MakeUserID(type_die->GetOffset()),
4015 MakeUserID(type_cu->GetOffset()));
4016
Greg Claytonc7f03b62012-01-12 04:33:28 +00004017 if (die)
4018 m_die_to_type[die] = resolved_type;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004019 type_sp = resolved_type;
4020 break;
4021 }
4022 }
4023 }
4024 }
4025 else
4026 {
4027 if (m_using_apple_tables)
4028 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004029 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4030 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004031 }
4032 }
4033
4034 }
4035 }
4036 return type_sp;
4037}
4038
Greg Clayton220a0072011-12-09 08:48:30 +00004039
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004040// This function can be used when a DIE is found that is a forward declaration
4041// DIE and we want to try and find a type that has the complete definition.
4042TypeSP
Greg Clayton7f995132011-10-04 22:41:51 +00004043SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
4044 const DWARFDebugInfoEntry *die,
4045 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004046{
4047 TypeSP type_sp;
4048
Greg Clayton1a65ae12011-01-25 23:55:37 +00004049 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004050 return type_sp;
4051
Greg Clayton7f995132011-10-04 22:41:51 +00004052 DIEArray die_offsets;
4053
Greg Clayton97fbc342011-10-20 22:30:33 +00004054 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004055 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004056 if (m_apple_types_ap.get())
4057 {
Greg Claytond1767f02011-12-08 02:13:16 +00004058 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4059 {
Greg Claytonae920b62012-01-06 00:17:16 +00004060 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004061 }
4062 else
4063 {
4064 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4065 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004066 }
Greg Clayton7f995132011-10-04 22:41:51 +00004067 }
4068 else
4069 {
4070 if (!m_indexed)
4071 Index ();
4072
4073 m_type_index.Find (type_name, die_offsets);
4074 }
Greg Clayton7f995132011-10-04 22:41:51 +00004075
4076 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004077
Greg Clayton934cb052011-12-03 00:27:05 +00004078 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004079
4080 DWARFCompileUnit* type_cu = NULL;
4081 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004082 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004083 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004084 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004085 for (size_t i=0; i<num_matches; ++i)
4086 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004087 const dw_offset_t die_offset = die_offsets[i];
4088 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004089
Greg Clayton95d87902011-11-11 03:16:25 +00004090 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004091 {
Greg Clayton934cb052011-12-03 00:27:05 +00004092 bool try_resolving_type = false;
4093
4094 // Don't try and resolve the DIE we are looking for with the DIE itself!
4095 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004096 {
Greg Clayton934cb052011-12-03 00:27:05 +00004097 const dw_tag_t type_die_tag = type_die->Tag();
4098 // Make sure the tags match
4099 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004100 {
Greg Clayton934cb052011-12-03 00:27:05 +00004101 // The tags match, lets try resolving this type
4102 try_resolving_type = true;
4103 }
4104 else
4105 {
4106 // The tags don't match, but we need to watch our for a
4107 // forward declaration for a struct and ("struct foo")
4108 // ends up being a class ("class foo { ... };") or
4109 // vice versa.
4110 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004111 {
Greg Clayton934cb052011-12-03 00:27:05 +00004112 case DW_TAG_class_type:
4113 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4114 try_resolving_type = (die_tag == DW_TAG_structure_type);
4115 break;
4116 case DW_TAG_structure_type:
4117 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4118 try_resolving_type = (die_tag == DW_TAG_class_type);
4119 break;
4120 default:
4121 // Tags don't match, don't event try to resolve
4122 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004123 break;
4124 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004125 }
4126 }
Greg Clayton934cb052011-12-03 00:27:05 +00004127
4128 if (try_resolving_type)
4129 {
4130 Type *resolved_type = ResolveType (type_cu, type_die, false);
4131 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4132 {
4133 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4134 MakeUserID(die->GetOffset()),
4135 MakeUserID(curr_cu->GetOffset()),
4136 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4137 MakeUserID(type_die->GetOffset()),
4138 MakeUserID(type_cu->GetOffset()));
4139
4140 m_die_to_type[die] = resolved_type;
4141 type_sp = resolved_type;
4142 break;
4143 }
4144 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004145 }
Greg Clayton95d87902011-11-11 03:16:25 +00004146 else
4147 {
4148 if (m_using_apple_tables)
4149 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004150 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4151 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004152 }
4153 }
4154
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004155 }
4156 }
4157 return type_sp;
4158}
4159
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004160TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00004161SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004162{
4163 TypeSP type_sp;
4164
Greg Clayton1be10fc2010-09-29 01:12:09 +00004165 if (type_is_new_ptr)
4166 *type_is_new_ptr = false;
4167
Sean Callananc7fbf732010-08-06 00:32:49 +00004168 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004169 if (die != NULL)
4170 {
Greg Clayton21f2a492011-10-06 00:09:08 +00004171 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00004172 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004173 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s'",
Greg Clayton3bffb082011-12-10 02:15:28 +00004174 die->GetOffset(),
4175 DW_TAG_value_to_name(die->Tag()),
4176 die->GetName(this, dwarf_cu));
4177//
4178// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4179// if (log && dwarf_cu)
4180// {
4181// StreamString s;
4182// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00004183// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00004184//
4185// }
Jim Ingham16746d12011-08-25 23:21:43 +00004186
Greg Clayton594e5ed2010-09-27 21:07:38 +00004187 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004188 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00004189 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004190 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004191 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00004192 if (type_is_new_ptr)
4193 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004194
Greg Clayton594e5ed2010-09-27 21:07:38 +00004195 const dw_tag_t tag = die->Tag();
4196
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004197 bool is_forward_declaration = false;
4198 DWARFDebugInfoEntry::Attributes attributes;
4199 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00004200 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00004201 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
4202 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00004203 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00004204 Declaration decl;
4205
Greg Clayton4957bf62010-09-30 21:49:03 +00004206 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00004207 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004208
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004209 dw_attr_t attr;
4210
4211 switch (tag)
4212 {
4213 case DW_TAG_base_type:
4214 case DW_TAG_pointer_type:
4215 case DW_TAG_reference_type:
4216 case DW_TAG_typedef:
4217 case DW_TAG_const_type:
4218 case DW_TAG_restrict_type:
4219 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00004220 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004221 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004222 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004223 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004224
Greg Claytond88d7592010-09-15 08:33:30 +00004225 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004226 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004227 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
4228
4229 if (num_attributes > 0)
4230 {
4231 uint32_t i;
4232 for (i=0; i<num_attributes; ++i)
4233 {
4234 attr = attributes.AttributeAtIndex(i);
4235 DWARFFormValue form_value;
4236 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4237 {
4238 switch (attr)
4239 {
4240 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4241 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4242 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4243 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00004244
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004245 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00004246 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
4247 // include the "&"...
4248 if (tag == DW_TAG_reference_type)
4249 {
4250 if (strchr (type_name_cstr, '&') == NULL)
4251 type_name_cstr = NULL;
4252 }
4253 if (type_name_cstr)
4254 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004255 break;
Greg Clayton36909642011-03-15 04:38:20 +00004256 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004257 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
4258 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
4259 default:
4260 case DW_AT_sibling:
4261 break;
4262 }
4263 }
4264 }
4265 }
4266
Greg Clayton81c22f62011-10-19 18:09:39 +00004267 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\") type => 0x%8.8x\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
Greg Claytonc93237c2010-10-01 20:48:32 +00004268
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004269 switch (tag)
4270 {
4271 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00004272 break;
4273
Greg Claytond4436412011-10-28 21:00:00 +00004274 case DW_TAG_unspecified_type:
4275 if (strcmp(type_name_cstr, "nullptr_t") == 0)
4276 {
4277 resolve_state = Type::eResolveStateFull;
4278 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
4279 break;
4280 }
4281 // Fall through to base type below in case we can handle the type there...
4282
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004283 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00004284 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004285 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
4286 encoding,
4287 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004288 break;
4289
Greg Clayton526e5af2010-11-13 03:52:47 +00004290 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
4291 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
4292 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
4293 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
4294 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
4295 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004296 }
4297
Greg Claytonc7f03b62012-01-12 04:33:28 +00004298 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004299 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004300 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
4301 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004302 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004303 static ConstString g_objc_type_name_id("id");
4304 static ConstString g_objc_type_name_Class("Class");
4305 static ConstString g_objc_type_name_selector("SEL");
4306
4307 if (type_name_const_str == g_objc_type_name_id)
4308 {
4309 if (log)
4310 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
4311 die->GetOffset(),
4312 DW_TAG_value_to_name(die->Tag()),
4313 die->GetName(this, dwarf_cu));
4314 clang_type = ast.GetBuiltInType_objc_id();
4315 encoding_data_type = Type::eEncodingIsUID;
4316 encoding_uid = LLDB_INVALID_UID;
4317 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00004318
Greg Claytonc7f03b62012-01-12 04:33:28 +00004319 }
4320 else if (type_name_const_str == g_objc_type_name_Class)
4321 {
4322 if (log)
4323 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
4324 die->GetOffset(),
4325 DW_TAG_value_to_name(die->Tag()),
4326 die->GetName(this, dwarf_cu));
4327 clang_type = ast.GetBuiltInType_objc_Class();
4328 encoding_data_type = Type::eEncodingIsUID;
4329 encoding_uid = LLDB_INVALID_UID;
4330 resolve_state = Type::eResolveStateFull;
4331 }
4332 else if (type_name_const_str == g_objc_type_name_selector)
4333 {
4334 if (log)
4335 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
4336 die->GetOffset(),
4337 DW_TAG_value_to_name(die->Tag()),
4338 die->GetName(this, dwarf_cu));
4339 clang_type = ast.GetBuiltInType_objc_selector();
4340 encoding_data_type = Type::eEncodingIsUID;
4341 encoding_uid = LLDB_INVALID_UID;
4342 resolve_state = Type::eResolveStateFull;
4343 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004344 }
4345 }
4346
Greg Clayton81c22f62011-10-19 18:09:39 +00004347 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004348 this,
4349 type_name_const_str,
4350 byte_size,
4351 NULL,
4352 encoding_uid,
4353 encoding_data_type,
4354 &decl,
4355 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004356 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004357
Greg Clayton594e5ed2010-09-27 21:07:38 +00004358 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004359
4360// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
4361// if (encoding_type != NULL)
4362// {
4363// if (encoding_type != DIE_IS_BEING_PARSED)
4364// type_sp->SetEncodingType(encoding_type);
4365// else
4366// m_indirect_fixups.push_back(type_sp.get());
4367// }
4368 }
4369 break;
4370
4371 case DW_TAG_structure_type:
4372 case DW_TAG_union_type:
4373 case DW_TAG_class_type:
4374 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004375 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004376 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004377
Greg Clayton9e409562010-07-28 02:04:09 +00004378 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00004379 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004380 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00004381 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004382 if (num_attributes > 0)
4383 {
4384 uint32_t i;
4385 for (i=0; i<num_attributes; ++i)
4386 {
4387 attr = attributes.AttributeAtIndex(i);
4388 DWARFFormValue form_value;
4389 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4390 {
4391 switch (attr)
4392 {
Greg Clayton9e409562010-07-28 02:04:09 +00004393 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00004394 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
4395 {
4396 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
4397 // point to the compile unit file, so we clear this invalid value
4398 // so that we can still unique types efficiently.
4399 decl.SetFile(FileSpec ("<invalid>", false));
4400 }
4401 else
4402 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00004403 break;
4404
4405 case DW_AT_decl_line:
4406 decl.SetLine(form_value.Unsigned());
4407 break;
4408
4409 case DW_AT_decl_column:
4410 decl.SetColumn(form_value.Unsigned());
4411 break;
4412
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004413 case DW_AT_name:
4414 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004415 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004416 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004417
4418 case DW_AT_byte_size:
4419 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00004420 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004421 break;
4422
4423 case DW_AT_accessibility:
4424 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
4425 break;
4426
4427 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00004428 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00004429 break;
4430
4431 case DW_AT_APPLE_runtime_class:
4432 class_language = (LanguageType)form_value.Signed();
4433 break;
4434
Greg Clayton18774842011-11-29 23:40:34 +00004435 case DW_AT_APPLE_objc_complete_type:
4436 is_complete_objc_class = form_value.Signed();
4437 break;
4438
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004439 case DW_AT_allocated:
4440 case DW_AT_associated:
4441 case DW_AT_data_location:
4442 case DW_AT_description:
4443 case DW_AT_start_scope:
4444 case DW_AT_visibility:
4445 default:
4446 case DW_AT_sibling:
4447 break;
4448 }
4449 }
4450 }
4451 }
4452
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004453 UniqueDWARFASTType unique_ast_entry;
4454 if (decl.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004455 {
Greg Claytone576ab22011-02-15 00:19:15 +00004456 if (GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Greg Clayton36909642011-03-15 04:38:20 +00004457 this,
4458 dwarf_cu,
Greg Claytone576ab22011-02-15 00:19:15 +00004459 die,
4460 decl,
Greg Clayton36909642011-03-15 04:38:20 +00004461 byte_size_valid ? byte_size : -1,
Greg Claytone576ab22011-02-15 00:19:15 +00004462 unique_ast_entry))
Greg Claytonc615ce42010-11-09 04:42:43 +00004463 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004464 // We have already parsed this type or from another
4465 // compile unit. GCC loves to use the "one definition
4466 // rule" which can result in multiple definitions
4467 // of the same class over and over in each compile
4468 // unit.
4469 type_sp = unique_ast_entry.m_type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004470 if (type_sp)
4471 {
Greg Clayton4272cc72011-02-02 02:24:04 +00004472 m_die_to_type[die] = type_sp.get();
4473 return type_sp;
4474 }
4475 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004476 }
4477
Greg Clayton81c22f62011-10-19 18:09:39 +00004478 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004479
4480 int tag_decl_kind = -1;
4481 AccessType default_accessibility = eAccessNone;
4482 if (tag == DW_TAG_structure_type)
4483 {
4484 tag_decl_kind = clang::TTK_Struct;
4485 default_accessibility = eAccessPublic;
4486 }
4487 else if (tag == DW_TAG_union_type)
4488 {
4489 tag_decl_kind = clang::TTK_Union;
4490 default_accessibility = eAccessPublic;
4491 }
4492 else if (tag == DW_TAG_class_type)
4493 {
4494 tag_decl_kind = clang::TTK_Class;
4495 default_accessibility = eAccessPrivate;
4496 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00004497
4498 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
4499 die->HasChildren() == false &&
4500 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
4501 {
4502 // Work around an issue with clang at the moment where
4503 // forward declarations for objective C classes are emitted
4504 // as:
4505 // DW_TAG_structure_type [2]
4506 // DW_AT_name( "ForwardObjcClass" )
4507 // DW_AT_byte_size( 0x00 )
4508 // DW_AT_decl_file( "..." )
4509 // DW_AT_decl_line( 1 )
4510 //
4511 // Note that there is no DW_AT_declaration and there are
4512 // no children, and the byte size is zero.
4513 is_forward_declaration = true;
4514 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004515
Greg Clayton18774842011-11-29 23:40:34 +00004516 if (class_language == eLanguageTypeObjC)
4517 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004518 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004519 {
4520 // We have a valid eSymbolTypeObjCClass class symbol whose
4521 // name matches the current objective C class that we
4522 // are trying to find and this DIE isn't the complete
4523 // definition (we checked is_complete_objc_class above and
4524 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00004525 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004526
Greg Clayton901c5ca2011-12-03 04:40:03 +00004527 if (!type_sp && m_debug_map_symfile)
4528 {
4529 // We weren't able to find a full declaration in
4530 // this DWARF, see if we have a declaration anywhere
4531 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00004532 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004533 }
4534
4535 if (type_sp)
4536 {
4537 if (log)
4538 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004539 GetObjectFile()->GetModule()->LogMessage (log.get(),
4540 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
4541 this,
4542 die->GetOffset(),
4543 DW_TAG_value_to_name(tag),
4544 type_name_cstr,
4545 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004546 }
4547
4548 // We found a real definition for this type elsewhere
4549 // so lets use it and cache the fact that we found
4550 // a complete type for this die
4551 m_die_to_type[die] = type_sp.get();
4552 return type_sp;
4553 }
4554 }
4555 }
4556
4557
4558 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004559 {
4560 // We have a forward declaration to a type and we need
4561 // to try and find a full declaration. We look in the
4562 // current type index just in case we have a forward
4563 // declaration followed by an actual declarations in the
4564 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00004565 if (log)
4566 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004567 GetObjectFile()->GetModule()->LogMessage (log.get(),
4568 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
4569 this,
4570 die->GetOffset(),
4571 DW_TAG_value_to_name(tag),
4572 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00004573 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004574
4575 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
4576
4577 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00004578 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004579 // We weren't able to find a full declaration in
4580 // this DWARF, see if we have a declaration anywhere
4581 // else...
4582 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
Greg Clayton4272cc72011-02-02 02:24:04 +00004583 }
4584
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004585 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00004586 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00004587 if (log)
4588 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004589 GetObjectFile()->GetModule()->LogMessage (log.get(),
4590 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
4591 this,
4592 die->GetOffset(),
4593 DW_TAG_value_to_name(tag),
4594 type_name_cstr,
4595 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00004596 }
4597
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004598 // We found a real definition for this type elsewhere
4599 // so lets use it and cache the fact that we found
4600 // a complete type for this die
4601 m_die_to_type[die] = type_sp.get();
4602 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004603 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004604 }
4605 assert (tag_decl_kind != -1);
4606 bool clang_type_was_created = false;
4607 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
4608 if (clang_type == NULL)
4609 {
Greg Claytonf0705c82011-10-22 03:33:13 +00004610 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton55561e92011-10-26 03:31:36 +00004611 if (accessibility == eAccessNone && decl_ctx)
4612 {
4613 // Check the decl context that contains this class/struct/union.
4614 // If it is a class we must give it an accessability.
4615 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
4616 if (DeclKindIsCXXClass (containing_decl_kind))
4617 accessibility = default_accessibility;
4618 }
4619
Greg Claytonf0705c82011-10-22 03:33:13 +00004620 if (type_name_cstr && strchr (type_name_cstr, '<'))
4621 {
4622 ClangASTContext::TemplateParameterInfos template_param_infos;
4623 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
4624 {
4625 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00004626 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00004627 type_name_cstr,
4628 tag_decl_kind,
4629 template_param_infos);
4630
4631 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
4632 class_template_decl,
4633 tag_decl_kind,
4634 template_param_infos);
4635 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
4636 clang_type_was_created = true;
4637 }
4638 }
4639
4640 if (!clang_type_was_created)
4641 {
4642 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00004643 clang_type = ast.CreateRecordType (decl_ctx,
4644 accessibility,
4645 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00004646 tag_decl_kind,
Greg Claytonf0705c82011-10-22 03:33:13 +00004647 class_language);
4648 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004649 }
4650
4651 // Store a forward declaration to this class type in case any
4652 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00004653 // types for function prototypes.
4654 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00004655 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004656 this,
4657 type_name_const_str,
4658 byte_size,
4659 NULL,
4660 LLDB_INVALID_UID,
4661 Type::eEncodingIsUID,
4662 &decl,
4663 clang_type,
4664 Type::eResolveStateForward));
4665
4666
4667 // Add our type to the unique type map so we don't
4668 // end up creating many copies of the same type over
4669 // and over in the ASTContext for our module
4670 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00004671 unique_ast_entry.m_symfile = this;
4672 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004673 unique_ast_entry.m_die = die;
4674 unique_ast_entry.m_declaration = decl;
Greg Claytone576ab22011-02-15 00:19:15 +00004675 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
4676 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004677
Sean Callanan12014a02011-12-08 23:45:45 +00004678 if (!is_forward_declaration)
4679 {
4680 if (die->HasChildren() == false)
4681 {
4682 // No children for this struct/union/class, lets finish it
4683 ast.StartTagDeclarationDefinition (clang_type);
4684 ast.CompleteTagDeclarationDefinition (clang_type);
4685 }
4686 else if (clang_type_was_created)
4687 {
4688 // Leave this as a forward declaration until we need
4689 // to know the details of the type. lldb_private::Type
4690 // will automatically call the SymbolFile virtual function
4691 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
4692 // When the definition needs to be defined.
4693 m_forward_decl_die_to_clang_type[die] = clang_type;
4694 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
4695 ClangASTContext::SetHasExternalStorage (clang_type, true);
4696 }
Greg Claytonc615ce42010-11-09 04:42:43 +00004697 }
Greg Claytoncab36a32011-12-08 05:16:30 +00004698
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004699 }
4700 break;
4701
4702 case DW_TAG_enumeration_type:
4703 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004704 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004705 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004706
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004707 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004708
Greg Claytond88d7592010-09-15 08:33:30 +00004709 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004710 if (num_attributes > 0)
4711 {
4712 uint32_t i;
4713
4714 for (i=0; i<num_attributes; ++i)
4715 {
4716 attr = attributes.AttributeAtIndex(i);
4717 DWARFFormValue form_value;
4718 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4719 {
4720 switch (attr)
4721 {
Greg Clayton7a345282010-11-09 23:46:37 +00004722 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4723 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4724 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004725 case DW_AT_name:
4726 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004727 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004728 break;
Greg Clayton7a345282010-11-09 23:46:37 +00004729 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00004730 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00004731 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
4732 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004733 case DW_AT_allocated:
4734 case DW_AT_associated:
4735 case DW_AT_bit_stride:
4736 case DW_AT_byte_stride:
4737 case DW_AT_data_location:
4738 case DW_AT_description:
4739 case DW_AT_start_scope:
4740 case DW_AT_visibility:
4741 case DW_AT_specification:
4742 case DW_AT_abstract_origin:
4743 case DW_AT_sibling:
4744 break;
4745 }
4746 }
4747 }
4748
Greg Clayton81c22f62011-10-19 18:09:39 +00004749 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00004750
Greg Clayton1be10fc2010-09-29 01:12:09 +00004751 clang_type_t enumerator_clang_type = NULL;
4752 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
4753 if (clang_type == NULL)
4754 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004755 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
4756 DW_ATE_signed,
4757 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00004758 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00004759 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00004760 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004761 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00004762 }
4763 else
4764 {
4765 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
4766 assert (enumerator_clang_type != NULL);
4767 }
4768
Greg Claytona2721472011-06-25 00:44:06 +00004769 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
4770
Greg Clayton81c22f62011-10-19 18:09:39 +00004771 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004772 this,
4773 type_name_const_str,
4774 byte_size,
4775 NULL,
4776 encoding_uid,
4777 Type::eEncodingIsUID,
4778 &decl,
4779 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004780 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004781
Greg Clayton6beaaa62011-01-17 03:46:26 +00004782 ast.StartTagDeclarationDefinition (clang_type);
4783 if (die->HasChildren())
4784 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00004785 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
4786 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00004787 }
4788 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004789 }
4790 }
4791 break;
4792
Jim Inghamb0be4422010-08-12 01:20:14 +00004793 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004794 case DW_TAG_subprogram:
4795 case DW_TAG_subroutine_type:
4796 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004797 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004798 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004799
4800 const char *mangled = NULL;
4801 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00004802 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004803 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00004804 bool is_static = false;
4805 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00004806 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00004807 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00004808 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
4809 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00004810
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004811 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00004812 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004813
4814
Greg Claytond88d7592010-09-15 08:33:30 +00004815 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004816 if (num_attributes > 0)
4817 {
4818 uint32_t i;
4819 for (i=0; i<num_attributes; ++i)
4820 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00004821 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004822 DWARFFormValue form_value;
4823 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4824 {
4825 switch (attr)
4826 {
4827 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4828 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4829 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4830 case DW_AT_name:
4831 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004832 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004833 break;
4834
4835 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
4836 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00004837 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00004838 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00004839 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
4840 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00004841 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00004842 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
4843
Greg Claytonf51de672010-10-01 02:31:07 +00004844
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004845 case DW_AT_external:
4846 if (form_value.Unsigned())
4847 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00004848 if (storage == clang::SC_None)
4849 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004850 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00004851 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004852 }
4853 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004854
Greg Clayton72da3972011-08-16 18:40:23 +00004855 case DW_AT_specification:
4856 specification_die_offset = form_value.Reference(dwarf_cu);
4857 break;
4858
4859 case DW_AT_abstract_origin:
4860 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
4861 break;
4862
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004863 case DW_AT_allocated:
4864 case DW_AT_associated:
4865 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004866 case DW_AT_calling_convention:
4867 case DW_AT_data_location:
4868 case DW_AT_elemental:
4869 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004870 case DW_AT_frame_base:
4871 case DW_AT_high_pc:
4872 case DW_AT_low_pc:
4873 case DW_AT_object_pointer:
4874 case DW_AT_prototyped:
4875 case DW_AT_pure:
4876 case DW_AT_ranges:
4877 case DW_AT_recursive:
4878 case DW_AT_return_addr:
4879 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004880 case DW_AT_start_scope:
4881 case DW_AT_static_link:
4882 case DW_AT_trampoline:
4883 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004884 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004885 case DW_AT_description:
4886 case DW_AT_sibling:
4887 break;
4888 }
4889 }
4890 }
Greg Clayton24739922010-10-13 03:15:28 +00004891 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004892
Greg Clayton81c22f62011-10-19 18:09:39 +00004893 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00004894
Greg Clayton24739922010-10-13 03:15:28 +00004895 clang_type_t return_clang_type = NULL;
4896 Type *func_type = NULL;
4897
4898 if (type_die_offset != DW_INVALID_OFFSET)
4899 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00004900
Greg Clayton24739922010-10-13 03:15:28 +00004901 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00004902 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00004903 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004904 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004905
Greg Claytonf51de672010-10-01 02:31:07 +00004906
Greg Clayton24739922010-10-13 03:15:28 +00004907 std::vector<clang_type_t> function_param_types;
4908 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004909
Greg Clayton24739922010-10-13 03:15:28 +00004910 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00004911
Greg Claytoncb5860a2011-10-13 23:49:28 +00004912 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
4913 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00004914 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
4915
Greg Claytonf0705c82011-10-22 03:33:13 +00004916 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00004917 // Start off static. This will be set to false in ParseChildParameters(...)
4918 // if we find a "this" paramters as the first parameter
4919 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00004920 is_static = true;
4921
Greg Clayton24739922010-10-13 03:15:28 +00004922 if (die->HasChildren())
4923 {
Greg Clayton0fffff52010-09-24 05:15:53 +00004924 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004925 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00004926 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004927 type_sp,
4928 dwarf_cu,
4929 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00004930 skip_artificial,
4931 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004932 type_list,
4933 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00004934 function_param_decls,
4935 type_quals);
Greg Clayton24739922010-10-13 03:15:28 +00004936 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004937
Greg Clayton24739922010-10-13 03:15:28 +00004938 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004939 clang_type = ast.CreateFunctionType (return_clang_type,
4940 &function_param_types[0],
4941 function_param_types.size(),
4942 is_variadic,
4943 type_quals);
4944
Greg Clayton24739922010-10-13 03:15:28 +00004945 if (type_name_cstr)
4946 {
4947 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00004948 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004949 {
Greg Clayton7c810422012-01-18 23:40:49 +00004950 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00004951 ConstString class_name_no_category;
4952 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00004953 {
Greg Claytone42ae842012-01-19 03:24:53 +00004954 // Use the class name with no category if there is one
4955 if (class_name_no_category)
4956 class_name = class_name_no_category;
4957
Greg Clayton24739922010-10-13 03:15:28 +00004958 SymbolContext empty_sc;
4959 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00004960 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00004961 {
Greg Clayton24739922010-10-13 03:15:28 +00004962 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00004963 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00004964
4965 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00004966 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004967 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
4968 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
4969 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00004970 }
Greg Clayton24739922010-10-13 03:15:28 +00004971 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004972
Greg Clayton24739922010-10-13 03:15:28 +00004973 if (class_opaque_type)
4974 {
4975 // If accessibility isn't set to anything valid, assume public for
4976 // now...
4977 if (accessibility == eAccessNone)
4978 accessibility = eAccessPublic;
4979
4980 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004981 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
4982 type_name_cstr,
4983 clang_type,
4984 accessibility);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00004985 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton24739922010-10-13 03:15:28 +00004986 type_handled = objc_method_decl != NULL;
4987 }
4988 }
Greg Clayton5113dc82011-08-12 06:47:54 +00004989 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00004990 {
4991 // Look at the parent of this DIE and see if is is
4992 // a class or struct and see if this is actually a
4993 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00004994 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00004995 if (class_type)
4996 {
Greg Clayton72da3972011-08-16 18:40:23 +00004997 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00004998 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004999 // We have a specification which we are going to base our function
5000 // prototype off of, so we need this type to be completed so that the
5001 // m_die_to_decl_ctx for the method in the specification has a valid
5002 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005003 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00005004 // If we have a specification, then the function type should have been
5005 // made with the specification and not with this die.
5006 DWARFCompileUnitSP spec_cu_sp;
5007 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005008 clang::DeclContext *spec_clang_decl_ctx = GetCachedClangDeclContextForDIE (spec_die);
5009 if (spec_clang_decl_ctx)
5010 {
5011 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
5012 }
5013 else
Jim Inghamc1663042011-09-29 22:12:35 +00005014 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005015 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
5016 MakeUserID(die->GetOffset()),
5017 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005018 }
Greg Clayton72da3972011-08-16 18:40:23 +00005019 type_handled = true;
5020 }
5021 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
5022 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005023 // We have a specification which we are going to base our function
5024 // prototype off of, so we need this type to be completed so that the
5025 // m_die_to_decl_ctx for the method in the abstract origin has a valid
5026 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005027 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00005028
Greg Clayton72da3972011-08-16 18:40:23 +00005029 DWARFCompileUnitSP abs_cu_sp;
5030 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005031 clang::DeclContext *abs_clang_decl_ctx = GetCachedClangDeclContextForDIE (abs_die);
5032 if (abs_clang_decl_ctx)
5033 {
5034 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
5035 }
5036 else
Jim Inghamc1663042011-09-29 22:12:35 +00005037 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005038 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
5039 MakeUserID(die->GetOffset()),
5040 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005041 }
Greg Clayton72da3972011-08-16 18:40:23 +00005042 type_handled = true;
5043 }
5044 else
5045 {
5046 clang_type_t class_opaque_type = class_type->GetClangForwardType();
5047 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00005048 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005049 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00005050 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005051 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
5052 // in the DWARF for C++ methods... Default to public for now...
5053 if (accessibility == eAccessNone)
5054 accessibility = eAccessPublic;
5055
5056 if (!is_static && !die->HasChildren())
5057 {
5058 // We have a C++ member function with no children (this pointer!)
5059 // and clang will get mad if we try and make a function that isn't
5060 // well formed in the DWARF, so we will just skip it...
5061 type_handled = true;
5062 }
5063 else
5064 {
5065 clang::CXXMethodDecl *cxx_method_decl;
5066 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00005067 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8llx from %s/%s",
Greg Clayton20568dd2011-10-13 23:13:20 +00005068 type_name_cstr,
5069 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00005070 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00005071 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
5072 m_obj_file->GetFileSpec().GetFilename().GetCString());
5073
Sean Callananc1b732d2011-11-01 18:07:13 +00005074 const bool is_attr_used = false;
5075
Greg Clayton20568dd2011-10-13 23:13:20 +00005076 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
5077 type_name_cstr,
5078 clang_type,
5079 accessibility,
5080 is_virtual,
5081 is_static,
5082 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00005083 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00005084 is_attr_used,
5085 is_artificial);
Greg Clayton20568dd2011-10-13 23:13:20 +00005086 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
5087
Greg Claytonf49e65a2011-11-10 18:31:53 +00005088 Host::SetCrashDescription (NULL);
5089
Greg Clayton20568dd2011-10-13 23:13:20 +00005090 type_handled = cxx_method_decl != NULL;
5091 }
Greg Clayton72da3972011-08-16 18:40:23 +00005092 }
5093 else
5094 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005095 // We were asked to parse the type for a method in a class, yet the
5096 // class hasn't been asked to complete itself through the
5097 // clang::ExternalASTSource protocol, so we need to just have the
5098 // class complete itself and do things the right way, then our
5099 // DIE should then have an entry in the m_die_to_type map. First
5100 // we need to modify the m_die_to_type so it doesn't think we are
5101 // trying to parse this DIE anymore...
5102 m_die_to_type[die] = NULL;
5103
5104 // Now we get the full type to force our class type to complete itself
5105 // using the clang::ExternalASTSource protocol which will parse all
5106 // base classes and all methods (including the method for this DIE).
5107 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005108
Greg Clayton20568dd2011-10-13 23:13:20 +00005109 // The type for this DIE should have been filled in the function call above
5110 type_ptr = m_die_to_type[die];
5111 if (type_ptr)
5112 {
Greg Clayton85ae2e12011-10-18 23:36:41 +00005113 type_sp = type_ptr;
Greg Clayton20568dd2011-10-13 23:13:20 +00005114 break;
5115 }
Greg Clayton72da3972011-08-16 18:40:23 +00005116 }
Greg Clayton931180e2011-01-27 06:44:37 +00005117 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005118 }
5119 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005120 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005121 }
Greg Clayton24739922010-10-13 03:15:28 +00005122
5123 if (!type_handled)
5124 {
5125 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00005126 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
5127 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005128 clang_type,
5129 storage,
5130 is_inline);
Greg Clayton24739922010-10-13 03:15:28 +00005131
5132 // Add the decl to our DIE to decl context map
5133 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00005134 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00005135 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005136 ast.SetFunctionParameters (function_decl,
5137 &function_param_decls.front(),
5138 function_param_decls.size());
Greg Clayton24739922010-10-13 03:15:28 +00005139 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005140 }
Greg Clayton81c22f62011-10-19 18:09:39 +00005141 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005142 this,
5143 type_name_const_str,
5144 0,
5145 NULL,
5146 LLDB_INVALID_UID,
5147 Type::eEncodingIsUID,
5148 &decl,
5149 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005150 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00005151 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005152 }
5153 break;
5154
5155 case DW_TAG_array_type:
5156 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005157 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005158 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005159
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005160 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005161 int64_t first_index = 0;
5162 uint32_t byte_stride = 0;
5163 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00005164 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005165
5166 if (num_attributes > 0)
5167 {
5168 uint32_t i;
5169 for (i=0; i<num_attributes; ++i)
5170 {
5171 attr = attributes.AttributeAtIndex(i);
5172 DWARFFormValue form_value;
5173 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5174 {
5175 switch (attr)
5176 {
5177 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5178 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5179 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5180 case DW_AT_name:
5181 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005182 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005183 break;
5184
5185 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005186 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005187 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
5188 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005189 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005190 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005191 case DW_AT_allocated:
5192 case DW_AT_associated:
5193 case DW_AT_data_location:
5194 case DW_AT_description:
5195 case DW_AT_ordering:
5196 case DW_AT_start_scope:
5197 case DW_AT_visibility:
5198 case DW_AT_specification:
5199 case DW_AT_abstract_origin:
5200 case DW_AT_sibling:
5201 break;
5202 }
5203 }
5204 }
5205
Greg Clayton81c22f62011-10-19 18:09:39 +00005206 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00005207
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005208 Type *element_type = ResolveTypeUID(type_die_offset);
5209
5210 if (element_type)
5211 {
5212 std::vector<uint64_t> element_orders;
5213 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00005214 // We have an array that claims to have no members, lets give it at least one member...
5215 if (element_orders.empty())
5216 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005217 if (byte_stride == 0 && bit_stride == 0)
5218 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00005219 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005220 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
5221 uint64_t num_elements = 0;
5222 std::vector<uint64_t>::const_reverse_iterator pos;
5223 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
5224 for (pos = element_orders.rbegin(); pos != end; ++pos)
5225 {
5226 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005227 clang_type = ast.CreateArrayType (array_element_type,
5228 num_elements,
5229 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005230 array_element_type = clang_type;
5231 array_element_bit_stride = array_element_bit_stride * num_elements;
5232 }
5233 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00005234 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005235 this,
5236 empty_name,
5237 array_element_bit_stride / 8,
5238 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00005239 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005240 Type::eEncodingIsUID,
5241 &decl,
5242 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005243 Type::eResolveStateFull));
5244 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005245 }
5246 }
5247 }
5248 break;
5249
Greg Clayton9b81a312010-06-12 01:20:30 +00005250 case DW_TAG_ptr_to_member_type:
5251 {
5252 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
5253 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
5254
Greg Claytond88d7592010-09-15 08:33:30 +00005255 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00005256
5257 if (num_attributes > 0) {
5258 uint32_t i;
5259 for (i=0; i<num_attributes; ++i)
5260 {
5261 attr = attributes.AttributeAtIndex(i);
5262 DWARFFormValue form_value;
5263 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5264 {
5265 switch (attr)
5266 {
5267 case DW_AT_type:
5268 type_die_offset = form_value.Reference(dwarf_cu); break;
5269 case DW_AT_containing_type:
5270 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
5271 }
5272 }
5273 }
5274
5275 Type *pointee_type = ResolveTypeUID(type_die_offset);
5276 Type *class_type = ResolveTypeUID(containing_type_die_offset);
5277
Greg Clayton526e5af2010-11-13 03:52:47 +00005278 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
5279 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00005280
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005281 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
5282 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00005283
Greg Clayton526e5af2010-11-13 03:52:47 +00005284 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
5285 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00005286
Greg Clayton81c22f62011-10-19 18:09:39 +00005287 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005288 this,
5289 type_name_const_str,
5290 byte_size,
5291 NULL,
5292 LLDB_INVALID_UID,
5293 Type::eEncodingIsUID,
5294 NULL,
5295 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005296 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00005297 }
5298
5299 break;
5300 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005301 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00005302 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005303 break;
5304 }
5305
5306 if (type_sp.get())
5307 {
5308 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5309 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
5310
5311 SymbolContextScope * symbol_context_scope = NULL;
5312 if (sc_parent_tag == DW_TAG_compile_unit)
5313 {
5314 symbol_context_scope = sc.comp_unit;
5315 }
5316 else if (sc.function != NULL)
5317 {
Greg Clayton81c22f62011-10-19 18:09:39 +00005318 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005319 if (symbol_context_scope == NULL)
5320 symbol_context_scope = sc.function;
5321 }
5322
5323 if (symbol_context_scope != NULL)
5324 {
5325 type_sp->SetSymbolContextScope(symbol_context_scope);
5326 }
5327
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005328 // We are ready to put this type into the uniqued list up at the module level
5329 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00005330
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005331 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005332 }
5333 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00005334 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005335 {
Greg Clayton85ae2e12011-10-18 23:36:41 +00005336 type_sp = type_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005337 }
5338 }
5339 return type_sp;
5340}
5341
5342size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00005343SymbolFileDWARF::ParseTypes
5344(
5345 const SymbolContext& sc,
5346 DWARFCompileUnit* dwarf_cu,
5347 const DWARFDebugInfoEntry *die,
5348 bool parse_siblings,
5349 bool parse_children
5350)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005351{
5352 size_t types_added = 0;
5353 while (die != NULL)
5354 {
5355 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005356 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005357 {
5358 if (type_is_new)
5359 ++types_added;
5360 }
5361
5362 if (parse_children && die->HasChildren())
5363 {
5364 if (die->Tag() == DW_TAG_subprogram)
5365 {
5366 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00005367 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005368 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
5369 }
5370 else
5371 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
5372 }
5373
5374 if (parse_siblings)
5375 die = die->GetSibling();
5376 else
5377 die = NULL;
5378 }
5379 return types_added;
5380}
5381
5382
5383size_t
5384SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
5385{
5386 assert(sc.comp_unit && sc.function);
5387 size_t functions_added = 0;
5388 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5389 if (dwarf_cu)
5390 {
5391 dw_offset_t function_die_offset = sc.function->GetID();
5392 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
5393 if (function_die)
5394 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00005395 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005396 }
5397 }
5398
5399 return functions_added;
5400}
5401
5402
5403size_t
5404SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
5405{
5406 // At least a compile unit must be valid
5407 assert(sc.comp_unit);
5408 size_t types_added = 0;
5409 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5410 if (dwarf_cu)
5411 {
5412 if (sc.function)
5413 {
5414 dw_offset_t function_die_offset = sc.function->GetID();
5415 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
5416 if (func_die && func_die->HasChildren())
5417 {
5418 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
5419 }
5420 }
5421 else
5422 {
5423 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
5424 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
5425 {
5426 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
5427 }
5428 }
5429 }
5430
5431 return types_added;
5432}
5433
5434size_t
5435SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
5436{
5437 if (sc.comp_unit != NULL)
5438 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00005439 DWARFDebugInfo* info = DebugInfo();
5440 if (info == NULL)
5441 return 0;
5442
5443 uint32_t cu_idx = UINT32_MAX;
5444 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005445
5446 if (dwarf_cu == NULL)
5447 return 0;
5448
5449 if (sc.function)
5450 {
5451 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00005452
5453 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005454 if (func_lo_pc != DW_INVALID_ADDRESS)
5455 {
5456 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00005457
Greg Claytone38a5ed2012-01-05 03:57:59 +00005458 // Let all blocks know they have parse all their variables
5459 sc.function->GetBlock (false).SetDidParseVariables (true, true);
5460 return num_variables;
5461 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005462 }
5463 else if (sc.comp_unit)
5464 {
5465 uint32_t vars_added = 0;
5466 VariableListSP variables (sc.comp_unit->GetVariableList(false));
5467
5468 if (variables.get() == NULL)
5469 {
5470 variables.reset(new VariableList());
5471 sc.comp_unit->SetVariableList(variables);
5472
Greg Claytond4a2b372011-09-12 23:21:58 +00005473 DWARFCompileUnit* match_dwarf_cu = NULL;
5474 const DWARFDebugInfoEntry* die = NULL;
5475 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00005476 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00005477 {
Greg Clayton97fbc342011-10-20 22:30:33 +00005478 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00005479 {
5480 DWARFMappedHash::DIEInfoArray hash_data_array;
5481 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
5482 dwarf_cu->GetNextCompileUnitOffset(),
5483 hash_data_array))
5484 {
5485 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
5486 }
5487 }
Greg Clayton7f995132011-10-04 22:41:51 +00005488 }
5489 else
5490 {
5491 // Index if we already haven't to make sure the compile units
5492 // get indexed and make their global DIE index list
5493 if (!m_indexed)
5494 Index ();
5495
5496 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
5497 dwarf_cu->GetNextCompileUnitOffset(),
5498 die_offsets);
5499 }
5500
5501 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00005502 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005503 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005504 DWARFDebugInfo* debug_info = DebugInfo();
5505 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005506 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005507 const dw_offset_t die_offset = die_offsets[i];
5508 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00005509 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00005510 {
Greg Clayton95d87902011-11-11 03:16:25 +00005511 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
5512 if (var_sp)
5513 {
5514 variables->AddVariableIfUnique (var_sp);
5515 ++vars_added;
5516 }
Greg Claytond4a2b372011-09-12 23:21:58 +00005517 }
Greg Clayton95d87902011-11-11 03:16:25 +00005518 else
5519 {
5520 if (m_using_apple_tables)
5521 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005522 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x)\n", die_offset);
Greg Clayton95d87902011-11-11 03:16:25 +00005523 }
5524 }
5525
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005526 }
5527 }
5528 }
5529 return vars_added;
5530 }
5531 }
5532 return 0;
5533}
5534
5535
5536VariableSP
5537SymbolFileDWARF::ParseVariableDIE
5538(
5539 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00005540 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00005541 const DWARFDebugInfoEntry *die,
5542 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005543)
5544{
5545
Greg Clayton83c5cd92010-11-14 22:13:40 +00005546 VariableSP var_sp (m_die_to_variable_sp[die]);
5547 if (var_sp)
5548 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005549
5550 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00005551
5552 if ((tag == DW_TAG_variable) ||
5553 (tag == DW_TAG_constant) ||
5554 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005555 {
Greg Clayton7f995132011-10-04 22:41:51 +00005556 DWARFDebugInfoEntry::Attributes attributes;
5557 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
5558 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005559 {
Greg Clayton7f995132011-10-04 22:41:51 +00005560 const char *name = NULL;
5561 const char *mangled = NULL;
5562 Declaration decl;
5563 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00005564 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00005565 DWARFExpression location;
5566 bool is_external = false;
5567 bool is_artificial = false;
5568 bool location_is_const_value_data = false;
5569 AccessType accessibility = eAccessNone;
5570
5571 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005572 {
Greg Clayton7f995132011-10-04 22:41:51 +00005573 dw_attr_t attr = attributes.AttributeAtIndex(i);
5574 DWARFFormValue form_value;
5575 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005576 {
Greg Clayton7f995132011-10-04 22:41:51 +00005577 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005578 {
Greg Clayton7f995132011-10-04 22:41:51 +00005579 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5580 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5581 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5582 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
5583 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00005584 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00005585 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
5586 case DW_AT_const_value:
5587 location_is_const_value_data = true;
5588 // Fall through...
5589 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005590 {
Greg Clayton7f995132011-10-04 22:41:51 +00005591 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005592 {
Greg Clayton7f995132011-10-04 22:41:51 +00005593 const DataExtractor& debug_info_data = get_debug_info_data();
5594
5595 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
5596 uint32_t block_length = form_value.Unsigned();
5597 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
5598 }
5599 else
5600 {
5601 const DataExtractor& debug_loc_data = get_debug_loc_data();
5602 const dw_offset_t debug_loc_offset = form_value.Unsigned();
5603
5604 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
5605 if (loc_list_length > 0)
5606 {
5607 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
5608 assert (func_low_pc != LLDB_INVALID_ADDRESS);
5609 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
5610 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005611 }
5612 }
Greg Clayton7f995132011-10-04 22:41:51 +00005613 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005614
Greg Clayton7f995132011-10-04 22:41:51 +00005615 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5616 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5617 case DW_AT_declaration:
5618 case DW_AT_description:
5619 case DW_AT_endianity:
5620 case DW_AT_segment:
5621 case DW_AT_start_scope:
5622 case DW_AT_visibility:
5623 default:
5624 case DW_AT_abstract_origin:
5625 case DW_AT_sibling:
5626 case DW_AT_specification:
5627 break;
5628 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005629 }
5630 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005631
Greg Clayton7f995132011-10-04 22:41:51 +00005632 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005633 {
Greg Clayton7f995132011-10-04 22:41:51 +00005634 ValueType scope = eValueTypeInvalid;
5635
5636 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5637 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005638 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00005639
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005640 // DWARF doesn't specify if a DW_TAG_variable is a local, global
5641 // or static variable, so we have to do a little digging by
5642 // looking at the location of a varaible to see if it contains
5643 // a DW_OP_addr opcode _somewhere_ in the definition. I say
5644 // somewhere because clang likes to combine small global variables
5645 // into the same symbol and have locations like:
5646 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
5647 // So if we don't have a DW_TAG_formal_parameter, we can look at
5648 // the location to see if it contains a DW_OP_addr opcode, and
5649 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00005650 if (tag == DW_TAG_formal_parameter)
5651 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00005652 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005653 {
Greg Clayton96c09682012-01-04 22:56:43 +00005654 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00005655 // Check if the location has a DW_OP_addr with any address value...
5656 addr_t location_has_op_addr = false;
5657 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00005658 {
5659 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
5660 if (op_error)
5661 {
5662 StreamString strm;
5663 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005664 GetObjectFile()->GetModule()->ReportError ("0x%8.8x: %s has an invalid location: %s", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), strm.GetString().c_str());
Greg Clayton96c09682012-01-04 22:56:43 +00005665 }
5666 }
Greg Claytond1767f02011-12-08 02:13:16 +00005667
5668 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005669 {
Greg Claytond1767f02011-12-08 02:13:16 +00005670 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005671 {
Greg Claytond1767f02011-12-08 02:13:16 +00005672 scope = eValueTypeVariableGlobal;
5673
5674 if (m_debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005675 {
Greg Claytond1767f02011-12-08 02:13:16 +00005676 // When leaving the DWARF in the .o files on darwin,
5677 // when we have a global variable that wasn't initialized,
5678 // the .o file might not have allocated a virtual
5679 // address for the global variable. In this case it will
5680 // have created a symbol for the global variable
5681 // that is undefined and external and the value will
5682 // be the byte size of the variable. When we do the
5683 // address map in SymbolFileDWARFDebugMap we rely on
5684 // having an address, we need to do some magic here
5685 // so we can get the correct address for our global
5686 // variable. The address for all of these entries
5687 // will be zero, and there will be an undefined symbol
5688 // in this object file, and the executable will have
5689 // a matching symbol with a good address. So here we
5690 // dig up the correct address and replace it in the
5691 // location for the variable, and set the variable's
5692 // symbol context scope to be that of the main executable
5693 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00005694 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005695 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005696
Greg Claytond1767f02011-12-08 02:13:16 +00005697 // we have a possible uninitialized extern global
5698 Symtab *symtab = m_obj_file->GetSymtab();
5699 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005700 {
Greg Claytond1767f02011-12-08 02:13:16 +00005701 ConstString const_name(name);
5702 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
5703 eSymbolTypeUndefined,
5704 Symtab::eDebugNo,
5705 Symtab::eVisibilityExtern);
5706
5707 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005708 {
Greg Claytond1767f02011-12-08 02:13:16 +00005709 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
5710 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005711 {
Greg Claytond1767f02011-12-08 02:13:16 +00005712 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
5713 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
5714 eSymbolTypeData,
5715 Symtab::eDebugYes,
5716 Symtab::eVisibilityExtern);
5717 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005718 {
Greg Claytond1767f02011-12-08 02:13:16 +00005719 const AddressRange *defined_range = defined_symbol->GetAddressRangePtr();
5720 if (defined_range)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005721 {
Greg Claytond1767f02011-12-08 02:13:16 +00005722 const addr_t defined_addr = defined_range->GetBaseAddress().GetFileAddress();
5723 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005724 {
Greg Claytond1767f02011-12-08 02:13:16 +00005725 if (location.Update_DW_OP_addr (defined_addr))
5726 {
5727 symbol_context_scope = defined_symbol;
5728 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005729 }
5730 }
5731 }
5732 }
5733 }
5734 }
5735 }
5736 }
5737 }
Greg Claytond1767f02011-12-08 02:13:16 +00005738 else
5739 {
5740 scope = eValueTypeVariableStatic;
5741 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005742 }
5743 else
Greg Claytond1767f02011-12-08 02:13:16 +00005744 {
5745 scope = eValueTypeVariableLocal;
5746 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005747 }
Greg Clayton7f995132011-10-04 22:41:51 +00005748
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005749 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00005750 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005751 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00005752 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005753 case DW_TAG_subprogram:
5754 case DW_TAG_inlined_subroutine:
5755 case DW_TAG_lexical_block:
5756 if (sc.function)
5757 {
5758 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
5759 if (symbol_context_scope == NULL)
5760 symbol_context_scope = sc.function;
5761 }
5762 break;
5763
5764 default:
5765 symbol_context_scope = sc.comp_unit;
5766 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00005767 }
Greg Clayton7f995132011-10-04 22:41:51 +00005768 }
5769
Greg Clayton5cf58b92011-10-05 22:22:08 +00005770 if (symbol_context_scope)
5771 {
Greg Clayton81c22f62011-10-19 18:09:39 +00005772 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
5773 name,
5774 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00005775 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00005776 scope,
5777 symbol_context_scope,
5778 &decl,
5779 location,
5780 is_external,
5781 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00005782
5783 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
5784 }
5785 else
5786 {
5787 // Not ready to parse this variable yet. It might be a global
5788 // or static variable that is in a function scope and the function
5789 // in the symbol context wasn't filled in yet
5790 return var_sp;
5791 }
Greg Clayton7f995132011-10-04 22:41:51 +00005792 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005793 }
Greg Clayton7f995132011-10-04 22:41:51 +00005794 // Cache var_sp even if NULL (the variable was just a specification or
5795 // was missing vital information to be able to be displayed in the debugger
5796 // (missing location due to optimization, etc)) so we don't re-parse
5797 // this DIE over and over later...
5798 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005799 }
5800 return var_sp;
5801}
5802
Greg Claytonc662ec82011-06-17 22:10:16 +00005803
5804const DWARFDebugInfoEntry *
5805SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
5806 dw_offset_t spec_block_die_offset,
5807 DWARFCompileUnit **result_die_cu_handle)
5808{
5809 // Give the concrete function die specified by "func_die_offset", find the
5810 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
5811 // to "spec_block_die_offset"
5812 DWARFDebugInfo* info = DebugInfo();
5813
5814 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
5815 if (die)
5816 {
5817 assert (*result_die_cu_handle);
5818 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
5819 }
5820 return NULL;
5821}
5822
5823
5824const DWARFDebugInfoEntry *
5825SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
5826 const DWARFDebugInfoEntry *die,
5827 dw_offset_t spec_block_die_offset,
5828 DWARFCompileUnit **result_die_cu_handle)
5829{
5830 if (die)
5831 {
5832 switch (die->Tag())
5833 {
5834 case DW_TAG_subprogram:
5835 case DW_TAG_inlined_subroutine:
5836 case DW_TAG_lexical_block:
5837 {
5838 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
5839 {
5840 *result_die_cu_handle = dwarf_cu;
5841 return die;
5842 }
5843
5844 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
5845 {
5846 *result_die_cu_handle = dwarf_cu;
5847 return die;
5848 }
5849 }
5850 break;
5851 }
5852
5853 // Give the concrete function die specified by "func_die_offset", find the
5854 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
5855 // to "spec_block_die_offset"
5856 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
5857 {
5858 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
5859 child_die,
5860 spec_block_die_offset,
5861 result_die_cu_handle);
5862 if (result_die)
5863 return result_die;
5864 }
5865 }
5866
5867 *result_die_cu_handle = NULL;
5868 return NULL;
5869}
5870
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005871size_t
5872SymbolFileDWARF::ParseVariables
5873(
5874 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00005875 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00005876 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005877 const DWARFDebugInfoEntry *orig_die,
5878 bool parse_siblings,
5879 bool parse_children,
5880 VariableList* cc_variable_list
5881)
5882{
5883 if (orig_die == NULL)
5884 return 0;
5885
Greg Claytonc662ec82011-06-17 22:10:16 +00005886 VariableListSP variable_list_sp;
5887
5888 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005889 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00005890 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005891 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005892 dw_tag_t tag = die->Tag();
5893
5894 // Check to see if we have already parsed this variable or constant?
5895 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005896 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005897 if (cc_variable_list)
5898 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005899 }
5900 else
5901 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005902 // We haven't already parsed it, lets do that now.
5903 if ((tag == DW_TAG_variable) ||
5904 (tag == DW_TAG_constant) ||
5905 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005906 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005907 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00005908 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005909 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
5910 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
5911 switch (parent_tag)
5912 {
5913 case DW_TAG_compile_unit:
5914 if (sc.comp_unit != NULL)
5915 {
5916 variable_list_sp = sc.comp_unit->GetVariableList(false);
5917 if (variable_list_sp.get() == NULL)
5918 {
5919 variable_list_sp.reset(new VariableList());
5920 sc.comp_unit->SetVariableList(variable_list_sp);
5921 }
5922 }
5923 else
5924 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005925 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
5926 MakeUserID(sc_parent_die->GetOffset()),
5927 DW_TAG_value_to_name (parent_tag),
5928 MakeUserID(orig_die->GetOffset()),
5929 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00005930 }
5931 break;
5932
5933 case DW_TAG_subprogram:
5934 case DW_TAG_inlined_subroutine:
5935 case DW_TAG_lexical_block:
5936 if (sc.function != NULL)
5937 {
5938 // Check to see if we already have parsed the variables for the given scope
5939
Greg Clayton81c22f62011-10-19 18:09:39 +00005940 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00005941 if (block == NULL)
5942 {
5943 // This must be a specification or abstract origin with
5944 // a concrete block couterpart in the current function. We need
5945 // to find the concrete block so we can correctly add the
5946 // variable to it
5947 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
5948 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
5949 sc_parent_die->GetOffset(),
5950 &concrete_block_die_cu);
5951 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00005952 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00005953 }
5954
5955 if (block != NULL)
5956 {
5957 const bool can_create = false;
5958 variable_list_sp = block->GetBlockVariableList (can_create);
5959 if (variable_list_sp.get() == NULL)
5960 {
5961 variable_list_sp.reset(new VariableList());
5962 block->SetVariableList(variable_list_sp);
5963 }
5964 }
5965 }
5966 break;
5967
5968 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00005969 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
5970 MakeUserID(orig_die->GetOffset()),
5971 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00005972 break;
5973 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00005974 }
Greg Claytonc662ec82011-06-17 22:10:16 +00005975
5976 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005977 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00005978 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
5979 if (var_sp)
5980 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005981 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00005982 if (cc_variable_list)
5983 cc_variable_list->AddVariableIfUnique (var_sp);
5984 ++vars_added;
5985 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005986 }
5987 }
5988 }
Greg Claytonc662ec82011-06-17 22:10:16 +00005989
5990 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
5991
5992 if (!skip_children && parse_children && die->HasChildren())
5993 {
5994 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
5995 }
5996
5997 if (parse_siblings)
5998 die = die->GetSibling();
5999 else
6000 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006001 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006002 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006003}
6004
6005//------------------------------------------------------------------
6006// PluginInterface protocol
6007//------------------------------------------------------------------
6008const char *
6009SymbolFileDWARF::GetPluginName()
6010{
6011 return "SymbolFileDWARF";
6012}
6013
6014const char *
6015SymbolFileDWARF::GetShortPluginName()
6016{
6017 return GetPluginNameStatic();
6018}
6019
6020uint32_t
6021SymbolFileDWARF::GetPluginVersion()
6022{
6023 return 1;
6024}
6025
6026void
Greg Clayton6beaaa62011-01-17 03:46:26 +00006027SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
6028{
6029 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6030 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6031 if (clang_type)
6032 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6033}
6034
6035void
6036SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
6037{
6038 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6039 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6040 if (clang_type)
6041 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6042}
6043
Greg Claytona2721472011-06-25 00:44:06 +00006044void
Sean Callanancc427fa2011-07-30 02:42:06 +00006045SymbolFileDWARF::DumpIndexes ()
6046{
6047 StreamFile s(stdout, false);
6048
6049 s.Printf ("DWARF index for (%s) '%s/%s':",
6050 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
6051 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
6052 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
6053 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
6054 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
6055 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
6056 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
6057 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
6058 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
6059 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
6060 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
6061}
6062
6063void
6064SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
6065 const char *name,
6066 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00006067{
Sean Callanancc427fa2011-07-30 02:42:06 +00006068 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00006069
6070 if (iter == m_decl_ctx_to_die.end())
6071 return;
6072
Greg Claytoncb5860a2011-10-13 23:49:28 +00006073 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00006074 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006075 const DWARFDebugInfoEntry *context_die = *pos;
6076
6077 if (!results)
6078 return;
6079
6080 DWARFDebugInfo* info = DebugInfo();
6081
6082 DIEArray die_offsets;
6083
6084 DWARFCompileUnit* dwarf_cu = NULL;
6085 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00006086
6087 if (m_using_apple_tables)
6088 {
6089 if (m_apple_types_ap.get())
6090 m_apple_types_ap->FindByName (name, die_offsets);
6091 }
6092 else
6093 {
6094 if (!m_indexed)
6095 Index ();
6096
6097 m_type_index.Find (ConstString(name), die_offsets);
6098 }
6099
Greg Clayton220a0072011-12-09 08:48:30 +00006100 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006101
6102 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00006103 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006104 for (size_t i = 0; i < num_matches; ++i)
6105 {
6106 const dw_offset_t die_offset = die_offsets[i];
6107 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00006108
Greg Claytoncb5860a2011-10-13 23:49:28 +00006109 if (die->GetParent() != context_die)
6110 continue;
6111
6112 Type *matching_type = ResolveType (dwarf_cu, die);
6113
Greg Clayton42ce2f32011-12-12 21:50:19 +00006114 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006115 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
6116
6117 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
6118 {
6119 clang::TagDecl *tag_decl = tag_type->getDecl();
6120 results->push_back(tag_decl);
6121 }
6122 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
6123 {
6124 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
6125 results->push_back(typedef_decl);
6126 }
Greg Claytona2721472011-06-25 00:44:06 +00006127 }
6128 }
6129 }
6130}
6131
6132void
6133SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00006134 const clang::DeclContext *decl_context,
6135 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00006136 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
6137{
Greg Clayton85ae2e12011-10-18 23:36:41 +00006138
6139 switch (decl_context->getDeclKind())
6140 {
6141 case clang::Decl::Namespace:
6142 case clang::Decl::TranslationUnit:
6143 {
6144 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6145 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
6146 }
6147 break;
6148 default:
6149 break;
6150 }
Greg Claytona2721472011-06-25 00:44:06 +00006151}
Greg Claytoncaab74e2012-01-28 00:48:57 +00006152
6153bool
6154SymbolFileDWARF::LayoutRecordType (void *baton,
6155 const clang::RecordDecl *record_decl,
6156 uint64_t &size,
6157 uint64_t &alignment,
6158 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6159 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6160 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6161{
6162 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6163 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
6164}
6165
6166
6167bool
6168SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
6169 uint64_t &bit_size,
6170 uint64_t &alignment,
6171 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6172 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6173 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6174{
6175 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
6176 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
6177 bool success = false;
6178 base_offsets.clear();
6179 vbase_offsets.clear();
6180 if (pos != m_record_decl_to_layout_map.end())
6181 {
6182 bit_size = pos->second.bit_size;
6183 alignment = pos->second.alignment;
6184 field_offsets.swap(pos->second.field_offsets);
6185 m_record_decl_to_layout_map.erase(pos);
6186 success = true;
6187 }
6188 else
6189 {
6190 bit_size = 0;
6191 alignment = 0;
6192 field_offsets.clear();
6193 }
6194
6195 if (log)
6196 GetObjectFile()->GetModule()->LogMessage (log.get(),
6197 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
6198 record_decl,
6199 bit_size,
6200 alignment,
6201 (uint32_t)field_offsets.size(),
6202 (uint32_t)base_offsets.size(),
6203 (uint32_t)vbase_offsets.size(),
6204 success);
6205 return success;
6206}
6207
6208
6209