180814287SRaphael Isemann //===-- ValueObjectCast.cpp -----------------------------------------------===//
221fd13f9SEnrico Granata //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
621fd13f9SEnrico Granata //
721fd13f9SEnrico Granata //===----------------------------------------------------------------------===//
821fd13f9SEnrico Granata 
921fd13f9SEnrico Granata #include "lldb/Core/ValueObjectCast.h"
1021fd13f9SEnrico Granata 
1121fd13f9SEnrico Granata #include "lldb/Core/Value.h"
1221fd13f9SEnrico Granata #include "lldb/Core/ValueObject.h"
13a1e5dc86SGreg Clayton #include "lldb/Symbol/CompilerType.h"
1421fd13f9SEnrico Granata #include "lldb/Target/ExecutionContext.h"
15672d2c12SJonas Devlieghere #include "lldb/Utility/Scalar.h"
16672d2c12SJonas Devlieghere #include "lldb/Utility/Status.h"
172f3df613SZachary Turner 
182f3df613SZachary Turner namespace lldb_private {
192f3df613SZachary Turner class ConstString;
202f3df613SZachary Turner }
2121fd13f9SEnrico Granata 
2221fd13f9SEnrico Granata using namespace lldb_private;
2321fd13f9SEnrico Granata 
Create(ValueObject & parent,ConstString name,const CompilerType & cast_type)24b9c1b51eSKate Stone lldb::ValueObjectSP ValueObjectCast::Create(ValueObject &parent,
250e4c4821SAdrian Prantl                                             ConstString name,
26b9c1b51eSKate Stone                                             const CompilerType &cast_type) {
27b9c1b51eSKate Stone   ValueObjectCast *cast_valobj_ptr =
28b9c1b51eSKate Stone       new ValueObjectCast(parent, name, cast_type);
2921fd13f9SEnrico Granata   return cast_valobj_ptr->GetSP();
3021fd13f9SEnrico Granata }
3121fd13f9SEnrico Granata 
ValueObjectCast(ValueObject & parent,ConstString name,const CompilerType & cast_type)320e4c4821SAdrian Prantl ValueObjectCast::ValueObjectCast(ValueObject &parent, ConstString name,
33b9c1b51eSKate Stone                                  const CompilerType &cast_type)
34b9c1b51eSKate Stone     : ValueObject(parent), m_cast_type(cast_type) {
3521fd13f9SEnrico Granata   SetName(name);
3699558cc4SGreg Clayton   m_value.SetCompilerType(cast_type);
3721fd13f9SEnrico Granata }
3821fd13f9SEnrico Granata 
39*fd2433e1SJonas Devlieghere ValueObjectCast::~ValueObjectCast() = default;
4021fd13f9SEnrico Granata 
GetCompilerTypeImpl()41b9c1b51eSKate Stone CompilerType ValueObjectCast::GetCompilerTypeImpl() { return m_cast_type; }
4221fd13f9SEnrico Granata 
CalculateNumChildren(uint32_t max)43b9c1b51eSKate Stone size_t ValueObjectCast::CalculateNumChildren(uint32_t max) {
44eca07c59SAdrian Prantl   ExecutionContext exe_ctx(GetExecutionContextRef());
45eca07c59SAdrian Prantl   auto children_count = GetCompilerType().GetNumChildren(
46eca07c59SAdrian Prantl       true, &exe_ctx);
479ac7a6c5SSiva Chandra   return children_count <= max ? children_count : max;
4821fd13f9SEnrico Granata }
4921fd13f9SEnrico Granata 
GetByteSize()50113f56fbSAdrian Prantl llvm::Optional<uint64_t> ValueObjectCast::GetByteSize() {
5195438038SEnrico Granata   ExecutionContext exe_ctx(GetExecutionContextRef());
5295438038SEnrico Granata   return m_value.GetValueByteSize(nullptr, &exe_ctx);
5321fd13f9SEnrico Granata }
5421fd13f9SEnrico Granata 
GetValueType() const55b9c1b51eSKate Stone lldb::ValueType ValueObjectCast::GetValueType() const {
5621fd13f9SEnrico Granata   // Let our parent answer global, local, argument, etc...
5721fd13f9SEnrico Granata   return m_parent->GetValueType();
5821fd13f9SEnrico Granata }
5921fd13f9SEnrico Granata 
UpdateValue()60b9c1b51eSKate Stone bool ValueObjectCast::UpdateValue() {
6121fd13f9SEnrico Granata   SetValueIsValid(false);
6221fd13f9SEnrico Granata   m_error.Clear();
6321fd13f9SEnrico Granata 
64b9c1b51eSKate Stone   if (m_parent->UpdateValueIfNeeded(false)) {
6521fd13f9SEnrico Granata     Value old_value(m_value);
6621fd13f9SEnrico Granata     m_update_point.SetUpdated();
6721fd13f9SEnrico Granata     m_value = m_parent->GetValue();
683ad353f3SBruce Mitchener     CompilerType compiler_type(GetCompilerType());
693ad353f3SBruce Mitchener     m_value.SetCompilerType(compiler_type);
7021fd13f9SEnrico Granata     SetAddressTypeOfChildren(m_parent->GetAddressTypeOfChildren());
71b9c1b51eSKate Stone     if (!CanProvideValue()) {
7205097246SAdrian Prantl       // this value object represents an aggregate type whose children have
7305097246SAdrian Prantl       // values, but this object does not. So we say we are changed if our
7405097246SAdrian Prantl       // location has changed.
75b9c1b51eSKate Stone       SetValueDidChange(m_value.GetValueType() != old_value.GetValueType() ||
76b9c1b51eSKate Stone                         m_value.GetScalar() != old_value.GetScalar());
7721fd13f9SEnrico Granata     }
7821fd13f9SEnrico Granata     ExecutionContext exe_ctx(GetExecutionContextRef());
79d9cbd2acSAdrian Prantl     m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
8021fd13f9SEnrico Granata     SetValueDidChange(m_parent->GetValueDidChange());
8121fd13f9SEnrico Granata     return true;
8221fd13f9SEnrico Granata   }
8321fd13f9SEnrico Granata 
8421fd13f9SEnrico Granata   // The dynamic value failed to get an error, pass the error along
8521fd13f9SEnrico Granata   if (m_error.Success() && m_parent->GetError().Fail())
8621fd13f9SEnrico Granata     m_error = m_parent->GetError();
8721fd13f9SEnrico Granata   SetValueIsValid(false);
8821fd13f9SEnrico Granata   return false;
8921fd13f9SEnrico Granata }
9021fd13f9SEnrico Granata 
IsInScope()91b9c1b51eSKate Stone bool ValueObjectCast::IsInScope() { return m_parent->IsInScope(); }
92