blob: e3adb445cce381157d7089eb2261a1ee7e4ed896 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBValueList.cpp -----------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Chris Lattner30fdc8d2010-06-08 16:52:24 +00009#include "lldb/API/SBValueList.h"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Caroline Ticeceb6b132010-10-26 03:11:13 +000011#include "lldb/API/SBStream.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000012#include "lldb/API/SBValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/ValueObjectList.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000014#include "lldb/Utility/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
Enrico Granata85425d72013-02-07 18:23:56 +000016#include <vector>
17
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018using namespace lldb;
19using namespace lldb_private;
20
Kate Stoneb9c1b512016-09-06 20:57:50 +000021class ValueListImpl {
Enrico Granata19f0e8c2013-04-22 22:57:56 +000022public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000023 ValueListImpl() : m_values() {}
24
25 ValueListImpl(const ValueListImpl &rhs) : m_values(rhs.m_values) {}
26
27 ValueListImpl &operator=(const ValueListImpl &rhs) {
28 if (this == &rhs)
29 return *this;
30 m_values = rhs.m_values;
31 return *this;
32 }
33
34 uint32_t GetSize() { return m_values.size(); }
35
36 void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); }
37
38 void Append(const ValueListImpl &list) {
39 for (auto val : list.m_values)
40 Append(val);
41 }
42
43 lldb::SBValue GetValueAtIndex(uint32_t index) {
44 if (index >= GetSize())
45 return lldb::SBValue();
46 return m_values[index];
47 }
48
49 lldb::SBValue FindValueByUID(lldb::user_id_t uid) {
50 for (auto val : m_values) {
51 if (val.IsValid() && val.GetID() == uid)
52 return val;
Enrico Granata19f0e8c2013-04-22 22:57:56 +000053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 return lldb::SBValue();
55 }
56
57 lldb::SBValue GetFirstValueByName(const char *name) const {
58 if (name) {
59 for (auto val : m_values) {
60 if (val.IsValid() && val.GetName() && strcmp(name, val.GetName()) == 0)
61 return val;
62 }
Enrico Granata19f0e8c2013-04-22 22:57:56 +000063 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 return lldb::SBValue();
65 }
Enrico Granata85425d72013-02-07 18:23:56 +000066
Enrico Granata19f0e8c2013-04-22 22:57:56 +000067private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 std::vector<lldb::SBValue> m_values;
Enrico Granata19f0e8c2013-04-22 22:57:56 +000069};
Enrico Granata85425d72013-02-07 18:23:56 +000070
Jonas Devliegherebaf56642019-03-06 00:06:00 +000071SBValueList::SBValueList() : m_opaque_up() {
72 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBValueList);
73}
Kate Stoneb9c1b512016-09-06 20:57:50 +000074
Jonas Devlieghered5b44032019-02-13 06:25:41 +000075SBValueList::SBValueList(const SBValueList &rhs) : m_opaque_up() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000076 LLDB_RECORD_CONSTRUCTOR(SBValueList, (const lldb::SBValueList &), rhs);
77
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
79
80 if (rhs.IsValid())
Jonas Devlieghered5b44032019-02-13 06:25:41 +000081 m_opaque_up.reset(new ValueListImpl(*rhs));
Kate Stoneb9c1b512016-09-06 20:57:50 +000082
83 if (log) {
84 log->Printf(
85 "SBValueList::SBValueList (rhs.ap=%p) => this.ap = %p",
Jonas Devlieghered5b44032019-02-13 06:25:41 +000086 static_cast<void *>(rhs.IsValid() ? rhs.m_opaque_up.get() : NULL),
87 static_cast<void *>(m_opaque_up.get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089}
90
Jonas Devlieghered5b44032019-02-13 06:25:41 +000091SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) : m_opaque_up() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 if (lldb_object_ptr)
Jonas Devlieghered5b44032019-02-13 06:25:41 +000095 m_opaque_up.reset(new ValueListImpl(*lldb_object_ptr));
Kate Stoneb9c1b512016-09-06 20:57:50 +000096
97 if (log) {
98 log->Printf("SBValueList::SBValueList (lldb_object_ptr=%p) => this.ap = %p",
99 static_cast<const void *>(lldb_object_ptr),
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000100 static_cast<void *>(m_opaque_up.get()));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 }
102}
103
104SBValueList::~SBValueList() {}
105
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000106bool SBValueList::IsValid() const {
107 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBValueList, IsValid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000109 return (m_opaque_up != NULL);
110}
111
112void SBValueList::Clear() {
113 LLDB_RECORD_METHOD_NO_ARGS(void, SBValueList, Clear);
114
115 m_opaque_up.reset();
116}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117
118const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000119 LLDB_RECORD_METHOD(const lldb::SBValueList &,
120 SBValueList, operator=,(const lldb::SBValueList &), rhs);
121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 if (this != &rhs) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 if (rhs.IsValid())
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000124 m_opaque_up.reset(new ValueListImpl(*rhs));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 else
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000126 m_opaque_up.reset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 }
128 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129}
130
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000131ValueListImpl *SBValueList::operator->() { return m_opaque_up.get(); }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000132
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000133ValueListImpl &SBValueList::operator*() { return *m_opaque_up; }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135const ValueListImpl *SBValueList::operator->() const {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000136 return m_opaque_up.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137}
138
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000139const ValueListImpl &SBValueList::operator*() const { return *m_opaque_up; }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140
141void SBValueList::Append(const SBValue &val_obj) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000142 LLDB_RECORD_METHOD(void, SBValueList, Append, (const lldb::SBValue &),
143 val_obj);
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 CreateIfNeeded();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000146 m_opaque_up->Append(val_obj);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) {
150 if (val_obj_sp) {
Greg Claytondea8cb42011-06-29 22:09:02 +0000151 CreateIfNeeded();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000152 m_opaque_up->Append(SBValue(val_obj_sp));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 }
154}
155
156void SBValueList::Append(const lldb::SBValueList &value_list) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000157 LLDB_RECORD_METHOD(void, SBValueList, Append, (const lldb::SBValueList &),
158 value_list);
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 if (value_list.IsValid()) {
161 CreateIfNeeded();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000162 m_opaque_up->Append(*value_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 }
164}
165
166SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000167 LLDB_RECORD_METHOD_CONST(lldb::SBValue, SBValueList, GetValueAtIndex,
168 (uint32_t), idx);
169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
171
172 // if (log)
173 // log->Printf ("SBValueList::GetValueAtIndex (uint32_t idx) idx = %d",
174 // idx);
175
176 SBValue sb_value;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000177 if (m_opaque_up)
178 sb_value = m_opaque_up->GetValueAtIndex(idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179
180 if (log) {
181 SBStream sstr;
182 sb_value.GetDescription(sstr);
183 log->Printf("SBValueList::GetValueAtIndex (this.ap=%p, idx=%d) => SBValue "
184 "(this.sp = %p, '%s')",
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000185 static_cast<void *>(m_opaque_up.get()), idx,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 static_cast<void *>(sb_value.GetSP().get()), sstr.GetData());
187 }
188
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000189 return LLDB_RECORD_RESULT(sb_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190}
191
192uint32_t SBValueList::GetSize() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000193 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBValueList, GetSize);
194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
196
197 // if (log)
198 // log->Printf ("SBValueList::GetSize ()");
199
200 uint32_t size = 0;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000201 if (m_opaque_up)
202 size = m_opaque_up->GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203
204 if (log)
205 log->Printf("SBValueList::GetSize (this.ap=%p) => %d",
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000206 static_cast<void *>(m_opaque_up.get()), size);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207
208 return size;
209}
210
211void SBValueList::CreateIfNeeded() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000212 if (m_opaque_up == NULL)
213 m_opaque_up.reset(new ValueListImpl());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214}
215
216SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000217 LLDB_RECORD_METHOD(lldb::SBValue, SBValueList, FindValueObjectByUID,
218 (lldb::user_id_t), uid);
219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 SBValue sb_value;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000221 if (m_opaque_up)
222 sb_value = m_opaque_up->FindValueByUID(uid);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000223 return LLDB_RECORD_RESULT(sb_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224}
225
226SBValue SBValueList::GetFirstValueByName(const char *name) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000227 LLDB_RECORD_METHOD_CONST(lldb::SBValue, SBValueList, GetFirstValueByName,
228 (const char *), name);
229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 SBValue sb_value;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000231 if (m_opaque_up)
232 sb_value = m_opaque_up->GetFirstValueByName(name);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000233 return LLDB_RECORD_RESULT(sb_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234}
235
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000236void *SBValueList::opaque_ptr() { return m_opaque_up.get(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237
238ValueListImpl &SBValueList::ref() {
239 CreateIfNeeded();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000240 return *m_opaque_up;
Greg Claytondea8cb42011-06-29 22:09:02 +0000241}