blob: bc24afcc60e72df189a3d39a2d42be6df1851e08 [file] [log] [blame]
Zachary Turner2f3df612017-04-06 21:28:29 +00001//===-- ValueObjectConstResultImpl.cpp ---------------------------*- C++-*-===//
Enrico Granata9128ee22011-09-06 19:20:51 +00002//
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
Zachary Turner2f3df612017-04-06 21:28:29 +000012#include "lldb/Core/Value.h" // for Value, Value::Val...
13#include "lldb/Core/ValueObject.h" // for ValueObject
Enrico Granata9128ee22011-09-06 19:20:51 +000014#include "lldb/Core/ValueObjectConstResult.h"
Siva Chandraf8877ef2015-07-16 01:47:12 +000015#include "lldb/Core/ValueObjectConstResultCast.h"
Enrico Granata9128ee22011-09-06 19:20:51 +000016#include "lldb/Core/ValueObjectConstResultChild.h"
Greg Claytona1e5dc82015-08-11 22:53:00 +000017#include "lldb/Symbol/CompilerType.h"
Enrico Granata9128ee22011-09-06 19:20:51 +000018#include "lldb/Target/ExecutionContext.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000019#include "lldb/Utility/DataBufferHeap.h" // for DataBufferHeap
20#include "lldb/Utility/Endian.h" // for InlHostByteOrder
Pavel Labathd821c992018-08-07 11:07:21 +000021#include "lldb/Utility/Scalar.h" // for Scalar
Zachary Turner2f3df612017-04-06 21:28:29 +000022#include "lldb/Utility/SharingPtr.h" // for SharingPtr
23
24#include <string> // for string
25
26namespace lldb_private {
27class DataExtractor;
28}
29namespace lldb_private {
Zachary Turner97206d52017-05-12 04:51:55 +000030class Status;
Zachary Turner2f3df612017-04-06 21:28:29 +000031}
Enrico Granata9128ee22011-09-06 19:20:51 +000032
33using namespace lldb;
34using namespace lldb_private;
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036ValueObjectConstResultImpl::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
Zachary Turner97206d52017-05-12 04:51:55 +000042lldb::ValueObjectSP ValueObjectConstResultImpl::Dereference(Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 if (m_impl_backend == NULL)
44 return lldb::ValueObjectSP();
45
46 return m_impl_backend->ValueObject::Dereference(error);
47}
48
49ValueObject *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);
Davide Italiano70152d32018-10-23 00:31:46 +000080
81 // One might think we should check that the size of the children
82 // is always strictly positive, hence we could avoid creating a
83 // ValueObject if that's not the case, but it turns out there
84 // are languages out there which allow zero-size types with
85 // children (e.g. Swift).
86 if (child_compiler_type) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 if (synthetic_index)
88 child_byte_offset += child_byte_size * synthetic_index;
89
90 ConstString child_name;
91 if (!child_name_str.empty())
92 child_name.SetCString(child_name_str.c_str());
93
94 valobj = new ValueObjectConstResultChild(
95 *m_impl_backend, child_compiler_type, child_name, child_byte_size,
96 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
97 child_is_base_class, child_is_deref_of_parent,
98 m_live_address == LLDB_INVALID_ADDRESS
99 ? m_live_address
100 : m_live_address + child_byte_offset,
101 language_flags);
102 }
103
104 return valobj;
105}
106
107lldb::ValueObjectSP ValueObjectConstResultImpl::GetSyntheticChildAtOffset(
108 uint32_t offset, const CompilerType &type, bool can_create,
109 ConstString name_const_str) {
110 if (m_impl_backend == NULL)
111 return lldb::ValueObjectSP();
112
113 return m_impl_backend->ValueObject::GetSyntheticChildAtOffset(
114 offset, type, can_create, name_const_str);
115}
116
Zachary Turner97206d52017-05-12 04:51:55 +0000117lldb::ValueObjectSP ValueObjectConstResultImpl::AddressOf(Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 if (m_address_of_backend.get() != NULL)
119 return m_address_of_backend;
120
121 if (m_impl_backend == NULL)
122 return lldb::ValueObjectSP();
123 if (m_live_address != LLDB_INVALID_ADDRESS) {
124 CompilerType compiler_type(m_impl_backend->GetCompilerType());
125
126 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(
127 &m_live_address, sizeof(lldb::addr_t)));
128
129 std::string new_name("&");
130 new_name.append(m_impl_backend->GetName().AsCString(""));
131 ExecutionContext exe_ctx(m_impl_backend->GetExecutionContextRef());
132 m_address_of_backend = ValueObjectConstResult::Create(
133 exe_ctx.GetBestExecutionContextScope(), compiler_type.GetPointerType(),
134 ConstString(new_name.c_str()), buffer, endian::InlHostByteOrder(),
135 exe_ctx.GetAddressByteSize());
136
137 m_address_of_backend->GetValue().SetValueType(Value::eValueTypeScalar);
138 m_address_of_backend->GetValue().GetScalar() = m_live_address;
139
140 return m_address_of_backend;
141 } else
142 return m_impl_backend->ValueObject::AddressOf(error);
Enrico Granata9128ee22011-09-06 19:20:51 +0000143}
144
145lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146ValueObjectConstResultImpl::Cast(const CompilerType &compiler_type) {
147 if (m_impl_backend == NULL)
148 return lldb::ValueObjectSP();
Enrico Granata9128ee22011-09-06 19:20:51 +0000149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 ValueObjectConstResultCast *result_cast =
151 new ValueObjectConstResultCast(*m_impl_backend, m_impl_backend->GetName(),
152 compiler_type, m_live_address);
153 return result_cast->GetSP();
Siva Chandraf8877ef2015-07-16 01:47:12 +0000154}
155
Johnny Chenb456b792011-12-16 23:04:52 +0000156lldb::addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157ValueObjectConstResultImpl::GetAddressOf(bool scalar_is_load_address,
158 AddressType *address_type) {
159
160 if (m_impl_backend == NULL)
161 return 0;
162
163 if (m_live_address == LLDB_INVALID_ADDRESS) {
164 return m_impl_backend->ValueObject::GetAddressOf(scalar_is_load_address,
165 address_type);
166 }
167
168 if (address_type)
169 *address_type = m_live_address_type;
170
171 return m_live_address;
Johnny Chenb456b792011-12-16 23:04:52 +0000172}
173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174size_t ValueObjectConstResultImpl::GetPointeeData(DataExtractor &data,
175 uint32_t item_idx,
176 uint32_t item_count) {
177 if (m_impl_backend == NULL)
178 return 0;
179 return m_impl_backend->ValueObject::GetPointeeData(data, item_idx,
180 item_count);
Enrico Granata9128ee22011-09-06 19:20:51 +0000181}