blob: 358febc4b3286af8731c467c1a1ed353cd942ab9 [file] [log] [blame]
Greg Clayton261ac3f2015-08-28 01:01:03 +00001//===-- DWARFASTParserClang.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 "DWARFASTParserClang.h"
11#include "DWARFCompileUnit.h"
12#include "DWARFDebugInfo.h"
13#include "DWARFDeclContext.h"
14#include "DWARFDefines.h"
15#include "DWARFDIE.h"
16#include "DWARFDIECollection.h"
17#include "SymbolFileDWARF.h"
18#include "SymbolFileDWARFDebugMap.h"
19#include "UniqueDWARFASTType.h"
20
Jim Inghamaa816b82015-09-02 01:59:14 +000021#include "lldb/Interpreter/Args.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000022#include "lldb/Core/Log.h"
23#include "lldb/Core/Module.h"
Jim Inghamaa816b82015-09-02 01:59:14 +000024#include "lldb/Core/StreamString.h"
25#include "lldb/Core/Value.h"
26#include "lldb/Host/Host.h"
Greg Claytone6b36cd2015-12-08 01:02:08 +000027#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000028#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
29#include "lldb/Symbol/CompileUnit.h"
30#include "lldb/Symbol/Function.h"
31#include "lldb/Symbol/ObjectFile.h"
Greg Claytone6b36cd2015-12-08 01:02:08 +000032#include "lldb/Symbol/SymbolVendor.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000033#include "lldb/Symbol/TypeList.h"
Greg Claytone6b36cd2015-12-08 01:02:08 +000034#include "lldb/Symbol/TypeMap.h"
Jim Ingham0e0984e2015-09-02 01:06:46 +000035#include "lldb/Target/Language.h"
Jim Inghamaa816b82015-09-02 01:59:14 +000036#include "Plugins/Language/ObjC/ObjCLanguage.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000037
38#include "clang/AST/DeclCXX.h"
39#include "clang/AST/DeclObjC.h"
40
Paul Hermanea188fc2015-09-16 18:48:30 +000041#include <map>
42#include <vector>
43
Greg Clayton261ac3f2015-08-28 01:01:03 +000044//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
45
46#ifdef ENABLE_DEBUG_PRINTF
47#include <stdio.h>
48#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
49#else
50#define DEBUG_PRINTF(fmt, ...)
51#endif
52
53
54using namespace lldb;
55using namespace lldb_private;
56DWARFASTParserClang::DWARFASTParserClang (ClangASTContext &ast) :
57 m_ast (ast),
58 m_die_to_decl_ctx (),
59 m_decl_ctx_to_die ()
60{
61}
62
63DWARFASTParserClang::~DWARFASTParserClang ()
64{
65}
66
67
68static AccessType
69DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
70{
71 switch (dwarf_accessibility)
72 {
73 case DW_ACCESS_public: return eAccessPublic;
74 case DW_ACCESS_private: return eAccessPrivate;
75 case DW_ACCESS_protected: return eAccessProtected;
76 default: break;
77 }
78 return eAccessNone;
79}
80
81static bool
82DeclKindIsCXXClass (clang::Decl::Kind decl_kind)
83{
84 switch (decl_kind)
85 {
86 case clang::Decl::CXXRecord:
87 case clang::Decl::ClassTemplateSpecialization:
88 return true;
89 default:
90 break;
91 }
92 return false;
93}
94
95struct BitfieldInfo
96{
97 uint64_t bit_size;
98 uint64_t bit_offset;
99
100 BitfieldInfo () :
101 bit_size (LLDB_INVALID_ADDRESS),
102 bit_offset (LLDB_INVALID_ADDRESS)
103 {
104 }
105
106 void
107 Clear()
108 {
109 bit_size = LLDB_INVALID_ADDRESS;
110 bit_offset = LLDB_INVALID_ADDRESS;
111 }
112
113 bool IsValid ()
114 {
115 return (bit_size != LLDB_INVALID_ADDRESS) &&
116 (bit_offset != LLDB_INVALID_ADDRESS);
117 }
118};
119
Greg Claytone6b36cd2015-12-08 01:02:08 +0000120
121ClangASTImporter &
122DWARFASTParserClang::GetClangASTImporter()
123{
124 if (!m_clang_ast_importer_ap)
125 {
126 m_clang_ast_importer_ap.reset (new ClangASTImporter);
127 }
128 return *m_clang_ast_importer_ap;
129}
130
131
132TypeSP
133DWARFASTParserClang::ParseTypeFromDWO (const DWARFDIE &die, Log *log)
134{
135 ModuleSP dwo_module_sp = die.GetContainingDWOModule();
136 if (dwo_module_sp)
137 {
138 // This type comes from an external DWO module
139 std::vector<CompilerContext> dwo_context;
140 die.GetDWOContext(dwo_context);
141 TypeMap dwo_types;
142 if (dwo_module_sp->GetSymbolVendor()->FindTypes(dwo_context, true, dwo_types))
143 {
144 const size_t num_dwo_types = dwo_types.GetSize();
145 if (num_dwo_types == 1)
146 {
147 // We found a real definition for this type elsewhere
148 // so lets use it and cache the fact that we found
149 // a complete type for this die
150 TypeSP dwo_type_sp = dwo_types.GetTypeAtIndex(0);
151 if (dwo_type_sp)
152 {
153 lldb_private::CompilerType dwo_type = dwo_type_sp->GetForwardCompilerType();
154
155 lldb_private::CompilerType type = GetClangASTImporter().CopyType (m_ast, dwo_type);
156
157 //printf ("copied_qual_type: ast = %p, clang_type = %p, name = '%s'\n", m_ast, copied_qual_type.getAsOpaquePtr(), external_type->GetName().GetCString());
158 if (type)
159 {
160 SymbolFileDWARF *dwarf = die.GetDWARF();
161 TypeSP type_sp (new Type (die.GetID(),
162 dwarf,
163 dwo_type_sp->GetName(),
164 dwo_type_sp->GetByteSize(),
165 NULL,
166 LLDB_INVALID_UID,
167 Type::eEncodingInvalid,
168 &dwo_type_sp->GetDeclaration(),
169 type,
170 Type::eResolveStateForward));
171
172 dwarf->GetTypeList()->Insert(type_sp);
173 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
174 clang::TagDecl *tag_decl = ClangASTContext::GetAsTagDecl(type);
175 if (tag_decl)
176 LinkDeclContextToDIE(tag_decl, die);
177 else
178 {
179 clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(die);
180 if (defn_decl_ctx)
181 LinkDeclContextToDIE(defn_decl_ctx, die);
182 }
183 return type_sp;
184 }
185 }
186 }
187 }
188 }
189 return TypeSP();
190}
191
Greg Clayton261ac3f2015-08-28 01:01:03 +0000192TypeSP
193DWARFASTParserClang::ParseTypeFromDWARF (const SymbolContext& sc,
194 const DWARFDIE &die,
195 Log *log,
196 bool *type_is_new_ptr)
197{
198 TypeSP type_sp;
199
200 if (type_is_new_ptr)
201 *type_is_new_ptr = false;
202
203 AccessType accessibility = eAccessNone;
204 if (die)
205 {
206 SymbolFileDWARF *dwarf = die.GetDWARF();
207 if (log)
208 {
209 DWARFDIE context_die;
210 clang::DeclContext *context = GetClangDeclContextContainingDIE (die, &context_die);
211
212 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
213 die.GetOffset(),
214 static_cast<void*>(context),
215 context_die.GetOffset(),
216 die.GetTagAsCString(),
217 die.GetName());
218
219 }
220 //
221 // Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
222 // if (log && dwarf_cu)
223 // {
224 // StreamString s;
225 // die->DumpLocation (this, dwarf_cu, s);
226 // dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
227 //
228 // }
229
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000230 Type *type_ptr = dwarf->GetDIEToType().lookup (die.GetDIE());
Greg Clayton261ac3f2015-08-28 01:01:03 +0000231 TypeList* type_list = dwarf->GetTypeList();
232 if (type_ptr == NULL)
233 {
234 if (type_is_new_ptr)
235 *type_is_new_ptr = true;
236
237 const dw_tag_t tag = die.Tag();
238
239 bool is_forward_declaration = false;
240 DWARFAttributes attributes;
241 const char *type_name_cstr = NULL;
242 ConstString type_name_const_str;
243 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
244 uint64_t byte_size = 0;
245 Declaration decl;
246
247 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
248 CompilerType clang_type;
249 DWARFFormValue form_value;
250
251 dw_attr_t attr;
252
253 switch (tag)
254 {
Greg Claytona5d1f622016-01-21 22:26:13 +0000255 case DW_TAG_typedef:
256 // Try to parse a typedef from the DWO file first as modules
257 // can contain typedef'ed structures that have no names like:
258 //
259 // typedef struct { int a; } Foo;
260 //
261 // In this case we will have a structure with no name and a
262 // typedef named "Foo" that points to this unnamed structure.
263 // The name in the typedef is the only identifier for the struct,
264 // so always try to get typedefs from DWO files if possible.
265 //
266 // The type_sp returned will be empty if the typedef doesn't exist
267 // in a DWO file, so it is cheap to call this function just to check.
268 //
269 // If we don't do this we end up creating a TypeSP that says this
270 // is a typedef to type 0x123 (the DW_AT_type value would be 0x123
271 // in the DW_TAG_typedef), and this is the unnamed structure type.
272 // We will have a hard time tracking down an unnammed structure
273 // type in the module DWO file, so we make sure we don't get into
274 // this situation by always resolving typedefs from the DWO file.
275 type_sp = ParseTypeFromDWO(die, log);
276 if (type_sp)
277 return type_sp;
278
Greg Clayton261ac3f2015-08-28 01:01:03 +0000279 case DW_TAG_base_type:
280 case DW_TAG_pointer_type:
281 case DW_TAG_reference_type:
282 case DW_TAG_rvalue_reference_type:
Greg Clayton261ac3f2015-08-28 01:01:03 +0000283 case DW_TAG_const_type:
284 case DW_TAG_restrict_type:
285 case DW_TAG_volatile_type:
286 case DW_TAG_unspecified_type:
287 {
288 // Set a bit that lets us know that we are currently parsing this
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000289 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
Greg Clayton261ac3f2015-08-28 01:01:03 +0000290
291 const size_t num_attributes = die.GetAttributes (attributes);
292 uint32_t encoding = 0;
293 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
294
295 if (num_attributes > 0)
296 {
297 uint32_t i;
298 for (i=0; i<num_attributes; ++i)
299 {
300 attr = attributes.AttributeAtIndex(i);
301 if (attributes.ExtractFormValueAtIndex(i, form_value))
302 {
303 switch (attr)
304 {
305 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
306 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
307 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
308 case DW_AT_name:
309
310 type_name_cstr = form_value.AsCString();
311 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
312 // include the "&"...
313 if (tag == DW_TAG_reference_type)
314 {
315 if (strchr (type_name_cstr, '&') == NULL)
316 type_name_cstr = NULL;
317 }
318 if (type_name_cstr)
319 type_name_const_str.SetCString(type_name_cstr);
320 break;
321 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
322 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000323 case DW_AT_type: encoding_uid = DIERef(form_value).GetUID(); break;
Greg Clayton261ac3f2015-08-28 01:01:03 +0000324 default:
325 case DW_AT_sibling:
326 break;
327 }
328 }
329 }
330 }
331
332 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
333
334 switch (tag)
335 {
336 default:
337 break;
338
339 case DW_TAG_unspecified_type:
340 if (strcmp(type_name_cstr, "nullptr_t") == 0 ||
341 strcmp(type_name_cstr, "decltype(nullptr)") == 0 )
342 {
343 resolve_state = Type::eResolveStateFull;
344 clang_type = m_ast.GetBasicType(eBasicTypeNullPtr);
345 break;
346 }
347 // Fall through to base type below in case we can handle the type there...
Jason Molenda62e06812016-02-16 04:14:33 +0000348 LLVM_FALLTHROUGH;
Greg Clayton261ac3f2015-08-28 01:01:03 +0000349
350 case DW_TAG_base_type:
351 resolve_state = Type::eResolveStateFull;
352 clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
353 encoding,
354 byte_size * 8);
355 break;
356
357 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
358 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
359 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
360 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
361 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
362 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
363 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
364 }
365
366 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL)
367 {
368 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
369
370 if (translation_unit_is_objc)
371 {
372 if (type_name_cstr != NULL)
373 {
374 static ConstString g_objc_type_name_id("id");
375 static ConstString g_objc_type_name_Class("Class");
376 static ConstString g_objc_type_name_selector("SEL");
377
378 if (type_name_const_str == g_objc_type_name_id)
379 {
380 if (log)
381 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
382 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
383 die.GetOffset(),
384 die.GetTagAsCString(),
385 die.GetName());
386 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
387 encoding_data_type = Type::eEncodingIsUID;
388 encoding_uid = LLDB_INVALID_UID;
389 resolve_state = Type::eResolveStateFull;
390
391 }
392 else if (type_name_const_str == g_objc_type_name_Class)
393 {
394 if (log)
395 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
396 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
397 die.GetOffset(),
398 die.GetTagAsCString(),
399 die.GetName());
400 clang_type = m_ast.GetBasicType(eBasicTypeObjCClass);
401 encoding_data_type = Type::eEncodingIsUID;
402 encoding_uid = LLDB_INVALID_UID;
403 resolve_state = Type::eResolveStateFull;
404 }
405 else if (type_name_const_str == g_objc_type_name_selector)
406 {
407 if (log)
408 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
409 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
410 die.GetOffset(),
411 die.GetTagAsCString(),
412 die.GetName());
413 clang_type = m_ast.GetBasicType(eBasicTypeObjCSel);
414 encoding_data_type = Type::eEncodingIsUID;
415 encoding_uid = LLDB_INVALID_UID;
416 resolve_state = Type::eResolveStateFull;
417 }
418 }
419 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID)
420 {
421 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id".
422
423 const DWARFDIE encoding_die = die.GetDIE(encoding_uid);
424
425 if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type)
426 {
427 if (const char *struct_name = encoding_die.GetName())
428 {
429 if (!strcmp(struct_name, "objc_object"))
430 {
431 if (log)
432 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
433 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.",
434 die.GetOffset(),
435 die.GetTagAsCString(),
436 die.GetName());
437 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
438 encoding_data_type = Type::eEncodingIsUID;
439 encoding_uid = LLDB_INVALID_UID;
440 resolve_state = Type::eResolveStateFull;
441 }
442 }
443 }
444 }
445 }
446 }
447
448 type_sp.reset( new Type (die.GetID(),
449 dwarf,
450 type_name_const_str,
451 byte_size,
452 NULL,
453 encoding_uid,
454 encoding_data_type,
455 &decl,
456 clang_type,
457 resolve_state));
458
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000459 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
Greg Clayton261ac3f2015-08-28 01:01:03 +0000460
461 // Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
462 // if (encoding_type != NULL)
463 // {
464 // if (encoding_type != DIE_IS_BEING_PARSED)
465 // type_sp->SetEncodingType(encoding_type);
466 // else
467 // m_indirect_fixups.push_back(type_sp.get());
468 // }
469 }
470 break;
471
472 case DW_TAG_structure_type:
473 case DW_TAG_union_type:
474 case DW_TAG_class_type:
475 {
476 // Set a bit that lets us know that we are currently parsing this
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000477 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
Greg Clayton261ac3f2015-08-28 01:01:03 +0000478 bool byte_size_valid = false;
479
480 LanguageType class_language = eLanguageTypeUnknown;
481 bool is_complete_objc_class = false;
482 //bool struct_is_class = false;
483 const size_t num_attributes = die.GetAttributes (attributes);
484 if (num_attributes > 0)
485 {
486 uint32_t i;
487 for (i=0; i<num_attributes; ++i)
488 {
489 attr = attributes.AttributeAtIndex(i);
490 if (attributes.ExtractFormValueAtIndex(i, form_value))
491 {
492 switch (attr)
493 {
494 case DW_AT_decl_file:
495 if (die.GetCU()->DW_AT_decl_file_attributes_are_invalid())
496 {
497 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
498 // point to the compile unit file, so we clear this invalid value
499 // so that we can still unique types efficiently.
500 decl.SetFile(FileSpec ("<invalid>", false));
501 }
502 else
503 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
504 break;
505
506 case DW_AT_decl_line:
507 decl.SetLine(form_value.Unsigned());
508 break;
509
510 case DW_AT_decl_column:
511 decl.SetColumn(form_value.Unsigned());
512 break;
513
514 case DW_AT_name:
515 type_name_cstr = form_value.AsCString();
516 type_name_const_str.SetCString(type_name_cstr);
517 break;
518
519 case DW_AT_byte_size:
520 byte_size = form_value.Unsigned();
521 byte_size_valid = true;
522 break;
523
524 case DW_AT_accessibility:
525 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
526 break;
527
528 case DW_AT_declaration:
529 is_forward_declaration = form_value.Boolean();
530 break;
531
532 case DW_AT_APPLE_runtime_class:
533 class_language = (LanguageType)form_value.Signed();
534 break;
535
536 case DW_AT_APPLE_objc_complete_type:
537 is_complete_objc_class = form_value.Signed();
538 break;
539
540 case DW_AT_allocated:
541 case DW_AT_associated:
542 case DW_AT_data_location:
543 case DW_AT_description:
544 case DW_AT_start_scope:
545 case DW_AT_visibility:
546 default:
547 case DW_AT_sibling:
548 break;
549 }
550 }
551 }
552 }
553
554 // UniqueDWARFASTType is large, so don't create a local variables on the
555 // stack, put it on the heap. This function is often called recursively
556 // and clang isn't good and sharing the stack space for variables in different blocks.
557 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType());
558
Greg Claytonfb85e622016-02-09 22:36:24 +0000559 ConstString unique_typename(type_name_const_str);
560 Declaration unique_decl(decl);
561
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +0000562 if (type_name_const_str)
Greg Clayton261ac3f2015-08-28 01:01:03 +0000563 {
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +0000564 LanguageType die_language = die.GetLanguage();
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +0000565 if (Language::LanguageIsCPlusPlus(die_language))
Greg Clayton261ac3f2015-08-28 01:01:03 +0000566 {
Greg Claytonfb85e622016-02-09 22:36:24 +0000567 // For C++, we rely solely upon the one definition rule that says only
568 // one thing can exist at a given decl context. We ignore the file and
569 // line that things are declared on.
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +0000570 std::string qualified_name;
571 if (die.GetQualifiedName(qualified_name))
Greg Claytonfb85e622016-02-09 22:36:24 +0000572 unique_typename = ConstString(qualified_name);
573 unique_decl.Clear();
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +0000574 }
575
Greg Claytonfb85e622016-02-09 22:36:24 +0000576 if (dwarf->GetUniqueDWARFASTTypeMap().Find(unique_typename, die, unique_decl,
577 byte_size_valid ? byte_size : -1,
578 *unique_ast_entry_ap))
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +0000579 {
Greg Claytonfb85e622016-02-09 22:36:24 +0000580 type_sp = unique_ast_entry_ap->m_type_sp;
581 if (type_sp)
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +0000582 {
Greg Claytonfb85e622016-02-09 22:36:24 +0000583 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
584 return type_sp;
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +0000585 }
586 }
Greg Clayton261ac3f2015-08-28 01:01:03 +0000587 }
588
589 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr);
590
591 int tag_decl_kind = -1;
592 AccessType default_accessibility = eAccessNone;
593 if (tag == DW_TAG_structure_type)
594 {
595 tag_decl_kind = clang::TTK_Struct;
596 default_accessibility = eAccessPublic;
597 }
598 else if (tag == DW_TAG_union_type)
599 {
600 tag_decl_kind = clang::TTK_Union;
601 default_accessibility = eAccessPublic;
602 }
603 else if (tag == DW_TAG_class_type)
604 {
605 tag_decl_kind = clang::TTK_Class;
606 default_accessibility = eAccessPrivate;
607 }
608
609 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
610 die.HasChildren() == false &&
611 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
612 {
613 // Work around an issue with clang at the moment where
614 // forward declarations for objective C classes are emitted
615 // as:
616 // DW_TAG_structure_type [2]
617 // DW_AT_name( "ForwardObjcClass" )
618 // DW_AT_byte_size( 0x00 )
619 // DW_AT_decl_file( "..." )
620 // DW_AT_decl_line( 1 )
621 //
622 // Note that there is no DW_AT_declaration and there are
623 // no children, and the byte size is zero.
624 is_forward_declaration = true;
625 }
626
627 if (class_language == eLanguageTypeObjC ||
628 class_language == eLanguageTypeObjC_plus_plus)
629 {
630 if (!is_complete_objc_class && die.Supports_DW_AT_APPLE_objc_complete_type())
631 {
632 // We have a valid eSymbolTypeObjCClass class symbol whose
633 // name matches the current objective C class that we
634 // are trying to find and this DIE isn't the complete
635 // definition (we checked is_complete_objc_class above and
636 // know it is false), so the real definition is in here somewhere
637 type_sp = dwarf->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
638
639 if (!type_sp)
640 {
641 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
642 if (debug_map_symfile)
643 {
644 // We weren't able to find a full declaration in
645 // this DWARF, see if we have a declaration anywhere
646 // else...
647 type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
648 }
649 }
650
651 if (type_sp)
652 {
653 if (log)
654 {
655 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
656 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64,
657 static_cast<void*>(this),
658 die.GetOffset(),
659 DW_TAG_value_to_name(tag),
660 type_name_cstr,
661 type_sp->GetID());
662 }
663
664 // We found a real definition for this type elsewhere
665 // so lets use it and cache the fact that we found
666 // a complete type for this die
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000667 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
Greg Clayton261ac3f2015-08-28 01:01:03 +0000668 return type_sp;
669 }
670 }
671 }
672
673
674 if (is_forward_declaration)
675 {
676 // We have a forward declaration to a type and we need
677 // to try and find a full declaration. We look in the
678 // current type index just in case we have a forward
679 // declaration followed by an actual declarations in the
680 // DWARF. If this fails, we need to look elsewhere...
681 if (log)
682 {
683 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
684 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
685 static_cast<void*>(this),
686 die.GetOffset(),
687 DW_TAG_value_to_name(tag),
688 type_name_cstr);
689 }
690
Greg Claytone6b36cd2015-12-08 01:02:08 +0000691 // See if the type comes from a DWO module and if so, track down that type.
692 type_sp = ParseTypeFromDWO(die, log);
693 if (type_sp)
694 return type_sp;
695
Greg Clayton261ac3f2015-08-28 01:01:03 +0000696 DWARFDeclContext die_decl_ctx;
697 die.GetDWARFDeclContext(die_decl_ctx);
698
699 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
700 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
701
702 if (!type_sp)
703 {
704 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
705 if (debug_map_symfile)
706 {
707 // We weren't able to find a full declaration in
708 // this DWARF, see if we have a declaration anywhere
709 // else...
710 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
711 }
712 }
713
714 if (type_sp)
715 {
716 if (log)
717 {
718 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
719 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
720 static_cast<void*>(this),
721 die.GetOffset(),
722 DW_TAG_value_to_name(tag),
723 type_name_cstr,
724 type_sp->GetID());
725 }
726
727 // We found a real definition for this type elsewhere
728 // so lets use it and cache the fact that we found
729 // a complete type for this die
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000730 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
Siva Chandra27e33a82015-10-07 22:11:52 +0000731 clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(
732 dwarf->DebugInfo()->GetDIE(DIERef(type_sp->GetID())));
733 if (defn_decl_ctx)
734 LinkDeclContextToDIE(defn_decl_ctx, die);
Greg Clayton261ac3f2015-08-28 01:01:03 +0000735 return type_sp;
736 }
737 }
738 assert (tag_decl_kind != -1);
739 bool clang_type_was_created = false;
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000740 clang_type.SetCompilerType(&m_ast, dwarf->GetForwardDeclDieToClangType().lookup (die.GetDIE()));
Greg Clayton261ac3f2015-08-28 01:01:03 +0000741 if (!clang_type)
742 {
743 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (die, nullptr);
744 if (accessibility == eAccessNone && decl_ctx)
745 {
746 // Check the decl context that contains this class/struct/union.
747 // If it is a class we must give it an accessibility.
748 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
749 if (DeclKindIsCXXClass (containing_decl_kind))
750 accessibility = default_accessibility;
751 }
752
753 ClangASTMetadata metadata;
754 metadata.SetUserID(die.GetID());
755 metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual (die));
756
757 if (type_name_cstr && strchr (type_name_cstr, '<'))
758 {
759 ClangASTContext::TemplateParameterInfos template_param_infos;
760 if (ParseTemplateParameterInfos (die, template_param_infos))
761 {
762 clang::ClassTemplateDecl *class_template_decl = m_ast.ParseClassTemplateDecl (decl_ctx,
763 accessibility,
764 type_name_cstr,
765 tag_decl_kind,
766 template_param_infos);
767
768 clang::ClassTemplateSpecializationDecl *class_specialization_decl = m_ast.CreateClassTemplateSpecializationDecl (decl_ctx,
769 class_template_decl,
770 tag_decl_kind,
771 template_param_infos);
772 clang_type = m_ast.CreateClassTemplateSpecializationType (class_specialization_decl);
773 clang_type_was_created = true;
774
775 m_ast.SetMetadata (class_template_decl, metadata);
776 m_ast.SetMetadata (class_specialization_decl, metadata);
777 }
778 }
779
780 if (!clang_type_was_created)
781 {
782 clang_type_was_created = true;
783 clang_type = m_ast.CreateRecordType (decl_ctx,
784 accessibility,
785 type_name_cstr,
786 tag_decl_kind,
787 class_language,
788 &metadata);
789 }
790 }
791
792 // Store a forward declaration to this class type in case any
793 // parameters in any class methods need it for the clang
794 // types for function prototypes.
795 LinkDeclContextToDIE(m_ast.GetDeclContextForType(clang_type), die);
796 type_sp.reset (new Type (die.GetID(),
797 dwarf,
798 type_name_const_str,
799 byte_size,
800 NULL,
801 LLDB_INVALID_UID,
802 Type::eEncodingIsUID,
803 &decl,
804 clang_type,
805 Type::eResolveStateForward));
806
807 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
808
809
810 // Add our type to the unique type map so we don't
811 // end up creating many copies of the same type over
812 // and over in the ASTContext for our module
813 unique_ast_entry_ap->m_type_sp = type_sp;
814 unique_ast_entry_ap->m_die = die;
Greg Claytonfb85e622016-02-09 22:36:24 +0000815 unique_ast_entry_ap->m_declaration = unique_decl;
Greg Clayton261ac3f2015-08-28 01:01:03 +0000816 unique_ast_entry_ap->m_byte_size = byte_size;
Greg Claytonfb85e622016-02-09 22:36:24 +0000817 dwarf->GetUniqueDWARFASTTypeMap().Insert (unique_typename,
Greg Clayton261ac3f2015-08-28 01:01:03 +0000818 *unique_ast_entry_ap);
819
820 if (is_forward_declaration && die.HasChildren())
821 {
822 // Check to see if the DIE actually has a definition, some version of GCC will
823 // emit DIEs with DW_AT_declaration set to true, but yet still have subprogram,
824 // members, or inheritance, so we can't trust it
825 DWARFDIE child_die = die.GetFirstChild();
826 while (child_die)
827 {
828 switch (child_die.Tag())
829 {
830 case DW_TAG_inheritance:
831 case DW_TAG_subprogram:
832 case DW_TAG_member:
833 case DW_TAG_APPLE_property:
834 case DW_TAG_class_type:
835 case DW_TAG_structure_type:
836 case DW_TAG_enumeration_type:
837 case DW_TAG_typedef:
838 case DW_TAG_union_type:
839 child_die.Clear();
840 is_forward_declaration = false;
841 break;
842 default:
843 child_die = child_die.GetSibling();
844 break;
845 }
846 }
847 }
848
849 if (!is_forward_declaration)
850 {
851 // Always start the definition for a class type so that
852 // if the class has child classes or types that require
853 // the class to be created for use as their decl contexts
854 // the class will be ready to accept these child definitions.
855 if (die.HasChildren() == false)
856 {
857 // No children for this struct/union/class, lets finish it
858 ClangASTContext::StartTagDeclarationDefinition (clang_type);
859 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
860
861 if (tag == DW_TAG_structure_type) // this only applies in C
862 {
863 clang::RecordDecl *record_decl = ClangASTContext::GetAsRecordDecl(clang_type);
864
865 if (record_decl)
866 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, LayoutInfo()));
867 }
868 }
869 else if (clang_type_was_created)
870 {
871 // Start the definition if the class is not objective C since
872 // the underlying decls respond to isCompleteDefinition(). Objective
873 // C decls don't respond to isCompleteDefinition() so we can't
874 // start the declaration definition right away. For C++ class/union/structs
875 // we want to start the definition in case the class is needed as the
876 // declaration context for a contained class or type without the need
877 // to complete that type..
878
879 if (class_language != eLanguageTypeObjC &&
880 class_language != eLanguageTypeObjC_plus_plus)
881 ClangASTContext::StartTagDeclarationDefinition (clang_type);
882
883 // Leave this as a forward declaration until we need
884 // to know the details of the type. lldb_private::Type
885 // will automatically call the SymbolFile virtual function
886 // "SymbolFileDWARF::CompleteType(Type *)"
887 // When the definition needs to be defined.
Tamas Berghammer69d0b332015-10-09 12:43:08 +0000888 assert(!dwarf->GetForwardDeclClangTypeToDie().count(ClangASTContext::RemoveFastQualifiers(clang_type).GetOpaqueQualType()) &&
889 "Type already in the forward declaration map!");
Greg Clayton565aaf62016-02-11 23:36:57 +0000890 // Can't assume m_ast.GetSymbolFile() is actually a SymbolFileDWARF, it can be a
891 // SymbolFileDWARFDebugMap for Apple binaries.
892 //assert(((SymbolFileDWARF*)m_ast.GetSymbolFile())->UserIDMatches(die.GetDIERef().GetUID()) &&
893 // "Adding incorrect type to forward declaration map");
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000894 dwarf->GetForwardDeclDieToClangType()[die.GetDIE()] = clang_type.GetOpaqueQualType();
895 dwarf->GetForwardDeclClangTypeToDie()[ClangASTContext::RemoveFastQualifiers(clang_type).GetOpaqueQualType()] = die.GetDIERef();
Greg Clayton261ac3f2015-08-28 01:01:03 +0000896 m_ast.SetHasExternalStorage (clang_type.GetOpaqueQualType(), true);
897 }
898 }
Greg Clayton261ac3f2015-08-28 01:01:03 +0000899 }
900 break;
901
902 case DW_TAG_enumeration_type:
903 {
904 // Set a bit that lets us know that we are currently parsing this
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000905 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
Greg Clayton261ac3f2015-08-28 01:01:03 +0000906
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000907 DWARFFormValue encoding_form;
Greg Clayton261ac3f2015-08-28 01:01:03 +0000908
909 const size_t num_attributes = die.GetAttributes (attributes);
910 if (num_attributes > 0)
911 {
912 uint32_t i;
913
914 for (i=0; i<num_attributes; ++i)
915 {
916 attr = attributes.AttributeAtIndex(i);
917 if (attributes.ExtractFormValueAtIndex(i, form_value))
918 {
919 switch (attr)
920 {
921 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
922 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
923 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
924 case DW_AT_name:
925 type_name_cstr = form_value.AsCString();
926 type_name_const_str.SetCString(type_name_cstr);
927 break;
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000928 case DW_AT_type: encoding_form = form_value; break;
Greg Clayton261ac3f2015-08-28 01:01:03 +0000929 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
930 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Claytone6b36cd2015-12-08 01:02:08 +0000931 case DW_AT_declaration: is_forward_declaration = form_value.Boolean(); break;
Greg Clayton261ac3f2015-08-28 01:01:03 +0000932 case DW_AT_allocated:
933 case DW_AT_associated:
934 case DW_AT_bit_stride:
935 case DW_AT_byte_stride:
936 case DW_AT_data_location:
937 case DW_AT_description:
938 case DW_AT_start_scope:
939 case DW_AT_visibility:
940 case DW_AT_specification:
941 case DW_AT_abstract_origin:
942 case DW_AT_sibling:
943 break;
944 }
945 }
946 }
947
Greg Claytone6b36cd2015-12-08 01:02:08 +0000948 if (is_forward_declaration)
949 {
950 type_sp = ParseTypeFromDWO(die, log);
951 if (type_sp)
952 return type_sp;
953
954 DWARFDeclContext die_decl_ctx;
955 die.GetDWARFDeclContext(die_decl_ctx);
956
957 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
958
959 if (!type_sp)
960 {
961 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
962 if (debug_map_symfile)
963 {
964 // We weren't able to find a full declaration in
965 // this DWARF, see if we have a declaration anywhere
966 // else...
967 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
968 }
969 }
970
971 if (type_sp)
972 {
973 if (log)
974 {
975 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
976 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
977 static_cast<void*>(this),
978 die.GetOffset(),
979 DW_TAG_value_to_name(tag),
980 type_name_cstr,
981 type_sp->GetID());
982 }
983
984 // We found a real definition for this type elsewhere
985 // so lets use it and cache the fact that we found
986 // a complete type for this die
987 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
988 clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(
989 dwarf->DebugInfo()->GetDIE(DIERef(type_sp->GetID())));
990 if (defn_decl_ctx)
991 LinkDeclContextToDIE(defn_decl_ctx, die);
992 return type_sp;
993 }
994
995 }
Greg Clayton261ac3f2015-08-28 01:01:03 +0000996 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr);
997
998 CompilerType enumerator_clang_type;
Tamas Berghammereb882fc2015-09-09 10:20:48 +0000999 clang_type.SetCompilerType (&m_ast, dwarf->GetForwardDeclDieToClangType().lookup (die.GetDIE()));
Greg Clayton261ac3f2015-08-28 01:01:03 +00001000 if (!clang_type)
1001 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001002 if (encoding_form.IsValid())
Greg Clayton261ac3f2015-08-28 01:01:03 +00001003 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001004 Type *enumerator_type = dwarf->ResolveTypeUID(DIERef(encoding_form).GetUID());
Greg Clayton261ac3f2015-08-28 01:01:03 +00001005 if (enumerator_type)
1006 enumerator_clang_type = enumerator_type->GetFullCompilerType ();
1007 }
1008
1009 if (!enumerator_clang_type)
1010 enumerator_clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
1011 DW_ATE_signed,
1012 byte_size * 8);
1013
1014 clang_type = m_ast.CreateEnumerationType (type_name_cstr,
1015 GetClangDeclContextContainingDIE (die, nullptr),
1016 decl,
1017 enumerator_clang_type);
1018 }
1019 else
1020 {
1021 enumerator_clang_type = m_ast.GetEnumerationIntegerType (clang_type.GetOpaqueQualType());
1022 }
1023
1024 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
1025
1026 type_sp.reset( new Type (die.GetID(),
1027 dwarf,
1028 type_name_const_str,
1029 byte_size,
1030 NULL,
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001031 DIERef(encoding_form).GetUID(),
Greg Clayton261ac3f2015-08-28 01:01:03 +00001032 Type::eEncodingIsUID,
1033 &decl,
1034 clang_type,
1035 Type::eResolveStateForward));
1036
1037 ClangASTContext::StartTagDeclarationDefinition (clang_type);
1038 if (die.HasChildren())
1039 {
1040 SymbolContext cu_sc(die.GetLLDBCompileUnit());
1041 bool is_signed = false;
1042 enumerator_clang_type.IsIntegerType(is_signed);
1043 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), die);
1044 }
1045 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
1046 }
1047 }
1048 break;
1049
1050 case DW_TAG_inlined_subroutine:
1051 case DW_TAG_subprogram:
1052 case DW_TAG_subroutine_type:
1053 {
1054 // Set a bit that lets us know that we are currently parsing this
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001055 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001056
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001057 DWARFFormValue type_die_form;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001058 bool is_variadic = false;
1059 bool is_inline = false;
1060 bool is_static = false;
1061 bool is_virtual = false;
1062 bool is_explicit = false;
1063 bool is_artificial = false;
Greg Claytonfb85e622016-02-09 22:36:24 +00001064 bool has_template_params = false;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001065 DWARFFormValue specification_die_form;
1066 DWARFFormValue abstract_origin_die_form;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001067 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
1068
1069 unsigned type_quals = 0;
1070 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
1071
1072
1073 const size_t num_attributes = die.GetAttributes (attributes);
1074 if (num_attributes > 0)
1075 {
1076 uint32_t i;
1077 for (i=0; i<num_attributes; ++i)
1078 {
1079 attr = attributes.AttributeAtIndex(i);
1080 if (attributes.ExtractFormValueAtIndex(i, form_value))
1081 {
1082 switch (attr)
1083 {
1084 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1085 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1086 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1087 case DW_AT_name:
1088 type_name_cstr = form_value.AsCString();
1089 type_name_const_str.SetCString(type_name_cstr);
1090 break;
1091
1092 case DW_AT_linkage_name:
1093 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&dwarf->get_debug_str_data()); break;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001094 case DW_AT_type: type_die_form = form_value; break;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001095 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
1096 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
1097 case DW_AT_inline: is_inline = form_value.Boolean(); break;
1098 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break;
1099 case DW_AT_explicit: is_explicit = form_value.Boolean(); break;
1100 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
1101
1102
1103 case DW_AT_external:
1104 if (form_value.Unsigned())
1105 {
1106 if (storage == clang::SC_None)
1107 storage = clang::SC_Extern;
1108 else
1109 storage = clang::SC_PrivateExtern;
1110 }
1111 break;
1112
1113 case DW_AT_specification:
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001114 specification_die_form = form_value;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001115 break;
1116
1117 case DW_AT_abstract_origin:
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001118 abstract_origin_die_form = form_value;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001119 break;
1120
1121 case DW_AT_object_pointer:
1122 object_pointer_die_offset = form_value.Reference();
1123 break;
1124
1125 case DW_AT_allocated:
1126 case DW_AT_associated:
1127 case DW_AT_address_class:
1128 case DW_AT_calling_convention:
1129 case DW_AT_data_location:
1130 case DW_AT_elemental:
1131 case DW_AT_entry_pc:
1132 case DW_AT_frame_base:
1133 case DW_AT_high_pc:
1134 case DW_AT_low_pc:
1135 case DW_AT_prototyped:
1136 case DW_AT_pure:
1137 case DW_AT_ranges:
1138 case DW_AT_recursive:
1139 case DW_AT_return_addr:
1140 case DW_AT_segment:
1141 case DW_AT_start_scope:
1142 case DW_AT_static_link:
1143 case DW_AT_trampoline:
1144 case DW_AT_visibility:
1145 case DW_AT_vtable_elem_location:
1146 case DW_AT_description:
1147 case DW_AT_sibling:
1148 break;
1149 }
1150 }
1151 }
1152 }
1153
1154 std::string object_pointer_name;
1155 if (object_pointer_die_offset != DW_INVALID_OFFSET)
1156 {
1157 DWARFDIE object_pointer_die = die.GetDIE (object_pointer_die_offset);
1158 if (object_pointer_die)
1159 {
1160 const char *object_pointer_name_cstr = object_pointer_die.GetName();
1161 if (object_pointer_name_cstr)
1162 object_pointer_name = object_pointer_name_cstr;
1163 }
1164 }
1165
1166 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr);
1167
1168 CompilerType return_clang_type;
1169 Type *func_type = NULL;
1170
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001171 if (type_die_form.IsValid())
1172 func_type = dwarf->ResolveTypeUID(DIERef(type_die_form).GetUID());
Greg Clayton261ac3f2015-08-28 01:01:03 +00001173
1174 if (func_type)
1175 return_clang_type = func_type->GetForwardCompilerType ();
1176 else
1177 return_clang_type = m_ast.GetBasicType(eBasicTypeVoid);
1178
1179
1180 std::vector<CompilerType> function_param_types;
1181 std::vector<clang::ParmVarDecl*> function_param_decls;
1182
1183 // Parse the function children for the parameters
1184
1185 DWARFDIE decl_ctx_die;
1186 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (die, &decl_ctx_die);
1187 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
1188
Greg Claytonfb85e622016-02-09 22:36:24 +00001189 bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton261ac3f2015-08-28 01:01:03 +00001190 // Start off static. This will be set to false in ParseChildParameters(...)
1191 // if we find a "this" parameters as the first parameter
1192 if (is_cxx_method)
Greg Claytonfb85e622016-02-09 22:36:24 +00001193 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001194 is_static = true;
Greg Claytonfb85e622016-02-09 22:36:24 +00001195 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00001196
1197 if (die.HasChildren())
1198 {
1199 bool skip_artificial = true;
1200 ParseChildParameters (sc,
1201 containing_decl_ctx,
1202 die,
1203 skip_artificial,
1204 is_static,
1205 is_variadic,
Greg Claytonfb85e622016-02-09 22:36:24 +00001206 has_template_params,
Greg Clayton261ac3f2015-08-28 01:01:03 +00001207 function_param_types,
1208 function_param_decls,
1209 type_quals);
1210 }
1211
Greg Claytonfb85e622016-02-09 22:36:24 +00001212 bool ignore_containing_context = false;
1213 // Check for templatized class member functions. If we had any DW_TAG_template_type_parameter
1214 // or DW_TAG_template_value_parameter the DW_TAG_subprogram DIE, then we can't let this become
1215 // a method in a class. Why? Because templatized functions are only emitted if one of the
1216 // templatized methods is used in the current compile unit and we will end up with classes
1217 // that may or may not include these member functions and this means one class won't match another
1218 // class definition and it affects our ability to use a class in the clang expression parser. So
1219 // for the greater good, we currently must not allow any template member functions in a class definition.
Greg Clayton343f8982016-02-09 23:25:54 +00001220 if (is_cxx_method && has_template_params)
1221 {
1222 ignore_containing_context = true;
1223 is_cxx_method = false;
1224 }
Greg Claytonfb85e622016-02-09 22:36:24 +00001225
Greg Clayton261ac3f2015-08-28 01:01:03 +00001226 // clang_type will get the function prototype clang type after this call
1227 clang_type = m_ast.CreateFunctionType (return_clang_type,
1228 function_param_types.data(),
1229 function_param_types.size(),
1230 is_variadic,
1231 type_quals);
1232
Greg Clayton261ac3f2015-08-28 01:01:03 +00001233
1234 if (type_name_cstr)
1235 {
1236 bool type_handled = false;
1237 if (tag == DW_TAG_subprogram ||
1238 tag == DW_TAG_inlined_subroutine)
1239 {
Jim Inghamaa816b82015-09-02 01:59:14 +00001240 ObjCLanguage::MethodName objc_method (type_name_cstr, true);
Greg Clayton261ac3f2015-08-28 01:01:03 +00001241 if (objc_method.IsValid(true))
1242 {
1243 CompilerType class_opaque_type;
1244 ConstString class_name(objc_method.GetClassName());
1245 if (class_name)
1246 {
1247 TypeSP complete_objc_class_type_sp (dwarf->FindCompleteObjCDefinitionTypeForDIE (DWARFDIE(), class_name, false));
1248
1249 if (complete_objc_class_type_sp)
1250 {
1251 CompilerType type_clang_forward_type = complete_objc_class_type_sp->GetForwardCompilerType ();
1252 if (ClangASTContext::IsObjCObjectOrInterfaceType(type_clang_forward_type))
1253 class_opaque_type = type_clang_forward_type;
1254 }
1255 }
1256
1257 if (class_opaque_type)
1258 {
1259 // If accessibility isn't set to anything valid, assume public for
1260 // now...
1261 if (accessibility == eAccessNone)
1262 accessibility = eAccessPublic;
1263
1264 clang::ObjCMethodDecl *objc_method_decl = m_ast.AddMethodToObjCObjectType (class_opaque_type,
1265 type_name_cstr,
1266 clang_type,
1267 accessibility,
1268 is_artificial);
1269 type_handled = objc_method_decl != NULL;
1270 if (type_handled)
1271 {
1272 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
1273 m_ast.SetMetadataAsUserID (objc_method_decl, die.GetID());
1274 }
1275 else
1276 {
1277 dwarf->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",
1278 die.GetOffset(),
1279 tag,
1280 DW_TAG_value_to_name(tag));
1281 }
1282 }
1283 }
1284 else if (is_cxx_method)
1285 {
1286 // Look at the parent of this DIE and see if is is
1287 // a class or struct and see if this is actually a
1288 // C++ method
1289 Type *class_type = dwarf->ResolveType (decl_ctx_die);
1290 if (class_type)
1291 {
Siva Chandra27e33a82015-10-07 22:11:52 +00001292 bool alternate_defn = false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00001293 if (class_type->GetID() != decl_ctx_die.GetID() || decl_ctx_die.GetContainingDWOModuleDIE())
Greg Clayton261ac3f2015-08-28 01:01:03 +00001294 {
Siva Chandra27e33a82015-10-07 22:11:52 +00001295 alternate_defn = true;
1296
Greg Clayton261ac3f2015-08-28 01:01:03 +00001297 // We uniqued the parent class of this function to another class
1298 // so we now need to associate all dies under "decl_ctx_die" to
1299 // DIEs in the DIE for "class_type"...
1300 SymbolFileDWARF *class_symfile = NULL;
1301 DWARFDIE class_type_die;
1302
1303 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
1304 if (debug_map_symfile)
1305 {
1306 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID()));
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001307 class_type_die = class_symfile->DebugInfo()->GetDIE (DIERef(class_type->GetID()));
Greg Clayton261ac3f2015-08-28 01:01:03 +00001308 }
1309 else
1310 {
1311 class_symfile = dwarf;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001312 class_type_die = dwarf->DebugInfo()->GetDIE (DIERef(class_type->GetID()));
Greg Clayton261ac3f2015-08-28 01:01:03 +00001313 }
1314 if (class_type_die)
1315 {
1316 DWARFDIECollection failures;
1317
1318 CopyUniqueClassMethodTypes (decl_ctx_die,
1319 class_type_die,
1320 class_type,
1321 failures);
1322
1323 // FIXME do something with these failures that's smarter than
1324 // just dropping them on the ground. Unfortunately classes don't
1325 // like having stuff added to them after their definitions are
1326 // complete...
1327
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001328 type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
Greg Clayton261ac3f2015-08-28 01:01:03 +00001329 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
1330 {
1331 type_sp = type_ptr->shared_from_this();
1332 break;
1333 }
1334 }
1335 }
1336
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001337 if (specification_die_form.IsValid())
Greg Clayton261ac3f2015-08-28 01:01:03 +00001338 {
1339 // We have a specification which we are going to base our function
1340 // prototype off of, so we need this type to be completed so that the
1341 // m_die_to_decl_ctx for the method in the specification has a valid
1342 // clang decl context.
1343 class_type->GetForwardCompilerType ();
1344 // If we have a specification, then the function type should have been
1345 // made with the specification and not with this die.
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001346 DWARFDIE spec_die = dwarf->DebugInfo()->GetDIE(DIERef(specification_die_form));
Greg Clayton261ac3f2015-08-28 01:01:03 +00001347 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (spec_die);
1348 if (spec_clang_decl_ctx)
1349 {
1350 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
1351 }
1352 else
1353 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001354 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8" PRIx64 ") has no decl\n",
Greg Clayton261ac3f2015-08-28 01:01:03 +00001355 die.GetID(),
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001356 specification_die_form.Reference());
Greg Clayton261ac3f2015-08-28 01:01:03 +00001357 }
1358 type_handled = true;
1359 }
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001360 else if (abstract_origin_die_form.IsValid())
Greg Clayton261ac3f2015-08-28 01:01:03 +00001361 {
1362 // We have a specification which we are going to base our function
1363 // prototype off of, so we need this type to be completed so that the
1364 // m_die_to_decl_ctx for the method in the abstract origin has a valid
1365 // clang decl context.
1366 class_type->GetForwardCompilerType ();
1367
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001368 DWARFDIE abs_die = dwarf->DebugInfo()->GetDIE (DIERef(abstract_origin_die_form));
Greg Clayton261ac3f2015-08-28 01:01:03 +00001369 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (abs_die);
1370 if (abs_clang_decl_ctx)
1371 {
1372 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
1373 }
1374 else
1375 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001376 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8" PRIx64 ") has no decl\n",
Greg Clayton261ac3f2015-08-28 01:01:03 +00001377 die.GetID(),
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001378 abstract_origin_die_form.Reference());
Greg Clayton261ac3f2015-08-28 01:01:03 +00001379 }
1380 type_handled = true;
1381 }
1382 else
1383 {
1384 CompilerType class_opaque_type = class_type->GetForwardCompilerType ();
1385 if (ClangASTContext::IsCXXClassType(class_opaque_type))
1386 {
Siva Chandra27e33a82015-10-07 22:11:52 +00001387 if (class_opaque_type.IsBeingDefined () || alternate_defn)
Greg Clayton261ac3f2015-08-28 01:01:03 +00001388 {
Greg Clayton261ac3f2015-08-28 01:01:03 +00001389 if (!is_static && !die.HasChildren())
1390 {
1391 // We have a C++ member function with no children (this pointer!)
1392 // and clang will get mad if we try and make a function that isn't
1393 // well formed in the DWARF, so we will just skip it...
1394 type_handled = true;
1395 }
1396 else
1397 {
Siva Chandra27e33a82015-10-07 22:11:52 +00001398 bool add_method = true;
1399 if (alternate_defn)
Greg Clayton261ac3f2015-08-28 01:01:03 +00001400 {
Siva Chandra27e33a82015-10-07 22:11:52 +00001401 // If an alternate definition for the class exists, then add the method only if an
1402 // equivalent is not already present.
1403 clang::CXXRecordDecl *record_decl = m_ast.GetAsCXXRecordDecl(class_opaque_type.GetOpaqueQualType());
1404 if (record_decl)
Greg Clayton261ac3f2015-08-28 01:01:03 +00001405 {
Siva Chandra27e33a82015-10-07 22:11:52 +00001406 for (auto method_iter = record_decl->method_begin();
1407 method_iter != record_decl->method_end();
1408 method_iter++)
1409 {
1410 clang::CXXMethodDecl *method_decl = *method_iter;
1411 if (method_decl->getNameInfo().getAsString() == std::string(type_name_cstr))
1412 {
1413 if (method_decl->getType() == ClangASTContext::GetQualType(clang_type))
1414 {
1415 add_method = false;
1416 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(method_decl), die);
1417 type_handled = true;
1418
1419 break;
1420 }
1421 }
1422 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00001423 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00001424 }
Siva Chandra27e33a82015-10-07 22:11:52 +00001425
1426 if (add_method)
Greg Clayton261ac3f2015-08-28 01:01:03 +00001427 {
Siva Chandra27e33a82015-10-07 22:11:52 +00001428 // REMOVE THE CRASH DESCRIPTION BELOW
1429 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s",
1430 type_name_cstr,
1431 class_type->GetName().GetCString(),
1432 die.GetID(),
1433 dwarf->GetObjectFile()->GetFileSpec().GetPath().c_str());
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001434
Siva Chandra27e33a82015-10-07 22:11:52 +00001435 const bool is_attr_used = false;
1436 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
1437 // in the DWARF for C++ methods... Default to public for now...
1438 if (accessibility == eAccessNone)
1439 accessibility = eAccessPublic;
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001440
Siva Chandra27e33a82015-10-07 22:11:52 +00001441 clang::CXXMethodDecl *cxx_method_decl;
1442 cxx_method_decl = m_ast.AddMethodToCXXRecordType (class_opaque_type.GetOpaqueQualType(),
1443 type_name_cstr,
1444 clang_type,
1445 accessibility,
1446 is_virtual,
1447 is_static,
1448 is_inline,
1449 is_explicit,
1450 is_attr_used,
1451 is_artificial);
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001452
Siva Chandra27e33a82015-10-07 22:11:52 +00001453 type_handled = cxx_method_decl != NULL;
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001454
Siva Chandra27e33a82015-10-07 22:11:52 +00001455 if (type_handled)
1456 {
1457 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001458
Siva Chandra27e33a82015-10-07 22:11:52 +00001459 Host::SetCrashDescription (NULL);
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001460
Siva Chandra27e33a82015-10-07 22:11:52 +00001461 ClangASTMetadata metadata;
1462 metadata.SetUserID(die.GetID());
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001463
Siva Chandra27e33a82015-10-07 22:11:52 +00001464 if (!object_pointer_name.empty())
1465 {
1466 metadata.SetObjectPtrName(object_pointer_name.c_str());
1467 if (log)
1468 log->Printf ("Setting object pointer name: %s on method object %p.\n",
1469 object_pointer_name.c_str(),
1470 static_cast<void*>(cxx_method_decl));
1471 }
1472 m_ast.SetMetadata (cxx_method_decl, metadata);
1473 }
1474 else
1475 {
1476 ignore_containing_context = true;
1477 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00001478 }
1479 }
1480 }
1481 else
1482 {
1483 // We were asked to parse the type for a method in a class, yet the
1484 // class hasn't been asked to complete itself through the
1485 // clang::ExternalASTSource protocol, so we need to just have the
1486 // class complete itself and do things the right way, then our
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001487 // DIE should then have an entry in the dwarf->GetDIEToType() map. First
1488 // we need to modify the dwarf->GetDIEToType() so it doesn't think we are
Greg Clayton261ac3f2015-08-28 01:01:03 +00001489 // trying to parse this DIE anymore...
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001490 dwarf->GetDIEToType()[die.GetDIE()] = NULL;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001491
1492 // Now we get the full type to force our class type to complete itself
1493 // using the clang::ExternalASTSource protocol which will parse all
1494 // base classes and all methods (including the method for this DIE).
1495 class_type->GetFullCompilerType ();
1496
1497 // The type for this DIE should have been filled in the function call above
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001498 type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
Greg Clayton261ac3f2015-08-28 01:01:03 +00001499 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
1500 {
1501 type_sp = type_ptr->shared_from_this();
1502 break;
1503 }
1504
1505 // FIXME This is fixing some even uglier behavior but we really need to
1506 // uniq the methods of each class as well as the class itself.
1507 // <rdar://problem/11240464>
1508 type_handled = true;
1509 }
1510 }
1511 }
1512 }
1513 }
1514 }
1515
1516 if (!type_handled)
1517 {
1518 // We just have a function that isn't part of a class
1519 clang::FunctionDecl *function_decl = m_ast.CreateFunctionDeclaration (ignore_containing_context ? m_ast.GetTranslationUnitDecl() : containing_decl_ctx,
1520 type_name_cstr,
1521 clang_type,
1522 storage,
1523 is_inline);
1524
1525 // if (template_param_infos.GetSize() > 0)
1526 // {
1527 // clang::FunctionTemplateDecl *func_template_decl = CreateFunctionTemplateDecl (containing_decl_ctx,
1528 // function_decl,
1529 // type_name_cstr,
1530 // template_param_infos);
1531 //
1532 // CreateFunctionTemplateSpecializationInfo (function_decl,
1533 // func_template_decl,
1534 // template_param_infos);
1535 // }
1536 // Add the decl to our DIE to decl context map
1537 assert (function_decl);
1538 LinkDeclContextToDIE(function_decl, die);
1539 if (!function_param_decls.empty())
1540 m_ast.SetFunctionParameters (function_decl,
1541 &function_param_decls.front(),
1542 function_param_decls.size());
1543
1544 ClangASTMetadata metadata;
1545 metadata.SetUserID(die.GetID());
1546
1547 if (!object_pointer_name.empty())
1548 {
1549 metadata.SetObjectPtrName(object_pointer_name.c_str());
1550 if (log)
1551 log->Printf ("Setting object pointer name: %s on function object %p.",
1552 object_pointer_name.c_str(),
1553 static_cast<void*>(function_decl));
1554 }
1555 m_ast.SetMetadata (function_decl, metadata);
1556 }
1557 }
1558 type_sp.reset( new Type (die.GetID(),
1559 dwarf,
1560 type_name_const_str,
1561 0,
1562 NULL,
1563 LLDB_INVALID_UID,
1564 Type::eEncodingIsUID,
1565 &decl,
1566 clang_type,
1567 Type::eResolveStateFull));
1568 assert(type_sp.get());
1569 }
1570 break;
1571
1572 case DW_TAG_array_type:
1573 {
1574 // Set a bit that lets us know that we are currently parsing this
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001575 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001576
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001577 DWARFFormValue type_die_form;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001578 int64_t first_index = 0;
1579 uint32_t byte_stride = 0;
1580 uint32_t bit_stride = 0;
1581 bool is_vector = false;
1582 const size_t num_attributes = die.GetAttributes (attributes);
1583
1584 if (num_attributes > 0)
1585 {
1586 uint32_t i;
1587 for (i=0; i<num_attributes; ++i)
1588 {
1589 attr = attributes.AttributeAtIndex(i);
1590 if (attributes.ExtractFormValueAtIndex(i, form_value))
1591 {
1592 switch (attr)
1593 {
1594 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1595 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1596 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1597 case DW_AT_name:
1598 type_name_cstr = form_value.AsCString();
1599 type_name_const_str.SetCString(type_name_cstr);
1600 break;
1601
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001602 case DW_AT_type: type_die_form = form_value; break;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001603 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
1604 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
1605 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
1606 case DW_AT_GNU_vector: is_vector = form_value.Boolean(); break;
1607 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
1608 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
1609 case DW_AT_allocated:
1610 case DW_AT_associated:
1611 case DW_AT_data_location:
1612 case DW_AT_description:
1613 case DW_AT_ordering:
1614 case DW_AT_start_scope:
1615 case DW_AT_visibility:
1616 case DW_AT_specification:
1617 case DW_AT_abstract_origin:
1618 case DW_AT_sibling:
1619 break;
1620 }
1621 }
1622 }
1623
1624 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr);
1625
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001626 Type *element_type = dwarf->ResolveTypeUID(DIERef(type_die_form).GetUID());
Greg Clayton261ac3f2015-08-28 01:01:03 +00001627
1628 if (element_type)
1629 {
1630 std::vector<uint64_t> element_orders;
1631 ParseChildArrayInfo(sc, die, first_index, element_orders, byte_stride, bit_stride);
1632 if (byte_stride == 0 && bit_stride == 0)
1633 byte_stride = element_type->GetByteSize();
1634 CompilerType array_element_type = element_type->GetForwardCompilerType ();
1635 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
1636 if (element_orders.size() > 0)
1637 {
1638 uint64_t num_elements = 0;
1639 std::vector<uint64_t>::const_reverse_iterator pos;
1640 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
1641 for (pos = element_orders.rbegin(); pos != end; ++pos)
1642 {
1643 num_elements = *pos;
1644 clang_type = m_ast.CreateArrayType (array_element_type,
1645 num_elements,
1646 is_vector);
1647 array_element_type = clang_type;
1648 array_element_bit_stride = num_elements ?
1649 array_element_bit_stride * num_elements :
1650 array_element_bit_stride;
1651 }
1652 }
1653 else
1654 {
1655 clang_type = m_ast.CreateArrayType (array_element_type, 0, is_vector);
1656 }
1657 ConstString empty_name;
1658 type_sp.reset( new Type (die.GetID(),
1659 dwarf,
1660 empty_name,
1661 array_element_bit_stride / 8,
1662 NULL,
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001663 DIERef(type_die_form).GetUID(),
Greg Clayton261ac3f2015-08-28 01:01:03 +00001664 Type::eEncodingIsUID,
1665 &decl,
1666 clang_type,
1667 Type::eResolveStateFull));
1668 type_sp->SetEncodingType (element_type);
1669 }
1670 }
1671 }
1672 break;
1673
1674 case DW_TAG_ptr_to_member_type:
1675 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001676 DWARFFormValue type_die_form;
1677 DWARFFormValue containing_type_die_form;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001678
1679 const size_t num_attributes = die.GetAttributes (attributes);
1680
1681 if (num_attributes > 0) {
1682 uint32_t i;
1683 for (i=0; i<num_attributes; ++i)
1684 {
1685 attr = attributes.AttributeAtIndex(i);
1686 if (attributes.ExtractFormValueAtIndex(i, form_value))
1687 {
1688 switch (attr)
1689 {
1690 case DW_AT_type:
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001691 type_die_form = form_value; break;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001692 case DW_AT_containing_type:
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001693 containing_type_die_form = form_value; break;
Greg Clayton261ac3f2015-08-28 01:01:03 +00001694 }
1695 }
1696 }
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001697
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001698 Type *pointee_type = dwarf->ResolveTypeUID(DIERef(type_die_form).GetUID());
1699 Type *class_type = dwarf->ResolveTypeUID(DIERef(containing_type_die_form).GetUID());
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001700
Greg Clayton261ac3f2015-08-28 01:01:03 +00001701 CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType ();
1702 CompilerType class_clang_type = class_type->GetLayoutCompilerType ();
1703
Tamas Berghammerc9eb2fc2015-10-26 18:10:55 +00001704 clang_type = ClangASTContext::CreateMemberPointerType(class_clang_type, pointee_clang_type);
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001705
Greg Clayton261ac3f2015-08-28 01:01:03 +00001706 byte_size = clang_type.GetByteSize(nullptr);
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001707
1708 type_sp.reset(new Type(die.GetID(), dwarf, type_name_const_str, byte_size, NULL,
1709 LLDB_INVALID_UID, Type::eEncodingIsUID, NULL, clang_type,
1710 Type::eResolveStateForward));
Greg Clayton261ac3f2015-08-28 01:01:03 +00001711 }
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001712
Greg Clayton261ac3f2015-08-28 01:01:03 +00001713 break;
1714 }
1715 default:
1716 dwarf->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",
1717 die.GetOffset(),
1718 tag,
1719 DW_TAG_value_to_name(tag));
1720 break;
1721 }
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001722
Greg Clayton261ac3f2015-08-28 01:01:03 +00001723 if (type_sp.get())
1724 {
1725 DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
1726 dw_tag_t sc_parent_tag = sc_parent_die.Tag();
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001727
Greg Clayton261ac3f2015-08-28 01:01:03 +00001728 SymbolContextScope * symbol_context_scope = NULL;
1729 if (sc_parent_tag == DW_TAG_compile_unit)
1730 {
1731 symbol_context_scope = sc.comp_unit;
1732 }
1733 else if (sc.function != NULL && sc_parent_die)
1734 {
1735 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
1736 if (symbol_context_scope == NULL)
1737 symbol_context_scope = sc.function;
1738 }
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001739
Greg Clayton261ac3f2015-08-28 01:01:03 +00001740 if (symbol_context_scope != NULL)
1741 {
1742 type_sp->SetSymbolContextScope(symbol_context_scope);
1743 }
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001744
Greg Clayton261ac3f2015-08-28 01:01:03 +00001745 // We are ready to put this type into the uniqued list up at the module level
1746 type_list->Insert (type_sp);
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00001747
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001748 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
Greg Clayton261ac3f2015-08-28 01:01:03 +00001749 }
1750 }
1751 else if (type_ptr != DIE_IS_BEING_PARSED)
1752 {
1753 type_sp = type_ptr->shared_from_this();
1754 }
1755 }
1756 return type_sp;
1757}
1758
1759// DWARF parsing functions
1760
1761class DWARFASTParserClang::DelayedAddObjCClassProperty
1762{
1763public:
1764 DelayedAddObjCClassProperty(const CompilerType &class_opaque_type,
1765 const char *property_name,
1766 const CompilerType &property_opaque_type, // The property type is only required if you don't have an ivar decl
1767 clang::ObjCIvarDecl *ivar_decl,
1768 const char *property_setter_name,
1769 const char *property_getter_name,
1770 uint32_t property_attributes,
1771 const ClangASTMetadata *metadata) :
1772 m_class_opaque_type (class_opaque_type),
1773 m_property_name (property_name),
1774 m_property_opaque_type (property_opaque_type),
1775 m_ivar_decl (ivar_decl),
1776 m_property_setter_name (property_setter_name),
1777 m_property_getter_name (property_getter_name),
1778 m_property_attributes (property_attributes)
1779 {
1780 if (metadata != NULL)
1781 {
1782 m_metadata_ap.reset(new ClangASTMetadata());
1783 *m_metadata_ap = *metadata;
1784 }
1785 }
1786
1787 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
1788 {
1789 *this = rhs;
1790 }
1791
1792 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs)
1793 {
1794 m_class_opaque_type = rhs.m_class_opaque_type;
1795 m_property_name = rhs.m_property_name;
1796 m_property_opaque_type = rhs.m_property_opaque_type;
1797 m_ivar_decl = rhs.m_ivar_decl;
1798 m_property_setter_name = rhs.m_property_setter_name;
1799 m_property_getter_name = rhs.m_property_getter_name;
1800 m_property_attributes = rhs.m_property_attributes;
1801
1802 if (rhs.m_metadata_ap.get())
1803 {
1804 m_metadata_ap.reset (new ClangASTMetadata());
1805 *m_metadata_ap = *rhs.m_metadata_ap;
1806 }
1807 return *this;
1808 }
1809
1810 bool
1811 Finalize()
1812 {
Zachary Turner89a79382015-10-02 22:47:14 +00001813 return ClangASTContext::AddObjCClassProperty (m_class_opaque_type,
1814 m_property_name,
1815 m_property_opaque_type,
1816 m_ivar_decl,
1817 m_property_setter_name,
1818 m_property_getter_name,
1819 m_property_attributes,
1820 m_metadata_ap.get());
Greg Clayton261ac3f2015-08-28 01:01:03 +00001821 }
1822
1823private:
1824 CompilerType m_class_opaque_type;
1825 const char *m_property_name;
1826 CompilerType m_property_opaque_type;
1827 clang::ObjCIvarDecl *m_ivar_decl;
1828 const char *m_property_setter_name;
1829 const char *m_property_getter_name;
1830 uint32_t m_property_attributes;
1831 std::unique_ptr<ClangASTMetadata> m_metadata_ap;
1832};
1833
1834bool
1835DWARFASTParserClang::ParseTemplateDIE (const DWARFDIE &die,
1836 ClangASTContext::TemplateParameterInfos &template_param_infos)
1837{
1838 const dw_tag_t tag = die.Tag();
1839
1840 switch (tag)
1841 {
1842 case DW_TAG_template_type_parameter:
1843 case DW_TAG_template_value_parameter:
1844 {
1845 DWARFAttributes attributes;
1846 const size_t num_attributes = die.GetAttributes (attributes);
1847 const char *name = NULL;
1848 Type *lldb_type = NULL;
1849 CompilerType clang_type;
1850 uint64_t uval64 = 0;
1851 bool uval64_valid = false;
1852 if (num_attributes > 0)
1853 {
1854 DWARFFormValue form_value;
1855 for (size_t i=0; i<num_attributes; ++i)
1856 {
1857 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1858
1859 switch (attr)
1860 {
1861 case DW_AT_name:
1862 if (attributes.ExtractFormValueAtIndex(i, form_value))
1863 name = form_value.AsCString();
1864 break;
1865
1866 case DW_AT_type:
1867 if (attributes.ExtractFormValueAtIndex(i, form_value))
1868 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00001869 lldb_type = die.ResolveTypeUID(DIERef(form_value).GetUID());
Greg Clayton261ac3f2015-08-28 01:01:03 +00001870 if (lldb_type)
1871 clang_type = lldb_type->GetForwardCompilerType ();
1872 }
1873 break;
1874
1875 case DW_AT_const_value:
1876 if (attributes.ExtractFormValueAtIndex(i, form_value))
1877 {
1878 uval64_valid = true;
1879 uval64 = form_value.Unsigned();
1880 }
1881 break;
1882 default:
1883 break;
1884 }
1885 }
1886
1887 clang::ASTContext *ast = m_ast.getASTContext();
1888 if (!clang_type)
1889 clang_type = m_ast.GetBasicType(eBasicTypeVoid);
1890
1891 if (clang_type)
1892 {
1893 bool is_signed = false;
1894 if (name && name[0])
1895 template_param_infos.names.push_back(name);
1896 else
1897 template_param_infos.names.push_back(NULL);
1898
1899 if (tag == DW_TAG_template_value_parameter &&
1900 lldb_type != NULL &&
1901 clang_type.IsIntegerType (is_signed) &&
1902 uval64_valid)
1903 {
1904 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1905 template_param_infos.args.push_back (clang::TemplateArgument (*ast,
1906 llvm::APSInt(apint),
1907 ClangASTContext::GetQualType(clang_type)));
1908 }
1909 else
1910 {
1911 template_param_infos.args.push_back (clang::TemplateArgument (ClangASTContext::GetQualType(clang_type)));
1912 }
1913 }
1914 else
1915 {
1916 return false;
1917 }
1918
1919 }
1920 }
1921 return true;
1922
1923 default:
1924 break;
1925 }
1926 return false;
1927}
1928
1929bool
1930DWARFASTParserClang::ParseTemplateParameterInfos (const DWARFDIE &parent_die,
1931 ClangASTContext::TemplateParameterInfos &template_param_infos)
1932{
1933
1934 if (!parent_die)
1935 return false;
1936
1937 Args template_parameter_names;
1938 for (DWARFDIE die = parent_die.GetFirstChild();
1939 die.IsValid();
1940 die = die.GetSibling())
1941 {
1942 const dw_tag_t tag = die.Tag();
1943
1944 switch (tag)
1945 {
1946 case DW_TAG_template_type_parameter:
1947 case DW_TAG_template_value_parameter:
1948 ParseTemplateDIE (die, template_param_infos);
1949 break;
1950
1951 default:
1952 break;
1953 }
1954 }
1955 if (template_param_infos.args.empty())
1956 return false;
1957 return template_param_infos.args.size() == template_param_infos.names.size();
1958}
1959
1960bool
Greg Claytone6b36cd2015-12-08 01:02:08 +00001961DWARFASTParserClang::CanCompleteType (const lldb_private::CompilerType &compiler_type)
1962{
1963 if (m_clang_ast_importer_ap)
1964 return ClangASTContext::CanImport(compiler_type, GetClangASTImporter());
1965 else
1966 return false;
1967}
1968
1969bool
1970DWARFASTParserClang::CompleteType (const lldb_private::CompilerType &compiler_type)
1971{
1972 if (CanCompleteType(compiler_type))
1973 {
1974 if (ClangASTContext::Import(compiler_type, GetClangASTImporter()))
1975 {
1976 ClangASTContext::CompleteTagDeclarationDefinition(compiler_type);
1977 return true;
1978 }
1979 else
1980 {
1981 ClangASTContext::SetHasExternalStorage (compiler_type.GetOpaqueQualType(), false);
1982 }
1983 }
1984 return false;
1985}
1986
1987bool
Greg Clayton261ac3f2015-08-28 01:01:03 +00001988DWARFASTParserClang::CompleteTypeFromDWARF (const DWARFDIE &die,
1989 lldb_private::Type *type,
1990 CompilerType &clang_type)
1991{
Greg Claytonfb85e622016-02-09 22:36:24 +00001992 SymbolFileDWARF *dwarf = die.GetDWARF();
1993
1994 lldb_private::Mutex::Locker locker(dwarf->GetObjectFile()->GetModule()->GetMutex());
1995
Greg Clayton261ac3f2015-08-28 01:01:03 +00001996 // Disable external storage for this type so we don't get anymore
1997 // clang::ExternalASTSource queries for this type.
1998 m_ast.SetHasExternalStorage (clang_type.GetOpaqueQualType(), false);
1999
2000 if (!die)
2001 return false;
2002
2003 const dw_tag_t tag = die.Tag();
2004
Greg Clayton261ac3f2015-08-28 01:01:03 +00002005 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
2006 if (log)
2007 dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
2008 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
2009 die.GetID(),
2010 die.GetTagAsCString(),
2011 type->GetName().AsCString());
2012 assert (clang_type);
2013 DWARFAttributes attributes;
Greg Clayton261ac3f2015-08-28 01:01:03 +00002014 switch (tag)
2015 {
2016 case DW_TAG_structure_type:
2017 case DW_TAG_union_type:
2018 case DW_TAG_class_type:
2019 {
2020 LayoutInfo layout_info;
2021
2022 {
2023 if (die.HasChildren())
2024 {
2025 LanguageType class_language = eLanguageTypeUnknown;
2026 if (ClangASTContext::IsObjCObjectOrInterfaceType(clang_type))
2027 {
2028 class_language = eLanguageTypeObjC;
2029 // For objective C we don't start the definition when
2030 // the class is created.
2031 ClangASTContext::StartTagDeclarationDefinition (clang_type);
2032 }
2033
2034 int tag_decl_kind = -1;
2035 AccessType default_accessibility = eAccessNone;
2036 if (tag == DW_TAG_structure_type)
2037 {
2038 tag_decl_kind = clang::TTK_Struct;
2039 default_accessibility = eAccessPublic;
2040 }
2041 else if (tag == DW_TAG_union_type)
2042 {
2043 tag_decl_kind = clang::TTK_Union;
2044 default_accessibility = eAccessPublic;
2045 }
2046 else if (tag == DW_TAG_class_type)
2047 {
2048 tag_decl_kind = clang::TTK_Class;
2049 default_accessibility = eAccessPrivate;
2050 }
2051
2052 SymbolContext sc(die.GetLLDBCompileUnit());
2053 std::vector<clang::CXXBaseSpecifier *> base_classes;
2054 std::vector<int> member_accessibilities;
2055 bool is_a_class = false;
2056 // Parse members and base classes first
2057 DWARFDIECollection member_function_dies;
2058
2059 DelayedPropertyList delayed_properties;
Greg Claytone6b36cd2015-12-08 01:02:08 +00002060 ParseChildMembers (sc,
2061 die,
2062 clang_type,
2063 class_language,
2064 base_classes,
2065 member_accessibilities,
2066 member_function_dies,
2067 delayed_properties,
2068 default_accessibility,
2069 is_a_class,
2070 layout_info);
Greg Clayton261ac3f2015-08-28 01:01:03 +00002071
2072 // Now parse any methods if there were any...
2073 size_t num_functions = member_function_dies.Size();
2074 if (num_functions > 0)
2075 {
2076 for (size_t i=0; i<num_functions; ++i)
2077 {
2078 dwarf->ResolveType(member_function_dies.GetDIEAtIndex(i));
2079 }
2080 }
2081
2082 if (class_language == eLanguageTypeObjC)
2083 {
2084 ConstString class_name (clang_type.GetTypeName());
2085 if (class_name)
2086 {
2087 DIEArray method_die_offsets;
2088 dwarf->GetObjCMethodDIEOffsets(class_name, method_die_offsets);
2089
2090 if (!method_die_offsets.empty())
2091 {
2092 DWARFDebugInfo* debug_info = dwarf->DebugInfo();
2093
2094 const size_t num_matches = method_die_offsets.size();
2095 for (size_t i=0; i<num_matches; ++i)
2096 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002097 const DIERef& die_ref = method_die_offsets[i];
2098 DWARFDIE method_die = debug_info->GetDIE (die_ref);
Greg Clayton261ac3f2015-08-28 01:01:03 +00002099
2100 if (method_die)
2101 method_die.ResolveType ();
2102 }
2103 }
2104
2105 for (DelayedPropertyList::iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
2106 pi != pe;
2107 ++pi)
2108 pi->Finalize();
2109 }
2110 }
2111
2112 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2113 // need to tell the clang type it is actually a class.
2114 if (class_language != eLanguageTypeObjC)
2115 {
2116 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2117 m_ast.SetTagTypeKind (ClangASTContext::GetQualType(clang_type), clang::TTK_Class);
2118 }
2119
2120 // Since DW_TAG_structure_type gets used for both classes
2121 // and structures, we may need to set any DW_TAG_member
2122 // fields to have a "private" access if none was specified.
2123 // When we parsed the child members we tracked that actual
2124 // accessibility value for each DW_TAG_member in the
2125 // "member_accessibilities" array. If the value for the
2126 // member is zero, then it was set to the "default_accessibility"
2127 // which for structs was "public". Below we correct this
2128 // by setting any fields to "private" that weren't correctly
2129 // set.
2130 if (is_a_class && !member_accessibilities.empty())
2131 {
2132 // This is a class and all members that didn't have
2133 // their access specified are private.
2134 m_ast.SetDefaultAccessForRecordFields (m_ast.GetAsRecordDecl(clang_type),
2135 eAccessPrivate,
2136 &member_accessibilities.front(),
2137 member_accessibilities.size());
2138 }
2139
2140 if (!base_classes.empty())
2141 {
2142 // Make sure all base classes refer to complete types and not
2143 // forward declarations. If we don't do this, clang will crash
2144 // with an assertion in the call to clang_type.SetBaseClassesForClassType()
Greg Clayton261ac3f2015-08-28 01:01:03 +00002145 for (auto &base_class : base_classes)
2146 {
2147 clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo();
2148 if (type_source_info)
2149 {
2150 CompilerType base_class_type (&m_ast, type_source_info->getType().getAsOpaquePtr());
2151 if (base_class_type.GetCompleteType() == false)
2152 {
Siva Chandracebabb92015-09-23 17:47:08 +00002153 auto module = dwarf->GetObjectFile()->GetModule();
2154 module->ReportError (
2155 ":: Class '%s' has a base class '%s' which does not have a complete definition.",
2156 die.GetName(),
2157 base_class_type.GetTypeName().GetCString());
2158 if (die.GetCU()->GetProducer() == DWARFCompileUnit::eProducerClang)
2159 module->ReportError (":: Try compiling the source file with -fno-limit-debug-info.");
Greg Clayton261ac3f2015-08-28 01:01:03 +00002160
Greg Claytone6b36cd2015-12-08 01:02:08 +00002161 // We have no choice other than to pretend that the base class
2162 // is complete. If we don't do this, clang will crash when we
2163 // call setBases() inside of "clang_type.SetBaseClassesForClassType()"
2164 // below. Since we provide layout assistance, all ivars in this
2165 // class and other classes will be fine, this is the best we can do
2166 // short of crashing.
2167 ClangASTContext::StartTagDeclarationDefinition (base_class_type);
2168 ClangASTContext::CompleteTagDeclarationDefinition (base_class_type);
Greg Clayton261ac3f2015-08-28 01:01:03 +00002169 }
2170 }
2171 }
2172 m_ast.SetBaseClassesForClassType (clang_type.GetOpaqueQualType(),
2173 &base_classes.front(),
2174 base_classes.size());
2175
2176 // Clang will copy each CXXBaseSpecifier in "base_classes"
2177 // so we have to free them all.
2178 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2179 base_classes.size());
2180 }
2181 }
2182 }
2183
2184 ClangASTContext::BuildIndirectFields (clang_type);
2185 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
2186
2187 if (!layout_info.field_offsets.empty() ||
2188 !layout_info.base_offsets.empty() ||
2189 !layout_info.vbase_offsets.empty() )
2190 {
2191 if (type)
2192 layout_info.bit_size = type->GetByteSize() * 8;
2193 if (layout_info.bit_size == 0)
2194 layout_info.bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2195
2196 clang::CXXRecordDecl *record_decl = m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
2197 if (record_decl)
2198 {
2199 if (log)
2200 {
2201 ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
2202
2203 if (module_sp)
2204 {
2205 module_sp->LogMessage (log,
2206 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) caching layout info for record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])",
2207 static_cast<void*>(clang_type.GetOpaqueQualType()),
2208 static_cast<void*>(record_decl),
2209 layout_info.bit_size,
2210 layout_info.alignment,
2211 static_cast<uint32_t>(layout_info.field_offsets.size()),
2212 static_cast<uint32_t>(layout_info.base_offsets.size()),
2213 static_cast<uint32_t>(layout_info.vbase_offsets.size()));
2214
2215 uint32_t idx;
2216 {
2217 llvm::DenseMap<const clang::FieldDecl *, uint64_t>::const_iterator pos,
2218 end = layout_info.field_offsets.end();
2219 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx)
2220 {
2221 module_sp->LogMessage(log,
2222 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }",
2223 static_cast<void *>(clang_type.GetOpaqueQualType()),
2224 idx,
2225 static_cast<uint32_t>(pos->second),
2226 pos->first->getNameAsString().c_str());
2227 }
2228 }
2229
2230 {
2231 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos,
2232 base_end = layout_info.base_offsets.end();
2233 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx)
2234 {
2235 module_sp->LogMessage(log,
2236 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }",
2237 clang_type.GetOpaqueQualType(), idx, (uint32_t)base_pos->second.getQuantity(),
2238 base_pos->first->getNameAsString().c_str());
2239 }
2240 }
2241 {
2242 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos,
2243 vbase_end = layout_info.vbase_offsets.end();
2244 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx)
2245 {
2246 module_sp->LogMessage(log,
2247 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }",
2248 static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
2249 static_cast<uint32_t>(vbase_pos->second.getQuantity()),
2250 vbase_pos->first->getNameAsString().c_str());
2251 }
2252 }
2253
2254 }
2255 }
2256 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2257 }
2258 }
2259 }
2260
2261 return (bool)clang_type;
2262
2263 case DW_TAG_enumeration_type:
2264 ClangASTContext::StartTagDeclarationDefinition (clang_type);
2265 if (die.HasChildren())
2266 {
2267 SymbolContext sc(die.GetLLDBCompileUnit());
2268 bool is_signed = false;
2269 clang_type.IsIntegerType(is_signed);
2270 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), die);
2271 }
2272 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
2273 return (bool)clang_type;
2274
2275 default:
2276 assert(false && "not a forward clang type decl!");
2277 break;
2278 }
2279
2280 return false;
2281}
2282
Paul Hermanea188fc2015-09-16 18:48:30 +00002283std::vector<DWARFDIE>
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00002284DWARFASTParserClang::GetDIEForDeclContext(lldb_private::CompilerDeclContext decl_context)
Paul Hermanea188fc2015-09-16 18:48:30 +00002285{
2286 std::vector<DWARFDIE> result;
2287 for (auto it = m_decl_ctx_to_die.find((clang::DeclContext *)decl_context.GetOpaqueDeclContext()); it != m_decl_ctx_to_die.end(); it++)
2288 result.push_back(it->second);
2289 return result;
2290}
2291
Paul Hermand628cbb2015-09-15 23:44:17 +00002292CompilerDecl
2293DWARFASTParserClang::GetDeclForUIDFromDWARF (const DWARFDIE &die)
2294{
2295 clang::Decl *clang_decl = GetClangDeclForDIE(die);
2296 if (clang_decl != nullptr)
2297 return CompilerDecl(&m_ast, clang_decl);
2298 return CompilerDecl();
2299}
2300
Greg Clayton261ac3f2015-08-28 01:01:03 +00002301CompilerDeclContext
2302DWARFASTParserClang::GetDeclContextForUIDFromDWARF (const DWARFDIE &die)
2303{
2304 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE (die);
2305 if (clang_decl_ctx)
2306 return CompilerDeclContext(&m_ast, clang_decl_ctx);
2307 return CompilerDeclContext();
2308}
2309
2310CompilerDeclContext
2311DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF (const DWARFDIE &die)
2312{
2313 clang::DeclContext *clang_decl_ctx = GetClangDeclContextContainingDIE (die, nullptr);
2314 if (clang_decl_ctx)
2315 return CompilerDeclContext(&m_ast, clang_decl_ctx);
2316 return CompilerDeclContext();
2317}
2318
2319size_t
2320DWARFASTParserClang::ParseChildEnumerators (const SymbolContext& sc,
2321 lldb_private::CompilerType &clang_type,
2322 bool is_signed,
2323 uint32_t enumerator_byte_size,
2324 const DWARFDIE &parent_die)
2325{
2326 if (!parent_die)
2327 return 0;
2328
2329 size_t enumerators_added = 0;
2330
2331 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
2332 {
2333 const dw_tag_t tag = die.Tag();
2334 if (tag == DW_TAG_enumerator)
2335 {
2336 DWARFAttributes attributes;
2337 const size_t num_child_attributes = die.GetAttributes(attributes);
2338 if (num_child_attributes > 0)
2339 {
2340 const char *name = NULL;
2341 bool got_value = false;
2342 int64_t enum_value = 0;
2343 Declaration decl;
2344
2345 uint32_t i;
2346 for (i=0; i<num_child_attributes; ++i)
2347 {
2348 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2349 DWARFFormValue form_value;
2350 if (attributes.ExtractFormValueAtIndex(i, form_value))
2351 {
2352 switch (attr)
2353 {
2354 case DW_AT_const_value:
2355 got_value = true;
2356 if (is_signed)
2357 enum_value = form_value.Signed();
2358 else
2359 enum_value = form_value.Unsigned();
2360 break;
2361
2362 case DW_AT_name:
2363 name = form_value.AsCString();
2364 break;
2365
2366 case DW_AT_description:
2367 default:
2368 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2369 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2370 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2371 case DW_AT_sibling:
2372 break;
2373 }
2374 }
2375 }
2376
2377 if (name && name[0] && got_value)
2378 {
2379 m_ast.AddEnumerationValueToEnumerationType (clang_type.GetOpaqueQualType(),
2380 m_ast.GetEnumerationIntegerType(clang_type.GetOpaqueQualType()),
2381 decl,
2382 name,
2383 enum_value,
2384 enumerator_byte_size * 8);
2385 ++enumerators_added;
2386 }
2387 }
2388 }
2389 }
2390 return enumerators_added;
2391}
2392
2393#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
2394
2395class DIEStack
2396{
2397public:
2398
2399 void Push (const DWARFDIE &die)
2400 {
2401 m_dies.push_back (die);
2402 }
2403
2404
2405 void LogDIEs (Log *log)
2406 {
2407 StreamString log_strm;
2408 const size_t n = m_dies.size();
2409 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
2410 for (size_t i=0; i<n; i++)
2411 {
2412 std::string qualified_name;
2413 const DWARFDIE &die = m_dies[i];
2414 die.GetQualifiedName(qualified_name);
2415 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n",
2416 (uint64_t)i,
2417 die.GetOffset(),
2418 die.GetTagAsCString(),
2419 qualified_name.c_str());
2420 }
2421 log->PutCString(log_strm.GetData());
2422 }
2423 void Pop ()
2424 {
2425 m_dies.pop_back();
2426 }
2427
2428 class ScopedPopper
2429 {
2430 public:
2431 ScopedPopper (DIEStack &die_stack) :
2432 m_die_stack (die_stack),
2433 m_valid (false)
2434 {
2435 }
2436
2437 void
2438 Push (const DWARFDIE &die)
2439 {
2440 m_valid = true;
2441 m_die_stack.Push (die);
2442 }
2443
2444 ~ScopedPopper ()
2445 {
2446 if (m_valid)
2447 m_die_stack.Pop();
2448 }
2449
2450
2451
2452 protected:
2453 DIEStack &m_die_stack;
2454 bool m_valid;
2455 };
2456
2457protected:
2458 typedef std::vector<DWARFDIE> Stack;
2459 Stack m_dies;
2460};
2461#endif
2462
Greg Clayton261ac3f2015-08-28 01:01:03 +00002463Function *
2464DWARFASTParserClang::ParseFunctionFromDWARF (const SymbolContext& sc,
2465 const DWARFDIE &die)
2466{
2467 DWARFRangeList func_ranges;
2468 const char *name = NULL;
2469 const char *mangled = NULL;
2470 int decl_file = 0;
2471 int decl_line = 0;
2472 int decl_column = 0;
2473 int call_file = 0;
2474 int call_line = 0;
2475 int call_column = 0;
2476 DWARFExpression frame_base(die.GetCU());
2477
2478 const dw_tag_t tag = die.Tag();
2479
2480 if (tag != DW_TAG_subprogram)
2481 return NULL;
2482
2483 if (die.GetDIENamesAndRanges (name,
2484 mangled,
2485 func_ranges,
2486 decl_file,
2487 decl_line,
2488 decl_column,
2489 call_file,
2490 call_line,
2491 call_column,
2492 &frame_base))
2493 {
2494
2495 // Union of all ranges in the function DIE (if the function is discontiguous)
2496 AddressRange func_range;
2497 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
2498 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
2499 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
2500 {
2501 ModuleSP module_sp (die.GetModule());
2502 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, module_sp->GetSectionList());
2503 if (func_range.GetBaseAddress().IsValid())
2504 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
2505 }
2506
2507 if (func_range.GetBaseAddress().IsValid())
2508 {
2509 Mangled func_name;
2510 if (mangled)
2511 func_name.SetValue(ConstString(mangled), true);
2512 else if (die.GetParent().Tag() == DW_TAG_compile_unit &&
Jim Ingham0e0984e2015-09-02 01:06:46 +00002513 Language::LanguageIsCPlusPlus(die.GetLanguage()) &&
Greg Clayton261ac3f2015-08-28 01:01:03 +00002514 name && strcmp(name, "main") != 0)
2515 {
2516 // If the mangled name is not present in the DWARF, generate the demangled name
2517 // using the decl context. We skip if the function is "main" as its name is
2518 // never mangled.
2519 bool is_static = false;
2520 bool is_variadic = false;
Greg Claytonfb85e622016-02-09 22:36:24 +00002521 bool has_template_params = false;
Greg Clayton261ac3f2015-08-28 01:01:03 +00002522 unsigned type_quals = 0;
2523 std::vector<CompilerType> param_types;
2524 std::vector<clang::ParmVarDecl*> param_decls;
2525 DWARFDeclContext decl_ctx;
2526 StreamString sstr;
2527
2528 die.GetDWARFDeclContext(decl_ctx);
2529 sstr << decl_ctx.GetQualifiedName();
2530
2531 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE(die, nullptr);
2532 ParseChildParameters(sc,
2533 containing_decl_ctx,
2534 die,
2535 true,
2536 is_static,
2537 is_variadic,
Greg Claytonfb85e622016-02-09 22:36:24 +00002538 has_template_params,
Greg Clayton261ac3f2015-08-28 01:01:03 +00002539 param_types,
2540 param_decls,
2541 type_quals);
2542 sstr << "(";
2543 for (size_t i = 0; i < param_types.size(); i++)
2544 {
2545 if (i > 0)
2546 sstr << ", ";
2547 sstr << param_types[i].GetTypeName();
2548 }
2549 if (is_variadic)
2550 sstr << ", ...";
2551 sstr << ")";
2552 if (type_quals & clang::Qualifiers::Const)
2553 sstr << " const";
2554
2555 func_name.SetValue(ConstString(sstr.GetData()), false);
2556 }
2557 else
2558 func_name.SetValue(ConstString(name), false);
2559
2560 FunctionSP func_sp;
2561 std::unique_ptr<Declaration> decl_ap;
2562 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
2563 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
2564 decl_line,
2565 decl_column));
2566
2567 SymbolFileDWARF *dwarf = die.GetDWARF();
2568 // Supply the type _only_ if it has already been parsed
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002569 Type *func_type = dwarf->GetDIEToType().lookup (die.GetDIE());
Greg Clayton261ac3f2015-08-28 01:01:03 +00002570
2571 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
2572
2573 if (dwarf->FixupAddress (func_range.GetBaseAddress()))
2574 {
2575 const user_id_t func_user_id = die.GetID();
2576 func_sp.reset(new Function (sc.comp_unit,
2577 func_user_id, // UserID is the DIE offset
2578 func_user_id,
2579 func_name,
2580 func_type,
2581 func_range)); // first address range
2582
2583 if (func_sp.get() != NULL)
2584 {
2585 if (frame_base.IsValid())
2586 func_sp->GetFrameBaseExpression() = frame_base;
2587 sc.comp_unit->AddFunction(func_sp);
2588 return func_sp.get();
2589 }
2590 }
2591 }
2592 }
2593 return NULL;
2594}
2595
2596
Siva Chandracebabb92015-09-23 17:47:08 +00002597bool
Greg Clayton261ac3f2015-08-28 01:01:03 +00002598DWARFASTParserClang::ParseChildMembers (const SymbolContext& sc,
2599 const DWARFDIE &parent_die,
2600 CompilerType &class_clang_type,
2601 const LanguageType class_language,
2602 std::vector<clang::CXXBaseSpecifier *>& base_classes,
2603 std::vector<int>& member_accessibilities,
2604 DWARFDIECollection& member_function_dies,
2605 DelayedPropertyList& delayed_properties,
2606 AccessType& default_accessibility,
2607 bool &is_a_class,
2608 LayoutInfo &layout_info)
2609{
2610 if (!parent_die)
2611 return 0;
2612
Greg Clayton261ac3f2015-08-28 01:01:03 +00002613 uint32_t member_idx = 0;
2614 BitfieldInfo last_field_info;
2615
2616 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
Greg Claytonf73034f2015-09-08 18:15:05 +00002617 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(class_clang_type.GetTypeSystem());
Greg Clayton261ac3f2015-08-28 01:01:03 +00002618 if (ast == nullptr)
2619 return 0;
2620
2621 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
2622 {
2623 dw_tag_t tag = die.Tag();
2624
2625 switch (tag)
2626 {
2627 case DW_TAG_member:
2628 case DW_TAG_APPLE_property:
2629 {
2630 DWARFAttributes attributes;
2631 const size_t num_attributes = die.GetAttributes (attributes);
2632 if (num_attributes > 0)
2633 {
2634 Declaration decl;
2635 //DWARFExpression location;
2636 const char *name = NULL;
2637 const char *prop_name = NULL;
2638 const char *prop_getter_name = NULL;
2639 const char *prop_setter_name = NULL;
2640 uint32_t prop_attributes = 0;
2641
2642
2643 bool is_artificial = false;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002644 DWARFFormValue encoding_form;
Greg Clayton261ac3f2015-08-28 01:01:03 +00002645 AccessType accessibility = eAccessNone;
2646 uint32_t member_byte_offset = UINT32_MAX;
2647 size_t byte_size = 0;
2648 size_t bit_offset = 0;
2649 size_t bit_size = 0;
2650 bool is_external = false; // On DW_TAG_members, this means the member is static
2651 uint32_t i;
2652 for (i=0; i<num_attributes && !is_artificial; ++i)
2653 {
2654 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2655 DWARFFormValue form_value;
2656 if (attributes.ExtractFormValueAtIndex(i, form_value))
2657 {
2658 switch (attr)
2659 {
2660 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2661 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2662 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2663 case DW_AT_name: name = form_value.AsCString(); break;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002664 case DW_AT_type: encoding_form = form_value; break;
Greg Clayton261ac3f2015-08-28 01:01:03 +00002665 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
2666 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
2667 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
2668 case DW_AT_data_member_location:
2669 if (form_value.BlockData())
2670 {
2671 Value initialValue(0);
2672 Value memberOffset(0);
2673 const DWARFDataExtractor& debug_info_data = die.GetDWARF()->get_debug_info_data();
2674 uint32_t block_length = form_value.Unsigned();
2675 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
2676 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
2677 NULL, // ClangExpressionVariableList *
2678 NULL, // ClangExpressionDeclMap *
2679 NULL, // RegisterContext *
2680 module_sp,
2681 debug_info_data,
2682 die.GetCU(),
2683 block_offset,
2684 block_length,
2685 eRegisterKindDWARF,
2686 &initialValue,
2687 memberOffset,
2688 NULL))
2689 {
2690 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
2691 }
2692 }
2693 else
2694 {
2695 // With DWARF 3 and later, if the value is an integer constant,
2696 // this form value is the offset in bytes from the beginning
2697 // of the containing entity.
2698 member_byte_offset = form_value.Unsigned();
2699 }
2700 break;
2701
2702 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
2703 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
2704 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString();
2705 break;
2706 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString();
2707 break;
2708 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString();
2709 break;
2710 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
2711 case DW_AT_external: is_external = form_value.Boolean(); break;
2712
2713 default:
2714 case DW_AT_declaration:
2715 case DW_AT_description:
2716 case DW_AT_mutable:
2717 case DW_AT_visibility:
2718 case DW_AT_sibling:
2719 break;
2720 }
2721 }
2722 }
2723
2724 if (prop_name)
2725 {
2726 ConstString fixed_getter;
2727 ConstString fixed_setter;
2728
2729 // Check if the property getter/setter were provided as full
2730 // names. We want basenames, so we extract them.
2731
2732 if (prop_getter_name && prop_getter_name[0] == '-')
2733 {
Jim Inghamaa816b82015-09-02 01:59:14 +00002734 ObjCLanguage::MethodName prop_getter_method(prop_getter_name, true);
Greg Clayton261ac3f2015-08-28 01:01:03 +00002735 prop_getter_name = prop_getter_method.GetSelector().GetCString();
2736 }
2737
2738 if (prop_setter_name && prop_setter_name[0] == '-')
2739 {
Jim Inghamaa816b82015-09-02 01:59:14 +00002740 ObjCLanguage::MethodName prop_setter_method(prop_setter_name, true);
Greg Clayton261ac3f2015-08-28 01:01:03 +00002741 prop_setter_name = prop_setter_method.GetSelector().GetCString();
2742 }
2743
2744 // If the names haven't been provided, they need to be
2745 // filled in.
2746
2747 if (!prop_getter_name)
2748 {
2749 prop_getter_name = prop_name;
2750 }
2751 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
2752 {
2753 StreamString ss;
2754
2755 ss.Printf("set%c%s:",
2756 toupper(prop_name[0]),
2757 &prop_name[1]);
2758
2759 fixed_setter.SetCString(ss.GetData());
2760 prop_setter_name = fixed_setter.GetCString();
2761 }
2762 }
2763
2764 // Clang has a DWARF generation bug where sometimes it
2765 // represents fields that are references with bad byte size
2766 // and bit size/offset information such as:
2767 //
2768 // DW_AT_byte_size( 0x00 )
2769 // DW_AT_bit_size( 0x40 )
2770 // DW_AT_bit_offset( 0xffffffffffffffc0 )
2771 //
2772 // So check the bit offset to make sure it is sane, and if
2773 // the values are not sane, remove them. If we don't do this
2774 // then we will end up with a crash if we try to use this
2775 // type in an expression when clang becomes unhappy with its
2776 // recycled debug info.
2777
2778 if (bit_offset > 128)
2779 {
2780 bit_size = 0;
2781 bit_offset = 0;
2782 }
2783
2784 // FIXME: Make Clang ignore Objective-C accessibility for expressions
2785 if (class_language == eLanguageTypeObjC ||
2786 class_language == eLanguageTypeObjC_plus_plus)
2787 accessibility = eAccessNone;
2788
2789 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
2790 {
2791 // Not all compilers will mark the vtable pointer
2792 // member as artificial (llvm-gcc). We can't have
2793 // the virtual members in our classes otherwise it
2794 // throws off all child offsets since we end up
2795 // having and extra pointer sized member in our
2796 // class layouts.
2797 is_artificial = true;
2798 }
2799
2800 // Handle static members
2801 if (is_external && member_byte_offset == UINT32_MAX)
2802 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002803 Type *var_type = die.ResolveTypeUID(DIERef(encoding_form).GetUID());
Greg Clayton261ac3f2015-08-28 01:01:03 +00002804
2805 if (var_type)
2806 {
2807 if (accessibility == eAccessNone)
2808 accessibility = eAccessPublic;
2809 ClangASTContext::AddVariableToRecordType (class_clang_type,
2810 name,
2811 var_type->GetLayoutCompilerType (),
2812 accessibility);
2813 }
2814 break;
2815 }
2816
2817 if (is_artificial == false)
2818 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002819 Type *member_type = die.ResolveTypeUID(DIERef(encoding_form).GetUID());
Greg Clayton261ac3f2015-08-28 01:01:03 +00002820
2821 clang::FieldDecl *field_decl = NULL;
2822 if (tag == DW_TAG_member)
2823 {
2824 if (member_type)
2825 {
2826 if (accessibility == eAccessNone)
2827 accessibility = default_accessibility;
2828 member_accessibilities.push_back(accessibility);
2829
2830 uint64_t field_bit_offset = (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
2831 if (bit_size > 0)
2832 {
2833
2834 BitfieldInfo this_field_info;
2835 this_field_info.bit_offset = field_bit_offset;
2836 this_field_info.bit_size = bit_size;
2837
2838 /////////////////////////////////////////////////////////////
2839 // How to locate a field given the DWARF debug information
2840 //
2841 // AT_byte_size indicates the size of the word in which the
2842 // bit offset must be interpreted.
2843 //
2844 // AT_data_member_location indicates the byte offset of the
2845 // word from the base address of the structure.
2846 //
2847 // AT_bit_offset indicates how many bits into the word
2848 // (according to the host endianness) the low-order bit of
2849 // the field starts. AT_bit_offset can be negative.
2850 //
2851 // AT_bit_size indicates the size of the field in bits.
2852 /////////////////////////////////////////////////////////////
2853
2854 if (byte_size == 0)
2855 byte_size = member_type->GetByteSize();
2856
2857 if (die.GetDWARF()->GetObjectFile()->GetByteOrder() == eByteOrderLittle)
2858 {
2859 this_field_info.bit_offset += byte_size * 8;
2860 this_field_info.bit_offset -= (bit_offset + bit_size);
2861 }
2862 else
2863 {
2864 this_field_info.bit_offset += bit_offset;
2865 }
2866
2867 // Update the field bit offset we will report for layout
2868 field_bit_offset = this_field_info.bit_offset;
2869
2870 // If the member to be emitted did not start on a character boundary and there is
2871 // empty space between the last field and this one, then we need to emit an
2872 // anonymous member filling up the space up to its start. There are three cases
2873 // here:
2874 //
2875 // 1 If the previous member ended on a character boundary, then we can emit an
2876 // anonymous member starting at the most recent character boundary.
2877 //
2878 // 2 If the previous member did not end on a character boundary and the distance
2879 // from the end of the previous member to the current member is less than a
2880 // word width, then we can emit an anonymous member starting right after the
2881 // previous member and right before this member.
2882 //
2883 // 3 If the previous member did not end on a character boundary and the distance
2884 // from the end of the previous member to the current member is greater than
2885 // or equal a word width, then we act as in Case 1.
2886
2887 const uint64_t character_width = 8;
2888 const uint64_t word_width = 32;
2889
2890 // Objective-C has invalid DW_AT_bit_offset values in older versions
2891 // of clang, so we have to be careful and only insert unnamed bitfields
2892 // if we have a new enough clang.
2893 bool detect_unnamed_bitfields = true;
2894
2895 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus)
2896 detect_unnamed_bitfields = die.GetCU()->Supports_unnamed_objc_bitfields ();
2897
2898 if (detect_unnamed_bitfields)
2899 {
2900 BitfieldInfo anon_field_info;
2901
2902 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned
2903 {
2904 uint64_t last_field_end = 0;
2905
2906 if (last_field_info.IsValid())
2907 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
2908
2909 if (this_field_info.bit_offset != last_field_end)
2910 {
2911 if (((last_field_end % character_width) == 0) || // case 1
2912 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3
2913 {
2914 anon_field_info.bit_size = this_field_info.bit_offset % character_width;
2915 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size;
2916 }
2917 else // case 2
2918 {
2919 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end;
2920 anon_field_info.bit_offset = last_field_end;
2921 }
2922 }
2923 }
2924
2925 if (anon_field_info.IsValid())
2926 {
2927 clang::FieldDecl *unnamed_bitfield_decl =
2928 ClangASTContext::AddFieldToRecordType (class_clang_type,
2929 NULL,
2930 m_ast.GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width),
2931 accessibility,
2932 anon_field_info.bit_size);
2933
2934 layout_info.field_offsets.insert(
2935 std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset));
2936 }
2937 }
2938 last_field_info = this_field_info;
2939 }
2940 else
2941 {
2942 last_field_info.Clear();
2943 }
2944
2945 CompilerType member_clang_type = member_type->GetLayoutCompilerType ();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002946 if (!member_clang_type.IsCompleteType())
2947 member_clang_type.GetCompleteType();
Greg Clayton261ac3f2015-08-28 01:01:03 +00002948
2949 {
2950 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>).
2951 // If the current field is at the end of the structure, then there is definitely no room for extra
2952 // elements and we override the type to array[0].
2953
2954 CompilerType member_array_element_type;
2955 uint64_t member_array_size;
2956 bool member_array_is_incomplete;
2957
2958 if (member_clang_type.IsArrayType(&member_array_element_type,
2959 &member_array_size,
2960 &member_array_is_incomplete) &&
2961 !member_array_is_incomplete)
2962 {
2963 uint64_t parent_byte_size = parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
2964
2965 if (member_byte_offset >= parent_byte_size)
2966 {
Tamas Berghammer808ff182016-01-13 14:58:48 +00002967 if (member_array_size != 1 && (member_array_size != 0 || member_byte_offset > parent_byte_size))
Greg Clayton261ac3f2015-08-28 01:01:03 +00002968 {
2969 module_sp->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,
2970 die.GetID(),
2971 name,
Tamas Berghammereb882fc2015-09-09 10:20:48 +00002972 encoding_form.Reference(),
Greg Clayton261ac3f2015-08-28 01:01:03 +00002973 parent_die.GetID());
2974 }
2975
2976 member_clang_type = m_ast.CreateArrayType(member_array_element_type, 0, false);
2977 }
2978 }
2979 }
2980
Greg Claytone6b36cd2015-12-08 01:02:08 +00002981 if (ClangASTContext::IsCXXClassType(member_clang_type) && member_clang_type.GetCompleteType() == false)
2982 {
2983 if (die.GetCU()->GetProducer() == DWARFCompileUnit::eProducerClang)
2984 module_sp->ReportError ("DWARF DIE at 0x%8.8x (class %s) has a member variable 0x%8.8x (%s) whose type is a forward declaration, not a complete definition.\nTry compiling the source file with -fno-limit-debug-info",
2985 parent_die.GetOffset(),
2986 parent_die.GetName(),
2987 die.GetOffset(),
2988 name);
2989 else
2990 module_sp->ReportError ("DWARF DIE at 0x%8.8x (class %s) has a member variable 0x%8.8x (%s) whose type is a forward declaration, not a complete definition.\nPlease file a bug against the compiler and include the preprocessed output for %s",
2991 parent_die.GetOffset(),
2992 parent_die.GetName(),
2993 die.GetOffset(),
2994 name,
2995 sc.comp_unit ? sc.comp_unit->GetPath().c_str() : "the source file");
2996 // We have no choice other than to pretend that the member class
2997 // is complete. If we don't do this, clang will crash when trying
2998 // to layout the class. Since we provide layout assistance, all
2999 // ivars in this class and other classes will be fine, this is
3000 // the best we can do short of crashing.
3001 ClangASTContext::StartTagDeclarationDefinition(member_clang_type);
3002 ClangASTContext::CompleteTagDeclarationDefinition(member_clang_type);
3003 }
3004
Greg Clayton261ac3f2015-08-28 01:01:03 +00003005 field_decl = ClangASTContext::AddFieldToRecordType (class_clang_type,
3006 name,
3007 member_clang_type,
3008 accessibility,
3009 bit_size);
3010
3011 m_ast.SetMetadataAsUserID (field_decl, die.GetID());
3012
3013 layout_info.field_offsets.insert(std::make_pair(field_decl, field_bit_offset));
3014 }
3015 else
3016 {
3017 if (name)
3018 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
3019 die.GetID(),
3020 name,
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003021 encoding_form.Reference());
Greg Clayton261ac3f2015-08-28 01:01:03 +00003022 else
3023 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
3024 die.GetID(),
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003025 encoding_form.Reference());
Greg Clayton261ac3f2015-08-28 01:01:03 +00003026 }
3027 }
3028
3029 if (prop_name != NULL && member_type)
3030 {
3031 clang::ObjCIvarDecl *ivar_decl = NULL;
3032
3033 if (field_decl)
3034 {
3035 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
3036 assert (ivar_decl != NULL);
3037 }
3038
3039 ClangASTMetadata metadata;
3040 metadata.SetUserID (die.GetID());
3041 delayed_properties.push_back(DelayedAddObjCClassProperty(class_clang_type,
3042 prop_name,
3043 member_type->GetLayoutCompilerType (),
3044 ivar_decl,
3045 prop_setter_name,
3046 prop_getter_name,
3047 prop_attributes,
3048 &metadata));
3049
3050 if (ivar_decl)
3051 m_ast.SetMetadataAsUserID (ivar_decl, die.GetID());
3052 }
3053 }
3054 }
3055 ++member_idx;
3056 }
3057 break;
3058
3059 case DW_TAG_subprogram:
3060 // Let the type parsing code handle this one for us.
3061 member_function_dies.Append (die);
3062 break;
3063
3064 case DW_TAG_inheritance:
3065 {
3066 is_a_class = true;
3067 if (default_accessibility == eAccessNone)
3068 default_accessibility = eAccessPrivate;
3069 // TODO: implement DW_TAG_inheritance type parsing
3070 DWARFAttributes attributes;
3071 const size_t num_attributes = die.GetAttributes (attributes);
3072 if (num_attributes > 0)
3073 {
3074 Declaration decl;
3075 DWARFExpression location(die.GetCU());
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003076 DWARFFormValue encoding_form;
Greg Clayton261ac3f2015-08-28 01:01:03 +00003077 AccessType accessibility = default_accessibility;
3078 bool is_virtual = false;
3079 bool is_base_of_class = true;
3080 off_t member_byte_offset = 0;
3081 uint32_t i;
3082 for (i=0; i<num_attributes; ++i)
3083 {
3084 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3085 DWARFFormValue form_value;
3086 if (attributes.ExtractFormValueAtIndex(i, form_value))
3087 {
3088 switch (attr)
3089 {
3090 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3091 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3092 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003093 case DW_AT_type: encoding_form = form_value; break;
Greg Clayton261ac3f2015-08-28 01:01:03 +00003094 case DW_AT_data_member_location:
3095 if (form_value.BlockData())
3096 {
3097 Value initialValue(0);
3098 Value memberOffset(0);
3099 const DWARFDataExtractor& debug_info_data = die.GetDWARF()->get_debug_info_data();
3100 uint32_t block_length = form_value.Unsigned();
3101 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
3102 if (DWARFExpression::Evaluate (NULL,
3103 NULL,
3104 NULL,
3105 NULL,
3106 module_sp,
3107 debug_info_data,
3108 die.GetCU(),
3109 block_offset,
3110 block_length,
3111 eRegisterKindDWARF,
3112 &initialValue,
3113 memberOffset,
3114 NULL))
3115 {
3116 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
3117 }
3118 }
3119 else
3120 {
3121 // With DWARF 3 and later, if the value is an integer constant,
3122 // this form value is the offset in bytes from the beginning
3123 // of the containing entity.
3124 member_byte_offset = form_value.Unsigned();
3125 }
3126 break;
3127
3128 case DW_AT_accessibility:
3129 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
3130 break;
3131
3132 case DW_AT_virtuality:
3133 is_virtual = form_value.Boolean();
3134 break;
3135
3136 case DW_AT_sibling:
3137 break;
3138
3139 default:
3140 break;
3141 }
3142 }
3143 }
3144
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003145 Type *base_class_type = die.ResolveTypeUID(DIERef(encoding_form).GetUID());
Greg Clayton261ac3f2015-08-28 01:01:03 +00003146 if (base_class_type == NULL)
3147 {
3148 module_sp->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",
3149 die.GetOffset(),
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003150 encoding_form.Reference(),
Greg Clayton261ac3f2015-08-28 01:01:03 +00003151 parent_die.GetOffset());
3152 break;
3153 }
3154
3155 CompilerType base_class_clang_type = base_class_type->GetFullCompilerType ();
3156 assert (base_class_clang_type);
3157 if (class_language == eLanguageTypeObjC)
3158 {
3159 ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
3160 }
3161 else
3162 {
3163 base_classes.push_back (ast->CreateBaseClassSpecifier (base_class_clang_type.GetOpaqueQualType(),
3164 accessibility,
3165 is_virtual,
3166 is_base_of_class));
3167
3168 if (is_virtual)
3169 {
3170 // Do not specify any offset for virtual inheritance. The DWARF produced by clang doesn't
3171 // give us a constant offset, but gives us a DWARF expressions that requires an actual object
3172 // in memory. the DW_AT_data_member_location for a virtual base class looks like:
3173 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, DW_OP_plus )
3174 // Given this, there is really no valid response we can give to clang for virtual base
3175 // class offsets, and this should eventually be removed from LayoutRecordType() in the external
3176 // AST source in clang.
3177 }
3178 else
3179 {
3180 layout_info.base_offsets.insert(
3181 std::make_pair(ast->GetAsCXXRecordDecl(base_class_clang_type.GetOpaqueQualType()),
3182 clang::CharUnits::fromQuantity(member_byte_offset)));
3183 }
3184 }
3185 }
3186 }
3187 break;
3188
3189 default:
3190 break;
3191 }
3192 }
3193
Greg Claytone6b36cd2015-12-08 01:02:08 +00003194 return true;
Greg Clayton261ac3f2015-08-28 01:01:03 +00003195}
3196
3197
3198size_t
3199DWARFASTParserClang::ParseChildParameters (const SymbolContext& sc,
3200 clang::DeclContext *containing_decl_ctx,
3201 const DWARFDIE &parent_die,
3202 bool skip_artificial,
3203 bool &is_static,
3204 bool &is_variadic,
Greg Claytonfb85e622016-02-09 22:36:24 +00003205 bool &has_template_params,
Greg Clayton261ac3f2015-08-28 01:01:03 +00003206 std::vector<CompilerType>& function_param_types,
3207 std::vector<clang::ParmVarDecl*>& function_param_decls,
3208 unsigned &type_quals)
3209{
3210 if (!parent_die)
3211 return 0;
3212
3213 size_t arg_idx = 0;
3214 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
3215 {
3216 const dw_tag_t tag = die.Tag();
3217 switch (tag)
3218 {
3219 case DW_TAG_formal_parameter:
3220 {
3221 DWARFAttributes attributes;
3222 const size_t num_attributes = die.GetAttributes(attributes);
3223 if (num_attributes > 0)
3224 {
3225 const char *name = NULL;
3226 Declaration decl;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003227 DWARFFormValue param_type_die_form;
Greg Clayton261ac3f2015-08-28 01:01:03 +00003228 bool is_artificial = false;
3229 // one of None, Auto, Register, Extern, Static, PrivateExtern
3230
3231 clang::StorageClass storage = clang::SC_None;
3232 uint32_t i;
3233 for (i=0; i<num_attributes; ++i)
3234 {
3235 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3236 DWARFFormValue form_value;
3237 if (attributes.ExtractFormValueAtIndex(i, form_value))
3238 {
3239 switch (attr)
3240 {
3241 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3242 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3243 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3244 case DW_AT_name: name = form_value.AsCString();
3245 break;
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003246 case DW_AT_type: param_type_die_form = form_value; break;
Greg Clayton261ac3f2015-08-28 01:01:03 +00003247 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
3248 case DW_AT_location:
3249 // if (form_value.BlockData())
3250 // {
3251 // const DWARFDataExtractor& debug_info_data = debug_info();
3252 // uint32_t block_length = form_value.Unsigned();
3253 // DWARFDataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3254 // }
3255 // else
3256 // {
3257 // }
3258 // break;
3259 case DW_AT_const_value:
3260 case DW_AT_default_value:
3261 case DW_AT_description:
3262 case DW_AT_endianity:
3263 case DW_AT_is_optional:
3264 case DW_AT_segment:
3265 case DW_AT_variable_parameter:
3266 default:
3267 case DW_AT_abstract_origin:
3268 case DW_AT_sibling:
3269 break;
3270 }
3271 }
3272 }
3273
3274 bool skip = false;
3275 if (skip_artificial)
3276 {
3277 if (is_artificial)
3278 {
3279 // In order to determine if a C++ member function is
3280 // "const" we have to look at the const-ness of "this"...
3281 // Ugly, but that
3282 if (arg_idx == 0)
3283 {
3284 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
3285 {
3286 // Often times compilers omit the "this" name for the
3287 // specification DIEs, so we can't rely upon the name
3288 // being in the formal parameter DIE...
3289 if (name == NULL || ::strcmp(name, "this")==0)
3290 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003291 Type *this_type = die.ResolveTypeUID (DIERef(param_type_die_form).GetUID());
Greg Clayton261ac3f2015-08-28 01:01:03 +00003292 if (this_type)
3293 {
3294 uint32_t encoding_mask = this_type->GetEncodingMask();
3295 if (encoding_mask & Type::eEncodingIsPointerUID)
3296 {
3297 is_static = false;
3298
3299 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3300 type_quals |= clang::Qualifiers::Const;
3301 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3302 type_quals |= clang::Qualifiers::Volatile;
3303 }
3304 }
3305 }
3306 }
3307 }
3308 skip = true;
3309 }
3310 else
3311 {
3312
3313 // HACK: Objective C formal parameters "self" and "_cmd"
3314 // are not marked as artificial in the DWARF...
3315 CompileUnit *comp_unit = die.GetLLDBCompileUnit();
3316 if (comp_unit)
3317 {
3318 switch (comp_unit->GetLanguage())
3319 {
3320 case eLanguageTypeObjC:
3321 case eLanguageTypeObjC_plus_plus:
3322 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3323 skip = true;
3324 break;
3325 default:
3326 break;
3327 }
3328 }
3329 }
3330 }
3331
3332 if (!skip)
3333 {
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003334 Type *type = die.ResolveTypeUID(DIERef(param_type_die_form).GetUID());
Greg Clayton261ac3f2015-08-28 01:01:03 +00003335 if (type)
3336 {
3337 function_param_types.push_back (type->GetForwardCompilerType ());
3338
3339 clang::ParmVarDecl *param_var_decl = m_ast.CreateParameterDeclaration (name,
3340 type->GetForwardCompilerType (),
3341 storage);
3342 assert(param_var_decl);
3343 function_param_decls.push_back(param_var_decl);
3344
3345 m_ast.SetMetadataAsUserID (param_var_decl, die.GetID());
3346 }
3347 }
3348 }
3349 arg_idx++;
3350 }
3351 break;
3352
3353 case DW_TAG_unspecified_parameters:
3354 is_variadic = true;
3355 break;
3356
3357 case DW_TAG_template_type_parameter:
3358 case DW_TAG_template_value_parameter:
3359 // The one caller of this was never using the template_param_infos,
3360 // and the local variable was taking up a large amount of stack space
3361 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need
3362 // the template params back, we can add them back.
3363 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonfb85e622016-02-09 22:36:24 +00003364 has_template_params = true;
Greg Clayton261ac3f2015-08-28 01:01:03 +00003365 break;
3366
3367 default:
3368 break;
3369 }
3370 }
3371 return arg_idx;
3372}
3373
3374void
3375DWARFASTParserClang::ParseChildArrayInfo (const SymbolContext& sc,
3376 const DWARFDIE &parent_die,
3377 int64_t& first_index,
3378 std::vector<uint64_t>& element_orders,
3379 uint32_t& byte_stride,
3380 uint32_t& bit_stride)
3381{
3382 if (!parent_die)
3383 return;
3384
3385 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
3386 {
3387 const dw_tag_t tag = die.Tag();
3388 switch (tag)
3389 {
3390 case DW_TAG_subrange_type:
3391 {
3392 DWARFAttributes attributes;
3393 const size_t num_child_attributes = die.GetAttributes(attributes);
3394 if (num_child_attributes > 0)
3395 {
3396 uint64_t num_elements = 0;
3397 uint64_t lower_bound = 0;
3398 uint64_t upper_bound = 0;
3399 bool upper_bound_valid = false;
3400 uint32_t i;
3401 for (i=0; i<num_child_attributes; ++i)
3402 {
3403 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3404 DWARFFormValue form_value;
3405 if (attributes.ExtractFormValueAtIndex(i, form_value))
3406 {
3407 switch (attr)
3408 {
3409 case DW_AT_name:
3410 break;
3411
3412 case DW_AT_count:
3413 num_elements = form_value.Unsigned();
3414 break;
3415
3416 case DW_AT_bit_stride:
3417 bit_stride = form_value.Unsigned();
3418 break;
3419
3420 case DW_AT_byte_stride:
3421 byte_stride = form_value.Unsigned();
3422 break;
3423
3424 case DW_AT_lower_bound:
3425 lower_bound = form_value.Unsigned();
3426 break;
3427
3428 case DW_AT_upper_bound:
3429 upper_bound_valid = true;
3430 upper_bound = form_value.Unsigned();
3431 break;
3432
3433 default:
3434 case DW_AT_abstract_origin:
3435 case DW_AT_accessibility:
3436 case DW_AT_allocated:
3437 case DW_AT_associated:
3438 case DW_AT_data_location:
3439 case DW_AT_declaration:
3440 case DW_AT_description:
3441 case DW_AT_sibling:
3442 case DW_AT_threads_scaled:
3443 case DW_AT_type:
3444 case DW_AT_visibility:
3445 break;
3446 }
3447 }
3448 }
3449
3450 if (num_elements == 0)
3451 {
3452 if (upper_bound_valid && upper_bound >= lower_bound)
3453 num_elements = upper_bound - lower_bound + 1;
3454 }
3455
3456 element_orders.push_back (num_elements);
3457 }
3458 }
3459 break;
3460 }
3461 }
3462}
3463
Paul Hermand628cbb2015-09-15 23:44:17 +00003464Type *
3465DWARFASTParserClang::GetTypeForDIE (const DWARFDIE &die)
3466{
3467 if (die)
3468 {
3469 SymbolFileDWARF *dwarf = die.GetDWARF();
3470 DWARFAttributes attributes;
3471 const size_t num_attributes = die.GetAttributes(attributes);
3472 if (num_attributes > 0)
3473 {
3474 DWARFFormValue type_die_form;
3475 for (size_t i = 0; i < num_attributes; ++i)
3476 {
3477 dw_attr_t attr = attributes.AttributeAtIndex(i);
3478 DWARFFormValue form_value;
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00003479
Paul Hermand628cbb2015-09-15 23:44:17 +00003480 if (attr == DW_AT_type && attributes.ExtractFormValueAtIndex(i, form_value))
3481 return dwarf->ResolveTypeUID(DIERef(form_value).GetUID());
3482 }
3483 }
3484 }
3485
3486 return nullptr;
3487}
3488
3489clang::Decl *
3490DWARFASTParserClang::GetClangDeclForDIE (const DWARFDIE &die)
3491{
3492 if (!die)
3493 return nullptr;
3494
Paul Hermanf6681b42015-09-17 19:32:02 +00003495 switch (die.Tag())
3496 {
3497 case DW_TAG_variable:
3498 case DW_TAG_constant:
3499 case DW_TAG_formal_parameter:
3500 case DW_TAG_imported_declaration:
3501 case DW_TAG_imported_module:
3502 break;
3503 default:
3504 return nullptr;
3505 }
Paul Hermanea188fc2015-09-16 18:48:30 +00003506
Paul Hermanf6681b42015-09-17 19:32:02 +00003507 DIEToDeclMap::iterator cache_pos = m_die_to_decl.find(die.GetDIE());
3508 if (cache_pos != m_die_to_decl.end())
3509 return cache_pos->second;
3510
3511 if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification))
3512 {
3513 clang::Decl *decl = GetClangDeclForDIE(spec_die);
3514 m_die_to_decl[die.GetDIE()] = decl;
3515 m_decl_to_die[decl].insert(die.GetDIE());
Paul Hermand628cbb2015-09-15 23:44:17 +00003516 return decl;
Paul Hermanf6681b42015-09-17 19:32:02 +00003517 }
Paul Hermand628cbb2015-09-15 23:44:17 +00003518
Paul Hermanf6681b42015-09-17 19:32:02 +00003519 clang::Decl *decl = nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00003520 switch (die.Tag())
3521 {
3522 case DW_TAG_variable:
3523 case DW_TAG_constant:
3524 case DW_TAG_formal_parameter:
3525 {
3526 SymbolFileDWARF *dwarf = die.GetDWARF();
3527 Type *type = GetTypeForDIE(die);
3528 const char *name = die.GetName();
3529 clang::DeclContext *decl_context = ClangASTContext::DeclContextGetAsDeclContext(dwarf->GetDeclContextContainingUID(die.GetID()));
3530 decl = m_ast.CreateVariableDeclaration(
3531 decl_context,
3532 name,
3533 ClangASTContext::GetQualType(type->GetForwardCompilerType()));
3534 break;
3535 }
3536 case DW_TAG_imported_declaration:
3537 {
3538 SymbolFileDWARF *dwarf = die.GetDWARF();
3539 lldb::user_id_t imported_uid = die.GetAttributeValueAsReference(DW_AT_import, DW_INVALID_OFFSET);
3540
3541 if (dwarf->UserIDMatches(imported_uid))
3542 {
3543 CompilerDecl imported_decl = dwarf->GetDeclForUID(imported_uid);
3544 if (imported_decl)
3545 {
3546 clang::DeclContext *decl_context = ClangASTContext::DeclContextGetAsDeclContext(dwarf->GetDeclContextContainingUID(die.GetID()));
3547 if (clang::NamedDecl *clang_imported_decl = llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)imported_decl.GetOpaqueDecl()))
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00003548 decl = m_ast.CreateUsingDeclaration(decl_context, clang_imported_decl);
Paul Hermand628cbb2015-09-15 23:44:17 +00003549 }
3550 }
3551 break;
3552 }
3553 case DW_TAG_imported_module:
3554 {
3555 SymbolFileDWARF *dwarf = die.GetDWARF();
3556 lldb::user_id_t imported_uid = die.GetAttributeValueAsReference(DW_AT_import, DW_INVALID_OFFSET);
3557
3558 if (dwarf->UserIDMatches(imported_uid))
3559 {
3560 CompilerDeclContext imported_decl = dwarf->GetDeclContextForUID(imported_uid);
3561 if (imported_decl)
3562 {
3563 clang::DeclContext *decl_context = ClangASTContext::DeclContextGetAsDeclContext(dwarf->GetDeclContextContainingUID(die.GetID()));
Paul Hermanea188fc2015-09-16 18:48:30 +00003564 if (clang::NamespaceDecl *ns_decl = ClangASTContext::DeclContextGetAsNamespaceDecl(imported_decl))
3565 decl = m_ast.CreateUsingDirectiveDeclaration(decl_context, ns_decl);
Paul Hermand628cbb2015-09-15 23:44:17 +00003566 }
3567 }
3568 break;
3569 }
3570 default:
3571 break;
3572 }
3573
3574 m_die_to_decl[die.GetDIE()] = decl;
3575 m_decl_to_die[decl].insert(die.GetDIE());
3576
3577 return decl;
3578}
3579
Greg Clayton261ac3f2015-08-28 01:01:03 +00003580clang::DeclContext *
3581DWARFASTParserClang::GetClangDeclContextForDIE (const DWARFDIE &die)
3582{
3583 if (die)
3584 {
3585 clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE (die);
3586 if (decl_ctx)
3587 return decl_ctx;
3588
3589 bool try_parsing_type = true;
3590 switch (die.Tag())
3591 {
3592 case DW_TAG_compile_unit:
3593 decl_ctx = m_ast.GetTranslationUnitDecl();
3594 try_parsing_type = false;
3595 break;
3596
3597 case DW_TAG_namespace:
3598 decl_ctx = ResolveNamespaceDIE (die);
3599 try_parsing_type = false;
3600 break;
3601
Paul Hermand628cbb2015-09-15 23:44:17 +00003602 case DW_TAG_lexical_block:
3603 decl_ctx = (clang::DeclContext *)ResolveBlockDIE(die);
3604 try_parsing_type = false;
3605 break;
3606
Greg Clayton261ac3f2015-08-28 01:01:03 +00003607 default:
3608 break;
3609 }
3610
3611 if (decl_ctx == nullptr && try_parsing_type)
3612 {
3613 Type* type = die.GetDWARF()->ResolveType (die);
3614 if (type)
3615 decl_ctx = GetCachedClangDeclContextForDIE (die);
3616 }
3617
3618 if (decl_ctx)
3619 {
3620 LinkDeclContextToDIE (decl_ctx, die);
3621 return decl_ctx;
3622 }
3623 }
3624 return nullptr;
3625}
3626
Paul Hermand628cbb2015-09-15 23:44:17 +00003627clang::BlockDecl *
3628DWARFASTParserClang::ResolveBlockDIE (const DWARFDIE &die)
3629{
3630 if (die && die.Tag() == DW_TAG_lexical_block)
3631 {
3632 clang::BlockDecl *decl = llvm::cast_or_null<clang::BlockDecl>(m_die_to_decl_ctx[die.GetDIE()]);
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00003633
Paul Hermand628cbb2015-09-15 23:44:17 +00003634 if (!decl)
3635 {
3636 DWARFDIE decl_context_die;
3637 clang::DeclContext *decl_context = GetClangDeclContextContainingDIE(die, &decl_context_die);
3638 decl = m_ast.CreateBlockDeclaration(decl_context);
3639
3640 if (decl)
3641 LinkDeclContextToDIE((clang::DeclContext *)decl, die);
3642 }
3643
3644 return decl;
3645 }
3646 return nullptr;
3647}
3648
Greg Clayton261ac3f2015-08-28 01:01:03 +00003649clang::NamespaceDecl *
3650DWARFASTParserClang::ResolveNamespaceDIE (const DWARFDIE &die)
3651{
3652 if (die && die.Tag() == DW_TAG_namespace)
3653 {
3654 // See if we already parsed this namespace DIE and associated it with a
3655 // uniqued namespace declaration
3656 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die.GetDIE()]);
3657 if (namespace_decl)
3658 return namespace_decl;
3659 else
3660 {
3661 const char *namespace_name = die.GetName();
3662 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (die, nullptr);
3663 namespace_decl = m_ast.GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
3664 Log *log = nullptr;// (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3665 if (log)
3666 {
3667 SymbolFileDWARF *dwarf = die.GetDWARF();
3668 if (namespace_name)
3669 {
3670 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
3671 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
3672 static_cast<void*>(m_ast.getASTContext()),
3673 die.GetID(),
3674 namespace_name,
3675 static_cast<void*>(namespace_decl),
3676 static_cast<void*>(namespace_decl->getOriginalNamespace()));
3677 }
3678 else
3679 {
3680 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
3681 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
3682 static_cast<void*>(m_ast.getASTContext()),
3683 die.GetID(),
3684 static_cast<void*>(namespace_decl),
3685 static_cast<void*>(namespace_decl->getOriginalNamespace()));
3686 }
3687 }
3688
3689 if (namespace_decl)
3690 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
3691 return namespace_decl;
3692 }
3693 }
3694 return nullptr;
3695}
3696
3697clang::DeclContext *
3698DWARFASTParserClang::GetClangDeclContextContainingDIE (const DWARFDIE &die,
3699 DWARFDIE *decl_ctx_die_copy)
3700{
3701 SymbolFileDWARF *dwarf = die.GetDWARF();
3702
3703 DWARFDIE decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE (die);
3704
3705 if (decl_ctx_die_copy)
3706 *decl_ctx_die_copy = decl_ctx_die;
3707
3708 if (decl_ctx_die)
3709 {
3710 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE (decl_ctx_die);
3711 if (clang_decl_ctx)
3712 return clang_decl_ctx;
3713 }
3714 return m_ast.GetTranslationUnitDecl();
3715}
3716
Greg Clayton261ac3f2015-08-28 01:01:03 +00003717clang::DeclContext *
3718DWARFASTParserClang::GetCachedClangDeclContextForDIE (const DWARFDIE &die)
3719{
3720 if (die)
3721 {
3722 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die.GetDIE());
3723 if (pos != m_die_to_decl_ctx.end())
3724 return pos->second;
3725 }
3726 return nullptr;
3727}
3728
3729void
3730DWARFASTParserClang::LinkDeclContextToDIE (clang::DeclContext *decl_ctx, const DWARFDIE &die)
3731{
3732 m_die_to_decl_ctx[die.GetDIE()] = decl_ctx;
3733 // There can be many DIEs for a single decl context
Paul Hermanea188fc2015-09-16 18:48:30 +00003734 //m_decl_ctx_to_die[decl_ctx].insert(die.GetDIE());
3735 m_decl_ctx_to_die.insert(std::make_pair(decl_ctx, die));
Greg Clayton261ac3f2015-08-28 01:01:03 +00003736}
3737
3738bool
3739DWARFASTParserClang::CopyUniqueClassMethodTypes (const DWARFDIE &src_class_die,
3740 const DWARFDIE &dst_class_die,
3741 lldb_private::Type *class_type,
3742 DWARFDIECollection &failures)
3743{
3744 if (!class_type || !src_class_die || !dst_class_die)
3745 return false;
3746 if (src_class_die.Tag() != dst_class_die.Tag())
3747 return false;
3748
3749 // We need to complete the class type so we can get all of the method types
3750 // parsed so we can then unique those types to their equivalent counterparts
3751 // in "dst_cu" and "dst_class_die"
3752 class_type->GetFullCompilerType ();
3753
3754 DWARFDIE src_die;
3755 DWARFDIE dst_die;
3756 UniqueCStringMap<DWARFDIE> src_name_to_die;
3757 UniqueCStringMap<DWARFDIE> dst_name_to_die;
3758 UniqueCStringMap<DWARFDIE> src_name_to_die_artificial;
3759 UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial;
3760 for (src_die = src_class_die.GetFirstChild(); src_die.IsValid(); src_die = src_die.GetSibling())
3761 {
3762 if (src_die.Tag() == DW_TAG_subprogram)
3763 {
3764 // Make sure this is a declaration and not a concrete instance by looking
3765 // for DW_AT_declaration set to 1. Sometimes concrete function instances
3766 // are placed inside the class definitions and shouldn't be included in
3767 // the list of things are are tracking here.
3768 if (src_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1)
3769 {
3770 const char *src_name = src_die.GetMangledName ();
3771 if (src_name)
3772 {
3773 ConstString src_const_name(src_name);
3774 if (src_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3775 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die);
3776 else
3777 src_name_to_die.Append(src_const_name.GetCString(), src_die);
3778 }
3779 }
3780 }
3781 }
3782 for (dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid(); dst_die = dst_die.GetSibling())
3783 {
3784 if (dst_die.Tag() == DW_TAG_subprogram)
3785 {
3786 // Make sure this is a declaration and not a concrete instance by looking
3787 // for DW_AT_declaration set to 1. Sometimes concrete function instances
3788 // are placed inside the class definitions and shouldn't be included in
3789 // the list of things are are tracking here.
3790 if (dst_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1)
3791 {
3792 const char *dst_name = dst_die.GetMangledName ();
3793 if (dst_name)
3794 {
3795 ConstString dst_const_name(dst_name);
3796 if ( dst_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3797 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die);
3798 else
3799 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die);
3800 }
3801 }
3802 }
3803 }
3804 const uint32_t src_size = src_name_to_die.GetSize ();
3805 const uint32_t dst_size = dst_name_to_die.GetSize ();
3806 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
3807
3808 // Is everything kosher so we can go through the members at top speed?
3809 bool fast_path = true;
3810
3811 if (src_size != dst_size)
3812 {
3813 if (src_size != 0 && dst_size != 0)
3814 {
3815 if (log)
3816 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)",
3817 src_class_die.GetOffset(),
3818 dst_class_die.GetOffset(),
3819 src_size,
3820 dst_size);
3821 }
3822
3823 fast_path = false;
3824 }
3825
3826 uint32_t idx;
3827
3828 if (fast_path)
3829 {
3830 for (idx = 0; idx < src_size; ++idx)
3831 {
3832 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
3833 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
3834
3835 if (src_die.Tag() != dst_die.Tag())
3836 {
3837 if (log)
3838 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)",
3839 src_class_die.GetOffset(),
3840 dst_class_die.GetOffset(),
3841 src_die.GetOffset(),
3842 src_die.GetTagAsCString(),
3843 dst_die.GetOffset(),
3844 dst_die.GetTagAsCString());
3845 fast_path = false;
3846 }
3847
3848 const char *src_name = src_die.GetMangledName ();
3849 const char *dst_name = dst_die.GetMangledName ();
3850
3851 // Make sure the names match
3852 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
3853 continue;
3854
3855 if (log)
3856 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)",
3857 src_class_die.GetOffset(),
3858 dst_class_die.GetOffset(),
3859 src_die.GetOffset(),
3860 src_name,
3861 dst_die.GetOffset(),
3862 dst_name);
3863
3864 fast_path = false;
3865 }
3866 }
3867
3868 DWARFASTParserClang *src_dwarf_ast_parser = (DWARFASTParserClang *)src_die.GetDWARFParser();
3869 DWARFASTParserClang *dst_dwarf_ast_parser = (DWARFASTParserClang *)dst_die.GetDWARFParser();
3870
3871 // Now do the work of linking the DeclContexts and Types.
3872 if (fast_path)
3873 {
3874 // We can do this quickly. Just run across the tables index-for-index since
3875 // we know each node has matching names and tags.
3876 for (idx = 0; idx < src_size; ++idx)
3877 {
3878 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
3879 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
3880
3881 clang::DeclContext *src_decl_ctx = src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3882 if (src_decl_ctx)
3883 {
3884 if (log)
3885 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3886 static_cast<void*>(src_decl_ctx),
3887 src_die.GetOffset(), dst_die.GetOffset());
3888 dst_dwarf_ast_parser->LinkDeclContextToDIE (src_decl_ctx, dst_die);
3889 }
3890 else
3891 {
3892 if (log)
3893 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found",
3894 src_die.GetOffset(), dst_die.GetOffset());
3895 }
3896
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003897 Type *src_child_type = dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
Greg Clayton261ac3f2015-08-28 01:01:03 +00003898 if (src_child_type)
3899 {
3900 if (log)
3901 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3902 static_cast<void*>(src_child_type),
3903 src_child_type->GetID(),
3904 src_die.GetOffset(), dst_die.GetOffset());
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003905 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
Greg Clayton261ac3f2015-08-28 01:01:03 +00003906 }
3907 else
3908 {
3909 if (log)
3910 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());
3911 }
3912 }
3913 }
3914 else
3915 {
3916 // We must do this slowly. For each member of the destination, look
3917 // up a member in the source with the same name, check its tag, and
3918 // unique them if everything matches up. Report failures.
3919
3920 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty())
3921 {
3922 src_name_to_die.Sort();
3923
3924 for (idx = 0; idx < dst_size; ++idx)
3925 {
3926 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx);
3927 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3928 src_die = src_name_to_die.Find(dst_name, DWARFDIE());
3929
3930 if (src_die && (src_die.Tag() == dst_die.Tag()))
3931 {
3932 clang::DeclContext *src_decl_ctx = src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3933 if (src_decl_ctx)
3934 {
3935 if (log)
3936 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3937 static_cast<void*>(src_decl_ctx),
3938 src_die.GetOffset(),
3939 dst_die.GetOffset());
3940 dst_dwarf_ast_parser->LinkDeclContextToDIE (src_decl_ctx, dst_die);
3941 }
3942 else
3943 {
3944 if (log)
3945 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());
3946 }
3947
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003948 Type *src_child_type = dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
Greg Clayton261ac3f2015-08-28 01:01:03 +00003949 if (src_child_type)
3950 {
3951 if (log)
3952 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3953 static_cast<void*>(src_child_type),
3954 src_child_type->GetID(),
3955 src_die.GetOffset(),
3956 dst_die.GetOffset());
Tamas Berghammereb882fc2015-09-09 10:20:48 +00003957 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
Greg Clayton261ac3f2015-08-28 01:01:03 +00003958 }
3959 else
3960 {
3961 if (log)
3962 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());
3963 }
3964 }
3965 else
3966 {
3967 if (log)
3968 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die.GetOffset());
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00003969
Greg Clayton261ac3f2015-08-28 01:01:03 +00003970 failures.Append(dst_die);
3971 }
3972 }
3973 }
3974 }
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00003975
Greg Clayton261ac3f2015-08-28 01:01:03 +00003976 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize ();
3977 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize ();
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00003978
Greg Clayton261ac3f2015-08-28 01:01:03 +00003979 if (src_size_artificial && dst_size_artificial)
3980 {
3981 dst_name_to_die_artificial.Sort();
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00003982
Greg Clayton261ac3f2015-08-28 01:01:03 +00003983 for (idx = 0; idx < src_size_artificial; ++idx)
3984 {
3985 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx);
3986 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
3987 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, DWARFDIE());
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00003988
Greg Clayton261ac3f2015-08-28 01:01:03 +00003989 if (dst_die)
3990 {
3991 // Both classes have the artificial types, link them
3992 clang::DeclContext *src_decl_ctx = src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3993 if (src_decl_ctx)
3994 {
3995 if (log)
3996 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3997 static_cast<void*>(src_decl_ctx),
3998 src_die.GetOffset(), dst_die.GetOffset());
3999 dst_dwarf_ast_parser->LinkDeclContextToDIE (src_decl_ctx, dst_die);
4000 }
4001 else
4002 {
4003 if (log)
4004 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());
4005 }
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00004006
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004007 Type *src_child_type = dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
Greg Clayton261ac3f2015-08-28 01:01:03 +00004008 if (src_child_type)
4009 {
4010 if (log)
4011 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
4012 static_cast<void*>(src_child_type),
4013 src_child_type->GetID(),
4014 src_die.GetOffset(), dst_die.GetOffset());
Tamas Berghammereb882fc2015-09-09 10:20:48 +00004015 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
Greg Clayton261ac3f2015-08-28 01:01:03 +00004016 }
4017 else
4018 {
4019 if (log)
4020 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());
4021 }
4022 }
4023 }
4024 }
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00004025
Greg Clayton261ac3f2015-08-28 01:01:03 +00004026 if (dst_size_artificial)
4027 {
4028 for (idx = 0; idx < dst_size_artificial; ++idx)
4029 {
4030 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx);
4031 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
4032 if (log)
4033 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die.GetOffset(), dst_name_artificial);
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00004034
Greg Clayton261ac3f2015-08-28 01:01:03 +00004035 failures.Append(dst_die);
4036 }
4037 }
Ramkumar Ramachandra314b7522015-11-12 14:44:24 +00004038
Greg Clayton261ac3f2015-08-28 01:01:03 +00004039 return (failures.Size() != 0);
4040}
4041
4042
4043bool
4044DWARFASTParserClang::LayoutRecordType(const clang::RecordDecl *record_decl,
4045 uint64_t &bit_size,
4046 uint64_t &alignment,
4047 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
4048 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
4049 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
4050{
4051 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
4052 bool success = false;
4053 base_offsets.clear();
4054 vbase_offsets.clear();
4055 if (pos != m_record_decl_to_layout_map.end())
4056 {
4057 bit_size = pos->second.bit_size;
4058 alignment = pos->second.alignment;
4059 field_offsets.swap(pos->second.field_offsets);
4060 base_offsets.swap (pos->second.base_offsets);
4061 vbase_offsets.swap (pos->second.vbase_offsets);
4062 m_record_decl_to_layout_map.erase(pos);
4063 success = true;
4064 }
4065 else
4066 {
4067 bit_size = 0;
4068 alignment = 0;
4069 field_offsets.clear();
4070 }
4071 return success;
4072}