xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1bdd1243dSDimitry Andric //===-- LibCxxRangesRefView.cpp -------------------------------------------===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric //
7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8bdd1243dSDimitry Andric 
9bdd1243dSDimitry Andric #include "LibCxx.h"
10bdd1243dSDimitry Andric 
11bdd1243dSDimitry Andric #include "lldb/Core/ValueObject.h"
12bdd1243dSDimitry Andric #include "lldb/DataFormatters/FormattersHelpers.h"
13bdd1243dSDimitry Andric #include "lldb/Utility/ConstString.h"
14bdd1243dSDimitry Andric #include "llvm/ADT/APSInt.h"
15bdd1243dSDimitry Andric 
16bdd1243dSDimitry Andric using namespace lldb;
17bdd1243dSDimitry Andric using namespace lldb_private;
18bdd1243dSDimitry Andric using namespace lldb_private::formatters;
19bdd1243dSDimitry Andric 
20bdd1243dSDimitry Andric namespace lldb_private {
21bdd1243dSDimitry Andric namespace formatters {
22bdd1243dSDimitry Andric 
23bdd1243dSDimitry Andric class LibcxxStdRangesRefViewSyntheticFrontEnd
24bdd1243dSDimitry Andric     : public SyntheticChildrenFrontEnd {
25bdd1243dSDimitry Andric public:
26bdd1243dSDimitry Andric   LibcxxStdRangesRefViewSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
27bdd1243dSDimitry Andric 
28bdd1243dSDimitry Andric   ~LibcxxStdRangesRefViewSyntheticFrontEnd() override = default;
29bdd1243dSDimitry Andric 
30*0fca6ea1SDimitry Andric   llvm::Expected<uint32_t> CalculateNumChildren() override {
31bdd1243dSDimitry Andric     // __range_ will be the sole child of this type
32bdd1243dSDimitry Andric     return 1;
33bdd1243dSDimitry Andric   }
34bdd1243dSDimitry Andric 
35*0fca6ea1SDimitry Andric   lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
36bdd1243dSDimitry Andric     // Since we only have a single child, return it
37bdd1243dSDimitry Andric     assert(idx == 0);
38bdd1243dSDimitry Andric     return m_range_sp;
39bdd1243dSDimitry Andric   }
40bdd1243dSDimitry Andric 
41*0fca6ea1SDimitry Andric   lldb::ChildCacheState Update() override;
42bdd1243dSDimitry Andric 
43bdd1243dSDimitry Andric   bool MightHaveChildren() override { return true; }
44bdd1243dSDimitry Andric 
45bdd1243dSDimitry Andric   size_t GetIndexOfChildWithName(ConstString name) override {
46bdd1243dSDimitry Andric     // We only have a single child
47bdd1243dSDimitry Andric     return 0;
48bdd1243dSDimitry Andric   }
49bdd1243dSDimitry Andric 
50bdd1243dSDimitry Andric private:
51bdd1243dSDimitry Andric   /// Pointer to the dereferenced __range_ member
52bdd1243dSDimitry Andric   lldb::ValueObjectSP m_range_sp = nullptr;
53bdd1243dSDimitry Andric };
54bdd1243dSDimitry Andric 
55bdd1243dSDimitry Andric lldb_private::formatters::LibcxxStdRangesRefViewSyntheticFrontEnd::
56bdd1243dSDimitry Andric     LibcxxStdRangesRefViewSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
57bdd1243dSDimitry Andric     : SyntheticChildrenFrontEnd(*valobj_sp) {
58bdd1243dSDimitry Andric   if (valobj_sp)
59bdd1243dSDimitry Andric     Update();
60bdd1243dSDimitry Andric }
61bdd1243dSDimitry Andric 
62*0fca6ea1SDimitry Andric lldb::ChildCacheState
63*0fca6ea1SDimitry Andric lldb_private::formatters::LibcxxStdRangesRefViewSyntheticFrontEnd::Update() {
64bdd1243dSDimitry Andric   ValueObjectSP range_ptr =
65bdd1243dSDimitry Andric       GetChildMemberWithName(m_backend, {ConstString("__range_")});
66bdd1243dSDimitry Andric   if (!range_ptr)
67*0fca6ea1SDimitry Andric     return lldb::ChildCacheState::eRefetch;
68bdd1243dSDimitry Andric 
69bdd1243dSDimitry Andric   lldb_private::Status error;
70bdd1243dSDimitry Andric   m_range_sp = range_ptr->Dereference(error);
71bdd1243dSDimitry Andric 
72*0fca6ea1SDimitry Andric   return error.Success() ? lldb::ChildCacheState::eReuse
73*0fca6ea1SDimitry Andric                          : lldb::ChildCacheState::eRefetch;
74bdd1243dSDimitry Andric }
75bdd1243dSDimitry Andric 
76bdd1243dSDimitry Andric lldb_private::SyntheticChildrenFrontEnd *
77bdd1243dSDimitry Andric LibcxxStdRangesRefViewSyntheticFrontEndCreator(CXXSyntheticChildren *,
78bdd1243dSDimitry Andric                                                lldb::ValueObjectSP valobj_sp) {
79bdd1243dSDimitry Andric   if (!valobj_sp)
80bdd1243dSDimitry Andric     return nullptr;
81bdd1243dSDimitry Andric   CompilerType type = valobj_sp->GetCompilerType();
82bdd1243dSDimitry Andric   if (!type.IsValid())
83bdd1243dSDimitry Andric     return nullptr;
84bdd1243dSDimitry Andric   return new LibcxxStdRangesRefViewSyntheticFrontEnd(valobj_sp);
85bdd1243dSDimitry Andric }
86bdd1243dSDimitry Andric 
87bdd1243dSDimitry Andric } // namespace formatters
88bdd1243dSDimitry Andric } // namespace lldb_private
89