1*0b57cec5SDimitry Andric //===-- ValueObjectConstResultImpl.cpp ------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "lldb/Core/ValueObjectConstResultImpl.h"
10*0b57cec5SDimitry Andric
11*0b57cec5SDimitry Andric #include "lldb/Core/Value.h"
12*0b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
13*0b57cec5SDimitry Andric #include "lldb/Core/ValueObjectConstResult.h"
14*0b57cec5SDimitry Andric #include "lldb/Core/ValueObjectConstResultCast.h"
15*0b57cec5SDimitry Andric #include "lldb/Core/ValueObjectConstResultChild.h"
16*0b57cec5SDimitry Andric #include "lldb/Symbol/CompilerType.h"
17*0b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
18*0b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
19*0b57cec5SDimitry Andric #include "lldb/Utility/Endian.h"
20*0b57cec5SDimitry Andric #include "lldb/Utility/Scalar.h"
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric #include <string>
23*0b57cec5SDimitry Andric
24*0b57cec5SDimitry Andric namespace lldb_private {
25*0b57cec5SDimitry Andric class DataExtractor;
26*0b57cec5SDimitry Andric }
27*0b57cec5SDimitry Andric namespace lldb_private {
28*0b57cec5SDimitry Andric class Status;
29*0b57cec5SDimitry Andric }
30*0b57cec5SDimitry Andric
31*0b57cec5SDimitry Andric using namespace lldb;
32*0b57cec5SDimitry Andric using namespace lldb_private;
33*0b57cec5SDimitry Andric
ValueObjectConstResultImpl(ValueObject * valobj,lldb::addr_t live_address)34*0b57cec5SDimitry Andric ValueObjectConstResultImpl::ValueObjectConstResultImpl(
35*0b57cec5SDimitry Andric ValueObject *valobj, lldb::addr_t live_address)
36*0b57cec5SDimitry Andric : m_impl_backend(valobj), m_live_address(live_address),
37*0b57cec5SDimitry Andric m_live_address_type(eAddressTypeLoad),
38*0b57cec5SDimitry Andric m_address_of_backend() {}
39*0b57cec5SDimitry Andric
Dereference(Status & error)40*0b57cec5SDimitry Andric lldb::ValueObjectSP ValueObjectConstResultImpl::Dereference(Status &error) {
41*0b57cec5SDimitry Andric if (m_impl_backend == nullptr)
42*0b57cec5SDimitry Andric return lldb::ValueObjectSP();
43*0b57cec5SDimitry Andric
44*0b57cec5SDimitry Andric return m_impl_backend->ValueObject::Dereference(error);
45*0b57cec5SDimitry Andric }
46*0b57cec5SDimitry Andric
CreateChildAtIndex(size_t idx,bool synthetic_array_member,int32_t synthetic_index)47*0b57cec5SDimitry Andric ValueObject *ValueObjectConstResultImpl::CreateChildAtIndex(
48*0b57cec5SDimitry Andric size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
49*0b57cec5SDimitry Andric if (m_impl_backend == nullptr)
50*0b57cec5SDimitry Andric return nullptr;
51*0b57cec5SDimitry Andric
52*0b57cec5SDimitry Andric m_impl_backend->UpdateValueIfNeeded(false);
53*0b57cec5SDimitry Andric
54*0b57cec5SDimitry Andric ValueObjectConstResultChild *valobj = nullptr;
55*0b57cec5SDimitry Andric
56*0b57cec5SDimitry Andric bool omit_empty_base_classes = true;
57*0b57cec5SDimitry Andric bool ignore_array_bounds = synthetic_array_member;
58*0b57cec5SDimitry Andric std::string child_name_str;
59*0b57cec5SDimitry Andric uint32_t child_byte_size = 0;
60*0b57cec5SDimitry Andric int32_t child_byte_offset = 0;
61*0b57cec5SDimitry Andric uint32_t child_bitfield_bit_size = 0;
62*0b57cec5SDimitry Andric uint32_t child_bitfield_bit_offset = 0;
63*0b57cec5SDimitry Andric bool child_is_base_class = false;
64*0b57cec5SDimitry Andric bool child_is_deref_of_parent = false;
65*0b57cec5SDimitry Andric uint64_t language_flags;
66*0b57cec5SDimitry Andric
67*0b57cec5SDimitry Andric const bool transparent_pointers = !synthetic_array_member;
68*0b57cec5SDimitry Andric CompilerType compiler_type = m_impl_backend->GetCompilerType();
69*0b57cec5SDimitry Andric CompilerType child_compiler_type;
70*0b57cec5SDimitry Andric
71*0b57cec5SDimitry Andric ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
72*0b57cec5SDimitry Andric
73*0b57cec5SDimitry Andric child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
74*0b57cec5SDimitry Andric &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
75*0b57cec5SDimitry Andric ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
76*0b57cec5SDimitry Andric child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
77*0b57cec5SDimitry Andric child_is_deref_of_parent, m_impl_backend, language_flags);
78*0b57cec5SDimitry Andric
79*0b57cec5SDimitry Andric // One might think we should check that the size of the children
80*0b57cec5SDimitry Andric // is always strictly positive, hence we could avoid creating a
81*0b57cec5SDimitry Andric // ValueObject if that's not the case, but it turns out there
82*0b57cec5SDimitry Andric // are languages out there which allow zero-size types with
83*0b57cec5SDimitry Andric // children (e.g. Swift).
84*0b57cec5SDimitry Andric if (child_compiler_type) {
85*0b57cec5SDimitry Andric if (synthetic_index)
86*0b57cec5SDimitry Andric child_byte_offset += child_byte_size * synthetic_index;
87*0b57cec5SDimitry Andric
88*0b57cec5SDimitry Andric ConstString child_name;
89*0b57cec5SDimitry Andric if (!child_name_str.empty())
90*0b57cec5SDimitry Andric child_name.SetCString(child_name_str.c_str());
91*0b57cec5SDimitry Andric
92*0b57cec5SDimitry Andric lldb::addr_t child_live_addr = LLDB_INVALID_ADDRESS;
93*0b57cec5SDimitry Andric // Transfer the live address (with offset) to the child. But if
94*0b57cec5SDimitry Andric // the parent is a pointer, the live address is where that pointer
95*0b57cec5SDimitry Andric // value lives in memory, so the children live addresses aren't
96*0b57cec5SDimitry Andric // offsets from that value, they are just other load addresses that
97*0b57cec5SDimitry Andric // are recorded in the Value of the child ValueObjects.
98*0b57cec5SDimitry Andric if (m_live_address != LLDB_INVALID_ADDRESS) {
99*0b57cec5SDimitry Andric if (!compiler_type.IsPointerType())
100*0b57cec5SDimitry Andric child_live_addr = m_live_address + child_byte_offset;
101*0b57cec5SDimitry Andric }
102*0b57cec5SDimitry Andric valobj = new ValueObjectConstResultChild(
103*0b57cec5SDimitry Andric *m_impl_backend, child_compiler_type, child_name, child_byte_size,
104*0b57cec5SDimitry Andric child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
105*0b57cec5SDimitry Andric child_is_base_class, child_is_deref_of_parent, child_live_addr,
106*0b57cec5SDimitry Andric language_flags);
107*0b57cec5SDimitry Andric }
108*0b57cec5SDimitry Andric
109*0b57cec5SDimitry Andric return valobj;
110*0b57cec5SDimitry Andric }
111*0b57cec5SDimitry Andric
GetSyntheticChildAtOffset(uint32_t offset,const CompilerType & type,bool can_create,ConstString name_const_str)112*0b57cec5SDimitry Andric lldb::ValueObjectSP ValueObjectConstResultImpl::GetSyntheticChildAtOffset(
113*0b57cec5SDimitry Andric uint32_t offset, const CompilerType &type, bool can_create,
114*0b57cec5SDimitry Andric ConstString name_const_str) {
115*0b57cec5SDimitry Andric if (m_impl_backend == nullptr)
116*0b57cec5SDimitry Andric return lldb::ValueObjectSP();
117*0b57cec5SDimitry Andric
118*0b57cec5SDimitry Andric return m_impl_backend->ValueObject::GetSyntheticChildAtOffset(
119*0b57cec5SDimitry Andric offset, type, can_create, name_const_str);
120*0b57cec5SDimitry Andric }
121*0b57cec5SDimitry Andric
AddressOf(Status & error)122*0b57cec5SDimitry Andric lldb::ValueObjectSP ValueObjectConstResultImpl::AddressOf(Status &error) {
123*0b57cec5SDimitry Andric if (m_address_of_backend.get() != nullptr)
124*0b57cec5SDimitry Andric return m_address_of_backend;
125*0b57cec5SDimitry Andric
126*0b57cec5SDimitry Andric if (m_impl_backend == nullptr)
127*0b57cec5SDimitry Andric return lldb::ValueObjectSP();
128*0b57cec5SDimitry Andric if (m_live_address != LLDB_INVALID_ADDRESS) {
129*0b57cec5SDimitry Andric CompilerType compiler_type(m_impl_backend->GetCompilerType());
130*0b57cec5SDimitry Andric
131*0b57cec5SDimitry Andric lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(
132*0b57cec5SDimitry Andric &m_live_address, sizeof(lldb::addr_t)));
133*0b57cec5SDimitry Andric
134*0b57cec5SDimitry Andric std::string new_name("&");
135*0b57cec5SDimitry Andric new_name.append(m_impl_backend->GetName().AsCString(""));
136*0b57cec5SDimitry Andric ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
137*0b57cec5SDimitry Andric m_address_of_backend = ValueObjectConstResult::Create(
138*0b57cec5SDimitry Andric exe_ctx.GetBestExecutionContextScope(), compiler_type.GetPointerType(),
139*0b57cec5SDimitry Andric ConstString(new_name.c_str()), buffer, endian::InlHostByteOrder(),
140*0b57cec5SDimitry Andric exe_ctx.GetAddressByteSize());
141*0b57cec5SDimitry Andric
142*0b57cec5SDimitry Andric m_address_of_backend->GetValue().SetValueType(Value::ValueType::Scalar);
143*0b57cec5SDimitry Andric m_address_of_backend->GetValue().GetScalar() = m_live_address;
144*0b57cec5SDimitry Andric
145*0b57cec5SDimitry Andric return m_address_of_backend;
146*0b57cec5SDimitry Andric } else
147*0b57cec5SDimitry Andric return m_impl_backend->ValueObject::AddressOf(error);
148*0b57cec5SDimitry Andric }
149*0b57cec5SDimitry Andric
150*0b57cec5SDimitry Andric lldb::ValueObjectSP
Cast(const CompilerType & compiler_type)151*0b57cec5SDimitry Andric ValueObjectConstResultImpl::Cast(const CompilerType &compiler_type) {
152*0b57cec5SDimitry Andric if (m_impl_backend == nullptr)
153*0b57cec5SDimitry Andric return lldb::ValueObjectSP();
154*0b57cec5SDimitry Andric
155*0b57cec5SDimitry Andric ValueObjectConstResultCast *result_cast =
156*0b57cec5SDimitry Andric new ValueObjectConstResultCast(*m_impl_backend, m_impl_backend->GetName(),
157*0b57cec5SDimitry Andric compiler_type, m_live_address);
158*0b57cec5SDimitry Andric return result_cast->GetSP();
159*0b57cec5SDimitry Andric }
160*0b57cec5SDimitry Andric
161*0b57cec5SDimitry Andric lldb::addr_t
GetAddressOf(bool scalar_is_load_address,AddressType * address_type)162*0b57cec5SDimitry Andric ValueObjectConstResultImpl::GetAddressOf(bool scalar_is_load_address,
163*0b57cec5SDimitry Andric AddressType *address_type) {
164*0b57cec5SDimitry Andric
165*0b57cec5SDimitry Andric if (m_impl_backend == nullptr)
166*0b57cec5SDimitry Andric return 0;
167*0b57cec5SDimitry Andric
168*0b57cec5SDimitry Andric if (m_live_address == LLDB_INVALID_ADDRESS) {
169*0b57cec5SDimitry Andric return m_impl_backend->ValueObject::GetAddressOf(scalar_is_load_address,
170*0b57cec5SDimitry Andric address_type);
171*0b57cec5SDimitry Andric }
172*0b57cec5SDimitry Andric
173*0b57cec5SDimitry Andric if (address_type)
174*0b57cec5SDimitry Andric *address_type = m_live_address_type;
175*0b57cec5SDimitry Andric
176*0b57cec5SDimitry Andric return m_live_address;
177*0b57cec5SDimitry Andric }
178*0b57cec5SDimitry Andric
GetPointeeData(DataExtractor & data,uint32_t item_idx,uint32_t item_count)179*0b57cec5SDimitry Andric size_t ValueObjectConstResultImpl::GetPointeeData(DataExtractor &data,
180*0b57cec5SDimitry Andric uint32_t item_idx,
181 uint32_t item_count) {
182 if (m_impl_backend == nullptr)
183 return 0;
184 return m_impl_backend->ValueObject::GetPointeeData(data, item_idx,
185 item_count);
186 }
187