blob: 297f9bac1a87abc571e36fa984160049940e20e1 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Greg Clayton3c2e3ae2012-02-06 06:42:51 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "clang/Basic/Builtins.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000025#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanancc427fa2011-07-30 02:42:06 +000027#include "llvm/Support/Casting.h"
28
Robert Flackeb83fab2015-05-15 18:59:59 +000029#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/Module.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000031#include "lldb/Core/ModuleList.h"
32#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/PluginManager.h"
34#include "lldb/Core/RegularExpression.h"
35#include "lldb/Core/Scalar.h"
36#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000037#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000038#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Core/Timer.h"
40#include "lldb/Core/Value.h"
41
Sean Callananf0c5aeb2015-04-20 16:31:29 +000042#include "lldb/Expression/ClangModulesDeclVendor.h"
43
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +000044#include "lldb/Host/FileSystem.h"
Greg Clayton20568dd2011-10-13 23:13:20 +000045#include "lldb/Host/Host.h"
46
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +000047#include "lldb/Interpreter/OptionValueFileSpecList.h"
48#include "lldb/Interpreter/OptionValueProperties.h"
49
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000051#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Symbol/CompileUnit.h"
53#include "lldb/Symbol/LineTable.h"
54#include "lldb/Symbol/ObjectFile.h"
55#include "lldb/Symbol/SymbolVendor.h"
56#include "lldb/Symbol/VariableList.h"
57
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000058#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000059#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000060
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFCompileUnit.h"
62#include "DWARFDebugAbbrev.h"
63#include "DWARFDebugAranges.h"
64#include "DWARFDebugInfo.h"
65#include "DWARFDebugInfoEntry.h"
66#include "DWARFDebugLine.h"
67#include "DWARFDebugPubnames.h"
68#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000069#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070#include "DWARFDIECollection.h"
71#include "DWARFFormValue.h"
72#include "DWARFLocationList.h"
73#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000074#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075
76#include <map>
77
Matthew Gardinere81df3b2014-08-26 06:57:23 +000078#include <ctype.h>
79#include <string.h>
80
Greg Clayton62742b12010-11-11 01:09:45 +000081//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000082
83#ifdef ENABLE_DEBUG_PRINTF
84#include <stdio.h>
Ed Mastea0191d12013-10-17 20:42:56 +000085#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
Greg Claytonc93237c2010-10-01 20:48:32 +000086#else
87#define DEBUG_PRINTF(fmt, ...)
88#endif
89
Greg Clayton594e5ed2010-09-27 21:07:38 +000090#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091
92using namespace lldb;
93using namespace lldb_private;
94
Greg Clayton219cf312012-03-30 00:51:13 +000095//static inline bool
96//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
97//{
98// switch (tag)
99// {
100// default:
101// break;
102// case DW_TAG_subprogram:
103// case DW_TAG_inlined_subroutine:
104// case DW_TAG_class_type:
105// case DW_TAG_structure_type:
106// case DW_TAG_union_type:
107// return true;
108// }
109// return false;
110//}
111//
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000112
113namespace {
114
115 PropertyDefinition
116 g_properties[] =
117 {
118 { "comp-dir-symlink-paths" , OptionValue::eTypeFileSpecList, true, 0 , nullptr, nullptr, "If the DW_AT_comp_dir matches any of these paths the symbolic links will be resolved at DWARF parse time." },
119 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr }
120 };
121
122 enum
123 {
124 ePropertySymLinkPaths
125 };
126
127
128 class PluginProperties : public Properties
129 {
130 public:
131 static ConstString
132 GetSettingName()
133 {
134 return SymbolFileDWARF::GetPluginNameStatic();
135 }
136
137 PluginProperties()
138 {
139 m_collection_sp.reset (new OptionValueProperties(GetSettingName()));
140 m_collection_sp->Initialize(g_properties);
141 }
142
143 FileSpecList&
144 GetSymLinkPaths()
145 {
146 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, true, ePropertySymLinkPaths);
147 assert(option_value);
148 return option_value->GetCurrentValue();
149 }
150
151 };
152
153 typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
154
155 static const SymbolFileDWARFPropertiesSP&
156 GetGlobalPluginProperties()
157 {
158 static const auto g_settings_sp(std::make_shared<PluginProperties>());
159 return g_settings_sp;
160 }
161
162} // anonymous namespace end
163
164
Sean Callananc7fbf732010-08-06 00:32:49 +0000165static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000166DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167{
168 switch (dwarf_accessibility)
169 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000170 case DW_ACCESS_public: return eAccessPublic;
171 case DW_ACCESS_private: return eAccessPrivate;
172 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000173 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000175 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176}
177
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000178static const char*
179removeHostnameFromPathname(const char* path_from_dwarf)
180{
181 if (!path_from_dwarf || !path_from_dwarf[0])
182 {
183 return path_from_dwarf;
184 }
Enrico Granata99e5e222015-07-27 21:27:02 +0000185
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000186 const char *colon_pos = strchr(path_from_dwarf, ':');
Enrico Granata99e5e222015-07-27 21:27:02 +0000187 if (nullptr == colon_pos)
188 {
189 return path_from_dwarf;
190 }
191
192 const char *slash_pos = strchr(path_from_dwarf, '/');
193 if (slash_pos && (slash_pos < colon_pos))
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000194 {
195 return path_from_dwarf;
196 }
197
198 // check whether we have a windows path, and so the first character
199 // is a drive-letter not a hostname.
200 if (
201 colon_pos == path_from_dwarf + 1 &&
202 isalpha(*path_from_dwarf) &&
203 strlen(path_from_dwarf) > 2 &&
204 '\\' == path_from_dwarf[2])
205 {
206 return path_from_dwarf;
207 }
Enrico Granata99e5e222015-07-27 21:27:02 +0000208
Matthew Gardinere81df3b2014-08-26 06:57:23 +0000209 return colon_pos + 1;
210}
211
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000212static const char*
213resolveCompDir(const char* path_from_dwarf)
214{
215 if (!path_from_dwarf)
216 return nullptr;
217
218 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
219 // Remove the host part if present.
220 const char* local_path = removeHostnameFromPathname(path_from_dwarf);
221 if (!local_path)
222 return nullptr;
223
224 bool is_symlink = false;
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000225 FileSpec local_path_spec(local_path, false);
226 const auto& file_specs = GetGlobalPluginProperties()->GetSymLinkPaths();
227 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i)
228 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i), local_path_spec, true);
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000229
230 if (!is_symlink)
231 return local_path;
232
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +0000233 if (!local_path_spec.IsSymbolicLink())
234 return local_path;
235
236 FileSpec resolved_local_path_spec;
237 const auto error = FileSystem::Readlink(local_path_spec, resolved_local_path_spec);
238 if (error.Success())
239 return resolved_local_path_spec.GetCString();
240
241 return nullptr;
242}
243
244
Todd Fiala0a70a842014-05-28 16:43:26 +0000245#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
Greg Clayton1fba87112012-02-09 20:03:49 +0000246
247class DIEStack
248{
249public:
250
251 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
252 {
253 m_dies.push_back (DIEInfo(cu, die));
254 }
255
256
257 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
258 {
259 StreamString log_strm;
260 const size_t n = m_dies.size();
Daniel Malead01b2952012-11-29 21:49:15 +0000261 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
Greg Clayton1fba87112012-02-09 20:03:49 +0000262 for (size_t i=0; i<n; i++)
263 {
264 DWARFCompileUnit *cu = m_dies[i].cu;
265 const DWARFDebugInfoEntry *die = m_dies[i].die;
266 std::string qualified_name;
267 die->GetQualifiedName(dwarf, cu, qualified_name);
Daniel Malead01b2952012-11-29 21:49:15 +0000268 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n",
Greg Clayton43e0af02012-09-18 18:04:04 +0000269 (uint64_t)i,
Greg Clayton1fba87112012-02-09 20:03:49 +0000270 die->GetOffset(),
271 DW_TAG_value_to_name(die->Tag()),
272 qualified_name.c_str());
273 }
274 log->PutCString(log_strm.GetData());
275 }
276 void Pop ()
277 {
278 m_dies.pop_back();
279 }
280
281 class ScopedPopper
282 {
283 public:
284 ScopedPopper (DIEStack &die_stack) :
285 m_die_stack (die_stack),
286 m_valid (false)
287 {
288 }
289
290 void
291 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
292 {
293 m_valid = true;
294 m_die_stack.Push (cu, die);
295 }
296
297 ~ScopedPopper ()
298 {
299 if (m_valid)
300 m_die_stack.Pop();
301 }
302
303
304
305 protected:
306 DIEStack &m_die_stack;
307 bool m_valid;
308 };
309
310protected:
311 struct DIEInfo {
312 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
313 cu(c),
314 die(d)
315 {
316 }
317 DWARFCompileUnit *cu;
318 const DWARFDebugInfoEntry *die;
319 };
320 typedef std::vector<DIEInfo> Stack;
321 Stack m_dies;
322};
323#endif
324
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325void
326SymbolFileDWARF::Initialize()
327{
328 LogChannelDWARF::Initialize();
329 PluginManager::RegisterPlugin (GetPluginNameStatic(),
330 GetPluginDescriptionStatic(),
Oleksiy Vyalovabb5a352015-07-29 22:18:16 +0000331 CreateInstance,
332 DebuggerInitialize);
333}
334
335void
336SymbolFileDWARF::DebuggerInitialize(Debugger &debugger)
337{
338 if (!PluginManager::GetSettingForSymbolFilePlugin(debugger, PluginProperties::GetSettingName()))
339 {
340 const bool is_global_setting = true;
341 PluginManager::CreateSettingForSymbolFilePlugin(debugger,
342 GetGlobalPluginProperties()->GetValueProperties(),
343 ConstString ("Properties for the dwarf symbol-file plug-in."),
344 is_global_setting);
345 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346}
347
348void
349SymbolFileDWARF::Terminate()
350{
351 PluginManager::UnregisterPlugin (CreateInstance);
352 LogChannelDWARF::Initialize();
353}
354
355
Greg Clayton57abc5d2013-05-10 21:47:16 +0000356lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357SymbolFileDWARF::GetPluginNameStatic()
358{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000359 static ConstString g_name("dwarf");
360 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361}
362
363const char *
364SymbolFileDWARF::GetPluginDescriptionStatic()
365{
366 return "DWARF and DWARF3 debug symbol file reader.";
367}
368
369
370SymbolFile*
371SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
372{
373 return new SymbolFileDWARF(obj_file);
374}
375
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000376TypeList *
377SymbolFileDWARF::GetTypeList ()
378{
Greg Clayton1f746072012-08-29 21:13:06 +0000379 if (GetDebugMapSymfile ())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000380 return m_debug_map_symfile->GetTypeList();
381 return m_obj_file->GetModule()->GetTypeList();
382
383}
Greg Claytonf02500c2013-06-18 22:51:05 +0000384void
385SymbolFileDWARF::GetTypes (DWARFCompileUnit* cu,
386 const DWARFDebugInfoEntry *die,
387 dw_offset_t min_die_offset,
388 dw_offset_t max_die_offset,
389 uint32_t type_mask,
390 TypeSet &type_set)
391{
392 if (cu)
393 {
394 if (die)
395 {
396 const dw_offset_t die_offset = die->GetOffset();
397
398 if (die_offset >= max_die_offset)
399 return;
400
401 if (die_offset >= min_die_offset)
402 {
403 const dw_tag_t tag = die->Tag();
404
405 bool add_type = false;
406
407 switch (tag)
408 {
409 case DW_TAG_array_type: add_type = (type_mask & eTypeClassArray ) != 0; break;
410 case DW_TAG_unspecified_type:
411 case DW_TAG_base_type: add_type = (type_mask & eTypeClassBuiltin ) != 0; break;
412 case DW_TAG_class_type: add_type = (type_mask & eTypeClassClass ) != 0; break;
413 case DW_TAG_structure_type: add_type = (type_mask & eTypeClassStruct ) != 0; break;
414 case DW_TAG_union_type: add_type = (type_mask & eTypeClassUnion ) != 0; break;
415 case DW_TAG_enumeration_type: add_type = (type_mask & eTypeClassEnumeration ) != 0; break;
416 case DW_TAG_subroutine_type:
417 case DW_TAG_subprogram:
418 case DW_TAG_inlined_subroutine: add_type = (type_mask & eTypeClassFunction ) != 0; break;
419 case DW_TAG_pointer_type: add_type = (type_mask & eTypeClassPointer ) != 0; break;
420 case DW_TAG_rvalue_reference_type:
421 case DW_TAG_reference_type: add_type = (type_mask & eTypeClassReference ) != 0; break;
422 case DW_TAG_typedef: add_type = (type_mask & eTypeClassTypedef ) != 0; break;
423 case DW_TAG_ptr_to_member_type: add_type = (type_mask & eTypeClassMemberPointer ) != 0; break;
424 }
425
426 if (add_type)
427 {
428 const bool assert_not_being_parsed = true;
429 Type *type = ResolveTypeUID (cu, die, assert_not_being_parsed);
430 if (type)
431 {
432 if (type_set.find(type) == type_set.end())
433 type_set.insert(type);
434 }
435 }
436 }
437
438 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild();
439 child_die != NULL;
440 child_die = child_die->GetSibling())
441 {
442 GetTypes (cu, child_die, min_die_offset, max_die_offset, type_mask, type_set);
443 }
444 }
445 }
446}
447
448size_t
449SymbolFileDWARF::GetTypes (SymbolContextScope *sc_scope,
450 uint32_t type_mask,
451 TypeList &type_list)
452
453{
454 TypeSet type_set;
455
456 CompileUnit *comp_unit = NULL;
457 DWARFCompileUnit* dwarf_cu = NULL;
458 if (sc_scope)
459 comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
460
461 if (comp_unit)
462 {
463 dwarf_cu = GetDWARFCompileUnit(comp_unit);
464 if (dwarf_cu == 0)
465 return 0;
466 GetTypes (dwarf_cu,
467 dwarf_cu->DIE(),
468 dwarf_cu->GetOffset(),
469 dwarf_cu->GetNextCompileUnitOffset(),
470 type_mask,
471 type_set);
472 }
473 else
474 {
475 DWARFDebugInfo* info = DebugInfo();
476 if (info)
477 {
478 const size_t num_cus = info->GetNumCompileUnits();
479 for (size_t cu_idx=0; cu_idx<num_cus; ++cu_idx)
480 {
481 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
482 if (dwarf_cu)
483 {
484 GetTypes (dwarf_cu,
485 dwarf_cu->DIE(),
486 0,
487 UINT32_MAX,
488 type_mask,
489 type_set);
490 }
491 }
492 }
493 }
494// if (m_using_apple_tables)
495// {
496// DWARFMappedHash::MemoryTable *apple_types = m_apple_types_ap.get();
497// if (apple_types)
498// {
499// apple_types->ForEach([this, &type_set, apple_types, type_mask](const DWARFMappedHash::DIEInfoArray &die_info_array) -> bool {
500//
501// for (auto die_info: die_info_array)
502// {
503// bool add_type = TagMatchesTypeMask (type_mask, 0);
504// if (!add_type)
505// {
506// dw_tag_t tag = die_info.tag;
507// if (tag == 0)
508// {
509// const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_info.offset, NULL);
510// tag = die->Tag();
511// }
512// add_type = TagMatchesTypeMask (type_mask, tag);
513// }
514// if (add_type)
515// {
516// Type *type = ResolveTypeUID(die_info.offset);
517//
518// if (type_set.find(type) == type_set.end())
519// type_set.insert(type);
520// }
521// }
522// return true; // Keep iterating
523// });
524// }
525// }
526// else
527// {
528// if (!m_indexed)
529// Index ();
530//
531// m_type_index.ForEach([this, &type_set, type_mask](const char *name, uint32_t die_offset) -> bool {
532//
533// bool add_type = TagMatchesTypeMask (type_mask, 0);
534//
535// if (!add_type)
536// {
537// const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_offset, NULL);
538// if (die)
539// {
540// const dw_tag_t tag = die->Tag();
541// add_type = TagMatchesTypeMask (type_mask, tag);
542// }
543// }
544//
545// if (add_type)
546// {
547// Type *type = ResolveTypeUID(die_offset);
548//
549// if (type_set.find(type) == type_set.end())
550// type_set.insert(type);
551// }
552// return true; // Keep iterating
553// });
554// }
555
Greg Clayton57ee3062013-07-11 22:46:58 +0000556 std::set<ClangASTType> clang_type_set;
Greg Claytonf02500c2013-06-18 22:51:05 +0000557 size_t num_types_added = 0;
558 for (Type *type : type_set)
559 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000560 ClangASTType clang_type = type->GetClangForwardType();
Greg Clayton0fc4f312013-06-20 01:23:18 +0000561 if (clang_type_set.find(clang_type) == clang_type_set.end())
562 {
563 clang_type_set.insert(clang_type);
564 type_list.Insert (type->shared_from_this());
565 ++num_types_added;
566 }
Greg Claytonf02500c2013-06-18 22:51:05 +0000567 }
568 return num_types_added;
569}
570
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000571
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572//----------------------------------------------------------------------
573// Gets the first parent that is a lexical block, function or inlined
574// subroutine, or compile unit.
575//----------------------------------------------------------------------
576static const DWARFDebugInfoEntry *
577GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
578{
579 const DWARFDebugInfoEntry *die;
580 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
581 {
582 dw_tag_t tag = die->Tag();
583
584 switch (tag)
585 {
586 case DW_TAG_compile_unit:
587 case DW_TAG_subprogram:
588 case DW_TAG_inlined_subroutine:
589 case DW_TAG_lexical_block:
590 return die;
591 }
592 }
593 return NULL;
594}
595
596
Greg Clayton450e3f32010-10-12 02:24:53 +0000597SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
598 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000599 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton1f746072012-08-29 21:13:06 +0000600 m_debug_map_module_wp (),
Greg Clayton450e3f32010-10-12 02:24:53 +0000601 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000602 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000604 m_data_debug_abbrev (),
605 m_data_debug_aranges (),
606 m_data_debug_frame (),
607 m_data_debug_info (),
608 m_data_debug_line (),
609 m_data_debug_loc (),
610 m_data_debug_ranges (),
611 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000612 m_data_apple_names (),
613 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000614 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 m_info(),
617 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000618 m_apple_names_ap (),
619 m_apple_types_ap (),
620 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000621 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000622 m_function_basename_index(),
623 m_function_fullname_index(),
624 m_function_method_index(),
625 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000626 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000627 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000628 m_type_index(),
629 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000630 m_indexed (false),
631 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000632 m_using_apple_tables (false),
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000633 m_fetched_external_modules (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000634 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000635 m_ranges(),
636 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637{
638}
639
640SymbolFileDWARF::~SymbolFileDWARF()
641{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000642 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000643 {
644 ModuleSP module_sp (m_obj_file->GetModule());
645 if (module_sp)
646 module_sp->GetClangASTContext().RemoveExternalSource ();
647 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000648}
649
650static const ConstString &
651GetDWARFMachOSegmentName ()
652{
653 static ConstString g_dwarf_section_name ("__DWARF");
654 return g_dwarf_section_name;
655}
656
Greg Claytone576ab22011-02-15 00:19:15 +0000657UniqueDWARFASTTypeMap &
658SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
659{
Greg Clayton1f746072012-08-29 21:13:06 +0000660 if (GetDebugMapSymfile ())
Greg Claytone576ab22011-02-15 00:19:15 +0000661 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
662 return m_unique_ast_type_map;
663}
664
Greg Clayton6beaaa62011-01-17 03:46:26 +0000665ClangASTContext &
666SymbolFileDWARF::GetClangASTContext ()
667{
Greg Clayton1f746072012-08-29 21:13:06 +0000668 if (GetDebugMapSymfile ())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000669 return m_debug_map_symfile->GetClangASTContext ();
670
671 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
672 if (!m_is_external_ast_source)
673 {
674 m_is_external_ast_source = true;
Todd Fiala955fe6f2014-02-27 17:18:23 +0000675 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (
Greg Clayton6beaaa62011-01-17 03:46:26 +0000676 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
677 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000678 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000679 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000680 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000681 ast.SetExternalSource (ast_source_ap);
682 }
683 return ast;
684}
685
686void
687SymbolFileDWARF::InitializeObject()
688{
689 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000690 ModuleSP module_sp (m_obj_file->GetModule());
691 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000692 {
Greg Clayton3046e662013-07-10 01:23:25 +0000693 const SectionList *section_list = module_sp->GetSectionList();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000694
695 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
696
697 // Memory map the DWARF mach-o segment so we have everything mmap'ed
698 // to keep our heap memory usage down.
699 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000700 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000701 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000702 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000703 if (m_data_apple_names.GetByteSize() > 0)
704 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000705 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
706 if (m_apple_names_ap->IsValid())
707 m_using_apple_tables = true;
708 else
Greg Clayton7f995132011-10-04 22:41:51 +0000709 m_apple_names_ap.reset();
710 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000711 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000712 if (m_data_apple_types.GetByteSize() > 0)
713 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000714 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
715 if (m_apple_types_ap->IsValid())
716 m_using_apple_tables = true;
717 else
Greg Clayton7f995132011-10-04 22:41:51 +0000718 m_apple_types_ap.reset();
719 }
720
721 get_apple_namespaces_data();
722 if (m_data_apple_namespaces.GetByteSize() > 0)
723 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000724 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
725 if (m_apple_namespaces_ap->IsValid())
726 m_using_apple_tables = true;
727 else
Greg Clayton7f995132011-10-04 22:41:51 +0000728 m_apple_namespaces_ap.reset();
729 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000730
Greg Clayton5009f9d2011-10-27 17:55:14 +0000731 get_apple_objc_data();
732 if (m_data_apple_objc.GetByteSize() > 0)
733 {
734 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
735 if (m_apple_objc_ap->IsValid())
736 m_using_apple_tables = true;
737 else
738 m_apple_objc_ap.reset();
739 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740}
741
742bool
743SymbolFileDWARF::SupportedVersion(uint16_t version)
744{
Greg Claytonabcbfe52013-04-04 00:00:36 +0000745 return version == 2 || version == 3 || version == 4;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746}
747
748uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000749SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750{
751 uint32_t abilities = 0;
752 if (m_obj_file != NULL)
753 {
754 const Section* section = NULL;
755 const SectionList *section_list = m_obj_file->GetSectionList();
756 if (section_list == NULL)
757 return 0;
758
759 uint64_t debug_abbrev_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760 uint64_t debug_info_file_size = 0;
761 uint64_t debug_line_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762
Greg Clayton6beaaa62011-01-17 03:46:26 +0000763 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764
765 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000766 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767
Greg Clayton4ceb9982010-07-21 22:54:26 +0000768 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769 if (section != NULL)
770 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000771 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772
Greg Clayton4ceb9982010-07-21 22:54:26 +0000773 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000775 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776 else
777 m_flags.Set (flagsGotDebugAbbrevData);
778
Greg Clayton4ceb9982010-07-21 22:54:26 +0000779 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000780 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781 m_flags.Set (flagsGotDebugArangesData);
782
Greg Clayton4ceb9982010-07-21 22:54:26 +0000783 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000784 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 m_flags.Set (flagsGotDebugFrameData);
786
Greg Clayton4ceb9982010-07-21 22:54:26 +0000787 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000789 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 else
791 m_flags.Set (flagsGotDebugLineData);
792
Greg Clayton4ceb9982010-07-21 22:54:26 +0000793 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000794 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 m_flags.Set (flagsGotDebugLocData);
796
Greg Clayton4ceb9982010-07-21 22:54:26 +0000797 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000798 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 m_flags.Set (flagsGotDebugMacInfoData);
800
Greg Clayton4ceb9982010-07-21 22:54:26 +0000801 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000802 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 m_flags.Set (flagsGotDebugPubNamesData);
804
Greg Clayton4ceb9982010-07-21 22:54:26 +0000805 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000806 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 m_flags.Set (flagsGotDebugPubTypesData);
808
Greg Clayton4ceb9982010-07-21 22:54:26 +0000809 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000810 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 m_flags.Set (flagsGotDebugRangesData);
812
Greg Clayton4ceb9982010-07-21 22:54:26 +0000813 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000814 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815 m_flags.Set (flagsGotDebugStrData);
816 }
Greg Clayton6c596612012-05-18 21:47:20 +0000817 else
818 {
819 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString();
820 if (symfile_dir_cstr)
821 {
822 if (strcasestr(symfile_dir_cstr, ".dsym"))
823 {
824 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo)
825 {
826 // We have a dSYM file that didn't have a any debug info.
827 // If the string table has a size of 1, then it was made from
828 // an executable with no debug info, or from an executable that
829 // was stripped.
830 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
831 if (section && section->GetFileSize() == 1)
832 {
833 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info.");
834 }
835 }
836 }
837 }
838 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839
840 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
841 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
842
843 if (debug_line_file_size > 0)
844 abilities |= LineTables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000845 }
846 return abilities;
847}
848
Ed Masteeeae7212013-10-24 20:43:47 +0000849const DWARFDataExtractor&
850SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DWARFDataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851{
852 if (m_flags.IsClear (got_flag))
853 {
Michael Sartaina7499c92013-07-01 19:45:50 +0000854 ModuleSP module_sp (m_obj_file->GetModule());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 m_flags.Set (got_flag);
Greg Clayton3046e662013-07-10 01:23:25 +0000856 const SectionList *section_list = module_sp->GetSectionList();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857 if (section_list)
858 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000859 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
860 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 {
862 // See if we memory mapped the DWARF segment?
863 if (m_dwarf_data.GetByteSize())
864 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000865 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 }
867 else
868 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000869 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870 data.Clear();
871 }
872 }
873 }
874 }
875 return data;
876}
877
Ed Masteeeae7212013-10-24 20:43:47 +0000878const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879SymbolFileDWARF::get_debug_abbrev_data()
880{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000881 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882}
883
Ed Masteeeae7212013-10-24 20:43:47 +0000884const DWARFDataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000885SymbolFileDWARF::get_debug_aranges_data()
886{
887 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
888}
889
Ed Masteeeae7212013-10-24 20:43:47 +0000890const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891SymbolFileDWARF::get_debug_frame_data()
892{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000893 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000894}
895
Ed Masteeeae7212013-10-24 20:43:47 +0000896const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897SymbolFileDWARF::get_debug_info_data()
898{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000899 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900}
901
Ed Masteeeae7212013-10-24 20:43:47 +0000902const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903SymbolFileDWARF::get_debug_line_data()
904{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000905 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000906}
907
Ed Masteeeae7212013-10-24 20:43:47 +0000908const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909SymbolFileDWARF::get_debug_loc_data()
910{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000911 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912}
913
Ed Masteeeae7212013-10-24 20:43:47 +0000914const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915SymbolFileDWARF::get_debug_ranges_data()
916{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000917 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918}
919
Ed Masteeeae7212013-10-24 20:43:47 +0000920const DWARFDataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000921SymbolFileDWARF::get_debug_str_data()
922{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000923 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924}
925
Ed Masteeeae7212013-10-24 20:43:47 +0000926const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000927SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000928{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000929 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000930}
931
Ed Masteeeae7212013-10-24 20:43:47 +0000932const DWARFDataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000933SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000934{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000935 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000936}
937
Ed Masteeeae7212013-10-24 20:43:47 +0000938const DWARFDataExtractor&
Greg Clayton7f995132011-10-04 22:41:51 +0000939SymbolFileDWARF::get_apple_namespaces_data()
940{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000941 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
942}
943
Ed Masteeeae7212013-10-24 20:43:47 +0000944const DWARFDataExtractor&
Greg Clayton5009f9d2011-10-27 17:55:14 +0000945SymbolFileDWARF::get_apple_objc_data()
946{
947 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000948}
949
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950
951DWARFDebugAbbrev*
952SymbolFileDWARF::DebugAbbrev()
953{
954 if (m_abbr.get() == NULL)
955 {
Ed Masteeeae7212013-10-24 20:43:47 +0000956 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000957 if (debug_abbrev_data.GetByteSize() > 0)
958 {
959 m_abbr.reset(new DWARFDebugAbbrev());
960 if (m_abbr.get())
961 m_abbr->Parse(debug_abbrev_data);
962 }
963 }
964 return m_abbr.get();
965}
966
967const DWARFDebugAbbrev*
968SymbolFileDWARF::DebugAbbrev() const
969{
970 return m_abbr.get();
971}
972
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973
974DWARFDebugInfo*
975SymbolFileDWARF::DebugInfo()
976{
977 if (m_info.get() == NULL)
978 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000979 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
980 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 if (get_debug_info_data().GetByteSize() > 0)
982 {
983 m_info.reset(new DWARFDebugInfo());
984 if (m_info.get())
985 {
986 m_info->SetDwarfData(this);
987 }
988 }
989 }
990 return m_info.get();
991}
992
993const DWARFDebugInfo*
994SymbolFileDWARF::DebugInfo() const
995{
996 return m_info.get();
997}
998
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000999DWARFCompileUnit*
Greg Clayton1f746072012-08-29 21:13:06 +00001000SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001001{
1002 DWARFDebugInfo* info = DebugInfo();
Greg Clayton1f746072012-08-29 21:13:06 +00001003 if (info)
1004 {
1005 if (GetDebugMapSymfile ())
1006 {
1007 // The debug map symbol file made the compile units for this DWARF
1008 // file which is .o file with DWARF in it, and we should have
1009 // only 1 compile unit which is at offset zero in the DWARF.
1010 // TODO: modify to support LTO .o files where each .o file might
1011 // have multiple DW_TAG_compile_unit tags.
Greg Clayton68c00bd2015-02-05 02:10:29 +00001012
1013 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(0).get();
1014 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
1015 dwarf_cu->SetUserData(comp_unit);
1016 return dwarf_cu;
Greg Clayton1f746072012-08-29 21:13:06 +00001017 }
1018 else
1019 {
1020 // Just a normal DWARF file whose user ID for the compile unit is
1021 // the DWARF offset itself
Greg Clayton68c00bd2015-02-05 02:10:29 +00001022
1023 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit((dw_offset_t)comp_unit->GetID()).get();
1024 if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
1025 dwarf_cu->SetUserData(comp_unit);
1026 return dwarf_cu;
1027
Greg Clayton1f746072012-08-29 21:13:06 +00001028 }
1029 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030 return NULL;
1031}
1032
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001033
1034DWARFDebugRanges*
1035SymbolFileDWARF::DebugRanges()
1036{
1037 if (m_ranges.get() == NULL)
1038 {
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001039 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
1040 __PRETTY_FUNCTION__, static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 if (get_debug_ranges_data().GetByteSize() > 0)
1042 {
1043 m_ranges.reset(new DWARFDebugRanges());
1044 if (m_ranges.get())
1045 m_ranges->Extract(this);
1046 }
1047 }
1048 return m_ranges.get();
1049}
1050
1051const DWARFDebugRanges*
1052SymbolFileDWARF::DebugRanges() const
1053{
1054 return m_ranges.get();
1055}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001056
Greg Clayton53eb1c22012-04-02 22:59:12 +00001057lldb::CompUnitSP
1058SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059{
Greg Clayton53eb1c22012-04-02 22:59:12 +00001060 CompUnitSP cu_sp;
1061 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001063 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
1064 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001066 // We already parsed this compile unit, had out a shared pointer to it
1067 cu_sp = comp_unit->shared_from_this();
1068 }
1069 else
1070 {
Greg Clayton1f746072012-08-29 21:13:06 +00001071 if (GetDebugMapSymfile ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 {
Greg Clayton1f746072012-08-29 21:13:06 +00001073 // Let the debug map create the compile unit
1074 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
1075 dwarf_cu->SetUserData(cu_sp.get());
1076 }
1077 else
1078 {
1079 ModuleSP module_sp (m_obj_file->GetModule());
1080 if (module_sp)
Greg Clayton53eb1c22012-04-02 22:59:12 +00001081 {
Greg Clayton1f746072012-08-29 21:13:06 +00001082 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
1083 if (cu_die)
Greg Clayton53eb1c22012-04-02 22:59:12 +00001084 {
Chaoren Lin0c5a9c12015-06-05 00:28:06 +00001085 FileSpec cu_file_spec{cu_die->GetName(this, dwarf_cu), false};
1086 if (cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +00001087 {
Chaoren Lin0c5a9c12015-06-05 00:28:06 +00001088 // If we have a full path to the compile unit, we don't need to resolve
1089 // the file. This can be expensive e.g. when the source files are NFS mounted.
Chaoren Lin372e9062015-06-09 17:54:27 +00001090 if (cu_file_spec.IsRelative())
Greg Clayton53eb1c22012-04-02 22:59:12 +00001091 {
Chaoren Lin0c5a9c12015-06-05 00:28:06 +00001092 const char *cu_comp_dir{cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, nullptr)};
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +00001093 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir));
Greg Clayton1f746072012-08-29 21:13:06 +00001094 }
1095
Chaoren Lin0c5a9c12015-06-05 00:28:06 +00001096 std::string remapped_file;
1097 if (module_sp->RemapSourceFile(cu_file_spec.GetCString(), remapped_file))
1098 cu_file_spec.SetFile(remapped_file, false);
David Srbeckyd515e942015-07-08 14:00:04 +00001099 }
Chaoren Lin0c5a9c12015-06-05 00:28:06 +00001100
David Srbeckyd515e942015-07-08 14:00:04 +00001101 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0));
Chaoren Lin0c5a9c12015-06-05 00:28:06 +00001102
Jason Molenda6ab659a2015-07-29 00:42:47 +00001103 bool is_optimized = false;
1104 if (cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_APPLE_optimized, 0) == 1)
1105 {
1106 is_optimized = true;
1107 }
David Srbeckyd515e942015-07-08 14:00:04 +00001108 cu_sp.reset(new CompileUnit (module_sp,
1109 dwarf_cu,
1110 cu_file_spec,
1111 MakeUserID(dwarf_cu->GetOffset()),
Jason Molenda6ab659a2015-07-29 00:42:47 +00001112 cu_language,
1113 is_optimized));
David Srbeckyd515e942015-07-08 14:00:04 +00001114 if (cu_sp)
1115 {
1116 // If we just created a compile unit with an invalid file spec, try and get the
1117 // first entry in the supports files from the line table as that should be the
1118 // compile unit.
1119 if (!cu_file_spec)
Greg Clayton1f746072012-08-29 21:13:06 +00001120 {
David Srbeckyd515e942015-07-08 14:00:04 +00001121 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
1122 if (cu_file_spec)
1123 {
1124 (FileSpec &)(*cu_sp) = cu_file_spec;
1125 // Also fix the invalid file spec which was copied from the compile unit.
1126 cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
1127 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001128 }
David Srbeckyd515e942015-07-08 14:00:04 +00001129
1130 dwarf_cu->SetUserData(cu_sp.get());
1131
1132 // Figure out the compile unit index if we weren't given one
1133 if (cu_idx == UINT32_MAX)
1134 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
1135
1136 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
Greg Clayton53eb1c22012-04-02 22:59:12 +00001137 }
1138 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 }
1140 }
1141 }
1142 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001143 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144}
1145
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146uint32_t
1147SymbolFileDWARF::GetNumCompileUnits()
1148{
1149 DWARFDebugInfo* info = DebugInfo();
1150 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152 return 0;
1153}
1154
1155CompUnitSP
1156SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
1157{
Greg Clayton53eb1c22012-04-02 22:59:12 +00001158 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159 DWARFDebugInfo* info = DebugInfo();
1160 if (info)
1161 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00001162 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
1163 if (dwarf_cu)
1164 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00001166 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167}
1168
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169Function *
Greg Clayton0fffff52010-09-24 05:15:53 +00001170SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171{
1172 DWARFDebugRanges::RangeList func_ranges;
1173 const char *name = NULL;
1174 const char *mangled = NULL;
1175 int decl_file = 0;
1176 int decl_line = 0;
1177 int decl_column = 0;
1178 int call_file = 0;
1179 int call_line = 0;
1180 int call_column = 0;
1181 DWARFExpression frame_base;
1182
Greg Claytonc93237c2010-10-01 20:48:32 +00001183 assert (die->Tag() == DW_TAG_subprogram);
1184
1185 if (die->Tag() != DW_TAG_subprogram)
1186 return NULL;
1187
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001188 if (die->GetDIENamesAndRanges (this,
1189 dwarf_cu,
1190 name,
1191 mangled,
1192 func_ranges,
1193 decl_file,
1194 decl_line,
1195 decl_column,
1196 call_file,
1197 call_line,
1198 call_column,
1199 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200 {
1201 // Union of all ranges in the function DIE (if the function is discontiguous)
1202 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +00001203 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
1204 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
1206 {
Michael Sartaina7499c92013-07-01 19:45:50 +00001207 ModuleSP module_sp (m_obj_file->GetModule());
Greg Clayton3046e662013-07-10 01:23:25 +00001208 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, module_sp->GetSectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209 if (func_range.GetBaseAddress().IsValid())
1210 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
1211 }
1212
1213 if (func_range.GetBaseAddress().IsValid())
1214 {
1215 Mangled func_name;
1216 if (mangled)
Greg Clayton037520e2012-07-18 23:18:10 +00001217 func_name.SetValue(ConstString(mangled), true);
Siva Chandra462722d2015-03-27 00:10:04 +00001218 else if (die->GetParent()->Tag() == DW_TAG_compile_unit &&
1219 LanguageRuntime::LanguageIsCPlusPlus(dwarf_cu->GetLanguageType()) &&
Greg Clayton94380562015-05-15 22:20:29 +00001220 name && strcmp(name, "main") != 0)
Siva Chandra462722d2015-03-27 00:10:04 +00001221 {
1222 // If the mangled name is not present in the DWARF, generate the demangled name
1223 // using the decl context. We skip if the function is "main" as its name is
1224 // never mangled.
1225 bool is_static = false;
1226 bool is_variadic = false;
1227 unsigned type_quals = 0;
1228 std::vector<ClangASTType> param_types;
1229 std::vector<clang::ParmVarDecl*> param_decls;
1230 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
1231 DWARFDeclContext decl_ctx;
1232 StreamString sstr;
1233
1234 die->GetDWARFDeclContext(this, dwarf_cu, decl_ctx);
1235 sstr << decl_ctx.GetQualifiedName();
1236
1237 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE(dwarf_cu,
1238 die,
1239 &decl_ctx_die);
1240 ParseChildParameters(sc,
1241 containing_decl_ctx,
1242 dwarf_cu,
1243 die,
1244 true,
1245 is_static,
1246 is_variadic,
1247 param_types,
1248 param_decls,
1249 type_quals);
1250 sstr << "(";
1251 for (size_t i = 0; i < param_types.size(); i++)
1252 {
1253 if (i > 0)
1254 sstr << ", ";
1255 sstr << param_types[i].GetTypeName();
1256 }
1257 if (is_variadic)
1258 sstr << ", ...";
1259 sstr << ")";
1260 if (type_quals & clang::Qualifiers::Const)
1261 sstr << " const";
1262
1263 func_name.SetValue(ConstString(sstr.GetData()), false);
1264 }
1265 else
Greg Clayton037520e2012-07-18 23:18:10 +00001266 func_name.SetValue(ConstString(name), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267
1268 FunctionSP func_sp;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001269 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +00001271 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1272 decl_line,
1273 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274
Greg Clayton0bd4e1b2011-09-30 20:52:25 +00001275 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +00001276 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001277
1278 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
1279
Greg Clayton9422dd62013-03-04 21:46:16 +00001280 if (FixupAddress (func_range.GetBaseAddress()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001282 const user_id_t func_user_id = MakeUserID(die->GetOffset());
1283 func_sp.reset(new Function (sc.comp_unit,
1284 MakeUserID(func_user_id), // UserID is the DIE offset
1285 MakeUserID(func_user_id),
1286 func_name,
1287 func_type,
1288 func_range)); // first address range
1289
1290 if (func_sp.get() != NULL)
1291 {
1292 if (frame_base.IsValid())
1293 func_sp->GetFrameBaseExpression() = frame_base;
1294 sc.comp_unit->AddFunction(func_sp);
1295 return func_sp.get();
1296 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297 }
1298 }
1299 }
1300 return NULL;
1301}
1302
Greg Clayton9422dd62013-03-04 21:46:16 +00001303bool
1304SymbolFileDWARF::FixupAddress (Address &addr)
1305{
1306 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile ();
1307 if (debug_map_symfile)
1308 {
1309 return debug_map_symfile->LinkOSOAddress(addr);
1310 }
1311 // This is a normal DWARF file, no address fixups need to happen
1312 return true;
1313}
Greg Clayton1f746072012-08-29 21:13:06 +00001314lldb::LanguageType
1315SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
1316{
1317 assert (sc.comp_unit);
1318 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1319 if (dwarf_cu)
1320 {
1321 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly();
Jim Ingham28eb5712012-10-12 17:34:26 +00001322 if (die)
Dawn Perchikd0e87eb2015-06-17 22:30:24 +00001323 return DWARFCompileUnit::LanguageTypeFromDWARF(die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0));
Greg Clayton1f746072012-08-29 21:13:06 +00001324 }
1325 return eLanguageTypeUnknown;
1326}
1327
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328size_t
1329SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
1330{
1331 assert (sc.comp_unit);
1332 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00001333 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 if (dwarf_cu)
1335 {
1336 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +00001337 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +00001339 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 {
1341 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +00001342 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343 {
1344 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
1345 ++functions_added;
1346 }
1347 }
1348 //FixupTypes();
1349 }
1350 return functions_added;
1351}
1352
1353bool
1354SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
1355{
1356 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +00001357 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Claytonda2455b2012-11-01 17:28:37 +00001358 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359 {
Greg Claytonda2455b2012-11-01 17:28:37 +00001360 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361
Greg Claytonda2455b2012-11-01 17:28:37 +00001362 if (cu_die)
1363 {
Oleksiy Vyalov5d9c50b2015-07-21 02:09:42 +00001364 const char * cu_comp_dir = resolveCompDir(cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, nullptr));
Matthew Gardinere81df3b2014-08-26 06:57:23 +00001365
Greg Claytonda2455b2012-11-01 17:28:37 +00001366 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367
Greg Claytonda2455b2012-11-01 17:28:37 +00001368 // All file indexes in DWARF are one based and a file of index zero is
1369 // supposed to be the compile unit itself.
1370 support_files.Append (*sc.comp_unit);
1371
1372 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
1373 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 }
1375 return false;
1376}
1377
Sean Callananf0c5aeb2015-04-20 16:31:29 +00001378bool
1379SymbolFileDWARF::ParseImportedModules (const lldb_private::SymbolContext &sc, std::vector<lldb_private::ConstString> &imported_modules)
1380{
1381 assert (sc.comp_unit);
1382 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
1383 if (dwarf_cu)
1384 {
1385 if (ClangModulesDeclVendor::LanguageSupportsClangModules(sc.comp_unit->GetLanguage()))
1386 {
1387 UpdateExternalModuleListIfNeeded();
1388 for (const std::pair<uint64_t, const ClangModuleInfo> &external_type_module : m_external_type_modules)
1389 {
1390 imported_modules.push_back(external_type_module.second.m_name);
1391 }
1392 }
1393 }
1394 return false;
1395}
1396
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397struct ParseDWARFLineTableCallbackInfo
1398{
1399 LineTable* line_table;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001400 std::unique_ptr<LineSequence> sequence_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401};
1402
1403//----------------------------------------------------------------------
1404// ParseStatementTableCallback
1405//----------------------------------------------------------------------
1406static void
1407ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
1408{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1410 {
1411 // Just started parsing the line table
1412 }
1413 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1414 {
1415 // Done parsing line table, nothing to do for the cleanup
1416 }
1417 else
1418 {
1419 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
Greg Clayton9422dd62013-03-04 21:46:16 +00001420 LineTable* line_table = info->line_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421
Greg Clayton9422dd62013-03-04 21:46:16 +00001422 // If this is our first time here, we need to create a
1423 // sequence container.
1424 if (!info->sequence_ap.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001425 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001426 info->sequence_ap.reset(line_table->CreateLineSequenceContainer());
1427 assert(info->sequence_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001429 line_table->AppendLineEntryToSequence (info->sequence_ap.get(),
1430 state.address,
1431 state.line,
1432 state.column,
1433 state.file,
1434 state.is_stmt,
1435 state.basic_block,
1436 state.prologue_end,
1437 state.epilogue_begin,
1438 state.end_sequence);
1439 if (state.end_sequence)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001441 // First, put the current sequence into the line table.
1442 line_table->InsertSequence(info->sequence_ap.get());
1443 // Then, empty it to prepare for the next sequence.
1444 info->sequence_ap->Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001445 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001446 }
1447}
1448
1449bool
1450SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1451{
1452 assert (sc.comp_unit);
1453 if (sc.comp_unit->GetLineTable() != NULL)
1454 return true;
1455
Greg Clayton1f746072012-08-29 21:13:06 +00001456 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457 if (dwarf_cu)
1458 {
1459 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001460 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001462 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1463 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001465 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
Greg Clayton129d12c2011-11-28 03:29:03 +00001466 if (line_table_ap.get())
1467 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001468 ParseDWARFLineTableCallbackInfo info;
1469 info.line_table = line_table_ap.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00001470 lldb::offset_t offset = cu_line_offset;
Greg Clayton129d12c2011-11-28 03:29:03 +00001471 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
Greg Clayton9422dd62013-03-04 21:46:16 +00001472 if (m_debug_map_symfile)
1473 {
1474 // We have an object file that has a line table with addresses
1475 // that are not linked. We need to link the line table and convert
1476 // the addresses that are relative to the .o file into addresses
1477 // for the main executable.
1478 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get()));
1479 }
1480 else
1481 {
1482 sc.comp_unit->SetLineTable(line_table_ap.release());
1483 return true;
1484 }
Greg Clayton129d12c2011-11-28 03:29:03 +00001485 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001486 }
1487 }
1488 }
1489 return false;
1490}
1491
1492size_t
1493SymbolFileDWARF::ParseFunctionBlocks
1494(
1495 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001496 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001497 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001498 const DWARFDebugInfoEntry *die,
1499 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001500 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001501)
1502{
1503 size_t blocks_added = 0;
1504 while (die != NULL)
1505 {
1506 dw_tag_t tag = die->Tag();
1507
1508 switch (tag)
1509 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001511 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512 case DW_TAG_lexical_block:
1513 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001514 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001515 if (tag == DW_TAG_subprogram)
1516 {
1517 // Skip any DW_TAG_subprogram DIEs that are inside
1518 // of a normal or inlined functions. These will be
1519 // parsed on their own as separate entities.
1520
1521 if (depth > 0)
1522 break;
1523
1524 block = parent_block;
1525 }
1526 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001527 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001528 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001529 parent_block->AddChild(block_sp);
1530 block = block_sp.get();
1531 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001532 DWARFDebugRanges::RangeList ranges;
1533 const char *name = NULL;
1534 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001536 int decl_file = 0;
1537 int decl_line = 0;
1538 int decl_column = 0;
1539 int call_file = 0;
1540 int call_line = 0;
1541 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001542 if (die->GetDIENamesAndRanges (this,
1543 dwarf_cu,
1544 name,
1545 mangled_name,
1546 ranges,
1547 decl_file, decl_line, decl_column,
1548 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001549 {
1550 if (tag == DW_TAG_subprogram)
1551 {
1552 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001553 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001555 else if (tag == DW_TAG_inlined_subroutine)
1556 {
1557 // We get called here for inlined subroutines in two ways.
1558 // The first time is when we are making the Function object
1559 // for this inlined concrete instance. Since we're creating a top level block at
1560 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1561 // adjust the containing address.
1562 // The second time is when we are parsing the blocks inside the function that contains
1563 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1564 // function the offset will be for that function.
1565 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1566 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001567 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001568 }
1569 }
Greg Clayton103f3092015-01-15 03:04:37 +00001570
1571 const size_t num_ranges = ranges.GetSize();
1572 for (size_t i = 0; i<num_ranges; ++i)
1573 {
1574 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
1575 const addr_t range_base = range.GetRangeBase();
1576 if (range_base >= subprogram_low_pc)
1577 block->AddRange(Block::Range (range_base - subprogram_low_pc, range.GetByteSize()));
1578 else
1579 {
1580 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64 ") which has a base that is less than the function's low PC 0x%" PRIx64 ". Please file a bug and attach the file at the start of this error message",
1581 block->GetID(),
1582 range_base,
1583 range.GetRangeEnd(),
1584 subprogram_low_pc);
1585 }
1586 }
1587 block->FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001588
1589 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1590 {
Greg Clayton7b0992d2013-04-18 22:45:39 +00001591 std::unique_ptr<Declaration> decl_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001593 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1594 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001595
Greg Clayton7b0992d2013-04-18 22:45:39 +00001596 std::unique_ptr<Declaration> call_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001597 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001598 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1599 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001600
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001601 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001602 }
1603
1604 ++blocks_added;
1605
Greg Claytondd7feaf2011-08-12 17:54:33 +00001606 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001607 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001608 blocks_added += ParseFunctionBlocks (sc,
1609 block,
1610 dwarf_cu,
1611 die->GetFirstChild(),
1612 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001613 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001614 }
1615 }
1616 }
1617 break;
1618 default:
1619 break;
1620 }
1621
Greg Claytondd7feaf2011-08-12 17:54:33 +00001622 // Only parse siblings of the block if we are not at depth zero. A depth
1623 // of zero indicates we are currently parsing the top level
1624 // DW_TAG_subprogram DIE
1625
1626 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001627 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001628 else
1629 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630 }
1631 return blocks_added;
1632}
1633
Greg Claytonf0705c82011-10-22 03:33:13 +00001634bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001635SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1636 const DWARFDebugInfoEntry *die,
1637 ClangASTContext::TemplateParameterInfos &template_param_infos)
1638{
1639 const dw_tag_t tag = die->Tag();
1640
1641 switch (tag)
1642 {
1643 case DW_TAG_template_type_parameter:
1644 case DW_TAG_template_value_parameter:
1645 {
Todd Fialaee8bfc62014-09-11 17:29:12 +00001646 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001647
1648 DWARFDebugInfoEntry::Attributes attributes;
1649 const size_t num_attributes = die->GetAttributes (this,
1650 dwarf_cu,
1651 fixed_form_sizes,
1652 attributes);
1653 const char *name = NULL;
1654 Type *lldb_type = NULL;
Greg Clayton57ee3062013-07-11 22:46:58 +00001655 ClangASTType clang_type;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001656 uint64_t uval64 = 0;
1657 bool uval64_valid = false;
1658 if (num_attributes > 0)
1659 {
1660 DWARFFormValue form_value;
1661 for (size_t i=0; i<num_attributes; ++i)
1662 {
1663 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1664
1665 switch (attr)
1666 {
1667 case DW_AT_name:
1668 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1669 name = form_value.AsCString(&get_debug_str_data());
1670 break;
1671
1672 case DW_AT_type:
1673 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1674 {
Greg Clayton54166af2014-11-22 01:58:59 +00001675 const dw_offset_t type_die_offset = form_value.Reference();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001676 lldb_type = ResolveTypeUID(type_die_offset);
1677 if (lldb_type)
1678 clang_type = lldb_type->GetClangForwardType();
1679 }
1680 break;
1681
1682 case DW_AT_const_value:
1683 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1684 {
1685 uval64_valid = true;
1686 uval64 = form_value.Unsigned();
1687 }
1688 break;
1689 default:
1690 break;
1691 }
1692 }
1693
Greg Clayton283b2652013-04-23 22:38:02 +00001694 clang::ASTContext *ast = GetClangASTContext().getASTContext();
1695 if (!clang_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00001696 clang_type = GetClangASTContext().GetBasicType(eBasicTypeVoid);
Greg Clayton283b2652013-04-23 22:38:02 +00001697
1698 if (clang_type)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001699 {
1700 bool is_signed = false;
Greg Clayton283b2652013-04-23 22:38:02 +00001701 if (name && name[0])
1702 template_param_infos.names.push_back(name);
1703 else
1704 template_param_infos.names.push_back(NULL);
1705
Greg Clayton283b2652013-04-23 22:38:02 +00001706 if (tag == DW_TAG_template_value_parameter &&
1707 lldb_type != NULL &&
Greg Clayton57ee3062013-07-11 22:46:58 +00001708 clang_type.IsIntegerType (is_signed) &&
Greg Clayton283b2652013-04-23 22:38:02 +00001709 uval64_valid)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001710 {
1711 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
Greg Clayton283b2652013-04-23 22:38:02 +00001712 template_param_infos.args.push_back (clang::TemplateArgument (*ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001713 llvm::APSInt(apint),
Pavel Labathc7c30eb2015-06-08 23:38:06 +00001714 clang_type.GetQualType()));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001715 }
1716 else
1717 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +00001718 template_param_infos.args.push_back (clang::TemplateArgument (clang_type.GetQualType()));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001719 }
1720 }
1721 else
1722 {
1723 return false;
1724 }
1725
1726 }
1727 }
1728 return true;
1729
1730 default:
1731 break;
1732 }
1733 return false;
1734}
1735
1736bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001737SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1738 const DWARFDebugInfoEntry *parent_die,
1739 ClangASTContext::TemplateParameterInfos &template_param_infos)
1740{
1741
1742 if (parent_die == NULL)
Filipe Cabecinhas52008c62012-05-23 16:24:11 +00001743 return false;
Greg Claytonf0705c82011-10-22 03:33:13 +00001744
Greg Claytonf0705c82011-10-22 03:33:13 +00001745 Args template_parameter_names;
1746 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1747 die != NULL;
1748 die = die->GetSibling())
1749 {
1750 const dw_tag_t tag = die->Tag();
1751
1752 switch (tag)
1753 {
1754 case DW_TAG_template_type_parameter:
1755 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001756 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001757 break;
1758
1759 default:
1760 break;
1761 }
1762 }
1763 if (template_param_infos.args.empty())
1764 return false;
1765 return template_param_infos.args.size() == template_param_infos.names.size();
1766}
1767
1768clang::ClassTemplateDecl *
1769SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001770 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001771 const char *parent_name,
1772 int tag_decl_kind,
1773 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1774{
1775 if (template_param_infos.IsValid())
1776 {
1777 std::string template_basename(parent_name);
1778 template_basename.erase (template_basename.find('<'));
1779 ClangASTContext &ast = GetClangASTContext();
1780
1781 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001782 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001783 template_basename.c_str(),
1784 tag_decl_kind,
1785 template_param_infos);
1786 }
1787 return NULL;
1788}
1789
Sean Callanana0b80ab2012-06-04 22:28:05 +00001790class SymbolFileDWARF::DelayedAddObjCClassProperty
1791{
1792public:
1793 DelayedAddObjCClassProperty
1794 (
Greg Clayton57ee3062013-07-11 22:46:58 +00001795 const ClangASTType &class_opaque_type,
Sean Callanana0b80ab2012-06-04 22:28:05 +00001796 const char *property_name,
Greg Clayton57ee3062013-07-11 22:46:58 +00001797 const ClangASTType &property_opaque_type, // The property type is only required if you don't have an ivar decl
Sean Callanana0b80ab2012-06-04 22:28:05 +00001798 clang::ObjCIvarDecl *ivar_decl,
1799 const char *property_setter_name,
1800 const char *property_getter_name,
1801 uint32_t property_attributes,
Greg Claytonc4ffd662013-03-08 01:37:30 +00001802 const ClangASTMetadata *metadata
Sean Callanana0b80ab2012-06-04 22:28:05 +00001803 ) :
Sean Callanana0b80ab2012-06-04 22:28:05 +00001804 m_class_opaque_type (class_opaque_type),
1805 m_property_name (property_name),
1806 m_property_opaque_type (property_opaque_type),
1807 m_ivar_decl (ivar_decl),
1808 m_property_setter_name (property_setter_name),
1809 m_property_getter_name (property_getter_name),
Jim Ingham379397632012-10-27 02:54:13 +00001810 m_property_attributes (property_attributes)
Sean Callanana0b80ab2012-06-04 22:28:05 +00001811 {
Jim Ingham379397632012-10-27 02:54:13 +00001812 if (metadata != NULL)
1813 {
1814 m_metadata_ap.reset(new ClangASTMetadata());
Greg Claytonc4ffd662013-03-08 01:37:30 +00001815 *m_metadata_ap = *metadata;
Jim Ingham379397632012-10-27 02:54:13 +00001816 }
1817 }
1818
1819 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
1820 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001821 *this = rhs;
Daniel Malead4c5be62012-11-12 21:02:14 +00001822 }
1823
1824 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs)
1825 {
Jim Ingham379397632012-10-27 02:54:13 +00001826 m_class_opaque_type = rhs.m_class_opaque_type;
1827 m_property_name = rhs.m_property_name;
1828 m_property_opaque_type = rhs.m_property_opaque_type;
1829 m_ivar_decl = rhs.m_ivar_decl;
1830 m_property_setter_name = rhs.m_property_setter_name;
1831 m_property_getter_name = rhs.m_property_getter_name;
1832 m_property_attributes = rhs.m_property_attributes;
1833
1834 if (rhs.m_metadata_ap.get())
1835 {
1836 m_metadata_ap.reset (new ClangASTMetadata());
Greg Claytonc4ffd662013-03-08 01:37:30 +00001837 *m_metadata_ap = *rhs.m_metadata_ap;
Jim Ingham379397632012-10-27 02:54:13 +00001838 }
Daniel Malead4c5be62012-11-12 21:02:14 +00001839 return *this;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001840 }
1841
Greg Clayton57ee3062013-07-11 22:46:58 +00001842 bool
1843 Finalize()
Sean Callanana0b80ab2012-06-04 22:28:05 +00001844 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +00001845 return m_class_opaque_type.AddObjCClassProperty (m_property_name,
1846 m_property_opaque_type,
1847 m_ivar_decl,
1848 m_property_setter_name,
1849 m_property_getter_name,
1850 m_property_attributes,
1851 m_metadata_ap.get());
Sean Callanana0b80ab2012-06-04 22:28:05 +00001852 }
1853private:
Greg Clayton57ee3062013-07-11 22:46:58 +00001854 ClangASTType m_class_opaque_type;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001855 const char *m_property_name;
Greg Clayton57ee3062013-07-11 22:46:58 +00001856 ClangASTType m_property_opaque_type;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001857 clang::ObjCIvarDecl *m_ivar_decl;
1858 const char *m_property_setter_name;
1859 const char *m_property_getter_name;
1860 uint32_t m_property_attributes;
Greg Clayton7b0992d2013-04-18 22:45:39 +00001861 std::unique_ptr<ClangASTMetadata> m_metadata_ap;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001862};
1863
Sean Callananfaa0bb32012-12-05 23:37:14 +00001864struct BitfieldInfo
1865{
1866 uint64_t bit_size;
1867 uint64_t bit_offset;
1868
1869 BitfieldInfo () :
1870 bit_size (LLDB_INVALID_ADDRESS),
1871 bit_offset (LLDB_INVALID_ADDRESS)
1872 {
1873 }
1874
Greg Clayton78f4d952013-12-11 23:10:39 +00001875 void
1876 Clear()
1877 {
1878 bit_size = LLDB_INVALID_ADDRESS;
1879 bit_offset = LLDB_INVALID_ADDRESS;
1880 }
1881
Sean Callananfaa0bb32012-12-05 23:37:14 +00001882 bool IsValid ()
1883 {
1884 return (bit_size != LLDB_INVALID_ADDRESS) &&
1885 (bit_offset != LLDB_INVALID_ADDRESS);
1886 }
1887};
1888
Greg Claytonc4ffd662013-03-08 01:37:30 +00001889
1890bool
1891SymbolFileDWARF::ClassOrStructIsVirtual (DWARFCompileUnit* dwarf_cu,
1892 const DWARFDebugInfoEntry *parent_die)
1893{
1894 if (parent_die)
1895 {
1896 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1897 {
1898 dw_tag_t tag = die->Tag();
1899 bool check_virtuality = false;
1900 switch (tag)
1901 {
1902 case DW_TAG_inheritance:
1903 case DW_TAG_subprogram:
1904 check_virtuality = true;
1905 break;
1906 default:
1907 break;
1908 }
1909 if (check_virtuality)
1910 {
1911 if (die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_virtuality, 0) != 0)
1912 return true;
1913 }
1914 }
1915 }
1916 return false;
1917}
1918
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001919size_t
1920SymbolFileDWARF::ParseChildMembers
1921(
1922 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001923 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001924 const DWARFDebugInfoEntry *parent_die,
Greg Clayton57ee3062013-07-11 22:46:58 +00001925 ClangASTType &class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001926 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001927 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1928 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001929 DWARFDIECollection& member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00001930 DelayedPropertyList& delayed_properties,
Sean Callananc7fbf732010-08-06 00:32:49 +00001931 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001932 bool &is_a_class,
1933 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001934)
1935{
1936 if (parent_die == NULL)
1937 return 0;
1938
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001939 size_t count = 0;
1940 const DWARFDebugInfoEntry *die;
Todd Fialaee8bfc62014-09-11 17:29:12 +00001941 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001942 uint32_t member_idx = 0;
Sean Callananfaa0bb32012-12-05 23:37:14 +00001943 BitfieldInfo last_field_info;
Richard Mitton0a558352013-10-17 21:14:00 +00001944 ModuleSP module = GetObjectFile()->GetModule();
Greg Claytond88d7592010-09-15 08:33:30 +00001945
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1947 {
1948 dw_tag_t tag = die->Tag();
1949
1950 switch (tag)
1951 {
1952 case DW_TAG_member:
Sean Callanan3d654b32012-09-24 22:25:51 +00001953 case DW_TAG_APPLE_property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001954 {
1955 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001956 const size_t num_attributes = die->GetAttributes (this,
1957 dwarf_cu,
1958 fixed_form_sizes,
1959 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001960 if (num_attributes > 0)
1961 {
1962 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001963 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001964 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001965 const char *prop_name = NULL;
1966 const char *prop_getter_name = NULL;
1967 const char *prop_setter_name = NULL;
Greg Claytone528efd72013-02-26 23:45:18 +00001968 uint32_t prop_attributes = 0;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001969
1970
Greg Clayton24739922010-10-13 03:15:28 +00001971 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001972 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001973 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001974 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975 size_t byte_size = 0;
1976 size_t bit_offset = 0;
1977 size_t bit_size = 0;
Greg Claytone528efd72013-02-26 23:45:18 +00001978 bool is_external = false; // On DW_TAG_members, this means the member is static
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001979 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001980 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981 {
1982 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1983 DWARFFormValue form_value;
1984 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1985 {
1986 switch (attr)
1987 {
1988 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1989 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1990 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1991 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00001992 case DW_AT_type: encoding_uid = form_value.Reference(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001993 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1994 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1995 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1996 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001997 if (form_value.BlockData())
1998 {
1999 Value initialValue(0);
2000 Value memberOffset(0);
Ed Masteeeae7212013-10-24 20:43:47 +00002001 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002002 uint32_t block_length = form_value.Unsigned();
2003 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
2004 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
Greg Claytoncaab74e2012-01-28 00:48:57 +00002005 NULL, // ClangExpressionVariableList *
2006 NULL, // ClangExpressionDeclMap *
2007 NULL, // RegisterContext *
Richard Mitton0a558352013-10-17 21:14:00 +00002008 module,
Greg Claytoncaab74e2012-01-28 00:48:57 +00002009 debug_info_data,
2010 block_offset,
2011 block_length,
2012 eRegisterKindDWARF,
2013 &initialValue,
2014 memberOffset,
2015 NULL))
2016 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002017 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002018 }
2019 }
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00002020 else
2021 {
2022 // With DWARF 3 and later, if the value is an integer constant,
2023 // this form value is the offset in bytes from the beginning
2024 // of the containing entity.
2025 member_byte_offset = form_value.Unsigned();
2026 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002027 break;
2028
Greg Clayton8cf05932010-07-22 18:30:50 +00002029 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00002030 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00002031 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
2032 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
2033 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
2034 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00002035 case DW_AT_external: is_external = form_value.Boolean(); break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00002036
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002037 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00002038 case DW_AT_declaration:
2039 case DW_AT_description:
2040 case DW_AT_mutable:
2041 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002042 case DW_AT_sibling:
2043 break;
2044 }
2045 }
2046 }
Sean Callanana6582262012-04-05 00:12:52 +00002047
2048 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00002049 {
Sean Callanana6582262012-04-05 00:12:52 +00002050 ConstString fixed_getter;
2051 ConstString fixed_setter;
2052
2053 // Check if the property getter/setter were provided as full
2054 // names. We want basenames, so we extract them.
2055
2056 if (prop_getter_name && prop_getter_name[0] == '-')
2057 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00002058 ObjCLanguageRuntime::MethodName prop_getter_method(prop_getter_name, true);
2059 prop_getter_name = prop_getter_method.GetSelector().GetCString();
Sean Callanana6582262012-04-05 00:12:52 +00002060 }
2061
2062 if (prop_setter_name && prop_setter_name[0] == '-')
2063 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00002064 ObjCLanguageRuntime::MethodName prop_setter_method(prop_setter_name, true);
2065 prop_setter_name = prop_setter_method.GetSelector().GetCString();
Sean Callanana6582262012-04-05 00:12:52 +00002066 }
2067
2068 // If the names haven't been provided, they need to be
2069 // filled in.
2070
2071 if (!prop_getter_name)
2072 {
2073 prop_getter_name = prop_name;
2074 }
2075 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
2076 {
2077 StreamString ss;
2078
2079 ss.Printf("set%c%s:",
2080 toupper(prop_name[0]),
2081 &prop_name[1]);
2082
2083 fixed_setter.SetCString(ss.GetData());
2084 prop_setter_name = fixed_setter.GetCString();
2085 }
Sean Callanan751aac62012-03-29 19:07:06 +00002086 }
2087
2088 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00002089 // represents fields that are references with bad byte size
2090 // and bit size/offset information such as:
2091 //
2092 // DW_AT_byte_size( 0x00 )
2093 // DW_AT_bit_size( 0x40 )
2094 // DW_AT_bit_offset( 0xffffffffffffffc0 )
2095 //
2096 // So check the bit offset to make sure it is sane, and if
2097 // the values are not sane, remove them. If we don't do this
2098 // then we will end up with a crash if we try to use this
2099 // type in an expression when clang becomes unhappy with its
2100 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00002101
Greg Clayton44953932011-10-25 01:25:35 +00002102 if (bit_offset > 128)
2103 {
2104 bit_size = 0;
2105 bit_offset = 0;
2106 }
2107
2108 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00002109 if (class_language == eLanguageTypeObjC ||
2110 class_language == eLanguageTypeObjC_plus_plus)
2111 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002112
2113 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
2114 {
2115 // Not all compilers will mark the vtable pointer
2116 // member as artificial (llvm-gcc). We can't have
2117 // the virtual members in our classes otherwise it
2118 // throws off all child offsets since we end up
2119 // having and extra pointer sized member in our
2120 // class layouts.
2121 is_artificial = true;
2122 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002123
Greg Clayton29659712013-07-12 20:08:35 +00002124 // Handle static members
Greg Claytone528efd72013-02-26 23:45:18 +00002125 if (is_external && member_byte_offset == UINT32_MAX)
Greg Clayton973b6c92013-04-11 16:57:51 +00002126 {
2127 Type *var_type = ResolveTypeUID(encoding_uid);
2128
2129 if (var_type)
2130 {
Greg Clayton29659712013-07-12 20:08:35 +00002131 if (accessibility == eAccessNone)
2132 accessibility = eAccessPublic;
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002133 class_clang_type.AddVariableToRecordType (name,
Greg Clayton57ee3062013-07-11 22:46:58 +00002134 var_type->GetClangLayoutType(),
2135 accessibility);
Greg Clayton973b6c92013-04-11 16:57:51 +00002136 }
Greg Claytone528efd72013-02-26 23:45:18 +00002137 break;
Greg Clayton973b6c92013-04-11 16:57:51 +00002138 }
Greg Claytone528efd72013-02-26 23:45:18 +00002139
Greg Clayton24739922010-10-13 03:15:28 +00002140 if (is_artificial == false)
2141 {
2142 Type *member_type = ResolveTypeUID(encoding_uid);
Sean Callananfa3ab452012-12-14 00:54:13 +00002143
Jim Inghame3ae82a2011-11-12 01:36:43 +00002144 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00002145 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00002146 {
Sean Callanan751aac62012-03-29 19:07:06 +00002147 if (member_type)
2148 {
2149 if (accessibility == eAccessNone)
2150 accessibility = default_accessibility;
2151 member_accessibilities.push_back(accessibility);
Sean Callananfaa0bb32012-12-05 23:37:14 +00002152
Greg Clayton78f4d952013-12-11 23:10:39 +00002153 uint64_t field_bit_offset = (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
2154 if (bit_size > 0)
Greg Clayton88bc7f32012-11-06 00:20:41 +00002155 {
Greg Clayton78f4d952013-12-11 23:10:39 +00002156
2157 BitfieldInfo this_field_info;
2158 this_field_info.bit_offset = field_bit_offset;
2159 this_field_info.bit_size = bit_size;
2160
Sean Callananfaa0bb32012-12-05 23:37:14 +00002161 /////////////////////////////////////////////////////////////
2162 // How to locate a field given the DWARF debug information
2163 //
2164 // AT_byte_size indicates the size of the word in which the
2165 // bit offset must be interpreted.
2166 //
2167 // AT_data_member_location indicates the byte offset of the
2168 // word from the base address of the structure.
2169 //
2170 // AT_bit_offset indicates how many bits into the word
2171 // (according to the host endianness) the low-order bit of
2172 // the field starts. AT_bit_offset can be negative.
2173 //
2174 // AT_bit_size indicates the size of the field in bits.
2175 /////////////////////////////////////////////////////////////
2176
Greg Clayton78f4d952013-12-11 23:10:39 +00002177 if (byte_size == 0)
2178 byte_size = member_type->GetByteSize();
2179
Sean Callananfaa0bb32012-12-05 23:37:14 +00002180 if (GetObjectFile()->GetByteOrder() == eByteOrderLittle)
2181 {
2182 this_field_info.bit_offset += byte_size * 8;
2183 this_field_info.bit_offset -= (bit_offset + bit_size);
2184 }
2185 else
2186 {
2187 this_field_info.bit_offset += bit_offset;
2188 }
Greg Clayton78f4d952013-12-11 23:10:39 +00002189
2190 // Update the field bit offset we will report for layout
2191 field_bit_offset = this_field_info.bit_offset;
Sean Callananfaa0bb32012-12-05 23:37:14 +00002192
Greg Clayton78f4d952013-12-11 23:10:39 +00002193 // If the member to be emitted did not start on a character boundary and there is
2194 // empty space between the last field and this one, then we need to emit an
2195 // anonymous member filling up the space up to its start. There are three cases
2196 // here:
2197 //
2198 // 1 If the previous member ended on a character boundary, then we can emit an
2199 // anonymous member starting at the most recent character boundary.
2200 //
2201 // 2 If the previous member did not end on a character boundary and the distance
2202 // from the end of the previous member to the current member is less than a
2203 // word width, then we can emit an anonymous member starting right after the
2204 // previous member and right before this member.
2205 //
2206 // 3 If the previous member did not end on a character boundary and the distance
2207 // from the end of the previous member to the current member is greater than
2208 // or equal a word width, then we act as in Case 1.
2209
2210 const uint64_t character_width = 8;
2211 const uint64_t word_width = 32;
2212
Sean Callananfaa0bb32012-12-05 23:37:14 +00002213 // Objective-C has invalid DW_AT_bit_offset values in older versions
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00002214 // of clang, so we have to be careful and only insert unnamed bitfields
Greg Clayton37c36e42012-11-27 00:59:26 +00002215 // if we have a new enough clang.
2216 bool detect_unnamed_bitfields = true;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002217
Greg Clayton37c36e42012-11-27 00:59:26 +00002218 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus)
2219 detect_unnamed_bitfields = dwarf_cu->Supports_unnamed_objc_bitfields ();
2220
2221 if (detect_unnamed_bitfields)
Greg Clayton88bc7f32012-11-06 00:20:41 +00002222 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00002223 BitfieldInfo anon_field_info;
2224
2225 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned
Greg Clayton88bc7f32012-11-06 00:20:41 +00002226 {
Sean Callananfaa0bb32012-12-05 23:37:14 +00002227 uint64_t last_field_end = 0;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002228
Sean Callananfaa0bb32012-12-05 23:37:14 +00002229 if (last_field_info.IsValid())
2230 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002231
Sean Callananfaa0bb32012-12-05 23:37:14 +00002232 if (this_field_info.bit_offset != last_field_end)
2233 {
2234 if (((last_field_end % character_width) == 0) || // case 1
2235 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3
2236 {
2237 anon_field_info.bit_size = this_field_info.bit_offset % character_width;
2238 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size;
2239 }
2240 else // case 2
2241 {
2242 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end;
2243 anon_field_info.bit_offset = last_field_end;
2244 }
Greg Clayton88bc7f32012-11-06 00:20:41 +00002245 }
Greg Clayton88bc7f32012-11-06 00:20:41 +00002246 }
2247
Sean Callananfaa0bb32012-12-05 23:37:14 +00002248 if (anon_field_info.IsValid())
Greg Clayton88bc7f32012-11-06 00:20:41 +00002249 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002250 clang::FieldDecl *unnamed_bitfield_decl = class_clang_type.AddFieldToRecordType (NULL,
2251 GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width),
2252 accessibility,
2253 anon_field_info.bit_size);
Zachary Turner504f38d2015-03-24 16:24:50 +00002254
Zachary Turnera98fac22015-03-24 18:56:08 +00002255 layout_info.field_offsets.insert(
Zachary Turner504f38d2015-03-24 16:24:50 +00002256 std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset));
Greg Clayton88bc7f32012-11-06 00:20:41 +00002257 }
2258 }
Greg Clayton78f4d952013-12-11 23:10:39 +00002259 last_field_info = this_field_info;
2260 }
2261 else
2262 {
2263 last_field_info.Clear();
Greg Clayton88bc7f32012-11-06 00:20:41 +00002264 }
Sean Callananfaa0bb32012-12-05 23:37:14 +00002265
Greg Clayton57ee3062013-07-11 22:46:58 +00002266 ClangASTType member_clang_type = member_type->GetClangLayoutType();
Sean Callananfa3ab452012-12-14 00:54:13 +00002267
2268 {
2269 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>).
2270 // If the current field is at the end of the structure, then there is definitely no room for extra
2271 // elements and we override the type to array[0].
2272
Greg Clayton57ee3062013-07-11 22:46:58 +00002273 ClangASTType member_array_element_type;
Sean Callananfa3ab452012-12-14 00:54:13 +00002274 uint64_t member_array_size;
2275 bool member_array_is_incomplete;
2276
Greg Clayton57ee3062013-07-11 22:46:58 +00002277 if (member_clang_type.IsArrayType(&member_array_element_type,
2278 &member_array_size,
2279 &member_array_is_incomplete) &&
Sean Callananfa3ab452012-12-14 00:54:13 +00002280 !member_array_is_incomplete)
2281 {
2282 uint64_t parent_byte_size = parent_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, UINT64_MAX);
2283
2284 if (member_byte_offset >= parent_byte_size)
2285 {
2286 if (member_array_size != 1)
2287 {
2288 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which extends beyond the bounds of 0x%8.8" PRIx64,
2289 MakeUserID(die->GetOffset()),
2290 name,
2291 encoding_uid,
2292 MakeUserID(parent_die->GetOffset()));
2293 }
2294
Greg Clayton1c8ef472013-04-05 23:27:21 +00002295 member_clang_type = GetClangASTContext().CreateArrayType(member_array_element_type, 0, false);
Sean Callananfa3ab452012-12-14 00:54:13 +00002296 }
2297 }
2298 }
2299
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002300 field_decl = class_clang_type.AddFieldToRecordType (name,
Greg Clayton57ee3062013-07-11 22:46:58 +00002301 member_clang_type,
2302 accessibility,
2303 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00002304
Greg Claytond0029442013-03-27 01:48:02 +00002305 GetClangASTContext().SetMetadataAsUserID (field_decl, MakeUserID(die->GetOffset()));
Greg Clayton78f4d952013-12-11 23:10:39 +00002306
Zachary Turnera98fac22015-03-24 18:56:08 +00002307 layout_info.field_offsets.insert(std::make_pair(field_decl, field_bit_offset));
Sean Callanan5b26f272012-02-04 08:49:35 +00002308 }
2309 else
2310 {
Sean Callanan751aac62012-03-29 19:07:06 +00002311 if (name)
Daniel Malead01b2952012-11-29 21:49:15 +00002312 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
Sean Callanan751aac62012-03-29 19:07:06 +00002313 MakeUserID(die->GetOffset()),
2314 name,
2315 encoding_uid);
2316 else
Daniel Malead01b2952012-11-29 21:49:15 +00002317 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
Sean Callanan751aac62012-03-29 19:07:06 +00002318 MakeUserID(die->GetOffset()),
2319 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00002320 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002321 }
Sean Callanan751aac62012-03-29 19:07:06 +00002322
Greg Clayton3ca8f422015-07-13 22:08:16 +00002323 if (prop_name != NULL && member_type)
Jim Inghame3ae82a2011-11-12 01:36:43 +00002324 {
Sean Callanan751aac62012-03-29 19:07:06 +00002325 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00002326
Sean Callanan751aac62012-03-29 19:07:06 +00002327 if (field_decl)
2328 {
2329 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
2330 assert (ivar_decl != NULL);
2331 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00002332
Jim Ingham379397632012-10-27 02:54:13 +00002333 ClangASTMetadata metadata;
2334 metadata.SetUserID (MakeUserID(die->GetOffset()));
Greg Clayton57ee3062013-07-11 22:46:58 +00002335 delayed_properties.push_back(DelayedAddObjCClassProperty(class_clang_type,
Sean Callanana0b80ab2012-06-04 22:28:05 +00002336 prop_name,
2337 member_type->GetClangLayoutType(),
2338 ivar_decl,
2339 prop_setter_name,
2340 prop_getter_name,
2341 prop_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00002342 &metadata));
Sean Callanan60217122012-04-13 00:10:03 +00002343
Sean Callananad880762012-04-18 01:06:17 +00002344 if (ivar_decl)
Greg Claytond0029442013-03-27 01:48:02 +00002345 GetClangASTContext().SetMetadataAsUserID (ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00002346 }
Greg Clayton24739922010-10-13 03:15:28 +00002347 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002348 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00002349 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002350 }
2351 break;
2352
2353 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00002354 // Let the type parsing code handle this one for us.
2355 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002356 break;
2357
2358 case DW_TAG_inheritance:
2359 {
2360 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00002361 if (default_accessibility == eAccessNone)
2362 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002363 // TODO: implement DW_TAG_inheritance type parsing
2364 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00002365 const size_t num_attributes = die->GetAttributes (this,
2366 dwarf_cu,
2367 fixed_form_sizes,
2368 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002369 if (num_attributes > 0)
2370 {
2371 Declaration decl;
2372 DWARFExpression location;
2373 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00002374 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002375 bool is_virtual = false;
2376 bool is_base_of_class = true;
Greg Clayton2508b9b2012-11-01 23:20:02 +00002377 off_t member_byte_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002378 uint32_t i;
2379 for (i=0; i<num_attributes; ++i)
2380 {
2381 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2382 DWARFFormValue form_value;
2383 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2384 {
2385 switch (attr)
2386 {
2387 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2388 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2389 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00002390 case DW_AT_type: encoding_uid = form_value.Reference(); break;
Greg Clayton2508b9b2012-11-01 23:20:02 +00002391 case DW_AT_data_member_location:
2392 if (form_value.BlockData())
2393 {
2394 Value initialValue(0);
2395 Value memberOffset(0);
Ed Masteeeae7212013-10-24 20:43:47 +00002396 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Clayton2508b9b2012-11-01 23:20:02 +00002397 uint32_t block_length = form_value.Unsigned();
2398 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
2399 if (DWARFExpression::Evaluate (NULL,
2400 NULL,
2401 NULL,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002402 NULL,
Richard Mitton0a558352013-10-17 21:14:00 +00002403 module,
Greg Clayton2508b9b2012-11-01 23:20:02 +00002404 debug_info_data,
2405 block_offset,
2406 block_length,
2407 eRegisterKindDWARF,
2408 &initialValue,
2409 memberOffset,
2410 NULL))
2411 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002412 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
Greg Clayton2508b9b2012-11-01 23:20:02 +00002413 }
2414 }
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00002415 else
2416 {
2417 // With DWARF 3 and later, if the value is an integer constant,
2418 // this form value is the offset in bytes from the beginning
2419 // of the containing entity.
2420 member_byte_offset = form_value.Unsigned();
2421 }
Greg Clayton2508b9b2012-11-01 23:20:02 +00002422 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002423
2424 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00002425 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002426 break;
2427
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00002428 case DW_AT_virtuality:
2429 is_virtual = form_value.Boolean();
2430 break;
2431
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002432 case DW_AT_sibling:
2433 break;
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00002434
2435 default:
2436 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002437 }
2438 }
2439 }
2440
Greg Clayton526e5af2010-11-13 03:52:47 +00002441 Type *base_class_type = ResolveTypeUID(encoding_uid);
Greg Clayton898c1b22015-05-15 22:31:18 +00002442 if (base_class_type == NULL)
2443 {
Bruce Mitchener58ef3912015-06-18 05:27:05 +00002444 GetObjectFile()->GetModule()->ReportError("0x%8.8x: DW_TAG_inheritance failed to resolve the base class at 0x%8.8" PRIx64 " from enclosing type 0x%8.8x. \nPlease file a bug and attach the file at the start of this error message",
Greg Clayton898c1b22015-05-15 22:31:18 +00002445 die->GetOffset(),
2446 encoding_uid,
2447 parent_die->GetOffset());
2448 break;
2449 }
2450
Greg Clayton57ee3062013-07-11 22:46:58 +00002451 ClangASTType base_class_clang_type = base_class_type->GetClangFullType();
Greg Claytonf4ecaa52011-02-16 23:00:21 +00002452 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002453 if (class_language == eLanguageTypeObjC)
2454 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002455 class_clang_type.SetObjCSuperClass(base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00002456 }
2457 else
2458 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002459 base_classes.push_back (base_class_clang_type.CreateBaseClassSpecifier (accessibility,
2460 is_virtual,
2461 is_base_of_class));
Greg Clayton2508b9b2012-11-01 23:20:02 +00002462
2463 if (is_virtual)
2464 {
Greg Clayton52694e32013-09-16 21:57:07 +00002465 // Do not specify any offset for virtual inheritance. The DWARF produced by clang doesn't
2466 // give us a constant offset, but gives us a DWARF expressions that requires an actual object
2467 // in memory. the DW_AT_data_member_location for a virtual base class looks like:
2468 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, DW_OP_plus )
2469 // Given this, there is really no valid response we can give to clang for virtual base
2470 // class offsets, and this should eventually be removed from LayoutRecordType() in the external
2471 // AST source in clang.
Greg Clayton2508b9b2012-11-01 23:20:02 +00002472 }
2473 else
2474 {
Zachary Turnera98fac22015-03-24 18:56:08 +00002475 layout_info.base_offsets.insert(
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002476 std::make_pair(base_class_clang_type.GetAsCXXRecordDecl(),
Zachary Turner504f38d2015-03-24 16:24:50 +00002477 clang::CharUnits::fromQuantity(member_byte_offset)));
Greg Clayton2508b9b2012-11-01 23:20:02 +00002478 }
Greg Clayton9e409562010-07-28 02:04:09 +00002479 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002480 }
2481 }
2482 break;
2483
2484 default:
2485 break;
2486 }
2487 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00002488
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002489 return count;
2490}
2491
2492
2493clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00002494SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002495{
2496 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00002497 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002498 {
2499 DWARFCompileUnitSP cu_sp;
2500 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
2501 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00002502 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00002503 }
2504 return NULL;
2505}
2506
2507clang::DeclContext*
2508SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
2509{
Greg Clayton81c22f62011-10-19 18:09:39 +00002510 if (UserIDMatches(type_uid))
2511 return GetClangDeclContextForDIEOffset (sc, type_uid);
2512 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002513}
2514
2515Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00002516SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002517{
Greg Clayton81c22f62011-10-19 18:09:39 +00002518 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002519 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002520 DWARFDebugInfo* debug_info = DebugInfo();
2521 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00002522 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002523 DWARFCompileUnitSP cu_sp;
2524 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00002525 const bool assert_not_being_parsed = true;
2526 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00002527 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002528 }
2529 return NULL;
2530}
2531
Greg Claytoncab36a32011-12-08 05:16:30 +00002532Type*
2533SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00002534{
Greg Claytoncab36a32011-12-08 05:16:30 +00002535 if (die != NULL)
2536 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002537 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00002538 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002539 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002540 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
2541 die->GetOffset(),
2542 DW_TAG_value_to_name(die->Tag()),
2543 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00002544
Greg Claytoncab36a32011-12-08 05:16:30 +00002545 // We might be coming in in the middle of a type tree (a class
2546 // withing a class, an enum within a class), so parse any needed
2547 // parent DIEs before we get to this one...
2548 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2549 switch (decl_ctx_die->Tag())
2550 {
2551 case DW_TAG_structure_type:
2552 case DW_TAG_union_type:
2553 case DW_TAG_class_type:
2554 {
2555 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00002556 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002557 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00002558 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
2559 die->GetOffset(),
2560 DW_TAG_value_to_name(die->Tag()),
2561 die->GetName(this, cu),
2562 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00002563//
2564// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
2565// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
2566// {
2567// if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002568// GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton219cf312012-03-30 00:51:13 +00002569// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
2570// die->GetOffset(),
2571// DW_TAG_value_to_name(die->Tag()),
2572// die->GetName(this, cu),
2573// decl_ctx_die->GetOffset());
2574// // Ask the type to complete itself if it already hasn't since if we
2575// // want a function (method or static) from a class, the class must
2576// // create itself and add it's own methods and class functions.
2577// if (parent_type)
2578// parent_type->GetClangFullType();
2579// }
Greg Claytoncab36a32011-12-08 05:16:30 +00002580 }
2581 break;
2582
2583 default:
2584 break;
2585 }
2586 return ResolveType (cu, die);
2587 }
2588 return NULL;
2589}
2590
Greg Clayton6beaaa62011-01-17 03:46:26 +00002591// This function is used when SymbolFileDWARFDebugMap owns a bunch of
2592// SymbolFileDWARF objects to detect if this DWARF file is the one that
2593// can resolve a clang_type.
2594bool
Greg Clayton57ee3062013-07-11 22:46:58 +00002595SymbolFileDWARF::HasForwardDeclForClangType (const ClangASTType &clang_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00002596{
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002597 ClangASTType clang_type_no_qualifiers = clang_type.RemoveFastQualifiers();
Greg Clayton57ee3062013-07-11 22:46:58 +00002598 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton6beaaa62011-01-17 03:46:26 +00002599 return die != NULL;
2600}
2601
2602
Greg Clayton57ee3062013-07-11 22:46:58 +00002603bool
2604SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (ClangASTType &clang_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00002605{
2606 // We have a struct/union/class/enum that needs to be fully resolved.
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002607 ClangASTType clang_type_no_qualifiers = clang_type.RemoveFastQualifiers();
Greg Clayton57ee3062013-07-11 22:46:58 +00002608 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton1be10fc2010-09-29 01:12:09 +00002609 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002610 {
2611 // We have already resolved this type...
Greg Clayton57ee3062013-07-11 22:46:58 +00002612 return true;
Greg Clayton73b472d2010-10-27 03:32:59 +00002613 }
2614 // Once we start resolving this type, remove it from the forward declaration
2615 // map in case anyone child members or other types require this type to get resolved.
2616 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
2617 // are done.
Greg Clayton57ee3062013-07-11 22:46:58 +00002618 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers.GetOpaqueQualType());
Greg Clayton1be10fc2010-09-29 01:12:09 +00002619
Greg Clayton85ae2e12011-10-18 23:36:41 +00002620 // Disable external storage for this type so we don't get anymore
2621 // clang::ExternalASTSource queries for this type.
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002622 clang_type.SetHasExternalStorage (false);
Greg Clayton85ae2e12011-10-18 23:36:41 +00002623
Greg Clayton450e3f32010-10-12 02:24:53 +00002624 DWARFDebugInfo* debug_info = DebugInfo();
2625
Greg Clayton53eb1c22012-04-02 22:59:12 +00002626 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002627 Type *type = m_die_to_type.lookup (die);
2628
2629 const dw_tag_t tag = die->Tag();
2630
Greg Clayton5160ce52013-03-27 23:08:40 +00002631 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00002632 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00002633 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
Daniel Malead01b2952012-11-29 21:49:15 +00002634 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
Greg Claytond61c0fc2012-04-23 22:55:20 +00002635 MakeUserID(die->GetOffset()),
2636 DW_TAG_value_to_name(tag),
2637 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00002638 assert (clang_type);
2639 DWARFDebugInfoEntry::Attributes attributes;
2640
Greg Clayton1be10fc2010-09-29 01:12:09 +00002641 switch (tag)
2642 {
2643 case DW_TAG_structure_type:
2644 case DW_TAG_union_type:
2645 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002646 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002647 LayoutInfo layout_info;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002648
Greg Clayton1be10fc2010-09-29 01:12:09 +00002649 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002650 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002651 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002652 LanguageType class_language = eLanguageTypeUnknown;
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002653 if (clang_type.IsObjCObjectOrInterfaceType())
Greg Clayton219cf312012-03-30 00:51:13 +00002654 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002655 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002656 // For objective C we don't start the definition when
2657 // the class is created.
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002658 clang_type.StartTagDeclarationDefinition ();
Greg Clayton219cf312012-03-30 00:51:13 +00002659 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002660
Sean Callanan77a1fd32012-02-27 20:07:01 +00002661 int tag_decl_kind = -1;
2662 AccessType default_accessibility = eAccessNone;
2663 if (tag == DW_TAG_structure_type)
2664 {
2665 tag_decl_kind = clang::TTK_Struct;
2666 default_accessibility = eAccessPublic;
2667 }
2668 else if (tag == DW_TAG_union_type)
2669 {
2670 tag_decl_kind = clang::TTK_Union;
2671 default_accessibility = eAccessPublic;
2672 }
2673 else if (tag == DW_TAG_class_type)
2674 {
2675 tag_decl_kind = clang::TTK_Class;
2676 default_accessibility = eAccessPrivate;
2677 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002678
Greg Clayton53eb1c22012-04-02 22:59:12 +00002679 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002680 std::vector<clang::CXXBaseSpecifier *> base_classes;
2681 std::vector<int> member_accessibilities;
2682 bool is_a_class = false;
2683 // Parse members and base classes first
2684 DWARFDIECollection member_function_dies;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002685
Sean Callanana0b80ab2012-06-04 22:28:05 +00002686 DelayedPropertyList delayed_properties;
Greg Clayton88bc7f32012-11-06 00:20:41 +00002687 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002688 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002689 die,
2690 clang_type,
2691 class_language,
2692 base_classes,
2693 member_accessibilities,
2694 member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00002695 delayed_properties,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002696 default_accessibility,
2697 is_a_class,
2698 layout_info);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002699
Sean Callanan77a1fd32012-02-27 20:07:01 +00002700 // Now parse any methods if there were any...
2701 size_t num_functions = member_function_dies.Size();
2702 if (num_functions > 0)
2703 {
2704 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002705 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002706 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002707 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002708 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002709
Sean Callanan77a1fd32012-02-27 20:07:01 +00002710 if (class_language == eLanguageTypeObjC)
2711 {
Jim Inghamfb6fc0d2013-09-27 20:59:37 +00002712 ConstString class_name (clang_type.GetTypeName());
2713 if (class_name)
Greg Claytoncaab74e2012-01-28 00:48:57 +00002714 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002715 DIEArray method_die_offsets;
2716 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002717 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002718 if (m_apple_objc_ap.get())
Jim Inghamfb6fc0d2013-09-27 20:59:37 +00002719 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets);
Sean Callanan77a1fd32012-02-27 20:07:01 +00002720 }
2721 else
2722 {
2723 if (!m_indexed)
2724 Index ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002725
Sean Callanan77a1fd32012-02-27 20:07:01 +00002726 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2727 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002728
Sean Callanan77a1fd32012-02-27 20:07:01 +00002729 if (!method_die_offsets.empty())
2730 {
2731 DWARFDebugInfo* debug_info = DebugInfo();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002732
Sean Callanan77a1fd32012-02-27 20:07:01 +00002733 DWARFCompileUnit* method_cu = NULL;
2734 const size_t num_matches = method_die_offsets.size();
2735 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002736 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002737 const dw_offset_t die_offset = method_die_offsets[i];
2738 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002739
Sean Callanan77a1fd32012-02-27 20:07:01 +00002740 if (method_die)
2741 ResolveType (method_cu, method_die);
2742 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002743 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002744 if (m_using_apple_tables)
2745 {
2746 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
Jim Inghamfb6fc0d2013-09-27 20:59:37 +00002747 die_offset, class_name.GetCString());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002748 }
2749 }
2750 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002751 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002752
Greg Clayton57ee3062013-07-11 22:46:58 +00002753 for (DelayedPropertyList::iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
Sean Callanana0b80ab2012-06-04 22:28:05 +00002754 pi != pe;
2755 ++pi)
2756 pi->Finalize();
Greg Clayton450e3f32010-10-12 02:24:53 +00002757 }
2758 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002759
Sean Callanan77a1fd32012-02-27 20:07:01 +00002760 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2761 // need to tell the clang type it is actually a class.
2762 if (class_language != eLanguageTypeObjC)
2763 {
2764 if (is_a_class && tag_decl_kind != clang::TTK_Class)
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002765 clang_type.SetTagTypeKind (clang::TTK_Class);
Sean Callanan77a1fd32012-02-27 20:07:01 +00002766 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002767
Sean Callanan77a1fd32012-02-27 20:07:01 +00002768 // Since DW_TAG_structure_type gets used for both classes
2769 // and structures, we may need to set any DW_TAG_member
2770 // fields to have a "private" access if none was specified.
2771 // When we parsed the child members we tracked that actual
2772 // accessibility value for each DW_TAG_member in the
2773 // "member_accessibilities" array. If the value for the
2774 // member is zero, then it was set to the "default_accessibility"
2775 // which for structs was "public". Below we correct this
2776 // by setting any fields to "private" that weren't correctly
2777 // set.
2778 if (is_a_class && !member_accessibilities.empty())
2779 {
2780 // This is a class and all members that didn't have
2781 // their access specified are private.
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002782 clang_type.SetDefaultAccessForRecordFields (eAccessPrivate,
2783 &member_accessibilities.front(),
2784 member_accessibilities.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002785 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002786
Sean Callanan77a1fd32012-02-27 20:07:01 +00002787 if (!base_classes.empty())
2788 {
Greg Clayton4705f8d2013-12-12 01:54:04 +00002789 // Make sure all base classes refer to complete types and not
2790 // forward declarations. If we don't do this, clang will crash
2791 // with an assertion in the call to clang_type.SetBaseClassesForClassType()
2792 bool base_class_error = false;
2793 for (auto &base_class : base_classes)
2794 {
2795 clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo();
2796 if (type_source_info)
2797 {
2798 ClangASTType base_class_type (GetClangASTContext().getASTContext(), type_source_info->getType());
2799 if (base_class_type.GetCompleteType() == false)
2800 {
2801 if (!base_class_error)
2802 {
2803 GetObjectFile()->GetModule()->ReportError ("DWARF DIE at 0x%8.8x for class '%s' has a base class '%s' that is a forward declaration, not a complete definition.\nPlease file a bug against the compiler and include the preprocessed output for %s",
2804 die->GetOffset(),
2805 die->GetName(this, dwarf_cu),
2806 base_class_type.GetTypeName().GetCString(),
2807 sc.comp_unit ? sc.comp_unit->GetPath().c_str() : "the source file");
2808 }
2809 // We have no choice other than to pretend that the base class
2810 // is complete. If we don't do this, clang will crash when we
2811 // call setBases() inside of "clang_type.SetBaseClassesForClassType()"
2812 // below. Since we provide layout assistance, all ivars in this
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002813 // class and other classes will be fine, this is the best we can do
Greg Clayton4705f8d2013-12-12 01:54:04 +00002814 // short of crashing.
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002815 base_class_type.StartTagDeclarationDefinition ();
2816 base_class_type.CompleteTagDeclarationDefinition ();
Greg Clayton4705f8d2013-12-12 01:54:04 +00002817 }
2818 }
2819 }
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002820 clang_type.SetBaseClassesForClassType (&base_classes.front(),
2821 base_classes.size());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002822
Sean Callanan77a1fd32012-02-27 20:07:01 +00002823 // Clang will copy each CXXBaseSpecifier in "base_classes"
2824 // so we have to free them all.
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002825 ClangASTType::DeleteBaseClassSpecifiers (&base_classes.front(),
2826 base_classes.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002827 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002828 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002829 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002830
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002831 clang_type.BuildIndirectFields ();
2832 clang_type.CompleteTagDeclarationDefinition ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002833
Greg Clayton2508b9b2012-11-01 23:20:02 +00002834 if (!layout_info.field_offsets.empty() ||
2835 !layout_info.base_offsets.empty() ||
2836 !layout_info.vbase_offsets.empty() )
Greg Claytoncaab74e2012-01-28 00:48:57 +00002837 {
2838 if (type)
2839 layout_info.bit_size = type->GetByteSize() * 8;
2840 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002841 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002842
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002843 clang::CXXRecordDecl *record_decl = clang_type.GetAsCXXRecordDecl();
Greg Clayton2508b9b2012-11-01 23:20:02 +00002844 if (record_decl)
Greg Claytoncaab74e2012-01-28 00:48:57 +00002845 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002846 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002847 {
Greg Clayton5160ce52013-03-27 23:08:40 +00002848 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00002849 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) caching layout info for record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002850 static_cast<void*>(clang_type.GetOpaqueQualType()),
2851 static_cast<void*>(record_decl),
Greg Claytoncaab74e2012-01-28 00:48:57 +00002852 layout_info.bit_size,
2853 layout_info.alignment,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00002854 static_cast<uint32_t>(layout_info.field_offsets.size()),
2855 static_cast<uint32_t>(layout_info.base_offsets.size()),
2856 static_cast<uint32_t>(layout_info.vbase_offsets.size()));
2857
Zachary Turnera98fac22015-03-24 18:56:08 +00002858 uint32_t idx;
2859 {
2860 llvm::DenseMap<const clang::FieldDecl *, uint64_t>::const_iterator pos,
2861 end = layout_info.field_offsets.end();
2862 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx)
Greg Clayton2508b9b2012-11-01 23:20:02 +00002863 {
Zachary Turner504f38d2015-03-24 16:24:50 +00002864 GetObjectFile()->GetModule()->LogMessage(
2865 log, "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field[%u] = "
2866 "{ bit_offset=%u, name='%s' }",
Zachary Turner504f38d2015-03-24 16:24:50 +00002867 static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
Zachary Turnera98fac22015-03-24 18:56:08 +00002868 static_cast<uint32_t>(pos->second), pos->first->getNameAsString().c_str());
2869 }
2870 }
2871
2872 {
2873 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos,
2874 base_end = layout_info.base_offsets.end();
2875 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end;
2876 ++base_pos, ++idx)
2877 {
2878 GetObjectFile()->GetModule()->LogMessage(
2879 log, "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) base[%u] "
2880 "= { byte_offset=%u, name='%s' }",
2881 clang_type.GetOpaqueQualType(), idx, (uint32_t)base_pos->second.getQuantity(),
2882 base_pos->first->getNameAsString().c_str());
2883 }
2884 }
2885 {
2886 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos,
2887 vbase_end = layout_info.vbase_offsets.end();
2888 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end;
2889 ++vbase_pos, ++idx)
2890 {
2891 GetObjectFile()->GetModule()->LogMessage(
2892 log, "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) "
2893 "vbase[%u] = { byte_offset=%u, name='%s' }",
2894 static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
2895 static_cast<uint32_t>(vbase_pos->second.getQuantity()),
2896 vbase_pos->first->getNameAsString().c_str());
2897 }
Greg Clayton2508b9b2012-11-01 23:20:02 +00002898 }
Greg Clayton821ac6d2012-01-28 02:22:27 +00002899 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002900 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2901 }
2902 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002903 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002904
Sean Callanan9076c0f2013-10-04 21:35:29 +00002905 return (bool)clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002906
2907 case DW_TAG_enumeration_type:
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002908 clang_type.StartTagDeclarationDefinition ();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002909 if (die->HasChildren())
2910 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002911 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Greg Clayton3e067532013-03-05 23:54:39 +00002912 bool is_signed = false;
Greg Clayton57ee3062013-07-11 22:46:58 +00002913 clang_type.IsIntegerType(is_signed);
Greg Clayton3e067532013-03-05 23:54:39 +00002914 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002915 }
Pavel Labathc7c30eb2015-06-08 23:38:06 +00002916 clang_type.CompleteTagDeclarationDefinition ();
Sean Callanan9076c0f2013-10-04 21:35:29 +00002917 return (bool)clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002918
2919 default:
2920 assert(false && "not a forward clang type decl!");
2921 break;
2922 }
Ashok Thirumurthia4658a52013-07-30 14:58:39 +00002923 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002924}
2925
Greg Claytonc685f8e2010-09-15 04:15:46 +00002926Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002927SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002928{
2929 if (type_die != NULL)
2930 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002931 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002932
Greg Claytonc685f8e2010-09-15 04:15:46 +00002933 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002934 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002935
Greg Clayton24739922010-10-13 03:15:28 +00002936 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002937 {
2938 if (type != DIE_IS_BEING_PARSED)
2939 return type;
2940
2941 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002942 type_die->GetOffset(),
2943 DW_TAG_value_to_name(type_die->Tag()),
2944 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002945
2946 }
2947 else
2948 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002949 }
2950 return NULL;
2951}
2952
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002953CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002954SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002955{
2956 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002957 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002958 {
2959 // The symbol vendor doesn't know about this compile unit, we
2960 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002961 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002962 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002963 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002964}
2965
2966bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002967SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002968{
Greg Clayton72310352013-02-23 04:12:47 +00002969 sc.Clear(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002970 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002971 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002972
Greg Clayton81c22f62011-10-19 18:09:39 +00002973 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002974 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002975 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002976
2977 if (sc.function)
2978 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002979 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002980 return true;
2981 }
2982
2983 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002984}
2985
Sean Callananf0c5aeb2015-04-20 16:31:29 +00002986void
2987SymbolFileDWARF::UpdateExternalModuleListIfNeeded()
2988{
2989 if (m_fetched_external_modules)
2990 return;
2991 m_fetched_external_modules = true;
2992
2993 DWARFDebugInfo * debug_info = DebugInfo();
2994 debug_info->GetNumCompileUnits();
2995
2996 const uint32_t num_compile_units = GetNumCompileUnits();
2997 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2998 {
2999 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
3000
3001 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly();
3002 if (die && die->HasChildren() == false)
3003 {
3004 const uint64_t name_strp = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_name, UINT64_MAX);
3005 const uint64_t dwo_path_strp = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_GNU_dwo_name, UINT64_MAX);
3006
3007 if (name_strp != UINT64_MAX)
3008 {
3009 if (m_external_type_modules.find(dwo_path_strp) == m_external_type_modules.end())
3010 {
3011 const char *name = get_debug_str_data().PeekCStr(name_strp);
3012 const char *dwo_path = get_debug_str_data().PeekCStr(dwo_path_strp);
3013 if (name || dwo_path)
3014 {
3015 ModuleSP module_sp;
3016 if (dwo_path)
3017 {
3018 ModuleSpec dwo_module_spec;
3019 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false);
3020 dwo_module_spec.GetArchitecture() = m_obj_file->GetModule()->GetArchitecture();
3021 //printf ("Loading dwo = '%s'\n", dwo_path);
3022 Error error = ModuleList::GetSharedModule (dwo_module_spec, module_sp, NULL, NULL, NULL);
3023 }
3024
3025 if (dwo_path_strp != LLDB_INVALID_UID)
3026 {
3027 m_external_type_modules[dwo_path_strp] = ClangModuleInfo { ConstString(name), module_sp };
3028 }
3029 else
3030 {
3031 // This hack should be removed promptly once clang emits both.
3032 m_external_type_modules[name_strp] = ClangModuleInfo { ConstString(name), module_sp };
3033 }
3034 }
3035 }
3036 }
3037 }
3038 }
3039}
Greg Clayton2501e5e2015-01-15 02:59:20 +00003040
3041SymbolFileDWARF::GlobalVariableMap &
3042SymbolFileDWARF::GetGlobalAranges()
3043{
3044 if (!m_global_aranges_ap)
3045 {
3046 m_global_aranges_ap.reset (new GlobalVariableMap());
3047
3048 ModuleSP module_sp = GetObjectFile()->GetModule();
3049 if (module_sp)
3050 {
3051 const size_t num_cus = module_sp->GetNumCompileUnits();
3052 for (size_t i = 0; i < num_cus; ++i)
3053 {
3054 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i);
3055 if (cu_sp)
3056 {
3057 VariableListSP globals_sp = cu_sp->GetVariableList(true);
3058 if (globals_sp)
3059 {
3060 const size_t num_globals = globals_sp->GetSize();
3061 for (size_t g = 0; g < num_globals; ++g)
3062 {
3063 VariableSP var_sp = globals_sp->GetVariableAtIndex(g);
3064 if (var_sp && !var_sp->GetLocationIsConstantValueData())
3065 {
3066 const DWARFExpression &location = var_sp->LocationExpression();
3067 Value location_result;
3068 Error error;
3069 if (location.Evaluate(NULL, NULL, NULL, LLDB_INVALID_ADDRESS, NULL, location_result, &error))
3070 {
3071 if (location_result.GetValueType() == Value::eValueTypeFileAddress)
3072 {
3073 lldb::addr_t file_addr = location_result.GetScalar().ULongLong();
3074 lldb::addr_t byte_size = 1;
3075 if (var_sp->GetType())
3076 byte_size = var_sp->GetType()->GetByteSize();
3077 m_global_aranges_ap->Append(GlobalVariableMap::Entry(file_addr, byte_size, var_sp.get()));
3078 }
3079 }
3080 }
3081 }
3082 }
3083 }
3084 }
3085 }
3086 m_global_aranges_ap->Sort();
3087 }
3088 return *m_global_aranges_ap;
3089}
3090
3091
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003092uint32_t
3093SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
3094{
3095 Timer scoped_timer(__PRETTY_FUNCTION__,
Daniel Malead01b2952012-11-29 21:49:15 +00003096 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003097 static_cast<void*>(so_addr.GetSection().get()),
3098 so_addr.GetOffset(), resolve_scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003099 uint32_t resolved = 0;
Greg Clayton2501e5e2015-01-15 02:59:20 +00003100 if (resolve_scope & ( eSymbolContextCompUnit |
3101 eSymbolContextFunction |
3102 eSymbolContextBlock |
3103 eSymbolContextLineEntry |
3104 eSymbolContextVariable ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003105 {
3106 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
3107
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003108 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00003109 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003110 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003111 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Greg Clayton2501e5e2015-01-15 02:59:20 +00003112 if (cu_offset == DW_INVALID_OFFSET)
3113 {
3114 // Global variables are not in the compile unit address ranges. The only way to
3115 // currently find global variables is to iterate over the .debug_pubnames or the
3116 // __apple_names table and find all items in there that point to DW_TAG_variable
3117 // DIEs and then find the address that matches.
3118 if (resolve_scope & eSymbolContextVariable)
3119 {
3120 GlobalVariableMap &map = GetGlobalAranges();
3121 const GlobalVariableMap::Entry *entry = map.FindEntryThatContains(file_vm_addr);
3122 if (entry && entry->data)
3123 {
3124 Variable *variable = entry->data;
3125 SymbolContextScope *scc = variable->GetSymbolContextScope();
3126 if (scc)
3127 {
3128 scc->CalculateSymbolContext(&sc);
3129 sc.variable = variable;
3130 }
3131 return sc.GetResolvedMask();
3132 }
3133 }
3134 }
3135 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003136 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003137 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton53eb1c22012-04-02 22:59:12 +00003138 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
3139 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003140 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003141 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00003142 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003143 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003144 resolved |= eSymbolContextCompUnit;
3145
Greg Clayton6ab80132012-12-12 17:30:52 +00003146 bool force_check_line_table = false;
Greg Clayton526a4ae2012-05-16 22:09:01 +00003147 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
3148 {
3149 DWARFDebugInfoEntry *function_die = NULL;
3150 DWARFDebugInfoEntry *block_die = NULL;
3151 if (resolve_scope & eSymbolContextBlock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003152 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003153 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
3154 }
3155 else
3156 {
3157 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
3158 }
3159
3160 if (function_die != NULL)
3161 {
3162 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
3163 if (sc.function == NULL)
3164 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
3165 }
3166 else
3167 {
3168 // We might have had a compile unit that had discontiguous
3169 // address ranges where the gaps are symbols that don't have
3170 // any debug info. Discontiguous compile unit address ranges
3171 // should only happen when there aren't other functions from
3172 // other compile units in these gaps. This helps keep the size
3173 // of the aranges down.
Greg Clayton6ab80132012-12-12 17:30:52 +00003174 force_check_line_table = true;
Greg Clayton526a4ae2012-05-16 22:09:01 +00003175 }
3176
3177 if (sc.function != NULL)
3178 {
3179 resolved |= eSymbolContextFunction;
3180
3181 if (resolve_scope & eSymbolContextBlock)
3182 {
3183 Block& block = sc.function->GetBlock (true);
3184
3185 if (block_die != NULL)
3186 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
3187 else
3188 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
3189 if (sc.block)
3190 resolved |= eSymbolContextBlock;
3191 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003192 }
3193 }
Greg Clayton6ab80132012-12-12 17:30:52 +00003194
3195 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table)
3196 {
3197 LineTable *line_table = sc.comp_unit->GetLineTable();
3198 if (line_table != NULL)
3199 {
Greg Clayton9422dd62013-03-04 21:46:16 +00003200 // And address that makes it into this function should be in terms
3201 // of this debug file if there is no debug map, or it will be an
3202 // address in the .o file which needs to be fixed up to be in terms
3203 // of the debug map executable. Either way, calling FixupAddress()
3204 // will work for us.
3205 Address exe_so_addr (so_addr);
3206 if (FixupAddress(exe_so_addr))
Greg Clayton6ab80132012-12-12 17:30:52 +00003207 {
Greg Clayton9422dd62013-03-04 21:46:16 +00003208 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry))
Greg Clayton6ab80132012-12-12 17:30:52 +00003209 {
3210 resolved |= eSymbolContextLineEntry;
3211 }
3212 }
Greg Clayton6ab80132012-12-12 17:30:52 +00003213 }
3214 }
3215
3216 if (force_check_line_table && !(resolved & eSymbolContextLineEntry))
3217 {
3218 // We might have had a compile unit that had discontiguous
3219 // address ranges where the gaps are symbols that don't have
3220 // any debug info. Discontiguous compile unit address ranges
3221 // should only happen when there aren't other functions from
3222 // other compile units in these gaps. This helps keep the size
3223 // of the aranges down.
3224 sc.comp_unit = NULL;
3225 resolved &= ~eSymbolContextCompUnit;
3226 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003227 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00003228 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003229 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003230 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
3231 cu_offset,
3232 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003233 }
3234 }
3235 }
3236 }
3237 }
3238 return resolved;
3239}
3240
3241
3242
3243uint32_t
3244SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
3245{
3246 const uint32_t prev_size = sc_list.GetSize();
3247 if (resolve_scope & eSymbolContextCompUnit)
3248 {
3249 DWARFDebugInfo* debug_info = DebugInfo();
3250 if (debug_info)
3251 {
3252 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00003253 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003254
Greg Clayton53eb1c22012-04-02 22:59:12 +00003255 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003256 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003257 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Sean Callananddd7a2a2013-10-03 22:27:29 +00003258 const bool full_match = (bool)file_spec.GetDirectory();
Greg Clayton1f746072012-08-29 21:13:06 +00003259 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003260 if (check_inlines || file_spec_matches_cu_file_spec)
3261 {
3262 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00003263 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00003264 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003265 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003266 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003267
Greg Clayton526a4ae2012-05-16 22:09:01 +00003268 // If we are looking for inline functions only and we don't
3269 // find it in the support files, we are done.
3270 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003271 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003272 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
3273 if (file_idx == UINT32_MAX)
3274 continue;
3275 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003276
Greg Clayton526a4ae2012-05-16 22:09:01 +00003277 if (line != 0)
3278 {
3279 LineTable *line_table = sc.comp_unit->GetLineTable();
3280
3281 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003282 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003283 // We will have already looked up the file index if
3284 // we are searching for inline entries.
3285 if (!check_inlines)
3286 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003287
Greg Clayton526a4ae2012-05-16 22:09:01 +00003288 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003289 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003290 uint32_t found_line;
3291 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
3292 found_line = sc.line_entry.line;
3293
3294 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003295 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003296 sc.function = NULL;
3297 sc.block = NULL;
3298 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003299 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003300 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
3301 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003302 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00003303 DWARFDebugInfoEntry *function_die = NULL;
3304 DWARFDebugInfoEntry *block_die = NULL;
3305 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003306
Greg Clayton526a4ae2012-05-16 22:09:01 +00003307 if (function_die != NULL)
3308 {
3309 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
3310 if (sc.function == NULL)
3311 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
3312 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003313
Greg Clayton526a4ae2012-05-16 22:09:01 +00003314 if (sc.function != NULL)
3315 {
3316 Block& block = sc.function->GetBlock (true);
3317
3318 if (block_die != NULL)
3319 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Jason Molenda60db6e42014-10-16 01:40:16 +00003320 else if (function_die != NULL)
Greg Clayton526a4ae2012-05-16 22:09:01 +00003321 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
3322 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003323 }
3324 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003325
Greg Clayton526a4ae2012-05-16 22:09:01 +00003326 sc_list.Append(sc);
3327 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
3328 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003329 }
3330 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00003331 else if (file_spec_matches_cu_file_spec && !check_inlines)
3332 {
3333 // only append the context if we aren't looking for inline call sites
3334 // by file and line and if the file spec matches that of the compile unit
3335 sc_list.Append(sc);
3336 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003337 }
3338 else if (file_spec_matches_cu_file_spec && !check_inlines)
3339 {
3340 // only append the context if we aren't looking for inline call sites
3341 // by file and line and if the file spec matches that of the compile unit
3342 sc_list.Append(sc);
3343 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003344
Greg Clayton526a4ae2012-05-16 22:09:01 +00003345 if (!check_inlines)
3346 break;
3347 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003348 }
3349 }
3350 }
3351 }
3352 return sc_list.GetSize() - prev_size;
3353}
3354
3355void
3356SymbolFileDWARF::Index ()
3357{
3358 if (m_indexed)
3359 return;
3360 m_indexed = true;
3361 Timer scoped_timer (__PRETTY_FUNCTION__,
3362 "SymbolFileDWARF::Index (%s)",
Jim Ingham4af59612014-12-19 19:20:44 +00003363 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>"));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003364
3365 DWARFDebugInfo* debug_info = DebugInfo();
3366 if (debug_info)
3367 {
3368 uint32_t cu_idx = 0;
3369 const uint32_t num_compile_units = GetNumCompileUnits();
3370 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
3371 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003372 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003373
Greg Clayton53eb1c22012-04-02 22:59:12 +00003374 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003375
Greg Clayton53eb1c22012-04-02 22:59:12 +00003376 dwarf_cu->Index (cu_idx,
3377 m_function_basename_index,
3378 m_function_fullname_index,
3379 m_function_method_index,
3380 m_function_selector_index,
3381 m_objc_class_selectors_index,
3382 m_global_index,
3383 m_type_index,
3384 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003385
3386 // Keep memory down by clearing DIEs if this generate function
3387 // caused them to be parsed
3388 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00003389 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003390 }
3391
Greg Claytond4a2b372011-09-12 23:21:58 +00003392 m_function_basename_index.Finalize();
3393 m_function_fullname_index.Finalize();
3394 m_function_method_index.Finalize();
3395 m_function_selector_index.Finalize();
3396 m_objc_class_selectors_index.Finalize();
3397 m_global_index.Finalize();
3398 m_type_index.Finalize();
3399 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00003400
Greg Clayton24739922010-10-13 03:15:28 +00003401#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00003402 StreamFile s(stdout, false);
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00003403 s.Printf ("DWARF index for '%s':",
3404 GetObjectFile()->GetFileSpec().GetPath().c_str());
Greg Claytonba2d22d2010-11-13 22:57:37 +00003405 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
3406 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
3407 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
3408 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
3409 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
3410 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00003411 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00003412 s.Printf("\nNamespaces:\n") m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00003413#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003414 }
3415}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00003416
3417bool
3418SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
3419{
3420 if (namespace_decl == NULL)
3421 {
3422 // Invalid namespace decl which means we aren't matching only things
3423 // in this symbol file, so return true to indicate it matches this
3424 // symbol file.
3425 return true;
3426 }
3427
3428 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
3429
3430 if (namespace_ast == NULL)
3431 return true; // No AST in the "namespace_decl", return true since it
3432 // could then match any symbol file, including this one
3433
3434 if (namespace_ast == GetClangASTContext().getASTContext())
3435 return true; // The ASTs match, return true
3436
3437 // The namespace AST was valid, and it does not match...
Greg Clayton5160ce52013-03-27 23:08:40 +00003438 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Sean Callananc41e68b2011-10-13 21:08:11 +00003439
3440 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003441 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00003442
Greg Claytonbfe3dd42011-10-13 00:00:53 +00003443 return false;
3444}
3445
Greg Clayton2506a7a2011-10-12 23:34:26 +00003446bool
3447SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
3448 DWARFCompileUnit* cu,
3449 const DWARFDebugInfoEntry* die)
3450{
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00003451 // No namespace specified, so the answer is
Greg Clayton2506a7a2011-10-12 23:34:26 +00003452 if (namespace_decl == NULL)
3453 return true;
Sean Callananebe60672011-10-13 21:50:33 +00003454
Greg Clayton5160ce52013-03-27 23:08:40 +00003455 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00003456
Greg Clayton437a1352012-04-09 22:43:43 +00003457 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
3458 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00003459 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00003460 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00003461 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00003462
Greg Clayton2506a7a2011-10-12 23:34:26 +00003463 if (clang_namespace_decl)
3464 {
3465 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00003466 {
3467 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003468 GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00003469 return false;
Sean Callananebe60672011-10-13 21:50:33 +00003470 }
3471
Greg Clayton437a1352012-04-09 22:43:43 +00003472 if (clang_namespace_decl == die_clang_decl_ctx)
3473 return true;
3474 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00003475 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00003476 }
3477 else
3478 {
3479 // We have a namespace_decl that was not NULL but it contained
3480 // a NULL "clang::NamespaceDecl", so this means the global namespace
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003481 // So as long the contained decl context DIE isn't a namespace
Greg Clayton2506a7a2011-10-12 23:34:26 +00003482 // we should be ok.
3483 if (decl_ctx_die->Tag() != DW_TAG_namespace)
3484 return true;
3485 }
3486 }
Sean Callananebe60672011-10-13 21:50:33 +00003487
3488 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003489 GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00003490
Greg Clayton2506a7a2011-10-12 23:34:26 +00003491 return false;
3492}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003493uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00003494SymbolFileDWARF::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 +00003495{
Greg Clayton5160ce52013-03-27 23:08:40 +00003496 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003497
3498 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00003499 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003500 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
3501 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003502 static_cast<const void*>(namespace_decl),
3503 append, max_matches);
3504
Sean Callanan213fdb82011-10-13 01:49:10 +00003505 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00003506 return 0;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003507
Greg Claytonc685f8e2010-09-15 04:15:46 +00003508 DWARFDebugInfo* info = DebugInfo();
3509 if (info == NULL)
3510 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003511
3512 // If we aren't appending the results to this list, then clear the list
3513 if (!append)
3514 variables.Clear();
3515
3516 // Remember how many variables are in the list before we search in case
3517 // we are appending the results to a variable list.
3518 const uint32_t original_size = variables.GetSize();
3519
Greg Claytond4a2b372011-09-12 23:21:58 +00003520 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003521
Greg Clayton97fbc342011-10-20 22:30:33 +00003522 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003523 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003524 if (m_apple_names_ap.get())
3525 {
3526 const char *name_cstr = name.GetCString();
Jim Inghamfa39bb42014-10-25 00:33:55 +00003527 llvm::StringRef basename;
3528 llvm::StringRef context;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003529
Jim Inghamfa39bb42014-10-25 00:33:55 +00003530 if (!CPPLanguageRuntime::ExtractContextAndIdentifier(name_cstr, context, basename))
3531 basename = name_cstr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003532
Jim Inghamfa39bb42014-10-25 00:33:55 +00003533 m_apple_names_ap->FindByName (basename.data(), die_offsets);
Greg Clayton97fbc342011-10-20 22:30:33 +00003534 }
Greg Clayton7f995132011-10-04 22:41:51 +00003535 }
3536 else
3537 {
3538 // Index the DWARF if we haven't already
3539 if (!m_indexed)
3540 Index ();
3541
3542 m_global_index.Find (name, die_offsets);
3543 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003544
Greg Clayton437a1352012-04-09 22:43:43 +00003545 const size_t num_die_matches = die_offsets.size();
3546 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003547 {
Greg Clayton7f995132011-10-04 22:41:51 +00003548 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00003549 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00003550 assert (sc.module_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003551
Greg Claytond4a2b372011-09-12 23:21:58 +00003552 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00003553 DWARFCompileUnit* dwarf_cu = NULL;
3554 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00003555 bool done = false;
3556 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00003557 {
3558 const dw_offset_t die_offset = die_offsets[i];
3559 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003560
Greg Clayton95d87902011-11-11 03:16:25 +00003561 if (die)
3562 {
Greg Clayton437a1352012-04-09 22:43:43 +00003563 switch (die->Tag())
3564 {
3565 default:
3566 case DW_TAG_subprogram:
3567 case DW_TAG_inlined_subroutine:
3568 case DW_TAG_try_block:
3569 case DW_TAG_catch_block:
3570 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003571
Greg Clayton437a1352012-04-09 22:43:43 +00003572 case DW_TAG_variable:
3573 {
3574 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003575
Greg Clayton437a1352012-04-09 22:43:43 +00003576 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3577 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003578
Greg Clayton437a1352012-04-09 22:43:43 +00003579 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003580
Greg Clayton437a1352012-04-09 22:43:43 +00003581 if (variables.GetSize() - original_size >= max_matches)
3582 done = true;
3583 }
3584 break;
3585 }
Greg Clayton95d87902011-11-11 03:16:25 +00003586 }
3587 else
3588 {
3589 if (m_using_apple_tables)
3590 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003591 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
3592 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003593 }
3594 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003595 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003596 }
3597
3598 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003599 const uint32_t num_matches = variables.GetSize() - original_size;
3600 if (log && num_matches > 0)
3601 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003602 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00003603 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003604 name.GetCString(),
3605 static_cast<const void*>(namespace_decl),
3606 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00003607 num_matches);
3608 }
3609 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003610}
3611
3612uint32_t
3613SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
3614{
Greg Clayton5160ce52013-03-27 23:08:40 +00003615 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003616
Greg Clayton21f2a492011-10-06 00:09:08 +00003617 if (log)
3618 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003619 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003620 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00003621 regex.GetText(), append,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003622 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003623 }
3624
Greg Claytonc685f8e2010-09-15 04:15:46 +00003625 DWARFDebugInfo* info = DebugInfo();
3626 if (info == NULL)
3627 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003628
3629 // If we aren't appending the results to this list, then clear the list
3630 if (!append)
3631 variables.Clear();
3632
3633 // Remember how many variables are in the list before we search in case
3634 // we are appending the results to a variable list.
3635 const uint32_t original_size = variables.GetSize();
3636
Greg Clayton7f995132011-10-04 22:41:51 +00003637 DIEArray die_offsets;
3638
Greg Clayton97fbc342011-10-20 22:30:33 +00003639 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003640 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003641 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00003642 {
3643 DWARFMappedHash::DIEInfoArray hash_data_array;
3644 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
3645 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
3646 }
Greg Clayton7f995132011-10-04 22:41:51 +00003647 }
3648 else
3649 {
3650 // Index the DWARF if we haven't already
3651 if (!m_indexed)
3652 Index ();
3653
3654 m_global_index.Find (regex, die_offsets);
3655 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003656
Greg Claytonc685f8e2010-09-15 04:15:46 +00003657 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00003658 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00003659 assert (sc.module_sp);
3660
Greg Claytond4a2b372011-09-12 23:21:58 +00003661 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00003662 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003663 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003664 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003665 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003666 DWARFDebugInfo* debug_info = DebugInfo();
3667 for (size_t i=0; i<num_matches; ++i)
3668 {
3669 const dw_offset_t die_offset = die_offsets[i];
3670 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00003671
3672 if (die)
3673 {
3674 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003675
Greg Clayton95d87902011-11-11 03:16:25 +00003676 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003677
Greg Clayton95d87902011-11-11 03:16:25 +00003678 if (variables.GetSize() - original_size >= max_matches)
3679 break;
3680 }
3681 else
3682 {
3683 if (m_using_apple_tables)
3684 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003685 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
3686 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00003687 }
3688 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003689 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003690 }
3691
3692 // Return the number of variable that were appended to the list
3693 return variables.GetSize() - original_size;
3694}
3695
Greg Claytonaa044962011-10-13 00:59:38 +00003696
Jim Ingham4cda6e02011-10-07 22:23:45 +00003697bool
3698SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
3699 DWARFCompileUnit *&dwarf_cu,
Pavel Labatha73d6572015-03-13 10:22:00 +00003700 bool include_inlines,
Jim Ingham4cda6e02011-10-07 22:23:45 +00003701 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00003702{
Greg Claytonaa044962011-10-13 00:59:38 +00003703 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Pavel Labatha73d6572015-03-13 10:22:00 +00003704 return ResolveFunction (dwarf_cu, die, include_inlines, sc_list);
Greg Claytonaa044962011-10-13 00:59:38 +00003705}
3706
3707
3708bool
3709SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
3710 const DWARFDebugInfoEntry *die,
Pavel Labatha73d6572015-03-13 10:22:00 +00003711 bool include_inlines,
Greg Claytonaa044962011-10-13 00:59:38 +00003712 SymbolContextList& sc_list)
3713{
Greg Clayton9e315582011-09-02 04:03:59 +00003714 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00003715
3716 if (die == NULL)
3717 return false;
3718
Jim Ingham4cda6e02011-10-07 22:23:45 +00003719 // If we were passed a die that is not a function, just return false...
Pavel Labatha73d6572015-03-13 10:22:00 +00003720 if (! (die->Tag() == DW_TAG_subprogram || (include_inlines && die->Tag() == DW_TAG_inlined_subroutine)))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003721 return false;
3722
3723 const DWARFDebugInfoEntry* inlined_die = NULL;
3724 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00003725 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003726 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00003727
Jim Ingham4cda6e02011-10-07 22:23:45 +00003728 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003729 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003730 if (die->Tag() == DW_TAG_subprogram)
3731 break;
Greg Clayton9e315582011-09-02 04:03:59 +00003732 }
3733 }
Jason Molenda60db6e42014-10-16 01:40:16 +00003734 assert (die && die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00003735 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003736 {
3737 Address addr;
3738 // Parse all blocks if needed
3739 if (inlined_die)
3740 {
Greg Claytonf7bb1fb2015-01-15 03:13:44 +00003741 Block &function_block = sc.function->GetBlock (true);
3742 sc.block = function_block.FindBlockByID (MakeUserID(inlined_die->GetOffset()));
3743 if (sc.block == NULL)
3744 sc.block = function_block.FindBlockByID (inlined_die->GetOffset());
3745 if (sc.block == NULL || sc.block->GetStartAddress (addr) == false)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003746 addr.Clear();
3747 }
3748 else
3749 {
3750 sc.block = NULL;
3751 addr = sc.function->GetAddressRange().GetBaseAddress();
3752 }
3753
3754 if (addr.IsValid())
3755 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003756 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00003757 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003758 }
3759 }
3760
Greg Claytonaa044962011-10-13 00:59:38 +00003761 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00003762}
3763
Greg Clayton7f995132011-10-04 22:41:51 +00003764void
3765SymbolFileDWARF::FindFunctions (const ConstString &name,
3766 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00003767 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00003768 SymbolContextList& sc_list)
3769{
Greg Claytond4a2b372011-09-12 23:21:58 +00003770 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003771 if (name_to_die.Find (name, die_offsets))
3772 {
Pavel Labatha73d6572015-03-13 10:22:00 +00003773 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003774 }
3775}
3776
3777
3778void
3779SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3780 const NameToDIE &name_to_die,
Pavel Labatha73d6572015-03-13 10:22:00 +00003781 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00003782 SymbolContextList& sc_list)
3783{
3784 DIEArray die_offsets;
3785 if (name_to_die.Find (regex, die_offsets))
3786 {
Pavel Labatha73d6572015-03-13 10:22:00 +00003787 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003788 }
3789}
3790
3791
3792void
3793SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3794 const DWARFMappedHash::MemoryTable &memory_table,
Pavel Labatha73d6572015-03-13 10:22:00 +00003795 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00003796 SymbolContextList& sc_list)
3797{
3798 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00003799 DWARFMappedHash::DIEInfoArray hash_data_array;
3800 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00003801 {
Greg Claytond1767f02011-12-08 02:13:16 +00003802 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Pavel Labatha73d6572015-03-13 10:22:00 +00003803 ParseFunctions (die_offsets, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003804 }
3805}
3806
3807void
3808SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
Pavel Labatha73d6572015-03-13 10:22:00 +00003809 bool include_inlines,
Greg Clayton7f995132011-10-04 22:41:51 +00003810 SymbolContextList& sc_list)
3811{
3812 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003813 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00003814 {
Greg Clayton7f995132011-10-04 22:41:51 +00003815 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003816 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00003817 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003818 const dw_offset_t die_offset = die_offsets[i];
Pavel Labatha73d6572015-03-13 10:22:00 +00003819 ResolveFunction (die_offset, dwarf_cu, include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003820 }
3821 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00003822}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003823
Jim Ingham4cda6e02011-10-07 22:23:45 +00003824bool
3825SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
3826 const DWARFCompileUnit *dwarf_cu,
3827 uint32_t name_type_mask,
3828 const char *partial_name,
3829 const char *base_name_start,
3830 const char *base_name_end)
3831{
Greg Claytonfbea0f62012-11-15 18:05:43 +00003832 // If we are looking only for methods, throw away all the ones that are or aren't in C++ classes:
3833 if (name_type_mask == eFunctionNameTypeMethod || name_type_mask == eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003834 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003835 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
3836 if (!containing_decl_ctx)
3837 return false;
3838
3839 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
3840
Greg Claytonfbea0f62012-11-15 18:05:43 +00003841 if (name_type_mask == eFunctionNameTypeMethod)
3842 {
3843 if (is_cxx_method == false)
3844 return false;
3845 }
3846
3847 if (name_type_mask == eFunctionNameTypeBase)
3848 {
3849 if (is_cxx_method == true)
3850 return false;
3851 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003852 }
3853
3854 // Now we need to check whether the name we got back for this type matches the extra specifications
3855 // that were in the name we're looking up:
3856 if (base_name_start != partial_name || *base_name_end != '\0')
3857 {
3858 // First see if the stuff to the left matches the full name. To do that let's see if
3859 // we can pull out the mips linkage name attribute:
3860
3861 Mangled best_name;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003862 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonfbea0f62012-11-15 18:05:43 +00003863 DWARFFormValue form_value;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003864 die->GetAttributes(this, dwarf_cu, NULL, attributes);
3865 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
Greg Clayton71415542012-12-08 00:24:40 +00003866 if (idx == UINT32_MAX)
3867 idx = attributes.FindAttributeIndex(DW_AT_linkage_name);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003868 if (idx != UINT32_MAX)
3869 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003870 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3871 {
Greg Claytonfbea0f62012-11-15 18:05:43 +00003872 const char *mangled_name = form_value.AsCString(&get_debug_str_data());
3873 if (mangled_name)
3874 best_name.SetValue (ConstString(mangled_name), true);
3875 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003876 }
Greg Claytonfbea0f62012-11-15 18:05:43 +00003877
3878 if (!best_name)
3879 {
3880 idx = attributes.FindAttributeIndex(DW_AT_name);
3881 if (idx != UINT32_MAX && attributes.ExtractFormValueAtIndex(this, idx, form_value))
3882 {
3883 const char *name = form_value.AsCString(&get_debug_str_data());
3884 best_name.SetValue (ConstString(name), false);
3885 }
3886 }
3887
Greg Claytonddaf6a72015-07-08 22:32:23 +00003888 const LanguageType cu_language = const_cast<DWARFCompileUnit *>(dwarf_cu)->GetLanguageType();
3889 if (best_name.GetDemangledName(cu_language))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003890 {
Greg Claytonddaf6a72015-07-08 22:32:23 +00003891 const char *demangled = best_name.GetDemangledName(cu_language).GetCString();
Jim Ingham4cda6e02011-10-07 22:23:45 +00003892 if (demangled)
3893 {
3894 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003895 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3896 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003897 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003898 else
3899 {
3900 // Sort out the case where our name is something like "Process::Destroy" and the match is
3901 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3902 // namespace boundaries...
3903
3904 if (partial_name[0] == ':' && partial_name[1] == ':')
3905 {
3906 // The partial name was already on a namespace boundary so all matches are good.
3907 return true;
3908 }
3909 else if (partial_in_demangled == demangled)
3910 {
3911 // They both start the same, so this is an good match.
3912 return true;
3913 }
3914 else
3915 {
3916 if (partial_in_demangled - demangled == 1)
3917 {
3918 // Only one character difference, can't be a namespace boundary...
3919 return false;
3920 }
3921 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3922 {
3923 // We are on a namespace boundary, so this is also good.
3924 return true;
3925 }
3926 else
3927 return false;
3928 }
3929 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003930 }
3931 }
3932 }
3933
3934 return true;
3935}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003936
Greg Clayton0c5cd902010-06-28 21:30:43 +00003937uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003938SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003939 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003940 uint32_t name_type_mask,
3941 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003942 bool append,
3943 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003944{
3945 Timer scoped_timer (__PRETTY_FUNCTION__,
3946 "SymbolFileDWARF::FindFunctions (name = '%s')",
3947 name.AsCString());
3948
Greg Clayton43fe2172013-04-03 02:00:15 +00003949 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
3950 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
3951
Greg Clayton5160ce52013-03-27 23:08:40 +00003952 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00003953
3954 if (log)
3955 {
Greg Clayton5160ce52013-03-27 23:08:40 +00003956 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00003957 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3958 name.GetCString(),
3959 name_type_mask,
3960 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003961 }
3962
Greg Clayton0c5cd902010-06-28 21:30:43 +00003963 // If we aren't appending the results to this list, then clear the list
3964 if (!append)
3965 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003966
3967 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00003968 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003969
3970 // If name is empty then we won't find anything.
3971 if (name.IsEmpty())
3972 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003973
3974 // Remember how many sc_list are in the list before we search in case
3975 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003976
Jim Ingham4cda6e02011-10-07 22:23:45 +00003977 const char *name_cstr = name.GetCString();
Greg Clayton43fe2172013-04-03 02:00:15 +00003978
3979 const uint32_t original_size = sc_list.GetSize();
3980
Jim Ingham4cda6e02011-10-07 22:23:45 +00003981 DWARFDebugInfo* info = DebugInfo();
3982 if (info == NULL)
3983 return 0;
3984
Greg Claytonaa044962011-10-13 00:59:38 +00003985 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton43fe2172013-04-03 02:00:15 +00003986 std::set<const DWARFDebugInfoEntry *> resolved_dies;
Greg Clayton97fbc342011-10-20 22:30:33 +00003987 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003988 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003989 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003990 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003991
3992 DIEArray die_offsets;
3993
3994 uint32_t num_matches = 0;
3995
Greg Clayton43fe2172013-04-03 02:00:15 +00003996 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003997 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003998 // If they asked for the full name, match what they typed. At some point we may
3999 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
4000 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00004001 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00004002 for (uint32_t i = 0; i < num_matches; i++)
4003 {
Greg Clayton95d87902011-11-11 03:16:25 +00004004 const dw_offset_t die_offset = die_offsets[i];
4005 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00004006 if (die)
4007 {
Sean Callanan213fdb82011-10-13 01:49:10 +00004008 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
4009 continue;
4010
Greg Clayton43fe2172013-04-03 02:00:15 +00004011 if (resolved_dies.find(die) == resolved_dies.end())
4012 {
Pavel Labatha73d6572015-03-13 10:22:00 +00004013 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00004014 resolved_dies.insert(die);
4015 }
Greg Claytonaa044962011-10-13 00:59:38 +00004016 }
Greg Clayton95d87902011-11-11 03:16:25 +00004017 else
4018 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004019 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
4020 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00004021 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00004022 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004023 }
Greg Clayton43fe2172013-04-03 02:00:15 +00004024
4025 if (name_type_mask & eFunctionNameTypeSelector)
4026 {
4027 if (namespace_decl && *namespace_decl)
4028 return 0; // no selectors in namespaces
Greg Clayton97fbc342011-10-20 22:30:33 +00004029
Greg Clayton43fe2172013-04-03 02:00:15 +00004030 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
4031 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
4032 // and if it is an ObjC method name, we're good.
Greg Clayton97fbc342011-10-20 22:30:33 +00004033
Greg Clayton43fe2172013-04-03 02:00:15 +00004034 for (uint32_t i = 0; i < num_matches; i++)
Greg Clayton97fbc342011-10-20 22:30:33 +00004035 {
Greg Clayton43fe2172013-04-03 02:00:15 +00004036 const dw_offset_t die_offset = die_offsets[i];
4037 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
4038 if (die)
Greg Clayton97fbc342011-10-20 22:30:33 +00004039 {
Greg Clayton43fe2172013-04-03 02:00:15 +00004040 const char *die_name = die->GetName(this, dwarf_cu);
4041 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Greg Clayton97fbc342011-10-20 22:30:33 +00004042 {
Greg Clayton43fe2172013-04-03 02:00:15 +00004043 if (resolved_dies.find(die) == resolved_dies.end())
4044 {
Pavel Labatha73d6572015-03-13 10:22:00 +00004045 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00004046 resolved_dies.insert(die);
4047 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004048 }
4049 }
Greg Clayton43fe2172013-04-03 02:00:15 +00004050 else
4051 {
4052 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
4053 die_offset, name_cstr);
4054 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004055 }
Greg Clayton43fe2172013-04-03 02:00:15 +00004056 die_offsets.clear();
4057 }
4058
4059 if (((name_type_mask & eFunctionNameTypeMethod) && !namespace_decl) || name_type_mask & eFunctionNameTypeBase)
4060 {
4061 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
4062 // extract the base name, look that up, and if there is any other information in the name we were
4063 // passed in we have to post-filter based on that.
4064
4065 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
4066 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
4067
4068 for (uint32_t i = 0; i < num_matches; i++)
4069 {
4070 const dw_offset_t die_offset = die_offsets[i];
4071 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
4072 if (die)
4073 {
Greg Clayton43fe2172013-04-03 02:00:15 +00004074 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
4075 continue;
4076
4077 // If we get to here, the die is good, and we should add it:
4078 if (resolved_dies.find(die) == resolved_dies.end())
Pavel Labatha73d6572015-03-13 10:22:00 +00004079 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00004080 {
4081 bool keep_die = true;
4082 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod))
4083 {
4084 // We are looking for either basenames or methods, so we need to
4085 // trim out the ones we won't want by looking at the type
4086 SymbolContext sc;
4087 if (sc_list.GetLastContext(sc))
4088 {
4089 if (sc.block)
4090 {
4091 // We have an inlined function
4092 }
4093 else if (sc.function)
4094 {
4095 Type *type = sc.function->GetType();
4096
Sean Callananc370a8a2013-09-18 22:59:55 +00004097 if (type)
Greg Clayton43fe2172013-04-03 02:00:15 +00004098 {
Sean Callananc370a8a2013-09-18 22:59:55 +00004099 clang::DeclContext* decl_ctx = GetClangDeclContextContainingTypeUID (type->GetID());
4100 if (decl_ctx->isRecord())
Greg Clayton43fe2172013-04-03 02:00:15 +00004101 {
Sean Callananc370a8a2013-09-18 22:59:55 +00004102 if (name_type_mask & eFunctionNameTypeBase)
4103 {
4104 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
4105 keep_die = false;
4106 }
4107 }
4108 else
4109 {
4110 if (name_type_mask & eFunctionNameTypeMethod)
4111 {
4112 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1);
4113 keep_die = false;
4114 }
Greg Clayton43fe2172013-04-03 02:00:15 +00004115 }
4116 }
4117 else
4118 {
Sean Callananc370a8a2013-09-18 22:59:55 +00004119 GetObjectFile()->GetModule()->ReportWarning ("function at die offset 0x%8.8x had no function type",
4120 die_offset);
Greg Clayton43fe2172013-04-03 02:00:15 +00004121 }
4122 }
4123 }
4124 }
4125 if (keep_die)
4126 resolved_dies.insert(die);
4127 }
4128 }
4129 else
4130 {
4131 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
4132 die_offset, name_cstr);
4133 }
4134 }
4135 die_offsets.clear();
Jim Ingham4cda6e02011-10-07 22:23:45 +00004136 }
4137 }
Greg Clayton7f995132011-10-04 22:41:51 +00004138 }
4139 else
4140 {
4141
4142 // Index the DWARF if we haven't already
4143 if (!m_indexed)
4144 Index ();
4145
Greg Clayton7f995132011-10-04 22:41:51 +00004146 if (name_type_mask & eFunctionNameTypeFull)
Matt Kopecd6089962013-05-10 17:53:48 +00004147 {
Pavel Labatha73d6572015-03-13 10:22:00 +00004148 FindFunctions (name, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00004149
Ed Mastefc7baa02013-09-09 18:00:45 +00004150 // FIXME Temporary workaround for global/anonymous namespace
Robert Flack5cbd3bf2015-05-13 18:20:02 +00004151 // functions debugging FreeBSD and Linux binaries.
Matt Kopecd6089962013-05-10 17:53:48 +00004152 // If we didn't find any functions in the global namespace try
4153 // looking in the basename index but ignore any returned
Robert Flackeb83fab2015-05-15 18:59:59 +00004154 // functions that have a namespace but keep functions which
4155 // have an anonymous namespace
4156 // TODO: The arch in the object file isn't correct for MSVC
4157 // binaries on windows, we should find a way to make it
4158 // correct and handle those symbols as well.
Matt Kopecd6089962013-05-10 17:53:48 +00004159 if (sc_list.GetSize() == 0)
4160 {
Robert Flackeb83fab2015-05-15 18:59:59 +00004161 ArchSpec arch;
4162 if (!namespace_decl &&
4163 GetObjectFile()->GetArchitecture(arch) &&
4164 (arch.GetTriple().isOSFreeBSD() || arch.GetTriple().isOSLinux() ||
4165 arch.GetMachine() == llvm::Triple::hexagon))
Matt Kopecd6089962013-05-10 17:53:48 +00004166 {
Robert Flackeb83fab2015-05-15 18:59:59 +00004167 SymbolContextList temp_sc_list;
4168 FindFunctions (name, m_function_basename_index, include_inlines, temp_sc_list);
Matt Kopecd6089962013-05-10 17:53:48 +00004169 SymbolContext sc;
4170 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++)
4171 {
4172 if (temp_sc_list.GetContextAtIndex(i, sc))
4173 {
Matt Kopeca189d492013-05-10 22:55:24 +00004174 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled);
4175 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled);
Robert Flackeb83fab2015-05-15 18:59:59 +00004176 // Mangled names on Linux and FreeBSD are of the form:
4177 // _ZN18function_namespace13function_nameEv.
Matt Kopec04e5d582013-05-14 19:00:41 +00004178 if (strncmp(mangled_name.GetCString(), "_ZN", 3) ||
4179 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21))
Matt Kopecd6089962013-05-10 17:53:48 +00004180 {
4181 sc_list.Append(sc);
4182 }
4183 }
4184 }
4185 }
4186 }
Matt Kopecd6089962013-05-10 17:53:48 +00004187 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00004188 DIEArray die_offsets;
4189 DWARFCompileUnit *dwarf_cu = NULL;
4190
Greg Clayton43fe2172013-04-03 02:00:15 +00004191 if (name_type_mask & eFunctionNameTypeBase)
Jim Ingham4cda6e02011-10-07 22:23:45 +00004192 {
Greg Clayton43fe2172013-04-03 02:00:15 +00004193 uint32_t num_base = m_function_basename_index.Find(name, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00004194 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00004195 {
Greg Claytonaa044962011-10-13 00:59:38 +00004196 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
4197 if (die)
4198 {
Sean Callanan213fdb82011-10-13 01:49:10 +00004199 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
4200 continue;
4201
Greg Claytonaa044962011-10-13 00:59:38 +00004202 // If we get to here, the die is good, and we should add it:
Greg Clayton43fe2172013-04-03 02:00:15 +00004203 if (resolved_dies.find(die) == resolved_dies.end())
4204 {
Pavel Labatha73d6572015-03-13 10:22:00 +00004205 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00004206 resolved_dies.insert(die);
4207 }
Greg Claytonaa044962011-10-13 00:59:38 +00004208 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00004209 }
4210 die_offsets.clear();
4211 }
4212
Greg Clayton43fe2172013-04-03 02:00:15 +00004213 if (name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00004214 {
Sean Callanan213fdb82011-10-13 01:49:10 +00004215 if (namespace_decl && *namespace_decl)
4216 return 0; // no methods in namespaces
4217
Greg Clayton43fe2172013-04-03 02:00:15 +00004218 uint32_t num_base = m_function_method_index.Find(name, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00004219 {
Greg Claytonaa044962011-10-13 00:59:38 +00004220 for (uint32_t i = 0; i < num_base; i++)
4221 {
4222 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
4223 if (die)
4224 {
Greg Claytonaa044962011-10-13 00:59:38 +00004225 // If we get to here, the die is good, and we should add it:
Greg Clayton43fe2172013-04-03 02:00:15 +00004226 if (resolved_dies.find(die) == resolved_dies.end())
4227 {
Pavel Labatha73d6572015-03-13 10:22:00 +00004228 if (ResolveFunction (dwarf_cu, die, include_inlines, sc_list))
Greg Clayton43fe2172013-04-03 02:00:15 +00004229 resolved_dies.insert(die);
4230 }
Greg Claytonaa044962011-10-13 00:59:38 +00004231 }
4232 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00004233 }
4234 die_offsets.clear();
4235 }
Greg Clayton7f995132011-10-04 22:41:51 +00004236
Greg Clayton43fe2172013-04-03 02:00:15 +00004237 if ((name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00004238 {
Pavel Labatha73d6572015-03-13 10:22:00 +00004239 FindFunctions (name, m_function_selector_index, include_inlines, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00004240 }
4241
Greg Clayton4d01ace2011-09-29 16:58:15 +00004242 }
4243
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004244 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00004245 const uint32_t num_matches = sc_list.GetSize() - original_size;
4246
4247 if (log && num_matches > 0)
4248 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004249 GetObjectFile()->GetModule()->LogMessage (log,
Pavel Labatha73d6572015-03-13 10:22:00 +00004250 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => %u",
Greg Clayton437a1352012-04-09 22:43:43 +00004251 name.GetCString(),
4252 name_type_mask,
Pavel Labatha73d6572015-03-13 10:22:00 +00004253 include_inlines,
Greg Clayton437a1352012-04-09 22:43:43 +00004254 append,
4255 num_matches);
4256 }
4257 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004258}
4259
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004260uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00004261SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004262{
4263 Timer scoped_timer (__PRETTY_FUNCTION__,
4264 "SymbolFileDWARF::FindFunctions (regex = '%s')",
4265 regex.GetText());
4266
Greg Clayton5160ce52013-03-27 23:08:40 +00004267 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00004268
4269 if (log)
4270 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004271 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00004272 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
4273 regex.GetText(),
4274 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00004275 }
4276
4277
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004278 // If we aren't appending the results to this list, then clear the list
4279 if (!append)
4280 sc_list.Clear();
4281
4282 // Remember how many sc_list are in the list before we search in case
4283 // we are appending the results to a variable list.
4284 uint32_t original_size = sc_list.GetSize();
4285
Greg Clayton97fbc342011-10-20 22:30:33 +00004286 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004287 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004288 if (m_apple_names_ap.get())
Pavel Labatha73d6572015-03-13 10:22:00 +00004289 FindFunctions (regex, *m_apple_names_ap, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00004290 }
4291 else
4292 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00004293 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00004294 if (!m_indexed)
4295 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004296
Pavel Labatha73d6572015-03-13 10:22:00 +00004297 FindFunctions (regex, m_function_basename_index, include_inlines, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004298
Pavel Labatha73d6572015-03-13 10:22:00 +00004299 FindFunctions (regex, m_function_fullname_index, include_inlines, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00004300 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004301
4302 // Return the number of variable that were appended to the list
4303 return sc_list.GetSize() - original_size;
4304}
Jim Ingham318c9f22011-08-26 19:44:13 +00004305
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004306uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00004307SymbolFileDWARF::FindTypes (const SymbolContext& sc,
4308 const ConstString &name,
4309 const lldb_private::ClangNamespaceDecl *namespace_decl,
4310 bool append,
4311 uint32_t max_matches,
4312 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004313{
Greg Claytonc685f8e2010-09-15 04:15:46 +00004314 DWARFDebugInfo* info = DebugInfo();
4315 if (info == NULL)
4316 return 0;
4317
Greg Clayton5160ce52013-03-27 23:08:40 +00004318 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004319
Greg Clayton21f2a492011-10-06 00:09:08 +00004320 if (log)
4321 {
Greg Clayton437a1352012-04-09 22:43:43 +00004322 if (namespace_decl)
Greg Clayton5160ce52013-03-27 23:08:40 +00004323 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004324 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
4325 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004326 static_cast<void*>(namespace_decl->GetNamespaceDecl()),
Greg Clayton437a1352012-04-09 22:43:43 +00004327 namespace_decl->GetQualifiedName().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004328 append, max_matches);
Greg Clayton437a1352012-04-09 22:43:43 +00004329 else
Greg Clayton5160ce52013-03-27 23:08:40 +00004330 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004331 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004332 name.GetCString(), append,
Greg Clayton437a1352012-04-09 22:43:43 +00004333 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00004334 }
4335
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004336 // If we aren't appending the results to this list, then clear the list
4337 if (!append)
4338 types.Clear();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004339
Sean Callanan213fdb82011-10-13 01:49:10 +00004340 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00004341 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004342
Greg Claytond4a2b372011-09-12 23:21:58 +00004343 DIEArray die_offsets;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004344
Greg Clayton97fbc342011-10-20 22:30:33 +00004345 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004346 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004347 if (m_apple_types_ap.get())
4348 {
4349 const char *name_cstr = name.GetCString();
4350 m_apple_types_ap->FindByName (name_cstr, die_offsets);
4351 }
Greg Clayton7f995132011-10-04 22:41:51 +00004352 }
4353 else
4354 {
4355 if (!m_indexed)
4356 Index ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004357
Greg Clayton7f995132011-10-04 22:41:51 +00004358 m_type_index.Find (name, die_offsets);
4359 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004360
Greg Clayton437a1352012-04-09 22:43:43 +00004361 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00004362
Greg Clayton437a1352012-04-09 22:43:43 +00004363 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004364 {
Greg Clayton7f995132011-10-04 22:41:51 +00004365 const uint32_t initial_types_size = types.GetSize();
4366 DWARFCompileUnit* dwarf_cu = NULL;
4367 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004368 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00004369 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004370 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004371 const dw_offset_t die_offset = die_offsets[i];
4372 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
4373
Greg Clayton95d87902011-11-11 03:16:25 +00004374 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00004375 {
Greg Clayton95d87902011-11-11 03:16:25 +00004376 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
4377 continue;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004378
Greg Clayton95d87902011-11-11 03:16:25 +00004379 Type *matching_type = ResolveType (dwarf_cu, die);
4380 if (matching_type)
4381 {
4382 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00004383 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00004384 if (types.GetSize() >= max_matches)
4385 break;
4386 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00004387 }
Greg Clayton95d87902011-11-11 03:16:25 +00004388 else
4389 {
4390 if (m_using_apple_tables)
4391 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004392 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4393 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004394 }
4395 }
4396
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004397 }
Greg Clayton437a1352012-04-09 22:43:43 +00004398 const uint32_t num_matches = types.GetSize() - initial_types_size;
4399 if (log && num_matches)
4400 {
4401 if (namespace_decl)
4402 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004403 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004404 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
4405 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004406 static_cast<void*>(namespace_decl->GetNamespaceDecl()),
Greg Clayton437a1352012-04-09 22:43:43 +00004407 namespace_decl->GetQualifiedName().c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004408 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00004409 num_matches);
4410 }
4411 else
4412 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004413 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004414 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
4415 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004416 append, max_matches,
Greg Clayton437a1352012-04-09 22:43:43 +00004417 num_matches);
4418 }
4419 }
4420 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004421 }
Greg Clayton7f995132011-10-04 22:41:51 +00004422 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004423}
4424
4425
Greg Clayton526e5af2010-11-13 03:52:47 +00004426ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00004427SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00004428 const ConstString &name,
4429 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004430{
Greg Clayton5160ce52013-03-27 23:08:40 +00004431 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Clayton21f2a492011-10-06 00:09:08 +00004432
4433 if (log)
4434 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004435 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00004436 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
4437 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00004438 }
Sean Callanan213fdb82011-10-13 01:49:10 +00004439
4440 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
Ed Maste4c24b122013-10-17 20:13:14 +00004441 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00004442
Greg Clayton526e5af2010-11-13 03:52:47 +00004443 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004444 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00004445 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00004446 {
Greg Clayton7f995132011-10-04 22:41:51 +00004447 DIEArray die_offsets;
4448
Greg Clayton526e5af2010-11-13 03:52:47 +00004449 // Index if we already haven't to make sure the compile units
4450 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00004451 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004452 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004453 if (m_apple_namespaces_ap.get())
4454 {
4455 const char *name_cstr = name.GetCString();
4456 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
4457 }
Greg Clayton7f995132011-10-04 22:41:51 +00004458 }
4459 else
4460 {
4461 if (!m_indexed)
4462 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00004463
Greg Clayton7f995132011-10-04 22:41:51 +00004464 m_namespace_index.Find (name, die_offsets);
4465 }
Greg Claytond4a2b372011-09-12 23:21:58 +00004466
4467 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00004468 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00004469 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00004470 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00004471 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004472 DWARFDebugInfo* debug_info = DebugInfo();
4473 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00004474 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004475 const dw_offset_t die_offset = die_offsets[i];
4476 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00004477
Greg Clayton95d87902011-11-11 03:16:25 +00004478 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00004479 {
Greg Clayton95d87902011-11-11 03:16:25 +00004480 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
4481 continue;
4482
4483 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
4484 if (clang_namespace_decl)
4485 {
4486 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
4487 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00004488 break;
Greg Clayton95d87902011-11-11 03:16:25 +00004489 }
Greg Claytond4a2b372011-09-12 23:21:58 +00004490 }
Greg Clayton95d87902011-11-11 03:16:25 +00004491 else
4492 {
4493 if (m_using_apple_tables)
4494 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004495 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
4496 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004497 }
4498 }
4499
Greg Clayton526e5af2010-11-13 03:52:47 +00004500 }
4501 }
Greg Clayton96d7d742010-11-10 23:42:09 +00004502 }
Greg Clayton437a1352012-04-09 22:43:43 +00004503 if (log && namespace_decl.GetNamespaceDecl())
4504 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004505 GetObjectFile()->GetModule()->LogMessage (log,
Greg Clayton437a1352012-04-09 22:43:43 +00004506 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
4507 name.GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004508 static_cast<const void*>(namespace_decl.GetNamespaceDecl()),
Greg Clayton437a1352012-04-09 22:43:43 +00004509 namespace_decl.GetQualifiedName().c_str());
4510 }
4511
Greg Clayton526e5af2010-11-13 03:52:47 +00004512 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004513}
4514
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004515uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004516SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004517{
4518 // Remember how many sc_list are in the list before we search in case
4519 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004520 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004521
4522 const uint32_t num_die_offsets = die_offsets.size();
4523 // Parse all of the types we found from the pubtypes matches
4524 uint32_t i;
4525 uint32_t num_matches = 0;
4526 for (i = 0; i < num_die_offsets; ++i)
4527 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004528 Type *matching_type = ResolveTypeUID (die_offsets[i]);
4529 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004530 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004531 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00004532 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004533 ++num_matches;
4534 if (num_matches >= max_matches)
4535 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004536 }
4537 }
4538
4539 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004540 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004541}
4542
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004543
4544size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00004545SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
4546 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00004547 DWARFCompileUnit* dwarf_cu,
4548 const DWARFDebugInfoEntry *parent_die,
4549 bool skip_artificial,
4550 bool &is_static,
Greg Clayton03969752014-02-24 18:53:11 +00004551 bool &is_variadic,
Greg Clayton57ee3062013-07-11 22:46:58 +00004552 std::vector<ClangASTType>& function_param_types,
Greg Clayton5113dc82011-08-12 06:47:54 +00004553 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Claytond20deac2014-04-04 18:15:18 +00004554 unsigned &type_quals) // ,
4555 // ClangASTContext::TemplateParameterInfos &template_param_infos))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004556{
4557 if (parent_die == NULL)
4558 return 0;
4559
Todd Fialaee8bfc62014-09-11 17:29:12 +00004560 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Greg Claytond88d7592010-09-15 08:33:30 +00004561
Greg Clayton7fedea22010-11-16 02:10:54 +00004562 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004563 const DWARFDebugInfoEntry *die;
4564 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4565 {
4566 dw_tag_t tag = die->Tag();
4567 switch (tag)
4568 {
4569 case DW_TAG_formal_parameter:
4570 {
4571 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004572 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004573 if (num_attributes > 0)
4574 {
4575 const char *name = NULL;
4576 Declaration decl;
4577 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00004578 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004579 // one of None, Auto, Register, Extern, Static, PrivateExtern
4580
Sean Callanane2ef6e32010-09-23 03:01:22 +00004581 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004582 uint32_t i;
4583 for (i=0; i<num_attributes; ++i)
4584 {
4585 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4586 DWARFFormValue form_value;
4587 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4588 {
4589 switch (attr)
4590 {
4591 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4592 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4593 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4594 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00004595 case DW_AT_type: param_type_die_offset = form_value.Reference(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00004596 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004597 case DW_AT_location:
4598 // if (form_value.BlockData())
4599 // {
Ed Masteeeae7212013-10-24 20:43:47 +00004600 // const DWARFDataExtractor& debug_info_data = debug_info();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004601 // uint32_t block_length = form_value.Unsigned();
Ed Masteeeae7212013-10-24 20:43:47 +00004602 // DWARFDataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004603 // }
4604 // else
4605 // {
4606 // }
4607 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004608 case DW_AT_const_value:
4609 case DW_AT_default_value:
4610 case DW_AT_description:
4611 case DW_AT_endianity:
4612 case DW_AT_is_optional:
4613 case DW_AT_segment:
4614 case DW_AT_variable_parameter:
4615 default:
4616 case DW_AT_abstract_origin:
4617 case DW_AT_sibling:
4618 break;
4619 }
4620 }
4621 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00004622
Greg Clayton0fffff52010-09-24 05:15:53 +00004623 bool skip = false;
4624 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004625 {
Greg Clayton0fffff52010-09-24 05:15:53 +00004626 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00004627 {
4628 // In order to determine if a C++ member function is
4629 // "const" we have to look at the const-ness of "this"...
4630 // Ugly, but that
4631 if (arg_idx == 0)
4632 {
Greg Claytonf0705c82011-10-22 03:33:13 +00004633 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00004634 {
Greg Clayton5113dc82011-08-12 06:47:54 +00004635 // Often times compilers omit the "this" name for the
4636 // specification DIEs, so we can't rely upon the name
4637 // being in the formal parameter DIE...
4638 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00004639 {
Greg Clayton5113dc82011-08-12 06:47:54 +00004640 Type *this_type = ResolveTypeUID (param_type_die_offset);
4641 if (this_type)
4642 {
4643 uint32_t encoding_mask = this_type->GetEncodingMask();
4644 if (encoding_mask & Type::eEncodingIsPointerUID)
4645 {
4646 is_static = false;
4647
4648 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
4649 type_quals |= clang::Qualifiers::Const;
4650 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
4651 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00004652 }
4653 }
4654 }
4655 }
4656 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004657 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00004658 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004659 else
4660 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004661
Greg Clayton0fffff52010-09-24 05:15:53 +00004662 // HACK: Objective C formal parameters "self" and "_cmd"
4663 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00004664 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
4665 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00004666 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004667 switch (comp_unit->GetLanguage())
4668 {
4669 case eLanguageTypeObjC:
4670 case eLanguageTypeObjC_plus_plus:
4671 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
4672 skip = true;
4673 break;
4674 default:
4675 break;
4676 }
Greg Clayton0fffff52010-09-24 05:15:53 +00004677 }
4678 }
4679 }
4680
4681 if (!skip)
4682 {
4683 Type *type = ResolveTypeUID(param_type_die_offset);
4684 if (type)
4685 {
Greg Claytonc93237c2010-10-01 20:48:32 +00004686 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00004687
Greg Clayton42ce2f32011-12-12 21:50:19 +00004688 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
4689 type->GetClangForwardType(),
4690 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00004691 assert(param_var_decl);
4692 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00004693
Greg Claytond0029442013-03-27 01:48:02 +00004694 GetClangASTContext().SetMetadataAsUserID (param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00004695 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004696 }
4697 }
Greg Clayton7fedea22010-11-16 02:10:54 +00004698 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004699 }
4700 break;
4701
Greg Clayton03969752014-02-24 18:53:11 +00004702 case DW_TAG_unspecified_parameters:
4703 is_variadic = true;
4704 break;
4705
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00004706 case DW_TAG_template_type_parameter:
4707 case DW_TAG_template_value_parameter:
Greg Claytond20deac2014-04-04 18:15:18 +00004708 // The one caller of this was never using the template_param_infos,
4709 // and the local variable was taking up a large amount of stack space
4710 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need
4711 // the template params back, we can add them back.
4712 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00004713 break;
4714
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004715 default:
4716 break;
4717 }
4718 }
Greg Clayton7fedea22010-11-16 02:10:54 +00004719 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004720}
4721
4722size_t
4723SymbolFileDWARF::ParseChildEnumerators
4724(
4725 const SymbolContext& sc,
Greg Clayton57ee3062013-07-11 22:46:58 +00004726 lldb_private::ClangASTType &clang_type,
Greg Clayton3e067532013-03-05 23:54:39 +00004727 bool is_signed,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004728 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00004729 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004730 const DWARFDebugInfoEntry *parent_die
4731)
4732{
4733 if (parent_die == NULL)
4734 return 0;
4735
4736 size_t enumerators_added = 0;
4737 const DWARFDebugInfoEntry *die;
Todd Fialaee8bfc62014-09-11 17:29:12 +00004738 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Greg Claytond88d7592010-09-15 08:33:30 +00004739
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004740 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4741 {
4742 const dw_tag_t tag = die->Tag();
4743 if (tag == DW_TAG_enumerator)
4744 {
4745 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004746 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004747 if (num_child_attributes > 0)
4748 {
4749 const char *name = NULL;
4750 bool got_value = false;
4751 int64_t enum_value = 0;
4752 Declaration decl;
4753
4754 uint32_t i;
4755 for (i=0; i<num_child_attributes; ++i)
4756 {
4757 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4758 DWARFFormValue form_value;
4759 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4760 {
4761 switch (attr)
4762 {
4763 case DW_AT_const_value:
4764 got_value = true;
Greg Clayton3e067532013-03-05 23:54:39 +00004765 if (is_signed)
4766 enum_value = form_value.Signed();
4767 else
4768 enum_value = form_value.Unsigned();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004769 break;
4770
4771 case DW_AT_name:
4772 name = form_value.AsCString(&get_debug_str_data());
4773 break;
4774
4775 case DW_AT_description:
4776 default:
4777 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4778 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4779 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4780 case DW_AT_sibling:
4781 break;
4782 }
4783 }
4784 }
4785
4786 if (name && name[0] && got_value)
4787 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +00004788 clang_type.AddEnumerationValueToEnumerationType (clang_type.GetEnumerationIntegerType(),
4789 decl,
4790 name,
4791 enum_value,
4792 enumerator_byte_size * 8);
4793 ++enumerators_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004794 }
4795 }
4796 }
4797 }
4798 return enumerators_added;
4799}
4800
4801void
4802SymbolFileDWARF::ParseChildArrayInfo
4803(
4804 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004805 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004806 const DWARFDebugInfoEntry *parent_die,
4807 int64_t& first_index,
4808 std::vector<uint64_t>& element_orders,
4809 uint32_t& byte_stride,
4810 uint32_t& bit_stride
4811)
4812{
4813 if (parent_die == NULL)
4814 return;
4815
4816 const DWARFDebugInfoEntry *die;
Todd Fialaee8bfc62014-09-11 17:29:12 +00004817 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004818 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4819 {
4820 const dw_tag_t tag = die->Tag();
4821 switch (tag)
4822 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004823 case DW_TAG_subrange_type:
4824 {
4825 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004826 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004827 if (num_child_attributes > 0)
4828 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004829 uint64_t num_elements = 0;
4830 uint64_t lower_bound = 0;
4831 uint64_t upper_bound = 0;
Greg Clayton4ef877f2012-12-06 02:33:54 +00004832 bool upper_bound_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004833 uint32_t i;
4834 for (i=0; i<num_child_attributes; ++i)
4835 {
4836 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4837 DWARFFormValue form_value;
4838 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4839 {
4840 switch (attr)
4841 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004842 case DW_AT_name:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004843 break;
4844
4845 case DW_AT_count:
4846 num_elements = form_value.Unsigned();
4847 break;
4848
4849 case DW_AT_bit_stride:
4850 bit_stride = form_value.Unsigned();
4851 break;
4852
4853 case DW_AT_byte_stride:
4854 byte_stride = form_value.Unsigned();
4855 break;
4856
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004857 case DW_AT_lower_bound:
4858 lower_bound = form_value.Unsigned();
4859 break;
4860
4861 case DW_AT_upper_bound:
Greg Clayton4ef877f2012-12-06 02:33:54 +00004862 upper_bound_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004863 upper_bound = form_value.Unsigned();
4864 break;
4865
4866 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004867 case DW_AT_abstract_origin:
4868 case DW_AT_accessibility:
4869 case DW_AT_allocated:
4870 case DW_AT_associated:
4871 case DW_AT_data_location:
4872 case DW_AT_declaration:
4873 case DW_AT_description:
4874 case DW_AT_sibling:
4875 case DW_AT_threads_scaled:
4876 case DW_AT_type:
4877 case DW_AT_visibility:
4878 break;
4879 }
4880 }
4881 }
4882
Greg Claytone6a07792012-12-05 21:59:39 +00004883 if (num_elements == 0)
4884 {
Greg Clayton4ef877f2012-12-06 02:33:54 +00004885 if (upper_bound_valid && upper_bound >= lower_bound)
Greg Claytone6a07792012-12-05 21:59:39 +00004886 num_elements = upper_bound - lower_bound + 1;
4887 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004888
Sean Callananec979ba2012-10-22 23:56:48 +00004889 element_orders.push_back (num_elements);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004890 }
4891 }
4892 break;
4893 }
4894 }
4895}
4896
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004897TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004898SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004899{
4900 TypeSP type_sp;
4901 if (die != NULL)
4902 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004903 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004904 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004905 if (type_ptr == NULL)
4906 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004907 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004908 assert (lldb_cu);
4909 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004910 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004911 }
4912 else if (type_ptr != DIE_IS_BEING_PARSED)
4913 {
4914 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004915 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004916 }
4917
4918 }
4919 return type_sp;
4920}
4921
4922clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004923SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004924{
4925 if (die_offset != DW_INVALID_OFFSET)
4926 {
4927 DWARFCompileUnitSP cu_sp;
4928 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004929 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004930 }
4931 return NULL;
4932}
4933
Sean Callanan72e49402011-08-05 23:43:37 +00004934clang::DeclContext *
4935SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4936{
4937 if (die_offset != DW_INVALID_OFFSET)
4938 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004939 DWARFDebugInfo* debug_info = DebugInfo();
4940 if (debug_info)
4941 {
4942 DWARFCompileUnitSP cu_sp;
4943 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4944 if (die)
4945 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4946 }
Sean Callanan72e49402011-08-05 23:43:37 +00004947 }
4948 return NULL;
4949}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004950
Greg Clayton96d7d742010-11-10 23:42:09 +00004951clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004952SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004953{
Greg Clayton030a2042011-10-14 21:34:45 +00004954 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004955 {
Greg Clayton030a2042011-10-14 21:34:45 +00004956 // See if we already parsed this namespace DIE and associated it with a
4957 // uniqued namespace declaration
4958 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4959 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004960 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004961 else
4962 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004963 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4964 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004965 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
Greg Clayton5160ce52013-03-27 23:08:40 +00004966 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton9d3d6882011-10-31 23:51:19 +00004967 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004968 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004969 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004970 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004971 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00004972 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004973 static_cast<void*>(GetClangASTContext().getASTContext()),
Greg Claytone38a5ed2012-01-05 03:57:59 +00004974 MakeUserID(die->GetOffset()),
4975 namespace_name,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004976 static_cast<void*>(namespace_decl),
4977 static_cast<void*>(namespace_decl->getOriginalNamespace()));
Greg Clayton030a2042011-10-14 21:34:45 +00004978 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004979 else
4980 {
Greg Clayton5160ce52013-03-27 23:08:40 +00004981 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00004982 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004983 static_cast<void*>(GetClangASTContext().getASTContext()),
Greg Claytone38a5ed2012-01-05 03:57:59 +00004984 MakeUserID(die->GetOffset()),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00004985 static_cast<void*>(namespace_decl),
4986 static_cast<void*>(namespace_decl->getOriginalNamespace()));
Greg Clayton9d3d6882011-10-31 23:51:19 +00004987 }
Greg Clayton030a2042011-10-14 21:34:45 +00004988 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004989
4990 if (namespace_decl)
4991 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4992 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004993 }
4994 }
4995 return NULL;
4996}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004997
4998clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004999SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00005000{
Greg Clayton5cf58b92011-10-05 22:22:08 +00005001 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
5002 if (clang_decl_ctx)
5003 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00005004 // If this DIE has a specification, or an abstract origin, then trace to those.
5005
Greg Claytoncab36a32011-12-08 05:16:30 +00005006 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00005007 if (die_offset != DW_INVALID_OFFSET)
5008 return GetClangDeclContextForDIEOffset (sc, die_offset);
5009
Greg Claytoncab36a32011-12-08 05:16:30 +00005010 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00005011 if (die_offset != DW_INVALID_OFFSET)
5012 return GetClangDeclContextForDIEOffset (sc, die_offset);
5013
Greg Clayton5160ce52013-03-27 23:08:40 +00005014 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005015 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005016 GetObjectFile()->GetModule()->LogMessage(log, "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 +00005017 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00005018 bool assert_not_being_parsed = true;
5019 ResolveTypeUID (cu, die, assert_not_being_parsed);
5020
Greg Clayton5cf58b92011-10-05 22:22:08 +00005021 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
5022
5023 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00005024}
5025
5026clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00005027SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005028{
Greg Claytonca512b32011-01-14 04:54:56 +00005029 if (m_clang_tu_decl == NULL)
5030 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005031
Greg Clayton2bc22f82011-09-30 03:20:47 +00005032 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00005033
5034 if (decl_ctx_die_copy)
5035 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00005036
5037 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005038 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00005039
Greg Clayton2bc22f82011-09-30 03:20:47 +00005040 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
5041 if (pos != m_die_to_decl_ctx.end())
5042 return pos->second;
5043
5044 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005045 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00005046 case DW_TAG_compile_unit:
5047 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005048
Greg Clayton2bc22f82011-09-30 03:20:47 +00005049 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00005050 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00005051 break;
5052
5053 case DW_TAG_structure_type:
5054 case DW_TAG_union_type:
5055 case DW_TAG_class_type:
5056 {
5057 Type* type = ResolveType (cu, decl_ctx_die);
5058 if (type)
5059 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +00005060 clang::DeclContext *decl_ctx = type->GetClangForwardType().GetDeclContextForType ();
Greg Clayton2bc22f82011-09-30 03:20:47 +00005061 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00005062 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00005063 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
5064 if (decl_ctx)
5065 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00005066 }
5067 }
Greg Claytonca512b32011-01-14 04:54:56 +00005068 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00005069 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005070
Greg Clayton2bc22f82011-09-30 03:20:47 +00005071 default:
5072 break;
Greg Claytonca512b32011-01-14 04:54:56 +00005073 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005074 }
Greg Clayton7a345282010-11-09 23:46:37 +00005075 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005076}
5077
Greg Clayton2bc22f82011-09-30 03:20:47 +00005078
5079const DWARFDebugInfoEntry *
5080SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
5081{
5082 if (cu && die)
5083 {
5084 const DWARFDebugInfoEntry * const decl_die = die;
5085
5086 while (die != NULL)
5087 {
5088 // If this is the original DIE that we are searching for a declaration
5089 // for, then don't look in the cache as we don't want our own decl
5090 // context to be our decl context...
5091 if (decl_die != die)
5092 {
5093 switch (die->Tag())
5094 {
5095 case DW_TAG_compile_unit:
5096 case DW_TAG_namespace:
5097 case DW_TAG_structure_type:
5098 case DW_TAG_union_type:
5099 case DW_TAG_class_type:
5100 return die;
5101
5102 default:
5103 break;
5104 }
5105 }
5106
5107 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
5108 if (die_offset != DW_INVALID_OFFSET)
5109 {
5110 DWARFCompileUnit *spec_cu = cu;
5111 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
5112 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
5113 if (spec_die_decl_ctx_die)
5114 return spec_die_decl_ctx_die;
5115 }
5116
5117 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
5118 if (die_offset != DW_INVALID_OFFSET)
5119 {
5120 DWARFCompileUnit *abs_cu = cu;
5121 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
5122 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
5123 if (abs_die_decl_ctx_die)
5124 return abs_die_decl_ctx_die;
5125 }
5126
5127 die = die->GetParent();
5128 }
5129 }
5130 return NULL;
5131}
5132
5133
Greg Clayton901c5ca2011-12-03 04:40:03 +00005134Symbol *
5135SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
5136{
5137 Symbol *objc_class_symbol = NULL;
5138 if (m_obj_file)
5139 {
Greg Clayton3046e662013-07-10 01:23:25 +00005140 Symtab *symtab = m_obj_file->GetSymtab ();
Greg Clayton901c5ca2011-12-03 04:40:03 +00005141 if (symtab)
5142 {
5143 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
5144 eSymbolTypeObjCClass,
5145 Symtab::eDebugNo,
5146 Symtab::eVisibilityAny);
5147 }
5148 }
5149 return objc_class_symbol;
5150}
5151
Greg Claytonc7f03b62012-01-12 04:33:28 +00005152// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
5153// then we can end up looking through all class types for a complete type and never find
5154// the full definition. We need to know if this attribute is supported, so we determine
5155// this here and cache th result. We also need to worry about the debug map DWARF file
5156// if we are doing darwin DWARF in .o file debugging.
5157bool
5158SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
5159{
5160 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
5161 {
5162 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
5163 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
5164 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
5165 else
5166 {
5167 DWARFDebugInfo* debug_info = DebugInfo();
5168 const uint32_t num_compile_units = GetNumCompileUnits();
5169 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
5170 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00005171 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
5172 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00005173 {
5174 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
5175 break;
5176 }
5177 }
5178 }
Greg Clayton1f746072012-08-29 21:13:06 +00005179 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00005180 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
5181 }
5182 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
5183}
Greg Clayton901c5ca2011-12-03 04:40:03 +00005184
5185// This function can be used when a DIE is found that is a forward declaration
5186// DIE and we want to try and find a type that has the complete definition.
5187TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00005188SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
5189 const ConstString &type_name,
5190 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00005191{
5192
5193 TypeSP type_sp;
5194
Greg Claytonc7f03b62012-01-12 04:33:28 +00005195 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00005196 return type_sp;
5197
5198 DIEArray die_offsets;
5199
5200 if (m_using_apple_tables)
5201 {
5202 if (m_apple_types_ap.get())
5203 {
5204 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00005205 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005206 }
5207 }
5208 else
5209 {
5210 if (!m_indexed)
5211 Index ();
5212
5213 m_type_index.Find (type_name, die_offsets);
5214 }
5215
Greg Clayton901c5ca2011-12-03 04:40:03 +00005216 const size_t num_matches = die_offsets.size();
5217
Greg Clayton901c5ca2011-12-03 04:40:03 +00005218 DWARFCompileUnit* type_cu = NULL;
5219 const DWARFDebugInfoEntry* type_die = NULL;
5220 if (num_matches)
5221 {
5222 DWARFDebugInfo* debug_info = DebugInfo();
5223 for (size_t i=0; i<num_matches; ++i)
5224 {
5225 const dw_offset_t die_offset = die_offsets[i];
5226 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
5227
5228 if (type_die)
5229 {
5230 bool try_resolving_type = false;
5231
5232 // Don't try and resolve the DIE we are looking for with the DIE itself!
5233 if (type_die != die)
5234 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005235 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00005236 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005237 case DW_TAG_class_type:
5238 case DW_TAG_structure_type:
5239 try_resolving_type = true;
5240 break;
5241 default:
5242 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00005243 }
5244 }
5245
5246 if (try_resolving_type)
5247 {
Ed Maste4c24b122013-10-17 20:13:14 +00005248 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
5249 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005250
5251 if (try_resolving_type)
5252 {
5253 Type *resolved_type = ResolveType (type_cu, type_die, false);
5254 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
5255 {
Ed Mastea0191d12013-10-17 20:42:56 +00005256 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n",
Greg Clayton901c5ca2011-12-03 04:40:03 +00005257 MakeUserID(die->GetOffset()),
Jim Ingham4af59612014-12-19 19:20:44 +00005258 m_obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Greg Clayton901c5ca2011-12-03 04:40:03 +00005259 MakeUserID(type_die->GetOffset()),
5260 MakeUserID(type_cu->GetOffset()));
5261
Greg Claytonc7f03b62012-01-12 04:33:28 +00005262 if (die)
5263 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00005264 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00005265 break;
5266 }
5267 }
5268 }
5269 }
5270 else
5271 {
5272 if (m_using_apple_tables)
5273 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005274 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
5275 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00005276 }
5277 }
5278
5279 }
5280 }
5281 return type_sp;
5282}
5283
Greg Claytona8022fa2012-04-24 21:22:41 +00005284
Greg Clayton80c26302012-02-05 06:12:47 +00005285//----------------------------------------------------------------------
5286// This function helps to ensure that the declaration contexts match for
5287// two different DIEs. Often times debug information will refer to a
5288// forward declaration of a type (the equivalent of "struct my_struct;".
5289// There will often be a declaration of that type elsewhere that has the
5290// full definition. When we go looking for the full type "my_struct", we
5291// will find one or more matches in the accelerator tables and we will
5292// then need to make sure the type was in the same declaration context
5293// as the original DIE. This function can efficiently compare two DIEs
5294// and will return true when the declaration context matches, and false
5295// when they don't.
5296//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00005297bool
5298SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
5299 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
5300{
Greg Claytona8022fa2012-04-24 21:22:41 +00005301 if (die1 == die2)
5302 return true;
5303
5304#if defined (LLDB_CONFIGURATION_DEBUG)
5305 // You can't and shouldn't call this function with a compile unit from
5306 // two different SymbolFileDWARF instances.
5307 assert (DebugInfo()->ContainsCompileUnit (cu1));
5308 assert (DebugInfo()->ContainsCompileUnit (cu2));
5309#endif
5310
Greg Clayton890ff562012-02-02 05:48:16 +00005311 DWARFDIECollection decl_ctx_1;
5312 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00005313 //The declaration DIE stack is a stack of the declaration context
5314 // DIEs all the way back to the compile unit. If a type "T" is
5315 // declared inside a class "B", and class "B" is declared inside
5316 // a class "A" and class "A" is in a namespace "lldb", and the
5317 // namespace is in a compile unit, there will be a stack of DIEs:
5318 //
5319 // [0] DW_TAG_class_type for "B"
5320 // [1] DW_TAG_class_type for "A"
5321 // [2] DW_TAG_namespace for "lldb"
5322 // [3] DW_TAG_compile_unit for the source file.
5323 //
5324 // We grab both contexts and make sure that everything matches
5325 // all the way back to the compiler unit.
5326
5327 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00005328 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00005329 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00005330 // Make sure the context arrays have the same size, otherwise
5331 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00005332 const size_t count1 = decl_ctx_1.Size();
5333 const size_t count2 = decl_ctx_2.Size();
5334 if (count1 != count2)
5335 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00005336
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00005337 // Make sure the DW_TAG values match all the way back up the
Greg Clayton80c26302012-02-05 06:12:47 +00005338 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00005339 const DWARFDebugInfoEntry *decl_ctx_die1;
5340 const DWARFDebugInfoEntry *decl_ctx_die2;
5341 size_t i;
5342 for (i=0; i<count1; i++)
5343 {
5344 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
5345 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
5346 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
5347 return false;
5348 }
Greg Clayton890ff562012-02-02 05:48:16 +00005349#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00005350
5351 // Make sure the top item in the decl context die array is always
5352 // DW_TAG_compile_unit. If it isn't then something went wrong in
5353 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00005354 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00005355
Greg Clayton890ff562012-02-02 05:48:16 +00005356#endif
5357 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00005358 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00005359 for (i=0; i<count1 - 1; i++)
5360 {
5361 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
5362 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
5363 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00005364 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00005365 // If the string was from a DW_FORM_strp, then the pointer will often
5366 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00005367 if (name1 == name2)
5368 continue;
5369
5370 // Name pointers are not equal, so only compare the strings
5371 // if both are not NULL.
5372 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00005373 {
Greg Clayton5569e642012-02-06 01:44:54 +00005374 // If the strings don't compare, we are done...
5375 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00005376 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00005377 }
5378 else
5379 {
5380 // One name was NULL while the other wasn't
5381 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00005382 }
5383 }
Greg Clayton80c26302012-02-05 06:12:47 +00005384 // We made it through all of the checks and the declaration contexts
5385 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00005386 return true;
5387}
Greg Clayton220a0072011-12-09 08:48:30 +00005388
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00005389
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005390TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00005391SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
5392{
5393 TypeSP type_sp;
5394
5395 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
5396 if (dwarf_decl_ctx_count > 0)
5397 {
5398 const ConstString type_name(dwarf_decl_ctx[0].name);
5399 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
5400
5401 if (type_name)
5402 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005403 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
Greg Claytona8022fa2012-04-24 21:22:41 +00005404 if (log)
5405 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005406 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00005407 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
5408 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5409 dwarf_decl_ctx.GetQualifiedName());
5410 }
5411
5412 DIEArray die_offsets;
5413
5414 if (m_using_apple_tables)
5415 {
5416 if (m_apple_types_ap.get())
5417 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005418 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag);
5419 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash);
5420 if (has_tag && has_qualified_name_hash)
Greg Claytona8022fa2012-04-24 21:22:41 +00005421 {
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005422 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName();
5423 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name);
5424 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005425 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()");
Greg Claytoncb9c8cf2013-02-06 23:56:13 +00005426 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets);
5427 }
5428 else if (has_tag)
5429 {
5430 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00005431 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()");
Greg Claytona8022fa2012-04-24 21:22:41 +00005432 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
5433 }
5434 else
5435 {
5436 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
5437 }
5438 }
5439 }
5440 else
5441 {
5442 if (!m_indexed)
5443 Index ();
5444
5445 m_type_index.Find (type_name, die_offsets);
5446 }
5447
5448 const size_t num_matches = die_offsets.size();
5449
5450
5451 DWARFCompileUnit* type_cu = NULL;
5452 const DWARFDebugInfoEntry* type_die = NULL;
5453 if (num_matches)
5454 {
5455 DWARFDebugInfo* debug_info = DebugInfo();
5456 for (size_t i=0; i<num_matches; ++i)
5457 {
5458 const dw_offset_t die_offset = die_offsets[i];
5459 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
5460
5461 if (type_die)
5462 {
5463 bool try_resolving_type = false;
5464
5465 // Don't try and resolve the DIE we are looking for with the DIE itself!
5466 const dw_tag_t type_tag = type_die->Tag();
5467 // Make sure the tags match
5468 if (type_tag == tag)
5469 {
5470 // The tags match, lets try resolving this type
5471 try_resolving_type = true;
5472 }
5473 else
5474 {
5475 // The tags don't match, but we need to watch our for a
5476 // forward declaration for a struct and ("struct foo")
5477 // ends up being a class ("class foo { ... };") or
5478 // vice versa.
5479 switch (type_tag)
5480 {
5481 case DW_TAG_class_type:
5482 // We had a "class foo", see if we ended up with a "struct foo { ... };"
5483 try_resolving_type = (tag == DW_TAG_structure_type);
5484 break;
5485 case DW_TAG_structure_type:
5486 // We had a "struct foo", see if we ended up with a "class foo { ... };"
5487 try_resolving_type = (tag == DW_TAG_class_type);
5488 break;
5489 default:
5490 // Tags don't match, don't event try to resolve
5491 // using this type whose name matches....
5492 break;
5493 }
5494 }
5495
5496 if (try_resolving_type)
5497 {
5498 DWARFDeclContext type_dwarf_decl_ctx;
5499 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
5500
5501 if (log)
5502 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005503 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00005504 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
5505 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5506 dwarf_decl_ctx.GetQualifiedName(),
5507 type_die->GetOffset(),
5508 type_dwarf_decl_ctx.GetQualifiedName());
5509 }
5510
5511 // Make sure the decl contexts match all the way up
5512 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
5513 {
5514 Type *resolved_type = ResolveType (type_cu, type_die, false);
5515 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
5516 {
5517 type_sp = resolved_type->shared_from_this();
5518 break;
5519 }
5520 }
5521 }
5522 else
5523 {
5524 if (log)
5525 {
5526 std::string qualified_name;
5527 type_die->GetQualifiedName(this, type_cu, qualified_name);
Greg Clayton5160ce52013-03-27 23:08:40 +00005528 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytona8022fa2012-04-24 21:22:41 +00005529 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
5530 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
5531 dwarf_decl_ctx.GetQualifiedName(),
5532 type_die->GetOffset(),
5533 qualified_name.c_str());
5534 }
5535 }
5536 }
5537 else
5538 {
5539 if (m_using_apple_tables)
5540 {
5541 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
5542 die_offset, type_name.GetCString());
5543 }
5544 }
5545
5546 }
5547 }
5548 }
5549 }
5550 return type_sp;
5551}
5552
Greg Clayton4116e932012-05-15 02:33:01 +00005553bool
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005554SymbolFileDWARF::CopyUniqueClassMethodTypes (SymbolFileDWARF *src_symfile,
Sean Callanan2367f8a2013-02-26 01:12:25 +00005555 Type *class_type,
Greg Clayton4116e932012-05-15 02:33:01 +00005556 DWARFCompileUnit* src_cu,
5557 const DWARFDebugInfoEntry *src_class_die,
5558 DWARFCompileUnit* dst_cu,
Sean Callanan2367f8a2013-02-26 01:12:25 +00005559 const DWARFDebugInfoEntry *dst_class_die,
Greg Claytond20deac2014-04-04 18:15:18 +00005560 DWARFDIECollection &failures)
Greg Clayton4116e932012-05-15 02:33:01 +00005561{
5562 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
5563 return false;
5564 if (src_class_die->Tag() != dst_class_die->Tag())
5565 return false;
5566
5567 // We need to complete the class type so we can get all of the method types
5568 // parsed so we can then unique those types to their equivalent counterparts
5569 // in "dst_cu" and "dst_class_die"
5570 class_type->GetClangFullType();
5571
5572 const DWARFDebugInfoEntry *src_die;
5573 const DWARFDebugInfoEntry *dst_die;
5574 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
5575 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
Greg Clayton65ec1032012-11-12 21:27:20 +00005576 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die_artificial;
5577 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die_artificial;
Greg Clayton4116e932012-05-15 02:33:01 +00005578 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
5579 {
5580 if (src_die->Tag() == DW_TAG_subprogram)
5581 {
Greg Clayton84afacd2012-11-07 23:09:32 +00005582 // Make sure this is a declaration and not a concrete instance by looking
5583 // for DW_AT_declaration set to 1. Sometimes concrete function instances
5584 // are placed inside the class definitions and shouldn't be included in
5585 // the list of things are are tracking here.
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005586 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_declaration, 0) == 1)
Greg Clayton84afacd2012-11-07 23:09:32 +00005587 {
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005588 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
Greg Clayton84afacd2012-11-07 23:09:32 +00005589 if (src_name)
Greg Clayton65ec1032012-11-12 21:27:20 +00005590 {
5591 ConstString src_const_name(src_name);
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005592 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_artificial, 0))
Greg Clayton65ec1032012-11-12 21:27:20 +00005593 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die);
5594 else
5595 src_name_to_die.Append(src_const_name.GetCString(), src_die);
5596 }
Greg Clayton84afacd2012-11-07 23:09:32 +00005597 }
Greg Clayton4116e932012-05-15 02:33:01 +00005598 }
5599 }
5600 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
5601 {
5602 if (dst_die->Tag() == DW_TAG_subprogram)
5603 {
Greg Clayton84afacd2012-11-07 23:09:32 +00005604 // Make sure this is a declaration and not a concrete instance by looking
5605 // for DW_AT_declaration set to 1. Sometimes concrete function instances
5606 // are placed inside the class definitions and shouldn't be included in
5607 // the list of things are are tracking here.
5608 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_declaration, 0) == 1)
5609 {
5610 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
5611 if (dst_name)
Greg Clayton65ec1032012-11-12 21:27:20 +00005612 {
5613 ConstString dst_const_name(dst_name);
5614 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_artificial, 0))
5615 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die);
5616 else
5617 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die);
5618 }
Greg Clayton84afacd2012-11-07 23:09:32 +00005619 }
Greg Clayton4116e932012-05-15 02:33:01 +00005620 }
5621 }
5622 const uint32_t src_size = src_name_to_die.GetSize ();
5623 const uint32_t dst_size = dst_name_to_die.GetSize ();
Greg Clayton5160ce52013-03-27 23:08:40 +00005624 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
Sean Callanan2367f8a2013-02-26 01:12:25 +00005625
5626 // Is everything kosher so we can go through the members at top speed?
5627 bool fast_path = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005628
Sean Callanan2367f8a2013-02-26 01:12:25 +00005629 if (src_size != dst_size)
Greg Clayton4116e932012-05-15 02:33:01 +00005630 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00005631 if (src_size != 0 && dst_size != 0)
5632 {
5633 if (log)
5634 log->Printf("warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)",
5635 src_class_die->GetOffset(),
5636 dst_class_die->GetOffset(),
5637 src_size,
5638 dst_size);
5639 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005640
Sean Callanan2367f8a2013-02-26 01:12:25 +00005641 fast_path = false;
5642 }
5643
5644 uint32_t idx;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005645
Sean Callanan2367f8a2013-02-26 01:12:25 +00005646 if (fast_path)
5647 {
Greg Clayton4116e932012-05-15 02:33:01 +00005648 for (idx = 0; idx < src_size; ++idx)
5649 {
5650 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5651 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
5652
5653 if (src_die->Tag() != dst_die->Tag())
5654 {
5655 if (log)
5656 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
5657 src_class_die->GetOffset(),
5658 dst_class_die->GetOffset(),
5659 src_die->GetOffset(),
5660 DW_TAG_value_to_name(src_die->Tag()),
5661 dst_die->GetOffset(),
5662 DW_TAG_value_to_name(src_die->Tag()));
Sean Callanan2367f8a2013-02-26 01:12:25 +00005663 fast_path = false;
Greg Clayton4116e932012-05-15 02:33:01 +00005664 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005665
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005666 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
Greg Clayton4116e932012-05-15 02:33:01 +00005667 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005668
Greg Clayton4116e932012-05-15 02:33:01 +00005669 // Make sure the names match
5670 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
5671 continue;
5672
5673 if (log)
5674 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
5675 src_class_die->GetOffset(),
5676 dst_class_die->GetOffset(),
5677 src_die->GetOffset(),
5678 src_name,
5679 dst_die->GetOffset(),
5680 dst_name);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005681
Sean Callanan2367f8a2013-02-26 01:12:25 +00005682 fast_path = false;
Greg Clayton4116e932012-05-15 02:33:01 +00005683 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005684 }
Greg Clayton4116e932012-05-15 02:33:01 +00005685
Sean Callanan2367f8a2013-02-26 01:12:25 +00005686 // Now do the work of linking the DeclContexts and Types.
5687 if (fast_path)
5688 {
5689 // We can do this quickly. Just run across the tables index-for-index since
5690 // we know each node has matching names and tags.
Greg Clayton4116e932012-05-15 02:33:01 +00005691 for (idx = 0; idx < src_size; ++idx)
5692 {
5693 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5694 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005695
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005696 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
Greg Clayton4116e932012-05-15 02:33:01 +00005697 if (src_decl_ctx)
5698 {
5699 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005700 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
5701 static_cast<void*>(src_decl_ctx),
5702 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005703 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5704 }
5705 else
5706 {
5707 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005708 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found",
5709 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005710 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005711
Greg Clayton4116e932012-05-15 02:33:01 +00005712 Type *src_child_type = m_die_to_type[src_die];
5713 if (src_child_type)
5714 {
5715 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005716 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
5717 static_cast<void*>(src_child_type),
5718 src_child_type->GetID(),
5719 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton4116e932012-05-15 02:33:01 +00005720 m_die_to_type[dst_die] = src_child_type;
5721 }
5722 else
5723 {
5724 if (log)
Greg Clayton65ec1032012-11-12 21:27:20 +00005725 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5726 }
5727 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005728 }
5729 else
5730 {
5731 // We must do this slowly. For each member of the destination, look
5732 // up a member in the source with the same name, check its tag, and
5733 // unique them if everything matches up. Report failures.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005734
Sean Callanan2367f8a2013-02-26 01:12:25 +00005735 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty())
5736 {
5737 src_name_to_die.Sort();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005738
Sean Callanan2367f8a2013-02-26 01:12:25 +00005739 for (idx = 0; idx < dst_size; ++idx)
5740 {
5741 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx);
5742 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
5743 src_die = src_name_to_die.Find(dst_name, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005744
Sean Callanan2367f8a2013-02-26 01:12:25 +00005745 if (src_die && (src_die->Tag() == dst_die->Tag()))
5746 {
Greg Clayton5d650c6a2013-02-26 01:31:37 +00005747 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
Sean Callanan2367f8a2013-02-26 01:12:25 +00005748 if (src_decl_ctx)
5749 {
5750 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005751 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
5752 static_cast<void*>(src_decl_ctx),
5753 src_die->GetOffset(),
5754 dst_die->GetOffset());
Sean Callanan2367f8a2013-02-26 01:12:25 +00005755 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5756 }
5757 else
5758 {
5759 if (log)
5760 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5761 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005762
Sean Callanan2367f8a2013-02-26 01:12:25 +00005763 Type *src_child_type = m_die_to_type[src_die];
5764 if (src_child_type)
5765 {
5766 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005767 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
5768 static_cast<void*>(src_child_type),
5769 src_child_type->GetID(),
5770 src_die->GetOffset(),
5771 dst_die->GetOffset());
Sean Callanan2367f8a2013-02-26 01:12:25 +00005772 m_die_to_type[dst_die] = src_child_type;
5773 }
5774 else
5775 {
5776 if (log)
5777 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5778 }
5779 }
5780 else
5781 {
5782 if (log)
5783 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005784
Greg Claytond20deac2014-04-04 18:15:18 +00005785 failures.Append(dst_die);
Sean Callanan2367f8a2013-02-26 01:12:25 +00005786 }
5787 }
5788 }
5789 }
5790
5791 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize ();
5792 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize ();
5793
5794 UniqueCStringMap<const DWARFDebugInfoEntry *> name_to_die_artificial_not_in_src;
5795
5796 if (src_size_artificial && dst_size_artificial)
5797 {
5798 dst_name_to_die_artificial.Sort();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005799
Greg Clayton65ec1032012-11-12 21:27:20 +00005800 for (idx = 0; idx < src_size_artificial; ++idx)
5801 {
5802 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx);
5803 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
5804 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, NULL);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005805
Greg Clayton65ec1032012-11-12 21:27:20 +00005806 if (dst_die)
5807 {
Greg Clayton65ec1032012-11-12 21:27:20 +00005808 // Both classes have the artificial types, link them
5809 clang::DeclContext *src_decl_ctx = m_die_to_decl_ctx[src_die];
5810 if (src_decl_ctx)
5811 {
5812 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005813 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
5814 static_cast<void*>(src_decl_ctx),
5815 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005816 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5817 }
5818 else
5819 {
5820 if (log)
5821 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5822 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005823
Greg Clayton65ec1032012-11-12 21:27:20 +00005824 Type *src_child_type = m_die_to_type[src_die];
5825 if (src_child_type)
5826 {
5827 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005828 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
5829 static_cast<void*>(src_child_type),
5830 src_child_type->GetID(),
5831 src_die->GetOffset(), dst_die->GetOffset());
Greg Clayton65ec1032012-11-12 21:27:20 +00005832 m_die_to_type[dst_die] = src_child_type;
5833 }
5834 else
5835 {
5836 if (log)
5837 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
5838 }
5839 }
5840 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005841 }
Greg Clayton65ec1032012-11-12 21:27:20 +00005842
Sean Callanan2367f8a2013-02-26 01:12:25 +00005843 if (dst_size_artificial)
Greg Clayton4116e932012-05-15 02:33:01 +00005844 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00005845 for (idx = 0; idx < dst_size_artificial; ++idx)
5846 {
5847 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx);
5848 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
5849 if (log)
5850 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die->GetOffset(), dst_name_artificial);
5851
Greg Claytond20deac2014-04-04 18:15:18 +00005852 failures.Append(dst_die);
Sean Callanan2367f8a2013-02-26 01:12:25 +00005853 }
Greg Clayton4116e932012-05-15 02:33:01 +00005854 }
Sean Callanan2367f8a2013-02-26 01:12:25 +00005855
Greg Claytond20deac2014-04-04 18:15:18 +00005856 return (failures.Size() != 0);
Greg Clayton4116e932012-05-15 02:33:01 +00005857}
Greg Claytona8022fa2012-04-24 21:22:41 +00005858
5859TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00005860SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005861{
5862 TypeSP type_sp;
5863
Greg Clayton1be10fc2010-09-29 01:12:09 +00005864 if (type_is_new_ptr)
5865 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00005866
Todd Fiala0a70a842014-05-28 16:43:26 +00005867#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
Greg Clayton1fba87112012-02-09 20:03:49 +00005868 static DIEStack g_die_stack;
5869 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
5870#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00005871
Sean Callananc7fbf732010-08-06 00:32:49 +00005872 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005873 if (die != NULL)
5874 {
Greg Clayton5160ce52013-03-27 23:08:40 +00005875 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005876 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00005877 {
5878 const DWARFDebugInfoEntry *context_die;
5879 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005880
Greg Clayton5160ce52013-03-27 23:08:40 +00005881 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005882 die->GetOffset(),
5883 static_cast<void*>(context),
5884 context_die->GetOffset(),
5885 DW_TAG_value_to_name(die->Tag()),
5886 die->GetName(this, dwarf_cu));
5887
Todd Fiala0a70a842014-05-28 16:43:26 +00005888#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
Greg Clayton1fba87112012-02-09 20:03:49 +00005889 scoped_die_logger.Push (dwarf_cu, die);
Greg Clayton5160ce52013-03-27 23:08:40 +00005890 g_die_stack.LogDIEs(log, this);
Greg Clayton1fba87112012-02-09 20:03:49 +00005891#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00005892 }
Greg Clayton3bffb082011-12-10 02:15:28 +00005893//
Greg Clayton5160ce52013-03-27 23:08:40 +00005894// Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005895// if (log && dwarf_cu)
5896// {
5897// StreamString s;
5898// die->DumpLocation (this, dwarf_cu, s);
Greg Clayton5160ce52013-03-27 23:08:40 +00005899// GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00005900//
5901// }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005902
Greg Clayton594e5ed2010-09-27 21:07:38 +00005903 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005904 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00005905 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005906 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005907 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005908 if (type_is_new_ptr)
5909 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005910
Greg Clayton594e5ed2010-09-27 21:07:38 +00005911 const dw_tag_t tag = die->Tag();
5912
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005913 bool is_forward_declaration = false;
5914 DWARFDebugInfoEntry::Attributes attributes;
5915 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00005916 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00005917 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
Greg Claytonfaac1112013-03-14 18:31:44 +00005918 uint64_t byte_size = 0;
Greg Clayton526e5af2010-11-13 03:52:47 +00005919 Declaration decl;
5920
Greg Clayton4957bf62010-09-30 21:49:03 +00005921 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton57ee3062013-07-11 22:46:58 +00005922 ClangASTType clang_type;
Greg Claytond20deac2014-04-04 18:15:18 +00005923 DWARFFormValue form_value;
5924
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005925 dw_attr_t attr;
5926
5927 switch (tag)
5928 {
5929 case DW_TAG_base_type:
5930 case DW_TAG_pointer_type:
5931 case DW_TAG_reference_type:
Sean Callanance8af862012-05-21 23:31:51 +00005932 case DW_TAG_rvalue_reference_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005933 case DW_TAG_typedef:
5934 case DW_TAG_const_type:
5935 case DW_TAG_restrict_type:
5936 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00005937 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005938 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005939 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005940 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005941
Greg Claytond88d7592010-09-15 08:33:30 +00005942 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005943 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005944 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
5945
5946 if (num_attributes > 0)
5947 {
5948 uint32_t i;
5949 for (i=0; i<num_attributes; ++i)
5950 {
5951 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005952 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5953 {
5954 switch (attr)
5955 {
5956 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5957 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5958 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5959 case DW_AT_name:
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00005960
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005961 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00005962 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
5963 // include the "&"...
5964 if (tag == DW_TAG_reference_type)
5965 {
5966 if (strchr (type_name_cstr, '&') == NULL)
5967 type_name_cstr = NULL;
5968 }
5969 if (type_name_cstr)
5970 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005971 break;
Greg Clayton23f59502012-07-17 03:23:13 +00005972 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005973 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
Greg Clayton54166af2014-11-22 01:58:59 +00005974 case DW_AT_type: encoding_uid = form_value.Reference(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005975 default:
5976 case DW_AT_sibling:
5977 break;
5978 }
5979 }
5980 }
5981 }
5982
Ed Mastea0191d12013-10-17 20:42:56 +00005983 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
Greg Claytonc93237c2010-10-01 20:48:32 +00005984
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005985 switch (tag)
5986 {
5987 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00005988 break;
5989
Greg Claytond4436412011-10-28 21:00:00 +00005990 case DW_TAG_unspecified_type:
Greg Clayton7fba2632013-07-01 21:01:52 +00005991 if (strcmp(type_name_cstr, "nullptr_t") == 0 ||
5992 strcmp(type_name_cstr, "decltype(nullptr)") == 0 )
Greg Claytond4436412011-10-28 21:00:00 +00005993 {
5994 resolve_state = Type::eResolveStateFull;
Greg Clayton57ee3062013-07-11 22:46:58 +00005995 clang_type = ast.GetBasicType(eBasicTypeNullPtr);
Greg Claytond4436412011-10-28 21:00:00 +00005996 break;
5997 }
5998 // Fall through to base type below in case we can handle the type there...
5999
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006000 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00006001 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006002 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
6003 encoding,
6004 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006005 break;
6006
Sean Callanance8af862012-05-21 23:31:51 +00006007 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
6008 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
6009 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
6010 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
6011 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
6012 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
6013 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006014 }
6015
Greg Clayton57ee3062013-07-11 22:46:58 +00006016 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00006017 {
Sean Callanan82587052013-01-03 00:05:56 +00006018 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006019
Sean Callanan82587052013-01-03 00:05:56 +00006020 if (translation_unit_is_objc)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00006021 {
Sean Callanan82587052013-01-03 00:05:56 +00006022 if (type_name_cstr != NULL)
Greg Claytonc7f03b62012-01-12 04:33:28 +00006023 {
Sean Callanan82587052013-01-03 00:05:56 +00006024 static ConstString g_objc_type_name_id("id");
6025 static ConstString g_objc_type_name_Class("Class");
6026 static ConstString g_objc_type_name_selector("SEL");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006027
Sean Callanan82587052013-01-03 00:05:56 +00006028 if (type_name_const_str == g_objc_type_name_id)
6029 {
6030 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00006031 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
Sean Callanan82587052013-01-03 00:05:56 +00006032 die->GetOffset(),
6033 DW_TAG_value_to_name(die->Tag()),
6034 die->GetName(this, dwarf_cu));
Greg Clayton57ee3062013-07-11 22:46:58 +00006035 clang_type = ast.GetBasicType(eBasicTypeObjCID);
Sean Callanan82587052013-01-03 00:05:56 +00006036 encoding_data_type = Type::eEncodingIsUID;
6037 encoding_uid = LLDB_INVALID_UID;
6038 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00006039
Sean Callanan82587052013-01-03 00:05:56 +00006040 }
6041 else if (type_name_const_str == g_objc_type_name_Class)
6042 {
6043 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00006044 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
Sean Callanan82587052013-01-03 00:05:56 +00006045 die->GetOffset(),
6046 DW_TAG_value_to_name(die->Tag()),
6047 die->GetName(this, dwarf_cu));
Greg Clayton57ee3062013-07-11 22:46:58 +00006048 clang_type = ast.GetBasicType(eBasicTypeObjCClass);
Sean Callanan82587052013-01-03 00:05:56 +00006049 encoding_data_type = Type::eEncodingIsUID;
6050 encoding_uid = LLDB_INVALID_UID;
6051 resolve_state = Type::eResolveStateFull;
6052 }
6053 else if (type_name_const_str == g_objc_type_name_selector)
6054 {
6055 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00006056 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
Sean Callanan82587052013-01-03 00:05:56 +00006057 die->GetOffset(),
6058 DW_TAG_value_to_name(die->Tag()),
6059 die->GetName(this, dwarf_cu));
Greg Clayton57ee3062013-07-11 22:46:58 +00006060 clang_type = ast.GetBasicType(eBasicTypeObjCSel);
Sean Callanan82587052013-01-03 00:05:56 +00006061 encoding_data_type = Type::eEncodingIsUID;
6062 encoding_uid = LLDB_INVALID_UID;
6063 resolve_state = Type::eResolveStateFull;
6064 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00006065 }
Sean Callanan82587052013-01-03 00:05:56 +00006066 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID)
Greg Claytonc7f03b62012-01-12 04:33:28 +00006067 {
Sean Callanan82587052013-01-03 00:05:56 +00006068 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id".
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006069
Sean Callanan82587052013-01-03 00:05:56 +00006070 DWARFDebugInfoEntry* encoding_die = dwarf_cu->GetDIEPtr(encoding_uid);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006071
Sean Callanan82587052013-01-03 00:05:56 +00006072 if (encoding_die && encoding_die->Tag() == DW_TAG_structure_type)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006073 {
Sean Callanan82587052013-01-03 00:05:56 +00006074 if (const char *struct_name = encoding_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL))
6075 {
6076 if (!strcmp(struct_name, "objc_object"))
6077 {
6078 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00006079 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.",
Sean Callanan82587052013-01-03 00:05:56 +00006080 die->GetOffset(),
6081 DW_TAG_value_to_name(die->Tag()),
6082 die->GetName(this, dwarf_cu));
Greg Clayton57ee3062013-07-11 22:46:58 +00006083 clang_type = ast.GetBasicType(eBasicTypeObjCID);
Sean Callanan82587052013-01-03 00:05:56 +00006084 encoding_data_type = Type::eEncodingIsUID;
6085 encoding_uid = LLDB_INVALID_UID;
6086 resolve_state = Type::eResolveStateFull;
6087 }
6088 }
6089 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00006090 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00006091 }
6092 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006093
Greg Clayton81c22f62011-10-19 18:09:39 +00006094 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006095 this,
6096 type_name_const_str,
6097 byte_size,
6098 NULL,
6099 encoding_uid,
6100 encoding_data_type,
6101 &decl,
6102 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006103 resolve_state));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006104
Greg Clayton594e5ed2010-09-27 21:07:38 +00006105 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006106
6107// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
6108// if (encoding_type != NULL)
6109// {
6110// if (encoding_type != DIE_IS_BEING_PARSED)
6111// type_sp->SetEncodingType(encoding_type);
6112// else
6113// m_indirect_fixups.push_back(type_sp.get());
6114// }
6115 }
6116 break;
6117
6118 case DW_TAG_structure_type:
6119 case DW_TAG_union_type:
6120 case DW_TAG_class_type:
6121 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006122 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006123 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Greg Clayton23f59502012-07-17 03:23:13 +00006124 bool byte_size_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006125
Greg Clayton9e409562010-07-28 02:04:09 +00006126 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00006127 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006128 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00006129 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006130 if (num_attributes > 0)
6131 {
6132 uint32_t i;
6133 for (i=0; i<num_attributes; ++i)
6134 {
6135 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006136 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6137 {
6138 switch (attr)
6139 {
Greg Clayton9e409562010-07-28 02:04:09 +00006140 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00006141 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
Ed Maste4c24b122013-10-17 20:13:14 +00006142 {
6143 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
6144 // point to the compile unit file, so we clear this invalid value
6145 // so that we can still unique types efficiently.
Greg Claytonc7f03b62012-01-12 04:33:28 +00006146 decl.SetFile(FileSpec ("<invalid>", false));
Ed Maste4c24b122013-10-17 20:13:14 +00006147 }
Greg Claytonc7f03b62012-01-12 04:33:28 +00006148 else
6149 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00006150 break;
6151
6152 case DW_AT_decl_line:
6153 decl.SetLine(form_value.Unsigned());
6154 break;
6155
6156 case DW_AT_decl_column:
6157 decl.SetColumn(form_value.Unsigned());
6158 break;
6159
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006160 case DW_AT_name:
6161 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006162 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006163 break;
Greg Clayton9e409562010-07-28 02:04:09 +00006164
6165 case DW_AT_byte_size:
6166 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00006167 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00006168 break;
6169
6170 case DW_AT_accessibility:
6171 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
6172 break;
6173
6174 case DW_AT_declaration:
Greg Clayton1c8ef472013-04-05 23:27:21 +00006175 is_forward_declaration = form_value.Boolean();
Greg Clayton9e409562010-07-28 02:04:09 +00006176 break;
6177
6178 case DW_AT_APPLE_runtime_class:
6179 class_language = (LanguageType)form_value.Signed();
6180 break;
6181
Greg Clayton18774842011-11-29 23:40:34 +00006182 case DW_AT_APPLE_objc_complete_type:
6183 is_complete_objc_class = form_value.Signed();
6184 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006185
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006186 case DW_AT_allocated:
6187 case DW_AT_associated:
6188 case DW_AT_data_location:
6189 case DW_AT_description:
6190 case DW_AT_start_scope:
6191 case DW_AT_visibility:
6192 default:
6193 case DW_AT_sibling:
6194 break;
6195 }
6196 }
6197 }
6198 }
Greg Claytond20deac2014-04-04 18:15:18 +00006199
6200 // UniqueDWARFASTType is large, so don't create a local variables on the
6201 // stack, put it on the heap. This function is often called recursively
6202 // and clang isn't good and sharing the stack space for variables in different blocks.
6203 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType());
Sean Callanan8e8072d2012-02-07 21:13:38 +00006204
Greg Clayton123edca2012-03-02 00:07:15 +00006205 // Only try and unique the type if it has a name.
6206 if (type_name_const_str &&
6207 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00006208 this,
6209 dwarf_cu,
6210 die,
6211 decl,
6212 byte_size_valid ? byte_size : -1,
Greg Claytond20deac2014-04-04 18:15:18 +00006213 *unique_ast_entry_ap))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006214 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00006215 // We have already parsed this type or from another
6216 // compile unit. GCC loves to use the "one definition
6217 // rule" which can result in multiple definitions
6218 // of the same class over and over in each compile
6219 // unit.
Greg Claytond20deac2014-04-04 18:15:18 +00006220 type_sp = unique_ast_entry_ap->m_type_sp;
Sean Callanan8e8072d2012-02-07 21:13:38 +00006221 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00006222 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00006223 m_die_to_type[die] = type_sp.get();
6224 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00006225 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006226 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006227
Daniel Malead01b2952012-11-29 21:49:15 +00006228 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006229
6230 int tag_decl_kind = -1;
6231 AccessType default_accessibility = eAccessNone;
6232 if (tag == DW_TAG_structure_type)
6233 {
6234 tag_decl_kind = clang::TTK_Struct;
6235 default_accessibility = eAccessPublic;
6236 }
6237 else if (tag == DW_TAG_union_type)
6238 {
6239 tag_decl_kind = clang::TTK_Union;
6240 default_accessibility = eAccessPublic;
6241 }
6242 else if (tag == DW_TAG_class_type)
6243 {
6244 tag_decl_kind = clang::TTK_Class;
6245 default_accessibility = eAccessPrivate;
6246 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006247
Greg Clayton3a5f29a2011-11-30 02:48:28 +00006248 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
6249 die->HasChildren() == false &&
6250 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
6251 {
6252 // Work around an issue with clang at the moment where
6253 // forward declarations for objective C classes are emitted
6254 // as:
6255 // DW_TAG_structure_type [2]
6256 // DW_AT_name( "ForwardObjcClass" )
6257 // DW_AT_byte_size( 0x00 )
6258 // DW_AT_decl_file( "..." )
6259 // DW_AT_decl_line( 1 )
6260 //
6261 // Note that there is no DW_AT_declaration and there are
6262 // no children, and the byte size is zero.
6263 is_forward_declaration = true;
6264 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006265
Sean Callanan7457f8d2012-04-25 01:03:57 +00006266 if (class_language == eLanguageTypeObjC ||
6267 class_language == eLanguageTypeObjC_plus_plus)
Greg Clayton18774842011-11-29 23:40:34 +00006268 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00006269 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00006270 {
6271 // We have a valid eSymbolTypeObjCClass class symbol whose
6272 // name matches the current objective C class that we
6273 // are trying to find and this DIE isn't the complete
6274 // definition (we checked is_complete_objc_class above and
6275 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00006276 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006277
Greg Clayton1f746072012-08-29 21:13:06 +00006278 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton901c5ca2011-12-03 04:40:03 +00006279 {
6280 // We weren't able to find a full declaration in
6281 // this DWARF, see if we have a declaration anywhere
6282 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00006283 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00006284 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006285
Greg Clayton901c5ca2011-12-03 04:40:03 +00006286 if (type_sp)
6287 {
6288 if (log)
6289 {
Greg Clayton5160ce52013-03-27 23:08:40 +00006290 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00006291 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006292 static_cast<void*>(this),
Greg Claytone38a5ed2012-01-05 03:57:59 +00006293 die->GetOffset(),
6294 DW_TAG_value_to_name(tag),
6295 type_name_cstr,
6296 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00006297 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006298
Greg Clayton901c5ca2011-12-03 04:40:03 +00006299 // We found a real definition for this type elsewhere
6300 // so lets use it and cache the fact that we found
6301 // a complete type for this die
6302 m_die_to_type[die] = type_sp.get();
6303 return type_sp;
6304 }
6305 }
6306 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006307
Greg Clayton901c5ca2011-12-03 04:40:03 +00006308
6309 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006310 {
6311 // We have a forward declaration to a type and we need
6312 // to try and find a full declaration. We look in the
6313 // current type index just in case we have a forward
6314 // declaration followed by an actual declarations in the
6315 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00006316 if (log)
6317 {
Greg Clayton5160ce52013-03-27 23:08:40 +00006318 GetObjectFile()->GetModule()->LogMessage (log,
Greg Claytone38a5ed2012-01-05 03:57:59 +00006319 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006320 static_cast<void*>(this),
Greg Claytone38a5ed2012-01-05 03:57:59 +00006321 die->GetOffset(),
6322 DW_TAG_value_to_name(tag),
6323 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00006324 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006325
Greg Claytona8022fa2012-04-24 21:22:41 +00006326 DWARFDeclContext die_decl_ctx;
6327 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx);
6328
6329 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
6330 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006331
Greg Clayton1f746072012-08-29 21:13:06 +00006332 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton4272cc72011-02-02 02:24:04 +00006333 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006334 // We weren't able to find a full declaration in
6335 // this DWARF, see if we have a declaration anywhere
6336 // else...
Greg Claytona8022fa2012-04-24 21:22:41 +00006337 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton4272cc72011-02-02 02:24:04 +00006338 }
6339
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006340 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00006341 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00006342 if (log)
6343 {
Greg Clayton5160ce52013-03-27 23:08:40 +00006344 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00006345 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006346 static_cast<void*>(this),
Greg Claytone38a5ed2012-01-05 03:57:59 +00006347 die->GetOffset(),
6348 DW_TAG_value_to_name(tag),
6349 type_name_cstr,
6350 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00006351 }
6352
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006353 // We found a real definition for this type elsewhere
6354 // so lets use it and cache the fact that we found
6355 // a complete type for this die
6356 m_die_to_type[die] = type_sp.get();
6357 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00006358 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006359 }
6360 assert (tag_decl_kind != -1);
6361 bool clang_type_was_created = false;
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006362 clang_type.SetClangType(ast.getASTContext(), m_forward_decl_die_to_clang_type.lookup (die));
Greg Clayton57ee3062013-07-11 22:46:58 +00006363 if (!clang_type)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006364 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00006365 const DWARFDebugInfoEntry *decl_ctx_die;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006366
Sean Callanan4d04c6a2012-02-09 22:54:11 +00006367 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00006368 if (accessibility == eAccessNone && decl_ctx)
6369 {
6370 // Check the decl context that contains this class/struct/union.
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00006371 // If it is a class we must give it an accessibility.
Greg Clayton55561e92011-10-26 03:31:36 +00006372 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
6373 if (DeclKindIsCXXClass (containing_decl_kind))
6374 accessibility = default_accessibility;
6375 }
6376
Greg Claytonc4ffd662013-03-08 01:37:30 +00006377 ClangASTMetadata metadata;
6378 metadata.SetUserID(MakeUserID(die->GetOffset()));
6379 metadata.SetIsDynamicCXXType(ClassOrStructIsVirtual (dwarf_cu, die));
6380
Greg Claytonf0705c82011-10-22 03:33:13 +00006381 if (type_name_cstr && strchr (type_name_cstr, '<'))
6382 {
6383 ClangASTContext::TemplateParameterInfos template_param_infos;
6384 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
6385 {
6386 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00006387 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00006388 type_name_cstr,
6389 tag_decl_kind,
6390 template_param_infos);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006391
Greg Claytonf0705c82011-10-22 03:33:13 +00006392 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
6393 class_template_decl,
6394 tag_decl_kind,
6395 template_param_infos);
6396 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
6397 clang_type_was_created = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006398
Greg Claytond0029442013-03-27 01:48:02 +00006399 GetClangASTContext().SetMetadata (class_template_decl, metadata);
6400 GetClangASTContext().SetMetadata (class_specialization_decl, metadata);
Greg Claytonf0705c82011-10-22 03:33:13 +00006401 }
6402 }
6403
6404 if (!clang_type_was_created)
6405 {
6406 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00006407 clang_type = ast.CreateRecordType (decl_ctx,
6408 accessibility,
6409 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00006410 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00006411 class_language,
Jim Ingham379397632012-10-27 02:54:13 +00006412 &metadata);
Greg Claytonf0705c82011-10-22 03:33:13 +00006413 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006414 }
6415
6416 // Store a forward declaration to this class type in case any
6417 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00006418 // types for function prototypes.
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006419 LinkDeclContextToDIE(clang_type.GetDeclContextForType(), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00006420 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006421 this,
6422 type_name_const_str,
6423 byte_size,
6424 NULL,
6425 LLDB_INVALID_UID,
6426 Type::eEncodingIsUID,
6427 &decl,
6428 clang_type,
6429 Type::eResolveStateForward));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006430
Sean Callanan72772842012-02-22 23:57:45 +00006431 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006432
6433
6434 // Add our type to the unique type map so we don't
6435 // end up creating many copies of the same type over
6436 // and over in the ASTContext for our module
Greg Claytond20deac2014-04-04 18:15:18 +00006437 unique_ast_entry_ap->m_type_sp = type_sp;
6438 unique_ast_entry_ap->m_symfile = this;
6439 unique_ast_entry_ap->m_cu = dwarf_cu;
6440 unique_ast_entry_ap->m_die = die;
6441 unique_ast_entry_ap->m_declaration = decl;
6442 unique_ast_entry_ap->m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00006443 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
Greg Claytond20deac2014-04-04 18:15:18 +00006444 *unique_ast_entry_ap);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006445
Greg Clayton0ec71a02013-08-10 00:09:35 +00006446 if (is_forward_declaration && die->HasChildren())
6447 {
6448 // Check to see if the DIE actually has a definition, some version of GCC will
6449 // emit DIEs with DW_AT_declaration set to true, but yet still have subprogram,
6450 // members, or inheritance, so we can't trust it
6451 const DWARFDebugInfoEntry *child_die = die->GetFirstChild();
6452 while (child_die)
6453 {
6454 switch (child_die->Tag())
6455 {
6456 case DW_TAG_inheritance:
6457 case DW_TAG_subprogram:
6458 case DW_TAG_member:
6459 case DW_TAG_APPLE_property:
Greg Clayton4c484ec2014-02-07 19:21:56 +00006460 case DW_TAG_class_type:
6461 case DW_TAG_structure_type:
6462 case DW_TAG_enumeration_type:
6463 case DW_TAG_typedef:
6464 case DW_TAG_union_type:
Greg Clayton0ec71a02013-08-10 00:09:35 +00006465 child_die = NULL;
6466 is_forward_declaration = false;
6467 break;
6468 default:
6469 child_die = child_die->GetSibling();
6470 break;
6471 }
6472 }
6473 }
6474
Sean Callanan12014a02011-12-08 23:45:45 +00006475 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00006476 {
6477 // Always start the definition for a class type so that
6478 // if the class has child classes or types that require
6479 // the class to be created for use as their decl contexts
6480 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00006481 if (die->HasChildren() == false)
6482 {
6483 // No children for this struct/union/class, lets finish it
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006484 clang_type.StartTagDeclarationDefinition ();
6485 clang_type.CompleteTagDeclarationDefinition ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006486
Sean Callanan433cd222012-10-19 01:37:25 +00006487 if (tag == DW_TAG_structure_type) // this only applies in C
6488 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006489 clang::RecordDecl *record_decl = clang_type.GetAsRecordDecl();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006490
Greg Clayton57ee3062013-07-11 22:46:58 +00006491 if (record_decl)
Greg Claytond20deac2014-04-04 18:15:18 +00006492 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, LayoutInfo()));
Sean Callanan433cd222012-10-19 01:37:25 +00006493 }
Sean Callanan12014a02011-12-08 23:45:45 +00006494 }
6495 else if (clang_type_was_created)
6496 {
Greg Clayton219cf312012-03-30 00:51:13 +00006497 // Start the definition if the class is not objective C since
6498 // the underlying decls respond to isCompleteDefinition(). Objective
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +00006499 // C decls don't respond to isCompleteDefinition() so we can't
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00006500 // start the declaration definition right away. For C++ class/union/structs
Greg Clayton219cf312012-03-30 00:51:13 +00006501 // we want to start the definition in case the class is needed as the
6502 // declaration context for a contained class or type without the need
6503 // to complete that type..
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006504
Sean Callanan7457f8d2012-04-25 01:03:57 +00006505 if (class_language != eLanguageTypeObjC &&
6506 class_language != eLanguageTypeObjC_plus_plus)
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006507 clang_type.StartTagDeclarationDefinition ();
Greg Clayton219cf312012-03-30 00:51:13 +00006508
Sean Callanan12014a02011-12-08 23:45:45 +00006509 // Leave this as a forward declaration until we need
6510 // to know the details of the type. lldb_private::Type
6511 // will automatically call the SymbolFile virtual function
6512 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
6513 // When the definition needs to be defined.
Greg Clayton57ee3062013-07-11 22:46:58 +00006514 m_forward_decl_die_to_clang_type[die] = clang_type.GetOpaqueQualType();
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006515 m_forward_decl_clang_type_to_die[clang_type.RemoveFastQualifiers().GetOpaqueQualType()] = die;
6516 clang_type.SetHasExternalStorage (true);
Sean Callanan12014a02011-12-08 23:45:45 +00006517 }
Greg Claytonc615ce42010-11-09 04:42:43 +00006518 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006519
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006520 }
6521 break;
6522
6523 case DW_TAG_enumeration_type:
6524 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006525 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006526 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006527
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006528 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006529
Greg Claytond88d7592010-09-15 08:33:30 +00006530 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006531 if (num_attributes > 0)
6532 {
6533 uint32_t i;
6534
6535 for (i=0; i<num_attributes; ++i)
6536 {
6537 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006538 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6539 {
6540 switch (attr)
6541 {
Greg Clayton7a345282010-11-09 23:46:37 +00006542 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6543 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6544 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006545 case DW_AT_name:
6546 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006547 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006548 break;
Greg Clayton54166af2014-11-22 01:58:59 +00006549 case DW_AT_type: encoding_uid = form_value.Reference(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006550 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
6551 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006552 case DW_AT_declaration: break; //is_forward_declaration = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006553 case DW_AT_allocated:
6554 case DW_AT_associated:
6555 case DW_AT_bit_stride:
6556 case DW_AT_byte_stride:
6557 case DW_AT_data_location:
6558 case DW_AT_description:
6559 case DW_AT_start_scope:
6560 case DW_AT_visibility:
6561 case DW_AT_specification:
6562 case DW_AT_abstract_origin:
6563 case DW_AT_sibling:
6564 break;
6565 }
6566 }
6567 }
6568
Daniel Malead01b2952012-11-29 21:49:15 +00006569 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006570
Greg Clayton57ee3062013-07-11 22:46:58 +00006571 ClangASTType enumerator_clang_type;
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006572 clang_type.SetClangType (ast.getASTContext(), m_forward_decl_die_to_clang_type.lookup (die));
Greg Clayton57ee3062013-07-11 22:46:58 +00006573 if (!clang_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00006574 {
Greg Clayton5d14fc02013-03-06 06:23:54 +00006575 if (encoding_uid != DW_INVALID_OFFSET)
6576 {
6577 Type *enumerator_type = ResolveTypeUID(encoding_uid);
6578 if (enumerator_type)
6579 enumerator_clang_type = enumerator_type->GetClangFullType();
6580 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006581
Greg Clayton57ee3062013-07-11 22:46:58 +00006582 if (!enumerator_clang_type)
Greg Clayton5d14fc02013-03-06 06:23:54 +00006583 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
6584 DW_ATE_signed,
6585 byte_size * 8);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006586
Greg Claytonca512b32011-01-14 04:54:56 +00006587 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00006588 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00006589 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006590 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00006591 }
6592 else
6593 {
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006594 enumerator_clang_type = clang_type.GetEnumerationIntegerType ();
Greg Clayton1be10fc2010-09-29 01:12:09 +00006595 }
6596
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006597 LinkDeclContextToDIE(clang_type.GetDeclContextForType(), die);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006598
Greg Clayton81c22f62011-10-19 18:09:39 +00006599 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006600 this,
6601 type_name_const_str,
6602 byte_size,
6603 NULL,
6604 encoding_uid,
6605 Type::eEncodingIsUID,
6606 &decl,
6607 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006608 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006609
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006610 clang_type.StartTagDeclarationDefinition ();
Greg Clayton6beaaa62011-01-17 03:46:26 +00006611 if (die->HasChildren())
6612 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00006613 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Greg Clayton3e067532013-03-05 23:54:39 +00006614 bool is_signed = false;
Greg Clayton57ee3062013-07-11 22:46:58 +00006615 enumerator_clang_type.IsIntegerType(is_signed);
Greg Clayton3e067532013-03-05 23:54:39 +00006616 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00006617 }
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006618 clang_type.CompleteTagDeclarationDefinition ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006619 }
6620 }
6621 break;
6622
Jim Inghamb0be4422010-08-12 01:20:14 +00006623 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006624 case DW_TAG_subprogram:
6625 case DW_TAG_subroutine_type:
6626 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006627 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006628 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006629
Greg Clayton23f59502012-07-17 03:23:13 +00006630 //const char *mangled = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006631 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00006632 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006633 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00006634 bool is_static = false;
6635 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00006636 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00006637 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00006638 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
6639 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Jim Ingham379397632012-10-27 02:54:13 +00006640 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00006641
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006642 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00006643 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006644
6645
Greg Claytond88d7592010-09-15 08:33:30 +00006646 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006647 if (num_attributes > 0)
6648 {
6649 uint32_t i;
6650 for (i=0; i<num_attributes; ++i)
6651 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00006652 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006653 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6654 {
6655 switch (attr)
6656 {
6657 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6658 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6659 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6660 case DW_AT_name:
6661 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006662 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006663 break;
6664
Greg Clayton71415542012-12-08 00:24:40 +00006665 case DW_AT_linkage_name:
Greg Clayton23f59502012-07-17 03:23:13 +00006666 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00006667 case DW_AT_type: type_die_offset = form_value.Reference(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00006668 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00006669 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
6670 case DW_AT_inline: is_inline = form_value.Boolean(); break;
6671 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break;
6672 case DW_AT_explicit: is_explicit = form_value.Boolean(); break;
6673 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006674
Greg Claytonf51de672010-10-01 02:31:07 +00006675
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006676 case DW_AT_external:
6677 if (form_value.Unsigned())
6678 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00006679 if (storage == clang::SC_None)
6680 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006681 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00006682 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006683 }
6684 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006685
Greg Clayton72da3972011-08-16 18:40:23 +00006686 case DW_AT_specification:
Greg Clayton54166af2014-11-22 01:58:59 +00006687 specification_die_offset = form_value.Reference();
Greg Clayton72da3972011-08-16 18:40:23 +00006688 break;
6689
6690 case DW_AT_abstract_origin:
Greg Clayton54166af2014-11-22 01:58:59 +00006691 abstract_origin_die_offset = form_value.Reference();
Greg Clayton72da3972011-08-16 18:40:23 +00006692 break;
6693
Jim Ingham379397632012-10-27 02:54:13 +00006694 case DW_AT_object_pointer:
Greg Clayton54166af2014-11-22 01:58:59 +00006695 object_pointer_die_offset = form_value.Reference();
Jim Ingham379397632012-10-27 02:54:13 +00006696 break;
6697
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006698 case DW_AT_allocated:
6699 case DW_AT_associated:
6700 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006701 case DW_AT_calling_convention:
6702 case DW_AT_data_location:
6703 case DW_AT_elemental:
6704 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006705 case DW_AT_frame_base:
6706 case DW_AT_high_pc:
6707 case DW_AT_low_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006708 case DW_AT_prototyped:
6709 case DW_AT_pure:
6710 case DW_AT_ranges:
6711 case DW_AT_recursive:
6712 case DW_AT_return_addr:
6713 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006714 case DW_AT_start_scope:
6715 case DW_AT_static_link:
6716 case DW_AT_trampoline:
6717 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006718 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006719 case DW_AT_description:
6720 case DW_AT_sibling:
6721 break;
6722 }
6723 }
6724 }
Greg Clayton24739922010-10-13 03:15:28 +00006725 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006726
Jim Ingham379397632012-10-27 02:54:13 +00006727 std::string object_pointer_name;
6728 if (object_pointer_die_offset != DW_INVALID_OFFSET)
6729 {
6730 // Get the name from the object pointer die
6731 StreamString s;
6732 if (DWARFDebugInfoEntry::GetName (this, dwarf_cu, object_pointer_die_offset, s))
6733 {
6734 object_pointer_name.assign(s.GetData());
6735 }
6736 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006737
Daniel Malead01b2952012-11-29 21:49:15 +00006738 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006739
Greg Clayton57ee3062013-07-11 22:46:58 +00006740 ClangASTType return_clang_type;
Greg Clayton24739922010-10-13 03:15:28 +00006741 Type *func_type = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006742
Greg Clayton24739922010-10-13 03:15:28 +00006743 if (type_die_offset != DW_INVALID_OFFSET)
6744 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00006745
Greg Clayton24739922010-10-13 03:15:28 +00006746 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00006747 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00006748 else
Greg Clayton57ee3062013-07-11 22:46:58 +00006749 return_clang_type = ast.GetBasicType(eBasicTypeVoid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006750
Greg Claytonf51de672010-10-01 02:31:07 +00006751
Greg Clayton57ee3062013-07-11 22:46:58 +00006752 std::vector<ClangASTType> function_param_types;
Greg Clayton24739922010-10-13 03:15:28 +00006753 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006754
Greg Clayton24739922010-10-13 03:15:28 +00006755 // Parse the function children for the parameters
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006756
Greg Claytoncb5860a2011-10-13 23:49:28 +00006757 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
6758 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00006759 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
6760
Greg Claytonf0705c82011-10-22 03:33:13 +00006761 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00006762 // Start off static. This will be set to false in ParseChildParameters(...)
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00006763 // if we find a "this" parameters as the first parameter
Greg Clayton5113dc82011-08-12 06:47:54 +00006764 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00006765 is_static = true;
Greg Claytond20deac2014-04-04 18:15:18 +00006766
Greg Clayton24739922010-10-13 03:15:28 +00006767 if (die->HasChildren())
6768 {
Greg Clayton0fffff52010-09-24 05:15:53 +00006769 bool skip_artificial = true;
Jim Ingham379397632012-10-27 02:54:13 +00006770 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00006771 containing_decl_ctx,
Jim Ingham379397632012-10-27 02:54:13 +00006772 dwarf_cu,
6773 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00006774 skip_artificial,
6775 is_static,
Greg Clayton03969752014-02-24 18:53:11 +00006776 is_variadic,
Jim Ingham379397632012-10-27 02:54:13 +00006777 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00006778 function_param_decls,
Greg Claytond20deac2014-04-04 18:15:18 +00006779 type_quals);
Greg Clayton24739922010-10-13 03:15:28 +00006780 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006781
Greg Clayton24739922010-10-13 03:15:28 +00006782 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006783 clang_type = ast.CreateFunctionType (return_clang_type,
Greg Clayton9cb25c42012-10-30 17:02:18 +00006784 function_param_types.data(),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006785 function_param_types.size(),
6786 is_variadic,
6787 type_quals);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006788
Sean Callanande9ce872013-05-09 23:13:13 +00006789 bool ignore_containing_context = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006790
Greg Clayton24739922010-10-13 03:15:28 +00006791 if (type_name_cstr)
6792 {
6793 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00006794 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006795 {
Greg Clayton1b3815c2013-01-30 00:18:29 +00006796 ObjCLanguageRuntime::MethodName objc_method (type_name_cstr, true);
6797 if (objc_method.IsValid(true))
Greg Clayton0fffff52010-09-24 05:15:53 +00006798 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006799 ClangASTType class_opaque_type;
Greg Clayton1b3815c2013-01-30 00:18:29 +00006800 ConstString class_name(objc_method.GetClassName());
Greg Clayton7c810422012-01-18 23:40:49 +00006801 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00006802 {
Greg Clayton278a16b2012-01-19 00:52:59 +00006803 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00006804
6805 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00006806 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006807 ClangASTType type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006808 if (type_clang_forward_type.IsObjCObjectOrInterfaceType ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00006809 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00006810 }
Greg Clayton24739922010-10-13 03:15:28 +00006811 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006812
Greg Clayton24739922010-10-13 03:15:28 +00006813 if (class_opaque_type)
6814 {
6815 // If accessibility isn't set to anything valid, assume public for
6816 // now...
6817 if (accessibility == eAccessNone)
6818 accessibility = eAccessPublic;
6819
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006820 clang::ObjCMethodDecl *objc_method_decl = class_opaque_type.AddMethodToObjCObjectType (type_name_cstr,
6821 clang_type,
6822 accessibility,
6823 is_artificial);
Greg Clayton24739922010-10-13 03:15:28 +00006824 type_handled = objc_method_decl != NULL;
Sean Callanan464a5b52012-10-04 22:06:29 +00006825 if (type_handled)
6826 {
6827 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Claytond0029442013-03-27 01:48:02 +00006828 GetClangASTContext().SetMetadataAsUserID (objc_method_decl, MakeUserID(die->GetOffset()));
Sean Callanan464a5b52012-10-04 22:06:29 +00006829 }
Sean Callananc3a1d562013-02-06 23:21:59 +00006830 else
6831 {
Jason Molendac1a65832013-03-07 22:44:42 +00006832 GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
Sean Callananc3a1d562013-02-06 23:21:59 +00006833 die->GetOffset(),
6834 tag,
6835 DW_TAG_value_to_name(tag));
6836 }
Greg Clayton24739922010-10-13 03:15:28 +00006837 }
6838 }
Greg Clayton5113dc82011-08-12 06:47:54 +00006839 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00006840 {
6841 // Look at the parent of this DIE and see if is is
6842 // a class or struct and see if this is actually a
6843 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00006844 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00006845 if (class_type)
6846 {
Greg Clayton4116e932012-05-15 02:33:01 +00006847 if (class_type->GetID() != MakeUserID(decl_ctx_die->GetOffset()))
6848 {
6849 // We uniqued the parent class of this function to another class
6850 // so we now need to associate all dies under "decl_ctx_die" to
6851 // DIEs in the DIE for "class_type"...
Greg Clayton5d650c6a2013-02-26 01:31:37 +00006852 SymbolFileDWARF *class_symfile = NULL;
Greg Clayton4116e932012-05-15 02:33:01 +00006853 DWARFCompileUnitSP class_type_cu_sp;
Greg Clayton5d650c6a2013-02-26 01:31:37 +00006854 const DWARFDebugInfoEntry *class_type_die = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006855
Sean Callanan2367f8a2013-02-26 01:12:25 +00006856 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
6857 if (debug_map_symfile)
6858 {
6859 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID()));
6860 class_type_die = class_symfile->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6861 }
6862 else
6863 {
6864 class_symfile = this;
6865 class_type_die = DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6866 }
Greg Clayton4116e932012-05-15 02:33:01 +00006867 if (class_type_die)
6868 {
Greg Claytond20deac2014-04-04 18:15:18 +00006869 DWARFDIECollection failures;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006870
Sean Callanan2367f8a2013-02-26 01:12:25 +00006871 CopyUniqueClassMethodTypes (class_symfile,
6872 class_type,
6873 class_type_cu_sp.get(),
6874 class_type_die,
6875 dwarf_cu,
6876 decl_ctx_die,
6877 failures);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006878
Sean Callanan2367f8a2013-02-26 01:12:25 +00006879 // FIXME do something with these failures that's smarter than
6880 // just dropping them on the ground. Unfortunately classes don't
6881 // like having stuff added to them after their definitions are
6882 // complete...
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006883
Sean Callanan2367f8a2013-02-26 01:12:25 +00006884 type_ptr = m_die_to_type[die];
6885 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton4116e932012-05-15 02:33:01 +00006886 {
Sean Callanan2367f8a2013-02-26 01:12:25 +00006887 type_sp = type_ptr->shared_from_this();
6888 break;
Greg Clayton4116e932012-05-15 02:33:01 +00006889 }
6890 }
6891 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006892
Greg Clayton72da3972011-08-16 18:40:23 +00006893 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00006894 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006895 // We have a specification which we are going to base our function
6896 // prototype off of, so we need this type to be completed so that the
6897 // m_die_to_decl_ctx for the method in the specification has a valid
6898 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006899 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00006900 // If we have a specification, then the function type should have been
6901 // made with the specification and not with this die.
6902 DWARFCompileUnitSP spec_cu_sp;
6903 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006904 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006905 if (spec_clang_decl_ctx)
6906 {
6907 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
6908 }
6909 else
Jim Inghamc1663042011-09-29 22:12:35 +00006910 {
Daniel Malead01b2952012-11-29 21:49:15 +00006911 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x) has no decl\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00006912 MakeUserID(die->GetOffset()),
6913 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006914 }
Greg Clayton72da3972011-08-16 18:40:23 +00006915 type_handled = true;
6916 }
6917 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
6918 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006919 // We have a specification which we are going to base our function
6920 // prototype off of, so we need this type to be completed so that the
6921 // m_die_to_decl_ctx for the method in the abstract origin has a valid
6922 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006923 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00006924
Greg Clayton72da3972011-08-16 18:40:23 +00006925 DWARFCompileUnitSP abs_cu_sp;
6926 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006927 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006928 if (abs_clang_decl_ctx)
6929 {
6930 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
6931 }
6932 else
Jim Inghamc1663042011-09-29 22:12:35 +00006933 {
Daniel Malead01b2952012-11-29 21:49:15 +00006934 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x) has no decl\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00006935 MakeUserID(die->GetOffset()),
6936 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006937 }
Greg Clayton72da3972011-08-16 18:40:23 +00006938 type_handled = true;
6939 }
6940 else
6941 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006942 ClangASTType class_opaque_type = class_type->GetClangForwardType();
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006943 if (class_opaque_type.IsCXXClassType ())
Greg Clayton931180e2011-01-27 06:44:37 +00006944 {
Greg Clayton57ee3062013-07-11 22:46:58 +00006945 if (class_opaque_type.IsBeingDefined ())
Greg Clayton72da3972011-08-16 18:40:23 +00006946 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006947 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
6948 // in the DWARF for C++ methods... Default to public for now...
6949 if (accessibility == eAccessNone)
6950 accessibility = eAccessPublic;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006951
Greg Clayton20568dd2011-10-13 23:13:20 +00006952 if (!is_static && !die->HasChildren())
6953 {
6954 // We have a C++ member function with no children (this pointer!)
6955 // and clang will get mad if we try and make a function that isn't
6956 // well formed in the DWARF, so we will just skip it...
6957 type_handled = true;
6958 }
6959 else
6960 {
6961 clang::CXXMethodDecl *cxx_method_decl;
6962 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00006963 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s",
Greg Clayton20568dd2011-10-13 23:13:20 +00006964 type_name_cstr,
6965 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00006966 MakeUserID(die->GetOffset()),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00006967 m_obj_file->GetFileSpec().GetPath().c_str());
Greg Clayton20568dd2011-10-13 23:13:20 +00006968
Sean Callananc1b732d2011-11-01 18:07:13 +00006969 const bool is_attr_used = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006970
Pavel Labathc7c30eb2015-06-08 23:38:06 +00006971 cxx_method_decl = class_opaque_type.AddMethodToCXXRecordType (type_name_cstr,
6972 clang_type,
6973 accessibility,
6974 is_virtual,
6975 is_static,
6976 is_inline,
6977 is_explicit,
6978 is_attr_used,
6979 is_artificial);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006980
Greg Clayton20568dd2011-10-13 23:13:20 +00006981 type_handled = cxx_method_decl != NULL;
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006982
6983 if (type_handled)
Jim Ingham379397632012-10-27 02:54:13 +00006984 {
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006985 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
6986
6987 Host::SetCrashDescription (NULL);
6988
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006989
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006990 ClangASTMetadata metadata;
6991 metadata.SetUserID(MakeUserID(die->GetOffset()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006992
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006993 if (!object_pointer_name.empty())
6994 {
6995 metadata.SetObjectPtrName(object_pointer_name.c_str());
6996 if (log)
Greg Claytond0029442013-03-27 01:48:02 +00006997 log->Printf ("Setting object pointer name: %s on method object %p.\n",
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006998 object_pointer_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00006999 static_cast<void*>(cxx_method_decl));
Sean Callanan4ac7ec72012-10-31 02:01:58 +00007000 }
Greg Claytond0029442013-03-27 01:48:02 +00007001 GetClangASTContext().SetMetadata (cxx_method_decl, metadata);
Jim Ingham379397632012-10-27 02:54:13 +00007002 }
Sean Callananb0640dc2013-04-09 23:22:08 +00007003 else
7004 {
Sean Callanande9ce872013-05-09 23:13:13 +00007005 ignore_containing_context = true;
Sean Callananb0640dc2013-04-09 23:22:08 +00007006 }
Greg Clayton20568dd2011-10-13 23:13:20 +00007007 }
Greg Clayton72da3972011-08-16 18:40:23 +00007008 }
7009 else
7010 {
Greg Clayton20568dd2011-10-13 23:13:20 +00007011 // We were asked to parse the type for a method in a class, yet the
7012 // class hasn't been asked to complete itself through the
7013 // clang::ExternalASTSource protocol, so we need to just have the
7014 // class complete itself and do things the right way, then our
7015 // DIE should then have an entry in the m_die_to_type map. First
7016 // we need to modify the m_die_to_type so it doesn't think we are
7017 // trying to parse this DIE anymore...
7018 m_die_to_type[die] = NULL;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007019
Greg Clayton20568dd2011-10-13 23:13:20 +00007020 // Now we get the full type to force our class type to complete itself
7021 // using the clang::ExternalASTSource protocol which will parse all
7022 // base classes and all methods (including the method for this DIE).
7023 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00007024
Greg Clayton20568dd2011-10-13 23:13:20 +00007025 // The type for this DIE should have been filled in the function call above
7026 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00007027 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton20568dd2011-10-13 23:13:20 +00007028 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00007029 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00007030 break;
7031 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007032
Sean Callanan02eee4d2012-04-12 23:10:00 +00007033 // FIXME This is fixing some even uglier behavior but we really need to
7034 // uniq the methods of each class as well as the class itself.
7035 // <rdar://problem/11240464>
7036 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00007037 }
Greg Clayton931180e2011-01-27 06:44:37 +00007038 }
Greg Clayton0fffff52010-09-24 05:15:53 +00007039 }
7040 }
Greg Clayton0fffff52010-09-24 05:15:53 +00007041 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007042 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007043
Greg Clayton24739922010-10-13 03:15:28 +00007044 if (!type_handled)
7045 {
7046 // We just have a function that isn't part of a class
Sean Callanande9ce872013-05-09 23:13:13 +00007047 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (ignore_containing_context ? GetClangASTContext().GetTranslationUnitDecl() : containing_decl_ctx,
Greg Clayton147e1fa2011-10-14 22:47:18 +00007048 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00007049 clang_type,
7050 storage,
7051 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00007052
7053// if (template_param_infos.GetSize() > 0)
7054// {
7055// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
7056// function_decl,
7057// type_name_cstr,
7058// template_param_infos);
7059//
7060// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
7061// func_template_decl,
7062// template_param_infos);
7063// }
Greg Clayton24739922010-10-13 03:15:28 +00007064 // Add the decl to our DIE to decl context map
7065 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00007066 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00007067 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00007068 ast.SetFunctionParameters (function_decl,
7069 &function_param_decls.front(),
7070 function_param_decls.size());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007071
Jim Ingham379397632012-10-27 02:54:13 +00007072 ClangASTMetadata metadata;
7073 metadata.SetUserID(MakeUserID(die->GetOffset()));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007074
Jim Ingham379397632012-10-27 02:54:13 +00007075 if (!object_pointer_name.empty())
7076 {
7077 metadata.SetObjectPtrName(object_pointer_name.c_str());
Jim Ingham2cffda3f2012-10-30 23:34:07 +00007078 if (log)
Greg Claytond0029442013-03-27 01:48:02 +00007079 log->Printf ("Setting object pointer name: %s on function object %p.",
Jim Ingham2cffda3f2012-10-30 23:34:07 +00007080 object_pointer_name.c_str(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007081 static_cast<void*>(function_decl));
Jim Ingham379397632012-10-27 02:54:13 +00007082 }
Greg Claytond0029442013-03-27 01:48:02 +00007083 GetClangASTContext().SetMetadata (function_decl, metadata);
Greg Clayton24739922010-10-13 03:15:28 +00007084 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007085 }
Greg Clayton81c22f62011-10-19 18:09:39 +00007086 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00007087 this,
7088 type_name_const_str,
7089 0,
7090 NULL,
7091 LLDB_INVALID_UID,
7092 Type::eEncodingIsUID,
7093 &decl,
7094 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00007095 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00007096 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007097 }
7098 break;
7099
7100 case DW_TAG_array_type:
7101 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007102 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00007103 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007104
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007105 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007106 int64_t first_index = 0;
7107 uint32_t byte_stride = 0;
7108 uint32_t bit_stride = 0;
Greg Clayton1c8ef472013-04-05 23:27:21 +00007109 bool is_vector = false;
Greg Claytond88d7592010-09-15 08:33:30 +00007110 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007111
7112 if (num_attributes > 0)
7113 {
7114 uint32_t i;
7115 for (i=0; i<num_attributes; ++i)
7116 {
7117 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007118 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
7119 {
7120 switch (attr)
7121 {
7122 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
7123 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
7124 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
7125 case DW_AT_name:
7126 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00007127 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007128 break;
7129
Greg Clayton54166af2014-11-22 01:58:59 +00007130 case DW_AT_type: type_die_offset = form_value.Reference(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00007131 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007132 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
7133 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00007134 case DW_AT_GNU_vector: is_vector = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00007135 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00007136 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007137 case DW_AT_allocated:
7138 case DW_AT_associated:
7139 case DW_AT_data_location:
7140 case DW_AT_description:
7141 case DW_AT_ordering:
7142 case DW_AT_start_scope:
7143 case DW_AT_visibility:
7144 case DW_AT_specification:
7145 case DW_AT_abstract_origin:
7146 case DW_AT_sibling:
7147 break;
7148 }
7149 }
7150 }
7151
Daniel Malead01b2952012-11-29 21:49:15 +00007152 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00007153
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007154 Type *element_type = ResolveTypeUID(type_die_offset);
7155
7156 if (element_type)
7157 {
7158 std::vector<uint64_t> element_orders;
7159 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
7160 if (byte_stride == 0 && bit_stride == 0)
7161 byte_stride = element_type->GetByteSize();
Greg Clayton57ee3062013-07-11 22:46:58 +00007162 ClangASTType array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007163 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
Siva Chandra89ce9552015-01-05 23:06:14 +00007164 if (element_orders.size() > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007165 {
Siva Chandra89ce9552015-01-05 23:06:14 +00007166 uint64_t num_elements = 0;
7167 std::vector<uint64_t>::const_reverse_iterator pos;
7168 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
7169 for (pos = element_orders.rbegin(); pos != end; ++pos)
7170 {
7171 num_elements = *pos;
7172 clang_type = ast.CreateArrayType (array_element_type,
7173 num_elements,
7174 is_vector);
7175 array_element_type = clang_type;
7176 array_element_bit_stride = num_elements ?
7177 array_element_bit_stride * num_elements :
7178 array_element_bit_stride;
7179 }
7180 }
7181 else
7182 {
7183 clang_type = ast.CreateArrayType (array_element_type, 0, is_vector);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007184 }
7185 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00007186 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00007187 this,
7188 empty_name,
7189 array_element_bit_stride / 8,
7190 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00007191 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00007192 Type::eEncodingIsUID,
7193 &decl,
7194 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00007195 Type::eResolveStateFull));
7196 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007197 }
7198 }
7199 }
7200 break;
7201
Greg Clayton9b81a312010-06-12 01:20:30 +00007202 case DW_TAG_ptr_to_member_type:
7203 {
7204 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
7205 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
7206
Greg Claytond88d7592010-09-15 08:33:30 +00007207 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007208
Greg Clayton9b81a312010-06-12 01:20:30 +00007209 if (num_attributes > 0) {
7210 uint32_t i;
7211 for (i=0; i<num_attributes; ++i)
7212 {
7213 attr = attributes.AttributeAtIndex(i);
Greg Clayton9b81a312010-06-12 01:20:30 +00007214 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
7215 {
7216 switch (attr)
7217 {
7218 case DW_AT_type:
Greg Clayton54166af2014-11-22 01:58:59 +00007219 type_die_offset = form_value.Reference(); break;
Greg Clayton9b81a312010-06-12 01:20:30 +00007220 case DW_AT_containing_type:
Greg Clayton54166af2014-11-22 01:58:59 +00007221 containing_type_die_offset = form_value.Reference(); break;
Greg Clayton9b81a312010-06-12 01:20:30 +00007222 }
7223 }
7224 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007225
Greg Clayton9b81a312010-06-12 01:20:30 +00007226 Type *pointee_type = ResolveTypeUID(type_die_offset);
7227 Type *class_type = ResolveTypeUID(containing_type_die_offset);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007228
Greg Clayton57ee3062013-07-11 22:46:58 +00007229 ClangASTType pointee_clang_type = pointee_type->GetClangForwardType();
7230 ClangASTType class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00007231
Pavel Labathc7c30eb2015-06-08 23:38:06 +00007232 clang_type = pointee_clang_type.CreateMemberPointerType(class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00007233
Enrico Granata1cd5e922015-01-28 00:07:51 +00007234 byte_size = clang_type.GetByteSize(nullptr);
Greg Clayton9b81a312010-06-12 01:20:30 +00007235
Greg Clayton81c22f62011-10-19 18:09:39 +00007236 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00007237 this,
7238 type_name_const_str,
7239 byte_size,
7240 NULL,
7241 LLDB_INVALID_UID,
7242 Type::eEncodingIsUID,
7243 NULL,
7244 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00007245 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00007246 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00007247
Greg Clayton9b81a312010-06-12 01:20:30 +00007248 break;
7249 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007250 default:
Greg Clayton84afacd2012-11-07 23:09:32 +00007251 GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
7252 die->GetOffset(),
7253 tag,
7254 DW_TAG_value_to_name(tag));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007255 break;
7256 }
7257
7258 if (type_sp.get())
7259 {
7260 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
7261 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
7262
7263 SymbolContextScope * symbol_context_scope = NULL;
7264 if (sc_parent_tag == DW_TAG_compile_unit)
7265 {
7266 symbol_context_scope = sc.comp_unit;
7267 }
Jason Molenda60db6e42014-10-16 01:40:16 +00007268 else if (sc.function != NULL && sc_parent_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007269 {
Greg Clayton81c22f62011-10-19 18:09:39 +00007270 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007271 if (symbol_context_scope == NULL)
7272 symbol_context_scope = sc.function;
7273 }
7274
7275 if (symbol_context_scope != NULL)
7276 {
7277 type_sp->SetSymbolContextScope(symbol_context_scope);
7278 }
7279
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00007280 // We are ready to put this type into the uniqued list up at the module level
7281 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00007282
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00007283 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007284 }
7285 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00007286 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007287 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00007288 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007289 }
7290 }
7291 return type_sp;
7292}
7293
7294size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00007295SymbolFileDWARF::ParseTypes
7296(
7297 const SymbolContext& sc,
7298 DWARFCompileUnit* dwarf_cu,
7299 const DWARFDebugInfoEntry *die,
7300 bool parse_siblings,
7301 bool parse_children
7302)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007303{
7304 size_t types_added = 0;
7305 while (die != NULL)
7306 {
7307 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00007308 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007309 {
7310 if (type_is_new)
7311 ++types_added;
7312 }
7313
7314 if (parse_children && die->HasChildren())
7315 {
7316 if (die->Tag() == DW_TAG_subprogram)
7317 {
7318 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00007319 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007320 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
7321 }
7322 else
7323 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
7324 }
7325
7326 if (parse_siblings)
7327 die = die->GetSibling();
7328 else
7329 die = NULL;
7330 }
7331 return types_added;
7332}
7333
7334
7335size_t
7336SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
7337{
7338 assert(sc.comp_unit && sc.function);
7339 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00007340 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007341 if (dwarf_cu)
7342 {
7343 dw_offset_t function_die_offset = sc.function->GetID();
7344 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
7345 if (function_die)
7346 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00007347 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007348 }
7349 }
7350
7351 return functions_added;
7352}
7353
7354
7355size_t
7356SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
7357{
7358 // At least a compile unit must be valid
7359 assert(sc.comp_unit);
7360 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00007361 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007362 if (dwarf_cu)
7363 {
7364 if (sc.function)
7365 {
7366 dw_offset_t function_die_offset = sc.function->GetID();
7367 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
7368 if (func_die && func_die->HasChildren())
7369 {
7370 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
7371 }
7372 }
7373 else
7374 {
7375 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
7376 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
7377 {
7378 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
7379 }
7380 }
7381 }
7382
7383 return types_added;
7384}
7385
7386size_t
7387SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
7388{
7389 if (sc.comp_unit != NULL)
7390 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00007391 DWARFDebugInfo* info = DebugInfo();
7392 if (info == NULL)
7393 return 0;
7394
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007395 if (sc.function)
7396 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007397 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitContainingDIE(sc.function->GetID()).get();
7398
7399 if (dwarf_cu == NULL)
7400 return 0;
7401
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007402 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00007403
Greg Claytonc7bece562013-01-25 18:06:21 +00007404 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
7405 if (func_lo_pc != LLDB_INVALID_ADDRESS)
Greg Claytone38a5ed2012-01-05 03:57:59 +00007406 {
7407 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00007408
Greg Claytone38a5ed2012-01-05 03:57:59 +00007409 // Let all blocks know they have parse all their variables
7410 sc.function->GetBlock (false).SetDidParseVariables (true, true);
7411 return num_variables;
7412 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007413 }
7414 else if (sc.comp_unit)
7415 {
Greg Clayton9422dd62013-03-04 21:46:16 +00007416 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID()).get();
7417
7418 if (dwarf_cu == NULL)
7419 return 0;
7420
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007421 uint32_t vars_added = 0;
7422 VariableListSP variables (sc.comp_unit->GetVariableList(false));
7423
7424 if (variables.get() == NULL)
7425 {
7426 variables.reset(new VariableList());
7427 sc.comp_unit->SetVariableList(variables);
7428
Greg Claytond4a2b372011-09-12 23:21:58 +00007429 DWARFCompileUnit* match_dwarf_cu = NULL;
7430 const DWARFDebugInfoEntry* die = NULL;
7431 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00007432 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00007433 {
Greg Clayton97fbc342011-10-20 22:30:33 +00007434 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00007435 {
7436 DWARFMappedHash::DIEInfoArray hash_data_array;
7437 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
7438 dwarf_cu->GetNextCompileUnitOffset(),
7439 hash_data_array))
7440 {
7441 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
7442 }
7443 }
Greg Clayton7f995132011-10-04 22:41:51 +00007444 }
7445 else
7446 {
7447 // Index if we already haven't to make sure the compile units
7448 // get indexed and make their global DIE index list
7449 if (!m_indexed)
7450 Index ();
7451
7452 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
7453 dwarf_cu->GetNextCompileUnitOffset(),
7454 die_offsets);
7455 }
7456
7457 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00007458 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007459 {
Greg Claytond4a2b372011-09-12 23:21:58 +00007460 DWARFDebugInfo* debug_info = DebugInfo();
7461 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007462 {
Greg Claytond4a2b372011-09-12 23:21:58 +00007463 const dw_offset_t die_offset = die_offsets[i];
7464 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00007465 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00007466 {
Greg Clayton95d87902011-11-11 03:16:25 +00007467 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
7468 if (var_sp)
7469 {
7470 variables->AddVariableIfUnique (var_sp);
7471 ++vars_added;
7472 }
Greg Claytond4a2b372011-09-12 23:21:58 +00007473 }
Greg Clayton95d87902011-11-11 03:16:25 +00007474 else
7475 {
7476 if (m_using_apple_tables)
7477 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00007478 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 +00007479 }
7480 }
7481
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007482 }
7483 }
7484 }
7485 return vars_added;
7486 }
7487 }
7488 return 0;
7489}
7490
7491
7492VariableSP
7493SymbolFileDWARF::ParseVariableDIE
7494(
7495 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00007496 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00007497 const DWARFDebugInfoEntry *die,
7498 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007499)
7500{
Greg Clayton83c5cd92010-11-14 22:13:40 +00007501 VariableSP var_sp (m_die_to_variable_sp[die]);
7502 if (var_sp)
7503 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007504
7505 const dw_tag_t tag = die->Tag();
Richard Mitton0a558352013-10-17 21:14:00 +00007506 ModuleSP module = GetObjectFile()->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00007507
7508 if ((tag == DW_TAG_variable) ||
7509 (tag == DW_TAG_constant) ||
7510 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007511 {
Greg Clayton7f995132011-10-04 22:41:51 +00007512 DWARFDebugInfoEntry::Attributes attributes;
7513 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
7514 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007515 {
Greg Clayton7f995132011-10-04 22:41:51 +00007516 const char *name = NULL;
7517 const char *mangled = NULL;
7518 Declaration decl;
7519 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00007520 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00007521 DWARFExpression location;
7522 bool is_external = false;
7523 bool is_artificial = false;
7524 bool location_is_const_value_data = false;
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007525 bool has_explicit_location = false;
Enrico Granata4ec130d2014-08-11 19:16:35 +00007526 DWARFFormValue const_value;
Greg Clayton23f59502012-07-17 03:23:13 +00007527 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00007528
7529 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007530 {
Greg Clayton7f995132011-10-04 22:41:51 +00007531 dw_attr_t attr = attributes.AttributeAtIndex(i);
7532 DWARFFormValue form_value;
Greg Clayton54166af2014-11-22 01:58:59 +00007533
Greg Clayton7f995132011-10-04 22:41:51 +00007534 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007535 {
Greg Clayton7f995132011-10-04 22:41:51 +00007536 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007537 {
Greg Clayton7f995132011-10-04 22:41:51 +00007538 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
7539 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
7540 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
7541 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton71415542012-12-08 00:24:40 +00007542 case DW_AT_linkage_name:
Greg Clayton7f995132011-10-04 22:41:51 +00007543 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton54166af2014-11-22 01:58:59 +00007544 case DW_AT_type: type_uid = form_value.Reference(); break;
Greg Clayton1c8ef472013-04-05 23:27:21 +00007545 case DW_AT_external: is_external = form_value.Boolean(); break;
Greg Clayton7f995132011-10-04 22:41:51 +00007546 case DW_AT_const_value:
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007547 // If we have already found a DW_AT_location attribute, ignore this attribute.
7548 if (!has_explicit_location)
7549 {
7550 location_is_const_value_data = true;
7551 // The constant value will be either a block, a data value or a string.
Ed Masteeeae7212013-10-24 20:43:47 +00007552 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007553 if (DWARFFormValue::IsBlockForm(form_value.Form()))
7554 {
7555 // Retrieve the value as a block expression.
7556 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
7557 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00007558 location.CopyOpcodeData(module, debug_info_data, block_offset, block_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007559 }
7560 else if (DWARFFormValue::IsDataForm(form_value.Form()))
7561 {
7562 // Retrieve the value as a data expression.
Greg Clayton54166af2014-11-22 01:58:59 +00007563 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007564 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
7565 uint32_t data_length = fixed_form_sizes[form_value.Form()];
Enrico Granata4ec130d2014-08-11 19:16:35 +00007566 if (data_length == 0)
7567 {
7568 const uint8_t *data_pointer = form_value.BlockData();
7569 if (data_pointer)
7570 {
Jason Molenda18f5fd32014-10-16 07:52:17 +00007571 form_value.Unsigned();
Enrico Granata4ec130d2014-08-11 19:16:35 +00007572 }
7573 else if (DWARFFormValue::IsDataForm(form_value.Form()))
7574 {
7575 // we need to get the byte size of the type later after we create the variable
7576 const_value = form_value;
7577 }
7578 }
7579 else
7580 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007581 }
7582 else
7583 {
7584 // Retrieve the value as a string expression.
7585 if (form_value.Form() == DW_FORM_strp)
7586 {
Greg Clayton54166af2014-11-22 01:58:59 +00007587 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), attributes.CompileUnitAtIndex(i)->IsDWARF64());
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007588 uint32_t data_offset = attributes.DIEOffsetAtIndex(i);
7589 uint32_t data_length = fixed_form_sizes[form_value.Form()];
Richard Mitton0a558352013-10-17 21:14:00 +00007590 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007591 }
7592 else
7593 {
7594 const char *str = form_value.AsCString(&debug_info_data);
7595 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart();
7596 uint32_t string_length = strlen(str) + 1;
Richard Mitton0a558352013-10-17 21:14:00 +00007597 location.CopyOpcodeData(module, debug_info_data, string_offset, string_length);
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007598 }
7599 }
7600 }
7601 break;
Greg Clayton7f995132011-10-04 22:41:51 +00007602 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007603 {
Andrew Kaylorb32581f2013-02-13 19:57:06 +00007604 location_is_const_value_data = false;
7605 has_explicit_location = true;
Greg Clayton7f995132011-10-04 22:41:51 +00007606 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007607 {
Ed Masteeeae7212013-10-24 20:43:47 +00007608 const DWARFDataExtractor& debug_info_data = get_debug_info_data();
Greg Clayton7f995132011-10-04 22:41:51 +00007609
7610 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
7611 uint32_t block_length = form_value.Unsigned();
Richard Mitton0a558352013-10-17 21:14:00 +00007612 location.CopyOpcodeData(module, get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00007613 }
7614 else
7615 {
Ed Masteeeae7212013-10-24 20:43:47 +00007616 const DWARFDataExtractor& debug_loc_data = get_debug_loc_data();
Greg Clayton7f995132011-10-04 22:41:51 +00007617 const dw_offset_t debug_loc_offset = form_value.Unsigned();
7618
7619 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
7620 if (loc_list_length > 0)
7621 {
Richard Mitton0a558352013-10-17 21:14:00 +00007622 location.CopyOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00007623 assert (func_low_pc != LLDB_INVALID_ADDRESS);
Greg Clayton54166af2014-11-22 01:58:59 +00007624 location.SetLocationListSlide (func_low_pc - attributes.CompileUnitAtIndex(i)->GetBaseAddress());
Greg Clayton7f995132011-10-04 22:41:51 +00007625 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007626 }
7627 }
Greg Clayton7f995132011-10-04 22:41:51 +00007628 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007629
Greg Clayton1c8ef472013-04-05 23:27:21 +00007630 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00007631 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00007632 case DW_AT_declaration:
7633 case DW_AT_description:
7634 case DW_AT_endianity:
7635 case DW_AT_segment:
7636 case DW_AT_start_scope:
7637 case DW_AT_visibility:
7638 default:
7639 case DW_AT_abstract_origin:
7640 case DW_AT_sibling:
7641 case DW_AT_specification:
7642 break;
7643 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007644 }
7645 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007646
Greg Clayton9e9f2192013-05-17 00:55:28 +00007647 ValueType scope = eValueTypeInvalid;
7648
7649 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
7650 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
7651 SymbolContextScope * symbol_context_scope = NULL;
7652
Siva Chandra0783ab92015-03-24 18:32:27 +00007653 if (!mangled)
7654 {
7655 // LLDB relies on the mangled name (DW_TAG_linkage_name or DW_AT_MIPS_linkage_name) to
7656 // generate fully qualified names of global variables with commands like "frame var j".
7657 // For example, if j were an int variable holding a value 4 and declared in a namespace
7658 // B which in turn is contained in a namespace A, the command "frame var j" returns
7659 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we should be able
7660 // to generate a fully qualified name from the declaration context.
7661 if (die->GetParent()->Tag() == DW_TAG_compile_unit &&
7662 LanguageRuntime::LanguageIsCPlusPlus(dwarf_cu->GetLanguageType()))
7663 {
7664 DWARFDeclContext decl_ctx;
7665
7666 die->GetDWARFDeclContext(this, dwarf_cu, decl_ctx);
7667 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString();
7668 }
7669 }
7670
Greg Clayton9e9f2192013-05-17 00:55:28 +00007671 // DWARF doesn't specify if a DW_TAG_variable is a local, global
7672 // or static variable, so we have to do a little digging by
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00007673 // looking at the location of a variable to see if it contains
Greg Clayton9e9f2192013-05-17 00:55:28 +00007674 // a DW_OP_addr opcode _somewhere_ in the definition. I say
7675 // somewhere because clang likes to combine small global variables
7676 // into the same symbol and have locations like:
7677 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
7678 // So if we don't have a DW_TAG_formal_parameter, we can look at
7679 // the location to see if it contains a DW_OP_addr opcode, and
7680 // then we can correctly classify our variables.
7681 if (tag == DW_TAG_formal_parameter)
7682 scope = eValueTypeVariableArgument;
7683 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007684 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007685 bool op_error = false;
7686 // Check if the location has a DW_OP_addr with any address value...
7687 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS;
7688 if (!location_is_const_value_data)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007689 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007690 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error);
7691 if (op_error)
Greg Clayton96c09682012-01-04 22:56:43 +00007692 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007693 StreamString strm;
7694 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
7695 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 +00007696 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00007697 }
Greg Claytond1767f02011-12-08 02:13:16 +00007698
Greg Clayton9e9f2192013-05-17 00:55:28 +00007699 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS)
7700 {
7701 if (is_external)
7702 scope = eValueTypeVariableGlobal;
7703 else
7704 scope = eValueTypeVariableStatic;
7705
7706
7707 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile ();
7708
7709 if (debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007710 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007711 // When leaving the DWARF in the .o files on darwin,
7712 // when we have a global variable that wasn't initialized,
7713 // the .o file might not have allocated a virtual
7714 // address for the global variable. In this case it will
7715 // have created a symbol for the global variable
7716 // that is undefined/data and external and the value will
7717 // be the byte size of the variable. When we do the
7718 // address map in SymbolFileDWARFDebugMap we rely on
7719 // having an address, we need to do some magic here
7720 // so we can get the correct address for our global
7721 // variable. The address for all of these entries
7722 // will be zero, and there will be an undefined symbol
7723 // in this object file, and the executable will have
7724 // a matching symbol with a good address. So here we
7725 // dig up the correct address and replace it in the
7726 // location for the variable, and set the variable's
7727 // symbol context scope to be that of the main executable
7728 // so the file address will resolve correctly.
7729 bool linked_oso_file_addr = false;
7730 if (is_external && location_DW_OP_addr == 0)
Greg Clayton9422dd62013-03-04 21:46:16 +00007731 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007732 // we have a possible uninitialized extern global
7733 ConstString const_name(mangled ? mangled : name);
7734 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile();
7735 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007736 {
Greg Clayton3046e662013-07-10 01:23:25 +00007737 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
Greg Clayton9e9f2192013-05-17 00:55:28 +00007738 if (debug_map_symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007739 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007740 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
7741 eSymbolTypeData,
7742 Symtab::eDebugYes,
7743 Symtab::eVisibilityExtern);
7744 if (exe_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007745 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007746 if (exe_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007747 {
Greg Clayton358cf1e2015-06-25 21:46:34 +00007748 const addr_t exe_file_addr = exe_symbol->GetAddressRef().GetFileAddress();
Greg Clayton9e9f2192013-05-17 00:55:28 +00007749 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007750 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007751 if (location.Update_DW_OP_addr (exe_file_addr))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007752 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007753 linked_oso_file_addr = true;
7754 symbol_context_scope = exe_symbol;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007755 }
7756 }
7757 }
7758 }
7759 }
7760 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00007761 }
Greg Clayton9422dd62013-03-04 21:46:16 +00007762
Greg Clayton9e9f2192013-05-17 00:55:28 +00007763 if (!linked_oso_file_addr)
7764 {
7765 // The DW_OP_addr is not zero, but it contains a .o file address which
7766 // needs to be linked up correctly.
7767 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr);
7768 if (exe_file_addr != LLDB_INVALID_ADDRESS)
Greg Clayton9422dd62013-03-04 21:46:16 +00007769 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007770 // Update the file address for this variable
7771 location.Update_DW_OP_addr (exe_file_addr);
7772 }
7773 else
7774 {
7775 // Variable didn't make it into the final executable
7776 return var_sp;
Greg Clayton9422dd62013-03-04 21:46:16 +00007777 }
Greg Claytond1767f02011-12-08 02:13:16 +00007778 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00007779 }
Greg Clayton5cf58b92011-10-05 22:22:08 +00007780 }
7781 else
7782 {
Greg Clayton9e9f2192013-05-17 00:55:28 +00007783 scope = eValueTypeVariableLocal;
Greg Clayton5cf58b92011-10-05 22:22:08 +00007784 }
Greg Clayton7f995132011-10-04 22:41:51 +00007785 }
Greg Clayton9e9f2192013-05-17 00:55:28 +00007786
7787 if (symbol_context_scope == NULL)
7788 {
7789 switch (parent_tag)
7790 {
7791 case DW_TAG_subprogram:
7792 case DW_TAG_inlined_subroutine:
7793 case DW_TAG_lexical_block:
7794 if (sc.function)
7795 {
7796 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
7797 if (symbol_context_scope == NULL)
7798 symbol_context_scope = sc.function;
7799 }
7800 break;
7801
7802 default:
7803 symbol_context_scope = sc.comp_unit;
7804 break;
7805 }
7806 }
7807
7808 if (symbol_context_scope)
7809 {
Enrico Granata4ec130d2014-08-11 19:16:35 +00007810 SymbolFileTypeSP type_sp(new SymbolFileType(*this, type_uid));
7811
7812 if (const_value.Form() && type_sp && type_sp->GetType())
7813 location.CopyOpcodeData(const_value.Unsigned(), type_sp->GetType()->GetByteSize(), dwarf_cu->GetAddressByteSize());
7814
Greg Clayton9e9f2192013-05-17 00:55:28 +00007815 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
7816 name,
7817 mangled,
Enrico Granata4ec130d2014-08-11 19:16:35 +00007818 type_sp,
Greg Clayton9e9f2192013-05-17 00:55:28 +00007819 scope,
7820 symbol_context_scope,
7821 &decl,
7822 location,
7823 is_external,
7824 is_artificial));
7825
7826 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
7827 }
7828 else
7829 {
7830 // Not ready to parse this variable yet. It might be a global
7831 // or static variable that is in a function scope and the function
7832 // in the symbol context wasn't filled in yet
7833 return var_sp;
7834 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007835 }
Greg Clayton7f995132011-10-04 22:41:51 +00007836 // Cache var_sp even if NULL (the variable was just a specification or
7837 // was missing vital information to be able to be displayed in the debugger
7838 // (missing location due to optimization, etc)) so we don't re-parse
7839 // this DIE over and over later...
7840 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007841 }
7842 return var_sp;
7843}
7844
Greg Claytonc662ec82011-06-17 22:10:16 +00007845
7846const DWARFDebugInfoEntry *
7847SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
7848 dw_offset_t spec_block_die_offset,
7849 DWARFCompileUnit **result_die_cu_handle)
7850{
7851 // Give the concrete function die specified by "func_die_offset", find the
7852 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
7853 // to "spec_block_die_offset"
7854 DWARFDebugInfo* info = DebugInfo();
7855
7856 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
7857 if (die)
7858 {
7859 assert (*result_die_cu_handle);
7860 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
7861 }
7862 return NULL;
7863}
7864
7865
7866const DWARFDebugInfoEntry *
7867SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
7868 const DWARFDebugInfoEntry *die,
7869 dw_offset_t spec_block_die_offset,
7870 DWARFCompileUnit **result_die_cu_handle)
7871{
7872 if (die)
7873 {
7874 switch (die->Tag())
7875 {
7876 case DW_TAG_subprogram:
7877 case DW_TAG_inlined_subroutine:
7878 case DW_TAG_lexical_block:
7879 {
7880 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
7881 {
7882 *result_die_cu_handle = dwarf_cu;
7883 return die;
7884 }
7885
7886 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
7887 {
7888 *result_die_cu_handle = dwarf_cu;
7889 return die;
7890 }
7891 }
7892 break;
7893 }
7894
7895 // Give the concrete function die specified by "func_die_offset", find the
7896 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
7897 // to "spec_block_die_offset"
7898 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
7899 {
7900 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
7901 child_die,
7902 spec_block_die_offset,
7903 result_die_cu_handle);
7904 if (result_die)
7905 return result_die;
7906 }
7907 }
7908
7909 *result_die_cu_handle = NULL;
7910 return NULL;
7911}
7912
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007913size_t
7914SymbolFileDWARF::ParseVariables
7915(
7916 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00007917 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00007918 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007919 const DWARFDebugInfoEntry *orig_die,
7920 bool parse_siblings,
7921 bool parse_children,
7922 VariableList* cc_variable_list
7923)
7924{
7925 if (orig_die == NULL)
7926 return 0;
7927
Greg Claytonc662ec82011-06-17 22:10:16 +00007928 VariableListSP variable_list_sp;
7929
7930 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007931 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00007932 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007933 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007934 dw_tag_t tag = die->Tag();
7935
7936 // Check to see if we have already parsed this variable or constant?
7937 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007938 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007939 if (cc_variable_list)
7940 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007941 }
7942 else
7943 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007944 // We haven't already parsed it, lets do that now.
7945 if ((tag == DW_TAG_variable) ||
7946 (tag == DW_TAG_constant) ||
7947 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007948 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007949 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00007950 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007951 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
7952 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
7953 switch (parent_tag)
7954 {
7955 case DW_TAG_compile_unit:
7956 if (sc.comp_unit != NULL)
7957 {
7958 variable_list_sp = sc.comp_unit->GetVariableList(false);
7959 if (variable_list_sp.get() == NULL)
7960 {
7961 variable_list_sp.reset(new VariableList());
7962 sc.comp_unit->SetVariableList(variable_list_sp);
7963 }
7964 }
7965 else
7966 {
Daniel Malead01b2952012-11-29 21:49:15 +00007967 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8" PRIx64 " %s with no valid compile unit in symbol context for 0x%8.8" PRIx64 " %s.\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00007968 MakeUserID(sc_parent_die->GetOffset()),
7969 DW_TAG_value_to_name (parent_tag),
7970 MakeUserID(orig_die->GetOffset()),
7971 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007972 }
7973 break;
7974
7975 case DW_TAG_subprogram:
7976 case DW_TAG_inlined_subroutine:
7977 case DW_TAG_lexical_block:
7978 if (sc.function != NULL)
7979 {
7980 // Check to see if we already have parsed the variables for the given scope
7981
Greg Clayton81c22f62011-10-19 18:09:39 +00007982 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007983 if (block == NULL)
7984 {
7985 // This must be a specification or abstract origin with
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00007986 // a concrete block counterpart in the current function. We need
Greg Claytonc662ec82011-06-17 22:10:16 +00007987 // to find the concrete block so we can correctly add the
7988 // variable to it
7989 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
7990 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
7991 sc_parent_die->GetOffset(),
7992 &concrete_block_die_cu);
7993 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00007994 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007995 }
7996
7997 if (block != NULL)
7998 {
7999 const bool can_create = false;
8000 variable_list_sp = block->GetBlockVariableList (can_create);
8001 if (variable_list_sp.get() == NULL)
8002 {
8003 variable_list_sp.reset(new VariableList());
8004 block->SetVariableList(variable_list_sp);
8005 }
8006 }
8007 }
8008 break;
8009
8010 default:
Daniel Malead01b2952012-11-29 21:49:15 +00008011 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8" PRIx64 " %s.\n",
Greg Claytone38a5ed2012-01-05 03:57:59 +00008012 MakeUserID(orig_die->GetOffset()),
8013 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00008014 break;
8015 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00008016 }
Greg Claytonc662ec82011-06-17 22:10:16 +00008017
8018 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00008019 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00008020 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
8021 if (var_sp)
8022 {
Greg Claytonc662ec82011-06-17 22:10:16 +00008023 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00008024 if (cc_variable_list)
8025 cc_variable_list->AddVariableIfUnique (var_sp);
8026 ++vars_added;
8027 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00008028 }
8029 }
8030 }
Greg Claytonc662ec82011-06-17 22:10:16 +00008031
8032 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
8033
8034 if (!skip_children && parse_children && die->HasChildren())
8035 {
8036 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
8037 }
8038
8039 if (parse_siblings)
8040 die = die->GetSibling();
8041 else
8042 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00008043 }
Greg Claytonc662ec82011-06-17 22:10:16 +00008044 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00008045}
8046
8047//------------------------------------------------------------------
8048// PluginInterface protocol
8049//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +00008050ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +00008051SymbolFileDWARF::GetPluginName()
8052{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00008053 return GetPluginNameStatic();
8054}
8055
8056uint32_t
8057SymbolFileDWARF::GetPluginVersion()
8058{
8059 return 1;
8060}
8061
8062void
Greg Clayton6beaaa62011-01-17 03:46:26 +00008063SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
8064{
8065 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
Greg Clayton57ee3062013-07-11 22:46:58 +00008066 ClangASTType clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00008067 if (clang_type)
8068 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
8069}
8070
8071void
8072SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
8073{
8074 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
Greg Clayton57ee3062013-07-11 22:46:58 +00008075 ClangASTType clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
Greg Clayton6beaaa62011-01-17 03:46:26 +00008076 if (clang_type)
8077 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
8078}
8079
Greg Claytona2721472011-06-25 00:44:06 +00008080void
Sean Callanancc427fa2011-07-30 02:42:06 +00008081SymbolFileDWARF::DumpIndexes ()
8082{
8083 StreamFile s(stdout, false);
8084
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00008085 s.Printf ("DWARF index for (%s) '%s':",
Sean Callanancc427fa2011-07-30 02:42:06 +00008086 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00008087 GetObjectFile()->GetFileSpec().GetPath().c_str());
Sean Callanancc427fa2011-07-30 02:42:06 +00008088 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
8089 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
8090 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
8091 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
8092 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
8093 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
8094 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Bruce Mitchenere171da52015-07-22 00:16:02 +00008095 s.Printf("\nNamespaces:\n"); m_namespace_index.Dump (&s);
Sean Callanancc427fa2011-07-30 02:42:06 +00008096}
8097
8098void
8099SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
8100 const char *name,
8101 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00008102{
Sean Callanancc427fa2011-07-30 02:42:06 +00008103 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00008104
8105 if (iter == m_decl_ctx_to_die.end())
8106 return;
8107
Greg Claytoncb5860a2011-10-13 23:49:28 +00008108 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00008109 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00008110 const DWARFDebugInfoEntry *context_die = *pos;
8111
8112 if (!results)
8113 return;
8114
8115 DWARFDebugInfo* info = DebugInfo();
8116
8117 DIEArray die_offsets;
8118
8119 DWARFCompileUnit* dwarf_cu = NULL;
8120 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00008121
8122 if (m_using_apple_tables)
8123 {
8124 if (m_apple_types_ap.get())
8125 m_apple_types_ap->FindByName (name, die_offsets);
8126 }
8127 else
8128 {
8129 if (!m_indexed)
8130 Index ();
8131
8132 m_type_index.Find (ConstString(name), die_offsets);
8133 }
8134
Greg Clayton220a0072011-12-09 08:48:30 +00008135 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00008136
8137 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00008138 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00008139 for (size_t i = 0; i < num_matches; ++i)
8140 {
8141 const dw_offset_t die_offset = die_offsets[i];
8142 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00008143
Greg Claytoncb5860a2011-10-13 23:49:28 +00008144 if (die->GetParent() != context_die)
8145 continue;
8146
8147 Type *matching_type = ResolveType (dwarf_cu, die);
8148
Pavel Labathc7c30eb2015-06-08 23:38:06 +00008149 clang::QualType qual_type = matching_type->GetClangForwardType().GetQualType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00008150
8151 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
8152 {
8153 clang::TagDecl *tag_decl = tag_type->getDecl();
8154 results->push_back(tag_decl);
8155 }
8156 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
8157 {
8158 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
8159 results->push_back(typedef_decl);
8160 }
Greg Claytona2721472011-06-25 00:44:06 +00008161 }
8162 }
8163 }
8164}
8165
8166void
8167SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00008168 const clang::DeclContext *decl_context,
8169 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00008170 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
8171{
Greg Clayton85ae2e12011-10-18 23:36:41 +00008172
8173 switch (decl_context->getDeclKind())
8174 {
8175 case clang::Decl::Namespace:
8176 case clang::Decl::TranslationUnit:
8177 {
8178 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
8179 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
8180 }
8181 break;
8182 default:
8183 break;
8184 }
Greg Claytona2721472011-06-25 00:44:06 +00008185}
Greg Claytoncaab74e2012-01-28 00:48:57 +00008186
Zachary Turner504f38d2015-03-24 16:24:50 +00008187bool
8188SymbolFileDWARF::LayoutRecordType(void *baton, const clang::RecordDecl *record_decl, uint64_t &size,
8189 uint64_t &alignment,
Zachary Turnera98fac22015-03-24 18:56:08 +00008190 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
8191 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
8192 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
Greg Claytoncaab74e2012-01-28 00:48:57 +00008193{
8194 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
8195 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
8196}
8197
Zachary Turner504f38d2015-03-24 16:24:50 +00008198bool
8199SymbolFileDWARF::LayoutRecordType(const clang::RecordDecl *record_decl, uint64_t &bit_size, uint64_t &alignment,
Zachary Turnera98fac22015-03-24 18:56:08 +00008200 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
8201 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
8202 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
Greg Claytoncaab74e2012-01-28 00:48:57 +00008203{
Greg Clayton5160ce52013-03-27 23:08:40 +00008204 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Claytoncaab74e2012-01-28 00:48:57 +00008205 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
8206 bool success = false;
8207 base_offsets.clear();
8208 vbase_offsets.clear();
8209 if (pos != m_record_decl_to_layout_map.end())
8210 {
8211 bit_size = pos->second.bit_size;
8212 alignment = pos->second.alignment;
8213 field_offsets.swap(pos->second.field_offsets);
Greg Clayton2508b9b2012-11-01 23:20:02 +00008214 base_offsets.swap (pos->second.base_offsets);
8215 vbase_offsets.swap (pos->second.vbase_offsets);
Greg Claytoncaab74e2012-01-28 00:48:57 +00008216 m_record_decl_to_layout_map.erase(pos);
8217 success = true;
8218 }
8219 else
8220 {
8221 bit_size = 0;
8222 alignment = 0;
8223 field_offsets.clear();
8224 }
8225
8226 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +00008227 GetObjectFile()->GetModule()->LogMessage (log,
Daniel Malead01b2952012-11-29 21:49:15 +00008228 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00008229 static_cast<const void*>(record_decl),
8230 bit_size, alignment,
8231 static_cast<uint32_t>(field_offsets.size()),
8232 static_cast<uint32_t>(base_offsets.size()),
8233 static_cast<uint32_t>(vbase_offsets.size()),
Greg Claytoncaab74e2012-01-28 00:48:57 +00008234 success);
8235 return success;
8236}
8237
8238
Greg Clayton1f746072012-08-29 21:13:06 +00008239SymbolFileDWARFDebugMap *
8240SymbolFileDWARF::GetDebugMapSymfile ()
8241{
8242 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
8243 {
8244 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
8245 if (module_sp)
8246 {
8247 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
8248 if (sym_vendor)
8249 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
8250 }
8251 }
8252 return m_debug_map_symfile;
8253}
8254
Greg Claytoncaab74e2012-01-28 00:48:57 +00008255