1 //===-- LibCxxInitializerList.cpp -------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "lldb/Core/ValueObject.h"
12 #include "lldb/DataFormatters/FormattersHelpers.h"
13 #include "lldb/Utility/ConstString.h"
16 using namespace lldb_private;
17 using namespace lldb_private::formatters;
19 namespace lldb_private {
20 namespace formatters {
21 class LibcxxInitializerListSyntheticFrontEnd
22 : public SyntheticChildrenFrontEnd {
24 LibcxxInitializerListSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
26 ~LibcxxInitializerListSyntheticFrontEnd() override;
28 size_t CalculateNumChildren() override;
30 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
32 bool Update() override;
34 bool MightHaveChildren() override;
36 size_t GetIndexOfChildWithName(ConstString name) override;
40 CompilerType m_element_type;
41 uint32_t m_element_size;
42 size_t m_num_elements;
44 } // namespace formatters
45 } // namespace lldb_private
47 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
48 LibcxxInitializerListSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
49 : SyntheticChildrenFrontEnd(*valobj_sp), m_start(nullptr), m_element_type(),
50 m_element_size(0), m_num_elements(0) {
55 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
56 ~LibcxxInitializerListSyntheticFrontEnd() {
57 // this needs to stay around because it's a child object who will follow its
58 // parent's life cycle
62 size_t lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
63 CalculateNumChildren() {
64 static ConstString g___size_("__size_");
66 ValueObjectSP size_sp(m_backend.GetChildMemberWithName(g___size_, true));
68 m_num_elements = size_sp->GetValueAsUnsigned(0);
69 return m_num_elements;
72 lldb::ValueObjectSP lldb_private::formatters::
73 LibcxxInitializerListSyntheticFrontEnd::GetChildAtIndex(size_t idx) {
75 return lldb::ValueObjectSP();
77 uint64_t offset = idx * m_element_size;
78 offset = offset + m_start->GetValueAsUnsigned(0);
80 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
81 return CreateValueObjectFromAddress(name.GetString(), offset,
82 m_backend.GetExecutionContextRef(),
86 bool lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
88 static ConstString g___begin_("__begin_");
92 m_element_type = m_backend.GetCompilerType().GetTypeTemplateArgument(0);
93 if (!m_element_type.IsValid())
96 if (llvm::Optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) {
97 m_element_size = *size;
98 // Store raw pointers or end up with a circular dependency.
99 m_start = m_backend.GetChildMemberWithName(g___begin_, true).get();
105 bool lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
106 MightHaveChildren() {
110 size_t lldb_private::formatters::LibcxxInitializerListSyntheticFrontEnd::
111 GetIndexOfChildWithName(ConstString name) {
114 return ExtractIndexFromString(name.GetCString());
117 lldb_private::SyntheticChildrenFrontEnd *
118 lldb_private::formatters::LibcxxInitializerListSyntheticFrontEndCreator(
119 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
120 return (valobj_sp ? new LibcxxInitializerListSyntheticFrontEnd(valobj_sp)