1 //===-- ValueObjectConstResultImpl.cpp ---------------------------*- C++-*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Core/ValueObjectConstResultImpl.h"
11 
12 #include "lldb/Core/Scalar.h"      // for Scalar
13 #include "lldb/Core/Value.h"       // for Value, Value::Val...
14 #include "lldb/Core/ValueObject.h" // for ValueObject
15 #include "lldb/Core/ValueObjectConstResult.h"
16 #include "lldb/Core/ValueObjectConstResultCast.h"
17 #include "lldb/Core/ValueObjectConstResultChild.h"
18 #include "lldb/Symbol/CompilerType.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Utility/DataBufferHeap.h" // for DataBufferHeap
21 #include "lldb/Utility/Endian.h"         // for InlHostByteOrder
22 #include "lldb/Utility/SharingPtr.h"     // for SharingPtr
23 
24 #include <string> // for string
25 
26 namespace lldb_private {
27 class DataExtractor;
28 }
29 namespace lldb_private {
30 class Error;
31 }
32 
33 using namespace lldb;
34 using namespace lldb_private;
35 
36 ValueObjectConstResultImpl::ValueObjectConstResultImpl(
37     ValueObject *valobj, lldb::addr_t live_address)
38     : m_impl_backend(valobj), m_live_address(live_address),
39       m_live_address_type(eAddressTypeLoad), m_load_addr_backend(),
40       m_address_of_backend() {}
41 
42 lldb::ValueObjectSP ValueObjectConstResultImpl::Dereference(Error &error) {
43   if (m_impl_backend == NULL)
44     return lldb::ValueObjectSP();
45 
46   return m_impl_backend->ValueObject::Dereference(error);
47 }
48 
49 ValueObject *ValueObjectConstResultImpl::CreateChildAtIndex(
50     size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
51   if (m_impl_backend == NULL)
52     return NULL;
53 
54   m_impl_backend->UpdateValueIfNeeded(false);
55 
56   ValueObjectConstResultChild *valobj = NULL;
57 
58   bool omit_empty_base_classes = true;
59   bool ignore_array_bounds = synthetic_array_member;
60   std::string child_name_str;
61   uint32_t child_byte_size = 0;
62   int32_t child_byte_offset = 0;
63   uint32_t child_bitfield_bit_size = 0;
64   uint32_t child_bitfield_bit_offset = 0;
65   bool child_is_base_class = false;
66   bool child_is_deref_of_parent = false;
67   uint64_t language_flags;
68 
69   const bool transparent_pointers = synthetic_array_member == false;
70   CompilerType compiler_type = m_impl_backend->GetCompilerType();
71   CompilerType child_compiler_type;
72 
73   ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
74 
75   child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
76       &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
77       ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
78       child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
79       child_is_deref_of_parent, m_impl_backend, language_flags);
80   if (child_compiler_type && child_byte_size) {
81     if (synthetic_index)
82       child_byte_offset += child_byte_size * synthetic_index;
83 
84     ConstString child_name;
85     if (!child_name_str.empty())
86       child_name.SetCString(child_name_str.c_str());
87 
88     valobj = new ValueObjectConstResultChild(
89         *m_impl_backend, child_compiler_type, child_name, child_byte_size,
90         child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
91         child_is_base_class, child_is_deref_of_parent,
92         m_live_address == LLDB_INVALID_ADDRESS
93             ? m_live_address
94             : m_live_address + child_byte_offset,
95         language_flags);
96   }
97 
98   return valobj;
99 }
100 
101 lldb::ValueObjectSP ValueObjectConstResultImpl::GetSyntheticChildAtOffset(
102     uint32_t offset, const CompilerType &type, bool can_create,
103     ConstString name_const_str) {
104   if (m_impl_backend == NULL)
105     return lldb::ValueObjectSP();
106 
107   return m_impl_backend->ValueObject::GetSyntheticChildAtOffset(
108       offset, type, can_create, name_const_str);
109 }
110 
111 lldb::ValueObjectSP ValueObjectConstResultImpl::AddressOf(Error &error) {
112   if (m_address_of_backend.get() != NULL)
113     return m_address_of_backend;
114 
115   if (m_impl_backend == NULL)
116     return lldb::ValueObjectSP();
117   if (m_live_address != LLDB_INVALID_ADDRESS) {
118     CompilerType compiler_type(m_impl_backend->GetCompilerType());
119 
120     lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(
121         &m_live_address, sizeof(lldb::addr_t)));
122 
123     std::string new_name("&");
124     new_name.append(m_impl_backend->GetName().AsCString(""));
125     ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
126     m_address_of_backend = ValueObjectConstResult::Create(
127         exe_ctx.GetBestExecutionContextScope(), compiler_type.GetPointerType(),
128         ConstString(new_name.c_str()), buffer, endian::InlHostByteOrder(),
129         exe_ctx.GetAddressByteSize());
130 
131     m_address_of_backend->GetValue().SetValueType(Value::eValueTypeScalar);
132     m_address_of_backend->GetValue().GetScalar() = m_live_address;
133 
134     return m_address_of_backend;
135   } else
136     return m_impl_backend->ValueObject::AddressOf(error);
137 }
138 
139 lldb::ValueObjectSP
140 ValueObjectConstResultImpl::Cast(const CompilerType &compiler_type) {
141   if (m_impl_backend == NULL)
142     return lldb::ValueObjectSP();
143 
144   ValueObjectConstResultCast *result_cast =
145       new ValueObjectConstResultCast(*m_impl_backend, m_impl_backend->GetName(),
146                                      compiler_type, m_live_address);
147   return result_cast->GetSP();
148 }
149 
150 lldb::addr_t
151 ValueObjectConstResultImpl::GetAddressOf(bool scalar_is_load_address,
152                                          AddressType *address_type) {
153 
154   if (m_impl_backend == NULL)
155     return 0;
156 
157   if (m_live_address == LLDB_INVALID_ADDRESS) {
158     return m_impl_backend->ValueObject::GetAddressOf(scalar_is_load_address,
159                                                      address_type);
160   }
161 
162   if (address_type)
163     *address_type = m_live_address_type;
164 
165   return m_live_address;
166 }
167 
168 size_t ValueObjectConstResultImpl::GetPointeeData(DataExtractor &data,
169                                                   uint32_t item_idx,
170                                                   uint32_t item_count) {
171   if (m_impl_backend == NULL)
172     return 0;
173   return m_impl_backend->ValueObject::GetPointeeData(data, item_idx,
174                                                      item_count);
175 }
176