15ffd83dbSDimitry Andric //===-- LibCxxMap.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 "LibCxx.h"
100b57cec5SDimitry Andric 
115ffd83dbSDimitry Andric #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
120b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
130b57cec5SDimitry Andric #include "lldb/Core/ValueObjectConstResult.h"
140b57cec5SDimitry Andric #include "lldb/DataFormatters/FormattersHelpers.h"
150b57cec5SDimitry Andric #include "lldb/Target/Target.h"
160b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
170b57cec5SDimitry Andric #include "lldb/Utility/Endian.h"
180b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
190b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace lldb;
220b57cec5SDimitry Andric using namespace lldb_private;
230b57cec5SDimitry Andric using namespace lldb_private::formatters;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric class MapEntry {
260b57cec5SDimitry Andric public:
270b57cec5SDimitry Andric   MapEntry() = default;
MapEntry(ValueObjectSP entry_sp)280b57cec5SDimitry Andric   explicit MapEntry(ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {}
MapEntry(ValueObject * entry)290b57cec5SDimitry Andric   explicit MapEntry(ValueObject *entry)
300b57cec5SDimitry Andric       : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
310b57cec5SDimitry Andric 
left() const320b57cec5SDimitry Andric   ValueObjectSP left() const {
330b57cec5SDimitry Andric     static ConstString g_left("__left_");
340b57cec5SDimitry Andric     if (!m_entry_sp)
350b57cec5SDimitry Andric       return m_entry_sp;
360b57cec5SDimitry Andric     return m_entry_sp->GetSyntheticChildAtOffset(
370b57cec5SDimitry Andric         0, m_entry_sp->GetCompilerType(), true);
380b57cec5SDimitry Andric   }
390b57cec5SDimitry Andric 
right() const400b57cec5SDimitry Andric   ValueObjectSP right() const {
410b57cec5SDimitry Andric     static ConstString g_right("__right_");
420b57cec5SDimitry Andric     if (!m_entry_sp)
430b57cec5SDimitry Andric       return m_entry_sp;
440b57cec5SDimitry Andric     return m_entry_sp->GetSyntheticChildAtOffset(
450b57cec5SDimitry Andric         m_entry_sp->GetProcessSP()->GetAddressByteSize(),
460b57cec5SDimitry Andric         m_entry_sp->GetCompilerType(), true);
470b57cec5SDimitry Andric   }
480b57cec5SDimitry Andric 
parent() const490b57cec5SDimitry Andric   ValueObjectSP parent() const {
500b57cec5SDimitry Andric     static ConstString g_parent("__parent_");
510b57cec5SDimitry Andric     if (!m_entry_sp)
520b57cec5SDimitry Andric       return m_entry_sp;
530b57cec5SDimitry Andric     return m_entry_sp->GetSyntheticChildAtOffset(
540b57cec5SDimitry Andric         2 * m_entry_sp->GetProcessSP()->GetAddressByteSize(),
550b57cec5SDimitry Andric         m_entry_sp->GetCompilerType(), true);
560b57cec5SDimitry Andric   }
570b57cec5SDimitry Andric 
value() const580b57cec5SDimitry Andric   uint64_t value() const {
590b57cec5SDimitry Andric     if (!m_entry_sp)
600b57cec5SDimitry Andric       return 0;
610b57cec5SDimitry Andric     return m_entry_sp->GetValueAsUnsigned(0);
620b57cec5SDimitry Andric   }
630b57cec5SDimitry Andric 
error() const640b57cec5SDimitry Andric   bool error() const {
650b57cec5SDimitry Andric     if (!m_entry_sp)
660b57cec5SDimitry Andric       return true;
670b57cec5SDimitry Andric     return m_entry_sp->GetError().Fail();
680b57cec5SDimitry Andric   }
690b57cec5SDimitry Andric 
null() const700b57cec5SDimitry Andric   bool null() const { return (value() == 0); }
710b57cec5SDimitry Andric 
GetEntry() const720b57cec5SDimitry Andric   ValueObjectSP GetEntry() const { return m_entry_sp; }
730b57cec5SDimitry Andric 
SetEntry(ValueObjectSP entry)740b57cec5SDimitry Andric   void SetEntry(ValueObjectSP entry) { m_entry_sp = entry; }
750b57cec5SDimitry Andric 
operator ==(const MapEntry & rhs) const760b57cec5SDimitry Andric   bool operator==(const MapEntry &rhs) const {
770b57cec5SDimitry Andric     return (rhs.m_entry_sp.get() == m_entry_sp.get());
780b57cec5SDimitry Andric   }
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric private:
810b57cec5SDimitry Andric   ValueObjectSP m_entry_sp;
820b57cec5SDimitry Andric };
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric class MapIterator {
850b57cec5SDimitry Andric public:
860b57cec5SDimitry Andric   MapIterator() = default;
MapIterator(MapEntry entry,size_t depth=0)870b57cec5SDimitry Andric   MapIterator(MapEntry entry, size_t depth = 0)
88fe6060f1SDimitry Andric       : m_entry(std::move(entry)), m_max_depth(depth), m_error(false) {}
MapIterator(ValueObjectSP entry,size_t depth=0)890b57cec5SDimitry Andric   MapIterator(ValueObjectSP entry, size_t depth = 0)
90fe6060f1SDimitry Andric       : m_entry(std::move(entry)), m_max_depth(depth), m_error(false) {}
MapIterator(const MapIterator & rhs)910b57cec5SDimitry Andric   MapIterator(const MapIterator &rhs)
920b57cec5SDimitry Andric       : m_entry(rhs.m_entry), m_max_depth(rhs.m_max_depth), m_error(false) {}
MapIterator(ValueObject * entry,size_t depth=0)930b57cec5SDimitry Andric   MapIterator(ValueObject *entry, size_t depth = 0)
940b57cec5SDimitry Andric       : m_entry(entry), m_max_depth(depth), m_error(false) {}
950b57cec5SDimitry Andric 
96480093f4SDimitry Andric   MapIterator &operator=(const MapIterator &) = default;
97480093f4SDimitry Andric 
value()980b57cec5SDimitry Andric   ValueObjectSP value() { return m_entry.GetEntry(); }
990b57cec5SDimitry Andric 
advance(size_t count)1000b57cec5SDimitry Andric   ValueObjectSP advance(size_t count) {
1010b57cec5SDimitry Andric     ValueObjectSP fail;
1020b57cec5SDimitry Andric     if (m_error)
1030b57cec5SDimitry Andric       return fail;
1040b57cec5SDimitry Andric     size_t steps = 0;
1050b57cec5SDimitry Andric     while (count > 0) {
1060b57cec5SDimitry Andric       next();
1070b57cec5SDimitry Andric       count--, steps++;
1080b57cec5SDimitry Andric       if (m_error || m_entry.null() || (steps > m_max_depth))
1090b57cec5SDimitry Andric         return fail;
1100b57cec5SDimitry Andric     }
1110b57cec5SDimitry Andric     return m_entry.GetEntry();
1120b57cec5SDimitry Andric   }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric protected:
next()1150b57cec5SDimitry Andric   void next() {
1160b57cec5SDimitry Andric     if (m_entry.null())
1170b57cec5SDimitry Andric       return;
1180b57cec5SDimitry Andric     MapEntry right(m_entry.right());
1190b57cec5SDimitry Andric     if (!right.null()) {
1200b57cec5SDimitry Andric       m_entry = tree_min(std::move(right));
1210b57cec5SDimitry Andric       return;
1220b57cec5SDimitry Andric     }
1230b57cec5SDimitry Andric     size_t steps = 0;
1240b57cec5SDimitry Andric     while (!is_left_child(m_entry)) {
1250b57cec5SDimitry Andric       if (m_entry.error()) {
1260b57cec5SDimitry Andric         m_error = true;
1270b57cec5SDimitry Andric         return;
1280b57cec5SDimitry Andric       }
1290b57cec5SDimitry Andric       m_entry.SetEntry(m_entry.parent());
1300b57cec5SDimitry Andric       steps++;
1310b57cec5SDimitry Andric       if (steps > m_max_depth) {
1320b57cec5SDimitry Andric         m_entry = MapEntry();
1330b57cec5SDimitry Andric         return;
1340b57cec5SDimitry Andric       }
1350b57cec5SDimitry Andric     }
1360b57cec5SDimitry Andric     m_entry = MapEntry(m_entry.parent());
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric private:
tree_min(MapEntry x)140fe6060f1SDimitry Andric   MapEntry tree_min(MapEntry x) {
1410b57cec5SDimitry Andric     if (x.null())
1420b57cec5SDimitry Andric       return MapEntry();
1430b57cec5SDimitry Andric     MapEntry left(x.left());
1440b57cec5SDimitry Andric     size_t steps = 0;
1450b57cec5SDimitry Andric     while (!left.null()) {
1460b57cec5SDimitry Andric       if (left.error()) {
1470b57cec5SDimitry Andric         m_error = true;
1480b57cec5SDimitry Andric         return MapEntry();
1490b57cec5SDimitry Andric       }
1500b57cec5SDimitry Andric       x = left;
1510b57cec5SDimitry Andric       left.SetEntry(x.left());
1520b57cec5SDimitry Andric       steps++;
1530b57cec5SDimitry Andric       if (steps > m_max_depth)
1540b57cec5SDimitry Andric         return MapEntry();
1550b57cec5SDimitry Andric     }
1560b57cec5SDimitry Andric     return x;
1570b57cec5SDimitry Andric   }
1580b57cec5SDimitry Andric 
is_left_child(const MapEntry & x)1590b57cec5SDimitry Andric   bool is_left_child(const MapEntry &x) {
1600b57cec5SDimitry Andric     if (x.null())
1610b57cec5SDimitry Andric       return false;
1620b57cec5SDimitry Andric     MapEntry rhs(x.parent());
1630b57cec5SDimitry Andric     rhs.SetEntry(rhs.left());
1640b57cec5SDimitry Andric     return x.value() == rhs.value();
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   MapEntry m_entry;
168fcaf7f86SDimitry Andric   size_t m_max_depth = 0;
169fcaf7f86SDimitry Andric   bool m_error = false;
1700b57cec5SDimitry Andric };
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric namespace lldb_private {
1730b57cec5SDimitry Andric namespace formatters {
1740b57cec5SDimitry Andric class LibcxxStdMapSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
1750b57cec5SDimitry Andric public:
1760b57cec5SDimitry Andric   LibcxxStdMapSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   ~LibcxxStdMapSyntheticFrontEnd() override = default;
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   size_t CalculateNumChildren() override;
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   bool Update() override;
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   bool MightHaveChildren() override;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   size_t GetIndexOfChildWithName(ConstString name) override;
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric private:
1910b57cec5SDimitry Andric   bool GetDataType();
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   void GetValueOffset(const lldb::ValueObjectSP &node);
1940b57cec5SDimitry Andric 
19581ad6265SDimitry Andric   ValueObject *m_tree = nullptr;
19681ad6265SDimitry Andric   ValueObject *m_root_node = nullptr;
1970b57cec5SDimitry Andric   CompilerType m_element_type;
19881ad6265SDimitry Andric   uint32_t m_skip_size = UINT32_MAX;
19981ad6265SDimitry Andric   size_t m_count = UINT32_MAX;
2000b57cec5SDimitry Andric   std::map<size_t, MapIterator> m_iterators;
2010b57cec5SDimitry Andric };
2020b57cec5SDimitry Andric } // namespace formatters
2030b57cec5SDimitry Andric } // namespace lldb_private
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
LibcxxStdMapSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)2060b57cec5SDimitry Andric     LibcxxStdMapSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
20781ad6265SDimitry Andric     : SyntheticChildrenFrontEnd(*valobj_sp), m_element_type(), m_iterators() {
2080b57cec5SDimitry Andric   if (valobj_sp)
2090b57cec5SDimitry Andric     Update();
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric size_t lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
CalculateNumChildren()2130b57cec5SDimitry Andric     CalculateNumChildren() {
2140b57cec5SDimitry Andric   if (m_count != UINT32_MAX)
2150b57cec5SDimitry Andric     return m_count;
2160b57cec5SDimitry Andric   if (m_tree == nullptr)
2170b57cec5SDimitry Andric     return 0;
21806c3fb27SDimitry Andric   ValueObjectSP m_item(m_tree->GetChildMemberWithName("__pair3_"));
2190b57cec5SDimitry Andric   if (!m_item)
2200b57cec5SDimitry Andric     return 0;
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   switch (m_item->GetCompilerType().GetNumDirectBaseClasses()) {
2230b57cec5SDimitry Andric   case 1:
2240b57cec5SDimitry Andric     // Assume a pre llvm r300140 __compressed_pair implementation:
22506c3fb27SDimitry Andric     m_item = m_item->GetChildMemberWithName("__first_");
2260b57cec5SDimitry Andric     break;
2270b57cec5SDimitry Andric   case 2: {
2280b57cec5SDimitry Andric     // Assume a post llvm r300140 __compressed_pair implementation:
22906c3fb27SDimitry Andric     ValueObjectSP first_elem_parent = m_item->GetChildAtIndex(0);
23006c3fb27SDimitry Andric     m_item = first_elem_parent->GetChildMemberWithName("__value_");
2310b57cec5SDimitry Andric     break;
2320b57cec5SDimitry Andric   }
2330b57cec5SDimitry Andric   default:
2340b57cec5SDimitry Andric     return false;
2350b57cec5SDimitry Andric   }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   if (!m_item)
2380b57cec5SDimitry Andric     return 0;
2390b57cec5SDimitry Andric   m_count = m_item->GetValueAsUnsigned(0);
2400b57cec5SDimitry Andric   return m_count;
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
GetDataType()2430b57cec5SDimitry Andric bool lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetDataType() {
244bdd1243dSDimitry Andric   if (m_element_type.IsValid())
2450b57cec5SDimitry Andric     return true;
2460b57cec5SDimitry Andric   m_element_type.Clear();
2470b57cec5SDimitry Andric   ValueObjectSP deref;
2480b57cec5SDimitry Andric   Status error;
2490b57cec5SDimitry Andric   deref = m_root_node->Dereference(error);
2500b57cec5SDimitry Andric   if (!deref || error.Fail())
2510b57cec5SDimitry Andric     return false;
25206c3fb27SDimitry Andric   deref = deref->GetChildMemberWithName("__value_");
2530b57cec5SDimitry Andric   if (deref) {
2540b57cec5SDimitry Andric     m_element_type = deref->GetCompilerType();
2550b57cec5SDimitry Andric     return true;
2560b57cec5SDimitry Andric   }
25706c3fb27SDimitry Andric   deref = m_backend.GetChildAtNamePath({"__tree_", "__pair3_"});
2580b57cec5SDimitry Andric   if (!deref)
2590b57cec5SDimitry Andric     return false;
2600b57cec5SDimitry Andric   m_element_type = deref->GetCompilerType()
2610b57cec5SDimitry Andric                        .GetTypeTemplateArgument(1)
2620b57cec5SDimitry Andric                        .GetTypeTemplateArgument(1);
2630b57cec5SDimitry Andric   if (m_element_type) {
2640b57cec5SDimitry Andric     std::string name;
2650b57cec5SDimitry Andric     uint64_t bit_offset_ptr;
2660b57cec5SDimitry Andric     uint32_t bitfield_bit_size_ptr;
2670b57cec5SDimitry Andric     bool is_bitfield_ptr;
2680b57cec5SDimitry Andric     m_element_type = m_element_type.GetFieldAtIndex(
2690b57cec5SDimitry Andric         0, name, &bit_offset_ptr, &bitfield_bit_size_ptr, &is_bitfield_ptr);
2700b57cec5SDimitry Andric     m_element_type = m_element_type.GetTypedefedType();
2710b57cec5SDimitry Andric     return m_element_type.IsValid();
2720b57cec5SDimitry Andric   } else {
2730b57cec5SDimitry Andric     m_element_type = m_backend.GetCompilerType().GetTypeTemplateArgument(0);
2740b57cec5SDimitry Andric     return m_element_type.IsValid();
2750b57cec5SDimitry Andric   }
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric 
GetValueOffset(const lldb::ValueObjectSP & node)2780b57cec5SDimitry Andric void lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset(
2790b57cec5SDimitry Andric     const lldb::ValueObjectSP &node) {
2800b57cec5SDimitry Andric   if (m_skip_size != UINT32_MAX)
2810b57cec5SDimitry Andric     return;
2820b57cec5SDimitry Andric   if (!node)
2830b57cec5SDimitry Andric     return;
2840b57cec5SDimitry Andric   CompilerType node_type(node->GetCompilerType());
2850b57cec5SDimitry Andric   uint64_t bit_offset;
2860b57cec5SDimitry Andric   if (node_type.GetIndexOfFieldWithName("__value_", nullptr, &bit_offset) !=
2870b57cec5SDimitry Andric       UINT32_MAX) {
2880b57cec5SDimitry Andric     m_skip_size = bit_offset / 8u;
2890b57cec5SDimitry Andric   } else {
290bdd1243dSDimitry Andric     auto ast_ctx = node_type.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
2910b57cec5SDimitry Andric     if (!ast_ctx)
2920b57cec5SDimitry Andric       return;
2930b57cec5SDimitry Andric     CompilerType tree_node_type = ast_ctx->CreateStructForIdentifier(
29406c3fb27SDimitry Andric         llvm::StringRef(),
2950b57cec5SDimitry Andric         {{"ptr0", ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
2960b57cec5SDimitry Andric          {"ptr1", ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
2970b57cec5SDimitry Andric          {"ptr2", ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
2980b57cec5SDimitry Andric          {"cw", ast_ctx->GetBasicType(lldb::eBasicTypeBool)},
2990b57cec5SDimitry Andric          {"payload", (m_element_type.GetCompleteType(), m_element_type)}});
3000b57cec5SDimitry Andric     std::string child_name;
3010b57cec5SDimitry Andric     uint32_t child_byte_size;
3020b57cec5SDimitry Andric     int32_t child_byte_offset = 0;
3030b57cec5SDimitry Andric     uint32_t child_bitfield_bit_size;
3040b57cec5SDimitry Andric     uint32_t child_bitfield_bit_offset;
3050b57cec5SDimitry Andric     bool child_is_base_class;
3060b57cec5SDimitry Andric     bool child_is_deref_of_parent;
3070b57cec5SDimitry Andric     uint64_t language_flags;
3080b57cec5SDimitry Andric     if (tree_node_type
3090b57cec5SDimitry Andric             .GetChildCompilerTypeAtIndex(
3100b57cec5SDimitry Andric                 nullptr, 4, true, true, true, child_name, child_byte_size,
3110b57cec5SDimitry Andric                 child_byte_offset, child_bitfield_bit_size,
3120b57cec5SDimitry Andric                 child_bitfield_bit_offset, child_is_base_class,
3130b57cec5SDimitry Andric                 child_is_deref_of_parent, nullptr, language_flags)
3140b57cec5SDimitry Andric             .IsValid())
3150b57cec5SDimitry Andric       m_skip_size = (uint32_t)child_byte_offset;
3160b57cec5SDimitry Andric   }
3170b57cec5SDimitry Andric }
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric lldb::ValueObjectSP
GetChildAtIndex(size_t idx)3200b57cec5SDimitry Andric lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetChildAtIndex(
3210b57cec5SDimitry Andric     size_t idx) {
322bdd1243dSDimitry Andric   static ConstString g_cc_("__cc_"), g_cc("__cc");
32381ad6265SDimitry Andric   static ConstString g_nc("__nc");
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric   if (idx >= CalculateNumChildren())
3260b57cec5SDimitry Andric     return lldb::ValueObjectSP();
3270b57cec5SDimitry Andric   if (m_tree == nullptr || m_root_node == nullptr)
3280b57cec5SDimitry Andric     return lldb::ValueObjectSP();
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   MapIterator iterator(m_root_node, CalculateNumChildren());
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   const bool need_to_skip = (idx > 0);
3330b57cec5SDimitry Andric   size_t actual_advancde = idx;
3340b57cec5SDimitry Andric   if (need_to_skip) {
3350b57cec5SDimitry Andric     auto cached_iterator = m_iterators.find(idx - 1);
3360b57cec5SDimitry Andric     if (cached_iterator != m_iterators.end()) {
3370b57cec5SDimitry Andric       iterator = cached_iterator->second;
3380b57cec5SDimitry Andric       actual_advancde = 1;
3390b57cec5SDimitry Andric     }
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   ValueObjectSP iterated_sp(iterator.advance(actual_advancde));
3430b57cec5SDimitry Andric   if (!iterated_sp) {
3440b57cec5SDimitry Andric     // this tree is garbage - stop
3450b57cec5SDimitry Andric     m_tree =
3460b57cec5SDimitry Andric         nullptr; // this will stop all future searches until an Update() happens
3470b57cec5SDimitry Andric     return iterated_sp;
3480b57cec5SDimitry Andric   }
3490b57cec5SDimitry Andric   if (GetDataType()) {
3500b57cec5SDimitry Andric     if (!need_to_skip) {
3510b57cec5SDimitry Andric       Status error;
3520b57cec5SDimitry Andric       iterated_sp = iterated_sp->Dereference(error);
3530b57cec5SDimitry Andric       if (!iterated_sp || error.Fail()) {
3540b57cec5SDimitry Andric         m_tree = nullptr;
3550b57cec5SDimitry Andric         return lldb::ValueObjectSP();
3560b57cec5SDimitry Andric       }
3570b57cec5SDimitry Andric       GetValueOffset(iterated_sp);
35806c3fb27SDimitry Andric       auto child_sp = iterated_sp->GetChildMemberWithName("__value_");
3590b57cec5SDimitry Andric       if (child_sp)
3600b57cec5SDimitry Andric         iterated_sp = child_sp;
3610b57cec5SDimitry Andric       else
3620b57cec5SDimitry Andric         iterated_sp = iterated_sp->GetSyntheticChildAtOffset(
3630b57cec5SDimitry Andric             m_skip_size, m_element_type, true);
3640b57cec5SDimitry Andric       if (!iterated_sp) {
3650b57cec5SDimitry Andric         m_tree = nullptr;
3660b57cec5SDimitry Andric         return lldb::ValueObjectSP();
3670b57cec5SDimitry Andric       }
3680b57cec5SDimitry Andric     } else {
3690b57cec5SDimitry Andric       // because of the way our debug info is made, we need to read item 0
3700b57cec5SDimitry Andric       // first so that we can cache information used to generate other elements
3710b57cec5SDimitry Andric       if (m_skip_size == UINT32_MAX)
3720b57cec5SDimitry Andric         GetChildAtIndex(0);
3730b57cec5SDimitry Andric       if (m_skip_size == UINT32_MAX) {
3740b57cec5SDimitry Andric         m_tree = nullptr;
3750b57cec5SDimitry Andric         return lldb::ValueObjectSP();
3760b57cec5SDimitry Andric       }
3770b57cec5SDimitry Andric       iterated_sp = iterated_sp->GetSyntheticChildAtOffset(
3780b57cec5SDimitry Andric           m_skip_size, m_element_type, true);
3790b57cec5SDimitry Andric       if (!iterated_sp) {
3800b57cec5SDimitry Andric         m_tree = nullptr;
3810b57cec5SDimitry Andric         return lldb::ValueObjectSP();
3820b57cec5SDimitry Andric       }
3830b57cec5SDimitry Andric     }
3840b57cec5SDimitry Andric   } else {
3850b57cec5SDimitry Andric     m_tree = nullptr;
3860b57cec5SDimitry Andric     return lldb::ValueObjectSP();
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric   // at this point we have a valid
3890b57cec5SDimitry Andric   // we need to copy current_sp into a new object otherwise we will end up with
3900b57cec5SDimitry Andric   // all items named __value_
3910b57cec5SDimitry Andric   StreamString name;
3920b57cec5SDimitry Andric   name.Printf("[%" PRIu64 "]", (uint64_t)idx);
39306c3fb27SDimitry Andric   auto potential_child_sp = iterated_sp->Clone(ConstString(name.GetString()));
3940b57cec5SDimitry Andric   if (potential_child_sp) {
3950b57cec5SDimitry Andric     switch (potential_child_sp->GetNumChildren()) {
3960b57cec5SDimitry Andric     case 1: {
39706c3fb27SDimitry Andric       auto child0_sp = potential_child_sp->GetChildAtIndex(0);
398bdd1243dSDimitry Andric       if (child0_sp &&
399bdd1243dSDimitry Andric           (child0_sp->GetName() == g_cc_ || child0_sp->GetName() == g_cc))
4000b57cec5SDimitry Andric         potential_child_sp = child0_sp->Clone(ConstString(name.GetString()));
4010b57cec5SDimitry Andric       break;
4020b57cec5SDimitry Andric     }
4030b57cec5SDimitry Andric     case 2: {
40406c3fb27SDimitry Andric       auto child0_sp = potential_child_sp->GetChildAtIndex(0);
40506c3fb27SDimitry Andric       auto child1_sp = potential_child_sp->GetChildAtIndex(1);
406bdd1243dSDimitry Andric       if (child0_sp &&
407bdd1243dSDimitry Andric           (child0_sp->GetName() == g_cc_ || child0_sp->GetName() == g_cc) &&
408bdd1243dSDimitry Andric           child1_sp && child1_sp->GetName() == g_nc)
4090b57cec5SDimitry Andric         potential_child_sp = child0_sp->Clone(ConstString(name.GetString()));
4100b57cec5SDimitry Andric       break;
4110b57cec5SDimitry Andric     }
4120b57cec5SDimitry Andric     }
4130b57cec5SDimitry Andric   }
4140b57cec5SDimitry Andric   m_iterators[idx] = iterator;
4150b57cec5SDimitry Andric   return potential_child_sp;
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric 
Update()4180b57cec5SDimitry Andric bool lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::Update() {
4190b57cec5SDimitry Andric   m_count = UINT32_MAX;
4200b57cec5SDimitry Andric   m_tree = m_root_node = nullptr;
4210b57cec5SDimitry Andric   m_iterators.clear();
42206c3fb27SDimitry Andric   m_tree = m_backend.GetChildMemberWithName("__tree_").get();
4230b57cec5SDimitry Andric   if (!m_tree)
4240b57cec5SDimitry Andric     return false;
42506c3fb27SDimitry Andric   m_root_node = m_tree->GetChildMemberWithName("__begin_node_").get();
4260b57cec5SDimitry Andric   return false;
4270b57cec5SDimitry Andric }
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric bool lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
MightHaveChildren()4300b57cec5SDimitry Andric     MightHaveChildren() {
4310b57cec5SDimitry Andric   return true;
4320b57cec5SDimitry Andric }
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric size_t lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
GetIndexOfChildWithName(ConstString name)4350b57cec5SDimitry Andric     GetIndexOfChildWithName(ConstString name) {
4360b57cec5SDimitry Andric   return ExtractIndexFromString(name.GetCString());
4370b57cec5SDimitry Andric }
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric SyntheticChildrenFrontEnd *
LibcxxStdMapSyntheticFrontEndCreator(CXXSyntheticChildren *,lldb::ValueObjectSP valobj_sp)4400b57cec5SDimitry Andric lldb_private::formatters::LibcxxStdMapSyntheticFrontEndCreator(
4410b57cec5SDimitry Andric     CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
4420b57cec5SDimitry Andric   return (valobj_sp ? new LibcxxStdMapSyntheticFrontEnd(valobj_sp) : nullptr);
4430b57cec5SDimitry Andric }
444