[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / source / ValueObject / ValueObjectConstResult.cpp
blobba4f7aa244626d1fbac28942bff0671c0217fc69
1 //===-- ValueObjectConstResult.cpp ----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "lldb/ValueObject/ValueObjectConstResult.h"
11 #include "lldb/Symbol/CompilerType.h"
12 #include "lldb/Target/ExecutionContext.h"
13 #include "lldb/Target/ExecutionContextScope.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Utility/DataBuffer.h"
16 #include "lldb/Utility/DataBufferHeap.h"
17 #include "lldb/Utility/DataExtractor.h"
18 #include "lldb/Utility/Scalar.h"
19 #include "lldb/ValueObject/ValueObjectDynamicValue.h"
20 #include <optional>
22 namespace lldb_private {
23 class Module;
26 using namespace lldb;
27 using namespace lldb_private;
29 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
30 ByteOrder byte_order,
31 uint32_t addr_byte_size,
32 lldb::addr_t address) {
33 auto manager_sp = ValueObjectManager::Create();
34 return (new ValueObjectConstResult(exe_scope, *manager_sp, byte_order,
35 addr_byte_size, address))
36 ->GetSP();
39 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
40 ValueObjectManager &manager,
41 ByteOrder byte_order,
42 uint32_t addr_byte_size,
43 lldb::addr_t address)
44 : ValueObject(exe_scope, manager), m_impl(this, address) {
45 SetIsConstant();
46 SetValueIsValid(true);
47 m_data.SetByteOrder(byte_order);
48 m_data.SetAddressByteSize(addr_byte_size);
49 SetAddressTypeOfChildren(eAddressTypeLoad);
52 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
53 const CompilerType &compiler_type,
54 ConstString name,
55 const DataExtractor &data,
56 lldb::addr_t address) {
57 auto manager_sp = ValueObjectManager::Create();
58 return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
59 name, data, address))
60 ->GetSP();
63 ValueObjectConstResult::ValueObjectConstResult(
64 ExecutionContextScope *exe_scope, ValueObjectManager &manager,
65 const CompilerType &compiler_type, ConstString name,
66 const DataExtractor &data, lldb::addr_t address)
67 : ValueObject(exe_scope, manager), m_impl(this, address) {
68 m_data = data;
70 if (!m_data.GetSharedDataBuffer()) {
71 DataBufferSP shared_data_buffer(
72 new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
73 m_data.SetData(shared_data_buffer);
76 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
77 m_value.SetValueType(Value::ValueType::HostAddress);
78 m_value.SetCompilerType(compiler_type);
79 m_name = name;
80 SetIsConstant();
81 SetValueIsValid(true);
82 SetAddressTypeOfChildren(eAddressTypeLoad);
85 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
86 const CompilerType &compiler_type,
87 ConstString name,
88 const lldb::DataBufferSP &data_sp,
89 lldb::ByteOrder data_byte_order,
90 uint32_t data_addr_size,
91 lldb::addr_t address) {
92 auto manager_sp = ValueObjectManager::Create();
93 return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
94 name, data_sp, data_byte_order,
95 data_addr_size, address))
96 ->GetSP();
99 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
100 Value &value, ConstString name,
101 Module *module) {
102 auto manager_sp = ValueObjectManager::Create();
103 return (new ValueObjectConstResult(exe_scope, *manager_sp, value, name,
104 module))
105 ->GetSP();
108 ValueObjectConstResult::ValueObjectConstResult(
109 ExecutionContextScope *exe_scope, ValueObjectManager &manager,
110 const CompilerType &compiler_type, ConstString name,
111 const lldb::DataBufferSP &data_sp, lldb::ByteOrder data_byte_order,
112 uint32_t data_addr_size, lldb::addr_t address)
113 : ValueObject(exe_scope, manager), m_impl(this, address) {
114 m_data.SetByteOrder(data_byte_order);
115 m_data.SetAddressByteSize(data_addr_size);
116 m_data.SetData(data_sp);
117 m_value.GetScalar() = (uintptr_t)data_sp->GetBytes();
118 m_value.SetValueType(Value::ValueType::HostAddress);
119 m_value.SetCompilerType(compiler_type);
120 m_name = name;
121 SetIsConstant();
122 SetValueIsValid(true);
123 SetAddressTypeOfChildren(eAddressTypeLoad);
126 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
127 const CompilerType &compiler_type,
128 ConstString name,
129 lldb::addr_t address,
130 AddressType address_type,
131 uint32_t addr_byte_size) {
132 auto manager_sp = ValueObjectManager::Create();
133 return (new ValueObjectConstResult(exe_scope, *manager_sp, compiler_type,
134 name, address, address_type,
135 addr_byte_size))
136 ->GetSP();
139 ValueObjectConstResult::ValueObjectConstResult(
140 ExecutionContextScope *exe_scope, ValueObjectManager &manager,
141 const CompilerType &compiler_type, ConstString name, lldb::addr_t address,
142 AddressType address_type, uint32_t addr_byte_size)
143 : ValueObject(exe_scope, manager), m_type_name(), m_impl(this, address) {
144 m_value.GetScalar() = address;
145 m_data.SetAddressByteSize(addr_byte_size);
146 m_value.GetScalar().GetData(m_data, addr_byte_size);
147 // m_value.SetValueType(Value::ValueType::HostAddress);
148 switch (address_type) {
149 case eAddressTypeInvalid:
150 m_value.SetValueType(Value::ValueType::Scalar);
151 break;
152 case eAddressTypeFile:
153 m_value.SetValueType(Value::ValueType::FileAddress);
154 break;
155 case eAddressTypeLoad:
156 m_value.SetValueType(Value::ValueType::LoadAddress);
157 break;
158 case eAddressTypeHost:
159 m_value.SetValueType(Value::ValueType::HostAddress);
160 break;
162 m_value.SetCompilerType(compiler_type);
163 m_name = name;
164 SetIsConstant();
165 SetValueIsValid(true);
166 SetAddressTypeOfChildren(eAddressTypeLoad);
169 ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope,
170 Status &&error) {
171 auto manager_sp = ValueObjectManager::Create();
172 return (new ValueObjectConstResult(exe_scope, *manager_sp, std::move(error)))
173 ->GetSP();
176 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
177 ValueObjectManager &manager,
178 Status &&error)
179 : ValueObject(exe_scope, manager), m_impl(this) {
180 m_error = std::move(error);
181 SetIsConstant();
184 ValueObjectConstResult::ValueObjectConstResult(ExecutionContextScope *exe_scope,
185 ValueObjectManager &manager,
186 const Value &value,
187 ConstString name, Module *module)
188 : ValueObject(exe_scope, manager), m_impl(this) {
189 m_value = value;
190 m_name = name;
191 ExecutionContext exe_ctx;
192 exe_scope->CalculateExecutionContext(exe_ctx);
193 m_error = m_value.GetValueAsData(&exe_ctx, m_data, module);
196 ValueObjectConstResult::~ValueObjectConstResult() = default;
198 CompilerType ValueObjectConstResult::GetCompilerTypeImpl() {
199 return m_value.GetCompilerType();
202 lldb::ValueType ValueObjectConstResult::GetValueType() const {
203 return eValueTypeConstResult;
206 std::optional<uint64_t> ValueObjectConstResult::GetByteSize() {
207 ExecutionContext exe_ctx(GetExecutionContextRef());
208 if (!m_byte_size) {
209 if (auto size = GetCompilerType().GetByteSize(
210 exe_ctx.GetBestExecutionContextScope()))
211 SetByteSize(*size);
213 return m_byte_size;
216 void ValueObjectConstResult::SetByteSize(size_t size) { m_byte_size = size; }
218 llvm::Expected<uint32_t>
219 ValueObjectConstResult::CalculateNumChildren(uint32_t max) {
220 ExecutionContext exe_ctx(GetExecutionContextRef());
221 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
222 if (!children_count)
223 return children_count;
224 return *children_count <= max ? *children_count : max;
227 ConstString ValueObjectConstResult::GetTypeName() {
228 if (m_type_name.IsEmpty())
229 m_type_name = GetCompilerType().GetTypeName();
230 return m_type_name;
233 ConstString ValueObjectConstResult::GetDisplayTypeName() {
234 return GetCompilerType().GetDisplayTypeName();
237 bool ValueObjectConstResult::UpdateValue() {
238 // Const value is always valid
239 SetValueIsValid(true);
240 return true;
243 bool ValueObjectConstResult::IsInScope() {
244 // A const result value is always in scope since it serializes all
245 // information needed to contain the constant value.
246 return true;
249 lldb::ValueObjectSP ValueObjectConstResult::Dereference(Status &error) {
250 return m_impl.Dereference(error);
253 lldb::ValueObjectSP ValueObjectConstResult::GetSyntheticChildAtOffset(
254 uint32_t offset, const CompilerType &type, bool can_create,
255 ConstString name_const_str) {
256 return m_impl.GetSyntheticChildAtOffset(offset, type, can_create,
257 name_const_str);
260 lldb::ValueObjectSP ValueObjectConstResult::AddressOf(Status &error) {
261 return m_impl.AddressOf(error);
264 lldb::addr_t ValueObjectConstResult::GetAddressOf(bool scalar_is_load_address,
265 AddressType *address_type) {
266 return m_impl.GetAddressOf(scalar_is_load_address, address_type);
269 size_t ValueObjectConstResult::GetPointeeData(DataExtractor &data,
270 uint32_t item_idx,
271 uint32_t item_count) {
272 return m_impl.GetPointeeData(data, item_idx, item_count);
275 lldb::ValueObjectSP
276 ValueObjectConstResult::GetDynamicValue(lldb::DynamicValueType use_dynamic) {
277 // Always recalculate dynamic values for const results as the memory that
278 // they might point to might have changed at any time.
279 if (use_dynamic != eNoDynamicValues) {
280 if (!IsDynamic()) {
281 ExecutionContext exe_ctx(GetExecutionContextRef());
282 Process *process = exe_ctx.GetProcessPtr();
283 if (process && process->IsPossibleDynamicValue(*this))
284 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
286 if (m_dynamic_value && m_dynamic_value->GetError().Success())
287 return m_dynamic_value->GetSP();
289 return ValueObjectSP();
292 lldb::ValueObjectSP
293 ValueObjectConstResult::DoCast(const CompilerType &compiler_type) {
294 return m_impl.Cast(compiler_type);
297 lldb::LanguageType ValueObjectConstResult::GetPreferredDisplayLanguage() {
298 if (m_preferred_display_language != lldb::eLanguageTypeUnknown)
299 return m_preferred_display_language;
300 return GetCompilerTypeImpl().GetMinimumLanguage();