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