xref: /src/contrib/llvm-project/lldb/source/Core/ValueObjectConstResultImpl.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- ValueObjectConstResultImpl.cpp ------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/Core/ValueObjectConstResultImpl.h"
10f034231aSEd Maste 
1194994d37SDimitry Andric #include "lldb/Core/Value.h"
1294994d37SDimitry Andric #include "lldb/Core/ValueObject.h"
13f034231aSEd Maste #include "lldb/Core/ValueObjectConstResult.h"
14e81d9d49SDimitry Andric #include "lldb/Core/ValueObjectConstResultCast.h"
15f034231aSEd Maste #include "lldb/Core/ValueObjectConstResultChild.h"
16e81d9d49SDimitry Andric #include "lldb/Symbol/CompilerType.h"
17f034231aSEd Maste #include "lldb/Target/ExecutionContext.h"
1894994d37SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
1994994d37SDimitry Andric #include "lldb/Utility/Endian.h"
20ac9a064cSDimitry Andric #include "lldb/Utility/LLDBLog.h"
21ac9a064cSDimitry Andric #include "lldb/Utility/Log.h"
2294994d37SDimitry Andric #include "lldb/Utility/Scalar.h"
2374a628f7SDimitry Andric 
2494994d37SDimitry Andric #include <string>
2574a628f7SDimitry Andric 
2674a628f7SDimitry Andric namespace lldb_private {
2774a628f7SDimitry Andric class DataExtractor;
2874a628f7SDimitry Andric }
2974a628f7SDimitry Andric namespace lldb_private {
30b76161e4SDimitry Andric class Status;
3174a628f7SDimitry Andric }
32f034231aSEd Maste 
33f034231aSEd Maste using namespace lldb;
34f034231aSEd Maste using namespace lldb_private;
35f034231aSEd Maste 
ValueObjectConstResultImpl(ValueObject * valobj,lldb::addr_t live_address)3614f1b3e8SDimitry Andric ValueObjectConstResultImpl::ValueObjectConstResultImpl(
3714f1b3e8SDimitry Andric     ValueObject *valobj, lldb::addr_t live_address)
3814f1b3e8SDimitry Andric     : m_impl_backend(valobj), m_live_address(live_address),
39c0981da4SDimitry Andric       m_live_address_type(eAddressTypeLoad),
4014f1b3e8SDimitry Andric       m_address_of_backend() {}
41f034231aSEd Maste 
Dereference(Status & error)42b76161e4SDimitry Andric lldb::ValueObjectSP ValueObjectConstResultImpl::Dereference(Status &error) {
435f29bb8aSDimitry Andric   if (m_impl_backend == nullptr)
44f034231aSEd Maste     return lldb::ValueObjectSP();
45f034231aSEd Maste 
46f034231aSEd Maste   return m_impl_backend->ValueObject::Dereference(error);
47f034231aSEd Maste }
48f034231aSEd Maste 
CreateChildAtIndex(size_t idx)49ac9a064cSDimitry Andric ValueObject *ValueObjectConstResultImpl::CreateChildAtIndex(size_t idx) {
505f29bb8aSDimitry Andric   if (m_impl_backend == nullptr)
515f29bb8aSDimitry Andric     return nullptr;
52f034231aSEd Maste 
53f034231aSEd Maste   m_impl_backend->UpdateValueIfNeeded(false);
54f034231aSEd Maste 
55f034231aSEd Maste   bool omit_empty_base_classes = true;
56ac9a064cSDimitry Andric   bool ignore_array_bounds = false;
57ac9a064cSDimitry Andric   std::string child_name;
58f034231aSEd Maste   uint32_t child_byte_size = 0;
59f034231aSEd Maste   int32_t child_byte_offset = 0;
60f034231aSEd Maste   uint32_t child_bitfield_bit_size = 0;
61f034231aSEd Maste   uint32_t child_bitfield_bit_offset = 0;
62f034231aSEd Maste   bool child_is_base_class = false;
63f034231aSEd Maste   bool child_is_deref_of_parent = false;
64e81d9d49SDimitry Andric   uint64_t language_flags;
65ac9a064cSDimitry Andric   const bool transparent_pointers = true;
66e81d9d49SDimitry Andric   CompilerType compiler_type = m_impl_backend->GetCompilerType();
67f034231aSEd Maste 
68f034231aSEd Maste   ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
69f034231aSEd Maste 
70ac9a064cSDimitry Andric   auto child_compiler_type_or_err = compiler_type.GetChildCompilerTypeAtIndex(
7114f1b3e8SDimitry Andric       &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
72ac9a064cSDimitry Andric       ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7314f1b3e8SDimitry Andric       child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7414f1b3e8SDimitry Andric       child_is_deref_of_parent, m_impl_backend, language_flags);
7594994d37SDimitry Andric 
7694994d37SDimitry Andric   // One might think we should check that the size of the children
7794994d37SDimitry Andric   // is always strictly positive, hence we could avoid creating a
7894994d37SDimitry Andric   // ValueObject if that's not the case, but it turns out there
7994994d37SDimitry Andric   // are languages out there which allow zero-size types with
8094994d37SDimitry Andric   // children (e.g. Swift).
81ac9a064cSDimitry Andric   if (!child_compiler_type_or_err || !child_compiler_type_or_err->IsValid()) {
82ac9a064cSDimitry Andric     LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
83ac9a064cSDimitry Andric                    child_compiler_type_or_err.takeError(),
84ac9a064cSDimitry Andric                    "could not find child: {0}");
85ac9a064cSDimitry Andric     return nullptr;
86ac9a064cSDimitry Andric   }
87f034231aSEd Maste 
88e3b55780SDimitry Andric   lldb::addr_t child_live_addr = LLDB_INVALID_ADDRESS;
89e3b55780SDimitry Andric   // Transfer the live address (with offset) to the child.  But if
90e3b55780SDimitry Andric   // the parent is a pointer, the live address is where that pointer
91e3b55780SDimitry Andric   // value lives in memory, so the children live addresses aren't
92e3b55780SDimitry Andric   // offsets from that value, they are just other load addresses that
93e3b55780SDimitry Andric   // are recorded in the Value of the child ValueObjects.
94ac9a064cSDimitry Andric   if (m_live_address != LLDB_INVALID_ADDRESS && !compiler_type.IsPointerType())
95e3b55780SDimitry Andric     child_live_addr = m_live_address + child_byte_offset;
96ac9a064cSDimitry Andric 
97ac9a064cSDimitry Andric   return new ValueObjectConstResultChild(
98ac9a064cSDimitry Andric       *m_impl_backend, *child_compiler_type_or_err, ConstString(child_name),
99ac9a064cSDimitry Andric       child_byte_size, child_byte_offset, child_bitfield_bit_size,
100ac9a064cSDimitry Andric       child_bitfield_bit_offset, child_is_base_class, child_is_deref_of_parent,
101ac9a064cSDimitry Andric       child_live_addr, language_flags);
102f034231aSEd Maste }
103f034231aSEd Maste 
104ac9a064cSDimitry Andric ValueObject *
CreateSyntheticArrayMember(size_t idx)105ac9a064cSDimitry Andric ValueObjectConstResultImpl::CreateSyntheticArrayMember(size_t idx) {
106ac9a064cSDimitry Andric   if (m_impl_backend == nullptr)
107ac9a064cSDimitry Andric     return nullptr;
108ac9a064cSDimitry Andric 
109ac9a064cSDimitry Andric   m_impl_backend->UpdateValueIfNeeded(false);
110ac9a064cSDimitry Andric 
111ac9a064cSDimitry Andric   bool omit_empty_base_classes = true;
112ac9a064cSDimitry Andric   bool ignore_array_bounds = true;
113ac9a064cSDimitry Andric   std::string child_name;
114ac9a064cSDimitry Andric   uint32_t child_byte_size = 0;
115ac9a064cSDimitry Andric   int32_t child_byte_offset = 0;
116ac9a064cSDimitry Andric   uint32_t child_bitfield_bit_size = 0;
117ac9a064cSDimitry Andric   uint32_t child_bitfield_bit_offset = 0;
118ac9a064cSDimitry Andric   bool child_is_base_class = false;
119ac9a064cSDimitry Andric   bool child_is_deref_of_parent = false;
120ac9a064cSDimitry Andric   uint64_t language_flags;
121ac9a064cSDimitry Andric 
122ac9a064cSDimitry Andric   const bool transparent_pointers = false;
123ac9a064cSDimitry Andric   CompilerType compiler_type = m_impl_backend->GetCompilerType();
124ac9a064cSDimitry Andric 
125ac9a064cSDimitry Andric   ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
126ac9a064cSDimitry Andric 
127ac9a064cSDimitry Andric   auto child_compiler_type_or_err = compiler_type.GetChildCompilerTypeAtIndex(
128ac9a064cSDimitry Andric       &exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
129ac9a064cSDimitry Andric       ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
130ac9a064cSDimitry Andric       child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
131ac9a064cSDimitry Andric       child_is_deref_of_parent, m_impl_backend, language_flags);
132ac9a064cSDimitry Andric   // One might think we should check that the size of the children
133ac9a064cSDimitry Andric   // is always strictly positive, hence we could avoid creating a
134ac9a064cSDimitry Andric   // ValueObject if that's not the case, but it turns out there
135ac9a064cSDimitry Andric   // are languages out there which allow zero-size types with
136ac9a064cSDimitry Andric   // children (e.g. Swift).
137ac9a064cSDimitry Andric   if (!child_compiler_type_or_err || !child_compiler_type_or_err->IsValid()) {
138ac9a064cSDimitry Andric     LLDB_LOG_ERROR(GetLog(LLDBLog::Types),
139ac9a064cSDimitry Andric                    child_compiler_type_or_err.takeError(),
140ac9a064cSDimitry Andric                    "could not find child: {0}");
141ac9a064cSDimitry Andric     return nullptr;
142ac9a064cSDimitry Andric   }
143ac9a064cSDimitry Andric 
144ac9a064cSDimitry Andric   child_byte_offset += child_byte_size * idx;
145ac9a064cSDimitry Andric 
146ac9a064cSDimitry Andric   lldb::addr_t child_live_addr = LLDB_INVALID_ADDRESS;
147ac9a064cSDimitry Andric   // Transfer the live address (with offset) to the child.  But if
148ac9a064cSDimitry Andric   // the parent is a pointer, the live address is where that pointer
149ac9a064cSDimitry Andric   // value lives in memory, so the children live addresses aren't
150ac9a064cSDimitry Andric   // offsets from that value, they are just other load addresses that
151ac9a064cSDimitry Andric   // are recorded in the Value of the child ValueObjects.
152ac9a064cSDimitry Andric   if (m_live_address != LLDB_INVALID_ADDRESS && !compiler_type.IsPointerType())
153ac9a064cSDimitry Andric     child_live_addr = m_live_address + child_byte_offset;
154ac9a064cSDimitry Andric   return new ValueObjectConstResultChild(
155ac9a064cSDimitry Andric       *m_impl_backend, *child_compiler_type_or_err, ConstString(child_name),
156ac9a064cSDimitry Andric       child_byte_size, child_byte_offset, child_bitfield_bit_size,
157ac9a064cSDimitry Andric       child_bitfield_bit_offset, child_is_base_class, child_is_deref_of_parent,
158ac9a064cSDimitry Andric       child_live_addr, language_flags);
159f034231aSEd Maste }
160f034231aSEd Maste 
GetSyntheticChildAtOffset(uint32_t offset,const CompilerType & type,bool can_create,ConstString name_const_str)16114f1b3e8SDimitry Andric lldb::ValueObjectSP ValueObjectConstResultImpl::GetSyntheticChildAtOffset(
16214f1b3e8SDimitry Andric     uint32_t offset, const CompilerType &type, bool can_create,
16314f1b3e8SDimitry Andric     ConstString name_const_str) {
1645f29bb8aSDimitry Andric   if (m_impl_backend == nullptr)
165f034231aSEd Maste     return lldb::ValueObjectSP();
166f034231aSEd Maste 
16714f1b3e8SDimitry Andric   return m_impl_backend->ValueObject::GetSyntheticChildAtOffset(
16814f1b3e8SDimitry Andric       offset, type, can_create, name_const_str);
169f034231aSEd Maste }
170f034231aSEd Maste 
AddressOf(Status & error)171b76161e4SDimitry Andric lldb::ValueObjectSP ValueObjectConstResultImpl::AddressOf(Status &error) {
1725f29bb8aSDimitry Andric   if (m_address_of_backend.get() != nullptr)
173f034231aSEd Maste     return m_address_of_backend;
174f034231aSEd Maste 
1755f29bb8aSDimitry Andric   if (m_impl_backend == nullptr)
176f034231aSEd Maste     return lldb::ValueObjectSP();
17714f1b3e8SDimitry Andric   if (m_live_address != LLDB_INVALID_ADDRESS) {
178e81d9d49SDimitry Andric     CompilerType compiler_type(m_impl_backend->GetCompilerType());
179f034231aSEd Maste 
18014f1b3e8SDimitry Andric     lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(
18114f1b3e8SDimitry Andric         &m_live_address, sizeof(lldb::addr_t)));
182f034231aSEd Maste 
183f034231aSEd Maste     std::string new_name("&");
184f034231aSEd Maste     new_name.append(m_impl_backend->GetName().AsCString(""));
185f034231aSEd Maste     ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
18614f1b3e8SDimitry Andric     m_address_of_backend = ValueObjectConstResult::Create(
18714f1b3e8SDimitry Andric         exe_ctx.GetBestExecutionContextScope(), compiler_type.GetPointerType(),
18814f1b3e8SDimitry Andric         ConstString(new_name.c_str()), buffer, endian::InlHostByteOrder(),
189f034231aSEd Maste         exe_ctx.GetAddressByteSize());
190f034231aSEd Maste 
191344a3780SDimitry Andric     m_address_of_backend->GetValue().SetValueType(Value::ValueType::Scalar);
192f034231aSEd Maste     m_address_of_backend->GetValue().GetScalar() = m_live_address;
193f034231aSEd Maste 
194f034231aSEd Maste     return m_address_of_backend;
19514f1b3e8SDimitry Andric   } else
196205afe67SEd Maste     return m_impl_backend->ValueObject::AddressOf(error);
197f034231aSEd Maste }
198f034231aSEd Maste 
199e81d9d49SDimitry Andric lldb::ValueObjectSP
Cast(const CompilerType & compiler_type)20014f1b3e8SDimitry Andric ValueObjectConstResultImpl::Cast(const CompilerType &compiler_type) {
2015f29bb8aSDimitry Andric   if (m_impl_backend == nullptr)
202e81d9d49SDimitry Andric     return lldb::ValueObjectSP();
203e81d9d49SDimitry Andric 
20414f1b3e8SDimitry Andric   ValueObjectConstResultCast *result_cast =
20514f1b3e8SDimitry Andric       new ValueObjectConstResultCast(*m_impl_backend, m_impl_backend->GetName(),
20614f1b3e8SDimitry Andric                                      compiler_type, m_live_address);
207e81d9d49SDimitry Andric   return result_cast->GetSP();
208e81d9d49SDimitry Andric }
209e81d9d49SDimitry Andric 
210f034231aSEd Maste lldb::addr_t
GetAddressOf(bool scalar_is_load_address,AddressType * address_type)211f034231aSEd Maste ValueObjectConstResultImpl::GetAddressOf(bool scalar_is_load_address,
21214f1b3e8SDimitry Andric                                          AddressType *address_type) {
213f034231aSEd Maste 
2145f29bb8aSDimitry Andric   if (m_impl_backend == nullptr)
215f034231aSEd Maste     return 0;
216f034231aSEd Maste 
21714f1b3e8SDimitry Andric   if (m_live_address == LLDB_INVALID_ADDRESS) {
218f034231aSEd Maste     return m_impl_backend->ValueObject::GetAddressOf(scalar_is_load_address,
219f034231aSEd Maste                                                      address_type);
220f034231aSEd Maste   }
221f034231aSEd Maste 
222f034231aSEd Maste   if (address_type)
223f034231aSEd Maste     *address_type = m_live_address_type;
224f034231aSEd Maste 
225f034231aSEd Maste   return m_live_address;
226f034231aSEd Maste }
227f034231aSEd Maste 
GetPointeeData(DataExtractor & data,uint32_t item_idx,uint32_t item_count)22814f1b3e8SDimitry Andric size_t ValueObjectConstResultImpl::GetPointeeData(DataExtractor &data,
229f034231aSEd Maste                                                   uint32_t item_idx,
23014f1b3e8SDimitry Andric                                                   uint32_t item_count) {
2315f29bb8aSDimitry Andric   if (m_impl_backend == nullptr)
232f034231aSEd Maste     return 0;
23314f1b3e8SDimitry Andric   return m_impl_backend->ValueObject::GetPointeeData(data, item_idx,
23414f1b3e8SDimitry Andric                                                      item_count);
235f034231aSEd Maste }
236