15ffd83dbSDimitry Andric //===-- DWARFDIE.cpp ------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "DWARFDIE.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "DWARFASTParser.h"
120b57cec5SDimitry Andric #include "DWARFDebugInfo.h"
130b57cec5SDimitry Andric #include "DWARFDebugInfoEntry.h"
140b57cec5SDimitry Andric #include "DWARFDeclContext.h"
150b57cec5SDimitry Andric #include "DWARFUnit.h"
160b57cec5SDimitry Andric 
17bdd1243dSDimitry Andric #include "llvm/ADT/iterator.h"
18bdd1243dSDimitry Andric 
190b57cec5SDimitry Andric using namespace lldb_private;
2081ad6265SDimitry Andric using namespace lldb_private::dwarf;
215f757f3fSDimitry Andric using namespace lldb_private::plugin::dwarf;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace {
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric /// Iterate through all DIEs elaborating (i.e. reachable by a chain of
260b57cec5SDimitry Andric /// DW_AT_specification and DW_AT_abstract_origin attributes) a given DIE. For
270b57cec5SDimitry Andric /// convenience, the starting die is included in the sequence as the first
280b57cec5SDimitry Andric /// item.
290b57cec5SDimitry Andric class ElaboratingDIEIterator
30bdd1243dSDimitry Andric     : public llvm::iterator_facade_base<
31bdd1243dSDimitry Andric           ElaboratingDIEIterator, std::input_iterator_tag, DWARFDIE,
32bdd1243dSDimitry Andric           std::ptrdiff_t, DWARFDIE *, DWARFDIE *> {
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric   // The operating invariant is: top of m_worklist contains the "current" item
350b57cec5SDimitry Andric   // and the rest of the list are items yet to be visited. An empty worklist
360b57cec5SDimitry Andric   // means we've reached the end.
370b57cec5SDimitry Andric   // Infinite recursion is prevented by maintaining a list of seen DIEs.
380b57cec5SDimitry Andric   // Container sizes are optimized for the case of following DW_AT_specification
390b57cec5SDimitry Andric   // and DW_AT_abstract_origin just once.
400b57cec5SDimitry Andric   llvm::SmallVector<DWARFDIE, 2> m_worklist;
415ffd83dbSDimitry Andric   llvm::SmallSet<DWARFDebugInfoEntry *, 3> m_seen;
420b57cec5SDimitry Andric 
Next()430b57cec5SDimitry Andric   void Next() {
440b57cec5SDimitry Andric     assert(!m_worklist.empty() && "Incrementing end iterator?");
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric     // Pop the current item from the list.
470b57cec5SDimitry Andric     DWARFDIE die = m_worklist.back();
480b57cec5SDimitry Andric     m_worklist.pop_back();
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric     // And add back any items that elaborate it.
510b57cec5SDimitry Andric     for (dw_attr_t attr : {DW_AT_specification, DW_AT_abstract_origin}) {
520b57cec5SDimitry Andric       if (DWARFDIE d = die.GetReferencedDIE(attr))
535ffd83dbSDimitry Andric         if (m_seen.insert(die.GetDIE()).second)
540b57cec5SDimitry Andric           m_worklist.push_back(d);
550b57cec5SDimitry Andric     }
560b57cec5SDimitry Andric   }
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric public:
590b57cec5SDimitry Andric   /// An iterator starting at die d.
ElaboratingDIEIterator(DWARFDIE d)600b57cec5SDimitry Andric   explicit ElaboratingDIEIterator(DWARFDIE d) : m_worklist(1, d) {}
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   /// End marker
63fe6060f1SDimitry Andric   ElaboratingDIEIterator() = default;
640b57cec5SDimitry Andric 
operator *() const650b57cec5SDimitry Andric   const DWARFDIE &operator*() const { return m_worklist.back(); }
operator ++()660b57cec5SDimitry Andric   ElaboratingDIEIterator &operator++() {
670b57cec5SDimitry Andric     Next();
680b57cec5SDimitry Andric     return *this;
690b57cec5SDimitry Andric   }
700b57cec5SDimitry Andric 
operator ==(const ElaboratingDIEIterator & a,const ElaboratingDIEIterator & b)710b57cec5SDimitry Andric   friend bool operator==(const ElaboratingDIEIterator &a,
720b57cec5SDimitry Andric                          const ElaboratingDIEIterator &b) {
730b57cec5SDimitry Andric     if (a.m_worklist.empty() || b.m_worklist.empty())
740b57cec5SDimitry Andric       return a.m_worklist.empty() == b.m_worklist.empty();
750b57cec5SDimitry Andric     return a.m_worklist.back() == b.m_worklist.back();
760b57cec5SDimitry Andric   }
770b57cec5SDimitry Andric };
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric llvm::iterator_range<ElaboratingDIEIterator>
elaborating_dies(const DWARFDIE & die)800b57cec5SDimitry Andric elaborating_dies(const DWARFDIE &die) {
810b57cec5SDimitry Andric   return llvm::make_range(ElaboratingDIEIterator(die),
820b57cec5SDimitry Andric                           ElaboratingDIEIterator());
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric } // namespace
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric DWARFDIE
GetParent() const870b57cec5SDimitry Andric DWARFDIE::GetParent() const {
880b57cec5SDimitry Andric   if (IsValid())
890b57cec5SDimitry Andric     return DWARFDIE(m_cu, m_die->GetParent());
900b57cec5SDimitry Andric   else
910b57cec5SDimitry Andric     return DWARFDIE();
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric DWARFDIE
GetFirstChild() const950b57cec5SDimitry Andric DWARFDIE::GetFirstChild() const {
960b57cec5SDimitry Andric   if (IsValid())
970b57cec5SDimitry Andric     return DWARFDIE(m_cu, m_die->GetFirstChild());
980b57cec5SDimitry Andric   else
990b57cec5SDimitry Andric     return DWARFDIE();
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric DWARFDIE
GetSibling() const1030b57cec5SDimitry Andric DWARFDIE::GetSibling() const {
1040b57cec5SDimitry Andric   if (IsValid())
1050b57cec5SDimitry Andric     return DWARFDIE(m_cu, m_die->GetSibling());
1060b57cec5SDimitry Andric   else
1070b57cec5SDimitry Andric     return DWARFDIE();
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric DWARFDIE
GetReferencedDIE(const dw_attr_t attr) const1110b57cec5SDimitry Andric DWARFDIE::GetReferencedDIE(const dw_attr_t attr) const {
1120b57cec5SDimitry Andric   if (IsValid())
1130b57cec5SDimitry Andric     return m_die->GetAttributeValueAsReference(GetCU(), attr);
1140b57cec5SDimitry Andric   else
1150b57cec5SDimitry Andric     return {};
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric DWARFDIE
GetDIE(dw_offset_t die_offset) const1190b57cec5SDimitry Andric DWARFDIE::GetDIE(dw_offset_t die_offset) const {
1200b57cec5SDimitry Andric   if (IsValid())
1210b57cec5SDimitry Andric     return m_cu->GetDIE(die_offset);
1220b57cec5SDimitry Andric   else
1230b57cec5SDimitry Andric     return DWARFDIE();
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric DWARFDIE
GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const1270b57cec5SDimitry Andric DWARFDIE::GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const {
1280b57cec5SDimitry Andric   if (IsValid()) {
1290b57cec5SDimitry Andric     DWARFUnit *cu = GetCU();
1300b57cec5SDimitry Andric     const bool check_specification_or_abstract_origin = true;
1310b57cec5SDimitry Andric     DWARFFormValue form_value;
1320b57cec5SDimitry Andric     if (m_die->GetAttributeValue(cu, attr, form_value, nullptr,
1330b57cec5SDimitry Andric                                  check_specification_or_abstract_origin))
1340b57cec5SDimitry Andric       return form_value.Reference();
1350b57cec5SDimitry Andric   }
1360b57cec5SDimitry Andric   return DWARFDIE();
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric DWARFDIE
LookupDeepestBlock(lldb::addr_t address) const1405ffd83dbSDimitry Andric DWARFDIE::LookupDeepestBlock(lldb::addr_t address) const {
1415ffd83dbSDimitry Andric   if (!IsValid())
1420b57cec5SDimitry Andric     return DWARFDIE();
1435ffd83dbSDimitry Andric 
1445ffd83dbSDimitry Andric   DWARFDIE result;
1455ffd83dbSDimitry Andric   bool check_children = false;
1465ffd83dbSDimitry Andric   bool match_addr_range = false;
1475ffd83dbSDimitry Andric   switch (Tag()) {
1485ffd83dbSDimitry Andric   case DW_TAG_class_type:
1495ffd83dbSDimitry Andric   case DW_TAG_namespace:
1505ffd83dbSDimitry Andric   case DW_TAG_structure_type:
1515ffd83dbSDimitry Andric   case DW_TAG_common_block:
1525ffd83dbSDimitry Andric     check_children = true;
1535ffd83dbSDimitry Andric     break;
1545ffd83dbSDimitry Andric   case DW_TAG_compile_unit:
1555ffd83dbSDimitry Andric   case DW_TAG_module:
1565ffd83dbSDimitry Andric   case DW_TAG_catch_block:
1575ffd83dbSDimitry Andric   case DW_TAG_subprogram:
1585ffd83dbSDimitry Andric   case DW_TAG_try_block:
1595ffd83dbSDimitry Andric   case DW_TAG_partial_unit:
1605ffd83dbSDimitry Andric     match_addr_range = true;
1615ffd83dbSDimitry Andric     break;
1625ffd83dbSDimitry Andric   case DW_TAG_lexical_block:
1635ffd83dbSDimitry Andric   case DW_TAG_inlined_subroutine:
1645ffd83dbSDimitry Andric     check_children = true;
1655ffd83dbSDimitry Andric     match_addr_range = true;
1665ffd83dbSDimitry Andric     break;
1675ffd83dbSDimitry Andric   default:
1685ffd83dbSDimitry Andric     break;
1695ffd83dbSDimitry Andric   }
1705ffd83dbSDimitry Andric 
1715ffd83dbSDimitry Andric   if (match_addr_range) {
17206c3fb27SDimitry Andric     DWARFRangeList ranges =
17306c3fb27SDimitry Andric         m_die->GetAttributeAddressRanges(m_cu, /*check_hi_lo_pc=*/true);
17406c3fb27SDimitry Andric     if (ranges.FindEntryThatContains(address)) {
1755ffd83dbSDimitry Andric       check_children = true;
1765ffd83dbSDimitry Andric       switch (Tag()) {
1775ffd83dbSDimitry Andric       default:
1785ffd83dbSDimitry Andric         break;
1795ffd83dbSDimitry Andric 
1805ffd83dbSDimitry Andric       case DW_TAG_inlined_subroutine: // Inlined Function
1815ffd83dbSDimitry Andric       case DW_TAG_lexical_block:      // Block { } in code
1825ffd83dbSDimitry Andric         result = *this;
1835ffd83dbSDimitry Andric         break;
1845ffd83dbSDimitry Andric       }
1855ffd83dbSDimitry Andric     } else {
1865ffd83dbSDimitry Andric       check_children = false;
1875ffd83dbSDimitry Andric     }
1885ffd83dbSDimitry Andric   }
1895ffd83dbSDimitry Andric 
1905ffd83dbSDimitry Andric   if (check_children) {
191fe6060f1SDimitry Andric     for (DWARFDIE child : children()) {
1925ffd83dbSDimitry Andric       if (DWARFDIE child_result = child.LookupDeepestBlock(address))
1935ffd83dbSDimitry Andric         return child_result;
1945ffd83dbSDimitry Andric     }
1955ffd83dbSDimitry Andric   }
1965ffd83dbSDimitry Andric   return result;
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
GetMangledName() const1990b57cec5SDimitry Andric const char *DWARFDIE::GetMangledName() const {
2000b57cec5SDimitry Andric   if (IsValid())
2010b57cec5SDimitry Andric     return m_die->GetMangledName(m_cu);
2020b57cec5SDimitry Andric   else
2030b57cec5SDimitry Andric     return nullptr;
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric 
GetPubname() const2060b57cec5SDimitry Andric const char *DWARFDIE::GetPubname() const {
2070b57cec5SDimitry Andric   if (IsValid())
2080b57cec5SDimitry Andric     return m_die->GetPubname(m_cu);
2090b57cec5SDimitry Andric   else
2100b57cec5SDimitry Andric     return nullptr;
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric // GetName
2140b57cec5SDimitry Andric //
2150b57cec5SDimitry Andric // Get value of the DW_AT_name attribute and place that value into the supplied
2160b57cec5SDimitry Andric // stream object. If the DIE is a NULL object "NULL" is placed into the stream,
2170b57cec5SDimitry Andric // and if no DW_AT_name attribute exists for the DIE then nothing is printed.
GetName(Stream & s) const2180b57cec5SDimitry Andric void DWARFDIE::GetName(Stream &s) const {
2190b57cec5SDimitry Andric   if (!IsValid())
2200b57cec5SDimitry Andric     return;
2210b57cec5SDimitry Andric   if (GetDIE()->IsNULL()) {
2220b57cec5SDimitry Andric     s.PutCString("NULL");
2230b57cec5SDimitry Andric     return;
2240b57cec5SDimitry Andric   }
2250b57cec5SDimitry Andric   const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true);
2260b57cec5SDimitry Andric   if (!name)
2270b57cec5SDimitry Andric     return;
2280b57cec5SDimitry Andric   s.PutCString(name);
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric // AppendTypeName
2320b57cec5SDimitry Andric //
2330b57cec5SDimitry Andric // Follows the type name definition down through all needed tags to end up with
2340b57cec5SDimitry Andric // a fully qualified type name and dump the results to the supplied stream.
2350b57cec5SDimitry Andric // This is used to show the name of types given a type identifier.
AppendTypeName(Stream & s) const2360b57cec5SDimitry Andric void DWARFDIE::AppendTypeName(Stream &s) const {
2370b57cec5SDimitry Andric   if (!IsValid())
2380b57cec5SDimitry Andric     return;
2390b57cec5SDimitry Andric   if (GetDIE()->IsNULL()) {
2400b57cec5SDimitry Andric     s.PutCString("NULL");
2410b57cec5SDimitry Andric     return;
2420b57cec5SDimitry Andric   }
2430b57cec5SDimitry Andric   if (const char *name = GetPubname()) {
2440b57cec5SDimitry Andric     s.PutCString(name);
2450b57cec5SDimitry Andric     return;
2460b57cec5SDimitry Andric   }
2470b57cec5SDimitry Andric   switch (Tag()) {
2480b57cec5SDimitry Andric   case DW_TAG_array_type:
2490b57cec5SDimitry Andric     break; // print out a "[]" after printing the full type of the element
2500b57cec5SDimitry Andric            // below
2510b57cec5SDimitry Andric   case DW_TAG_base_type:
2520b57cec5SDimitry Andric     s.PutCString("base ");
2530b57cec5SDimitry Andric     break;
2540b57cec5SDimitry Andric   case DW_TAG_class_type:
2550b57cec5SDimitry Andric     s.PutCString("class ");
2560b57cec5SDimitry Andric     break;
2570b57cec5SDimitry Andric   case DW_TAG_const_type:
2580b57cec5SDimitry Andric     s.PutCString("const ");
2590b57cec5SDimitry Andric     break;
2600b57cec5SDimitry Andric   case DW_TAG_enumeration_type:
2610b57cec5SDimitry Andric     s.PutCString("enum ");
2620b57cec5SDimitry Andric     break;
2630b57cec5SDimitry Andric   case DW_TAG_file_type:
2640b57cec5SDimitry Andric     s.PutCString("file ");
2650b57cec5SDimitry Andric     break;
2660b57cec5SDimitry Andric   case DW_TAG_interface_type:
2670b57cec5SDimitry Andric     s.PutCString("interface ");
2680b57cec5SDimitry Andric     break;
2690b57cec5SDimitry Andric   case DW_TAG_packed_type:
2700b57cec5SDimitry Andric     s.PutCString("packed ");
2710b57cec5SDimitry Andric     break;
2720b57cec5SDimitry Andric   case DW_TAG_pointer_type:
2730b57cec5SDimitry Andric     break; // print out a '*' after printing the full type below
2740b57cec5SDimitry Andric   case DW_TAG_ptr_to_member_type:
2750b57cec5SDimitry Andric     break; // print out a '*' after printing the full type below
2760b57cec5SDimitry Andric   case DW_TAG_reference_type:
2770b57cec5SDimitry Andric     break; // print out a '&' after printing the full type below
2780b57cec5SDimitry Andric   case DW_TAG_restrict_type:
2790b57cec5SDimitry Andric     s.PutCString("restrict ");
2800b57cec5SDimitry Andric     break;
2810b57cec5SDimitry Andric   case DW_TAG_set_type:
2820b57cec5SDimitry Andric     s.PutCString("set ");
2830b57cec5SDimitry Andric     break;
2840b57cec5SDimitry Andric   case DW_TAG_shared_type:
2850b57cec5SDimitry Andric     s.PutCString("shared ");
2860b57cec5SDimitry Andric     break;
2870b57cec5SDimitry Andric   case DW_TAG_string_type:
2880b57cec5SDimitry Andric     s.PutCString("string ");
2890b57cec5SDimitry Andric     break;
2900b57cec5SDimitry Andric   case DW_TAG_structure_type:
2910b57cec5SDimitry Andric     s.PutCString("struct ");
2920b57cec5SDimitry Andric     break;
2930b57cec5SDimitry Andric   case DW_TAG_subrange_type:
2940b57cec5SDimitry Andric     s.PutCString("subrange ");
2950b57cec5SDimitry Andric     break;
2960b57cec5SDimitry Andric   case DW_TAG_subroutine_type:
2970b57cec5SDimitry Andric     s.PutCString("function ");
2980b57cec5SDimitry Andric     break;
2990b57cec5SDimitry Andric   case DW_TAG_thrown_type:
3000b57cec5SDimitry Andric     s.PutCString("thrown ");
3010b57cec5SDimitry Andric     break;
3020b57cec5SDimitry Andric   case DW_TAG_union_type:
3030b57cec5SDimitry Andric     s.PutCString("union ");
3040b57cec5SDimitry Andric     break;
3050b57cec5SDimitry Andric   case DW_TAG_unspecified_type:
3060b57cec5SDimitry Andric     s.PutCString("unspecified ");
3070b57cec5SDimitry Andric     break;
3080b57cec5SDimitry Andric   case DW_TAG_volatile_type:
3090b57cec5SDimitry Andric     s.PutCString("volatile ");
3100b57cec5SDimitry Andric     break;
311bdd1243dSDimitry Andric   case DW_TAG_LLVM_ptrauth_type: {
312bdd1243dSDimitry Andric     unsigned key = GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_key, 0);
313bdd1243dSDimitry Andric     bool isAddressDiscriminated = GetAttributeValueAsUnsigned(
314bdd1243dSDimitry Andric         DW_AT_LLVM_ptrauth_address_discriminated, 0);
315bdd1243dSDimitry Andric     unsigned extraDiscriminator =
316bdd1243dSDimitry Andric         GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_extra_discriminator, 0);
317bdd1243dSDimitry Andric     bool isaPointer =
318bdd1243dSDimitry Andric         GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_isa_pointer, 0);
319bdd1243dSDimitry Andric     s.Printf("__ptrauth(%d, %d, 0x0%x, %d)", key, isAddressDiscriminated,
320bdd1243dSDimitry Andric              extraDiscriminator, isaPointer);
321bdd1243dSDimitry Andric     break;
322bdd1243dSDimitry Andric   }
3230b57cec5SDimitry Andric   default:
3240b57cec5SDimitry Andric     return;
3250b57cec5SDimitry Andric   }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric   // Follow the DW_AT_type if possible
3280b57cec5SDimitry Andric   if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type))
3290b57cec5SDimitry Andric     next_die.AppendTypeName(s);
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric   switch (Tag()) {
3320b57cec5SDimitry Andric   case DW_TAG_array_type:
3330b57cec5SDimitry Andric     s.PutCString("[]");
3340b57cec5SDimitry Andric     break;
3350b57cec5SDimitry Andric   case DW_TAG_pointer_type:
3360b57cec5SDimitry Andric     s.PutChar('*');
3370b57cec5SDimitry Andric     break;
3380b57cec5SDimitry Andric   case DW_TAG_ptr_to_member_type:
3390b57cec5SDimitry Andric     s.PutChar('*');
3400b57cec5SDimitry Andric     break;
3410b57cec5SDimitry Andric   case DW_TAG_reference_type:
3420b57cec5SDimitry Andric     s.PutChar('&');
3430b57cec5SDimitry Andric     break;
3440b57cec5SDimitry Andric   default:
3450b57cec5SDimitry Andric     break;
3460b57cec5SDimitry Andric   }
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric 
ResolveType() const3490b57cec5SDimitry Andric lldb_private::Type *DWARFDIE::ResolveType() const {
3500b57cec5SDimitry Andric   if (IsValid())
3510b57cec5SDimitry Andric     return GetDWARF()->ResolveType(*this, true);
3520b57cec5SDimitry Andric   else
3530b57cec5SDimitry Andric     return nullptr;
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric 
ResolveTypeUID(const DWARFDIE & die) const3560b57cec5SDimitry Andric lldb_private::Type *DWARFDIE::ResolveTypeUID(const DWARFDIE &die) const {
3570b57cec5SDimitry Andric   if (SymbolFileDWARF *dwarf = GetDWARF())
3580b57cec5SDimitry Andric     return dwarf->ResolveTypeUID(die, true);
3590b57cec5SDimitry Andric   return nullptr;
3600b57cec5SDimitry Andric }
3610b57cec5SDimitry Andric 
GetDeclContextDIEs() const3620b57cec5SDimitry Andric std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
3630b57cec5SDimitry Andric   if (!IsValid())
3640b57cec5SDimitry Andric     return {};
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric   std::vector<DWARFDIE> result;
3670b57cec5SDimitry Andric   DWARFDIE parent = GetParentDeclContextDIE();
3680b57cec5SDimitry Andric   while (parent.IsValid() && parent.GetDIE() != GetDIE()) {
3690b57cec5SDimitry Andric     result.push_back(std::move(parent));
3700b57cec5SDimitry Andric     parent = parent.GetParentDeclContextDIE();
3710b57cec5SDimitry Andric   }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   return result;
3740b57cec5SDimitry Andric }
3750b57cec5SDimitry Andric 
376297eecfbSDimitry Andric static std::vector<lldb_private::CompilerContext>
GetDeclContextImpl(llvm::SmallSet<lldb::user_id_t,4> & seen,DWARFDIE die)377297eecfbSDimitry Andric GetDeclContextImpl(llvm::SmallSet<lldb::user_id_t, 4> &seen, DWARFDIE die) {
3785f757f3fSDimitry Andric   std::vector<lldb_private::CompilerContext> context;
379297eecfbSDimitry Andric   // Stop if we hit a cycle.
380297eecfbSDimitry Andric   if (!die || !seen.insert(die.GetID()).second)
3815f757f3fSDimitry Andric     return context;
382297eecfbSDimitry Andric 
383297eecfbSDimitry Andric   // Handle outline member function DIEs by following the specification.
384297eecfbSDimitry Andric   if (DWARFDIE spec = die.GetReferencedDIE(DW_AT_specification))
385297eecfbSDimitry Andric     return GetDeclContextImpl(seen, spec);
386297eecfbSDimitry Andric 
387297eecfbSDimitry Andric   // Get the parent context chain.
388297eecfbSDimitry Andric   context = GetDeclContextImpl(seen, die.GetParent());
389297eecfbSDimitry Andric 
390297eecfbSDimitry Andric   // Add this DIE's contribution at the end of the chain.
3915f757f3fSDimitry Andric   auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
3925f757f3fSDimitry Andric     context.push_back({kind, ConstString(name)});
3935f757f3fSDimitry Andric   };
394297eecfbSDimitry Andric   switch (die.Tag()) {
3950b57cec5SDimitry Andric   case DW_TAG_module:
396297eecfbSDimitry Andric     push_ctx(CompilerContextKind::Module, die.GetName());
3970b57cec5SDimitry Andric     break;
3980b57cec5SDimitry Andric   case DW_TAG_namespace:
399297eecfbSDimitry Andric     push_ctx(CompilerContextKind::Namespace, die.GetName());
4000b57cec5SDimitry Andric     break;
4010b57cec5SDimitry Andric   case DW_TAG_structure_type:
402297eecfbSDimitry Andric     push_ctx(CompilerContextKind::Struct, die.GetName());
4030b57cec5SDimitry Andric     break;
4040b57cec5SDimitry Andric   case DW_TAG_union_type:
405297eecfbSDimitry Andric     push_ctx(CompilerContextKind::Union, die.GetName());
4060b57cec5SDimitry Andric     break;
4070b57cec5SDimitry Andric   case DW_TAG_class_type:
408297eecfbSDimitry Andric     push_ctx(CompilerContextKind::Class, die.GetName());
4090b57cec5SDimitry Andric     break;
4100b57cec5SDimitry Andric   case DW_TAG_enumeration_type:
411297eecfbSDimitry Andric     push_ctx(CompilerContextKind::Enum, die.GetName());
4120b57cec5SDimitry Andric     break;
4130b57cec5SDimitry Andric   case DW_TAG_subprogram:
414297eecfbSDimitry Andric     push_ctx(CompilerContextKind::Function, die.GetName());
4150b57cec5SDimitry Andric     break;
4160b57cec5SDimitry Andric   case DW_TAG_variable:
417297eecfbSDimitry Andric     push_ctx(CompilerContextKind::Variable, die.GetPubname());
4180b57cec5SDimitry Andric     break;
4190b57cec5SDimitry Andric   case DW_TAG_typedef:
420297eecfbSDimitry Andric     push_ctx(CompilerContextKind::Typedef, die.GetName());
4210b57cec5SDimitry Andric     break;
4220b57cec5SDimitry Andric   default:
4230b57cec5SDimitry Andric     break;
4240b57cec5SDimitry Andric   }
4255f757f3fSDimitry Andric   return context;
4265f757f3fSDimitry Andric }
4275f757f3fSDimitry Andric 
GetDeclContext() const428297eecfbSDimitry Andric std::vector<lldb_private::CompilerContext> DWARFDIE::GetDeclContext() const {
429297eecfbSDimitry Andric   llvm::SmallSet<lldb::user_id_t, 4> seen;
430297eecfbSDimitry Andric   return GetDeclContextImpl(seen, *this);
431297eecfbSDimitry Andric }
432297eecfbSDimitry Andric 
4335f757f3fSDimitry Andric std::vector<lldb_private::CompilerContext>
GetTypeLookupContext() const4345f757f3fSDimitry Andric DWARFDIE::GetTypeLookupContext() const {
4355f757f3fSDimitry Andric   std::vector<lldb_private::CompilerContext> context;
4365f757f3fSDimitry Andric   // If there is no name, then there is no need to look anything up for this
4375f757f3fSDimitry Andric   // DIE.
4385f757f3fSDimitry Andric   const char *name = GetName();
4395f757f3fSDimitry Andric   if (!name || !name[0])
4405f757f3fSDimitry Andric     return context;
4415f757f3fSDimitry Andric   const dw_tag_t tag = Tag();
4425f757f3fSDimitry Andric   if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
4435f757f3fSDimitry Andric     return context;
4445f757f3fSDimitry Andric   DWARFDIE parent = GetParent();
4455f757f3fSDimitry Andric   if (parent)
4465f757f3fSDimitry Andric     context = parent.GetTypeLookupContext();
4475f757f3fSDimitry Andric   auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
4485f757f3fSDimitry Andric     context.push_back({kind, ConstString(name)});
4495f757f3fSDimitry Andric   };
4505f757f3fSDimitry Andric   switch (tag) {
4515f757f3fSDimitry Andric   case DW_TAG_namespace:
4525f757f3fSDimitry Andric     push_ctx(CompilerContextKind::Namespace, name);
4535f757f3fSDimitry Andric     break;
4545f757f3fSDimitry Andric   case DW_TAG_structure_type:
4555f757f3fSDimitry Andric     push_ctx(CompilerContextKind::Struct, name);
4565f757f3fSDimitry Andric     break;
4575f757f3fSDimitry Andric   case DW_TAG_union_type:
4585f757f3fSDimitry Andric     push_ctx(CompilerContextKind::Union, name);
4595f757f3fSDimitry Andric     break;
4605f757f3fSDimitry Andric   case DW_TAG_class_type:
4615f757f3fSDimitry Andric     push_ctx(CompilerContextKind::Class, name);
4625f757f3fSDimitry Andric     break;
4635f757f3fSDimitry Andric   case DW_TAG_enumeration_type:
4645f757f3fSDimitry Andric     push_ctx(CompilerContextKind::Enum, name);
4655f757f3fSDimitry Andric     break;
4665f757f3fSDimitry Andric   case DW_TAG_variable:
4675f757f3fSDimitry Andric     push_ctx(CompilerContextKind::Variable, GetPubname());
4685f757f3fSDimitry Andric     break;
4695f757f3fSDimitry Andric   case DW_TAG_typedef:
4705f757f3fSDimitry Andric     push_ctx(CompilerContextKind::Typedef, name);
4715f757f3fSDimitry Andric     break;
4725f757f3fSDimitry Andric   case DW_TAG_base_type:
4735f757f3fSDimitry Andric     push_ctx(CompilerContextKind::Builtin, name);
4745f757f3fSDimitry Andric     break;
4755f757f3fSDimitry Andric   default:
4765f757f3fSDimitry Andric     break;
4775f757f3fSDimitry Andric   }
4785f757f3fSDimitry Andric   return context;
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric DWARFDIE
GetParentDeclContextDIE() const4820b57cec5SDimitry Andric DWARFDIE::GetParentDeclContextDIE() const {
4830b57cec5SDimitry Andric   if (IsValid())
4840b57cec5SDimitry Andric     return m_die->GetParentDeclContextDIE(m_cu);
4850b57cec5SDimitry Andric   else
4860b57cec5SDimitry Andric     return DWARFDIE();
4870b57cec5SDimitry Andric }
4880b57cec5SDimitry Andric 
IsStructUnionOrClass() const4890b57cec5SDimitry Andric bool DWARFDIE::IsStructUnionOrClass() const {
4900b57cec5SDimitry Andric   const dw_tag_t tag = Tag();
4910b57cec5SDimitry Andric   return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
4920b57cec5SDimitry Andric          tag == DW_TAG_union_type;
4930b57cec5SDimitry Andric }
4940b57cec5SDimitry Andric 
IsMethod() const4950b57cec5SDimitry Andric bool DWARFDIE::IsMethod() const {
4960b57cec5SDimitry Andric   for (DWARFDIE d : elaborating_dies(*this))
4970b57cec5SDimitry Andric     if (d.GetParent().IsStructUnionOrClass())
4980b57cec5SDimitry Andric       return true;
4990b57cec5SDimitry Andric   return false;
5000b57cec5SDimitry Andric }
5010b57cec5SDimitry Andric 
GetDIENamesAndRanges(const char * & name,const char * & mangled,DWARFRangeList & ranges,std::optional<int> & decl_file,std::optional<int> & decl_line,std::optional<int> & decl_column,std::optional<int> & call_file,std::optional<int> & call_line,std::optional<int> & call_column,lldb_private::DWARFExpressionList * frame_base) const5020b57cec5SDimitry Andric bool DWARFDIE::GetDIENamesAndRanges(
5030b57cec5SDimitry Andric     const char *&name, const char *&mangled, DWARFRangeList &ranges,
50406c3fb27SDimitry Andric     std::optional<int> &decl_file, std::optional<int> &decl_line,
50506c3fb27SDimitry Andric     std::optional<int> &decl_column, std::optional<int> &call_file,
50606c3fb27SDimitry Andric     std::optional<int> &call_line, std::optional<int> &call_column,
507753f127fSDimitry Andric     lldb_private::DWARFExpressionList *frame_base) const {
5080b57cec5SDimitry Andric   if (IsValid()) {
5090b57cec5SDimitry Andric     return m_die->GetDIENamesAndRanges(
5100b57cec5SDimitry Andric         GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column,
5110b57cec5SDimitry Andric         call_file, call_line, call_column, frame_base);
5120b57cec5SDimitry Andric   } else
5130b57cec5SDimitry Andric     return false;
5140b57cec5SDimitry Andric }
515349cc55cSDimitry Andric 
children() const516349cc55cSDimitry Andric llvm::iterator_range<DWARFDIE::child_iterator> DWARFDIE::children() const {
517349cc55cSDimitry Andric   return llvm::make_range(child_iterator(*this), child_iterator());
518349cc55cSDimitry Andric }
519