xref: /src/contrib/llvm-project/lldb/source/API/SBValueList.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1cfca06d7SDimitry Andric //===-- SBValueList.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/API/SBValueList.h"
10e3b55780SDimitry Andric #include "lldb/API/SBError.h"
11f034231aSEd Maste #include "lldb/API/SBStream.h"
1214f1b3e8SDimitry Andric #include "lldb/API/SBValue.h"
13f034231aSEd Maste #include "lldb/Core/ValueObjectList.h"
146f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
15e3b55780SDimitry Andric #include "lldb/Utility/Status.h"
16f034231aSEd Maste #include <vector>
17f034231aSEd Maste 
18f034231aSEd Maste using namespace lldb;
19f034231aSEd Maste using namespace lldb_private;
20f034231aSEd Maste 
2114f1b3e8SDimitry Andric class ValueListImpl {
22f034231aSEd Maste public:
23145449b1SDimitry Andric   ValueListImpl() = default;
24f034231aSEd Maste 
25145449b1SDimitry Andric   ValueListImpl(const ValueListImpl &rhs) = default;
26f034231aSEd Maste 
operator =(const ValueListImpl & rhs)2714f1b3e8SDimitry Andric   ValueListImpl &operator=(const ValueListImpl &rhs) {
28f034231aSEd Maste     if (this == &rhs)
29f034231aSEd Maste       return *this;
30f034231aSEd Maste     m_values = rhs.m_values;
31e3b55780SDimitry Andric     m_error = rhs.m_error;
32f034231aSEd Maste     return *this;
33e81d9d49SDimitry Andric   }
34f034231aSEd Maste 
GetSize()3514f1b3e8SDimitry Andric   uint32_t GetSize() { return m_values.size(); }
36f034231aSEd Maste 
Append(const lldb::SBValue & sb_value)3714f1b3e8SDimitry Andric   void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); }
38f034231aSEd Maste 
Append(const ValueListImpl & list)3914f1b3e8SDimitry Andric   void Append(const ValueListImpl &list) {
40f034231aSEd Maste     for (auto val : list.m_values)
41f034231aSEd Maste       Append(val);
42f034231aSEd Maste   }
43f034231aSEd Maste 
GetValueAtIndex(uint32_t index)4414f1b3e8SDimitry Andric   lldb::SBValue GetValueAtIndex(uint32_t index) {
45f034231aSEd Maste     if (index >= GetSize())
46f034231aSEd Maste       return lldb::SBValue();
47f034231aSEd Maste     return m_values[index];
48f034231aSEd Maste   }
49f034231aSEd Maste 
FindValueByUID(lldb::user_id_t uid)5014f1b3e8SDimitry Andric   lldb::SBValue FindValueByUID(lldb::user_id_t uid) {
5114f1b3e8SDimitry Andric     for (auto val : m_values) {
52f034231aSEd Maste       if (val.IsValid() && val.GetID() == uid)
53f034231aSEd Maste         return val;
54f034231aSEd Maste     }
55f034231aSEd Maste     return lldb::SBValue();
56f034231aSEd Maste   }
57f034231aSEd Maste 
GetFirstValueByName(const char * name) const5814f1b3e8SDimitry Andric   lldb::SBValue GetFirstValueByName(const char *name) const {
5914f1b3e8SDimitry Andric     if (name) {
6014f1b3e8SDimitry Andric       for (auto val : m_values) {
6114f1b3e8SDimitry Andric         if (val.IsValid() && val.GetName() && strcmp(name, val.GetName()) == 0)
62205afe67SEd Maste           return val;
63205afe67SEd Maste       }
64205afe67SEd Maste     }
65205afe67SEd Maste     return lldb::SBValue();
66205afe67SEd Maste   }
67205afe67SEd Maste 
GetError() const68e3b55780SDimitry Andric   const Status &GetError() const { return m_error; }
69e3b55780SDimitry Andric 
SetError(const Status & error)70e3b55780SDimitry Andric   void SetError(const Status &error) { m_error = error; }
71e3b55780SDimitry Andric 
72f034231aSEd Maste private:
73f034231aSEd Maste   std::vector<lldb::SBValue> m_values;
74e3b55780SDimitry Andric   Status m_error;
75f034231aSEd Maste };
76f034231aSEd Maste 
SBValueList()776f8fc217SDimitry Andric SBValueList::SBValueList() { LLDB_INSTRUMENT_VA(this); }
78f034231aSEd Maste 
SBValueList(const SBValueList & rhs)796f8fc217SDimitry Andric SBValueList::SBValueList(const SBValueList &rhs) {
806f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
81f034231aSEd Maste 
82f034231aSEd Maste   if (rhs.IsValid())
83cfca06d7SDimitry Andric     m_opaque_up = std::make_unique<ValueListImpl>(*rhs);
84f034231aSEd Maste }
85f034231aSEd Maste 
SBValueList(const ValueListImpl * lldb_object_ptr)866f8fc217SDimitry Andric SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) {
87f034231aSEd Maste   if (lldb_object_ptr)
88cfca06d7SDimitry Andric     m_opaque_up = std::make_unique<ValueListImpl>(*lldb_object_ptr);
89f034231aSEd Maste }
90f034231aSEd Maste 
91cfca06d7SDimitry Andric SBValueList::~SBValueList() = default;
92f034231aSEd Maste 
IsValid() const935f29bb8aSDimitry Andric bool SBValueList::IsValid() const {
946f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
955f29bb8aSDimitry Andric   return this->operator bool();
965f29bb8aSDimitry Andric }
operator bool() const975f29bb8aSDimitry Andric SBValueList::operator bool() const {
986f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
99f034231aSEd Maste 
1005f29bb8aSDimitry Andric   return (m_opaque_up != nullptr);
1015f29bb8aSDimitry Andric }
1025f29bb8aSDimitry Andric 
Clear()1035f29bb8aSDimitry Andric void SBValueList::Clear() {
1046f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1055f29bb8aSDimitry Andric 
1065f29bb8aSDimitry Andric   m_opaque_up.reset();
1075f29bb8aSDimitry Andric }
108f034231aSEd Maste 
operator =(const SBValueList & rhs)10914f1b3e8SDimitry Andric const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
1106f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
1115f29bb8aSDimitry Andric 
11214f1b3e8SDimitry Andric   if (this != &rhs) {
113f034231aSEd Maste     if (rhs.IsValid())
114cfca06d7SDimitry Andric       m_opaque_up = std::make_unique<ValueListImpl>(*rhs);
115f034231aSEd Maste     else
1165f29bb8aSDimitry Andric       m_opaque_up.reset();
117f034231aSEd Maste   }
1186f8fc217SDimitry Andric   return *this;
119f034231aSEd Maste }
120f034231aSEd Maste 
operator ->()1215f29bb8aSDimitry Andric ValueListImpl *SBValueList::operator->() { return m_opaque_up.get(); }
12214f1b3e8SDimitry Andric 
operator *()1235f29bb8aSDimitry Andric ValueListImpl &SBValueList::operator*() { return *m_opaque_up; }
12414f1b3e8SDimitry Andric 
operator ->() const12514f1b3e8SDimitry Andric const ValueListImpl *SBValueList::operator->() const {
1265f29bb8aSDimitry Andric   return m_opaque_up.get();
127f034231aSEd Maste }
128f034231aSEd Maste 
operator *() const1295f29bb8aSDimitry Andric const ValueListImpl &SBValueList::operator*() const { return *m_opaque_up; }
130f034231aSEd Maste 
Append(const SBValue & val_obj)13114f1b3e8SDimitry Andric void SBValueList::Append(const SBValue &val_obj) {
1326f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, val_obj);
1335f29bb8aSDimitry Andric 
134f034231aSEd Maste   CreateIfNeeded();
1355f29bb8aSDimitry Andric   m_opaque_up->Append(val_obj);
136f034231aSEd Maste }
137f034231aSEd Maste 
Append(lldb::ValueObjectSP & val_obj_sp)13814f1b3e8SDimitry Andric void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) {
13914f1b3e8SDimitry Andric   if (val_obj_sp) {
140f034231aSEd Maste     CreateIfNeeded();
1415f29bb8aSDimitry Andric     m_opaque_up->Append(SBValue(val_obj_sp));
142f034231aSEd Maste   }
143f034231aSEd Maste }
144f034231aSEd Maste 
Append(const lldb::SBValueList & value_list)14514f1b3e8SDimitry Andric void SBValueList::Append(const lldb::SBValueList &value_list) {
1466f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, value_list);
1475f29bb8aSDimitry Andric 
14814f1b3e8SDimitry Andric   if (value_list.IsValid()) {
149f034231aSEd Maste     CreateIfNeeded();
1505f29bb8aSDimitry Andric     m_opaque_up->Append(*value_list);
151f034231aSEd Maste   }
152f034231aSEd Maste }
153f034231aSEd Maste 
GetValueAtIndex(uint32_t idx) const15414f1b3e8SDimitry Andric SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
1556f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, idx);
156f034231aSEd Maste 
157f034231aSEd Maste   SBValue sb_value;
1585f29bb8aSDimitry Andric   if (m_opaque_up)
1595f29bb8aSDimitry Andric     sb_value = m_opaque_up->GetValueAtIndex(idx);
160f034231aSEd Maste 
1616f8fc217SDimitry Andric   return sb_value;
162f034231aSEd Maste }
163f034231aSEd Maste 
GetSize() const16414f1b3e8SDimitry Andric uint32_t SBValueList::GetSize() const {
1656f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
166f034231aSEd Maste 
167f034231aSEd Maste   uint32_t size = 0;
1685f29bb8aSDimitry Andric   if (m_opaque_up)
1695f29bb8aSDimitry Andric     size = m_opaque_up->GetSize();
170f034231aSEd Maste 
171f034231aSEd Maste   return size;
172f034231aSEd Maste }
173f034231aSEd Maste 
CreateIfNeeded()17414f1b3e8SDimitry Andric void SBValueList::CreateIfNeeded() {
1755f29bb8aSDimitry Andric   if (m_opaque_up == nullptr)
176cfca06d7SDimitry Andric     m_opaque_up = std::make_unique<ValueListImpl>();
177f034231aSEd Maste }
178f034231aSEd Maste 
FindValueObjectByUID(lldb::user_id_t uid)17914f1b3e8SDimitry Andric SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
1806f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, uid);
1815f29bb8aSDimitry Andric 
182f034231aSEd Maste   SBValue sb_value;
1835f29bb8aSDimitry Andric   if (m_opaque_up)
1845f29bb8aSDimitry Andric     sb_value = m_opaque_up->FindValueByUID(uid);
1856f8fc217SDimitry Andric   return sb_value;
186f034231aSEd Maste }
187f034231aSEd Maste 
GetFirstValueByName(const char * name) const18814f1b3e8SDimitry Andric SBValue SBValueList::GetFirstValueByName(const char *name) const {
1896f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, name);
1905f29bb8aSDimitry Andric 
191205afe67SEd Maste   SBValue sb_value;
1925f29bb8aSDimitry Andric   if (m_opaque_up)
1935f29bb8aSDimitry Andric     sb_value = m_opaque_up->GetFirstValueByName(name);
1946f8fc217SDimitry Andric   return sb_value;
195205afe67SEd Maste }
196205afe67SEd Maste 
opaque_ptr()1975f29bb8aSDimitry Andric void *SBValueList::opaque_ptr() { return m_opaque_up.get(); }
198f034231aSEd Maste 
ref()19914f1b3e8SDimitry Andric ValueListImpl &SBValueList::ref() {
200f034231aSEd Maste   CreateIfNeeded();
2015f29bb8aSDimitry Andric   return *m_opaque_up;
2025f29bb8aSDimitry Andric }
203e3b55780SDimitry Andric 
GetError()204e3b55780SDimitry Andric lldb::SBError SBValueList::GetError() {
205e3b55780SDimitry Andric   LLDB_INSTRUMENT_VA(this);
206e3b55780SDimitry Andric   SBError sb_error;
207e3b55780SDimitry Andric   if (m_opaque_up)
208e3b55780SDimitry Andric     sb_error.SetError(m_opaque_up->GetError());
209e3b55780SDimitry Andric   return sb_error;
210e3b55780SDimitry Andric }
211e3b55780SDimitry Andric 
SetError(const lldb_private::Status & status)212e3b55780SDimitry Andric void SBValueList::SetError(const lldb_private::Status &status) {
213e3b55780SDimitry Andric   ref().SetError(status);
214e3b55780SDimitry Andric }
215