1 //===-- SBValueList.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 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBValueList.h"
10 #include "lldb/Utility/ReproducerInstrumentation.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/API/SBValue.h"
13 #include "lldb/Core/ValueObjectList.h"
14 
15 #include <vector>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 class ValueListImpl {
21 public:
22   ValueListImpl() {}
23 
24   ValueListImpl(const ValueListImpl &rhs) : m_values(rhs.m_values) {}
25 
26   ValueListImpl &operator=(const ValueListImpl &rhs) {
27     if (this == &rhs)
28       return *this;
29     m_values = rhs.m_values;
30     return *this;
31   }
32 
33   uint32_t GetSize() { return m_values.size(); }
34 
35   void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); }
36 
37   void Append(const ValueListImpl &list) {
38     for (auto val : list.m_values)
39       Append(val);
40   }
41 
42   lldb::SBValue GetValueAtIndex(uint32_t index) {
43     if (index >= GetSize())
44       return lldb::SBValue();
45     return m_values[index];
46   }
47 
48   lldb::SBValue FindValueByUID(lldb::user_id_t uid) {
49     for (auto val : m_values) {
50       if (val.IsValid() && val.GetID() == uid)
51         return val;
52     }
53     return lldb::SBValue();
54   }
55 
56   lldb::SBValue GetFirstValueByName(const char *name) const {
57     if (name) {
58       for (auto val : m_values) {
59         if (val.IsValid() && val.GetName() && strcmp(name, val.GetName()) == 0)
60           return val;
61       }
62     }
63     return lldb::SBValue();
64   }
65 
66 private:
67   std::vector<lldb::SBValue> m_values;
68 };
69 
70 SBValueList::SBValueList() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBValueList); }
71 
72 SBValueList::SBValueList(const SBValueList &rhs) {
73   LLDB_RECORD_CONSTRUCTOR(SBValueList, (const lldb::SBValueList &), rhs);
74 
75   if (rhs.IsValid())
76     m_opaque_up = std::make_unique<ValueListImpl>(*rhs);
77 }
78 
79 SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) {
80   if (lldb_object_ptr)
81     m_opaque_up = std::make_unique<ValueListImpl>(*lldb_object_ptr);
82 }
83 
84 SBValueList::~SBValueList() = default;
85 
86 bool SBValueList::IsValid() const {
87   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBValueList, IsValid);
88   return this->operator bool();
89 }
90 SBValueList::operator bool() const {
91   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBValueList, operator bool);
92 
93   return (m_opaque_up != nullptr);
94 }
95 
96 void SBValueList::Clear() {
97   LLDB_RECORD_METHOD_NO_ARGS(void, SBValueList, Clear);
98 
99   m_opaque_up.reset();
100 }
101 
102 const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
103   LLDB_RECORD_METHOD(const lldb::SBValueList &,
104                      SBValueList, operator=,(const lldb::SBValueList &), rhs);
105 
106   if (this != &rhs) {
107     if (rhs.IsValid())
108       m_opaque_up = std::make_unique<ValueListImpl>(*rhs);
109     else
110       m_opaque_up.reset();
111   }
112   return *this;
113 }
114 
115 ValueListImpl *SBValueList::operator->() { return m_opaque_up.get(); }
116 
117 ValueListImpl &SBValueList::operator*() { return *m_opaque_up; }
118 
119 const ValueListImpl *SBValueList::operator->() const {
120   return m_opaque_up.get();
121 }
122 
123 const ValueListImpl &SBValueList::operator*() const { return *m_opaque_up; }
124 
125 void SBValueList::Append(const SBValue &val_obj) {
126   LLDB_RECORD_METHOD(void, SBValueList, Append, (const lldb::SBValue &),
127                      val_obj);
128 
129   CreateIfNeeded();
130   m_opaque_up->Append(val_obj);
131 }
132 
133 void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) {
134   if (val_obj_sp) {
135     CreateIfNeeded();
136     m_opaque_up->Append(SBValue(val_obj_sp));
137   }
138 }
139 
140 void SBValueList::Append(const lldb::SBValueList &value_list) {
141   LLDB_RECORD_METHOD(void, SBValueList, Append, (const lldb::SBValueList &),
142                      value_list);
143 
144   if (value_list.IsValid()) {
145     CreateIfNeeded();
146     m_opaque_up->Append(*value_list);
147   }
148 }
149 
150 SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
151   LLDB_RECORD_METHOD_CONST(lldb::SBValue, SBValueList, GetValueAtIndex,
152                            (uint32_t), idx);
153 
154 
155   SBValue sb_value;
156   if (m_opaque_up)
157     sb_value = m_opaque_up->GetValueAtIndex(idx);
158 
159   return sb_value;
160 }
161 
162 uint32_t SBValueList::GetSize() const {
163   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBValueList, GetSize);
164 
165   uint32_t size = 0;
166   if (m_opaque_up)
167     size = m_opaque_up->GetSize();
168 
169   return size;
170 }
171 
172 void SBValueList::CreateIfNeeded() {
173   if (m_opaque_up == nullptr)
174     m_opaque_up = std::make_unique<ValueListImpl>();
175 }
176 
177 SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
178   LLDB_RECORD_METHOD(lldb::SBValue, SBValueList, FindValueObjectByUID,
179                      (lldb::user_id_t), uid);
180 
181   SBValue sb_value;
182   if (m_opaque_up)
183     sb_value = m_opaque_up->FindValueByUID(uid);
184   return sb_value;
185 }
186 
187 SBValue SBValueList::GetFirstValueByName(const char *name) const {
188   LLDB_RECORD_METHOD_CONST(lldb::SBValue, SBValueList, GetFirstValueByName,
189                            (const char *), name);
190 
191   SBValue sb_value;
192   if (m_opaque_up)
193     sb_value = m_opaque_up->GetFirstValueByName(name);
194   return sb_value;
195 }
196 
197 void *SBValueList::opaque_ptr() { return m_opaque_up.get(); }
198 
199 ValueListImpl &SBValueList::ref() {
200   CreateIfNeeded();
201   return *m_opaque_up;
202 }
203