180814287SRaphael Isemann //===-- ValueObjectConstResultImpl.cpp ------------------------------------===//
29128ee2fSEnrico Granata //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69128ee2fSEnrico Granata //
79128ee2fSEnrico Granata //===----------------------------------------------------------------------===//
89128ee2fSEnrico Granata
99128ee2fSEnrico Granata #include "lldb/Core/ValueObjectConstResultImpl.h"
109128ee2fSEnrico Granata
11672d2c12SJonas Devlieghere #include "lldb/Core/Value.h"
12672d2c12SJonas Devlieghere #include "lldb/Core/ValueObject.h"
139128ee2fSEnrico Granata #include "lldb/Core/ValueObjectConstResult.h"
14f8877efcSSiva Chandra #include "lldb/Core/ValueObjectConstResultCast.h"
159128ee2fSEnrico Granata #include "lldb/Core/ValueObjectConstResultChild.h"
16a1e5dc86SGreg Clayton #include "lldb/Symbol/CompilerType.h"
179128ee2fSEnrico Granata #include "lldb/Target/ExecutionContext.h"
18672d2c12SJonas Devlieghere #include "lldb/Utility/DataBufferHeap.h"
19672d2c12SJonas Devlieghere #include "lldb/Utility/Endian.h"
20672d2c12SJonas Devlieghere #include "lldb/Utility/Scalar.h"
212f3df613SZachary Turner
22672d2c12SJonas Devlieghere #include <string>
232f3df613SZachary Turner
242f3df613SZachary Turner namespace lldb_private {
252f3df613SZachary Turner class DataExtractor;
262f3df613SZachary Turner }
272f3df613SZachary Turner namespace lldb_private {
2897206d57SZachary Turner class Status;
292f3df613SZachary Turner }
309128ee2fSEnrico Granata
319128ee2fSEnrico Granata using namespace lldb;
329128ee2fSEnrico Granata using namespace lldb_private;
339128ee2fSEnrico Granata
ValueObjectConstResultImpl(ValueObject * valobj,lldb::addr_t live_address)34b9c1b51eSKate Stone ValueObjectConstResultImpl::ValueObjectConstResultImpl(
35b9c1b51eSKate Stone ValueObject *valobj, lldb::addr_t live_address)
36b9c1b51eSKate Stone : m_impl_backend(valobj), m_live_address(live_address),
37*d9f56fedSJim Ingham m_live_address_type(eAddressTypeLoad),
38b9c1b51eSKate Stone m_address_of_backend() {}
399128ee2fSEnrico Granata
Dereference(Status & error)4097206d57SZachary Turner lldb::ValueObjectSP ValueObjectConstResultImpl::Dereference(Status &error) {
41248a1305SKonrad Kleine if (m_impl_backend == nullptr)
429128ee2fSEnrico Granata return lldb::ValueObjectSP();
439128ee2fSEnrico Granata
449128ee2fSEnrico Granata return m_impl_backend->ValueObject::Dereference(error);
459128ee2fSEnrico Granata }
469128ee2fSEnrico Granata
CreateChildAtIndex(size_t idx,bool synthetic_array_member,int32_t synthetic_index)47b9c1b51eSKate Stone ValueObject *ValueObjectConstResultImpl::CreateChildAtIndex(
48b9c1b51eSKate Stone size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
49248a1305SKonrad Kleine if (m_impl_backend == nullptr)
50248a1305SKonrad Kleine return nullptr;
519128ee2fSEnrico Granata
529128ee2fSEnrico Granata m_impl_backend->UpdateValueIfNeeded(false);
539128ee2fSEnrico Granata
54248a1305SKonrad Kleine ValueObjectConstResultChild *valobj = nullptr;
559128ee2fSEnrico Granata
569128ee2fSEnrico Granata bool omit_empty_base_classes = true;
579128ee2fSEnrico Granata bool ignore_array_bounds = synthetic_array_member;
589128ee2fSEnrico Granata std::string child_name_str;
599128ee2fSEnrico Granata uint32_t child_byte_size = 0;
609128ee2fSEnrico Granata int32_t child_byte_offset = 0;
619128ee2fSEnrico Granata uint32_t child_bitfield_bit_size = 0;
629128ee2fSEnrico Granata uint32_t child_bitfield_bit_offset = 0;
639128ee2fSEnrico Granata bool child_is_base_class = false;
649128ee2fSEnrico Granata bool child_is_deref_of_parent = false;
65dc62ffd3SEnrico Granata uint64_t language_flags;
669128ee2fSEnrico Granata
67a6682a41SJonas Devlieghere const bool transparent_pointers = !synthetic_array_member;
683ad353f3SBruce Mitchener CompilerType compiler_type = m_impl_backend->GetCompilerType();
694ad83340SBruce Mitchener CompilerType child_compiler_type;
709128ee2fSEnrico Granata
71cc4d0146SGreg Clayton ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
729128ee2fSEnrico Granata
73b9c1b51eSKate Stone child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
74b9c1b51eSKate Stone &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
75b9c1b51eSKate Stone ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
76b9c1b51eSKate Stone child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
77b9c1b51eSKate Stone child_is_deref_of_parent, m_impl_backend, language_flags);
7870152d32SDavide Italiano
7970152d32SDavide Italiano // One might think we should check that the size of the children
8070152d32SDavide Italiano // is always strictly positive, hence we could avoid creating a
8170152d32SDavide Italiano // ValueObject if that's not the case, but it turns out there
8270152d32SDavide Italiano // are languages out there which allow zero-size types with
8370152d32SDavide Italiano // children (e.g. Swift).
8470152d32SDavide Italiano if (child_compiler_type) {
859128ee2fSEnrico Granata if (synthetic_index)
869128ee2fSEnrico Granata child_byte_offset += child_byte_size * synthetic_index;
879128ee2fSEnrico Granata
889128ee2fSEnrico Granata ConstString child_name;
899128ee2fSEnrico Granata if (!child_name_str.empty())
909128ee2fSEnrico Granata child_name.SetCString(child_name_str.c_str());
919128ee2fSEnrico Granata
92b9c1b51eSKate Stone valobj = new ValueObjectConstResultChild(
93b9c1b51eSKate Stone *m_impl_backend, child_compiler_type, child_name, child_byte_size,
94b9c1b51eSKate Stone child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
95b9c1b51eSKate Stone child_is_base_class, child_is_deref_of_parent,
96b9c1b51eSKate Stone m_live_address == LLDB_INVALID_ADDRESS
97b9c1b51eSKate Stone ? m_live_address
98b9c1b51eSKate Stone : m_live_address + child_byte_offset,
99dc62ffd3SEnrico Granata language_flags);
1009128ee2fSEnrico Granata }
1019128ee2fSEnrico Granata
1029128ee2fSEnrico Granata return valobj;
1039128ee2fSEnrico Granata }
1049128ee2fSEnrico Granata
GetSyntheticChildAtOffset(uint32_t offset,const CompilerType & type,bool can_create,ConstString name_const_str)105b9c1b51eSKate Stone lldb::ValueObjectSP ValueObjectConstResultImpl::GetSyntheticChildAtOffset(
106b9c1b51eSKate Stone uint32_t offset, const CompilerType &type, bool can_create,
107b9c1b51eSKate Stone ConstString name_const_str) {
108248a1305SKonrad Kleine if (m_impl_backend == nullptr)
1099128ee2fSEnrico Granata return lldb::ValueObjectSP();
1109128ee2fSEnrico Granata
111b9c1b51eSKate Stone return m_impl_backend->ValueObject::GetSyntheticChildAtOffset(
112b9c1b51eSKate Stone offset, type, can_create, name_const_str);
1139128ee2fSEnrico Granata }
1149128ee2fSEnrico Granata
AddressOf(Status & error)11597206d57SZachary Turner lldb::ValueObjectSP ValueObjectConstResultImpl::AddressOf(Status &error) {
116248a1305SKonrad Kleine if (m_address_of_backend.get() != nullptr)
1179128ee2fSEnrico Granata return m_address_of_backend;
1189128ee2fSEnrico Granata
119248a1305SKonrad Kleine if (m_impl_backend == nullptr)
1209128ee2fSEnrico Granata return lldb::ValueObjectSP();
121b9c1b51eSKate Stone if (m_live_address != LLDB_INVALID_ADDRESS) {
1223ad353f3SBruce Mitchener CompilerType compiler_type(m_impl_backend->GetCompilerType());
1239128ee2fSEnrico Granata
124b9c1b51eSKate Stone lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(
125b9c1b51eSKate Stone &m_live_address, sizeof(lldb::addr_t)));
1269128ee2fSEnrico Granata
1279128ee2fSEnrico Granata std::string new_name("&");
1289128ee2fSEnrico Granata new_name.append(m_impl_backend->GetName().AsCString(""));
129cc4d0146SGreg Clayton ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
130b9c1b51eSKate Stone m_address_of_backend = ValueObjectConstResult::Create(
131b9c1b51eSKate Stone exe_ctx.GetBestExecutionContextScope(), compiler_type.GetPointerType(),
132b9c1b51eSKate Stone ConstString(new_name.c_str()), buffer, endian::InlHostByteOrder(),
133cc4d0146SGreg Clayton exe_ctx.GetAddressByteSize());
1349128ee2fSEnrico Granata
135057efa99SAdrian Prantl m_address_of_backend->GetValue().SetValueType(Value::ValueType::Scalar);
1369128ee2fSEnrico Granata m_address_of_backend->GetValue().GetScalar() = m_live_address;
1379128ee2fSEnrico Granata
1389128ee2fSEnrico Granata return m_address_of_backend;
139b9c1b51eSKate Stone } else
140987ed3ceSEnrico Granata return m_impl_backend->ValueObject::AddressOf(error);
1419128ee2fSEnrico Granata }
1429128ee2fSEnrico Granata
143f8877efcSSiva Chandra lldb::ValueObjectSP
Cast(const CompilerType & compiler_type)144b9c1b51eSKate Stone ValueObjectConstResultImpl::Cast(const CompilerType &compiler_type) {
145248a1305SKonrad Kleine if (m_impl_backend == nullptr)
146f8877efcSSiva Chandra return lldb::ValueObjectSP();
147f8877efcSSiva Chandra
148b9c1b51eSKate Stone ValueObjectConstResultCast *result_cast =
149b9c1b51eSKate Stone new ValueObjectConstResultCast(*m_impl_backend, m_impl_backend->GetName(),
150b9c1b51eSKate Stone compiler_type, m_live_address);
151f8877efcSSiva Chandra return result_cast->GetSP();
152f8877efcSSiva Chandra }
153f8877efcSSiva Chandra
154b456b792SJohnny Chen lldb::addr_t
GetAddressOf(bool scalar_is_load_address,AddressType * address_type)155b456b792SJohnny Chen ValueObjectConstResultImpl::GetAddressOf(bool scalar_is_load_address,
156b9c1b51eSKate Stone AddressType *address_type) {
157b456b792SJohnny Chen
158248a1305SKonrad Kleine if (m_impl_backend == nullptr)
159b456b792SJohnny Chen return 0;
160b456b792SJohnny Chen
161b9c1b51eSKate Stone if (m_live_address == LLDB_INVALID_ADDRESS) {
162b456b792SJohnny Chen return m_impl_backend->ValueObject::GetAddressOf(scalar_is_load_address,
163b456b792SJohnny Chen address_type);
164b456b792SJohnny Chen }
165b456b792SJohnny Chen
166b456b792SJohnny Chen if (address_type)
167b456b792SJohnny Chen *address_type = m_live_address_type;
168b456b792SJohnny Chen
169b456b792SJohnny Chen return m_live_address;
170b456b792SJohnny Chen }
171b456b792SJohnny Chen
GetPointeeData(DataExtractor & data,uint32_t item_idx,uint32_t item_count)172b9c1b51eSKate Stone size_t ValueObjectConstResultImpl::GetPointeeData(DataExtractor &data,
1739128ee2fSEnrico Granata uint32_t item_idx,
174b9c1b51eSKate Stone uint32_t item_count) {
175248a1305SKonrad Kleine if (m_impl_backend == nullptr)
1769128ee2fSEnrico Granata return 0;
177b9c1b51eSKate Stone return m_impl_backend->ValueObject::GetPointeeData(data, item_idx,
178b9c1b51eSKate Stone item_count);
1799128ee2fSEnrico Granata }
180