1f678e45dSDimitry Andric //===-- ValueObjectConstResultImpl.cpp ---------------------------*- C++-*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/Core/ValueObjectConstResultImpl.h"
11ac7ddfbfSEd Maste 
12*b5893f02SDimitry Andric #include "lldb/Core/Value.h"
13*b5893f02SDimitry Andric #include "lldb/Core/ValueObject.h"
14ac7ddfbfSEd Maste #include "lldb/Core/ValueObjectConstResult.h"
159f2f44ceSEd Maste #include "lldb/Core/ValueObjectConstResultCast.h"
16ac7ddfbfSEd Maste #include "lldb/Core/ValueObjectConstResultChild.h"
179f2f44ceSEd Maste #include "lldb/Symbol/CompilerType.h"
18ac7ddfbfSEd Maste #include "lldb/Target/ExecutionContext.h"
19*b5893f02SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
20*b5893f02SDimitry Andric #include "lldb/Utility/Endian.h"
21*b5893f02SDimitry Andric #include "lldb/Utility/Scalar.h"
22*b5893f02SDimitry Andric #include "lldb/Utility/SharingPtr.h"
23f678e45dSDimitry Andric 
24*b5893f02SDimitry Andric #include <string>
25f678e45dSDimitry Andric 
26f678e45dSDimitry Andric namespace lldb_private {
27f678e45dSDimitry Andric class DataExtractor;
28f678e45dSDimitry Andric }
29f678e45dSDimitry Andric namespace lldb_private {
305517e702SDimitry Andric class Status;
31f678e45dSDimitry Andric }
32ac7ddfbfSEd Maste 
33ac7ddfbfSEd Maste using namespace lldb;
34ac7ddfbfSEd Maste using namespace lldb_private;
35ac7ddfbfSEd Maste 
ValueObjectConstResultImpl(ValueObject * valobj,lldb::addr_t live_address)36435933ddSDimitry Andric ValueObjectConstResultImpl::ValueObjectConstResultImpl(
37435933ddSDimitry Andric     ValueObject *valobj, lldb::addr_t live_address)
38435933ddSDimitry Andric     : m_impl_backend(valobj), m_live_address(live_address),
39435933ddSDimitry Andric       m_live_address_type(eAddressTypeLoad), m_load_addr_backend(),
40435933ddSDimitry Andric       m_address_of_backend() {}
41ac7ddfbfSEd Maste 
Dereference(Status & error)425517e702SDimitry Andric lldb::ValueObjectSP ValueObjectConstResultImpl::Dereference(Status &error) {
43ac7ddfbfSEd Maste   if (m_impl_backend == NULL)
44ac7ddfbfSEd Maste     return lldb::ValueObjectSP();
45ac7ddfbfSEd Maste 
46ac7ddfbfSEd Maste   return m_impl_backend->ValueObject::Dereference(error);
47ac7ddfbfSEd Maste }
48ac7ddfbfSEd Maste 
CreateChildAtIndex(size_t idx,bool synthetic_array_member,int32_t synthetic_index)49435933ddSDimitry Andric ValueObject *ValueObjectConstResultImpl::CreateChildAtIndex(
50435933ddSDimitry Andric     size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
51ac7ddfbfSEd Maste   if (m_impl_backend == NULL)
52ac7ddfbfSEd Maste     return NULL;
53ac7ddfbfSEd Maste 
54ac7ddfbfSEd Maste   m_impl_backend->UpdateValueIfNeeded(false);
55ac7ddfbfSEd Maste 
56ac7ddfbfSEd Maste   ValueObjectConstResultChild *valobj = NULL;
57ac7ddfbfSEd Maste 
58ac7ddfbfSEd Maste   bool omit_empty_base_classes = true;
59ac7ddfbfSEd Maste   bool ignore_array_bounds = synthetic_array_member;
60ac7ddfbfSEd Maste   std::string child_name_str;
61ac7ddfbfSEd Maste   uint32_t child_byte_size = 0;
62ac7ddfbfSEd Maste   int32_t child_byte_offset = 0;
63ac7ddfbfSEd Maste   uint32_t child_bitfield_bit_size = 0;
64ac7ddfbfSEd Maste   uint32_t child_bitfield_bit_offset = 0;
65ac7ddfbfSEd Maste   bool child_is_base_class = false;
66ac7ddfbfSEd Maste   bool child_is_deref_of_parent = false;
679f2f44ceSEd Maste   uint64_t language_flags;
68ac7ddfbfSEd Maste 
69*b5893f02SDimitry Andric   const bool transparent_pointers = !synthetic_array_member;
709f2f44ceSEd Maste   CompilerType compiler_type = m_impl_backend->GetCompilerType();
719f2f44ceSEd Maste   CompilerType child_compiler_type;
72ac7ddfbfSEd Maste 
73ac7ddfbfSEd Maste   ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
74ac7ddfbfSEd Maste 
75435933ddSDimitry Andric   child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
76435933ddSDimitry Andric       &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
77435933ddSDimitry Andric       ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
78435933ddSDimitry Andric       child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
79435933ddSDimitry Andric       child_is_deref_of_parent, m_impl_backend, language_flags);
80*b5893f02SDimitry Andric 
81*b5893f02SDimitry Andric   // One might think we should check that the size of the children
82*b5893f02SDimitry Andric   // is always strictly positive, hence we could avoid creating a
83*b5893f02SDimitry Andric   // ValueObject if that's not the case, but it turns out there
84*b5893f02SDimitry Andric   // are languages out there which allow zero-size types with
85*b5893f02SDimitry Andric   // children (e.g. Swift).
86*b5893f02SDimitry Andric   if (child_compiler_type) {
87ac7ddfbfSEd Maste     if (synthetic_index)
88ac7ddfbfSEd Maste       child_byte_offset += child_byte_size * synthetic_index;
89ac7ddfbfSEd Maste 
90ac7ddfbfSEd Maste     ConstString child_name;
91ac7ddfbfSEd Maste     if (!child_name_str.empty())
92ac7ddfbfSEd Maste       child_name.SetCString(child_name_str.c_str());
93ac7ddfbfSEd Maste 
94435933ddSDimitry Andric     valobj = new ValueObjectConstResultChild(
95435933ddSDimitry Andric         *m_impl_backend, child_compiler_type, child_name, child_byte_size,
96435933ddSDimitry Andric         child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
97435933ddSDimitry Andric         child_is_base_class, child_is_deref_of_parent,
98435933ddSDimitry Andric         m_live_address == LLDB_INVALID_ADDRESS
99435933ddSDimitry Andric             ? m_live_address
100435933ddSDimitry Andric             : m_live_address + child_byte_offset,
1019f2f44ceSEd Maste         language_flags);
102ac7ddfbfSEd Maste   }
103ac7ddfbfSEd Maste 
104ac7ddfbfSEd Maste   return valobj;
105ac7ddfbfSEd Maste }
106ac7ddfbfSEd Maste 
GetSyntheticChildAtOffset(uint32_t offset,const CompilerType & type,bool can_create,ConstString name_const_str)107435933ddSDimitry Andric lldb::ValueObjectSP ValueObjectConstResultImpl::GetSyntheticChildAtOffset(
108435933ddSDimitry Andric     uint32_t offset, const CompilerType &type, bool can_create,
109435933ddSDimitry Andric     ConstString name_const_str) {
110ac7ddfbfSEd Maste   if (m_impl_backend == NULL)
111ac7ddfbfSEd Maste     return lldb::ValueObjectSP();
112ac7ddfbfSEd Maste 
113435933ddSDimitry Andric   return m_impl_backend->ValueObject::GetSyntheticChildAtOffset(
114435933ddSDimitry Andric       offset, type, can_create, name_const_str);
115ac7ddfbfSEd Maste }
116ac7ddfbfSEd Maste 
AddressOf(Status & error)1175517e702SDimitry Andric lldb::ValueObjectSP ValueObjectConstResultImpl::AddressOf(Status &error) {
118ac7ddfbfSEd Maste   if (m_address_of_backend.get() != NULL)
119ac7ddfbfSEd Maste     return m_address_of_backend;
120ac7ddfbfSEd Maste 
121ac7ddfbfSEd Maste   if (m_impl_backend == NULL)
122ac7ddfbfSEd Maste     return lldb::ValueObjectSP();
123435933ddSDimitry Andric   if (m_live_address != LLDB_INVALID_ADDRESS) {
1249f2f44ceSEd Maste     CompilerType compiler_type(m_impl_backend->GetCompilerType());
125ac7ddfbfSEd Maste 
126435933ddSDimitry Andric     lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(
127435933ddSDimitry Andric         &m_live_address, sizeof(lldb::addr_t)));
128ac7ddfbfSEd Maste 
129ac7ddfbfSEd Maste     std::string new_name("&");
130ac7ddfbfSEd Maste     new_name.append(m_impl_backend->GetName().AsCString(""));
131ac7ddfbfSEd Maste     ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
132435933ddSDimitry Andric     m_address_of_backend = ValueObjectConstResult::Create(
133435933ddSDimitry Andric         exe_ctx.GetBestExecutionContextScope(), compiler_type.GetPointerType(),
134435933ddSDimitry Andric         ConstString(new_name.c_str()), buffer, endian::InlHostByteOrder(),
135ac7ddfbfSEd Maste         exe_ctx.GetAddressByteSize());
136ac7ddfbfSEd Maste 
137ac7ddfbfSEd Maste     m_address_of_backend->GetValue().SetValueType(Value::eValueTypeScalar);
138ac7ddfbfSEd Maste     m_address_of_backend->GetValue().GetScalar() = m_live_address;
139ac7ddfbfSEd Maste 
140ac7ddfbfSEd Maste     return m_address_of_backend;
141435933ddSDimitry Andric   } else
1427aa51b79SEd Maste     return m_impl_backend->ValueObject::AddressOf(error);
143ac7ddfbfSEd Maste }
144ac7ddfbfSEd Maste 
1459f2f44ceSEd Maste lldb::ValueObjectSP
Cast(const CompilerType & compiler_type)146435933ddSDimitry Andric ValueObjectConstResultImpl::Cast(const CompilerType &compiler_type) {
1479f2f44ceSEd Maste   if (m_impl_backend == NULL)
1489f2f44ceSEd Maste     return lldb::ValueObjectSP();
1499f2f44ceSEd Maste 
150435933ddSDimitry Andric   ValueObjectConstResultCast *result_cast =
151435933ddSDimitry Andric       new ValueObjectConstResultCast(*m_impl_backend, m_impl_backend->GetName(),
152435933ddSDimitry Andric                                      compiler_type, m_live_address);
1539f2f44ceSEd Maste   return result_cast->GetSP();
1549f2f44ceSEd Maste }
1559f2f44ceSEd Maste 
156ac7ddfbfSEd Maste lldb::addr_t
GetAddressOf(bool scalar_is_load_address,AddressType * address_type)157ac7ddfbfSEd Maste ValueObjectConstResultImpl::GetAddressOf(bool scalar_is_load_address,
158435933ddSDimitry Andric                                          AddressType *address_type) {
159ac7ddfbfSEd Maste 
160ac7ddfbfSEd Maste   if (m_impl_backend == NULL)
161ac7ddfbfSEd Maste     return 0;
162ac7ddfbfSEd Maste 
163435933ddSDimitry Andric   if (m_live_address == LLDB_INVALID_ADDRESS) {
164ac7ddfbfSEd Maste     return m_impl_backend->ValueObject::GetAddressOf(scalar_is_load_address,
165ac7ddfbfSEd Maste                                                      address_type);
166ac7ddfbfSEd Maste   }
167ac7ddfbfSEd Maste 
168ac7ddfbfSEd Maste   if (address_type)
169ac7ddfbfSEd Maste     *address_type = m_live_address_type;
170ac7ddfbfSEd Maste 
171ac7ddfbfSEd Maste   return m_live_address;
172ac7ddfbfSEd Maste }
173ac7ddfbfSEd Maste 
GetPointeeData(DataExtractor & data,uint32_t item_idx,uint32_t item_count)174435933ddSDimitry Andric size_t ValueObjectConstResultImpl::GetPointeeData(DataExtractor &data,
175ac7ddfbfSEd Maste                                                   uint32_t item_idx,
176435933ddSDimitry Andric                                                   uint32_t item_count) {
177ac7ddfbfSEd Maste   if (m_impl_backend == NULL)
178ac7ddfbfSEd Maste     return 0;
179435933ddSDimitry Andric   return m_impl_backend->ValueObject::GetPointeeData(data, item_idx,
180435933ddSDimitry Andric                                                      item_count);
181ac7ddfbfSEd Maste }
182