1*0b57cec5SDimitry Andric //===-- ValueObjectCast.cpp -----------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Core/ValueObjectCast.h"
10*0b57cec5SDimitry Andric 
11*0b57cec5SDimitry Andric #include "lldb/Core/Value.h"
12*0b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
13*0b57cec5SDimitry Andric #include "lldb/Symbol/CompilerType.h"
14*0b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
15*0b57cec5SDimitry Andric #include "lldb/Utility/Scalar.h"
16*0b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
17*0b57cec5SDimitry Andric #include <optional>
18*0b57cec5SDimitry Andric 
19*0b57cec5SDimitry Andric namespace lldb_private {
20*0b57cec5SDimitry Andric class ConstString;
21*0b57cec5SDimitry Andric }
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric using namespace lldb_private;
24*0b57cec5SDimitry Andric 
Create(ValueObject & parent,ConstString name,const CompilerType & cast_type)25*0b57cec5SDimitry Andric lldb::ValueObjectSP ValueObjectCast::Create(ValueObject &parent,
26*0b57cec5SDimitry Andric                                             ConstString name,
27*0b57cec5SDimitry Andric                                             const CompilerType &cast_type) {
28*0b57cec5SDimitry Andric   ValueObjectCast *cast_valobj_ptr =
29*0b57cec5SDimitry Andric       new ValueObjectCast(parent, name, cast_type);
30*0b57cec5SDimitry Andric   return cast_valobj_ptr->GetSP();
31*0b57cec5SDimitry Andric }
32*0b57cec5SDimitry Andric 
ValueObjectCast(ValueObject & parent,ConstString name,const CompilerType & cast_type)33*0b57cec5SDimitry Andric ValueObjectCast::ValueObjectCast(ValueObject &parent, ConstString name,
34*0b57cec5SDimitry Andric                                  const CompilerType &cast_type)
35*0b57cec5SDimitry Andric     : ValueObject(parent), m_cast_type(cast_type) {
36*0b57cec5SDimitry Andric   SetName(name);
37*0b57cec5SDimitry Andric   m_value.SetCompilerType(cast_type);
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric ValueObjectCast::~ValueObjectCast() = default;
41*0b57cec5SDimitry Andric 
GetCompilerTypeImpl()42*0b57cec5SDimitry Andric CompilerType ValueObjectCast::GetCompilerTypeImpl() { return m_cast_type; }
43*0b57cec5SDimitry Andric 
CalculateNumChildren(uint32_t max)44*0b57cec5SDimitry Andric size_t ValueObjectCast::CalculateNumChildren(uint32_t max) {
45*0b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
46*0b57cec5SDimitry Andric   auto children_count = GetCompilerType().GetNumChildren(
47*0b57cec5SDimitry Andric       true, &exe_ctx);
48*0b57cec5SDimitry Andric   return children_count <= max ? children_count : max;
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
GetByteSize()51*0b57cec5SDimitry Andric std::optional<uint64_t> ValueObjectCast::GetByteSize() {
52*0b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
53*0b57cec5SDimitry Andric   return m_value.GetValueByteSize(nullptr, &exe_ctx);
54*0b57cec5SDimitry Andric }
55*0b57cec5SDimitry Andric 
GetValueType() const56*0b57cec5SDimitry Andric lldb::ValueType ValueObjectCast::GetValueType() const {
57*0b57cec5SDimitry Andric   // Let our parent answer global, local, argument, etc...
58*0b57cec5SDimitry Andric   return m_parent->GetValueType();
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric 
UpdateValue()61*0b57cec5SDimitry Andric bool ValueObjectCast::UpdateValue() {
62*0b57cec5SDimitry Andric   SetValueIsValid(false);
63*0b57cec5SDimitry Andric   m_error.Clear();
64*0b57cec5SDimitry Andric 
65*0b57cec5SDimitry Andric   if (m_parent->UpdateValueIfNeeded(false)) {
66*0b57cec5SDimitry Andric     Value old_value(m_value);
67*0b57cec5SDimitry Andric     m_update_point.SetUpdated();
68*0b57cec5SDimitry Andric     m_value = m_parent->GetValue();
69*0b57cec5SDimitry Andric     CompilerType compiler_type(GetCompilerType());
70*0b57cec5SDimitry Andric     m_value.SetCompilerType(compiler_type);
71*0b57cec5SDimitry Andric     SetAddressTypeOfChildren(m_parent->GetAddressTypeOfChildren());
72*0b57cec5SDimitry Andric     if (!CanProvideValue()) {
73*0b57cec5SDimitry Andric       // this value object represents an aggregate type whose children have
74*0b57cec5SDimitry Andric       // values, but this object does not. So we say we are changed if our
75*0b57cec5SDimitry Andric       // location has changed.
76*0b57cec5SDimitry Andric       SetValueDidChange(m_value.GetValueType() != old_value.GetValueType() ||
77*0b57cec5SDimitry Andric                         m_value.GetScalar() != old_value.GetScalar());
78*0b57cec5SDimitry Andric     }
79*0b57cec5SDimitry Andric     ExecutionContext exe_ctx(GetExecutionContextRef());
80*0b57cec5SDimitry Andric     m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
81*0b57cec5SDimitry Andric     SetValueDidChange(m_parent->GetValueDidChange());
82*0b57cec5SDimitry Andric     return true;
83*0b57cec5SDimitry Andric   }
84*0b57cec5SDimitry Andric 
85*0b57cec5SDimitry Andric   // The dynamic value failed to get an error, pass the error along
86*0b57cec5SDimitry Andric   if (m_error.Success() && m_parent->GetError().Fail())
87*0b57cec5SDimitry Andric     m_error = m_parent->GetError();
88*0b57cec5SDimitry Andric   SetValueIsValid(false);
89*0b57cec5SDimitry Andric   return false;
90*0b57cec5SDimitry Andric }
91*0b57cec5SDimitry Andric 
IsInScope()92*0b57cec5SDimitry Andric bool ValueObjectCast::IsInScope() { return m_parent->IsInScope(); }
93*0b57cec5SDimitry Andric