1cfca06d7SDimitry Andric //===-- ValueObjectCast.cpp -----------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Core/ValueObjectCast.h"
10f034231aSEd Maste
11f034231aSEd Maste #include "lldb/Core/Value.h"
12f034231aSEd Maste #include "lldb/Core/ValueObject.h"
13e81d9d49SDimitry Andric #include "lldb/Symbol/CompilerType.h"
14f034231aSEd Maste #include "lldb/Target/ExecutionContext.h"
1594994d37SDimitry Andric #include "lldb/Utility/Scalar.h"
1694994d37SDimitry Andric #include "lldb/Utility/Status.h"
17e3b55780SDimitry Andric #include <optional>
1874a628f7SDimitry Andric
1974a628f7SDimitry Andric namespace lldb_private {
2074a628f7SDimitry Andric class ConstString;
2174a628f7SDimitry Andric }
22f034231aSEd Maste
23f034231aSEd Maste using namespace lldb_private;
24f034231aSEd Maste
Create(ValueObject & parent,ConstString name,const CompilerType & cast_type)2514f1b3e8SDimitry Andric lldb::ValueObjectSP ValueObjectCast::Create(ValueObject &parent,
265f29bb8aSDimitry Andric ConstString name,
2714f1b3e8SDimitry Andric const CompilerType &cast_type) {
2814f1b3e8SDimitry Andric ValueObjectCast *cast_valobj_ptr =
2914f1b3e8SDimitry Andric new ValueObjectCast(parent, name, cast_type);
30f034231aSEd Maste return cast_valobj_ptr->GetSP();
31f034231aSEd Maste }
32f034231aSEd Maste
ValueObjectCast(ValueObject & parent,ConstString name,const CompilerType & cast_type)335f29bb8aSDimitry Andric ValueObjectCast::ValueObjectCast(ValueObject &parent, ConstString name,
3414f1b3e8SDimitry Andric const CompilerType &cast_type)
3514f1b3e8SDimitry Andric : ValueObject(parent), m_cast_type(cast_type) {
36f034231aSEd Maste SetName(name);
37e81d9d49SDimitry Andric m_value.SetCompilerType(cast_type);
38f034231aSEd Maste }
39f034231aSEd Maste
40344a3780SDimitry Andric ValueObjectCast::~ValueObjectCast() = default;
41f034231aSEd Maste
GetCompilerTypeImpl()4214f1b3e8SDimitry Andric CompilerType ValueObjectCast::GetCompilerTypeImpl() { return m_cast_type; }
43f034231aSEd Maste
CalculateNumChildren(uint32_t max)44ac9a064cSDimitry Andric llvm::Expected<uint32_t> ValueObjectCast::CalculateNumChildren(uint32_t max) {
4594994d37SDimitry Andric ExecutionContext exe_ctx(GetExecutionContextRef());
4694994d37SDimitry Andric auto children_count = GetCompilerType().GetNumChildren(
4794994d37SDimitry Andric true, &exe_ctx);
48ac9a064cSDimitry Andric if (!children_count)
49ac9a064cSDimitry Andric return children_count;
50ac9a064cSDimitry Andric return *children_count <= max ? *children_count : max;
51f034231aSEd Maste }
52f034231aSEd Maste
GetByteSize()53e3b55780SDimitry Andric std::optional<uint64_t> ValueObjectCast::GetByteSize() {
54e81d9d49SDimitry Andric ExecutionContext exe_ctx(GetExecutionContextRef());
55e81d9d49SDimitry Andric return m_value.GetValueByteSize(nullptr, &exe_ctx);
56f034231aSEd Maste }
57f034231aSEd Maste
GetValueType() const5814f1b3e8SDimitry Andric lldb::ValueType ValueObjectCast::GetValueType() const {
59f034231aSEd Maste // Let our parent answer global, local, argument, etc...
60f034231aSEd Maste return m_parent->GetValueType();
61f034231aSEd Maste }
62f034231aSEd Maste
UpdateValue()6314f1b3e8SDimitry Andric bool ValueObjectCast::UpdateValue() {
64f034231aSEd Maste SetValueIsValid(false);
65f034231aSEd Maste m_error.Clear();
66f034231aSEd Maste
6714f1b3e8SDimitry Andric if (m_parent->UpdateValueIfNeeded(false)) {
68f034231aSEd Maste Value old_value(m_value);
69f034231aSEd Maste m_update_point.SetUpdated();
70f034231aSEd Maste m_value = m_parent->GetValue();
71e81d9d49SDimitry Andric CompilerType compiler_type(GetCompilerType());
72e81d9d49SDimitry Andric m_value.SetCompilerType(compiler_type);
73f034231aSEd Maste SetAddressTypeOfChildren(m_parent->GetAddressTypeOfChildren());
7414f1b3e8SDimitry Andric if (!CanProvideValue()) {
75f73363f1SDimitry Andric // this value object represents an aggregate type whose children have
76f73363f1SDimitry Andric // values, but this object does not. So we say we are changed if our
77f73363f1SDimitry Andric // location has changed.
7814f1b3e8SDimitry Andric SetValueDidChange(m_value.GetValueType() != old_value.GetValueType() ||
7914f1b3e8SDimitry Andric m_value.GetScalar() != old_value.GetScalar());
80f034231aSEd Maste }
81f034231aSEd Maste ExecutionContext exe_ctx(GetExecutionContextRef());
82ead24645SDimitry Andric m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
83f034231aSEd Maste SetValueDidChange(m_parent->GetValueDidChange());
84f034231aSEd Maste return true;
85f034231aSEd Maste }
86f034231aSEd Maste
87f034231aSEd Maste // The dynamic value failed to get an error, pass the error along
88f034231aSEd Maste if (m_error.Success() && m_parent->GetError().Fail())
89f034231aSEd Maste m_error = m_parent->GetError();
90f034231aSEd Maste SetValueIsValid(false);
91f034231aSEd Maste return false;
92f034231aSEd Maste }
93f034231aSEd Maste
IsInScope()9414f1b3e8SDimitry Andric bool ValueObjectCast::IsInScope() { return m_parent->IsInScope(); }
95