blob: 689bcdcddde24f9d91d86e13f6a4ec6fc52f5211 [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 Clayton1c9e5ac2011-02-09 19:06:17 +0000205 m_ranges(),
206 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207{
208}
209
210SymbolFileDWARF::~SymbolFileDWARF()
211{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000212 if (m_is_external_ast_source)
213 m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource ();
214}
215
216static const ConstString &
217GetDWARFMachOSegmentName ()
218{
219 static ConstString g_dwarf_section_name ("__DWARF");
220 return g_dwarf_section_name;
221}
222
Greg Claytone576ab22011-02-15 00:19:15 +0000223UniqueDWARFASTTypeMap &
224SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
225{
226 if (m_debug_map_symfile)
227 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
228 return m_unique_ast_type_map;
229}
230
Greg Clayton6beaaa62011-01-17 03:46:26 +0000231ClangASTContext &
232SymbolFileDWARF::GetClangASTContext ()
233{
234 if (m_debug_map_symfile)
235 return m_debug_map_symfile->GetClangASTContext ();
236
237 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
238 if (!m_is_external_ast_source)
239 {
240 m_is_external_ast_source = true;
241 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
242 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
243 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000244 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000245 this));
246
247 ast.SetExternalSource (ast_source_ap);
248 }
249 return ast;
250}
251
252void
253SymbolFileDWARF::InitializeObject()
254{
255 // Install our external AST source callbacks so we can complete Clang types.
256 Module *module = m_obj_file->GetModule();
257 if (module)
258 {
259 const SectionList *section_list = m_obj_file->GetSectionList();
260
261 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
262
263 // Memory map the DWARF mach-o segment so we have everything mmap'ed
264 // to keep our heap memory usage down.
265 if (section)
266 section->MemoryMapSectionDataFromObjectFile(m_obj_file, m_dwarf_data);
267 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000268 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000269 if (m_data_apple_names.GetByteSize() > 0)
270 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000271 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
272 if (m_apple_names_ap->IsValid())
273 m_using_apple_tables = true;
274 else
Greg Clayton7f995132011-10-04 22:41:51 +0000275 m_apple_names_ap.reset();
276 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000277 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000278 if (m_data_apple_types.GetByteSize() > 0)
279 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000280 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
281 if (m_apple_types_ap->IsValid())
282 m_using_apple_tables = true;
283 else
Greg Clayton7f995132011-10-04 22:41:51 +0000284 m_apple_types_ap.reset();
285 }
286
287 get_apple_namespaces_data();
288 if (m_data_apple_namespaces.GetByteSize() > 0)
289 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000290 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
291 if (m_apple_namespaces_ap->IsValid())
292 m_using_apple_tables = true;
293 else
Greg Clayton7f995132011-10-04 22:41:51 +0000294 m_apple_namespaces_ap.reset();
295 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000296
Greg Clayton5009f9d2011-10-27 17:55:14 +0000297 get_apple_objc_data();
298 if (m_data_apple_objc.GetByteSize() > 0)
299 {
300 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
301 if (m_apple_objc_ap->IsValid())
302 m_using_apple_tables = true;
303 else
304 m_apple_objc_ap.reset();
305 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306}
307
308bool
309SymbolFileDWARF::SupportedVersion(uint16_t version)
310{
311 return version == 2 || version == 3;
312}
313
314uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000315SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316{
317 uint32_t abilities = 0;
318 if (m_obj_file != NULL)
319 {
320 const Section* section = NULL;
321 const SectionList *section_list = m_obj_file->GetSectionList();
322 if (section_list == NULL)
323 return 0;
324
325 uint64_t debug_abbrev_file_size = 0;
326 uint64_t debug_aranges_file_size = 0;
327 uint64_t debug_frame_file_size = 0;
328 uint64_t debug_info_file_size = 0;
329 uint64_t debug_line_file_size = 0;
330 uint64_t debug_loc_file_size = 0;
331 uint64_t debug_macinfo_file_size = 0;
332 uint64_t debug_pubnames_file_size = 0;
333 uint64_t debug_pubtypes_file_size = 0;
334 uint64_t debug_ranges_file_size = 0;
335 uint64_t debug_str_file_size = 0;
336
Greg Clayton6beaaa62011-01-17 03:46:26 +0000337 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338
339 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000340 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341
Greg Clayton4ceb9982010-07-21 22:54:26 +0000342 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 if (section != NULL)
344 {
345 debug_info_file_size = section->GetByteSize();
346
Greg Clayton4ceb9982010-07-21 22:54:26 +0000347 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 if (section)
349 debug_abbrev_file_size = section->GetByteSize();
350 else
351 m_flags.Set (flagsGotDebugAbbrevData);
352
Greg Clayton4ceb9982010-07-21 22:54:26 +0000353 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 if (section)
355 debug_aranges_file_size = section->GetByteSize();
356 else
357 m_flags.Set (flagsGotDebugArangesData);
358
Greg Clayton4ceb9982010-07-21 22:54:26 +0000359 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 if (section)
361 debug_frame_file_size = section->GetByteSize();
362 else
363 m_flags.Set (flagsGotDebugFrameData);
364
Greg Clayton4ceb9982010-07-21 22:54:26 +0000365 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366 if (section)
367 debug_line_file_size = section->GetByteSize();
368 else
369 m_flags.Set (flagsGotDebugLineData);
370
Greg Clayton4ceb9982010-07-21 22:54:26 +0000371 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 if (section)
373 debug_loc_file_size = section->GetByteSize();
374 else
375 m_flags.Set (flagsGotDebugLocData);
376
Greg Clayton4ceb9982010-07-21 22:54:26 +0000377 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 if (section)
379 debug_macinfo_file_size = section->GetByteSize();
380 else
381 m_flags.Set (flagsGotDebugMacInfoData);
382
Greg Clayton4ceb9982010-07-21 22:54:26 +0000383 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 if (section)
385 debug_pubnames_file_size = section->GetByteSize();
386 else
387 m_flags.Set (flagsGotDebugPubNamesData);
388
Greg Clayton4ceb9982010-07-21 22:54:26 +0000389 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 if (section)
391 debug_pubtypes_file_size = section->GetByteSize();
392 else
393 m_flags.Set (flagsGotDebugPubTypesData);
394
Greg Clayton4ceb9982010-07-21 22:54:26 +0000395 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396 if (section)
397 debug_ranges_file_size = section->GetByteSize();
398 else
399 m_flags.Set (flagsGotDebugRangesData);
400
Greg Clayton4ceb9982010-07-21 22:54:26 +0000401 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 if (section)
403 debug_str_file_size = section->GetByteSize();
404 else
405 m_flags.Set (flagsGotDebugStrData);
406 }
407
408 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
409 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
410
411 if (debug_line_file_size > 0)
412 abilities |= LineTables;
413
414 if (debug_aranges_file_size > 0)
415 abilities |= AddressAcceleratorTable;
416
417 if (debug_pubnames_file_size > 0)
418 abilities |= FunctionAcceleratorTable;
419
420 if (debug_pubtypes_file_size > 0)
421 abilities |= TypeAcceleratorTable;
422
423 if (debug_macinfo_file_size > 0)
424 abilities |= MacroInformation;
425
426 if (debug_frame_file_size > 0)
427 abilities |= CallFrameInformation;
428 }
429 return abilities;
430}
431
432const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000433SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434{
435 if (m_flags.IsClear (got_flag))
436 {
437 m_flags.Set (got_flag);
438 const SectionList *section_list = m_obj_file->GetSectionList();
439 if (section_list)
440 {
Greg Clayton4ceb9982010-07-21 22:54:26 +0000441 Section *section = section_list->FindSectionByType(sect_type, true).get();
442 if (section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443 {
444 // See if we memory mapped the DWARF segment?
445 if (m_dwarf_data.GetByteSize())
446 {
447 data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize());
448 }
449 else
450 {
451 if (section->ReadSectionDataFromObjectFile(m_obj_file, data) == 0)
452 data.Clear();
453 }
454 }
455 }
456 }
457 return data;
458}
459
460const DataExtractor&
461SymbolFileDWARF::get_debug_abbrev_data()
462{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000463 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464}
465
466const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000467SymbolFileDWARF::get_debug_aranges_data()
468{
469 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
470}
471
472const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473SymbolFileDWARF::get_debug_frame_data()
474{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000475 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476}
477
478const DataExtractor&
479SymbolFileDWARF::get_debug_info_data()
480{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000481 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482}
483
484const DataExtractor&
485SymbolFileDWARF::get_debug_line_data()
486{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000487 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488}
489
490const DataExtractor&
491SymbolFileDWARF::get_debug_loc_data()
492{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000493 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494}
495
496const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497SymbolFileDWARF::get_debug_ranges_data()
498{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000499 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500}
501
502const DataExtractor&
503SymbolFileDWARF::get_debug_str_data()
504{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000505 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506}
507
Greg Claytonf9eec202011-09-01 23:16:13 +0000508const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000509SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000510{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000511 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000512}
513
514const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000515SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000516{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000517 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000518}
519
Greg Clayton7f995132011-10-04 22:41:51 +0000520const DataExtractor&
521SymbolFileDWARF::get_apple_namespaces_data()
522{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000523 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
524}
525
526const DataExtractor&
527SymbolFileDWARF::get_apple_objc_data()
528{
529 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000530}
531
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532
533DWARFDebugAbbrev*
534SymbolFileDWARF::DebugAbbrev()
535{
536 if (m_abbr.get() == NULL)
537 {
538 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
539 if (debug_abbrev_data.GetByteSize() > 0)
540 {
541 m_abbr.reset(new DWARFDebugAbbrev());
542 if (m_abbr.get())
543 m_abbr->Parse(debug_abbrev_data);
544 }
545 }
546 return m_abbr.get();
547}
548
549const DWARFDebugAbbrev*
550SymbolFileDWARF::DebugAbbrev() const
551{
552 return m_abbr.get();
553}
554
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555
556DWARFDebugInfo*
557SymbolFileDWARF::DebugInfo()
558{
559 if (m_info.get() == NULL)
560 {
561 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
562 if (get_debug_info_data().GetByteSize() > 0)
563 {
564 m_info.reset(new DWARFDebugInfo());
565 if (m_info.get())
566 {
567 m_info->SetDwarfData(this);
568 }
569 }
570 }
571 return m_info.get();
572}
573
574const DWARFDebugInfo*
575SymbolFileDWARF::DebugInfo() const
576{
577 return m_info.get();
578}
579
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580DWARFCompileUnit*
581SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
582{
583 DWARFDebugInfo* info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +0000584 if (info && UserIDMatches(cu_uid))
585 return info->GetCompileUnit((dw_offset_t)cu_uid).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586 return NULL;
587}
588
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589
590DWARFDebugRanges*
591SymbolFileDWARF::DebugRanges()
592{
593 if (m_ranges.get() == NULL)
594 {
595 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
596 if (get_debug_ranges_data().GetByteSize() > 0)
597 {
598 m_ranges.reset(new DWARFDebugRanges());
599 if (m_ranges.get())
600 m_ranges->Extract(this);
601 }
602 }
603 return m_ranges.get();
604}
605
606const DWARFDebugRanges*
607SymbolFileDWARF::DebugRanges() const
608{
609 return m_ranges.get();
610}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611
612bool
Greg Clayton96d7d742010-11-10 23:42:09 +0000613SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614{
Greg Clayton96d7d742010-11-10 23:42:09 +0000615 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000617 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618 if (cu_die)
619 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000620 const char * cu_die_name = cu_die->GetName(this, curr_cu);
621 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
Jim Ingham0f35ac22011-08-24 23:34:20 +0000622 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 if (cu_die_name)
624 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000625 FileSpec cu_file_spec;
626
Greg Clayton7bd65b92011-02-09 23:39:34 +0000627 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000629 // If we have a full path to the compile unit, we don't need to resolve
630 // the file. This can be expensive e.g. when the source files are NFS mounted.
631 cu_file_spec.SetFile (cu_die_name, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 }
633 else
634 {
635 std::string fullpath(cu_comp_dir);
636 if (*fullpath.rbegin() != '/')
637 fullpath += '/';
638 fullpath += cu_die_name;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000639 cu_file_spec.SetFile (fullpath.c_str(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640 }
641
Greg Clayton81c22f62011-10-19 18:09:39 +0000642 compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(),
643 curr_cu,
644 cu_file_spec,
645 MakeUserID(curr_cu->GetOffset()),
646 cu_language));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647 if (compile_unit_sp.get())
648 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000649 curr_cu->SetUserData(compile_unit_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 return true;
651 }
652 }
653 }
654 }
655 return false;
656}
657
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658uint32_t
659SymbolFileDWARF::GetNumCompileUnits()
660{
661 DWARFDebugInfo* info = DebugInfo();
662 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 return 0;
665}
666
667CompUnitSP
668SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
669{
670 CompUnitSP comp_unit;
671 DWARFDebugInfo* info = DebugInfo();
672 if (info)
673 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000674 DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx);
675 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676 {
677 // Our symbol vendor shouldn't be asking us to add a compile unit that
678 // has already been added to it, which this DWARF plug-in knows as it
679 // stores the lldb compile unit (CompileUnit) pointer in each
680 // DWARFCompileUnit object when it gets added.
Greg Clayton96d7d742010-11-10 23:42:09 +0000681 assert(curr_cu->GetUserData() == NULL);
682 ParseCompileUnit(curr_cu, comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 }
684 }
685 return comp_unit;
686}
687
688static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000689AddRangesToBlock (Block& block,
690 DWARFDebugRanges::RangeList& ranges,
691 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000693 const size_t num_ranges = ranges.GetSize();
694 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000696 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
697 const addr_t range_base = range.GetRangeBase();
698 assert (range_base >= block_base_addr);
699 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000701 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702}
703
704
705Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000706SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707{
708 DWARFDebugRanges::RangeList func_ranges;
709 const char *name = NULL;
710 const char *mangled = NULL;
711 int decl_file = 0;
712 int decl_line = 0;
713 int decl_column = 0;
714 int call_file = 0;
715 int call_line = 0;
716 int call_column = 0;
717 DWARFExpression frame_base;
718
Greg Claytonc93237c2010-10-01 20:48:32 +0000719 assert (die->Tag() == DW_TAG_subprogram);
720
721 if (die->Tag() != DW_TAG_subprogram)
722 return NULL;
723
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 if (die->GetDIENamesAndRanges(this, dwarf_cu, name, mangled, func_ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column, &frame_base))
725 {
726 // Union of all ranges in the function DIE (if the function is discontiguous)
727 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000728 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
729 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
731 {
732 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
733 if (func_range.GetBaseAddress().IsValid())
734 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
735 }
736
737 if (func_range.GetBaseAddress().IsValid())
738 {
739 Mangled func_name;
740 if (mangled)
741 func_name.SetValue(mangled, true);
742 else if (name)
743 func_name.SetValue(name, false);
744
745 FunctionSP func_sp;
746 std::auto_ptr<Declaration> decl_ap;
747 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000748 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
749 decl_line,
750 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000752 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000753 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754
755 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
756
757 func_range.GetBaseAddress().ResolveLinkedAddress();
758
Greg Clayton81c22f62011-10-19 18:09:39 +0000759 const user_id_t func_user_id = MakeUserID(die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760 func_sp.reset(new Function (sc.comp_unit,
Greg Clayton81c22f62011-10-19 18:09:39 +0000761 func_user_id, // UserID is the DIE offset
762 func_user_id,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763 func_name,
764 func_type,
765 func_range)); // first address range
766
767 if (func_sp.get() != NULL)
768 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000769 if (frame_base.IsValid())
770 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771 sc.comp_unit->AddFunction(func_sp);
772 return func_sp.get();
773 }
774 }
775 }
776 return NULL;
777}
778
779size_t
780SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
781{
782 assert (sc.comp_unit);
783 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000784 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 if (dwarf_cu)
786 {
787 DWARFDIECollection function_dies;
788 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
789 size_t func_idx;
790 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
791 {
792 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000793 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794 {
795 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
796 ++functions_added;
797 }
798 }
799 //FixupTypes();
800 }
801 return functions_added;
802}
803
804bool
805SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
806{
807 assert (sc.comp_unit);
Greg Clayton96d7d742010-11-10 23:42:09 +0000808 DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
809 assert (curr_cu);
810 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811
812 if (cu_die)
813 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000814 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
815 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 +0000816
817 // All file indexes in DWARF are one based and a file of index zero is
818 // supposed to be the compile unit itself.
819 support_files.Append (*sc.comp_unit);
820
821 return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
822 }
823 return false;
824}
825
826struct ParseDWARFLineTableCallbackInfo
827{
828 LineTable* line_table;
829 const SectionList *section_list;
830 lldb::addr_t prev_sect_file_base_addr;
831 lldb::addr_t curr_sect_file_base_addr;
832 bool is_oso_for_debug_map;
833 bool prev_in_final_executable;
834 DWARFDebugLine::Row prev_row;
835 SectionSP prev_section_sp;
836 SectionSP curr_section_sp;
837};
838
839//----------------------------------------------------------------------
840// ParseStatementTableCallback
841//----------------------------------------------------------------------
842static void
843ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
844{
845 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
846 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
847 {
848 // Just started parsing the line table
849 }
850 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
851 {
852 // Done parsing line table, nothing to do for the cleanup
853 }
854 else
855 {
856 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
857 // We have a new row, lets append it
858
859 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
860 {
861 info->prev_section_sp = info->curr_section_sp;
862 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
863 // If this is an end sequence entry, then we subtract one from the
864 // address to make sure we get an address that is not the end of
865 // a section.
866 if (state.end_sequence && state.address != 0)
867 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
868 else
869 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
870
871 if (info->curr_section_sp.get())
872 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
873 else
874 info->curr_sect_file_base_addr = 0;
875 }
876 if (info->curr_section_sp.get())
877 {
878 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
879 // Check for the fancy section magic to determine if we
880
881 if (info->is_oso_for_debug_map)
882 {
883 // When this is a debug map object file that contains DWARF
884 // (referenced from an N_OSO debug map nlist entry) we will have
885 // a file address in the file range for our section from the
886 // original .o file, and a load address in the executable that
887 // contains the debug map.
888 //
889 // If the sections for the file range and load range are
890 // different, we have a remapped section for the function and
891 // this address is resolved. If they are the same, then the
892 // function for this address didn't make it into the final
893 // executable.
894 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
895
896 // If we are doing DWARF with debug map, then we need to carefully
897 // add each line table entry as there may be gaps as functions
898 // get moved around or removed.
899 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
900 {
901 if (info->prev_in_final_executable)
902 {
903 bool terminate_previous_entry = false;
904 if (!curr_in_final_executable)
905 {
906 // Check for the case where the previous line entry
907 // in a function made it into the final executable,
908 // yet the current line entry falls in a function
909 // that didn't. The line table used to be contiguous
910 // through this address range but now it isn't. We
911 // need to terminate the previous line entry so
912 // that we can reconstruct the line range correctly
913 // for it and to keep the line table correct.
914 terminate_previous_entry = true;
915 }
916 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
917 {
918 // Check for cases where the line entries used to be
919 // contiguous address ranges, but now they aren't.
920 // This can happen when order files specify the
921 // ordering of the functions.
922 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
923 Section *curr_sect = info->curr_section_sp.get();
924 Section *prev_sect = info->prev_section_sp.get();
925 assert (curr_sect->GetLinkedSection());
926 assert (prev_sect->GetLinkedSection());
927 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
928 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
929 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
930 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
931 if (object_file_addr_delta != linked_file_addr_delta)
932 terminate_previous_entry = true;
933 }
934
935 if (terminate_previous_entry)
936 {
937 line_table->InsertLineEntry (info->prev_section_sp,
938 state.address - info->prev_sect_file_base_addr,
939 info->prev_row.line,
940 info->prev_row.column,
941 info->prev_row.file,
942 false, // is_stmt
943 false, // basic_block
944 false, // state.prologue_end
945 false, // state.epilogue_begin
946 true); // end_sequence);
947 }
948 }
949 }
950
951 if (curr_in_final_executable)
952 {
953 line_table->InsertLineEntry (info->curr_section_sp,
954 curr_line_section_offset,
955 state.line,
956 state.column,
957 state.file,
958 state.is_stmt,
959 state.basic_block,
960 state.prologue_end,
961 state.epilogue_begin,
962 state.end_sequence);
963 info->prev_section_sp = info->curr_section_sp;
964 }
965 else
966 {
967 // If the current address didn't make it into the final
968 // executable, the current section will be the __text
969 // segment in the .o file, so we need to clear this so
970 // we can catch the next function that did make it into
971 // the final executable.
972 info->prev_section_sp.reset();
973 info->curr_section_sp.reset();
974 }
975
976 info->prev_in_final_executable = curr_in_final_executable;
977 }
978 else
979 {
980 // We are not in an object file that contains DWARF for an
981 // N_OSO, this is just a normal DWARF file. The DWARF spec
982 // guarantees that the addresses will be in increasing order
983 // so, since we store line tables in file address order, we
984 // can always just append the line entry without needing to
985 // search for the correct insertion point (we don't need to
986 // use LineEntry::InsertLineEntry()).
987 line_table->AppendLineEntry (info->curr_section_sp,
988 curr_line_section_offset,
989 state.line,
990 state.column,
991 state.file,
992 state.is_stmt,
993 state.basic_block,
994 state.prologue_end,
995 state.epilogue_begin,
996 state.end_sequence);
997 }
998 }
999
1000 info->prev_row = state;
1001 }
1002}
1003
1004bool
1005SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1006{
1007 assert (sc.comp_unit);
1008 if (sc.comp_unit->GetLineTable() != NULL)
1009 return true;
1010
1011 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
1012 if (dwarf_cu)
1013 {
1014 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001015 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001017 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1018 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001020 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1021 if (line_table_ap.get())
1022 {
1023 ParseDWARFLineTableCallbackInfo info = {
1024 line_table_ap.get(),
1025 m_obj_file->GetSectionList(),
1026 0,
1027 0,
1028 m_debug_map_symfile != NULL,
1029 false,
1030 DWARFDebugLine::Row(),
1031 SectionSP(),
1032 SectionSP()
1033 };
1034 uint32_t offset = cu_line_offset;
1035 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1036 sc.comp_unit->SetLineTable(line_table_ap.release());
1037 return true;
1038 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039 }
1040 }
1041 }
1042 return false;
1043}
1044
1045size_t
1046SymbolFileDWARF::ParseFunctionBlocks
1047(
1048 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001049 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001050 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051 const DWARFDebugInfoEntry *die,
1052 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001053 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054)
1055{
1056 size_t blocks_added = 0;
1057 while (die != NULL)
1058 {
1059 dw_tag_t tag = die->Tag();
1060
1061 switch (tag)
1062 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001064 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065 case DW_TAG_lexical_block:
1066 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001067 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001068 if (tag == DW_TAG_subprogram)
1069 {
1070 // Skip any DW_TAG_subprogram DIEs that are inside
1071 // of a normal or inlined functions. These will be
1072 // parsed on their own as separate entities.
1073
1074 if (depth > 0)
1075 break;
1076
1077 block = parent_block;
1078 }
1079 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001080 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001081 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001082 parent_block->AddChild(block_sp);
1083 block = block_sp.get();
1084 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001085 DWARFDebugRanges::RangeList ranges;
1086 const char *name = NULL;
1087 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001088
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 int decl_file = 0;
1090 int decl_line = 0;
1091 int decl_column = 0;
1092 int call_file = 0;
1093 int call_line = 0;
1094 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001095 if (die->GetDIENamesAndRanges (this,
1096 dwarf_cu,
1097 name,
1098 mangled_name,
1099 ranges,
1100 decl_file, decl_line, decl_column,
1101 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102 {
1103 if (tag == DW_TAG_subprogram)
1104 {
1105 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001106 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001108 else if (tag == DW_TAG_inlined_subroutine)
1109 {
1110 // We get called here for inlined subroutines in two ways.
1111 // The first time is when we are making the Function object
1112 // for this inlined concrete instance. Since we're creating a top level block at
1113 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1114 // adjust the containing address.
1115 // The second time is when we are parsing the blocks inside the function that contains
1116 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1117 // function the offset will be for that function.
1118 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1119 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001120 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001121 }
1122 }
1123
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001124 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125
1126 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1127 {
1128 std::auto_ptr<Declaration> decl_ap;
1129 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001130 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1131 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132
1133 std::auto_ptr<Declaration> call_ap;
1134 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001135 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1136 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001138 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 }
1140
1141 ++blocks_added;
1142
Greg Claytondd7feaf2011-08-12 17:54:33 +00001143 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001145 blocks_added += ParseFunctionBlocks (sc,
1146 block,
1147 dwarf_cu,
1148 die->GetFirstChild(),
1149 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001150 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151 }
1152 }
1153 }
1154 break;
1155 default:
1156 break;
1157 }
1158
Greg Claytondd7feaf2011-08-12 17:54:33 +00001159 // Only parse siblings of the block if we are not at depth zero. A depth
1160 // of zero indicates we are currently parsing the top level
1161 // DW_TAG_subprogram DIE
1162
1163 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001164 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001165 else
1166 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167 }
1168 return blocks_added;
1169}
1170
Greg Claytonf0705c82011-10-22 03:33:13 +00001171bool
1172SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1173 const DWARFDebugInfoEntry *parent_die,
1174 ClangASTContext::TemplateParameterInfos &template_param_infos)
1175{
1176
1177 if (parent_die == NULL)
1178 return NULL;
1179
1180 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1181
1182 Args template_parameter_names;
1183 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1184 die != NULL;
1185 die = die->GetSibling())
1186 {
1187 const dw_tag_t tag = die->Tag();
1188
1189 switch (tag)
1190 {
1191 case DW_TAG_template_type_parameter:
1192 case DW_TAG_template_value_parameter:
1193 {
1194 DWARFDebugInfoEntry::Attributes attributes;
1195 const size_t num_attributes = die->GetAttributes (this,
1196 dwarf_cu,
1197 fixed_form_sizes,
1198 attributes);
1199 const char *name = NULL;
1200 Type *lldb_type = NULL;
1201 clang_type_t clang_type = NULL;
1202 uint64_t uval64 = 0;
1203 bool uval64_valid = false;
1204 if (num_attributes > 0)
1205 {
1206 DWARFFormValue form_value;
1207 for (size_t i=0; i<num_attributes; ++i)
1208 {
1209 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1210
1211 switch (attr)
1212 {
1213 case DW_AT_name:
1214 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1215 name = form_value.AsCString(&get_debug_str_data());
1216 break;
1217
1218 case DW_AT_type:
1219 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1220 {
1221 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1222 lldb_type = ResolveTypeUID(type_die_offset);
1223 if (lldb_type)
1224 clang_type = lldb_type->GetClangForwardType();
1225 }
1226 break;
1227
1228 case DW_AT_const_value:
1229 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1230 {
1231 uval64_valid = true;
1232 uval64 = form_value.Unsigned();
1233 }
1234 break;
1235 default:
1236 break;
1237 }
1238 }
1239
1240 if (name && lldb_type && clang_type)
1241 {
1242 bool is_signed = false;
1243 template_param_infos.names.push_back(name);
1244 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1245 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1246 {
1247 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1248 template_param_infos.args.push_back (clang::TemplateArgument (llvm::APSInt(apint), clang_qual_type));
1249 }
1250 else
1251 {
1252 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1253 }
1254 }
1255 else
1256 {
1257 return false;
1258 }
1259
1260 }
1261 }
1262 break;
1263
1264 default:
1265 break;
1266 }
1267 }
1268 if (template_param_infos.args.empty())
1269 return false;
1270 return template_param_infos.args.size() == template_param_infos.names.size();
1271}
1272
1273clang::ClassTemplateDecl *
1274SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001275 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001276 const char *parent_name,
1277 int tag_decl_kind,
1278 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1279{
1280 if (template_param_infos.IsValid())
1281 {
1282 std::string template_basename(parent_name);
1283 template_basename.erase (template_basename.find('<'));
1284 ClangASTContext &ast = GetClangASTContext();
1285
1286 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001287 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001288 template_basename.c_str(),
1289 tag_decl_kind,
1290 template_param_infos);
1291 }
1292 return NULL;
1293}
1294
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295size_t
1296SymbolFileDWARF::ParseChildMembers
1297(
1298 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001299 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001300 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001301 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001302 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1304 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001305 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001306 AccessType& default_accessibility,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307 bool &is_a_class
1308)
1309{
1310 if (parent_die == NULL)
1311 return 0;
1312
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313 size_t count = 0;
1314 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001315 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001316 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001317
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1319 {
1320 dw_tag_t tag = die->Tag();
1321
1322 switch (tag)
1323 {
1324 case DW_TAG_member:
1325 {
1326 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001327 const size_t num_attributes = die->GetAttributes (this,
1328 dwarf_cu,
1329 fixed_form_sizes,
1330 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331 if (num_attributes > 0)
1332 {
1333 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001334 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001335 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001336 const char *prop_name = NULL;
1337 const char *prop_getter_name = NULL;
1338 const char *prop_setter_name = NULL;
1339 uint32_t prop_attributes = 0;
1340
1341
Greg Clayton24739922010-10-13 03:15:28 +00001342 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001344 AccessType accessibility = eAccessNone;
Greg Clayton73b472d2010-10-27 03:32:59 +00001345 //off_t member_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346 size_t byte_size = 0;
1347 size_t bit_offset = 0;
1348 size_t bit_size = 0;
1349 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001350 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351 {
1352 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1353 DWARFFormValue form_value;
1354 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1355 {
1356 switch (attr)
1357 {
1358 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1359 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1360 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1361 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1362 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1363 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1364 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1365 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1366 case DW_AT_data_member_location:
Greg Clayton73b472d2010-10-27 03:32:59 +00001367// if (form_value.BlockData())
1368// {
1369// Value initialValue(0);
1370// Value memberOffset(0);
1371// const DataExtractor& debug_info_data = get_debug_info_data();
1372// uint32_t block_length = form_value.Unsigned();
1373// uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1374// if (DWARFExpression::Evaluate(NULL, NULL, debug_info_data, NULL, NULL, block_offset, block_length, eRegisterKindDWARF, &initialValue, memberOffset, NULL))
1375// {
1376// member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1377// }
1378// }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379 break;
1380
Greg Clayton8cf05932010-07-22 18:30:50 +00001381 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton24739922010-10-13 03:15:28 +00001382 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383 case DW_AT_declaration:
1384 case DW_AT_description:
1385 case DW_AT_mutable:
1386 case DW_AT_visibility:
Jim Inghame3ae82a2011-11-12 01:36:43 +00001387
1388 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1389 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1390 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1391 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1392
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393 default:
1394 case DW_AT_sibling:
1395 break;
1396 }
1397 }
1398 }
Sean Callanan5a477cf2010-10-30 01:56:10 +00001399
Greg Clayton44953932011-10-25 01:25:35 +00001400 // Clang has a DWARF generation bug where sometimes it
1401 // represents fields that are references with bad byte size
1402 // and bit size/offset information such as:
1403 //
1404 // DW_AT_byte_size( 0x00 )
1405 // DW_AT_bit_size( 0x40 )
1406 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1407 //
1408 // So check the bit offset to make sure it is sane, and if
1409 // the values are not sane, remove them. If we don't do this
1410 // then we will end up with a crash if we try to use this
1411 // type in an expression when clang becomes unhappy with its
1412 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001413
Greg Clayton44953932011-10-25 01:25:35 +00001414 if (bit_offset > 128)
1415 {
1416 bit_size = 0;
1417 bit_offset = 0;
1418 }
1419
1420 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001421 if (class_language == eLanguageTypeObjC ||
1422 class_language == eLanguageTypeObjC_plus_plus)
1423 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001424
1425 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1426 {
1427 // Not all compilers will mark the vtable pointer
1428 // member as artificial (llvm-gcc). We can't have
1429 // the virtual members in our classes otherwise it
1430 // throws off all child offsets since we end up
1431 // having and extra pointer sized member in our
1432 // class layouts.
1433 is_artificial = true;
1434 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001435
Greg Clayton24739922010-10-13 03:15:28 +00001436 if (is_artificial == false)
1437 {
1438 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001439 clang::FieldDecl *field_decl = NULL;
Greg Claytond16e1e52011-07-12 17:06:17 +00001440 if (member_type)
1441 {
1442 if (accessibility == eAccessNone)
1443 accessibility = default_accessibility;
1444 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001445
Jim Inghame3ae82a2011-11-12 01:36:43 +00001446 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
Greg Clayton42ce2f32011-12-12 21:50:19 +00001447 name,
1448 member_type->GetClangLayoutType(),
1449 accessibility,
1450 bit_size);
Greg Claytond16e1e52011-07-12 17:06:17 +00001451 }
1452 else
1453 {
1454 if (name)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001455 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1456 MakeUserID(die->GetOffset()),
1457 name,
1458 encoding_uid);
Greg Claytond16e1e52011-07-12 17:06:17 +00001459 else
Greg Claytone38a5ed2012-01-05 03:57:59 +00001460 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1461 MakeUserID(die->GetOffset()),
1462 encoding_uid);
Greg Claytond16e1e52011-07-12 17:06:17 +00001463 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001464
1465 if (prop_name != NULL)
1466 {
1467
1468 clang::ObjCIvarDecl *ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1469 assert (ivar_decl != NULL);
1470
1471
1472 GetClangASTContext().AddObjCClassProperty (class_clang_type,
1473 prop_name,
1474 0,
1475 ivar_decl,
1476 prop_setter_name,
1477 prop_getter_name,
1478 prop_attributes);
1479 }
Greg Clayton24739922010-10-13 03:15:28 +00001480 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001482 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483 }
1484 break;
1485
1486 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001487 // Let the type parsing code handle this one for us.
1488 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489 break;
1490
1491 case DW_TAG_inheritance:
1492 {
1493 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001494 if (default_accessibility == eAccessNone)
1495 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001496 // TODO: implement DW_TAG_inheritance type parsing
1497 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001498 const size_t num_attributes = die->GetAttributes (this,
1499 dwarf_cu,
1500 fixed_form_sizes,
1501 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502 if (num_attributes > 0)
1503 {
1504 Declaration decl;
1505 DWARFExpression location;
1506 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001507 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 bool is_virtual = false;
1509 bool is_base_of_class = true;
1510 off_t member_offset = 0;
1511 uint32_t i;
1512 for (i=0; i<num_attributes; ++i)
1513 {
1514 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1515 DWARFFormValue form_value;
1516 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1517 {
1518 switch (attr)
1519 {
1520 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1521 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1522 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1523 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1524 case DW_AT_data_member_location:
1525 if (form_value.BlockData())
1526 {
1527 Value initialValue(0);
1528 Value memberOffset(0);
1529 const DataExtractor& debug_info_data = get_debug_info_data();
1530 uint32_t block_length = form_value.Unsigned();
1531 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001532 if (DWARFExpression::Evaluate (NULL,
1533 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001534 NULL,
1535 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001536 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001537 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001538 block_offset,
1539 block_length,
1540 eRegisterKindDWARF,
1541 &initialValue,
1542 memberOffset,
1543 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001544 {
1545 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1546 }
1547 }
1548 break;
1549
1550 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001551 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001552 break;
1553
1554 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1555 default:
1556 case DW_AT_sibling:
1557 break;
1558 }
1559 }
1560 }
1561
Greg Clayton526e5af2010-11-13 03:52:47 +00001562 Type *base_class_type = ResolveTypeUID(encoding_uid);
1563 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001564
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001565 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1566 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001567 if (class_language == eLanguageTypeObjC)
1568 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001569 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001570 }
1571 else
1572 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001573 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001574 accessibility,
1575 is_virtual,
1576 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001577 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001578 }
1579 }
1580 break;
1581
1582 default:
1583 break;
1584 }
1585 }
1586 return count;
1587}
1588
1589
1590clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001591SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592{
1593 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001594 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001595 {
1596 DWARFCompileUnitSP cu_sp;
1597 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1598 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001599 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001600 }
1601 return NULL;
1602}
1603
1604clang::DeclContext*
1605SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1606{
Greg Clayton81c22f62011-10-19 18:09:39 +00001607 if (UserIDMatches(type_uid))
1608 return GetClangDeclContextForDIEOffset (sc, type_uid);
1609 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001610}
1611
1612Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001613SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001614{
Greg Clayton81c22f62011-10-19 18:09:39 +00001615 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001616 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001617 DWARFDebugInfo* debug_info = DebugInfo();
1618 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001619 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001620 DWARFCompileUnitSP cu_sp;
1621 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001622 const bool assert_not_being_parsed = true;
1623 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001624 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001625 }
1626 return NULL;
1627}
1628
Greg Claytoncab36a32011-12-08 05:16:30 +00001629Type*
1630SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
1631{
1632 if (die != NULL)
1633 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001634 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1635 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001636 GetObjectFile()->GetModule()->LogMessage (log.get(),
1637 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1638 die->GetOffset(),
1639 DW_TAG_value_to_name(die->Tag()),
1640 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001641
Greg Claytoncab36a32011-12-08 05:16:30 +00001642 // We might be coming in in the middle of a type tree (a class
1643 // withing a class, an enum within a class), so parse any needed
1644 // parent DIEs before we get to this one...
1645 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1646 switch (decl_ctx_die->Tag())
1647 {
1648 case DW_TAG_structure_type:
1649 case DW_TAG_union_type:
1650 case DW_TAG_class_type:
1651 {
1652 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001653 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001654 GetObjectFile()->GetModule()->LogMessage (log.get(),
1655 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1656 die->GetOffset(),
1657 DW_TAG_value_to_name(die->Tag()),
1658 die->GetName(this, cu),
1659 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001660
Greg Claytoncab36a32011-12-08 05:16:30 +00001661 Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
Greg Clayton3bffb082011-12-10 02:15:28 +00001662 if (DW_TAG_is_function_tag(die->Tag()))
1663 {
1664 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001665 GetObjectFile()->GetModule()->LogMessage (log.get(),
1666 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
1667 die->GetOffset(),
1668 DW_TAG_value_to_name(die->Tag()),
1669 die->GetName(this, cu),
1670 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001671 // Ask the type to complete itself if it already hasn't since if we
1672 // want a function (method or static) from a class, the class must
1673 // create itself and add it's own methods and class functions.
1674 if (parent_type)
1675 parent_type->GetClangFullType();
1676 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001677 }
1678 break;
1679
1680 default:
1681 break;
1682 }
1683 return ResolveType (cu, die);
1684 }
1685 return NULL;
1686}
1687
Greg Clayton6beaaa62011-01-17 03:46:26 +00001688// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1689// SymbolFileDWARF objects to detect if this DWARF file is the one that
1690// can resolve a clang_type.
1691bool
1692SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1693{
1694 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1695 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1696 return die != NULL;
1697}
1698
1699
Greg Clayton1be10fc2010-09-29 01:12:09 +00001700lldb::clang_type_t
1701SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1702{
1703 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001704 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1705 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001706 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001707 {
1708 // We have already resolved this type...
1709 return clang_type;
1710 }
1711 // Once we start resolving this type, remove it from the forward declaration
1712 // map in case anyone child members or other types require this type to get resolved.
1713 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1714 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001715 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001716
Greg Clayton1be10fc2010-09-29 01:12:09 +00001717
Greg Clayton85ae2e12011-10-18 23:36:41 +00001718 // Disable external storage for this type so we don't get anymore
1719 // clang::ExternalASTSource queries for this type.
1720 ClangASTContext::SetHasExternalStorage (clang_type, false);
1721
Greg Clayton450e3f32010-10-12 02:24:53 +00001722 DWARFDebugInfo* debug_info = DebugInfo();
1723
Greg Clayton96d7d742010-11-10 23:42:09 +00001724 DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001725 Type *type = m_die_to_type.lookup (die);
1726
1727 const dw_tag_t tag = die->Tag();
1728
Greg Clayton3bffb082011-12-10 02:15:28 +00001729 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1730 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001731 GetObjectFile()->GetModule()->LogMessage (log.get(),
1732 "0x%8.8llx: %s '%s' resolving forward declaration...\n",
1733 MakeUserID(die->GetOffset()),
1734 DW_TAG_value_to_name(tag),
1735 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001736 assert (clang_type);
1737 DWARFDebugInfoEntry::Attributes attributes;
1738
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001739 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001740
1741 switch (tag)
1742 {
1743 case DW_TAG_structure_type:
1744 case DW_TAG_union_type:
1745 case DW_TAG_class_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001746 ast.StartTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00001747 if (die->HasChildren())
1748 {
1749 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton450e3f32010-10-12 02:24:53 +00001750 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
1751 if (is_objc_class)
Greg Claytonc93237c2010-10-01 20:48:32 +00001752 class_language = eLanguageTypeObjC;
1753
1754 int tag_decl_kind = -1;
1755 AccessType default_accessibility = eAccessNone;
1756 if (tag == DW_TAG_structure_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00001757 {
Greg Claytonc93237c2010-10-01 20:48:32 +00001758 tag_decl_kind = clang::TTK_Struct;
1759 default_accessibility = eAccessPublic;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001760 }
Greg Claytonc93237c2010-10-01 20:48:32 +00001761 else if (tag == DW_TAG_union_type)
1762 {
1763 tag_decl_kind = clang::TTK_Union;
1764 default_accessibility = eAccessPublic;
1765 }
1766 else if (tag == DW_TAG_class_type)
1767 {
1768 tag_decl_kind = clang::TTK_Class;
1769 default_accessibility = eAccessPrivate;
1770 }
1771
Greg Clayton96d7d742010-11-10 23:42:09 +00001772 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
Greg Claytonc93237c2010-10-01 20:48:32 +00001773 std::vector<clang::CXXBaseSpecifier *> base_classes;
1774 std::vector<int> member_accessibilities;
1775 bool is_a_class = false;
1776 // Parse members and base classes first
1777 DWARFDIECollection member_function_dies;
1778
1779 ParseChildMembers (sc,
Greg Clayton96d7d742010-11-10 23:42:09 +00001780 curr_cu,
Greg Claytonc93237c2010-10-01 20:48:32 +00001781 die,
1782 clang_type,
1783 class_language,
1784 base_classes,
1785 member_accessibilities,
1786 member_function_dies,
1787 default_accessibility,
1788 is_a_class);
1789
1790 // Now parse any methods if there were any...
1791 size_t num_functions = member_function_dies.Size();
1792 if (num_functions > 0)
1793 {
1794 for (size_t i=0; i<num_functions; ++i)
1795 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001796 ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytonc93237c2010-10-01 20:48:32 +00001797 }
1798 }
1799
Greg Clayton450e3f32010-10-12 02:24:53 +00001800 if (class_language == eLanguageTypeObjC)
1801 {
Greg Claytone3055942011-06-30 02:28:26 +00001802 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(clang_type));
Greg Clayton450e3f32010-10-12 02:24:53 +00001803 if (!class_str.empty())
1804 {
1805
Greg Claytond4a2b372011-09-12 23:21:58 +00001806 DIEArray method_die_offsets;
Greg Clayton5009f9d2011-10-27 17:55:14 +00001807 if (m_using_apple_tables)
1808 {
1809 if (m_apple_objc_ap.get())
1810 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
1811 }
1812 else
1813 {
1814 if (!m_indexed)
1815 Index ();
1816
1817 ConstString class_name (class_str.c_str());
1818 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
1819 }
1820
1821 if (!method_die_offsets.empty())
Greg Clayton450e3f32010-10-12 02:24:53 +00001822 {
Greg Claytond4a2b372011-09-12 23:21:58 +00001823 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton450e3f32010-10-12 02:24:53 +00001824
Greg Claytond4a2b372011-09-12 23:21:58 +00001825 DWARFCompileUnit* method_cu = NULL;
1826 const size_t num_matches = method_die_offsets.size();
1827 for (size_t i=0; i<num_matches; ++i)
1828 {
1829 const dw_offset_t die_offset = method_die_offsets[i];
1830 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
Greg Clayton450e3f32010-10-12 02:24:53 +00001831
Greg Clayton95d87902011-11-11 03:16:25 +00001832 if (method_die)
1833 ResolveType (method_cu, method_die);
1834 else
1835 {
1836 if (m_using_apple_tables)
1837 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001838 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
1839 die_offset, class_str.c_str());
Greg Clayton95d87902011-11-11 03:16:25 +00001840 }
1841 }
Greg Clayton450e3f32010-10-12 02:24:53 +00001842 }
1843 }
1844 }
1845 }
1846
Greg Claytonc93237c2010-10-01 20:48:32 +00001847 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
1848 // need to tell the clang type it is actually a class.
1849 if (class_language != eLanguageTypeObjC)
1850 {
1851 if (is_a_class && tag_decl_kind != clang::TTK_Class)
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001852 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
Greg Claytonc93237c2010-10-01 20:48:32 +00001853 }
1854
1855 // Since DW_TAG_structure_type gets used for both classes
1856 // and structures, we may need to set any DW_TAG_member
1857 // fields to have a "private" access if none was specified.
1858 // When we parsed the child members we tracked that actual
1859 // accessibility value for each DW_TAG_member in the
1860 // "member_accessibilities" array. If the value for the
1861 // member is zero, then it was set to the "default_accessibility"
1862 // which for structs was "public". Below we correct this
1863 // by setting any fields to "private" that weren't correctly
1864 // set.
1865 if (is_a_class && !member_accessibilities.empty())
1866 {
1867 // This is a class and all members that didn't have
1868 // their access specified are private.
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001869 ast.SetDefaultAccessForRecordFields (clang_type,
1870 eAccessPrivate,
1871 &member_accessibilities.front(),
1872 member_accessibilities.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001873 }
1874
1875 if (!base_classes.empty())
1876 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001877 ast.SetBaseClassesForClassType (clang_type,
1878 &base_classes.front(),
1879 base_classes.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001880
1881 // Clang will copy each CXXBaseSpecifier in "base_classes"
1882 // so we have to free them all.
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001883 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
1884 base_classes.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001885 }
1886
1887 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001888 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00001889 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001890
1891 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001892 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001893 if (die->HasChildren())
1894 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001895 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1896 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001897 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001898 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001899 return clang_type;
1900
1901 default:
1902 assert(false && "not a forward clang type decl!");
1903 break;
1904 }
1905 return NULL;
1906}
1907
Greg Claytonc685f8e2010-09-15 04:15:46 +00001908Type*
Greg Clayton96d7d742010-11-10 23:42:09 +00001909SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00001910{
1911 if (type_die != NULL)
1912 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00001913 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00001914
Greg Claytonc685f8e2010-09-15 04:15:46 +00001915 if (type == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001916 type = GetTypeForDIE (curr_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00001917
Greg Clayton24739922010-10-13 03:15:28 +00001918 if (assert_not_being_parsed)
1919 assert (type != DIE_IS_BEING_PARSED);
Greg Clayton594e5ed2010-09-27 21:07:38 +00001920 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001921 }
1922 return NULL;
1923}
1924
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001925CompileUnit*
Greg Clayton96d7d742010-11-10 23:42:09 +00001926SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001927{
1928 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00001929 if (curr_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001930 {
1931 // The symbol vendor doesn't know about this compile unit, we
1932 // need to parse and add it to the symbol vendor object.
1933 CompUnitSP dc_cu;
Greg Clayton96d7d742010-11-10 23:42:09 +00001934 ParseCompileUnit(curr_cu, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001935 if (dc_cu.get())
1936 {
1937 // Figure out the compile unit index if we weren't given one
Greg Clayton016a95e2010-09-14 02:20:48 +00001938 if (cu_idx == UINT32_MAX)
Greg Clayton96d7d742010-11-10 23:42:09 +00001939 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001940
1941 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx);
Greg Clayton450e3f32010-10-12 02:24:53 +00001942
1943 if (m_debug_map_symfile)
1944 m_debug_map_symfile->SetCompileUnit(this, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001945 }
1946 }
Greg Clayton96d7d742010-11-10 23:42:09 +00001947 return (CompileUnit*)curr_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001948}
1949
1950bool
Greg Clayton96d7d742010-11-10 23:42:09 +00001951SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001952{
1953 sc.Clear();
1954 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00001955 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001956
Greg Clayton81c22f62011-10-19 18:09:39 +00001957 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001958 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001959 sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00001960
1961 if (sc.function)
1962 {
1963 sc.module_sp = sc.function->CalculateSymbolContextModule();
1964 return true;
1965 }
1966
1967 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001968}
1969
1970uint32_t
1971SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1972{
1973 Timer scoped_timer(__PRETTY_FUNCTION__,
1974 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
1975 so_addr.GetSection(),
1976 so_addr.GetOffset(),
1977 resolve_scope);
1978 uint32_t resolved = 0;
1979 if (resolve_scope & ( eSymbolContextCompUnit |
1980 eSymbolContextFunction |
1981 eSymbolContextBlock |
1982 eSymbolContextLineEntry))
1983 {
1984 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1985
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00001987 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001988 {
Greg Claytond4a2b372011-09-12 23:21:58 +00001989 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990 if (cu_offset != DW_INVALID_OFFSET)
1991 {
1992 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00001993 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
1994 if (curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001995 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001996 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001997 assert(sc.comp_unit != NULL);
1998 resolved |= eSymbolContextCompUnit;
1999
2000 if (resolve_scope & eSymbolContextLineEntry)
2001 {
2002 LineTable *line_table = sc.comp_unit->GetLineTable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002003 if (line_table != NULL)
2004 {
2005 if (so_addr.IsLinkedAddress())
2006 {
2007 Address linked_addr (so_addr);
2008 linked_addr.ResolveLinkedAddress();
2009 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2010 {
2011 resolved |= eSymbolContextLineEntry;
2012 }
2013 }
2014 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
2015 {
2016 resolved |= eSymbolContextLineEntry;
2017 }
2018 }
2019 }
2020
2021 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2022 {
2023 DWARFDebugInfoEntry *function_die = NULL;
2024 DWARFDebugInfoEntry *block_die = NULL;
2025 if (resolve_scope & eSymbolContextBlock)
2026 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002027 curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028 }
2029 else
2030 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002031 curr_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002032 }
2033
2034 if (function_die != NULL)
2035 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002036 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002037 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002038 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002039 }
2040
2041 if (sc.function != NULL)
2042 {
2043 resolved |= eSymbolContextFunction;
2044
2045 if (resolve_scope & eSymbolContextBlock)
2046 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002047 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002048
2049 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002050 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002051 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002052 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002053 if (sc.block)
2054 resolved |= eSymbolContextBlock;
2055 }
2056 }
2057 }
2058 }
2059 }
2060 }
2061 }
2062 return resolved;
2063}
2064
2065
2066
2067uint32_t
2068SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2069{
2070 const uint32_t prev_size = sc_list.GetSize();
2071 if (resolve_scope & eSymbolContextCompUnit)
2072 {
2073 DWARFDebugInfo* debug_info = DebugInfo();
2074 if (debug_info)
2075 {
2076 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00002077 DWARFCompileUnit* curr_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002078
Greg Clayton96d7d742010-11-10 23:42:09 +00002079 for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002080 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002081 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002082 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
2083 if (check_inlines || file_spec_matches_cu_file_spec)
2084 {
2085 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton96d7d742010-11-10 23:42:09 +00002086 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002087 assert(sc.comp_unit != NULL);
2088
2089 uint32_t file_idx = UINT32_MAX;
2090
2091 // If we are looking for inline functions only and we don't
2092 // find it in the support files, we are done.
2093 if (check_inlines)
2094 {
Jim Ingham87df91b2011-09-23 00:54:11 +00002095 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002096 if (file_idx == UINT32_MAX)
2097 continue;
2098 }
2099
2100 if (line != 0)
2101 {
2102 LineTable *line_table = sc.comp_unit->GetLineTable();
2103
2104 if (line_table != NULL && line != 0)
2105 {
2106 // We will have already looked up the file index if
2107 // we are searching for inline entries.
2108 if (!check_inlines)
Jim Ingham87df91b2011-09-23 00:54:11 +00002109 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002110
2111 if (file_idx != UINT32_MAX)
2112 {
2113 uint32_t found_line;
2114 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2115 found_line = sc.line_entry.line;
2116
Greg Clayton016a95e2010-09-14 02:20:48 +00002117 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002118 {
2119 sc.function = NULL;
2120 sc.block = NULL;
2121 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2122 {
2123 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2124 if (file_vm_addr != LLDB_INVALID_ADDRESS)
2125 {
2126 DWARFDebugInfoEntry *function_die = NULL;
2127 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton96d7d742010-11-10 23:42:09 +00002128 curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002129
2130 if (function_die != NULL)
2131 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002132 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002133 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002134 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002135 }
2136
2137 if (sc.function != NULL)
2138 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002139 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002140
2141 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002142 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002143 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002144 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002145 }
2146 }
2147 }
2148
2149 sc_list.Append(sc);
2150 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2151 }
2152 }
2153 }
2154 else if (file_spec_matches_cu_file_spec && !check_inlines)
2155 {
2156 // only append the context if we aren't looking for inline call sites
2157 // by file and line and if the file spec matches that of the compile unit
2158 sc_list.Append(sc);
2159 }
2160 }
2161 else if (file_spec_matches_cu_file_spec && !check_inlines)
2162 {
2163 // only append the context if we aren't looking for inline call sites
2164 // by file and line and if the file spec matches that of the compile unit
2165 sc_list.Append(sc);
2166 }
2167
2168 if (!check_inlines)
2169 break;
2170 }
2171 }
2172 }
2173 }
2174 return sc_list.GetSize() - prev_size;
2175}
2176
2177void
2178SymbolFileDWARF::Index ()
2179{
2180 if (m_indexed)
2181 return;
2182 m_indexed = true;
2183 Timer scoped_timer (__PRETTY_FUNCTION__,
2184 "SymbolFileDWARF::Index (%s)",
2185 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2186
2187 DWARFDebugInfo* debug_info = DebugInfo();
2188 if (debug_info)
2189 {
2190 uint32_t cu_idx = 0;
2191 const uint32_t num_compile_units = GetNumCompileUnits();
2192 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2193 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002194 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002195
Greg Clayton96d7d742010-11-10 23:42:09 +00002196 bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002197
Greg Clayton96d7d742010-11-10 23:42:09 +00002198 curr_cu->Index (cu_idx,
Greg Clayton83c5cd92010-11-14 22:13:40 +00002199 m_function_basename_index,
2200 m_function_fullname_index,
2201 m_function_method_index,
2202 m_function_selector_index,
2203 m_objc_class_selectors_index,
2204 m_global_index,
2205 m_type_index,
Greg Claytond4a2b372011-09-12 23:21:58 +00002206 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002207
2208 // Keep memory down by clearing DIEs if this generate function
2209 // caused them to be parsed
2210 if (clear_dies)
Greg Clayton96d7d742010-11-10 23:42:09 +00002211 curr_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002212 }
2213
Greg Claytond4a2b372011-09-12 23:21:58 +00002214 m_function_basename_index.Finalize();
2215 m_function_fullname_index.Finalize();
2216 m_function_method_index.Finalize();
2217 m_function_selector_index.Finalize();
2218 m_objc_class_selectors_index.Finalize();
2219 m_global_index.Finalize();
2220 m_type_index.Finalize();
2221 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002222
Greg Clayton24739922010-10-13 03:15:28 +00002223#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002224 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002225 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002226 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2227 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002228 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2229 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2230 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2231 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2232 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2233 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002234 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002235 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002236#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002237 }
2238}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002239
2240bool
2241SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2242{
2243 if (namespace_decl == NULL)
2244 {
2245 // Invalid namespace decl which means we aren't matching only things
2246 // in this symbol file, so return true to indicate it matches this
2247 // symbol file.
2248 return true;
2249 }
2250
2251 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2252
2253 if (namespace_ast == NULL)
2254 return true; // No AST in the "namespace_decl", return true since it
2255 // could then match any symbol file, including this one
2256
2257 if (namespace_ast == GetClangASTContext().getASTContext())
2258 return true; // The ASTs match, return true
2259
2260 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002261 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2262
2263 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002264 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002265
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002266 return false;
2267}
2268
Greg Clayton2506a7a2011-10-12 23:34:26 +00002269bool
2270SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2271 DWARFCompileUnit* cu,
2272 const DWARFDebugInfoEntry* die)
2273{
2274 // No namespace specified, so the answesr i
2275 if (namespace_decl == NULL)
2276 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002277
2278 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002279
Greg Clayton2506a7a2011-10-12 23:34:26 +00002280 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2281 if (decl_ctx_die)
2282 {
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002283
Greg Clayton2506a7a2011-10-12 23:34:26 +00002284 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
2285 if (clang_namespace_decl)
2286 {
2287 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002288 {
2289 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002290 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002291 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002292 }
2293
Greg Clayton2506a7a2011-10-12 23:34:26 +00002294 DeclContextToDIEMap::iterator pos = m_decl_ctx_to_die.find(clang_namespace_decl);
2295
2296 if (pos == m_decl_ctx_to_die.end())
Sean Callananebe60672011-10-13 21:50:33 +00002297 {
2298 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002299 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 +00002300
Greg Clayton2506a7a2011-10-12 23:34:26 +00002301 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002302 }
Greg Clayton2506a7a2011-10-12 23:34:26 +00002303
Greg Claytoncb5860a2011-10-13 23:49:28 +00002304 return pos->second.count (decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002305 }
2306 else
2307 {
2308 // We have a namespace_decl that was not NULL but it contained
2309 // a NULL "clang::NamespaceDecl", so this means the global namespace
2310 // So as long the the contained decl context DIE isn't a namespace
2311 // we should be ok.
2312 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2313 return true;
2314 }
2315 }
Sean Callananebe60672011-10-13 21:50:33 +00002316
2317 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002318 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002319
Greg Clayton2506a7a2011-10-12 23:34:26 +00002320 return false;
2321}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002322uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002323SymbolFileDWARF::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 +00002324{
Greg Clayton21f2a492011-10-06 00:09:08 +00002325 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2326
2327 if (log)
2328 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002329 GetObjectFile()->GetModule()->LogMessage (log.get(),
2330 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2331 name.GetCString(),
2332 namespace_decl,
2333 append,
2334 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002335 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002336
2337 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2338 return 0;
2339
Greg Claytonc685f8e2010-09-15 04:15:46 +00002340 DWARFDebugInfo* info = DebugInfo();
2341 if (info == NULL)
2342 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002343
2344 // If we aren't appending the results to this list, then clear the list
2345 if (!append)
2346 variables.Clear();
2347
2348 // Remember how many variables are in the list before we search in case
2349 // we are appending the results to a variable list.
2350 const uint32_t original_size = variables.GetSize();
2351
Greg Claytond4a2b372011-09-12 23:21:58 +00002352 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002353
Greg Clayton97fbc342011-10-20 22:30:33 +00002354 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002355 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002356 if (m_apple_names_ap.get())
2357 {
2358 const char *name_cstr = name.GetCString();
2359 const char *base_name_start;
2360 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002361
Greg Clayton97fbc342011-10-20 22:30:33 +00002362 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2363 base_name_start = name_cstr;
2364
2365 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2366 }
Greg Clayton7f995132011-10-04 22:41:51 +00002367 }
2368 else
2369 {
2370 // Index the DWARF if we haven't already
2371 if (!m_indexed)
2372 Index ();
2373
2374 m_global_index.Find (name, die_offsets);
2375 }
2376
2377 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002378 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002379 {
Greg Clayton7f995132011-10-04 22:41:51 +00002380 SymbolContext sc;
2381 sc.module_sp = m_obj_file->GetModule();
2382 assert (sc.module_sp);
2383
Greg Claytond4a2b372011-09-12 23:21:58 +00002384 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002385 DWARFCompileUnit* dwarf_cu = NULL;
2386 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002387 for (size_t i=0; i<num_matches; ++i)
2388 {
2389 const dw_offset_t die_offset = die_offsets[i];
2390 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002391
Greg Clayton95d87902011-11-11 03:16:25 +00002392 if (die)
2393 {
2394 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2395 assert(sc.comp_unit != NULL);
2396
2397 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2398 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002399
Greg Clayton95d87902011-11-11 03:16:25 +00002400 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002401
Greg Clayton95d87902011-11-11 03:16:25 +00002402 if (variables.GetSize() - original_size >= max_matches)
2403 break;
2404 }
2405 else
2406 {
2407 if (m_using_apple_tables)
2408 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002409 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2410 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002411 }
2412 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002413 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002414 }
2415
2416 // Return the number of variable that were appended to the list
2417 return variables.GetSize() - original_size;
2418}
2419
2420uint32_t
2421SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2422{
Greg Clayton21f2a492011-10-06 00:09:08 +00002423 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2424
2425 if (log)
2426 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002427 GetObjectFile()->GetModule()->LogMessage (log.get(),
2428 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2429 regex.GetText(),
2430 append,
2431 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002432 }
2433
Greg Claytonc685f8e2010-09-15 04:15:46 +00002434 DWARFDebugInfo* info = DebugInfo();
2435 if (info == NULL)
2436 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002437
2438 // If we aren't appending the results to this list, then clear the list
2439 if (!append)
2440 variables.Clear();
2441
2442 // Remember how many variables are in the list before we search in case
2443 // we are appending the results to a variable list.
2444 const uint32_t original_size = variables.GetSize();
2445
Greg Clayton7f995132011-10-04 22:41:51 +00002446 DIEArray die_offsets;
2447
Greg Clayton97fbc342011-10-20 22:30:33 +00002448 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002449 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002450 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002451 {
2452 DWARFMappedHash::DIEInfoArray hash_data_array;
2453 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2454 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2455 }
Greg Clayton7f995132011-10-04 22:41:51 +00002456 }
2457 else
2458 {
2459 // Index the DWARF if we haven't already
2460 if (!m_indexed)
2461 Index ();
2462
2463 m_global_index.Find (regex, die_offsets);
2464 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002465
Greg Claytonc685f8e2010-09-15 04:15:46 +00002466 SymbolContext sc;
Greg Claytona2eee182011-09-17 07:23:18 +00002467 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002468 assert (sc.module_sp);
2469
Greg Claytond4a2b372011-09-12 23:21:58 +00002470 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002471 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002472 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002473 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002474 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002475 DWARFDebugInfo* debug_info = DebugInfo();
2476 for (size_t i=0; i<num_matches; ++i)
2477 {
2478 const dw_offset_t die_offset = die_offsets[i];
2479 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002480
2481 if (die)
2482 {
2483 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002484
Greg Clayton95d87902011-11-11 03:16:25 +00002485 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002486
Greg Clayton95d87902011-11-11 03:16:25 +00002487 if (variables.GetSize() - original_size >= max_matches)
2488 break;
2489 }
2490 else
2491 {
2492 if (m_using_apple_tables)
2493 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002494 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2495 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002496 }
2497 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002498 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002499 }
2500
2501 // Return the number of variable that were appended to the list
2502 return variables.GetSize() - original_size;
2503}
2504
Greg Claytonaa044962011-10-13 00:59:38 +00002505
Jim Ingham4cda6e02011-10-07 22:23:45 +00002506bool
2507SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2508 DWARFCompileUnit *&dwarf_cu,
2509 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002510{
Greg Claytonaa044962011-10-13 00:59:38 +00002511 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2512 return ResolveFunction (dwarf_cu, die, sc_list);
2513}
2514
2515
2516bool
2517SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2518 const DWARFDebugInfoEntry *die,
2519 SymbolContextList& sc_list)
2520{
Greg Clayton9e315582011-09-02 04:03:59 +00002521 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002522
2523 if (die == NULL)
2524 return false;
2525
Jim Ingham4cda6e02011-10-07 22:23:45 +00002526 // If we were passed a die that is not a function, just return false...
2527 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2528 return false;
2529
2530 const DWARFDebugInfoEntry* inlined_die = NULL;
2531 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002532 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002533 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002534
Jim Ingham4cda6e02011-10-07 22:23:45 +00002535 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002536 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002537 if (die->Tag() == DW_TAG_subprogram)
2538 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002539 }
2540 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002541 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002542 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002543 {
2544 Address addr;
2545 // Parse all blocks if needed
2546 if (inlined_die)
2547 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002548 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00002549 assert (sc.block != NULL);
2550 if (sc.block->GetStartAddress (addr) == false)
2551 addr.Clear();
2552 }
2553 else
2554 {
2555 sc.block = NULL;
2556 addr = sc.function->GetAddressRange().GetBaseAddress();
2557 }
2558
2559 if (addr.IsValid())
2560 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002561 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002562 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002563 }
2564 }
2565
Greg Claytonaa044962011-10-13 00:59:38 +00002566 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002567}
2568
Greg Clayton7f995132011-10-04 22:41:51 +00002569void
2570SymbolFileDWARF::FindFunctions (const ConstString &name,
2571 const NameToDIE &name_to_die,
2572 SymbolContextList& sc_list)
2573{
Greg Claytond4a2b372011-09-12 23:21:58 +00002574 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002575 if (name_to_die.Find (name, die_offsets))
2576 {
2577 ParseFunctions (die_offsets, sc_list);
2578 }
2579}
2580
2581
2582void
2583SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2584 const NameToDIE &name_to_die,
2585 SymbolContextList& sc_list)
2586{
2587 DIEArray die_offsets;
2588 if (name_to_die.Find (regex, die_offsets))
2589 {
2590 ParseFunctions (die_offsets, sc_list);
2591 }
2592}
2593
2594
2595void
2596SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2597 const DWARFMappedHash::MemoryTable &memory_table,
2598 SymbolContextList& sc_list)
2599{
2600 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002601 DWARFMappedHash::DIEInfoArray hash_data_array;
2602 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002603 {
Greg Claytond1767f02011-12-08 02:13:16 +00002604 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002605 ParseFunctions (die_offsets, sc_list);
2606 }
2607}
2608
2609void
2610SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2611 SymbolContextList& sc_list)
2612{
2613 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002614 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002615 {
Greg Clayton7f995132011-10-04 22:41:51 +00002616 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00002617
2618 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002619 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002620 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002621 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00002622 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002623 }
2624 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002625}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002626
Jim Ingham4cda6e02011-10-07 22:23:45 +00002627bool
2628SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2629 const DWARFCompileUnit *dwarf_cu,
2630 uint32_t name_type_mask,
2631 const char *partial_name,
2632 const char *base_name_start,
2633 const char *base_name_end)
2634{
2635 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2636 if (name_type_mask == eFunctionNameTypeMethod
2637 || name_type_mask == eFunctionNameTypeBase)
2638 {
Greg Claytonf0705c82011-10-22 03:33:13 +00002639 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2640 if (!containing_decl_ctx)
2641 return false;
2642
2643 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
2644
2645 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2646 return false;
2647 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2648 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002649 }
2650
2651 // Now we need to check whether the name we got back for this type matches the extra specifications
2652 // that were in the name we're looking up:
2653 if (base_name_start != partial_name || *base_name_end != '\0')
2654 {
2655 // First see if the stuff to the left matches the full name. To do that let's see if
2656 // we can pull out the mips linkage name attribute:
2657
2658 Mangled best_name;
2659
2660 DWARFDebugInfoEntry::Attributes attributes;
2661 die->GetAttributes(this, dwarf_cu, NULL, attributes);
2662 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
2663 if (idx != UINT32_MAX)
2664 {
2665 DWARFFormValue form_value;
2666 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
2667 {
2668 const char *name = form_value.AsCString(&get_debug_str_data());
2669 best_name.SetValue (name, true);
2670 }
2671 }
2672 if (best_name)
2673 {
2674 const char *demangled = best_name.GetDemangledName().GetCString();
2675 if (demangled)
2676 {
2677 std::string name_no_parens(partial_name, base_name_end - partial_name);
2678 if (strstr (demangled, name_no_parens.c_str()) == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002679 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002680 }
2681 }
2682 }
2683
2684 return true;
2685}
Greg Claytonc685f8e2010-09-15 04:15:46 +00002686
Greg Clayton0c5cd902010-06-28 21:30:43 +00002687uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00002688SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00002689 const lldb_private::ClangNamespaceDecl *namespace_decl,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002690 uint32_t name_type_mask,
2691 bool append,
2692 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002693{
2694 Timer scoped_timer (__PRETTY_FUNCTION__,
2695 "SymbolFileDWARF::FindFunctions (name = '%s')",
2696 name.AsCString());
2697
Greg Clayton21f2a492011-10-06 00:09:08 +00002698 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2699
2700 if (log)
2701 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002702 GetObjectFile()->GetModule()->LogMessage (log.get(),
2703 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2704 name.GetCString(),
2705 name_type_mask,
2706 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002707 }
2708
Greg Clayton0c5cd902010-06-28 21:30:43 +00002709 // If we aren't appending the results to this list, then clear the list
2710 if (!append)
2711 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00002712
2713 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2714 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002715
2716 // If name is empty then we won't find anything.
2717 if (name.IsEmpty())
2718 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002719
2720 // Remember how many sc_list are in the list before we search in case
2721 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002722
Greg Clayton9e315582011-09-02 04:03:59 +00002723 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00002724
Jim Ingham4cda6e02011-10-07 22:23:45 +00002725 const char *name_cstr = name.GetCString();
2726 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
2727 const char *base_name_start = name_cstr;
2728 const char *base_name_end = name_cstr + strlen(name_cstr);
2729
2730 if (name_type_mask & eFunctionNameTypeAuto)
2731 {
2732 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
2733 effective_name_type_mask = eFunctionNameTypeFull;
2734 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
2735 effective_name_type_mask = eFunctionNameTypeFull;
2736 else
2737 {
2738 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2739 effective_name_type_mask |= eFunctionNameTypeSelector;
2740
2741 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2742 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
2743 }
2744 }
2745 else
2746 {
2747 effective_name_type_mask = name_type_mask;
2748 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
2749 {
2750 // If they've asked for a CPP method or function name and it can't be that, we don't
2751 // even need to search for CPP methods or names.
2752 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2753 {
2754 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
2755 if (effective_name_type_mask == eFunctionNameTypeNone)
2756 return 0;
2757 }
2758 }
2759
2760 if (effective_name_type_mask & eFunctionNameTypeSelector)
2761 {
2762 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2763 {
2764 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
2765 if (effective_name_type_mask == eFunctionNameTypeNone)
2766 return 0;
2767 }
2768 }
2769 }
2770
2771 DWARFDebugInfo* info = DebugInfo();
2772 if (info == NULL)
2773 return 0;
2774
Greg Claytonaa044962011-10-13 00:59:38 +00002775 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00002776 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00002777 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002778 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00002779 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002780
2781 DIEArray die_offsets;
2782
2783 uint32_t num_matches = 0;
2784
2785 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00002786 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002787 // If they asked for the full name, match what they typed. At some point we may
2788 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
2789 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00002790 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002791 for (uint32_t i = 0; i < num_matches; i++)
2792 {
Greg Clayton95d87902011-11-11 03:16:25 +00002793 const dw_offset_t die_offset = die_offsets[i];
2794 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00002795 if (die)
2796 {
Sean Callanan213fdb82011-10-13 01:49:10 +00002797 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2798 continue;
2799
Greg Claytonaa044962011-10-13 00:59:38 +00002800 ResolveFunction (dwarf_cu, die, sc_list);
2801 }
Greg Clayton95d87902011-11-11 03:16:25 +00002802 else
2803 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002804 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2805 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002806 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002807 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002808 }
2809 else
2810 {
2811 if (effective_name_type_mask & eFunctionNameTypeSelector)
2812 {
2813 if (namespace_decl && *namespace_decl)
2814 return 0; // no selectors in namespaces
2815
2816 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2817 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
2818 // and if it is an ObjC method name, we're good.
2819
2820 for (uint32_t i = 0; i < num_matches; i++)
2821 {
Greg Clayton95d87902011-11-11 03:16:25 +00002822 const dw_offset_t die_offset = die_offsets[i];
2823 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00002824 if (die)
2825 {
2826 const char *die_name = die->GetName(this, dwarf_cu);
2827 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
2828 ResolveFunction (dwarf_cu, die, sc_list);
2829 }
Greg Clayton95d87902011-11-11 03:16:25 +00002830 else
2831 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002832 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2833 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002834 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002835 }
2836 die_offsets.clear();
2837 }
2838
2839 if (effective_name_type_mask & eFunctionNameTypeMethod
2840 || effective_name_type_mask & eFunctionNameTypeBase)
2841 {
2842 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
2843 (namespace_decl && *namespace_decl))
2844 return 0; // no methods in namespaces
2845
2846 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
2847 // extract the base name, look that up, and if there is any other information in the name we were
2848 // passed in we have to post-filter based on that.
2849
2850 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
2851 std::string base_name(base_name_start, base_name_end - base_name_start);
2852 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
2853
2854 for (uint32_t i = 0; i < num_matches; i++)
2855 {
Greg Clayton95d87902011-11-11 03:16:25 +00002856 const dw_offset_t die_offset = die_offsets[i];
2857 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00002858 if (die)
2859 {
2860 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2861 continue;
2862
2863 if (!FunctionDieMatchesPartialName(die,
2864 dwarf_cu,
2865 effective_name_type_mask,
2866 name_cstr,
2867 base_name_start,
2868 base_name_end))
2869 continue;
2870
2871 // If we get to here, the die is good, and we should add it:
2872 ResolveFunction (dwarf_cu, die, sc_list);
2873 }
Greg Clayton95d87902011-11-11 03:16:25 +00002874 else
2875 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002876 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
2877 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00002878 }
Greg Clayton97fbc342011-10-20 22:30:33 +00002879 }
2880 die_offsets.clear();
2881 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002882 }
2883 }
Greg Clayton7f995132011-10-04 22:41:51 +00002884 }
2885 else
2886 {
2887
2888 // Index the DWARF if we haven't already
2889 if (!m_indexed)
2890 Index ();
2891
Greg Clayton7f995132011-10-04 22:41:51 +00002892 if (name_type_mask & eFunctionNameTypeFull)
2893 FindFunctions (name, m_function_fullname_index, sc_list);
2894
Jim Ingham4cda6e02011-10-07 22:23:45 +00002895 std::string base_name(base_name_start, base_name_end - base_name_start);
2896 ConstString base_name_const(base_name.c_str());
2897 DIEArray die_offsets;
2898 DWARFCompileUnit *dwarf_cu = NULL;
2899
2900 if (effective_name_type_mask & eFunctionNameTypeBase)
2901 {
2902 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00002903 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002904 {
Greg Claytonaa044962011-10-13 00:59:38 +00002905 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2906 if (die)
2907 {
Sean Callanan213fdb82011-10-13 01:49:10 +00002908 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2909 continue;
2910
Greg Claytonaa044962011-10-13 00:59:38 +00002911 if (!FunctionDieMatchesPartialName(die,
2912 dwarf_cu,
2913 effective_name_type_mask,
2914 name_cstr,
2915 base_name_start,
2916 base_name_end))
2917 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002918
Greg Claytonaa044962011-10-13 00:59:38 +00002919 // If we get to here, the die is good, and we should add it:
2920 ResolveFunction (dwarf_cu, die, sc_list);
2921 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002922 }
2923 die_offsets.clear();
2924 }
2925
Jim Inghamea8005a2011-10-11 01:18:11 +00002926 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002927 {
Sean Callanan213fdb82011-10-13 01:49:10 +00002928 if (namespace_decl && *namespace_decl)
2929 return 0; // no methods in namespaces
2930
Jim Ingham4cda6e02011-10-07 22:23:45 +00002931 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
2932 {
Greg Claytonaa044962011-10-13 00:59:38 +00002933 for (uint32_t i = 0; i < num_base; i++)
2934 {
2935 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2936 if (die)
2937 {
2938 if (!FunctionDieMatchesPartialName(die,
2939 dwarf_cu,
2940 effective_name_type_mask,
2941 name_cstr,
2942 base_name_start,
2943 base_name_end))
2944 continue;
2945
2946 // If we get to here, the die is good, and we should add it:
2947 ResolveFunction (dwarf_cu, die, sc_list);
2948 }
2949 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002950 }
2951 die_offsets.clear();
2952 }
Greg Clayton7f995132011-10-04 22:41:51 +00002953
Sean Callanan213fdb82011-10-13 01:49:10 +00002954 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002955 {
Greg Clayton7f995132011-10-04 22:41:51 +00002956 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002957 }
2958
Greg Clayton4d01ace2011-09-29 16:58:15 +00002959 }
2960
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002961 // Return the number of variable that were appended to the list
2962 return sc_list.GetSize() - original_size;
2963}
2964
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002965uint32_t
2966SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list)
2967{
2968 Timer scoped_timer (__PRETTY_FUNCTION__,
2969 "SymbolFileDWARF::FindFunctions (regex = '%s')",
2970 regex.GetText());
2971
Greg Clayton21f2a492011-10-06 00:09:08 +00002972 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2973
2974 if (log)
2975 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002976 GetObjectFile()->GetModule()->LogMessage (log.get(),
2977 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
2978 regex.GetText(),
2979 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002980 }
2981
2982
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002983 // If we aren't appending the results to this list, then clear the list
2984 if (!append)
2985 sc_list.Clear();
2986
2987 // Remember how many sc_list are in the list before we search in case
2988 // we are appending the results to a variable list.
2989 uint32_t original_size = sc_list.GetSize();
2990
Greg Clayton97fbc342011-10-20 22:30:33 +00002991 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002992 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002993 if (m_apple_names_ap.get())
2994 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00002995 }
2996 else
2997 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002998 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00002999 if (!m_indexed)
3000 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003001
Greg Clayton7f995132011-10-04 22:41:51 +00003002 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003003
Greg Clayton7f995132011-10-04 22:41:51 +00003004 FindFunctions (regex, m_function_fullname_index, sc_list);
3005 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003006
3007 // Return the number of variable that were appended to the list
3008 return sc_list.GetSize() - original_size;
3009}
Jim Ingham318c9f22011-08-26 19:44:13 +00003010
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003011uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003012SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3013 const ConstString &name,
3014 const lldb_private::ClangNamespaceDecl *namespace_decl,
3015 bool append,
3016 uint32_t max_matches,
3017 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003018{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003019 DWARFDebugInfo* info = DebugInfo();
3020 if (info == NULL)
3021 return 0;
3022
Greg Clayton21f2a492011-10-06 00:09:08 +00003023 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3024
3025 if (log)
3026 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003027 GetObjectFile()->GetModule()->LogMessage (log.get(),
3028 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", append=%u, max_matches=%u, type_list)",
3029 name.GetCString(),
3030 append,
3031 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003032 }
3033
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003034 // If we aren't appending the results to this list, then clear the list
3035 if (!append)
3036 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003037
3038 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3039 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003040
Greg Claytond4a2b372011-09-12 23:21:58 +00003041 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003042
Greg Clayton97fbc342011-10-20 22:30:33 +00003043 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003044 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003045 if (m_apple_types_ap.get())
3046 {
3047 const char *name_cstr = name.GetCString();
3048 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3049 }
Greg Clayton7f995132011-10-04 22:41:51 +00003050 }
3051 else
3052 {
3053 if (!m_indexed)
3054 Index ();
3055
3056 m_type_index.Find (name, die_offsets);
3057 }
3058
Greg Clayton7f995132011-10-04 22:41:51 +00003059 const size_t num_matches = die_offsets.size();
3060
Greg Claytond4a2b372011-09-12 23:21:58 +00003061 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003062 {
Greg Clayton7f995132011-10-04 22:41:51 +00003063 const uint32_t initial_types_size = types.GetSize();
3064 DWARFCompileUnit* dwarf_cu = NULL;
3065 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003066 DWARFDebugInfo* debug_info = DebugInfo();
3067 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003068 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003069 const dw_offset_t die_offset = die_offsets[i];
3070 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3071
Greg Clayton95d87902011-11-11 03:16:25 +00003072 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003073 {
Greg Clayton95d87902011-11-11 03:16:25 +00003074 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3075 continue;
3076
3077 Type *matching_type = ResolveType (dwarf_cu, die);
3078 if (matching_type)
3079 {
3080 // We found a type pointer, now find the shared pointer form our type list
3081 types.InsertUnique (TypeSP (matching_type));
3082 if (types.GetSize() >= max_matches)
3083 break;
3084 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003085 }
Greg Clayton95d87902011-11-11 03:16:25 +00003086 else
3087 {
3088 if (m_using_apple_tables)
3089 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003090 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3091 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003092 }
3093 }
3094
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003095 }
Greg Clayton7f995132011-10-04 22:41:51 +00003096 return types.GetSize() - initial_types_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003097 }
Greg Clayton7f995132011-10-04 22:41:51 +00003098 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003099}
3100
3101
Greg Clayton526e5af2010-11-13 03:52:47 +00003102ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003103SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003104 const ConstString &name,
3105 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003106{
Greg Clayton21f2a492011-10-06 00:09:08 +00003107 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3108
3109 if (log)
3110 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003111 GetObjectFile()->GetModule()->LogMessage (log.get(),
3112 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3113 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003114 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003115
3116 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3117 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003118
Greg Clayton526e5af2010-11-13 03:52:47 +00003119 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003120 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003121 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003122 {
Greg Clayton7f995132011-10-04 22:41:51 +00003123 DIEArray die_offsets;
3124
Greg Clayton526e5af2010-11-13 03:52:47 +00003125 // Index if we already haven't to make sure the compile units
3126 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003127 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003128 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003129 if (m_apple_namespaces_ap.get())
3130 {
3131 const char *name_cstr = name.GetCString();
3132 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3133 }
Greg Clayton7f995132011-10-04 22:41:51 +00003134 }
3135 else
3136 {
3137 if (!m_indexed)
3138 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003139
Greg Clayton7f995132011-10-04 22:41:51 +00003140 m_namespace_index.Find (name, die_offsets);
3141 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003142
3143 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003144 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003145 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003146 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003147 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003148 DWARFDebugInfo* debug_info = DebugInfo();
3149 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003150 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003151 const dw_offset_t die_offset = die_offsets[i];
3152 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003153
Greg Clayton95d87902011-11-11 03:16:25 +00003154 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003155 {
Greg Clayton95d87902011-11-11 03:16:25 +00003156 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3157 continue;
3158
3159 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3160 if (clang_namespace_decl)
3161 {
3162 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3163 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003164 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003165 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003166 }
Greg Clayton95d87902011-11-11 03:16:25 +00003167 else
3168 {
3169 if (m_using_apple_tables)
3170 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003171 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3172 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003173 }
3174 }
3175
Greg Clayton526e5af2010-11-13 03:52:47 +00003176 }
3177 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003178 }
Greg Clayton526e5af2010-11-13 03:52:47 +00003179 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003180}
3181
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003182uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003183SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003184{
3185 // Remember how many sc_list are in the list before we search in case
3186 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003187 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003188
3189 const uint32_t num_die_offsets = die_offsets.size();
3190 // Parse all of the types we found from the pubtypes matches
3191 uint32_t i;
3192 uint32_t num_matches = 0;
3193 for (i = 0; i < num_die_offsets; ++i)
3194 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003195 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3196 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003197 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003198 // We found a type pointer, now find the shared pointer form our type list
Greg Clayton85ae2e12011-10-18 23:36:41 +00003199 types.InsertUnique (TypeSP (matching_type));
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003200 ++num_matches;
3201 if (num_matches >= max_matches)
3202 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003203 }
3204 }
3205
3206 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003207 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003208}
3209
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003210
3211size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003212SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3213 clang::DeclContext *containing_decl_ctx,
3214 TypeSP& type_sp,
3215 DWARFCompileUnit* dwarf_cu,
3216 const DWARFDebugInfoEntry *parent_die,
3217 bool skip_artificial,
3218 bool &is_static,
3219 TypeList* type_list,
3220 std::vector<clang_type_t>& function_param_types,
3221 std::vector<clang::ParmVarDecl*>& function_param_decls,
3222 unsigned &type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003223{
3224 if (parent_die == NULL)
3225 return 0;
3226
Greg Claytond88d7592010-09-15 08:33:30 +00003227 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3228
Greg Clayton7fedea22010-11-16 02:10:54 +00003229 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003230 const DWARFDebugInfoEntry *die;
3231 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3232 {
3233 dw_tag_t tag = die->Tag();
3234 switch (tag)
3235 {
3236 case DW_TAG_formal_parameter:
3237 {
3238 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003239 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003240 if (num_attributes > 0)
3241 {
3242 const char *name = NULL;
3243 Declaration decl;
3244 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003245 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003246 // one of None, Auto, Register, Extern, Static, PrivateExtern
3247
Sean Callanane2ef6e32010-09-23 03:01:22 +00003248 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003249 uint32_t i;
3250 for (i=0; i<num_attributes; ++i)
3251 {
3252 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3253 DWARFFormValue form_value;
3254 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3255 {
3256 switch (attr)
3257 {
3258 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3259 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3260 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3261 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3262 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003263 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003264 case DW_AT_location:
3265 // if (form_value.BlockData())
3266 // {
3267 // const DataExtractor& debug_info_data = debug_info();
3268 // uint32_t block_length = form_value.Unsigned();
3269 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3270 // }
3271 // else
3272 // {
3273 // }
3274 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003275 case DW_AT_const_value:
3276 case DW_AT_default_value:
3277 case DW_AT_description:
3278 case DW_AT_endianity:
3279 case DW_AT_is_optional:
3280 case DW_AT_segment:
3281 case DW_AT_variable_parameter:
3282 default:
3283 case DW_AT_abstract_origin:
3284 case DW_AT_sibling:
3285 break;
3286 }
3287 }
3288 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003289
Greg Clayton0fffff52010-09-24 05:15:53 +00003290 bool skip = false;
3291 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003292 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003293 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003294 {
3295 // In order to determine if a C++ member function is
3296 // "const" we have to look at the const-ness of "this"...
3297 // Ugly, but that
3298 if (arg_idx == 0)
3299 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003300 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003301 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003302 // Often times compilers omit the "this" name for the
3303 // specification DIEs, so we can't rely upon the name
3304 // being in the formal parameter DIE...
3305 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003306 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003307 Type *this_type = ResolveTypeUID (param_type_die_offset);
3308 if (this_type)
3309 {
3310 uint32_t encoding_mask = this_type->GetEncodingMask();
3311 if (encoding_mask & Type::eEncodingIsPointerUID)
3312 {
3313 is_static = false;
3314
3315 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3316 type_quals |= clang::Qualifiers::Const;
3317 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3318 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003319 }
3320 }
3321 }
3322 }
3323 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003324 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003325 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003326 else
3327 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003328
Greg Clayton0fffff52010-09-24 05:15:53 +00003329 // HACK: Objective C formal parameters "self" and "_cmd"
3330 // are not marked as artificial in the DWARF...
Greg Clayton96d7d742010-11-10 23:42:09 +00003331 CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3332 if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Clayton0fffff52010-09-24 05:15:53 +00003333 {
3334 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3335 skip = true;
3336 }
3337 }
3338 }
3339
3340 if (!skip)
3341 {
3342 Type *type = ResolveTypeUID(param_type_die_offset);
3343 if (type)
3344 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003345 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003346
Greg Clayton42ce2f32011-12-12 21:50:19 +00003347 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3348 type->GetClangForwardType(),
3349 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003350 assert(param_var_decl);
3351 function_param_decls.push_back(param_var_decl);
3352 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003353 }
3354 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003355 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003356 }
3357 break;
3358
3359 default:
3360 break;
3361 }
3362 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003363 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003364}
3365
3366size_t
3367SymbolFileDWARF::ParseChildEnumerators
3368(
3369 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003370 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003371 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003372 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003373 const DWARFDebugInfoEntry *parent_die
3374)
3375{
3376 if (parent_die == NULL)
3377 return 0;
3378
3379 size_t enumerators_added = 0;
3380 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003381 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3382
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003383 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3384 {
3385 const dw_tag_t tag = die->Tag();
3386 if (tag == DW_TAG_enumerator)
3387 {
3388 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003389 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003390 if (num_child_attributes > 0)
3391 {
3392 const char *name = NULL;
3393 bool got_value = false;
3394 int64_t enum_value = 0;
3395 Declaration decl;
3396
3397 uint32_t i;
3398 for (i=0; i<num_child_attributes; ++i)
3399 {
3400 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3401 DWARFFormValue form_value;
3402 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3403 {
3404 switch (attr)
3405 {
3406 case DW_AT_const_value:
3407 got_value = true;
3408 enum_value = form_value.Unsigned();
3409 break;
3410
3411 case DW_AT_name:
3412 name = form_value.AsCString(&get_debug_str_data());
3413 break;
3414
3415 case DW_AT_description:
3416 default:
3417 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3418 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3419 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3420 case DW_AT_sibling:
3421 break;
3422 }
3423 }
3424 }
3425
3426 if (name && name[0] && got_value)
3427 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003428 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3429 enumerator_clang_type,
3430 decl,
3431 name,
3432 enum_value,
3433 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003434 ++enumerators_added;
3435 }
3436 }
3437 }
3438 }
3439 return enumerators_added;
3440}
3441
3442void
3443SymbolFileDWARF::ParseChildArrayInfo
3444(
3445 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003446 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003447 const DWARFDebugInfoEntry *parent_die,
3448 int64_t& first_index,
3449 std::vector<uint64_t>& element_orders,
3450 uint32_t& byte_stride,
3451 uint32_t& bit_stride
3452)
3453{
3454 if (parent_die == NULL)
3455 return;
3456
3457 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003458 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003459 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3460 {
3461 const dw_tag_t tag = die->Tag();
3462 switch (tag)
3463 {
3464 case DW_TAG_enumerator:
3465 {
3466 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003467 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003468 if (num_child_attributes > 0)
3469 {
3470 const char *name = NULL;
3471 bool got_value = false;
3472 int64_t enum_value = 0;
3473
3474 uint32_t i;
3475 for (i=0; i<num_child_attributes; ++i)
3476 {
3477 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3478 DWARFFormValue form_value;
3479 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3480 {
3481 switch (attr)
3482 {
3483 case DW_AT_const_value:
3484 got_value = true;
3485 enum_value = form_value.Unsigned();
3486 break;
3487
3488 case DW_AT_name:
3489 name = form_value.AsCString(&get_debug_str_data());
3490 break;
3491
3492 case DW_AT_description:
3493 default:
3494 case DW_AT_decl_file:
3495 case DW_AT_decl_line:
3496 case DW_AT_decl_column:
3497 case DW_AT_sibling:
3498 break;
3499 }
3500 }
3501 }
3502 }
3503 }
3504 break;
3505
3506 case DW_TAG_subrange_type:
3507 {
3508 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003509 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003510 if (num_child_attributes > 0)
3511 {
3512 const char *name = NULL;
3513 bool got_value = false;
3514 uint64_t byte_size = 0;
3515 int64_t enum_value = 0;
3516 uint64_t num_elements = 0;
3517 uint64_t lower_bound = 0;
3518 uint64_t upper_bound = 0;
3519 uint32_t i;
3520 for (i=0; i<num_child_attributes; ++i)
3521 {
3522 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3523 DWARFFormValue form_value;
3524 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3525 {
3526 switch (attr)
3527 {
3528 case DW_AT_const_value:
3529 got_value = true;
3530 enum_value = form_value.Unsigned();
3531 break;
3532
3533 case DW_AT_name:
3534 name = form_value.AsCString(&get_debug_str_data());
3535 break;
3536
3537 case DW_AT_count:
3538 num_elements = form_value.Unsigned();
3539 break;
3540
3541 case DW_AT_bit_stride:
3542 bit_stride = form_value.Unsigned();
3543 break;
3544
3545 case DW_AT_byte_stride:
3546 byte_stride = form_value.Unsigned();
3547 break;
3548
3549 case DW_AT_byte_size:
3550 byte_size = form_value.Unsigned();
3551 break;
3552
3553 case DW_AT_lower_bound:
3554 lower_bound = form_value.Unsigned();
3555 break;
3556
3557 case DW_AT_upper_bound:
3558 upper_bound = form_value.Unsigned();
3559 break;
3560
3561 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003562 case DW_AT_abstract_origin:
3563 case DW_AT_accessibility:
3564 case DW_AT_allocated:
3565 case DW_AT_associated:
3566 case DW_AT_data_location:
3567 case DW_AT_declaration:
3568 case DW_AT_description:
3569 case DW_AT_sibling:
3570 case DW_AT_threads_scaled:
3571 case DW_AT_type:
3572 case DW_AT_visibility:
3573 break;
3574 }
3575 }
3576 }
3577
3578 if (upper_bound > lower_bound)
3579 num_elements = upper_bound - lower_bound + 1;
3580
3581 if (num_elements > 0)
3582 element_orders.push_back (num_elements);
3583 }
3584 }
3585 break;
3586 }
3587 }
3588}
3589
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003590TypeSP
Greg Clayton96d7d742010-11-10 23:42:09 +00003591SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003592{
3593 TypeSP type_sp;
3594 if (die != NULL)
3595 {
Greg Clayton96d7d742010-11-10 23:42:09 +00003596 assert(curr_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00003597 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003598 if (type_ptr == NULL)
3599 {
Greg Claytonca512b32011-01-14 04:54:56 +00003600 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu);
3601 assert (lldb_cu);
3602 SymbolContext sc(lldb_cu);
Greg Clayton96d7d742010-11-10 23:42:09 +00003603 type_sp = ParseType(sc, curr_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003604 }
3605 else if (type_ptr != DIE_IS_BEING_PARSED)
3606 {
3607 // Grab the existing type from the master types lists
Greg Clayton85ae2e12011-10-18 23:36:41 +00003608 type_sp = type_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003609 }
3610
3611 }
3612 return type_sp;
3613}
3614
3615clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00003616SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003617{
3618 if (die_offset != DW_INVALID_OFFSET)
3619 {
3620 DWARFCompileUnitSP cu_sp;
3621 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00003622 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003623 }
3624 return NULL;
3625}
3626
Sean Callanan72e49402011-08-05 23:43:37 +00003627clang::DeclContext *
3628SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
3629{
3630 if (die_offset != DW_INVALID_OFFSET)
3631 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00003632 DWARFDebugInfo* debug_info = DebugInfo();
3633 if (debug_info)
3634 {
3635 DWARFCompileUnitSP cu_sp;
3636 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
3637 if (die)
3638 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
3639 }
Sean Callanan72e49402011-08-05 23:43:37 +00003640 }
3641 return NULL;
3642}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003643
Greg Clayton96d7d742010-11-10 23:42:09 +00003644clang::NamespaceDecl *
3645SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
3646{
Greg Clayton030a2042011-10-14 21:34:45 +00003647 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00003648 {
Greg Clayton030a2042011-10-14 21:34:45 +00003649 // See if we already parsed this namespace DIE and associated it with a
3650 // uniqued namespace declaration
3651 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
3652 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003653 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00003654 else
3655 {
3656 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00003657 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (curr_cu, die, NULL);
3658 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
3659 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3660 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00003661 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00003662 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00003663 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003664 GetObjectFile()->GetModule()->LogMessage (log.get(),
3665 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
3666 GetClangASTContext().getASTContext(),
3667 MakeUserID(die->GetOffset()),
3668 namespace_name,
3669 namespace_decl,
3670 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00003671 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00003672 else
3673 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003674 GetObjectFile()->GetModule()->LogMessage (log.get(),
3675 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
3676 GetClangASTContext().getASTContext(),
3677 MakeUserID(die->GetOffset()),
3678 namespace_decl,
3679 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00003680 }
Greg Clayton030a2042011-10-14 21:34:45 +00003681 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00003682
3683 if (namespace_decl)
3684 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
3685 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003686 }
3687 }
3688 return NULL;
3689}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003690
3691clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00003692SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00003693{
Greg Clayton5cf58b92011-10-05 22:22:08 +00003694 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3695 if (clang_decl_ctx)
3696 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00003697 // If this DIE has a specification, or an abstract origin, then trace to those.
3698
Greg Claytoncab36a32011-12-08 05:16:30 +00003699 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00003700 if (die_offset != DW_INVALID_OFFSET)
3701 return GetClangDeclContextForDIEOffset (sc, die_offset);
3702
Greg Claytoncab36a32011-12-08 05:16:30 +00003703 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00003704 if (die_offset != DW_INVALID_OFFSET)
3705 return GetClangDeclContextForDIEOffset (sc, die_offset);
3706
Greg Clayton3bffb082011-12-10 02:15:28 +00003707 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3708 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00003709 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 +00003710 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00003711 bool assert_not_being_parsed = true;
3712 ResolveTypeUID (cu, die, assert_not_being_parsed);
3713
Greg Clayton5cf58b92011-10-05 22:22:08 +00003714 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3715
3716 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00003717}
3718
3719clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00003720SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003721{
Greg Claytonca512b32011-01-14 04:54:56 +00003722 if (m_clang_tu_decl == NULL)
3723 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003724
Greg Clayton2bc22f82011-09-30 03:20:47 +00003725 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00003726
3727 if (decl_ctx_die_copy)
3728 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003729
3730 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003731 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00003732
Greg Clayton2bc22f82011-09-30 03:20:47 +00003733 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
3734 if (pos != m_die_to_decl_ctx.end())
3735 return pos->second;
3736
3737 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003738 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003739 case DW_TAG_compile_unit:
3740 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003741
Greg Clayton2bc22f82011-09-30 03:20:47 +00003742 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00003743 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00003744 break;
3745
3746 case DW_TAG_structure_type:
3747 case DW_TAG_union_type:
3748 case DW_TAG_class_type:
3749 {
3750 Type* type = ResolveType (cu, decl_ctx_die);
3751 if (type)
3752 {
3753 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
3754 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00003755 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003756 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
3757 if (decl_ctx)
3758 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00003759 }
3760 }
Greg Claytonca512b32011-01-14 04:54:56 +00003761 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00003762 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003763
Greg Clayton2bc22f82011-09-30 03:20:47 +00003764 default:
3765 break;
Greg Claytonca512b32011-01-14 04:54:56 +00003766 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003767 }
Greg Clayton7a345282010-11-09 23:46:37 +00003768 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003769}
3770
Greg Clayton2bc22f82011-09-30 03:20:47 +00003771
3772const DWARFDebugInfoEntry *
3773SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
3774{
3775 if (cu && die)
3776 {
3777 const DWARFDebugInfoEntry * const decl_die = die;
3778
3779 while (die != NULL)
3780 {
3781 // If this is the original DIE that we are searching for a declaration
3782 // for, then don't look in the cache as we don't want our own decl
3783 // context to be our decl context...
3784 if (decl_die != die)
3785 {
3786 switch (die->Tag())
3787 {
3788 case DW_TAG_compile_unit:
3789 case DW_TAG_namespace:
3790 case DW_TAG_structure_type:
3791 case DW_TAG_union_type:
3792 case DW_TAG_class_type:
3793 return die;
3794
3795 default:
3796 break;
3797 }
3798 }
3799
3800 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
3801 if (die_offset != DW_INVALID_OFFSET)
3802 {
3803 DWARFCompileUnit *spec_cu = cu;
3804 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
3805 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
3806 if (spec_die_decl_ctx_die)
3807 return spec_die_decl_ctx_die;
3808 }
3809
3810 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
3811 if (die_offset != DW_INVALID_OFFSET)
3812 {
3813 DWARFCompileUnit *abs_cu = cu;
3814 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
3815 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
3816 if (abs_die_decl_ctx_die)
3817 return abs_die_decl_ctx_die;
3818 }
3819
3820 die = die->GetParent();
3821 }
3822 }
3823 return NULL;
3824}
3825
3826
Greg Clayton901c5ca2011-12-03 04:40:03 +00003827Symbol *
3828SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
3829{
3830 Symbol *objc_class_symbol = NULL;
3831 if (m_obj_file)
3832 {
3833 Symtab *symtab = m_obj_file->GetSymtab();
3834 if (symtab)
3835 {
3836 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
3837 eSymbolTypeObjCClass,
3838 Symtab::eDebugNo,
3839 Symtab::eVisibilityAny);
3840 }
3841 }
3842 return objc_class_symbol;
3843}
3844
3845
3846// This function can be used when a DIE is found that is a forward declaration
3847// DIE and we want to try and find a type that has the complete definition.
3848TypeSP
3849SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (DWARFCompileUnit* cu,
3850 const DWARFDebugInfoEntry *die,
3851 const ConstString &type_name)
3852{
3853
3854 TypeSP type_sp;
3855
3856 if (cu == NULL || die == NULL || !type_name || !GetObjCClassSymbol (type_name))
3857 return type_sp;
3858
3859 DIEArray die_offsets;
3860
3861 if (m_using_apple_tables)
3862 {
3863 if (m_apple_types_ap.get())
3864 {
3865 const char *name_cstr = type_name.GetCString();
Greg Clayton220a0072011-12-09 08:48:30 +00003866 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets);
Greg Clayton901c5ca2011-12-03 04:40:03 +00003867 }
3868 }
3869 else
3870 {
3871 if (!m_indexed)
3872 Index ();
3873
3874 m_type_index.Find (type_name, die_offsets);
3875 }
3876
Greg Clayton901c5ca2011-12-03 04:40:03 +00003877 const size_t num_matches = die_offsets.size();
3878
3879 const dw_tag_t die_tag = die->Tag();
3880
3881 DWARFCompileUnit* type_cu = NULL;
3882 const DWARFDebugInfoEntry* type_die = NULL;
3883 if (num_matches)
3884 {
3885 DWARFDebugInfo* debug_info = DebugInfo();
3886 for (size_t i=0; i<num_matches; ++i)
3887 {
3888 const dw_offset_t die_offset = die_offsets[i];
3889 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
3890
3891 if (type_die)
3892 {
3893 bool try_resolving_type = false;
3894
3895 // Don't try and resolve the DIE we are looking for with the DIE itself!
3896 if (type_die != die)
3897 {
3898 const dw_tag_t type_die_tag = type_die->Tag();
3899 // Make sure the tags match
3900 if (type_die_tag == die_tag)
3901 {
3902 // The tags match, lets try resolving this type
3903 try_resolving_type = true;
3904 }
3905 else
3906 {
3907 // The tags don't match, but we need to watch our for a
3908 // forward declaration for a struct and ("struct foo")
3909 // ends up being a class ("class foo { ... };") or
3910 // vice versa.
3911 switch (type_die_tag)
3912 {
3913 case DW_TAG_class_type:
3914 // We had a "class foo", see if we ended up with a "struct foo { ... };"
3915 try_resolving_type = (die_tag == DW_TAG_structure_type);
3916 break;
3917 case DW_TAG_structure_type:
3918 // We had a "struct foo", see if we ended up with a "class foo { ... };"
3919 try_resolving_type = (die_tag == DW_TAG_class_type);
3920 break;
3921 default:
3922 // Tags don't match, don't event try to resolve
3923 // using this type whose name matches....
3924 break;
3925 }
3926 }
3927 }
3928
3929 if (try_resolving_type)
3930 {
3931 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
3932
3933 if (try_resolving_type)
3934 {
3935 Type *resolved_type = ResolveType (type_cu, type_die, false);
3936 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3937 {
3938 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
3939 MakeUserID(die->GetOffset()),
3940 MakeUserID(curr_cu->GetOffset()),
3941 m_obj_file->GetFileSpec().GetFilename().AsCString(),
3942 MakeUserID(type_die->GetOffset()),
3943 MakeUserID(type_cu->GetOffset()));
3944
3945 m_die_to_type[die] = resolved_type;
3946 type_sp = resolved_type;
3947 break;
3948 }
3949 }
3950 }
3951 }
3952 else
3953 {
3954 if (m_using_apple_tables)
3955 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003956 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3957 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00003958 }
3959 }
3960
3961 }
3962 }
3963 return type_sp;
3964}
3965
Greg Clayton220a0072011-12-09 08:48:30 +00003966
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003967// This function can be used when a DIE is found that is a forward declaration
3968// DIE and we want to try and find a type that has the complete definition.
3969TypeSP
Greg Clayton7f995132011-10-04 22:41:51 +00003970SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
3971 const DWARFDebugInfoEntry *die,
3972 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003973{
3974 TypeSP type_sp;
3975
Greg Clayton1a65ae12011-01-25 23:55:37 +00003976 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00003977 return type_sp;
3978
Greg Clayton7f995132011-10-04 22:41:51 +00003979 DIEArray die_offsets;
3980
Greg Clayton97fbc342011-10-20 22:30:33 +00003981 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003982 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003983 if (m_apple_types_ap.get())
3984 {
Greg Claytond1767f02011-12-08 02:13:16 +00003985 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
3986 {
Greg Claytonae920b62012-01-06 00:17:16 +00003987 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00003988 }
3989 else
3990 {
3991 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
3992 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003993 }
Greg Clayton7f995132011-10-04 22:41:51 +00003994 }
3995 else
3996 {
3997 if (!m_indexed)
3998 Index ();
3999
4000 m_type_index.Find (type_name, die_offsets);
4001 }
Greg Clayton7f995132011-10-04 22:41:51 +00004002
4003 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004004
Greg Clayton934cb052011-12-03 00:27:05 +00004005 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004006
4007 DWARFCompileUnit* type_cu = NULL;
4008 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004009 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004010 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004011 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004012 for (size_t i=0; i<num_matches; ++i)
4013 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004014 const dw_offset_t die_offset = die_offsets[i];
4015 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004016
Greg Clayton95d87902011-11-11 03:16:25 +00004017 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004018 {
Greg Clayton934cb052011-12-03 00:27:05 +00004019 bool try_resolving_type = false;
4020
4021 // Don't try and resolve the DIE we are looking for with the DIE itself!
4022 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004023 {
Greg Clayton934cb052011-12-03 00:27:05 +00004024 const dw_tag_t type_die_tag = type_die->Tag();
4025 // Make sure the tags match
4026 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004027 {
Greg Clayton934cb052011-12-03 00:27:05 +00004028 // The tags match, lets try resolving this type
4029 try_resolving_type = true;
4030 }
4031 else
4032 {
4033 // The tags don't match, but we need to watch our for a
4034 // forward declaration for a struct and ("struct foo")
4035 // ends up being a class ("class foo { ... };") or
4036 // vice versa.
4037 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004038 {
Greg Clayton934cb052011-12-03 00:27:05 +00004039 case DW_TAG_class_type:
4040 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4041 try_resolving_type = (die_tag == DW_TAG_structure_type);
4042 break;
4043 case DW_TAG_structure_type:
4044 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4045 try_resolving_type = (die_tag == DW_TAG_class_type);
4046 break;
4047 default:
4048 // Tags don't match, don't event try to resolve
4049 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004050 break;
4051 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004052 }
4053 }
Greg Clayton934cb052011-12-03 00:27:05 +00004054
4055 if (try_resolving_type)
4056 {
4057 Type *resolved_type = ResolveType (type_cu, type_die, false);
4058 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4059 {
4060 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4061 MakeUserID(die->GetOffset()),
4062 MakeUserID(curr_cu->GetOffset()),
4063 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4064 MakeUserID(type_die->GetOffset()),
4065 MakeUserID(type_cu->GetOffset()));
4066
4067 m_die_to_type[die] = resolved_type;
4068 type_sp = resolved_type;
4069 break;
4070 }
4071 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004072 }
Greg Clayton95d87902011-11-11 03:16:25 +00004073 else
4074 {
4075 if (m_using_apple_tables)
4076 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004077 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4078 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004079 }
4080 }
4081
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004082 }
4083 }
4084 return type_sp;
4085}
4086
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004087TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00004088SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004089{
4090 TypeSP type_sp;
4091
Greg Clayton1be10fc2010-09-29 01:12:09 +00004092 if (type_is_new_ptr)
4093 *type_is_new_ptr = false;
4094
Sean Callananc7fbf732010-08-06 00:32:49 +00004095 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004096 if (die != NULL)
4097 {
Greg Clayton21f2a492011-10-06 00:09:08 +00004098 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00004099 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004100 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s'",
Greg Clayton3bffb082011-12-10 02:15:28 +00004101 die->GetOffset(),
4102 DW_TAG_value_to_name(die->Tag()),
4103 die->GetName(this, dwarf_cu));
4104//
4105// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4106// if (log && dwarf_cu)
4107// {
4108// StreamString s;
4109// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00004110// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00004111//
4112// }
Jim Ingham16746d12011-08-25 23:21:43 +00004113
Greg Clayton594e5ed2010-09-27 21:07:38 +00004114 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004115 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00004116 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004117 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004118 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00004119 if (type_is_new_ptr)
4120 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004121
Greg Clayton594e5ed2010-09-27 21:07:38 +00004122 const dw_tag_t tag = die->Tag();
4123
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004124 bool is_forward_declaration = false;
4125 DWARFDebugInfoEntry::Attributes attributes;
4126 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00004127 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00004128 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
4129 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00004130 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00004131 Declaration decl;
4132
Greg Clayton4957bf62010-09-30 21:49:03 +00004133 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00004134 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004135
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004136 dw_attr_t attr;
4137
4138 switch (tag)
4139 {
4140 case DW_TAG_base_type:
4141 case DW_TAG_pointer_type:
4142 case DW_TAG_reference_type:
4143 case DW_TAG_typedef:
4144 case DW_TAG_const_type:
4145 case DW_TAG_restrict_type:
4146 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00004147 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004148 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004149 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004150 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004151
Greg Claytond88d7592010-09-15 08:33:30 +00004152 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004153 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004154 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
4155
4156 if (num_attributes > 0)
4157 {
4158 uint32_t i;
4159 for (i=0; i<num_attributes; ++i)
4160 {
4161 attr = attributes.AttributeAtIndex(i);
4162 DWARFFormValue form_value;
4163 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4164 {
4165 switch (attr)
4166 {
4167 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4168 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4169 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4170 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00004171
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004172 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00004173 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
4174 // include the "&"...
4175 if (tag == DW_TAG_reference_type)
4176 {
4177 if (strchr (type_name_cstr, '&') == NULL)
4178 type_name_cstr = NULL;
4179 }
4180 if (type_name_cstr)
4181 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004182 break;
Greg Clayton36909642011-03-15 04:38:20 +00004183 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004184 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
4185 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
4186 default:
4187 case DW_AT_sibling:
4188 break;
4189 }
4190 }
4191 }
4192 }
4193
Greg Clayton81c22f62011-10-19 18:09:39 +00004194 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 +00004195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004196 switch (tag)
4197 {
4198 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00004199 break;
4200
Greg Claytond4436412011-10-28 21:00:00 +00004201 case DW_TAG_unspecified_type:
4202 if (strcmp(type_name_cstr, "nullptr_t") == 0)
4203 {
4204 resolve_state = Type::eResolveStateFull;
4205 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
4206 break;
4207 }
4208 // Fall through to base type below in case we can handle the type there...
4209
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004210 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00004211 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004212 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
4213 encoding,
4214 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004215 break;
4216
Greg Clayton526e5af2010-11-13 03:52:47 +00004217 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
4218 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
4219 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
4220 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
4221 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
4222 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004223 }
4224
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004225 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
4226 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
4227 {
4228 static ConstString g_objc_type_name_id("id");
4229 static ConstString g_objc_type_name_Class("Class");
4230 static ConstString g_objc_type_name_selector("SEL");
4231
Greg Clayton24739922010-10-13 03:15:28 +00004232 if (type_name_const_str == g_objc_type_name_id)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004233 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004234 clang_type = ast.GetBuiltInType_objc_id();
Greg Clayton526e5af2010-11-13 03:52:47 +00004235 resolve_state = Type::eResolveStateFull;
4236
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004237 }
Greg Clayton24739922010-10-13 03:15:28 +00004238 else if (type_name_const_str == g_objc_type_name_Class)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004239 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004240 clang_type = ast.GetBuiltInType_objc_Class();
Greg Clayton526e5af2010-11-13 03:52:47 +00004241 resolve_state = Type::eResolveStateFull;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004242 }
Greg Clayton24739922010-10-13 03:15:28 +00004243 else if (type_name_const_str == g_objc_type_name_selector)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004244 {
Sean Callananf6c73082010-12-06 23:53:20 +00004245 clang_type = ast.GetBuiltInType_objc_selector();
Greg Clayton526e5af2010-11-13 03:52:47 +00004246 resolve_state = Type::eResolveStateFull;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004247 }
4248 }
4249
Greg Clayton81c22f62011-10-19 18:09:39 +00004250 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004251 this,
4252 type_name_const_str,
4253 byte_size,
4254 NULL,
4255 encoding_uid,
4256 encoding_data_type,
4257 &decl,
4258 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004259 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004260
Greg Clayton594e5ed2010-09-27 21:07:38 +00004261 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004262
4263// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
4264// if (encoding_type != NULL)
4265// {
4266// if (encoding_type != DIE_IS_BEING_PARSED)
4267// type_sp->SetEncodingType(encoding_type);
4268// else
4269// m_indirect_fixups.push_back(type_sp.get());
4270// }
4271 }
4272 break;
4273
4274 case DW_TAG_structure_type:
4275 case DW_TAG_union_type:
4276 case DW_TAG_class_type:
4277 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004278 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004279 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004280
Greg Clayton9e409562010-07-28 02:04:09 +00004281 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00004282 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004283 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00004284 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004285 if (num_attributes > 0)
4286 {
4287 uint32_t i;
4288 for (i=0; i<num_attributes; ++i)
4289 {
4290 attr = attributes.AttributeAtIndex(i);
4291 DWARFFormValue form_value;
4292 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4293 {
4294 switch (attr)
4295 {
Greg Clayton9e409562010-07-28 02:04:09 +00004296 case DW_AT_decl_file:
4297 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
4298 break;
4299
4300 case DW_AT_decl_line:
4301 decl.SetLine(form_value.Unsigned());
4302 break;
4303
4304 case DW_AT_decl_column:
4305 decl.SetColumn(form_value.Unsigned());
4306 break;
4307
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004308 case DW_AT_name:
4309 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004310 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004311 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004312
4313 case DW_AT_byte_size:
4314 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00004315 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004316 break;
4317
4318 case DW_AT_accessibility:
4319 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
4320 break;
4321
4322 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00004323 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00004324 break;
4325
4326 case DW_AT_APPLE_runtime_class:
4327 class_language = (LanguageType)form_value.Signed();
4328 break;
4329
Greg Clayton18774842011-11-29 23:40:34 +00004330 case DW_AT_APPLE_objc_complete_type:
4331 is_complete_objc_class = form_value.Signed();
4332 break;
4333
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004334 case DW_AT_allocated:
4335 case DW_AT_associated:
4336 case DW_AT_data_location:
4337 case DW_AT_description:
4338 case DW_AT_start_scope:
4339 case DW_AT_visibility:
4340 default:
4341 case DW_AT_sibling:
4342 break;
4343 }
4344 }
4345 }
4346 }
4347
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004348 UniqueDWARFASTType unique_ast_entry;
4349 if (decl.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004350 {
Greg Claytone576ab22011-02-15 00:19:15 +00004351 if (GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Greg Clayton36909642011-03-15 04:38:20 +00004352 this,
4353 dwarf_cu,
Greg Claytone576ab22011-02-15 00:19:15 +00004354 die,
4355 decl,
Greg Clayton36909642011-03-15 04:38:20 +00004356 byte_size_valid ? byte_size : -1,
Greg Claytone576ab22011-02-15 00:19:15 +00004357 unique_ast_entry))
Greg Claytonc615ce42010-11-09 04:42:43 +00004358 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004359 // We have already parsed this type or from another
4360 // compile unit. GCC loves to use the "one definition
4361 // rule" which can result in multiple definitions
4362 // of the same class over and over in each compile
4363 // unit.
4364 type_sp = unique_ast_entry.m_type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004365 if (type_sp)
4366 {
Greg Clayton4272cc72011-02-02 02:24:04 +00004367 m_die_to_type[die] = type_sp.get();
4368 return type_sp;
4369 }
4370 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004371 }
4372
Greg Clayton81c22f62011-10-19 18:09:39 +00004373 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 +00004374
4375 int tag_decl_kind = -1;
4376 AccessType default_accessibility = eAccessNone;
4377 if (tag == DW_TAG_structure_type)
4378 {
4379 tag_decl_kind = clang::TTK_Struct;
4380 default_accessibility = eAccessPublic;
4381 }
4382 else if (tag == DW_TAG_union_type)
4383 {
4384 tag_decl_kind = clang::TTK_Union;
4385 default_accessibility = eAccessPublic;
4386 }
4387 else if (tag == DW_TAG_class_type)
4388 {
4389 tag_decl_kind = clang::TTK_Class;
4390 default_accessibility = eAccessPrivate;
4391 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00004392
4393 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
4394 die->HasChildren() == false &&
4395 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
4396 {
4397 // Work around an issue with clang at the moment where
4398 // forward declarations for objective C classes are emitted
4399 // as:
4400 // DW_TAG_structure_type [2]
4401 // DW_AT_name( "ForwardObjcClass" )
4402 // DW_AT_byte_size( 0x00 )
4403 // DW_AT_decl_file( "..." )
4404 // DW_AT_decl_line( 1 )
4405 //
4406 // Note that there is no DW_AT_declaration and there are
4407 // no children, and the byte size is zero.
4408 is_forward_declaration = true;
4409 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004410
Greg Clayton18774842011-11-29 23:40:34 +00004411 if (class_language == eLanguageTypeObjC)
4412 {
Greg Clayton901c5ca2011-12-03 04:40:03 +00004413 if (!is_complete_objc_class)
4414 {
4415 // We have a valid eSymbolTypeObjCClass class symbol whose
4416 // name matches the current objective C class that we
4417 // are trying to find and this DIE isn't the complete
4418 // definition (we checked is_complete_objc_class above and
4419 // know it is false), so the real definition is in here somewhere
4420 type_sp = FindCompleteObjCDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004421
Greg Clayton901c5ca2011-12-03 04:40:03 +00004422 if (!type_sp && m_debug_map_symfile)
4423 {
4424 // We weren't able to find a full declaration in
4425 // this DWARF, see if we have a declaration anywhere
4426 // else...
4427 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
4428 }
4429
4430 if (type_sp)
4431 {
4432 if (log)
4433 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004434 GetObjectFile()->GetModule()->LogMessage (log.get(),
4435 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
4436 this,
4437 die->GetOffset(),
4438 DW_TAG_value_to_name(tag),
4439 type_name_cstr,
4440 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004441 }
4442
4443 // We found a real definition for this type elsewhere
4444 // so lets use it and cache the fact that we found
4445 // a complete type for this die
4446 m_die_to_type[die] = type_sp.get();
4447 return type_sp;
4448 }
4449 }
4450 }
4451
4452
4453 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004454 {
4455 // We have a forward declaration to a type and we need
4456 // to try and find a full declaration. We look in the
4457 // current type index just in case we have a forward
4458 // declaration followed by an actual declarations in the
4459 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00004460 if (log)
4461 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004462 GetObjectFile()->GetModule()->LogMessage (log.get(),
4463 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
4464 this,
4465 die->GetOffset(),
4466 DW_TAG_value_to_name(tag),
4467 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00004468 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004469
4470 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
4471
4472 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00004473 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004474 // We weren't able to find a full declaration in
4475 // this DWARF, see if we have a declaration anywhere
4476 // else...
4477 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
Greg Clayton4272cc72011-02-02 02:24:04 +00004478 }
4479
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004480 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00004481 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00004482 if (log)
4483 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004484 GetObjectFile()->GetModule()->LogMessage (log.get(),
4485 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
4486 this,
4487 die->GetOffset(),
4488 DW_TAG_value_to_name(tag),
4489 type_name_cstr,
4490 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00004491 }
4492
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004493 // We found a real definition for this type elsewhere
4494 // so lets use it and cache the fact that we found
4495 // a complete type for this die
4496 m_die_to_type[die] = type_sp.get();
4497 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004498 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004499 }
4500 assert (tag_decl_kind != -1);
4501 bool clang_type_was_created = false;
4502 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
4503 if (clang_type == NULL)
4504 {
Greg Claytonf0705c82011-10-22 03:33:13 +00004505 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton55561e92011-10-26 03:31:36 +00004506 if (accessibility == eAccessNone && decl_ctx)
4507 {
4508 // Check the decl context that contains this class/struct/union.
4509 // If it is a class we must give it an accessability.
4510 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
4511 if (DeclKindIsCXXClass (containing_decl_kind))
4512 accessibility = default_accessibility;
4513 }
4514
Greg Claytonf0705c82011-10-22 03:33:13 +00004515 if (type_name_cstr && strchr (type_name_cstr, '<'))
4516 {
4517 ClangASTContext::TemplateParameterInfos template_param_infos;
4518 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
4519 {
4520 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00004521 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00004522 type_name_cstr,
4523 tag_decl_kind,
4524 template_param_infos);
4525
4526 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
4527 class_template_decl,
4528 tag_decl_kind,
4529 template_param_infos);
4530 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
4531 clang_type_was_created = true;
4532 }
4533 }
4534
4535 if (!clang_type_was_created)
4536 {
4537 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00004538 clang_type = ast.CreateRecordType (decl_ctx,
4539 accessibility,
4540 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00004541 tag_decl_kind,
Greg Claytonf0705c82011-10-22 03:33:13 +00004542 class_language);
4543 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004544 }
4545
4546 // Store a forward declaration to this class type in case any
4547 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00004548 // types for function prototypes.
4549 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00004550 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004551 this,
4552 type_name_const_str,
4553 byte_size,
4554 NULL,
4555 LLDB_INVALID_UID,
4556 Type::eEncodingIsUID,
4557 &decl,
4558 clang_type,
4559 Type::eResolveStateForward));
4560
4561
4562 // Add our type to the unique type map so we don't
4563 // end up creating many copies of the same type over
4564 // and over in the ASTContext for our module
4565 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00004566 unique_ast_entry.m_symfile = this;
4567 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004568 unique_ast_entry.m_die = die;
4569 unique_ast_entry.m_declaration = decl;
Greg Claytone576ab22011-02-15 00:19:15 +00004570 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
4571 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004572
Sean Callanan12014a02011-12-08 23:45:45 +00004573 if (!is_forward_declaration)
4574 {
4575 if (die->HasChildren() == false)
4576 {
4577 // No children for this struct/union/class, lets finish it
4578 ast.StartTagDeclarationDefinition (clang_type);
4579 ast.CompleteTagDeclarationDefinition (clang_type);
4580 }
4581 else if (clang_type_was_created)
4582 {
4583 // Leave this as a forward declaration until we need
4584 // to know the details of the type. lldb_private::Type
4585 // will automatically call the SymbolFile virtual function
4586 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
4587 // When the definition needs to be defined.
4588 m_forward_decl_die_to_clang_type[die] = clang_type;
4589 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
4590 ClangASTContext::SetHasExternalStorage (clang_type, true);
4591 }
Greg Claytonc615ce42010-11-09 04:42:43 +00004592 }
Greg Claytoncab36a32011-12-08 05:16:30 +00004593
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004594 }
4595 break;
4596
4597 case DW_TAG_enumeration_type:
4598 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004599 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004600 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004601
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004602 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004603
Greg Claytond88d7592010-09-15 08:33:30 +00004604 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004605 if (num_attributes > 0)
4606 {
4607 uint32_t i;
4608
4609 for (i=0; i<num_attributes; ++i)
4610 {
4611 attr = attributes.AttributeAtIndex(i);
4612 DWARFFormValue form_value;
4613 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4614 {
4615 switch (attr)
4616 {
Greg Clayton7a345282010-11-09 23:46:37 +00004617 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4618 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4619 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004620 case DW_AT_name:
4621 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004622 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004623 break;
Greg Clayton7a345282010-11-09 23:46:37 +00004624 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00004625 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00004626 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
4627 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004628 case DW_AT_allocated:
4629 case DW_AT_associated:
4630 case DW_AT_bit_stride:
4631 case DW_AT_byte_stride:
4632 case DW_AT_data_location:
4633 case DW_AT_description:
4634 case DW_AT_start_scope:
4635 case DW_AT_visibility:
4636 case DW_AT_specification:
4637 case DW_AT_abstract_origin:
4638 case DW_AT_sibling:
4639 break;
4640 }
4641 }
4642 }
4643
Greg Clayton81c22f62011-10-19 18:09:39 +00004644 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 +00004645
Greg Clayton1be10fc2010-09-29 01:12:09 +00004646 clang_type_t enumerator_clang_type = NULL;
4647 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
4648 if (clang_type == NULL)
4649 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004650 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
4651 DW_ATE_signed,
4652 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00004653 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00004654 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00004655 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004656 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00004657 }
4658 else
4659 {
4660 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
4661 assert (enumerator_clang_type != NULL);
4662 }
4663
Greg Claytona2721472011-06-25 00:44:06 +00004664 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
4665
Greg Clayton81c22f62011-10-19 18:09:39 +00004666 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004667 this,
4668 type_name_const_str,
4669 byte_size,
4670 NULL,
4671 encoding_uid,
4672 Type::eEncodingIsUID,
4673 &decl,
4674 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004675 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004676
Greg Clayton6beaaa62011-01-17 03:46:26 +00004677 ast.StartTagDeclarationDefinition (clang_type);
4678 if (die->HasChildren())
4679 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00004680 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
4681 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00004682 }
4683 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004684 }
4685 }
4686 break;
4687
Jim Inghamb0be4422010-08-12 01:20:14 +00004688 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004689 case DW_TAG_subprogram:
4690 case DW_TAG_subroutine_type:
4691 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004692 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004693 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004694
4695 const char *mangled = NULL;
4696 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00004697 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004698 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00004699 bool is_static = false;
4700 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00004701 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00004702 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00004703 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
4704 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00004705
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004706 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00004707 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004708
4709
Greg Claytond88d7592010-09-15 08:33:30 +00004710 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004711 if (num_attributes > 0)
4712 {
4713 uint32_t i;
4714 for (i=0; i<num_attributes; ++i)
4715 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00004716 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004717 DWARFFormValue form_value;
4718 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4719 {
4720 switch (attr)
4721 {
4722 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;
4725 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;
4729
4730 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
4731 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00004732 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00004733 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00004734 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
4735 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00004736 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00004737 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
4738
Greg Claytonf51de672010-10-01 02:31:07 +00004739
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004740 case DW_AT_external:
4741 if (form_value.Unsigned())
4742 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00004743 if (storage == clang::SC_None)
4744 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004745 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00004746 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004747 }
4748 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004749
Greg Clayton72da3972011-08-16 18:40:23 +00004750 case DW_AT_specification:
4751 specification_die_offset = form_value.Reference(dwarf_cu);
4752 break;
4753
4754 case DW_AT_abstract_origin:
4755 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
4756 break;
4757
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004758 case DW_AT_allocated:
4759 case DW_AT_associated:
4760 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004761 case DW_AT_calling_convention:
4762 case DW_AT_data_location:
4763 case DW_AT_elemental:
4764 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004765 case DW_AT_frame_base:
4766 case DW_AT_high_pc:
4767 case DW_AT_low_pc:
4768 case DW_AT_object_pointer:
4769 case DW_AT_prototyped:
4770 case DW_AT_pure:
4771 case DW_AT_ranges:
4772 case DW_AT_recursive:
4773 case DW_AT_return_addr:
4774 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004775 case DW_AT_start_scope:
4776 case DW_AT_static_link:
4777 case DW_AT_trampoline:
4778 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004779 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004780 case DW_AT_description:
4781 case DW_AT_sibling:
4782 break;
4783 }
4784 }
4785 }
Greg Clayton24739922010-10-13 03:15:28 +00004786 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004787
Greg Clayton81c22f62011-10-19 18:09:39 +00004788 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 +00004789
Greg Clayton24739922010-10-13 03:15:28 +00004790 clang_type_t return_clang_type = NULL;
4791 Type *func_type = NULL;
4792
4793 if (type_die_offset != DW_INVALID_OFFSET)
4794 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00004795
Greg Clayton24739922010-10-13 03:15:28 +00004796 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00004797 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00004798 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004799 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004800
Greg Claytonf51de672010-10-01 02:31:07 +00004801
Greg Clayton24739922010-10-13 03:15:28 +00004802 std::vector<clang_type_t> function_param_types;
4803 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004804
Greg Clayton24739922010-10-13 03:15:28 +00004805 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00004806
Greg Claytoncb5860a2011-10-13 23:49:28 +00004807 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
4808 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00004809 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
4810
Greg Claytonf0705c82011-10-22 03:33:13 +00004811 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00004812 // Start off static. This will be set to false in ParseChildParameters(...)
4813 // if we find a "this" paramters as the first parameter
4814 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00004815 is_static = true;
4816
Greg Clayton24739922010-10-13 03:15:28 +00004817 if (die->HasChildren())
4818 {
Greg Clayton0fffff52010-09-24 05:15:53 +00004819 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004820 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00004821 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004822 type_sp,
4823 dwarf_cu,
4824 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00004825 skip_artificial,
4826 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004827 type_list,
4828 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00004829 function_param_decls,
4830 type_quals);
Greg Clayton24739922010-10-13 03:15:28 +00004831 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004832
Greg Clayton24739922010-10-13 03:15:28 +00004833 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004834 clang_type = ast.CreateFunctionType (return_clang_type,
4835 &function_param_types[0],
4836 function_param_types.size(),
4837 is_variadic,
4838 type_quals);
4839
Greg Clayton24739922010-10-13 03:15:28 +00004840 if (type_name_cstr)
4841 {
4842 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00004843 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004844 {
Jim Inghamb7f6b2f2011-09-08 22:13:49 +00004845 if (ObjCLanguageRuntime::IsPossibleObjCMethodName (type_name_cstr))
Greg Clayton0fffff52010-09-24 05:15:53 +00004846 {
Greg Clayton24739922010-10-13 03:15:28 +00004847 // We need to find the DW_TAG_class_type or
4848 // DW_TAG_struct_type by name so we can add this
4849 // as a member function of the class.
4850 const char *class_name_start = type_name_cstr + 2;
4851 const char *class_name_end = ::strchr (class_name_start, ' ');
4852 SymbolContext empty_sc;
4853 clang_type_t class_opaque_type = NULL;
4854 if (class_name_start < class_name_end)
Greg Clayton0fffff52010-09-24 05:15:53 +00004855 {
Greg Clayton24739922010-10-13 03:15:28 +00004856 ConstString class_name (class_name_start, class_name_end - class_name_start);
4857 TypeList types;
Sean Callanan213fdb82011-10-13 01:49:10 +00004858 const uint32_t match_count = FindTypes (empty_sc, class_name, NULL, true, UINT32_MAX, types);
Greg Clayton24739922010-10-13 03:15:28 +00004859 if (match_count > 0)
Greg Clayton0fffff52010-09-24 05:15:53 +00004860 {
Greg Clayton24739922010-10-13 03:15:28 +00004861 for (uint32_t i=0; i<match_count; ++i)
Greg Clayton0fffff52010-09-24 05:15:53 +00004862 {
Greg Clayton24739922010-10-13 03:15:28 +00004863 Type *type = types.GetTypeAtIndex (i).get();
4864 clang_type_t type_clang_forward_type = type->GetClangForwardType();
4865 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
Greg Clayton0fffff52010-09-24 05:15:53 +00004866 {
Greg Clayton24739922010-10-13 03:15:28 +00004867 class_opaque_type = type_clang_forward_type;
4868 break;
Greg Clayton0fffff52010-09-24 05:15:53 +00004869 }
4870 }
4871 }
Greg Clayton24739922010-10-13 03:15:28 +00004872 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004873
Greg Clayton24739922010-10-13 03:15:28 +00004874 if (class_opaque_type)
4875 {
4876 // If accessibility isn't set to anything valid, assume public for
4877 // now...
4878 if (accessibility == eAccessNone)
4879 accessibility = eAccessPublic;
4880
4881 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004882 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
4883 type_name_cstr,
4884 clang_type,
4885 accessibility);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00004886 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton24739922010-10-13 03:15:28 +00004887 type_handled = objc_method_decl != NULL;
4888 }
4889 }
Greg Clayton5113dc82011-08-12 06:47:54 +00004890 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00004891 {
4892 // Look at the parent of this DIE and see if is is
4893 // a class or struct and see if this is actually a
4894 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00004895 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00004896 if (class_type)
4897 {
Greg Clayton72da3972011-08-16 18:40:23 +00004898 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00004899 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004900 // We have a specification which we are going to base our function
4901 // prototype off of, so we need this type to be completed so that the
4902 // m_die_to_decl_ctx for the method in the specification has a valid
4903 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00004904 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00004905 // If we have a specification, then the function type should have been
4906 // made with the specification and not with this die.
4907 DWARFCompileUnitSP spec_cu_sp;
4908 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00004909 clang::DeclContext *spec_clang_decl_ctx = GetCachedClangDeclContextForDIE (spec_die);
4910 if (spec_clang_decl_ctx)
4911 {
4912 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
4913 }
4914 else
Jim Inghamc1663042011-09-29 22:12:35 +00004915 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004916 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
4917 MakeUserID(die->GetOffset()),
4918 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00004919 }
Greg Clayton72da3972011-08-16 18:40:23 +00004920 type_handled = true;
4921 }
4922 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
4923 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004924 // We have a specification which we are going to base our function
4925 // prototype off of, so we need this type to be completed so that the
4926 // m_die_to_decl_ctx for the method in the abstract origin has a valid
4927 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00004928 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00004929
Greg Clayton72da3972011-08-16 18:40:23 +00004930 DWARFCompileUnitSP abs_cu_sp;
4931 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00004932 clang::DeclContext *abs_clang_decl_ctx = GetCachedClangDeclContextForDIE (abs_die);
4933 if (abs_clang_decl_ctx)
4934 {
4935 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
4936 }
4937 else
Jim Inghamc1663042011-09-29 22:12:35 +00004938 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004939 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
4940 MakeUserID(die->GetOffset()),
4941 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00004942 }
Greg Clayton72da3972011-08-16 18:40:23 +00004943 type_handled = true;
4944 }
4945 else
4946 {
4947 clang_type_t class_opaque_type = class_type->GetClangForwardType();
4948 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00004949 {
Greg Clayton20568dd2011-10-13 23:13:20 +00004950 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00004951 {
Greg Clayton20568dd2011-10-13 23:13:20 +00004952 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
4953 // in the DWARF for C++ methods... Default to public for now...
4954 if (accessibility == eAccessNone)
4955 accessibility = eAccessPublic;
4956
4957 if (!is_static && !die->HasChildren())
4958 {
4959 // We have a C++ member function with no children (this pointer!)
4960 // and clang will get mad if we try and make a function that isn't
4961 // well formed in the DWARF, so we will just skip it...
4962 type_handled = true;
4963 }
4964 else
4965 {
4966 clang::CXXMethodDecl *cxx_method_decl;
4967 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00004968 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 +00004969 type_name_cstr,
4970 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00004971 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00004972 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
4973 m_obj_file->GetFileSpec().GetFilename().GetCString());
4974
Sean Callananc1b732d2011-11-01 18:07:13 +00004975 const bool is_attr_used = false;
4976
Greg Clayton20568dd2011-10-13 23:13:20 +00004977 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
4978 type_name_cstr,
4979 clang_type,
4980 accessibility,
4981 is_virtual,
4982 is_static,
4983 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00004984 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00004985 is_attr_used,
4986 is_artificial);
Greg Clayton20568dd2011-10-13 23:13:20 +00004987 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
4988
Greg Claytonf49e65a2011-11-10 18:31:53 +00004989 Host::SetCrashDescription (NULL);
4990
Greg Clayton20568dd2011-10-13 23:13:20 +00004991 type_handled = cxx_method_decl != NULL;
4992 }
Greg Clayton72da3972011-08-16 18:40:23 +00004993 }
4994 else
4995 {
Greg Clayton20568dd2011-10-13 23:13:20 +00004996 // We were asked to parse the type for a method in a class, yet the
4997 // class hasn't been asked to complete itself through the
4998 // clang::ExternalASTSource protocol, so we need to just have the
4999 // class complete itself and do things the right way, then our
5000 // DIE should then have an entry in the m_die_to_type map. First
5001 // we need to modify the m_die_to_type so it doesn't think we are
5002 // trying to parse this DIE anymore...
5003 m_die_to_type[die] = NULL;
5004
5005 // Now we get the full type to force our class type to complete itself
5006 // using the clang::ExternalASTSource protocol which will parse all
5007 // base classes and all methods (including the method for this DIE).
5008 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005009
Greg Clayton20568dd2011-10-13 23:13:20 +00005010 // The type for this DIE should have been filled in the function call above
5011 type_ptr = m_die_to_type[die];
5012 if (type_ptr)
5013 {
Greg Clayton85ae2e12011-10-18 23:36:41 +00005014 type_sp = type_ptr;
Greg Clayton20568dd2011-10-13 23:13:20 +00005015 break;
5016 }
Greg Clayton72da3972011-08-16 18:40:23 +00005017 }
Greg Clayton931180e2011-01-27 06:44:37 +00005018 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005019 }
5020 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005021 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005022 }
Greg Clayton24739922010-10-13 03:15:28 +00005023
5024 if (!type_handled)
5025 {
5026 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00005027 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
5028 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005029 clang_type,
5030 storage,
5031 is_inline);
Greg Clayton24739922010-10-13 03:15:28 +00005032
5033 // Add the decl to our DIE to decl context map
5034 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00005035 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00005036 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005037 ast.SetFunctionParameters (function_decl,
5038 &function_param_decls.front(),
5039 function_param_decls.size());
Greg Clayton24739922010-10-13 03:15:28 +00005040 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005041 }
Greg Clayton81c22f62011-10-19 18:09:39 +00005042 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005043 this,
5044 type_name_const_str,
5045 0,
5046 NULL,
5047 LLDB_INVALID_UID,
5048 Type::eEncodingIsUID,
5049 &decl,
5050 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005051 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00005052 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005053 }
5054 break;
5055
5056 case DW_TAG_array_type:
5057 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005058 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005059 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005060
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005061 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005062 int64_t first_index = 0;
5063 uint32_t byte_stride = 0;
5064 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00005065 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005066
5067 if (num_attributes > 0)
5068 {
5069 uint32_t i;
5070 for (i=0; i<num_attributes; ++i)
5071 {
5072 attr = attributes.AttributeAtIndex(i);
5073 DWARFFormValue form_value;
5074 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5075 {
5076 switch (attr)
5077 {
5078 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5079 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5080 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5081 case DW_AT_name:
5082 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005083 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005084 break;
5085
5086 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005087 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005088 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
5089 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005090 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005091 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005092 case DW_AT_allocated:
5093 case DW_AT_associated:
5094 case DW_AT_data_location:
5095 case DW_AT_description:
5096 case DW_AT_ordering:
5097 case DW_AT_start_scope:
5098 case DW_AT_visibility:
5099 case DW_AT_specification:
5100 case DW_AT_abstract_origin:
5101 case DW_AT_sibling:
5102 break;
5103 }
5104 }
5105 }
5106
Greg Clayton81c22f62011-10-19 18:09:39 +00005107 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 +00005108
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005109 Type *element_type = ResolveTypeUID(type_die_offset);
5110
5111 if (element_type)
5112 {
5113 std::vector<uint64_t> element_orders;
5114 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00005115 // We have an array that claims to have no members, lets give it at least one member...
5116 if (element_orders.empty())
5117 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005118 if (byte_stride == 0 && bit_stride == 0)
5119 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00005120 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005121 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
5122 uint64_t num_elements = 0;
5123 std::vector<uint64_t>::const_reverse_iterator pos;
5124 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
5125 for (pos = element_orders.rbegin(); pos != end; ++pos)
5126 {
5127 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005128 clang_type = ast.CreateArrayType (array_element_type,
5129 num_elements,
5130 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005131 array_element_type = clang_type;
5132 array_element_bit_stride = array_element_bit_stride * num_elements;
5133 }
5134 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00005135 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005136 this,
5137 empty_name,
5138 array_element_bit_stride / 8,
5139 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00005140 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005141 Type::eEncodingIsUID,
5142 &decl,
5143 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005144 Type::eResolveStateFull));
5145 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005146 }
5147 }
5148 }
5149 break;
5150
Greg Clayton9b81a312010-06-12 01:20:30 +00005151 case DW_TAG_ptr_to_member_type:
5152 {
5153 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
5154 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
5155
Greg Claytond88d7592010-09-15 08:33:30 +00005156 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00005157
5158 if (num_attributes > 0) {
5159 uint32_t i;
5160 for (i=0; i<num_attributes; ++i)
5161 {
5162 attr = attributes.AttributeAtIndex(i);
5163 DWARFFormValue form_value;
5164 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5165 {
5166 switch (attr)
5167 {
5168 case DW_AT_type:
5169 type_die_offset = form_value.Reference(dwarf_cu); break;
5170 case DW_AT_containing_type:
5171 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
5172 }
5173 }
5174 }
5175
5176 Type *pointee_type = ResolveTypeUID(type_die_offset);
5177 Type *class_type = ResolveTypeUID(containing_type_die_offset);
5178
Greg Clayton526e5af2010-11-13 03:52:47 +00005179 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
5180 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00005181
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005182 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
5183 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00005184
Greg Clayton526e5af2010-11-13 03:52:47 +00005185 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
5186 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00005187
Greg Clayton81c22f62011-10-19 18:09:39 +00005188 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005189 this,
5190 type_name_const_str,
5191 byte_size,
5192 NULL,
5193 LLDB_INVALID_UID,
5194 Type::eEncodingIsUID,
5195 NULL,
5196 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005197 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00005198 }
5199
5200 break;
5201 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005202 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00005203 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005204 break;
5205 }
5206
5207 if (type_sp.get())
5208 {
5209 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5210 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
5211
5212 SymbolContextScope * symbol_context_scope = NULL;
5213 if (sc_parent_tag == DW_TAG_compile_unit)
5214 {
5215 symbol_context_scope = sc.comp_unit;
5216 }
5217 else if (sc.function != NULL)
5218 {
Greg Clayton81c22f62011-10-19 18:09:39 +00005219 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005220 if (symbol_context_scope == NULL)
5221 symbol_context_scope = sc.function;
5222 }
5223
5224 if (symbol_context_scope != NULL)
5225 {
5226 type_sp->SetSymbolContextScope(symbol_context_scope);
5227 }
5228
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005229 // We are ready to put this type into the uniqued list up at the module level
5230 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00005231
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005232 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005233 }
5234 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00005235 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005236 {
Greg Clayton85ae2e12011-10-18 23:36:41 +00005237 type_sp = type_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005238 }
5239 }
5240 return type_sp;
5241}
5242
5243size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00005244SymbolFileDWARF::ParseTypes
5245(
5246 const SymbolContext& sc,
5247 DWARFCompileUnit* dwarf_cu,
5248 const DWARFDebugInfoEntry *die,
5249 bool parse_siblings,
5250 bool parse_children
5251)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005252{
5253 size_t types_added = 0;
5254 while (die != NULL)
5255 {
5256 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005257 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005258 {
5259 if (type_is_new)
5260 ++types_added;
5261 }
5262
5263 if (parse_children && die->HasChildren())
5264 {
5265 if (die->Tag() == DW_TAG_subprogram)
5266 {
5267 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00005268 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005269 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
5270 }
5271 else
5272 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
5273 }
5274
5275 if (parse_siblings)
5276 die = die->GetSibling();
5277 else
5278 die = NULL;
5279 }
5280 return types_added;
5281}
5282
5283
5284size_t
5285SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
5286{
5287 assert(sc.comp_unit && sc.function);
5288 size_t functions_added = 0;
5289 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5290 if (dwarf_cu)
5291 {
5292 dw_offset_t function_die_offset = sc.function->GetID();
5293 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
5294 if (function_die)
5295 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00005296 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005297 }
5298 }
5299
5300 return functions_added;
5301}
5302
5303
5304size_t
5305SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
5306{
5307 // At least a compile unit must be valid
5308 assert(sc.comp_unit);
5309 size_t types_added = 0;
5310 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5311 if (dwarf_cu)
5312 {
5313 if (sc.function)
5314 {
5315 dw_offset_t function_die_offset = sc.function->GetID();
5316 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
5317 if (func_die && func_die->HasChildren())
5318 {
5319 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
5320 }
5321 }
5322 else
5323 {
5324 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
5325 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
5326 {
5327 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
5328 }
5329 }
5330 }
5331
5332 return types_added;
5333}
5334
5335size_t
5336SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
5337{
5338 if (sc.comp_unit != NULL)
5339 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00005340 DWARFDebugInfo* info = DebugInfo();
5341 if (info == NULL)
5342 return 0;
5343
5344 uint32_t cu_idx = UINT32_MAX;
5345 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005346
5347 if (dwarf_cu == NULL)
5348 return 0;
5349
5350 if (sc.function)
5351 {
5352 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00005353
5354 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 +00005355 if (func_lo_pc != DW_INVALID_ADDRESS)
5356 {
5357 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00005358
Greg Claytone38a5ed2012-01-05 03:57:59 +00005359 // Let all blocks know they have parse all their variables
5360 sc.function->GetBlock (false).SetDidParseVariables (true, true);
5361 return num_variables;
5362 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005363 }
5364 else if (sc.comp_unit)
5365 {
5366 uint32_t vars_added = 0;
5367 VariableListSP variables (sc.comp_unit->GetVariableList(false));
5368
5369 if (variables.get() == NULL)
5370 {
5371 variables.reset(new VariableList());
5372 sc.comp_unit->SetVariableList(variables);
5373
Greg Claytond4a2b372011-09-12 23:21:58 +00005374 DWARFCompileUnit* match_dwarf_cu = NULL;
5375 const DWARFDebugInfoEntry* die = NULL;
5376 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00005377 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00005378 {
Greg Clayton97fbc342011-10-20 22:30:33 +00005379 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00005380 {
5381 DWARFMappedHash::DIEInfoArray hash_data_array;
5382 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
5383 dwarf_cu->GetNextCompileUnitOffset(),
5384 hash_data_array))
5385 {
5386 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
5387 }
5388 }
Greg Clayton7f995132011-10-04 22:41:51 +00005389 }
5390 else
5391 {
5392 // Index if we already haven't to make sure the compile units
5393 // get indexed and make their global DIE index list
5394 if (!m_indexed)
5395 Index ();
5396
5397 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
5398 dwarf_cu->GetNextCompileUnitOffset(),
5399 die_offsets);
5400 }
5401
5402 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00005403 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005404 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005405 DWARFDebugInfo* debug_info = DebugInfo();
5406 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005407 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005408 const dw_offset_t die_offset = die_offsets[i];
5409 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00005410 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00005411 {
Greg Clayton95d87902011-11-11 03:16:25 +00005412 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
5413 if (var_sp)
5414 {
5415 variables->AddVariableIfUnique (var_sp);
5416 ++vars_added;
5417 }
Greg Claytond4a2b372011-09-12 23:21:58 +00005418 }
Greg Clayton95d87902011-11-11 03:16:25 +00005419 else
5420 {
5421 if (m_using_apple_tables)
5422 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005423 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 +00005424 }
5425 }
5426
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005427 }
5428 }
5429 }
5430 return vars_added;
5431 }
5432 }
5433 return 0;
5434}
5435
5436
5437VariableSP
5438SymbolFileDWARF::ParseVariableDIE
5439(
5440 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00005441 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00005442 const DWARFDebugInfoEntry *die,
5443 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005444)
5445{
5446
Greg Clayton83c5cd92010-11-14 22:13:40 +00005447 VariableSP var_sp (m_die_to_variable_sp[die]);
5448 if (var_sp)
5449 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005450
5451 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00005452
5453 if ((tag == DW_TAG_variable) ||
5454 (tag == DW_TAG_constant) ||
5455 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005456 {
Greg Clayton7f995132011-10-04 22:41:51 +00005457 DWARFDebugInfoEntry::Attributes attributes;
5458 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
5459 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005460 {
Greg Clayton7f995132011-10-04 22:41:51 +00005461 const char *name = NULL;
5462 const char *mangled = NULL;
5463 Declaration decl;
5464 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00005465 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00005466 DWARFExpression location;
5467 bool is_external = false;
5468 bool is_artificial = false;
5469 bool location_is_const_value_data = false;
5470 AccessType accessibility = eAccessNone;
5471
5472 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005473 {
Greg Clayton7f995132011-10-04 22:41:51 +00005474 dw_attr_t attr = attributes.AttributeAtIndex(i);
5475 DWARFFormValue form_value;
5476 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005477 {
Greg Clayton7f995132011-10-04 22:41:51 +00005478 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005479 {
Greg Clayton7f995132011-10-04 22:41:51 +00005480 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5481 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5482 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5483 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
5484 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00005485 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00005486 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
5487 case DW_AT_const_value:
5488 location_is_const_value_data = true;
5489 // Fall through...
5490 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005491 {
Greg Clayton7f995132011-10-04 22:41:51 +00005492 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005493 {
Greg Clayton7f995132011-10-04 22:41:51 +00005494 const DataExtractor& debug_info_data = get_debug_info_data();
5495
5496 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
5497 uint32_t block_length = form_value.Unsigned();
5498 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
5499 }
5500 else
5501 {
5502 const DataExtractor& debug_loc_data = get_debug_loc_data();
5503 const dw_offset_t debug_loc_offset = form_value.Unsigned();
5504
5505 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
5506 if (loc_list_length > 0)
5507 {
5508 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
5509 assert (func_low_pc != LLDB_INVALID_ADDRESS);
5510 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
5511 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005512 }
5513 }
Greg Clayton7f995132011-10-04 22:41:51 +00005514 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005515
Greg Clayton7f995132011-10-04 22:41:51 +00005516 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5517 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5518 case DW_AT_declaration:
5519 case DW_AT_description:
5520 case DW_AT_endianity:
5521 case DW_AT_segment:
5522 case DW_AT_start_scope:
5523 case DW_AT_visibility:
5524 default:
5525 case DW_AT_abstract_origin:
5526 case DW_AT_sibling:
5527 case DW_AT_specification:
5528 break;
5529 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005530 }
5531 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005532
Greg Clayton7f995132011-10-04 22:41:51 +00005533 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005534 {
Greg Clayton7f995132011-10-04 22:41:51 +00005535 ValueType scope = eValueTypeInvalid;
5536
5537 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5538 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005539 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00005540
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005541 // DWARF doesn't specify if a DW_TAG_variable is a local, global
5542 // or static variable, so we have to do a little digging by
5543 // looking at the location of a varaible to see if it contains
5544 // a DW_OP_addr opcode _somewhere_ in the definition. I say
5545 // somewhere because clang likes to combine small global variables
5546 // into the same symbol and have locations like:
5547 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
5548 // So if we don't have a DW_TAG_formal_parameter, we can look at
5549 // the location to see if it contains a DW_OP_addr opcode, and
5550 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00005551 if (tag == DW_TAG_formal_parameter)
5552 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00005553 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005554 {
Greg Clayton96c09682012-01-04 22:56:43 +00005555 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00005556 // Check if the location has a DW_OP_addr with any address value...
5557 addr_t location_has_op_addr = false;
5558 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00005559 {
5560 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
5561 if (op_error)
5562 {
5563 StreamString strm;
5564 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005565 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 +00005566 }
5567 }
Greg Claytond1767f02011-12-08 02:13:16 +00005568
5569 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005570 {
Greg Claytond1767f02011-12-08 02:13:16 +00005571 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005572 {
Greg Claytond1767f02011-12-08 02:13:16 +00005573 scope = eValueTypeVariableGlobal;
5574
5575 if (m_debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005576 {
Greg Claytond1767f02011-12-08 02:13:16 +00005577 // When leaving the DWARF in the .o files on darwin,
5578 // when we have a global variable that wasn't initialized,
5579 // the .o file might not have allocated a virtual
5580 // address for the global variable. In this case it will
5581 // have created a symbol for the global variable
5582 // that is undefined and external and the value will
5583 // be the byte size of the variable. When we do the
5584 // address map in SymbolFileDWARFDebugMap we rely on
5585 // having an address, we need to do some magic here
5586 // so we can get the correct address for our global
5587 // variable. The address for all of these entries
5588 // will be zero, and there will be an undefined symbol
5589 // in this object file, and the executable will have
5590 // a matching symbol with a good address. So here we
5591 // dig up the correct address and replace it in the
5592 // location for the variable, and set the variable's
5593 // symbol context scope to be that of the main executable
5594 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00005595 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005596 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005597
Greg Claytond1767f02011-12-08 02:13:16 +00005598 // we have a possible uninitialized extern global
5599 Symtab *symtab = m_obj_file->GetSymtab();
5600 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005601 {
Greg Claytond1767f02011-12-08 02:13:16 +00005602 ConstString const_name(name);
5603 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
5604 eSymbolTypeUndefined,
5605 Symtab::eDebugNo,
5606 Symtab::eVisibilityExtern);
5607
5608 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005609 {
Greg Claytond1767f02011-12-08 02:13:16 +00005610 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
5611 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005612 {
Greg Claytond1767f02011-12-08 02:13:16 +00005613 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
5614 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
5615 eSymbolTypeData,
5616 Symtab::eDebugYes,
5617 Symtab::eVisibilityExtern);
5618 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005619 {
Greg Claytond1767f02011-12-08 02:13:16 +00005620 const AddressRange *defined_range = defined_symbol->GetAddressRangePtr();
5621 if (defined_range)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005622 {
Greg Claytond1767f02011-12-08 02:13:16 +00005623 const addr_t defined_addr = defined_range->GetBaseAddress().GetFileAddress();
5624 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005625 {
Greg Claytond1767f02011-12-08 02:13:16 +00005626 if (location.Update_DW_OP_addr (defined_addr))
5627 {
5628 symbol_context_scope = defined_symbol;
5629 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005630 }
5631 }
5632 }
5633 }
5634 }
5635 }
5636 }
5637 }
5638 }
Greg Claytond1767f02011-12-08 02:13:16 +00005639 else
5640 {
5641 scope = eValueTypeVariableStatic;
5642 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005643 }
5644 else
Greg Claytond1767f02011-12-08 02:13:16 +00005645 {
5646 scope = eValueTypeVariableLocal;
5647 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005648 }
Greg Clayton7f995132011-10-04 22:41:51 +00005649
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005650 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00005651 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005652 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00005653 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005654 case DW_TAG_subprogram:
5655 case DW_TAG_inlined_subroutine:
5656 case DW_TAG_lexical_block:
5657 if (sc.function)
5658 {
5659 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
5660 if (symbol_context_scope == NULL)
5661 symbol_context_scope = sc.function;
5662 }
5663 break;
5664
5665 default:
5666 symbol_context_scope = sc.comp_unit;
5667 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00005668 }
Greg Clayton7f995132011-10-04 22:41:51 +00005669 }
5670
Greg Clayton5cf58b92011-10-05 22:22:08 +00005671 if (symbol_context_scope)
5672 {
Greg Clayton81c22f62011-10-19 18:09:39 +00005673 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
5674 name,
5675 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00005676 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00005677 scope,
5678 symbol_context_scope,
5679 &decl,
5680 location,
5681 is_external,
5682 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00005683
5684 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
5685 }
5686 else
5687 {
5688 // Not ready to parse this variable yet. It might be a global
5689 // or static variable that is in a function scope and the function
5690 // in the symbol context wasn't filled in yet
5691 return var_sp;
5692 }
Greg Clayton7f995132011-10-04 22:41:51 +00005693 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005694 }
Greg Clayton7f995132011-10-04 22:41:51 +00005695 // Cache var_sp even if NULL (the variable was just a specification or
5696 // was missing vital information to be able to be displayed in the debugger
5697 // (missing location due to optimization, etc)) so we don't re-parse
5698 // this DIE over and over later...
5699 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005700 }
5701 return var_sp;
5702}
5703
Greg Claytonc662ec82011-06-17 22:10:16 +00005704
5705const DWARFDebugInfoEntry *
5706SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
5707 dw_offset_t spec_block_die_offset,
5708 DWARFCompileUnit **result_die_cu_handle)
5709{
5710 // Give the concrete function die specified by "func_die_offset", find the
5711 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
5712 // to "spec_block_die_offset"
5713 DWARFDebugInfo* info = DebugInfo();
5714
5715 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
5716 if (die)
5717 {
5718 assert (*result_die_cu_handle);
5719 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
5720 }
5721 return NULL;
5722}
5723
5724
5725const DWARFDebugInfoEntry *
5726SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
5727 const DWARFDebugInfoEntry *die,
5728 dw_offset_t spec_block_die_offset,
5729 DWARFCompileUnit **result_die_cu_handle)
5730{
5731 if (die)
5732 {
5733 switch (die->Tag())
5734 {
5735 case DW_TAG_subprogram:
5736 case DW_TAG_inlined_subroutine:
5737 case DW_TAG_lexical_block:
5738 {
5739 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
5740 {
5741 *result_die_cu_handle = dwarf_cu;
5742 return die;
5743 }
5744
5745 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
5746 {
5747 *result_die_cu_handle = dwarf_cu;
5748 return die;
5749 }
5750 }
5751 break;
5752 }
5753
5754 // Give the concrete function die specified by "func_die_offset", find the
5755 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
5756 // to "spec_block_die_offset"
5757 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
5758 {
5759 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
5760 child_die,
5761 spec_block_die_offset,
5762 result_die_cu_handle);
5763 if (result_die)
5764 return result_die;
5765 }
5766 }
5767
5768 *result_die_cu_handle = NULL;
5769 return NULL;
5770}
5771
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005772size_t
5773SymbolFileDWARF::ParseVariables
5774(
5775 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00005776 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00005777 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005778 const DWARFDebugInfoEntry *orig_die,
5779 bool parse_siblings,
5780 bool parse_children,
5781 VariableList* cc_variable_list
5782)
5783{
5784 if (orig_die == NULL)
5785 return 0;
5786
Greg Claytonc662ec82011-06-17 22:10:16 +00005787 VariableListSP variable_list_sp;
5788
5789 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005790 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00005791 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005792 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005793 dw_tag_t tag = die->Tag();
5794
5795 // Check to see if we have already parsed this variable or constant?
5796 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005797 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005798 if (cc_variable_list)
5799 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005800 }
5801 else
5802 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005803 // We haven't already parsed it, lets do that now.
5804 if ((tag == DW_TAG_variable) ||
5805 (tag == DW_TAG_constant) ||
5806 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005807 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005808 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00005809 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005810 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
5811 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
5812 switch (parent_tag)
5813 {
5814 case DW_TAG_compile_unit:
5815 if (sc.comp_unit != NULL)
5816 {
5817 variable_list_sp = sc.comp_unit->GetVariableList(false);
5818 if (variable_list_sp.get() == NULL)
5819 {
5820 variable_list_sp.reset(new VariableList());
5821 sc.comp_unit->SetVariableList(variable_list_sp);
5822 }
5823 }
5824 else
5825 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005826 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
5827 MakeUserID(sc_parent_die->GetOffset()),
5828 DW_TAG_value_to_name (parent_tag),
5829 MakeUserID(orig_die->GetOffset()),
5830 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00005831 }
5832 break;
5833
5834 case DW_TAG_subprogram:
5835 case DW_TAG_inlined_subroutine:
5836 case DW_TAG_lexical_block:
5837 if (sc.function != NULL)
5838 {
5839 // Check to see if we already have parsed the variables for the given scope
5840
Greg Clayton81c22f62011-10-19 18:09:39 +00005841 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00005842 if (block == NULL)
5843 {
5844 // This must be a specification or abstract origin with
5845 // a concrete block couterpart in the current function. We need
5846 // to find the concrete block so we can correctly add the
5847 // variable to it
5848 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
5849 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
5850 sc_parent_die->GetOffset(),
5851 &concrete_block_die_cu);
5852 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00005853 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00005854 }
5855
5856 if (block != NULL)
5857 {
5858 const bool can_create = false;
5859 variable_list_sp = block->GetBlockVariableList (can_create);
5860 if (variable_list_sp.get() == NULL)
5861 {
5862 variable_list_sp.reset(new VariableList());
5863 block->SetVariableList(variable_list_sp);
5864 }
5865 }
5866 }
5867 break;
5868
5869 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00005870 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
5871 MakeUserID(orig_die->GetOffset()),
5872 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00005873 break;
5874 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00005875 }
Greg Claytonc662ec82011-06-17 22:10:16 +00005876
5877 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005878 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00005879 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
5880 if (var_sp)
5881 {
Greg Claytonc662ec82011-06-17 22:10:16 +00005882 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00005883 if (cc_variable_list)
5884 cc_variable_list->AddVariableIfUnique (var_sp);
5885 ++vars_added;
5886 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005887 }
5888 }
5889 }
Greg Claytonc662ec82011-06-17 22:10:16 +00005890
5891 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
5892
5893 if (!skip_children && parse_children && die->HasChildren())
5894 {
5895 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
5896 }
5897
5898 if (parse_siblings)
5899 die = die->GetSibling();
5900 else
5901 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005902 }
Greg Claytonc662ec82011-06-17 22:10:16 +00005903 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005904}
5905
5906//------------------------------------------------------------------
5907// PluginInterface protocol
5908//------------------------------------------------------------------
5909const char *
5910SymbolFileDWARF::GetPluginName()
5911{
5912 return "SymbolFileDWARF";
5913}
5914
5915const char *
5916SymbolFileDWARF::GetShortPluginName()
5917{
5918 return GetPluginNameStatic();
5919}
5920
5921uint32_t
5922SymbolFileDWARF::GetPluginVersion()
5923{
5924 return 1;
5925}
5926
5927void
Greg Clayton6beaaa62011-01-17 03:46:26 +00005928SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
5929{
5930 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
5931 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
5932 if (clang_type)
5933 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
5934}
5935
5936void
5937SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
5938{
5939 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
5940 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
5941 if (clang_type)
5942 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
5943}
5944
Greg Claytona2721472011-06-25 00:44:06 +00005945void
Sean Callanancc427fa2011-07-30 02:42:06 +00005946SymbolFileDWARF::DumpIndexes ()
5947{
5948 StreamFile s(stdout, false);
5949
5950 s.Printf ("DWARF index for (%s) '%s/%s':",
5951 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
5952 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
5953 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
5954 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
5955 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
5956 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
5957 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
5958 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
5959 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
5960 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
5961 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
5962}
5963
5964void
5965SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
5966 const char *name,
5967 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00005968{
Sean Callanancc427fa2011-07-30 02:42:06 +00005969 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00005970
5971 if (iter == m_decl_ctx_to_die.end())
5972 return;
5973
Greg Claytoncb5860a2011-10-13 23:49:28 +00005974 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00005975 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00005976 const DWARFDebugInfoEntry *context_die = *pos;
5977
5978 if (!results)
5979 return;
5980
5981 DWARFDebugInfo* info = DebugInfo();
5982
5983 DIEArray die_offsets;
5984
5985 DWARFCompileUnit* dwarf_cu = NULL;
5986 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00005987
5988 if (m_using_apple_tables)
5989 {
5990 if (m_apple_types_ap.get())
5991 m_apple_types_ap->FindByName (name, die_offsets);
5992 }
5993 else
5994 {
5995 if (!m_indexed)
5996 Index ();
5997
5998 m_type_index.Find (ConstString(name), die_offsets);
5999 }
6000
Greg Clayton220a0072011-12-09 08:48:30 +00006001 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006002
6003 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00006004 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006005 for (size_t i = 0; i < num_matches; ++i)
6006 {
6007 const dw_offset_t die_offset = die_offsets[i];
6008 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00006009
Greg Claytoncb5860a2011-10-13 23:49:28 +00006010 if (die->GetParent() != context_die)
6011 continue;
6012
6013 Type *matching_type = ResolveType (dwarf_cu, die);
6014
Greg Clayton42ce2f32011-12-12 21:50:19 +00006015 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006016 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
6017
6018 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
6019 {
6020 clang::TagDecl *tag_decl = tag_type->getDecl();
6021 results->push_back(tag_decl);
6022 }
6023 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
6024 {
6025 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
6026 results->push_back(typedef_decl);
6027 }
Greg Claytona2721472011-06-25 00:44:06 +00006028 }
6029 }
6030 }
6031}
6032
6033void
6034SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00006035 const clang::DeclContext *decl_context,
6036 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00006037 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
6038{
Greg Clayton85ae2e12011-10-18 23:36:41 +00006039
6040 switch (decl_context->getDeclKind())
6041 {
6042 case clang::Decl::Namespace:
6043 case clang::Decl::TranslationUnit:
6044 {
6045 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6046 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
6047 }
6048 break;
6049 default:
6050 break;
6051 }
Greg Claytona2721472011-06-25 00:44:06 +00006052}