xref: /llvm-project/lldb/source/Plugins/Language/CPlusPlus/LibCxxRangesRefView.cpp (revision b852fb1ec5fa15f0b913cc4988cbd09239b19904)
177b22052SMichael Buch //===-- LibCxxRangesRefView.cpp -------------------------------------------===//
277b22052SMichael Buch //
377b22052SMichael Buch // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
477b22052SMichael Buch // See https://llvm.org/LICENSE.txt for license information.
577b22052SMichael Buch // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
677b22052SMichael Buch //
777b22052SMichael Buch //===----------------------------------------------------------------------===//
877b22052SMichael Buch 
977b22052SMichael Buch #include "LibCxx.h"
1077b22052SMichael Buch 
1177b22052SMichael Buch #include "lldb/DataFormatters/FormattersHelpers.h"
1277b22052SMichael Buch #include "lldb/Utility/ConstString.h"
13*b852fb1eSJonas Devlieghere #include "lldb/ValueObject/ValueObject.h"
1477b22052SMichael Buch #include "llvm/ADT/APSInt.h"
1577b22052SMichael Buch 
1677b22052SMichael Buch using namespace lldb;
1777b22052SMichael Buch using namespace lldb_private;
1877b22052SMichael Buch using namespace lldb_private::formatters;
1977b22052SMichael Buch 
2077b22052SMichael Buch namespace lldb_private {
2177b22052SMichael Buch namespace formatters {
2277b22052SMichael Buch 
2377b22052SMichael Buch class LibcxxStdRangesRefViewSyntheticFrontEnd
2477b22052SMichael Buch     : public SyntheticChildrenFrontEnd {
2577b22052SMichael Buch public:
2677b22052SMichael Buch   LibcxxStdRangesRefViewSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
2777b22052SMichael Buch 
2877b22052SMichael Buch   ~LibcxxStdRangesRefViewSyntheticFrontEnd() override = default;
2977b22052SMichael Buch 
30624ea68cSAdrian Prantl   llvm::Expected<uint32_t> CalculateNumChildren() override {
3177b22052SMichael Buch     // __range_ will be the sole child of this type
3277b22052SMichael Buch     return 1;
3377b22052SMichael Buch   }
3477b22052SMichael Buch 
35e710523eSAdrian Prantl   lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
3677b22052SMichael Buch     // Since we only have a single child, return it
3777b22052SMichael Buch     assert(idx == 0);
3877b22052SMichael Buch     return m_range_sp;
3977b22052SMichael Buch   }
4077b22052SMichael Buch 
41d7fb94b6SMichael Buch   lldb::ChildCacheState Update() override;
4277b22052SMichael Buch 
4377b22052SMichael Buch   bool MightHaveChildren() override { return true; }
4477b22052SMichael Buch 
4577b22052SMichael Buch   size_t GetIndexOfChildWithName(ConstString name) override {
4677b22052SMichael Buch     // We only have a single child
4777b22052SMichael Buch     return 0;
4877b22052SMichael Buch   }
4977b22052SMichael Buch 
5077b22052SMichael Buch private:
5177b22052SMichael Buch   /// Pointer to the dereferenced __range_ member
5277b22052SMichael Buch   lldb::ValueObjectSP m_range_sp = nullptr;
5377b22052SMichael Buch };
5477b22052SMichael Buch 
5577b22052SMichael Buch lldb_private::formatters::LibcxxStdRangesRefViewSyntheticFrontEnd::
5677b22052SMichael Buch     LibcxxStdRangesRefViewSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
5777b22052SMichael Buch     : SyntheticChildrenFrontEnd(*valobj_sp) {
5877b22052SMichael Buch   if (valobj_sp)
5977b22052SMichael Buch     Update();
6077b22052SMichael Buch }
6177b22052SMichael Buch 
62d7fb94b6SMichael Buch lldb::ChildCacheState
63d7fb94b6SMichael Buch lldb_private::formatters::LibcxxStdRangesRefViewSyntheticFrontEnd::Update() {
6477b22052SMichael Buch   ValueObjectSP range_ptr =
6577b22052SMichael Buch       GetChildMemberWithName(m_backend, {ConstString("__range_")});
6677b22052SMichael Buch   if (!range_ptr)
67d7fb94b6SMichael Buch     return lldb::ChildCacheState::eRefetch;
6877b22052SMichael Buch 
6977b22052SMichael Buch   lldb_private::Status error;
7077b22052SMichael Buch   m_range_sp = range_ptr->Dereference(error);
7177b22052SMichael Buch 
72d7fb94b6SMichael Buch   return error.Success() ? lldb::ChildCacheState::eReuse
73d7fb94b6SMichael Buch                          : lldb::ChildCacheState::eRefetch;
7477b22052SMichael Buch }
7577b22052SMichael Buch 
7677b22052SMichael Buch lldb_private::SyntheticChildrenFrontEnd *
7777b22052SMichael Buch LibcxxStdRangesRefViewSyntheticFrontEndCreator(CXXSyntheticChildren *,
7877b22052SMichael Buch                                                lldb::ValueObjectSP valobj_sp) {
7977b22052SMichael Buch   if (!valobj_sp)
8077b22052SMichael Buch     return nullptr;
8177b22052SMichael Buch   CompilerType type = valobj_sp->GetCompilerType();
8277b22052SMichael Buch   if (!type.IsValid())
8377b22052SMichael Buch     return nullptr;
8477b22052SMichael Buch   return new LibcxxStdRangesRefViewSyntheticFrontEnd(valobj_sp);
8577b22052SMichael Buch }
8677b22052SMichael Buch 
8777b22052SMichael Buch } // namespace formatters
8877b22052SMichael Buch } // namespace lldb_private
89