LLDB mainline
DWARFASTParserClang.cpp
Go to the documentation of this file.
1//===-- DWARFASTParserClang.cpp -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <cstdlib>
10
11#include "DWARFASTParser.h"
12#include "DWARFASTParserClang.h"
13#include "DWARFDebugInfo.h"
14#include "DWARFDeclContext.h"
15#include "DWARFDefines.h"
16#include "SymbolFileDWARF.h"
18#include "SymbolFileDWARFDwo.h"
19#include "UniqueDWARFASTType.h"
20
25#include "lldb/Core/Module.h"
26#include "lldb/Core/Value.h"
27#include "lldb/Host/Host.h"
33#include "lldb/Symbol/TypeMap.h"
36#include "lldb/Utility/Log.h"
38
39#include "clang/AST/CXXInheritance.h"
40#include "clang/AST/DeclCXX.h"
41#include "clang/AST/DeclObjC.h"
42#include "clang/AST/DeclTemplate.h"
43#include "clang/AST/Type.h"
44#include "llvm/Demangle/Demangle.h"
45
46#include <map>
47#include <memory>
48#include <optional>
49#include <vector>
50
51//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
52
53#ifdef ENABLE_DEBUG_PRINTF
54#include <cstdio>
55#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
56#else
57#define DEBUG_PRINTF(fmt, ...)
58#endif
59
60using namespace lldb;
61using namespace lldb_private;
62using namespace lldb_private::dwarf;
64 : m_ast(ast), m_die_to_decl_ctx(), m_decl_ctx_to_die() {}
65
67
68static bool DeclKindIsCXXClass(clang::Decl::Kind decl_kind) {
69 switch (decl_kind) {
70 case clang::Decl::CXXRecord:
71 case clang::Decl::ClassTemplateSpecialization:
72 return true;
73 default:
74 break;
75 }
76 return false;
77}
78
79
82 m_clang_ast_importer_up = std::make_unique<ClangASTImporter>();
83 }
85}
86
87/// Detect a forward declaration that is nested in a DW_TAG_module.
88static bool IsClangModuleFwdDecl(const DWARFDIE &Die) {
89 if (!Die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0))
90 return false;
91 auto Parent = Die.GetParent();
92 while (Parent.IsValid()) {
93 if (Parent.Tag() == DW_TAG_module)
94 return true;
95 Parent = Parent.GetParent();
96 }
97 return false;
98}
99
101 if (die.IsValid()) {
102 DWARFDIE top_module_die;
103 // Now make sure this DIE is scoped in a DW_TAG_module tag and return true
104 // if so
105 for (DWARFDIE parent = die.GetParent(); parent.IsValid();
106 parent = parent.GetParent()) {
107 const dw_tag_t tag = parent.Tag();
108 if (tag == DW_TAG_module)
109 top_module_die = parent;
110 else if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
111 break;
112 }
113
114 return top_module_die;
115 }
116 return DWARFDIE();
117}
118
119static lldb::ModuleSP GetContainingClangModule(const DWARFDIE &die) {
120 if (die.IsValid()) {
121 DWARFDIE clang_module_die = GetContainingClangModuleDIE(die);
122
123 if (clang_module_die) {
124 const char *module_name = clang_module_die.GetName();
125 if (module_name)
126 return die.GetDWARF()->GetExternalModule(
127 lldb_private::ConstString(module_name));
128 }
129 }
130 return lldb::ModuleSP();
131}
132
134 const DWARFDIE &die,
135 Log *log) {
136 ModuleSP clang_module_sp = GetContainingClangModule(die);
137 if (!clang_module_sp)
138 return TypeSP();
139
140 // If this type comes from a Clang module, recursively look in the
141 // DWARF section of the .pcm file in the module cache. Clang
142 // generates DWO skeleton units as breadcrumbs to find them.
143 llvm::SmallVector<CompilerContext, 4> decl_context;
144 die.GetDeclContext(decl_context);
145 TypeMap pcm_types;
146
147 // The type in the Clang module must have the same language as the current CU.
148 LanguageSet languages;
150 llvm::DenseSet<SymbolFile *> searched_symbol_files;
151 clang_module_sp->GetSymbolFile()->FindTypes(decl_context, languages,
152 searched_symbol_files, pcm_types);
153 if (pcm_types.Empty()) {
154 // Since this type is defined in one of the Clang modules imported
155 // by this symbol file, search all of them. Instead of calling
156 // sym_file->FindTypes(), which would return this again, go straight
157 // to the imported modules.
158 auto &sym_file = die.GetCU()->GetSymbolFileDWARF();
159
160 // Well-formed clang modules never form cycles; guard against corrupted
161 // ones by inserting the current file.
162 searched_symbol_files.insert(&sym_file);
163 sym_file.ForEachExternalModule(
164 *sc.comp_unit, searched_symbol_files, [&](Module &module) {
165 module.GetSymbolFile()->FindTypes(decl_context, languages,
166 searched_symbol_files, pcm_types);
167 return pcm_types.GetSize();
168 });
169 }
170
171 if (!pcm_types.GetSize())
172 return TypeSP();
173
174 // We found a real definition for this type in the Clang module, so lets use
175 // it and cache the fact that we found a complete type for this die.
176 TypeSP pcm_type_sp = pcm_types.GetTypeAtIndex(0);
177 if (!pcm_type_sp)
178 return TypeSP();
179
180 lldb_private::CompilerType pcm_type = pcm_type_sp->GetForwardCompilerType();
183
184 if (!type)
185 return TypeSP();
186
187 // Under normal operation pcm_type is a shallow forward declaration
188 // that gets completed later. This is necessary to support cyclic
189 // data structures. If, however, pcm_type is already complete (for
190 // example, because it was loaded for a different target before),
191 // the definition needs to be imported right away, too.
192 // Type::ResolveClangType() effectively ignores the ResolveState
193 // inside type_sp and only looks at IsDefined(), so it never calls
194 // ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(),
195 // which does extra work for Objective-C classes. This would result
196 // in only the forward declaration to be visible.
197 if (pcm_type.IsDefined())
199
201 auto type_sp = dwarf->MakeType(
202 die.GetID(), pcm_type_sp->GetName(), pcm_type_sp->GetByteSize(nullptr),
204 &pcm_type_sp->GetDeclaration(), type, Type::ResolveState::Forward,
206 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
207 clang::TagDecl *tag_decl = TypeSystemClang::GetAsTagDecl(type);
208 if (tag_decl) {
209 LinkDeclContextToDIE(tag_decl, die);
210 } else {
211 clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(die);
212 if (defn_decl_ctx)
213 LinkDeclContextToDIE(defn_decl_ctx, die);
214 }
215
216 return type_sp;
217}
218
221 lldbassert(started && "Unable to start a class type definition.");
223 const clang::TagDecl *td = ClangUtil::GetAsTagDecl(type);
224 auto ts_sp = type.GetTypeSystem();
225 auto ts = ts_sp.dyn_cast_or_null<TypeSystemClang>();
226 if (ts)
228}
229
230/// This function serves a similar purpose as RequireCompleteType above, but it
231/// avoids completing the type if it is not immediately necessary. It only
232/// ensures we _can_ complete the type later.
234 ClangASTImporter &ast_importer,
235 clang::DeclContext *decl_ctx,
236 DWARFDIE die,
237 const char *type_name_cstr) {
238 auto *tag_decl_ctx = clang::dyn_cast<clang::TagDecl>(decl_ctx);
239 if (!tag_decl_ctx)
240 return; // Non-tag context are always ready.
241
242 // We have already completed the type, or we have found its definition and are
243 // ready to complete it later (cf. ParseStructureLikeDIE).
244 if (tag_decl_ctx->isCompleteDefinition() || tag_decl_ctx->isBeingDefined())
245 return;
246
247 // We reach this point of the tag was present in the debug info as a
248 // declaration only. If it was imported from another AST context (in the
249 // gmodules case), we can complete the type by doing a full import.
250
251 // If this type was not imported from an external AST, there's nothing to do.
252 CompilerType type = ast.GetTypeForDecl(tag_decl_ctx);
253 if (type && ast_importer.CanImport(type)) {
254 auto qual_type = ClangUtil::GetQualType(type);
255 if (ast_importer.RequireCompleteType(qual_type))
256 return;
257 die.GetDWARF()->GetObjectFile()->GetModule()->ReportError(
258 "Unable to complete the Decl context for DIE {0} at offset "
259 "{1:x16}.\nPlease file a bug report.",
260 type_name_cstr ? type_name_cstr : "", die.GetOffset());
261 }
262
263 // We don't have a type definition and/or the import failed. We must
264 // forcefully complete the type to avoid crashes.
266}
267
269 DWARFAttributes attributes;
270 size_t num_attributes = die.GetAttributes(attributes);
271 for (size_t i = 0; i < num_attributes; ++i) {
272 dw_attr_t attr = attributes.AttributeAtIndex(i);
273 DWARFFormValue form_value;
274 if (!attributes.ExtractFormValueAtIndex(i, form_value))
275 continue;
276 switch (attr) {
277 case DW_AT_abstract_origin:
278 abstract_origin = form_value;
279 break;
280
281 case DW_AT_accessibility:
283 break;
284
285 case DW_AT_artificial:
286 is_artificial = form_value.Boolean();
287 break;
288
289 case DW_AT_bit_stride:
290 bit_stride = form_value.Unsigned();
291 break;
292
293 case DW_AT_byte_size:
294 byte_size = form_value.Unsigned();
295 break;
296
297 case DW_AT_byte_stride:
298 byte_stride = form_value.Unsigned();
299 break;
300
301 case DW_AT_calling_convention:
302 calling_convention = form_value.Unsigned();
303 break;
304
305 case DW_AT_containing_type:
306 containing_type = form_value;
307 break;
308
309 case DW_AT_decl_file:
310 // die.GetCU() can differ if DW_AT_specification uses DW_FORM_ref_addr.
312 attributes.CompileUnitAtIndex(i)->GetFile(form_value.Unsigned()));
313 break;
314 case DW_AT_decl_line:
315 decl.SetLine(form_value.Unsigned());
316 break;
317 case DW_AT_decl_column:
318 decl.SetColumn(form_value.Unsigned());
319 break;
320
321 case DW_AT_declaration:
322 is_forward_declaration = form_value.Boolean();
323 break;
324
325 case DW_AT_encoding:
326 encoding = form_value.Unsigned();
327 break;
328
329 case DW_AT_enum_class:
330 is_scoped_enum = form_value.Boolean();
331 break;
332
333 case DW_AT_explicit:
334 is_explicit = form_value.Boolean();
335 break;
336
337 case DW_AT_external:
338 if (form_value.Unsigned())
339 storage = clang::SC_Extern;
340 break;
341
342 case DW_AT_inline:
343 is_inline = form_value.Boolean();
344 break;
345
346 case DW_AT_linkage_name:
347 case DW_AT_MIPS_linkage_name:
348 mangled_name = form_value.AsCString();
349 break;
350
351 case DW_AT_name:
352 name.SetCString(form_value.AsCString());
353 break;
354
355 case DW_AT_object_pointer:
356 object_pointer = form_value.Reference();
357 break;
358
359 case DW_AT_signature:
360 signature = form_value;
361 break;
362
363 case DW_AT_specification:
364 specification = form_value;
365 break;
366
367 case DW_AT_type:
368 type = form_value;
369 break;
370
371 case DW_AT_virtuality:
372 is_virtual = form_value.Boolean();
373 break;
374
375 case DW_AT_APPLE_objc_complete_type:
376 is_complete_objc_class = form_value.Signed();
377 break;
378
379 case DW_AT_APPLE_objc_direct:
380 is_objc_direct_call = true;
381 break;
382
383 case DW_AT_APPLE_runtime_class:
384 class_language = (LanguageType)form_value.Signed();
385 break;
386
387 case DW_AT_GNU_vector:
388 is_vector = form_value.Boolean();
389 break;
390 case DW_AT_export_symbols:
391 exports_symbols = form_value.Boolean();
392 break;
393 case DW_AT_rvalue_reference:
394 ref_qual = clang::RQ_RValue;
395 break;
396 case DW_AT_reference:
397 ref_qual = clang::RQ_LValue;
398 break;
399 }
400 }
401}
402
403static std::string GetUnitName(const DWARFDIE &die) {
404 if (DWARFUnit *unit = die.GetCU())
405 return unit->GetAbsolutePath().GetPath();
406 return "<missing DWARF unit path>";
407}
408
410 const DWARFDIE &die,
411 bool *type_is_new_ptr) {
412 if (type_is_new_ptr)
413 *type_is_new_ptr = false;
414
415 if (!die)
416 return nullptr;
417
418 Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
419
421 if (log) {
422 DWARFDIE context_die;
423 clang::DeclContext *context =
424 GetClangDeclContextContainingDIE(die, &context_die);
425
426 dwarf->GetObjectFile()->GetModule()->LogMessage(
427 log,
428 "DWARFASTParserClang::ParseTypeFromDWARF "
429 "(die = {0:x16}, decl_ctx = {1:p} (die "
430 "{2:x16})) {3} name = '{4}')",
431 die.GetOffset(), static_cast<void *>(context), context_die.GetOffset(),
432 die.GetTagAsCString(), die.GetName());
433 }
434
435 Type *type_ptr = dwarf->GetDIEToType().lookup(die.GetDIE());
436 if (type_ptr == DIE_IS_BEING_PARSED)
437 return nullptr;
438 if (type_ptr)
439 return type_ptr->shared_from_this();
440 // Set a bit that lets us know that we are currently parsing this
441 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
442
443 ParsedDWARFTypeAttributes attrs(die);
444
445 if (DWARFDIE signature_die = attrs.signature.Reference()) {
446 if (TypeSP type_sp =
447 ParseTypeFromDWARF(sc, signature_die, type_is_new_ptr)) {
448 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
449 if (clang::DeclContext *decl_ctx =
450 GetCachedClangDeclContextForDIE(signature_die))
451 LinkDeclContextToDIE(decl_ctx, die);
452 return type_sp;
453 }
454 return nullptr;
455 }
456
457 if (type_is_new_ptr)
458 *type_is_new_ptr = true;
459
460 const dw_tag_t tag = die.Tag();
461
462 TypeSP type_sp;
463
464 switch (tag) {
465 case DW_TAG_typedef:
466 case DW_TAG_base_type:
467 case DW_TAG_pointer_type:
468 case DW_TAG_reference_type:
469 case DW_TAG_rvalue_reference_type:
470 case DW_TAG_const_type:
471 case DW_TAG_restrict_type:
472 case DW_TAG_volatile_type:
473 case DW_TAG_atomic_type:
474 case DW_TAG_unspecified_type: {
475 type_sp = ParseTypeModifier(sc, die, attrs);
476 break;
477 }
478
479 case DW_TAG_structure_type:
480 case DW_TAG_union_type:
481 case DW_TAG_class_type: {
482 type_sp = ParseStructureLikeDIE(sc, die, attrs);
483 break;
484 }
485
486 case DW_TAG_enumeration_type: {
487 type_sp = ParseEnum(sc, die, attrs);
488 break;
489 }
490
491 case DW_TAG_inlined_subroutine:
492 case DW_TAG_subprogram:
493 case DW_TAG_subroutine_type: {
494 type_sp = ParseSubroutine(die, attrs);
495 break;
496 }
497 case DW_TAG_array_type: {
498 type_sp = ParseArrayType(die, attrs);
499 break;
500 }
501 case DW_TAG_ptr_to_member_type: {
502 type_sp = ParsePointerToMemberType(die, attrs);
503 break;
504 }
505 default:
506 dwarf->GetObjectFile()->GetModule()->ReportError(
507 "[{0:x16}]: unhandled type tag {1:x4} ({2}), "
508 "please file a bug and "
509 "attach the file at the start of this error message",
510 die.GetOffset(), tag, DW_TAG_value_to_name(tag));
511 break;
512 }
513
514 // TODO: We should consider making the switch above exhaustive to simplify
515 // control flow in ParseTypeFromDWARF. Then, we could simply replace this
516 // return statement with a call to llvm_unreachable.
517 return UpdateSymbolContextScopeForType(sc, die, type_sp);
518}
519
520lldb::TypeSP
522 const DWARFDIE &die,
524 Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
526 const dw_tag_t tag = die.Tag();
527 LanguageType cu_language = SymbolFileDWARF::GetLanguage(*die.GetCU());
528 Type::ResolveState resolve_state = Type::ResolveState::Unresolved;
529 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
530 TypeSP type_sp;
531 CompilerType clang_type;
532
533 if (tag == DW_TAG_typedef) {
534 // DeclContext will be populated when the clang type is materialized in
535 // Type::ResolveCompilerType.
538 GetClangDeclContextContainingDIE(die, nullptr), die,
539 attrs.name.GetCString());
540
541 if (attrs.type.IsValid()) {
542 // Try to parse a typedef from the (DWARF embedded in the) Clang
543 // module file first as modules can contain typedef'ed
544 // structures that have no names like:
545 //
546 // typedef struct { int a; } Foo;
547 //
548 // In this case we will have a structure with no name and a
549 // typedef named "Foo" that points to this unnamed
550 // structure. The name in the typedef is the only identifier for
551 // the struct, so always try to get typedefs from Clang modules
552 // if possible.
553 //
554 // The type_sp returned will be empty if the typedef doesn't
555 // exist in a module file, so it is cheap to call this function
556 // just to check.
557 //
558 // If we don't do this we end up creating a TypeSP that says
559 // this is a typedef to type 0x123 (the DW_AT_type value would
560 // be 0x123 in the DW_TAG_typedef), and this is the unnamed
561 // structure type. We will have a hard time tracking down an
562 // unnammed structure type in the module debug info, so we make
563 // sure we don't get into this situation by always resolving
564 // typedefs from the module.
565 const DWARFDIE encoding_die = attrs.type.Reference();
566
567 // First make sure that the die that this is typedef'ed to _is_
568 // just a declaration (DW_AT_declaration == 1), not a full
569 // definition since template types can't be represented in
570 // modules since only concrete instances of templates are ever
571 // emitted and modules won't contain those
572 if (encoding_die &&
573 encoding_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
574 type_sp = ParseTypeFromClangModule(sc, die, log);
575 if (type_sp)
576 return type_sp;
577 }
578 }
579 }
580
581 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", die.GetID(),
582 DW_TAG_value_to_name(tag), type_name_cstr,
583 encoding_uid.Reference());
584
585 switch (tag) {
586 default:
587 break;
588
589 case DW_TAG_unspecified_type:
590 if (attrs.name == "nullptr_t" || attrs.name == "decltype(nullptr)") {
591 resolve_state = Type::ResolveState::Full;
593 break;
594 }
595 // Fall through to base type below in case we can handle the type
596 // there...
597 [[fallthrough]];
598
599 case DW_TAG_base_type:
600 resolve_state = Type::ResolveState::Full;
602 attrs.name.GetStringRef(), attrs.encoding,
603 attrs.byte_size.value_or(0) * 8);
604 break;
605
606 case DW_TAG_pointer_type:
607 encoding_data_type = Type::eEncodingIsPointerUID;
608 break;
609 case DW_TAG_reference_type:
610 encoding_data_type = Type::eEncodingIsLValueReferenceUID;
611 break;
612 case DW_TAG_rvalue_reference_type:
613 encoding_data_type = Type::eEncodingIsRValueReferenceUID;
614 break;
615 case DW_TAG_typedef:
616 encoding_data_type = Type::eEncodingIsTypedefUID;
617 break;
618 case DW_TAG_const_type:
619 encoding_data_type = Type::eEncodingIsConstUID;
620 break;
621 case DW_TAG_restrict_type:
622 encoding_data_type = Type::eEncodingIsRestrictUID;
623 break;
624 case DW_TAG_volatile_type:
625 encoding_data_type = Type::eEncodingIsVolatileUID;
626 break;
627 case DW_TAG_atomic_type:
628 encoding_data_type = Type::eEncodingIsAtomicUID;
629 break;
630 }
631
632 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID ||
633 encoding_data_type == Type::eEncodingIsTypedefUID)) {
634 if (tag == DW_TAG_pointer_type) {
635 DWARFDIE target_die = die.GetReferencedDIE(DW_AT_type);
636
637 if (target_die.GetAttributeValueAsUnsigned(DW_AT_APPLE_block, 0)) {
638 // Blocks have a __FuncPtr inside them which is a pointer to a
639 // function of the proper type.
640
641 for (DWARFDIE child_die : target_die.children()) {
642 if (!strcmp(child_die.GetAttributeValueAsString(DW_AT_name, ""),
643 "__FuncPtr")) {
644 DWARFDIE function_pointer_type =
645 child_die.GetReferencedDIE(DW_AT_type);
646
647 if (function_pointer_type) {
648 DWARFDIE function_type =
649 function_pointer_type.GetReferencedDIE(DW_AT_type);
650
651 bool function_type_is_new_pointer;
652 TypeSP lldb_function_type_sp = ParseTypeFromDWARF(
653 sc, function_type, &function_type_is_new_pointer);
654
655 if (lldb_function_type_sp) {
656 clang_type = m_ast.CreateBlockPointerType(
657 lldb_function_type_sp->GetForwardCompilerType());
658 encoding_data_type = Type::eEncodingIsUID;
659 attrs.type.Clear();
660 resolve_state = Type::ResolveState::Full;
661 }
662 }
663
664 break;
665 }
666 }
667 }
668 }
669
670 if (cu_language == eLanguageTypeObjC ||
671 cu_language == eLanguageTypeObjC_plus_plus) {
672 if (attrs.name) {
673 if (attrs.name == "id") {
674 if (log)
675 dwarf->GetObjectFile()->GetModule()->LogMessage(
676 log,
677 "SymbolFileDWARF::ParseType (die = {0:x16}) {1} '{2}' "
678 "is Objective-C 'id' built-in type.",
679 die.GetOffset(), die.GetTagAsCString(), die.GetName());
680 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
681 encoding_data_type = Type::eEncodingIsUID;
682 attrs.type.Clear();
683 resolve_state = Type::ResolveState::Full;
684 } else if (attrs.name == "Class") {
685 if (log)
686 dwarf->GetObjectFile()->GetModule()->LogMessage(
687 log,
688 "SymbolFileDWARF::ParseType (die = {0:x16}) {1} '{2}' "
689 "is Objective-C 'Class' built-in type.",
690 die.GetOffset(), die.GetTagAsCString(), die.GetName());
692 encoding_data_type = Type::eEncodingIsUID;
693 attrs.type.Clear();
694 resolve_state = Type::ResolveState::Full;
695 } else if (attrs.name == "SEL") {
696 if (log)
697 dwarf->GetObjectFile()->GetModule()->LogMessage(
698 log,
699 "SymbolFileDWARF::ParseType (die = {0:x16}) {1} '{2}' "
700 "is Objective-C 'selector' built-in type.",
701 die.GetOffset(), die.GetTagAsCString(), die.GetName());
703 encoding_data_type = Type::eEncodingIsUID;
704 attrs.type.Clear();
705 resolve_state = Type::ResolveState::Full;
706 }
707 } else if (encoding_data_type == Type::eEncodingIsPointerUID &&
708 attrs.type.IsValid()) {
709 // Clang sometimes erroneously emits id as objc_object*. In that
710 // case we fix up the type to "id".
711
712 const DWARFDIE encoding_die = attrs.type.Reference();
713
714 if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type) {
715 llvm::StringRef struct_name = encoding_die.GetName();
716 if (struct_name == "objc_object") {
717 if (log)
718 dwarf->GetObjectFile()->GetModule()->LogMessage(
719 log,
720 "SymbolFileDWARF::ParseType (die = {0:x16}) {1} "
721 "'{2}' is 'objc_object*', which we overrode to "
722 "'id'.",
723 die.GetOffset(), die.GetTagAsCString(), die.GetName());
724 clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
725 encoding_data_type = Type::eEncodingIsUID;
726 attrs.type.Clear();
727 resolve_state = Type::ResolveState::Full;
728 }
729 }
730 }
731 }
732 }
733
734 type_sp = dwarf->MakeType(
735 die.GetID(), attrs.name, attrs.byte_size, nullptr,
736 dwarf->GetUID(attrs.type.Reference()), encoding_data_type, &attrs.decl,
737 clang_type, resolve_state, TypePayloadClang(GetOwningClangModule(die)));
738
739 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
740 return type_sp;
741}
742
745 if (llvm::StringRef(die.GetName()).contains("<"))
746 return ConstString();
747
748 TypeSystemClang::TemplateParameterInfos template_param_infos;
749 if (ParseTemplateParameterInfos(die, template_param_infos)) {
750 return ConstString(m_ast.PrintTemplateParams(template_param_infos));
751 }
752 return ConstString();
753}
754
756 const DWARFDIE &die,
758 Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
760 const dw_tag_t tag = die.Tag();
761 TypeSP type_sp;
762
763 if (attrs.is_forward_declaration) {
764 type_sp = ParseTypeFromClangModule(sc, die, log);
765 if (type_sp)
766 return type_sp;
767
768 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die);
769
770 if (!type_sp) {
771 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
772 if (debug_map_symfile) {
773 // We weren't able to find a full declaration in this DWARF,
774 // see if we have a declaration anywhere else...
775 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(die);
776 }
777 }
778
779 if (type_sp) {
780 if (log) {
781 dwarf->GetObjectFile()->GetModule()->LogMessage(
782 log,
783 "SymbolFileDWARF({0:p}) - {1:x16}}: {2} type \"{3}\" is a "
784 "forward declaration, complete type is {4:x8}",
785 static_cast<void *>(this), die.GetOffset(),
787 type_sp->GetID());
788 }
789
790 // We found a real definition for this type elsewhere so lets use
791 // it and cache the fact that we found a complete type for this
792 // die
793 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
794 clang::DeclContext *defn_decl_ctx =
795 GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
796 if (defn_decl_ctx)
797 LinkDeclContextToDIE(defn_decl_ctx, die);
798 return type_sp;
799 }
800 }
801 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
802 DW_TAG_value_to_name(tag), type_name_cstr);
803
804 CompilerType enumerator_clang_type;
805 CompilerType clang_type;
806 clang_type =
807 CompilerType(m_ast.weak_from_this(),
808 dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
809 if (!clang_type) {
810 if (attrs.type.IsValid()) {
811 Type *enumerator_type =
812 dwarf->ResolveTypeUID(attrs.type.Reference(), true);
813 if (enumerator_type)
814 enumerator_clang_type = enumerator_type->GetFullCompilerType();
815 }
816
817 if (!enumerator_clang_type) {
818 if (attrs.byte_size) {
819 enumerator_clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
820 "", DW_ATE_signed, *attrs.byte_size * 8);
821 } else {
822 enumerator_clang_type = m_ast.GetBasicType(eBasicTypeInt);
823 }
824 }
825
826 clang_type = m_ast.CreateEnumerationType(
827 attrs.name.GetStringRef(),
829 GetOwningClangModule(die), attrs.decl, enumerator_clang_type,
830 attrs.is_scoped_enum);
831 } else {
832 enumerator_clang_type = m_ast.GetEnumerationIntegerType(clang_type);
833 }
834
836
837 type_sp = dwarf->MakeType(die.GetID(), attrs.name, attrs.byte_size, nullptr,
838 dwarf->GetUID(attrs.type.Reference()),
839 Type::eEncodingIsUID, &attrs.decl, clang_type,
840 Type::ResolveState::Forward,
842
844 if (die.HasChildren()) {
845 bool is_signed = false;
846 enumerator_clang_type.IsIntegerType(is_signed);
847 ParseChildEnumerators(clang_type, is_signed,
848 type_sp->GetByteSize(nullptr).value_or(0), die);
849 }
851 } else {
852 dwarf->GetObjectFile()->GetModule()->ReportError(
853 "DWARF DIE at {0:x16} named \"{1}\" was not able to start its "
854 "definition.\nPlease file a bug and attach the file at the "
855 "start of this error message",
856 die.GetOffset(), attrs.name.GetCString());
857 }
858 return type_sp;
859}
860
861static clang::CallingConv
863 switch (attrs.calling_convention) {
864 case llvm::dwarf::DW_CC_normal:
865 return clang::CC_C;
866 case llvm::dwarf::DW_CC_BORLAND_stdcall:
867 return clang::CC_X86StdCall;
868 case llvm::dwarf::DW_CC_BORLAND_msfastcall:
869 return clang::CC_X86FastCall;
870 case llvm::dwarf::DW_CC_LLVM_vectorcall:
871 return clang::CC_X86VectorCall;
872 case llvm::dwarf::DW_CC_BORLAND_pascal:
873 return clang::CC_X86Pascal;
874 case llvm::dwarf::DW_CC_LLVM_Win64:
875 return clang::CC_Win64;
876 case llvm::dwarf::DW_CC_LLVM_X86_64SysV:
877 return clang::CC_X86_64SysV;
878 case llvm::dwarf::DW_CC_LLVM_X86RegCall:
879 return clang::CC_X86RegCall;
880 default:
881 break;
882 }
883
884 Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
885 LLDB_LOG(log, "Unsupported DW_AT_calling_convention value: {0}",
886 attrs.calling_convention);
887 // Use the default calling convention as a fallback.
888 return clang::CC_C;
889}
890
893 Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
894
896 const dw_tag_t tag = die.Tag();
897
898 bool is_variadic = false;
899 bool is_static = false;
900 bool has_template_params = false;
901
902 unsigned type_quals = 0;
903
904 std::string object_pointer_name;
905 if (attrs.object_pointer) {
906 const char *object_pointer_name_cstr = attrs.object_pointer.GetName();
907 if (object_pointer_name_cstr)
908 object_pointer_name = object_pointer_name_cstr;
909 }
910
911 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
912 DW_TAG_value_to_name(tag), type_name_cstr);
913
914 CompilerType return_clang_type;
915 Type *func_type = nullptr;
916
917 if (attrs.type.IsValid())
918 func_type = dwarf->ResolveTypeUID(attrs.type.Reference(), true);
919
920 if (func_type)
921 return_clang_type = func_type->GetForwardCompilerType();
922 else
923 return_clang_type = m_ast.GetBasicType(eBasicTypeVoid);
924
925 std::vector<CompilerType> function_param_types;
926 std::vector<clang::ParmVarDecl *> function_param_decls;
927
928 // Parse the function children for the parameters
929
930 DWARFDIE decl_ctx_die;
931 clang::DeclContext *containing_decl_ctx =
932 GetClangDeclContextContainingDIE(die, &decl_ctx_die);
933 const clang::Decl::Kind containing_decl_kind =
934 containing_decl_ctx->getDeclKind();
935
936 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_kind);
937 // Start off static. This will be set to false in
938 // ParseChildParameters(...) if we find a "this" parameters as the
939 // first parameter
940 if (is_cxx_method) {
941 is_static = true;
942 }
943
944 if (die.HasChildren()) {
945 bool skip_artificial = true;
946 ParseChildParameters(containing_decl_ctx, die, skip_artificial, is_static,
947 is_variadic, has_template_params,
948 function_param_types, function_param_decls,
949 type_quals);
950 }
951
952 bool ignore_containing_context = false;
953 // Check for templatized class member functions. If we had any
954 // DW_TAG_template_type_parameter or DW_TAG_template_value_parameter
955 // the DW_TAG_subprogram DIE, then we can't let this become a method in
956 // a class. Why? Because templatized functions are only emitted if one
957 // of the templatized methods is used in the current compile unit and
958 // we will end up with classes that may or may not include these member
959 // functions and this means one class won't match another class
960 // definition and it affects our ability to use a class in the clang
961 // expression parser. So for the greater good, we currently must not
962 // allow any template member functions in a class definition.
963 if (is_cxx_method && has_template_params) {
964 ignore_containing_context = true;
965 is_cxx_method = false;
966 }
967
968 clang::CallingConv calling_convention =
970
971 // clang_type will get the function prototype clang type after this
972 // call
973 CompilerType clang_type =
974 m_ast.CreateFunctionType(return_clang_type, function_param_types.data(),
975 function_param_types.size(), is_variadic,
976 type_quals, calling_convention, attrs.ref_qual);
977
978 if (attrs.name) {
979 bool type_handled = false;
980 if (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) {
981 ObjCLanguage::MethodName objc_method(attrs.name.GetStringRef(), true);
982 if (objc_method.IsValid(true)) {
983 CompilerType class_opaque_type;
984 ConstString class_name(objc_method.GetClassName());
985 if (class_name) {
986 TypeSP complete_objc_class_type_sp(
987 dwarf->FindCompleteObjCDefinitionTypeForDIE(DWARFDIE(),
988 class_name, false));
989
990 if (complete_objc_class_type_sp) {
991 CompilerType type_clang_forward_type =
992 complete_objc_class_type_sp->GetForwardCompilerType();
994 type_clang_forward_type))
995 class_opaque_type = type_clang_forward_type;
996 }
997 }
998
999 if (class_opaque_type) {
1000 // If accessibility isn't set to anything valid, assume public
1001 // for now...
1002 if (attrs.accessibility == eAccessNone)
1004
1005 clang::ObjCMethodDecl *objc_method_decl =
1007 class_opaque_type, attrs.name.GetCString(), clang_type,
1008 attrs.accessibility, attrs.is_artificial, is_variadic,
1009 attrs.is_objc_direct_call);
1010 type_handled = objc_method_decl != nullptr;
1011 if (type_handled) {
1012 LinkDeclContextToDIE(objc_method_decl, die);
1013 m_ast.SetMetadataAsUserID(objc_method_decl, die.GetID());
1014 } else {
1015 dwarf->GetObjectFile()->GetModule()->ReportError(
1016 "[{0:x16}]: invalid Objective-C method {1:x4} ({2}), "
1017 "please file a bug and attach the file at the start of "
1018 "this error message",
1019 die.GetOffset(), tag, DW_TAG_value_to_name(tag));
1020 }
1021 }
1022 } else if (is_cxx_method) {
1023 // Look at the parent of this DIE and see if is is a class or
1024 // struct and see if this is actually a C++ method
1025 Type *class_type = dwarf->ResolveType(decl_ctx_die);
1026 if (class_type) {
1027 if (class_type->GetID() != decl_ctx_die.GetID() ||
1028 IsClangModuleFwdDecl(decl_ctx_die)) {
1029
1030 // We uniqued the parent class of this function to another
1031 // class so we now need to associate all dies under
1032 // "decl_ctx_die" to DIEs in the DIE for "class_type"...
1033 DWARFDIE class_type_die = dwarf->GetDIE(class_type->GetID());
1034
1035 if (class_type_die) {
1036 std::vector<DWARFDIE> failures;
1037
1038 CopyUniqueClassMethodTypes(decl_ctx_die, class_type_die,
1039 class_type, failures);
1040
1041 // FIXME do something with these failures that's
1042 // smarter than just dropping them on the ground.
1043 // Unfortunately classes don't like having stuff added
1044 // to them after their definitions are complete...
1045
1046 Type *type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1047 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
1048 return type_ptr->shared_from_this();
1049 }
1050 }
1051 }
1052
1053 if (attrs.specification.IsValid()) {
1054 // We have a specification which we are going to base our
1055 // function prototype off of, so we need this type to be
1056 // completed so that the m_die_to_decl_ctx for the method in
1057 // the specification has a valid clang decl context.
1058 class_type->GetForwardCompilerType();
1059 // If we have a specification, then the function type should
1060 // have been made with the specification and not with this
1061 // die.
1062 DWARFDIE spec_die = attrs.specification.Reference();
1063 clang::DeclContext *spec_clang_decl_ctx =
1064 GetClangDeclContextForDIE(spec_die);
1065 if (spec_clang_decl_ctx) {
1066 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
1067 } else {
1068 dwarf->GetObjectFile()->GetModule()->ReportWarning(
1069 "{0:x8}: DW_AT_specification({1:x16}"
1070 ") has no decl\n",
1071 die.GetID(), spec_die.GetOffset());
1072 }
1073 type_handled = true;
1074 } else if (attrs.abstract_origin.IsValid()) {
1075 // We have a specification which we are going to base our
1076 // function prototype off of, so we need this type to be
1077 // completed so that the m_die_to_decl_ctx for the method in
1078 // the abstract origin has a valid clang decl context.
1079 class_type->GetForwardCompilerType();
1080
1081 DWARFDIE abs_die = attrs.abstract_origin.Reference();
1082 clang::DeclContext *abs_clang_decl_ctx =
1084 if (abs_clang_decl_ctx) {
1085 LinkDeclContextToDIE(abs_clang_decl_ctx, die);
1086 } else {
1087 dwarf->GetObjectFile()->GetModule()->ReportWarning(
1088 "{0:x8}: DW_AT_abstract_origin({1:x16}"
1089 ") has no decl\n",
1090 die.GetID(), abs_die.GetOffset());
1091 }
1092 type_handled = true;
1093 } else {
1094 CompilerType class_opaque_type =
1095 class_type->GetForwardCompilerType();
1096 if (TypeSystemClang::IsCXXClassType(class_opaque_type)) {
1097 if (class_opaque_type.IsBeingDefined()) {
1098 if (!is_static && !die.HasChildren()) {
1099 // We have a C++ member function with no children (this
1100 // pointer!) and clang will get mad if we try and make
1101 // a function that isn't well formed in the DWARF, so
1102 // we will just skip it...
1103 type_handled = true;
1104 } else {
1105 llvm::PrettyStackTraceFormat stack_trace(
1106 "SymbolFileDWARF::ParseType() is adding a method "
1107 "%s to class %s in DIE 0x%8.8" PRIx64 " from %s",
1108 attrs.name.GetCString(),
1109 class_type->GetName().GetCString(), die.GetID(),
1110 dwarf->GetObjectFile()->GetFileSpec().GetPath().c_str());
1111
1112 const bool is_attr_used = false;
1113 // Neither GCC 4.2 nor clang++ currently set a valid
1114 // accessibility in the DWARF for C++ methods...
1115 // Default to public for now...
1116 if (attrs.accessibility == eAccessNone)
1118
1119 clang::CXXMethodDecl *cxx_method_decl =
1121 class_opaque_type.GetOpaqueQualType(),
1122 attrs.name.GetCString(), attrs.mangled_name,
1123 clang_type, attrs.accessibility, attrs.is_virtual,
1124 is_static, attrs.is_inline, attrs.is_explicit,
1125 is_attr_used, attrs.is_artificial);
1126
1127 type_handled = cxx_method_decl != nullptr;
1128 // Artificial methods are always handled even when we
1129 // don't create a new declaration for them.
1130 type_handled |= attrs.is_artificial;
1131
1132 if (cxx_method_decl) {
1133 LinkDeclContextToDIE(cxx_method_decl, die);
1134
1135 ClangASTMetadata metadata;
1136 metadata.SetUserID(die.GetID());
1137
1138 if (!object_pointer_name.empty()) {
1139 metadata.SetObjectPtrName(object_pointer_name.c_str());
1140 LLDB_LOGF(log,
1141 "Setting object pointer name: %s on method "
1142 "object %p.\n",
1143 object_pointer_name.c_str(),
1144 static_cast<void *>(cxx_method_decl));
1145 }
1146 m_ast.SetMetadata(cxx_method_decl, metadata);
1147 } else {
1148 ignore_containing_context = true;
1149 }
1150 }
1151 } else {
1152 // We were asked to parse the type for a method in a
1153 // class, yet the class hasn't been asked to complete
1154 // itself through the clang::ExternalASTSource protocol,
1155 // so we need to just have the class complete itself and
1156 // do things the right way, then our
1157 // DIE should then have an entry in the
1158 // dwarf->GetDIEToType() map. First
1159 // we need to modify the dwarf->GetDIEToType() so it
1160 // doesn't think we are trying to parse this DIE
1161 // anymore...
1162 dwarf->GetDIEToType()[die.GetDIE()] = NULL;
1163
1164 // Now we get the full type to force our class type to
1165 // complete itself using the clang::ExternalASTSource
1166 // protocol which will parse all base classes and all
1167 // methods (including the method for this DIE).
1168 class_type->GetFullCompilerType();
1169
1170 // The type for this DIE should have been filled in the
1171 // function call above
1172 Type *type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1173 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
1174 return type_ptr->shared_from_this();
1175 }
1176
1177 // FIXME This is fixing some even uglier behavior but we
1178 // really need to
1179 // uniq the methods of each class as well as the class
1180 // itself. <rdar://problem/11240464>
1181 type_handled = true;
1182 }
1183 }
1184 }
1185 }
1186 }
1187 }
1188
1189 if (!type_handled) {
1190 clang::FunctionDecl *function_decl = nullptr;
1191 clang::FunctionDecl *template_function_decl = nullptr;
1192
1193 if (attrs.abstract_origin.IsValid()) {
1194 DWARFDIE abs_die = attrs.abstract_origin.Reference();
1195
1196 if (dwarf->ResolveType(abs_die)) {
1197 function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(
1199
1200 if (function_decl) {
1201 LinkDeclContextToDIE(function_decl, die);
1202 }
1203 }
1204 }
1205
1206 if (!function_decl) {
1207 char *name_buf = nullptr;
1208 llvm::StringRef name = attrs.name.GetStringRef();
1209
1210 // We currently generate function templates with template parameters in
1211 // their name. In order to get closer to the AST that clang generates
1212 // we want to strip these from the name when creating the AST.
1213 if (attrs.mangled_name) {
1214 llvm::ItaniumPartialDemangler D;
1215 if (!D.partialDemangle(attrs.mangled_name)) {
1216 name_buf = D.getFunctionBaseName(nullptr, nullptr);
1217 name = name_buf;
1218 }
1219 }
1220
1221 // We just have a function that isn't part of a class
1222 function_decl = m_ast.CreateFunctionDeclaration(
1223 ignore_containing_context ? m_ast.GetTranslationUnitDecl()
1224 : containing_decl_ctx,
1225 GetOwningClangModule(die), name, clang_type, attrs.storage,
1226 attrs.is_inline);
1227 std::free(name_buf);
1228
1229 if (has_template_params) {
1230 TypeSystemClang::TemplateParameterInfos template_param_infos;
1231 ParseTemplateParameterInfos(die, template_param_infos);
1232 template_function_decl = m_ast.CreateFunctionDeclaration(
1233 ignore_containing_context ? m_ast.GetTranslationUnitDecl()
1234 : containing_decl_ctx,
1235 GetOwningClangModule(die), attrs.name.GetStringRef(), clang_type,
1236 attrs.storage, attrs.is_inline);
1237 clang::FunctionTemplateDecl *func_template_decl =
1239 containing_decl_ctx, GetOwningClangModule(die),
1240 template_function_decl, template_param_infos);
1242 template_function_decl, func_template_decl, template_param_infos);
1243 }
1244
1245 lldbassert(function_decl);
1246
1247 if (function_decl) {
1248 // Attach an asm(<mangled_name>) label to the FunctionDecl.
1249 // This ensures that clang::CodeGen emits function calls
1250 // using symbols that are mangled according to the DW_AT_linkage_name.
1251 // If we didn't do this, the external symbols wouldn't exactly
1252 // match the mangled name LLDB knows about and the IRExecutionUnit
1253 // would have to fall back to searching object files for
1254 // approximately matching function names. The motivating
1255 // example is generating calls to ABI-tagged template functions.
1256 // This is done separately for member functions in
1257 // AddMethodToCXXRecordType.
1258 if (attrs.mangled_name)
1259 function_decl->addAttr(clang::AsmLabelAttr::CreateImplicit(
1260 m_ast.getASTContext(), attrs.mangled_name, /*literal=*/false));
1261
1262 LinkDeclContextToDIE(function_decl, die);
1263
1264 if (!function_param_decls.empty()) {
1265 m_ast.SetFunctionParameters(function_decl, function_param_decls);
1266 if (template_function_decl)
1267 m_ast.SetFunctionParameters(template_function_decl,
1268 function_param_decls);
1269 }
1270
1271 ClangASTMetadata metadata;
1272 metadata.SetUserID(die.GetID());
1273
1274 if (!object_pointer_name.empty()) {
1275 metadata.SetObjectPtrName(object_pointer_name.c_str());
1276 LLDB_LOGF(log,
1277 "Setting object pointer name: %s on function "
1278 "object %p.",
1279 object_pointer_name.c_str(),
1280 static_cast<void *>(function_decl));
1281 }
1282 m_ast.SetMetadata(function_decl, metadata);
1283 }
1284 }
1285 }
1286 }
1287 return dwarf->MakeType(
1288 die.GetID(), attrs.name, std::nullopt, nullptr, LLDB_INVALID_UID,
1289 Type::eEncodingIsUID, &attrs.decl, clang_type, Type::ResolveState::Full);
1290}
1291
1292TypeSP
1294 const ParsedDWARFTypeAttributes &attrs) {
1296
1297 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1298 DW_TAG_value_to_name(tag), type_name_cstr);
1299
1300 DWARFDIE type_die = attrs.type.Reference();
1301 Type *element_type = dwarf->ResolveTypeUID(type_die, true);
1302
1303 if (!element_type)
1304 return nullptr;
1305
1306 std::optional<SymbolFile::ArrayInfo> array_info = ParseChildArrayInfo(die);
1307 uint32_t byte_stride = attrs.byte_stride;
1308 uint32_t bit_stride = attrs.bit_stride;
1309 if (array_info) {
1310 byte_stride = array_info->byte_stride;
1311 bit_stride = array_info->bit_stride;
1312 }
1313 if (byte_stride == 0 && bit_stride == 0)
1314 byte_stride = element_type->GetByteSize(nullptr).value_or(0);
1315 CompilerType array_element_type = element_type->GetForwardCompilerType();
1316 TypeSystemClang::RequireCompleteType(array_element_type);
1317
1318 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
1319 CompilerType clang_type;
1320 if (array_info && array_info->element_orders.size() > 0) {
1321 uint64_t num_elements = 0;
1322 auto end = array_info->element_orders.rend();
1323 for (auto pos = array_info->element_orders.rbegin(); pos != end; ++pos) {
1324 num_elements = *pos;
1325 clang_type = m_ast.CreateArrayType(array_element_type, num_elements,
1326 attrs.is_vector);
1327 array_element_type = clang_type;
1328 array_element_bit_stride = num_elements
1329 ? array_element_bit_stride * num_elements
1330 : array_element_bit_stride;
1331 }
1332 } else {
1333 clang_type =
1334 m_ast.CreateArrayType(array_element_type, 0, attrs.is_vector);
1335 }
1336 ConstString empty_name;
1337 TypeSP type_sp =
1338 dwarf->MakeType(die.GetID(), empty_name, array_element_bit_stride / 8,
1339 nullptr, dwarf->GetUID(type_die), Type::eEncodingIsUID,
1340 &attrs.decl, clang_type, Type::ResolveState::Full);
1341 type_sp->SetEncodingType(element_type);
1342 const clang::Type *type = ClangUtil::GetQualType(clang_type).getTypePtr();
1343 m_ast.SetMetadataAsUserID(type, die.GetID());
1344 return type_sp;
1345}
1346
1348 const DWARFDIE &die, const ParsedDWARFTypeAttributes &attrs) {
1350 Type *pointee_type = dwarf->ResolveTypeUID(attrs.type.Reference(), true);
1351 Type *class_type =
1352 dwarf->ResolveTypeUID(attrs.containing_type.Reference(), true);
1353
1354 CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType();
1355 CompilerType class_clang_type = class_type->GetForwardCompilerType();
1356
1358 class_clang_type, pointee_clang_type);
1359
1360 if (std::optional<uint64_t> clang_type_size =
1361 clang_type.GetByteSize(nullptr)) {
1362 return dwarf->MakeType(die.GetID(), attrs.name, *clang_type_size, nullptr,
1364 clang_type, Type::ResolveState::Forward);
1365 }
1366 return nullptr;
1367}
1368
1370 const DWARFDIE &die, const DWARFDIE &parent_die,
1371 const CompilerType class_clang_type, const AccessType default_accessibility,
1372 const lldb::ModuleSP &module_sp,
1373 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
1374 ClangASTImporter::LayoutInfo &layout_info) {
1375 auto ast =
1376 class_clang_type.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
1377 if (ast == nullptr)
1378 return;
1379
1380 // TODO: implement DW_TAG_inheritance type parsing.
1381 DWARFAttributes attributes;
1382 const size_t num_attributes = die.GetAttributes(attributes);
1383 if (num_attributes == 0)
1384 return;
1385
1386 DWARFFormValue encoding_form;
1387 AccessType accessibility = default_accessibility;
1388 bool is_virtual = false;
1389 bool is_base_of_class = true;
1390 off_t member_byte_offset = 0;
1391
1392 for (uint32_t i = 0; i < num_attributes; ++i) {
1393 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1394 DWARFFormValue form_value;
1395 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1396 switch (attr) {
1397 case DW_AT_type:
1398 encoding_form = form_value;
1399 break;
1400 case DW_AT_data_member_location:
1401 if (form_value.BlockData()) {
1402 Value initialValue(0);
1403 Value memberOffset(0);
1404 const DWARFDataExtractor &debug_info_data = die.GetData();
1405 uint32_t block_length = form_value.Unsigned();
1406 uint32_t block_offset =
1407 form_value.BlockData() - debug_info_data.GetDataStart();
1409 nullptr, nullptr, module_sp,
1410 DataExtractor(debug_info_data, block_offset, block_length),
1411 die.GetCU(), eRegisterKindDWARF, &initialValue, nullptr,
1412 memberOffset, nullptr)) {
1413 member_byte_offset = memberOffset.ResolveValue(nullptr).UInt();
1414 }
1415 } else {
1416 // With DWARF 3 and later, if the value is an integer constant,
1417 // this form value is the offset in bytes from the beginning of
1418 // the containing entity.
1419 member_byte_offset = form_value.Unsigned();
1420 }
1421 break;
1422
1423 case DW_AT_accessibility:
1424 accessibility = DWARFASTParser::GetAccessTypeFromDWARF(form_value.Unsigned());
1425 break;
1426
1427 case DW_AT_virtuality:
1428 is_virtual = form_value.Boolean();
1429 break;
1430
1431 default:
1432 break;
1433 }
1434 }
1435 }
1436
1437 Type *base_class_type = die.ResolveTypeUID(encoding_form.Reference());
1438 if (base_class_type == nullptr) {
1439 module_sp->ReportError("{0:x16}: DW_TAG_inheritance failed to "
1440 "resolve the base class at {1:x16}"
1441 " from enclosing type {2:x16}. \nPlease file "
1442 "a bug and attach the file at the start of "
1443 "this error message",
1444 die.GetOffset(),
1445 encoding_form.Reference().GetOffset(),
1446 parent_die.GetOffset());
1447 return;
1448 }
1449
1450 CompilerType base_class_clang_type = base_class_type->GetFullCompilerType();
1451 assert(base_class_clang_type);
1452 if (TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type)) {
1453 ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
1454 return;
1455 }
1456 std::unique_ptr<clang::CXXBaseSpecifier> result =
1457 ast->CreateBaseClassSpecifier(base_class_clang_type.GetOpaqueQualType(),
1458 accessibility, is_virtual,
1459 is_base_of_class);
1460 if (!result)
1461 return;
1462
1463 base_classes.push_back(std::move(result));
1464
1465 if (is_virtual) {
1466 // Do not specify any offset for virtual inheritance. The DWARF
1467 // produced by clang doesn't give us a constant offset, but gives
1468 // us a DWARF expressions that requires an actual object in memory.
1469 // the DW_AT_data_member_location for a virtual base class looks
1470 // like:
1471 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref,
1472 // DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref,
1473 // DW_OP_plus )
1474 // Given this, there is really no valid response we can give to
1475 // clang for virtual base class offsets, and this should eventually
1476 // be removed from LayoutRecordType() in the external
1477 // AST source in clang.
1478 } else {
1479 layout_info.base_offsets.insert(std::make_pair(
1480 ast->GetAsCXXRecordDecl(base_class_clang_type.GetOpaqueQualType()),
1481 clang::CharUnits::fromQuantity(member_byte_offset)));
1482 }
1483}
1484
1486 const SymbolContext &sc, const DWARFDIE &die, TypeSP type_sp) {
1487 if (!type_sp)
1488 return type_sp;
1489
1492 dw_tag_t sc_parent_tag = sc_parent_die.Tag();
1493
1494 SymbolContextScope *symbol_context_scope = nullptr;
1495 if (sc_parent_tag == DW_TAG_compile_unit ||
1496 sc_parent_tag == DW_TAG_partial_unit) {
1497 symbol_context_scope = sc.comp_unit;
1498 } else if (sc.function != nullptr && sc_parent_die) {
1499 symbol_context_scope =
1500 sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
1501 if (symbol_context_scope == nullptr)
1502 symbol_context_scope = sc.function;
1503 } else {
1504 symbol_context_scope = sc.module_sp.get();
1505 }
1506
1507 if (symbol_context_scope != nullptr)
1508 type_sp->SetSymbolContextScope(symbol_context_scope);
1509
1510 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1511 return type_sp;
1512}
1513
1514std::string
1516 if (!die.IsValid())
1517 return "";
1518 const char *name = die.GetName();
1519 if (!name)
1520 return "";
1521 std::string qualified_name;
1522 DWARFDIE parent_decl_ctx_die = die.GetParentDeclContextDIE();
1523 // TODO: change this to get the correct decl context parent....
1524 while (parent_decl_ctx_die) {
1525 // The name may not contain template parameters due to
1526 // -gsimple-template-names; we must reconstruct the full name from child
1527 // template parameter dies via GetDIEClassTemplateParams().
1528 const dw_tag_t parent_tag = parent_decl_ctx_die.Tag();
1529 switch (parent_tag) {
1530 case DW_TAG_namespace: {
1531 if (const char *namespace_name = parent_decl_ctx_die.GetName()) {
1532 qualified_name.insert(0, "::");
1533 qualified_name.insert(0, namespace_name);
1534 } else {
1535 qualified_name.insert(0, "(anonymous namespace)::");
1536 }
1537 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1538 break;
1539 }
1540
1541 case DW_TAG_class_type:
1542 case DW_TAG_structure_type:
1543 case DW_TAG_union_type: {
1544 if (const char *class_union_struct_name = parent_decl_ctx_die.GetName()) {
1545 qualified_name.insert(
1546 0, GetDIEClassTemplateParams(parent_decl_ctx_die).AsCString(""));
1547 qualified_name.insert(0, "::");
1548 qualified_name.insert(0, class_union_struct_name);
1549 }
1550 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE();
1551 break;
1552 }
1553
1554 default:
1555 parent_decl_ctx_die.Clear();
1556 break;
1557 }
1558 }
1559
1560 if (qualified_name.empty())
1561 qualified_name.append("::");
1562
1563 qualified_name.append(name);
1564 qualified_name.append(GetDIEClassTemplateParams(die).AsCString(""));
1565
1566 return qualified_name;
1567}
1568
1569TypeSP
1571 const DWARFDIE &die,
1573 TypeSP type_sp;
1574 CompilerType clang_type;
1575 const dw_tag_t tag = die.Tag();
1577 LanguageType cu_language = SymbolFileDWARF::GetLanguage(*die.GetCU());
1578 Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
1579
1580 // UniqueDWARFASTType is large, so don't create a local variables on the
1581 // stack, put it on the heap. This function is often called recursively and
1582 // clang isn't good at sharing the stack space for variables in different
1583 // blocks.
1584 auto unique_ast_entry_up = std::make_unique<UniqueDWARFASTType>();
1585
1586 ConstString unique_typename(attrs.name);
1587 Declaration unique_decl(attrs.decl);
1588
1589 if (attrs.name) {
1590 if (Language::LanguageIsCPlusPlus(cu_language)) {
1591 // For C++, we rely solely upon the one definition rule that says
1592 // only one thing can exist at a given decl context. We ignore the
1593 // file and line that things are declared on.
1594 std::string qualified_name = GetCPlusPlusQualifiedName(die);
1595 if (!qualified_name.empty())
1596 unique_typename = ConstString(qualified_name);
1597 unique_decl.Clear();
1598 }
1599
1600 if (dwarf->GetUniqueDWARFASTTypeMap().Find(
1601 unique_typename, die, unique_decl, attrs.byte_size.value_or(-1),
1602 *unique_ast_entry_up)) {
1603 type_sp = unique_ast_entry_up->m_type_sp;
1604 if (type_sp) {
1605 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1607 GetCachedClangDeclContextForDIE(unique_ast_entry_up->m_die), die);
1608 return type_sp;
1609 }
1610 }
1611 }
1612
1613 DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1614 DW_TAG_value_to_name(tag), type_name_cstr);
1615
1616 int tag_decl_kind = -1;
1617 AccessType default_accessibility = eAccessNone;
1618 if (tag == DW_TAG_structure_type) {
1619 tag_decl_kind = clang::TTK_Struct;
1620 default_accessibility = eAccessPublic;
1621 } else if (tag == DW_TAG_union_type) {
1622 tag_decl_kind = clang::TTK_Union;
1623 default_accessibility = eAccessPublic;
1624 } else if (tag == DW_TAG_class_type) {
1625 tag_decl_kind = clang::TTK_Class;
1626 default_accessibility = eAccessPrivate;
1627 }
1628
1629 if (attrs.byte_size && *attrs.byte_size == 0 && attrs.name &&
1630 !die.HasChildren() && cu_language == eLanguageTypeObjC) {
1631 // Work around an issue with clang at the moment where forward
1632 // declarations for objective C classes are emitted as:
1633 // DW_TAG_structure_type [2]
1634 // DW_AT_name( "ForwardObjcClass" )
1635 // DW_AT_byte_size( 0x00 )
1636 // DW_AT_decl_file( "..." )
1637 // DW_AT_decl_line( 1 )
1638 //
1639 // Note that there is no DW_AT_declaration and there are no children,
1640 // and the byte size is zero.
1641 attrs.is_forward_declaration = true;
1642 }
1643
1644 if (attrs.class_language == eLanguageTypeObjC ||
1646 if (!attrs.is_complete_objc_class &&
1648 // We have a valid eSymbolTypeObjCClass class symbol whose name
1649 // matches the current objective C class that we are trying to find
1650 // and this DIE isn't the complete definition (we checked
1651 // is_complete_objc_class above and know it is false), so the real
1652 // definition is in here somewhere
1653 type_sp =
1654 dwarf->FindCompleteObjCDefinitionTypeForDIE(die, attrs.name, true);
1655
1656 if (!type_sp) {
1657 SymbolFileDWARFDebugMap *debug_map_symfile =
1658 dwarf->GetDebugMapSymfile();
1659 if (debug_map_symfile) {
1660 // We weren't able to find a full declaration in this DWARF,
1661 // see if we have a declaration anywhere else...
1662 type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE(
1663 die, attrs.name, true);
1664 }
1665 }
1666
1667 if (type_sp) {
1668 if (log) {
1669 dwarf->GetObjectFile()->GetModule()->LogMessage(
1670 log,
1671 "SymbolFileDWARF({0:p}) - {1:x16}: {2} type "
1672 "\"{3}\" is an "
1673 "incomplete objc type, complete type is {4:x8}",
1674 static_cast<void *>(this), die.GetOffset(),
1675 DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1676 type_sp->GetID());
1677 }
1678
1679 // We found a real definition for this type elsewhere so lets use
1680 // it and cache the fact that we found a complete type for this
1681 // die
1682 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1683 return type_sp;
1684 }
1685 }
1686 }
1687
1688 if (attrs.is_forward_declaration) {
1689 // We have a forward declaration to a type and we need to try and
1690 // find a full declaration. We look in the current type index just in
1691 // case we have a forward declaration followed by an actual
1692 // declarations in the DWARF. If this fails, we need to look
1693 // elsewhere...
1694 if (log) {
1695 dwarf->GetObjectFile()->GetModule()->LogMessage(
1696 log,
1697 "SymbolFileDWARF({0:p}) - {1:x16}: {2} type \"{3}\" is a "
1698 "forward declaration, trying to find complete type",
1699 static_cast<void *>(this), die.GetOffset(), DW_TAG_value_to_name(tag),
1700 attrs.name.GetCString());
1701 }
1702
1703 // See if the type comes from a Clang module and if so, track down
1704 // that type.
1705 type_sp = ParseTypeFromClangModule(sc, die, log);
1706 if (type_sp)
1707 return type_sp;
1708
1709 // type_sp = FindDefinitionTypeForDIE (dwarf_cu, die,
1710 // type_name_const_str);
1711 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die);
1712
1713 if (!type_sp) {
1714 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
1715 if (debug_map_symfile) {
1716 // We weren't able to find a full declaration in this DWARF, see
1717 // if we have a declaration anywhere else...
1718 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(die);
1719 }
1720 }
1721
1722 if (type_sp) {
1723 if (log) {
1724 dwarf->GetObjectFile()->GetModule()->LogMessage(
1725 log,
1726 "SymbolFileDWARF({0:p}) - {1:x16}: {2} type \"{3}\" is a "
1727 "forward declaration, complete type is {4:x8}",
1728 static_cast<void *>(this), die.GetOffset(),
1729 DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1730 type_sp->GetID());
1731 }
1732
1733 // We found a real definition for this type elsewhere so lets use
1734 // it and cache the fact that we found a complete type for this die
1735 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1736 clang::DeclContext *defn_decl_ctx =
1737 GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
1738 if (defn_decl_ctx)
1739 LinkDeclContextToDIE(defn_decl_ctx, die);
1740 return type_sp;
1741 }
1742 }
1743 assert(tag_decl_kind != -1);
1744 (void)tag_decl_kind;
1745 bool clang_type_was_created = false;
1746 clang_type =
1747 CompilerType(m_ast.weak_from_this(),
1748 dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
1749 if (!clang_type) {
1750 clang::DeclContext *decl_ctx =
1752
1754 attrs.name.GetCString());
1755
1756 if (attrs.accessibility == eAccessNone && decl_ctx) {
1757 // Check the decl context that contains this class/struct/union. If
1758 // it is a class we must give it an accessibility.
1759 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
1760 if (DeclKindIsCXXClass(containing_decl_kind))
1761 attrs.accessibility = default_accessibility;
1762 }
1763
1764 ClangASTMetadata metadata;
1765 metadata.SetUserID(die.GetID());
1766 metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual(die));
1767
1768 TypeSystemClang::TemplateParameterInfos template_param_infos;
1769 if (ParseTemplateParameterInfos(die, template_param_infos)) {
1770 clang::ClassTemplateDecl *class_template_decl =
1772 decl_ctx, GetOwningClangModule(die), attrs.accessibility,
1773 attrs.name.GetCString(), tag_decl_kind, template_param_infos);
1774 if (!class_template_decl) {
1775 if (log) {
1776 dwarf->GetObjectFile()->GetModule()->LogMessage(
1777 log,
1778 "SymbolFileDWARF({0:p}) - {1:x16}: {2} type \"{3}\" "
1779 "clang::ClassTemplateDecl failed to return a decl.",
1780 static_cast<void *>(this), die.GetOffset(),
1781 DW_TAG_value_to_name(tag), attrs.name.GetCString());
1782 }
1783 return TypeSP();
1784 }
1785
1786 clang::ClassTemplateSpecializationDecl *class_specialization_decl =
1788 decl_ctx, GetOwningClangModule(die), class_template_decl,
1789 tag_decl_kind, template_param_infos);
1791 class_specialization_decl);
1792 clang_type_was_created = true;
1793
1794 m_ast.SetMetadata(class_template_decl, metadata);
1795 m_ast.SetMetadata(class_specialization_decl, metadata);
1796 }
1797
1798 if (!clang_type_was_created) {
1799 clang_type_was_created = true;
1800 clang_type = m_ast.CreateRecordType(
1801 decl_ctx, GetOwningClangModule(die), attrs.accessibility,
1802 attrs.name.GetCString(), tag_decl_kind, attrs.class_language,
1803 &metadata, attrs.exports_symbols);
1804 }
1805 }
1806
1807 // Store a forward declaration to this class type in case any
1808 // parameters in any class methods need it for the clang types for
1809 // function prototypes.
1811 type_sp = dwarf->MakeType(
1812 die.GetID(), attrs.name, attrs.byte_size, nullptr, LLDB_INVALID_UID,
1813 Type::eEncodingIsUID, &attrs.decl, clang_type,
1814 Type::ResolveState::Forward,
1816
1817 // Add our type to the unique type map so we don't end up creating many
1818 // copies of the same type over and over in the ASTContext for our
1819 // module
1820 unique_ast_entry_up->m_type_sp = type_sp;
1821 unique_ast_entry_up->m_die = die;
1822 unique_ast_entry_up->m_declaration = unique_decl;
1823 unique_ast_entry_up->m_byte_size = attrs.byte_size.value_or(0);
1824 dwarf->GetUniqueDWARFASTTypeMap().Insert(unique_typename,
1825 *unique_ast_entry_up);
1826
1827 if (!attrs.is_forward_declaration) {
1828 // Always start the definition for a class type so that if the class
1829 // has child classes or types that require the class to be created
1830 // for use as their decl contexts the class will be ready to accept
1831 // these child definitions.
1832 if (!die.HasChildren()) {
1833 // No children for this struct/union/class, lets finish it
1836 } else {
1837 dwarf->GetObjectFile()->GetModule()->ReportError(
1838
1839 "DWARF DIE at {0:x16} named \"{1}\" was not able to start "
1840 "its "
1841 "definition.\nPlease file a bug and attach the file at the "
1842 "start of this error message",
1843 die.GetOffset(), attrs.name.GetCString());
1844 }
1845
1846 // If the byte size of the record is specified then overwrite the size
1847 // that would be computed by Clang. This is only needed as LLDB's
1848 // TypeSystemClang is always in C++ mode, but some compilers such as
1849 // GCC and Clang give empty structs a size of 0 in C mode (in contrast to
1850 // the size of 1 for empty structs that would be computed in C++ mode).
1851 if (attrs.byte_size) {
1852 clang::RecordDecl *record_decl =
1854 if (record_decl) {
1856 layout.bit_size = *attrs.byte_size * 8;
1857 GetClangASTImporter().SetRecordLayout(record_decl, layout);
1858 }
1859 }
1860 } else if (clang_type_was_created) {
1861 // Start the definition if the class is not objective C since the
1862 // underlying decls respond to isCompleteDefinition(). Objective
1863 // C decls don't respond to isCompleteDefinition() so we can't
1864 // start the declaration definition right away. For C++
1865 // class/union/structs we want to start the definition in case the
1866 // class is needed as the declaration context for a contained class
1867 // or type without the need to complete that type..
1868
1869 if (attrs.class_language != eLanguageTypeObjC &&
1872
1873 // Leave this as a forward declaration until we need to know the
1874 // details of the type. lldb_private::Type will automatically call
1875 // the SymbolFile virtual function
1876 // "SymbolFileDWARF::CompleteType(Type *)" When the definition
1877 // needs to be defined.
1878 assert(!dwarf->GetForwardDeclClangTypeToDie().count(
1880 .GetOpaqueQualType()) &&
1881 "Type already in the forward declaration map!");
1882 // Can't assume m_ast.GetSymbolFile() is actually a
1883 // SymbolFileDWARF, it can be a SymbolFileDWARFDebugMap for Apple
1884 // binaries.
1885 dwarf->GetForwardDeclDieToClangType()[die.GetDIE()] =
1886 clang_type.GetOpaqueQualType();
1887 dwarf->GetForwardDeclClangTypeToDie().try_emplace(
1888 ClangUtil::RemoveFastQualifiers(clang_type).GetOpaqueQualType(),
1889 *die.GetDIERef());
1890 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
1891 }
1892 }
1893
1894 // If we made a clang type, set the trivial abi if applicable: We only
1895 // do this for pass by value - which implies the Trivial ABI. There
1896 // isn't a way to assert that something that would normally be pass by
1897 // value is pass by reference, so we ignore that attribute if set.
1898 if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_value) {
1899 clang::CXXRecordDecl *record_decl =
1901 if (record_decl && record_decl->getDefinition()) {
1902 record_decl->setHasTrivialSpecialMemberForCall();
1903 }
1904 }
1905
1906 if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_reference) {
1907 clang::CXXRecordDecl *record_decl =
1909 if (record_decl)
1910 record_decl->setArgPassingRestrictions(
1911 clang::RecordDecl::APK_CannotPassInRegs);
1912 }
1913 return type_sp;
1914}
1915
1916// DWARF parsing functions
1917
1919public:
1921 const CompilerType &class_opaque_type, const char *property_name,
1922 const CompilerType &property_opaque_type, // The property type is only
1923 // required if you don't have an
1924 // ivar decl
1925 const char *property_setter_name, const char *property_getter_name,
1926 uint32_t property_attributes, const ClangASTMetadata *metadata)
1927 : m_class_opaque_type(class_opaque_type), m_property_name(property_name),
1928 m_property_opaque_type(property_opaque_type),
1929 m_property_setter_name(property_setter_name),
1930 m_property_getter_name(property_getter_name),
1931 m_property_attributes(property_attributes) {
1932 if (metadata != nullptr) {
1933 m_metadata_up = std::make_unique<ClangASTMetadata>();
1934 *m_metadata_up = *metadata;
1935 }
1936 }
1937
1939 *this = rhs;
1940 }
1941
1950
1951 if (rhs.m_metadata_up) {
1952 m_metadata_up = std::make_unique<ClangASTMetadata>();
1954 }
1955 return *this;
1956 }
1957
1958 bool Finalize() {
1961 /*ivar_decl=*/nullptr, m_property_setter_name, m_property_getter_name,
1963 }
1964
1965private:
1967 const char *m_property_name;
1972 std::unique_ptr<ClangASTMetadata> m_metadata_up;
1973};
1974
1976 const DWARFDIE &die,
1977 TypeSystemClang::TemplateParameterInfos &template_param_infos) {
1978 const dw_tag_t tag = die.Tag();
1979 bool is_template_template_argument = false;
1980
1981 switch (tag) {
1982 case DW_TAG_GNU_template_parameter_pack: {
1983 template_param_infos.SetParameterPack(
1984 std::make_unique<TypeSystemClang::TemplateParameterInfos>());
1985 for (DWARFDIE child_die : die.children()) {
1986 if (!ParseTemplateDIE(child_die, template_param_infos.GetParameterPack()))
1987 return false;
1988 }
1989 if (const char *name = die.GetName()) {
1990 template_param_infos.SetPackName(name);
1991 }
1992 return true;
1993 }
1994 case DW_TAG_GNU_template_template_param:
1995 is_template_template_argument = true;
1996 [[fallthrough]];
1997 case DW_TAG_template_type_parameter:
1998 case DW_TAG_template_value_parameter: {
1999 DWARFAttributes attributes;
2000 const size_t num_attributes = die.GetAttributes(attributes);
2001 const char *name = nullptr;
2002 const char *template_name = nullptr;
2003 CompilerType clang_type;
2004 uint64_t uval64 = 0;
2005 bool uval64_valid = false;
2006 bool is_default_template_arg = false;
2007 if (num_attributes > 0) {
2008 DWARFFormValue form_value;
2009 for (size_t i = 0; i < num_attributes; ++i) {
2010 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2011
2012 switch (attr) {
2013 case DW_AT_name:
2014 if (attributes.ExtractFormValueAtIndex(i, form_value))
2015 name = form_value.AsCString();
2016 break;
2017
2018 case DW_AT_GNU_template_name:
2019 if (attributes.ExtractFormValueAtIndex(i, form_value))
2020 template_name = form_value.AsCString();
2021 break;
2022
2023 case DW_AT_type:
2024 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2025 Type *lldb_type = die.ResolveTypeUID(form_value.Reference());
2026 if (lldb_type)
2027 clang_type = lldb_type->GetForwardCompilerType();
2028 }
2029 break;
2030
2031 case DW_AT_const_value:
2032 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2033 uval64_valid = true;
2034 uval64 = form_value.Unsigned();
2035 }
2036 break;
2037 case DW_AT_default_value:
2038 if (attributes.ExtractFormValueAtIndex(i, form_value))
2039 is_default_template_arg = form_value.Boolean();
2040 break;
2041 default:
2042 break;
2043 }
2044 }
2045
2046 clang::ASTContext &ast = m_ast.getASTContext();
2047 if (!clang_type)
2048 clang_type = m_ast.GetBasicType(eBasicTypeVoid);
2049
2050 if (!is_template_template_argument) {
2051 bool is_signed = false;
2052 // Get the signed value for any integer or enumeration if available
2053 clang_type.IsIntegerOrEnumerationType(is_signed);
2054
2055 if (name && !name[0])
2056 name = nullptr;
2057
2058 if (tag == DW_TAG_template_value_parameter && uval64_valid) {
2059 std::optional<uint64_t> size = clang_type.GetBitSize(nullptr);
2060 if (!size)
2061 return false;
2062 llvm::APInt apint(*size, uval64, is_signed);
2063 template_param_infos.InsertArg(
2064 name,
2065 clang::TemplateArgument(ast, llvm::APSInt(apint, !is_signed),
2066 ClangUtil::GetQualType(clang_type),
2067 is_default_template_arg));
2068 } else {
2069 template_param_infos.InsertArg(
2070 name, clang::TemplateArgument(ClangUtil::GetQualType(clang_type),
2071 /*isNullPtr*/ false,
2072 is_default_template_arg));
2073 }
2074 } else {
2075 auto *tplt_type = m_ast.CreateTemplateTemplateParmDecl(template_name);
2076 template_param_infos.InsertArg(
2077 name, clang::TemplateArgument(clang::TemplateName(tplt_type),
2078 is_default_template_arg));
2079 }
2080 }
2081 }
2082 return true;
2083
2084 default:
2085 break;
2086 }
2087 return false;
2088}
2089
2091 const DWARFDIE &parent_die,
2092 TypeSystemClang::TemplateParameterInfos &template_param_infos) {
2093
2094 if (!parent_die)
2095 return false;
2096
2097 for (DWARFDIE die : parent_die.children()) {
2098 const dw_tag_t tag = die.Tag();
2099
2100 switch (tag) {
2101 case DW_TAG_template_type_parameter:
2102 case DW_TAG_template_value_parameter:
2103 case DW_TAG_GNU_template_parameter_pack:
2104 case DW_TAG_GNU_template_template_param:
2105 ParseTemplateDIE(die, template_param_infos);
2106 break;
2107
2108 default:
2109 break;
2110 }
2111 }
2112
2113 return !template_param_infos.IsEmpty() ||
2114 template_param_infos.hasParameterPack();
2115}
2116
2118 lldb_private::Type *type,
2119 CompilerType &clang_type) {
2120 const dw_tag_t tag = die.Tag();
2122
2123 ClangASTImporter::LayoutInfo layout_info;
2124
2125 if (die.HasChildren()) {
2126 const bool type_is_objc_object_or_interface =
2128 if (type_is_objc_object_or_interface) {
2129 // For objective C we don't start the definition when the class is
2130 // created.
2132 }
2133
2134 AccessType default_accessibility = eAccessNone;
2135 if (tag == DW_TAG_structure_type) {
2136 default_accessibility = eAccessPublic;
2137 } else if (tag == DW_TAG_union_type) {
2138 default_accessibility = eAccessPublic;
2139 } else if (tag == DW_TAG_class_type) {
2140 default_accessibility = eAccessPrivate;
2141 }
2142
2143 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases;
2144 // Parse members and base classes first
2145 std::vector<DWARFDIE> member_function_dies;
2146
2147 DelayedPropertyList delayed_properties;
2148 ParseChildMembers(die, clang_type, bases, member_function_dies,
2149 delayed_properties, default_accessibility, layout_info);
2150
2151 // Now parse any methods if there were any...
2152 for (const DWARFDIE &die : member_function_dies)
2153 dwarf->ResolveType(die);
2154
2155 if (type_is_objc_object_or_interface) {
2156 ConstString class_name(clang_type.GetTypeName());
2157 if (class_name) {
2158 dwarf->GetObjCMethods(class_name, [&](DWARFDIE method_die) {
2159 method_die.ResolveType();
2160 return true;
2161 });
2162
2163 for (DelayedAddObjCClassProperty &property : delayed_properties)
2164 property.Finalize();
2165 }
2166 }
2167
2168 if (!bases.empty()) {
2169 // Make sure all base classes refer to complete types and not forward
2170 // declarations. If we don't do this, clang will crash with an
2171 // assertion in the call to clang_type.TransferBaseClasses()
2172 for (const auto &base_class : bases) {
2173 clang::TypeSourceInfo *type_source_info =
2174 base_class->getTypeSourceInfo();
2175 if (type_source_info)
2177 m_ast.GetType(type_source_info->getType()));
2178 }
2179
2181 std::move(bases));
2182 }
2183 }
2184
2188
2189 if (!layout_info.field_offsets.empty() || !layout_info.base_offsets.empty() ||
2190 !layout_info.vbase_offsets.empty()) {
2191 if (type)
2192 layout_info.bit_size = type->GetByteSize(nullptr).value_or(0) * 8;
2193 if (layout_info.bit_size == 0)
2194 layout_info.bit_size =
2195 die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2196
2197 clang::CXXRecordDecl *record_decl =
2199 if (record_decl)
2200 GetClangASTImporter().SetRecordLayout(record_decl, layout_info);
2201 }
2202
2203 return (bool)clang_type;
2204}
2205
2207 lldb_private::Type *type,
2208 CompilerType &clang_type) {
2210 if (die.HasChildren()) {
2211 bool is_signed = false;
2212 clang_type.IsIntegerType(is_signed);
2213 ParseChildEnumerators(clang_type, is_signed,
2214 type->GetByteSize(nullptr).value_or(0), die);
2215 }
2217 }
2218 return (bool)clang_type;
2219}
2220
2222 lldb_private::Type *type,
2223 CompilerType &clang_type) {
2225
2226 std::lock_guard<std::recursive_mutex> guard(
2227 dwarf->GetObjectFile()->GetModule()->GetMutex());
2228
2229 // Disable external storage for this type so we don't get anymore
2230 // clang::ExternalASTSource queries for this type.
2231 m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), false);
2232
2233 if (!die)
2234 return false;
2235
2236 const dw_tag_t tag = die.Tag();
2237
2238 assert(clang_type);
2239 DWARFAttributes attributes;
2240 switch (tag) {
2241 case DW_TAG_structure_type:
2242 case DW_TAG_union_type:
2243 case DW_TAG_class_type:
2244 return CompleteRecordType(die, type, clang_type);
2245 case DW_TAG_enumeration_type:
2246 return CompleteEnumType(die, type, clang_type);
2247 default:
2248 assert(false && "not a forward clang type decl!");
2249 break;
2250 }
2251
2252 return false;
2253}
2254
2256 lldb_private::CompilerDeclContext decl_context) {
2257 auto opaque_decl_ctx =
2258 (clang::DeclContext *)decl_context.GetOpaqueDeclContext();
2259 for (auto it = m_decl_ctx_to_die.find(opaque_decl_ctx);
2260 it != m_decl_ctx_to_die.end() && it->first == opaque_decl_ctx;
2261 it = m_decl_ctx_to_die.erase(it))
2262 for (DWARFDIE decl : it->second.children())
2263 GetClangDeclForDIE(decl);
2264}
2265
2267 clang::Decl *clang_decl = GetClangDeclForDIE(die);
2268 if (clang_decl != nullptr)
2269 return m_ast.GetCompilerDecl(clang_decl);
2270 return CompilerDecl();
2271}
2272
2275 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die);
2276 if (clang_decl_ctx)
2277 return m_ast.CreateDeclContext(clang_decl_ctx);
2278 return CompilerDeclContext();
2279}
2280
2283 clang::DeclContext *clang_decl_ctx =
2285 if (clang_decl_ctx)
2286 return m_ast.CreateDeclContext(clang_decl_ctx);
2287 return CompilerDeclContext();
2288}
2289
2291 lldb_private::CompilerType &clang_type, bool is_signed,
2292 uint32_t enumerator_byte_size, const DWARFDIE &parent_die) {
2293 if (!parent_die)
2294 return 0;
2295
2296 size_t enumerators_added = 0;
2297
2298 for (DWARFDIE die : parent_die.children()) {
2299 const dw_tag_t tag = die.Tag();
2300 if (tag == DW_TAG_enumerator) {
2301 DWARFAttributes attributes;
2302 const size_t num_child_attributes = die.GetAttributes(attributes);
2303 if (num_child_attributes > 0) {
2304 const char *name = nullptr;
2305 bool got_value = false;
2306 int64_t enum_value = 0;
2307 Declaration decl;
2308
2309 uint32_t i;
2310 for (i = 0; i < num_child_attributes; ++i) {
2311 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2312 DWARFFormValue form_value;
2313 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2314 switch (attr) {
2315 case DW_AT_const_value:
2316 got_value = true;
2317 if (is_signed)
2318 enum_value = form_value.Signed();
2319 else
2320 enum_value = form_value.Unsigned();
2321 break;
2322
2323 case DW_AT_name:
2324 name = form_value.AsCString();
2325 break;
2326
2327 case DW_AT_description:
2328 default:
2329 case DW_AT_decl_file:
2330 decl.SetFile(attributes.CompileUnitAtIndex(i)->GetFile(
2331 form_value.Unsigned()));
2332 break;
2333 case DW_AT_decl_line:
2334 decl.SetLine(form_value.Unsigned());
2335 break;
2336 case DW_AT_decl_column:
2337 decl.SetColumn(form_value.Unsigned());
2338 break;
2339 case DW_AT_sibling:
2340 break;
2341 }
2342 }
2343 }
2344
2345 if (name && name[0] && got_value) {
2347 clang_type, decl, name, enum_value, enumerator_byte_size * 8);
2348 ++enumerators_added;
2349 }
2350 }
2351 }
2352 }
2353 return enumerators_added;
2354}
2355
2358 bool is_static = false;
2359 bool is_variadic = false;
2360 bool has_template_params = false;
2361 unsigned type_quals = 0;
2362 std::vector<CompilerType> param_types;
2363 std::vector<clang::ParmVarDecl *> param_decls;
2364 StreamString sstr;
2365
2367 sstr << decl_ctx.GetQualifiedName();
2368
2369 clang::DeclContext *containing_decl_ctx =
2371 ParseChildParameters(containing_decl_ctx, die, true, is_static, is_variadic,
2372 has_template_params, param_types, param_decls,
2373 type_quals);
2374 sstr << "(";
2375 for (size_t i = 0; i < param_types.size(); i++) {
2376 if (i > 0)
2377 sstr << ", ";
2378 sstr << param_types[i].GetTypeName();
2379 }
2380 if (is_variadic)
2381 sstr << ", ...";
2382 sstr << ")";
2383 if (type_quals & clang::Qualifiers::Const)
2384 sstr << " const";
2385
2386 return ConstString(sstr.GetString());
2387}
2388
2389Function *
2391 const DWARFDIE &die,
2392 const AddressRange &func_range) {
2393 assert(func_range.GetBaseAddress().IsValid());
2394 DWARFRangeList func_ranges;
2395 const char *name = nullptr;
2396 const char *mangled = nullptr;
2397 int decl_file = 0;
2398 int decl_line = 0;
2399 int decl_column = 0;
2400 int call_file = 0;
2401 int call_line = 0;
2402 int call_column = 0;
2403 DWARFExpressionList frame_base;
2404
2405 const dw_tag_t tag = die.Tag();
2406
2407 if (tag != DW_TAG_subprogram)
2408 return nullptr;
2409
2410 if (die.GetDIENamesAndRanges(name, mangled, func_ranges, decl_file, decl_line,
2411 decl_column, call_file, call_line, call_column,
2412 &frame_base)) {
2413 Mangled func_name;
2414 if (mangled)
2415 func_name.SetValue(ConstString(mangled), true);
2416 else if ((die.GetParent().Tag() == DW_TAG_compile_unit ||
2417 die.GetParent().Tag() == DW_TAG_partial_unit) &&
2422 name && strcmp(name, "main") != 0) {
2423 // If the mangled name is not present in the DWARF, generate the
2424 // demangled name using the decl context. We skip if the function is
2425 // "main" as its name is never mangled.
2426 func_name.SetValue(ConstructDemangledNameFromDWARF(die), false);
2427 } else
2428 func_name.SetValue(ConstString(name), false);
2429
2430 FunctionSP func_sp;
2431 std::unique_ptr<Declaration> decl_up;
2432 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
2433 decl_up = std::make_unique<Declaration>(die.GetCU()->GetFile(decl_file),
2434 decl_line, decl_column);
2435
2437 // Supply the type _only_ if it has already been parsed
2438 Type *func_type = dwarf->GetDIEToType().lookup(die.GetDIE());
2439
2440 assert(func_type == nullptr || func_type != DIE_IS_BEING_PARSED);
2441
2442 const user_id_t func_user_id = die.GetID();
2443 func_sp =
2444 std::make_shared<Function>(&comp_unit,
2445 func_user_id, // UserID is the DIE offset
2446 func_user_id, func_name, func_type,
2447 func_range); // first address range
2448
2449 if (func_sp.get() != nullptr) {
2450 if (frame_base.IsValid())
2451 func_sp->GetFrameBaseExpression() = frame_base;
2452 comp_unit.AddFunction(func_sp);
2453 return func_sp.get();
2454 }
2455 }
2456 return nullptr;
2457}
2458
2459namespace {
2460/// Parsed form of all attributes that are relevant for parsing type members.
2461struct MemberAttributes {
2462 explicit MemberAttributes(const DWARFDIE &die, const DWARFDIE &parent_die,
2463 ModuleSP module_sp);
2464 const char *name = nullptr;
2465 /// Indicates how many bits into the word (according to the host endianness)
2466 /// the low-order bit of the field starts. Can be negative.
2467 int64_t bit_offset = 0;
2468 /// Indicates the size of the field in bits.
2469 size_t bit_size = 0;
2470 uint64_t data_bit_offset = UINT64_MAX;
2471 AccessType accessibility = eAccessNone;
2472 std::optional<uint64_t> byte_size;
2473 std::optional<DWARFFormValue> const_value_form;
2474 DWARFFormValue encoding_form;
2475 /// Indicates the byte offset of the word from the base address of the
2476 /// structure.
2477 uint32_t member_byte_offset;
2478 bool is_artificial = false;
2479};
2480
2481/// Parsed form of all attributes that are relevant for parsing Objective-C
2482/// properties.
2483struct PropertyAttributes {
2484 explicit PropertyAttributes(const DWARFDIE &die);
2485 const char *prop_name = nullptr;
2486 const char *prop_getter_name = nullptr;
2487 const char *prop_setter_name = nullptr;
2488 /// \see clang::ObjCPropertyAttribute
2489 uint32_t prop_attributes = 0;
2490};
2491} // namespace
2492
2493MemberAttributes::MemberAttributes(const DWARFDIE &die,
2494 const DWARFDIE &parent_die,
2495 ModuleSP module_sp) {
2496 member_byte_offset = (parent_die.Tag() == DW_TAG_union_type) ? 0 : UINT32_MAX;
2497
2498 DWARFAttributes attributes;
2499 const size_t num_attributes = die.GetAttributes(attributes);
2500 for (std::size_t i = 0; i < num_attributes; ++i) {
2501 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2502 DWARFFormValue form_value;
2503 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2504 switch (attr) {
2505 case DW_AT_name:
2506 name = form_value.AsCString();
2507 break;
2508 case DW_AT_type:
2509 encoding_form = form_value;
2510 break;
2511 case DW_AT_bit_offset:
2512 bit_offset = form_value.Signed();
2513 break;
2514 case DW_AT_bit_size:
2515 bit_size = form_value.Unsigned();
2516 break;
2517 case DW_AT_byte_size:
2518 byte_size = form_value.Unsigned();
2519 break;
2520 case DW_AT_const_value:
2521 const_value_form = form_value;
2522 break;
2523 case DW_AT_data_bit_offset:
2524 data_bit_offset = form_value.Unsigned();
2525 break;
2526 case DW_AT_data_member_location:
2527 if (form_value.BlockData()) {
2528 Value initialValue(0);
2529 Value memberOffset(0);
2530 const DWARFDataExtractor &debug_info_data = die.GetData();
2531 uint32_t block_length = form_value.Unsigned();
2532 uint32_t block_offset =
2533 form_value.BlockData() - debug_info_data.GetDataStart();
2535 nullptr, // ExecutionContext *
2536 nullptr, // RegisterContext *
2537 module_sp,
2538 DataExtractor(debug_info_data, block_offset, block_length),
2539 die.GetCU(), eRegisterKindDWARF, &initialValue, nullptr,
2540 memberOffset, nullptr)) {
2541 member_byte_offset = memberOffset.ResolveValue(nullptr).UInt();
2542 }
2543 } else {
2544 // With DWARF 3 and later, if the value is an integer constant,
2545 // this form value is the offset in bytes from the beginning of
2546 // the containing entity.
2547 member_byte_offset = form_value.Unsigned();
2548 }
2549 break;
2550
2551 case DW_AT_accessibility:
2552 accessibility = DWARFASTParser::GetAccessTypeFromDWARF(form_value.Unsigned());
2553 break;
2554 case DW_AT_artificial:
2555 is_artificial = form_value.Boolean();
2556 break;
2557 default:
2558 break;
2559 }
2560 }
2561 }
2562
2563 // Clang has a DWARF generation bug where sometimes it represents
2564 // fields that are references with bad byte size and bit size/offset
2565 // information such as:
2566 //
2567 // DW_AT_byte_size( 0x00 )
2568 // DW_AT_bit_size( 0x40 )
2569 // DW_AT_bit_offset( 0xffffffffffffffc0 )
2570 //
2571 // So check the bit offset to make sure it is sane, and if the values
2572 // are not sane, remove them. If we don't do this then we will end up
2573 // with a crash if we try to use this type in an expression when clang
2574 // becomes unhappy with its recycled debug info.
2575 if (byte_size.value_or(0) == 0 && bit_offset < 0) {
2576 bit_size = 0;
2577 bit_offset = 0;
2578 }
2579}
2580
2581PropertyAttributes::PropertyAttributes(const DWARFDIE &die) {
2582
2583 DWARFAttributes attributes;
2584 const size_t num_attributes = die.GetAttributes(attributes);
2585 for (size_t i = 0; i < num_attributes; ++i) {
2586 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2587 DWARFFormValue form_value;
2588 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2589 switch (attr) {
2590 case DW_AT_APPLE_property_name:
2591 prop_name = form_value.AsCString();
2592 break;
2593 case DW_AT_APPLE_property_getter:
2594 prop_getter_name = form_value.AsCString();
2595 break;
2596 case DW_AT_APPLE_property_setter:
2597 prop_setter_name = form_value.AsCString();
2598 break;
2599 case DW_AT_APPLE_property_attribute:
2600 prop_attributes = form_value.Unsigned();
2601 break;
2602 default:
2603 break;
2604 }
2605 }
2606 }
2607
2608 if (!prop_name)
2609 return;
2610 ConstString fixed_setter;
2611
2612 // Check if the property getter/setter were provided as full names.
2613 // We want basenames, so we extract them.
2614 if (prop_getter_name && prop_getter_name[0] == '-') {
2615 ObjCLanguage::MethodName prop_getter_method(prop_getter_name, true);
2616 prop_getter_name = prop_getter_method.GetSelector().GetCString();
2617 }
2618
2619 if (prop_setter_name && prop_setter_name[0] == '-') {
2620 ObjCLanguage::MethodName prop_setter_method(prop_setter_name, true);
2621 prop_setter_name = prop_setter_method.GetSelector().GetCString();
2622 }
2623
2624 // If the names haven't been provided, they need to be filled in.
2625 if (!prop_getter_name)
2626 prop_getter_name = prop_name;
2627 if (!prop_setter_name && prop_name[0] &&
2628 !(prop_attributes & DW_APPLE_PROPERTY_readonly)) {
2629 StreamString ss;
2630
2631 ss.Printf("set%c%s:", toupper(prop_name[0]), &prop_name[1]);
2632
2633 fixed_setter.SetString(ss.GetString());
2634 prop_setter_name = fixed_setter.GetCString();
2635 }
2636}
2637
2639 const DWARFDIE &die, const DWARFDIE &parent_die,
2640 const lldb_private::CompilerType &class_clang_type,
2641 DelayedPropertyList &delayed_properties) {
2642 // This function can only parse DW_TAG_APPLE_property.
2643 assert(die.Tag() == DW_TAG_APPLE_property);
2644
2645 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2646
2647 const MemberAttributes attrs(die, parent_die, module_sp);
2648 const PropertyAttributes propAttrs(die);
2649
2650 if (!propAttrs.prop_name) {
2651 module_sp->ReportError("{0:x8}: DW_TAG_APPLE_property has no name.",
2652 die.GetID());
2653 return;
2654 }
2655
2656 Type *member_type = die.ResolveTypeUID(attrs.encoding_form.Reference());
2657 if (!member_type) {
2658 module_sp->ReportError(
2659 "{0:x8}: DW_TAG_APPLE_property '{1}' refers to type {2:x16}"
2660 " which was unable to be parsed",
2661 die.GetID(), propAttrs.prop_name,
2662 attrs.encoding_form.Reference().GetOffset());
2663 return;
2664 }
2665
2666 ClangASTMetadata metadata;
2667 metadata.SetUserID(die.GetID());
2668 delayed_properties.push_back(DelayedAddObjCClassProperty(
2669 class_clang_type, propAttrs.prop_name,
2670 member_type->GetLayoutCompilerType(), propAttrs.prop_setter_name,
2671 propAttrs.prop_getter_name, propAttrs.prop_attributes, &metadata));
2672}
2673
2675 const CompilerType &int_type, const DWARFFormValue &form_value) const {
2676 clang::QualType qt = ClangUtil::GetQualType(int_type);
2677 assert(qt->isIntegralOrEnumerationType());
2678 auto ts_ptr = int_type.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
2679 if (!ts_ptr)
2680 return llvm::createStringError(llvm::inconvertibleErrorCode(),
2681 "TypeSystem not clang");
2682 TypeSystemClang &ts = *ts_ptr;
2683 clang::ASTContext &ast = ts.getASTContext();
2684
2685 const unsigned type_bits = ast.getIntWidth(qt);
2686 const bool is_unsigned = qt->isUnsignedIntegerType();
2687
2688 // The maximum int size supported at the moment by this function. Limited
2689 // by the uint64_t return type of DWARFFormValue::Signed/Unsigned.
2690 constexpr std::size_t max_bit_size = 64;
2691
2692 // For values bigger than 64 bit (e.g. __int128_t values),
2693 // DWARFFormValue's Signed/Unsigned functions will return wrong results so
2694 // emit an error for now.
2695 if (type_bits > max_bit_size) {
2696 auto msg = llvm::formatv("Can only parse integers with up to {0} bits, but "
2697 "given integer has {1} bits.",
2698 max_bit_size, type_bits);
2699 return llvm::createStringError(llvm::inconvertibleErrorCode(), msg.str());
2700 }
2701
2702 // Construct an APInt with the maximum bit size and the given integer.
2703 llvm::APInt result(max_bit_size, form_value.Unsigned(), !is_unsigned);
2704
2705 // Calculate how many bits are required to represent the input value.
2706 // For unsigned types, take the number of active bits in the APInt.
2707 // For signed types, ask APInt how many bits are required to represent the
2708 // signed integer.
2709 const unsigned required_bits =
2710 is_unsigned ? result.getActiveBits() : result.getMinSignedBits();
2711
2712 // If the input value doesn't fit into the integer type, return an error.
2713 if (required_bits > type_bits) {
2714 std::string value_as_str = is_unsigned
2715 ? std::to_string(form_value.Unsigned())
2716 : std::to_string(form_value.Signed());
2717 auto msg = llvm::formatv("Can't store {0} value {1} in integer with {2} "
2718 "bits.",
2719 (is_unsigned ? "unsigned" : "signed"),
2720 value_as_str, type_bits);
2721 return llvm::createStringError(llvm::inconvertibleErrorCode(), msg.str());
2722 }
2723
2724 // Trim the result to the bit width our the int type.
2725 if (result.getBitWidth() > type_bits)
2726 result = result.trunc(type_bits);
2727 return result;
2728}
2729
2731 const DWARFDIE &die, const DWARFDIE &parent_die,
2732 const lldb_private::CompilerType &class_clang_type,
2733 lldb::AccessType default_accessibility,
2735 FieldInfo &last_field_info) {
2736 Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
2737 // This function can only parse DW_TAG_member.
2738 assert(die.Tag() == DW_TAG_member);
2739
2740 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2741 const dw_tag_t tag = die.Tag();
2742 // Get the parent byte size so we can verify any members will fit
2743 const uint64_t parent_byte_size =
2744 parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
2745 const uint64_t parent_bit_size =
2746 parent_byte_size == UINT64_MAX ? UINT64_MAX : parent_byte_size * 8;
2747
2748 // FIXME: Remove the workarounds below and make this const.
2749 MemberAttributes attrs(die, parent_die, module_sp);
2750
2751 const bool class_is_objc_object_or_interface =
2753
2754 // FIXME: Make Clang ignore Objective-C accessibility for expressions
2755 if (class_is_objc_object_or_interface)
2756 attrs.accessibility = eAccessNone;
2757
2758 // Handle static members, which is any member that doesn't have a bit or a
2759 // byte member offset.
2760 if (attrs.member_byte_offset == UINT32_MAX &&
2761 attrs.data_bit_offset == UINT64_MAX) {
2762 Type *var_type = die.ResolveTypeUID(attrs.encoding_form.Reference());
2763
2764 if (var_type) {
2765 if (attrs.accessibility == eAccessNone)
2766 attrs.accessibility = eAccessPublic;
2767 CompilerType ct = var_type->GetForwardCompilerType();
2768 clang::VarDecl *v = TypeSystemClang::AddVariableToRecordType(
2769 class_clang_type, attrs.name, ct, attrs.accessibility);
2770 if (!v) {
2771 LLDB_LOG(log, "Failed to add variable to the record type");
2772 return;
2773 }
2774
2775 bool unused;
2776 // TODO: Support float/double static members as well.
2777 if (!attrs.const_value_form || !ct.IsIntegerOrEnumerationType(unused))
2778 return;
2779
2780 llvm::Expected<llvm::APInt> const_value_or_err =
2781 ExtractIntFromFormValue(ct, *attrs.const_value_form);
2782 if (!const_value_or_err) {
2783 LLDB_LOG_ERROR(log, const_value_or_err.takeError(),
2784 "Failed to add const value to variable {1}: {0}",
2785 v->getQualifiedNameAsString());
2786 return;
2787 }
2788
2790 }
2791 return;
2792 }
2793
2794 Type *member_type = die.ResolveTypeUID(attrs.encoding_form.Reference());
2795 if (!member_type) {
2796 if (attrs.name)
2797 module_sp->ReportError(
2798 "{0:x8}: DW_TAG_member '{1}' refers to type {2:x16}"
2799 " which was unable to be parsed",
2800 die.GetID(), attrs.name, attrs.encoding_form.Reference().GetOffset());
2801 else
2802 module_sp->ReportError("{0:x8}: DW_TAG_member refers to type {1:x16}"
2803 " which was unable to be parsed",
2804 die.GetID(),
2805 attrs.encoding_form.Reference().GetOffset());
2806 return;
2807 }
2808
2809 const uint64_t character_width = 8;
2810 const uint64_t word_width = 32;
2811 CompilerType member_clang_type = member_type->GetLayoutCompilerType();
2812
2813 if (attrs.accessibility == eAccessNone)
2814 attrs.accessibility = default_accessibility;
2815
2816 uint64_t field_bit_offset = (attrs.member_byte_offset == UINT32_MAX
2817 ? 0
2818 : (attrs.member_byte_offset * 8ULL));
2819
2820 if (attrs.bit_size > 0) {
2821 FieldInfo this_field_info;
2822 this_field_info.bit_offset = field_bit_offset;
2823 this_field_info.bit_size = attrs.bit_size;
2824
2825 if (attrs.data_bit_offset != UINT64_MAX) {
2826 this_field_info.bit_offset = attrs.data_bit_offset;
2827 } else {
2828 if (!attrs.byte_size)
2829 attrs.byte_size = member_type->GetByteSize(nullptr);
2830
2831 ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2832 if (objfile->GetByteOrder() == eByteOrderLittle) {
2833 this_field_info.bit_offset += attrs.byte_size.value_or(0) * 8;
2834 this_field_info.bit_offset -= (attrs.bit_offset + attrs.bit_size);
2835 } else {
2836 this_field_info.bit_offset += attrs.bit_offset;
2837 }
2838 }
2839
2840 // The ObjC runtime knows the byte offset but we still need to provide
2841 // the bit-offset in the layout. It just means something different then
2842 // what it does in C and C++. So we skip this check for ObjC types.
2843 //
2844 // We also skip this for fields of a union since they will all have a
2845 // zero offset.
2846 if (!TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type) &&
2847 !(parent_die.Tag() == DW_TAG_union_type &&
2848 this_field_info.bit_offset == 0) &&
2849 ((this_field_info.bit_offset >= parent_bit_size) ||
2850 (last_field_info.IsBitfield() &&
2851 !last_field_info.NextBitfieldOffsetIsValid(
2852 this_field_info.bit_offset)))) {
2853 ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2854 objfile->GetModule()->ReportWarning(
2855 "{0:x16}: {1} bitfield named \"{2}\" has invalid "
2856 "bit offset ({3:x8}) member will be ignored. Please file a bug "
2857 "against the "
2858 "compiler and include the preprocessed output for {4}\n",
2859 die.GetID(), DW_TAG_value_to_name(tag), attrs.name,
2860 this_field_info.bit_offset, GetUnitName(parent_die).c_str());
2861 return;
2862 }
2863
2864 // Update the field bit offset we will report for layout
2865 field_bit_offset = this_field_info.bit_offset;
2866
2867 // Objective-C has invalid DW_AT_bit_offset values in older
2868 // versions of clang, so we have to be careful and only insert
2869 // unnamed bitfields if we have a new enough clang.
2870 bool detect_unnamed_bitfields = true;
2871
2872 if (class_is_objc_object_or_interface)
2873 detect_unnamed_bitfields =
2875
2876 if (detect_unnamed_bitfields) {
2877 std::optional<FieldInfo> unnamed_field_info;
2878 uint64_t last_field_end = 0;
2879
2880 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
2881
2882 if (!last_field_info.IsBitfield()) {
2883 // The last field was not a bit-field...
2884 // but if it did take up the entire word then we need to extend
2885 // last_field_end so the bit-field does not step into the last
2886 // fields padding.
2887 if (last_field_end != 0 && ((last_field_end % word_width) != 0))
2888 last_field_end += word_width - (last_field_end % word_width);
2889 }
2890
2891 // If we have a gap between the last_field_end and the current
2892 // field we have an unnamed bit-field.
2893 // If we have a base class, we assume there is no unnamed
2894 // bit-field if this is the first field since the gap can be
2895 // attributed to the members from the base class. This assumption
2896 // is not correct if the first field of the derived class is
2897 // indeed an unnamed bit-field. We currently do not have the
2898 // machinary to track the offset of the last field of classes we
2899 // have seen before, so we are not handling this case.
2900 if (this_field_info.bit_offset != last_field_end &&
2901 this_field_info.bit_offset > last_field_end &&
2902 !(last_field_info.bit_offset == 0 &&
2903 last_field_info.bit_size == 0 &&
2904 layout_info.base_offsets.size() != 0)) {
2905 unnamed_field_info = FieldInfo{};
2906 unnamed_field_info->bit_size =
2907 this_field_info.bit_offset - last_field_end;
2908 unnamed_field_info->bit_offset = last_field_end;
2909 }
2910
2911 if (unnamed_field_info) {
2912 clang::FieldDecl *unnamed_bitfield_decl =
2914 class_clang_type, llvm::StringRef(),
2916 word_width),
2917 attrs.accessibility, unnamed_field_info->bit_size);
2918
2919 layout_info.field_offsets.insert(std::make_pair(
2920 unnamed_bitfield_decl, unnamed_field_info->bit_offset));
2921 }
2922 }
2923
2924 last_field_info = this_field_info;
2925 last_field_info.SetIsBitfield(true);
2926 } else {
2927 last_field_info.bit_offset = field_bit_offset;
2928
2929 if (std::optional<uint64_t> clang_type_size =
2930 member_type->GetByteSize(nullptr)) {
2931 last_field_info.bit_size = *clang_type_size * character_width;
2932 }
2933
2934 last_field_info.SetIsBitfield(false);
2935 }
2936
2937 // Don't turn artificial members such as vtable pointers into real FieldDecls
2938 // in our AST. Clang will re-create those articial members and they would
2939 // otherwise just overlap in the layout with the FieldDecls we add here.
2940 // This needs to be done after updating FieldInfo which keeps track of where
2941 // field start/end so we don't later try to fill the the space of this
2942 // artificial member with (unnamed bitfield) padding.
2943 // FIXME: This check should verify that this is indeed an artificial member
2944 // we are supposed to ignore.
2945 if (attrs.is_artificial)
2946 return;
2947
2948 if (!member_clang_type.IsCompleteType())
2949 member_clang_type.GetCompleteType();
2950
2951 {
2952 // Older versions of clang emit array[0] and array[1] in the
2953 // same way (<rdar://problem/12566646>). If the current field
2954 // is at the end of the structure, then there is definitely no
2955 // room for extra elements and we override the type to
2956 // array[0].
2957
2958 CompilerType member_array_element_type;
2959 uint64_t member_array_size;
2960 bool member_array_is_incomplete;
2961
2962 if (member_clang_type.IsArrayType(&member_array_element_type,
2963 &member_array_size,
2964 &member_array_is_incomplete) &&
2965 !member_array_is_incomplete) {
2966 uint64_t parent_byte_size =
2967 parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
2968
2969 if (attrs.member_byte_offset >= parent_byte_size) {
2970 if (member_array_size != 1 &&
2971 (member_array_size != 0 ||
2972 attrs.member_byte_offset > parent_byte_size)) {
2973 module_sp->ReportError(
2974 "{0:x8}: DW_TAG_member '{1}' refers to type {2:x16}"
2975 " which extends beyond the bounds of {3:x8}",
2976 die.GetID(), attrs.name,
2977 attrs.encoding_form.Reference().GetOffset(), parent_die.GetID());
2978 }
2979
2980 member_clang_type =
2981 m_ast.CreateArrayType(member_array_element_type, 0, false);
2982 }
2983 }
2984 }
2985
2986 TypeSystemClang::RequireCompleteType(member_clang_type);
2987
2988 clang::FieldDecl *field_decl = TypeSystemClang::AddFieldToRecordType(
2989 class_clang_type, attrs.name, member_clang_type, attrs.accessibility,
2990 attrs.bit_size);
2991
2992 m_ast.SetMetadataAsUserID(field_decl, die.GetID());
2993
2994 layout_info.field_offsets.insert(
2995 std::make_pair(field_decl, field_bit_offset));
2996}
2997
2999 const DWARFDIE &parent_die, CompilerType &class_clang_type,
3000 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
3001 std::vector<DWARFDIE> &member_function_dies,
3002 DelayedPropertyList &delayed_properties,
3003 const AccessType default_accessibility,
3004 ClangASTImporter::LayoutInfo &layout_info) {
3005 if (!parent_die)
3006 return false;
3007
3008 FieldInfo last_field_info;
3009
3010 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
3011 auto ts = class_clang_type.GetTypeSystem();
3012 auto ast = ts.dyn_cast_or_null<TypeSystemClang>();
3013 if (ast == nullptr)
3014 return false;
3015
3016 for (DWARFDIE die : parent_die.children()) {
3017 dw_tag_t tag = die.Tag();
3018
3019 switch (tag) {
3020 case DW_TAG_APPLE_property:
3021 ParseObjCProperty(die, parent_die, class_clang_type, delayed_properties);
3022 break;
3023
3024 case DW_TAG_member:
3025 ParseSingleMember(die, parent_die, class_clang_type,
3026 default_accessibility, layout_info, last_field_info);
3027 break;
3028
3029 case DW_TAG_subprogram:
3030 // Let the type parsing code handle this one for us.
3031 member_function_dies.push_back(die);
3032 break;
3033
3034 case DW_TAG_inheritance:
3035 ParseInheritance(die, parent_die, class_clang_type, default_accessibility,
3036 module_sp, base_classes, layout_info);
3037 break;
3038
3039 default:
3040 break;
3041 }
3042 }
3043
3044 return true;
3045}
3046
3048 clang::DeclContext *containing_decl_ctx, const DWARFDIE &parent_die,
3049 bool skip_artificial, bool &is_static, bool &is_variadic,
3050 bool &has_template_params, std::vector<CompilerType> &function_param_types,
3051 std::vector<clang::ParmVarDecl *> &function_param_decls,
3052 unsigned &type_quals) {
3053 if (!parent_die)
3054 return 0;
3055
3056 size_t arg_idx = 0;
3057 for (DWARFDIE die : parent_die.children()) {
3058 const dw_tag_t tag = die.Tag();
3059 switch (tag) {
3060 case DW_TAG_formal_parameter: {
3061 DWARFAttributes attributes;
3062 const size_t num_attributes = die.GetAttributes(attributes);
3063 if (num_attributes > 0) {
3064 const char *name = nullptr;
3065 DWARFFormValue param_type_die_form;
3066 bool is_artificial = false;
3067 // one of None, Auto, Register, Extern, Static, PrivateExtern
3068
3069 clang::StorageClass storage = clang::SC_None;
3070 uint32_t i;
3071 for (i = 0; i < num_attributes; ++i) {
3072 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3073 DWARFFormValue form_value;
3074 if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3075 switch (attr) {
3076 case DW_AT_name:
3077 name = form_value.AsCString();
3078 break;
3079 case DW_AT_type:
3080 param_type_die_form = form_value;
3081 break;
3082 case DW_AT_artificial:
3083 is_artificial = form_value.Boolean();
3084 break;
3085 case DW_AT_location:
3086 case DW_AT_const_value:
3087 case DW_AT_default_value:
3088 case DW_AT_description:
3089 case DW_AT_endianity:
3090 case DW_AT_is_optional:
3091 case DW_AT_segment:
3092 case DW_AT_variable_parameter:
3093 default:
3094 case DW_AT_abstract_origin:
3095 case DW_AT_sibling:
3096 break;
3097 }
3098 }
3099 }
3100
3101 bool skip = false;
3102 if (skip_artificial && is_artificial) {
3103 // In order to determine if a C++ member function is "const" we
3104 // have to look at the const-ness of "this"...
3105 if (arg_idx == 0 &&
3106 DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()) &&
3107 // Often times compilers omit the "this" name for the
3108 // specification DIEs, so we can't rely upon the name being in
3109 // the formal parameter DIE...
3110 (name == nullptr || ::strcmp(name, "this") == 0)) {
3111 Type *this_type =
3112 die.ResolveTypeUID(param_type_die_form.Reference());
3113 if (this_type) {
3114 uint32_t encoding_mask = this_type->GetEncodingMask();
3115 if (encoding_mask & Type::eEncodingIsPointerUID) {
3116 is_static = false;
3117
3118 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3119 type_quals |= clang::Qualifiers::Const;
3120 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3121 type_quals |= clang::Qualifiers::Volatile;
3122 }
3123 }
3124 }
3125 skip = true;
3126 }
3127
3128 if (!skip) {
3129 Type *type = die.ResolveTypeUID(param_type_die_form.Reference());
3130 if (type) {
3131 function_param_types.push_back(type->GetForwardCompilerType());
3132
3133 clang::ParmVarDecl *param_var_decl =
3135 containing_decl_ctx, GetOwningClangModule(die), name,
3136 type->GetForwardCompilerType(), storage);
3137 assert(param_var_decl);
3138 function_param_decls.push_back(param_var_decl);
3139
3140 m_ast.SetMetadataAsUserID(param_var_decl, die.GetID());
3141 }
3142 }
3143 }
3144 arg_idx++;
3145 } break;
3146
3147 case DW_TAG_unspecified_parameters:
3148 is_variadic = true;
3149 break;
3150
3151 case DW_TAG_template_type_parameter:
3152 case DW_TAG_template_value_parameter:
3153 case DW_TAG_GNU_template_parameter_pack:
3154 // The one caller of this was never using the template_param_infos, and
3155 // the local variable was taking up a large amount of stack space in
3156 // SymbolFileDWARF::ParseType() so this was removed. If we ever need the
3157 // template params back, we can add them back.
3158 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
3159 has_template_params = true;
3160 break;
3161
3162 default:
3163 break;
3164 }
3165 }
3166 return arg_idx;
3167}
3168
3170 if (die) {
3172 DWARFAttributes attributes;
3173 const size_t num_attributes = die.GetAttributes(attributes);
3174 if (num_attributes > 0) {
3175 DWARFFormValue type_die_form;
3176 for (size_t i = 0; i < num_attributes; ++i) {
3177 dw_attr_t attr = attributes.AttributeAtIndex(i);
3178 DWARFFormValue form_value;
3179
3180 if (attr == DW_AT_type &&
3181 attributes.ExtractFormValueAtIndex(i, form_value))
3182 return dwarf->ResolveTypeUID(form_value.Reference(), true);
3183 }
3184 }
3185 }
3186
3187 return nullptr;
3188}
3189
3191 if (!die)
3192 return nullptr;
3193
3194 switch (die.Tag()) {
3195 case DW_TAG_variable:
3196 case DW_TAG_constant:
3197 case DW_TAG_formal_parameter:
3198 case DW_TAG_imported_declaration:
3199 case DW_TAG_imported_module:
3200 break;
3201 default:
3202 return nullptr;
3203 }
3204
3205 DIEToDeclMap::iterator cache_pos = m_die_to_decl.find(die.GetDIE());
3206 if (cache_pos != m_die_to_decl.end())
3207 return cache_pos->second;
3208
3209 if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification)) {
3210 clang::Decl *decl = GetClangDeclForDIE(spec_die);
3211 m_die_to_decl[die.GetDIE()] = decl;
3212 return decl;
3213 }
3214
3215 if (DWARFDIE abstract_origin_die =
3216 die.GetReferencedDIE(DW_AT_abstract_origin)) {
3217 clang::Decl *decl = GetClangDeclForDIE(abstract_origin_die);
3218 m_die_to_decl[die.GetDIE()] = decl;
3219 return decl;
3220 }
3221
3222 clang::Decl *decl = nullptr;
3223 switch (die.Tag()) {
3224 case DW_TAG_variable:
3225 case DW_TAG_constant:
3226 case DW_TAG_formal_parameter: {
3228 Type *type = GetTypeForDIE(die);
3229 if (dwarf && type) {
3230 const char *name = die.GetName();
3231 clang::DeclContext *decl_context =
3233 dwarf->GetDeclContextContainingUID(die.GetID()));
3235 decl_context, GetOwningClangModule(die), name,
3237 }
3238 break;
3239 }
3240 case DW_TAG_imported_declaration: {
3242 DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3243 if (imported_uid) {
3244 CompilerDecl imported_decl = SymbolFileDWARF::GetDecl(imported_uid);
3245 if (imported_decl) {
3246 clang::DeclContext *decl_context =
3248 dwarf->GetDeclContextContainingUID(die.GetID()));
3249 if (clang::NamedDecl *clang_imported_decl =
3250 llvm::dyn_cast<clang::NamedDecl>(
3251 (clang::Decl *)imported_decl.GetOpaqueDecl()))
3253 decl_context, OptionalClangModuleID(), clang_imported_decl);
3254 }
3255 }
3256 break;
3257 }
3258 case DW_TAG_imported_module: {
3260 DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3261
3262 if (imported_uid) {
3263 CompilerDeclContext imported_decl_ctx =
3264 SymbolFileDWARF::GetDeclContext(imported_uid);
3265 if (imported_decl_ctx) {
3266 clang::DeclContext *decl_context =
3268 dwarf->GetDeclContextContainingUID(die.GetID()));
3269 if (clang::NamespaceDecl *ns_decl =
3271 imported_decl_ctx))
3273 decl_context, OptionalClangModuleID(), ns_decl);
3274 }
3275 }
3276 break;
3277 }
3278 default:
3279 break;
3280 }
3281
3282 m_die_to_decl[die.GetDIE()] = decl;
3283
3284 return decl;
3285}
3286
3287clang::DeclContext *
3289 if (die) {
3290 clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE(die);
3291 if (decl_ctx)
3292 return decl_ctx;
3293
3294 bool try_parsing_type = true;
3295 switch (die.Tag()) {
3296 case DW_TAG_compile_unit:
3297 case DW_TAG_partial_unit:
3298 decl_ctx = m_ast.GetTranslationUnitDecl();
3299 try_parsing_type = false;
3300 break;
3301
3302 case DW_TAG_namespace:
3303 decl_ctx = ResolveNamespaceDIE(die);
3304 try_parsing_type = false;
3305 break;
3306
3307 case DW_TAG_lexical_block:
3308 decl_ctx = GetDeclContextForBlock(die);
3309 try_parsing_type = false;
3310 break;
3311
3312 default:
3313 break;
3314 }
3315
3316 if (decl_ctx == nullptr && try_parsing_type) {
3317 Type *type = die.GetDWARF()->ResolveType(die);
3318 if (type)
3319 decl_ctx = GetCachedClangDeclContextForDIE(die);
3320 }
3321
3322 if (decl_ctx) {
3323 LinkDeclContextToDIE(decl_ctx, die);
3324 return decl_ctx;
3325 }
3326 }
3327 return nullptr;
3328}
3329
3332 if (!die.IsValid())
3333 return {};
3334
3335 for (DWARFDIE parent = die.GetParent(); parent.IsValid();
3336 parent = parent.GetParent()) {
3337 const dw_tag_t tag = parent.Tag();
3338 if (tag == DW_TAG_module) {
3339 DWARFDIE module_die = parent;
3340 auto it = m_die_to_module.find(module_die.GetDIE());
3341 if (it != m_die_to_module.end())
3342 return it->second;
3343 const char *name =
3344 module_die.GetAttributeValueAsString(DW_AT_name, nullptr);
3345 if (!name)
3346 return {};
3347
3350 m_die_to_module.insert({module_die.GetDIE(), id});
3351 return id;
3352 }
3353 }
3354 return {};
3355}
3356
3357static bool IsSubroutine(const DWARFDIE &die) {
3358 switch (die.Tag()) {
3359 case DW_TAG_subprogram:
3360 case DW_TAG_inlined_subroutine:
3361 return true;
3362 default:
3363 return false;
3364 }
3365}
3366
3368 for (DWARFDIE candidate = die; candidate; candidate = candidate.GetParent()) {
3369 if (IsSubroutine(candidate)) {
3370 if (candidate.GetReferencedDIE(DW_AT_abstract_origin)) {
3371 return candidate;
3372 } else {
3373 return DWARFDIE();
3374 }
3375 }
3376 }
3377 assert(0 && "Shouldn't call GetContainingFunctionWithAbstractOrigin on "
3378 "something not in a function");
3379 return DWARFDIE();
3380}
3381
3383 for (DWARFDIE candidate : context.children()) {
3384 if (candidate.GetReferencedDIE(DW_AT_abstract_origin)) {
3385 return candidate;
3386 }
3387 }
3388 return DWARFDIE();
3389}
3390
3392 const DWARFDIE &function) {
3393 assert(IsSubroutine(function));
3394 for (DWARFDIE context = block; context != function.GetParent();
3395 context = context.GetParent()) {
3396 assert(!IsSubroutine(context) || context == function);
3397 if (DWARFDIE child = FindAnyChildWithAbstractOrigin(context)) {
3398 return child;
3399 }
3400 }
3401 return DWARFDIE();
3402}
3403
3404clang::DeclContext *
3406 assert(die.Tag() == DW_TAG_lexical_block);
3407 DWARFDIE containing_function_with_abstract_origin =
3409 if (!containing_function_with_abstract_origin) {
3410 return (clang::DeclContext *)ResolveBlockDIE(die);
3411 }
3413 die, containing_function_with_abstract_origin);
3414 CompilerDeclContext decl_context =
3416 return (clang::DeclContext *)decl_context.GetOpaqueDeclContext();
3417}
3418
3419clang::BlockDecl *DWARFASTParserClang::ResolveBlockDIE(const DWARFDIE &die) {
3420 if (die && die.Tag() == DW_TAG_lexical_block) {
3421 clang::BlockDecl *decl =
3422 llvm::cast_or_null<clang::BlockDecl>(m_die_to_decl_ctx[die.GetDIE()]);
3423
3424 if (!decl) {
3425 DWARFDIE decl_context_die;
3426 clang::DeclContext *decl_context =
3427 GetClangDeclContextContainingDIE(die, &decl_context_die);
3428 decl =
3430
3431 if (decl)
3432 LinkDeclContextToDIE((clang::DeclContext *)decl, die);
3433 }
3434
3435 return decl;
3436 }
3437 return nullptr;
3438}
3439
3440clang::NamespaceDecl *
3442 if (die && die.Tag() == DW_TAG_namespace) {
3443 // See if we already parsed this namespace DIE and associated it with a
3444 // uniqued namespace declaration
3445 clang::NamespaceDecl *namespace_decl =
3446 static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die.GetDIE()]);
3447 if (namespace_decl)
3448 return namespace_decl;
3449 else {
3450 const char *namespace_name = die.GetName();
3451 clang::DeclContext *containing_decl_ctx =
3453 bool is_inline =
3454 die.GetAttributeValueAsUnsigned(DW_AT_export_symbols, 0) != 0;
3455
3456 namespace_decl = m_ast.GetUniqueNamespaceDeclaration(
3457 namespace_name, containing_decl_ctx, GetOwningClangModule(die),
3458 is_inline);
3459
3460 if (namespace_decl)
3461 LinkDeclContextToDIE((clang::DeclContext *)namespace_decl, die);
3462 return namespace_decl;
3463 }
3464 }
3465 return nullptr;
3466}
3467
3469 const DWARFDIE &die, DWARFDIE *decl_ctx_die_copy) {
3471
3472 DWARFDIE decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE(die);
3473
3474 if (decl_ctx_die_copy)
3475 *decl_ctx_die_copy = decl_ctx_die;
3476
3477 if (decl_ctx_die) {
3478 clang::DeclContext *clang_decl_ctx =
3479 GetClangDeclContextForDIE(decl_ctx_die);
3480 if (clang_decl_ctx)
3481 return clang_decl_ctx;
3482 }
3484}
3485
3486clang::DeclContext *
3488 if (die) {
3489 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die.GetDIE());
3490 if (pos != m_die_to_decl_ctx.end())
3491 return pos->second;
3492 }
3493 return nullptr;
3494}
3495
3496void DWARFASTParserClang::LinkDeclContextToDIE(clang::DeclContext *decl_ctx,
3497 const DWARFDIE &die) {
3498 m_die_to_decl_ctx[die.GetDIE()] = decl_ctx;
3499 // There can be many DIEs for a single decl context
3500 // m_decl_ctx_to_die[decl_ctx].insert(die.GetDIE());
3501 m_decl_ctx_to_die.insert(std::make_pair(decl_ctx, die));
3502}
3503
3505 const DWARFDIE &src_class_die, const DWARFDIE &dst_class_die,
3506 lldb_private::Type *class_type, std::vector<DWARFDIE> &failures) {
3507 if (!class_type || !src_class_die || !dst_class_die)
3508 return false;
3509 if (src_class_die.Tag() != dst_class_die.Tag())
3510 return false;
3511
3512 // We need to complete the class type so we can get all of the method types
3513 // parsed so we can then unique those types to their equivalent counterparts
3514 // in "dst_cu" and "dst_class_die"
3515 class_type->GetFullCompilerType();
3516
3517 auto gather = [](DWARFDIE die, UniqueCStringMap<DWARFDIE> &map,
3518 UniqueCStringMap<DWARFDIE> &map_artificial) {
3519 if (die.Tag() != DW_TAG_subprogram)
3520 return;
3521 // Make sure this is a declaration and not a concrete instance by looking
3522 // for DW_AT_declaration set to 1. Sometimes concrete function instances are
3523 // placed inside the class definitions and shouldn't be included in the list
3524 // of things are are tracking here.
3525 if (die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) != 1)
3526 return;
3527
3528 if (const char *name = die.GetMangledName()) {
3529 ConstString const_name(name);
3530 if (die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3531 map_artificial.Append(const_name, die);
3532 else
3533 map.Append(const_name, die);
3534 }
3535 };
3536
3537 UniqueCStringMap<DWARFDIE> src_name_to_die;
3538 UniqueCStringMap<DWARFDIE> dst_name_to_die;
3539 UniqueCStringMap<DWARFDIE> src_name_to_die_artificial;
3540 UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial;
3541 for (DWARFDIE src_die = src_class_die.GetFirstChild(); src_die.IsValid();
3542 src_die = src_die.GetSibling()) {
3543 gather(src_die, src_name_to_die, src_name_to_die_artificial);
3544 }
3545 for (DWARFDIE dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid();
3546 dst_die = dst_die.GetSibling()) {
3547 gather(dst_die, dst_name_to_die, dst_name_to_die_artificial);
3548 }
3549 const uint32_t src_size = src_name_to_die.GetSize();
3550 const uint32_t dst_size = dst_name_to_die.GetSize();
3551
3552 // Is everything kosher so we can go through the members at top speed?
3553 bool fast_path = true;
3554
3555 if (src_size != dst_size)
3556 fast_path = false;
3557
3558 uint32_t idx;
3559
3560 if (fast_path) {
3561 for (idx = 0; idx < src_size; ++idx) {
3562 DWARFDIE src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3563 DWARFDIE dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3564
3565 if (src_die.Tag() != dst_die.Tag())
3566 fast_path = false;
3567
3568 const char *src_name = src_die.GetMangledName();
3569 const char *dst_name = dst_die.GetMangledName();
3570
3571 // Make sure the names match
3572 if (src_name == dst_name || (strcmp(src_name, dst_name) == 0))
3573 continue;
3574
3575 fast_path = false;
3576 }
3577 }
3578
3579 DWARFASTParserClang *src_dwarf_ast_parser =
3580 static_cast<DWARFASTParserClang *>(
3581 SymbolFileDWARF::GetDWARFParser(*src_class_die.GetCU()));
3582 DWARFASTParserClang *dst_dwarf_ast_parser =
3583 static_cast<DWARFASTParserClang *>(
3584 SymbolFileDWARF::GetDWARFParser(*dst_class_die.GetCU()));
3585 auto link = [&](DWARFDIE src, DWARFDIE dst) {
3586 SymbolFileDWARF::DIEToTypePtr &die_to_type =
3587 dst_class_die.GetDWARF()->GetDIEToType();
3588 clang::DeclContext *dst_decl_ctx =
3589 dst_dwarf_ast_parser->m_die_to_decl_ctx[dst.GetDIE()];
3590 if (dst_decl_ctx)
3591 src_dwarf_ast_parser->LinkDeclContextToDIE(dst_decl_ctx, src);
3592
3593 if (Type *src_child_type = die_to_type[src.GetDIE()])
3594 die_to_type[dst.GetDIE()] = src_child_type;
3595 };
3596
3597 // Now do the work of linking the DeclContexts and Types.
3598 if (fast_path) {
3599 // We can do this quickly. Just run across the tables index-for-index
3600 // since we know each node has matching names and tags.
3601 for (idx = 0; idx < src_size; ++idx) {
3602 link(src_name_to_die.GetValueAtIndexUnchecked(idx),
3603 dst_name_to_die.GetValueAtIndexUnchecked(idx));
3604 }
3605 } else {
3606 // We must do this slowly. For each member of the destination, look up a
3607 // member in the source with the same name, check its tag, and unique them
3608 // if everything matches up. Report failures.
3609
3610 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty()) {
3611 src_name_to_die.Sort();
3612
3613 for (idx = 0; idx < dst_size; ++idx) {
3614 ConstString dst_name = dst_name_to_die.GetCStringAtIndex(idx);
3615 DWARFDIE dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3616 DWARFDIE src_die = src_name_to_die.Find(dst_name, DWARFDIE());
3617
3618 if (src_die && (src_die.Tag() == dst_die.Tag()))
3619 link(src_die, dst_die);
3620 else
3621 failures.push_back(dst_die);
3622 }
3623 }
3624 }
3625
3626 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize();
3627 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize();
3628
3629 if (src_size_artificial && dst_size_artificial) {
3630 dst_name_to_die_artificial.Sort();
3631
3632 for (idx = 0; idx < src_size_artificial; ++idx) {
3633 ConstString src_name_artificial =
3634 src_name_to_die_artificial.GetCStringAtIndex(idx);
3635 DWARFDIE src_die =
3636 src_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3637 DWARFDIE dst_die =
3638 dst_name_to_die_artificial.Find(src_name_artificial, DWARFDIE());
3639
3640 // Both classes have the artificial types, link them
3641 if (dst_die)
3642 link(src_die, dst_die);
3643 }
3644 }
3645
3646 if (dst_size_artificial) {
3647 for (idx = 0; idx < dst_size_artificial; ++idx) {
3648 failures.push_back(
3649 dst_name_to_die_artificial.GetValueAtIndexUnchecked(idx));
3650 }
3651 }
3652
3653 return !failures.empty();
3654}
static bool DeclKindIsCXXClass(clang::Decl::Kind decl_kind)
static std::string GetUnitName(const DWARFDIE &die)
static void ForcefullyCompleteType(CompilerType type)
static clang::CallingConv ConvertDWARFCallingConventionToClang(const ParsedDWARFTypeAttributes &attrs)
static bool IsSubroutine(const DWARFDIE &die)
static lldb::ModuleSP GetContainingClangModule(const DWARFDIE &die)
static DWARFDIE FindFirstChildWithAbstractOrigin(const DWARFDIE &block, const DWARFDIE &function)
static DWARFDIE FindAnyChildWithAbstractOrigin(const DWARFDIE &context)
static bool IsClangModuleFwdDecl(const DWARFDIE &Die)
Detect a forward declaration that is nested in a DW_TAG_module.
static DWARFDIE GetContainingClangModuleDIE(const DWARFDIE &die)
static DWARFDIE GetContainingFunctionWithAbstractOrigin(const DWARFDIE &die)
static void PrepareContextToReceiveMembers(TypeSystemClang &ast, ClangASTImporter &ast_importer, clang::DeclContext *decl_ctx, DWARFDIE die, const char *type_name_cstr)
This function serves a similar purpose as RequireCompleteType above, but it avoids completing the typ...
#define DEBUG_PRINTF(fmt,...)
#define lldbassert(x)
Definition: LLDBAssert.h:15
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:337
#define LLDB_LOGF(log,...)
Definition: Log.h:344
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:360
#define DIE_IS_BEING_PARSED
std::unique_ptr< ClangASTMetadata > m_metadata_up
DelayedAddObjCClassProperty(const CompilerType &class_opaque_type, const char *property_name, const CompilerType &property_opaque_type, const char *property_setter_name, const char *property_getter_name, uint32_t property_attributes, const ClangASTMetadata *metadata)
DelayedAddObjCClassProperty(const DelayedAddObjCClassProperty &rhs)
DelayedAddObjCClassProperty & operator=(const DelayedAddObjCClassProperty &rhs)
bool CompleteRecordType(const DWARFDIE &die, lldb_private::Type *type, lldb_private::CompilerType &clang_type)
bool CompleteEnumType(const DWARFDIE &die, lldb_private::Type *type, lldb_private::CompilerType &clang_type)
lldb::TypeSP ParsePointerToMemberType(const DWARFDIE &die, const ParsedDWARFTypeAttributes &attrs)
bool ParseTemplateDIE(const DWARFDIE &die, lldb_private::TypeSystemClang::TemplateParameterInfos &template_param_infos)
llvm::Expected< llvm::APInt > ExtractIntFromFormValue(const lldb_private::CompilerType &int_type, const DWARFFormValue &form_value) const
Extracts an value for a given Clang integer type from a DWARFFormValue.
std::unique_ptr< lldb_private::ClangASTImporter > m_clang_ast_importer_up
lldb::TypeSP ParseSubroutine(const DWARFDIE &die, ParsedDWARFTypeAttributes &attrs)
size_t ParseChildParameters(clang::DeclContext *containing_decl_ctx, const DWARFDIE &parent_die, bool skip_artificial, bool &is_static, bool &is_variadic, bool &has_template_params, std::vector< lldb_private::CompilerType > &function_args, std::vector< clang::ParmVarDecl * > &function_param_decls, unsigned &type_quals)
lldb_private::TypeSystemClang & m_ast
lldb_private::Function * ParseFunctionFromDWARF(lldb_private::CompileUnit &comp_unit, const DWARFDIE &die, const lldb_private::AddressRange &func_range) override
lldb_private::ClangASTImporter & GetClangASTImporter()
clang::NamespaceDecl * ResolveNamespaceDIE(const DWARFDIE &die)
lldb_private::CompilerDeclContext GetDeclContextForUIDFromDWARF(const DWARFDIE &die) override
void ParseObjCProperty(const DWARFDIE &die, const DWARFDIE &parent_die, const lldb_private::CompilerType &class_clang_type, DelayedPropertyList &delayed_properties)
Parses a DW_TAG_APPLE_property DIE and appends the parsed data to the list of delayed Objective-C pro...
lldb_private::CompilerDeclContext GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) override
DIEToDeclContextMap m_die_to_decl_ctx
lldb_private::Type * GetTypeForDIE(const DWARFDIE &die)
std::string GetCPlusPlusQualifiedName(const DWARFDIE &die)
void EnsureAllDIEsInDeclContextHaveBeenParsed(lldb_private::CompilerDeclContext decl_context) override
lldb::TypeSP ParseArrayType(const DWARFDIE &die, const ParsedDWARFTypeAttributes &attrs)
size_t ParseChildEnumerators(lldb_private::CompilerType &compiler_type, bool is_signed, uint32_t enumerator_byte_size, const DWARFDIE &parent_die)
clang::DeclContext * GetCachedClangDeclContextForDIE(const DWARFDIE &die)
bool CopyUniqueClassMethodTypes(const DWARFDIE &src_class_die, const DWARFDIE &dst_class_die, lldb_private::Type *class_type, std::vector< DWARFDIE > &failures)
lldb::TypeSP UpdateSymbolContextScopeForType(const lldb_private::SymbolContext &sc, const DWARFDIE &die, lldb::TypeSP type_sp)
If type_sp is valid, calculate and set its symbol context scope, and update the type list for its bac...
lldb::TypeSP ParseTypeFromDWARF(const lldb_private::SymbolContext &sc, const DWARFDIE &die, bool *type_is_new_ptr) override
clang::DeclContext * GetClangDeclContextContainingDIE(const DWARFDIE &die, DWARFDIE *decl_ctx_die)
lldb::TypeSP ParseTypeModifier(const lldb_private::SymbolContext &sc, const DWARFDIE &die, ParsedDWARFTypeAttributes &attrs)
~DWARFASTParserClang() override
bool CompleteTypeFromDWARF(const DWARFDIE &die, lldb_private::Type *type, lldb_private::CompilerType &compiler_type) override
std::vector< DelayedAddObjCClassProperty > DelayedPropertyList
clang::BlockDecl * ResolveBlockDIE(const DWARFDIE &die)
lldb::TypeSP ParseStructureLikeDIE(const lldb_private::SymbolContext &sc, const DWARFDIE &die, ParsedDWARFTypeAttributes &attrs)
Parse a structure, class, or union type DIE.
lldb::TypeSP ParseEnum(const lldb_private::SymbolContext &sc, const DWARFDIE &die, ParsedDWARFTypeAttributes &attrs)
clang::DeclContext * GetDeclContextForBlock(const DWARFDIE &die)
lldb::TypeSP ParseTypeFromClangModule(const lldb_private::SymbolContext &sc, const DWARFDIE &die, lldb_private::Log *log)
Follow Clang Module Skeleton CU references to find a type definition.
bool ParseChildMembers(const DWARFDIE &die, lldb_private::CompilerType &class_compiler_type, std::vector< std::unique_ptr< clang::CXXBaseSpecifier > > &base_classes, std::vector< DWARFDIE > &member_function_dies, DelayedPropertyList &delayed_properties, const lldb::AccessType default_accessibility, lldb_private::ClangASTImporter::LayoutInfo &layout_info)
DIEToModuleMap m_die_to_module
DeclContextToDIEMap m_decl_ctx_to_die
void ParseInheritance(const DWARFDIE &die, const DWARFDIE &parent_die, const lldb_private::CompilerType class_clang_type, const lldb::AccessType default_accessibility, const lldb::ModuleSP &module_sp, std::vector< std::unique_ptr< clang::CXXBaseSpecifier > > &base_classes, lldb_private::ClangASTImporter::LayoutInfo &layout_info)
Parses a DW_TAG_inheritance DIE into a base/super class.
lldb_private::OptionalClangModuleID GetOwningClangModule(const DWARFDIE &die)
lldb_private::ConstString GetDIEClassTemplateParams(const DWARFDIE &die) override
Returns the template parameters of a class DWARFDIE as a string.
void LinkDeclContextToDIE(clang::DeclContext *decl_ctx, const DWARFDIE &die)
void ParseSingleMember(const DWARFDIE &die, const DWARFDIE &parent_die, const lldb_private::CompilerType &class_clang_type, lldb::AccessType default_accessibility, lldb_private::ClangASTImporter::LayoutInfo &layout_info, FieldInfo &last_field_info)
DWARFASTParserClang(lldb_private::TypeSystemClang &ast)
lldb_private::ConstString ConstructDemangledNameFromDWARF(const DWARFDIE &die) override
bool ParseTemplateParameterInfos(const DWARFDIE &parent_die, lldb_private::TypeSystemClang::TemplateParameterInfos &template_param_infos)
clang::DeclContext * GetClangDeclContextForDIE(const DWARFDIE &die)
clang::Decl * GetClangDeclForDIE(const DWARFDIE &die)
lldb_private::CompilerDecl GetDeclForUIDFromDWARF(const DWARFDIE &die) override
static lldb::AccessType GetAccessTypeFromDWARF(uint32_t dwarf_accessibility)
static std::optional< lldb_private::SymbolFile::ArrayInfo > ParseChildArrayInfo(const DWARFDIE &parent_die, const lldb_private::ExecutionContext *exe_ctx=nullptr)
dw_attr_t AttributeAtIndex(uint32_t i) const
DWARFUnit * CompileUnitAtIndex(uint32_t i) const
bool ExtractFormValueAtIndex(uint32_t i, DWARFFormValue &form_value) const
dw_tag_t Tag() const
const char * GetTagAsCString() const
bool HasChildren() const
size_t GetAttributes(DWARFAttributes &attributes, Recurse recurse=Recurse::yes) const
const lldb_private::DWARFDataExtractor & GetData() const
bool IsValid() const
Definition: DWARFBaseDIE.h:46
void Clear()
Definition: DWARFBaseDIE.h:70
const char * GetAttributeValueAsString(const dw_attr_t attr, const char *fail_value) const
DWARFUnit * GetCU() const
Definition: DWARFBaseDIE.h:55
std::optional< DIERef > GetDIERef() const
SymbolFileDWARF * GetDWARF() const
bool Supports_DW_AT_APPLE_objc_complete_type() const
uint64_t GetAttributeValueAsUnsigned(const dw_attr_t attr, uint64_t fail_value) const
dw_offset_t GetOffset() const
lldb::user_id_t GetID() const
void GetName(lldb_private::Stream &s) const
Definition: DWARFDIE.cpp:218
DWARFDIE GetFirstChild() const
Definition: DWARFDIE.cpp:94
DWARFDIE GetParent() const
Definition: DWARFDIE.cpp:86
void GetDeclContext(llvm::SmallVectorImpl< lldb_private::CompilerContext > &context) const
Return this DIE's decl context as it is needed to look up types in Clang's -gmodules debug info forma...
Definition: DWARFDIE.cpp:376
const char * GetMangledName() const
Definition: DWARFDIE.cpp:199
DWARFDIE GetDIE(dw_offset_t die_offset) const
Definition: DWARFDIE.cpp:118
llvm::iterator_range< child_iterator > children() const
The range of all the children of this DIE.
Definition: DWARFDIE.cpp:453
DWARFDIE GetParentDeclContextDIE() const
Definition: DWARFDIE.cpp:420
bool GetDIENamesAndRanges(const char *&name, const char *&mangled, DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column, int &call_file, int &call_line, int &call_column, lldb_private::DWARFExpressionList *frame_base) const
Definition: DWARFDIE.cpp:440
lldb_private::Type * ResolveTypeUID(const DWARFDIE &die) const
Definition: DWARFDIE.cpp:356
DWARFDIE GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const
Definition: DWARFDIE.cpp:126
DWARFDIE GetReferencedDIE(const dw_attr_t attr) const
Definition: DWARFDIE.cpp:110
lldb_private::Type * ResolveType() const
Definition: DWARFDIE.cpp:349
const char * GetQualifiedName() const
DWARFDIE Reference() const
bool IsValid() const
const char * AsCString() const
uint64_t Unsigned() const
const uint8_t * BlockData() const
bool Boolean() const
int64_t Signed() const
SymbolFileDWARF & GetSymbolFileDWARF() const
Definition: DWARFUnit.h:200
bool Supports_unnamed_objc_bitfields()
Definition: DWARFUnit.cpp:691
lldb_private::FileSpec GetFile(size_t file_idx)
Definition: DWARFUnit.cpp:789
lldb::TypeSP FindCompleteObjCDefinitionTypeForDIE(const DWARFDIE &die, lldb_private::ConstString type_name, bool must_be_implementation)
lldb::TypeSP FindDefinitionTypeForDWARFDeclContext(const DWARFDIE &die)
static DWARFASTParser * GetDWARFParser(DWARFUnit &unit)
static lldb::LanguageType GetLanguageFamily(DWARFUnit &unit)
Same as GetLanguage() but reports all C++ versions as C++ (no version).
lldb_private::Type * ResolveType(const DWARFDIE &die, bool assert_not_being_parsed=true, bool resolve_function_context=false)
llvm::DenseMap< const DWARFDebugInfoEntry *, lldb_private::Type * > DIEToTypePtr
lldb::ModuleSP GetExternalModule(lldb_private::ConstString name)
bool ForEachExternalModule(lldb_private::CompileUnit &, llvm::DenseSet< lldb_private::SymbolFile * > &, llvm::function_ref< bool(lldb_private::Module &)>) override
static lldb_private::CompilerDecl GetDecl(const DWARFDIE &die)
static lldb::LanguageType GetLanguage(DWARFUnit &unit)
static DWARFDeclContext GetDWARFDeclContext(const DWARFDIE &die)
static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die)
virtual DIEToTypePtr & GetDIEToType()
static lldb_private::CompilerDeclContext GetDeclContext(const DWARFDIE &die)
A section + offset based address range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:345
Block * FindBlockByID(lldb::user_id_t block_id)
Definition: Block.cpp:112
Manages and observes all Clang AST node importing in LLDB.
CompilerType CopyType(TypeSystemClang &dst, const CompilerType &src_type)
Copies the given type and the respective declarations to the destination type system.
bool CanImport(const CompilerType &type)
Returns true iff the given type was copied from another TypeSystemClang and the original type in this...
void SetRecordLayout(clang::RecordDecl *decl, const LayoutInfo &layout)
Sets the layout for the given RecordDecl.
bool RequireCompleteType(clang::QualType type)
void SetUserID(lldb::user_id_t user_id)
void SetObjectPtrName(const char *name)
A class that describes a compilation unit.
Definition: CompileUnit.h:41
void AddFunction(lldb::FunctionSP &function_sp)
Add a function to this compile unit.
Represents a generic declaration context in a program.
Represents a generic declaration such as a function declaration.
Definition: CompilerDecl.h:28
void * GetOpaqueDecl() const
Definition: CompilerDecl.h:58
std::shared_ptr< TypeSystemType > dyn_cast_or_null()
Return a shared_ptr<TypeSystemType> if dyn_cast succeeds.
Definition: CompilerType.h:68
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
bool IsArrayType(CompilerType *element_type=nullptr, uint64_t *size=nullptr, bool *is_incomplete=nullptr) const
lldb::opaque_compiler_type_t GetOpaqueQualType() const
Definition: CompilerType.h:232
bool IsIntegerOrEnumerationType(bool &is_signed) const
ConstString GetTypeName(bool BaseOnly=false) const
bool IsIntegerType(bool &is_signed) const
bool GetCompleteType() const
Type Completion.
std::optional< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
A uniqued constant string class.
Definition: ConstString.h:39
void SetCString(const char *cstr)
Set the C string value.
void SetString(const llvm::StringRef &s)
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:215
"lldb/Expression/DWARFExpressionList.h" Encapsulates a range map from file address range to a single ...
bool IsValid() const
Return true if the location expression contains data.
static bool Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx, lldb::ModuleSP module_sp, const DataExtractor &opcodes, const DWARFUnit *dwarf_cu, const lldb::RegisterKind reg_set, const Value *initial_value_ptr, const Value *object_address_ptr, Value &result, Status *error_ptr)
Evaluate a DWARF location expression in a particular context.
An data extractor class.
Definition: DataExtractor.h:48
const uint8_t * GetDataStart() const
Get the data start pointer.
A class that describes the declaration location of a lldb object.
Definition: Declaration.h:24
void SetLine(uint32_t line)
Set accessor for the declaration line number.
Definition: Declaration.h:168
void SetColumn(uint16_t column)
Set accessor for the declaration column number.
Definition: Declaration.h:175
void Clear()
Clear the object's state.
Definition: Declaration.h:57
void SetFile(const FileSpec &file_spec)
Set accessor for the declaration file specification.
Definition: Declaration.h:161
A class that describes a function.
Definition: Function.h:409
Block & GetBlock(bool can_create)
Get accessor for the block list.
Definition: Function.cpp:345
static bool LanguageIsCPlusPlus(lldb::LanguageType language)
Definition: Language.cpp:250
static bool LanguageIsObjC(lldb::LanguageType language)
Definition: Language.cpp:263
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:24
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:88
bool IsValid(bool strict) const
Definition: ObjCLanguage.h:45
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:62
virtual lldb::ByteOrder GetByteOrder() const =0
Gets whether endian swapping should occur when extracting data from this object file.
unsigned int UInt(unsigned int fail_value=0) const
Definition: Scalar.cpp:320
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
"lldb/Symbol/SymbolContextScope.h" Inherit from this if your object is part of a symbol context and c...
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
Function * function
The Function for a given query.
lldb::ModuleSP module_sp
The Module for a given query.
CompileUnit * comp_unit
The CompileUnit for a given query.
ObjectFile * GetObjectFile() override
Definition: SymbolFile.h:465
uint32_t GetSize() const
Definition: TypeMap.cpp:75
bool Empty() const
Definition: TypeMap.cpp:77
lldb::TypeSP GetTypeAtIndex(uint32_t idx)
Definition: TypeMap.cpp:83
The implementation of lldb::Type's m_payload field for TypeSystemClang.
void SetParameterPack(std::unique_ptr< TemplateParameterInfos > args)
void InsertArg(char const *name, clang::TemplateArgument arg)
TemplateParameterInfos const & GetParameterPack() const
A TypeSystem implementation based on Clang.
clang::TranslationUnitDecl * GetTranslationUnitDecl()
void SetMetadata(const clang::Decl *object, ClangASTMetadata &meta_data)
CompilerType GetBasicType(lldb::BasicType type)
clang::UsingDecl * CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, OptionalClangModuleID owning_module, clang::NamedDecl *target)
OptionalClangModuleID GetOrCreateClangModule(llvm::StringRef name, OptionalClangModuleID parent, bool is_framework=false, bool is_explicit=false)
Synthesize a clang::Module and return its ID or a default-constructed ID.
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, lldb::AccessType access, uint32_t bitfield_bit_size)
CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, size_t bit_size) override
clang::FunctionTemplateDecl * CreateFunctionTemplateDecl(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, clang::FunctionDecl *func_decl, const TemplateParameterInfos &infos)
bool SetDeclIsForcefullyCompleted(const clang::TagDecl *td)
static clang::DeclContext * GetDeclContextForType(clang::QualType type)
static bool IsCXXClassType(const CompilerType &type)
static bool AddObjCClassProperty(const CompilerType &type, const char *property_name, const CompilerType &property_compiler_type, clang::ObjCIvarDecl *ivar_decl, const char *property_setter_name, const char *property_getter_name, uint32_t property_attributes, ClangASTMetadata *metadata)
CompilerType GetType(clang::QualType qt)
Creates a CompilerType from the given QualType with the current TypeSystemClang instance as the Compi...
static clang::TagDecl * GetAsTagDecl(const CompilerType &type)
bool TransferBaseClasses(lldb::opaque_compiler_type_t type, std::vector< std::unique_ptr< clang::CXXBaseSpecifier > > bases)
static void SetIntegerInitializerForVariable(clang::VarDecl *var, const llvm::APInt &init_value)
Initializes a variable with an integer value.
CompilerType CreateClassTemplateSpecializationType(clang::ClassTemplateSpecializationDecl *class_template_specialization_decl)
void CreateFunctionTemplateSpecializationInfo(clang::FunctionDecl *func_decl, clang::FunctionTemplateDecl *Template, const TemplateParameterInfos &infos)
CompilerDecl GetCompilerDecl(clang::Decl *decl)
Creates a CompilerDecl from the given Decl with the current TypeSystemClang instance as its typesyste...
CompilerType GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) override
std::string PrintTemplateParams(const TemplateParameterInfos &template_param_infos)
Return the template parameters (including surrounding <>) in string form.
CompilerDeclContext CreateDeclContext(clang::DeclContext *ctx)
Creates a CompilerDeclContext from the given DeclContext with the current TypeSystemClang instance as...
void SetMetadataAsUserID(const clang::Decl *decl, lldb::user_id_t user_id)
static clang::RecordDecl * GetAsRecordDecl(const CompilerType &type)
clang::FunctionDecl * CreateFunctionDeclaration(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, llvm::StringRef name, const CompilerType &function_Type, clang::StorageClass storage, bool is_inline)
CompilerType GetBuiltinTypeForDWARFEncodingAndBitSize(llvm::StringRef type_name, uint32_t dw_ate, uint32_t bit_size)
static void BuildIndirectFields(const CompilerType &type)
void AddMethodOverridesForCXXRecordType(lldb::opaque_compiler_type_t type)
CompilerType CreateBlockPointerType(const CompilerType &function_type)
clang::ClassTemplateSpecializationDecl * CreateClassTemplateSpecializationDecl(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, clang::ClassTemplateDecl *class_template_decl, int kind, const TemplateParameterInfos &infos)
clang::CXXMethodDecl * AddMethodToCXXRecordType(lldb::opaque_compiler_type_t type, llvm::StringRef name, const char *mangled_name, const CompilerType &method_type, lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline, bool is_explicit, bool is_attr_used, bool is_artificial)
clang::EnumConstantDecl * AddEnumerationValueToEnumerationType(const CompilerType &enum_type, const Declaration &decl, const char *name, int64_t enum_value, uint32_t enum_value_bit_size)
clang::NamespaceDecl * GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, bool is_inline=false)
static clang::CXXRecordDecl * GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type)
clang::UsingDirectiveDecl * CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, clang::NamespaceDecl *ns_decl)
clang::ASTContext & getASTContext()
Returns the clang::ASTContext instance managed by this TypeSystemClang.
static clang::ObjCMethodDecl * AddMethodToObjCObjectType(const CompilerType &type, const char *name, const CompilerType &method_compiler_type, lldb::AccessType access, bool is_artificial, bool is_variadic, bool is_objc_direct_call)
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, bool has_extern)
CompilerType CreateEnumerationType(llvm::StringRef name, clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, const Declaration &decl, const CompilerType &integer_qual_type, bool is_scoped)
clang::ParmVarDecl * CreateParameterDeclaration(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, const char *name, const CompilerType &param_type, int storage, bool add_decl=false)
static clang::NamespaceDecl * DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc)
CompilerType CreateArrayType(const CompilerType &element_type, size_t element_count, bool is_vector)
CompilerType GetTypeForDecl(clang::NamedDecl *decl)
static clang::DeclContext * DeclContextGetAsDeclContext(const CompilerDeclContext &dc)
static bool StartTagDeclarationDefinition(const CompilerType &type)
static clang::VarDecl * AddVariableToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &var_type, lldb::AccessType access)
CompilerType CreateFunctionType(const CompilerType &result_type, const CompilerType *args, unsigned num_args, bool is_variadic, unsigned type_quals, clang::CallingConv cc=clang::CC_C, clang::RefQualifierKind ref_qual=clang::RQ_None)
void SetFunctionParameters(clang::FunctionDecl *function_decl, llvm::ArrayRef< clang::ParmVarDecl * > params)
static bool IsObjCObjectOrInterfaceType(const CompilerType &type)
CompilerType CreateRecordType(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, lldb::AccessType access_type, llvm::StringRef name, int kind, lldb::LanguageType language, ClangASTMetadata *metadata=nullptr, bool exports_symbols=false)
static void RequireCompleteType(CompilerType type)
Complete a type from debug info, or mark it as forcefully completed if there is no definition of the ...
clang::TemplateTemplateParmDecl * CreateTemplateTemplateParmDecl(const char *template_name)
clang::ClassTemplateDecl * ParseClassTemplateDecl(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, lldb::AccessType access_type, const char *parent_name, int tag_decl_kind, const TypeSystemClang::TemplateParameterInfos &template_param_infos)
static CompilerType CreateMemberPointerType(const CompilerType &type, const CompilerType &pointee_type)
clang::VarDecl * CreateVariableDeclaration(clang::DeclContext *decl_context, OptionalClangModuleID owning_module, const char *name, clang::QualType type)
clang::BlockDecl * CreateBlockDeclaration(clang::DeclContext *ctx, OptionalClangModuleID owning_module)
CompilerType GetForwardCompilerType()
Definition: Type.cpp:667
ConstString GetName()
Definition: Type.cpp:303
uint32_t GetEncodingMask()
Definition: Type.cpp:648
@ eEncodingIsRestrictUID
This type is the type whose UID is m_encoding_uid with the restrict qualifier added.
Definition: Type.h:80
@ eEncodingIsConstUID
This type is the type whose UID is m_encoding_uid with the const qualifier added.
Definition: Type.h:77
@ eEncodingIsVolatileUID
This type is the type whose UID is m_encoding_uid with the volatile qualifier added.
Definition: Type.h:83
@ eEncodingIsAtomicUID
This type is the type whose UID is m_encoding_uid as an atomic type.
Definition: Type.h:93
@ eEncodingInvalid
Invalid encoding.
Definition: Type.h:72
@ eEncodingIsTypedefUID
This type is alias to a type whose UID is m_encoding_uid.
Definition: Type.h:85
@ eEncodingIsPointerUID
This type is pointer to a type whose UID is m_encoding_uid.
Definition: Type.h:87
@ eEncodingIsLValueReferenceUID
This type is L value reference to a type whose UID is m_encoding_uid.
Definition: Type.h:89
@ eEncodingIsRValueReferenceUID
This type is R value reference to a type whose UID is m_encoding_uid.
Definition: Type.h:91
@ eEncodingIsUID
This type is the type whose UID is m_encoding_uid.
Definition: Type.h:74
CompilerType GetLayoutCompilerType()
Definition: Type.cpp:662
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope)
Definition: Type.cpp:345
CompilerType GetFullCompilerType()
Definition: Type.cpp:657
ConstString GetCStringAtIndex(uint32_t idx) const
T GetValueAtIndexUnchecked(uint32_t idx) const
T Find(ConstString unique_cstr, T fail_value) const
Scalar & ResolveValue(ExecutionContext *exe_ctx)
Definition: Value.cpp:561
llvm::dwarf::Tag dw_tag_t
Definition: dwarf.h:28
#define UINT64_MAX
Definition: lldb-defines.h:23
#define LLDB_INVALID_UID
Definition: lldb-defines.h:80
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
const char * DW_TAG_value_to_name(uint32_t val)
Definition: SBAddress.h:15
@ eBasicTypeNullPtr
@ eBasicTypeObjCSel
@ eBasicTypeObjCID
@ eBasicTypeObjCClass
LanguageType
Programming language type.
@ eLanguageTypeObjC_plus_plus
Objective-C++.
@ eLanguageTypeObjC
Objective-C.
@ eEncodingSint
signed integer
@ eByteOrderLittle
uint64_t user_id_t
Definition: lldb-types.h:84
@ eRegisterKindDWARF
the register numbers seen DWARF
bool NextBitfieldOffsetIsValid(const uint64_t next_bit_offset) const
Parsed form of all attributes that are relevant for type reconstruction.
lldb_private::Declaration decl
lldb_private::ConstString name
lldb::LanguageType class_language
std::optional< uint64_t > byte_size
ParsedDWARFTypeAttributes(const DWARFDIE &die)
clang::RefQualifierKind ref_qual
Indicates ref-qualifier of C++ member function if present.
llvm::DenseMap< const clang::FieldDecl *, uint64_t > field_offsets
static clang::QualType GetQualType(const CompilerType &ct)
Definition: ClangUtil.cpp:36
static CompilerType RemoveFastQualifiers(const CompilerType &ct)
Definition: ClangUtil.cpp:51
static clang::TagDecl * GetAsTagDecl(const CompilerType &type)
Definition: ClangUtil.cpp:60
A SmallBitVector that represents a set of source languages (lldb::LanguageType).
Definition: TypeSystem.h:45
void Insert(lldb::LanguageType language)
Definition: TypeSystem.cpp:34
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47