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/API/SBStream.h"
11 #include "lldb/API/SBValue.h"
12 #include "lldb/Core/ValueObjectList.h"
13 #include "lldb/Utility/Instrumentation.h"
14 
15 #include <vector>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 class ValueListImpl {
21 public:
22   ValueListImpl() = default;
23 
24   ValueListImpl(const ValueListImpl &rhs) = default;
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_INSTRUMENT_VA(this); }
71 
72 SBValueList::SBValueList(const SBValueList &rhs) {
73   LLDB_INSTRUMENT_VA(this, 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_INSTRUMENT_VA(this);
88   return this->operator bool();
89 }
90 SBValueList::operator bool() const {
91   LLDB_INSTRUMENT_VA(this);
92 
93   return (m_opaque_up != nullptr);
94 }
95 
96 void SBValueList::Clear() {
97   LLDB_INSTRUMENT_VA(this);
98 
99   m_opaque_up.reset();
100 }
101 
102 const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
103   LLDB_INSTRUMENT_VA(this, rhs);
104 
105   if (this != &rhs) {
106     if (rhs.IsValid())
107       m_opaque_up = std::make_unique<ValueListImpl>(*rhs);
108     else
109       m_opaque_up.reset();
110   }
111   return *this;
112 }
113 
114 ValueListImpl *SBValueList::operator->() { return m_opaque_up.get(); }
115 
116 ValueListImpl &SBValueList::operator*() { return *m_opaque_up; }
117 
118 const ValueListImpl *SBValueList::operator->() const {
119   return m_opaque_up.get();
120 }
121 
122 const ValueListImpl &SBValueList::operator*() const { return *m_opaque_up; }
123 
124 void SBValueList::Append(const SBValue &val_obj) {
125   LLDB_INSTRUMENT_VA(this, val_obj);
126 
127   CreateIfNeeded();
128   m_opaque_up->Append(val_obj);
129 }
130 
131 void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) {
132   if (val_obj_sp) {
133     CreateIfNeeded();
134     m_opaque_up->Append(SBValue(val_obj_sp));
135   }
136 }
137 
138 void SBValueList::Append(const lldb::SBValueList &value_list) {
139   LLDB_INSTRUMENT_VA(this, value_list);
140 
141   if (value_list.IsValid()) {
142     CreateIfNeeded();
143     m_opaque_up->Append(*value_list);
144   }
145 }
146 
147 SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
148   LLDB_INSTRUMENT_VA(this, idx);
149 
150   SBValue sb_value;
151   if (m_opaque_up)
152     sb_value = m_opaque_up->GetValueAtIndex(idx);
153 
154   return sb_value;
155 }
156 
157 uint32_t SBValueList::GetSize() const {
158   LLDB_INSTRUMENT_VA(this);
159 
160   uint32_t size = 0;
161   if (m_opaque_up)
162     size = m_opaque_up->GetSize();
163 
164   return size;
165 }
166 
167 void SBValueList::CreateIfNeeded() {
168   if (m_opaque_up == nullptr)
169     m_opaque_up = std::make_unique<ValueListImpl>();
170 }
171 
172 SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
173   LLDB_INSTRUMENT_VA(this, uid);
174 
175   SBValue sb_value;
176   if (m_opaque_up)
177     sb_value = m_opaque_up->FindValueByUID(uid);
178   return sb_value;
179 }
180 
181 SBValue SBValueList::GetFirstValueByName(const char *name) const {
182   LLDB_INSTRUMENT_VA(this, name);
183 
184   SBValue sb_value;
185   if (m_opaque_up)
186     sb_value = m_opaque_up->GetFirstValueByName(name);
187   return sb_value;
188 }
189 
190 void *SBValueList::opaque_ptr() { return m_opaque_up.get(); }
191 
192 ValueListImpl &SBValueList::ref() {
193   CreateIfNeeded();
194   return *m_opaque_up;
195 }
196